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