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