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"
59 /* Mask out flags that are inappropriate for the given type of inode. */
60 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
64 else if (S_ISREG(mode))
65 return flags & ~FS_DIRSYNC_FL;
67 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
71 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
73 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
75 unsigned int iflags = 0;
77 if (flags & BTRFS_INODE_SYNC)
79 if (flags & BTRFS_INODE_IMMUTABLE)
80 iflags |= FS_IMMUTABLE_FL;
81 if (flags & BTRFS_INODE_APPEND)
82 iflags |= FS_APPEND_FL;
83 if (flags & BTRFS_INODE_NODUMP)
84 iflags |= FS_NODUMP_FL;
85 if (flags & BTRFS_INODE_NOATIME)
86 iflags |= FS_NOATIME_FL;
87 if (flags & BTRFS_INODE_DIRSYNC)
88 iflags |= FS_DIRSYNC_FL;
89 if (flags & BTRFS_INODE_NODATACOW)
90 iflags |= FS_NOCOW_FL;
92 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
93 iflags |= FS_COMPR_FL;
94 else if (flags & BTRFS_INODE_NOCOMPRESS)
95 iflags |= FS_NOCOMP_FL;
101 * Update inode->i_flags based on the btrfs internal flags.
103 void btrfs_update_iflags(struct inode *inode)
105 struct btrfs_inode *ip = BTRFS_I(inode);
107 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
109 if (ip->flags & BTRFS_INODE_SYNC)
110 inode->i_flags |= S_SYNC;
111 if (ip->flags & BTRFS_INODE_IMMUTABLE)
112 inode->i_flags |= S_IMMUTABLE;
113 if (ip->flags & BTRFS_INODE_APPEND)
114 inode->i_flags |= S_APPEND;
115 if (ip->flags & BTRFS_INODE_NOATIME)
116 inode->i_flags |= S_NOATIME;
117 if (ip->flags & BTRFS_INODE_DIRSYNC)
118 inode->i_flags |= S_DIRSYNC;
122 * Inherit flags from the parent inode.
124 * Currently only the compression flags and the cow flags are inherited.
126 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
133 flags = BTRFS_I(dir)->flags;
135 if (flags & BTRFS_INODE_NOCOMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
138 } else if (flags & BTRFS_INODE_COMPRESS) {
139 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
140 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
143 if (flags & BTRFS_INODE_NODATACOW)
144 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
146 btrfs_update_iflags(inode);
149 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
151 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
152 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
154 if (copy_to_user(arg, &flags, sizeof(flags)))
159 static int check_flags(unsigned int flags)
161 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
162 FS_NOATIME_FL | FS_NODUMP_FL | \
163 FS_SYNC_FL | FS_DIRSYNC_FL | \
164 FS_NOCOMP_FL | FS_COMPR_FL |
168 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
174 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
176 struct inode *inode = file->f_path.dentry->d_inode;
177 struct btrfs_inode *ip = BTRFS_I(inode);
178 struct btrfs_root *root = ip->root;
179 struct btrfs_trans_handle *trans;
180 unsigned int flags, oldflags;
183 unsigned int i_oldflags;
186 if (btrfs_root_readonly(root))
189 if (copy_from_user(&flags, arg, sizeof(flags)))
192 ret = check_flags(flags);
196 if (!inode_owner_or_capable(inode))
199 ret = mnt_want_write_file(file);
203 mutex_lock(&inode->i_mutex);
205 ip_oldflags = ip->flags;
206 i_oldflags = inode->i_flags;
207 mode = inode->i_mode;
209 flags = btrfs_mask_flags(inode->i_mode, flags);
210 oldflags = btrfs_flags_to_ioctl(ip->flags);
211 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
212 if (!capable(CAP_LINUX_IMMUTABLE)) {
218 if (flags & FS_SYNC_FL)
219 ip->flags |= BTRFS_INODE_SYNC;
221 ip->flags &= ~BTRFS_INODE_SYNC;
222 if (flags & FS_IMMUTABLE_FL)
223 ip->flags |= BTRFS_INODE_IMMUTABLE;
225 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
226 if (flags & FS_APPEND_FL)
227 ip->flags |= BTRFS_INODE_APPEND;
229 ip->flags &= ~BTRFS_INODE_APPEND;
230 if (flags & FS_NODUMP_FL)
231 ip->flags |= BTRFS_INODE_NODUMP;
233 ip->flags &= ~BTRFS_INODE_NODUMP;
234 if (flags & FS_NOATIME_FL)
235 ip->flags |= BTRFS_INODE_NOATIME;
237 ip->flags &= ~BTRFS_INODE_NOATIME;
238 if (flags & FS_DIRSYNC_FL)
239 ip->flags |= BTRFS_INODE_DIRSYNC;
241 ip->flags &= ~BTRFS_INODE_DIRSYNC;
242 if (flags & FS_NOCOW_FL) {
245 * It's safe to turn csums off here, no extents exist.
246 * Otherwise we want the flag to reflect the real COW
247 * status of the file and will not set it.
249 if (inode->i_size == 0)
250 ip->flags |= BTRFS_INODE_NODATACOW
251 | BTRFS_INODE_NODATASUM;
253 ip->flags |= BTRFS_INODE_NODATACOW;
257 * Revert back under same assuptions as above
260 if (inode->i_size == 0)
261 ip->flags &= ~(BTRFS_INODE_NODATACOW
262 | BTRFS_INODE_NODATASUM);
264 ip->flags &= ~BTRFS_INODE_NODATACOW;
269 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
270 * flag may be changed automatically if compression code won't make
273 if (flags & FS_NOCOMP_FL) {
274 ip->flags &= ~BTRFS_INODE_COMPRESS;
275 ip->flags |= BTRFS_INODE_NOCOMPRESS;
276 } else if (flags & FS_COMPR_FL) {
277 ip->flags |= BTRFS_INODE_COMPRESS;
278 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
280 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
283 trans = btrfs_start_transaction(root, 1);
285 ret = PTR_ERR(trans);
289 btrfs_update_iflags(inode);
290 inode_inc_iversion(inode);
291 inode->i_ctime = CURRENT_TIME;
292 ret = btrfs_update_inode(trans, root, inode);
294 btrfs_end_transaction(trans, root);
297 ip->flags = ip_oldflags;
298 inode->i_flags = i_oldflags;
302 mutex_unlock(&inode->i_mutex);
303 mnt_drop_write_file(file);
307 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
309 struct inode *inode = file->f_path.dentry->d_inode;
311 return put_user(inode->i_generation, arg);
314 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
316 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
317 struct btrfs_device *device;
318 struct request_queue *q;
319 struct fstrim_range range;
320 u64 minlen = ULLONG_MAX;
322 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
325 if (!capable(CAP_SYS_ADMIN))
329 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
333 q = bdev_get_queue(device->bdev);
334 if (blk_queue_discard(q)) {
336 minlen = min((u64)q->limits.discard_granularity,
344 if (copy_from_user(&range, arg, sizeof(range)))
346 if (range.start > total_bytes ||
347 range.len < fs_info->sb->s_blocksize)
350 range.len = min(range.len, total_bytes - range.start);
351 range.minlen = max(range.minlen, minlen);
352 ret = btrfs_trim_fs(fs_info->tree_root, &range);
356 if (copy_to_user(arg, &range, sizeof(range)))
362 static noinline int create_subvol(struct btrfs_root *root,
363 struct dentry *dentry,
364 char *name, int namelen,
366 struct btrfs_qgroup_inherit **inherit)
368 struct btrfs_trans_handle *trans;
369 struct btrfs_key key;
370 struct btrfs_root_item root_item;
371 struct btrfs_inode_item *inode_item;
372 struct extent_buffer *leaf;
373 struct btrfs_root *new_root;
374 struct dentry *parent = dentry->d_parent;
376 struct timespec cur_time = CURRENT_TIME;
380 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
384 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
388 dir = parent->d_inode;
396 trans = btrfs_start_transaction(root, 6);
398 return PTR_ERR(trans);
400 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
401 inherit ? *inherit : NULL);
405 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
406 0, objectid, NULL, 0, 0, 0);
412 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
413 btrfs_set_header_bytenr(leaf, leaf->start);
414 btrfs_set_header_generation(leaf, trans->transid);
415 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
416 btrfs_set_header_owner(leaf, objectid);
418 write_extent_buffer(leaf, root->fs_info->fsid,
419 (unsigned long)btrfs_header_fsid(leaf),
421 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
422 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
424 btrfs_mark_buffer_dirty(leaf);
426 memset(&root_item, 0, sizeof(root_item));
428 inode_item = &root_item.inode;
429 inode_item->generation = cpu_to_le64(1);
430 inode_item->size = cpu_to_le64(3);
431 inode_item->nlink = cpu_to_le32(1);
432 inode_item->nbytes = cpu_to_le64(root->leafsize);
433 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
436 root_item.byte_limit = 0;
437 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
439 btrfs_set_root_bytenr(&root_item, leaf->start);
440 btrfs_set_root_generation(&root_item, trans->transid);
441 btrfs_set_root_level(&root_item, 0);
442 btrfs_set_root_refs(&root_item, 1);
443 btrfs_set_root_used(&root_item, leaf->len);
444 btrfs_set_root_last_snapshot(&root_item, 0);
446 btrfs_set_root_generation_v2(&root_item,
447 btrfs_root_generation(&root_item));
448 uuid_le_gen(&new_uuid);
449 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
450 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
451 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
452 root_item.ctime = root_item.otime;
453 btrfs_set_root_ctransid(&root_item, trans->transid);
454 btrfs_set_root_otransid(&root_item, trans->transid);
456 btrfs_tree_unlock(leaf);
457 free_extent_buffer(leaf);
460 btrfs_set_root_dirid(&root_item, new_dirid);
462 key.objectid = objectid;
464 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
465 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
470 key.offset = (u64)-1;
471 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
472 if (IS_ERR(new_root)) {
473 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
474 ret = PTR_ERR(new_root);
478 btrfs_record_root_in_trans(trans, new_root);
480 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
482 /* We potentially lose an unused inode item here */
483 btrfs_abort_transaction(trans, root, ret);
488 * insert the directory item
490 ret = btrfs_set_inode_index(dir, &index);
492 btrfs_abort_transaction(trans, root, ret);
496 ret = btrfs_insert_dir_item(trans, root,
497 name, namelen, dir, &key,
498 BTRFS_FT_DIR, index);
500 btrfs_abort_transaction(trans, root, ret);
504 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
505 ret = btrfs_update_inode(trans, root, dir);
508 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
509 objectid, root->root_key.objectid,
510 btrfs_ino(dir), index, name, namelen);
514 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
517 *async_transid = trans->transid;
518 err = btrfs_commit_transaction_async(trans, root, 1);
520 err = btrfs_commit_transaction(trans, root);
527 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
528 char *name, int namelen, u64 *async_transid,
529 bool readonly, struct btrfs_qgroup_inherit **inherit)
532 struct btrfs_pending_snapshot *pending_snapshot;
533 struct btrfs_trans_handle *trans;
539 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
540 if (!pending_snapshot)
543 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
544 BTRFS_BLOCK_RSV_TEMP);
545 pending_snapshot->dentry = dentry;
546 pending_snapshot->root = root;
547 pending_snapshot->readonly = readonly;
549 pending_snapshot->inherit = *inherit;
550 *inherit = NULL; /* take responsibility to free it */
553 trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
555 ret = PTR_ERR(trans);
559 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
562 spin_lock(&root->fs_info->trans_lock);
563 list_add(&pending_snapshot->list,
564 &trans->transaction->pending_snapshots);
565 spin_unlock(&root->fs_info->trans_lock);
567 *async_transid = trans->transid;
568 ret = btrfs_commit_transaction_async(trans,
569 root->fs_info->extent_root, 1);
571 ret = btrfs_commit_transaction(trans,
572 root->fs_info->extent_root);
575 /* cleanup_transaction has freed this for us */
577 pending_snapshot = NULL;
581 ret = pending_snapshot->error;
585 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
589 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
591 ret = PTR_ERR(inode);
595 d_instantiate(dentry, inode);
598 kfree(pending_snapshot);
602 /* copy of check_sticky in fs/namei.c()
603 * It's inline, so penalty for filesystems that don't use sticky bit is
606 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
608 kuid_t fsuid = current_fsuid();
610 if (!(dir->i_mode & S_ISVTX))
612 if (uid_eq(inode->i_uid, fsuid))
614 if (uid_eq(dir->i_uid, fsuid))
616 return !capable(CAP_FOWNER);
619 /* copy of may_delete in fs/namei.c()
620 * Check whether we can remove a link victim from directory dir, check
621 * whether the type of victim is right.
622 * 1. We can't do it if dir is read-only (done in permission())
623 * 2. We should have write and exec permissions on dir
624 * 3. We can't remove anything from append-only dir
625 * 4. We can't do anything with immutable dir (done in permission())
626 * 5. If the sticky bit on dir is set we should either
627 * a. be owner of dir, or
628 * b. be owner of victim, or
629 * c. have CAP_FOWNER capability
630 * 6. If the victim is append-only or immutable we can't do antyhing with
631 * links pointing to it.
632 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
633 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
634 * 9. We can't remove a root or mountpoint.
635 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
636 * nfs_async_unlink().
639 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
643 if (!victim->d_inode)
646 BUG_ON(victim->d_parent->d_inode != dir);
647 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
649 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
654 if (btrfs_check_sticky(dir, victim->d_inode)||
655 IS_APPEND(victim->d_inode)||
656 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
659 if (!S_ISDIR(victim->d_inode->i_mode))
663 } else if (S_ISDIR(victim->d_inode->i_mode))
667 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
672 /* copy of may_create in fs/namei.c() */
673 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
679 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
683 * Create a new subvolume below @parent. This is largely modeled after
684 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
685 * inside this filesystem so it's quite a bit simpler.
687 static noinline int btrfs_mksubvol(struct path *parent,
688 char *name, int namelen,
689 struct btrfs_root *snap_src,
690 u64 *async_transid, bool readonly,
691 struct btrfs_qgroup_inherit **inherit)
693 struct inode *dir = parent->dentry->d_inode;
694 struct dentry *dentry;
697 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
699 dentry = lookup_one_len(name, parent->dentry, namelen);
700 error = PTR_ERR(dentry);
708 error = btrfs_may_create(dir, dentry);
712 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
714 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
718 error = create_snapshot(snap_src, dentry, name, namelen,
719 async_transid, readonly, inherit);
721 error = create_subvol(BTRFS_I(dir)->root, dentry,
722 name, namelen, async_transid, inherit);
725 fsnotify_mkdir(dir, dentry);
727 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
731 mutex_unlock(&dir->i_mutex);
736 * When we're defragging a range, we don't want to kick it off again
737 * if it is really just waiting for delalloc to send it down.
738 * If we find a nice big extent or delalloc range for the bytes in the
739 * file you want to defrag, we return 0 to let you know to skip this
742 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
744 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
745 struct extent_map *em = NULL;
746 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
749 read_lock(&em_tree->lock);
750 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
751 read_unlock(&em_tree->lock);
754 end = extent_map_end(em);
756 if (end - offset > thresh)
759 /* if we already have a nice delalloc here, just stop */
761 end = count_range_bits(io_tree, &offset, offset + thresh,
762 thresh, EXTENT_DELALLOC, 1);
769 * helper function to walk through a file and find extents
770 * newer than a specific transid, and smaller than thresh.
772 * This is used by the defragging code to find new and small
775 static int find_new_extents(struct btrfs_root *root,
776 struct inode *inode, u64 newer_than,
777 u64 *off, int thresh)
779 struct btrfs_path *path;
780 struct btrfs_key min_key;
781 struct btrfs_key max_key;
782 struct extent_buffer *leaf;
783 struct btrfs_file_extent_item *extent;
786 u64 ino = btrfs_ino(inode);
788 path = btrfs_alloc_path();
792 min_key.objectid = ino;
793 min_key.type = BTRFS_EXTENT_DATA_KEY;
794 min_key.offset = *off;
796 max_key.objectid = ino;
797 max_key.type = (u8)-1;
798 max_key.offset = (u64)-1;
800 path->keep_locks = 1;
803 ret = btrfs_search_forward(root, &min_key, &max_key,
804 path, 0, newer_than);
807 if (min_key.objectid != ino)
809 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
812 leaf = path->nodes[0];
813 extent = btrfs_item_ptr(leaf, path->slots[0],
814 struct btrfs_file_extent_item);
816 type = btrfs_file_extent_type(leaf, extent);
817 if (type == BTRFS_FILE_EXTENT_REG &&
818 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
819 check_defrag_in_cache(inode, min_key.offset, thresh)) {
820 *off = min_key.offset;
821 btrfs_free_path(path);
825 if (min_key.offset == (u64)-1)
829 btrfs_release_path(path);
832 btrfs_free_path(path);
836 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
838 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
840 struct extent_map *em;
841 u64 len = PAGE_CACHE_SIZE;
844 * hopefully we have this extent in the tree already, try without
845 * the full extent lock
847 read_lock(&em_tree->lock);
848 em = lookup_extent_mapping(em_tree, start, len);
849 read_unlock(&em_tree->lock);
852 /* get the big lock and read metadata off disk */
853 lock_extent(io_tree, start, start + len - 1);
854 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
855 unlock_extent(io_tree, start, start + len - 1);
864 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
866 struct extent_map *next;
869 /* this is the last extent */
870 if (em->start + em->len >= i_size_read(inode))
873 next = defrag_lookup_extent(inode, em->start + em->len);
874 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
877 free_extent_map(next);
881 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
882 u64 *last_len, u64 *skip, u64 *defrag_end,
885 struct extent_map *em;
887 bool next_mergeable = true;
890 * make sure that once we start defragging an extent, we keep on
893 if (start < *defrag_end)
898 em = defrag_lookup_extent(inode, start);
902 /* this will cover holes, and inline extents */
903 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
908 next_mergeable = defrag_check_next_extent(inode, em);
911 * we hit a real extent, if it is big or the next extent is not a
912 * real extent, don't bother defragging it
914 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
915 (em->len >= thresh || !next_mergeable))
919 * last_len ends up being a counter of how many bytes we've defragged.
920 * every time we choose not to defrag an extent, we reset *last_len
921 * so that the next tiny extent will force a defrag.
923 * The end result of this is that tiny extents before a single big
924 * extent will force at least part of that big extent to be defragged.
927 *defrag_end = extent_map_end(em);
930 *skip = extent_map_end(em);
939 * it doesn't do much good to defrag one or two pages
940 * at a time. This pulls in a nice chunk of pages
943 * It also makes sure the delalloc code has enough
944 * dirty data to avoid making new small extents as part
947 * It's a good idea to start RA on this range
948 * before calling this.
950 static int cluster_pages_for_defrag(struct inode *inode,
952 unsigned long start_index,
955 unsigned long file_end;
956 u64 isize = i_size_read(inode);
963 struct btrfs_ordered_extent *ordered;
964 struct extent_state *cached_state = NULL;
965 struct extent_io_tree *tree;
966 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
968 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
969 if (!isize || start_index > file_end)
972 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
974 ret = btrfs_delalloc_reserve_space(inode,
975 page_cnt << PAGE_CACHE_SHIFT);
979 tree = &BTRFS_I(inode)->io_tree;
981 /* step one, lock all the pages */
982 for (i = 0; i < page_cnt; i++) {
985 page = find_or_create_page(inode->i_mapping,
986 start_index + i, mask);
990 page_start = page_offset(page);
991 page_end = page_start + PAGE_CACHE_SIZE - 1;
993 lock_extent(tree, page_start, page_end);
994 ordered = btrfs_lookup_ordered_extent(inode,
996 unlock_extent(tree, page_start, page_end);
1001 btrfs_start_ordered_extent(inode, ordered, 1);
1002 btrfs_put_ordered_extent(ordered);
1005 * we unlocked the page above, so we need check if
1006 * it was released or not.
1008 if (page->mapping != inode->i_mapping) {
1010 page_cache_release(page);
1015 if (!PageUptodate(page)) {
1016 btrfs_readpage(NULL, page);
1018 if (!PageUptodate(page)) {
1020 page_cache_release(page);
1026 if (page->mapping != inode->i_mapping) {
1028 page_cache_release(page);
1038 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1042 * so now we have a nice long stream of locked
1043 * and up to date pages, lets wait on them
1045 for (i = 0; i < i_done; i++)
1046 wait_on_page_writeback(pages[i]);
1048 page_start = page_offset(pages[0]);
1049 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1051 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1052 page_start, page_end - 1, 0, &cached_state);
1053 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1054 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1055 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1056 &cached_state, GFP_NOFS);
1058 if (i_done != page_cnt) {
1059 spin_lock(&BTRFS_I(inode)->lock);
1060 BTRFS_I(inode)->outstanding_extents++;
1061 spin_unlock(&BTRFS_I(inode)->lock);
1062 btrfs_delalloc_release_space(inode,
1063 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1067 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1068 &cached_state, GFP_NOFS);
1070 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1071 page_start, page_end - 1, &cached_state,
1074 for (i = 0; i < i_done; i++) {
1075 clear_page_dirty_for_io(pages[i]);
1076 ClearPageChecked(pages[i]);
1077 set_page_extent_mapped(pages[i]);
1078 set_page_dirty(pages[i]);
1079 unlock_page(pages[i]);
1080 page_cache_release(pages[i]);
1084 for (i = 0; i < i_done; i++) {
1085 unlock_page(pages[i]);
1086 page_cache_release(pages[i]);
1088 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1093 int btrfs_defrag_file(struct inode *inode, struct file *file,
1094 struct btrfs_ioctl_defrag_range_args *range,
1095 u64 newer_than, unsigned long max_to_defrag)
1097 struct btrfs_root *root = BTRFS_I(inode)->root;
1098 struct file_ra_state *ra = NULL;
1099 unsigned long last_index;
1100 u64 isize = i_size_read(inode);
1104 u64 newer_off = range->start;
1106 unsigned long ra_index = 0;
1108 int defrag_count = 0;
1109 int compress_type = BTRFS_COMPRESS_ZLIB;
1110 int extent_thresh = range->extent_thresh;
1111 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1112 int cluster = max_cluster;
1113 u64 new_align = ~((u64)128 * 1024 - 1);
1114 struct page **pages = NULL;
1116 if (extent_thresh == 0)
1117 extent_thresh = 256 * 1024;
1119 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1120 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1122 if (range->compress_type)
1123 compress_type = range->compress_type;
1130 * if we were not given a file, allocate a readahead
1134 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1137 file_ra_state_init(ra, inode->i_mapping);
1142 pages = kmalloc(sizeof(struct page *) * max_cluster,
1149 /* find the last page to defrag */
1150 if (range->start + range->len > range->start) {
1151 last_index = min_t(u64, isize - 1,
1152 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1154 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1158 ret = find_new_extents(root, inode, newer_than,
1159 &newer_off, 64 * 1024);
1161 range->start = newer_off;
1163 * we always align our defrag to help keep
1164 * the extents in the file evenly spaced
1166 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1170 i = range->start >> PAGE_CACHE_SHIFT;
1173 max_to_defrag = last_index + 1;
1176 * make writeback starts from i, so the defrag range can be
1177 * written sequentially.
1179 if (i < inode->i_mapping->writeback_index)
1180 inode->i_mapping->writeback_index = i;
1182 while (i <= last_index && defrag_count < max_to_defrag &&
1183 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1184 PAGE_CACHE_SHIFT)) {
1186 * make sure we stop running if someone unmounts
1189 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1192 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1193 extent_thresh, &last_len, &skip,
1194 &defrag_end, range->flags &
1195 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1198 * the should_defrag function tells us how much to skip
1199 * bump our counter by the suggested amount
1201 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1202 i = max(i + 1, next);
1207 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1208 PAGE_CACHE_SHIFT) - i;
1209 cluster = min(cluster, max_cluster);
1211 cluster = max_cluster;
1214 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1215 BTRFS_I(inode)->force_compress = compress_type;
1217 if (i + cluster > ra_index) {
1218 ra_index = max(i, ra_index);
1219 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1221 ra_index += max_cluster;
1224 mutex_lock(&inode->i_mutex);
1225 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1227 mutex_unlock(&inode->i_mutex);
1231 defrag_count += ret;
1232 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1233 mutex_unlock(&inode->i_mutex);
1236 if (newer_off == (u64)-1)
1242 newer_off = max(newer_off + 1,
1243 (u64)i << PAGE_CACHE_SHIFT);
1245 ret = find_new_extents(root, inode,
1246 newer_than, &newer_off,
1249 range->start = newer_off;
1250 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1257 last_len += ret << PAGE_CACHE_SHIFT;
1265 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1266 filemap_flush(inode->i_mapping);
1268 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1269 /* the filemap_flush will queue IO into the worker threads, but
1270 * we have to make sure the IO is actually started and that
1271 * ordered extents get created before we return
1273 atomic_inc(&root->fs_info->async_submit_draining);
1274 while (atomic_read(&root->fs_info->nr_async_submits) ||
1275 atomic_read(&root->fs_info->async_delalloc_pages)) {
1276 wait_event(root->fs_info->async_submit_wait,
1277 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1278 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1280 atomic_dec(&root->fs_info->async_submit_draining);
1282 mutex_lock(&inode->i_mutex);
1283 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1284 mutex_unlock(&inode->i_mutex);
1287 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1288 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1300 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1306 struct btrfs_ioctl_vol_args *vol_args;
1307 struct btrfs_trans_handle *trans;
1308 struct btrfs_device *device = NULL;
1310 char *devstr = NULL;
1314 if (root->fs_info->sb->s_flags & MS_RDONLY)
1317 if (!capable(CAP_SYS_ADMIN))
1320 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1322 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1323 return -EINPROGRESS;
1326 mutex_lock(&root->fs_info->volume_mutex);
1327 vol_args = memdup_user(arg, sizeof(*vol_args));
1328 if (IS_ERR(vol_args)) {
1329 ret = PTR_ERR(vol_args);
1333 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1335 sizestr = vol_args->name;
1336 devstr = strchr(sizestr, ':');
1339 sizestr = devstr + 1;
1341 devstr = vol_args->name;
1342 devid = simple_strtoull(devstr, &end, 10);
1343 printk(KERN_INFO "btrfs: resizing devid %llu\n",
1344 (unsigned long long)devid);
1346 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1348 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1349 (unsigned long long)devid);
1353 if (device->fs_devices && device->fs_devices->seeding) {
1354 printk(KERN_INFO "btrfs: resizer unable to apply on "
1355 "seeding device %llu\n",
1356 (unsigned long long)devid);
1361 if (!strcmp(sizestr, "max"))
1362 new_size = device->bdev->bd_inode->i_size;
1364 if (sizestr[0] == '-') {
1367 } else if (sizestr[0] == '+') {
1371 new_size = memparse(sizestr, NULL);
1372 if (new_size == 0) {
1378 if (device->is_tgtdev_for_dev_replace) {
1383 old_size = device->total_bytes;
1386 if (new_size > old_size) {
1390 new_size = old_size - new_size;
1391 } else if (mod > 0) {
1392 new_size = old_size + new_size;
1395 if (new_size < 256 * 1024 * 1024) {
1399 if (new_size > device->bdev->bd_inode->i_size) {
1404 do_div(new_size, root->sectorsize);
1405 new_size *= root->sectorsize;
1407 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1408 rcu_str_deref(device->name),
1409 (unsigned long long)new_size);
1411 if (new_size > old_size) {
1412 trans = btrfs_start_transaction(root, 0);
1413 if (IS_ERR(trans)) {
1414 ret = PTR_ERR(trans);
1417 ret = btrfs_grow_device(trans, device, new_size);
1418 btrfs_commit_transaction(trans, root);
1419 } else if (new_size < old_size) {
1420 ret = btrfs_shrink_device(device, new_size);
1421 } /* equal, nothing need to do */
1426 mutex_unlock(&root->fs_info->volume_mutex);
1427 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1431 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1432 char *name, unsigned long fd, int subvol,
1433 u64 *transid, bool readonly,
1434 struct btrfs_qgroup_inherit **inherit)
1439 ret = mnt_want_write_file(file);
1443 namelen = strlen(name);
1444 if (strchr(name, '/')) {
1446 goto out_drop_write;
1449 if (name[0] == '.' &&
1450 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1452 goto out_drop_write;
1456 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1457 NULL, transid, readonly, inherit);
1459 struct fd src = fdget(fd);
1460 struct inode *src_inode;
1463 goto out_drop_write;
1466 src_inode = src.file->f_path.dentry->d_inode;
1467 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1468 printk(KERN_INFO "btrfs: Snapshot src from "
1472 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1473 BTRFS_I(src_inode)->root,
1474 transid, readonly, inherit);
1479 mnt_drop_write_file(file);
1484 static noinline int btrfs_ioctl_snap_create(struct file *file,
1485 void __user *arg, int subvol)
1487 struct btrfs_ioctl_vol_args *vol_args;
1490 vol_args = memdup_user(arg, sizeof(*vol_args));
1491 if (IS_ERR(vol_args))
1492 return PTR_ERR(vol_args);
1493 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1495 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1496 vol_args->fd, subvol,
1503 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1504 void __user *arg, int subvol)
1506 struct btrfs_ioctl_vol_args_v2 *vol_args;
1510 bool readonly = false;
1511 struct btrfs_qgroup_inherit *inherit = NULL;
1513 vol_args = memdup_user(arg, sizeof(*vol_args));
1514 if (IS_ERR(vol_args))
1515 return PTR_ERR(vol_args);
1516 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1518 if (vol_args->flags &
1519 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1520 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1525 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1527 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1529 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1530 if (vol_args->size > PAGE_CACHE_SIZE) {
1534 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1535 if (IS_ERR(inherit)) {
1536 ret = PTR_ERR(inherit);
1541 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1542 vol_args->fd, subvol, ptr,
1543 readonly, &inherit);
1545 if (ret == 0 && ptr &&
1547 offsetof(struct btrfs_ioctl_vol_args_v2,
1548 transid), ptr, sizeof(*ptr)))
1556 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1559 struct inode *inode = fdentry(file)->d_inode;
1560 struct btrfs_root *root = BTRFS_I(inode)->root;
1564 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1567 down_read(&root->fs_info->subvol_sem);
1568 if (btrfs_root_readonly(root))
1569 flags |= BTRFS_SUBVOL_RDONLY;
1570 up_read(&root->fs_info->subvol_sem);
1572 if (copy_to_user(arg, &flags, sizeof(flags)))
1578 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1581 struct inode *inode = fdentry(file)->d_inode;
1582 struct btrfs_root *root = BTRFS_I(inode)->root;
1583 struct btrfs_trans_handle *trans;
1588 ret = mnt_want_write_file(file);
1592 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1594 goto out_drop_write;
1597 if (copy_from_user(&flags, arg, sizeof(flags))) {
1599 goto out_drop_write;
1602 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1604 goto out_drop_write;
1607 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1609 goto out_drop_write;
1612 if (!inode_owner_or_capable(inode)) {
1614 goto out_drop_write;
1617 down_write(&root->fs_info->subvol_sem);
1620 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1623 root_flags = btrfs_root_flags(&root->root_item);
1624 if (flags & BTRFS_SUBVOL_RDONLY)
1625 btrfs_set_root_flags(&root->root_item,
1626 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1628 btrfs_set_root_flags(&root->root_item,
1629 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1631 trans = btrfs_start_transaction(root, 1);
1632 if (IS_ERR(trans)) {
1633 ret = PTR_ERR(trans);
1637 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1638 &root->root_key, &root->root_item);
1640 btrfs_commit_transaction(trans, root);
1643 btrfs_set_root_flags(&root->root_item, root_flags);
1645 up_write(&root->fs_info->subvol_sem);
1647 mnt_drop_write_file(file);
1653 * helper to check if the subvolume references other subvolumes
1655 static noinline int may_destroy_subvol(struct btrfs_root *root)
1657 struct btrfs_path *path;
1658 struct btrfs_key key;
1661 path = btrfs_alloc_path();
1665 key.objectid = root->root_key.objectid;
1666 key.type = BTRFS_ROOT_REF_KEY;
1667 key.offset = (u64)-1;
1669 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1676 if (path->slots[0] > 0) {
1678 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1679 if (key.objectid == root->root_key.objectid &&
1680 key.type == BTRFS_ROOT_REF_KEY)
1684 btrfs_free_path(path);
1688 static noinline int key_in_sk(struct btrfs_key *key,
1689 struct btrfs_ioctl_search_key *sk)
1691 struct btrfs_key test;
1694 test.objectid = sk->min_objectid;
1695 test.type = sk->min_type;
1696 test.offset = sk->min_offset;
1698 ret = btrfs_comp_cpu_keys(key, &test);
1702 test.objectid = sk->max_objectid;
1703 test.type = sk->max_type;
1704 test.offset = sk->max_offset;
1706 ret = btrfs_comp_cpu_keys(key, &test);
1712 static noinline int copy_to_sk(struct btrfs_root *root,
1713 struct btrfs_path *path,
1714 struct btrfs_key *key,
1715 struct btrfs_ioctl_search_key *sk,
1717 unsigned long *sk_offset,
1721 struct extent_buffer *leaf;
1722 struct btrfs_ioctl_search_header sh;
1723 unsigned long item_off;
1724 unsigned long item_len;
1730 leaf = path->nodes[0];
1731 slot = path->slots[0];
1732 nritems = btrfs_header_nritems(leaf);
1734 if (btrfs_header_generation(leaf) > sk->max_transid) {
1738 found_transid = btrfs_header_generation(leaf);
1740 for (i = slot; i < nritems; i++) {
1741 item_off = btrfs_item_ptr_offset(leaf, i);
1742 item_len = btrfs_item_size_nr(leaf, i);
1744 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1747 if (sizeof(sh) + item_len + *sk_offset >
1748 BTRFS_SEARCH_ARGS_BUFSIZE) {
1753 btrfs_item_key_to_cpu(leaf, key, i);
1754 if (!key_in_sk(key, sk))
1757 sh.objectid = key->objectid;
1758 sh.offset = key->offset;
1759 sh.type = key->type;
1761 sh.transid = found_transid;
1763 /* copy search result header */
1764 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1765 *sk_offset += sizeof(sh);
1768 char *p = buf + *sk_offset;
1770 read_extent_buffer(leaf, p,
1771 item_off, item_len);
1772 *sk_offset += item_len;
1776 if (*num_found >= sk->nr_items)
1781 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1783 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1786 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1796 static noinline int search_ioctl(struct inode *inode,
1797 struct btrfs_ioctl_search_args *args)
1799 struct btrfs_root *root;
1800 struct btrfs_key key;
1801 struct btrfs_key max_key;
1802 struct btrfs_path *path;
1803 struct btrfs_ioctl_search_key *sk = &args->key;
1804 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1807 unsigned long sk_offset = 0;
1809 path = btrfs_alloc_path();
1813 if (sk->tree_id == 0) {
1814 /* search the root of the inode that was passed */
1815 root = BTRFS_I(inode)->root;
1817 key.objectid = sk->tree_id;
1818 key.type = BTRFS_ROOT_ITEM_KEY;
1819 key.offset = (u64)-1;
1820 root = btrfs_read_fs_root_no_name(info, &key);
1822 printk(KERN_ERR "could not find root %llu\n",
1824 btrfs_free_path(path);
1829 key.objectid = sk->min_objectid;
1830 key.type = sk->min_type;
1831 key.offset = sk->min_offset;
1833 max_key.objectid = sk->max_objectid;
1834 max_key.type = sk->max_type;
1835 max_key.offset = sk->max_offset;
1837 path->keep_locks = 1;
1840 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1847 ret = copy_to_sk(root, path, &key, sk, args->buf,
1848 &sk_offset, &num_found);
1849 btrfs_release_path(path);
1850 if (ret || num_found >= sk->nr_items)
1856 sk->nr_items = num_found;
1857 btrfs_free_path(path);
1861 static noinline int btrfs_ioctl_tree_search(struct file *file,
1864 struct btrfs_ioctl_search_args *args;
1865 struct inode *inode;
1868 if (!capable(CAP_SYS_ADMIN))
1871 args = memdup_user(argp, sizeof(*args));
1873 return PTR_ERR(args);
1875 inode = fdentry(file)->d_inode;
1876 ret = search_ioctl(inode, args);
1877 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1884 * Search INODE_REFs to identify path name of 'dirid' directory
1885 * in a 'tree_id' tree. and sets path name to 'name'.
1887 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1888 u64 tree_id, u64 dirid, char *name)
1890 struct btrfs_root *root;
1891 struct btrfs_key key;
1897 struct btrfs_inode_ref *iref;
1898 struct extent_buffer *l;
1899 struct btrfs_path *path;
1901 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1906 path = btrfs_alloc_path();
1910 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1912 key.objectid = tree_id;
1913 key.type = BTRFS_ROOT_ITEM_KEY;
1914 key.offset = (u64)-1;
1915 root = btrfs_read_fs_root_no_name(info, &key);
1917 printk(KERN_ERR "could not find root %llu\n", tree_id);
1922 key.objectid = dirid;
1923 key.type = BTRFS_INODE_REF_KEY;
1924 key.offset = (u64)-1;
1927 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1932 slot = path->slots[0];
1933 if (ret > 0 && slot > 0)
1935 btrfs_item_key_to_cpu(l, &key, slot);
1937 if (ret > 0 && (key.objectid != dirid ||
1938 key.type != BTRFS_INODE_REF_KEY)) {
1943 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1944 len = btrfs_inode_ref_name_len(l, iref);
1946 total_len += len + 1;
1951 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1953 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1956 btrfs_release_path(path);
1957 key.objectid = key.offset;
1958 key.offset = (u64)-1;
1959 dirid = key.objectid;
1963 memmove(name, ptr, total_len);
1964 name[total_len]='\0';
1967 btrfs_free_path(path);
1971 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1974 struct btrfs_ioctl_ino_lookup_args *args;
1975 struct inode *inode;
1978 if (!capable(CAP_SYS_ADMIN))
1981 args = memdup_user(argp, sizeof(*args));
1983 return PTR_ERR(args);
1985 inode = fdentry(file)->d_inode;
1987 if (args->treeid == 0)
1988 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1990 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1991 args->treeid, args->objectid,
1994 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2001 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2004 struct dentry *parent = fdentry(file);
2005 struct dentry *dentry;
2006 struct inode *dir = parent->d_inode;
2007 struct inode *inode;
2008 struct btrfs_root *root = BTRFS_I(dir)->root;
2009 struct btrfs_root *dest = NULL;
2010 struct btrfs_ioctl_vol_args *vol_args;
2011 struct btrfs_trans_handle *trans;
2016 vol_args = memdup_user(arg, sizeof(*vol_args));
2017 if (IS_ERR(vol_args))
2018 return PTR_ERR(vol_args);
2020 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2021 namelen = strlen(vol_args->name);
2022 if (strchr(vol_args->name, '/') ||
2023 strncmp(vol_args->name, "..", namelen) == 0) {
2028 err = mnt_want_write_file(file);
2032 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2033 dentry = lookup_one_len(vol_args->name, parent, namelen);
2034 if (IS_ERR(dentry)) {
2035 err = PTR_ERR(dentry);
2036 goto out_unlock_dir;
2039 if (!dentry->d_inode) {
2044 inode = dentry->d_inode;
2045 dest = BTRFS_I(inode)->root;
2046 if (!capable(CAP_SYS_ADMIN)){
2048 * Regular user. Only allow this with a special mount
2049 * option, when the user has write+exec access to the
2050 * subvol root, and when rmdir(2) would have been
2053 * Note that this is _not_ check that the subvol is
2054 * empty or doesn't contain data that we wouldn't
2055 * otherwise be able to delete.
2057 * Users who want to delete empty subvols should try
2061 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2065 * Do not allow deletion if the parent dir is the same
2066 * as the dir to be deleted. That means the ioctl
2067 * must be called on the dentry referencing the root
2068 * of the subvol, not a random directory contained
2075 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2079 /* check if subvolume may be deleted by a non-root user */
2080 err = btrfs_may_delete(dir, dentry, 1);
2085 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2090 mutex_lock(&inode->i_mutex);
2091 err = d_invalidate(dentry);
2095 down_write(&root->fs_info->subvol_sem);
2097 err = may_destroy_subvol(dest);
2101 trans = btrfs_start_transaction(root, 0);
2102 if (IS_ERR(trans)) {
2103 err = PTR_ERR(trans);
2106 trans->block_rsv = &root->fs_info->global_block_rsv;
2108 ret = btrfs_unlink_subvol(trans, root, dir,
2109 dest->root_key.objectid,
2110 dentry->d_name.name,
2111 dentry->d_name.len);
2114 btrfs_abort_transaction(trans, root, ret);
2118 btrfs_record_root_in_trans(trans, dest);
2120 memset(&dest->root_item.drop_progress, 0,
2121 sizeof(dest->root_item.drop_progress));
2122 dest->root_item.drop_level = 0;
2123 btrfs_set_root_refs(&dest->root_item, 0);
2125 if (!xchg(&dest->orphan_item_inserted, 1)) {
2126 ret = btrfs_insert_orphan_item(trans,
2127 root->fs_info->tree_root,
2128 dest->root_key.objectid);
2130 btrfs_abort_transaction(trans, root, ret);
2136 ret = btrfs_end_transaction(trans, root);
2139 inode->i_flags |= S_DEAD;
2141 up_write(&root->fs_info->subvol_sem);
2143 mutex_unlock(&inode->i_mutex);
2145 shrink_dcache_sb(root->fs_info->sb);
2146 btrfs_invalidate_inodes(dest);
2152 mutex_unlock(&dir->i_mutex);
2153 mnt_drop_write_file(file);
2159 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2161 struct inode *inode = fdentry(file)->d_inode;
2162 struct btrfs_root *root = BTRFS_I(inode)->root;
2163 struct btrfs_ioctl_defrag_range_args *range;
2166 if (btrfs_root_readonly(root))
2169 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2171 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2172 return -EINPROGRESS;
2174 ret = mnt_want_write_file(file);
2176 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
2181 switch (inode->i_mode & S_IFMT) {
2183 if (!capable(CAP_SYS_ADMIN)) {
2187 ret = btrfs_defrag_root(root, 0);
2190 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2193 if (!(file->f_mode & FMODE_WRITE)) {
2198 range = kzalloc(sizeof(*range), GFP_KERNEL);
2205 if (copy_from_user(range, argp,
2211 /* compression requires us to start the IO */
2212 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2213 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2214 range->extent_thresh = (u32)-1;
2217 /* the rest are all set to zero by kzalloc */
2218 range->len = (u64)-1;
2220 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2230 mnt_drop_write_file(file);
2231 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2235 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2237 struct btrfs_ioctl_vol_args *vol_args;
2240 if (!capable(CAP_SYS_ADMIN))
2243 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2245 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2246 return -EINPROGRESS;
2249 mutex_lock(&root->fs_info->volume_mutex);
2250 vol_args = memdup_user(arg, sizeof(*vol_args));
2251 if (IS_ERR(vol_args)) {
2252 ret = PTR_ERR(vol_args);
2256 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2257 ret = btrfs_init_new_device(root, vol_args->name);
2261 mutex_unlock(&root->fs_info->volume_mutex);
2262 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2266 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2268 struct btrfs_ioctl_vol_args *vol_args;
2271 if (!capable(CAP_SYS_ADMIN))
2274 if (root->fs_info->sb->s_flags & MS_RDONLY)
2277 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2279 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2280 return -EINPROGRESS;
2283 mutex_lock(&root->fs_info->volume_mutex);
2284 vol_args = memdup_user(arg, sizeof(*vol_args));
2285 if (IS_ERR(vol_args)) {
2286 ret = PTR_ERR(vol_args);
2290 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2291 ret = btrfs_rm_device(root, vol_args->name);
2295 mutex_unlock(&root->fs_info->volume_mutex);
2296 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2300 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2302 struct btrfs_ioctl_fs_info_args *fi_args;
2303 struct btrfs_device *device;
2304 struct btrfs_device *next;
2305 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2308 if (!capable(CAP_SYS_ADMIN))
2311 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2315 fi_args->num_devices = fs_devices->num_devices;
2316 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2318 mutex_lock(&fs_devices->device_list_mutex);
2319 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2320 if (device->devid > fi_args->max_id)
2321 fi_args->max_id = device->devid;
2323 mutex_unlock(&fs_devices->device_list_mutex);
2325 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2332 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2334 struct btrfs_ioctl_dev_info_args *di_args;
2335 struct btrfs_device *dev;
2336 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2338 char *s_uuid = NULL;
2339 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2341 if (!capable(CAP_SYS_ADMIN))
2344 di_args = memdup_user(arg, sizeof(*di_args));
2345 if (IS_ERR(di_args))
2346 return PTR_ERR(di_args);
2348 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2349 s_uuid = di_args->uuid;
2351 mutex_lock(&fs_devices->device_list_mutex);
2352 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2353 mutex_unlock(&fs_devices->device_list_mutex);
2360 di_args->devid = dev->devid;
2361 di_args->bytes_used = dev->bytes_used;
2362 di_args->total_bytes = dev->total_bytes;
2363 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2365 struct rcu_string *name;
2368 name = rcu_dereference(dev->name);
2369 strncpy(di_args->path, name->str, sizeof(di_args->path));
2371 di_args->path[sizeof(di_args->path) - 1] = 0;
2373 di_args->path[0] = '\0';
2377 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2384 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2385 u64 off, u64 olen, u64 destoff)
2387 struct inode *inode = fdentry(file)->d_inode;
2388 struct btrfs_root *root = BTRFS_I(inode)->root;
2391 struct btrfs_trans_handle *trans;
2392 struct btrfs_path *path;
2393 struct extent_buffer *leaf;
2395 struct btrfs_key key;
2400 u64 bs = root->fs_info->sb->s_blocksize;
2404 * - split compressed inline extents. annoying: we need to
2405 * decompress into destination's address_space (the file offset
2406 * may change, so source mapping won't do), then recompress (or
2407 * otherwise reinsert) a subrange.
2408 * - allow ranges within the same file to be cloned (provided
2409 * they don't overlap)?
2412 /* the destination must be opened for writing */
2413 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2416 if (btrfs_root_readonly(root))
2419 ret = mnt_want_write_file(file);
2423 src_file = fdget(srcfd);
2424 if (!src_file.file) {
2426 goto out_drop_write;
2430 if (src_file.file->f_path.mnt != file->f_path.mnt)
2433 src = src_file.file->f_dentry->d_inode;
2439 /* the src must be open for reading */
2440 if (!(src_file.file->f_mode & FMODE_READ))
2443 /* don't make the dst file partly checksummed */
2444 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2445 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2449 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2453 if (src->i_sb != inode->i_sb)
2457 buf = vmalloc(btrfs_level_size(root, 0));
2461 path = btrfs_alloc_path();
2469 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2470 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2472 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2473 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2476 /* determine range to clone */
2478 if (off + len > src->i_size || off + len < off)
2481 olen = len = src->i_size - off;
2482 /* if we extend to eof, continue to block boundary */
2483 if (off + len == src->i_size)
2484 len = ALIGN(src->i_size, bs) - off;
2486 /* verify the end result is block aligned */
2487 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2488 !IS_ALIGNED(destoff, bs))
2491 if (destoff > inode->i_size) {
2492 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2497 /* truncate page cache pages from target inode range */
2498 truncate_inode_pages_range(&inode->i_data, destoff,
2499 PAGE_CACHE_ALIGN(destoff + len) - 1);
2501 /* do any pending delalloc/csum calc on src, one way or
2502 another, and lock file content */
2504 struct btrfs_ordered_extent *ordered;
2505 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2506 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
2508 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2509 EXTENT_DELALLOC, 0, NULL))
2511 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2513 btrfs_put_ordered_extent(ordered);
2514 btrfs_wait_ordered_range(src, off, len);
2518 key.objectid = btrfs_ino(src);
2519 key.type = BTRFS_EXTENT_DATA_KEY;
2524 * note the key will change type as we walk through the
2527 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2532 nritems = btrfs_header_nritems(path->nodes[0]);
2533 if (path->slots[0] >= nritems) {
2534 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2539 nritems = btrfs_header_nritems(path->nodes[0]);
2541 leaf = path->nodes[0];
2542 slot = path->slots[0];
2544 btrfs_item_key_to_cpu(leaf, &key, slot);
2545 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2546 key.objectid != btrfs_ino(src))
2549 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2550 struct btrfs_file_extent_item *extent;
2553 struct btrfs_key new_key;
2554 u64 disko = 0, diskl = 0;
2555 u64 datao = 0, datal = 0;
2559 size = btrfs_item_size_nr(leaf, slot);
2560 read_extent_buffer(leaf, buf,
2561 btrfs_item_ptr_offset(leaf, slot),
2564 extent = btrfs_item_ptr(leaf, slot,
2565 struct btrfs_file_extent_item);
2566 comp = btrfs_file_extent_compression(leaf, extent);
2567 type = btrfs_file_extent_type(leaf, extent);
2568 if (type == BTRFS_FILE_EXTENT_REG ||
2569 type == BTRFS_FILE_EXTENT_PREALLOC) {
2570 disko = btrfs_file_extent_disk_bytenr(leaf,
2572 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2574 datao = btrfs_file_extent_offset(leaf, extent);
2575 datal = btrfs_file_extent_num_bytes(leaf,
2577 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2578 /* take upper bound, may be compressed */
2579 datal = btrfs_file_extent_ram_bytes(leaf,
2582 btrfs_release_path(path);
2584 if (key.offset + datal <= off ||
2585 key.offset >= off + len - 1)
2588 memcpy(&new_key, &key, sizeof(new_key));
2589 new_key.objectid = btrfs_ino(inode);
2590 if (off <= key.offset)
2591 new_key.offset = key.offset + destoff - off;
2593 new_key.offset = destoff;
2596 * 1 - adjusting old extent (we may have to split it)
2597 * 1 - add new extent
2600 trans = btrfs_start_transaction(root, 3);
2601 if (IS_ERR(trans)) {
2602 ret = PTR_ERR(trans);
2606 if (type == BTRFS_FILE_EXTENT_REG ||
2607 type == BTRFS_FILE_EXTENT_PREALLOC) {
2609 * a | --- range to clone ---| b
2610 * | ------------- extent ------------- |
2613 /* substract range b */
2614 if (key.offset + datal > off + len)
2615 datal = off + len - key.offset;
2617 /* substract range a */
2618 if (off > key.offset) {
2619 datao += off - key.offset;
2620 datal -= off - key.offset;
2623 ret = btrfs_drop_extents(trans, root, inode,
2625 new_key.offset + datal,
2628 btrfs_abort_transaction(trans, root,
2630 btrfs_end_transaction(trans, root);
2634 ret = btrfs_insert_empty_item(trans, root, path,
2637 btrfs_abort_transaction(trans, root,
2639 btrfs_end_transaction(trans, root);
2643 leaf = path->nodes[0];
2644 slot = path->slots[0];
2645 write_extent_buffer(leaf, buf,
2646 btrfs_item_ptr_offset(leaf, slot),
2649 extent = btrfs_item_ptr(leaf, slot,
2650 struct btrfs_file_extent_item);
2652 /* disko == 0 means it's a hole */
2656 btrfs_set_file_extent_offset(leaf, extent,
2658 btrfs_set_file_extent_num_bytes(leaf, extent,
2661 inode_add_bytes(inode, datal);
2662 ret = btrfs_inc_extent_ref(trans, root,
2664 root->root_key.objectid,
2666 new_key.offset - datao,
2669 btrfs_abort_transaction(trans,
2672 btrfs_end_transaction(trans,
2678 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2681 if (off > key.offset) {
2682 skip = off - key.offset;
2683 new_key.offset += skip;
2686 if (key.offset + datal > off + len)
2687 trim = key.offset + datal - (off + len);
2689 if (comp && (skip || trim)) {
2691 btrfs_end_transaction(trans, root);
2694 size -= skip + trim;
2695 datal -= skip + trim;
2697 ret = btrfs_drop_extents(trans, root, inode,
2699 new_key.offset + datal,
2702 btrfs_abort_transaction(trans, root,
2704 btrfs_end_transaction(trans, root);
2708 ret = btrfs_insert_empty_item(trans, root, path,
2711 btrfs_abort_transaction(trans, root,
2713 btrfs_end_transaction(trans, root);
2719 btrfs_file_extent_calc_inline_size(0);
2720 memmove(buf+start, buf+start+skip,
2724 leaf = path->nodes[0];
2725 slot = path->slots[0];
2726 write_extent_buffer(leaf, buf,
2727 btrfs_item_ptr_offset(leaf, slot),
2729 inode_add_bytes(inode, datal);
2732 btrfs_mark_buffer_dirty(leaf);
2733 btrfs_release_path(path);
2735 inode_inc_iversion(inode);
2736 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2739 * we round up to the block size at eof when
2740 * determining which extents to clone above,
2741 * but shouldn't round up the file size
2743 endoff = new_key.offset + datal;
2744 if (endoff > destoff+olen)
2745 endoff = destoff+olen;
2746 if (endoff > inode->i_size)
2747 btrfs_i_size_write(inode, endoff);
2749 ret = btrfs_update_inode(trans, root, inode);
2751 btrfs_abort_transaction(trans, root, ret);
2752 btrfs_end_transaction(trans, root);
2755 ret = btrfs_end_transaction(trans, root);
2758 btrfs_release_path(path);
2763 btrfs_release_path(path);
2764 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2766 mutex_unlock(&src->i_mutex);
2767 mutex_unlock(&inode->i_mutex);
2769 btrfs_free_path(path);
2773 mnt_drop_write_file(file);
2777 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2779 struct btrfs_ioctl_clone_range_args args;
2781 if (copy_from_user(&args, argp, sizeof(args)))
2783 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2784 args.src_length, args.dest_offset);
2788 * there are many ways the trans_start and trans_end ioctls can lead
2789 * to deadlocks. They should only be used by applications that
2790 * basically own the machine, and have a very in depth understanding
2791 * of all the possible deadlocks and enospc problems.
2793 static long btrfs_ioctl_trans_start(struct file *file)
2795 struct inode *inode = fdentry(file)->d_inode;
2796 struct btrfs_root *root = BTRFS_I(inode)->root;
2797 struct btrfs_trans_handle *trans;
2801 if (!capable(CAP_SYS_ADMIN))
2805 if (file->private_data)
2809 if (btrfs_root_readonly(root))
2812 ret = mnt_want_write_file(file);
2816 atomic_inc(&root->fs_info->open_ioctl_trans);
2819 trans = btrfs_start_ioctl_transaction(root);
2823 file->private_data = trans;
2827 atomic_dec(&root->fs_info->open_ioctl_trans);
2828 mnt_drop_write_file(file);
2833 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2835 struct inode *inode = fdentry(file)->d_inode;
2836 struct btrfs_root *root = BTRFS_I(inode)->root;
2837 struct btrfs_root *new_root;
2838 struct btrfs_dir_item *di;
2839 struct btrfs_trans_handle *trans;
2840 struct btrfs_path *path;
2841 struct btrfs_key location;
2842 struct btrfs_disk_key disk_key;
2846 if (!capable(CAP_SYS_ADMIN))
2849 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2853 objectid = root->root_key.objectid;
2855 location.objectid = objectid;
2856 location.type = BTRFS_ROOT_ITEM_KEY;
2857 location.offset = (u64)-1;
2859 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2860 if (IS_ERR(new_root))
2861 return PTR_ERR(new_root);
2863 if (btrfs_root_refs(&new_root->root_item) == 0)
2866 path = btrfs_alloc_path();
2869 path->leave_spinning = 1;
2871 trans = btrfs_start_transaction(root, 1);
2872 if (IS_ERR(trans)) {
2873 btrfs_free_path(path);
2874 return PTR_ERR(trans);
2877 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
2878 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2879 dir_id, "default", 7, 1);
2880 if (IS_ERR_OR_NULL(di)) {
2881 btrfs_free_path(path);
2882 btrfs_end_transaction(trans, root);
2883 printk(KERN_ERR "Umm, you don't have the default dir item, "
2884 "this isn't going to work\n");
2888 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2889 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2890 btrfs_mark_buffer_dirty(path->nodes[0]);
2891 btrfs_free_path(path);
2893 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
2894 btrfs_end_transaction(trans, root);
2899 void btrfs_get_block_group_info(struct list_head *groups_list,
2900 struct btrfs_ioctl_space_info *space)
2902 struct btrfs_block_group_cache *block_group;
2904 space->total_bytes = 0;
2905 space->used_bytes = 0;
2907 list_for_each_entry(block_group, groups_list, list) {
2908 space->flags = block_group->flags;
2909 space->total_bytes += block_group->key.offset;
2910 space->used_bytes +=
2911 btrfs_block_group_used(&block_group->item);
2915 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2917 struct btrfs_ioctl_space_args space_args;
2918 struct btrfs_ioctl_space_info space;
2919 struct btrfs_ioctl_space_info *dest;
2920 struct btrfs_ioctl_space_info *dest_orig;
2921 struct btrfs_ioctl_space_info __user *user_dest;
2922 struct btrfs_space_info *info;
2923 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2924 BTRFS_BLOCK_GROUP_SYSTEM,
2925 BTRFS_BLOCK_GROUP_METADATA,
2926 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2933 if (copy_from_user(&space_args,
2934 (struct btrfs_ioctl_space_args __user *)arg,
2935 sizeof(space_args)))
2938 for (i = 0; i < num_types; i++) {
2939 struct btrfs_space_info *tmp;
2943 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2945 if (tmp->flags == types[i]) {
2955 down_read(&info->groups_sem);
2956 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2957 if (!list_empty(&info->block_groups[c]))
2960 up_read(&info->groups_sem);
2963 /* space_slots == 0 means they are asking for a count */
2964 if (space_args.space_slots == 0) {
2965 space_args.total_spaces = slot_count;
2969 slot_count = min_t(u64, space_args.space_slots, slot_count);
2971 alloc_size = sizeof(*dest) * slot_count;
2973 /* we generally have at most 6 or so space infos, one for each raid
2974 * level. So, a whole page should be more than enough for everyone
2976 if (alloc_size > PAGE_CACHE_SIZE)
2979 space_args.total_spaces = 0;
2980 dest = kmalloc(alloc_size, GFP_NOFS);
2985 /* now we have a buffer to copy into */
2986 for (i = 0; i < num_types; i++) {
2987 struct btrfs_space_info *tmp;
2994 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2996 if (tmp->flags == types[i]) {
3005 down_read(&info->groups_sem);
3006 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3007 if (!list_empty(&info->block_groups[c])) {
3008 btrfs_get_block_group_info(
3009 &info->block_groups[c], &space);
3010 memcpy(dest, &space, sizeof(space));
3012 space_args.total_spaces++;
3018 up_read(&info->groups_sem);
3021 user_dest = (struct btrfs_ioctl_space_info __user *)
3022 (arg + sizeof(struct btrfs_ioctl_space_args));
3024 if (copy_to_user(user_dest, dest_orig, alloc_size))
3029 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3036 * there are many ways the trans_start and trans_end ioctls can lead
3037 * to deadlocks. They should only be used by applications that
3038 * basically own the machine, and have a very in depth understanding
3039 * of all the possible deadlocks and enospc problems.
3041 long btrfs_ioctl_trans_end(struct file *file)
3043 struct inode *inode = fdentry(file)->d_inode;
3044 struct btrfs_root *root = BTRFS_I(inode)->root;
3045 struct btrfs_trans_handle *trans;
3047 trans = file->private_data;
3050 file->private_data = NULL;
3052 btrfs_end_transaction(trans, root);
3054 atomic_dec(&root->fs_info->open_ioctl_trans);
3056 mnt_drop_write_file(file);
3060 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
3062 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3063 struct btrfs_trans_handle *trans;
3067 trans = btrfs_start_transaction(root, 0);
3069 return PTR_ERR(trans);
3070 transid = trans->transid;
3071 ret = btrfs_commit_transaction_async(trans, root, 0);
3073 btrfs_end_transaction(trans, root);
3078 if (copy_to_user(argp, &transid, sizeof(transid)))
3083 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
3085 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3089 if (copy_from_user(&transid, argp, sizeof(transid)))
3092 transid = 0; /* current trans */
3094 return btrfs_wait_for_commit(root, transid);
3097 static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3100 struct btrfs_ioctl_scrub_args *sa;
3102 if (!capable(CAP_SYS_ADMIN))
3105 sa = memdup_user(arg, sizeof(*sa));
3109 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
3110 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3113 if (copy_to_user(arg, sa, sizeof(*sa)))
3120 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3122 if (!capable(CAP_SYS_ADMIN))
3125 return btrfs_scrub_cancel(root->fs_info);
3128 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3131 struct btrfs_ioctl_scrub_args *sa;
3134 if (!capable(CAP_SYS_ADMIN))
3137 sa = memdup_user(arg, sizeof(*sa));
3141 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3143 if (copy_to_user(arg, sa, sizeof(*sa)))
3150 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3153 struct btrfs_ioctl_get_dev_stats *sa;
3156 sa = memdup_user(arg, sizeof(*sa));
3160 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3165 ret = btrfs_get_dev_stats(root, sa);
3167 if (copy_to_user(arg, sa, sizeof(*sa)))
3174 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3180 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3181 struct inode_fs_paths *ipath = NULL;
3182 struct btrfs_path *path;
3184 if (!capable(CAP_SYS_ADMIN))
3187 path = btrfs_alloc_path();
3193 ipa = memdup_user(arg, sizeof(*ipa));
3200 size = min_t(u32, ipa->size, 4096);
3201 ipath = init_ipath(size, root, path);
3202 if (IS_ERR(ipath)) {
3203 ret = PTR_ERR(ipath);
3208 ret = paths_from_inode(ipa->inum, ipath);
3212 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3213 rel_ptr = ipath->fspath->val[i] -
3214 (u64)(unsigned long)ipath->fspath->val;
3215 ipath->fspath->val[i] = rel_ptr;
3218 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3219 (void *)(unsigned long)ipath->fspath, size);
3226 btrfs_free_path(path);
3233 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3235 struct btrfs_data_container *inodes = ctx;
3236 const size_t c = 3 * sizeof(u64);
3238 if (inodes->bytes_left >= c) {
3239 inodes->bytes_left -= c;
3240 inodes->val[inodes->elem_cnt] = inum;
3241 inodes->val[inodes->elem_cnt + 1] = offset;
3242 inodes->val[inodes->elem_cnt + 2] = root;
3243 inodes->elem_cnt += 3;
3245 inodes->bytes_missing += c - inodes->bytes_left;
3246 inodes->bytes_left = 0;
3247 inodes->elem_missed += 3;
3253 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3258 struct btrfs_ioctl_logical_ino_args *loi;
3259 struct btrfs_data_container *inodes = NULL;
3260 struct btrfs_path *path = NULL;
3262 if (!capable(CAP_SYS_ADMIN))
3265 loi = memdup_user(arg, sizeof(*loi));
3272 path = btrfs_alloc_path();
3278 size = min_t(u32, loi->size, 64 * 1024);
3279 inodes = init_data_container(size);
3280 if (IS_ERR(inodes)) {
3281 ret = PTR_ERR(inodes);
3286 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3287 build_ino_list, inodes);
3293 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3294 (void *)(unsigned long)inodes, size);
3299 btrfs_free_path(path);
3306 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3307 struct btrfs_ioctl_balance_args *bargs)
3309 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3311 bargs->flags = bctl->flags;
3313 if (atomic_read(&fs_info->balance_running))
3314 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3315 if (atomic_read(&fs_info->balance_pause_req))
3316 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3317 if (atomic_read(&fs_info->balance_cancel_req))
3318 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3320 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3321 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3322 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3325 spin_lock(&fs_info->balance_lock);
3326 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3327 spin_unlock(&fs_info->balance_lock);
3329 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3333 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3335 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3336 struct btrfs_fs_info *fs_info = root->fs_info;
3337 struct btrfs_ioctl_balance_args *bargs;
3338 struct btrfs_balance_control *bctl;
3340 int need_to_clear_lock = 0;
3342 if (!capable(CAP_SYS_ADMIN))
3345 ret = mnt_want_write_file(file);
3349 mutex_lock(&fs_info->volume_mutex);
3350 mutex_lock(&fs_info->balance_mutex);
3353 bargs = memdup_user(arg, sizeof(*bargs));
3354 if (IS_ERR(bargs)) {
3355 ret = PTR_ERR(bargs);
3359 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3360 if (!fs_info->balance_ctl) {
3365 bctl = fs_info->balance_ctl;
3366 spin_lock(&fs_info->balance_lock);
3367 bctl->flags |= BTRFS_BALANCE_RESUME;
3368 spin_unlock(&fs_info->balance_lock);
3376 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
3378 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3382 need_to_clear_lock = 1;
3384 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3390 bctl->fs_info = fs_info;
3392 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3393 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3394 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3396 bctl->flags = bargs->flags;
3398 /* balance everything - no filters */
3399 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3403 ret = btrfs_balance(bctl, bargs);
3405 * bctl is freed in __cancel_balance or in free_fs_info if
3406 * restriper was paused all the way until unmount
3409 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3416 if (need_to_clear_lock)
3417 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
3419 mutex_unlock(&fs_info->balance_mutex);
3420 mutex_unlock(&fs_info->volume_mutex);
3421 mnt_drop_write_file(file);
3425 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3427 if (!capable(CAP_SYS_ADMIN))
3431 case BTRFS_BALANCE_CTL_PAUSE:
3432 return btrfs_pause_balance(root->fs_info);
3433 case BTRFS_BALANCE_CTL_CANCEL:
3434 return btrfs_cancel_balance(root->fs_info);
3440 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3443 struct btrfs_fs_info *fs_info = root->fs_info;
3444 struct btrfs_ioctl_balance_args *bargs;
3447 if (!capable(CAP_SYS_ADMIN))
3450 mutex_lock(&fs_info->balance_mutex);
3451 if (!fs_info->balance_ctl) {
3456 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3462 update_ioctl_balance_args(fs_info, 1, bargs);
3464 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3469 mutex_unlock(&fs_info->balance_mutex);
3473 static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3475 struct btrfs_ioctl_quota_ctl_args *sa;
3476 struct btrfs_trans_handle *trans = NULL;
3480 if (!capable(CAP_SYS_ADMIN))
3483 if (root->fs_info->sb->s_flags & MS_RDONLY)
3486 sa = memdup_user(arg, sizeof(*sa));
3490 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3491 trans = btrfs_start_transaction(root, 2);
3492 if (IS_ERR(trans)) {
3493 ret = PTR_ERR(trans);
3499 case BTRFS_QUOTA_CTL_ENABLE:
3500 ret = btrfs_quota_enable(trans, root->fs_info);
3502 case BTRFS_QUOTA_CTL_DISABLE:
3503 ret = btrfs_quota_disable(trans, root->fs_info);
3505 case BTRFS_QUOTA_CTL_RESCAN:
3506 ret = btrfs_quota_rescan(root->fs_info);
3513 if (copy_to_user(arg, sa, sizeof(*sa)))
3517 err = btrfs_commit_transaction(trans, root);
3527 static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3529 struct btrfs_ioctl_qgroup_assign_args *sa;
3530 struct btrfs_trans_handle *trans;
3534 if (!capable(CAP_SYS_ADMIN))
3537 if (root->fs_info->sb->s_flags & MS_RDONLY)
3540 sa = memdup_user(arg, sizeof(*sa));
3544 trans = btrfs_join_transaction(root);
3545 if (IS_ERR(trans)) {
3546 ret = PTR_ERR(trans);
3550 /* FIXME: check if the IDs really exist */
3552 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3555 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3559 err = btrfs_end_transaction(trans, root);
3568 static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3570 struct btrfs_ioctl_qgroup_create_args *sa;
3571 struct btrfs_trans_handle *trans;
3575 if (!capable(CAP_SYS_ADMIN))
3578 if (root->fs_info->sb->s_flags & MS_RDONLY)
3581 sa = memdup_user(arg, sizeof(*sa));
3585 trans = btrfs_join_transaction(root);
3586 if (IS_ERR(trans)) {
3587 ret = PTR_ERR(trans);
3591 /* FIXME: check if the IDs really exist */
3593 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3596 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3599 err = btrfs_end_transaction(trans, root);
3608 static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3610 struct btrfs_ioctl_qgroup_limit_args *sa;
3611 struct btrfs_trans_handle *trans;
3616 if (!capable(CAP_SYS_ADMIN))
3619 if (root->fs_info->sb->s_flags & MS_RDONLY)
3622 sa = memdup_user(arg, sizeof(*sa));
3626 trans = btrfs_join_transaction(root);
3627 if (IS_ERR(trans)) {
3628 ret = PTR_ERR(trans);
3632 qgroupid = sa->qgroupid;
3634 /* take the current subvol as qgroup */
3635 qgroupid = root->root_key.objectid;
3638 /* FIXME: check if the IDs really exist */
3639 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3641 err = btrfs_end_transaction(trans, root);
3650 static long btrfs_ioctl_set_received_subvol(struct file *file,
3653 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3654 struct inode *inode = fdentry(file)->d_inode;
3655 struct btrfs_root *root = BTRFS_I(inode)->root;
3656 struct btrfs_root_item *root_item = &root->root_item;
3657 struct btrfs_trans_handle *trans;
3658 struct timespec ct = CURRENT_TIME;
3661 ret = mnt_want_write_file(file);
3665 down_write(&root->fs_info->subvol_sem);
3667 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3672 if (btrfs_root_readonly(root)) {
3677 if (!inode_owner_or_capable(inode)) {
3682 sa = memdup_user(arg, sizeof(*sa));
3689 trans = btrfs_start_transaction(root, 1);
3690 if (IS_ERR(trans)) {
3691 ret = PTR_ERR(trans);
3696 sa->rtransid = trans->transid;
3697 sa->rtime.sec = ct.tv_sec;
3698 sa->rtime.nsec = ct.tv_nsec;
3700 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3701 btrfs_set_root_stransid(root_item, sa->stransid);
3702 btrfs_set_root_rtransid(root_item, sa->rtransid);
3703 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3704 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3705 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3706 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3708 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3709 &root->root_key, &root->root_item);
3711 btrfs_end_transaction(trans, root);
3715 ret = btrfs_commit_transaction(trans, root);
3720 ret = copy_to_user(arg, sa, sizeof(*sa));
3726 up_write(&root->fs_info->subvol_sem);
3727 mnt_drop_write_file(file);
3731 long btrfs_ioctl(struct file *file, unsigned int
3732 cmd, unsigned long arg)
3734 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3735 void __user *argp = (void __user *)arg;
3738 case FS_IOC_GETFLAGS:
3739 return btrfs_ioctl_getflags(file, argp);
3740 case FS_IOC_SETFLAGS:
3741 return btrfs_ioctl_setflags(file, argp);
3742 case FS_IOC_GETVERSION:
3743 return btrfs_ioctl_getversion(file, argp);
3745 return btrfs_ioctl_fitrim(file, argp);
3746 case BTRFS_IOC_SNAP_CREATE:
3747 return btrfs_ioctl_snap_create(file, argp, 0);
3748 case BTRFS_IOC_SNAP_CREATE_V2:
3749 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3750 case BTRFS_IOC_SUBVOL_CREATE:
3751 return btrfs_ioctl_snap_create(file, argp, 1);
3752 case BTRFS_IOC_SUBVOL_CREATE_V2:
3753 return btrfs_ioctl_snap_create_v2(file, argp, 1);
3754 case BTRFS_IOC_SNAP_DESTROY:
3755 return btrfs_ioctl_snap_destroy(file, argp);
3756 case BTRFS_IOC_SUBVOL_GETFLAGS:
3757 return btrfs_ioctl_subvol_getflags(file, argp);
3758 case BTRFS_IOC_SUBVOL_SETFLAGS:
3759 return btrfs_ioctl_subvol_setflags(file, argp);
3760 case BTRFS_IOC_DEFAULT_SUBVOL:
3761 return btrfs_ioctl_default_subvol(file, argp);
3762 case BTRFS_IOC_DEFRAG:
3763 return btrfs_ioctl_defrag(file, NULL);
3764 case BTRFS_IOC_DEFRAG_RANGE:
3765 return btrfs_ioctl_defrag(file, argp);
3766 case BTRFS_IOC_RESIZE:
3767 return btrfs_ioctl_resize(root, argp);
3768 case BTRFS_IOC_ADD_DEV:
3769 return btrfs_ioctl_add_dev(root, argp);
3770 case BTRFS_IOC_RM_DEV:
3771 return btrfs_ioctl_rm_dev(root, argp);
3772 case BTRFS_IOC_FS_INFO:
3773 return btrfs_ioctl_fs_info(root, argp);
3774 case BTRFS_IOC_DEV_INFO:
3775 return btrfs_ioctl_dev_info(root, argp);
3776 case BTRFS_IOC_BALANCE:
3777 return btrfs_ioctl_balance(file, NULL);
3778 case BTRFS_IOC_CLONE:
3779 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3780 case BTRFS_IOC_CLONE_RANGE:
3781 return btrfs_ioctl_clone_range(file, argp);
3782 case BTRFS_IOC_TRANS_START:
3783 return btrfs_ioctl_trans_start(file);
3784 case BTRFS_IOC_TRANS_END:
3785 return btrfs_ioctl_trans_end(file);
3786 case BTRFS_IOC_TREE_SEARCH:
3787 return btrfs_ioctl_tree_search(file, argp);
3788 case BTRFS_IOC_INO_LOOKUP:
3789 return btrfs_ioctl_ino_lookup(file, argp);
3790 case BTRFS_IOC_INO_PATHS:
3791 return btrfs_ioctl_ino_to_path(root, argp);
3792 case BTRFS_IOC_LOGICAL_INO:
3793 return btrfs_ioctl_logical_to_ino(root, argp);
3794 case BTRFS_IOC_SPACE_INFO:
3795 return btrfs_ioctl_space_info(root, argp);
3796 case BTRFS_IOC_SYNC:
3797 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3799 case BTRFS_IOC_START_SYNC:
3800 return btrfs_ioctl_start_sync(file, argp);
3801 case BTRFS_IOC_WAIT_SYNC:
3802 return btrfs_ioctl_wait_sync(file, argp);
3803 case BTRFS_IOC_SCRUB:
3804 return btrfs_ioctl_scrub(root, argp);
3805 case BTRFS_IOC_SCRUB_CANCEL:
3806 return btrfs_ioctl_scrub_cancel(root, argp);
3807 case BTRFS_IOC_SCRUB_PROGRESS:
3808 return btrfs_ioctl_scrub_progress(root, argp);
3809 case BTRFS_IOC_BALANCE_V2:
3810 return btrfs_ioctl_balance(file, argp);
3811 case BTRFS_IOC_BALANCE_CTL:
3812 return btrfs_ioctl_balance_ctl(root, arg);
3813 case BTRFS_IOC_BALANCE_PROGRESS:
3814 return btrfs_ioctl_balance_progress(root, argp);
3815 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3816 return btrfs_ioctl_set_received_subvol(file, argp);
3817 case BTRFS_IOC_SEND:
3818 return btrfs_ioctl_send(file, argp);
3819 case BTRFS_IOC_GET_DEV_STATS:
3820 return btrfs_ioctl_get_dev_stats(root, argp);
3821 case BTRFS_IOC_QUOTA_CTL:
3822 return btrfs_ioctl_quota_ctl(root, argp);
3823 case BTRFS_IOC_QGROUP_ASSIGN:
3824 return btrfs_ioctl_qgroup_assign(root, argp);
3825 case BTRFS_IOC_QGROUP_CREATE:
3826 return btrfs_ioctl_qgroup_create(root, argp);
3827 case BTRFS_IOC_QGROUP_LIMIT:
3828 return btrfs_ioctl_qgroup_limit(root, argp);