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