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>
45 #include <linux/btrfs.h>
46 #include <linux/uaccess.h>
49 #include "transaction.h"
50 #include "btrfs_inode.h"
51 #include "print-tree.h"
54 #include "inode-map.h"
56 #include "rcu-string.h"
58 #include "dev-replace.h"
61 static int btrfs_clone(struct inode *src, struct inode *inode,
62 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
64 /* Mask out flags that are inappropriate for the given type of inode. */
65 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
69 else if (S_ISREG(mode))
70 return flags & ~FS_DIRSYNC_FL;
72 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
76 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
78 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
80 unsigned int iflags = 0;
82 if (flags & BTRFS_INODE_SYNC)
84 if (flags & BTRFS_INODE_IMMUTABLE)
85 iflags |= FS_IMMUTABLE_FL;
86 if (flags & BTRFS_INODE_APPEND)
87 iflags |= FS_APPEND_FL;
88 if (flags & BTRFS_INODE_NODUMP)
89 iflags |= FS_NODUMP_FL;
90 if (flags & BTRFS_INODE_NOATIME)
91 iflags |= FS_NOATIME_FL;
92 if (flags & BTRFS_INODE_DIRSYNC)
93 iflags |= FS_DIRSYNC_FL;
94 if (flags & BTRFS_INODE_NODATACOW)
95 iflags |= FS_NOCOW_FL;
97 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98 iflags |= FS_COMPR_FL;
99 else if (flags & BTRFS_INODE_NOCOMPRESS)
100 iflags |= FS_NOCOMP_FL;
106 * Update inode->i_flags based on the btrfs internal flags.
108 void btrfs_update_iflags(struct inode *inode)
110 struct btrfs_inode *ip = BTRFS_I(inode);
112 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
114 if (ip->flags & BTRFS_INODE_SYNC)
115 inode->i_flags |= S_SYNC;
116 if (ip->flags & BTRFS_INODE_IMMUTABLE)
117 inode->i_flags |= S_IMMUTABLE;
118 if (ip->flags & BTRFS_INODE_APPEND)
119 inode->i_flags |= S_APPEND;
120 if (ip->flags & BTRFS_INODE_NOATIME)
121 inode->i_flags |= S_NOATIME;
122 if (ip->flags & BTRFS_INODE_DIRSYNC)
123 inode->i_flags |= S_DIRSYNC;
127 * Inherit flags from the parent inode.
129 * Currently only the compression flags and the cow flags are inherited.
131 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
138 flags = BTRFS_I(dir)->flags;
140 if (flags & BTRFS_INODE_NOCOMPRESS) {
141 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143 } else if (flags & BTRFS_INODE_COMPRESS) {
144 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
148 if (flags & BTRFS_INODE_NODATACOW) {
149 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
150 if (S_ISREG(inode->i_mode))
151 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
154 btrfs_update_iflags(inode);
157 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
159 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
162 if (copy_to_user(arg, &flags, sizeof(flags)))
167 static int check_flags(unsigned int flags)
169 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
170 FS_NOATIME_FL | FS_NODUMP_FL | \
171 FS_SYNC_FL | FS_DIRSYNC_FL | \
172 FS_NOCOMP_FL | FS_COMPR_FL |
176 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
182 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
184 struct inode *inode = file_inode(file);
185 struct btrfs_inode *ip = BTRFS_I(inode);
186 struct btrfs_root *root = ip->root;
187 struct btrfs_trans_handle *trans;
188 unsigned int flags, oldflags;
191 unsigned int i_oldflags;
194 if (btrfs_root_readonly(root))
197 if (copy_from_user(&flags, arg, sizeof(flags)))
200 ret = check_flags(flags);
204 if (!inode_owner_or_capable(inode))
207 ret = mnt_want_write_file(file);
211 mutex_lock(&inode->i_mutex);
213 ip_oldflags = ip->flags;
214 i_oldflags = inode->i_flags;
215 mode = inode->i_mode;
217 flags = btrfs_mask_flags(inode->i_mode, flags);
218 oldflags = btrfs_flags_to_ioctl(ip->flags);
219 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
220 if (!capable(CAP_LINUX_IMMUTABLE)) {
226 if (flags & FS_SYNC_FL)
227 ip->flags |= BTRFS_INODE_SYNC;
229 ip->flags &= ~BTRFS_INODE_SYNC;
230 if (flags & FS_IMMUTABLE_FL)
231 ip->flags |= BTRFS_INODE_IMMUTABLE;
233 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
234 if (flags & FS_APPEND_FL)
235 ip->flags |= BTRFS_INODE_APPEND;
237 ip->flags &= ~BTRFS_INODE_APPEND;
238 if (flags & FS_NODUMP_FL)
239 ip->flags |= BTRFS_INODE_NODUMP;
241 ip->flags &= ~BTRFS_INODE_NODUMP;
242 if (flags & FS_NOATIME_FL)
243 ip->flags |= BTRFS_INODE_NOATIME;
245 ip->flags &= ~BTRFS_INODE_NOATIME;
246 if (flags & FS_DIRSYNC_FL)
247 ip->flags |= BTRFS_INODE_DIRSYNC;
249 ip->flags &= ~BTRFS_INODE_DIRSYNC;
250 if (flags & FS_NOCOW_FL) {
253 * It's safe to turn csums off here, no extents exist.
254 * Otherwise we want the flag to reflect the real COW
255 * status of the file and will not set it.
257 if (inode->i_size == 0)
258 ip->flags |= BTRFS_INODE_NODATACOW
259 | BTRFS_INODE_NODATASUM;
261 ip->flags |= BTRFS_INODE_NODATACOW;
265 * Revert back under same assuptions as above
268 if (inode->i_size == 0)
269 ip->flags &= ~(BTRFS_INODE_NODATACOW
270 | BTRFS_INODE_NODATASUM);
272 ip->flags &= ~BTRFS_INODE_NODATACOW;
277 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
278 * flag may be changed automatically if compression code won't make
281 if (flags & FS_NOCOMP_FL) {
282 ip->flags &= ~BTRFS_INODE_COMPRESS;
283 ip->flags |= BTRFS_INODE_NOCOMPRESS;
284 } else if (flags & FS_COMPR_FL) {
285 ip->flags |= BTRFS_INODE_COMPRESS;
286 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
288 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
291 trans = btrfs_start_transaction(root, 1);
293 ret = PTR_ERR(trans);
297 btrfs_update_iflags(inode);
298 inode_inc_iversion(inode);
299 inode->i_ctime = CURRENT_TIME;
300 ret = btrfs_update_inode(trans, root, inode);
302 btrfs_end_transaction(trans, root);
305 ip->flags = ip_oldflags;
306 inode->i_flags = i_oldflags;
310 mutex_unlock(&inode->i_mutex);
311 mnt_drop_write_file(file);
315 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
317 struct inode *inode = file_inode(file);
319 return put_user(inode->i_generation, arg);
322 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
324 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
325 struct btrfs_device *device;
326 struct request_queue *q;
327 struct fstrim_range range;
328 u64 minlen = ULLONG_MAX;
330 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
333 if (!capable(CAP_SYS_ADMIN))
337 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
341 q = bdev_get_queue(device->bdev);
342 if (blk_queue_discard(q)) {
344 minlen = min((u64)q->limits.discard_granularity,
352 if (copy_from_user(&range, arg, sizeof(range)))
354 if (range.start > total_bytes ||
355 range.len < fs_info->sb->s_blocksize)
358 range.len = min(range.len, total_bytes - range.start);
359 range.minlen = max(range.minlen, minlen);
360 ret = btrfs_trim_fs(fs_info->tree_root, &range);
364 if (copy_to_user(arg, &range, sizeof(range)))
370 int btrfs_is_empty_uuid(u8 *uuid)
374 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
381 static noinline int create_subvol(struct inode *dir,
382 struct dentry *dentry,
383 char *name, int namelen,
385 struct btrfs_qgroup_inherit *inherit)
387 struct btrfs_trans_handle *trans;
388 struct btrfs_key key;
389 struct btrfs_root_item root_item;
390 struct btrfs_inode_item *inode_item;
391 struct extent_buffer *leaf;
392 struct btrfs_root *root = BTRFS_I(dir)->root;
393 struct btrfs_root *new_root;
394 struct btrfs_block_rsv block_rsv;
395 struct timespec cur_time = CURRENT_TIME;
400 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
405 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
409 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
411 * The same as the snapshot creation, please see the comment
412 * of create_snapshot().
414 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
415 8, &qgroup_reserved, false);
419 trans = btrfs_start_transaction(root, 0);
421 ret = PTR_ERR(trans);
424 trans->block_rsv = &block_rsv;
425 trans->bytes_reserved = block_rsv.size;
427 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
431 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
432 0, objectid, NULL, 0, 0, 0);
438 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
439 btrfs_set_header_bytenr(leaf, leaf->start);
440 btrfs_set_header_generation(leaf, trans->transid);
441 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
442 btrfs_set_header_owner(leaf, objectid);
444 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
446 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
447 btrfs_header_chunk_tree_uuid(leaf),
449 btrfs_mark_buffer_dirty(leaf);
451 memset(&root_item, 0, sizeof(root_item));
453 inode_item = &root_item.inode;
454 btrfs_set_stack_inode_generation(inode_item, 1);
455 btrfs_set_stack_inode_size(inode_item, 3);
456 btrfs_set_stack_inode_nlink(inode_item, 1);
457 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
458 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
460 btrfs_set_root_flags(&root_item, 0);
461 btrfs_set_root_limit(&root_item, 0);
462 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
464 btrfs_set_root_bytenr(&root_item, leaf->start);
465 btrfs_set_root_generation(&root_item, trans->transid);
466 btrfs_set_root_level(&root_item, 0);
467 btrfs_set_root_refs(&root_item, 1);
468 btrfs_set_root_used(&root_item, leaf->len);
469 btrfs_set_root_last_snapshot(&root_item, 0);
471 btrfs_set_root_generation_v2(&root_item,
472 btrfs_root_generation(&root_item));
473 uuid_le_gen(&new_uuid);
474 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
475 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
476 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
477 root_item.ctime = root_item.otime;
478 btrfs_set_root_ctransid(&root_item, trans->transid);
479 btrfs_set_root_otransid(&root_item, trans->transid);
481 btrfs_tree_unlock(leaf);
482 free_extent_buffer(leaf);
485 btrfs_set_root_dirid(&root_item, new_dirid);
487 key.objectid = objectid;
489 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
490 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
495 key.offset = (u64)-1;
496 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
497 if (IS_ERR(new_root)) {
498 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
499 ret = PTR_ERR(new_root);
503 btrfs_record_root_in_trans(trans, new_root);
505 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
507 /* We potentially lose an unused inode item here */
508 btrfs_abort_transaction(trans, root, ret);
513 * insert the directory item
515 ret = btrfs_set_inode_index(dir, &index);
517 btrfs_abort_transaction(trans, root, ret);
521 ret = btrfs_insert_dir_item(trans, root,
522 name, namelen, dir, &key,
523 BTRFS_FT_DIR, index);
525 btrfs_abort_transaction(trans, root, ret);
529 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
530 ret = btrfs_update_inode(trans, root, dir);
533 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
534 objectid, root->root_key.objectid,
535 btrfs_ino(dir), index, name, namelen);
538 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
539 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
542 btrfs_abort_transaction(trans, root, ret);
545 trans->block_rsv = NULL;
546 trans->bytes_reserved = 0;
548 *async_transid = trans->transid;
549 err = btrfs_commit_transaction_async(trans, root, 1);
551 err = btrfs_commit_transaction(trans, root);
553 err = btrfs_commit_transaction(trans, root);
559 inode = btrfs_lookup_dentry(dir, dentry);
561 ret = PTR_ERR(inode);
564 d_instantiate(dentry, inode);
567 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
571 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
572 struct dentry *dentry, char *name, int namelen,
573 u64 *async_transid, bool readonly,
574 struct btrfs_qgroup_inherit *inherit)
577 struct btrfs_pending_snapshot *pending_snapshot;
578 struct btrfs_trans_handle *trans;
584 ret = btrfs_start_delalloc_inodes(root, 0);
588 btrfs_wait_ordered_extents(root, -1);
590 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
591 if (!pending_snapshot)
594 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
595 BTRFS_BLOCK_RSV_TEMP);
597 * 1 - parent dir inode
600 * 2 - root ref/backref
601 * 1 - root of snapshot
604 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
605 &pending_snapshot->block_rsv, 8,
606 &pending_snapshot->qgroup_reserved,
611 pending_snapshot->dentry = dentry;
612 pending_snapshot->root = root;
613 pending_snapshot->readonly = readonly;
614 pending_snapshot->dir = dir;
615 pending_snapshot->inherit = inherit;
617 trans = btrfs_start_transaction(root, 0);
619 ret = PTR_ERR(trans);
623 spin_lock(&root->fs_info->trans_lock);
624 list_add(&pending_snapshot->list,
625 &trans->transaction->pending_snapshots);
626 spin_unlock(&root->fs_info->trans_lock);
628 *async_transid = trans->transid;
629 ret = btrfs_commit_transaction_async(trans,
630 root->fs_info->extent_root, 1);
632 ret = btrfs_commit_transaction(trans, root);
634 ret = btrfs_commit_transaction(trans,
635 root->fs_info->extent_root);
640 ret = pending_snapshot->error;
644 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
648 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
650 ret = PTR_ERR(inode);
654 d_instantiate(dentry, inode);
657 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
658 &pending_snapshot->block_rsv,
659 pending_snapshot->qgroup_reserved);
661 kfree(pending_snapshot);
665 /* copy of check_sticky in fs/namei.c()
666 * It's inline, so penalty for filesystems that don't use sticky bit is
669 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
671 kuid_t fsuid = current_fsuid();
673 if (!(dir->i_mode & S_ISVTX))
675 if (uid_eq(inode->i_uid, fsuid))
677 if (uid_eq(dir->i_uid, fsuid))
679 return !capable(CAP_FOWNER);
682 /* copy of may_delete in fs/namei.c()
683 * Check whether we can remove a link victim from directory dir, check
684 * whether the type of victim is right.
685 * 1. We can't do it if dir is read-only (done in permission())
686 * 2. We should have write and exec permissions on dir
687 * 3. We can't remove anything from append-only dir
688 * 4. We can't do anything with immutable dir (done in permission())
689 * 5. If the sticky bit on dir is set we should either
690 * a. be owner of dir, or
691 * b. be owner of victim, or
692 * c. have CAP_FOWNER capability
693 * 6. If the victim is append-only or immutable we can't do antyhing with
694 * links pointing to it.
695 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
696 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
697 * 9. We can't remove a root or mountpoint.
698 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
699 * nfs_async_unlink().
702 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
706 if (!victim->d_inode)
709 BUG_ON(victim->d_parent->d_inode != dir);
710 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
712 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
717 if (btrfs_check_sticky(dir, victim->d_inode)||
718 IS_APPEND(victim->d_inode)||
719 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
722 if (!S_ISDIR(victim->d_inode->i_mode))
726 } else if (S_ISDIR(victim->d_inode->i_mode))
730 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
735 /* copy of may_create in fs/namei.c() */
736 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
742 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
746 * Create a new subvolume below @parent. This is largely modeled after
747 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
748 * inside this filesystem so it's quite a bit simpler.
750 static noinline int btrfs_mksubvol(struct path *parent,
751 char *name, int namelen,
752 struct btrfs_root *snap_src,
753 u64 *async_transid, bool readonly,
754 struct btrfs_qgroup_inherit *inherit)
756 struct inode *dir = parent->dentry->d_inode;
757 struct dentry *dentry;
760 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
764 dentry = lookup_one_len(name, parent->dentry, namelen);
765 error = PTR_ERR(dentry);
773 error = btrfs_may_create(dir, dentry);
778 * even if this name doesn't exist, we may get hash collisions.
779 * check for them now when we can safely fail
781 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
787 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
789 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
793 error = create_snapshot(snap_src, dir, dentry, name, namelen,
794 async_transid, readonly, inherit);
796 error = create_subvol(dir, dentry, name, namelen,
797 async_transid, inherit);
800 fsnotify_mkdir(dir, dentry);
802 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
806 mutex_unlock(&dir->i_mutex);
811 * When we're defragging a range, we don't want to kick it off again
812 * if it is really just waiting for delalloc to send it down.
813 * If we find a nice big extent or delalloc range for the bytes in the
814 * file you want to defrag, we return 0 to let you know to skip this
817 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
819 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
820 struct extent_map *em = NULL;
821 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
824 read_lock(&em_tree->lock);
825 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
826 read_unlock(&em_tree->lock);
829 end = extent_map_end(em);
831 if (end - offset > thresh)
834 /* if we already have a nice delalloc here, just stop */
836 end = count_range_bits(io_tree, &offset, offset + thresh,
837 thresh, EXTENT_DELALLOC, 1);
844 * helper function to walk through a file and find extents
845 * newer than a specific transid, and smaller than thresh.
847 * This is used by the defragging code to find new and small
850 static int find_new_extents(struct btrfs_root *root,
851 struct inode *inode, u64 newer_than,
852 u64 *off, int thresh)
854 struct btrfs_path *path;
855 struct btrfs_key min_key;
856 struct extent_buffer *leaf;
857 struct btrfs_file_extent_item *extent;
860 u64 ino = btrfs_ino(inode);
862 path = btrfs_alloc_path();
866 min_key.objectid = ino;
867 min_key.type = BTRFS_EXTENT_DATA_KEY;
868 min_key.offset = *off;
870 path->keep_locks = 1;
873 ret = btrfs_search_forward(root, &min_key, path, newer_than);
876 if (min_key.objectid != ino)
878 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
881 leaf = path->nodes[0];
882 extent = btrfs_item_ptr(leaf, path->slots[0],
883 struct btrfs_file_extent_item);
885 type = btrfs_file_extent_type(leaf, extent);
886 if (type == BTRFS_FILE_EXTENT_REG &&
887 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
888 check_defrag_in_cache(inode, min_key.offset, thresh)) {
889 *off = min_key.offset;
890 btrfs_free_path(path);
894 if (min_key.offset == (u64)-1)
898 btrfs_release_path(path);
901 btrfs_free_path(path);
905 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
907 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
909 struct extent_map *em;
910 u64 len = PAGE_CACHE_SIZE;
913 * hopefully we have this extent in the tree already, try without
914 * the full extent lock
916 read_lock(&em_tree->lock);
917 em = lookup_extent_mapping(em_tree, start, len);
918 read_unlock(&em_tree->lock);
921 /* get the big lock and read metadata off disk */
922 lock_extent(io_tree, start, start + len - 1);
923 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
924 unlock_extent(io_tree, start, start + len - 1);
933 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
935 struct extent_map *next;
938 /* this is the last extent */
939 if (em->start + em->len >= i_size_read(inode))
942 next = defrag_lookup_extent(inode, em->start + em->len);
943 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
946 free_extent_map(next);
950 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
951 u64 *last_len, u64 *skip, u64 *defrag_end,
954 struct extent_map *em;
956 bool next_mergeable = true;
959 * make sure that once we start defragging an extent, we keep on
962 if (start < *defrag_end)
967 em = defrag_lookup_extent(inode, start);
971 /* this will cover holes, and inline extents */
972 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
977 next_mergeable = defrag_check_next_extent(inode, em);
980 * we hit a real extent, if it is big or the next extent is not a
981 * real extent, don't bother defragging it
983 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
984 (em->len >= thresh || !next_mergeable))
988 * last_len ends up being a counter of how many bytes we've defragged.
989 * every time we choose not to defrag an extent, we reset *last_len
990 * so that the next tiny extent will force a defrag.
992 * The end result of this is that tiny extents before a single big
993 * extent will force at least part of that big extent to be defragged.
996 *defrag_end = extent_map_end(em);
999 *skip = extent_map_end(em);
1003 free_extent_map(em);
1008 * it doesn't do much good to defrag one or two pages
1009 * at a time. This pulls in a nice chunk of pages
1010 * to COW and defrag.
1012 * It also makes sure the delalloc code has enough
1013 * dirty data to avoid making new small extents as part
1016 * It's a good idea to start RA on this range
1017 * before calling this.
1019 static int cluster_pages_for_defrag(struct inode *inode,
1020 struct page **pages,
1021 unsigned long start_index,
1024 unsigned long file_end;
1025 u64 isize = i_size_read(inode);
1032 struct btrfs_ordered_extent *ordered;
1033 struct extent_state *cached_state = NULL;
1034 struct extent_io_tree *tree;
1035 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1037 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
1038 if (!isize || start_index > file_end)
1041 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1043 ret = btrfs_delalloc_reserve_space(inode,
1044 page_cnt << PAGE_CACHE_SHIFT);
1048 tree = &BTRFS_I(inode)->io_tree;
1050 /* step one, lock all the pages */
1051 for (i = 0; i < page_cnt; i++) {
1054 page = find_or_create_page(inode->i_mapping,
1055 start_index + i, mask);
1059 page_start = page_offset(page);
1060 page_end = page_start + PAGE_CACHE_SIZE - 1;
1062 lock_extent(tree, page_start, page_end);
1063 ordered = btrfs_lookup_ordered_extent(inode,
1065 unlock_extent(tree, page_start, page_end);
1070 btrfs_start_ordered_extent(inode, ordered, 1);
1071 btrfs_put_ordered_extent(ordered);
1074 * we unlocked the page above, so we need check if
1075 * it was released or not.
1077 if (page->mapping != inode->i_mapping) {
1079 page_cache_release(page);
1084 if (!PageUptodate(page)) {
1085 btrfs_readpage(NULL, page);
1087 if (!PageUptodate(page)) {
1089 page_cache_release(page);
1095 if (page->mapping != inode->i_mapping) {
1097 page_cache_release(page);
1107 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1111 * so now we have a nice long stream of locked
1112 * and up to date pages, lets wait on them
1114 for (i = 0; i < i_done; i++)
1115 wait_on_page_writeback(pages[i]);
1117 page_start = page_offset(pages[0]);
1118 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1120 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1121 page_start, page_end - 1, 0, &cached_state);
1122 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1123 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1124 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1125 &cached_state, GFP_NOFS);
1127 if (i_done != page_cnt) {
1128 spin_lock(&BTRFS_I(inode)->lock);
1129 BTRFS_I(inode)->outstanding_extents++;
1130 spin_unlock(&BTRFS_I(inode)->lock);
1131 btrfs_delalloc_release_space(inode,
1132 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1136 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1137 &cached_state, GFP_NOFS);
1139 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1140 page_start, page_end - 1, &cached_state,
1143 for (i = 0; i < i_done; i++) {
1144 clear_page_dirty_for_io(pages[i]);
1145 ClearPageChecked(pages[i]);
1146 set_page_extent_mapped(pages[i]);
1147 set_page_dirty(pages[i]);
1148 unlock_page(pages[i]);
1149 page_cache_release(pages[i]);
1153 for (i = 0; i < i_done; i++) {
1154 unlock_page(pages[i]);
1155 page_cache_release(pages[i]);
1157 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1162 int btrfs_defrag_file(struct inode *inode, struct file *file,
1163 struct btrfs_ioctl_defrag_range_args *range,
1164 u64 newer_than, unsigned long max_to_defrag)
1166 struct btrfs_root *root = BTRFS_I(inode)->root;
1167 struct file_ra_state *ra = NULL;
1168 unsigned long last_index;
1169 u64 isize = i_size_read(inode);
1173 u64 newer_off = range->start;
1175 unsigned long ra_index = 0;
1177 int defrag_count = 0;
1178 int compress_type = BTRFS_COMPRESS_ZLIB;
1179 int extent_thresh = range->extent_thresh;
1180 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1181 int cluster = max_cluster;
1182 u64 new_align = ~((u64)128 * 1024 - 1);
1183 struct page **pages = NULL;
1188 if (range->start >= isize)
1191 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1192 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1194 if (range->compress_type)
1195 compress_type = range->compress_type;
1198 if (extent_thresh == 0)
1199 extent_thresh = 256 * 1024;
1202 * if we were not given a file, allocate a readahead
1206 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1209 file_ra_state_init(ra, inode->i_mapping);
1214 pages = kmalloc_array(max_cluster, sizeof(struct page *),
1221 /* find the last page to defrag */
1222 if (range->start + range->len > range->start) {
1223 last_index = min_t(u64, isize - 1,
1224 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1226 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1230 ret = find_new_extents(root, inode, newer_than,
1231 &newer_off, 64 * 1024);
1233 range->start = newer_off;
1235 * we always align our defrag to help keep
1236 * the extents in the file evenly spaced
1238 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1242 i = range->start >> PAGE_CACHE_SHIFT;
1245 max_to_defrag = last_index + 1;
1248 * make writeback starts from i, so the defrag range can be
1249 * written sequentially.
1251 if (i < inode->i_mapping->writeback_index)
1252 inode->i_mapping->writeback_index = i;
1254 while (i <= last_index && defrag_count < max_to_defrag &&
1255 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1256 PAGE_CACHE_SHIFT)) {
1258 * make sure we stop running if someone unmounts
1261 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1264 if (btrfs_defrag_cancelled(root->fs_info)) {
1265 printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
1270 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1271 extent_thresh, &last_len, &skip,
1272 &defrag_end, range->flags &
1273 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1276 * the should_defrag function tells us how much to skip
1277 * bump our counter by the suggested amount
1279 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1280 i = max(i + 1, next);
1285 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1286 PAGE_CACHE_SHIFT) - i;
1287 cluster = min(cluster, max_cluster);
1289 cluster = max_cluster;
1292 if (i + cluster > ra_index) {
1293 ra_index = max(i, ra_index);
1294 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1296 ra_index += max_cluster;
1299 mutex_lock(&inode->i_mutex);
1300 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1301 BTRFS_I(inode)->force_compress = compress_type;
1302 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1304 mutex_unlock(&inode->i_mutex);
1308 defrag_count += ret;
1309 balance_dirty_pages_ratelimited(inode->i_mapping);
1310 mutex_unlock(&inode->i_mutex);
1313 if (newer_off == (u64)-1)
1319 newer_off = max(newer_off + 1,
1320 (u64)i << PAGE_CACHE_SHIFT);
1322 ret = find_new_extents(root, inode,
1323 newer_than, &newer_off,
1326 range->start = newer_off;
1327 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1334 last_len += ret << PAGE_CACHE_SHIFT;
1342 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1343 filemap_flush(inode->i_mapping);
1345 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1346 /* the filemap_flush will queue IO into the worker threads, but
1347 * we have to make sure the IO is actually started and that
1348 * ordered extents get created before we return
1350 atomic_inc(&root->fs_info->async_submit_draining);
1351 while (atomic_read(&root->fs_info->nr_async_submits) ||
1352 atomic_read(&root->fs_info->async_delalloc_pages)) {
1353 wait_event(root->fs_info->async_submit_wait,
1354 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1355 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1357 atomic_dec(&root->fs_info->async_submit_draining);
1360 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1361 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1367 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1368 mutex_lock(&inode->i_mutex);
1369 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1370 mutex_unlock(&inode->i_mutex);
1378 static noinline int btrfs_ioctl_resize(struct file *file,
1384 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1385 struct btrfs_ioctl_vol_args *vol_args;
1386 struct btrfs_trans_handle *trans;
1387 struct btrfs_device *device = NULL;
1389 char *devstr = NULL;
1393 if (!capable(CAP_SYS_ADMIN))
1396 ret = mnt_want_write_file(file);
1400 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1402 mnt_drop_write_file(file);
1403 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1406 mutex_lock(&root->fs_info->volume_mutex);
1407 vol_args = memdup_user(arg, sizeof(*vol_args));
1408 if (IS_ERR(vol_args)) {
1409 ret = PTR_ERR(vol_args);
1413 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1415 sizestr = vol_args->name;
1416 devstr = strchr(sizestr, ':');
1419 sizestr = devstr + 1;
1421 devstr = vol_args->name;
1422 devid = simple_strtoull(devstr, &end, 10);
1427 btrfs_info(root->fs_info, "resizing devid %llu", devid);
1430 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1432 btrfs_info(root->fs_info, "resizer unable to find device %llu",
1438 if (!device->writeable) {
1439 btrfs_info(root->fs_info,
1440 "resizer unable to apply on readonly device %llu",
1446 if (!strcmp(sizestr, "max"))
1447 new_size = device->bdev->bd_inode->i_size;
1449 if (sizestr[0] == '-') {
1452 } else if (sizestr[0] == '+') {
1456 new_size = memparse(sizestr, NULL);
1457 if (new_size == 0) {
1463 if (device->is_tgtdev_for_dev_replace) {
1468 old_size = device->total_bytes;
1471 if (new_size > old_size) {
1475 new_size = old_size - new_size;
1476 } else if (mod > 0) {
1477 if (new_size > ULLONG_MAX - old_size) {
1481 new_size = old_size + new_size;
1484 if (new_size < 256 * 1024 * 1024) {
1488 if (new_size > device->bdev->bd_inode->i_size) {
1493 do_div(new_size, root->sectorsize);
1494 new_size *= root->sectorsize;
1496 printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
1497 rcu_str_deref(device->name), new_size);
1499 if (new_size > old_size) {
1500 trans = btrfs_start_transaction(root, 0);
1501 if (IS_ERR(trans)) {
1502 ret = PTR_ERR(trans);
1505 ret = btrfs_grow_device(trans, device, new_size);
1506 btrfs_commit_transaction(trans, root);
1507 } else if (new_size < old_size) {
1508 ret = btrfs_shrink_device(device, new_size);
1509 } /* equal, nothing need to do */
1514 mutex_unlock(&root->fs_info->volume_mutex);
1515 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1516 mnt_drop_write_file(file);
1520 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1521 char *name, unsigned long fd, int subvol,
1522 u64 *transid, bool readonly,
1523 struct btrfs_qgroup_inherit *inherit)
1528 ret = mnt_want_write_file(file);
1532 namelen = strlen(name);
1533 if (strchr(name, '/')) {
1535 goto out_drop_write;
1538 if (name[0] == '.' &&
1539 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1541 goto out_drop_write;
1545 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1546 NULL, transid, readonly, inherit);
1548 struct fd src = fdget(fd);
1549 struct inode *src_inode;
1552 goto out_drop_write;
1555 src_inode = file_inode(src.file);
1556 if (src_inode->i_sb != file_inode(file)->i_sb) {
1557 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1558 "Snapshot src from another FS");
1561 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1562 BTRFS_I(src_inode)->root,
1563 transid, readonly, inherit);
1568 mnt_drop_write_file(file);
1573 static noinline int btrfs_ioctl_snap_create(struct file *file,
1574 void __user *arg, int subvol)
1576 struct btrfs_ioctl_vol_args *vol_args;
1579 vol_args = memdup_user(arg, sizeof(*vol_args));
1580 if (IS_ERR(vol_args))
1581 return PTR_ERR(vol_args);
1582 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1584 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1585 vol_args->fd, subvol,
1592 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1593 void __user *arg, int subvol)
1595 struct btrfs_ioctl_vol_args_v2 *vol_args;
1599 bool readonly = false;
1600 struct btrfs_qgroup_inherit *inherit = NULL;
1602 vol_args = memdup_user(arg, sizeof(*vol_args));
1603 if (IS_ERR(vol_args))
1604 return PTR_ERR(vol_args);
1605 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1607 if (vol_args->flags &
1608 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1609 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1614 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1616 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1618 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1619 if (vol_args->size > PAGE_CACHE_SIZE) {
1623 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1624 if (IS_ERR(inherit)) {
1625 ret = PTR_ERR(inherit);
1630 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1631 vol_args->fd, subvol, ptr,
1634 if (ret == 0 && ptr &&
1636 offsetof(struct btrfs_ioctl_vol_args_v2,
1637 transid), ptr, sizeof(*ptr)))
1645 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1648 struct inode *inode = file_inode(file);
1649 struct btrfs_root *root = BTRFS_I(inode)->root;
1653 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1656 down_read(&root->fs_info->subvol_sem);
1657 if (btrfs_root_readonly(root))
1658 flags |= BTRFS_SUBVOL_RDONLY;
1659 up_read(&root->fs_info->subvol_sem);
1661 if (copy_to_user(arg, &flags, sizeof(flags)))
1667 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1670 struct inode *inode = file_inode(file);
1671 struct btrfs_root *root = BTRFS_I(inode)->root;
1672 struct btrfs_trans_handle *trans;
1677 ret = mnt_want_write_file(file);
1681 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1683 goto out_drop_write;
1686 if (copy_from_user(&flags, arg, sizeof(flags))) {
1688 goto out_drop_write;
1691 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1693 goto out_drop_write;
1696 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1698 goto out_drop_write;
1701 if (!inode_owner_or_capable(inode)) {
1703 goto out_drop_write;
1706 down_write(&root->fs_info->subvol_sem);
1709 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1712 root_flags = btrfs_root_flags(&root->root_item);
1713 if (flags & BTRFS_SUBVOL_RDONLY) {
1714 btrfs_set_root_flags(&root->root_item,
1715 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1718 * Block RO -> RW transition if this subvolume is involved in
1721 spin_lock(&root->root_item_lock);
1722 if (root->send_in_progress == 0) {
1723 btrfs_set_root_flags(&root->root_item,
1724 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1725 spin_unlock(&root->root_item_lock);
1727 spin_unlock(&root->root_item_lock);
1728 btrfs_warn(root->fs_info,
1729 "Attempt to set subvolume %llu read-write during send",
1730 root->root_key.objectid);
1736 trans = btrfs_start_transaction(root, 1);
1737 if (IS_ERR(trans)) {
1738 ret = PTR_ERR(trans);
1742 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1743 &root->root_key, &root->root_item);
1745 btrfs_commit_transaction(trans, root);
1748 btrfs_set_root_flags(&root->root_item, root_flags);
1750 up_write(&root->fs_info->subvol_sem);
1752 mnt_drop_write_file(file);
1758 * helper to check if the subvolume references other subvolumes
1760 static noinline int may_destroy_subvol(struct btrfs_root *root)
1762 struct btrfs_path *path;
1763 struct btrfs_dir_item *di;
1764 struct btrfs_key key;
1768 path = btrfs_alloc_path();
1772 /* Make sure this root isn't set as the default subvol */
1773 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1774 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1775 dir_id, "default", 7, 0);
1776 if (di && !IS_ERR(di)) {
1777 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1778 if (key.objectid == root->root_key.objectid) {
1782 btrfs_release_path(path);
1785 key.objectid = root->root_key.objectid;
1786 key.type = BTRFS_ROOT_REF_KEY;
1787 key.offset = (u64)-1;
1789 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1796 if (path->slots[0] > 0) {
1798 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1799 if (key.objectid == root->root_key.objectid &&
1800 key.type == BTRFS_ROOT_REF_KEY)
1804 btrfs_free_path(path);
1808 static noinline int key_in_sk(struct btrfs_key *key,
1809 struct btrfs_ioctl_search_key *sk)
1811 struct btrfs_key test;
1814 test.objectid = sk->min_objectid;
1815 test.type = sk->min_type;
1816 test.offset = sk->min_offset;
1818 ret = btrfs_comp_cpu_keys(key, &test);
1822 test.objectid = sk->max_objectid;
1823 test.type = sk->max_type;
1824 test.offset = sk->max_offset;
1826 ret = btrfs_comp_cpu_keys(key, &test);
1832 static noinline int copy_to_sk(struct btrfs_root *root,
1833 struct btrfs_path *path,
1834 struct btrfs_key *key,
1835 struct btrfs_ioctl_search_key *sk,
1837 unsigned long *sk_offset,
1841 struct extent_buffer *leaf;
1842 struct btrfs_ioctl_search_header sh;
1843 unsigned long item_off;
1844 unsigned long item_len;
1850 leaf = path->nodes[0];
1851 slot = path->slots[0];
1852 nritems = btrfs_header_nritems(leaf);
1854 if (btrfs_header_generation(leaf) > sk->max_transid) {
1858 found_transid = btrfs_header_generation(leaf);
1860 for (i = slot; i < nritems; i++) {
1861 item_off = btrfs_item_ptr_offset(leaf, i);
1862 item_len = btrfs_item_size_nr(leaf, i);
1864 btrfs_item_key_to_cpu(leaf, key, i);
1865 if (!key_in_sk(key, sk))
1868 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1871 if (sizeof(sh) + item_len + *sk_offset >
1872 BTRFS_SEARCH_ARGS_BUFSIZE) {
1877 sh.objectid = key->objectid;
1878 sh.offset = key->offset;
1879 sh.type = key->type;
1881 sh.transid = found_transid;
1883 /* copy search result header */
1884 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1885 *sk_offset += sizeof(sh);
1888 char *p = buf + *sk_offset;
1890 read_extent_buffer(leaf, p,
1891 item_off, item_len);
1892 *sk_offset += item_len;
1896 if (*num_found >= sk->nr_items)
1901 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1903 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1906 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1916 static noinline int search_ioctl(struct inode *inode,
1917 struct btrfs_ioctl_search_args *args)
1919 struct btrfs_root *root;
1920 struct btrfs_key key;
1921 struct btrfs_path *path;
1922 struct btrfs_ioctl_search_key *sk = &args->key;
1923 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1926 unsigned long sk_offset = 0;
1928 path = btrfs_alloc_path();
1932 if (sk->tree_id == 0) {
1933 /* search the root of the inode that was passed */
1934 root = BTRFS_I(inode)->root;
1936 key.objectid = sk->tree_id;
1937 key.type = BTRFS_ROOT_ITEM_KEY;
1938 key.offset = (u64)-1;
1939 root = btrfs_read_fs_root_no_name(info, &key);
1941 printk(KERN_ERR "BTRFS: could not find root %llu\n",
1943 btrfs_free_path(path);
1948 key.objectid = sk->min_objectid;
1949 key.type = sk->min_type;
1950 key.offset = sk->min_offset;
1952 path->keep_locks = 1;
1955 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
1961 ret = copy_to_sk(root, path, &key, sk, args->buf,
1962 &sk_offset, &num_found);
1963 btrfs_release_path(path);
1964 if (ret || num_found >= sk->nr_items)
1970 sk->nr_items = num_found;
1971 btrfs_free_path(path);
1975 static noinline int btrfs_ioctl_tree_search(struct file *file,
1978 struct btrfs_ioctl_search_args *args;
1979 struct inode *inode;
1982 if (!capable(CAP_SYS_ADMIN))
1985 args = memdup_user(argp, sizeof(*args));
1987 return PTR_ERR(args);
1989 inode = file_inode(file);
1990 ret = search_ioctl(inode, args);
1991 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1998 * Search INODE_REFs to identify path name of 'dirid' directory
1999 * in a 'tree_id' tree. and sets path name to 'name'.
2001 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2002 u64 tree_id, u64 dirid, char *name)
2004 struct btrfs_root *root;
2005 struct btrfs_key key;
2011 struct btrfs_inode_ref *iref;
2012 struct extent_buffer *l;
2013 struct btrfs_path *path;
2015 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2020 path = btrfs_alloc_path();
2024 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
2026 key.objectid = tree_id;
2027 key.type = BTRFS_ROOT_ITEM_KEY;
2028 key.offset = (u64)-1;
2029 root = btrfs_read_fs_root_no_name(info, &key);
2031 printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
2036 key.objectid = dirid;
2037 key.type = BTRFS_INODE_REF_KEY;
2038 key.offset = (u64)-1;
2041 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2045 ret = btrfs_previous_item(root, path, dirid,
2046 BTRFS_INODE_REF_KEY);
2056 slot = path->slots[0];
2057 btrfs_item_key_to_cpu(l, &key, slot);
2059 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2060 len = btrfs_inode_ref_name_len(l, iref);
2062 total_len += len + 1;
2064 ret = -ENAMETOOLONG;
2069 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2071 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2074 btrfs_release_path(path);
2075 key.objectid = key.offset;
2076 key.offset = (u64)-1;
2077 dirid = key.objectid;
2079 memmove(name, ptr, total_len);
2080 name[total_len] = '\0';
2083 btrfs_free_path(path);
2087 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2090 struct btrfs_ioctl_ino_lookup_args *args;
2091 struct inode *inode;
2094 if (!capable(CAP_SYS_ADMIN))
2097 args = memdup_user(argp, sizeof(*args));
2099 return PTR_ERR(args);
2101 inode = file_inode(file);
2103 if (args->treeid == 0)
2104 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2106 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2107 args->treeid, args->objectid,
2110 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2117 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2120 struct dentry *parent = file->f_path.dentry;
2121 struct dentry *dentry;
2122 struct inode *dir = parent->d_inode;
2123 struct inode *inode;
2124 struct btrfs_root *root = BTRFS_I(dir)->root;
2125 struct btrfs_root *dest = NULL;
2126 struct btrfs_ioctl_vol_args *vol_args;
2127 struct btrfs_trans_handle *trans;
2128 struct btrfs_block_rsv block_rsv;
2129 u64 qgroup_reserved;
2134 vol_args = memdup_user(arg, sizeof(*vol_args));
2135 if (IS_ERR(vol_args))
2136 return PTR_ERR(vol_args);
2138 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2139 namelen = strlen(vol_args->name);
2140 if (strchr(vol_args->name, '/') ||
2141 strncmp(vol_args->name, "..", namelen) == 0) {
2146 err = mnt_want_write_file(file);
2150 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2152 goto out_drop_write;
2153 dentry = lookup_one_len(vol_args->name, parent, namelen);
2154 if (IS_ERR(dentry)) {
2155 err = PTR_ERR(dentry);
2156 goto out_unlock_dir;
2159 if (!dentry->d_inode) {
2164 inode = dentry->d_inode;
2165 dest = BTRFS_I(inode)->root;
2166 if (!capable(CAP_SYS_ADMIN)) {
2168 * Regular user. Only allow this with a special mount
2169 * option, when the user has write+exec access to the
2170 * subvol root, and when rmdir(2) would have been
2173 * Note that this is _not_ check that the subvol is
2174 * empty or doesn't contain data that we wouldn't
2175 * otherwise be able to delete.
2177 * Users who want to delete empty subvols should try
2181 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2185 * Do not allow deletion if the parent dir is the same
2186 * as the dir to be deleted. That means the ioctl
2187 * must be called on the dentry referencing the root
2188 * of the subvol, not a random directory contained
2195 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2200 /* check if subvolume may be deleted by a user */
2201 err = btrfs_may_delete(dir, dentry, 1);
2205 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2210 mutex_lock(&inode->i_mutex);
2211 err = d_invalidate(dentry);
2215 down_write(&root->fs_info->subvol_sem);
2217 err = may_destroy_subvol(dest);
2221 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2223 * One for dir inode, two for dir entries, two for root
2226 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2227 5, &qgroup_reserved, true);
2231 trans = btrfs_start_transaction(root, 0);
2232 if (IS_ERR(trans)) {
2233 err = PTR_ERR(trans);
2236 trans->block_rsv = &block_rsv;
2237 trans->bytes_reserved = block_rsv.size;
2239 ret = btrfs_unlink_subvol(trans, root, dir,
2240 dest->root_key.objectid,
2241 dentry->d_name.name,
2242 dentry->d_name.len);
2245 btrfs_abort_transaction(trans, root, ret);
2249 btrfs_record_root_in_trans(trans, dest);
2251 memset(&dest->root_item.drop_progress, 0,
2252 sizeof(dest->root_item.drop_progress));
2253 dest->root_item.drop_level = 0;
2254 btrfs_set_root_refs(&dest->root_item, 0);
2256 if (!xchg(&dest->orphan_item_inserted, 1)) {
2257 ret = btrfs_insert_orphan_item(trans,
2258 root->fs_info->tree_root,
2259 dest->root_key.objectid);
2261 btrfs_abort_transaction(trans, root, ret);
2267 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2268 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2269 dest->root_key.objectid);
2270 if (ret && ret != -ENOENT) {
2271 btrfs_abort_transaction(trans, root, ret);
2275 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2276 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2277 dest->root_item.received_uuid,
2278 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2279 dest->root_key.objectid);
2280 if (ret && ret != -ENOENT) {
2281 btrfs_abort_transaction(trans, root, ret);
2288 trans->block_rsv = NULL;
2289 trans->bytes_reserved = 0;
2290 ret = btrfs_end_transaction(trans, root);
2293 inode->i_flags |= S_DEAD;
2295 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
2297 up_write(&root->fs_info->subvol_sem);
2299 mutex_unlock(&inode->i_mutex);
2301 shrink_dcache_sb(root->fs_info->sb);
2302 btrfs_invalidate_inodes(dest);
2306 if (dest->cache_inode) {
2307 iput(dest->cache_inode);
2308 dest->cache_inode = NULL;
2314 mutex_unlock(&dir->i_mutex);
2316 mnt_drop_write_file(file);
2322 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2324 struct inode *inode = file_inode(file);
2325 struct btrfs_root *root = BTRFS_I(inode)->root;
2326 struct btrfs_ioctl_defrag_range_args *range;
2329 ret = mnt_want_write_file(file);
2333 if (btrfs_root_readonly(root)) {
2338 switch (inode->i_mode & S_IFMT) {
2340 if (!capable(CAP_SYS_ADMIN)) {
2344 ret = btrfs_defrag_root(root);
2347 ret = btrfs_defrag_root(root->fs_info->extent_root);
2350 if (!(file->f_mode & FMODE_WRITE)) {
2355 range = kzalloc(sizeof(*range), GFP_KERNEL);
2362 if (copy_from_user(range, argp,
2368 /* compression requires us to start the IO */
2369 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2370 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2371 range->extent_thresh = (u32)-1;
2374 /* the rest are all set to zero by kzalloc */
2375 range->len = (u64)-1;
2377 ret = btrfs_defrag_file(file_inode(file), file,
2387 mnt_drop_write_file(file);
2391 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2393 struct btrfs_ioctl_vol_args *vol_args;
2396 if (!capable(CAP_SYS_ADMIN))
2399 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2401 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2404 mutex_lock(&root->fs_info->volume_mutex);
2405 vol_args = memdup_user(arg, sizeof(*vol_args));
2406 if (IS_ERR(vol_args)) {
2407 ret = PTR_ERR(vol_args);
2411 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2412 ret = btrfs_init_new_device(root, vol_args->name);
2416 mutex_unlock(&root->fs_info->volume_mutex);
2417 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2421 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2423 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2424 struct btrfs_ioctl_vol_args *vol_args;
2427 if (!capable(CAP_SYS_ADMIN))
2430 ret = mnt_want_write_file(file);
2434 vol_args = memdup_user(arg, sizeof(*vol_args));
2435 if (IS_ERR(vol_args)) {
2436 ret = PTR_ERR(vol_args);
2440 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2442 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2444 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2448 mutex_lock(&root->fs_info->volume_mutex);
2449 ret = btrfs_rm_device(root, vol_args->name);
2450 mutex_unlock(&root->fs_info->volume_mutex);
2451 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2455 mnt_drop_write_file(file);
2459 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2461 struct btrfs_ioctl_fs_info_args *fi_args;
2462 struct btrfs_device *device;
2463 struct btrfs_device *next;
2464 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2467 if (!capable(CAP_SYS_ADMIN))
2470 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2474 mutex_lock(&fs_devices->device_list_mutex);
2475 fi_args->num_devices = fs_devices->num_devices;
2476 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2478 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2479 if (device->devid > fi_args->max_id)
2480 fi_args->max_id = device->devid;
2482 mutex_unlock(&fs_devices->device_list_mutex);
2484 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2491 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2493 struct btrfs_ioctl_dev_info_args *di_args;
2494 struct btrfs_device *dev;
2495 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2497 char *s_uuid = NULL;
2499 if (!capable(CAP_SYS_ADMIN))
2502 di_args = memdup_user(arg, sizeof(*di_args));
2503 if (IS_ERR(di_args))
2504 return PTR_ERR(di_args);
2506 if (!btrfs_is_empty_uuid(di_args->uuid))
2507 s_uuid = di_args->uuid;
2509 mutex_lock(&fs_devices->device_list_mutex);
2510 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2517 di_args->devid = dev->devid;
2518 di_args->bytes_used = dev->bytes_used;
2519 di_args->total_bytes = dev->total_bytes;
2520 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2522 struct rcu_string *name;
2525 name = rcu_dereference(dev->name);
2526 strncpy(di_args->path, name->str, sizeof(di_args->path));
2528 di_args->path[sizeof(di_args->path) - 1] = 0;
2530 di_args->path[0] = '\0';
2534 mutex_unlock(&fs_devices->device_list_mutex);
2535 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2542 static struct page *extent_same_get_page(struct inode *inode, u64 off)
2546 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2548 index = off >> PAGE_CACHE_SHIFT;
2550 page = grab_cache_page(inode->i_mapping, index);
2554 if (!PageUptodate(page)) {
2555 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2559 if (!PageUptodate(page)) {
2561 page_cache_release(page);
2570 static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2572 /* do any pending delalloc/csum calc on src, one way or
2573 another, and lock file content */
2575 struct btrfs_ordered_extent *ordered;
2576 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2577 ordered = btrfs_lookup_first_ordered_extent(inode,
2580 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2581 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2583 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2585 btrfs_put_ordered_extent(ordered);
2586 btrfs_wait_ordered_range(inode, off, len);
2590 static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2591 struct inode *inode2, u64 loff2, u64 len)
2593 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2594 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2596 mutex_unlock(&inode1->i_mutex);
2597 mutex_unlock(&inode2->i_mutex);
2600 static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2601 struct inode *inode2, u64 loff2, u64 len)
2603 if (inode1 < inode2) {
2604 swap(inode1, inode2);
2608 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2609 lock_extent_range(inode1, loff1, len);
2610 if (inode1 != inode2) {
2611 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2612 lock_extent_range(inode2, loff2, len);
2616 static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2617 u64 dst_loff, u64 len)
2620 struct page *src_page, *dst_page;
2621 unsigned int cmp_len = PAGE_CACHE_SIZE;
2622 void *addr, *dst_addr;
2625 if (len < PAGE_CACHE_SIZE)
2628 src_page = extent_same_get_page(src, loff);
2631 dst_page = extent_same_get_page(dst, dst_loff);
2633 page_cache_release(src_page);
2636 addr = kmap_atomic(src_page);
2637 dst_addr = kmap_atomic(dst_page);
2639 flush_dcache_page(src_page);
2640 flush_dcache_page(dst_page);
2642 if (memcmp(addr, dst_addr, cmp_len))
2643 ret = BTRFS_SAME_DATA_DIFFERS;
2645 kunmap_atomic(addr);
2646 kunmap_atomic(dst_addr);
2647 page_cache_release(src_page);
2648 page_cache_release(dst_page);
2654 dst_loff += cmp_len;
2661 static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2663 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2665 if (off + len > inode->i_size || off + len < off)
2667 /* Check that we are block aligned - btrfs_clone() requires this */
2668 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2674 static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2675 struct inode *dst, u64 dst_loff)
2680 * btrfs_clone() can't handle extents in the same file
2681 * yet. Once that works, we can drop this check and replace it
2682 * with a check for the same inode, but overlapping extents.
2687 btrfs_double_lock(src, loff, dst, dst_loff, len);
2689 ret = extent_same_check_offsets(src, loff, len);
2693 ret = extent_same_check_offsets(dst, dst_loff, len);
2697 /* don't make the dst file partly checksummed */
2698 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2699 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2704 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2706 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2709 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2714 #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2716 static long btrfs_ioctl_file_extent_same(struct file *file,
2719 struct btrfs_ioctl_same_args tmp;
2720 struct btrfs_ioctl_same_args *same;
2721 struct btrfs_ioctl_same_extent_info *info;
2722 struct inode *src = file->f_dentry->d_inode;
2723 struct file *dst_file = NULL;
2730 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2731 bool is_admin = capable(CAP_SYS_ADMIN);
2733 if (!(file->f_mode & FMODE_READ))
2736 ret = mnt_want_write_file(file);
2740 if (copy_from_user(&tmp,
2741 (struct btrfs_ioctl_same_args __user *)argp,
2747 size = sizeof(tmp) +
2748 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2750 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
2753 ret = PTR_ERR(same);
2757 off = same->logical_offset;
2761 * Limit the total length we will dedupe for each operation.
2762 * This is intended to bound the total time spent in this
2763 * ioctl to something sane.
2765 if (len > BTRFS_MAX_DEDUPE_LEN)
2766 len = BTRFS_MAX_DEDUPE_LEN;
2768 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2770 * Btrfs does not support blocksize < page_size. As a
2771 * result, btrfs_cmp_data() won't correctly handle
2772 * this situation without an update.
2779 if (S_ISDIR(src->i_mode))
2783 if (!S_ISREG(src->i_mode))
2786 /* pre-format output fields to sane values */
2787 for (i = 0; i < same->dest_count; i++) {
2788 same->info[i].bytes_deduped = 0ULL;
2789 same->info[i].status = 0;
2793 for (i = 0; i < same->dest_count; i++) {
2794 info = &same->info[i];
2796 dst_file = fget(info->fd);
2798 info->status = -EBADF;
2802 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2803 info->status = -EINVAL;
2807 info->status = -EXDEV;
2808 if (file->f_path.mnt != dst_file->f_path.mnt)
2811 dst = dst_file->f_dentry->d_inode;
2812 if (src->i_sb != dst->i_sb)
2815 if (S_ISDIR(dst->i_mode)) {
2816 info->status = -EISDIR;
2820 if (!S_ISREG(dst->i_mode)) {
2821 info->status = -EACCES;
2825 info->status = btrfs_extent_same(src, off, len, dst,
2826 info->logical_offset);
2827 if (info->status == 0)
2828 info->bytes_deduped += len;
2835 ret = copy_to_user(argp, same, size);
2840 mnt_drop_write_file(file);
2845 * btrfs_clone() - clone a range from inode file to another
2847 * @src: Inode to clone from
2848 * @inode: Inode to clone to
2849 * @off: Offset within source to start clone from
2850 * @olen: Original length, passed by user, of range to clone
2851 * @olen_aligned: Block-aligned value of olen, extent_same uses
2852 * identical values here
2853 * @destoff: Offset within @inode to start clone
2855 static int btrfs_clone(struct inode *src, struct inode *inode,
2856 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2858 struct btrfs_root *root = BTRFS_I(inode)->root;
2859 struct btrfs_path *path = NULL;
2860 struct extent_buffer *leaf;
2861 struct btrfs_trans_handle *trans;
2863 struct btrfs_key key;
2867 u64 len = olen_aligned;
2870 buf = vmalloc(btrfs_level_size(root, 0));
2874 path = btrfs_alloc_path();
2882 key.objectid = btrfs_ino(src);
2883 key.type = BTRFS_EXTENT_DATA_KEY;
2888 * note the key will change type as we walk through the
2891 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2896 nritems = btrfs_header_nritems(path->nodes[0]);
2897 if (path->slots[0] >= nritems) {
2898 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2903 nritems = btrfs_header_nritems(path->nodes[0]);
2905 leaf = path->nodes[0];
2906 slot = path->slots[0];
2908 btrfs_item_key_to_cpu(leaf, &key, slot);
2909 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2910 key.objectid != btrfs_ino(src))
2913 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2914 struct btrfs_file_extent_item *extent;
2917 struct btrfs_key new_key;
2918 u64 disko = 0, diskl = 0;
2919 u64 datao = 0, datal = 0;
2923 size = btrfs_item_size_nr(leaf, slot);
2924 read_extent_buffer(leaf, buf,
2925 btrfs_item_ptr_offset(leaf, slot),
2928 extent = btrfs_item_ptr(leaf, slot,
2929 struct btrfs_file_extent_item);
2930 comp = btrfs_file_extent_compression(leaf, extent);
2931 type = btrfs_file_extent_type(leaf, extent);
2932 if (type == BTRFS_FILE_EXTENT_REG ||
2933 type == BTRFS_FILE_EXTENT_PREALLOC) {
2934 disko = btrfs_file_extent_disk_bytenr(leaf,
2936 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2938 datao = btrfs_file_extent_offset(leaf, extent);
2939 datal = btrfs_file_extent_num_bytes(leaf,
2941 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2942 /* take upper bound, may be compressed */
2943 datal = btrfs_file_extent_ram_bytes(leaf,
2946 btrfs_release_path(path);
2948 if (key.offset + datal <= off ||
2949 key.offset >= off + len - 1)
2952 memcpy(&new_key, &key, sizeof(new_key));
2953 new_key.objectid = btrfs_ino(inode);
2954 if (off <= key.offset)
2955 new_key.offset = key.offset + destoff - off;
2957 new_key.offset = destoff;
2960 * 1 - adjusting old extent (we may have to split it)
2961 * 1 - add new extent
2964 trans = btrfs_start_transaction(root, 3);
2965 if (IS_ERR(trans)) {
2966 ret = PTR_ERR(trans);
2970 if (type == BTRFS_FILE_EXTENT_REG ||
2971 type == BTRFS_FILE_EXTENT_PREALLOC) {
2973 * a | --- range to clone ---| b
2974 * | ------------- extent ------------- |
2977 /* substract range b */
2978 if (key.offset + datal > off + len)
2979 datal = off + len - key.offset;
2981 /* substract range a */
2982 if (off > key.offset) {
2983 datao += off - key.offset;
2984 datal -= off - key.offset;
2987 ret = btrfs_drop_extents(trans, root, inode,
2989 new_key.offset + datal,
2992 btrfs_abort_transaction(trans, root,
2994 btrfs_end_transaction(trans, root);
2998 ret = btrfs_insert_empty_item(trans, root, path,
3001 btrfs_abort_transaction(trans, root,
3003 btrfs_end_transaction(trans, root);
3007 leaf = path->nodes[0];
3008 slot = path->slots[0];
3009 write_extent_buffer(leaf, buf,
3010 btrfs_item_ptr_offset(leaf, slot),
3013 extent = btrfs_item_ptr(leaf, slot,
3014 struct btrfs_file_extent_item);
3016 /* disko == 0 means it's a hole */
3020 btrfs_set_file_extent_offset(leaf, extent,
3022 btrfs_set_file_extent_num_bytes(leaf, extent,
3025 inode_add_bytes(inode, datal);
3026 ret = btrfs_inc_extent_ref(trans, root,
3028 root->root_key.objectid,
3030 new_key.offset - datao,
3033 btrfs_abort_transaction(trans,
3036 btrfs_end_transaction(trans,
3042 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3045 if (off > key.offset) {
3046 skip = off - key.offset;
3047 new_key.offset += skip;
3050 if (key.offset + datal > off + len)
3051 trim = key.offset + datal - (off + len);
3053 if (comp && (skip || trim)) {
3055 btrfs_end_transaction(trans, root);
3058 size -= skip + trim;
3059 datal -= skip + trim;
3061 ret = btrfs_drop_extents(trans, root, inode,
3063 new_key.offset + datal,
3066 btrfs_abort_transaction(trans, root,
3068 btrfs_end_transaction(trans, root);
3072 ret = btrfs_insert_empty_item(trans, root, path,
3075 btrfs_abort_transaction(trans, root,
3077 btrfs_end_transaction(trans, root);
3083 btrfs_file_extent_calc_inline_size(0);
3084 memmove(buf+start, buf+start+skip,
3088 leaf = path->nodes[0];
3089 slot = path->slots[0];
3090 write_extent_buffer(leaf, buf,
3091 btrfs_item_ptr_offset(leaf, slot),
3093 inode_add_bytes(inode, datal);
3096 btrfs_mark_buffer_dirty(leaf);
3097 btrfs_release_path(path);
3099 inode_inc_iversion(inode);
3100 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3103 * we round up to the block size at eof when
3104 * determining which extents to clone above,
3105 * but shouldn't round up the file size
3107 endoff = new_key.offset + datal;
3108 if (endoff > destoff+olen)
3109 endoff = destoff+olen;
3110 if (endoff > inode->i_size)
3111 btrfs_i_size_write(inode, endoff);
3113 ret = btrfs_update_inode(trans, root, inode);
3115 btrfs_abort_transaction(trans, root, ret);
3116 btrfs_end_transaction(trans, root);
3119 ret = btrfs_end_transaction(trans, root);
3122 btrfs_release_path(path);
3128 btrfs_release_path(path);
3129 btrfs_free_path(path);
3134 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3135 u64 off, u64 olen, u64 destoff)
3137 struct inode *inode = file_inode(file);
3138 struct btrfs_root *root = BTRFS_I(inode)->root;
3143 u64 bs = root->fs_info->sb->s_blocksize;
3148 * - split compressed inline extents. annoying: we need to
3149 * decompress into destination's address_space (the file offset
3150 * may change, so source mapping won't do), then recompress (or
3151 * otherwise reinsert) a subrange.
3152 * - allow ranges within the same file to be cloned (provided
3153 * they don't overlap)?
3156 /* the destination must be opened for writing */
3157 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3160 if (btrfs_root_readonly(root))
3163 ret = mnt_want_write_file(file);
3167 src_file = fdget(srcfd);
3168 if (!src_file.file) {
3170 goto out_drop_write;
3174 if (src_file.file->f_path.mnt != file->f_path.mnt)
3177 src = file_inode(src_file.file);
3183 /* the src must be open for reading */
3184 if (!(src_file.file->f_mode & FMODE_READ))
3187 /* don't make the dst file partly checksummed */
3188 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3189 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3193 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3197 if (src->i_sb != inode->i_sb)
3202 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3203 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3205 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3206 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3209 mutex_lock(&src->i_mutex);
3212 /* determine range to clone */
3214 if (off + len > src->i_size || off + len < off)
3217 olen = len = src->i_size - off;
3218 /* if we extend to eof, continue to block boundary */
3219 if (off + len == src->i_size)
3220 len = ALIGN(src->i_size, bs) - off;
3222 /* verify the end result is block aligned */
3223 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3224 !IS_ALIGNED(destoff, bs))
3227 /* verify if ranges are overlapped within the same file */
3229 if (destoff + len > off && destoff < off + len)
3233 if (destoff > inode->i_size) {
3234 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3239 /* truncate page cache pages from target inode range */
3240 truncate_inode_pages_range(&inode->i_data, destoff,
3241 PAGE_CACHE_ALIGN(destoff + len) - 1);
3243 lock_extent_range(src, off, len);
3245 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3247 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3249 mutex_unlock(&src->i_mutex);
3251 mutex_unlock(&inode->i_mutex);
3255 mnt_drop_write_file(file);
3259 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3261 struct btrfs_ioctl_clone_range_args args;
3263 if (copy_from_user(&args, argp, sizeof(args)))
3265 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3266 args.src_length, args.dest_offset);
3270 * there are many ways the trans_start and trans_end ioctls can lead
3271 * to deadlocks. They should only be used by applications that
3272 * basically own the machine, and have a very in depth understanding
3273 * of all the possible deadlocks and enospc problems.
3275 static long btrfs_ioctl_trans_start(struct file *file)
3277 struct inode *inode = file_inode(file);
3278 struct btrfs_root *root = BTRFS_I(inode)->root;
3279 struct btrfs_trans_handle *trans;
3283 if (!capable(CAP_SYS_ADMIN))
3287 if (file->private_data)
3291 if (btrfs_root_readonly(root))
3294 ret = mnt_want_write_file(file);
3298 atomic_inc(&root->fs_info->open_ioctl_trans);
3301 trans = btrfs_start_ioctl_transaction(root);
3305 file->private_data = trans;
3309 atomic_dec(&root->fs_info->open_ioctl_trans);
3310 mnt_drop_write_file(file);
3315 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3317 struct inode *inode = file_inode(file);
3318 struct btrfs_root *root = BTRFS_I(inode)->root;
3319 struct btrfs_root *new_root;
3320 struct btrfs_dir_item *di;
3321 struct btrfs_trans_handle *trans;
3322 struct btrfs_path *path;
3323 struct btrfs_key location;
3324 struct btrfs_disk_key disk_key;
3329 if (!capable(CAP_SYS_ADMIN))
3332 ret = mnt_want_write_file(file);
3336 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3342 objectid = BTRFS_FS_TREE_OBJECTID;
3344 location.objectid = objectid;
3345 location.type = BTRFS_ROOT_ITEM_KEY;
3346 location.offset = (u64)-1;
3348 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
3349 if (IS_ERR(new_root)) {
3350 ret = PTR_ERR(new_root);
3354 path = btrfs_alloc_path();
3359 path->leave_spinning = 1;
3361 trans = btrfs_start_transaction(root, 1);
3362 if (IS_ERR(trans)) {
3363 btrfs_free_path(path);
3364 ret = PTR_ERR(trans);
3368 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
3369 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3370 dir_id, "default", 7, 1);
3371 if (IS_ERR_OR_NULL(di)) {
3372 btrfs_free_path(path);
3373 btrfs_end_transaction(trans, root);
3374 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3375 "item, this isn't going to work");
3380 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3381 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3382 btrfs_mark_buffer_dirty(path->nodes[0]);
3383 btrfs_free_path(path);
3385 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
3386 btrfs_end_transaction(trans, root);
3388 mnt_drop_write_file(file);
3392 void btrfs_get_block_group_info(struct list_head *groups_list,
3393 struct btrfs_ioctl_space_info *space)
3395 struct btrfs_block_group_cache *block_group;
3397 space->total_bytes = 0;
3398 space->used_bytes = 0;
3400 list_for_each_entry(block_group, groups_list, list) {
3401 space->flags = block_group->flags;
3402 space->total_bytes += block_group->key.offset;
3403 space->used_bytes +=
3404 btrfs_block_group_used(&block_group->item);
3408 static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
3410 struct btrfs_ioctl_space_args space_args;
3411 struct btrfs_ioctl_space_info space;
3412 struct btrfs_ioctl_space_info *dest;
3413 struct btrfs_ioctl_space_info *dest_orig;
3414 struct btrfs_ioctl_space_info __user *user_dest;
3415 struct btrfs_space_info *info;
3416 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3417 BTRFS_BLOCK_GROUP_SYSTEM,
3418 BTRFS_BLOCK_GROUP_METADATA,
3419 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3426 if (copy_from_user(&space_args,
3427 (struct btrfs_ioctl_space_args __user *)arg,
3428 sizeof(space_args)))
3431 for (i = 0; i < num_types; i++) {
3432 struct btrfs_space_info *tmp;
3436 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3438 if (tmp->flags == types[i]) {
3448 down_read(&info->groups_sem);
3449 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3450 if (!list_empty(&info->block_groups[c]))
3453 up_read(&info->groups_sem);
3456 /* space_slots == 0 means they are asking for a count */
3457 if (space_args.space_slots == 0) {
3458 space_args.total_spaces = slot_count;
3462 slot_count = min_t(u64, space_args.space_slots, slot_count);
3464 alloc_size = sizeof(*dest) * slot_count;
3466 /* we generally have at most 6 or so space infos, one for each raid
3467 * level. So, a whole page should be more than enough for everyone
3469 if (alloc_size > PAGE_CACHE_SIZE)
3472 space_args.total_spaces = 0;
3473 dest = kmalloc(alloc_size, GFP_NOFS);
3478 /* now we have a buffer to copy into */
3479 for (i = 0; i < num_types; i++) {
3480 struct btrfs_space_info *tmp;
3487 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3489 if (tmp->flags == types[i]) {
3498 down_read(&info->groups_sem);
3499 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3500 if (!list_empty(&info->block_groups[c])) {
3501 btrfs_get_block_group_info(
3502 &info->block_groups[c], &space);
3503 memcpy(dest, &space, sizeof(space));
3505 space_args.total_spaces++;
3511 up_read(&info->groups_sem);
3514 user_dest = (struct btrfs_ioctl_space_info __user *)
3515 (arg + sizeof(struct btrfs_ioctl_space_args));
3517 if (copy_to_user(user_dest, dest_orig, alloc_size))
3522 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3528 static long btrfs_ioctl_global_rsv(struct btrfs_root *root, void __user *arg)
3530 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
3533 spin_lock(&block_rsv->lock);
3534 reserved = block_rsv->reserved;
3535 spin_unlock(&block_rsv->lock);
3537 if (arg && copy_to_user(arg, &reserved, sizeof(reserved)))
3543 * there are many ways the trans_start and trans_end ioctls can lead
3544 * to deadlocks. They should only be used by applications that
3545 * basically own the machine, and have a very in depth understanding
3546 * of all the possible deadlocks and enospc problems.
3548 long btrfs_ioctl_trans_end(struct file *file)
3550 struct inode *inode = file_inode(file);
3551 struct btrfs_root *root = BTRFS_I(inode)->root;
3552 struct btrfs_trans_handle *trans;
3554 trans = file->private_data;
3557 file->private_data = NULL;
3559 btrfs_end_transaction(trans, root);
3561 atomic_dec(&root->fs_info->open_ioctl_trans);
3563 mnt_drop_write_file(file);
3567 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3570 struct btrfs_trans_handle *trans;
3574 trans = btrfs_attach_transaction_barrier(root);
3575 if (IS_ERR(trans)) {
3576 if (PTR_ERR(trans) != -ENOENT)
3577 return PTR_ERR(trans);
3579 /* No running transaction, don't bother */
3580 transid = root->fs_info->last_trans_committed;
3583 transid = trans->transid;
3584 ret = btrfs_commit_transaction_async(trans, root, 0);
3586 btrfs_end_transaction(trans, root);
3591 if (copy_to_user(argp, &transid, sizeof(transid)))
3596 static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3602 if (copy_from_user(&transid, argp, sizeof(transid)))
3605 transid = 0; /* current trans */
3607 return btrfs_wait_for_commit(root, transid);
3610 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3612 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3613 struct btrfs_ioctl_scrub_args *sa;
3616 if (!capable(CAP_SYS_ADMIN))
3619 sa = memdup_user(arg, sizeof(*sa));
3623 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3624 ret = mnt_want_write_file(file);
3629 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
3630 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3633 if (copy_to_user(arg, sa, sizeof(*sa)))
3636 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3637 mnt_drop_write_file(file);
3643 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3645 if (!capable(CAP_SYS_ADMIN))
3648 return btrfs_scrub_cancel(root->fs_info);
3651 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3654 struct btrfs_ioctl_scrub_args *sa;
3657 if (!capable(CAP_SYS_ADMIN))
3660 sa = memdup_user(arg, sizeof(*sa));
3664 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3666 if (copy_to_user(arg, sa, sizeof(*sa)))
3673 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3676 struct btrfs_ioctl_get_dev_stats *sa;
3679 sa = memdup_user(arg, sizeof(*sa));
3683 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3688 ret = btrfs_get_dev_stats(root, sa);
3690 if (copy_to_user(arg, sa, sizeof(*sa)))
3697 static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3699 struct btrfs_ioctl_dev_replace_args *p;
3702 if (!capable(CAP_SYS_ADMIN))
3705 p = memdup_user(arg, sizeof(*p));
3710 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3711 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3716 &root->fs_info->mutually_exclusive_operation_running,
3718 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3720 ret = btrfs_dev_replace_start(root, p);
3722 &root->fs_info->mutually_exclusive_operation_running,
3726 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3727 btrfs_dev_replace_status(root->fs_info, p);
3730 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3731 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3738 if (copy_to_user(arg, p, sizeof(*p)))
3745 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3751 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3752 struct inode_fs_paths *ipath = NULL;
3753 struct btrfs_path *path;
3755 if (!capable(CAP_DAC_READ_SEARCH))
3758 path = btrfs_alloc_path();
3764 ipa = memdup_user(arg, sizeof(*ipa));
3771 size = min_t(u32, ipa->size, 4096);
3772 ipath = init_ipath(size, root, path);
3773 if (IS_ERR(ipath)) {
3774 ret = PTR_ERR(ipath);
3779 ret = paths_from_inode(ipa->inum, ipath);
3783 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3784 rel_ptr = ipath->fspath->val[i] -
3785 (u64)(unsigned long)ipath->fspath->val;
3786 ipath->fspath->val[i] = rel_ptr;
3789 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3790 (void *)(unsigned long)ipath->fspath, size);
3797 btrfs_free_path(path);
3804 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3806 struct btrfs_data_container *inodes = ctx;
3807 const size_t c = 3 * sizeof(u64);
3809 if (inodes->bytes_left >= c) {
3810 inodes->bytes_left -= c;
3811 inodes->val[inodes->elem_cnt] = inum;
3812 inodes->val[inodes->elem_cnt + 1] = offset;
3813 inodes->val[inodes->elem_cnt + 2] = root;
3814 inodes->elem_cnt += 3;
3816 inodes->bytes_missing += c - inodes->bytes_left;
3817 inodes->bytes_left = 0;
3818 inodes->elem_missed += 3;
3824 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3829 struct btrfs_ioctl_logical_ino_args *loi;
3830 struct btrfs_data_container *inodes = NULL;
3831 struct btrfs_path *path = NULL;
3833 if (!capable(CAP_SYS_ADMIN))
3836 loi = memdup_user(arg, sizeof(*loi));
3843 path = btrfs_alloc_path();
3849 size = min_t(u32, loi->size, 64 * 1024);
3850 inodes = init_data_container(size);
3851 if (IS_ERR(inodes)) {
3852 ret = PTR_ERR(inodes);
3857 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3858 build_ino_list, inodes);
3864 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3865 (void *)(unsigned long)inodes, size);
3870 btrfs_free_path(path);
3877 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3878 struct btrfs_ioctl_balance_args *bargs)
3880 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3882 bargs->flags = bctl->flags;
3884 if (atomic_read(&fs_info->balance_running))
3885 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3886 if (atomic_read(&fs_info->balance_pause_req))
3887 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3888 if (atomic_read(&fs_info->balance_cancel_req))
3889 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3891 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3892 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3893 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3896 spin_lock(&fs_info->balance_lock);
3897 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3898 spin_unlock(&fs_info->balance_lock);
3900 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3904 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3906 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3907 struct btrfs_fs_info *fs_info = root->fs_info;
3908 struct btrfs_ioctl_balance_args *bargs;
3909 struct btrfs_balance_control *bctl;
3910 bool need_unlock; /* for mut. excl. ops lock */
3913 if (!capable(CAP_SYS_ADMIN))
3916 ret = mnt_want_write_file(file);
3921 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3922 mutex_lock(&fs_info->volume_mutex);
3923 mutex_lock(&fs_info->balance_mutex);
3929 * mut. excl. ops lock is locked. Three possibilites:
3930 * (1) some other op is running
3931 * (2) balance is running
3932 * (3) balance is paused -- special case (think resume)
3934 mutex_lock(&fs_info->balance_mutex);
3935 if (fs_info->balance_ctl) {
3936 /* this is either (2) or (3) */
3937 if (!atomic_read(&fs_info->balance_running)) {
3938 mutex_unlock(&fs_info->balance_mutex);
3939 if (!mutex_trylock(&fs_info->volume_mutex))
3941 mutex_lock(&fs_info->balance_mutex);
3943 if (fs_info->balance_ctl &&
3944 !atomic_read(&fs_info->balance_running)) {
3946 need_unlock = false;
3950 mutex_unlock(&fs_info->balance_mutex);
3951 mutex_unlock(&fs_info->volume_mutex);
3955 mutex_unlock(&fs_info->balance_mutex);
3961 mutex_unlock(&fs_info->balance_mutex);
3962 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3967 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3970 bargs = memdup_user(arg, sizeof(*bargs));
3971 if (IS_ERR(bargs)) {
3972 ret = PTR_ERR(bargs);
3976 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3977 if (!fs_info->balance_ctl) {
3982 bctl = fs_info->balance_ctl;
3983 spin_lock(&fs_info->balance_lock);
3984 bctl->flags |= BTRFS_BALANCE_RESUME;
3985 spin_unlock(&fs_info->balance_lock);
3993 if (fs_info->balance_ctl) {
3998 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4004 bctl->fs_info = fs_info;
4006 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4007 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4008 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4010 bctl->flags = bargs->flags;
4012 /* balance everything - no filters */
4013 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4018 * Ownership of bctl and mutually_exclusive_operation_running
4019 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4020 * or, if restriper was paused all the way until unmount, in
4021 * free_fs_info. mutually_exclusive_operation_running is
4022 * cleared in __cancel_balance.
4024 need_unlock = false;
4026 ret = btrfs_balance(bctl, bargs);
4029 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4036 mutex_unlock(&fs_info->balance_mutex);
4037 mutex_unlock(&fs_info->volume_mutex);
4039 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4041 mnt_drop_write_file(file);
4045 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4047 if (!capable(CAP_SYS_ADMIN))
4051 case BTRFS_BALANCE_CTL_PAUSE:
4052 return btrfs_pause_balance(root->fs_info);
4053 case BTRFS_BALANCE_CTL_CANCEL:
4054 return btrfs_cancel_balance(root->fs_info);
4060 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4063 struct btrfs_fs_info *fs_info = root->fs_info;
4064 struct btrfs_ioctl_balance_args *bargs;
4067 if (!capable(CAP_SYS_ADMIN))
4070 mutex_lock(&fs_info->balance_mutex);
4071 if (!fs_info->balance_ctl) {
4076 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4082 update_ioctl_balance_args(fs_info, 1, bargs);
4084 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4089 mutex_unlock(&fs_info->balance_mutex);
4093 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4095 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4096 struct btrfs_ioctl_quota_ctl_args *sa;
4097 struct btrfs_trans_handle *trans = NULL;
4101 if (!capable(CAP_SYS_ADMIN))
4104 ret = mnt_want_write_file(file);
4108 sa = memdup_user(arg, sizeof(*sa));
4114 down_write(&root->fs_info->subvol_sem);
4115 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4116 if (IS_ERR(trans)) {
4117 ret = PTR_ERR(trans);
4122 case BTRFS_QUOTA_CTL_ENABLE:
4123 ret = btrfs_quota_enable(trans, root->fs_info);
4125 case BTRFS_QUOTA_CTL_DISABLE:
4126 ret = btrfs_quota_disable(trans, root->fs_info);
4133 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4138 up_write(&root->fs_info->subvol_sem);
4140 mnt_drop_write_file(file);
4144 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4146 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4147 struct btrfs_ioctl_qgroup_assign_args *sa;
4148 struct btrfs_trans_handle *trans;
4152 if (!capable(CAP_SYS_ADMIN))
4155 ret = mnt_want_write_file(file);
4159 sa = memdup_user(arg, sizeof(*sa));
4165 trans = btrfs_join_transaction(root);
4166 if (IS_ERR(trans)) {
4167 ret = PTR_ERR(trans);
4171 /* FIXME: check if the IDs really exist */
4173 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4176 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4180 err = btrfs_end_transaction(trans, root);
4187 mnt_drop_write_file(file);
4191 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4193 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4194 struct btrfs_ioctl_qgroup_create_args *sa;
4195 struct btrfs_trans_handle *trans;
4199 if (!capable(CAP_SYS_ADMIN))
4202 ret = mnt_want_write_file(file);
4206 sa = memdup_user(arg, sizeof(*sa));
4212 if (!sa->qgroupid) {
4217 trans = btrfs_join_transaction(root);
4218 if (IS_ERR(trans)) {
4219 ret = PTR_ERR(trans);
4223 /* FIXME: check if the IDs really exist */
4225 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4228 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4231 err = btrfs_end_transaction(trans, root);
4238 mnt_drop_write_file(file);
4242 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4244 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4245 struct btrfs_ioctl_qgroup_limit_args *sa;
4246 struct btrfs_trans_handle *trans;
4251 if (!capable(CAP_SYS_ADMIN))
4254 ret = mnt_want_write_file(file);
4258 sa = memdup_user(arg, sizeof(*sa));
4264 trans = btrfs_join_transaction(root);
4265 if (IS_ERR(trans)) {
4266 ret = PTR_ERR(trans);
4270 qgroupid = sa->qgroupid;
4272 /* take the current subvol as qgroup */
4273 qgroupid = root->root_key.objectid;
4276 /* FIXME: check if the IDs really exist */
4277 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4279 err = btrfs_end_transaction(trans, root);
4286 mnt_drop_write_file(file);
4290 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4292 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4293 struct btrfs_ioctl_quota_rescan_args *qsa;
4296 if (!capable(CAP_SYS_ADMIN))
4299 ret = mnt_want_write_file(file);
4303 qsa = memdup_user(arg, sizeof(*qsa));
4314 ret = btrfs_qgroup_rescan(root->fs_info);
4319 mnt_drop_write_file(file);
4323 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4325 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4326 struct btrfs_ioctl_quota_rescan_args *qsa;
4329 if (!capable(CAP_SYS_ADMIN))
4332 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4336 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4338 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4341 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4348 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4350 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4352 if (!capable(CAP_SYS_ADMIN))
4355 return btrfs_qgroup_wait_for_completion(root->fs_info);
4358 static long btrfs_ioctl_set_received_subvol(struct file *file,
4361 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4362 struct inode *inode = file_inode(file);
4363 struct btrfs_root *root = BTRFS_I(inode)->root;
4364 struct btrfs_root_item *root_item = &root->root_item;
4365 struct btrfs_trans_handle *trans;
4366 struct timespec ct = CURRENT_TIME;
4368 int received_uuid_changed;
4370 ret = mnt_want_write_file(file);
4374 down_write(&root->fs_info->subvol_sem);
4376 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4381 if (btrfs_root_readonly(root)) {
4386 if (!inode_owner_or_capable(inode)) {
4391 sa = memdup_user(arg, sizeof(*sa));
4400 * 2 - uuid items (received uuid + subvol uuid)
4402 trans = btrfs_start_transaction(root, 3);
4403 if (IS_ERR(trans)) {
4404 ret = PTR_ERR(trans);
4409 sa->rtransid = trans->transid;
4410 sa->rtime.sec = ct.tv_sec;
4411 sa->rtime.nsec = ct.tv_nsec;
4413 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4415 if (received_uuid_changed &&
4416 !btrfs_is_empty_uuid(root_item->received_uuid))
4417 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4418 root_item->received_uuid,
4419 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4420 root->root_key.objectid);
4421 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4422 btrfs_set_root_stransid(root_item, sa->stransid);
4423 btrfs_set_root_rtransid(root_item, sa->rtransid);
4424 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4425 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4426 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4427 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4429 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4430 &root->root_key, &root->root_item);
4432 btrfs_end_transaction(trans, root);
4435 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4436 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4438 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4439 root->root_key.objectid);
4440 if (ret < 0 && ret != -EEXIST) {
4441 btrfs_abort_transaction(trans, root, ret);
4445 ret = btrfs_commit_transaction(trans, root);
4447 btrfs_abort_transaction(trans, root, ret);
4451 ret = copy_to_user(arg, sa, sizeof(*sa));
4457 up_write(&root->fs_info->subvol_sem);
4458 mnt_drop_write_file(file);
4462 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4464 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4467 char label[BTRFS_LABEL_SIZE];
4469 spin_lock(&root->fs_info->super_lock);
4470 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4471 spin_unlock(&root->fs_info->super_lock);
4473 len = strnlen(label, BTRFS_LABEL_SIZE);
4475 if (len == BTRFS_LABEL_SIZE) {
4476 btrfs_warn(root->fs_info,
4477 "label is too long, return the first %zu bytes", --len);
4480 ret = copy_to_user(arg, label, len);
4482 return ret ? -EFAULT : 0;
4485 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4487 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4488 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4489 struct btrfs_trans_handle *trans;
4490 char label[BTRFS_LABEL_SIZE];
4493 if (!capable(CAP_SYS_ADMIN))
4496 if (copy_from_user(label, arg, sizeof(label)))
4499 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4500 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
4501 BTRFS_LABEL_SIZE - 1);
4505 ret = mnt_want_write_file(file);
4509 trans = btrfs_start_transaction(root, 0);
4510 if (IS_ERR(trans)) {
4511 ret = PTR_ERR(trans);
4515 spin_lock(&root->fs_info->super_lock);
4516 strcpy(super_block->label, label);
4517 spin_unlock(&root->fs_info->super_lock);
4518 ret = btrfs_end_transaction(trans, root);
4521 mnt_drop_write_file(file);
4525 #define INIT_FEATURE_FLAGS(suffix) \
4526 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4527 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4528 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4530 static int btrfs_ioctl_get_supported_features(struct file *file,
4533 static struct btrfs_ioctl_feature_flags features[3] = {
4534 INIT_FEATURE_FLAGS(SUPP),
4535 INIT_FEATURE_FLAGS(SAFE_SET),
4536 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4539 if (copy_to_user(arg, &features, sizeof(features)))
4545 static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
4547 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4548 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4549 struct btrfs_ioctl_feature_flags features;
4551 features.compat_flags = btrfs_super_compat_flags(super_block);
4552 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4553 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4555 if (copy_to_user(arg, &features, sizeof(features)))
4561 static int check_feature_bits(struct btrfs_root *root,
4562 enum btrfs_feature_set set,
4563 u64 change_mask, u64 flags, u64 supported_flags,
4564 u64 safe_set, u64 safe_clear)
4566 const char *type = btrfs_feature_set_names[set];
4568 u64 disallowed, unsupported;
4569 u64 set_mask = flags & change_mask;
4570 u64 clear_mask = ~flags & change_mask;
4572 unsupported = set_mask & ~supported_flags;
4574 names = btrfs_printable_features(set, unsupported);
4576 btrfs_warn(root->fs_info,
4577 "this kernel does not support the %s feature bit%s",
4578 names, strchr(names, ',') ? "s" : "");
4581 btrfs_warn(root->fs_info,
4582 "this kernel does not support %s bits 0x%llx",
4587 disallowed = set_mask & ~safe_set;
4589 names = btrfs_printable_features(set, disallowed);
4591 btrfs_warn(root->fs_info,
4592 "can't set the %s feature bit%s while mounted",
4593 names, strchr(names, ',') ? "s" : "");
4596 btrfs_warn(root->fs_info,
4597 "can't set %s bits 0x%llx while mounted",
4602 disallowed = clear_mask & ~safe_clear;
4604 names = btrfs_printable_features(set, disallowed);
4606 btrfs_warn(root->fs_info,
4607 "can't clear the %s feature bit%s while mounted",
4608 names, strchr(names, ',') ? "s" : "");
4611 btrfs_warn(root->fs_info,
4612 "can't clear %s bits 0x%llx while mounted",
4620 #define check_feature(root, change_mask, flags, mask_base) \
4621 check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
4622 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4623 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4624 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4626 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4628 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4629 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4630 struct btrfs_ioctl_feature_flags flags[2];
4631 struct btrfs_trans_handle *trans;
4635 if (!capable(CAP_SYS_ADMIN))
4638 if (copy_from_user(flags, arg, sizeof(flags)))
4642 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4643 !flags[0].incompat_flags)
4646 ret = check_feature(root, flags[0].compat_flags,
4647 flags[1].compat_flags, COMPAT);
4651 ret = check_feature(root, flags[0].compat_ro_flags,
4652 flags[1].compat_ro_flags, COMPAT_RO);
4656 ret = check_feature(root, flags[0].incompat_flags,
4657 flags[1].incompat_flags, INCOMPAT);
4661 trans = btrfs_start_transaction(root, 1);
4663 return PTR_ERR(trans);
4665 spin_lock(&root->fs_info->super_lock);
4666 newflags = btrfs_super_compat_flags(super_block);
4667 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4668 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4669 btrfs_set_super_compat_flags(super_block, newflags);
4671 newflags = btrfs_super_compat_ro_flags(super_block);
4672 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4673 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4674 btrfs_set_super_compat_ro_flags(super_block, newflags);
4676 newflags = btrfs_super_incompat_flags(super_block);
4677 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4678 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4679 btrfs_set_super_incompat_flags(super_block, newflags);
4680 spin_unlock(&root->fs_info->super_lock);
4682 return btrfs_end_transaction(trans, root);
4685 long btrfs_ioctl(struct file *file, unsigned int
4686 cmd, unsigned long arg)
4688 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4689 void __user *argp = (void __user *)arg;
4692 case FS_IOC_GETFLAGS:
4693 return btrfs_ioctl_getflags(file, argp);
4694 case FS_IOC_SETFLAGS:
4695 return btrfs_ioctl_setflags(file, argp);
4696 case FS_IOC_GETVERSION:
4697 return btrfs_ioctl_getversion(file, argp);
4699 return btrfs_ioctl_fitrim(file, argp);
4700 case BTRFS_IOC_SNAP_CREATE:
4701 return btrfs_ioctl_snap_create(file, argp, 0);
4702 case BTRFS_IOC_SNAP_CREATE_V2:
4703 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4704 case BTRFS_IOC_SUBVOL_CREATE:
4705 return btrfs_ioctl_snap_create(file, argp, 1);
4706 case BTRFS_IOC_SUBVOL_CREATE_V2:
4707 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4708 case BTRFS_IOC_SNAP_DESTROY:
4709 return btrfs_ioctl_snap_destroy(file, argp);
4710 case BTRFS_IOC_SUBVOL_GETFLAGS:
4711 return btrfs_ioctl_subvol_getflags(file, argp);
4712 case BTRFS_IOC_SUBVOL_SETFLAGS:
4713 return btrfs_ioctl_subvol_setflags(file, argp);
4714 case BTRFS_IOC_DEFAULT_SUBVOL:
4715 return btrfs_ioctl_default_subvol(file, argp);
4716 case BTRFS_IOC_DEFRAG:
4717 return btrfs_ioctl_defrag(file, NULL);
4718 case BTRFS_IOC_DEFRAG_RANGE:
4719 return btrfs_ioctl_defrag(file, argp);
4720 case BTRFS_IOC_RESIZE:
4721 return btrfs_ioctl_resize(file, argp);
4722 case BTRFS_IOC_ADD_DEV:
4723 return btrfs_ioctl_add_dev(root, argp);
4724 case BTRFS_IOC_RM_DEV:
4725 return btrfs_ioctl_rm_dev(file, argp);
4726 case BTRFS_IOC_FS_INFO:
4727 return btrfs_ioctl_fs_info(root, argp);
4728 case BTRFS_IOC_DEV_INFO:
4729 return btrfs_ioctl_dev_info(root, argp);
4730 case BTRFS_IOC_BALANCE:
4731 return btrfs_ioctl_balance(file, NULL);
4732 case BTRFS_IOC_CLONE:
4733 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4734 case BTRFS_IOC_CLONE_RANGE:
4735 return btrfs_ioctl_clone_range(file, argp);
4736 case BTRFS_IOC_TRANS_START:
4737 return btrfs_ioctl_trans_start(file);
4738 case BTRFS_IOC_TRANS_END:
4739 return btrfs_ioctl_trans_end(file);
4740 case BTRFS_IOC_TREE_SEARCH:
4741 return btrfs_ioctl_tree_search(file, argp);
4742 case BTRFS_IOC_INO_LOOKUP:
4743 return btrfs_ioctl_ino_lookup(file, argp);
4744 case BTRFS_IOC_INO_PATHS:
4745 return btrfs_ioctl_ino_to_path(root, argp);
4746 case BTRFS_IOC_LOGICAL_INO:
4747 return btrfs_ioctl_logical_to_ino(root, argp);
4748 case BTRFS_IOC_SPACE_INFO:
4749 return btrfs_ioctl_space_info(root, argp);
4750 case BTRFS_IOC_GLOBAL_RSV:
4751 return btrfs_ioctl_global_rsv(root, argp);
4752 case BTRFS_IOC_SYNC: {
4755 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
4758 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4761 case BTRFS_IOC_START_SYNC:
4762 return btrfs_ioctl_start_sync(root, argp);
4763 case BTRFS_IOC_WAIT_SYNC:
4764 return btrfs_ioctl_wait_sync(root, argp);
4765 case BTRFS_IOC_SCRUB:
4766 return btrfs_ioctl_scrub(file, argp);
4767 case BTRFS_IOC_SCRUB_CANCEL:
4768 return btrfs_ioctl_scrub_cancel(root, argp);
4769 case BTRFS_IOC_SCRUB_PROGRESS:
4770 return btrfs_ioctl_scrub_progress(root, argp);
4771 case BTRFS_IOC_BALANCE_V2:
4772 return btrfs_ioctl_balance(file, argp);
4773 case BTRFS_IOC_BALANCE_CTL:
4774 return btrfs_ioctl_balance_ctl(root, arg);
4775 case BTRFS_IOC_BALANCE_PROGRESS:
4776 return btrfs_ioctl_balance_progress(root, argp);
4777 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4778 return btrfs_ioctl_set_received_subvol(file, argp);
4779 case BTRFS_IOC_SEND:
4780 return btrfs_ioctl_send(file, argp);
4781 case BTRFS_IOC_GET_DEV_STATS:
4782 return btrfs_ioctl_get_dev_stats(root, argp);
4783 case BTRFS_IOC_QUOTA_CTL:
4784 return btrfs_ioctl_quota_ctl(file, argp);
4785 case BTRFS_IOC_QGROUP_ASSIGN:
4786 return btrfs_ioctl_qgroup_assign(file, argp);
4787 case BTRFS_IOC_QGROUP_CREATE:
4788 return btrfs_ioctl_qgroup_create(file, argp);
4789 case BTRFS_IOC_QGROUP_LIMIT:
4790 return btrfs_ioctl_qgroup_limit(file, argp);
4791 case BTRFS_IOC_QUOTA_RESCAN:
4792 return btrfs_ioctl_quota_rescan(file, argp);
4793 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4794 return btrfs_ioctl_quota_rescan_status(file, argp);
4795 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4796 return btrfs_ioctl_quota_rescan_wait(file, argp);
4797 case BTRFS_IOC_DEV_REPLACE:
4798 return btrfs_ioctl_dev_replace(root, argp);
4799 case BTRFS_IOC_GET_FSLABEL:
4800 return btrfs_ioctl_get_fslabel(file, argp);
4801 case BTRFS_IOC_SET_FSLABEL:
4802 return btrfs_ioctl_set_fslabel(file, argp);
4803 case BTRFS_IOC_FILE_EXTENT_SAME:
4804 return btrfs_ioctl_file_extent_same(file, argp);
4805 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
4806 return btrfs_ioctl_get_supported_features(file, argp);
4807 case BTRFS_IOC_GET_FEATURES:
4808 return btrfs_ioctl_get_features(file, argp);
4809 case BTRFS_IOC_SET_FEATURES:
4810 return btrfs_ioctl_set_features(file, argp);