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