]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/btrfs/ioctl.c
btrfs: Push mnt_want_write() outside of i_mutex
[karo-tx-linux.git] / 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 "compat.h"
45 #include "ctree.h"
46 #include "disk-io.h"
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "ioctl.h"
50 #include "print-tree.h"
51 #include "volumes.h"
52 #include "locking.h"
53 #include "inode-map.h"
54 #include "backref.h"
55 #include "rcu-string.h"
56
57 /* Mask out flags that are inappropriate for the given type of inode. */
58 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
59 {
60         if (S_ISDIR(mode))
61                 return flags;
62         else if (S_ISREG(mode))
63                 return flags & ~FS_DIRSYNC_FL;
64         else
65                 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
66 }
67
68 /*
69  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
70  */
71 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
72 {
73         unsigned int iflags = 0;
74
75         if (flags & BTRFS_INODE_SYNC)
76                 iflags |= FS_SYNC_FL;
77         if (flags & BTRFS_INODE_IMMUTABLE)
78                 iflags |= FS_IMMUTABLE_FL;
79         if (flags & BTRFS_INODE_APPEND)
80                 iflags |= FS_APPEND_FL;
81         if (flags & BTRFS_INODE_NODUMP)
82                 iflags |= FS_NODUMP_FL;
83         if (flags & BTRFS_INODE_NOATIME)
84                 iflags |= FS_NOATIME_FL;
85         if (flags & BTRFS_INODE_DIRSYNC)
86                 iflags |= FS_DIRSYNC_FL;
87         if (flags & BTRFS_INODE_NODATACOW)
88                 iflags |= FS_NOCOW_FL;
89
90         if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
91                 iflags |= FS_COMPR_FL;
92         else if (flags & BTRFS_INODE_NOCOMPRESS)
93                 iflags |= FS_NOCOMP_FL;
94
95         return iflags;
96 }
97
98 /*
99  * Update inode->i_flags based on the btrfs internal flags.
100  */
101 void btrfs_update_iflags(struct inode *inode)
102 {
103         struct btrfs_inode *ip = BTRFS_I(inode);
104
105         inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
106
107         if (ip->flags & BTRFS_INODE_SYNC)
108                 inode->i_flags |= S_SYNC;
109         if (ip->flags & BTRFS_INODE_IMMUTABLE)
110                 inode->i_flags |= S_IMMUTABLE;
111         if (ip->flags & BTRFS_INODE_APPEND)
112                 inode->i_flags |= S_APPEND;
113         if (ip->flags & BTRFS_INODE_NOATIME)
114                 inode->i_flags |= S_NOATIME;
115         if (ip->flags & BTRFS_INODE_DIRSYNC)
116                 inode->i_flags |= S_DIRSYNC;
117 }
118
119 /*
120  * Inherit flags from the parent inode.
121  *
122  * Currently only the compression flags and the cow flags are inherited.
123  */
124 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
125 {
126         unsigned int flags;
127
128         if (!dir)
129                 return;
130
131         flags = BTRFS_I(dir)->flags;
132
133         if (flags & BTRFS_INODE_NOCOMPRESS) {
134                 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
135                 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
136         } else if (flags & BTRFS_INODE_COMPRESS) {
137                 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
138                 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
139         }
140
141         if (flags & BTRFS_INODE_NODATACOW)
142                 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
143
144         btrfs_update_iflags(inode);
145 }
146
147 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
148 {
149         struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
150         unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
151
152         if (copy_to_user(arg, &flags, sizeof(flags)))
153                 return -EFAULT;
154         return 0;
155 }
156
157 static int check_flags(unsigned int flags)
158 {
159         if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
160                       FS_NOATIME_FL | FS_NODUMP_FL | \
161                       FS_SYNC_FL | FS_DIRSYNC_FL | \
162                       FS_NOCOMP_FL | FS_COMPR_FL |
163                       FS_NOCOW_FL))
164                 return -EOPNOTSUPP;
165
166         if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
167                 return -EINVAL;
168
169         return 0;
170 }
171
172 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
173 {
174         struct inode *inode = file->f_path.dentry->d_inode;
175         struct btrfs_inode *ip = BTRFS_I(inode);
176         struct btrfs_root *root = ip->root;
177         struct btrfs_trans_handle *trans;
178         unsigned int flags, oldflags;
179         int ret;
180         u64 ip_oldflags;
181         unsigned int i_oldflags;
182
183         if (btrfs_root_readonly(root))
184                 return -EROFS;
185
186         if (copy_from_user(&flags, arg, sizeof(flags)))
187                 return -EFAULT;
188
189         ret = check_flags(flags);
190         if (ret)
191                 return ret;
192
193         if (!inode_owner_or_capable(inode))
194                 return -EACCES;
195
196         ret = mnt_want_write_file(file);
197         if (ret)
198                 return ret;
199
200         mutex_lock(&inode->i_mutex);
201
202         ip_oldflags = ip->flags;
203         i_oldflags = inode->i_flags;
204
205         flags = btrfs_mask_flags(inode->i_mode, flags);
206         oldflags = btrfs_flags_to_ioctl(ip->flags);
207         if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
208                 if (!capable(CAP_LINUX_IMMUTABLE)) {
209                         ret = -EPERM;
210                         goto out_unlock;
211                 }
212         }
213
214         if (flags & FS_SYNC_FL)
215                 ip->flags |= BTRFS_INODE_SYNC;
216         else
217                 ip->flags &= ~BTRFS_INODE_SYNC;
218         if (flags & FS_IMMUTABLE_FL)
219                 ip->flags |= BTRFS_INODE_IMMUTABLE;
220         else
221                 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
222         if (flags & FS_APPEND_FL)
223                 ip->flags |= BTRFS_INODE_APPEND;
224         else
225                 ip->flags &= ~BTRFS_INODE_APPEND;
226         if (flags & FS_NODUMP_FL)
227                 ip->flags |= BTRFS_INODE_NODUMP;
228         else
229                 ip->flags &= ~BTRFS_INODE_NODUMP;
230         if (flags & FS_NOATIME_FL)
231                 ip->flags |= BTRFS_INODE_NOATIME;
232         else
233                 ip->flags &= ~BTRFS_INODE_NOATIME;
234         if (flags & FS_DIRSYNC_FL)
235                 ip->flags |= BTRFS_INODE_DIRSYNC;
236         else
237                 ip->flags &= ~BTRFS_INODE_DIRSYNC;
238         if (flags & FS_NOCOW_FL)
239                 ip->flags |= BTRFS_INODE_NODATACOW;
240         else
241                 ip->flags &= ~BTRFS_INODE_NODATACOW;
242
243         /*
244          * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
245          * flag may be changed automatically if compression code won't make
246          * things smaller.
247          */
248         if (flags & FS_NOCOMP_FL) {
249                 ip->flags &= ~BTRFS_INODE_COMPRESS;
250                 ip->flags |= BTRFS_INODE_NOCOMPRESS;
251         } else if (flags & FS_COMPR_FL) {
252                 ip->flags |= BTRFS_INODE_COMPRESS;
253                 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
254         } else {
255                 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
256         }
257
258         trans = btrfs_start_transaction(root, 1);
259         if (IS_ERR(trans)) {
260                 ret = PTR_ERR(trans);
261                 goto out_drop;
262         }
263
264         btrfs_update_iflags(inode);
265         inode_inc_iversion(inode);
266         inode->i_ctime = CURRENT_TIME;
267         ret = btrfs_update_inode(trans, root, inode);
268
269         btrfs_end_transaction(trans, root);
270  out_drop:
271         if (ret) {
272                 ip->flags = ip_oldflags;
273                 inode->i_flags = i_oldflags;
274         }
275
276  out_unlock:
277         mutex_unlock(&inode->i_mutex);
278         mnt_drop_write_file(file);
279         return ret;
280 }
281
282 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
283 {
284         struct inode *inode = file->f_path.dentry->d_inode;
285
286         return put_user(inode->i_generation, arg);
287 }
288
289 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
290 {
291         struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
292         struct btrfs_device *device;
293         struct request_queue *q;
294         struct fstrim_range range;
295         u64 minlen = ULLONG_MAX;
296         u64 num_devices = 0;
297         u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
298         int ret;
299
300         if (!capable(CAP_SYS_ADMIN))
301                 return -EPERM;
302
303         rcu_read_lock();
304         list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
305                                 dev_list) {
306                 if (!device->bdev)
307                         continue;
308                 q = bdev_get_queue(device->bdev);
309                 if (blk_queue_discard(q)) {
310                         num_devices++;
311                         minlen = min((u64)q->limits.discard_granularity,
312                                      minlen);
313                 }
314         }
315         rcu_read_unlock();
316
317         if (!num_devices)
318                 return -EOPNOTSUPP;
319         if (copy_from_user(&range, arg, sizeof(range)))
320                 return -EFAULT;
321         if (range.start > total_bytes)
322                 return -EINVAL;
323
324         range.len = min(range.len, total_bytes - range.start);
325         range.minlen = max(range.minlen, minlen);
326         ret = btrfs_trim_fs(fs_info->tree_root, &range);
327         if (ret < 0)
328                 return ret;
329
330         if (copy_to_user(arg, &range, sizeof(range)))
331                 return -EFAULT;
332
333         return 0;
334 }
335
336 static noinline int create_subvol(struct btrfs_root *root,
337                                   struct dentry *dentry,
338                                   char *name, int namelen,
339                                   u64 *async_transid)
340 {
341         struct btrfs_trans_handle *trans;
342         struct btrfs_key key;
343         struct btrfs_root_item root_item;
344         struct btrfs_inode_item *inode_item;
345         struct extent_buffer *leaf;
346         struct btrfs_root *new_root;
347         struct dentry *parent = dentry->d_parent;
348         struct inode *dir;
349         int ret;
350         int err;
351         u64 objectid;
352         u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
353         u64 index = 0;
354
355         ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
356         if (ret)
357                 return ret;
358
359         dir = parent->d_inode;
360
361         /*
362          * 1 - inode item
363          * 2 - refs
364          * 1 - root item
365          * 2 - dir items
366          */
367         trans = btrfs_start_transaction(root, 6);
368         if (IS_ERR(trans))
369                 return PTR_ERR(trans);
370
371         leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
372                                       0, objectid, NULL, 0, 0, 0);
373         if (IS_ERR(leaf)) {
374                 ret = PTR_ERR(leaf);
375                 goto fail;
376         }
377
378         memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
379         btrfs_set_header_bytenr(leaf, leaf->start);
380         btrfs_set_header_generation(leaf, trans->transid);
381         btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
382         btrfs_set_header_owner(leaf, objectid);
383
384         write_extent_buffer(leaf, root->fs_info->fsid,
385                             (unsigned long)btrfs_header_fsid(leaf),
386                             BTRFS_FSID_SIZE);
387         write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
388                             (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
389                             BTRFS_UUID_SIZE);
390         btrfs_mark_buffer_dirty(leaf);
391
392         inode_item = &root_item.inode;
393         memset(inode_item, 0, sizeof(*inode_item));
394         inode_item->generation = cpu_to_le64(1);
395         inode_item->size = cpu_to_le64(3);
396         inode_item->nlink = cpu_to_le32(1);
397         inode_item->nbytes = cpu_to_le64(root->leafsize);
398         inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
399
400         root_item.flags = 0;
401         root_item.byte_limit = 0;
402         inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
403
404         btrfs_set_root_bytenr(&root_item, leaf->start);
405         btrfs_set_root_generation(&root_item, trans->transid);
406         btrfs_set_root_level(&root_item, 0);
407         btrfs_set_root_refs(&root_item, 1);
408         btrfs_set_root_used(&root_item, leaf->len);
409         btrfs_set_root_last_snapshot(&root_item, 0);
410
411         memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
412         root_item.drop_level = 0;
413
414         btrfs_tree_unlock(leaf);
415         free_extent_buffer(leaf);
416         leaf = NULL;
417
418         btrfs_set_root_dirid(&root_item, new_dirid);
419
420         key.objectid = objectid;
421         key.offset = 0;
422         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
423         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
424                                 &root_item);
425         if (ret)
426                 goto fail;
427
428         key.offset = (u64)-1;
429         new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
430         if (IS_ERR(new_root)) {
431                 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
432                 ret = PTR_ERR(new_root);
433                 goto fail;
434         }
435
436         btrfs_record_root_in_trans(trans, new_root);
437
438         ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
439         if (ret) {
440                 /* We potentially lose an unused inode item here */
441                 btrfs_abort_transaction(trans, root, ret);
442                 goto fail;
443         }
444
445         /*
446          * insert the directory item
447          */
448         ret = btrfs_set_inode_index(dir, &index);
449         if (ret) {
450                 btrfs_abort_transaction(trans, root, ret);
451                 goto fail;
452         }
453
454         ret = btrfs_insert_dir_item(trans, root,
455                                     name, namelen, dir, &key,
456                                     BTRFS_FT_DIR, index);
457         if (ret) {
458                 btrfs_abort_transaction(trans, root, ret);
459                 goto fail;
460         }
461
462         btrfs_i_size_write(dir, dir->i_size + namelen * 2);
463         ret = btrfs_update_inode(trans, root, dir);
464         BUG_ON(ret);
465
466         ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
467                                  objectid, root->root_key.objectid,
468                                  btrfs_ino(dir), index, name, namelen);
469
470         BUG_ON(ret);
471
472         d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
473 fail:
474         if (async_transid) {
475                 *async_transid = trans->transid;
476                 err = btrfs_commit_transaction_async(trans, root, 1);
477         } else {
478                 err = btrfs_commit_transaction(trans, root);
479         }
480         if (err && !ret)
481                 ret = err;
482         return ret;
483 }
484
485 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
486                            char *name, int namelen, u64 *async_transid,
487                            bool readonly)
488 {
489         struct inode *inode;
490         struct btrfs_pending_snapshot *pending_snapshot;
491         struct btrfs_trans_handle *trans;
492         int ret;
493
494         if (!root->ref_cows)
495                 return -EINVAL;
496
497         pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
498         if (!pending_snapshot)
499                 return -ENOMEM;
500
501         btrfs_init_block_rsv(&pending_snapshot->block_rsv);
502         pending_snapshot->dentry = dentry;
503         pending_snapshot->root = root;
504         pending_snapshot->readonly = readonly;
505
506         trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
507         if (IS_ERR(trans)) {
508                 ret = PTR_ERR(trans);
509                 goto fail;
510         }
511
512         ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
513         BUG_ON(ret);
514
515         spin_lock(&root->fs_info->trans_lock);
516         list_add(&pending_snapshot->list,
517                  &trans->transaction->pending_snapshots);
518         spin_unlock(&root->fs_info->trans_lock);
519         if (async_transid) {
520                 *async_transid = trans->transid;
521                 ret = btrfs_commit_transaction_async(trans,
522                                      root->fs_info->extent_root, 1);
523         } else {
524                 ret = btrfs_commit_transaction(trans,
525                                                root->fs_info->extent_root);
526         }
527         BUG_ON(ret);
528
529         ret = pending_snapshot->error;
530         if (ret)
531                 goto fail;
532
533         ret = btrfs_orphan_cleanup(pending_snapshot->snap);
534         if (ret)
535                 goto fail;
536
537         inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
538         if (IS_ERR(inode)) {
539                 ret = PTR_ERR(inode);
540                 goto fail;
541         }
542         BUG_ON(!inode);
543         d_instantiate(dentry, inode);
544         ret = 0;
545 fail:
546         kfree(pending_snapshot);
547         return ret;
548 }
549
550 /*  copy of check_sticky in fs/namei.c()
551 * It's inline, so penalty for filesystems that don't use sticky bit is
552 * minimal.
553 */
554 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
555 {
556         uid_t fsuid = current_fsuid();
557
558         if (!(dir->i_mode & S_ISVTX))
559                 return 0;
560         if (inode->i_uid == fsuid)
561                 return 0;
562         if (dir->i_uid == fsuid)
563                 return 0;
564         return !capable(CAP_FOWNER);
565 }
566
567 /*  copy of may_delete in fs/namei.c()
568  *      Check whether we can remove a link victim from directory dir, check
569  *  whether the type of victim is right.
570  *  1. We can't do it if dir is read-only (done in permission())
571  *  2. We should have write and exec permissions on dir
572  *  3. We can't remove anything from append-only dir
573  *  4. We can't do anything with immutable dir (done in permission())
574  *  5. If the sticky bit on dir is set we should either
575  *      a. be owner of dir, or
576  *      b. be owner of victim, or
577  *      c. have CAP_FOWNER capability
578  *  6. If the victim is append-only or immutable we can't do antyhing with
579  *     links pointing to it.
580  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
581  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
582  *  9. We can't remove a root or mountpoint.
583  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
584  *     nfs_async_unlink().
585  */
586
587 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
588 {
589         int error;
590
591         if (!victim->d_inode)
592                 return -ENOENT;
593
594         BUG_ON(victim->d_parent->d_inode != dir);
595         audit_inode_child(victim, dir);
596
597         error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
598         if (error)
599                 return error;
600         if (IS_APPEND(dir))
601                 return -EPERM;
602         if (btrfs_check_sticky(dir, victim->d_inode)||
603                 IS_APPEND(victim->d_inode)||
604             IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
605                 return -EPERM;
606         if (isdir) {
607                 if (!S_ISDIR(victim->d_inode->i_mode))
608                         return -ENOTDIR;
609                 if (IS_ROOT(victim))
610                         return -EBUSY;
611         } else if (S_ISDIR(victim->d_inode->i_mode))
612                 return -EISDIR;
613         if (IS_DEADDIR(dir))
614                 return -ENOENT;
615         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
616                 return -EBUSY;
617         return 0;
618 }
619
620 /* copy of may_create in fs/namei.c() */
621 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
622 {
623         if (child->d_inode)
624                 return -EEXIST;
625         if (IS_DEADDIR(dir))
626                 return -ENOENT;
627         return inode_permission(dir, MAY_WRITE | MAY_EXEC);
628 }
629
630 /*
631  * Create a new subvolume below @parent.  This is largely modeled after
632  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
633  * inside this filesystem so it's quite a bit simpler.
634  */
635 static noinline int btrfs_mksubvol(struct path *parent,
636                                    char *name, int namelen,
637                                    struct btrfs_root *snap_src,
638                                    u64 *async_transid, bool readonly)
639 {
640         struct inode *dir  = parent->dentry->d_inode;
641         struct dentry *dentry;
642         int error;
643
644         error = mnt_want_write(parent->mnt);
645         if (error)
646                 return error;
647
648         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
649
650         dentry = lookup_one_len(name, parent->dentry, namelen);
651         error = PTR_ERR(dentry);
652         if (IS_ERR(dentry))
653                 goto out_unlock;
654
655         error = -EEXIST;
656         if (dentry->d_inode)
657                 goto out_dput;
658
659         error = btrfs_may_create(dir, dentry);
660         if (error)
661                 goto out_dput;
662
663         down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
664
665         if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
666                 goto out_up_read;
667
668         if (snap_src) {
669                 error = create_snapshot(snap_src, dentry,
670                                         name, namelen, async_transid, readonly);
671         } else {
672                 error = create_subvol(BTRFS_I(dir)->root, dentry,
673                                       name, namelen, async_transid);
674         }
675         if (!error)
676                 fsnotify_mkdir(dir, dentry);
677 out_up_read:
678         up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
679 out_dput:
680         dput(dentry);
681 out_unlock:
682         mutex_unlock(&dir->i_mutex);
683         mnt_drop_write(parent->mnt);
684         return error;
685 }
686
687 /*
688  * When we're defragging a range, we don't want to kick it off again
689  * if it is really just waiting for delalloc to send it down.
690  * If we find a nice big extent or delalloc range for the bytes in the
691  * file you want to defrag, we return 0 to let you know to skip this
692  * part of the file
693  */
694 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
695 {
696         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
697         struct extent_map *em = NULL;
698         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
699         u64 end;
700
701         read_lock(&em_tree->lock);
702         em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
703         read_unlock(&em_tree->lock);
704
705         if (em) {
706                 end = extent_map_end(em);
707                 free_extent_map(em);
708                 if (end - offset > thresh)
709                         return 0;
710         }
711         /* if we already have a nice delalloc here, just stop */
712         thresh /= 2;
713         end = count_range_bits(io_tree, &offset, offset + thresh,
714                                thresh, EXTENT_DELALLOC, 1);
715         if (end >= thresh)
716                 return 0;
717         return 1;
718 }
719
720 /*
721  * helper function to walk through a file and find extents
722  * newer than a specific transid, and smaller than thresh.
723  *
724  * This is used by the defragging code to find new and small
725  * extents
726  */
727 static int find_new_extents(struct btrfs_root *root,
728                             struct inode *inode, u64 newer_than,
729                             u64 *off, int thresh)
730 {
731         struct btrfs_path *path;
732         struct btrfs_key min_key;
733         struct btrfs_key max_key;
734         struct extent_buffer *leaf;
735         struct btrfs_file_extent_item *extent;
736         int type;
737         int ret;
738         u64 ino = btrfs_ino(inode);
739
740         path = btrfs_alloc_path();
741         if (!path)
742                 return -ENOMEM;
743
744         min_key.objectid = ino;
745         min_key.type = BTRFS_EXTENT_DATA_KEY;
746         min_key.offset = *off;
747
748         max_key.objectid = ino;
749         max_key.type = (u8)-1;
750         max_key.offset = (u64)-1;
751
752         path->keep_locks = 1;
753
754         while(1) {
755                 ret = btrfs_search_forward(root, &min_key, &max_key,
756                                            path, 0, newer_than);
757                 if (ret != 0)
758                         goto none;
759                 if (min_key.objectid != ino)
760                         goto none;
761                 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
762                         goto none;
763
764                 leaf = path->nodes[0];
765                 extent = btrfs_item_ptr(leaf, path->slots[0],
766                                         struct btrfs_file_extent_item);
767
768                 type = btrfs_file_extent_type(leaf, extent);
769                 if (type == BTRFS_FILE_EXTENT_REG &&
770                     btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
771                     check_defrag_in_cache(inode, min_key.offset, thresh)) {
772                         *off = min_key.offset;
773                         btrfs_free_path(path);
774                         return 0;
775                 }
776
777                 if (min_key.offset == (u64)-1)
778                         goto none;
779
780                 min_key.offset++;
781                 btrfs_release_path(path);
782         }
783 none:
784         btrfs_free_path(path);
785         return -ENOENT;
786 }
787
788 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
789 {
790         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
791         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
792         struct extent_map *em;
793         u64 len = PAGE_CACHE_SIZE;
794
795         /*
796          * hopefully we have this extent in the tree already, try without
797          * the full extent lock
798          */
799         read_lock(&em_tree->lock);
800         em = lookup_extent_mapping(em_tree, start, len);
801         read_unlock(&em_tree->lock);
802
803         if (!em) {
804                 /* get the big lock and read metadata off disk */
805                 lock_extent(io_tree, start, start + len - 1);
806                 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
807                 unlock_extent(io_tree, start, start + len - 1);
808
809                 if (IS_ERR(em))
810                         return NULL;
811         }
812
813         return em;
814 }
815
816 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
817 {
818         struct extent_map *next;
819         bool ret = true;
820
821         /* this is the last extent */
822         if (em->start + em->len >= i_size_read(inode))
823                 return false;
824
825         next = defrag_lookup_extent(inode, em->start + em->len);
826         if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
827                 ret = false;
828
829         free_extent_map(next);
830         return ret;
831 }
832
833 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
834                                u64 *last_len, u64 *skip, u64 *defrag_end)
835 {
836         struct extent_map *em;
837         int ret = 1;
838         bool next_mergeable = true;
839
840         /*
841          * make sure that once we start defragging an extent, we keep on
842          * defragging it
843          */
844         if (start < *defrag_end)
845                 return 1;
846
847         *skip = 0;
848
849         em = defrag_lookup_extent(inode, start);
850         if (!em)
851                 return 0;
852
853         /* this will cover holes, and inline extents */
854         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
855                 ret = 0;
856                 goto out;
857         }
858
859         next_mergeable = defrag_check_next_extent(inode, em);
860
861         /*
862          * we hit a real extent, if it is big or the next extent is not a
863          * real extent, don't bother defragging it
864          */
865         if ((*last_len == 0 || *last_len >= thresh) &&
866             (em->len >= thresh || !next_mergeable))
867                 ret = 0;
868 out:
869         /*
870          * last_len ends up being a counter of how many bytes we've defragged.
871          * every time we choose not to defrag an extent, we reset *last_len
872          * so that the next tiny extent will force a defrag.
873          *
874          * The end result of this is that tiny extents before a single big
875          * extent will force at least part of that big extent to be defragged.
876          */
877         if (ret) {
878                 *defrag_end = extent_map_end(em);
879         } else {
880                 *last_len = 0;
881                 *skip = extent_map_end(em);
882                 *defrag_end = 0;
883         }
884
885         free_extent_map(em);
886         return ret;
887 }
888
889 /*
890  * it doesn't do much good to defrag one or two pages
891  * at a time.  This pulls in a nice chunk of pages
892  * to COW and defrag.
893  *
894  * It also makes sure the delalloc code has enough
895  * dirty data to avoid making new small extents as part
896  * of the defrag
897  *
898  * It's a good idea to start RA on this range
899  * before calling this.
900  */
901 static int cluster_pages_for_defrag(struct inode *inode,
902                                     struct page **pages,
903                                     unsigned long start_index,
904                                     int num_pages)
905 {
906         unsigned long file_end;
907         u64 isize = i_size_read(inode);
908         u64 page_start;
909         u64 page_end;
910         u64 page_cnt;
911         int ret;
912         int i;
913         int i_done;
914         struct btrfs_ordered_extent *ordered;
915         struct extent_state *cached_state = NULL;
916         struct extent_io_tree *tree;
917         gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
918
919         file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
920         if (!isize || start_index > file_end)
921                 return 0;
922
923         page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
924
925         ret = btrfs_delalloc_reserve_space(inode,
926                                            page_cnt << PAGE_CACHE_SHIFT);
927         if (ret)
928                 return ret;
929         i_done = 0;
930         tree = &BTRFS_I(inode)->io_tree;
931
932         /* step one, lock all the pages */
933         for (i = 0; i < page_cnt; i++) {
934                 struct page *page;
935 again:
936                 page = find_or_create_page(inode->i_mapping,
937                                            start_index + i, mask);
938                 if (!page)
939                         break;
940
941                 page_start = page_offset(page);
942                 page_end = page_start + PAGE_CACHE_SIZE - 1;
943                 while (1) {
944                         lock_extent(tree, page_start, page_end);
945                         ordered = btrfs_lookup_ordered_extent(inode,
946                                                               page_start);
947                         unlock_extent(tree, page_start, page_end);
948                         if (!ordered)
949                                 break;
950
951                         unlock_page(page);
952                         btrfs_start_ordered_extent(inode, ordered, 1);
953                         btrfs_put_ordered_extent(ordered);
954                         lock_page(page);
955                         /*
956                          * we unlocked the page above, so we need check if
957                          * it was released or not.
958                          */
959                         if (page->mapping != inode->i_mapping) {
960                                 unlock_page(page);
961                                 page_cache_release(page);
962                                 goto again;
963                         }
964                 }
965
966                 if (!PageUptodate(page)) {
967                         btrfs_readpage(NULL, page);
968                         lock_page(page);
969                         if (!PageUptodate(page)) {
970                                 unlock_page(page);
971                                 page_cache_release(page);
972                                 ret = -EIO;
973                                 break;
974                         }
975                 }
976
977                 if (page->mapping != inode->i_mapping) {
978                         unlock_page(page);
979                         page_cache_release(page);
980                         goto again;
981                 }
982
983                 pages[i] = page;
984                 i_done++;
985         }
986         if (!i_done || ret)
987                 goto out;
988
989         if (!(inode->i_sb->s_flags & MS_ACTIVE))
990                 goto out;
991
992         /*
993          * so now we have a nice long stream of locked
994          * and up to date pages, lets wait on them
995          */
996         for (i = 0; i < i_done; i++)
997                 wait_on_page_writeback(pages[i]);
998
999         page_start = page_offset(pages[0]);
1000         page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1001
1002         lock_extent_bits(&BTRFS_I(inode)->io_tree,
1003                          page_start, page_end - 1, 0, &cached_state);
1004         clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1005                           page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1006                           EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
1007                           GFP_NOFS);
1008
1009         if (i_done != page_cnt) {
1010                 spin_lock(&BTRFS_I(inode)->lock);
1011                 BTRFS_I(inode)->outstanding_extents++;
1012                 spin_unlock(&BTRFS_I(inode)->lock);
1013                 btrfs_delalloc_release_space(inode,
1014                                      (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1015         }
1016
1017
1018         btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
1019                                   &cached_state);
1020
1021         unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1022                              page_start, page_end - 1, &cached_state,
1023                              GFP_NOFS);
1024
1025         for (i = 0; i < i_done; i++) {
1026                 clear_page_dirty_for_io(pages[i]);
1027                 ClearPageChecked(pages[i]);
1028                 set_page_extent_mapped(pages[i]);
1029                 set_page_dirty(pages[i]);
1030                 unlock_page(pages[i]);
1031                 page_cache_release(pages[i]);
1032         }
1033         return i_done;
1034 out:
1035         for (i = 0; i < i_done; i++) {
1036                 unlock_page(pages[i]);
1037                 page_cache_release(pages[i]);
1038         }
1039         btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1040         return ret;
1041
1042 }
1043
1044 int btrfs_defrag_file(struct inode *inode, struct file *file,
1045                       struct btrfs_ioctl_defrag_range_args *range,
1046                       u64 newer_than, unsigned long max_to_defrag)
1047 {
1048         struct btrfs_root *root = BTRFS_I(inode)->root;
1049         struct btrfs_super_block *disk_super;
1050         struct file_ra_state *ra = NULL;
1051         unsigned long last_index;
1052         u64 isize = i_size_read(inode);
1053         u64 features;
1054         u64 last_len = 0;
1055         u64 skip = 0;
1056         u64 defrag_end = 0;
1057         u64 newer_off = range->start;
1058         unsigned long i;
1059         unsigned long ra_index = 0;
1060         int ret;
1061         int defrag_count = 0;
1062         int compress_type = BTRFS_COMPRESS_ZLIB;
1063         int extent_thresh = range->extent_thresh;
1064         int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1065         int cluster = max_cluster;
1066         u64 new_align = ~((u64)128 * 1024 - 1);
1067         struct page **pages = NULL;
1068
1069         if (extent_thresh == 0)
1070                 extent_thresh = 256 * 1024;
1071
1072         if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1073                 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1074                         return -EINVAL;
1075                 if (range->compress_type)
1076                         compress_type = range->compress_type;
1077         }
1078
1079         if (isize == 0)
1080                 return 0;
1081
1082         /*
1083          * if we were not given a file, allocate a readahead
1084          * context
1085          */
1086         if (!file) {
1087                 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1088                 if (!ra)
1089                         return -ENOMEM;
1090                 file_ra_state_init(ra, inode->i_mapping);
1091         } else {
1092                 ra = &file->f_ra;
1093         }
1094
1095         pages = kmalloc(sizeof(struct page *) * max_cluster,
1096                         GFP_NOFS);
1097         if (!pages) {
1098                 ret = -ENOMEM;
1099                 goto out_ra;
1100         }
1101
1102         /* find the last page to defrag */
1103         if (range->start + range->len > range->start) {
1104                 last_index = min_t(u64, isize - 1,
1105                          range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1106         } else {
1107                 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1108         }
1109
1110         if (newer_than) {
1111                 ret = find_new_extents(root, inode, newer_than,
1112                                        &newer_off, 64 * 1024);
1113                 if (!ret) {
1114                         range->start = newer_off;
1115                         /*
1116                          * we always align our defrag to help keep
1117                          * the extents in the file evenly spaced
1118                          */
1119                         i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1120                 } else
1121                         goto out_ra;
1122         } else {
1123                 i = range->start >> PAGE_CACHE_SHIFT;
1124         }
1125         if (!max_to_defrag)
1126                 max_to_defrag = last_index + 1;
1127
1128         /*
1129          * make writeback starts from i, so the defrag range can be
1130          * written sequentially.
1131          */
1132         if (i < inode->i_mapping->writeback_index)
1133                 inode->i_mapping->writeback_index = i;
1134
1135         while (i <= last_index && defrag_count < max_to_defrag &&
1136                (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1137                 PAGE_CACHE_SHIFT)) {
1138                 /*
1139                  * make sure we stop running if someone unmounts
1140                  * the FS
1141                  */
1142                 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1143                         break;
1144
1145                 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1146                                          extent_thresh, &last_len, &skip,
1147                                          &defrag_end)) {
1148                         unsigned long next;
1149                         /*
1150                          * the should_defrag function tells us how much to skip
1151                          * bump our counter by the suggested amount
1152                          */
1153                         next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1154                         i = max(i + 1, next);
1155                         continue;
1156                 }
1157
1158                 if (!newer_than) {
1159                         cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1160                                    PAGE_CACHE_SHIFT) - i;
1161                         cluster = min(cluster, max_cluster);
1162                 } else {
1163                         cluster = max_cluster;
1164                 }
1165
1166                 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1167                         BTRFS_I(inode)->force_compress = compress_type;
1168
1169                 if (i + cluster > ra_index) {
1170                         ra_index = max(i, ra_index);
1171                         btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1172                                        cluster);
1173                         ra_index += max_cluster;
1174                 }
1175
1176                 mutex_lock(&inode->i_mutex);
1177                 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1178                 if (ret < 0) {
1179                         mutex_unlock(&inode->i_mutex);
1180                         goto out_ra;
1181                 }
1182
1183                 defrag_count += ret;
1184                 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1185                 mutex_unlock(&inode->i_mutex);
1186
1187                 if (newer_than) {
1188                         if (newer_off == (u64)-1)
1189                                 break;
1190
1191                         if (ret > 0)
1192                                 i += ret;
1193
1194                         newer_off = max(newer_off + 1,
1195                                         (u64)i << PAGE_CACHE_SHIFT);
1196
1197                         ret = find_new_extents(root, inode,
1198                                                newer_than, &newer_off,
1199                                                64 * 1024);
1200                         if (!ret) {
1201                                 range->start = newer_off;
1202                                 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1203                         } else {
1204                                 break;
1205                         }
1206                 } else {
1207                         if (ret > 0) {
1208                                 i += ret;
1209                                 last_len += ret << PAGE_CACHE_SHIFT;
1210                         } else {
1211                                 i++;
1212                                 last_len = 0;
1213                         }
1214                 }
1215         }
1216
1217         if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1218                 filemap_flush(inode->i_mapping);
1219
1220         if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1221                 /* the filemap_flush will queue IO into the worker threads, but
1222                  * we have to make sure the IO is actually started and that
1223                  * ordered extents get created before we return
1224                  */
1225                 atomic_inc(&root->fs_info->async_submit_draining);
1226                 while (atomic_read(&root->fs_info->nr_async_submits) ||
1227                       atomic_read(&root->fs_info->async_delalloc_pages)) {
1228                         wait_event(root->fs_info->async_submit_wait,
1229                            (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1230                             atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1231                 }
1232                 atomic_dec(&root->fs_info->async_submit_draining);
1233
1234                 mutex_lock(&inode->i_mutex);
1235                 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1236                 mutex_unlock(&inode->i_mutex);
1237         }
1238
1239         disk_super = root->fs_info->super_copy;
1240         features = btrfs_super_incompat_flags(disk_super);
1241         if (range->compress_type == BTRFS_COMPRESS_LZO) {
1242                 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1243                 btrfs_set_super_incompat_flags(disk_super, features);
1244         }
1245
1246         ret = defrag_count;
1247
1248 out_ra:
1249         if (!file)
1250                 kfree(ra);
1251         kfree(pages);
1252         return ret;
1253 }
1254
1255 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1256                                         void __user *arg)
1257 {
1258         u64 new_size;
1259         u64 old_size;
1260         u64 devid = 1;
1261         struct btrfs_ioctl_vol_args *vol_args;
1262         struct btrfs_trans_handle *trans;
1263         struct btrfs_device *device = NULL;
1264         char *sizestr;
1265         char *devstr = NULL;
1266         int ret = 0;
1267         int mod = 0;
1268
1269         if (root->fs_info->sb->s_flags & MS_RDONLY)
1270                 return -EROFS;
1271
1272         if (!capable(CAP_SYS_ADMIN))
1273                 return -EPERM;
1274
1275         mutex_lock(&root->fs_info->volume_mutex);
1276         if (root->fs_info->balance_ctl) {
1277                 printk(KERN_INFO "btrfs: balance in progress\n");
1278                 ret = -EINVAL;
1279                 goto out;
1280         }
1281
1282         vol_args = memdup_user(arg, sizeof(*vol_args));
1283         if (IS_ERR(vol_args)) {
1284                 ret = PTR_ERR(vol_args);
1285                 goto out;
1286         }
1287
1288         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1289
1290         sizestr = vol_args->name;
1291         devstr = strchr(sizestr, ':');
1292         if (devstr) {
1293                 char *end;
1294                 sizestr = devstr + 1;
1295                 *devstr = '\0';
1296                 devstr = vol_args->name;
1297                 devid = simple_strtoull(devstr, &end, 10);
1298                 printk(KERN_INFO "btrfs: resizing devid %llu\n",
1299                        (unsigned long long)devid);
1300         }
1301         device = btrfs_find_device(root, devid, NULL, NULL);
1302         if (!device) {
1303                 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1304                        (unsigned long long)devid);
1305                 ret = -EINVAL;
1306                 goto out_free;
1307         }
1308         if (device->fs_devices && device->fs_devices->seeding) {
1309                 printk(KERN_INFO "btrfs: resizer unable to apply on "
1310                        "seeding device %llu\n",
1311                        (unsigned long long)devid);
1312                 ret = -EINVAL;
1313                 goto out_free;
1314         }
1315
1316         if (!strcmp(sizestr, "max"))
1317                 new_size = device->bdev->bd_inode->i_size;
1318         else {
1319                 if (sizestr[0] == '-') {
1320                         mod = -1;
1321                         sizestr++;
1322                 } else if (sizestr[0] == '+') {
1323                         mod = 1;
1324                         sizestr++;
1325                 }
1326                 new_size = memparse(sizestr, NULL);
1327                 if (new_size == 0) {
1328                         ret = -EINVAL;
1329                         goto out_free;
1330                 }
1331         }
1332
1333         old_size = device->total_bytes;
1334
1335         if (mod < 0) {
1336                 if (new_size > old_size) {
1337                         ret = -EINVAL;
1338                         goto out_free;
1339                 }
1340                 new_size = old_size - new_size;
1341         } else if (mod > 0) {
1342                 new_size = old_size + new_size;
1343         }
1344
1345         if (new_size < 256 * 1024 * 1024) {
1346                 ret = -EINVAL;
1347                 goto out_free;
1348         }
1349         if (new_size > device->bdev->bd_inode->i_size) {
1350                 ret = -EFBIG;
1351                 goto out_free;
1352         }
1353
1354         do_div(new_size, root->sectorsize);
1355         new_size *= root->sectorsize;
1356
1357         printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1358                       rcu_str_deref(device->name),
1359                       (unsigned long long)new_size);
1360
1361         if (new_size > old_size) {
1362                 trans = btrfs_start_transaction(root, 0);
1363                 if (IS_ERR(trans)) {
1364                         ret = PTR_ERR(trans);
1365                         goto out_free;
1366                 }
1367                 ret = btrfs_grow_device(trans, device, new_size);
1368                 btrfs_commit_transaction(trans, root);
1369         } else if (new_size < old_size) {
1370                 ret = btrfs_shrink_device(device, new_size);
1371         }
1372
1373 out_free:
1374         kfree(vol_args);
1375 out:
1376         mutex_unlock(&root->fs_info->volume_mutex);
1377         return ret;
1378 }
1379
1380 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1381                                                     char *name,
1382                                                     unsigned long fd,
1383                                                     int subvol,
1384                                                     u64 *transid,
1385                                                     bool readonly)
1386 {
1387         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1388         struct file *src_file;
1389         int namelen;
1390         int ret = 0;
1391
1392         if (root->fs_info->sb->s_flags & MS_RDONLY)
1393                 return -EROFS;
1394
1395         namelen = strlen(name);
1396         if (strchr(name, '/')) {
1397                 ret = -EINVAL;
1398                 goto out;
1399         }
1400
1401         if (name[0] == '.' &&
1402            (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1403                 ret = -EEXIST;
1404                 goto out;
1405         }
1406
1407         if (subvol) {
1408                 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1409                                      NULL, transid, readonly);
1410         } else {
1411                 struct inode *src_inode;
1412                 src_file = fget(fd);
1413                 if (!src_file) {
1414                         ret = -EINVAL;
1415                         goto out;
1416                 }
1417
1418                 src_inode = src_file->f_path.dentry->d_inode;
1419                 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1420                         printk(KERN_INFO "btrfs: Snapshot src from "
1421                                "another FS\n");
1422                         ret = -EINVAL;
1423                         fput(src_file);
1424                         goto out;
1425                 }
1426                 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1427                                      BTRFS_I(src_inode)->root,
1428                                      transid, readonly);
1429                 fput(src_file);
1430         }
1431 out:
1432         return ret;
1433 }
1434
1435 static noinline int btrfs_ioctl_snap_create(struct file *file,
1436                                             void __user *arg, int subvol)
1437 {
1438         struct btrfs_ioctl_vol_args *vol_args;
1439         int ret;
1440
1441         vol_args = memdup_user(arg, sizeof(*vol_args));
1442         if (IS_ERR(vol_args))
1443                 return PTR_ERR(vol_args);
1444         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1445
1446         ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1447                                               vol_args->fd, subvol,
1448                                               NULL, false);
1449
1450         kfree(vol_args);
1451         return ret;
1452 }
1453
1454 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1455                                                void __user *arg, int subvol)
1456 {
1457         struct btrfs_ioctl_vol_args_v2 *vol_args;
1458         int ret;
1459         u64 transid = 0;
1460         u64 *ptr = NULL;
1461         bool readonly = false;
1462
1463         vol_args = memdup_user(arg, sizeof(*vol_args));
1464         if (IS_ERR(vol_args))
1465                 return PTR_ERR(vol_args);
1466         vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1467
1468         if (vol_args->flags &
1469             ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1470                 ret = -EOPNOTSUPP;
1471                 goto out;
1472         }
1473
1474         if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1475                 ptr = &transid;
1476         if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1477                 readonly = true;
1478
1479         ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1480                                               vol_args->fd, subvol,
1481                                               ptr, readonly);
1482
1483         if (ret == 0 && ptr &&
1484             copy_to_user(arg +
1485                          offsetof(struct btrfs_ioctl_vol_args_v2,
1486                                   transid), ptr, sizeof(*ptr)))
1487                 ret = -EFAULT;
1488 out:
1489         kfree(vol_args);
1490         return ret;
1491 }
1492
1493 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1494                                                 void __user *arg)
1495 {
1496         struct inode *inode = fdentry(file)->d_inode;
1497         struct btrfs_root *root = BTRFS_I(inode)->root;
1498         int ret = 0;
1499         u64 flags = 0;
1500
1501         if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1502                 return -EINVAL;
1503
1504         down_read(&root->fs_info->subvol_sem);
1505         if (btrfs_root_readonly(root))
1506                 flags |= BTRFS_SUBVOL_RDONLY;
1507         up_read(&root->fs_info->subvol_sem);
1508
1509         if (copy_to_user(arg, &flags, sizeof(flags)))
1510                 ret = -EFAULT;
1511
1512         return ret;
1513 }
1514
1515 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1516                                               void __user *arg)
1517 {
1518         struct inode *inode = fdentry(file)->d_inode;
1519         struct btrfs_root *root = BTRFS_I(inode)->root;
1520         struct btrfs_trans_handle *trans;
1521         u64 root_flags;
1522         u64 flags;
1523         int ret = 0;
1524
1525         if (root->fs_info->sb->s_flags & MS_RDONLY)
1526                 return -EROFS;
1527
1528         if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1529                 return -EINVAL;
1530
1531         if (copy_from_user(&flags, arg, sizeof(flags)))
1532                 return -EFAULT;
1533
1534         if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
1535                 return -EINVAL;
1536
1537         if (flags & ~BTRFS_SUBVOL_RDONLY)
1538                 return -EOPNOTSUPP;
1539
1540         if (!inode_owner_or_capable(inode))
1541                 return -EACCES;
1542
1543         down_write(&root->fs_info->subvol_sem);
1544
1545         /* nothing to do */
1546         if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1547                 goto out;
1548
1549         root_flags = btrfs_root_flags(&root->root_item);
1550         if (flags & BTRFS_SUBVOL_RDONLY)
1551                 btrfs_set_root_flags(&root->root_item,
1552                                      root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1553         else
1554                 btrfs_set_root_flags(&root->root_item,
1555                                      root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1556
1557         trans = btrfs_start_transaction(root, 1);
1558         if (IS_ERR(trans)) {
1559                 ret = PTR_ERR(trans);
1560                 goto out_reset;
1561         }
1562
1563         ret = btrfs_update_root(trans, root->fs_info->tree_root,
1564                                 &root->root_key, &root->root_item);
1565
1566         btrfs_commit_transaction(trans, root);
1567 out_reset:
1568         if (ret)
1569                 btrfs_set_root_flags(&root->root_item, root_flags);
1570 out:
1571         up_write(&root->fs_info->subvol_sem);
1572         return ret;
1573 }
1574
1575 /*
1576  * helper to check if the subvolume references other subvolumes
1577  */
1578 static noinline int may_destroy_subvol(struct btrfs_root *root)
1579 {
1580         struct btrfs_path *path;
1581         struct btrfs_key key;
1582         int ret;
1583
1584         path = btrfs_alloc_path();
1585         if (!path)
1586                 return -ENOMEM;
1587
1588         key.objectid = root->root_key.objectid;
1589         key.type = BTRFS_ROOT_REF_KEY;
1590         key.offset = (u64)-1;
1591
1592         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1593                                 &key, path, 0, 0);
1594         if (ret < 0)
1595                 goto out;
1596         BUG_ON(ret == 0);
1597
1598         ret = 0;
1599         if (path->slots[0] > 0) {
1600                 path->slots[0]--;
1601                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1602                 if (key.objectid == root->root_key.objectid &&
1603                     key.type == BTRFS_ROOT_REF_KEY)
1604                         ret = -ENOTEMPTY;
1605         }
1606 out:
1607         btrfs_free_path(path);
1608         return ret;
1609 }
1610
1611 static noinline int key_in_sk(struct btrfs_key *key,
1612                               struct btrfs_ioctl_search_key *sk)
1613 {
1614         struct btrfs_key test;
1615         int ret;
1616
1617         test.objectid = sk->min_objectid;
1618         test.type = sk->min_type;
1619         test.offset = sk->min_offset;
1620
1621         ret = btrfs_comp_cpu_keys(key, &test);
1622         if (ret < 0)
1623                 return 0;
1624
1625         test.objectid = sk->max_objectid;
1626         test.type = sk->max_type;
1627         test.offset = sk->max_offset;
1628
1629         ret = btrfs_comp_cpu_keys(key, &test);
1630         if (ret > 0)
1631                 return 0;
1632         return 1;
1633 }
1634
1635 static noinline int copy_to_sk(struct btrfs_root *root,
1636                                struct btrfs_path *path,
1637                                struct btrfs_key *key,
1638                                struct btrfs_ioctl_search_key *sk,
1639                                char *buf,
1640                                unsigned long *sk_offset,
1641                                int *num_found)
1642 {
1643         u64 found_transid;
1644         struct extent_buffer *leaf;
1645         struct btrfs_ioctl_search_header sh;
1646         unsigned long item_off;
1647         unsigned long item_len;
1648         int nritems;
1649         int i;
1650         int slot;
1651         int ret = 0;
1652
1653         leaf = path->nodes[0];
1654         slot = path->slots[0];
1655         nritems = btrfs_header_nritems(leaf);
1656
1657         if (btrfs_header_generation(leaf) > sk->max_transid) {
1658                 i = nritems;
1659                 goto advance_key;
1660         }
1661         found_transid = btrfs_header_generation(leaf);
1662
1663         for (i = slot; i < nritems; i++) {
1664                 item_off = btrfs_item_ptr_offset(leaf, i);
1665                 item_len = btrfs_item_size_nr(leaf, i);
1666
1667                 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1668                         item_len = 0;
1669
1670                 if (sizeof(sh) + item_len + *sk_offset >
1671                     BTRFS_SEARCH_ARGS_BUFSIZE) {
1672                         ret = 1;
1673                         goto overflow;
1674                 }
1675
1676                 btrfs_item_key_to_cpu(leaf, key, i);
1677                 if (!key_in_sk(key, sk))
1678                         continue;
1679
1680                 sh.objectid = key->objectid;
1681                 sh.offset = key->offset;
1682                 sh.type = key->type;
1683                 sh.len = item_len;
1684                 sh.transid = found_transid;
1685
1686                 /* copy search result header */
1687                 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1688                 *sk_offset += sizeof(sh);
1689
1690                 if (item_len) {
1691                         char *p = buf + *sk_offset;
1692                         /* copy the item */
1693                         read_extent_buffer(leaf, p,
1694                                            item_off, item_len);
1695                         *sk_offset += item_len;
1696                 }
1697                 (*num_found)++;
1698
1699                 if (*num_found >= sk->nr_items)
1700                         break;
1701         }
1702 advance_key:
1703         ret = 0;
1704         if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1705                 key->offset++;
1706         else if (key->type < (u8)-1 && key->type < sk->max_type) {
1707                 key->offset = 0;
1708                 key->type++;
1709         } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1710                 key->offset = 0;
1711                 key->type = 0;
1712                 key->objectid++;
1713         } else
1714                 ret = 1;
1715 overflow:
1716         return ret;
1717 }
1718
1719 static noinline int search_ioctl(struct inode *inode,
1720                                  struct btrfs_ioctl_search_args *args)
1721 {
1722         struct btrfs_root *root;
1723         struct btrfs_key key;
1724         struct btrfs_key max_key;
1725         struct btrfs_path *path;
1726         struct btrfs_ioctl_search_key *sk = &args->key;
1727         struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1728         int ret;
1729         int num_found = 0;
1730         unsigned long sk_offset = 0;
1731
1732         path = btrfs_alloc_path();
1733         if (!path)
1734                 return -ENOMEM;
1735
1736         if (sk->tree_id == 0) {
1737                 /* search the root of the inode that was passed */
1738                 root = BTRFS_I(inode)->root;
1739         } else {
1740                 key.objectid = sk->tree_id;
1741                 key.type = BTRFS_ROOT_ITEM_KEY;
1742                 key.offset = (u64)-1;
1743                 root = btrfs_read_fs_root_no_name(info, &key);
1744                 if (IS_ERR(root)) {
1745                         printk(KERN_ERR "could not find root %llu\n",
1746                                sk->tree_id);
1747                         btrfs_free_path(path);
1748                         return -ENOENT;
1749                 }
1750         }
1751
1752         key.objectid = sk->min_objectid;
1753         key.type = sk->min_type;
1754         key.offset = sk->min_offset;
1755
1756         max_key.objectid = sk->max_objectid;
1757         max_key.type = sk->max_type;
1758         max_key.offset = sk->max_offset;
1759
1760         path->keep_locks = 1;
1761
1762         while(1) {
1763                 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1764                                            sk->min_transid);
1765                 if (ret != 0) {
1766                         if (ret > 0)
1767                                 ret = 0;
1768                         goto err;
1769                 }
1770                 ret = copy_to_sk(root, path, &key, sk, args->buf,
1771                                  &sk_offset, &num_found);
1772                 btrfs_release_path(path);
1773                 if (ret || num_found >= sk->nr_items)
1774                         break;
1775
1776         }
1777         ret = 0;
1778 err:
1779         sk->nr_items = num_found;
1780         btrfs_free_path(path);
1781         return ret;
1782 }
1783
1784 static noinline int btrfs_ioctl_tree_search(struct file *file,
1785                                            void __user *argp)
1786 {
1787          struct btrfs_ioctl_search_args *args;
1788          struct inode *inode;
1789          int ret;
1790
1791         if (!capable(CAP_SYS_ADMIN))
1792                 return -EPERM;
1793
1794         args = memdup_user(argp, sizeof(*args));
1795         if (IS_ERR(args))
1796                 return PTR_ERR(args);
1797
1798         inode = fdentry(file)->d_inode;
1799         ret = search_ioctl(inode, args);
1800         if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1801                 ret = -EFAULT;
1802         kfree(args);
1803         return ret;
1804 }
1805
1806 /*
1807  * Search INODE_REFs to identify path name of 'dirid' directory
1808  * in a 'tree_id' tree. and sets path name to 'name'.
1809  */
1810 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1811                                 u64 tree_id, u64 dirid, char *name)
1812 {
1813         struct btrfs_root *root;
1814         struct btrfs_key key;
1815         char *ptr;
1816         int ret = -1;
1817         int slot;
1818         int len;
1819         int total_len = 0;
1820         struct btrfs_inode_ref *iref;
1821         struct extent_buffer *l;
1822         struct btrfs_path *path;
1823
1824         if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1825                 name[0]='\0';
1826                 return 0;
1827         }
1828
1829         path = btrfs_alloc_path();
1830         if (!path)
1831                 return -ENOMEM;
1832
1833         ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1834
1835         key.objectid = tree_id;
1836         key.type = BTRFS_ROOT_ITEM_KEY;
1837         key.offset = (u64)-1;
1838         root = btrfs_read_fs_root_no_name(info, &key);
1839         if (IS_ERR(root)) {
1840                 printk(KERN_ERR "could not find root %llu\n", tree_id);
1841                 ret = -ENOENT;
1842                 goto out;
1843         }
1844
1845         key.objectid = dirid;
1846         key.type = BTRFS_INODE_REF_KEY;
1847         key.offset = (u64)-1;
1848
1849         while(1) {
1850                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1851                 if (ret < 0)
1852                         goto out;
1853
1854                 l = path->nodes[0];
1855                 slot = path->slots[0];
1856                 if (ret > 0 && slot > 0)
1857                         slot--;
1858                 btrfs_item_key_to_cpu(l, &key, slot);
1859
1860                 if (ret > 0 && (key.objectid != dirid ||
1861                                 key.type != BTRFS_INODE_REF_KEY)) {
1862                         ret = -ENOENT;
1863                         goto out;
1864                 }
1865
1866                 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1867                 len = btrfs_inode_ref_name_len(l, iref);
1868                 ptr -= len + 1;
1869                 total_len += len + 1;
1870                 if (ptr < name)
1871                         goto out;
1872
1873                 *(ptr + len) = '/';
1874                 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1875
1876                 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1877                         break;
1878
1879                 btrfs_release_path(path);
1880                 key.objectid = key.offset;
1881                 key.offset = (u64)-1;
1882                 dirid = key.objectid;
1883         }
1884         if (ptr < name)
1885                 goto out;
1886         memmove(name, ptr, total_len);
1887         name[total_len]='\0';
1888         ret = 0;
1889 out:
1890         btrfs_free_path(path);
1891         return ret;
1892 }
1893
1894 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1895                                            void __user *argp)
1896 {
1897          struct btrfs_ioctl_ino_lookup_args *args;
1898          struct inode *inode;
1899          int ret;
1900
1901         if (!capable(CAP_SYS_ADMIN))
1902                 return -EPERM;
1903
1904         args = memdup_user(argp, sizeof(*args));
1905         if (IS_ERR(args))
1906                 return PTR_ERR(args);
1907
1908         inode = fdentry(file)->d_inode;
1909
1910         if (args->treeid == 0)
1911                 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1912
1913         ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1914                                         args->treeid, args->objectid,
1915                                         args->name);
1916
1917         if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1918                 ret = -EFAULT;
1919
1920         kfree(args);
1921         return ret;
1922 }
1923
1924 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1925                                              void __user *arg)
1926 {
1927         struct dentry *parent = fdentry(file);
1928         struct dentry *dentry;
1929         struct inode *dir = parent->d_inode;
1930         struct inode *inode;
1931         struct btrfs_root *root = BTRFS_I(dir)->root;
1932         struct btrfs_root *dest = NULL;
1933         struct btrfs_ioctl_vol_args *vol_args;
1934         struct btrfs_trans_handle *trans;
1935         int namelen;
1936         int ret;
1937         int err = 0;
1938
1939         vol_args = memdup_user(arg, sizeof(*vol_args));
1940         if (IS_ERR(vol_args))
1941                 return PTR_ERR(vol_args);
1942
1943         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1944         namelen = strlen(vol_args->name);
1945         if (strchr(vol_args->name, '/') ||
1946             strncmp(vol_args->name, "..", namelen) == 0) {
1947                 err = -EINVAL;
1948                 goto out;
1949         }
1950
1951         err = mnt_want_write_file(file);
1952         if (err)
1953                 goto out;
1954
1955         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1956         dentry = lookup_one_len(vol_args->name, parent, namelen);
1957         if (IS_ERR(dentry)) {
1958                 err = PTR_ERR(dentry);
1959                 goto out_unlock_dir;
1960         }
1961
1962         if (!dentry->d_inode) {
1963                 err = -ENOENT;
1964                 goto out_dput;
1965         }
1966
1967         inode = dentry->d_inode;
1968         dest = BTRFS_I(inode)->root;
1969         if (!capable(CAP_SYS_ADMIN)){
1970                 /*
1971                  * Regular user.  Only allow this with a special mount
1972                  * option, when the user has write+exec access to the
1973                  * subvol root, and when rmdir(2) would have been
1974                  * allowed.
1975                  *
1976                  * Note that this is _not_ check that the subvol is
1977                  * empty or doesn't contain data that we wouldn't
1978                  * otherwise be able to delete.
1979                  *
1980                  * Users who want to delete empty subvols should try
1981                  * rmdir(2).
1982                  */
1983                 err = -EPERM;
1984                 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1985                         goto out_dput;
1986
1987                 /*
1988                  * Do not allow deletion if the parent dir is the same
1989                  * as the dir to be deleted.  That means the ioctl
1990                  * must be called on the dentry referencing the root
1991                  * of the subvol, not a random directory contained
1992                  * within it.
1993                  */
1994                 err = -EINVAL;
1995                 if (root == dest)
1996                         goto out_dput;
1997
1998                 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1999                 if (err)
2000                         goto out_dput;
2001
2002                 /* check if subvolume may be deleted by a non-root user */
2003                 err = btrfs_may_delete(dir, dentry, 1);
2004                 if (err)
2005                         goto out_dput;
2006         }
2007
2008         if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2009                 err = -EINVAL;
2010                 goto out_dput;
2011         }
2012
2013         mutex_lock(&inode->i_mutex);
2014         err = d_invalidate(dentry);
2015         if (err)
2016                 goto out_unlock;
2017
2018         down_write(&root->fs_info->subvol_sem);
2019
2020         err = may_destroy_subvol(dest);
2021         if (err)
2022                 goto out_up_write;
2023
2024         trans = btrfs_start_transaction(root, 0);
2025         if (IS_ERR(trans)) {
2026                 err = PTR_ERR(trans);
2027                 goto out_up_write;
2028         }
2029         trans->block_rsv = &root->fs_info->global_block_rsv;
2030
2031         ret = btrfs_unlink_subvol(trans, root, dir,
2032                                 dest->root_key.objectid,
2033                                 dentry->d_name.name,
2034                                 dentry->d_name.len);
2035         if (ret) {
2036                 err = ret;
2037                 btrfs_abort_transaction(trans, root, ret);
2038                 goto out_end_trans;
2039         }
2040
2041         btrfs_record_root_in_trans(trans, dest);
2042
2043         memset(&dest->root_item.drop_progress, 0,
2044                 sizeof(dest->root_item.drop_progress));
2045         dest->root_item.drop_level = 0;
2046         btrfs_set_root_refs(&dest->root_item, 0);
2047
2048         if (!xchg(&dest->orphan_item_inserted, 1)) {
2049                 ret = btrfs_insert_orphan_item(trans,
2050                                         root->fs_info->tree_root,
2051                                         dest->root_key.objectid);
2052                 if (ret) {
2053                         btrfs_abort_transaction(trans, root, ret);
2054                         err = ret;
2055                         goto out_end_trans;
2056                 }
2057         }
2058 out_end_trans:
2059         ret = btrfs_end_transaction(trans, root);
2060         if (ret && !err)
2061                 err = ret;
2062         inode->i_flags |= S_DEAD;
2063 out_up_write:
2064         up_write(&root->fs_info->subvol_sem);
2065 out_unlock:
2066         mutex_unlock(&inode->i_mutex);
2067         if (!err) {
2068                 shrink_dcache_sb(root->fs_info->sb);
2069                 btrfs_invalidate_inodes(dest);
2070                 d_delete(dentry);
2071         }
2072 out_dput:
2073         dput(dentry);
2074 out_unlock_dir:
2075         mutex_unlock(&dir->i_mutex);
2076         mnt_drop_write_file(file);
2077 out:
2078         kfree(vol_args);
2079         return err;
2080 }
2081
2082 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2083 {
2084         struct inode *inode = fdentry(file)->d_inode;
2085         struct btrfs_root *root = BTRFS_I(inode)->root;
2086         struct btrfs_ioctl_defrag_range_args *range;
2087         int ret;
2088
2089         if (btrfs_root_readonly(root))
2090                 return -EROFS;
2091
2092         ret = mnt_want_write_file(file);
2093         if (ret)
2094                 return ret;
2095
2096         switch (inode->i_mode & S_IFMT) {
2097         case S_IFDIR:
2098                 if (!capable(CAP_SYS_ADMIN)) {
2099                         ret = -EPERM;
2100                         goto out;
2101                 }
2102                 ret = btrfs_defrag_root(root, 0);
2103                 if (ret)
2104                         goto out;
2105                 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2106                 break;
2107         case S_IFREG:
2108                 if (!(file->f_mode & FMODE_WRITE)) {
2109                         ret = -EINVAL;
2110                         goto out;
2111                 }
2112
2113                 range = kzalloc(sizeof(*range), GFP_KERNEL);
2114                 if (!range) {
2115                         ret = -ENOMEM;
2116                         goto out;
2117                 }
2118
2119                 if (argp) {
2120                         if (copy_from_user(range, argp,
2121                                            sizeof(*range))) {
2122                                 ret = -EFAULT;
2123                                 kfree(range);
2124                                 goto out;
2125                         }
2126                         /* compression requires us to start the IO */
2127                         if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2128                                 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2129                                 range->extent_thresh = (u32)-1;
2130                         }
2131                 } else {
2132                         /* the rest are all set to zero by kzalloc */
2133                         range->len = (u64)-1;
2134                 }
2135                 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2136                                         range, 0, 0);
2137                 if (ret > 0)
2138                         ret = 0;
2139                 kfree(range);
2140                 break;
2141         default:
2142                 ret = -EINVAL;
2143         }
2144 out:
2145         mnt_drop_write_file(file);
2146         return ret;
2147 }
2148
2149 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2150 {
2151         struct btrfs_ioctl_vol_args *vol_args;
2152         int ret;
2153
2154         if (!capable(CAP_SYS_ADMIN))
2155                 return -EPERM;
2156
2157         mutex_lock(&root->fs_info->volume_mutex);
2158         if (root->fs_info->balance_ctl) {
2159                 printk(KERN_INFO "btrfs: balance in progress\n");
2160                 ret = -EINVAL;
2161                 goto out;
2162         }
2163
2164         vol_args = memdup_user(arg, sizeof(*vol_args));
2165         if (IS_ERR(vol_args)) {
2166                 ret = PTR_ERR(vol_args);
2167                 goto out;
2168         }
2169
2170         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2171         ret = btrfs_init_new_device(root, vol_args->name);
2172
2173         kfree(vol_args);
2174 out:
2175         mutex_unlock(&root->fs_info->volume_mutex);
2176         return ret;
2177 }
2178
2179 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2180 {
2181         struct btrfs_ioctl_vol_args *vol_args;
2182         int ret;
2183
2184         if (!capable(CAP_SYS_ADMIN))
2185                 return -EPERM;
2186
2187         if (root->fs_info->sb->s_flags & MS_RDONLY)
2188                 return -EROFS;
2189
2190         mutex_lock(&root->fs_info->volume_mutex);
2191         if (root->fs_info->balance_ctl) {
2192                 printk(KERN_INFO "btrfs: balance in progress\n");
2193                 ret = -EINVAL;
2194                 goto out;
2195         }
2196
2197         vol_args = memdup_user(arg, sizeof(*vol_args));
2198         if (IS_ERR(vol_args)) {
2199                 ret = PTR_ERR(vol_args);
2200                 goto out;
2201         }
2202
2203         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2204         ret = btrfs_rm_device(root, vol_args->name);
2205
2206         kfree(vol_args);
2207 out:
2208         mutex_unlock(&root->fs_info->volume_mutex);
2209         return ret;
2210 }
2211
2212 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2213 {
2214         struct btrfs_ioctl_fs_info_args *fi_args;
2215         struct btrfs_device *device;
2216         struct btrfs_device *next;
2217         struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2218         int ret = 0;
2219
2220         if (!capable(CAP_SYS_ADMIN))
2221                 return -EPERM;
2222
2223         fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2224         if (!fi_args)
2225                 return -ENOMEM;
2226
2227         fi_args->num_devices = fs_devices->num_devices;
2228         memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2229
2230         mutex_lock(&fs_devices->device_list_mutex);
2231         list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2232                 if (device->devid > fi_args->max_id)
2233                         fi_args->max_id = device->devid;
2234         }
2235         mutex_unlock(&fs_devices->device_list_mutex);
2236
2237         if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2238                 ret = -EFAULT;
2239
2240         kfree(fi_args);
2241         return ret;
2242 }
2243
2244 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2245 {
2246         struct btrfs_ioctl_dev_info_args *di_args;
2247         struct btrfs_device *dev;
2248         struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2249         int ret = 0;
2250         char *s_uuid = NULL;
2251         char empty_uuid[BTRFS_UUID_SIZE] = {0};
2252
2253         if (!capable(CAP_SYS_ADMIN))
2254                 return -EPERM;
2255
2256         di_args = memdup_user(arg, sizeof(*di_args));
2257         if (IS_ERR(di_args))
2258                 return PTR_ERR(di_args);
2259
2260         if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2261                 s_uuid = di_args->uuid;
2262
2263         mutex_lock(&fs_devices->device_list_mutex);
2264         dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2265         mutex_unlock(&fs_devices->device_list_mutex);
2266
2267         if (!dev) {
2268                 ret = -ENODEV;
2269                 goto out;
2270         }
2271
2272         di_args->devid = dev->devid;
2273         di_args->bytes_used = dev->bytes_used;
2274         di_args->total_bytes = dev->total_bytes;
2275         memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2276         if (dev->name) {
2277                 struct rcu_string *name;
2278
2279                 rcu_read_lock();
2280                 name = rcu_dereference(dev->name);
2281                 strncpy(di_args->path, name->str, sizeof(di_args->path));
2282                 rcu_read_unlock();
2283                 di_args->path[sizeof(di_args->path) - 1] = 0;
2284         } else {
2285                 di_args->path[0] = '\0';
2286         }
2287
2288 out:
2289         if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2290                 ret = -EFAULT;
2291
2292         kfree(di_args);
2293         return ret;
2294 }
2295
2296 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2297                                        u64 off, u64 olen, u64 destoff)
2298 {
2299         struct inode *inode = fdentry(file)->d_inode;
2300         struct btrfs_root *root = BTRFS_I(inode)->root;
2301         struct file *src_file;
2302         struct inode *src;
2303         struct btrfs_trans_handle *trans;
2304         struct btrfs_path *path;
2305         struct extent_buffer *leaf;
2306         char *buf;
2307         struct btrfs_key key;
2308         u32 nritems;
2309         int slot;
2310         int ret;
2311         u64 len = olen;
2312         u64 bs = root->fs_info->sb->s_blocksize;
2313         u64 hint_byte;
2314
2315         /*
2316          * TODO:
2317          * - split compressed inline extents.  annoying: we need to
2318          *   decompress into destination's address_space (the file offset
2319          *   may change, so source mapping won't do), then recompress (or
2320          *   otherwise reinsert) a subrange.
2321          * - allow ranges within the same file to be cloned (provided
2322          *   they don't overlap)?
2323          */
2324
2325         /* the destination must be opened for writing */
2326         if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2327                 return -EINVAL;
2328
2329         if (btrfs_root_readonly(root))
2330                 return -EROFS;
2331
2332         ret = mnt_want_write_file(file);
2333         if (ret)
2334                 return ret;
2335
2336         src_file = fget(srcfd);
2337         if (!src_file) {
2338                 ret = -EBADF;
2339                 goto out_drop_write;
2340         }
2341
2342         src = src_file->f_dentry->d_inode;
2343
2344         ret = -EINVAL;
2345         if (src == inode)
2346                 goto out_fput;
2347
2348         /* the src must be open for reading */
2349         if (!(src_file->f_mode & FMODE_READ))
2350                 goto out_fput;
2351
2352         /* don't make the dst file partly checksummed */
2353         if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2354             (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2355                 goto out_fput;
2356
2357         ret = -EISDIR;
2358         if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2359                 goto out_fput;
2360
2361         ret = -EXDEV;
2362         if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2363                 goto out_fput;
2364
2365         ret = -ENOMEM;
2366         buf = vmalloc(btrfs_level_size(root, 0));
2367         if (!buf)
2368                 goto out_fput;
2369
2370         path = btrfs_alloc_path();
2371         if (!path) {
2372                 vfree(buf);
2373                 goto out_fput;
2374         }
2375         path->reada = 2;
2376
2377         if (inode < src) {
2378                 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2379                 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2380         } else {
2381                 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2382                 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2383         }
2384
2385         /* determine range to clone */
2386         ret = -EINVAL;
2387         if (off + len > src->i_size || off + len < off)
2388                 goto out_unlock;
2389         if (len == 0)
2390                 olen = len = src->i_size - off;
2391         /* if we extend to eof, continue to block boundary */
2392         if (off + len == src->i_size)
2393                 len = ALIGN(src->i_size, bs) - off;
2394
2395         /* verify the end result is block aligned */
2396         if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2397             !IS_ALIGNED(destoff, bs))
2398                 goto out_unlock;
2399
2400         if (destoff > inode->i_size) {
2401                 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2402                 if (ret)
2403                         goto out_unlock;
2404         }
2405
2406         /* truncate page cache pages from target inode range */
2407         truncate_inode_pages_range(&inode->i_data, destoff,
2408                                    PAGE_CACHE_ALIGN(destoff + len) - 1);
2409
2410         /* do any pending delalloc/csum calc on src, one way or
2411            another, and lock file content */
2412         while (1) {
2413                 struct btrfs_ordered_extent *ordered;
2414                 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2415                 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2416                 if (!ordered &&
2417                     !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2418                                    EXTENT_DELALLOC, 0, NULL))
2419                         break;
2420                 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2421                 if (ordered)
2422                         btrfs_put_ordered_extent(ordered);
2423                 btrfs_wait_ordered_range(src, off, len);
2424         }
2425
2426         /* clone data */
2427         key.objectid = btrfs_ino(src);
2428         key.type = BTRFS_EXTENT_DATA_KEY;
2429         key.offset = 0;
2430
2431         while (1) {
2432                 /*
2433                  * note the key will change type as we walk through the
2434                  * tree.
2435                  */
2436                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2437                 if (ret < 0)
2438                         goto out;
2439
2440                 nritems = btrfs_header_nritems(path->nodes[0]);
2441                 if (path->slots[0] >= nritems) {
2442                         ret = btrfs_next_leaf(root, path);
2443                         if (ret < 0)
2444                                 goto out;
2445                         if (ret > 0)
2446                                 break;
2447                         nritems = btrfs_header_nritems(path->nodes[0]);
2448                 }
2449                 leaf = path->nodes[0];
2450                 slot = path->slots[0];
2451
2452                 btrfs_item_key_to_cpu(leaf, &key, slot);
2453                 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2454                     key.objectid != btrfs_ino(src))
2455                         break;
2456
2457                 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2458                         struct btrfs_file_extent_item *extent;
2459                         int type;
2460                         u32 size;
2461                         struct btrfs_key new_key;
2462                         u64 disko = 0, diskl = 0;
2463                         u64 datao = 0, datal = 0;
2464                         u8 comp;
2465                         u64 endoff;
2466
2467                         size = btrfs_item_size_nr(leaf, slot);
2468                         read_extent_buffer(leaf, buf,
2469                                            btrfs_item_ptr_offset(leaf, slot),
2470                                            size);
2471
2472                         extent = btrfs_item_ptr(leaf, slot,
2473                                                 struct btrfs_file_extent_item);
2474                         comp = btrfs_file_extent_compression(leaf, extent);
2475                         type = btrfs_file_extent_type(leaf, extent);
2476                         if (type == BTRFS_FILE_EXTENT_REG ||
2477                             type == BTRFS_FILE_EXTENT_PREALLOC) {
2478                                 disko = btrfs_file_extent_disk_bytenr(leaf,
2479                                                                       extent);
2480                                 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2481                                                                  extent);
2482                                 datao = btrfs_file_extent_offset(leaf, extent);
2483                                 datal = btrfs_file_extent_num_bytes(leaf,
2484                                                                     extent);
2485                         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2486                                 /* take upper bound, may be compressed */
2487                                 datal = btrfs_file_extent_ram_bytes(leaf,
2488                                                                     extent);
2489                         }
2490                         btrfs_release_path(path);
2491
2492                         if (key.offset + datal <= off ||
2493                             key.offset >= off+len)
2494                                 goto next;
2495
2496                         memcpy(&new_key, &key, sizeof(new_key));
2497                         new_key.objectid = btrfs_ino(inode);
2498                         if (off <= key.offset)
2499                                 new_key.offset = key.offset + destoff - off;
2500                         else
2501                                 new_key.offset = destoff;
2502
2503                         /*
2504                          * 1 - adjusting old extent (we may have to split it)
2505                          * 1 - add new extent
2506                          * 1 - inode update
2507                          */
2508                         trans = btrfs_start_transaction(root, 3);
2509                         if (IS_ERR(trans)) {
2510                                 ret = PTR_ERR(trans);
2511                                 goto out;
2512                         }
2513
2514                         if (type == BTRFS_FILE_EXTENT_REG ||
2515                             type == BTRFS_FILE_EXTENT_PREALLOC) {
2516                                 /*
2517                                  *    a  | --- range to clone ---|  b
2518                                  * | ------------- extent ------------- |
2519                                  */
2520
2521                                 /* substract range b */
2522                                 if (key.offset + datal > off + len)
2523                                         datal = off + len - key.offset;
2524
2525                                 /* substract range a */
2526                                 if (off > key.offset) {
2527                                         datao += off - key.offset;
2528                                         datal -= off - key.offset;
2529                                 }
2530
2531                                 ret = btrfs_drop_extents(trans, inode,
2532                                                          new_key.offset,
2533                                                          new_key.offset + datal,
2534                                                          &hint_byte, 1);
2535                                 if (ret) {
2536                                         btrfs_abort_transaction(trans, root,
2537                                                                 ret);
2538                                         btrfs_end_transaction(trans, root);
2539                                         goto out;
2540                                 }
2541
2542                                 ret = btrfs_insert_empty_item(trans, root, path,
2543                                                               &new_key, size);
2544                                 if (ret) {
2545                                         btrfs_abort_transaction(trans, root,
2546                                                                 ret);
2547                                         btrfs_end_transaction(trans, root);
2548                                         goto out;
2549                                 }
2550
2551                                 leaf = path->nodes[0];
2552                                 slot = path->slots[0];
2553                                 write_extent_buffer(leaf, buf,
2554                                             btrfs_item_ptr_offset(leaf, slot),
2555                                             size);
2556
2557                                 extent = btrfs_item_ptr(leaf, slot,
2558                                                 struct btrfs_file_extent_item);
2559
2560                                 /* disko == 0 means it's a hole */
2561                                 if (!disko)
2562                                         datao = 0;
2563
2564                                 btrfs_set_file_extent_offset(leaf, extent,
2565                                                              datao);
2566                                 btrfs_set_file_extent_num_bytes(leaf, extent,
2567                                                                 datal);
2568                                 if (disko) {
2569                                         inode_add_bytes(inode, datal);
2570                                         ret = btrfs_inc_extent_ref(trans, root,
2571                                                         disko, diskl, 0,
2572                                                         root->root_key.objectid,
2573                                                         btrfs_ino(inode),
2574                                                         new_key.offset - datao,
2575                                                         0);
2576                                         if (ret) {
2577                                                 btrfs_abort_transaction(trans,
2578                                                                         root,
2579                                                                         ret);
2580                                                 btrfs_end_transaction(trans,
2581                                                                       root);
2582                                                 goto out;
2583
2584                                         }
2585                                 }
2586                         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2587                                 u64 skip = 0;
2588                                 u64 trim = 0;
2589                                 if (off > key.offset) {
2590                                         skip = off - key.offset;
2591                                         new_key.offset += skip;
2592                                 }
2593
2594                                 if (key.offset + datal > off+len)
2595                                         trim = key.offset + datal - (off+len);
2596
2597                                 if (comp && (skip || trim)) {
2598                                         ret = -EINVAL;
2599                                         btrfs_end_transaction(trans, root);
2600                                         goto out;
2601                                 }
2602                                 size -= skip + trim;
2603                                 datal -= skip + trim;
2604
2605                                 ret = btrfs_drop_extents(trans, inode,
2606                                                          new_key.offset,
2607                                                          new_key.offset + datal,
2608                                                          &hint_byte, 1);
2609                                 if (ret) {
2610                                         btrfs_abort_transaction(trans, root,
2611                                                                 ret);
2612                                         btrfs_end_transaction(trans, root);
2613                                         goto out;
2614                                 }
2615
2616                                 ret = btrfs_insert_empty_item(trans, root, path,
2617                                                               &new_key, size);
2618                                 if (ret) {
2619                                         btrfs_abort_transaction(trans, root,
2620                                                                 ret);
2621                                         btrfs_end_transaction(trans, root);
2622                                         goto out;
2623                                 }
2624
2625                                 if (skip) {
2626                                         u32 start =
2627                                           btrfs_file_extent_calc_inline_size(0);
2628                                         memmove(buf+start, buf+start+skip,
2629                                                 datal);
2630                                 }
2631
2632                                 leaf = path->nodes[0];
2633                                 slot = path->slots[0];
2634                                 write_extent_buffer(leaf, buf,
2635                                             btrfs_item_ptr_offset(leaf, slot),
2636                                             size);
2637                                 inode_add_bytes(inode, datal);
2638                         }
2639
2640                         btrfs_mark_buffer_dirty(leaf);
2641                         btrfs_release_path(path);
2642
2643                         inode_inc_iversion(inode);
2644                         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2645
2646                         /*
2647                          * we round up to the block size at eof when
2648                          * determining which extents to clone above,
2649                          * but shouldn't round up the file size
2650                          */
2651                         endoff = new_key.offset + datal;
2652                         if (endoff > destoff+olen)
2653                                 endoff = destoff+olen;
2654                         if (endoff > inode->i_size)
2655                                 btrfs_i_size_write(inode, endoff);
2656
2657                         ret = btrfs_update_inode(trans, root, inode);
2658                         if (ret) {
2659                                 btrfs_abort_transaction(trans, root, ret);
2660                                 btrfs_end_transaction(trans, root);
2661                                 goto out;
2662                         }
2663                         ret = btrfs_end_transaction(trans, root);
2664                 }
2665 next:
2666                 btrfs_release_path(path);
2667                 key.offset++;
2668         }
2669         ret = 0;
2670 out:
2671         btrfs_release_path(path);
2672         unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2673 out_unlock:
2674         mutex_unlock(&src->i_mutex);
2675         mutex_unlock(&inode->i_mutex);
2676         vfree(buf);
2677         btrfs_free_path(path);
2678 out_fput:
2679         fput(src_file);
2680 out_drop_write:
2681         mnt_drop_write_file(file);
2682         return ret;
2683 }
2684
2685 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2686 {
2687         struct btrfs_ioctl_clone_range_args args;
2688
2689         if (copy_from_user(&args, argp, sizeof(args)))
2690                 return -EFAULT;
2691         return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2692                                  args.src_length, args.dest_offset);
2693 }
2694
2695 /*
2696  * there are many ways the trans_start and trans_end ioctls can lead
2697  * to deadlocks.  They should only be used by applications that
2698  * basically own the machine, and have a very in depth understanding
2699  * of all the possible deadlocks and enospc problems.
2700  */
2701 static long btrfs_ioctl_trans_start(struct file *file)
2702 {
2703         struct inode *inode = fdentry(file)->d_inode;
2704         struct btrfs_root *root = BTRFS_I(inode)->root;
2705         struct btrfs_trans_handle *trans;
2706         int ret;
2707
2708         ret = -EPERM;
2709         if (!capable(CAP_SYS_ADMIN))
2710                 goto out;
2711
2712         ret = -EINPROGRESS;
2713         if (file->private_data)
2714                 goto out;
2715
2716         ret = -EROFS;
2717         if (btrfs_root_readonly(root))
2718                 goto out;
2719
2720         ret = mnt_want_write_file(file);
2721         if (ret)
2722                 goto out;
2723
2724         atomic_inc(&root->fs_info->open_ioctl_trans);
2725
2726         ret = -ENOMEM;
2727         trans = btrfs_start_ioctl_transaction(root);
2728         if (IS_ERR(trans))
2729                 goto out_drop;
2730
2731         file->private_data = trans;
2732         return 0;
2733
2734 out_drop:
2735         atomic_dec(&root->fs_info->open_ioctl_trans);
2736         mnt_drop_write_file(file);
2737 out:
2738         return ret;
2739 }
2740
2741 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2742 {
2743         struct inode *inode = fdentry(file)->d_inode;
2744         struct btrfs_root *root = BTRFS_I(inode)->root;
2745         struct btrfs_root *new_root;
2746         struct btrfs_dir_item *di;
2747         struct btrfs_trans_handle *trans;
2748         struct btrfs_path *path;
2749         struct btrfs_key location;
2750         struct btrfs_disk_key disk_key;
2751         struct btrfs_super_block *disk_super;
2752         u64 features;
2753         u64 objectid = 0;
2754         u64 dir_id;
2755
2756         if (!capable(CAP_SYS_ADMIN))
2757                 return -EPERM;
2758
2759         if (copy_from_user(&objectid, argp, sizeof(objectid)))
2760                 return -EFAULT;
2761
2762         if (!objectid)
2763                 objectid = root->root_key.objectid;
2764
2765         location.objectid = objectid;
2766         location.type = BTRFS_ROOT_ITEM_KEY;
2767         location.offset = (u64)-1;
2768
2769         new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2770         if (IS_ERR(new_root))
2771                 return PTR_ERR(new_root);
2772
2773         if (btrfs_root_refs(&new_root->root_item) == 0)
2774                 return -ENOENT;
2775
2776         path = btrfs_alloc_path();
2777         if (!path)
2778                 return -ENOMEM;
2779         path->leave_spinning = 1;
2780
2781         trans = btrfs_start_transaction(root, 1);
2782         if (IS_ERR(trans)) {
2783                 btrfs_free_path(path);
2784                 return PTR_ERR(trans);
2785         }
2786
2787         dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
2788         di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2789                                    dir_id, "default", 7, 1);
2790         if (IS_ERR_OR_NULL(di)) {
2791                 btrfs_free_path(path);
2792                 btrfs_end_transaction(trans, root);
2793                 printk(KERN_ERR "Umm, you don't have the default dir item, "
2794                        "this isn't going to work\n");
2795                 return -ENOENT;
2796         }
2797
2798         btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2799         btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2800         btrfs_mark_buffer_dirty(path->nodes[0]);
2801         btrfs_free_path(path);
2802
2803         disk_super = root->fs_info->super_copy;
2804         features = btrfs_super_incompat_flags(disk_super);
2805         if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2806                 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2807                 btrfs_set_super_incompat_flags(disk_super, features);
2808         }
2809         btrfs_end_transaction(trans, root);
2810
2811         return 0;
2812 }
2813
2814 static void get_block_group_info(struct list_head *groups_list,
2815                                  struct btrfs_ioctl_space_info *space)
2816 {
2817         struct btrfs_block_group_cache *block_group;
2818
2819         space->total_bytes = 0;
2820         space->used_bytes = 0;
2821         space->flags = 0;
2822         list_for_each_entry(block_group, groups_list, list) {
2823                 space->flags = block_group->flags;
2824                 space->total_bytes += block_group->key.offset;
2825                 space->used_bytes +=
2826                         btrfs_block_group_used(&block_group->item);
2827         }
2828 }
2829
2830 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2831 {
2832         struct btrfs_ioctl_space_args space_args;
2833         struct btrfs_ioctl_space_info space;
2834         struct btrfs_ioctl_space_info *dest;
2835         struct btrfs_ioctl_space_info *dest_orig;
2836         struct btrfs_ioctl_space_info __user *user_dest;
2837         struct btrfs_space_info *info;
2838         u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2839                        BTRFS_BLOCK_GROUP_SYSTEM,
2840                        BTRFS_BLOCK_GROUP_METADATA,
2841                        BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2842         int num_types = 4;
2843         int alloc_size;
2844         int ret = 0;
2845         u64 slot_count = 0;
2846         int i, c;
2847
2848         if (copy_from_user(&space_args,
2849                            (struct btrfs_ioctl_space_args __user *)arg,
2850                            sizeof(space_args)))
2851                 return -EFAULT;
2852
2853         for (i = 0; i < num_types; i++) {
2854                 struct btrfs_space_info *tmp;
2855
2856                 info = NULL;
2857                 rcu_read_lock();
2858                 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2859                                         list) {
2860                         if (tmp->flags == types[i]) {
2861                                 info = tmp;
2862                                 break;
2863                         }
2864                 }
2865                 rcu_read_unlock();
2866
2867                 if (!info)
2868                         continue;
2869
2870                 down_read(&info->groups_sem);
2871                 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2872                         if (!list_empty(&info->block_groups[c]))
2873                                 slot_count++;
2874                 }
2875                 up_read(&info->groups_sem);
2876         }
2877
2878         /* space_slots == 0 means they are asking for a count */
2879         if (space_args.space_slots == 0) {
2880                 space_args.total_spaces = slot_count;
2881                 goto out;
2882         }
2883
2884         slot_count = min_t(u64, space_args.space_slots, slot_count);
2885
2886         alloc_size = sizeof(*dest) * slot_count;
2887
2888         /* we generally have at most 6 or so space infos, one for each raid
2889          * level.  So, a whole page should be more than enough for everyone
2890          */
2891         if (alloc_size > PAGE_CACHE_SIZE)
2892                 return -ENOMEM;
2893
2894         space_args.total_spaces = 0;
2895         dest = kmalloc(alloc_size, GFP_NOFS);
2896         if (!dest)
2897                 return -ENOMEM;
2898         dest_orig = dest;
2899
2900         /* now we have a buffer to copy into */
2901         for (i = 0; i < num_types; i++) {
2902                 struct btrfs_space_info *tmp;
2903
2904                 if (!slot_count)
2905                         break;
2906
2907                 info = NULL;
2908                 rcu_read_lock();
2909                 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2910                                         list) {
2911                         if (tmp->flags == types[i]) {
2912                                 info = tmp;
2913                                 break;
2914                         }
2915                 }
2916                 rcu_read_unlock();
2917
2918                 if (!info)
2919                         continue;
2920                 down_read(&info->groups_sem);
2921                 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2922                         if (!list_empty(&info->block_groups[c])) {
2923                                 get_block_group_info(&info->block_groups[c],
2924                                                      &space);
2925                                 memcpy(dest, &space, sizeof(space));
2926                                 dest++;
2927                                 space_args.total_spaces++;
2928                                 slot_count--;
2929                         }
2930                         if (!slot_count)
2931                                 break;
2932                 }
2933                 up_read(&info->groups_sem);
2934         }
2935
2936         user_dest = (struct btrfs_ioctl_space_info __user *)
2937                 (arg + sizeof(struct btrfs_ioctl_space_args));
2938
2939         if (copy_to_user(user_dest, dest_orig, alloc_size))
2940                 ret = -EFAULT;
2941
2942         kfree(dest_orig);
2943 out:
2944         if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2945                 ret = -EFAULT;
2946
2947         return ret;
2948 }
2949
2950 /*
2951  * there are many ways the trans_start and trans_end ioctls can lead
2952  * to deadlocks.  They should only be used by applications that
2953  * basically own the machine, and have a very in depth understanding
2954  * of all the possible deadlocks and enospc problems.
2955  */
2956 long btrfs_ioctl_trans_end(struct file *file)
2957 {
2958         struct inode *inode = fdentry(file)->d_inode;
2959         struct btrfs_root *root = BTRFS_I(inode)->root;
2960         struct btrfs_trans_handle *trans;
2961
2962         trans = file->private_data;
2963         if (!trans)
2964                 return -EINVAL;
2965         file->private_data = NULL;
2966
2967         btrfs_end_transaction(trans, root);
2968
2969         atomic_dec(&root->fs_info->open_ioctl_trans);
2970
2971         mnt_drop_write_file(file);
2972         return 0;
2973 }
2974
2975 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2976 {
2977         struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2978         struct btrfs_trans_handle *trans;
2979         u64 transid;
2980         int ret;
2981
2982         trans = btrfs_start_transaction(root, 0);
2983         if (IS_ERR(trans))
2984                 return PTR_ERR(trans);
2985         transid = trans->transid;
2986         ret = btrfs_commit_transaction_async(trans, root, 0);
2987         if (ret) {
2988                 btrfs_end_transaction(trans, root);
2989                 return ret;
2990         }
2991
2992         if (argp)
2993                 if (copy_to_user(argp, &transid, sizeof(transid)))
2994                         return -EFAULT;
2995         return 0;
2996 }
2997
2998 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2999 {
3000         struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3001         u64 transid;
3002
3003         if (argp) {
3004                 if (copy_from_user(&transid, argp, sizeof(transid)))
3005                         return -EFAULT;
3006         } else {
3007                 transid = 0;  /* current trans */
3008         }
3009         return btrfs_wait_for_commit(root, transid);
3010 }
3011
3012 static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3013 {
3014         int ret;
3015         struct btrfs_ioctl_scrub_args *sa;
3016
3017         if (!capable(CAP_SYS_ADMIN))
3018                 return -EPERM;
3019
3020         sa = memdup_user(arg, sizeof(*sa));
3021         if (IS_ERR(sa))
3022                 return PTR_ERR(sa);
3023
3024         ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
3025                               &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
3026
3027         if (copy_to_user(arg, sa, sizeof(*sa)))
3028                 ret = -EFAULT;
3029
3030         kfree(sa);
3031         return ret;
3032 }
3033
3034 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3035 {
3036         if (!capable(CAP_SYS_ADMIN))
3037                 return -EPERM;
3038
3039         return btrfs_scrub_cancel(root);
3040 }
3041
3042 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3043                                        void __user *arg)
3044 {
3045         struct btrfs_ioctl_scrub_args *sa;
3046         int ret;
3047
3048         if (!capable(CAP_SYS_ADMIN))
3049                 return -EPERM;
3050
3051         sa = memdup_user(arg, sizeof(*sa));
3052         if (IS_ERR(sa))
3053                 return PTR_ERR(sa);
3054
3055         ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3056
3057         if (copy_to_user(arg, sa, sizeof(*sa)))
3058                 ret = -EFAULT;
3059
3060         kfree(sa);
3061         return ret;
3062 }
3063
3064 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3065                                       void __user *arg, int reset_after_read)
3066 {
3067         struct btrfs_ioctl_get_dev_stats *sa;
3068         int ret;
3069
3070         if (reset_after_read && !capable(CAP_SYS_ADMIN))
3071                 return -EPERM;
3072
3073         sa = memdup_user(arg, sizeof(*sa));
3074         if (IS_ERR(sa))
3075                 return PTR_ERR(sa);
3076
3077         ret = btrfs_get_dev_stats(root, sa, reset_after_read);
3078
3079         if (copy_to_user(arg, sa, sizeof(*sa)))
3080                 ret = -EFAULT;
3081
3082         kfree(sa);
3083         return ret;
3084 }
3085
3086 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3087 {
3088         int ret = 0;
3089         int i;
3090         u64 rel_ptr;
3091         int size;
3092         struct btrfs_ioctl_ino_path_args *ipa = NULL;
3093         struct inode_fs_paths *ipath = NULL;
3094         struct btrfs_path *path;
3095
3096         if (!capable(CAP_SYS_ADMIN))
3097                 return -EPERM;
3098
3099         path = btrfs_alloc_path();
3100         if (!path) {
3101                 ret = -ENOMEM;
3102                 goto out;
3103         }
3104
3105         ipa = memdup_user(arg, sizeof(*ipa));
3106         if (IS_ERR(ipa)) {
3107                 ret = PTR_ERR(ipa);
3108                 ipa = NULL;
3109                 goto out;
3110         }
3111
3112         size = min_t(u32, ipa->size, 4096);
3113         ipath = init_ipath(size, root, path);
3114         if (IS_ERR(ipath)) {
3115                 ret = PTR_ERR(ipath);
3116                 ipath = NULL;
3117                 goto out;
3118         }
3119
3120         ret = paths_from_inode(ipa->inum, ipath);
3121         if (ret < 0)
3122                 goto out;
3123
3124         for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3125                 rel_ptr = ipath->fspath->val[i] -
3126                           (u64)(unsigned long)ipath->fspath->val;
3127                 ipath->fspath->val[i] = rel_ptr;
3128         }
3129
3130         ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3131                            (void *)(unsigned long)ipath->fspath, size);
3132         if (ret) {
3133                 ret = -EFAULT;
3134                 goto out;
3135         }
3136
3137 out:
3138         btrfs_free_path(path);
3139         free_ipath(ipath);
3140         kfree(ipa);
3141
3142         return ret;
3143 }
3144
3145 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3146 {
3147         struct btrfs_data_container *inodes = ctx;
3148         const size_t c = 3 * sizeof(u64);
3149
3150         if (inodes->bytes_left >= c) {
3151                 inodes->bytes_left -= c;
3152                 inodes->val[inodes->elem_cnt] = inum;
3153                 inodes->val[inodes->elem_cnt + 1] = offset;
3154                 inodes->val[inodes->elem_cnt + 2] = root;
3155                 inodes->elem_cnt += 3;
3156         } else {
3157                 inodes->bytes_missing += c - inodes->bytes_left;
3158                 inodes->bytes_left = 0;
3159                 inodes->elem_missed += 3;
3160         }
3161
3162         return 0;
3163 }
3164
3165 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3166                                         void __user *arg)
3167 {
3168         int ret = 0;
3169         int size;
3170         u64 extent_item_pos;
3171         struct btrfs_ioctl_logical_ino_args *loi;
3172         struct btrfs_data_container *inodes = NULL;
3173         struct btrfs_path *path = NULL;
3174         struct btrfs_key key;
3175
3176         if (!capable(CAP_SYS_ADMIN))
3177                 return -EPERM;
3178
3179         loi = memdup_user(arg, sizeof(*loi));
3180         if (IS_ERR(loi)) {
3181                 ret = PTR_ERR(loi);
3182                 loi = NULL;
3183                 goto out;
3184         }
3185
3186         path = btrfs_alloc_path();
3187         if (!path) {
3188                 ret = -ENOMEM;
3189                 goto out;
3190         }
3191
3192         size = min_t(u32, loi->size, 4096);
3193         inodes = init_data_container(size);
3194         if (IS_ERR(inodes)) {
3195                 ret = PTR_ERR(inodes);
3196                 inodes = NULL;
3197                 goto out;
3198         }
3199
3200         ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
3201         btrfs_release_path(path);
3202
3203         if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3204                 ret = -ENOENT;
3205         if (ret < 0)
3206                 goto out;
3207
3208         extent_item_pos = loi->logical - key.objectid;
3209         ret = iterate_extent_inodes(root->fs_info, key.objectid,
3210                                         extent_item_pos, 0, build_ino_list,
3211                                         inodes);
3212
3213         if (ret < 0)
3214                 goto out;
3215
3216         ret = copy_to_user((void *)(unsigned long)loi->inodes,
3217                            (void *)(unsigned long)inodes, size);
3218         if (ret)
3219                 ret = -EFAULT;
3220
3221 out:
3222         btrfs_free_path(path);
3223         kfree(inodes);
3224         kfree(loi);
3225
3226         return ret;
3227 }
3228
3229 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3230                                struct btrfs_ioctl_balance_args *bargs)
3231 {
3232         struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3233
3234         bargs->flags = bctl->flags;
3235
3236         if (atomic_read(&fs_info->balance_running))
3237                 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3238         if (atomic_read(&fs_info->balance_pause_req))
3239                 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3240         if (atomic_read(&fs_info->balance_cancel_req))
3241                 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3242
3243         memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3244         memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3245         memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3246
3247         if (lock) {
3248                 spin_lock(&fs_info->balance_lock);
3249                 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3250                 spin_unlock(&fs_info->balance_lock);
3251         } else {
3252                 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3253         }
3254 }
3255
3256 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3257 {
3258         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3259         struct btrfs_fs_info *fs_info = root->fs_info;
3260         struct btrfs_ioctl_balance_args *bargs;
3261         struct btrfs_balance_control *bctl;
3262         int ret;
3263
3264         if (!capable(CAP_SYS_ADMIN))
3265                 return -EPERM;
3266
3267         if (fs_info->sb->s_flags & MS_RDONLY)
3268                 return -EROFS;
3269
3270         ret = mnt_want_write_file(file);
3271         if (ret)
3272                 return ret;
3273
3274         mutex_lock(&fs_info->volume_mutex);
3275         mutex_lock(&fs_info->balance_mutex);
3276
3277         if (arg) {
3278                 bargs = memdup_user(arg, sizeof(*bargs));
3279                 if (IS_ERR(bargs)) {
3280                         ret = PTR_ERR(bargs);
3281                         goto out;
3282                 }
3283
3284                 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3285                         if (!fs_info->balance_ctl) {
3286                                 ret = -ENOTCONN;
3287                                 goto out_bargs;
3288                         }
3289
3290                         bctl = fs_info->balance_ctl;
3291                         spin_lock(&fs_info->balance_lock);
3292                         bctl->flags |= BTRFS_BALANCE_RESUME;
3293                         spin_unlock(&fs_info->balance_lock);
3294
3295                         goto do_balance;
3296                 }
3297         } else {
3298                 bargs = NULL;
3299         }
3300
3301         if (fs_info->balance_ctl) {
3302                 ret = -EINPROGRESS;
3303                 goto out_bargs;
3304         }
3305
3306         bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3307         if (!bctl) {
3308                 ret = -ENOMEM;
3309                 goto out_bargs;
3310         }
3311
3312         bctl->fs_info = fs_info;
3313         if (arg) {
3314                 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3315                 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3316                 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3317
3318                 bctl->flags = bargs->flags;
3319         } else {
3320                 /* balance everything - no filters */
3321                 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3322         }
3323
3324 do_balance:
3325         ret = btrfs_balance(bctl, bargs);
3326         /*
3327          * bctl is freed in __cancel_balance or in free_fs_info if
3328          * restriper was paused all the way until unmount
3329          */
3330         if (arg) {
3331                 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3332                         ret = -EFAULT;
3333         }
3334
3335 out_bargs:
3336         kfree(bargs);
3337 out:
3338         mutex_unlock(&fs_info->balance_mutex);
3339         mutex_unlock(&fs_info->volume_mutex);
3340         mnt_drop_write_file(file);
3341         return ret;
3342 }
3343
3344 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3345 {
3346         if (!capable(CAP_SYS_ADMIN))
3347                 return -EPERM;
3348
3349         switch (cmd) {
3350         case BTRFS_BALANCE_CTL_PAUSE:
3351                 return btrfs_pause_balance(root->fs_info);
3352         case BTRFS_BALANCE_CTL_CANCEL:
3353                 return btrfs_cancel_balance(root->fs_info);
3354         }
3355
3356         return -EINVAL;
3357 }
3358
3359 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3360                                          void __user *arg)
3361 {
3362         struct btrfs_fs_info *fs_info = root->fs_info;
3363         struct btrfs_ioctl_balance_args *bargs;
3364         int ret = 0;
3365
3366         if (!capable(CAP_SYS_ADMIN))
3367                 return -EPERM;
3368
3369         mutex_lock(&fs_info->balance_mutex);
3370         if (!fs_info->balance_ctl) {
3371                 ret = -ENOTCONN;
3372                 goto out;
3373         }
3374
3375         bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3376         if (!bargs) {
3377                 ret = -ENOMEM;
3378                 goto out;
3379         }
3380
3381         update_ioctl_balance_args(fs_info, 1, bargs);
3382
3383         if (copy_to_user(arg, bargs, sizeof(*bargs)))
3384                 ret = -EFAULT;
3385
3386         kfree(bargs);
3387 out:
3388         mutex_unlock(&fs_info->balance_mutex);
3389         return ret;
3390 }
3391
3392 long btrfs_ioctl(struct file *file, unsigned int
3393                 cmd, unsigned long arg)
3394 {
3395         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3396         void __user *argp = (void __user *)arg;
3397
3398         switch (cmd) {
3399         case FS_IOC_GETFLAGS:
3400                 return btrfs_ioctl_getflags(file, argp);
3401         case FS_IOC_SETFLAGS:
3402                 return btrfs_ioctl_setflags(file, argp);
3403         case FS_IOC_GETVERSION:
3404                 return btrfs_ioctl_getversion(file, argp);
3405         case FITRIM:
3406                 return btrfs_ioctl_fitrim(file, argp);
3407         case BTRFS_IOC_SNAP_CREATE:
3408                 return btrfs_ioctl_snap_create(file, argp, 0);
3409         case BTRFS_IOC_SNAP_CREATE_V2:
3410                 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3411         case BTRFS_IOC_SUBVOL_CREATE:
3412                 return btrfs_ioctl_snap_create(file, argp, 1);
3413         case BTRFS_IOC_SNAP_DESTROY:
3414                 return btrfs_ioctl_snap_destroy(file, argp);
3415         case BTRFS_IOC_SUBVOL_GETFLAGS:
3416                 return btrfs_ioctl_subvol_getflags(file, argp);
3417         case BTRFS_IOC_SUBVOL_SETFLAGS:
3418                 return btrfs_ioctl_subvol_setflags(file, argp);
3419         case BTRFS_IOC_DEFAULT_SUBVOL:
3420                 return btrfs_ioctl_default_subvol(file, argp);
3421         case BTRFS_IOC_DEFRAG:
3422                 return btrfs_ioctl_defrag(file, NULL);
3423         case BTRFS_IOC_DEFRAG_RANGE:
3424                 return btrfs_ioctl_defrag(file, argp);
3425         case BTRFS_IOC_RESIZE:
3426                 return btrfs_ioctl_resize(root, argp);
3427         case BTRFS_IOC_ADD_DEV:
3428                 return btrfs_ioctl_add_dev(root, argp);
3429         case BTRFS_IOC_RM_DEV:
3430                 return btrfs_ioctl_rm_dev(root, argp);
3431         case BTRFS_IOC_FS_INFO:
3432                 return btrfs_ioctl_fs_info(root, argp);
3433         case BTRFS_IOC_DEV_INFO:
3434                 return btrfs_ioctl_dev_info(root, argp);
3435         case BTRFS_IOC_BALANCE:
3436                 return btrfs_ioctl_balance(file, NULL);
3437         case BTRFS_IOC_CLONE:
3438                 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3439         case BTRFS_IOC_CLONE_RANGE:
3440                 return btrfs_ioctl_clone_range(file, argp);
3441         case BTRFS_IOC_TRANS_START:
3442                 return btrfs_ioctl_trans_start(file);
3443         case BTRFS_IOC_TRANS_END:
3444                 return btrfs_ioctl_trans_end(file);
3445         case BTRFS_IOC_TREE_SEARCH:
3446                 return btrfs_ioctl_tree_search(file, argp);
3447         case BTRFS_IOC_INO_LOOKUP:
3448                 return btrfs_ioctl_ino_lookup(file, argp);
3449         case BTRFS_IOC_INO_PATHS:
3450                 return btrfs_ioctl_ino_to_path(root, argp);
3451         case BTRFS_IOC_LOGICAL_INO:
3452                 return btrfs_ioctl_logical_to_ino(root, argp);
3453         case BTRFS_IOC_SPACE_INFO:
3454                 return btrfs_ioctl_space_info(root, argp);
3455         case BTRFS_IOC_SYNC:
3456                 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3457                 return 0;
3458         case BTRFS_IOC_START_SYNC:
3459                 return btrfs_ioctl_start_sync(file, argp);
3460         case BTRFS_IOC_WAIT_SYNC:
3461                 return btrfs_ioctl_wait_sync(file, argp);
3462         case BTRFS_IOC_SCRUB:
3463                 return btrfs_ioctl_scrub(root, argp);
3464         case BTRFS_IOC_SCRUB_CANCEL:
3465                 return btrfs_ioctl_scrub_cancel(root, argp);
3466         case BTRFS_IOC_SCRUB_PROGRESS:
3467                 return btrfs_ioctl_scrub_progress(root, argp);
3468         case BTRFS_IOC_BALANCE_V2:
3469                 return btrfs_ioctl_balance(file, argp);
3470         case BTRFS_IOC_BALANCE_CTL:
3471                 return btrfs_ioctl_balance_ctl(root, arg);
3472         case BTRFS_IOC_BALANCE_PROGRESS:
3473                 return btrfs_ioctl_balance_progress(root, argp);
3474         case BTRFS_IOC_GET_DEV_STATS:
3475                 return btrfs_ioctl_get_dev_stats(root, argp, 0);
3476         case BTRFS_IOC_GET_AND_RESET_DEV_STATS:
3477                 return btrfs_ioctl_get_dev_stats(root, argp, 1);
3478         }
3479
3480         return -ENOTTY;
3481 }