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