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