2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/fsnotify.h>
25 #include <linux/pagemap.h>
26 #include <linux/highmem.h>
27 #include <linux/time.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mount.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/swap.h>
35 #include <linux/writeback.h>
36 #include <linux/statfs.h>
37 #include <linux/compat.h>
38 #include <linux/bit_spinlock.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/vmalloc.h>
42 #include <linux/slab.h>
43 #include <linux/blkdev.h>
44 #include <linux/uuid.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
51 #include "print-tree.h"
54 #include "inode-map.h"
56 #include "rcu-string.h"
58 #include "dev-replace.h"
60 /* Mask out flags that are inappropriate for the given type of inode. */
61 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
65 else if (S_ISREG(mode))
66 return flags & ~FS_DIRSYNC_FL;
68 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
72 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
74 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
76 unsigned int iflags = 0;
78 if (flags & BTRFS_INODE_SYNC)
80 if (flags & BTRFS_INODE_IMMUTABLE)
81 iflags |= FS_IMMUTABLE_FL;
82 if (flags & BTRFS_INODE_APPEND)
83 iflags |= FS_APPEND_FL;
84 if (flags & BTRFS_INODE_NODUMP)
85 iflags |= FS_NODUMP_FL;
86 if (flags & BTRFS_INODE_NOATIME)
87 iflags |= FS_NOATIME_FL;
88 if (flags & BTRFS_INODE_DIRSYNC)
89 iflags |= FS_DIRSYNC_FL;
90 if (flags & BTRFS_INODE_NODATACOW)
91 iflags |= FS_NOCOW_FL;
93 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
94 iflags |= FS_COMPR_FL;
95 else if (flags & BTRFS_INODE_NOCOMPRESS)
96 iflags |= FS_NOCOMP_FL;
102 * Update inode->i_flags based on the btrfs internal flags.
104 void btrfs_update_iflags(struct inode *inode)
106 struct btrfs_inode *ip = BTRFS_I(inode);
108 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
110 if (ip->flags & BTRFS_INODE_SYNC)
111 inode->i_flags |= S_SYNC;
112 if (ip->flags & BTRFS_INODE_IMMUTABLE)
113 inode->i_flags |= S_IMMUTABLE;
114 if (ip->flags & BTRFS_INODE_APPEND)
115 inode->i_flags |= S_APPEND;
116 if (ip->flags & BTRFS_INODE_NOATIME)
117 inode->i_flags |= S_NOATIME;
118 if (ip->flags & BTRFS_INODE_DIRSYNC)
119 inode->i_flags |= S_DIRSYNC;
123 * Inherit flags from the parent inode.
125 * Currently only the compression flags and the cow flags are inherited.
127 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
134 flags = BTRFS_I(dir)->flags;
136 if (flags & BTRFS_INODE_NOCOMPRESS) {
137 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
138 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
139 } else if (flags & BTRFS_INODE_COMPRESS) {
140 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
141 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
144 if (flags & BTRFS_INODE_NODATACOW)
145 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
147 btrfs_update_iflags(inode);
150 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
152 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
153 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
155 if (copy_to_user(arg, &flags, sizeof(flags)))
160 static int check_flags(unsigned int flags)
162 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
163 FS_NOATIME_FL | FS_NODUMP_FL | \
164 FS_SYNC_FL | FS_DIRSYNC_FL | \
165 FS_NOCOMP_FL | FS_COMPR_FL |
169 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
175 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
177 struct inode *inode = file->f_path.dentry->d_inode;
178 struct btrfs_inode *ip = BTRFS_I(inode);
179 struct btrfs_root *root = ip->root;
180 struct btrfs_trans_handle *trans;
181 unsigned int flags, oldflags;
184 unsigned int i_oldflags;
187 if (btrfs_root_readonly(root))
190 if (copy_from_user(&flags, arg, sizeof(flags)))
193 ret = check_flags(flags);
197 if (!inode_owner_or_capable(inode))
200 ret = mnt_want_write_file(file);
204 mutex_lock(&inode->i_mutex);
206 ip_oldflags = ip->flags;
207 i_oldflags = inode->i_flags;
208 mode = inode->i_mode;
210 flags = btrfs_mask_flags(inode->i_mode, flags);
211 oldflags = btrfs_flags_to_ioctl(ip->flags);
212 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
213 if (!capable(CAP_LINUX_IMMUTABLE)) {
219 if (flags & FS_SYNC_FL)
220 ip->flags |= BTRFS_INODE_SYNC;
222 ip->flags &= ~BTRFS_INODE_SYNC;
223 if (flags & FS_IMMUTABLE_FL)
224 ip->flags |= BTRFS_INODE_IMMUTABLE;
226 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
227 if (flags & FS_APPEND_FL)
228 ip->flags |= BTRFS_INODE_APPEND;
230 ip->flags &= ~BTRFS_INODE_APPEND;
231 if (flags & FS_NODUMP_FL)
232 ip->flags |= BTRFS_INODE_NODUMP;
234 ip->flags &= ~BTRFS_INODE_NODUMP;
235 if (flags & FS_NOATIME_FL)
236 ip->flags |= BTRFS_INODE_NOATIME;
238 ip->flags &= ~BTRFS_INODE_NOATIME;
239 if (flags & FS_DIRSYNC_FL)
240 ip->flags |= BTRFS_INODE_DIRSYNC;
242 ip->flags &= ~BTRFS_INODE_DIRSYNC;
243 if (flags & FS_NOCOW_FL) {
246 * It's safe to turn csums off here, no extents exist.
247 * Otherwise we want the flag to reflect the real COW
248 * status of the file and will not set it.
250 if (inode->i_size == 0)
251 ip->flags |= BTRFS_INODE_NODATACOW
252 | BTRFS_INODE_NODATASUM;
254 ip->flags |= BTRFS_INODE_NODATACOW;
258 * Revert back under same assuptions as above
261 if (inode->i_size == 0)
262 ip->flags &= ~(BTRFS_INODE_NODATACOW
263 | BTRFS_INODE_NODATASUM);
265 ip->flags &= ~BTRFS_INODE_NODATACOW;
270 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
271 * flag may be changed automatically if compression code won't make
274 if (flags & FS_NOCOMP_FL) {
275 ip->flags &= ~BTRFS_INODE_COMPRESS;
276 ip->flags |= BTRFS_INODE_NOCOMPRESS;
277 } else if (flags & FS_COMPR_FL) {
278 ip->flags |= BTRFS_INODE_COMPRESS;
279 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
281 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
284 trans = btrfs_start_transaction(root, 1);
286 ret = PTR_ERR(trans);
290 btrfs_update_iflags(inode);
291 inode_inc_iversion(inode);
292 inode->i_ctime = CURRENT_TIME;
293 ret = btrfs_update_inode(trans, root, inode);
295 btrfs_end_transaction(trans, root);
298 ip->flags = ip_oldflags;
299 inode->i_flags = i_oldflags;
303 mutex_unlock(&inode->i_mutex);
304 mnt_drop_write_file(file);
308 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
310 struct inode *inode = file->f_path.dentry->d_inode;
312 return put_user(inode->i_generation, arg);
315 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
317 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
318 struct btrfs_device *device;
319 struct request_queue *q;
320 struct fstrim_range range;
321 u64 minlen = ULLONG_MAX;
323 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
326 if (!capable(CAP_SYS_ADMIN))
330 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
334 q = bdev_get_queue(device->bdev);
335 if (blk_queue_discard(q)) {
337 minlen = min((u64)q->limits.discard_granularity,
345 if (copy_from_user(&range, arg, sizeof(range)))
347 if (range.start > total_bytes ||
348 range.len < fs_info->sb->s_blocksize)
351 range.len = min(range.len, total_bytes - range.start);
352 range.minlen = max(range.minlen, minlen);
353 ret = btrfs_trim_fs(fs_info->tree_root, &range);
357 if (copy_to_user(arg, &range, sizeof(range)))
363 static noinline int create_subvol(struct btrfs_root *root,
364 struct dentry *dentry,
365 char *name, int namelen,
367 struct btrfs_qgroup_inherit **inherit)
369 struct btrfs_trans_handle *trans;
370 struct btrfs_key key;
371 struct btrfs_root_item root_item;
372 struct btrfs_inode_item *inode_item;
373 struct extent_buffer *leaf;
374 struct btrfs_root *new_root;
375 struct dentry *parent = dentry->d_parent;
377 struct timespec cur_time = CURRENT_TIME;
381 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
385 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
389 dir = parent->d_inode;
397 trans = btrfs_start_transaction(root, 6);
399 return PTR_ERR(trans);
401 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
402 inherit ? *inherit : NULL);
406 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
407 0, objectid, NULL, 0, 0, 0);
413 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
414 btrfs_set_header_bytenr(leaf, leaf->start);
415 btrfs_set_header_generation(leaf, trans->transid);
416 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
417 btrfs_set_header_owner(leaf, objectid);
419 write_extent_buffer(leaf, root->fs_info->fsid,
420 (unsigned long)btrfs_header_fsid(leaf),
422 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
423 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
425 btrfs_mark_buffer_dirty(leaf);
427 memset(&root_item, 0, sizeof(root_item));
429 inode_item = &root_item.inode;
430 inode_item->generation = cpu_to_le64(1);
431 inode_item->size = cpu_to_le64(3);
432 inode_item->nlink = cpu_to_le32(1);
433 inode_item->nbytes = cpu_to_le64(root->leafsize);
434 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
437 root_item.byte_limit = 0;
438 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
440 btrfs_set_root_bytenr(&root_item, leaf->start);
441 btrfs_set_root_generation(&root_item, trans->transid);
442 btrfs_set_root_level(&root_item, 0);
443 btrfs_set_root_refs(&root_item, 1);
444 btrfs_set_root_used(&root_item, leaf->len);
445 btrfs_set_root_last_snapshot(&root_item, 0);
447 btrfs_set_root_generation_v2(&root_item,
448 btrfs_root_generation(&root_item));
449 uuid_le_gen(&new_uuid);
450 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
451 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
452 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
453 root_item.ctime = root_item.otime;
454 btrfs_set_root_ctransid(&root_item, trans->transid);
455 btrfs_set_root_otransid(&root_item, trans->transid);
457 btrfs_tree_unlock(leaf);
458 free_extent_buffer(leaf);
461 btrfs_set_root_dirid(&root_item, new_dirid);
463 key.objectid = objectid;
465 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
466 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
471 key.offset = (u64)-1;
472 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
473 if (IS_ERR(new_root)) {
474 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
475 ret = PTR_ERR(new_root);
479 btrfs_record_root_in_trans(trans, new_root);
481 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
483 /* We potentially lose an unused inode item here */
484 btrfs_abort_transaction(trans, root, ret);
489 * insert the directory item
491 ret = btrfs_set_inode_index(dir, &index);
493 btrfs_abort_transaction(trans, root, ret);
497 ret = btrfs_insert_dir_item(trans, root,
498 name, namelen, dir, &key,
499 BTRFS_FT_DIR, index);
501 btrfs_abort_transaction(trans, root, ret);
505 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
506 ret = btrfs_update_inode(trans, root, dir);
509 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
510 objectid, root->root_key.objectid,
511 btrfs_ino(dir), index, name, namelen);
515 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
518 *async_transid = trans->transid;
519 err = btrfs_commit_transaction_async(trans, root, 1);
521 err = btrfs_commit_transaction(trans, root);
528 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
529 char *name, int namelen, u64 *async_transid,
530 bool readonly, struct btrfs_qgroup_inherit **inherit)
533 struct btrfs_pending_snapshot *pending_snapshot;
534 struct btrfs_trans_handle *trans;
540 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
541 if (!pending_snapshot)
544 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
545 BTRFS_BLOCK_RSV_TEMP);
546 pending_snapshot->dentry = dentry;
547 pending_snapshot->root = root;
548 pending_snapshot->readonly = readonly;
550 pending_snapshot->inherit = *inherit;
551 *inherit = NULL; /* take responsibility to free it */
554 trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
556 ret = PTR_ERR(trans);
560 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
563 spin_lock(&root->fs_info->trans_lock);
564 list_add(&pending_snapshot->list,
565 &trans->transaction->pending_snapshots);
566 spin_unlock(&root->fs_info->trans_lock);
568 *async_transid = trans->transid;
569 ret = btrfs_commit_transaction_async(trans,
570 root->fs_info->extent_root, 1);
572 ret = btrfs_commit_transaction(trans,
573 root->fs_info->extent_root);
576 /* cleanup_transaction has freed this for us */
578 pending_snapshot = NULL;
582 ret = pending_snapshot->error;
586 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
590 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
592 ret = PTR_ERR(inode);
596 d_instantiate(dentry, inode);
599 kfree(pending_snapshot);
603 /* copy of check_sticky in fs/namei.c()
604 * It's inline, so penalty for filesystems that don't use sticky bit is
607 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
609 kuid_t fsuid = current_fsuid();
611 if (!(dir->i_mode & S_ISVTX))
613 if (uid_eq(inode->i_uid, fsuid))
615 if (uid_eq(dir->i_uid, fsuid))
617 return !capable(CAP_FOWNER);
620 /* copy of may_delete in fs/namei.c()
621 * Check whether we can remove a link victim from directory dir, check
622 * whether the type of victim is right.
623 * 1. We can't do it if dir is read-only (done in permission())
624 * 2. We should have write and exec permissions on dir
625 * 3. We can't remove anything from append-only dir
626 * 4. We can't do anything with immutable dir (done in permission())
627 * 5. If the sticky bit on dir is set we should either
628 * a. be owner of dir, or
629 * b. be owner of victim, or
630 * c. have CAP_FOWNER capability
631 * 6. If the victim is append-only or immutable we can't do antyhing with
632 * links pointing to it.
633 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
634 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
635 * 9. We can't remove a root or mountpoint.
636 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
637 * nfs_async_unlink().
640 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
644 if (!victim->d_inode)
647 BUG_ON(victim->d_parent->d_inode != dir);
648 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
650 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
655 if (btrfs_check_sticky(dir, victim->d_inode)||
656 IS_APPEND(victim->d_inode)||
657 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
660 if (!S_ISDIR(victim->d_inode->i_mode))
664 } else if (S_ISDIR(victim->d_inode->i_mode))
668 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
673 /* copy of may_create in fs/namei.c() */
674 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
680 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
684 * Create a new subvolume below @parent. This is largely modeled after
685 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
686 * inside this filesystem so it's quite a bit simpler.
688 static noinline int btrfs_mksubvol(struct path *parent,
689 char *name, int namelen,
690 struct btrfs_root *snap_src,
691 u64 *async_transid, bool readonly,
692 struct btrfs_qgroup_inherit **inherit)
694 struct inode *dir = parent->dentry->d_inode;
695 struct dentry *dentry;
698 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
700 dentry = lookup_one_len(name, parent->dentry, namelen);
701 error = PTR_ERR(dentry);
709 error = btrfs_may_create(dir, dentry);
713 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
715 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
719 error = create_snapshot(snap_src, dentry, name, namelen,
720 async_transid, readonly, inherit);
722 error = create_subvol(BTRFS_I(dir)->root, dentry,
723 name, namelen, async_transid, inherit);
726 fsnotify_mkdir(dir, dentry);
728 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
732 mutex_unlock(&dir->i_mutex);
737 * When we're defragging a range, we don't want to kick it off again
738 * if it is really just waiting for delalloc to send it down.
739 * If we find a nice big extent or delalloc range for the bytes in the
740 * file you want to defrag, we return 0 to let you know to skip this
743 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
745 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
746 struct extent_map *em = NULL;
747 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
750 read_lock(&em_tree->lock);
751 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
752 read_unlock(&em_tree->lock);
755 end = extent_map_end(em);
757 if (end - offset > thresh)
760 /* if we already have a nice delalloc here, just stop */
762 end = count_range_bits(io_tree, &offset, offset + thresh,
763 thresh, EXTENT_DELALLOC, 1);
770 * helper function to walk through a file and find extents
771 * newer than a specific transid, and smaller than thresh.
773 * This is used by the defragging code to find new and small
776 static int find_new_extents(struct btrfs_root *root,
777 struct inode *inode, u64 newer_than,
778 u64 *off, int thresh)
780 struct btrfs_path *path;
781 struct btrfs_key min_key;
782 struct btrfs_key max_key;
783 struct extent_buffer *leaf;
784 struct btrfs_file_extent_item *extent;
787 u64 ino = btrfs_ino(inode);
789 path = btrfs_alloc_path();
793 min_key.objectid = ino;
794 min_key.type = BTRFS_EXTENT_DATA_KEY;
795 min_key.offset = *off;
797 max_key.objectid = ino;
798 max_key.type = (u8)-1;
799 max_key.offset = (u64)-1;
801 path->keep_locks = 1;
804 ret = btrfs_search_forward(root, &min_key, &max_key,
805 path, 0, newer_than);
808 if (min_key.objectid != ino)
810 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
813 leaf = path->nodes[0];
814 extent = btrfs_item_ptr(leaf, path->slots[0],
815 struct btrfs_file_extent_item);
817 type = btrfs_file_extent_type(leaf, extent);
818 if (type == BTRFS_FILE_EXTENT_REG &&
819 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
820 check_defrag_in_cache(inode, min_key.offset, thresh)) {
821 *off = min_key.offset;
822 btrfs_free_path(path);
826 if (min_key.offset == (u64)-1)
830 btrfs_release_path(path);
833 btrfs_free_path(path);
837 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
839 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
840 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
841 struct extent_map *em;
842 u64 len = PAGE_CACHE_SIZE;
845 * hopefully we have this extent in the tree already, try without
846 * the full extent lock
848 read_lock(&em_tree->lock);
849 em = lookup_extent_mapping(em_tree, start, len);
850 read_unlock(&em_tree->lock);
853 /* get the big lock and read metadata off disk */
854 lock_extent(io_tree, start, start + len - 1);
855 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
856 unlock_extent(io_tree, start, start + len - 1);
865 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
867 struct extent_map *next;
870 /* this is the last extent */
871 if (em->start + em->len >= i_size_read(inode))
874 next = defrag_lookup_extent(inode, em->start + em->len);
875 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
878 free_extent_map(next);
882 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
883 u64 *last_len, u64 *skip, u64 *defrag_end,
886 struct extent_map *em;
888 bool next_mergeable = true;
891 * make sure that once we start defragging an extent, we keep on
894 if (start < *defrag_end)
899 em = defrag_lookup_extent(inode, start);
903 /* this will cover holes, and inline extents */
904 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
909 next_mergeable = defrag_check_next_extent(inode, em);
912 * we hit a real extent, if it is big or the next extent is not a
913 * real extent, don't bother defragging it
915 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
916 (em->len >= thresh || !next_mergeable))
920 * last_len ends up being a counter of how many bytes we've defragged.
921 * every time we choose not to defrag an extent, we reset *last_len
922 * so that the next tiny extent will force a defrag.
924 * The end result of this is that tiny extents before a single big
925 * extent will force at least part of that big extent to be defragged.
928 *defrag_end = extent_map_end(em);
931 *skip = extent_map_end(em);
940 * it doesn't do much good to defrag one or two pages
941 * at a time. This pulls in a nice chunk of pages
944 * It also makes sure the delalloc code has enough
945 * dirty data to avoid making new small extents as part
948 * It's a good idea to start RA on this range
949 * before calling this.
951 static int cluster_pages_for_defrag(struct inode *inode,
953 unsigned long start_index,
956 unsigned long file_end;
957 u64 isize = i_size_read(inode);
964 struct btrfs_ordered_extent *ordered;
965 struct extent_state *cached_state = NULL;
966 struct extent_io_tree *tree;
967 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
969 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
970 if (!isize || start_index > file_end)
973 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
975 ret = btrfs_delalloc_reserve_space(inode,
976 page_cnt << PAGE_CACHE_SHIFT);
980 tree = &BTRFS_I(inode)->io_tree;
982 /* step one, lock all the pages */
983 for (i = 0; i < page_cnt; i++) {
986 page = find_or_create_page(inode->i_mapping,
987 start_index + i, mask);
991 page_start = page_offset(page);
992 page_end = page_start + PAGE_CACHE_SIZE - 1;
994 lock_extent(tree, page_start, page_end);
995 ordered = btrfs_lookup_ordered_extent(inode,
997 unlock_extent(tree, page_start, page_end);
1002 btrfs_start_ordered_extent(inode, ordered, 1);
1003 btrfs_put_ordered_extent(ordered);
1006 * we unlocked the page above, so we need check if
1007 * it was released or not.
1009 if (page->mapping != inode->i_mapping) {
1011 page_cache_release(page);
1016 if (!PageUptodate(page)) {
1017 btrfs_readpage(NULL, page);
1019 if (!PageUptodate(page)) {
1021 page_cache_release(page);
1027 if (page->mapping != inode->i_mapping) {
1029 page_cache_release(page);
1039 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1043 * so now we have a nice long stream of locked
1044 * and up to date pages, lets wait on them
1046 for (i = 0; i < i_done; i++)
1047 wait_on_page_writeback(pages[i]);
1049 page_start = page_offset(pages[0]);
1050 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1052 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1053 page_start, page_end - 1, 0, &cached_state);
1054 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1055 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1056 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1057 &cached_state, GFP_NOFS);
1059 if (i_done != page_cnt) {
1060 spin_lock(&BTRFS_I(inode)->lock);
1061 BTRFS_I(inode)->outstanding_extents++;
1062 spin_unlock(&BTRFS_I(inode)->lock);
1063 btrfs_delalloc_release_space(inode,
1064 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1068 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1069 &cached_state, GFP_NOFS);
1071 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1072 page_start, page_end - 1, &cached_state,
1075 for (i = 0; i < i_done; i++) {
1076 clear_page_dirty_for_io(pages[i]);
1077 ClearPageChecked(pages[i]);
1078 set_page_extent_mapped(pages[i]);
1079 set_page_dirty(pages[i]);
1080 unlock_page(pages[i]);
1081 page_cache_release(pages[i]);
1085 for (i = 0; i < i_done; i++) {
1086 unlock_page(pages[i]);
1087 page_cache_release(pages[i]);
1089 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1094 int btrfs_defrag_file(struct inode *inode, struct file *file,
1095 struct btrfs_ioctl_defrag_range_args *range,
1096 u64 newer_than, unsigned long max_to_defrag)
1098 struct btrfs_root *root = BTRFS_I(inode)->root;
1099 struct file_ra_state *ra = NULL;
1100 unsigned long last_index;
1101 u64 isize = i_size_read(inode);
1105 u64 newer_off = range->start;
1107 unsigned long ra_index = 0;
1109 int defrag_count = 0;
1110 int compress_type = BTRFS_COMPRESS_ZLIB;
1111 int extent_thresh = range->extent_thresh;
1112 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1113 int cluster = max_cluster;
1114 u64 new_align = ~((u64)128 * 1024 - 1);
1115 struct page **pages = NULL;
1117 if (extent_thresh == 0)
1118 extent_thresh = 256 * 1024;
1120 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1121 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1123 if (range->compress_type)
1124 compress_type = range->compress_type;
1131 * if we were not given a file, allocate a readahead
1135 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1138 file_ra_state_init(ra, inode->i_mapping);
1143 pages = kmalloc(sizeof(struct page *) * max_cluster,
1150 /* find the last page to defrag */
1151 if (range->start + range->len > range->start) {
1152 last_index = min_t(u64, isize - 1,
1153 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1155 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1159 ret = find_new_extents(root, inode, newer_than,
1160 &newer_off, 64 * 1024);
1162 range->start = newer_off;
1164 * we always align our defrag to help keep
1165 * the extents in the file evenly spaced
1167 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1171 i = range->start >> PAGE_CACHE_SHIFT;
1174 max_to_defrag = last_index + 1;
1177 * make writeback starts from i, so the defrag range can be
1178 * written sequentially.
1180 if (i < inode->i_mapping->writeback_index)
1181 inode->i_mapping->writeback_index = i;
1183 while (i <= last_index && defrag_count < max_to_defrag &&
1184 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1185 PAGE_CACHE_SHIFT)) {
1187 * make sure we stop running if someone unmounts
1190 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1193 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1194 extent_thresh, &last_len, &skip,
1195 &defrag_end, range->flags &
1196 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1199 * the should_defrag function tells us how much to skip
1200 * bump our counter by the suggested amount
1202 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1203 i = max(i + 1, next);
1208 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1209 PAGE_CACHE_SHIFT) - i;
1210 cluster = min(cluster, max_cluster);
1212 cluster = max_cluster;
1215 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1216 BTRFS_I(inode)->force_compress = compress_type;
1218 if (i + cluster > ra_index) {
1219 ra_index = max(i, ra_index);
1220 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1222 ra_index += max_cluster;
1225 mutex_lock(&inode->i_mutex);
1226 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1228 mutex_unlock(&inode->i_mutex);
1232 defrag_count += ret;
1233 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1234 mutex_unlock(&inode->i_mutex);
1237 if (newer_off == (u64)-1)
1243 newer_off = max(newer_off + 1,
1244 (u64)i << PAGE_CACHE_SHIFT);
1246 ret = find_new_extents(root, inode,
1247 newer_than, &newer_off,
1250 range->start = newer_off;
1251 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1258 last_len += ret << PAGE_CACHE_SHIFT;
1266 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1267 filemap_flush(inode->i_mapping);
1269 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1270 /* the filemap_flush will queue IO into the worker threads, but
1271 * we have to make sure the IO is actually started and that
1272 * ordered extents get created before we return
1274 atomic_inc(&root->fs_info->async_submit_draining);
1275 while (atomic_read(&root->fs_info->nr_async_submits) ||
1276 atomic_read(&root->fs_info->async_delalloc_pages)) {
1277 wait_event(root->fs_info->async_submit_wait,
1278 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1279 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1281 atomic_dec(&root->fs_info->async_submit_draining);
1283 mutex_lock(&inode->i_mutex);
1284 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1285 mutex_unlock(&inode->i_mutex);
1288 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1289 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1301 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1307 struct btrfs_ioctl_vol_args *vol_args;
1308 struct btrfs_trans_handle *trans;
1309 struct btrfs_device *device = NULL;
1311 char *devstr = NULL;
1315 if (root->fs_info->sb->s_flags & MS_RDONLY)
1318 if (!capable(CAP_SYS_ADMIN))
1321 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1323 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1324 return -EINPROGRESS;
1327 mutex_lock(&root->fs_info->volume_mutex);
1328 vol_args = memdup_user(arg, sizeof(*vol_args));
1329 if (IS_ERR(vol_args)) {
1330 ret = PTR_ERR(vol_args);
1334 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1336 sizestr = vol_args->name;
1337 devstr = strchr(sizestr, ':');
1340 sizestr = devstr + 1;
1342 devstr = vol_args->name;
1343 devid = simple_strtoull(devstr, &end, 10);
1344 printk(KERN_INFO "btrfs: resizing devid %llu\n",
1345 (unsigned long long)devid);
1347 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1349 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1350 (unsigned long long)devid);
1354 if (device->fs_devices && device->fs_devices->seeding) {
1355 printk(KERN_INFO "btrfs: resizer unable to apply on "
1356 "seeding device %llu\n",
1357 (unsigned long long)devid);
1362 if (!strcmp(sizestr, "max"))
1363 new_size = device->bdev->bd_inode->i_size;
1365 if (sizestr[0] == '-') {
1368 } else if (sizestr[0] == '+') {
1372 new_size = memparse(sizestr, NULL);
1373 if (new_size == 0) {
1379 if (device->is_tgtdev_for_dev_replace) {
1384 old_size = device->total_bytes;
1387 if (new_size > old_size) {
1391 new_size = old_size - new_size;
1392 } else if (mod > 0) {
1393 new_size = old_size + new_size;
1396 if (new_size < 256 * 1024 * 1024) {
1400 if (new_size > device->bdev->bd_inode->i_size) {
1405 do_div(new_size, root->sectorsize);
1406 new_size *= root->sectorsize;
1408 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1409 rcu_str_deref(device->name),
1410 (unsigned long long)new_size);
1412 if (new_size > old_size) {
1413 trans = btrfs_start_transaction(root, 0);
1414 if (IS_ERR(trans)) {
1415 ret = PTR_ERR(trans);
1418 ret = btrfs_grow_device(trans, device, new_size);
1419 btrfs_commit_transaction(trans, root);
1420 } else if (new_size < old_size) {
1421 ret = btrfs_shrink_device(device, new_size);
1422 } /* equal, nothing need to do */
1427 mutex_unlock(&root->fs_info->volume_mutex);
1428 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1432 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1433 char *name, unsigned long fd, int subvol,
1434 u64 *transid, bool readonly,
1435 struct btrfs_qgroup_inherit **inherit)
1440 ret = mnt_want_write_file(file);
1444 namelen = strlen(name);
1445 if (strchr(name, '/')) {
1447 goto out_drop_write;
1450 if (name[0] == '.' &&
1451 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1453 goto out_drop_write;
1457 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1458 NULL, transid, readonly, inherit);
1460 struct fd src = fdget(fd);
1461 struct inode *src_inode;
1464 goto out_drop_write;
1467 src_inode = src.file->f_path.dentry->d_inode;
1468 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1469 printk(KERN_INFO "btrfs: Snapshot src from "
1473 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1474 BTRFS_I(src_inode)->root,
1475 transid, readonly, inherit);
1480 mnt_drop_write_file(file);
1485 static noinline int btrfs_ioctl_snap_create(struct file *file,
1486 void __user *arg, int subvol)
1488 struct btrfs_ioctl_vol_args *vol_args;
1491 vol_args = memdup_user(arg, sizeof(*vol_args));
1492 if (IS_ERR(vol_args))
1493 return PTR_ERR(vol_args);
1494 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1496 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1497 vol_args->fd, subvol,
1504 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1505 void __user *arg, int subvol)
1507 struct btrfs_ioctl_vol_args_v2 *vol_args;
1511 bool readonly = false;
1512 struct btrfs_qgroup_inherit *inherit = NULL;
1514 vol_args = memdup_user(arg, sizeof(*vol_args));
1515 if (IS_ERR(vol_args))
1516 return PTR_ERR(vol_args);
1517 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1519 if (vol_args->flags &
1520 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1521 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1526 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1528 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1530 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1531 if (vol_args->size > PAGE_CACHE_SIZE) {
1535 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1536 if (IS_ERR(inherit)) {
1537 ret = PTR_ERR(inherit);
1542 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1543 vol_args->fd, subvol, ptr,
1544 readonly, &inherit);
1546 if (ret == 0 && ptr &&
1548 offsetof(struct btrfs_ioctl_vol_args_v2,
1549 transid), ptr, sizeof(*ptr)))
1557 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1560 struct inode *inode = fdentry(file)->d_inode;
1561 struct btrfs_root *root = BTRFS_I(inode)->root;
1565 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1568 down_read(&root->fs_info->subvol_sem);
1569 if (btrfs_root_readonly(root))
1570 flags |= BTRFS_SUBVOL_RDONLY;
1571 up_read(&root->fs_info->subvol_sem);
1573 if (copy_to_user(arg, &flags, sizeof(flags)))
1579 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1582 struct inode *inode = fdentry(file)->d_inode;
1583 struct btrfs_root *root = BTRFS_I(inode)->root;
1584 struct btrfs_trans_handle *trans;
1589 ret = mnt_want_write_file(file);
1593 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1595 goto out_drop_write;
1598 if (copy_from_user(&flags, arg, sizeof(flags))) {
1600 goto out_drop_write;
1603 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1605 goto out_drop_write;
1608 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1610 goto out_drop_write;
1613 if (!inode_owner_or_capable(inode)) {
1615 goto out_drop_write;
1618 down_write(&root->fs_info->subvol_sem);
1621 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1624 root_flags = btrfs_root_flags(&root->root_item);
1625 if (flags & BTRFS_SUBVOL_RDONLY)
1626 btrfs_set_root_flags(&root->root_item,
1627 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1629 btrfs_set_root_flags(&root->root_item,
1630 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1632 trans = btrfs_start_transaction(root, 1);
1633 if (IS_ERR(trans)) {
1634 ret = PTR_ERR(trans);
1638 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1639 &root->root_key, &root->root_item);
1641 btrfs_commit_transaction(trans, root);
1644 btrfs_set_root_flags(&root->root_item, root_flags);
1646 up_write(&root->fs_info->subvol_sem);
1648 mnt_drop_write_file(file);
1654 * helper to check if the subvolume references other subvolumes
1656 static noinline int may_destroy_subvol(struct btrfs_root *root)
1658 struct btrfs_path *path;
1659 struct btrfs_key key;
1662 path = btrfs_alloc_path();
1666 key.objectid = root->root_key.objectid;
1667 key.type = BTRFS_ROOT_REF_KEY;
1668 key.offset = (u64)-1;
1670 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1677 if (path->slots[0] > 0) {
1679 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1680 if (key.objectid == root->root_key.objectid &&
1681 key.type == BTRFS_ROOT_REF_KEY)
1685 btrfs_free_path(path);
1689 static noinline int key_in_sk(struct btrfs_key *key,
1690 struct btrfs_ioctl_search_key *sk)
1692 struct btrfs_key test;
1695 test.objectid = sk->min_objectid;
1696 test.type = sk->min_type;
1697 test.offset = sk->min_offset;
1699 ret = btrfs_comp_cpu_keys(key, &test);
1703 test.objectid = sk->max_objectid;
1704 test.type = sk->max_type;
1705 test.offset = sk->max_offset;
1707 ret = btrfs_comp_cpu_keys(key, &test);
1713 static noinline int copy_to_sk(struct btrfs_root *root,
1714 struct btrfs_path *path,
1715 struct btrfs_key *key,
1716 struct btrfs_ioctl_search_key *sk,
1718 unsigned long *sk_offset,
1722 struct extent_buffer *leaf;
1723 struct btrfs_ioctl_search_header sh;
1724 unsigned long item_off;
1725 unsigned long item_len;
1731 leaf = path->nodes[0];
1732 slot = path->slots[0];
1733 nritems = btrfs_header_nritems(leaf);
1735 if (btrfs_header_generation(leaf) > sk->max_transid) {
1739 found_transid = btrfs_header_generation(leaf);
1741 for (i = slot; i < nritems; i++) {
1742 item_off = btrfs_item_ptr_offset(leaf, i);
1743 item_len = btrfs_item_size_nr(leaf, i);
1745 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1748 if (sizeof(sh) + item_len + *sk_offset >
1749 BTRFS_SEARCH_ARGS_BUFSIZE) {
1754 btrfs_item_key_to_cpu(leaf, key, i);
1755 if (!key_in_sk(key, sk))
1758 sh.objectid = key->objectid;
1759 sh.offset = key->offset;
1760 sh.type = key->type;
1762 sh.transid = found_transid;
1764 /* copy search result header */
1765 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1766 *sk_offset += sizeof(sh);
1769 char *p = buf + *sk_offset;
1771 read_extent_buffer(leaf, p,
1772 item_off, item_len);
1773 *sk_offset += item_len;
1777 if (*num_found >= sk->nr_items)
1782 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1784 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1787 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1797 static noinline int search_ioctl(struct inode *inode,
1798 struct btrfs_ioctl_search_args *args)
1800 struct btrfs_root *root;
1801 struct btrfs_key key;
1802 struct btrfs_key max_key;
1803 struct btrfs_path *path;
1804 struct btrfs_ioctl_search_key *sk = &args->key;
1805 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1808 unsigned long sk_offset = 0;
1810 path = btrfs_alloc_path();
1814 if (sk->tree_id == 0) {
1815 /* search the root of the inode that was passed */
1816 root = BTRFS_I(inode)->root;
1818 key.objectid = sk->tree_id;
1819 key.type = BTRFS_ROOT_ITEM_KEY;
1820 key.offset = (u64)-1;
1821 root = btrfs_read_fs_root_no_name(info, &key);
1823 printk(KERN_ERR "could not find root %llu\n",
1825 btrfs_free_path(path);
1830 key.objectid = sk->min_objectid;
1831 key.type = sk->min_type;
1832 key.offset = sk->min_offset;
1834 max_key.objectid = sk->max_objectid;
1835 max_key.type = sk->max_type;
1836 max_key.offset = sk->max_offset;
1838 path->keep_locks = 1;
1841 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1848 ret = copy_to_sk(root, path, &key, sk, args->buf,
1849 &sk_offset, &num_found);
1850 btrfs_release_path(path);
1851 if (ret || num_found >= sk->nr_items)
1857 sk->nr_items = num_found;
1858 btrfs_free_path(path);
1862 static noinline int btrfs_ioctl_tree_search(struct file *file,
1865 struct btrfs_ioctl_search_args *args;
1866 struct inode *inode;
1869 if (!capable(CAP_SYS_ADMIN))
1872 args = memdup_user(argp, sizeof(*args));
1874 return PTR_ERR(args);
1876 inode = fdentry(file)->d_inode;
1877 ret = search_ioctl(inode, args);
1878 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1885 * Search INODE_REFs to identify path name of 'dirid' directory
1886 * in a 'tree_id' tree. and sets path name to 'name'.
1888 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1889 u64 tree_id, u64 dirid, char *name)
1891 struct btrfs_root *root;
1892 struct btrfs_key key;
1898 struct btrfs_inode_ref *iref;
1899 struct extent_buffer *l;
1900 struct btrfs_path *path;
1902 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1907 path = btrfs_alloc_path();
1911 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1913 key.objectid = tree_id;
1914 key.type = BTRFS_ROOT_ITEM_KEY;
1915 key.offset = (u64)-1;
1916 root = btrfs_read_fs_root_no_name(info, &key);
1918 printk(KERN_ERR "could not find root %llu\n", tree_id);
1923 key.objectid = dirid;
1924 key.type = BTRFS_INODE_REF_KEY;
1925 key.offset = (u64)-1;
1928 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1933 slot = path->slots[0];
1934 if (ret > 0 && slot > 0)
1936 btrfs_item_key_to_cpu(l, &key, slot);
1938 if (ret > 0 && (key.objectid != dirid ||
1939 key.type != BTRFS_INODE_REF_KEY)) {
1944 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1945 len = btrfs_inode_ref_name_len(l, iref);
1947 total_len += len + 1;
1952 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1954 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1957 btrfs_release_path(path);
1958 key.objectid = key.offset;
1959 key.offset = (u64)-1;
1960 dirid = key.objectid;
1964 memmove(name, ptr, total_len);
1965 name[total_len]='\0';
1968 btrfs_free_path(path);
1972 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1975 struct btrfs_ioctl_ino_lookup_args *args;
1976 struct inode *inode;
1979 if (!capable(CAP_SYS_ADMIN))
1982 args = memdup_user(argp, sizeof(*args));
1984 return PTR_ERR(args);
1986 inode = fdentry(file)->d_inode;
1988 if (args->treeid == 0)
1989 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1991 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1992 args->treeid, args->objectid,
1995 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2002 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2005 struct dentry *parent = fdentry(file);
2006 struct dentry *dentry;
2007 struct inode *dir = parent->d_inode;
2008 struct inode *inode;
2009 struct btrfs_root *root = BTRFS_I(dir)->root;
2010 struct btrfs_root *dest = NULL;
2011 struct btrfs_ioctl_vol_args *vol_args;
2012 struct btrfs_trans_handle *trans;
2017 vol_args = memdup_user(arg, sizeof(*vol_args));
2018 if (IS_ERR(vol_args))
2019 return PTR_ERR(vol_args);
2021 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2022 namelen = strlen(vol_args->name);
2023 if (strchr(vol_args->name, '/') ||
2024 strncmp(vol_args->name, "..", namelen) == 0) {
2029 err = mnt_want_write_file(file);
2033 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2034 dentry = lookup_one_len(vol_args->name, parent, namelen);
2035 if (IS_ERR(dentry)) {
2036 err = PTR_ERR(dentry);
2037 goto out_unlock_dir;
2040 if (!dentry->d_inode) {
2045 inode = dentry->d_inode;
2046 dest = BTRFS_I(inode)->root;
2047 if (!capable(CAP_SYS_ADMIN)){
2049 * Regular user. Only allow this with a special mount
2050 * option, when the user has write+exec access to the
2051 * subvol root, and when rmdir(2) would have been
2054 * Note that this is _not_ check that the subvol is
2055 * empty or doesn't contain data that we wouldn't
2056 * otherwise be able to delete.
2058 * Users who want to delete empty subvols should try
2062 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2066 * Do not allow deletion if the parent dir is the same
2067 * as the dir to be deleted. That means the ioctl
2068 * must be called on the dentry referencing the root
2069 * of the subvol, not a random directory contained
2076 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2080 /* check if subvolume may be deleted by a non-root user */
2081 err = btrfs_may_delete(dir, dentry, 1);
2086 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2091 mutex_lock(&inode->i_mutex);
2092 err = d_invalidate(dentry);
2096 down_write(&root->fs_info->subvol_sem);
2098 err = may_destroy_subvol(dest);
2102 trans = btrfs_start_transaction(root, 0);
2103 if (IS_ERR(trans)) {
2104 err = PTR_ERR(trans);
2107 trans->block_rsv = &root->fs_info->global_block_rsv;
2109 ret = btrfs_unlink_subvol(trans, root, dir,
2110 dest->root_key.objectid,
2111 dentry->d_name.name,
2112 dentry->d_name.len);
2115 btrfs_abort_transaction(trans, root, ret);
2119 btrfs_record_root_in_trans(trans, dest);
2121 memset(&dest->root_item.drop_progress, 0,
2122 sizeof(dest->root_item.drop_progress));
2123 dest->root_item.drop_level = 0;
2124 btrfs_set_root_refs(&dest->root_item, 0);
2126 if (!xchg(&dest->orphan_item_inserted, 1)) {
2127 ret = btrfs_insert_orphan_item(trans,
2128 root->fs_info->tree_root,
2129 dest->root_key.objectid);
2131 btrfs_abort_transaction(trans, root, ret);
2137 ret = btrfs_end_transaction(trans, root);
2140 inode->i_flags |= S_DEAD;
2142 up_write(&root->fs_info->subvol_sem);
2144 mutex_unlock(&inode->i_mutex);
2146 shrink_dcache_sb(root->fs_info->sb);
2147 btrfs_invalidate_inodes(dest);
2153 mutex_unlock(&dir->i_mutex);
2154 mnt_drop_write_file(file);
2160 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2162 struct inode *inode = fdentry(file)->d_inode;
2163 struct btrfs_root *root = BTRFS_I(inode)->root;
2164 struct btrfs_ioctl_defrag_range_args *range;
2167 if (btrfs_root_readonly(root))
2170 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2172 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2173 return -EINPROGRESS;
2175 ret = mnt_want_write_file(file);
2177 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
2182 switch (inode->i_mode & S_IFMT) {
2184 if (!capable(CAP_SYS_ADMIN)) {
2188 ret = btrfs_defrag_root(root, 0);
2191 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2194 if (!(file->f_mode & FMODE_WRITE)) {
2199 range = kzalloc(sizeof(*range), GFP_KERNEL);
2206 if (copy_from_user(range, argp,
2212 /* compression requires us to start the IO */
2213 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2214 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2215 range->extent_thresh = (u32)-1;
2218 /* the rest are all set to zero by kzalloc */
2219 range->len = (u64)-1;
2221 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2231 mnt_drop_write_file(file);
2232 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2236 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2238 struct btrfs_ioctl_vol_args *vol_args;
2241 if (!capable(CAP_SYS_ADMIN))
2244 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2246 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2247 return -EINPROGRESS;
2250 mutex_lock(&root->fs_info->volume_mutex);
2251 vol_args = memdup_user(arg, sizeof(*vol_args));
2252 if (IS_ERR(vol_args)) {
2253 ret = PTR_ERR(vol_args);
2257 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2258 ret = btrfs_init_new_device(root, vol_args->name);
2262 mutex_unlock(&root->fs_info->volume_mutex);
2263 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2267 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2269 struct btrfs_ioctl_vol_args *vol_args;
2272 if (!capable(CAP_SYS_ADMIN))
2275 if (root->fs_info->sb->s_flags & MS_RDONLY)
2278 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2280 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2281 return -EINPROGRESS;
2284 mutex_lock(&root->fs_info->volume_mutex);
2285 vol_args = memdup_user(arg, sizeof(*vol_args));
2286 if (IS_ERR(vol_args)) {
2287 ret = PTR_ERR(vol_args);
2291 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2292 ret = btrfs_rm_device(root, vol_args->name);
2296 mutex_unlock(&root->fs_info->volume_mutex);
2297 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2301 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2303 struct btrfs_ioctl_fs_info_args *fi_args;
2304 struct btrfs_device *device;
2305 struct btrfs_device *next;
2306 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2309 if (!capable(CAP_SYS_ADMIN))
2312 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2316 fi_args->num_devices = fs_devices->num_devices;
2317 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2319 mutex_lock(&fs_devices->device_list_mutex);
2320 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2321 if (device->devid > fi_args->max_id)
2322 fi_args->max_id = device->devid;
2324 mutex_unlock(&fs_devices->device_list_mutex);
2326 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2333 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2335 struct btrfs_ioctl_dev_info_args *di_args;
2336 struct btrfs_device *dev;
2337 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2339 char *s_uuid = NULL;
2340 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2342 if (!capable(CAP_SYS_ADMIN))
2345 di_args = memdup_user(arg, sizeof(*di_args));
2346 if (IS_ERR(di_args))
2347 return PTR_ERR(di_args);
2349 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2350 s_uuid = di_args->uuid;
2352 mutex_lock(&fs_devices->device_list_mutex);
2353 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2354 mutex_unlock(&fs_devices->device_list_mutex);
2361 di_args->devid = dev->devid;
2362 di_args->bytes_used = dev->bytes_used;
2363 di_args->total_bytes = dev->total_bytes;
2364 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2366 struct rcu_string *name;
2369 name = rcu_dereference(dev->name);
2370 strncpy(di_args->path, name->str, sizeof(di_args->path));
2372 di_args->path[sizeof(di_args->path) - 1] = 0;
2374 di_args->path[0] = '\0';
2378 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2385 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2386 u64 off, u64 olen, u64 destoff)
2388 struct inode *inode = fdentry(file)->d_inode;
2389 struct btrfs_root *root = BTRFS_I(inode)->root;
2392 struct btrfs_trans_handle *trans;
2393 struct btrfs_path *path;
2394 struct extent_buffer *leaf;
2396 struct btrfs_key key;
2401 u64 bs = root->fs_info->sb->s_blocksize;
2405 * - split compressed inline extents. annoying: we need to
2406 * decompress into destination's address_space (the file offset
2407 * may change, so source mapping won't do), then recompress (or
2408 * otherwise reinsert) a subrange.
2409 * - allow ranges within the same file to be cloned (provided
2410 * they don't overlap)?
2413 /* the destination must be opened for writing */
2414 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2417 if (btrfs_root_readonly(root))
2420 ret = mnt_want_write_file(file);
2424 src_file = fdget(srcfd);
2425 if (!src_file.file) {
2427 goto out_drop_write;
2431 if (src_file.file->f_path.mnt != file->f_path.mnt)
2434 src = src_file.file->f_dentry->d_inode;
2440 /* the src must be open for reading */
2441 if (!(src_file.file->f_mode & FMODE_READ))
2444 /* don't make the dst file partly checksummed */
2445 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2446 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2450 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2454 if (src->i_sb != inode->i_sb)
2458 buf = vmalloc(btrfs_level_size(root, 0));
2462 path = btrfs_alloc_path();
2470 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2471 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2473 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2474 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2477 /* determine range to clone */
2479 if (off + len > src->i_size || off + len < off)
2482 olen = len = src->i_size - off;
2483 /* if we extend to eof, continue to block boundary */
2484 if (off + len == src->i_size)
2485 len = ALIGN(src->i_size, bs) - off;
2487 /* verify the end result is block aligned */
2488 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2489 !IS_ALIGNED(destoff, bs))
2492 if (destoff > inode->i_size) {
2493 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2498 /* truncate page cache pages from target inode range */
2499 truncate_inode_pages_range(&inode->i_data, destoff,
2500 PAGE_CACHE_ALIGN(destoff + len) - 1);
2502 /* do any pending delalloc/csum calc on src, one way or
2503 another, and lock file content */
2505 struct btrfs_ordered_extent *ordered;
2506 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2507 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
2509 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2510 EXTENT_DELALLOC, 0, NULL))
2512 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2514 btrfs_put_ordered_extent(ordered);
2515 btrfs_wait_ordered_range(src, off, len);
2519 key.objectid = btrfs_ino(src);
2520 key.type = BTRFS_EXTENT_DATA_KEY;
2525 * note the key will change type as we walk through the
2528 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2533 nritems = btrfs_header_nritems(path->nodes[0]);
2534 if (path->slots[0] >= nritems) {
2535 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2540 nritems = btrfs_header_nritems(path->nodes[0]);
2542 leaf = path->nodes[0];
2543 slot = path->slots[0];
2545 btrfs_item_key_to_cpu(leaf, &key, slot);
2546 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2547 key.objectid != btrfs_ino(src))
2550 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2551 struct btrfs_file_extent_item *extent;
2554 struct btrfs_key new_key;
2555 u64 disko = 0, diskl = 0;
2556 u64 datao = 0, datal = 0;
2560 size = btrfs_item_size_nr(leaf, slot);
2561 read_extent_buffer(leaf, buf,
2562 btrfs_item_ptr_offset(leaf, slot),
2565 extent = btrfs_item_ptr(leaf, slot,
2566 struct btrfs_file_extent_item);
2567 comp = btrfs_file_extent_compression(leaf, extent);
2568 type = btrfs_file_extent_type(leaf, extent);
2569 if (type == BTRFS_FILE_EXTENT_REG ||
2570 type == BTRFS_FILE_EXTENT_PREALLOC) {
2571 disko = btrfs_file_extent_disk_bytenr(leaf,
2573 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2575 datao = btrfs_file_extent_offset(leaf, extent);
2576 datal = btrfs_file_extent_num_bytes(leaf,
2578 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2579 /* take upper bound, may be compressed */
2580 datal = btrfs_file_extent_ram_bytes(leaf,
2583 btrfs_release_path(path);
2585 if (key.offset + datal <= off ||
2586 key.offset >= off + len - 1)
2589 memcpy(&new_key, &key, sizeof(new_key));
2590 new_key.objectid = btrfs_ino(inode);
2591 if (off <= key.offset)
2592 new_key.offset = key.offset + destoff - off;
2594 new_key.offset = destoff;
2597 * 1 - adjusting old extent (we may have to split it)
2598 * 1 - add new extent
2601 trans = btrfs_start_transaction(root, 3);
2602 if (IS_ERR(trans)) {
2603 ret = PTR_ERR(trans);
2607 if (type == BTRFS_FILE_EXTENT_REG ||
2608 type == BTRFS_FILE_EXTENT_PREALLOC) {
2610 * a | --- range to clone ---| b
2611 * | ------------- extent ------------- |
2614 /* substract range b */
2615 if (key.offset + datal > off + len)
2616 datal = off + len - key.offset;
2618 /* substract range a */
2619 if (off > key.offset) {
2620 datao += off - key.offset;
2621 datal -= off - key.offset;
2624 ret = btrfs_drop_extents(trans, root, inode,
2626 new_key.offset + datal,
2629 btrfs_abort_transaction(trans, root,
2631 btrfs_end_transaction(trans, root);
2635 ret = btrfs_insert_empty_item(trans, root, path,
2638 btrfs_abort_transaction(trans, root,
2640 btrfs_end_transaction(trans, root);
2644 leaf = path->nodes[0];
2645 slot = path->slots[0];
2646 write_extent_buffer(leaf, buf,
2647 btrfs_item_ptr_offset(leaf, slot),
2650 extent = btrfs_item_ptr(leaf, slot,
2651 struct btrfs_file_extent_item);
2653 /* disko == 0 means it's a hole */
2657 btrfs_set_file_extent_offset(leaf, extent,
2659 btrfs_set_file_extent_num_bytes(leaf, extent,
2662 inode_add_bytes(inode, datal);
2663 ret = btrfs_inc_extent_ref(trans, root,
2665 root->root_key.objectid,
2667 new_key.offset - datao,
2670 btrfs_abort_transaction(trans,
2673 btrfs_end_transaction(trans,
2679 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2682 if (off > key.offset) {
2683 skip = off - key.offset;
2684 new_key.offset += skip;
2687 if (key.offset + datal > off + len)
2688 trim = key.offset + datal - (off + len);
2690 if (comp && (skip || trim)) {
2692 btrfs_end_transaction(trans, root);
2695 size -= skip + trim;
2696 datal -= skip + trim;
2698 ret = btrfs_drop_extents(trans, root, inode,
2700 new_key.offset + datal,
2703 btrfs_abort_transaction(trans, root,
2705 btrfs_end_transaction(trans, root);
2709 ret = btrfs_insert_empty_item(trans, root, path,
2712 btrfs_abort_transaction(trans, root,
2714 btrfs_end_transaction(trans, root);
2720 btrfs_file_extent_calc_inline_size(0);
2721 memmove(buf+start, buf+start+skip,
2725 leaf = path->nodes[0];
2726 slot = path->slots[0];
2727 write_extent_buffer(leaf, buf,
2728 btrfs_item_ptr_offset(leaf, slot),
2730 inode_add_bytes(inode, datal);
2733 btrfs_mark_buffer_dirty(leaf);
2734 btrfs_release_path(path);
2736 inode_inc_iversion(inode);
2737 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2740 * we round up to the block size at eof when
2741 * determining which extents to clone above,
2742 * but shouldn't round up the file size
2744 endoff = new_key.offset + datal;
2745 if (endoff > destoff+olen)
2746 endoff = destoff+olen;
2747 if (endoff > inode->i_size)
2748 btrfs_i_size_write(inode, endoff);
2750 ret = btrfs_update_inode(trans, root, inode);
2752 btrfs_abort_transaction(trans, root, ret);
2753 btrfs_end_transaction(trans, root);
2756 ret = btrfs_end_transaction(trans, root);
2759 btrfs_release_path(path);
2764 btrfs_release_path(path);
2765 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2767 mutex_unlock(&src->i_mutex);
2768 mutex_unlock(&inode->i_mutex);
2770 btrfs_free_path(path);
2774 mnt_drop_write_file(file);
2778 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2780 struct btrfs_ioctl_clone_range_args args;
2782 if (copy_from_user(&args, argp, sizeof(args)))
2784 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2785 args.src_length, args.dest_offset);
2789 * there are many ways the trans_start and trans_end ioctls can lead
2790 * to deadlocks. They should only be used by applications that
2791 * basically own the machine, and have a very in depth understanding
2792 * of all the possible deadlocks and enospc problems.
2794 static long btrfs_ioctl_trans_start(struct file *file)
2796 struct inode *inode = fdentry(file)->d_inode;
2797 struct btrfs_root *root = BTRFS_I(inode)->root;
2798 struct btrfs_trans_handle *trans;
2802 if (!capable(CAP_SYS_ADMIN))
2806 if (file->private_data)
2810 if (btrfs_root_readonly(root))
2813 ret = mnt_want_write_file(file);
2817 atomic_inc(&root->fs_info->open_ioctl_trans);
2820 trans = btrfs_start_ioctl_transaction(root);
2824 file->private_data = trans;
2828 atomic_dec(&root->fs_info->open_ioctl_trans);
2829 mnt_drop_write_file(file);
2834 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2836 struct inode *inode = fdentry(file)->d_inode;
2837 struct btrfs_root *root = BTRFS_I(inode)->root;
2838 struct btrfs_root *new_root;
2839 struct btrfs_dir_item *di;
2840 struct btrfs_trans_handle *trans;
2841 struct btrfs_path *path;
2842 struct btrfs_key location;
2843 struct btrfs_disk_key disk_key;
2848 if (!capable(CAP_SYS_ADMIN))
2851 ret = mnt_want_write_file(file);
2855 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2861 objectid = root->root_key.objectid;
2863 location.objectid = objectid;
2864 location.type = BTRFS_ROOT_ITEM_KEY;
2865 location.offset = (u64)-1;
2867 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2868 if (IS_ERR(new_root)) {
2869 ret = PTR_ERR(new_root);
2873 if (btrfs_root_refs(&new_root->root_item) == 0) {
2878 path = btrfs_alloc_path();
2883 path->leave_spinning = 1;
2885 trans = btrfs_start_transaction(root, 1);
2886 if (IS_ERR(trans)) {
2887 btrfs_free_path(path);
2888 ret = PTR_ERR(trans);
2892 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
2893 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2894 dir_id, "default", 7, 1);
2895 if (IS_ERR_OR_NULL(di)) {
2896 btrfs_free_path(path);
2897 btrfs_end_transaction(trans, root);
2898 printk(KERN_ERR "Umm, you don't have the default dir item, "
2899 "this isn't going to work\n");
2904 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2905 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2906 btrfs_mark_buffer_dirty(path->nodes[0]);
2907 btrfs_free_path(path);
2909 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
2910 btrfs_end_transaction(trans, root);
2912 mnt_drop_write_file(file);
2916 void btrfs_get_block_group_info(struct list_head *groups_list,
2917 struct btrfs_ioctl_space_info *space)
2919 struct btrfs_block_group_cache *block_group;
2921 space->total_bytes = 0;
2922 space->used_bytes = 0;
2924 list_for_each_entry(block_group, groups_list, list) {
2925 space->flags = block_group->flags;
2926 space->total_bytes += block_group->key.offset;
2927 space->used_bytes +=
2928 btrfs_block_group_used(&block_group->item);
2932 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2934 struct btrfs_ioctl_space_args space_args;
2935 struct btrfs_ioctl_space_info space;
2936 struct btrfs_ioctl_space_info *dest;
2937 struct btrfs_ioctl_space_info *dest_orig;
2938 struct btrfs_ioctl_space_info __user *user_dest;
2939 struct btrfs_space_info *info;
2940 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2941 BTRFS_BLOCK_GROUP_SYSTEM,
2942 BTRFS_BLOCK_GROUP_METADATA,
2943 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2950 if (copy_from_user(&space_args,
2951 (struct btrfs_ioctl_space_args __user *)arg,
2952 sizeof(space_args)))
2955 for (i = 0; i < num_types; i++) {
2956 struct btrfs_space_info *tmp;
2960 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2962 if (tmp->flags == types[i]) {
2972 down_read(&info->groups_sem);
2973 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2974 if (!list_empty(&info->block_groups[c]))
2977 up_read(&info->groups_sem);
2980 /* space_slots == 0 means they are asking for a count */
2981 if (space_args.space_slots == 0) {
2982 space_args.total_spaces = slot_count;
2986 slot_count = min_t(u64, space_args.space_slots, slot_count);
2988 alloc_size = sizeof(*dest) * slot_count;
2990 /* we generally have at most 6 or so space infos, one for each raid
2991 * level. So, a whole page should be more than enough for everyone
2993 if (alloc_size > PAGE_CACHE_SIZE)
2996 space_args.total_spaces = 0;
2997 dest = kmalloc(alloc_size, GFP_NOFS);
3002 /* now we have a buffer to copy into */
3003 for (i = 0; i < num_types; i++) {
3004 struct btrfs_space_info *tmp;
3011 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3013 if (tmp->flags == types[i]) {
3022 down_read(&info->groups_sem);
3023 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3024 if (!list_empty(&info->block_groups[c])) {
3025 btrfs_get_block_group_info(
3026 &info->block_groups[c], &space);
3027 memcpy(dest, &space, sizeof(space));
3029 space_args.total_spaces++;
3035 up_read(&info->groups_sem);
3038 user_dest = (struct btrfs_ioctl_space_info __user *)
3039 (arg + sizeof(struct btrfs_ioctl_space_args));
3041 if (copy_to_user(user_dest, dest_orig, alloc_size))
3046 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3053 * there are many ways the trans_start and trans_end ioctls can lead
3054 * to deadlocks. They should only be used by applications that
3055 * basically own the machine, and have a very in depth understanding
3056 * of all the possible deadlocks and enospc problems.
3058 long btrfs_ioctl_trans_end(struct file *file)
3060 struct inode *inode = fdentry(file)->d_inode;
3061 struct btrfs_root *root = BTRFS_I(inode)->root;
3062 struct btrfs_trans_handle *trans;
3064 trans = file->private_data;
3067 file->private_data = NULL;
3069 btrfs_end_transaction(trans, root);
3071 atomic_dec(&root->fs_info->open_ioctl_trans);
3073 mnt_drop_write_file(file);
3077 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3080 struct btrfs_trans_handle *trans;
3084 trans = btrfs_attach_transaction(root);
3085 if (IS_ERR(trans)) {
3086 if (PTR_ERR(trans) != -ENOENT)
3087 return PTR_ERR(trans);
3089 /* No running transaction, don't bother */
3090 transid = root->fs_info->last_trans_committed;
3093 transid = trans->transid;
3094 ret = btrfs_commit_transaction_async(trans, root, 0);
3096 btrfs_end_transaction(trans, root);
3101 if (copy_to_user(argp, &transid, sizeof(transid)))
3106 static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3112 if (copy_from_user(&transid, argp, sizeof(transid)))
3115 transid = 0; /* current trans */
3117 return btrfs_wait_for_commit(root, transid);
3120 static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3123 struct btrfs_ioctl_scrub_args *sa;
3125 if (!capable(CAP_SYS_ADMIN))
3128 sa = memdup_user(arg, sizeof(*sa));
3132 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
3133 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3136 if (copy_to_user(arg, sa, sizeof(*sa)))
3143 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3145 if (!capable(CAP_SYS_ADMIN))
3148 return btrfs_scrub_cancel(root->fs_info);
3151 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3154 struct btrfs_ioctl_scrub_args *sa;
3157 if (!capable(CAP_SYS_ADMIN))
3160 sa = memdup_user(arg, sizeof(*sa));
3164 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3166 if (copy_to_user(arg, sa, sizeof(*sa)))
3173 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3176 struct btrfs_ioctl_get_dev_stats *sa;
3179 sa = memdup_user(arg, sizeof(*sa));
3183 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3188 ret = btrfs_get_dev_stats(root, sa);
3190 if (copy_to_user(arg, sa, sizeof(*sa)))
3197 static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3199 struct btrfs_ioctl_dev_replace_args *p;
3202 if (!capable(CAP_SYS_ADMIN))
3205 p = memdup_user(arg, sizeof(*p));
3210 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3212 &root->fs_info->mutually_exclusive_operation_running,
3214 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3217 ret = btrfs_dev_replace_start(root, p);
3219 &root->fs_info->mutually_exclusive_operation_running,
3223 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3224 btrfs_dev_replace_status(root->fs_info, p);
3227 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3228 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3235 if (copy_to_user(arg, p, sizeof(*p)))
3242 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3248 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3249 struct inode_fs_paths *ipath = NULL;
3250 struct btrfs_path *path;
3252 if (!capable(CAP_SYS_ADMIN))
3255 path = btrfs_alloc_path();
3261 ipa = memdup_user(arg, sizeof(*ipa));
3268 size = min_t(u32, ipa->size, 4096);
3269 ipath = init_ipath(size, root, path);
3270 if (IS_ERR(ipath)) {
3271 ret = PTR_ERR(ipath);
3276 ret = paths_from_inode(ipa->inum, ipath);
3280 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3281 rel_ptr = ipath->fspath->val[i] -
3282 (u64)(unsigned long)ipath->fspath->val;
3283 ipath->fspath->val[i] = rel_ptr;
3286 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3287 (void *)(unsigned long)ipath->fspath, size);
3294 btrfs_free_path(path);
3301 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3303 struct btrfs_data_container *inodes = ctx;
3304 const size_t c = 3 * sizeof(u64);
3306 if (inodes->bytes_left >= c) {
3307 inodes->bytes_left -= c;
3308 inodes->val[inodes->elem_cnt] = inum;
3309 inodes->val[inodes->elem_cnt + 1] = offset;
3310 inodes->val[inodes->elem_cnt + 2] = root;
3311 inodes->elem_cnt += 3;
3313 inodes->bytes_missing += c - inodes->bytes_left;
3314 inodes->bytes_left = 0;
3315 inodes->elem_missed += 3;
3321 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3326 struct btrfs_ioctl_logical_ino_args *loi;
3327 struct btrfs_data_container *inodes = NULL;
3328 struct btrfs_path *path = NULL;
3330 if (!capable(CAP_SYS_ADMIN))
3333 loi = memdup_user(arg, sizeof(*loi));
3340 path = btrfs_alloc_path();
3346 size = min_t(u32, loi->size, 64 * 1024);
3347 inodes = init_data_container(size);
3348 if (IS_ERR(inodes)) {
3349 ret = PTR_ERR(inodes);
3354 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3355 build_ino_list, inodes);
3361 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3362 (void *)(unsigned long)inodes, size);
3367 btrfs_free_path(path);
3374 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3375 struct btrfs_ioctl_balance_args *bargs)
3377 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3379 bargs->flags = bctl->flags;
3381 if (atomic_read(&fs_info->balance_running))
3382 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3383 if (atomic_read(&fs_info->balance_pause_req))
3384 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3385 if (atomic_read(&fs_info->balance_cancel_req))
3386 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3388 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3389 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3390 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3393 spin_lock(&fs_info->balance_lock);
3394 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3395 spin_unlock(&fs_info->balance_lock);
3397 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3401 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3403 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3404 struct btrfs_fs_info *fs_info = root->fs_info;
3405 struct btrfs_ioctl_balance_args *bargs;
3406 struct btrfs_balance_control *bctl;
3408 int need_to_clear_lock = 0;
3410 if (!capable(CAP_SYS_ADMIN))
3413 ret = mnt_want_write_file(file);
3417 mutex_lock(&fs_info->volume_mutex);
3418 mutex_lock(&fs_info->balance_mutex);
3421 bargs = memdup_user(arg, sizeof(*bargs));
3422 if (IS_ERR(bargs)) {
3423 ret = PTR_ERR(bargs);
3427 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3428 if (!fs_info->balance_ctl) {
3433 bctl = fs_info->balance_ctl;
3434 spin_lock(&fs_info->balance_lock);
3435 bctl->flags |= BTRFS_BALANCE_RESUME;
3436 spin_unlock(&fs_info->balance_lock);
3444 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
3446 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3450 need_to_clear_lock = 1;
3452 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3458 bctl->fs_info = fs_info;
3460 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3461 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3462 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3464 bctl->flags = bargs->flags;
3466 /* balance everything - no filters */
3467 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3471 ret = btrfs_balance(bctl, bargs);
3473 * bctl is freed in __cancel_balance or in free_fs_info if
3474 * restriper was paused all the way until unmount
3477 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3484 if (need_to_clear_lock)
3485 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
3487 mutex_unlock(&fs_info->balance_mutex);
3488 mutex_unlock(&fs_info->volume_mutex);
3489 mnt_drop_write_file(file);
3493 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3495 if (!capable(CAP_SYS_ADMIN))
3499 case BTRFS_BALANCE_CTL_PAUSE:
3500 return btrfs_pause_balance(root->fs_info);
3501 case BTRFS_BALANCE_CTL_CANCEL:
3502 return btrfs_cancel_balance(root->fs_info);
3508 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3511 struct btrfs_fs_info *fs_info = root->fs_info;
3512 struct btrfs_ioctl_balance_args *bargs;
3515 if (!capable(CAP_SYS_ADMIN))
3518 mutex_lock(&fs_info->balance_mutex);
3519 if (!fs_info->balance_ctl) {
3524 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3530 update_ioctl_balance_args(fs_info, 1, bargs);
3532 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3537 mutex_unlock(&fs_info->balance_mutex);
3541 static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3543 struct btrfs_ioctl_quota_ctl_args *sa;
3544 struct btrfs_trans_handle *trans = NULL;
3548 if (!capable(CAP_SYS_ADMIN))
3551 if (root->fs_info->sb->s_flags & MS_RDONLY)
3554 sa = memdup_user(arg, sizeof(*sa));
3558 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3559 trans = btrfs_start_transaction(root, 2);
3560 if (IS_ERR(trans)) {
3561 ret = PTR_ERR(trans);
3567 case BTRFS_QUOTA_CTL_ENABLE:
3568 ret = btrfs_quota_enable(trans, root->fs_info);
3570 case BTRFS_QUOTA_CTL_DISABLE:
3571 ret = btrfs_quota_disable(trans, root->fs_info);
3573 case BTRFS_QUOTA_CTL_RESCAN:
3574 ret = btrfs_quota_rescan(root->fs_info);
3581 if (copy_to_user(arg, sa, sizeof(*sa)))
3585 err = btrfs_commit_transaction(trans, root);
3595 static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3597 struct btrfs_ioctl_qgroup_assign_args *sa;
3598 struct btrfs_trans_handle *trans;
3602 if (!capable(CAP_SYS_ADMIN))
3605 if (root->fs_info->sb->s_flags & MS_RDONLY)
3608 sa = memdup_user(arg, sizeof(*sa));
3612 trans = btrfs_join_transaction(root);
3613 if (IS_ERR(trans)) {
3614 ret = PTR_ERR(trans);
3618 /* FIXME: check if the IDs really exist */
3620 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3623 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3627 err = btrfs_end_transaction(trans, root);
3636 static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3638 struct btrfs_ioctl_qgroup_create_args *sa;
3639 struct btrfs_trans_handle *trans;
3643 if (!capable(CAP_SYS_ADMIN))
3646 if (root->fs_info->sb->s_flags & MS_RDONLY)
3649 sa = memdup_user(arg, sizeof(*sa));
3653 trans = btrfs_join_transaction(root);
3654 if (IS_ERR(trans)) {
3655 ret = PTR_ERR(trans);
3659 /* FIXME: check if the IDs really exist */
3661 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3664 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3667 err = btrfs_end_transaction(trans, root);
3676 static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3678 struct btrfs_ioctl_qgroup_limit_args *sa;
3679 struct btrfs_trans_handle *trans;
3684 if (!capable(CAP_SYS_ADMIN))
3687 if (root->fs_info->sb->s_flags & MS_RDONLY)
3690 sa = memdup_user(arg, sizeof(*sa));
3694 trans = btrfs_join_transaction(root);
3695 if (IS_ERR(trans)) {
3696 ret = PTR_ERR(trans);
3700 qgroupid = sa->qgroupid;
3702 /* take the current subvol as qgroup */
3703 qgroupid = root->root_key.objectid;
3706 /* FIXME: check if the IDs really exist */
3707 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3709 err = btrfs_end_transaction(trans, root);
3718 static long btrfs_ioctl_set_received_subvol(struct file *file,
3721 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3722 struct inode *inode = fdentry(file)->d_inode;
3723 struct btrfs_root *root = BTRFS_I(inode)->root;
3724 struct btrfs_root_item *root_item = &root->root_item;
3725 struct btrfs_trans_handle *trans;
3726 struct timespec ct = CURRENT_TIME;
3729 ret = mnt_want_write_file(file);
3733 down_write(&root->fs_info->subvol_sem);
3735 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3740 if (btrfs_root_readonly(root)) {
3745 if (!inode_owner_or_capable(inode)) {
3750 sa = memdup_user(arg, sizeof(*sa));
3757 trans = btrfs_start_transaction(root, 1);
3758 if (IS_ERR(trans)) {
3759 ret = PTR_ERR(trans);
3764 sa->rtransid = trans->transid;
3765 sa->rtime.sec = ct.tv_sec;
3766 sa->rtime.nsec = ct.tv_nsec;
3768 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3769 btrfs_set_root_stransid(root_item, sa->stransid);
3770 btrfs_set_root_rtransid(root_item, sa->rtransid);
3771 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3772 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3773 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3774 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3776 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3777 &root->root_key, &root->root_item);
3779 btrfs_end_transaction(trans, root);
3783 ret = btrfs_commit_transaction(trans, root);
3788 ret = copy_to_user(arg, sa, sizeof(*sa));
3794 up_write(&root->fs_info->subvol_sem);
3795 mnt_drop_write_file(file);
3799 long btrfs_ioctl(struct file *file, unsigned int
3800 cmd, unsigned long arg)
3802 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3803 void __user *argp = (void __user *)arg;
3806 case FS_IOC_GETFLAGS:
3807 return btrfs_ioctl_getflags(file, argp);
3808 case FS_IOC_SETFLAGS:
3809 return btrfs_ioctl_setflags(file, argp);
3810 case FS_IOC_GETVERSION:
3811 return btrfs_ioctl_getversion(file, argp);
3813 return btrfs_ioctl_fitrim(file, argp);
3814 case BTRFS_IOC_SNAP_CREATE:
3815 return btrfs_ioctl_snap_create(file, argp, 0);
3816 case BTRFS_IOC_SNAP_CREATE_V2:
3817 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3818 case BTRFS_IOC_SUBVOL_CREATE:
3819 return btrfs_ioctl_snap_create(file, argp, 1);
3820 case BTRFS_IOC_SUBVOL_CREATE_V2:
3821 return btrfs_ioctl_snap_create_v2(file, argp, 1);
3822 case BTRFS_IOC_SNAP_DESTROY:
3823 return btrfs_ioctl_snap_destroy(file, argp);
3824 case BTRFS_IOC_SUBVOL_GETFLAGS:
3825 return btrfs_ioctl_subvol_getflags(file, argp);
3826 case BTRFS_IOC_SUBVOL_SETFLAGS:
3827 return btrfs_ioctl_subvol_setflags(file, argp);
3828 case BTRFS_IOC_DEFAULT_SUBVOL:
3829 return btrfs_ioctl_default_subvol(file, argp);
3830 case BTRFS_IOC_DEFRAG:
3831 return btrfs_ioctl_defrag(file, NULL);
3832 case BTRFS_IOC_DEFRAG_RANGE:
3833 return btrfs_ioctl_defrag(file, argp);
3834 case BTRFS_IOC_RESIZE:
3835 return btrfs_ioctl_resize(root, argp);
3836 case BTRFS_IOC_ADD_DEV:
3837 return btrfs_ioctl_add_dev(root, argp);
3838 case BTRFS_IOC_RM_DEV:
3839 return btrfs_ioctl_rm_dev(root, argp);
3840 case BTRFS_IOC_FS_INFO:
3841 return btrfs_ioctl_fs_info(root, argp);
3842 case BTRFS_IOC_DEV_INFO:
3843 return btrfs_ioctl_dev_info(root, argp);
3844 case BTRFS_IOC_BALANCE:
3845 return btrfs_ioctl_balance(file, NULL);
3846 case BTRFS_IOC_CLONE:
3847 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3848 case BTRFS_IOC_CLONE_RANGE:
3849 return btrfs_ioctl_clone_range(file, argp);
3850 case BTRFS_IOC_TRANS_START:
3851 return btrfs_ioctl_trans_start(file);
3852 case BTRFS_IOC_TRANS_END:
3853 return btrfs_ioctl_trans_end(file);
3854 case BTRFS_IOC_TREE_SEARCH:
3855 return btrfs_ioctl_tree_search(file, argp);
3856 case BTRFS_IOC_INO_LOOKUP:
3857 return btrfs_ioctl_ino_lookup(file, argp);
3858 case BTRFS_IOC_INO_PATHS:
3859 return btrfs_ioctl_ino_to_path(root, argp);
3860 case BTRFS_IOC_LOGICAL_INO:
3861 return btrfs_ioctl_logical_to_ino(root, argp);
3862 case BTRFS_IOC_SPACE_INFO:
3863 return btrfs_ioctl_space_info(root, argp);
3864 case BTRFS_IOC_SYNC:
3865 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3867 case BTRFS_IOC_START_SYNC:
3868 return btrfs_ioctl_start_sync(root, argp);
3869 case BTRFS_IOC_WAIT_SYNC:
3870 return btrfs_ioctl_wait_sync(root, argp);
3871 case BTRFS_IOC_SCRUB:
3872 return btrfs_ioctl_scrub(root, argp);
3873 case BTRFS_IOC_SCRUB_CANCEL:
3874 return btrfs_ioctl_scrub_cancel(root, argp);
3875 case BTRFS_IOC_SCRUB_PROGRESS:
3876 return btrfs_ioctl_scrub_progress(root, argp);
3877 case BTRFS_IOC_BALANCE_V2:
3878 return btrfs_ioctl_balance(file, argp);
3879 case BTRFS_IOC_BALANCE_CTL:
3880 return btrfs_ioctl_balance_ctl(root, arg);
3881 case BTRFS_IOC_BALANCE_PROGRESS:
3882 return btrfs_ioctl_balance_progress(root, argp);
3883 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3884 return btrfs_ioctl_set_received_subvol(file, argp);
3885 case BTRFS_IOC_SEND:
3886 return btrfs_ioctl_send(file, argp);
3887 case BTRFS_IOC_GET_DEV_STATS:
3888 return btrfs_ioctl_get_dev_stats(root, argp);
3889 case BTRFS_IOC_QUOTA_CTL:
3890 return btrfs_ioctl_quota_ctl(root, argp);
3891 case BTRFS_IOC_QGROUP_ASSIGN:
3892 return btrfs_ioctl_qgroup_assign(root, argp);
3893 case BTRFS_IOC_QGROUP_CREATE:
3894 return btrfs_ioctl_qgroup_create(root, argp);
3895 case BTRFS_IOC_QGROUP_LIMIT:
3896 return btrfs_ioctl_qgroup_limit(root, argp);
3897 case BTRFS_IOC_DEV_REPLACE:
3898 return btrfs_ioctl_dev_replace(root, argp);