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 file *file,
1307 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1308 struct btrfs_ioctl_vol_args *vol_args;
1309 struct btrfs_trans_handle *trans;
1310 struct btrfs_device *device = NULL;
1312 char *devstr = NULL;
1316 if (root->fs_info->sb->s_flags & MS_RDONLY)
1319 if (!capable(CAP_SYS_ADMIN))
1322 ret = mnt_want_write_file(file);
1326 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1328 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1329 return -EINPROGRESS;
1332 mutex_lock(&root->fs_info->volume_mutex);
1333 vol_args = memdup_user(arg, sizeof(*vol_args));
1334 if (IS_ERR(vol_args)) {
1335 ret = PTR_ERR(vol_args);
1339 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1341 sizestr = vol_args->name;
1342 devstr = strchr(sizestr, ':');
1345 sizestr = devstr + 1;
1347 devstr = vol_args->name;
1348 devid = simple_strtoull(devstr, &end, 10);
1349 printk(KERN_INFO "btrfs: resizing devid %llu\n",
1350 (unsigned long long)devid);
1352 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1354 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1355 (unsigned long long)devid);
1359 if (device->fs_devices && device->fs_devices->seeding) {
1360 printk(KERN_INFO "btrfs: resizer unable to apply on "
1361 "seeding device %llu\n",
1362 (unsigned long long)devid);
1367 if (!strcmp(sizestr, "max"))
1368 new_size = device->bdev->bd_inode->i_size;
1370 if (sizestr[0] == '-') {
1373 } else if (sizestr[0] == '+') {
1377 new_size = memparse(sizestr, NULL);
1378 if (new_size == 0) {
1384 if (device->is_tgtdev_for_dev_replace) {
1389 old_size = device->total_bytes;
1392 if (new_size > old_size) {
1396 new_size = old_size - new_size;
1397 } else if (mod > 0) {
1398 new_size = old_size + new_size;
1401 if (new_size < 256 * 1024 * 1024) {
1405 if (new_size > device->bdev->bd_inode->i_size) {
1410 do_div(new_size, root->sectorsize);
1411 new_size *= root->sectorsize;
1413 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1414 rcu_str_deref(device->name),
1415 (unsigned long long)new_size);
1417 if (new_size > old_size) {
1418 trans = btrfs_start_transaction(root, 0);
1419 if (IS_ERR(trans)) {
1420 ret = PTR_ERR(trans);
1423 ret = btrfs_grow_device(trans, device, new_size);
1424 btrfs_commit_transaction(trans, root);
1425 } else if (new_size < old_size) {
1426 ret = btrfs_shrink_device(device, new_size);
1427 } /* equal, nothing need to do */
1432 mutex_unlock(&root->fs_info->volume_mutex);
1433 mnt_drop_write_file(file);
1434 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1438 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1439 char *name, unsigned long fd, int subvol,
1440 u64 *transid, bool readonly,
1441 struct btrfs_qgroup_inherit **inherit)
1446 ret = mnt_want_write_file(file);
1450 namelen = strlen(name);
1451 if (strchr(name, '/')) {
1453 goto out_drop_write;
1456 if (name[0] == '.' &&
1457 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1459 goto out_drop_write;
1463 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1464 NULL, transid, readonly, inherit);
1466 struct fd src = fdget(fd);
1467 struct inode *src_inode;
1470 goto out_drop_write;
1473 src_inode = src.file->f_path.dentry->d_inode;
1474 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1475 printk(KERN_INFO "btrfs: Snapshot src from "
1479 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1480 BTRFS_I(src_inode)->root,
1481 transid, readonly, inherit);
1486 mnt_drop_write_file(file);
1491 static noinline int btrfs_ioctl_snap_create(struct file *file,
1492 void __user *arg, int subvol)
1494 struct btrfs_ioctl_vol_args *vol_args;
1497 vol_args = memdup_user(arg, sizeof(*vol_args));
1498 if (IS_ERR(vol_args))
1499 return PTR_ERR(vol_args);
1500 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1502 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1503 vol_args->fd, subvol,
1510 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1511 void __user *arg, int subvol)
1513 struct btrfs_ioctl_vol_args_v2 *vol_args;
1517 bool readonly = false;
1518 struct btrfs_qgroup_inherit *inherit = NULL;
1520 vol_args = memdup_user(arg, sizeof(*vol_args));
1521 if (IS_ERR(vol_args))
1522 return PTR_ERR(vol_args);
1523 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1525 if (vol_args->flags &
1526 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1527 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1532 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1534 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1536 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1537 if (vol_args->size > PAGE_CACHE_SIZE) {
1541 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1542 if (IS_ERR(inherit)) {
1543 ret = PTR_ERR(inherit);
1548 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1549 vol_args->fd, subvol, ptr,
1550 readonly, &inherit);
1552 if (ret == 0 && ptr &&
1554 offsetof(struct btrfs_ioctl_vol_args_v2,
1555 transid), ptr, sizeof(*ptr)))
1563 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1566 struct inode *inode = fdentry(file)->d_inode;
1567 struct btrfs_root *root = BTRFS_I(inode)->root;
1571 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1574 down_read(&root->fs_info->subvol_sem);
1575 if (btrfs_root_readonly(root))
1576 flags |= BTRFS_SUBVOL_RDONLY;
1577 up_read(&root->fs_info->subvol_sem);
1579 if (copy_to_user(arg, &flags, sizeof(flags)))
1585 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1588 struct inode *inode = fdentry(file)->d_inode;
1589 struct btrfs_root *root = BTRFS_I(inode)->root;
1590 struct btrfs_trans_handle *trans;
1595 ret = mnt_want_write_file(file);
1599 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1601 goto out_drop_write;
1604 if (copy_from_user(&flags, arg, sizeof(flags))) {
1606 goto out_drop_write;
1609 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1611 goto out_drop_write;
1614 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1616 goto out_drop_write;
1619 if (!inode_owner_or_capable(inode)) {
1621 goto out_drop_write;
1624 down_write(&root->fs_info->subvol_sem);
1627 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1630 root_flags = btrfs_root_flags(&root->root_item);
1631 if (flags & BTRFS_SUBVOL_RDONLY)
1632 btrfs_set_root_flags(&root->root_item,
1633 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1635 btrfs_set_root_flags(&root->root_item,
1636 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1638 trans = btrfs_start_transaction(root, 1);
1639 if (IS_ERR(trans)) {
1640 ret = PTR_ERR(trans);
1644 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1645 &root->root_key, &root->root_item);
1647 btrfs_commit_transaction(trans, root);
1650 btrfs_set_root_flags(&root->root_item, root_flags);
1652 up_write(&root->fs_info->subvol_sem);
1654 mnt_drop_write_file(file);
1660 * helper to check if the subvolume references other subvolumes
1662 static noinline int may_destroy_subvol(struct btrfs_root *root)
1664 struct btrfs_path *path;
1665 struct btrfs_key key;
1668 path = btrfs_alloc_path();
1672 key.objectid = root->root_key.objectid;
1673 key.type = BTRFS_ROOT_REF_KEY;
1674 key.offset = (u64)-1;
1676 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1683 if (path->slots[0] > 0) {
1685 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1686 if (key.objectid == root->root_key.objectid &&
1687 key.type == BTRFS_ROOT_REF_KEY)
1691 btrfs_free_path(path);
1695 static noinline int key_in_sk(struct btrfs_key *key,
1696 struct btrfs_ioctl_search_key *sk)
1698 struct btrfs_key test;
1701 test.objectid = sk->min_objectid;
1702 test.type = sk->min_type;
1703 test.offset = sk->min_offset;
1705 ret = btrfs_comp_cpu_keys(key, &test);
1709 test.objectid = sk->max_objectid;
1710 test.type = sk->max_type;
1711 test.offset = sk->max_offset;
1713 ret = btrfs_comp_cpu_keys(key, &test);
1719 static noinline int copy_to_sk(struct btrfs_root *root,
1720 struct btrfs_path *path,
1721 struct btrfs_key *key,
1722 struct btrfs_ioctl_search_key *sk,
1724 unsigned long *sk_offset,
1728 struct extent_buffer *leaf;
1729 struct btrfs_ioctl_search_header sh;
1730 unsigned long item_off;
1731 unsigned long item_len;
1737 leaf = path->nodes[0];
1738 slot = path->slots[0];
1739 nritems = btrfs_header_nritems(leaf);
1741 if (btrfs_header_generation(leaf) > sk->max_transid) {
1745 found_transid = btrfs_header_generation(leaf);
1747 for (i = slot; i < nritems; i++) {
1748 item_off = btrfs_item_ptr_offset(leaf, i);
1749 item_len = btrfs_item_size_nr(leaf, i);
1751 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1754 if (sizeof(sh) + item_len + *sk_offset >
1755 BTRFS_SEARCH_ARGS_BUFSIZE) {
1760 btrfs_item_key_to_cpu(leaf, key, i);
1761 if (!key_in_sk(key, sk))
1764 sh.objectid = key->objectid;
1765 sh.offset = key->offset;
1766 sh.type = key->type;
1768 sh.transid = found_transid;
1770 /* copy search result header */
1771 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1772 *sk_offset += sizeof(sh);
1775 char *p = buf + *sk_offset;
1777 read_extent_buffer(leaf, p,
1778 item_off, item_len);
1779 *sk_offset += item_len;
1783 if (*num_found >= sk->nr_items)
1788 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1790 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1793 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1803 static noinline int search_ioctl(struct inode *inode,
1804 struct btrfs_ioctl_search_args *args)
1806 struct btrfs_root *root;
1807 struct btrfs_key key;
1808 struct btrfs_key max_key;
1809 struct btrfs_path *path;
1810 struct btrfs_ioctl_search_key *sk = &args->key;
1811 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1814 unsigned long sk_offset = 0;
1816 path = btrfs_alloc_path();
1820 if (sk->tree_id == 0) {
1821 /* search the root of the inode that was passed */
1822 root = BTRFS_I(inode)->root;
1824 key.objectid = sk->tree_id;
1825 key.type = BTRFS_ROOT_ITEM_KEY;
1826 key.offset = (u64)-1;
1827 root = btrfs_read_fs_root_no_name(info, &key);
1829 printk(KERN_ERR "could not find root %llu\n",
1831 btrfs_free_path(path);
1836 key.objectid = sk->min_objectid;
1837 key.type = sk->min_type;
1838 key.offset = sk->min_offset;
1840 max_key.objectid = sk->max_objectid;
1841 max_key.type = sk->max_type;
1842 max_key.offset = sk->max_offset;
1844 path->keep_locks = 1;
1847 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1854 ret = copy_to_sk(root, path, &key, sk, args->buf,
1855 &sk_offset, &num_found);
1856 btrfs_release_path(path);
1857 if (ret || num_found >= sk->nr_items)
1863 sk->nr_items = num_found;
1864 btrfs_free_path(path);
1868 static noinline int btrfs_ioctl_tree_search(struct file *file,
1871 struct btrfs_ioctl_search_args *args;
1872 struct inode *inode;
1875 if (!capable(CAP_SYS_ADMIN))
1878 args = memdup_user(argp, sizeof(*args));
1880 return PTR_ERR(args);
1882 inode = fdentry(file)->d_inode;
1883 ret = search_ioctl(inode, args);
1884 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1891 * Search INODE_REFs to identify path name of 'dirid' directory
1892 * in a 'tree_id' tree. and sets path name to 'name'.
1894 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1895 u64 tree_id, u64 dirid, char *name)
1897 struct btrfs_root *root;
1898 struct btrfs_key key;
1904 struct btrfs_inode_ref *iref;
1905 struct extent_buffer *l;
1906 struct btrfs_path *path;
1908 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1913 path = btrfs_alloc_path();
1917 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1919 key.objectid = tree_id;
1920 key.type = BTRFS_ROOT_ITEM_KEY;
1921 key.offset = (u64)-1;
1922 root = btrfs_read_fs_root_no_name(info, &key);
1924 printk(KERN_ERR "could not find root %llu\n", tree_id);
1929 key.objectid = dirid;
1930 key.type = BTRFS_INODE_REF_KEY;
1931 key.offset = (u64)-1;
1934 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1939 slot = path->slots[0];
1940 if (ret > 0 && slot > 0)
1942 btrfs_item_key_to_cpu(l, &key, slot);
1944 if (ret > 0 && (key.objectid != dirid ||
1945 key.type != BTRFS_INODE_REF_KEY)) {
1950 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1951 len = btrfs_inode_ref_name_len(l, iref);
1953 total_len += len + 1;
1958 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1960 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1963 btrfs_release_path(path);
1964 key.objectid = key.offset;
1965 key.offset = (u64)-1;
1966 dirid = key.objectid;
1970 memmove(name, ptr, total_len);
1971 name[total_len]='\0';
1974 btrfs_free_path(path);
1978 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1981 struct btrfs_ioctl_ino_lookup_args *args;
1982 struct inode *inode;
1985 if (!capable(CAP_SYS_ADMIN))
1988 args = memdup_user(argp, sizeof(*args));
1990 return PTR_ERR(args);
1992 inode = fdentry(file)->d_inode;
1994 if (args->treeid == 0)
1995 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1997 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1998 args->treeid, args->objectid,
2001 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2008 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2011 struct dentry *parent = fdentry(file);
2012 struct dentry *dentry;
2013 struct inode *dir = parent->d_inode;
2014 struct inode *inode;
2015 struct btrfs_root *root = BTRFS_I(dir)->root;
2016 struct btrfs_root *dest = NULL;
2017 struct btrfs_ioctl_vol_args *vol_args;
2018 struct btrfs_trans_handle *trans;
2023 vol_args = memdup_user(arg, sizeof(*vol_args));
2024 if (IS_ERR(vol_args))
2025 return PTR_ERR(vol_args);
2027 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2028 namelen = strlen(vol_args->name);
2029 if (strchr(vol_args->name, '/') ||
2030 strncmp(vol_args->name, "..", namelen) == 0) {
2035 err = mnt_want_write_file(file);
2039 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2040 dentry = lookup_one_len(vol_args->name, parent, namelen);
2041 if (IS_ERR(dentry)) {
2042 err = PTR_ERR(dentry);
2043 goto out_unlock_dir;
2046 if (!dentry->d_inode) {
2051 inode = dentry->d_inode;
2052 dest = BTRFS_I(inode)->root;
2053 if (!capable(CAP_SYS_ADMIN)){
2055 * Regular user. Only allow this with a special mount
2056 * option, when the user has write+exec access to the
2057 * subvol root, and when rmdir(2) would have been
2060 * Note that this is _not_ check that the subvol is
2061 * empty or doesn't contain data that we wouldn't
2062 * otherwise be able to delete.
2064 * Users who want to delete empty subvols should try
2068 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2072 * Do not allow deletion if the parent dir is the same
2073 * as the dir to be deleted. That means the ioctl
2074 * must be called on the dentry referencing the root
2075 * of the subvol, not a random directory contained
2082 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2086 /* check if subvolume may be deleted by a non-root user */
2087 err = btrfs_may_delete(dir, dentry, 1);
2092 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2097 mutex_lock(&inode->i_mutex);
2098 err = d_invalidate(dentry);
2102 down_write(&root->fs_info->subvol_sem);
2104 err = may_destroy_subvol(dest);
2108 trans = btrfs_start_transaction(root, 0);
2109 if (IS_ERR(trans)) {
2110 err = PTR_ERR(trans);
2113 trans->block_rsv = &root->fs_info->global_block_rsv;
2115 ret = btrfs_unlink_subvol(trans, root, dir,
2116 dest->root_key.objectid,
2117 dentry->d_name.name,
2118 dentry->d_name.len);
2121 btrfs_abort_transaction(trans, root, ret);
2125 btrfs_record_root_in_trans(trans, dest);
2127 memset(&dest->root_item.drop_progress, 0,
2128 sizeof(dest->root_item.drop_progress));
2129 dest->root_item.drop_level = 0;
2130 btrfs_set_root_refs(&dest->root_item, 0);
2132 if (!xchg(&dest->orphan_item_inserted, 1)) {
2133 ret = btrfs_insert_orphan_item(trans,
2134 root->fs_info->tree_root,
2135 dest->root_key.objectid);
2137 btrfs_abort_transaction(trans, root, ret);
2143 ret = btrfs_end_transaction(trans, root);
2146 inode->i_flags |= S_DEAD;
2148 up_write(&root->fs_info->subvol_sem);
2150 mutex_unlock(&inode->i_mutex);
2152 shrink_dcache_sb(root->fs_info->sb);
2153 btrfs_invalidate_inodes(dest);
2159 mutex_unlock(&dir->i_mutex);
2160 mnt_drop_write_file(file);
2166 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2168 struct inode *inode = fdentry(file)->d_inode;
2169 struct btrfs_root *root = BTRFS_I(inode)->root;
2170 struct btrfs_ioctl_defrag_range_args *range;
2173 if (btrfs_root_readonly(root))
2176 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2178 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2179 return -EINPROGRESS;
2181 ret = mnt_want_write_file(file);
2183 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
2188 switch (inode->i_mode & S_IFMT) {
2190 if (!capable(CAP_SYS_ADMIN)) {
2194 ret = btrfs_defrag_root(root, 0);
2197 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2200 if (!(file->f_mode & FMODE_WRITE)) {
2205 range = kzalloc(sizeof(*range), GFP_KERNEL);
2212 if (copy_from_user(range, argp,
2218 /* compression requires us to start the IO */
2219 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2220 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2221 range->extent_thresh = (u32)-1;
2224 /* the rest are all set to zero by kzalloc */
2225 range->len = (u64)-1;
2227 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2237 mnt_drop_write_file(file);
2238 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2242 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2244 struct btrfs_ioctl_vol_args *vol_args;
2247 if (!capable(CAP_SYS_ADMIN))
2250 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2252 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2253 return -EINPROGRESS;
2256 mutex_lock(&root->fs_info->volume_mutex);
2257 vol_args = memdup_user(arg, sizeof(*vol_args));
2258 if (IS_ERR(vol_args)) {
2259 ret = PTR_ERR(vol_args);
2263 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2264 ret = btrfs_init_new_device(root, vol_args->name);
2268 mutex_unlock(&root->fs_info->volume_mutex);
2269 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2273 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2275 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2276 struct btrfs_ioctl_vol_args *vol_args;
2279 if (!capable(CAP_SYS_ADMIN))
2282 ret = mnt_want_write_file(file);
2286 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2288 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2289 mnt_drop_write_file(file);
2290 return -EINPROGRESS;
2293 mutex_lock(&root->fs_info->volume_mutex);
2294 vol_args = memdup_user(arg, sizeof(*vol_args));
2295 if (IS_ERR(vol_args)) {
2296 ret = PTR_ERR(vol_args);
2300 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2301 ret = btrfs_rm_device(root, vol_args->name);
2305 mutex_unlock(&root->fs_info->volume_mutex);
2306 mnt_drop_write_file(file);
2307 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2311 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2313 struct btrfs_ioctl_fs_info_args *fi_args;
2314 struct btrfs_device *device;
2315 struct btrfs_device *next;
2316 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2319 if (!capable(CAP_SYS_ADMIN))
2322 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2326 fi_args->num_devices = fs_devices->num_devices;
2327 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2329 mutex_lock(&fs_devices->device_list_mutex);
2330 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2331 if (device->devid > fi_args->max_id)
2332 fi_args->max_id = device->devid;
2334 mutex_unlock(&fs_devices->device_list_mutex);
2336 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2343 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2345 struct btrfs_ioctl_dev_info_args *di_args;
2346 struct btrfs_device *dev;
2347 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2349 char *s_uuid = NULL;
2350 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2352 if (!capable(CAP_SYS_ADMIN))
2355 di_args = memdup_user(arg, sizeof(*di_args));
2356 if (IS_ERR(di_args))
2357 return PTR_ERR(di_args);
2359 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2360 s_uuid = di_args->uuid;
2362 mutex_lock(&fs_devices->device_list_mutex);
2363 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2364 mutex_unlock(&fs_devices->device_list_mutex);
2371 di_args->devid = dev->devid;
2372 di_args->bytes_used = dev->bytes_used;
2373 di_args->total_bytes = dev->total_bytes;
2374 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2376 struct rcu_string *name;
2379 name = rcu_dereference(dev->name);
2380 strncpy(di_args->path, name->str, sizeof(di_args->path));
2382 di_args->path[sizeof(di_args->path) - 1] = 0;
2384 di_args->path[0] = '\0';
2388 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2395 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2396 u64 off, u64 olen, u64 destoff)
2398 struct inode *inode = fdentry(file)->d_inode;
2399 struct btrfs_root *root = BTRFS_I(inode)->root;
2402 struct btrfs_trans_handle *trans;
2403 struct btrfs_path *path;
2404 struct extent_buffer *leaf;
2406 struct btrfs_key key;
2411 u64 bs = root->fs_info->sb->s_blocksize;
2415 * - split compressed inline extents. annoying: we need to
2416 * decompress into destination's address_space (the file offset
2417 * may change, so source mapping won't do), then recompress (or
2418 * otherwise reinsert) a subrange.
2419 * - allow ranges within the same file to be cloned (provided
2420 * they don't overlap)?
2423 /* the destination must be opened for writing */
2424 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2427 if (btrfs_root_readonly(root))
2430 ret = mnt_want_write_file(file);
2434 src_file = fdget(srcfd);
2435 if (!src_file.file) {
2437 goto out_drop_write;
2441 if (src_file.file->f_path.mnt != file->f_path.mnt)
2444 src = src_file.file->f_dentry->d_inode;
2450 /* the src must be open for reading */
2451 if (!(src_file.file->f_mode & FMODE_READ))
2454 /* don't make the dst file partly checksummed */
2455 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2456 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2460 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2464 if (src->i_sb != inode->i_sb)
2468 buf = vmalloc(btrfs_level_size(root, 0));
2472 path = btrfs_alloc_path();
2480 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2481 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2483 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2484 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2487 /* determine range to clone */
2489 if (off + len > src->i_size || off + len < off)
2492 olen = len = src->i_size - off;
2493 /* if we extend to eof, continue to block boundary */
2494 if (off + len == src->i_size)
2495 len = ALIGN(src->i_size, bs) - off;
2497 /* verify the end result is block aligned */
2498 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2499 !IS_ALIGNED(destoff, bs))
2502 if (destoff > inode->i_size) {
2503 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2508 /* truncate page cache pages from target inode range */
2509 truncate_inode_pages_range(&inode->i_data, destoff,
2510 PAGE_CACHE_ALIGN(destoff + len) - 1);
2512 /* do any pending delalloc/csum calc on src, one way or
2513 another, and lock file content */
2515 struct btrfs_ordered_extent *ordered;
2516 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2517 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
2519 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2520 EXTENT_DELALLOC, 0, NULL))
2522 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2524 btrfs_put_ordered_extent(ordered);
2525 btrfs_wait_ordered_range(src, off, len);
2529 key.objectid = btrfs_ino(src);
2530 key.type = BTRFS_EXTENT_DATA_KEY;
2535 * note the key will change type as we walk through the
2538 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2543 nritems = btrfs_header_nritems(path->nodes[0]);
2544 if (path->slots[0] >= nritems) {
2545 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2550 nritems = btrfs_header_nritems(path->nodes[0]);
2552 leaf = path->nodes[0];
2553 slot = path->slots[0];
2555 btrfs_item_key_to_cpu(leaf, &key, slot);
2556 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2557 key.objectid != btrfs_ino(src))
2560 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2561 struct btrfs_file_extent_item *extent;
2564 struct btrfs_key new_key;
2565 u64 disko = 0, diskl = 0;
2566 u64 datao = 0, datal = 0;
2570 size = btrfs_item_size_nr(leaf, slot);
2571 read_extent_buffer(leaf, buf,
2572 btrfs_item_ptr_offset(leaf, slot),
2575 extent = btrfs_item_ptr(leaf, slot,
2576 struct btrfs_file_extent_item);
2577 comp = btrfs_file_extent_compression(leaf, extent);
2578 type = btrfs_file_extent_type(leaf, extent);
2579 if (type == BTRFS_FILE_EXTENT_REG ||
2580 type == BTRFS_FILE_EXTENT_PREALLOC) {
2581 disko = btrfs_file_extent_disk_bytenr(leaf,
2583 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2585 datao = btrfs_file_extent_offset(leaf, extent);
2586 datal = btrfs_file_extent_num_bytes(leaf,
2588 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2589 /* take upper bound, may be compressed */
2590 datal = btrfs_file_extent_ram_bytes(leaf,
2593 btrfs_release_path(path);
2595 if (key.offset + datal <= off ||
2596 key.offset >= off + len - 1)
2599 memcpy(&new_key, &key, sizeof(new_key));
2600 new_key.objectid = btrfs_ino(inode);
2601 if (off <= key.offset)
2602 new_key.offset = key.offset + destoff - off;
2604 new_key.offset = destoff;
2607 * 1 - adjusting old extent (we may have to split it)
2608 * 1 - add new extent
2611 trans = btrfs_start_transaction(root, 3);
2612 if (IS_ERR(trans)) {
2613 ret = PTR_ERR(trans);
2617 if (type == BTRFS_FILE_EXTENT_REG ||
2618 type == BTRFS_FILE_EXTENT_PREALLOC) {
2620 * a | --- range to clone ---| b
2621 * | ------------- extent ------------- |
2624 /* substract range b */
2625 if (key.offset + datal > off + len)
2626 datal = off + len - key.offset;
2628 /* substract range a */
2629 if (off > key.offset) {
2630 datao += off - key.offset;
2631 datal -= off - key.offset;
2634 ret = btrfs_drop_extents(trans, root, inode,
2636 new_key.offset + datal,
2639 btrfs_abort_transaction(trans, root,
2641 btrfs_end_transaction(trans, root);
2645 ret = btrfs_insert_empty_item(trans, root, path,
2648 btrfs_abort_transaction(trans, root,
2650 btrfs_end_transaction(trans, root);
2654 leaf = path->nodes[0];
2655 slot = path->slots[0];
2656 write_extent_buffer(leaf, buf,
2657 btrfs_item_ptr_offset(leaf, slot),
2660 extent = btrfs_item_ptr(leaf, slot,
2661 struct btrfs_file_extent_item);
2663 /* disko == 0 means it's a hole */
2667 btrfs_set_file_extent_offset(leaf, extent,
2669 btrfs_set_file_extent_num_bytes(leaf, extent,
2672 inode_add_bytes(inode, datal);
2673 ret = btrfs_inc_extent_ref(trans, root,
2675 root->root_key.objectid,
2677 new_key.offset - datao,
2680 btrfs_abort_transaction(trans,
2683 btrfs_end_transaction(trans,
2689 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2692 if (off > key.offset) {
2693 skip = off - key.offset;
2694 new_key.offset += skip;
2697 if (key.offset + datal > off + len)
2698 trim = key.offset + datal - (off + len);
2700 if (comp && (skip || trim)) {
2702 btrfs_end_transaction(trans, root);
2705 size -= skip + trim;
2706 datal -= skip + trim;
2708 ret = btrfs_drop_extents(trans, root, inode,
2710 new_key.offset + datal,
2713 btrfs_abort_transaction(trans, root,
2715 btrfs_end_transaction(trans, root);
2719 ret = btrfs_insert_empty_item(trans, root, path,
2722 btrfs_abort_transaction(trans, root,
2724 btrfs_end_transaction(trans, root);
2730 btrfs_file_extent_calc_inline_size(0);
2731 memmove(buf+start, buf+start+skip,
2735 leaf = path->nodes[0];
2736 slot = path->slots[0];
2737 write_extent_buffer(leaf, buf,
2738 btrfs_item_ptr_offset(leaf, slot),
2740 inode_add_bytes(inode, datal);
2743 btrfs_mark_buffer_dirty(leaf);
2744 btrfs_release_path(path);
2746 inode_inc_iversion(inode);
2747 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2750 * we round up to the block size at eof when
2751 * determining which extents to clone above,
2752 * but shouldn't round up the file size
2754 endoff = new_key.offset + datal;
2755 if (endoff > destoff+olen)
2756 endoff = destoff+olen;
2757 if (endoff > inode->i_size)
2758 btrfs_i_size_write(inode, endoff);
2760 ret = btrfs_update_inode(trans, root, inode);
2762 btrfs_abort_transaction(trans, root, ret);
2763 btrfs_end_transaction(trans, root);
2766 ret = btrfs_end_transaction(trans, root);
2769 btrfs_release_path(path);
2774 btrfs_release_path(path);
2775 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2777 mutex_unlock(&src->i_mutex);
2778 mutex_unlock(&inode->i_mutex);
2780 btrfs_free_path(path);
2784 mnt_drop_write_file(file);
2788 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2790 struct btrfs_ioctl_clone_range_args args;
2792 if (copy_from_user(&args, argp, sizeof(args)))
2794 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2795 args.src_length, args.dest_offset);
2799 * there are many ways the trans_start and trans_end ioctls can lead
2800 * to deadlocks. They should only be used by applications that
2801 * basically own the machine, and have a very in depth understanding
2802 * of all the possible deadlocks and enospc problems.
2804 static long btrfs_ioctl_trans_start(struct file *file)
2806 struct inode *inode = fdentry(file)->d_inode;
2807 struct btrfs_root *root = BTRFS_I(inode)->root;
2808 struct btrfs_trans_handle *trans;
2812 if (!capable(CAP_SYS_ADMIN))
2816 if (file->private_data)
2820 if (btrfs_root_readonly(root))
2823 ret = mnt_want_write_file(file);
2827 atomic_inc(&root->fs_info->open_ioctl_trans);
2830 trans = btrfs_start_ioctl_transaction(root);
2834 file->private_data = trans;
2838 atomic_dec(&root->fs_info->open_ioctl_trans);
2839 mnt_drop_write_file(file);
2844 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2846 struct inode *inode = fdentry(file)->d_inode;
2847 struct btrfs_root *root = BTRFS_I(inode)->root;
2848 struct btrfs_root *new_root;
2849 struct btrfs_dir_item *di;
2850 struct btrfs_trans_handle *trans;
2851 struct btrfs_path *path;
2852 struct btrfs_key location;
2853 struct btrfs_disk_key disk_key;
2858 if (!capable(CAP_SYS_ADMIN))
2861 ret = mnt_want_write_file(file);
2865 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2871 objectid = root->root_key.objectid;
2873 location.objectid = objectid;
2874 location.type = BTRFS_ROOT_ITEM_KEY;
2875 location.offset = (u64)-1;
2877 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2878 if (IS_ERR(new_root)) {
2879 ret = PTR_ERR(new_root);
2883 if (btrfs_root_refs(&new_root->root_item) == 0) {
2888 path = btrfs_alloc_path();
2893 path->leave_spinning = 1;
2895 trans = btrfs_start_transaction(root, 1);
2896 if (IS_ERR(trans)) {
2897 btrfs_free_path(path);
2898 ret = PTR_ERR(trans);
2902 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
2903 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2904 dir_id, "default", 7, 1);
2905 if (IS_ERR_OR_NULL(di)) {
2906 btrfs_free_path(path);
2907 btrfs_end_transaction(trans, root);
2908 printk(KERN_ERR "Umm, you don't have the default dir item, "
2909 "this isn't going to work\n");
2914 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2915 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2916 btrfs_mark_buffer_dirty(path->nodes[0]);
2917 btrfs_free_path(path);
2919 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
2920 btrfs_end_transaction(trans, root);
2922 mnt_drop_write_file(file);
2926 void btrfs_get_block_group_info(struct list_head *groups_list,
2927 struct btrfs_ioctl_space_info *space)
2929 struct btrfs_block_group_cache *block_group;
2931 space->total_bytes = 0;
2932 space->used_bytes = 0;
2934 list_for_each_entry(block_group, groups_list, list) {
2935 space->flags = block_group->flags;
2936 space->total_bytes += block_group->key.offset;
2937 space->used_bytes +=
2938 btrfs_block_group_used(&block_group->item);
2942 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2944 struct btrfs_ioctl_space_args space_args;
2945 struct btrfs_ioctl_space_info space;
2946 struct btrfs_ioctl_space_info *dest;
2947 struct btrfs_ioctl_space_info *dest_orig;
2948 struct btrfs_ioctl_space_info __user *user_dest;
2949 struct btrfs_space_info *info;
2950 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2951 BTRFS_BLOCK_GROUP_SYSTEM,
2952 BTRFS_BLOCK_GROUP_METADATA,
2953 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2960 if (copy_from_user(&space_args,
2961 (struct btrfs_ioctl_space_args __user *)arg,
2962 sizeof(space_args)))
2965 for (i = 0; i < num_types; i++) {
2966 struct btrfs_space_info *tmp;
2970 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2972 if (tmp->flags == types[i]) {
2982 down_read(&info->groups_sem);
2983 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2984 if (!list_empty(&info->block_groups[c]))
2987 up_read(&info->groups_sem);
2990 /* space_slots == 0 means they are asking for a count */
2991 if (space_args.space_slots == 0) {
2992 space_args.total_spaces = slot_count;
2996 slot_count = min_t(u64, space_args.space_slots, slot_count);
2998 alloc_size = sizeof(*dest) * slot_count;
3000 /* we generally have at most 6 or so space infos, one for each raid
3001 * level. So, a whole page should be more than enough for everyone
3003 if (alloc_size > PAGE_CACHE_SIZE)
3006 space_args.total_spaces = 0;
3007 dest = kmalloc(alloc_size, GFP_NOFS);
3012 /* now we have a buffer to copy into */
3013 for (i = 0; i < num_types; i++) {
3014 struct btrfs_space_info *tmp;
3021 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3023 if (tmp->flags == types[i]) {
3032 down_read(&info->groups_sem);
3033 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3034 if (!list_empty(&info->block_groups[c])) {
3035 btrfs_get_block_group_info(
3036 &info->block_groups[c], &space);
3037 memcpy(dest, &space, sizeof(space));
3039 space_args.total_spaces++;
3045 up_read(&info->groups_sem);
3048 user_dest = (struct btrfs_ioctl_space_info __user *)
3049 (arg + sizeof(struct btrfs_ioctl_space_args));
3051 if (copy_to_user(user_dest, dest_orig, alloc_size))
3056 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3063 * there are many ways the trans_start and trans_end ioctls can lead
3064 * to deadlocks. They should only be used by applications that
3065 * basically own the machine, and have a very in depth understanding
3066 * of all the possible deadlocks and enospc problems.
3068 long btrfs_ioctl_trans_end(struct file *file)
3070 struct inode *inode = fdentry(file)->d_inode;
3071 struct btrfs_root *root = BTRFS_I(inode)->root;
3072 struct btrfs_trans_handle *trans;
3074 trans = file->private_data;
3077 file->private_data = NULL;
3079 btrfs_end_transaction(trans, root);
3081 atomic_dec(&root->fs_info->open_ioctl_trans);
3083 mnt_drop_write_file(file);
3087 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3090 struct btrfs_trans_handle *trans;
3094 trans = btrfs_attach_transaction(root);
3095 if (IS_ERR(trans)) {
3096 if (PTR_ERR(trans) != -ENOENT)
3097 return PTR_ERR(trans);
3099 /* No running transaction, don't bother */
3100 transid = root->fs_info->last_trans_committed;
3103 transid = trans->transid;
3104 ret = btrfs_commit_transaction_async(trans, root, 0);
3106 btrfs_end_transaction(trans, root);
3111 if (copy_to_user(argp, &transid, sizeof(transid)))
3116 static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3122 if (copy_from_user(&transid, argp, sizeof(transid)))
3125 transid = 0; /* current trans */
3127 return btrfs_wait_for_commit(root, transid);
3130 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3132 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3133 struct btrfs_ioctl_scrub_args *sa;
3136 if (!capable(CAP_SYS_ADMIN))
3139 sa = memdup_user(arg, sizeof(*sa));
3143 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3144 ret = mnt_want_write_file(file);
3149 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
3150 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3153 if (copy_to_user(arg, sa, sizeof(*sa)))
3156 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3157 mnt_drop_write_file(file);
3163 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3165 if (!capable(CAP_SYS_ADMIN))
3168 return btrfs_scrub_cancel(root->fs_info);
3171 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3174 struct btrfs_ioctl_scrub_args *sa;
3177 if (!capable(CAP_SYS_ADMIN))
3180 sa = memdup_user(arg, sizeof(*sa));
3184 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3186 if (copy_to_user(arg, sa, sizeof(*sa)))
3193 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3196 struct btrfs_ioctl_get_dev_stats *sa;
3199 sa = memdup_user(arg, sizeof(*sa));
3203 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3208 ret = btrfs_get_dev_stats(root, sa);
3210 if (copy_to_user(arg, sa, sizeof(*sa)))
3217 static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3219 struct btrfs_ioctl_dev_replace_args *p;
3222 if (!capable(CAP_SYS_ADMIN))
3225 p = memdup_user(arg, sizeof(*p));
3230 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3232 &root->fs_info->mutually_exclusive_operation_running,
3234 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3237 ret = btrfs_dev_replace_start(root, p);
3239 &root->fs_info->mutually_exclusive_operation_running,
3243 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3244 btrfs_dev_replace_status(root->fs_info, p);
3247 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3248 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3255 if (copy_to_user(arg, p, sizeof(*p)))
3262 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3268 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3269 struct inode_fs_paths *ipath = NULL;
3270 struct btrfs_path *path;
3272 if (!capable(CAP_SYS_ADMIN))
3275 path = btrfs_alloc_path();
3281 ipa = memdup_user(arg, sizeof(*ipa));
3288 size = min_t(u32, ipa->size, 4096);
3289 ipath = init_ipath(size, root, path);
3290 if (IS_ERR(ipath)) {
3291 ret = PTR_ERR(ipath);
3296 ret = paths_from_inode(ipa->inum, ipath);
3300 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3301 rel_ptr = ipath->fspath->val[i] -
3302 (u64)(unsigned long)ipath->fspath->val;
3303 ipath->fspath->val[i] = rel_ptr;
3306 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3307 (void *)(unsigned long)ipath->fspath, size);
3314 btrfs_free_path(path);
3321 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3323 struct btrfs_data_container *inodes = ctx;
3324 const size_t c = 3 * sizeof(u64);
3326 if (inodes->bytes_left >= c) {
3327 inodes->bytes_left -= c;
3328 inodes->val[inodes->elem_cnt] = inum;
3329 inodes->val[inodes->elem_cnt + 1] = offset;
3330 inodes->val[inodes->elem_cnt + 2] = root;
3331 inodes->elem_cnt += 3;
3333 inodes->bytes_missing += c - inodes->bytes_left;
3334 inodes->bytes_left = 0;
3335 inodes->elem_missed += 3;
3341 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3346 struct btrfs_ioctl_logical_ino_args *loi;
3347 struct btrfs_data_container *inodes = NULL;
3348 struct btrfs_path *path = NULL;
3350 if (!capable(CAP_SYS_ADMIN))
3353 loi = memdup_user(arg, sizeof(*loi));
3360 path = btrfs_alloc_path();
3366 size = min_t(u32, loi->size, 64 * 1024);
3367 inodes = init_data_container(size);
3368 if (IS_ERR(inodes)) {
3369 ret = PTR_ERR(inodes);
3374 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3375 build_ino_list, inodes);
3381 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3382 (void *)(unsigned long)inodes, size);
3387 btrfs_free_path(path);
3394 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3395 struct btrfs_ioctl_balance_args *bargs)
3397 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3399 bargs->flags = bctl->flags;
3401 if (atomic_read(&fs_info->balance_running))
3402 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3403 if (atomic_read(&fs_info->balance_pause_req))
3404 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3405 if (atomic_read(&fs_info->balance_cancel_req))
3406 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3408 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3409 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3410 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3413 spin_lock(&fs_info->balance_lock);
3414 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3415 spin_unlock(&fs_info->balance_lock);
3417 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3421 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3423 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3424 struct btrfs_fs_info *fs_info = root->fs_info;
3425 struct btrfs_ioctl_balance_args *bargs;
3426 struct btrfs_balance_control *bctl;
3428 int need_to_clear_lock = 0;
3430 if (!capable(CAP_SYS_ADMIN))
3433 ret = mnt_want_write_file(file);
3437 mutex_lock(&fs_info->volume_mutex);
3438 mutex_lock(&fs_info->balance_mutex);
3441 bargs = memdup_user(arg, sizeof(*bargs));
3442 if (IS_ERR(bargs)) {
3443 ret = PTR_ERR(bargs);
3447 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3448 if (!fs_info->balance_ctl) {
3453 bctl = fs_info->balance_ctl;
3454 spin_lock(&fs_info->balance_lock);
3455 bctl->flags |= BTRFS_BALANCE_RESUME;
3456 spin_unlock(&fs_info->balance_lock);
3464 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
3466 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3470 need_to_clear_lock = 1;
3472 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3478 bctl->fs_info = fs_info;
3480 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3481 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3482 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3484 bctl->flags = bargs->flags;
3486 /* balance everything - no filters */
3487 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3491 ret = btrfs_balance(bctl, bargs);
3493 * bctl is freed in __cancel_balance or in free_fs_info if
3494 * restriper was paused all the way until unmount
3497 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3504 if (need_to_clear_lock)
3505 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
3507 mutex_unlock(&fs_info->balance_mutex);
3508 mutex_unlock(&fs_info->volume_mutex);
3509 mnt_drop_write_file(file);
3513 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3515 if (!capable(CAP_SYS_ADMIN))
3519 case BTRFS_BALANCE_CTL_PAUSE:
3520 return btrfs_pause_balance(root->fs_info);
3521 case BTRFS_BALANCE_CTL_CANCEL:
3522 return btrfs_cancel_balance(root->fs_info);
3528 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3531 struct btrfs_fs_info *fs_info = root->fs_info;
3532 struct btrfs_ioctl_balance_args *bargs;
3535 if (!capable(CAP_SYS_ADMIN))
3538 mutex_lock(&fs_info->balance_mutex);
3539 if (!fs_info->balance_ctl) {
3544 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3550 update_ioctl_balance_args(fs_info, 1, bargs);
3552 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3557 mutex_unlock(&fs_info->balance_mutex);
3561 static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3563 struct btrfs_ioctl_quota_ctl_args *sa;
3564 struct btrfs_trans_handle *trans = NULL;
3568 if (!capable(CAP_SYS_ADMIN))
3571 if (root->fs_info->sb->s_flags & MS_RDONLY)
3574 sa = memdup_user(arg, sizeof(*sa));
3578 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3579 trans = btrfs_start_transaction(root, 2);
3580 if (IS_ERR(trans)) {
3581 ret = PTR_ERR(trans);
3587 case BTRFS_QUOTA_CTL_ENABLE:
3588 ret = btrfs_quota_enable(trans, root->fs_info);
3590 case BTRFS_QUOTA_CTL_DISABLE:
3591 ret = btrfs_quota_disable(trans, root->fs_info);
3593 case BTRFS_QUOTA_CTL_RESCAN:
3594 ret = btrfs_quota_rescan(root->fs_info);
3601 if (copy_to_user(arg, sa, sizeof(*sa)))
3605 err = btrfs_commit_transaction(trans, root);
3615 static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3617 struct btrfs_ioctl_qgroup_assign_args *sa;
3618 struct btrfs_trans_handle *trans;
3622 if (!capable(CAP_SYS_ADMIN))
3625 if (root->fs_info->sb->s_flags & MS_RDONLY)
3628 sa = memdup_user(arg, sizeof(*sa));
3632 trans = btrfs_join_transaction(root);
3633 if (IS_ERR(trans)) {
3634 ret = PTR_ERR(trans);
3638 /* FIXME: check if the IDs really exist */
3640 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3643 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3647 err = btrfs_end_transaction(trans, root);
3656 static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3658 struct btrfs_ioctl_qgroup_create_args *sa;
3659 struct btrfs_trans_handle *trans;
3663 if (!capable(CAP_SYS_ADMIN))
3666 if (root->fs_info->sb->s_flags & MS_RDONLY)
3669 sa = memdup_user(arg, sizeof(*sa));
3673 trans = btrfs_join_transaction(root);
3674 if (IS_ERR(trans)) {
3675 ret = PTR_ERR(trans);
3679 /* FIXME: check if the IDs really exist */
3681 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3684 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3687 err = btrfs_end_transaction(trans, root);
3696 static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3698 struct btrfs_ioctl_qgroup_limit_args *sa;
3699 struct btrfs_trans_handle *trans;
3704 if (!capable(CAP_SYS_ADMIN))
3707 if (root->fs_info->sb->s_flags & MS_RDONLY)
3710 sa = memdup_user(arg, sizeof(*sa));
3714 trans = btrfs_join_transaction(root);
3715 if (IS_ERR(trans)) {
3716 ret = PTR_ERR(trans);
3720 qgroupid = sa->qgroupid;
3722 /* take the current subvol as qgroup */
3723 qgroupid = root->root_key.objectid;
3726 /* FIXME: check if the IDs really exist */
3727 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3729 err = btrfs_end_transaction(trans, root);
3738 static long btrfs_ioctl_set_received_subvol(struct file *file,
3741 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3742 struct inode *inode = fdentry(file)->d_inode;
3743 struct btrfs_root *root = BTRFS_I(inode)->root;
3744 struct btrfs_root_item *root_item = &root->root_item;
3745 struct btrfs_trans_handle *trans;
3746 struct timespec ct = CURRENT_TIME;
3749 ret = mnt_want_write_file(file);
3753 down_write(&root->fs_info->subvol_sem);
3755 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3760 if (btrfs_root_readonly(root)) {
3765 if (!inode_owner_or_capable(inode)) {
3770 sa = memdup_user(arg, sizeof(*sa));
3777 trans = btrfs_start_transaction(root, 1);
3778 if (IS_ERR(trans)) {
3779 ret = PTR_ERR(trans);
3784 sa->rtransid = trans->transid;
3785 sa->rtime.sec = ct.tv_sec;
3786 sa->rtime.nsec = ct.tv_nsec;
3788 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3789 btrfs_set_root_stransid(root_item, sa->stransid);
3790 btrfs_set_root_rtransid(root_item, sa->rtransid);
3791 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3792 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3793 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3794 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3796 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3797 &root->root_key, &root->root_item);
3799 btrfs_end_transaction(trans, root);
3803 ret = btrfs_commit_transaction(trans, root);
3808 ret = copy_to_user(arg, sa, sizeof(*sa));
3814 up_write(&root->fs_info->subvol_sem);
3815 mnt_drop_write_file(file);
3819 long btrfs_ioctl(struct file *file, unsigned int
3820 cmd, unsigned long arg)
3822 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3823 void __user *argp = (void __user *)arg;
3826 case FS_IOC_GETFLAGS:
3827 return btrfs_ioctl_getflags(file, argp);
3828 case FS_IOC_SETFLAGS:
3829 return btrfs_ioctl_setflags(file, argp);
3830 case FS_IOC_GETVERSION:
3831 return btrfs_ioctl_getversion(file, argp);
3833 return btrfs_ioctl_fitrim(file, argp);
3834 case BTRFS_IOC_SNAP_CREATE:
3835 return btrfs_ioctl_snap_create(file, argp, 0);
3836 case BTRFS_IOC_SNAP_CREATE_V2:
3837 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3838 case BTRFS_IOC_SUBVOL_CREATE:
3839 return btrfs_ioctl_snap_create(file, argp, 1);
3840 case BTRFS_IOC_SUBVOL_CREATE_V2:
3841 return btrfs_ioctl_snap_create_v2(file, argp, 1);
3842 case BTRFS_IOC_SNAP_DESTROY:
3843 return btrfs_ioctl_snap_destroy(file, argp);
3844 case BTRFS_IOC_SUBVOL_GETFLAGS:
3845 return btrfs_ioctl_subvol_getflags(file, argp);
3846 case BTRFS_IOC_SUBVOL_SETFLAGS:
3847 return btrfs_ioctl_subvol_setflags(file, argp);
3848 case BTRFS_IOC_DEFAULT_SUBVOL:
3849 return btrfs_ioctl_default_subvol(file, argp);
3850 case BTRFS_IOC_DEFRAG:
3851 return btrfs_ioctl_defrag(file, NULL);
3852 case BTRFS_IOC_DEFRAG_RANGE:
3853 return btrfs_ioctl_defrag(file, argp);
3854 case BTRFS_IOC_RESIZE:
3855 return btrfs_ioctl_resize(file, argp);
3856 case BTRFS_IOC_ADD_DEV:
3857 return btrfs_ioctl_add_dev(root, argp);
3858 case BTRFS_IOC_RM_DEV:
3859 return btrfs_ioctl_rm_dev(file, argp);
3860 case BTRFS_IOC_FS_INFO:
3861 return btrfs_ioctl_fs_info(root, argp);
3862 case BTRFS_IOC_DEV_INFO:
3863 return btrfs_ioctl_dev_info(root, argp);
3864 case BTRFS_IOC_BALANCE:
3865 return btrfs_ioctl_balance(file, NULL);
3866 case BTRFS_IOC_CLONE:
3867 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3868 case BTRFS_IOC_CLONE_RANGE:
3869 return btrfs_ioctl_clone_range(file, argp);
3870 case BTRFS_IOC_TRANS_START:
3871 return btrfs_ioctl_trans_start(file);
3872 case BTRFS_IOC_TRANS_END:
3873 return btrfs_ioctl_trans_end(file);
3874 case BTRFS_IOC_TREE_SEARCH:
3875 return btrfs_ioctl_tree_search(file, argp);
3876 case BTRFS_IOC_INO_LOOKUP:
3877 return btrfs_ioctl_ino_lookup(file, argp);
3878 case BTRFS_IOC_INO_PATHS:
3879 return btrfs_ioctl_ino_to_path(root, argp);
3880 case BTRFS_IOC_LOGICAL_INO:
3881 return btrfs_ioctl_logical_to_ino(root, argp);
3882 case BTRFS_IOC_SPACE_INFO:
3883 return btrfs_ioctl_space_info(root, argp);
3884 case BTRFS_IOC_SYNC:
3885 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3887 case BTRFS_IOC_START_SYNC:
3888 return btrfs_ioctl_start_sync(root, argp);
3889 case BTRFS_IOC_WAIT_SYNC:
3890 return btrfs_ioctl_wait_sync(root, argp);
3891 case BTRFS_IOC_SCRUB:
3892 return btrfs_ioctl_scrub(file, argp);
3893 case BTRFS_IOC_SCRUB_CANCEL:
3894 return btrfs_ioctl_scrub_cancel(root, argp);
3895 case BTRFS_IOC_SCRUB_PROGRESS:
3896 return btrfs_ioctl_scrub_progress(root, argp);
3897 case BTRFS_IOC_BALANCE_V2:
3898 return btrfs_ioctl_balance(file, argp);
3899 case BTRFS_IOC_BALANCE_CTL:
3900 return btrfs_ioctl_balance_ctl(root, arg);
3901 case BTRFS_IOC_BALANCE_PROGRESS:
3902 return btrfs_ioctl_balance_progress(root, argp);
3903 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3904 return btrfs_ioctl_set_received_subvol(file, argp);
3905 case BTRFS_IOC_SEND:
3906 return btrfs_ioctl_send(file, argp);
3907 case BTRFS_IOC_GET_DEV_STATS:
3908 return btrfs_ioctl_get_dev_stats(root, argp);
3909 case BTRFS_IOC_QUOTA_CTL:
3910 return btrfs_ioctl_quota_ctl(root, argp);
3911 case BTRFS_IOC_QGROUP_ASSIGN:
3912 return btrfs_ioctl_qgroup_assign(root, argp);
3913 case BTRFS_IOC_QGROUP_CREATE:
3914 return btrfs_ioctl_qgroup_create(root, argp);
3915 case BTRFS_IOC_QGROUP_LIMIT:
3916 return btrfs_ioctl_qgroup_limit(root, argp);
3917 case BTRFS_IOC_DEV_REPLACE:
3918 return btrfs_ioctl_dev_replace(root, argp);