1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/bio.h>
8 #include <linux/file.h>
9 #include <linux/fs.h>
10 #include <linux/fsnotify.h>
11 #include <linux/pagemap.h>
12 #include <linux/highmem.h>
13 #include <linux/time.h>
14 #include <linux/string.h>
15 #include <linux/backing-dev.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/writeback.h>
19 #include <linux/compat.h>
20 #include <linux/security.h>
21 #include <linux/xattr.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/blkdev.h>
25 #include <linux/uuid.h>
26 #include <linux/btrfs.h>
27 #include <linux/uaccess.h>
28 #include <linux/iversion.h>
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "export.h"
32 #include "transaction.h"
33 #include "btrfs_inode.h"
34 #include "print-tree.h"
35 #include "volumes.h"
36 #include "locking.h"
37 #include "inode-map.h"
38 #include "backref.h"
39 #include "rcu-string.h"
40 #include "send.h"
41 #include "dev-replace.h"
42 #include "props.h"
43 #include "sysfs.h"
44 #include "qgroup.h"
45 #include "tree-log.h"
46 #include "compression.h"
47 #include "space-info.h"
48 #include "delalloc-space.h"
49 #include "block-group.h"
50
51 #ifdef CONFIG_64BIT
52 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
53 * structures are incorrect, as the timespec structure from userspace
54 * is 4 bytes too small. We define these alternatives here to teach
55 * the kernel about the 32-bit struct packing.
56 */
57 struct btrfs_ioctl_timespec_32 {
58 __u64 sec;
59 __u32 nsec;
60 } __attribute__ ((__packed__));
61
62 struct btrfs_ioctl_received_subvol_args_32 {
63 char uuid[BTRFS_UUID_SIZE]; /* in */
64 __u64 stransid; /* in */
65 __u64 rtransid; /* out */
66 struct btrfs_ioctl_timespec_32 stime; /* in */
67 struct btrfs_ioctl_timespec_32 rtime; /* out */
68 __u64 flags; /* in */
69 __u64 reserved[16]; /* in */
70 } __attribute__ ((__packed__));
71
72 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
73 struct btrfs_ioctl_received_subvol_args_32)
74 #endif
75
76 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
77 struct btrfs_ioctl_send_args_32 {
78 __s64 send_fd; /* in */
79 __u64 clone_sources_count; /* in */
80 compat_uptr_t clone_sources; /* in */
81 __u64 parent_root; /* in */
82 __u64 flags; /* in */
83 __u64 reserved[4]; /* in */
84 } __attribute__ ((__packed__));
85
86 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
87 struct btrfs_ioctl_send_args_32)
88 #endif
89
90 /* Mask out flags that are inappropriate for the given type of inode. */
btrfs_mask_fsflags_for_type(struct inode * inode,unsigned int flags)91 static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
92 unsigned int flags)
93 {
94 if (S_ISDIR(inode->i_mode))
95 return flags;
96 else if (S_ISREG(inode->i_mode))
97 return flags & ~FS_DIRSYNC_FL;
98 else
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
100 }
101
102 /*
103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
104 * ioctl.
105 */
btrfs_inode_flags_to_fsflags(unsigned int flags)106 static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
107 {
108 unsigned int iflags = 0;
109
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
124
125 if (flags & BTRFS_INODE_NOCOMPRESS)
126 iflags |= FS_NOCOMP_FL;
127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
129
130 return iflags;
131 }
132
133 /*
134 * Update inode->i_flags based on the btrfs internal flags.
135 */
btrfs_sync_inode_flags_to_i_flags(struct inode * inode)136 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
137 {
138 struct btrfs_inode *binode = BTRFS_I(inode);
139 unsigned int new_fl = 0;
140
141 if (binode->flags & BTRFS_INODE_SYNC)
142 new_fl |= S_SYNC;
143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
144 new_fl |= S_IMMUTABLE;
145 if (binode->flags & BTRFS_INODE_APPEND)
146 new_fl |= S_APPEND;
147 if (binode->flags & BTRFS_INODE_NOATIME)
148 new_fl |= S_NOATIME;
149 if (binode->flags & BTRFS_INODE_DIRSYNC)
150 new_fl |= S_DIRSYNC;
151
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
154 new_fl);
155 }
156
btrfs_ioctl_getflags(struct file * file,void __user * arg)157 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158 {
159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165 }
166
167 /*
168 * Check if @flags are a supported and valid set of FS_*_FL flags and that
169 * the old and new flags are not conflicting
170 */
check_fsflags(unsigned int old_flags,unsigned int flags)171 static int check_fsflags(unsigned int old_flags, unsigned int flags)
172 {
173 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
174 FS_NOATIME_FL | FS_NODUMP_FL | \
175 FS_SYNC_FL | FS_DIRSYNC_FL | \
176 FS_NOCOMP_FL | FS_COMPR_FL |
177 FS_NOCOW_FL))
178 return -EOPNOTSUPP;
179
180 /* COMPR and NOCOMP on new/old are valid */
181 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
182 return -EINVAL;
183
184 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
185 return -EINVAL;
186
187 /* NOCOW and compression options are mutually exclusive */
188 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
189 return -EINVAL;
190 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
191 return -EINVAL;
192
193 return 0;
194 }
195
btrfs_ioctl_setflags(struct file * file,void __user * arg)196 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
197 {
198 struct inode *inode = file_inode(file);
199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
200 struct btrfs_inode *binode = BTRFS_I(inode);
201 struct btrfs_root *root = binode->root;
202 struct btrfs_trans_handle *trans;
203 unsigned int fsflags, old_fsflags;
204 int ret;
205 const char *comp = NULL;
206 u32 binode_flags;
207
208 if (!inode_owner_or_capable(inode))
209 return -EPERM;
210
211 if (btrfs_root_readonly(root))
212 return -EROFS;
213
214 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
215 return -EFAULT;
216
217 ret = mnt_want_write_file(file);
218 if (ret)
219 return ret;
220
221 inode_lock(inode);
222 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
223 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
224
225 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
226 if (ret)
227 goto out_unlock;
228
229 ret = check_fsflags(old_fsflags, fsflags);
230 if (ret)
231 goto out_unlock;
232
233 binode_flags = binode->flags;
234 if (fsflags & FS_SYNC_FL)
235 binode_flags |= BTRFS_INODE_SYNC;
236 else
237 binode_flags &= ~BTRFS_INODE_SYNC;
238 if (fsflags & FS_IMMUTABLE_FL)
239 binode_flags |= BTRFS_INODE_IMMUTABLE;
240 else
241 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
242 if (fsflags & FS_APPEND_FL)
243 binode_flags |= BTRFS_INODE_APPEND;
244 else
245 binode_flags &= ~BTRFS_INODE_APPEND;
246 if (fsflags & FS_NODUMP_FL)
247 binode_flags |= BTRFS_INODE_NODUMP;
248 else
249 binode_flags &= ~BTRFS_INODE_NODUMP;
250 if (fsflags & FS_NOATIME_FL)
251 binode_flags |= BTRFS_INODE_NOATIME;
252 else
253 binode_flags &= ~BTRFS_INODE_NOATIME;
254 if (fsflags & FS_DIRSYNC_FL)
255 binode_flags |= BTRFS_INODE_DIRSYNC;
256 else
257 binode_flags &= ~BTRFS_INODE_DIRSYNC;
258 if (fsflags & FS_NOCOW_FL) {
259 if (S_ISREG(inode->i_mode)) {
260 /*
261 * It's safe to turn csums off here, no extents exist.
262 * Otherwise we want the flag to reflect the real COW
263 * status of the file and will not set it.
264 */
265 if (inode->i_size == 0)
266 binode_flags |= BTRFS_INODE_NODATACOW |
267 BTRFS_INODE_NODATASUM;
268 } else {
269 binode_flags |= BTRFS_INODE_NODATACOW;
270 }
271 } else {
272 /*
273 * Revert back under same assumptions as above
274 */
275 if (S_ISREG(inode->i_mode)) {
276 if (inode->i_size == 0)
277 binode_flags &= ~(BTRFS_INODE_NODATACOW |
278 BTRFS_INODE_NODATASUM);
279 } else {
280 binode_flags &= ~BTRFS_INODE_NODATACOW;
281 }
282 }
283
284 /*
285 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
286 * flag may be changed automatically if compression code won't make
287 * things smaller.
288 */
289 if (fsflags & FS_NOCOMP_FL) {
290 binode_flags &= ~BTRFS_INODE_COMPRESS;
291 binode_flags |= BTRFS_INODE_NOCOMPRESS;
292 } else if (fsflags & FS_COMPR_FL) {
293
294 if (IS_SWAPFILE(inode)) {
295 ret = -ETXTBSY;
296 goto out_unlock;
297 }
298
299 binode_flags |= BTRFS_INODE_COMPRESS;
300 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
301
302 comp = btrfs_compress_type2str(fs_info->compress_type);
303 if (!comp || comp[0] == 0)
304 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
305 } else {
306 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
307 }
308
309 /*
310 * 1 for inode item
311 * 2 for properties
312 */
313 trans = btrfs_start_transaction(root, 3);
314 if (IS_ERR(trans)) {
315 ret = PTR_ERR(trans);
316 goto out_unlock;
317 }
318
319 if (comp) {
320 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
321 strlen(comp), 0);
322 if (ret) {
323 btrfs_abort_transaction(trans, ret);
324 goto out_end_trans;
325 }
326 } else {
327 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
328 0, 0);
329 if (ret && ret != -ENODATA) {
330 btrfs_abort_transaction(trans, ret);
331 goto out_end_trans;
332 }
333 }
334
335 binode->flags = binode_flags;
336 btrfs_sync_inode_flags_to_i_flags(inode);
337 inode_inc_iversion(inode);
338 inode->i_ctime = current_time(inode);
339 ret = btrfs_update_inode(trans, root, inode);
340
341 out_end_trans:
342 btrfs_end_transaction(trans);
343 out_unlock:
344 inode_unlock(inode);
345 mnt_drop_write_file(file);
346 return ret;
347 }
348
349 /*
350 * Translate btrfs internal inode flags to xflags as expected by the
351 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
352 * silently dropped.
353 */
btrfs_inode_flags_to_xflags(unsigned int flags)354 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
355 {
356 unsigned int xflags = 0;
357
358 if (flags & BTRFS_INODE_APPEND)
359 xflags |= FS_XFLAG_APPEND;
360 if (flags & BTRFS_INODE_IMMUTABLE)
361 xflags |= FS_XFLAG_IMMUTABLE;
362 if (flags & BTRFS_INODE_NOATIME)
363 xflags |= FS_XFLAG_NOATIME;
364 if (flags & BTRFS_INODE_NODUMP)
365 xflags |= FS_XFLAG_NODUMP;
366 if (flags & BTRFS_INODE_SYNC)
367 xflags |= FS_XFLAG_SYNC;
368
369 return xflags;
370 }
371
372 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
check_xflags(unsigned int flags)373 static int check_xflags(unsigned int flags)
374 {
375 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
376 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
377 return -EOPNOTSUPP;
378 return 0;
379 }
380
btrfs_exclop_start(struct btrfs_fs_info * fs_info,enum btrfs_exclusive_operation type)381 bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
382 enum btrfs_exclusive_operation type)
383 {
384 return !cmpxchg(&fs_info->exclusive_operation, BTRFS_EXCLOP_NONE, type);
385 }
386
btrfs_exclop_finish(struct btrfs_fs_info * fs_info)387 void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
388 {
389 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
390 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
391 }
392
393 /*
394 * Set the xflags from the internal inode flags. The remaining items of fsxattr
395 * are zeroed.
396 */
btrfs_ioctl_fsgetxattr(struct file * file,void __user * arg)397 static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
398 {
399 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
400 struct fsxattr fa;
401
402 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
403 if (copy_to_user(arg, &fa, sizeof(fa)))
404 return -EFAULT;
405
406 return 0;
407 }
408
btrfs_ioctl_fssetxattr(struct file * file,void __user * arg)409 static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
410 {
411 struct inode *inode = file_inode(file);
412 struct btrfs_inode *binode = BTRFS_I(inode);
413 struct btrfs_root *root = binode->root;
414 struct btrfs_trans_handle *trans;
415 struct fsxattr fa, old_fa;
416 unsigned old_flags;
417 unsigned old_i_flags;
418 int ret = 0;
419
420 if (!inode_owner_or_capable(inode))
421 return -EPERM;
422
423 if (btrfs_root_readonly(root))
424 return -EROFS;
425
426 if (copy_from_user(&fa, arg, sizeof(fa)))
427 return -EFAULT;
428
429 ret = check_xflags(fa.fsx_xflags);
430 if (ret)
431 return ret;
432
433 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
434 return -EOPNOTSUPP;
435
436 ret = mnt_want_write_file(file);
437 if (ret)
438 return ret;
439
440 inode_lock(inode);
441
442 old_flags = binode->flags;
443 old_i_flags = inode->i_flags;
444
445 simple_fill_fsxattr(&old_fa,
446 btrfs_inode_flags_to_xflags(binode->flags));
447 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
448 if (ret)
449 goto out_unlock;
450
451 if (fa.fsx_xflags & FS_XFLAG_SYNC)
452 binode->flags |= BTRFS_INODE_SYNC;
453 else
454 binode->flags &= ~BTRFS_INODE_SYNC;
455 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
456 binode->flags |= BTRFS_INODE_IMMUTABLE;
457 else
458 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
459 if (fa.fsx_xflags & FS_XFLAG_APPEND)
460 binode->flags |= BTRFS_INODE_APPEND;
461 else
462 binode->flags &= ~BTRFS_INODE_APPEND;
463 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
464 binode->flags |= BTRFS_INODE_NODUMP;
465 else
466 binode->flags &= ~BTRFS_INODE_NODUMP;
467 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
468 binode->flags |= BTRFS_INODE_NOATIME;
469 else
470 binode->flags &= ~BTRFS_INODE_NOATIME;
471
472 /* 1 item for the inode */
473 trans = btrfs_start_transaction(root, 1);
474 if (IS_ERR(trans)) {
475 ret = PTR_ERR(trans);
476 goto out_unlock;
477 }
478
479 btrfs_sync_inode_flags_to_i_flags(inode);
480 inode_inc_iversion(inode);
481 inode->i_ctime = current_time(inode);
482 ret = btrfs_update_inode(trans, root, inode);
483
484 btrfs_end_transaction(trans);
485
486 out_unlock:
487 if (ret) {
488 binode->flags = old_flags;
489 inode->i_flags = old_i_flags;
490 }
491
492 inode_unlock(inode);
493 mnt_drop_write_file(file);
494
495 return ret;
496 }
497
btrfs_ioctl_getversion(struct file * file,int __user * arg)498 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
499 {
500 struct inode *inode = file_inode(file);
501
502 return put_user(inode->i_generation, arg);
503 }
504
btrfs_ioctl_fitrim(struct btrfs_fs_info * fs_info,void __user * arg)505 static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
506 void __user *arg)
507 {
508 struct btrfs_device *device;
509 struct request_queue *q;
510 struct fstrim_range range;
511 u64 minlen = ULLONG_MAX;
512 u64 num_devices = 0;
513 int ret;
514
515 if (!capable(CAP_SYS_ADMIN))
516 return -EPERM;
517
518 /*
519 * If the fs is mounted with nologreplay, which requires it to be
520 * mounted in RO mode as well, we can not allow discard on free space
521 * inside block groups, because log trees refer to extents that are not
522 * pinned in a block group's free space cache (pinning the extents is
523 * precisely the first phase of replaying a log tree).
524 */
525 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
526 return -EROFS;
527
528 rcu_read_lock();
529 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
530 dev_list) {
531 if (!device->bdev)
532 continue;
533 q = bdev_get_queue(device->bdev);
534 if (blk_queue_discard(q)) {
535 num_devices++;
536 minlen = min_t(u64, q->limits.discard_granularity,
537 minlen);
538 }
539 }
540 rcu_read_unlock();
541
542 if (!num_devices)
543 return -EOPNOTSUPP;
544 if (copy_from_user(&range, arg, sizeof(range)))
545 return -EFAULT;
546
547 /*
548 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
549 * block group is in the logical address space, which can be any
550 * sectorsize aligned bytenr in the range [0, U64_MAX].
551 */
552 if (range.len < fs_info->sb->s_blocksize)
553 return -EINVAL;
554
555 range.minlen = max(range.minlen, minlen);
556 ret = btrfs_trim_fs(fs_info, &range);
557 if (ret < 0)
558 return ret;
559
560 if (copy_to_user(arg, &range, sizeof(range)))
561 return -EFAULT;
562
563 return 0;
564 }
565
btrfs_is_empty_uuid(u8 * uuid)566 int __pure btrfs_is_empty_uuid(u8 *uuid)
567 {
568 int i;
569
570 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
571 if (uuid[i])
572 return 0;
573 }
574 return 1;
575 }
576
create_subvol(struct inode * dir,struct dentry * dentry,const char * name,int namelen,struct btrfs_qgroup_inherit * inherit)577 static noinline int create_subvol(struct inode *dir,
578 struct dentry *dentry,
579 const char *name, int namelen,
580 struct btrfs_qgroup_inherit *inherit)
581 {
582 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
583 struct btrfs_trans_handle *trans;
584 struct btrfs_key key;
585 struct btrfs_root_item *root_item;
586 struct btrfs_inode_item *inode_item;
587 struct extent_buffer *leaf;
588 struct btrfs_root *root = BTRFS_I(dir)->root;
589 struct btrfs_root *new_root;
590 struct btrfs_block_rsv block_rsv;
591 struct timespec64 cur_time = current_time(dir);
592 struct inode *inode;
593 int ret;
594 int err;
595 dev_t anon_dev = 0;
596 u64 objectid;
597 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
598 u64 index = 0;
599
600 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
601 if (!root_item)
602 return -ENOMEM;
603
604 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
605 if (ret)
606 goto fail_free;
607
608 ret = get_anon_bdev(&anon_dev);
609 if (ret < 0)
610 goto fail_free;
611
612 /*
613 * Don't create subvolume whose level is not zero. Or qgroup will be
614 * screwed up since it assumes subvolume qgroup's level to be 0.
615 */
616 if (btrfs_qgroup_level(objectid)) {
617 ret = -ENOSPC;
618 goto fail_free;
619 }
620
621 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
622 /*
623 * The same as the snapshot creation, please see the comment
624 * of create_snapshot().
625 */
626 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
627 if (ret)
628 goto fail_free;
629
630 trans = btrfs_start_transaction(root, 0);
631 if (IS_ERR(trans)) {
632 ret = PTR_ERR(trans);
633 btrfs_subvolume_release_metadata(root, &block_rsv);
634 goto fail_free;
635 }
636 trans->block_rsv = &block_rsv;
637 trans->bytes_reserved = block_rsv.size;
638
639 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
640 if (ret)
641 goto fail;
642
643 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
644 BTRFS_NESTING_NORMAL);
645 if (IS_ERR(leaf)) {
646 ret = PTR_ERR(leaf);
647 goto fail;
648 }
649
650 btrfs_mark_buffer_dirty(leaf);
651
652 inode_item = &root_item->inode;
653 btrfs_set_stack_inode_generation(inode_item, 1);
654 btrfs_set_stack_inode_size(inode_item, 3);
655 btrfs_set_stack_inode_nlink(inode_item, 1);
656 btrfs_set_stack_inode_nbytes(inode_item,
657 fs_info->nodesize);
658 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
659
660 btrfs_set_root_flags(root_item, 0);
661 btrfs_set_root_limit(root_item, 0);
662 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
663
664 btrfs_set_root_bytenr(root_item, leaf->start);
665 btrfs_set_root_generation(root_item, trans->transid);
666 btrfs_set_root_level(root_item, 0);
667 btrfs_set_root_refs(root_item, 1);
668 btrfs_set_root_used(root_item, leaf->len);
669 btrfs_set_root_last_snapshot(root_item, 0);
670
671 btrfs_set_root_generation_v2(root_item,
672 btrfs_root_generation(root_item));
673 generate_random_guid(root_item->uuid);
674 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
675 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
676 root_item->ctime = root_item->otime;
677 btrfs_set_root_ctransid(root_item, trans->transid);
678 btrfs_set_root_otransid(root_item, trans->transid);
679
680 btrfs_tree_unlock(leaf);
681
682 btrfs_set_root_dirid(root_item, new_dirid);
683
684 key.objectid = objectid;
685 key.offset = 0;
686 key.type = BTRFS_ROOT_ITEM_KEY;
687 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
688 root_item);
689 if (ret) {
690 /*
691 * Since we don't abort the transaction in this case, free the
692 * tree block so that we don't leak space and leave the
693 * filesystem in an inconsistent state (an extent item in the
694 * extent tree without backreferences). Also no need to have
695 * the tree block locked since it is not in any tree at this
696 * point, so no other task can find it and use it.
697 */
698 btrfs_free_tree_block(trans, root, leaf, 0, 1);
699 free_extent_buffer(leaf);
700 goto fail;
701 }
702
703 free_extent_buffer(leaf);
704 leaf = NULL;
705
706 key.offset = (u64)-1;
707 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
708 if (IS_ERR(new_root)) {
709 free_anon_bdev(anon_dev);
710 ret = PTR_ERR(new_root);
711 btrfs_abort_transaction(trans, ret);
712 goto fail;
713 }
714 /* Freeing will be done in btrfs_put_root() of new_root */
715 anon_dev = 0;
716
717 btrfs_record_root_in_trans(trans, new_root);
718
719 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
720 btrfs_put_root(new_root);
721 if (ret) {
722 /* We potentially lose an unused inode item here */
723 btrfs_abort_transaction(trans, ret);
724 goto fail;
725 }
726
727 mutex_lock(&new_root->objectid_mutex);
728 new_root->highest_objectid = new_dirid;
729 mutex_unlock(&new_root->objectid_mutex);
730
731 /*
732 * insert the directory item
733 */
734 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
735 if (ret) {
736 btrfs_abort_transaction(trans, ret);
737 goto fail;
738 }
739
740 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
741 BTRFS_FT_DIR, index);
742 if (ret) {
743 btrfs_abort_transaction(trans, ret);
744 goto fail;
745 }
746
747 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
748 ret = btrfs_update_inode(trans, root, dir);
749 if (ret) {
750 btrfs_abort_transaction(trans, ret);
751 goto fail;
752 }
753
754 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
755 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
756 if (ret) {
757 btrfs_abort_transaction(trans, ret);
758 goto fail;
759 }
760
761 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
762 BTRFS_UUID_KEY_SUBVOL, objectid);
763 if (ret)
764 btrfs_abort_transaction(trans, ret);
765
766 fail:
767 kfree(root_item);
768 trans->block_rsv = NULL;
769 trans->bytes_reserved = 0;
770 btrfs_subvolume_release_metadata(root, &block_rsv);
771
772 err = btrfs_commit_transaction(trans);
773 if (err && !ret)
774 ret = err;
775
776 if (!ret) {
777 inode = btrfs_lookup_dentry(dir, dentry);
778 if (IS_ERR(inode))
779 return PTR_ERR(inode);
780 d_instantiate(dentry, inode);
781 }
782 return ret;
783
784 fail_free:
785 if (anon_dev)
786 free_anon_bdev(anon_dev);
787 kfree(root_item);
788 return ret;
789 }
790
create_snapshot(struct btrfs_root * root,struct inode * dir,struct dentry * dentry,bool readonly,struct btrfs_qgroup_inherit * inherit)791 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
792 struct dentry *dentry, bool readonly,
793 struct btrfs_qgroup_inherit *inherit)
794 {
795 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
796 struct inode *inode;
797 struct btrfs_pending_snapshot *pending_snapshot;
798 struct btrfs_trans_handle *trans;
799 int ret;
800
801 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
802 return -EINVAL;
803
804 if (atomic_read(&root->nr_swapfiles)) {
805 btrfs_warn(fs_info,
806 "cannot snapshot subvolume with active swapfile");
807 return -ETXTBSY;
808 }
809
810 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
811 if (!pending_snapshot)
812 return -ENOMEM;
813
814 ret = get_anon_bdev(&pending_snapshot->anon_dev);
815 if (ret < 0)
816 goto free_pending;
817 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
818 GFP_KERNEL);
819 pending_snapshot->path = btrfs_alloc_path();
820 if (!pending_snapshot->root_item || !pending_snapshot->path) {
821 ret = -ENOMEM;
822 goto free_pending;
823 }
824
825 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
826 BTRFS_BLOCK_RSV_TEMP);
827 /*
828 * 1 - parent dir inode
829 * 2 - dir entries
830 * 1 - root item
831 * 2 - root ref/backref
832 * 1 - root of snapshot
833 * 1 - UUID item
834 */
835 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
836 &pending_snapshot->block_rsv, 8,
837 false);
838 if (ret)
839 goto free_pending;
840
841 pending_snapshot->dentry = dentry;
842 pending_snapshot->root = root;
843 pending_snapshot->readonly = readonly;
844 pending_snapshot->dir = dir;
845 pending_snapshot->inherit = inherit;
846
847 trans = btrfs_start_transaction(root, 0);
848 if (IS_ERR(trans)) {
849 ret = PTR_ERR(trans);
850 goto fail;
851 }
852
853 spin_lock(&fs_info->trans_lock);
854 list_add(&pending_snapshot->list,
855 &trans->transaction->pending_snapshots);
856 spin_unlock(&fs_info->trans_lock);
857
858 ret = btrfs_commit_transaction(trans);
859 if (ret)
860 goto fail;
861
862 ret = pending_snapshot->error;
863 if (ret)
864 goto fail;
865
866 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
867 if (ret)
868 goto fail;
869
870 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
871 if (IS_ERR(inode)) {
872 ret = PTR_ERR(inode);
873 goto fail;
874 }
875
876 d_instantiate(dentry, inode);
877 ret = 0;
878 pending_snapshot->anon_dev = 0;
879 fail:
880 /* Prevent double freeing of anon_dev */
881 if (ret && pending_snapshot->snap)
882 pending_snapshot->snap->anon_dev = 0;
883 btrfs_put_root(pending_snapshot->snap);
884 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
885 free_pending:
886 if (pending_snapshot->anon_dev)
887 free_anon_bdev(pending_snapshot->anon_dev);
888 kfree(pending_snapshot->root_item);
889 btrfs_free_path(pending_snapshot->path);
890 kfree(pending_snapshot);
891
892 return ret;
893 }
894
895 /* copy of may_delete in fs/namei.c()
896 * Check whether we can remove a link victim from directory dir, check
897 * whether the type of victim is right.
898 * 1. We can't do it if dir is read-only (done in permission())
899 * 2. We should have write and exec permissions on dir
900 * 3. We can't remove anything from append-only dir
901 * 4. We can't do anything with immutable dir (done in permission())
902 * 5. If the sticky bit on dir is set we should either
903 * a. be owner of dir, or
904 * b. be owner of victim, or
905 * c. have CAP_FOWNER capability
906 * 6. If the victim is append-only or immutable we can't do anything with
907 * links pointing to it.
908 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
909 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
910 * 9. We can't remove a root or mountpoint.
911 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
912 * nfs_async_unlink().
913 */
914
btrfs_may_delete(struct inode * dir,struct dentry * victim,int isdir)915 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
916 {
917 int error;
918
919 if (d_really_is_negative(victim))
920 return -ENOENT;
921
922 BUG_ON(d_inode(victim->d_parent) != dir);
923 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
924
925 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
926 if (error)
927 return error;
928 if (IS_APPEND(dir))
929 return -EPERM;
930 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
931 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
932 return -EPERM;
933 if (isdir) {
934 if (!d_is_dir(victim))
935 return -ENOTDIR;
936 if (IS_ROOT(victim))
937 return -EBUSY;
938 } else if (d_is_dir(victim))
939 return -EISDIR;
940 if (IS_DEADDIR(dir))
941 return -ENOENT;
942 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
943 return -EBUSY;
944 return 0;
945 }
946
947 /* copy of may_create in fs/namei.c() */
btrfs_may_create(struct inode * dir,struct dentry * child)948 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
949 {
950 if (d_really_is_positive(child))
951 return -EEXIST;
952 if (IS_DEADDIR(dir))
953 return -ENOENT;
954 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
955 }
956
957 /*
958 * Create a new subvolume below @parent. This is largely modeled after
959 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
960 * inside this filesystem so it's quite a bit simpler.
961 */
btrfs_mksubvol(const struct path * parent,const char * name,int namelen,struct btrfs_root * snap_src,bool readonly,struct btrfs_qgroup_inherit * inherit)962 static noinline int btrfs_mksubvol(const struct path *parent,
963 const char *name, int namelen,
964 struct btrfs_root *snap_src,
965 bool readonly,
966 struct btrfs_qgroup_inherit *inherit)
967 {
968 struct inode *dir = d_inode(parent->dentry);
969 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
970 struct dentry *dentry;
971 int error;
972
973 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
974 if (error == -EINTR)
975 return error;
976
977 dentry = lookup_one_len(name, parent->dentry, namelen);
978 error = PTR_ERR(dentry);
979 if (IS_ERR(dentry))
980 goto out_unlock;
981
982 error = btrfs_may_create(dir, dentry);
983 if (error)
984 goto out_dput;
985
986 /*
987 * even if this name doesn't exist, we may get hash collisions.
988 * check for them now when we can safely fail
989 */
990 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
991 dir->i_ino, name,
992 namelen);
993 if (error)
994 goto out_dput;
995
996 down_read(&fs_info->subvol_sem);
997
998 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
999 goto out_up_read;
1000
1001 if (snap_src)
1002 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
1003 else
1004 error = create_subvol(dir, dentry, name, namelen, inherit);
1005
1006 if (!error)
1007 fsnotify_mkdir(dir, dentry);
1008 out_up_read:
1009 up_read(&fs_info->subvol_sem);
1010 out_dput:
1011 dput(dentry);
1012 out_unlock:
1013 inode_unlock(dir);
1014 return error;
1015 }
1016
btrfs_mksnapshot(const struct path * parent,const char * name,int namelen,struct btrfs_root * root,bool readonly,struct btrfs_qgroup_inherit * inherit)1017 static noinline int btrfs_mksnapshot(const struct path *parent,
1018 const char *name, int namelen,
1019 struct btrfs_root *root,
1020 bool readonly,
1021 struct btrfs_qgroup_inherit *inherit)
1022 {
1023 int ret;
1024 bool snapshot_force_cow = false;
1025
1026 /*
1027 * Force new buffered writes to reserve space even when NOCOW is
1028 * possible. This is to avoid later writeback (running dealloc) to
1029 * fallback to COW mode and unexpectedly fail with ENOSPC.
1030 */
1031 btrfs_drew_read_lock(&root->snapshot_lock);
1032
1033 ret = btrfs_start_delalloc_snapshot(root);
1034 if (ret)
1035 goto out;
1036
1037 /*
1038 * All previous writes have started writeback in NOCOW mode, so now
1039 * we force future writes to fallback to COW mode during snapshot
1040 * creation.
1041 */
1042 atomic_inc(&root->snapshot_force_cow);
1043 snapshot_force_cow = true;
1044
1045 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
1046
1047 ret = btrfs_mksubvol(parent, name, namelen,
1048 root, readonly, inherit);
1049 out:
1050 if (snapshot_force_cow)
1051 atomic_dec(&root->snapshot_force_cow);
1052 btrfs_drew_read_unlock(&root->snapshot_lock);
1053 return ret;
1054 }
1055
1056 /*
1057 * When we're defragging a range, we don't want to kick it off again
1058 * if it is really just waiting for delalloc to send it down.
1059 * If we find a nice big extent or delalloc range for the bytes in the
1060 * file you want to defrag, we return 0 to let you know to skip this
1061 * part of the file
1062 */
check_defrag_in_cache(struct inode * inode,u64 offset,u32 thresh)1063 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1064 {
1065 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1066 struct extent_map *em = NULL;
1067 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1068 u64 end;
1069
1070 read_lock(&em_tree->lock);
1071 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1072 read_unlock(&em_tree->lock);
1073
1074 if (em) {
1075 end = extent_map_end(em);
1076 free_extent_map(em);
1077 if (end - offset > thresh)
1078 return 0;
1079 }
1080 /* if we already have a nice delalloc here, just stop */
1081 thresh /= 2;
1082 end = count_range_bits(io_tree, &offset, offset + thresh,
1083 thresh, EXTENT_DELALLOC, 1);
1084 if (end >= thresh)
1085 return 0;
1086 return 1;
1087 }
1088
1089 /*
1090 * helper function to walk through a file and find extents
1091 * newer than a specific transid, and smaller than thresh.
1092 *
1093 * This is used by the defragging code to find new and small
1094 * extents
1095 */
find_new_extents(struct btrfs_root * root,struct inode * inode,u64 newer_than,u64 * off,u32 thresh)1096 static int find_new_extents(struct btrfs_root *root,
1097 struct inode *inode, u64 newer_than,
1098 u64 *off, u32 thresh)
1099 {
1100 struct btrfs_path *path;
1101 struct btrfs_key min_key;
1102 struct extent_buffer *leaf;
1103 struct btrfs_file_extent_item *extent;
1104 int type;
1105 int ret;
1106 u64 ino = btrfs_ino(BTRFS_I(inode));
1107
1108 path = btrfs_alloc_path();
1109 if (!path)
1110 return -ENOMEM;
1111
1112 min_key.objectid = ino;
1113 min_key.type = BTRFS_EXTENT_DATA_KEY;
1114 min_key.offset = *off;
1115
1116 while (1) {
1117 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1118 if (ret != 0)
1119 goto none;
1120 process_slot:
1121 if (min_key.objectid != ino)
1122 goto none;
1123 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1124 goto none;
1125
1126 leaf = path->nodes[0];
1127 extent = btrfs_item_ptr(leaf, path->slots[0],
1128 struct btrfs_file_extent_item);
1129
1130 type = btrfs_file_extent_type(leaf, extent);
1131 if (type == BTRFS_FILE_EXTENT_REG &&
1132 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1133 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1134 *off = min_key.offset;
1135 btrfs_free_path(path);
1136 return 0;
1137 }
1138
1139 path->slots[0]++;
1140 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1141 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1142 goto process_slot;
1143 }
1144
1145 if (min_key.offset == (u64)-1)
1146 goto none;
1147
1148 min_key.offset++;
1149 btrfs_release_path(path);
1150 }
1151 none:
1152 btrfs_free_path(path);
1153 return -ENOENT;
1154 }
1155
defrag_lookup_extent(struct inode * inode,u64 start)1156 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1157 {
1158 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1159 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1160 struct extent_map *em;
1161 u64 len = PAGE_SIZE;
1162
1163 /*
1164 * hopefully we have this extent in the tree already, try without
1165 * the full extent lock
1166 */
1167 read_lock(&em_tree->lock);
1168 em = lookup_extent_mapping(em_tree, start, len);
1169 read_unlock(&em_tree->lock);
1170
1171 if (!em) {
1172 struct extent_state *cached = NULL;
1173 u64 end = start + len - 1;
1174
1175 /* get the big lock and read metadata off disk */
1176 lock_extent_bits(io_tree, start, end, &cached);
1177 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
1178 unlock_extent_cached(io_tree, start, end, &cached);
1179
1180 if (IS_ERR(em))
1181 return NULL;
1182 }
1183
1184 return em;
1185 }
1186
defrag_check_next_extent(struct inode * inode,struct extent_map * em)1187 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1188 {
1189 struct extent_map *next;
1190 bool ret = true;
1191
1192 /* this is the last extent */
1193 if (em->start + em->len >= i_size_read(inode))
1194 return false;
1195
1196 next = defrag_lookup_extent(inode, em->start + em->len);
1197 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1198 ret = false;
1199 else if ((em->block_start + em->block_len == next->block_start) &&
1200 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1201 ret = false;
1202
1203 free_extent_map(next);
1204 return ret;
1205 }
1206
should_defrag_range(struct inode * inode,u64 start,u32 thresh,u64 * last_len,u64 * skip,u64 * defrag_end,int compress)1207 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1208 u64 *last_len, u64 *skip, u64 *defrag_end,
1209 int compress)
1210 {
1211 struct extent_map *em;
1212 int ret = 1;
1213 bool next_mergeable = true;
1214 bool prev_mergeable = true;
1215
1216 /*
1217 * make sure that once we start defragging an extent, we keep on
1218 * defragging it
1219 */
1220 if (start < *defrag_end)
1221 return 1;
1222
1223 *skip = 0;
1224
1225 em = defrag_lookup_extent(inode, start);
1226 if (!em)
1227 return 0;
1228
1229 /* this will cover holes, and inline extents */
1230 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1231 ret = 0;
1232 goto out;
1233 }
1234
1235 if (!*defrag_end)
1236 prev_mergeable = false;
1237
1238 next_mergeable = defrag_check_next_extent(inode, em);
1239 /*
1240 * we hit a real extent, if it is big or the next extent is not a
1241 * real extent, don't bother defragging it
1242 */
1243 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1244 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1245 ret = 0;
1246 out:
1247 /*
1248 * last_len ends up being a counter of how many bytes we've defragged.
1249 * every time we choose not to defrag an extent, we reset *last_len
1250 * so that the next tiny extent will force a defrag.
1251 *
1252 * The end result of this is that tiny extents before a single big
1253 * extent will force at least part of that big extent to be defragged.
1254 */
1255 if (ret) {
1256 *defrag_end = extent_map_end(em);
1257 } else {
1258 *last_len = 0;
1259 *skip = extent_map_end(em);
1260 *defrag_end = 0;
1261 }
1262
1263 free_extent_map(em);
1264 return ret;
1265 }
1266
1267 /*
1268 * it doesn't do much good to defrag one or two pages
1269 * at a time. This pulls in a nice chunk of pages
1270 * to COW and defrag.
1271 *
1272 * It also makes sure the delalloc code has enough
1273 * dirty data to avoid making new small extents as part
1274 * of the defrag
1275 *
1276 * It's a good idea to start RA on this range
1277 * before calling this.
1278 */
cluster_pages_for_defrag(struct inode * inode,struct page ** pages,unsigned long start_index,unsigned long num_pages)1279 static int cluster_pages_for_defrag(struct inode *inode,
1280 struct page **pages,
1281 unsigned long start_index,
1282 unsigned long num_pages)
1283 {
1284 unsigned long file_end;
1285 u64 isize = i_size_read(inode);
1286 u64 page_start;
1287 u64 page_end;
1288 u64 page_cnt;
1289 u64 start = (u64)start_index << PAGE_SHIFT;
1290 u64 search_start;
1291 int ret;
1292 int i;
1293 int i_done;
1294 struct btrfs_ordered_extent *ordered;
1295 struct extent_state *cached_state = NULL;
1296 struct extent_io_tree *tree;
1297 struct extent_changeset *data_reserved = NULL;
1298 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1299
1300 file_end = (isize - 1) >> PAGE_SHIFT;
1301 if (!isize || start_index > file_end)
1302 return 0;
1303
1304 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1305
1306 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
1307 start, page_cnt << PAGE_SHIFT);
1308 if (ret)
1309 return ret;
1310 i_done = 0;
1311 tree = &BTRFS_I(inode)->io_tree;
1312
1313 /* step one, lock all the pages */
1314 for (i = 0; i < page_cnt; i++) {
1315 struct page *page;
1316 again:
1317 page = find_or_create_page(inode->i_mapping,
1318 start_index + i, mask);
1319 if (!page)
1320 break;
1321
1322 page_start = page_offset(page);
1323 page_end = page_start + PAGE_SIZE - 1;
1324 while (1) {
1325 lock_extent_bits(tree, page_start, page_end,
1326 &cached_state);
1327 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode),
1328 page_start);
1329 unlock_extent_cached(tree, page_start, page_end,
1330 &cached_state);
1331 if (!ordered)
1332 break;
1333
1334 unlock_page(page);
1335 btrfs_start_ordered_extent(ordered, 1);
1336 btrfs_put_ordered_extent(ordered);
1337 lock_page(page);
1338 /*
1339 * we unlocked the page above, so we need check if
1340 * it was released or not.
1341 */
1342 if (page->mapping != inode->i_mapping) {
1343 unlock_page(page);
1344 put_page(page);
1345 goto again;
1346 }
1347 }
1348
1349 if (!PageUptodate(page)) {
1350 btrfs_readpage(NULL, page);
1351 lock_page(page);
1352 if (!PageUptodate(page)) {
1353 unlock_page(page);
1354 put_page(page);
1355 ret = -EIO;
1356 break;
1357 }
1358 }
1359
1360 if (page->mapping != inode->i_mapping) {
1361 unlock_page(page);
1362 put_page(page);
1363 goto again;
1364 }
1365
1366 pages[i] = page;
1367 i_done++;
1368 }
1369 if (!i_done || ret)
1370 goto out;
1371
1372 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1373 goto out;
1374
1375 /*
1376 * so now we have a nice long stream of locked
1377 * and up to date pages, lets wait on them
1378 */
1379 for (i = 0; i < i_done; i++)
1380 wait_on_page_writeback(pages[i]);
1381
1382 page_start = page_offset(pages[0]);
1383 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1384
1385 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1386 page_start, page_end - 1, &cached_state);
1387
1388 /*
1389 * When defragmenting we skip ranges that have holes or inline extents,
1390 * (check should_defrag_range()), to avoid unnecessary IO and wasting
1391 * space. At btrfs_defrag_file(), we check if a range should be defragged
1392 * before locking the inode and then, if it should, we trigger a sync
1393 * page cache readahead - we lock the inode only after that to avoid
1394 * blocking for too long other tasks that possibly want to operate on
1395 * other file ranges. But before we were able to get the inode lock,
1396 * some other task may have punched a hole in the range, or we may have
1397 * now an inline extent, in which case we should not defrag. So check
1398 * for that here, where we have the inode and the range locked, and bail
1399 * out if that happened.
1400 */
1401 search_start = page_start;
1402 while (search_start < page_end) {
1403 struct extent_map *em;
1404
1405 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, search_start,
1406 page_end - search_start);
1407 if (IS_ERR(em)) {
1408 ret = PTR_ERR(em);
1409 goto out_unlock_range;
1410 }
1411 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1412 free_extent_map(em);
1413 /* Ok, 0 means we did not defrag anything */
1414 ret = 0;
1415 goto out_unlock_range;
1416 }
1417 search_start = extent_map_end(em);
1418 free_extent_map(em);
1419 }
1420
1421 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1422 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1423 EXTENT_DEFRAG, 0, 0, &cached_state);
1424
1425 if (i_done != page_cnt) {
1426 spin_lock(&BTRFS_I(inode)->lock);
1427 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1428 spin_unlock(&BTRFS_I(inode)->lock);
1429 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1430 start, (page_cnt - i_done) << PAGE_SHIFT, true);
1431 }
1432
1433
1434 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1435 &cached_state);
1436
1437 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1438 page_start, page_end - 1, &cached_state);
1439
1440 for (i = 0; i < i_done; i++) {
1441 clear_page_dirty_for_io(pages[i]);
1442 ClearPageChecked(pages[i]);
1443 set_page_extent_mapped(pages[i]);
1444 set_page_dirty(pages[i]);
1445 unlock_page(pages[i]);
1446 put_page(pages[i]);
1447 }
1448 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1449 extent_changeset_free(data_reserved);
1450 return i_done;
1451
1452 out_unlock_range:
1453 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1454 page_start, page_end - 1, &cached_state);
1455 out:
1456 for (i = 0; i < i_done; i++) {
1457 unlock_page(pages[i]);
1458 put_page(pages[i]);
1459 }
1460 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1461 start, page_cnt << PAGE_SHIFT, true);
1462 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1463 extent_changeset_free(data_reserved);
1464 return ret;
1465
1466 }
1467
btrfs_defrag_file(struct inode * inode,struct file * file,struct btrfs_ioctl_defrag_range_args * range,u64 newer_than,unsigned long max_to_defrag)1468 int btrfs_defrag_file(struct inode *inode, struct file *file,
1469 struct btrfs_ioctl_defrag_range_args *range,
1470 u64 newer_than, unsigned long max_to_defrag)
1471 {
1472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1473 struct btrfs_root *root = BTRFS_I(inode)->root;
1474 struct file_ra_state *ra = NULL;
1475 unsigned long last_index;
1476 u64 isize = i_size_read(inode);
1477 u64 last_len = 0;
1478 u64 skip = 0;
1479 u64 defrag_end = 0;
1480 u64 newer_off = range->start;
1481 unsigned long i;
1482 unsigned long ra_index = 0;
1483 int ret;
1484 int defrag_count = 0;
1485 int compress_type = BTRFS_COMPRESS_ZLIB;
1486 u32 extent_thresh = range->extent_thresh;
1487 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1488 unsigned long cluster = max_cluster;
1489 u64 new_align = ~((u64)SZ_128K - 1);
1490 struct page **pages = NULL;
1491 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1492
1493 if (isize == 0)
1494 return 0;
1495
1496 if (range->start >= isize)
1497 return -EINVAL;
1498
1499 if (do_compress) {
1500 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
1501 return -EINVAL;
1502 if (range->compress_type)
1503 compress_type = range->compress_type;
1504 }
1505
1506 if (extent_thresh == 0)
1507 extent_thresh = SZ_256K;
1508
1509 /*
1510 * If we were not given a file, allocate a readahead context. As
1511 * readahead is just an optimization, defrag will work without it so
1512 * we don't error out.
1513 */
1514 if (!file) {
1515 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1516 if (ra)
1517 file_ra_state_init(ra, inode->i_mapping);
1518 } else {
1519 ra = &file->f_ra;
1520 }
1521
1522 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1523 if (!pages) {
1524 ret = -ENOMEM;
1525 goto out_ra;
1526 }
1527
1528 /* find the last page to defrag */
1529 if (range->start + range->len > range->start) {
1530 last_index = min_t(u64, isize - 1,
1531 range->start + range->len - 1) >> PAGE_SHIFT;
1532 } else {
1533 last_index = (isize - 1) >> PAGE_SHIFT;
1534 }
1535
1536 if (newer_than) {
1537 ret = find_new_extents(root, inode, newer_than,
1538 &newer_off, SZ_64K);
1539 if (!ret) {
1540 range->start = newer_off;
1541 /*
1542 * we always align our defrag to help keep
1543 * the extents in the file evenly spaced
1544 */
1545 i = (newer_off & new_align) >> PAGE_SHIFT;
1546 } else
1547 goto out_ra;
1548 } else {
1549 i = range->start >> PAGE_SHIFT;
1550 }
1551 if (!max_to_defrag)
1552 max_to_defrag = last_index - i + 1;
1553
1554 /*
1555 * make writeback starts from i, so the defrag range can be
1556 * written sequentially.
1557 */
1558 if (i < inode->i_mapping->writeback_index)
1559 inode->i_mapping->writeback_index = i;
1560
1561 while (i <= last_index && defrag_count < max_to_defrag &&
1562 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1563 /*
1564 * make sure we stop running if someone unmounts
1565 * the FS
1566 */
1567 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1568 break;
1569
1570 if (btrfs_defrag_cancelled(fs_info)) {
1571 btrfs_debug(fs_info, "defrag_file cancelled");
1572 ret = -EAGAIN;
1573 break;
1574 }
1575
1576 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1577 extent_thresh, &last_len, &skip,
1578 &defrag_end, do_compress)){
1579 unsigned long next;
1580 /*
1581 * the should_defrag function tells us how much to skip
1582 * bump our counter by the suggested amount
1583 */
1584 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1585 i = max(i + 1, next);
1586 continue;
1587 }
1588
1589 if (!newer_than) {
1590 cluster = (PAGE_ALIGN(defrag_end) >>
1591 PAGE_SHIFT) - i;
1592 cluster = min(cluster, max_cluster);
1593 } else {
1594 cluster = max_cluster;
1595 }
1596
1597 if (i + cluster > ra_index) {
1598 ra_index = max(i, ra_index);
1599 if (ra)
1600 page_cache_sync_readahead(inode->i_mapping, ra,
1601 file, ra_index, cluster);
1602 ra_index += cluster;
1603 }
1604
1605 inode_lock(inode);
1606 if (IS_SWAPFILE(inode)) {
1607 ret = -ETXTBSY;
1608 } else {
1609 if (do_compress)
1610 BTRFS_I(inode)->defrag_compress = compress_type;
1611 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1612 }
1613 if (ret < 0) {
1614 inode_unlock(inode);
1615 goto out_ra;
1616 }
1617
1618 defrag_count += ret;
1619 balance_dirty_pages_ratelimited(inode->i_mapping);
1620 inode_unlock(inode);
1621
1622 if (newer_than) {
1623 if (newer_off == (u64)-1)
1624 break;
1625
1626 if (ret > 0)
1627 i += ret;
1628
1629 newer_off = max(newer_off + 1,
1630 (u64)i << PAGE_SHIFT);
1631
1632 ret = find_new_extents(root, inode, newer_than,
1633 &newer_off, SZ_64K);
1634 if (!ret) {
1635 range->start = newer_off;
1636 i = (newer_off & new_align) >> PAGE_SHIFT;
1637 } else {
1638 break;
1639 }
1640 } else {
1641 if (ret > 0) {
1642 i += ret;
1643 last_len += ret << PAGE_SHIFT;
1644 } else {
1645 i++;
1646 last_len = 0;
1647 }
1648 }
1649 }
1650
1651 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1652 filemap_flush(inode->i_mapping);
1653 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1654 &BTRFS_I(inode)->runtime_flags))
1655 filemap_flush(inode->i_mapping);
1656 }
1657
1658 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1659 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1660 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1661 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1662 }
1663
1664 ret = defrag_count;
1665
1666 out_ra:
1667 if (do_compress) {
1668 inode_lock(inode);
1669 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1670 inode_unlock(inode);
1671 }
1672 if (!file)
1673 kfree(ra);
1674 kfree(pages);
1675 return ret;
1676 }
1677
btrfs_ioctl_resize(struct file * file,void __user * arg)1678 static noinline int btrfs_ioctl_resize(struct file *file,
1679 void __user *arg)
1680 {
1681 struct inode *inode = file_inode(file);
1682 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1683 u64 new_size;
1684 u64 old_size;
1685 u64 devid = 1;
1686 struct btrfs_root *root = BTRFS_I(inode)->root;
1687 struct btrfs_ioctl_vol_args *vol_args;
1688 struct btrfs_trans_handle *trans;
1689 struct btrfs_device *device = NULL;
1690 char *sizestr;
1691 char *retptr;
1692 char *devstr = NULL;
1693 int ret = 0;
1694 int mod = 0;
1695
1696 if (!capable(CAP_SYS_ADMIN))
1697 return -EPERM;
1698
1699 ret = mnt_want_write_file(file);
1700 if (ret)
1701 return ret;
1702
1703 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_RESIZE)) {
1704 mnt_drop_write_file(file);
1705 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1706 }
1707
1708 vol_args = memdup_user(arg, sizeof(*vol_args));
1709 if (IS_ERR(vol_args)) {
1710 ret = PTR_ERR(vol_args);
1711 goto out;
1712 }
1713
1714 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1715
1716 sizestr = vol_args->name;
1717 devstr = strchr(sizestr, ':');
1718 if (devstr) {
1719 sizestr = devstr + 1;
1720 *devstr = '\0';
1721 devstr = vol_args->name;
1722 ret = kstrtoull(devstr, 10, &devid);
1723 if (ret)
1724 goto out_free;
1725 if (!devid) {
1726 ret = -EINVAL;
1727 goto out_free;
1728 }
1729 btrfs_info(fs_info, "resizing devid %llu", devid);
1730 }
1731
1732 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
1733 if (!device) {
1734 btrfs_info(fs_info, "resizer unable to find device %llu",
1735 devid);
1736 ret = -ENODEV;
1737 goto out_free;
1738 }
1739
1740 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1741 btrfs_info(fs_info,
1742 "resizer unable to apply on readonly device %llu",
1743 devid);
1744 ret = -EPERM;
1745 goto out_free;
1746 }
1747
1748 if (!strcmp(sizestr, "max"))
1749 new_size = device->bdev->bd_inode->i_size;
1750 else {
1751 if (sizestr[0] == '-') {
1752 mod = -1;
1753 sizestr++;
1754 } else if (sizestr[0] == '+') {
1755 mod = 1;
1756 sizestr++;
1757 }
1758 new_size = memparse(sizestr, &retptr);
1759 if (*retptr != '\0' || new_size == 0) {
1760 ret = -EINVAL;
1761 goto out_free;
1762 }
1763 }
1764
1765 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1766 ret = -EPERM;
1767 goto out_free;
1768 }
1769
1770 old_size = btrfs_device_get_total_bytes(device);
1771
1772 if (mod < 0) {
1773 if (new_size > old_size) {
1774 ret = -EINVAL;
1775 goto out_free;
1776 }
1777 new_size = old_size - new_size;
1778 } else if (mod > 0) {
1779 if (new_size > ULLONG_MAX - old_size) {
1780 ret = -ERANGE;
1781 goto out_free;
1782 }
1783 new_size = old_size + new_size;
1784 }
1785
1786 if (new_size < SZ_256M) {
1787 ret = -EINVAL;
1788 goto out_free;
1789 }
1790 if (new_size > device->bdev->bd_inode->i_size) {
1791 ret = -EFBIG;
1792 goto out_free;
1793 }
1794
1795 new_size = round_down(new_size, fs_info->sectorsize);
1796
1797 if (new_size > old_size) {
1798 trans = btrfs_start_transaction(root, 0);
1799 if (IS_ERR(trans)) {
1800 ret = PTR_ERR(trans);
1801 goto out_free;
1802 }
1803 ret = btrfs_grow_device(trans, device, new_size);
1804 btrfs_commit_transaction(trans);
1805 } else if (new_size < old_size) {
1806 ret = btrfs_shrink_device(device, new_size);
1807 } /* equal, nothing need to do */
1808
1809 if (ret == 0 && new_size != old_size)
1810 btrfs_info_in_rcu(fs_info,
1811 "resize device %s (devid %llu) from %llu to %llu",
1812 rcu_str_deref(device->name), device->devid,
1813 old_size, new_size);
1814 out_free:
1815 kfree(vol_args);
1816 out:
1817 btrfs_exclop_finish(fs_info);
1818 mnt_drop_write_file(file);
1819 return ret;
1820 }
1821
__btrfs_ioctl_snap_create(struct file * file,const char * name,unsigned long fd,int subvol,bool readonly,struct btrfs_qgroup_inherit * inherit)1822 static noinline int __btrfs_ioctl_snap_create(struct file *file,
1823 const char *name, unsigned long fd, int subvol,
1824 bool readonly,
1825 struct btrfs_qgroup_inherit *inherit)
1826 {
1827 int namelen;
1828 int ret = 0;
1829
1830 if (!S_ISDIR(file_inode(file)->i_mode))
1831 return -ENOTDIR;
1832
1833 ret = mnt_want_write_file(file);
1834 if (ret)
1835 goto out;
1836
1837 namelen = strlen(name);
1838 if (strchr(name, '/')) {
1839 ret = -EINVAL;
1840 goto out_drop_write;
1841 }
1842
1843 if (name[0] == '.' &&
1844 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1845 ret = -EEXIST;
1846 goto out_drop_write;
1847 }
1848
1849 if (subvol) {
1850 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1851 NULL, readonly, inherit);
1852 } else {
1853 struct fd src = fdget(fd);
1854 struct inode *src_inode;
1855 if (!src.file) {
1856 ret = -EINVAL;
1857 goto out_drop_write;
1858 }
1859
1860 src_inode = file_inode(src.file);
1861 if (src_inode->i_sb != file_inode(file)->i_sb) {
1862 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1863 "Snapshot src from another FS");
1864 ret = -EXDEV;
1865 } else if (!inode_owner_or_capable(src_inode)) {
1866 /*
1867 * Subvolume creation is not restricted, but snapshots
1868 * are limited to own subvolumes only
1869 */
1870 ret = -EPERM;
1871 } else if (btrfs_ino(BTRFS_I(src_inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1872 /*
1873 * Snapshots must be made with the src_inode referring
1874 * to the subvolume inode, otherwise the permission
1875 * checking above is useless because we may have
1876 * permission on a lower directory but not the subvol
1877 * itself.
1878 */
1879 ret = -EINVAL;
1880 } else {
1881 ret = btrfs_mksnapshot(&file->f_path, name, namelen,
1882 BTRFS_I(src_inode)->root,
1883 readonly, inherit);
1884 }
1885 fdput(src);
1886 }
1887 out_drop_write:
1888 mnt_drop_write_file(file);
1889 out:
1890 return ret;
1891 }
1892
btrfs_ioctl_snap_create(struct file * file,void __user * arg,int subvol)1893 static noinline int btrfs_ioctl_snap_create(struct file *file,
1894 void __user *arg, int subvol)
1895 {
1896 struct btrfs_ioctl_vol_args *vol_args;
1897 int ret;
1898
1899 if (!S_ISDIR(file_inode(file)->i_mode))
1900 return -ENOTDIR;
1901
1902 vol_args = memdup_user(arg, sizeof(*vol_args));
1903 if (IS_ERR(vol_args))
1904 return PTR_ERR(vol_args);
1905 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1906
1907 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1908 subvol, false, NULL);
1909
1910 kfree(vol_args);
1911 return ret;
1912 }
1913
btrfs_ioctl_snap_create_v2(struct file * file,void __user * arg,int subvol)1914 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1915 void __user *arg, int subvol)
1916 {
1917 struct btrfs_ioctl_vol_args_v2 *vol_args;
1918 int ret;
1919 bool readonly = false;
1920 struct btrfs_qgroup_inherit *inherit = NULL;
1921
1922 if (!S_ISDIR(file_inode(file)->i_mode))
1923 return -ENOTDIR;
1924
1925 vol_args = memdup_user(arg, sizeof(*vol_args));
1926 if (IS_ERR(vol_args))
1927 return PTR_ERR(vol_args);
1928 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1929
1930 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
1931 ret = -EOPNOTSUPP;
1932 goto free_args;
1933 }
1934
1935 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1936 readonly = true;
1937 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1938 u64 nums;
1939
1940 if (vol_args->size < sizeof(*inherit) ||
1941 vol_args->size > PAGE_SIZE) {
1942 ret = -EINVAL;
1943 goto free_args;
1944 }
1945 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1946 if (IS_ERR(inherit)) {
1947 ret = PTR_ERR(inherit);
1948 goto free_args;
1949 }
1950
1951 if (inherit->num_qgroups > PAGE_SIZE ||
1952 inherit->num_ref_copies > PAGE_SIZE ||
1953 inherit->num_excl_copies > PAGE_SIZE) {
1954 ret = -EINVAL;
1955 goto free_inherit;
1956 }
1957
1958 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1959 2 * inherit->num_excl_copies;
1960 if (vol_args->size != struct_size(inherit, qgroups, nums)) {
1961 ret = -EINVAL;
1962 goto free_inherit;
1963 }
1964 }
1965
1966 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1967 subvol, readonly, inherit);
1968 if (ret)
1969 goto free_inherit;
1970 free_inherit:
1971 kfree(inherit);
1972 free_args:
1973 kfree(vol_args);
1974 return ret;
1975 }
1976
btrfs_ioctl_subvol_getflags(struct file * file,void __user * arg)1977 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1978 void __user *arg)
1979 {
1980 struct inode *inode = file_inode(file);
1981 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1982 struct btrfs_root *root = BTRFS_I(inode)->root;
1983 int ret = 0;
1984 u64 flags = 0;
1985
1986 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1987 return -EINVAL;
1988
1989 down_read(&fs_info->subvol_sem);
1990 if (btrfs_root_readonly(root))
1991 flags |= BTRFS_SUBVOL_RDONLY;
1992 up_read(&fs_info->subvol_sem);
1993
1994 if (copy_to_user(arg, &flags, sizeof(flags)))
1995 ret = -EFAULT;
1996
1997 return ret;
1998 }
1999
btrfs_ioctl_subvol_setflags(struct file * file,void __user * arg)2000 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
2001 void __user *arg)
2002 {
2003 struct inode *inode = file_inode(file);
2004 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2005 struct btrfs_root *root = BTRFS_I(inode)->root;
2006 struct btrfs_trans_handle *trans;
2007 u64 root_flags;
2008 u64 flags;
2009 int ret = 0;
2010
2011 if (!inode_owner_or_capable(inode))
2012 return -EPERM;
2013
2014 ret = mnt_want_write_file(file);
2015 if (ret)
2016 goto out;
2017
2018 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2019 ret = -EINVAL;
2020 goto out_drop_write;
2021 }
2022
2023 if (copy_from_user(&flags, arg, sizeof(flags))) {
2024 ret = -EFAULT;
2025 goto out_drop_write;
2026 }
2027
2028 if (flags & ~BTRFS_SUBVOL_RDONLY) {
2029 ret = -EOPNOTSUPP;
2030 goto out_drop_write;
2031 }
2032
2033 down_write(&fs_info->subvol_sem);
2034
2035 /* nothing to do */
2036 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
2037 goto out_drop_sem;
2038
2039 root_flags = btrfs_root_flags(&root->root_item);
2040 if (flags & BTRFS_SUBVOL_RDONLY) {
2041 btrfs_set_root_flags(&root->root_item,
2042 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
2043 } else {
2044 /*
2045 * Block RO -> RW transition if this subvolume is involved in
2046 * send
2047 */
2048 spin_lock(&root->root_item_lock);
2049 if (root->send_in_progress == 0) {
2050 btrfs_set_root_flags(&root->root_item,
2051 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
2052 spin_unlock(&root->root_item_lock);
2053 } else {
2054 spin_unlock(&root->root_item_lock);
2055 btrfs_warn(fs_info,
2056 "Attempt to set subvolume %llu read-write during send",
2057 root->root_key.objectid);
2058 ret = -EPERM;
2059 goto out_drop_sem;
2060 }
2061 }
2062
2063 trans = btrfs_start_transaction(root, 1);
2064 if (IS_ERR(trans)) {
2065 ret = PTR_ERR(trans);
2066 goto out_reset;
2067 }
2068
2069 ret = btrfs_update_root(trans, fs_info->tree_root,
2070 &root->root_key, &root->root_item);
2071 if (ret < 0) {
2072 btrfs_end_transaction(trans);
2073 goto out_reset;
2074 }
2075
2076 ret = btrfs_commit_transaction(trans);
2077
2078 out_reset:
2079 if (ret)
2080 btrfs_set_root_flags(&root->root_item, root_flags);
2081 out_drop_sem:
2082 up_write(&fs_info->subvol_sem);
2083 out_drop_write:
2084 mnt_drop_write_file(file);
2085 out:
2086 return ret;
2087 }
2088
key_in_sk(struct btrfs_key * key,struct btrfs_ioctl_search_key * sk)2089 static noinline int key_in_sk(struct btrfs_key *key,
2090 struct btrfs_ioctl_search_key *sk)
2091 {
2092 struct btrfs_key test;
2093 int ret;
2094
2095 test.objectid = sk->min_objectid;
2096 test.type = sk->min_type;
2097 test.offset = sk->min_offset;
2098
2099 ret = btrfs_comp_cpu_keys(key, &test);
2100 if (ret < 0)
2101 return 0;
2102
2103 test.objectid = sk->max_objectid;
2104 test.type = sk->max_type;
2105 test.offset = sk->max_offset;
2106
2107 ret = btrfs_comp_cpu_keys(key, &test);
2108 if (ret > 0)
2109 return 0;
2110 return 1;
2111 }
2112
copy_to_sk(struct btrfs_path * path,struct btrfs_key * key,struct btrfs_ioctl_search_key * sk,u64 * buf_size,char __user * ubuf,unsigned long * sk_offset,int * num_found)2113 static noinline int copy_to_sk(struct btrfs_path *path,
2114 struct btrfs_key *key,
2115 struct btrfs_ioctl_search_key *sk,
2116 u64 *buf_size,
2117 char __user *ubuf,
2118 unsigned long *sk_offset,
2119 int *num_found)
2120 {
2121 u64 found_transid;
2122 struct extent_buffer *leaf;
2123 struct btrfs_ioctl_search_header sh;
2124 struct btrfs_key test;
2125 unsigned long item_off;
2126 unsigned long item_len;
2127 int nritems;
2128 int i;
2129 int slot;
2130 int ret = 0;
2131
2132 leaf = path->nodes[0];
2133 slot = path->slots[0];
2134 nritems = btrfs_header_nritems(leaf);
2135
2136 if (btrfs_header_generation(leaf) > sk->max_transid) {
2137 i = nritems;
2138 goto advance_key;
2139 }
2140 found_transid = btrfs_header_generation(leaf);
2141
2142 for (i = slot; i < nritems; i++) {
2143 item_off = btrfs_item_ptr_offset(leaf, i);
2144 item_len = btrfs_item_size_nr(leaf, i);
2145
2146 btrfs_item_key_to_cpu(leaf, key, i);
2147 if (!key_in_sk(key, sk))
2148 continue;
2149
2150 if (sizeof(sh) + item_len > *buf_size) {
2151 if (*num_found) {
2152 ret = 1;
2153 goto out;
2154 }
2155
2156 /*
2157 * return one empty item back for v1, which does not
2158 * handle -EOVERFLOW
2159 */
2160
2161 *buf_size = sizeof(sh) + item_len;
2162 item_len = 0;
2163 ret = -EOVERFLOW;
2164 }
2165
2166 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2167 ret = 1;
2168 goto out;
2169 }
2170
2171 sh.objectid = key->objectid;
2172 sh.offset = key->offset;
2173 sh.type = key->type;
2174 sh.len = item_len;
2175 sh.transid = found_transid;
2176
2177 /*
2178 * Copy search result header. If we fault then loop again so we
2179 * can fault in the pages and -EFAULT there if there's a
2180 * problem. Otherwise we'll fault and then copy the buffer in
2181 * properly this next time through
2182 */
2183 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2184 ret = 0;
2185 goto out;
2186 }
2187
2188 *sk_offset += sizeof(sh);
2189
2190 if (item_len) {
2191 char __user *up = ubuf + *sk_offset;
2192 /*
2193 * Copy the item, same behavior as above, but reset the
2194 * * sk_offset so we copy the full thing again.
2195 */
2196 if (read_extent_buffer_to_user_nofault(leaf, up,
2197 item_off, item_len)) {
2198 ret = 0;
2199 *sk_offset -= sizeof(sh);
2200 goto out;
2201 }
2202
2203 *sk_offset += item_len;
2204 }
2205 (*num_found)++;
2206
2207 if (ret) /* -EOVERFLOW from above */
2208 goto out;
2209
2210 if (*num_found >= sk->nr_items) {
2211 ret = 1;
2212 goto out;
2213 }
2214 }
2215 advance_key:
2216 ret = 0;
2217 test.objectid = sk->max_objectid;
2218 test.type = sk->max_type;
2219 test.offset = sk->max_offset;
2220 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2221 ret = 1;
2222 else if (key->offset < (u64)-1)
2223 key->offset++;
2224 else if (key->type < (u8)-1) {
2225 key->offset = 0;
2226 key->type++;
2227 } else if (key->objectid < (u64)-1) {
2228 key->offset = 0;
2229 key->type = 0;
2230 key->objectid++;
2231 } else
2232 ret = 1;
2233 out:
2234 /*
2235 * 0: all items from this leaf copied, continue with next
2236 * 1: * more items can be copied, but unused buffer is too small
2237 * * all items were found
2238 * Either way, it will stops the loop which iterates to the next
2239 * leaf
2240 * -EOVERFLOW: item was to large for buffer
2241 * -EFAULT: could not copy extent buffer back to userspace
2242 */
2243 return ret;
2244 }
2245
search_ioctl(struct inode * inode,struct btrfs_ioctl_search_key * sk,u64 * buf_size,char __user * ubuf)2246 static noinline int search_ioctl(struct inode *inode,
2247 struct btrfs_ioctl_search_key *sk,
2248 u64 *buf_size,
2249 char __user *ubuf)
2250 {
2251 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2252 struct btrfs_root *root;
2253 struct btrfs_key key;
2254 struct btrfs_path *path;
2255 int ret;
2256 int num_found = 0;
2257 unsigned long sk_offset = 0;
2258
2259 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2260 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2261 return -EOVERFLOW;
2262 }
2263
2264 path = btrfs_alloc_path();
2265 if (!path)
2266 return -ENOMEM;
2267
2268 if (sk->tree_id == 0) {
2269 /* search the root of the inode that was passed */
2270 root = btrfs_grab_root(BTRFS_I(inode)->root);
2271 } else {
2272 root = btrfs_get_fs_root(info, sk->tree_id, true);
2273 if (IS_ERR(root)) {
2274 btrfs_free_path(path);
2275 return PTR_ERR(root);
2276 }
2277 }
2278
2279 key.objectid = sk->min_objectid;
2280 key.type = sk->min_type;
2281 key.offset = sk->min_offset;
2282
2283 while (1) {
2284 ret = fault_in_pages_writeable(ubuf + sk_offset,
2285 *buf_size - sk_offset);
2286 if (ret)
2287 break;
2288
2289 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2290 if (ret != 0) {
2291 if (ret > 0)
2292 ret = 0;
2293 goto err;
2294 }
2295 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2296 &sk_offset, &num_found);
2297 btrfs_release_path(path);
2298 if (ret)
2299 break;
2300
2301 }
2302 if (ret > 0)
2303 ret = 0;
2304 err:
2305 sk->nr_items = num_found;
2306 btrfs_put_root(root);
2307 btrfs_free_path(path);
2308 return ret;
2309 }
2310
btrfs_ioctl_tree_search(struct file * file,void __user * argp)2311 static noinline int btrfs_ioctl_tree_search(struct file *file,
2312 void __user *argp)
2313 {
2314 struct btrfs_ioctl_search_args __user *uargs;
2315 struct btrfs_ioctl_search_key sk;
2316 struct inode *inode;
2317 int ret;
2318 u64 buf_size;
2319
2320 if (!capable(CAP_SYS_ADMIN))
2321 return -EPERM;
2322
2323 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2324
2325 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2326 return -EFAULT;
2327
2328 buf_size = sizeof(uargs->buf);
2329
2330 inode = file_inode(file);
2331 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2332
2333 /*
2334 * In the origin implementation an overflow is handled by returning a
2335 * search header with a len of zero, so reset ret.
2336 */
2337 if (ret == -EOVERFLOW)
2338 ret = 0;
2339
2340 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2341 ret = -EFAULT;
2342 return ret;
2343 }
2344
btrfs_ioctl_tree_search_v2(struct file * file,void __user * argp)2345 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2346 void __user *argp)
2347 {
2348 struct btrfs_ioctl_search_args_v2 __user *uarg;
2349 struct btrfs_ioctl_search_args_v2 args;
2350 struct inode *inode;
2351 int ret;
2352 u64 buf_size;
2353 const u64 buf_limit = SZ_16M;
2354
2355 if (!capable(CAP_SYS_ADMIN))
2356 return -EPERM;
2357
2358 /* copy search header and buffer size */
2359 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2360 if (copy_from_user(&args, uarg, sizeof(args)))
2361 return -EFAULT;
2362
2363 buf_size = args.buf_size;
2364
2365 /* limit result size to 16MB */
2366 if (buf_size > buf_limit)
2367 buf_size = buf_limit;
2368
2369 inode = file_inode(file);
2370 ret = search_ioctl(inode, &args.key, &buf_size,
2371 (char __user *)(&uarg->buf[0]));
2372 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2373 ret = -EFAULT;
2374 else if (ret == -EOVERFLOW &&
2375 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2376 ret = -EFAULT;
2377
2378 return ret;
2379 }
2380
2381 /*
2382 * Search INODE_REFs to identify path name of 'dirid' directory
2383 * in a 'tree_id' tree. and sets path name to 'name'.
2384 */
btrfs_search_path_in_tree(struct btrfs_fs_info * info,u64 tree_id,u64 dirid,char * name)2385 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2386 u64 tree_id, u64 dirid, char *name)
2387 {
2388 struct btrfs_root *root;
2389 struct btrfs_key key;
2390 char *ptr;
2391 int ret = -1;
2392 int slot;
2393 int len;
2394 int total_len = 0;
2395 struct btrfs_inode_ref *iref;
2396 struct extent_buffer *l;
2397 struct btrfs_path *path;
2398
2399 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2400 name[0]='\0';
2401 return 0;
2402 }
2403
2404 path = btrfs_alloc_path();
2405 if (!path)
2406 return -ENOMEM;
2407
2408 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2409
2410 root = btrfs_get_fs_root(info, tree_id, true);
2411 if (IS_ERR(root)) {
2412 ret = PTR_ERR(root);
2413 root = NULL;
2414 goto out;
2415 }
2416
2417 key.objectid = dirid;
2418 key.type = BTRFS_INODE_REF_KEY;
2419 key.offset = (u64)-1;
2420
2421 while (1) {
2422 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2423 if (ret < 0)
2424 goto out;
2425 else if (ret > 0) {
2426 ret = btrfs_previous_item(root, path, dirid,
2427 BTRFS_INODE_REF_KEY);
2428 if (ret < 0)
2429 goto out;
2430 else if (ret > 0) {
2431 ret = -ENOENT;
2432 goto out;
2433 }
2434 }
2435
2436 l = path->nodes[0];
2437 slot = path->slots[0];
2438 btrfs_item_key_to_cpu(l, &key, slot);
2439
2440 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2441 len = btrfs_inode_ref_name_len(l, iref);
2442 ptr -= len + 1;
2443 total_len += len + 1;
2444 if (ptr < name) {
2445 ret = -ENAMETOOLONG;
2446 goto out;
2447 }
2448
2449 *(ptr + len) = '/';
2450 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2451
2452 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2453 break;
2454
2455 btrfs_release_path(path);
2456 key.objectid = key.offset;
2457 key.offset = (u64)-1;
2458 dirid = key.objectid;
2459 }
2460 memmove(name, ptr, total_len);
2461 name[total_len] = '\0';
2462 ret = 0;
2463 out:
2464 btrfs_put_root(root);
2465 btrfs_free_path(path);
2466 return ret;
2467 }
2468
btrfs_search_path_in_tree_user(struct inode * inode,struct btrfs_ioctl_ino_lookup_user_args * args)2469 static int btrfs_search_path_in_tree_user(struct inode *inode,
2470 struct btrfs_ioctl_ino_lookup_user_args *args)
2471 {
2472 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2473 struct super_block *sb = inode->i_sb;
2474 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2475 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2476 u64 dirid = args->dirid;
2477 unsigned long item_off;
2478 unsigned long item_len;
2479 struct btrfs_inode_ref *iref;
2480 struct btrfs_root_ref *rref;
2481 struct btrfs_root *root = NULL;
2482 struct btrfs_path *path;
2483 struct btrfs_key key, key2;
2484 struct extent_buffer *leaf;
2485 struct inode *temp_inode;
2486 char *ptr;
2487 int slot;
2488 int len;
2489 int total_len = 0;
2490 int ret;
2491
2492 path = btrfs_alloc_path();
2493 if (!path)
2494 return -ENOMEM;
2495
2496 /*
2497 * If the bottom subvolume does not exist directly under upper_limit,
2498 * construct the path in from the bottom up.
2499 */
2500 if (dirid != upper_limit.objectid) {
2501 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2502
2503 root = btrfs_get_fs_root(fs_info, treeid, true);
2504 if (IS_ERR(root)) {
2505 ret = PTR_ERR(root);
2506 goto out;
2507 }
2508
2509 key.objectid = dirid;
2510 key.type = BTRFS_INODE_REF_KEY;
2511 key.offset = (u64)-1;
2512 while (1) {
2513 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2514 if (ret < 0) {
2515 goto out_put;
2516 } else if (ret > 0) {
2517 ret = btrfs_previous_item(root, path, dirid,
2518 BTRFS_INODE_REF_KEY);
2519 if (ret < 0) {
2520 goto out_put;
2521 } else if (ret > 0) {
2522 ret = -ENOENT;
2523 goto out_put;
2524 }
2525 }
2526
2527 leaf = path->nodes[0];
2528 slot = path->slots[0];
2529 btrfs_item_key_to_cpu(leaf, &key, slot);
2530
2531 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2532 len = btrfs_inode_ref_name_len(leaf, iref);
2533 ptr -= len + 1;
2534 total_len += len + 1;
2535 if (ptr < args->path) {
2536 ret = -ENAMETOOLONG;
2537 goto out_put;
2538 }
2539
2540 *(ptr + len) = '/';
2541 read_extent_buffer(leaf, ptr,
2542 (unsigned long)(iref + 1), len);
2543
2544 /* Check the read+exec permission of this directory */
2545 ret = btrfs_previous_item(root, path, dirid,
2546 BTRFS_INODE_ITEM_KEY);
2547 if (ret < 0) {
2548 goto out_put;
2549 } else if (ret > 0) {
2550 ret = -ENOENT;
2551 goto out_put;
2552 }
2553
2554 leaf = path->nodes[0];
2555 slot = path->slots[0];
2556 btrfs_item_key_to_cpu(leaf, &key2, slot);
2557 if (key2.objectid != dirid) {
2558 ret = -ENOENT;
2559 goto out_put;
2560 }
2561
2562 /*
2563 * We don't need the path anymore, so release it and
2564 * avoid deadlocks and lockdep warnings in case
2565 * btrfs_iget() needs to lookup the inode from its root
2566 * btree and lock the same leaf.
2567 */
2568 btrfs_release_path(path);
2569 temp_inode = btrfs_iget(sb, key2.objectid, root);
2570 if (IS_ERR(temp_inode)) {
2571 ret = PTR_ERR(temp_inode);
2572 goto out_put;
2573 }
2574 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2575 iput(temp_inode);
2576 if (ret) {
2577 ret = -EACCES;
2578 goto out_put;
2579 }
2580
2581 if (key.offset == upper_limit.objectid)
2582 break;
2583 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2584 ret = -EACCES;
2585 goto out_put;
2586 }
2587
2588 key.objectid = key.offset;
2589 key.offset = (u64)-1;
2590 dirid = key.objectid;
2591 }
2592
2593 memmove(args->path, ptr, total_len);
2594 args->path[total_len] = '\0';
2595 btrfs_put_root(root);
2596 root = NULL;
2597 btrfs_release_path(path);
2598 }
2599
2600 /* Get the bottom subvolume's name from ROOT_REF */
2601 key.objectid = treeid;
2602 key.type = BTRFS_ROOT_REF_KEY;
2603 key.offset = args->treeid;
2604 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2605 if (ret < 0) {
2606 goto out;
2607 } else if (ret > 0) {
2608 ret = -ENOENT;
2609 goto out;
2610 }
2611
2612 leaf = path->nodes[0];
2613 slot = path->slots[0];
2614 btrfs_item_key_to_cpu(leaf, &key, slot);
2615
2616 item_off = btrfs_item_ptr_offset(leaf, slot);
2617 item_len = btrfs_item_size_nr(leaf, slot);
2618 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2619 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2620 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2621 ret = -EINVAL;
2622 goto out;
2623 }
2624
2625 /* Copy subvolume's name */
2626 item_off += sizeof(struct btrfs_root_ref);
2627 item_len -= sizeof(struct btrfs_root_ref);
2628 read_extent_buffer(leaf, args->name, item_off, item_len);
2629 args->name[item_len] = 0;
2630
2631 out_put:
2632 btrfs_put_root(root);
2633 out:
2634 btrfs_free_path(path);
2635 return ret;
2636 }
2637
btrfs_ioctl_ino_lookup(struct file * file,void __user * argp)2638 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2639 void __user *argp)
2640 {
2641 struct btrfs_ioctl_ino_lookup_args *args;
2642 struct inode *inode;
2643 int ret = 0;
2644
2645 args = memdup_user(argp, sizeof(*args));
2646 if (IS_ERR(args))
2647 return PTR_ERR(args);
2648
2649 inode = file_inode(file);
2650
2651 /*
2652 * Unprivileged query to obtain the containing subvolume root id. The
2653 * path is reset so it's consistent with btrfs_search_path_in_tree.
2654 */
2655 if (args->treeid == 0)
2656 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2657
2658 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2659 args->name[0] = 0;
2660 goto out;
2661 }
2662
2663 if (!capable(CAP_SYS_ADMIN)) {
2664 ret = -EPERM;
2665 goto out;
2666 }
2667
2668 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2669 args->treeid, args->objectid,
2670 args->name);
2671
2672 out:
2673 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2674 ret = -EFAULT;
2675
2676 kfree(args);
2677 return ret;
2678 }
2679
2680 /*
2681 * Version of ino_lookup ioctl (unprivileged)
2682 *
2683 * The main differences from ino_lookup ioctl are:
2684 *
2685 * 1. Read + Exec permission will be checked using inode_permission() during
2686 * path construction. -EACCES will be returned in case of failure.
2687 * 2. Path construction will be stopped at the inode number which corresponds
2688 * to the fd with which this ioctl is called. If constructed path does not
2689 * exist under fd's inode, -EACCES will be returned.
2690 * 3. The name of bottom subvolume is also searched and filled.
2691 */
btrfs_ioctl_ino_lookup_user(struct file * file,void __user * argp)2692 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2693 {
2694 struct btrfs_ioctl_ino_lookup_user_args *args;
2695 struct inode *inode;
2696 int ret;
2697
2698 args = memdup_user(argp, sizeof(*args));
2699 if (IS_ERR(args))
2700 return PTR_ERR(args);
2701
2702 inode = file_inode(file);
2703
2704 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2705 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2706 /*
2707 * The subvolume does not exist under fd with which this is
2708 * called
2709 */
2710 kfree(args);
2711 return -EACCES;
2712 }
2713
2714 ret = btrfs_search_path_in_tree_user(inode, args);
2715
2716 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2717 ret = -EFAULT;
2718
2719 kfree(args);
2720 return ret;
2721 }
2722
2723 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
btrfs_ioctl_get_subvol_info(struct file * file,void __user * argp)2724 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2725 {
2726 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2727 struct btrfs_fs_info *fs_info;
2728 struct btrfs_root *root;
2729 struct btrfs_path *path;
2730 struct btrfs_key key;
2731 struct btrfs_root_item *root_item;
2732 struct btrfs_root_ref *rref;
2733 struct extent_buffer *leaf;
2734 unsigned long item_off;
2735 unsigned long item_len;
2736 struct inode *inode;
2737 int slot;
2738 int ret = 0;
2739
2740 path = btrfs_alloc_path();
2741 if (!path)
2742 return -ENOMEM;
2743
2744 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2745 if (!subvol_info) {
2746 btrfs_free_path(path);
2747 return -ENOMEM;
2748 }
2749
2750 inode = file_inode(file);
2751 fs_info = BTRFS_I(inode)->root->fs_info;
2752
2753 /* Get root_item of inode's subvolume */
2754 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2755 root = btrfs_get_fs_root(fs_info, key.objectid, true);
2756 if (IS_ERR(root)) {
2757 ret = PTR_ERR(root);
2758 goto out_free;
2759 }
2760 root_item = &root->root_item;
2761
2762 subvol_info->treeid = key.objectid;
2763
2764 subvol_info->generation = btrfs_root_generation(root_item);
2765 subvol_info->flags = btrfs_root_flags(root_item);
2766
2767 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2768 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2769 BTRFS_UUID_SIZE);
2770 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2771 BTRFS_UUID_SIZE);
2772
2773 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2774 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2775 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2776
2777 subvol_info->otransid = btrfs_root_otransid(root_item);
2778 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2779 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2780
2781 subvol_info->stransid = btrfs_root_stransid(root_item);
2782 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2783 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2784
2785 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2786 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2787 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2788
2789 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2790 /* Search root tree for ROOT_BACKREF of this subvolume */
2791 key.type = BTRFS_ROOT_BACKREF_KEY;
2792 key.offset = 0;
2793 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2794 if (ret < 0) {
2795 goto out;
2796 } else if (path->slots[0] >=
2797 btrfs_header_nritems(path->nodes[0])) {
2798 ret = btrfs_next_leaf(fs_info->tree_root, path);
2799 if (ret < 0) {
2800 goto out;
2801 } else if (ret > 0) {
2802 ret = -EUCLEAN;
2803 goto out;
2804 }
2805 }
2806
2807 leaf = path->nodes[0];
2808 slot = path->slots[0];
2809 btrfs_item_key_to_cpu(leaf, &key, slot);
2810 if (key.objectid == subvol_info->treeid &&
2811 key.type == BTRFS_ROOT_BACKREF_KEY) {
2812 subvol_info->parent_id = key.offset;
2813
2814 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2815 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2816
2817 item_off = btrfs_item_ptr_offset(leaf, slot)
2818 + sizeof(struct btrfs_root_ref);
2819 item_len = btrfs_item_size_nr(leaf, slot)
2820 - sizeof(struct btrfs_root_ref);
2821 read_extent_buffer(leaf, subvol_info->name,
2822 item_off, item_len);
2823 } else {
2824 ret = -ENOENT;
2825 goto out;
2826 }
2827 }
2828
2829 btrfs_free_path(path);
2830 path = NULL;
2831 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2832 ret = -EFAULT;
2833
2834 out:
2835 btrfs_put_root(root);
2836 out_free:
2837 btrfs_free_path(path);
2838 kfree(subvol_info);
2839 return ret;
2840 }
2841
2842 /*
2843 * Return ROOT_REF information of the subvolume containing this inode
2844 * except the subvolume name.
2845 */
btrfs_ioctl_get_subvol_rootref(struct file * file,void __user * argp)2846 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2847 {
2848 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2849 struct btrfs_root_ref *rref;
2850 struct btrfs_root *root;
2851 struct btrfs_path *path;
2852 struct btrfs_key key;
2853 struct extent_buffer *leaf;
2854 struct inode *inode;
2855 u64 objectid;
2856 int slot;
2857 int ret;
2858 u8 found;
2859
2860 path = btrfs_alloc_path();
2861 if (!path)
2862 return -ENOMEM;
2863
2864 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2865 if (IS_ERR(rootrefs)) {
2866 btrfs_free_path(path);
2867 return PTR_ERR(rootrefs);
2868 }
2869
2870 inode = file_inode(file);
2871 root = BTRFS_I(inode)->root->fs_info->tree_root;
2872 objectid = BTRFS_I(inode)->root->root_key.objectid;
2873
2874 key.objectid = objectid;
2875 key.type = BTRFS_ROOT_REF_KEY;
2876 key.offset = rootrefs->min_treeid;
2877 found = 0;
2878
2879 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2880 if (ret < 0) {
2881 goto out;
2882 } else if (path->slots[0] >=
2883 btrfs_header_nritems(path->nodes[0])) {
2884 ret = btrfs_next_leaf(root, path);
2885 if (ret < 0) {
2886 goto out;
2887 } else if (ret > 0) {
2888 ret = -EUCLEAN;
2889 goto out;
2890 }
2891 }
2892 while (1) {
2893 leaf = path->nodes[0];
2894 slot = path->slots[0];
2895
2896 btrfs_item_key_to_cpu(leaf, &key, slot);
2897 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2898 ret = 0;
2899 goto out;
2900 }
2901
2902 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2903 ret = -EOVERFLOW;
2904 goto out;
2905 }
2906
2907 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2908 rootrefs->rootref[found].treeid = key.offset;
2909 rootrefs->rootref[found].dirid =
2910 btrfs_root_ref_dirid(leaf, rref);
2911 found++;
2912
2913 ret = btrfs_next_item(root, path);
2914 if (ret < 0) {
2915 goto out;
2916 } else if (ret > 0) {
2917 ret = -EUCLEAN;
2918 goto out;
2919 }
2920 }
2921
2922 out:
2923 btrfs_free_path(path);
2924
2925 if (!ret || ret == -EOVERFLOW) {
2926 rootrefs->num_items = found;
2927 /* update min_treeid for next search */
2928 if (found)
2929 rootrefs->min_treeid =
2930 rootrefs->rootref[found - 1].treeid + 1;
2931 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2932 ret = -EFAULT;
2933 }
2934
2935 kfree(rootrefs);
2936
2937 return ret;
2938 }
2939
btrfs_ioctl_snap_destroy(struct file * file,void __user * arg,bool destroy_v2)2940 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2941 void __user *arg,
2942 bool destroy_v2)
2943 {
2944 struct dentry *parent = file->f_path.dentry;
2945 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2946 struct dentry *dentry;
2947 struct inode *dir = d_inode(parent);
2948 struct inode *inode;
2949 struct btrfs_root *root = BTRFS_I(dir)->root;
2950 struct btrfs_root *dest = NULL;
2951 struct btrfs_ioctl_vol_args *vol_args = NULL;
2952 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2953 char *subvol_name, *subvol_name_ptr = NULL;
2954 int subvol_namelen;
2955 int err = 0;
2956 bool destroy_parent = false;
2957
2958 if (destroy_v2) {
2959 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2960 if (IS_ERR(vol_args2))
2961 return PTR_ERR(vol_args2);
2962
2963 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2964 err = -EOPNOTSUPP;
2965 goto out;
2966 }
2967
2968 /*
2969 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2970 * name, same as v1 currently does.
2971 */
2972 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2973 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2974 subvol_name = vol_args2->name;
2975
2976 err = mnt_want_write_file(file);
2977 if (err)
2978 goto out;
2979 } else {
2980 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2981 err = -EINVAL;
2982 goto out;
2983 }
2984
2985 err = mnt_want_write_file(file);
2986 if (err)
2987 goto out;
2988
2989 dentry = btrfs_get_dentry(fs_info->sb,
2990 BTRFS_FIRST_FREE_OBJECTID,
2991 vol_args2->subvolid, 0, 0);
2992 if (IS_ERR(dentry)) {
2993 err = PTR_ERR(dentry);
2994 goto out_drop_write;
2995 }
2996
2997 /*
2998 * Change the default parent since the subvolume being
2999 * deleted can be outside of the current mount point.
3000 */
3001 parent = btrfs_get_parent(dentry);
3002
3003 /*
3004 * At this point dentry->d_name can point to '/' if the
3005 * subvolume we want to destroy is outsite of the
3006 * current mount point, so we need to release the
3007 * current dentry and execute the lookup to return a new
3008 * one with ->d_name pointing to the
3009 * <mount point>/subvol_name.
3010 */
3011 dput(dentry);
3012 if (IS_ERR(parent)) {
3013 err = PTR_ERR(parent);
3014 goto out_drop_write;
3015 }
3016 dir = d_inode(parent);
3017
3018 /*
3019 * If v2 was used with SPEC_BY_ID, a new parent was
3020 * allocated since the subvolume can be outside of the
3021 * current mount point. Later on we need to release this
3022 * new parent dentry.
3023 */
3024 destroy_parent = true;
3025
3026 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
3027 fs_info, vol_args2->subvolid);
3028 if (IS_ERR(subvol_name_ptr)) {
3029 err = PTR_ERR(subvol_name_ptr);
3030 goto free_parent;
3031 }
3032 /* subvol_name_ptr is already NULL termined */
3033 subvol_name = (char *)kbasename(subvol_name_ptr);
3034 }
3035 } else {
3036 vol_args = memdup_user(arg, sizeof(*vol_args));
3037 if (IS_ERR(vol_args))
3038 return PTR_ERR(vol_args);
3039
3040 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
3041 subvol_name = vol_args->name;
3042
3043 err = mnt_want_write_file(file);
3044 if (err)
3045 goto out;
3046 }
3047
3048 subvol_namelen = strlen(subvol_name);
3049
3050 if (strchr(subvol_name, '/') ||
3051 strncmp(subvol_name, "..", subvol_namelen) == 0) {
3052 err = -EINVAL;
3053 goto free_subvol_name;
3054 }
3055
3056 if (!S_ISDIR(dir->i_mode)) {
3057 err = -ENOTDIR;
3058 goto free_subvol_name;
3059 }
3060
3061 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
3062 if (err == -EINTR)
3063 goto free_subvol_name;
3064 dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
3065 if (IS_ERR(dentry)) {
3066 err = PTR_ERR(dentry);
3067 goto out_unlock_dir;
3068 }
3069
3070 if (d_really_is_negative(dentry)) {
3071 err = -ENOENT;
3072 goto out_dput;
3073 }
3074
3075 inode = d_inode(dentry);
3076 dest = BTRFS_I(inode)->root;
3077 if (!capable(CAP_SYS_ADMIN)) {
3078 /*
3079 * Regular user. Only allow this with a special mount
3080 * option, when the user has write+exec access to the
3081 * subvol root, and when rmdir(2) would have been
3082 * allowed.
3083 *
3084 * Note that this is _not_ check that the subvol is
3085 * empty or doesn't contain data that we wouldn't
3086 * otherwise be able to delete.
3087 *
3088 * Users who want to delete empty subvols should try
3089 * rmdir(2).
3090 */
3091 err = -EPERM;
3092 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
3093 goto out_dput;
3094
3095 /*
3096 * Do not allow deletion if the parent dir is the same
3097 * as the dir to be deleted. That means the ioctl
3098 * must be called on the dentry referencing the root
3099 * of the subvol, not a random directory contained
3100 * within it.
3101 */
3102 err = -EINVAL;
3103 if (root == dest)
3104 goto out_dput;
3105
3106 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
3107 if (err)
3108 goto out_dput;
3109 }
3110
3111 /* check if subvolume may be deleted by a user */
3112 err = btrfs_may_delete(dir, dentry, 1);
3113 if (err)
3114 goto out_dput;
3115
3116 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
3117 err = -EINVAL;
3118 goto out_dput;
3119 }
3120
3121 inode_lock(inode);
3122 err = btrfs_delete_subvolume(dir, dentry);
3123 inode_unlock(inode);
3124 if (!err)
3125 d_delete_notify(dir, dentry);
3126
3127 out_dput:
3128 dput(dentry);
3129 out_unlock_dir:
3130 inode_unlock(dir);
3131 free_subvol_name:
3132 kfree(subvol_name_ptr);
3133 free_parent:
3134 if (destroy_parent)
3135 dput(parent);
3136 out_drop_write:
3137 mnt_drop_write_file(file);
3138 out:
3139 kfree(vol_args2);
3140 kfree(vol_args);
3141 return err;
3142 }
3143
btrfs_ioctl_defrag(struct file * file,void __user * argp)3144 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
3145 {
3146 struct inode *inode = file_inode(file);
3147 struct btrfs_root *root = BTRFS_I(inode)->root;
3148 struct btrfs_ioctl_defrag_range_args *range;
3149 int ret;
3150
3151 ret = mnt_want_write_file(file);
3152 if (ret)
3153 return ret;
3154
3155 if (btrfs_root_readonly(root)) {
3156 ret = -EROFS;
3157 goto out;
3158 }
3159
3160 switch (inode->i_mode & S_IFMT) {
3161 case S_IFDIR:
3162 if (!capable(CAP_SYS_ADMIN)) {
3163 ret = -EPERM;
3164 goto out;
3165 }
3166 ret = btrfs_defrag_root(root);
3167 break;
3168 case S_IFREG:
3169 /*
3170 * Note that this does not check the file descriptor for write
3171 * access. This prevents defragmenting executables that are
3172 * running and allows defrag on files open in read-only mode.
3173 */
3174 if (!capable(CAP_SYS_ADMIN) &&
3175 inode_permission(inode, MAY_WRITE)) {
3176 ret = -EPERM;
3177 goto out;
3178 }
3179
3180 range = kzalloc(sizeof(*range), GFP_KERNEL);
3181 if (!range) {
3182 ret = -ENOMEM;
3183 goto out;
3184 }
3185
3186 if (argp) {
3187 if (copy_from_user(range, argp,
3188 sizeof(*range))) {
3189 ret = -EFAULT;
3190 kfree(range);
3191 goto out;
3192 }
3193 /* compression requires us to start the IO */
3194 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3195 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3196 range->extent_thresh = (u32)-1;
3197 }
3198 } else {
3199 /* the rest are all set to zero by kzalloc */
3200 range->len = (u64)-1;
3201 }
3202 ret = btrfs_defrag_file(file_inode(file), file,
3203 range, BTRFS_OLDEST_GENERATION, 0);
3204 if (ret > 0)
3205 ret = 0;
3206 kfree(range);
3207 break;
3208 default:
3209 ret = -EINVAL;
3210 }
3211 out:
3212 mnt_drop_write_file(file);
3213 return ret;
3214 }
3215
btrfs_ioctl_add_dev(struct btrfs_fs_info * fs_info,void __user * arg)3216 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3217 {
3218 struct btrfs_ioctl_vol_args *vol_args;
3219 int ret;
3220
3221 if (!capable(CAP_SYS_ADMIN))
3222 return -EPERM;
3223
3224 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
3225 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3226
3227 vol_args = memdup_user(arg, sizeof(*vol_args));
3228 if (IS_ERR(vol_args)) {
3229 ret = PTR_ERR(vol_args);
3230 goto out;
3231 }
3232
3233 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3234 ret = btrfs_init_new_device(fs_info, vol_args->name);
3235
3236 if (!ret)
3237 btrfs_info(fs_info, "disk added %s", vol_args->name);
3238
3239 kfree(vol_args);
3240 out:
3241 btrfs_exclop_finish(fs_info);
3242 return ret;
3243 }
3244
btrfs_ioctl_rm_dev_v2(struct file * file,void __user * arg)3245 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3246 {
3247 struct inode *inode = file_inode(file);
3248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3249 struct btrfs_ioctl_vol_args_v2 *vol_args;
3250 int ret;
3251
3252 if (!capable(CAP_SYS_ADMIN))
3253 return -EPERM;
3254
3255 ret = mnt_want_write_file(file);
3256 if (ret)
3257 return ret;
3258
3259 vol_args = memdup_user(arg, sizeof(*vol_args));
3260 if (IS_ERR(vol_args)) {
3261 ret = PTR_ERR(vol_args);
3262 goto err_drop;
3263 }
3264
3265 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
3266 ret = -EOPNOTSUPP;
3267 goto out;
3268 }
3269
3270 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3271 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3272 goto out;
3273 }
3274
3275 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3276 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3277 } else {
3278 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3279 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3280 }
3281 btrfs_exclop_finish(fs_info);
3282
3283 if (!ret) {
3284 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3285 btrfs_info(fs_info, "device deleted: id %llu",
3286 vol_args->devid);
3287 else
3288 btrfs_info(fs_info, "device deleted: %s",
3289 vol_args->name);
3290 }
3291 out:
3292 kfree(vol_args);
3293 err_drop:
3294 mnt_drop_write_file(file);
3295 return ret;
3296 }
3297
btrfs_ioctl_rm_dev(struct file * file,void __user * arg)3298 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3299 {
3300 struct inode *inode = file_inode(file);
3301 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3302 struct btrfs_ioctl_vol_args *vol_args;
3303 int ret;
3304
3305 if (!capable(CAP_SYS_ADMIN))
3306 return -EPERM;
3307
3308 ret = mnt_want_write_file(file);
3309 if (ret)
3310 return ret;
3311
3312 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3313 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3314 goto out_drop_write;
3315 }
3316
3317 vol_args = memdup_user(arg, sizeof(*vol_args));
3318 if (IS_ERR(vol_args)) {
3319 ret = PTR_ERR(vol_args);
3320 goto out;
3321 }
3322
3323 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3324 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3325
3326 if (!ret)
3327 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3328 kfree(vol_args);
3329 out:
3330 btrfs_exclop_finish(fs_info);
3331 out_drop_write:
3332 mnt_drop_write_file(file);
3333
3334 return ret;
3335 }
3336
btrfs_ioctl_fs_info(struct btrfs_fs_info * fs_info,void __user * arg)3337 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3338 void __user *arg)
3339 {
3340 struct btrfs_ioctl_fs_info_args *fi_args;
3341 struct btrfs_device *device;
3342 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3343 u64 flags_in;
3344 int ret = 0;
3345
3346 fi_args = memdup_user(arg, sizeof(*fi_args));
3347 if (IS_ERR(fi_args))
3348 return PTR_ERR(fi_args);
3349
3350 flags_in = fi_args->flags;
3351 memset(fi_args, 0, sizeof(*fi_args));
3352
3353 rcu_read_lock();
3354 fi_args->num_devices = fs_devices->num_devices;
3355
3356 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3357 if (device->devid > fi_args->max_id)
3358 fi_args->max_id = device->devid;
3359 }
3360 rcu_read_unlock();
3361
3362 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3363 fi_args->nodesize = fs_info->nodesize;
3364 fi_args->sectorsize = fs_info->sectorsize;
3365 fi_args->clone_alignment = fs_info->sectorsize;
3366
3367 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3368 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3369 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3370 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3371 }
3372
3373 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3374 fi_args->generation = fs_info->generation;
3375 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3376 }
3377
3378 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3379 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3380 sizeof(fi_args->metadata_uuid));
3381 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3382 }
3383
3384 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3385 ret = -EFAULT;
3386
3387 kfree(fi_args);
3388 return ret;
3389 }
3390
btrfs_ioctl_dev_info(struct btrfs_fs_info * fs_info,void __user * arg)3391 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3392 void __user *arg)
3393 {
3394 struct btrfs_ioctl_dev_info_args *di_args;
3395 struct btrfs_device *dev;
3396 int ret = 0;
3397 char *s_uuid = NULL;
3398
3399 di_args = memdup_user(arg, sizeof(*di_args));
3400 if (IS_ERR(di_args))
3401 return PTR_ERR(di_args);
3402
3403 if (!btrfs_is_empty_uuid(di_args->uuid))
3404 s_uuid = di_args->uuid;
3405
3406 rcu_read_lock();
3407 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3408 NULL, true);
3409
3410 if (!dev) {
3411 ret = -ENODEV;
3412 goto out;
3413 }
3414
3415 di_args->devid = dev->devid;
3416 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3417 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3418 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3419 if (dev->name)
3420 strscpy(di_args->path, rcu_str_deref(dev->name), sizeof(di_args->path));
3421 else
3422 di_args->path[0] = '\0';
3423
3424 out:
3425 rcu_read_unlock();
3426 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3427 ret = -EFAULT;
3428
3429 kfree(di_args);
3430 return ret;
3431 }
3432
btrfs_ioctl_default_subvol(struct file * file,void __user * argp)3433 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3434 {
3435 struct inode *inode = file_inode(file);
3436 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3437 struct btrfs_root *root = BTRFS_I(inode)->root;
3438 struct btrfs_root *new_root;
3439 struct btrfs_dir_item *di;
3440 struct btrfs_trans_handle *trans;
3441 struct btrfs_path *path = NULL;
3442 struct btrfs_disk_key disk_key;
3443 u64 objectid = 0;
3444 u64 dir_id;
3445 int ret;
3446
3447 if (!capable(CAP_SYS_ADMIN))
3448 return -EPERM;
3449
3450 ret = mnt_want_write_file(file);
3451 if (ret)
3452 return ret;
3453
3454 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3455 ret = -EFAULT;
3456 goto out;
3457 }
3458
3459 if (!objectid)
3460 objectid = BTRFS_FS_TREE_OBJECTID;
3461
3462 new_root = btrfs_get_fs_root(fs_info, objectid, true);
3463 if (IS_ERR(new_root)) {
3464 ret = PTR_ERR(new_root);
3465 goto out;
3466 }
3467 if (!is_fstree(new_root->root_key.objectid)) {
3468 ret = -ENOENT;
3469 goto out_free;
3470 }
3471
3472 path = btrfs_alloc_path();
3473 if (!path) {
3474 ret = -ENOMEM;
3475 goto out_free;
3476 }
3477 path->leave_spinning = 1;
3478
3479 trans = btrfs_start_transaction(root, 1);
3480 if (IS_ERR(trans)) {
3481 ret = PTR_ERR(trans);
3482 goto out_free;
3483 }
3484
3485 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3486 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
3487 dir_id, "default", 7, 1);
3488 if (IS_ERR_OR_NULL(di)) {
3489 btrfs_release_path(path);
3490 btrfs_end_transaction(trans);
3491 btrfs_err(fs_info,
3492 "Umm, you don't have the default diritem, this isn't going to work");
3493 ret = -ENOENT;
3494 goto out_free;
3495 }
3496
3497 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3498 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3499 btrfs_mark_buffer_dirty(path->nodes[0]);
3500 btrfs_release_path(path);
3501
3502 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
3503 btrfs_end_transaction(trans);
3504 out_free:
3505 btrfs_put_root(new_root);
3506 btrfs_free_path(path);
3507 out:
3508 mnt_drop_write_file(file);
3509 return ret;
3510 }
3511
get_block_group_info(struct list_head * groups_list,struct btrfs_ioctl_space_info * space)3512 static void get_block_group_info(struct list_head *groups_list,
3513 struct btrfs_ioctl_space_info *space)
3514 {
3515 struct btrfs_block_group *block_group;
3516
3517 space->total_bytes = 0;
3518 space->used_bytes = 0;
3519 space->flags = 0;
3520 list_for_each_entry(block_group, groups_list, list) {
3521 space->flags = block_group->flags;
3522 space->total_bytes += block_group->length;
3523 space->used_bytes += block_group->used;
3524 }
3525 }
3526
btrfs_ioctl_space_info(struct btrfs_fs_info * fs_info,void __user * arg)3527 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3528 void __user *arg)
3529 {
3530 struct btrfs_ioctl_space_args space_args = { 0 };
3531 struct btrfs_ioctl_space_info space;
3532 struct btrfs_ioctl_space_info *dest;
3533 struct btrfs_ioctl_space_info *dest_orig;
3534 struct btrfs_ioctl_space_info __user *user_dest;
3535 struct btrfs_space_info *info;
3536 static const u64 types[] = {
3537 BTRFS_BLOCK_GROUP_DATA,
3538 BTRFS_BLOCK_GROUP_SYSTEM,
3539 BTRFS_BLOCK_GROUP_METADATA,
3540 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3541 };
3542 int num_types = 4;
3543 int alloc_size;
3544 int ret = 0;
3545 u64 slot_count = 0;
3546 int i, c;
3547
3548 if (copy_from_user(&space_args,
3549 (struct btrfs_ioctl_space_args __user *)arg,
3550 sizeof(space_args)))
3551 return -EFAULT;
3552
3553 for (i = 0; i < num_types; i++) {
3554 struct btrfs_space_info *tmp;
3555
3556 info = NULL;
3557 list_for_each_entry(tmp, &fs_info->space_info, list) {
3558 if (tmp->flags == types[i]) {
3559 info = tmp;
3560 break;
3561 }
3562 }
3563
3564 if (!info)
3565 continue;
3566
3567 down_read(&info->groups_sem);
3568 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3569 if (!list_empty(&info->block_groups[c]))
3570 slot_count++;
3571 }
3572 up_read(&info->groups_sem);
3573 }
3574
3575 /*
3576 * Global block reserve, exported as a space_info
3577 */
3578 slot_count++;
3579
3580 /* space_slots == 0 means they are asking for a count */
3581 if (space_args.space_slots == 0) {
3582 space_args.total_spaces = slot_count;
3583 goto out;
3584 }
3585
3586 slot_count = min_t(u64, space_args.space_slots, slot_count);
3587
3588 alloc_size = sizeof(*dest) * slot_count;
3589
3590 /* we generally have at most 6 or so space infos, one for each raid
3591 * level. So, a whole page should be more than enough for everyone
3592 */
3593 if (alloc_size > PAGE_SIZE)
3594 return -ENOMEM;
3595
3596 space_args.total_spaces = 0;
3597 dest = kmalloc(alloc_size, GFP_KERNEL);
3598 if (!dest)
3599 return -ENOMEM;
3600 dest_orig = dest;
3601
3602 /* now we have a buffer to copy into */
3603 for (i = 0; i < num_types; i++) {
3604 struct btrfs_space_info *tmp;
3605
3606 if (!slot_count)
3607 break;
3608
3609 info = NULL;
3610 list_for_each_entry(tmp, &fs_info->space_info, list) {
3611 if (tmp->flags == types[i]) {
3612 info = tmp;
3613 break;
3614 }
3615 }
3616
3617 if (!info)
3618 continue;
3619 down_read(&info->groups_sem);
3620 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3621 if (!list_empty(&info->block_groups[c])) {
3622 get_block_group_info(&info->block_groups[c],
3623 &space);
3624 memcpy(dest, &space, sizeof(space));
3625 dest++;
3626 space_args.total_spaces++;
3627 slot_count--;
3628 }
3629 if (!slot_count)
3630 break;
3631 }
3632 up_read(&info->groups_sem);
3633 }
3634
3635 /*
3636 * Add global block reserve
3637 */
3638 if (slot_count) {
3639 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3640
3641 spin_lock(&block_rsv->lock);
3642 space.total_bytes = block_rsv->size;
3643 space.used_bytes = block_rsv->size - block_rsv->reserved;
3644 spin_unlock(&block_rsv->lock);
3645 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3646 memcpy(dest, &space, sizeof(space));
3647 space_args.total_spaces++;
3648 }
3649
3650 user_dest = (struct btrfs_ioctl_space_info __user *)
3651 (arg + sizeof(struct btrfs_ioctl_space_args));
3652
3653 if (copy_to_user(user_dest, dest_orig, alloc_size))
3654 ret = -EFAULT;
3655
3656 kfree(dest_orig);
3657 out:
3658 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3659 ret = -EFAULT;
3660
3661 return ret;
3662 }
3663
btrfs_ioctl_start_sync(struct btrfs_root * root,void __user * argp)3664 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3665 void __user *argp)
3666 {
3667 struct btrfs_trans_handle *trans;
3668 u64 transid;
3669 int ret;
3670
3671 trans = btrfs_attach_transaction_barrier(root);
3672 if (IS_ERR(trans)) {
3673 if (PTR_ERR(trans) != -ENOENT)
3674 return PTR_ERR(trans);
3675
3676 /* No running transaction, don't bother */
3677 transid = root->fs_info->last_trans_committed;
3678 goto out;
3679 }
3680 transid = trans->transid;
3681 ret = btrfs_commit_transaction_async(trans, 0);
3682 if (ret) {
3683 btrfs_end_transaction(trans);
3684 return ret;
3685 }
3686 out:
3687 if (argp)
3688 if (copy_to_user(argp, &transid, sizeof(transid)))
3689 return -EFAULT;
3690 return 0;
3691 }
3692
btrfs_ioctl_wait_sync(struct btrfs_fs_info * fs_info,void __user * argp)3693 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
3694 void __user *argp)
3695 {
3696 u64 transid;
3697
3698 if (argp) {
3699 if (copy_from_user(&transid, argp, sizeof(transid)))
3700 return -EFAULT;
3701 } else {
3702 transid = 0; /* current trans */
3703 }
3704 return btrfs_wait_for_commit(fs_info, transid);
3705 }
3706
btrfs_ioctl_scrub(struct file * file,void __user * arg)3707 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3708 {
3709 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
3710 struct btrfs_ioctl_scrub_args *sa;
3711 int ret;
3712
3713 if (!capable(CAP_SYS_ADMIN))
3714 return -EPERM;
3715
3716 sa = memdup_user(arg, sizeof(*sa));
3717 if (IS_ERR(sa))
3718 return PTR_ERR(sa);
3719
3720 if (sa->flags & ~BTRFS_SCRUB_SUPPORTED_FLAGS) {
3721 ret = -EOPNOTSUPP;
3722 goto out;
3723 }
3724
3725 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3726 ret = mnt_want_write_file(file);
3727 if (ret)
3728 goto out;
3729 }
3730
3731 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
3732 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3733 0);
3734
3735 /*
3736 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3737 * error. This is important as it allows user space to know how much
3738 * progress scrub has done. For example, if scrub is canceled we get
3739 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3740 * space. Later user space can inspect the progress from the structure
3741 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3742 * previously (btrfs-progs does this).
3743 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3744 * then return -EFAULT to signal the structure was not copied or it may
3745 * be corrupt and unreliable due to a partial copy.
3746 */
3747 if (copy_to_user(arg, sa, sizeof(*sa)))
3748 ret = -EFAULT;
3749
3750 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3751 mnt_drop_write_file(file);
3752 out:
3753 kfree(sa);
3754 return ret;
3755 }
3756
btrfs_ioctl_scrub_cancel(struct btrfs_fs_info * fs_info)3757 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
3758 {
3759 if (!capable(CAP_SYS_ADMIN))
3760 return -EPERM;
3761
3762 return btrfs_scrub_cancel(fs_info);
3763 }
3764
btrfs_ioctl_scrub_progress(struct btrfs_fs_info * fs_info,void __user * arg)3765 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
3766 void __user *arg)
3767 {
3768 struct btrfs_ioctl_scrub_args *sa;
3769 int ret;
3770
3771 if (!capable(CAP_SYS_ADMIN))
3772 return -EPERM;
3773
3774 sa = memdup_user(arg, sizeof(*sa));
3775 if (IS_ERR(sa))
3776 return PTR_ERR(sa);
3777
3778 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
3779
3780 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3781 ret = -EFAULT;
3782
3783 kfree(sa);
3784 return ret;
3785 }
3786
btrfs_ioctl_get_dev_stats(struct btrfs_fs_info * fs_info,void __user * arg)3787 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
3788 void __user *arg)
3789 {
3790 struct btrfs_ioctl_get_dev_stats *sa;
3791 int ret;
3792
3793 sa = memdup_user(arg, sizeof(*sa));
3794 if (IS_ERR(sa))
3795 return PTR_ERR(sa);
3796
3797 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3798 kfree(sa);
3799 return -EPERM;
3800 }
3801
3802 ret = btrfs_get_dev_stats(fs_info, sa);
3803
3804 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3805 ret = -EFAULT;
3806
3807 kfree(sa);
3808 return ret;
3809 }
3810
btrfs_ioctl_dev_replace(struct btrfs_fs_info * fs_info,void __user * arg)3811 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3812 void __user *arg)
3813 {
3814 struct btrfs_ioctl_dev_replace_args *p;
3815 int ret;
3816
3817 if (!capable(CAP_SYS_ADMIN))
3818 return -EPERM;
3819
3820 p = memdup_user(arg, sizeof(*p));
3821 if (IS_ERR(p))
3822 return PTR_ERR(p);
3823
3824 switch (p->cmd) {
3825 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3826 if (sb_rdonly(fs_info->sb)) {
3827 ret = -EROFS;
3828 goto out;
3829 }
3830 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
3831 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3832 } else {
3833 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
3834 btrfs_exclop_finish(fs_info);
3835 }
3836 break;
3837 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3838 btrfs_dev_replace_status(fs_info, p);
3839 ret = 0;
3840 break;
3841 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3842 p->result = btrfs_dev_replace_cancel(fs_info);
3843 ret = 0;
3844 break;
3845 default:
3846 ret = -EINVAL;
3847 break;
3848 }
3849
3850 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
3851 ret = -EFAULT;
3852 out:
3853 kfree(p);
3854 return ret;
3855 }
3856
btrfs_ioctl_ino_to_path(struct btrfs_root * root,void __user * arg)3857 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3858 {
3859 int ret = 0;
3860 int i;
3861 u64 rel_ptr;
3862 int size;
3863 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3864 struct inode_fs_paths *ipath = NULL;
3865 struct btrfs_path *path;
3866
3867 if (!capable(CAP_DAC_READ_SEARCH))
3868 return -EPERM;
3869
3870 path = btrfs_alloc_path();
3871 if (!path) {
3872 ret = -ENOMEM;
3873 goto out;
3874 }
3875
3876 ipa = memdup_user(arg, sizeof(*ipa));
3877 if (IS_ERR(ipa)) {
3878 ret = PTR_ERR(ipa);
3879 ipa = NULL;
3880 goto out;
3881 }
3882
3883 size = min_t(u32, ipa->size, 4096);
3884 ipath = init_ipath(size, root, path);
3885 if (IS_ERR(ipath)) {
3886 ret = PTR_ERR(ipath);
3887 ipath = NULL;
3888 goto out;
3889 }
3890
3891 ret = paths_from_inode(ipa->inum, ipath);
3892 if (ret < 0)
3893 goto out;
3894
3895 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3896 rel_ptr = ipath->fspath->val[i] -
3897 (u64)(unsigned long)ipath->fspath->val;
3898 ipath->fspath->val[i] = rel_ptr;
3899 }
3900
3901 btrfs_free_path(path);
3902 path = NULL;
3903 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3904 ipath->fspath, size);
3905 if (ret) {
3906 ret = -EFAULT;
3907 goto out;
3908 }
3909
3910 out:
3911 btrfs_free_path(path);
3912 free_ipath(ipath);
3913 kfree(ipa);
3914
3915 return ret;
3916 }
3917
btrfs_ioctl_logical_to_ino(struct btrfs_fs_info * fs_info,void __user * arg,int version)3918 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
3919 void __user *arg, int version)
3920 {
3921 int ret = 0;
3922 int size;
3923 struct btrfs_ioctl_logical_ino_args *loi;
3924 struct btrfs_data_container *inodes = NULL;
3925 struct btrfs_path *path = NULL;
3926 bool ignore_offset;
3927
3928 if (!capable(CAP_SYS_ADMIN))
3929 return -EPERM;
3930
3931 loi = memdup_user(arg, sizeof(*loi));
3932 if (IS_ERR(loi))
3933 return PTR_ERR(loi);
3934
3935 if (version == 1) {
3936 ignore_offset = false;
3937 size = min_t(u32, loi->size, SZ_64K);
3938 } else {
3939 /* All reserved bits must be 0 for now */
3940 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3941 ret = -EINVAL;
3942 goto out_loi;
3943 }
3944 /* Only accept flags we have defined so far */
3945 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3946 ret = -EINVAL;
3947 goto out_loi;
3948 }
3949 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
3950 size = min_t(u32, loi->size, SZ_16M);
3951 }
3952
3953 inodes = init_data_container(size);
3954 if (IS_ERR(inodes)) {
3955 ret = PTR_ERR(inodes);
3956 goto out_loi;
3957 }
3958
3959 path = btrfs_alloc_path();
3960 if (!path) {
3961 ret = -ENOMEM;
3962 goto out;
3963 }
3964 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
3965 inodes, ignore_offset);
3966 btrfs_free_path(path);
3967 if (ret == -EINVAL)
3968 ret = -ENOENT;
3969 if (ret < 0)
3970 goto out;
3971
3972 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3973 size);
3974 if (ret)
3975 ret = -EFAULT;
3976
3977 out:
3978 kvfree(inodes);
3979 out_loi:
3980 kfree(loi);
3981
3982 return ret;
3983 }
3984
btrfs_update_ioctl_balance_args(struct btrfs_fs_info * fs_info,struct btrfs_ioctl_balance_args * bargs)3985 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3986 struct btrfs_ioctl_balance_args *bargs)
3987 {
3988 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3989
3990 bargs->flags = bctl->flags;
3991
3992 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
3993 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3994 if (atomic_read(&fs_info->balance_pause_req))
3995 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3996 if (atomic_read(&fs_info->balance_cancel_req))
3997 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3998
3999 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4000 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4001 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
4002
4003 spin_lock(&fs_info->balance_lock);
4004 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4005 spin_unlock(&fs_info->balance_lock);
4006 }
4007
btrfs_ioctl_balance(struct file * file,void __user * arg)4008 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4009 {
4010 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4011 struct btrfs_fs_info *fs_info = root->fs_info;
4012 struct btrfs_ioctl_balance_args *bargs;
4013 struct btrfs_balance_control *bctl;
4014 bool need_unlock; /* for mut. excl. ops lock */
4015 int ret;
4016
4017 if (!capable(CAP_SYS_ADMIN))
4018 return -EPERM;
4019
4020 ret = mnt_want_write_file(file);
4021 if (ret)
4022 return ret;
4023
4024 again:
4025 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
4026 mutex_lock(&fs_info->balance_mutex);
4027 need_unlock = true;
4028 goto locked;
4029 }
4030
4031 /*
4032 * mut. excl. ops lock is locked. Three possibilities:
4033 * (1) some other op is running
4034 * (2) balance is running
4035 * (3) balance is paused -- special case (think resume)
4036 */
4037 mutex_lock(&fs_info->balance_mutex);
4038 if (fs_info->balance_ctl) {
4039 /* this is either (2) or (3) */
4040 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4041 mutex_unlock(&fs_info->balance_mutex);
4042 /*
4043 * Lock released to allow other waiters to continue,
4044 * we'll reexamine the status again.
4045 */
4046 mutex_lock(&fs_info->balance_mutex);
4047
4048 if (fs_info->balance_ctl &&
4049 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4050 /* this is (3) */
4051 need_unlock = false;
4052 goto locked;
4053 }
4054
4055 mutex_unlock(&fs_info->balance_mutex);
4056 goto again;
4057 } else {
4058 /* this is (2) */
4059 mutex_unlock(&fs_info->balance_mutex);
4060 ret = -EINPROGRESS;
4061 goto out;
4062 }
4063 } else {
4064 /* this is (1) */
4065 mutex_unlock(&fs_info->balance_mutex);
4066 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4067 goto out;
4068 }
4069
4070 locked:
4071
4072 if (arg) {
4073 bargs = memdup_user(arg, sizeof(*bargs));
4074 if (IS_ERR(bargs)) {
4075 ret = PTR_ERR(bargs);
4076 goto out_unlock;
4077 }
4078
4079 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4080 if (!fs_info->balance_ctl) {
4081 ret = -ENOTCONN;
4082 goto out_bargs;
4083 }
4084
4085 bctl = fs_info->balance_ctl;
4086 spin_lock(&fs_info->balance_lock);
4087 bctl->flags |= BTRFS_BALANCE_RESUME;
4088 spin_unlock(&fs_info->balance_lock);
4089
4090 goto do_balance;
4091 }
4092 } else {
4093 bargs = NULL;
4094 }
4095
4096 if (fs_info->balance_ctl) {
4097 ret = -EINPROGRESS;
4098 goto out_bargs;
4099 }
4100
4101 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4102 if (!bctl) {
4103 ret = -ENOMEM;
4104 goto out_bargs;
4105 }
4106
4107 if (arg) {
4108 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4109 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4110 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4111
4112 bctl->flags = bargs->flags;
4113 } else {
4114 /* balance everything - no filters */
4115 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4116 }
4117
4118 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4119 ret = -EINVAL;
4120 goto out_bctl;
4121 }
4122
4123 do_balance:
4124 /*
4125 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4126 * bctl is freed in reset_balance_state, or, if restriper was paused
4127 * all the way until unmount, in free_fs_info. The flag should be
4128 * cleared after reset_balance_state.
4129 */
4130 need_unlock = false;
4131
4132 ret = btrfs_balance(fs_info, bctl, bargs);
4133 bctl = NULL;
4134
4135 if ((ret == 0 || ret == -ECANCELED) && arg) {
4136 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4137 ret = -EFAULT;
4138 }
4139
4140 out_bctl:
4141 kfree(bctl);
4142 out_bargs:
4143 kfree(bargs);
4144 out_unlock:
4145 mutex_unlock(&fs_info->balance_mutex);
4146 if (need_unlock)
4147 btrfs_exclop_finish(fs_info);
4148 out:
4149 mnt_drop_write_file(file);
4150 return ret;
4151 }
4152
btrfs_ioctl_balance_ctl(struct btrfs_fs_info * fs_info,int cmd)4153 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4154 {
4155 if (!capable(CAP_SYS_ADMIN))
4156 return -EPERM;
4157
4158 switch (cmd) {
4159 case BTRFS_BALANCE_CTL_PAUSE:
4160 return btrfs_pause_balance(fs_info);
4161 case BTRFS_BALANCE_CTL_CANCEL:
4162 return btrfs_cancel_balance(fs_info);
4163 }
4164
4165 return -EINVAL;
4166 }
4167
btrfs_ioctl_balance_progress(struct btrfs_fs_info * fs_info,void __user * arg)4168 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4169 void __user *arg)
4170 {
4171 struct btrfs_ioctl_balance_args *bargs;
4172 int ret = 0;
4173
4174 if (!capable(CAP_SYS_ADMIN))
4175 return -EPERM;
4176
4177 mutex_lock(&fs_info->balance_mutex);
4178 if (!fs_info->balance_ctl) {
4179 ret = -ENOTCONN;
4180 goto out;
4181 }
4182
4183 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4184 if (!bargs) {
4185 ret = -ENOMEM;
4186 goto out;
4187 }
4188
4189 btrfs_update_ioctl_balance_args(fs_info, bargs);
4190
4191 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4192 ret = -EFAULT;
4193
4194 kfree(bargs);
4195 out:
4196 mutex_unlock(&fs_info->balance_mutex);
4197 return ret;
4198 }
4199
btrfs_ioctl_quota_ctl(struct file * file,void __user * arg)4200 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4201 {
4202 struct inode *inode = file_inode(file);
4203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4204 struct btrfs_ioctl_quota_ctl_args *sa;
4205 int ret;
4206
4207 if (!capable(CAP_SYS_ADMIN))
4208 return -EPERM;
4209
4210 ret = mnt_want_write_file(file);
4211 if (ret)
4212 return ret;
4213
4214 sa = memdup_user(arg, sizeof(*sa));
4215 if (IS_ERR(sa)) {
4216 ret = PTR_ERR(sa);
4217 goto drop_write;
4218 }
4219
4220 down_write(&fs_info->subvol_sem);
4221
4222 switch (sa->cmd) {
4223 case BTRFS_QUOTA_CTL_ENABLE:
4224 ret = btrfs_quota_enable(fs_info);
4225 break;
4226 case BTRFS_QUOTA_CTL_DISABLE:
4227 ret = btrfs_quota_disable(fs_info);
4228 break;
4229 default:
4230 ret = -EINVAL;
4231 break;
4232 }
4233
4234 kfree(sa);
4235 up_write(&fs_info->subvol_sem);
4236 drop_write:
4237 mnt_drop_write_file(file);
4238 return ret;
4239 }
4240
btrfs_ioctl_qgroup_assign(struct file * file,void __user * arg)4241 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4242 {
4243 struct inode *inode = file_inode(file);
4244 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4245 struct btrfs_root *root = BTRFS_I(inode)->root;
4246 struct btrfs_ioctl_qgroup_assign_args *sa;
4247 struct btrfs_trans_handle *trans;
4248 int ret;
4249 int err;
4250
4251 if (!capable(CAP_SYS_ADMIN))
4252 return -EPERM;
4253
4254 ret = mnt_want_write_file(file);
4255 if (ret)
4256 return ret;
4257
4258 sa = memdup_user(arg, sizeof(*sa));
4259 if (IS_ERR(sa)) {
4260 ret = PTR_ERR(sa);
4261 goto drop_write;
4262 }
4263
4264 trans = btrfs_join_transaction(root);
4265 if (IS_ERR(trans)) {
4266 ret = PTR_ERR(trans);
4267 goto out;
4268 }
4269
4270 if (sa->assign) {
4271 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4272 } else {
4273 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4274 }
4275
4276 /* update qgroup status and info */
4277 mutex_lock(&fs_info->qgroup_ioctl_lock);
4278 err = btrfs_run_qgroups(trans);
4279 mutex_unlock(&fs_info->qgroup_ioctl_lock);
4280 if (err < 0)
4281 btrfs_handle_fs_error(fs_info, err,
4282 "failed to update qgroup status and info");
4283 err = btrfs_end_transaction(trans);
4284 if (err && !ret)
4285 ret = err;
4286
4287 out:
4288 kfree(sa);
4289 drop_write:
4290 mnt_drop_write_file(file);
4291 return ret;
4292 }
4293
btrfs_ioctl_qgroup_create(struct file * file,void __user * arg)4294 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4295 {
4296 struct inode *inode = file_inode(file);
4297 struct btrfs_root *root = BTRFS_I(inode)->root;
4298 struct btrfs_ioctl_qgroup_create_args *sa;
4299 struct btrfs_trans_handle *trans;
4300 int ret;
4301 int err;
4302
4303 if (!capable(CAP_SYS_ADMIN))
4304 return -EPERM;
4305
4306 ret = mnt_want_write_file(file);
4307 if (ret)
4308 return ret;
4309
4310 sa = memdup_user(arg, sizeof(*sa));
4311 if (IS_ERR(sa)) {
4312 ret = PTR_ERR(sa);
4313 goto drop_write;
4314 }
4315
4316 if (!sa->qgroupid) {
4317 ret = -EINVAL;
4318 goto out;
4319 }
4320
4321 trans = btrfs_join_transaction(root);
4322 if (IS_ERR(trans)) {
4323 ret = PTR_ERR(trans);
4324 goto out;
4325 }
4326
4327 if (sa->create) {
4328 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4329 } else {
4330 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4331 }
4332
4333 err = btrfs_end_transaction(trans);
4334 if (err && !ret)
4335 ret = err;
4336
4337 out:
4338 kfree(sa);
4339 drop_write:
4340 mnt_drop_write_file(file);
4341 return ret;
4342 }
4343
btrfs_ioctl_qgroup_limit(struct file * file,void __user * arg)4344 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4345 {
4346 struct inode *inode = file_inode(file);
4347 struct btrfs_root *root = BTRFS_I(inode)->root;
4348 struct btrfs_ioctl_qgroup_limit_args *sa;
4349 struct btrfs_trans_handle *trans;
4350 int ret;
4351 int err;
4352 u64 qgroupid;
4353
4354 if (!capable(CAP_SYS_ADMIN))
4355 return -EPERM;
4356
4357 ret = mnt_want_write_file(file);
4358 if (ret)
4359 return ret;
4360
4361 sa = memdup_user(arg, sizeof(*sa));
4362 if (IS_ERR(sa)) {
4363 ret = PTR_ERR(sa);
4364 goto drop_write;
4365 }
4366
4367 trans = btrfs_join_transaction(root);
4368 if (IS_ERR(trans)) {
4369 ret = PTR_ERR(trans);
4370 goto out;
4371 }
4372
4373 qgroupid = sa->qgroupid;
4374 if (!qgroupid) {
4375 /* take the current subvol as qgroup */
4376 qgroupid = root->root_key.objectid;
4377 }
4378
4379 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
4380
4381 err = btrfs_end_transaction(trans);
4382 if (err && !ret)
4383 ret = err;
4384
4385 out:
4386 kfree(sa);
4387 drop_write:
4388 mnt_drop_write_file(file);
4389 return ret;
4390 }
4391
btrfs_ioctl_quota_rescan(struct file * file,void __user * arg)4392 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4393 {
4394 struct inode *inode = file_inode(file);
4395 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4396 struct btrfs_ioctl_quota_rescan_args *qsa;
4397 int ret;
4398
4399 if (!capable(CAP_SYS_ADMIN))
4400 return -EPERM;
4401
4402 ret = mnt_want_write_file(file);
4403 if (ret)
4404 return ret;
4405
4406 qsa = memdup_user(arg, sizeof(*qsa));
4407 if (IS_ERR(qsa)) {
4408 ret = PTR_ERR(qsa);
4409 goto drop_write;
4410 }
4411
4412 if (qsa->flags) {
4413 ret = -EINVAL;
4414 goto out;
4415 }
4416
4417 ret = btrfs_qgroup_rescan(fs_info);
4418
4419 out:
4420 kfree(qsa);
4421 drop_write:
4422 mnt_drop_write_file(file);
4423 return ret;
4424 }
4425
btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info * fs_info,void __user * arg)4426 static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4427 void __user *arg)
4428 {
4429 struct btrfs_ioctl_quota_rescan_args *qsa;
4430 int ret = 0;
4431
4432 if (!capable(CAP_SYS_ADMIN))
4433 return -EPERM;
4434
4435 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
4436 if (!qsa)
4437 return -ENOMEM;
4438
4439 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4440 qsa->flags = 1;
4441 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
4442 }
4443
4444 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4445 ret = -EFAULT;
4446
4447 kfree(qsa);
4448 return ret;
4449 }
4450
btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info * fs_info,void __user * arg)4451 static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4452 void __user *arg)
4453 {
4454 if (!capable(CAP_SYS_ADMIN))
4455 return -EPERM;
4456
4457 return btrfs_qgroup_wait_for_completion(fs_info, true);
4458 }
4459
_btrfs_ioctl_set_received_subvol(struct file * file,struct btrfs_ioctl_received_subvol_args * sa)4460 static long _btrfs_ioctl_set_received_subvol(struct file *file,
4461 struct btrfs_ioctl_received_subvol_args *sa)
4462 {
4463 struct inode *inode = file_inode(file);
4464 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4465 struct btrfs_root *root = BTRFS_I(inode)->root;
4466 struct btrfs_root_item *root_item = &root->root_item;
4467 struct btrfs_trans_handle *trans;
4468 struct timespec64 ct = current_time(inode);
4469 int ret = 0;
4470 int received_uuid_changed;
4471
4472 if (!inode_owner_or_capable(inode))
4473 return -EPERM;
4474
4475 ret = mnt_want_write_file(file);
4476 if (ret < 0)
4477 return ret;
4478
4479 down_write(&fs_info->subvol_sem);
4480
4481 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
4482 ret = -EINVAL;
4483 goto out;
4484 }
4485
4486 if (btrfs_root_readonly(root)) {
4487 ret = -EROFS;
4488 goto out;
4489 }
4490
4491 /*
4492 * 1 - root item
4493 * 2 - uuid items (received uuid + subvol uuid)
4494 */
4495 trans = btrfs_start_transaction(root, 3);
4496 if (IS_ERR(trans)) {
4497 ret = PTR_ERR(trans);
4498 trans = NULL;
4499 goto out;
4500 }
4501
4502 sa->rtransid = trans->transid;
4503 sa->rtime.sec = ct.tv_sec;
4504 sa->rtime.nsec = ct.tv_nsec;
4505
4506 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4507 BTRFS_UUID_SIZE);
4508 if (received_uuid_changed &&
4509 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4510 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
4511 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4512 root->root_key.objectid);
4513 if (ret && ret != -ENOENT) {
4514 btrfs_abort_transaction(trans, ret);
4515 btrfs_end_transaction(trans);
4516 goto out;
4517 }
4518 }
4519 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4520 btrfs_set_root_stransid(root_item, sa->stransid);
4521 btrfs_set_root_rtransid(root_item, sa->rtransid);
4522 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4523 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4524 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4525 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4526
4527 ret = btrfs_update_root(trans, fs_info->tree_root,
4528 &root->root_key, &root->root_item);
4529 if (ret < 0) {
4530 btrfs_end_transaction(trans);
4531 goto out;
4532 }
4533 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4534 ret = btrfs_uuid_tree_add(trans, sa->uuid,
4535 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4536 root->root_key.objectid);
4537 if (ret < 0 && ret != -EEXIST) {
4538 btrfs_abort_transaction(trans, ret);
4539 btrfs_end_transaction(trans);
4540 goto out;
4541 }
4542 }
4543 ret = btrfs_commit_transaction(trans);
4544 out:
4545 up_write(&fs_info->subvol_sem);
4546 mnt_drop_write_file(file);
4547 return ret;
4548 }
4549
4550 #ifdef CONFIG_64BIT
btrfs_ioctl_set_received_subvol_32(struct file * file,void __user * arg)4551 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4552 void __user *arg)
4553 {
4554 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4555 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4556 int ret = 0;
4557
4558 args32 = memdup_user(arg, sizeof(*args32));
4559 if (IS_ERR(args32))
4560 return PTR_ERR(args32);
4561
4562 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
4563 if (!args64) {
4564 ret = -ENOMEM;
4565 goto out;
4566 }
4567
4568 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4569 args64->stransid = args32->stransid;
4570 args64->rtransid = args32->rtransid;
4571 args64->stime.sec = args32->stime.sec;
4572 args64->stime.nsec = args32->stime.nsec;
4573 args64->rtime.sec = args32->rtime.sec;
4574 args64->rtime.nsec = args32->rtime.nsec;
4575 args64->flags = args32->flags;
4576
4577 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4578 if (ret)
4579 goto out;
4580
4581 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4582 args32->stransid = args64->stransid;
4583 args32->rtransid = args64->rtransid;
4584 args32->stime.sec = args64->stime.sec;
4585 args32->stime.nsec = args64->stime.nsec;
4586 args32->rtime.sec = args64->rtime.sec;
4587 args32->rtime.nsec = args64->rtime.nsec;
4588 args32->flags = args64->flags;
4589
4590 ret = copy_to_user(arg, args32, sizeof(*args32));
4591 if (ret)
4592 ret = -EFAULT;
4593
4594 out:
4595 kfree(args32);
4596 kfree(args64);
4597 return ret;
4598 }
4599 #endif
4600
btrfs_ioctl_set_received_subvol(struct file * file,void __user * arg)4601 static long btrfs_ioctl_set_received_subvol(struct file *file,
4602 void __user *arg)
4603 {
4604 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4605 int ret = 0;
4606
4607 sa = memdup_user(arg, sizeof(*sa));
4608 if (IS_ERR(sa))
4609 return PTR_ERR(sa);
4610
4611 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4612
4613 if (ret)
4614 goto out;
4615
4616 ret = copy_to_user(arg, sa, sizeof(*sa));
4617 if (ret)
4618 ret = -EFAULT;
4619
4620 out:
4621 kfree(sa);
4622 return ret;
4623 }
4624
btrfs_ioctl_get_fslabel(struct btrfs_fs_info * fs_info,void __user * arg)4625 static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4626 void __user *arg)
4627 {
4628 size_t len;
4629 int ret;
4630 char label[BTRFS_LABEL_SIZE];
4631
4632 spin_lock(&fs_info->super_lock);
4633 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4634 spin_unlock(&fs_info->super_lock);
4635
4636 len = strnlen(label, BTRFS_LABEL_SIZE);
4637
4638 if (len == BTRFS_LABEL_SIZE) {
4639 btrfs_warn(fs_info,
4640 "label is too long, return the first %zu bytes",
4641 --len);
4642 }
4643
4644 ret = copy_to_user(arg, label, len);
4645
4646 return ret ? -EFAULT : 0;
4647 }
4648
btrfs_ioctl_set_fslabel(struct file * file,void __user * arg)4649 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4650 {
4651 struct inode *inode = file_inode(file);
4652 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4653 struct btrfs_root *root = BTRFS_I(inode)->root;
4654 struct btrfs_super_block *super_block = fs_info->super_copy;
4655 struct btrfs_trans_handle *trans;
4656 char label[BTRFS_LABEL_SIZE];
4657 int ret;
4658
4659 if (!capable(CAP_SYS_ADMIN))
4660 return -EPERM;
4661
4662 if (copy_from_user(label, arg, sizeof(label)))
4663 return -EFAULT;
4664
4665 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4666 btrfs_err(fs_info,
4667 "unable to set label with more than %d bytes",
4668 BTRFS_LABEL_SIZE - 1);
4669 return -EINVAL;
4670 }
4671
4672 ret = mnt_want_write_file(file);
4673 if (ret)
4674 return ret;
4675
4676 trans = btrfs_start_transaction(root, 0);
4677 if (IS_ERR(trans)) {
4678 ret = PTR_ERR(trans);
4679 goto out_unlock;
4680 }
4681
4682 spin_lock(&fs_info->super_lock);
4683 strcpy(super_block->label, label);
4684 spin_unlock(&fs_info->super_lock);
4685 ret = btrfs_commit_transaction(trans);
4686
4687 out_unlock:
4688 mnt_drop_write_file(file);
4689 return ret;
4690 }
4691
4692 #define INIT_FEATURE_FLAGS(suffix) \
4693 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4694 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4695 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4696
btrfs_ioctl_get_supported_features(void __user * arg)4697 int btrfs_ioctl_get_supported_features(void __user *arg)
4698 {
4699 static const struct btrfs_ioctl_feature_flags features[3] = {
4700 INIT_FEATURE_FLAGS(SUPP),
4701 INIT_FEATURE_FLAGS(SAFE_SET),
4702 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4703 };
4704
4705 if (copy_to_user(arg, &features, sizeof(features)))
4706 return -EFAULT;
4707
4708 return 0;
4709 }
4710
btrfs_ioctl_get_features(struct btrfs_fs_info * fs_info,void __user * arg)4711 static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4712 void __user *arg)
4713 {
4714 struct btrfs_super_block *super_block = fs_info->super_copy;
4715 struct btrfs_ioctl_feature_flags features;
4716
4717 features.compat_flags = btrfs_super_compat_flags(super_block);
4718 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4719 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4720
4721 if (copy_to_user(arg, &features, sizeof(features)))
4722 return -EFAULT;
4723
4724 return 0;
4725 }
4726
check_feature_bits(struct btrfs_fs_info * fs_info,enum btrfs_feature_set set,u64 change_mask,u64 flags,u64 supported_flags,u64 safe_set,u64 safe_clear)4727 static int check_feature_bits(struct btrfs_fs_info *fs_info,
4728 enum btrfs_feature_set set,
4729 u64 change_mask, u64 flags, u64 supported_flags,
4730 u64 safe_set, u64 safe_clear)
4731 {
4732 const char *type = btrfs_feature_set_name(set);
4733 char *names;
4734 u64 disallowed, unsupported;
4735 u64 set_mask = flags & change_mask;
4736 u64 clear_mask = ~flags & change_mask;
4737
4738 unsupported = set_mask & ~supported_flags;
4739 if (unsupported) {
4740 names = btrfs_printable_features(set, unsupported);
4741 if (names) {
4742 btrfs_warn(fs_info,
4743 "this kernel does not support the %s feature bit%s",
4744 names, strchr(names, ',') ? "s" : "");
4745 kfree(names);
4746 } else
4747 btrfs_warn(fs_info,
4748 "this kernel does not support %s bits 0x%llx",
4749 type, unsupported);
4750 return -EOPNOTSUPP;
4751 }
4752
4753 disallowed = set_mask & ~safe_set;
4754 if (disallowed) {
4755 names = btrfs_printable_features(set, disallowed);
4756 if (names) {
4757 btrfs_warn(fs_info,
4758 "can't set the %s feature bit%s while mounted",
4759 names, strchr(names, ',') ? "s" : "");
4760 kfree(names);
4761 } else
4762 btrfs_warn(fs_info,
4763 "can't set %s bits 0x%llx while mounted",
4764 type, disallowed);
4765 return -EPERM;
4766 }
4767
4768 disallowed = clear_mask & ~safe_clear;
4769 if (disallowed) {
4770 names = btrfs_printable_features(set, disallowed);
4771 if (names) {
4772 btrfs_warn(fs_info,
4773 "can't clear the %s feature bit%s while mounted",
4774 names, strchr(names, ',') ? "s" : "");
4775 kfree(names);
4776 } else
4777 btrfs_warn(fs_info,
4778 "can't clear %s bits 0x%llx while mounted",
4779 type, disallowed);
4780 return -EPERM;
4781 }
4782
4783 return 0;
4784 }
4785
4786 #define check_feature(fs_info, change_mask, flags, mask_base) \
4787 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
4788 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4789 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4790 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4791
btrfs_ioctl_set_features(struct file * file,void __user * arg)4792 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4793 {
4794 struct inode *inode = file_inode(file);
4795 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4796 struct btrfs_root *root = BTRFS_I(inode)->root;
4797 struct btrfs_super_block *super_block = fs_info->super_copy;
4798 struct btrfs_ioctl_feature_flags flags[2];
4799 struct btrfs_trans_handle *trans;
4800 u64 newflags;
4801 int ret;
4802
4803 if (!capable(CAP_SYS_ADMIN))
4804 return -EPERM;
4805
4806 if (copy_from_user(flags, arg, sizeof(flags)))
4807 return -EFAULT;
4808
4809 /* Nothing to do */
4810 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4811 !flags[0].incompat_flags)
4812 return 0;
4813
4814 ret = check_feature(fs_info, flags[0].compat_flags,
4815 flags[1].compat_flags, COMPAT);
4816 if (ret)
4817 return ret;
4818
4819 ret = check_feature(fs_info, flags[0].compat_ro_flags,
4820 flags[1].compat_ro_flags, COMPAT_RO);
4821 if (ret)
4822 return ret;
4823
4824 ret = check_feature(fs_info, flags[0].incompat_flags,
4825 flags[1].incompat_flags, INCOMPAT);
4826 if (ret)
4827 return ret;
4828
4829 ret = mnt_want_write_file(file);
4830 if (ret)
4831 return ret;
4832
4833 trans = btrfs_start_transaction(root, 0);
4834 if (IS_ERR(trans)) {
4835 ret = PTR_ERR(trans);
4836 goto out_drop_write;
4837 }
4838
4839 spin_lock(&fs_info->super_lock);
4840 newflags = btrfs_super_compat_flags(super_block);
4841 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4842 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4843 btrfs_set_super_compat_flags(super_block, newflags);
4844
4845 newflags = btrfs_super_compat_ro_flags(super_block);
4846 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4847 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4848 btrfs_set_super_compat_ro_flags(super_block, newflags);
4849
4850 newflags = btrfs_super_incompat_flags(super_block);
4851 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4852 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4853 btrfs_set_super_incompat_flags(super_block, newflags);
4854 spin_unlock(&fs_info->super_lock);
4855
4856 ret = btrfs_commit_transaction(trans);
4857 out_drop_write:
4858 mnt_drop_write_file(file);
4859
4860 return ret;
4861 }
4862
_btrfs_ioctl_send(struct file * file,void __user * argp,bool compat)4863 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4864 {
4865 struct btrfs_ioctl_send_args *arg;
4866 int ret;
4867
4868 if (compat) {
4869 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4870 struct btrfs_ioctl_send_args_32 args32 = { 0 };
4871
4872 ret = copy_from_user(&args32, argp, sizeof(args32));
4873 if (ret)
4874 return -EFAULT;
4875 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4876 if (!arg)
4877 return -ENOMEM;
4878 arg->send_fd = args32.send_fd;
4879 arg->clone_sources_count = args32.clone_sources_count;
4880 arg->clone_sources = compat_ptr(args32.clone_sources);
4881 arg->parent_root = args32.parent_root;
4882 arg->flags = args32.flags;
4883 memcpy(arg->reserved, args32.reserved,
4884 sizeof(args32.reserved));
4885 #else
4886 return -ENOTTY;
4887 #endif
4888 } else {
4889 arg = memdup_user(argp, sizeof(*arg));
4890 if (IS_ERR(arg))
4891 return PTR_ERR(arg);
4892 }
4893 ret = btrfs_ioctl_send(file, arg);
4894 kfree(arg);
4895 return ret;
4896 }
4897
btrfs_ioctl(struct file * file,unsigned int cmd,unsigned long arg)4898 long btrfs_ioctl(struct file *file, unsigned int
4899 cmd, unsigned long arg)
4900 {
4901 struct inode *inode = file_inode(file);
4902 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4903 struct btrfs_root *root = BTRFS_I(inode)->root;
4904 void __user *argp = (void __user *)arg;
4905
4906 switch (cmd) {
4907 case FS_IOC_GETFLAGS:
4908 return btrfs_ioctl_getflags(file, argp);
4909 case FS_IOC_SETFLAGS:
4910 return btrfs_ioctl_setflags(file, argp);
4911 case FS_IOC_GETVERSION:
4912 return btrfs_ioctl_getversion(file, argp);
4913 case FS_IOC_GETFSLABEL:
4914 return btrfs_ioctl_get_fslabel(fs_info, argp);
4915 case FS_IOC_SETFSLABEL:
4916 return btrfs_ioctl_set_fslabel(file, argp);
4917 case FITRIM:
4918 return btrfs_ioctl_fitrim(fs_info, argp);
4919 case BTRFS_IOC_SNAP_CREATE:
4920 return btrfs_ioctl_snap_create(file, argp, 0);
4921 case BTRFS_IOC_SNAP_CREATE_V2:
4922 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4923 case BTRFS_IOC_SUBVOL_CREATE:
4924 return btrfs_ioctl_snap_create(file, argp, 1);
4925 case BTRFS_IOC_SUBVOL_CREATE_V2:
4926 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4927 case BTRFS_IOC_SNAP_DESTROY:
4928 return btrfs_ioctl_snap_destroy(file, argp, false);
4929 case BTRFS_IOC_SNAP_DESTROY_V2:
4930 return btrfs_ioctl_snap_destroy(file, argp, true);
4931 case BTRFS_IOC_SUBVOL_GETFLAGS:
4932 return btrfs_ioctl_subvol_getflags(file, argp);
4933 case BTRFS_IOC_SUBVOL_SETFLAGS:
4934 return btrfs_ioctl_subvol_setflags(file, argp);
4935 case BTRFS_IOC_DEFAULT_SUBVOL:
4936 return btrfs_ioctl_default_subvol(file, argp);
4937 case BTRFS_IOC_DEFRAG:
4938 return btrfs_ioctl_defrag(file, NULL);
4939 case BTRFS_IOC_DEFRAG_RANGE:
4940 return btrfs_ioctl_defrag(file, argp);
4941 case BTRFS_IOC_RESIZE:
4942 return btrfs_ioctl_resize(file, argp);
4943 case BTRFS_IOC_ADD_DEV:
4944 return btrfs_ioctl_add_dev(fs_info, argp);
4945 case BTRFS_IOC_RM_DEV:
4946 return btrfs_ioctl_rm_dev(file, argp);
4947 case BTRFS_IOC_RM_DEV_V2:
4948 return btrfs_ioctl_rm_dev_v2(file, argp);
4949 case BTRFS_IOC_FS_INFO:
4950 return btrfs_ioctl_fs_info(fs_info, argp);
4951 case BTRFS_IOC_DEV_INFO:
4952 return btrfs_ioctl_dev_info(fs_info, argp);
4953 case BTRFS_IOC_BALANCE:
4954 return btrfs_ioctl_balance(file, NULL);
4955 case BTRFS_IOC_TREE_SEARCH:
4956 return btrfs_ioctl_tree_search(file, argp);
4957 case BTRFS_IOC_TREE_SEARCH_V2:
4958 return btrfs_ioctl_tree_search_v2(file, argp);
4959 case BTRFS_IOC_INO_LOOKUP:
4960 return btrfs_ioctl_ino_lookup(file, argp);
4961 case BTRFS_IOC_INO_PATHS:
4962 return btrfs_ioctl_ino_to_path(root, argp);
4963 case BTRFS_IOC_LOGICAL_INO:
4964 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4965 case BTRFS_IOC_LOGICAL_INO_V2:
4966 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
4967 case BTRFS_IOC_SPACE_INFO:
4968 return btrfs_ioctl_space_info(fs_info, argp);
4969 case BTRFS_IOC_SYNC: {
4970 int ret;
4971
4972 ret = btrfs_start_delalloc_roots(fs_info, U64_MAX, false);
4973 if (ret)
4974 return ret;
4975 ret = btrfs_sync_fs(inode->i_sb, 1);
4976 /*
4977 * The transaction thread may want to do more work,
4978 * namely it pokes the cleaner kthread that will start
4979 * processing uncleaned subvols.
4980 */
4981 wake_up_process(fs_info->transaction_kthread);
4982 return ret;
4983 }
4984 case BTRFS_IOC_START_SYNC:
4985 return btrfs_ioctl_start_sync(root, argp);
4986 case BTRFS_IOC_WAIT_SYNC:
4987 return btrfs_ioctl_wait_sync(fs_info, argp);
4988 case BTRFS_IOC_SCRUB:
4989 return btrfs_ioctl_scrub(file, argp);
4990 case BTRFS_IOC_SCRUB_CANCEL:
4991 return btrfs_ioctl_scrub_cancel(fs_info);
4992 case BTRFS_IOC_SCRUB_PROGRESS:
4993 return btrfs_ioctl_scrub_progress(fs_info, argp);
4994 case BTRFS_IOC_BALANCE_V2:
4995 return btrfs_ioctl_balance(file, argp);
4996 case BTRFS_IOC_BALANCE_CTL:
4997 return btrfs_ioctl_balance_ctl(fs_info, arg);
4998 case BTRFS_IOC_BALANCE_PROGRESS:
4999 return btrfs_ioctl_balance_progress(fs_info, argp);
5000 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5001 return btrfs_ioctl_set_received_subvol(file, argp);
5002 #ifdef CONFIG_64BIT
5003 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5004 return btrfs_ioctl_set_received_subvol_32(file, argp);
5005 #endif
5006 case BTRFS_IOC_SEND:
5007 return _btrfs_ioctl_send(file, argp, false);
5008 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5009 case BTRFS_IOC_SEND_32:
5010 return _btrfs_ioctl_send(file, argp, true);
5011 #endif
5012 case BTRFS_IOC_GET_DEV_STATS:
5013 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5014 case BTRFS_IOC_QUOTA_CTL:
5015 return btrfs_ioctl_quota_ctl(file, argp);
5016 case BTRFS_IOC_QGROUP_ASSIGN:
5017 return btrfs_ioctl_qgroup_assign(file, argp);
5018 case BTRFS_IOC_QGROUP_CREATE:
5019 return btrfs_ioctl_qgroup_create(file, argp);
5020 case BTRFS_IOC_QGROUP_LIMIT:
5021 return btrfs_ioctl_qgroup_limit(file, argp);
5022 case BTRFS_IOC_QUOTA_RESCAN:
5023 return btrfs_ioctl_quota_rescan(file, argp);
5024 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5025 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
5026 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5027 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
5028 case BTRFS_IOC_DEV_REPLACE:
5029 return btrfs_ioctl_dev_replace(fs_info, argp);
5030 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5031 return btrfs_ioctl_get_supported_features(argp);
5032 case BTRFS_IOC_GET_FEATURES:
5033 return btrfs_ioctl_get_features(fs_info, argp);
5034 case BTRFS_IOC_SET_FEATURES:
5035 return btrfs_ioctl_set_features(file, argp);
5036 case FS_IOC_FSGETXATTR:
5037 return btrfs_ioctl_fsgetxattr(file, argp);
5038 case FS_IOC_FSSETXATTR:
5039 return btrfs_ioctl_fssetxattr(file, argp);
5040 case BTRFS_IOC_GET_SUBVOL_INFO:
5041 return btrfs_ioctl_get_subvol_info(file, argp);
5042 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5043 return btrfs_ioctl_get_subvol_rootref(file, argp);
5044 case BTRFS_IOC_INO_LOOKUP_USER:
5045 return btrfs_ioctl_ino_lookup_user(file, argp);
5046 }
5047
5048 return -ENOTTY;
5049 }
5050
5051 #ifdef CONFIG_COMPAT
btrfs_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)5052 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5053 {
5054 /*
5055 * These all access 32-bit values anyway so no further
5056 * handling is necessary.
5057 */
5058 switch (cmd) {
5059 case FS_IOC32_GETFLAGS:
5060 cmd = FS_IOC_GETFLAGS;
5061 break;
5062 case FS_IOC32_SETFLAGS:
5063 cmd = FS_IOC_SETFLAGS;
5064 break;
5065 case FS_IOC32_GETVERSION:
5066 cmd = FS_IOC_GETVERSION;
5067 break;
5068 }
5069
5070 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5071 }
5072 #endif
5073