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"
62 static int btrfs_clone(struct inode *src, struct inode *inode,
63 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
65 /* Mask out flags that are inappropriate for the given type of inode. */
66 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
70 else if (S_ISREG(mode))
71 return flags & ~FS_DIRSYNC_FL;
73 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
77 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
79 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
81 unsigned int iflags = 0;
83 if (flags & BTRFS_INODE_SYNC)
85 if (flags & BTRFS_INODE_IMMUTABLE)
86 iflags |= FS_IMMUTABLE_FL;
87 if (flags & BTRFS_INODE_APPEND)
88 iflags |= FS_APPEND_FL;
89 if (flags & BTRFS_INODE_NODUMP)
90 iflags |= FS_NODUMP_FL;
91 if (flags & BTRFS_INODE_NOATIME)
92 iflags |= FS_NOATIME_FL;
93 if (flags & BTRFS_INODE_DIRSYNC)
94 iflags |= FS_DIRSYNC_FL;
95 if (flags & BTRFS_INODE_NODATACOW)
96 iflags |= FS_NOCOW_FL;
98 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
99 iflags |= FS_COMPR_FL;
100 else if (flags & BTRFS_INODE_NOCOMPRESS)
101 iflags |= FS_NOCOMP_FL;
107 * Update inode->i_flags based on the btrfs internal flags.
109 void btrfs_update_iflags(struct inode *inode)
111 struct btrfs_inode *ip = BTRFS_I(inode);
113 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
115 if (ip->flags & BTRFS_INODE_SYNC)
116 inode->i_flags |= S_SYNC;
117 if (ip->flags & BTRFS_INODE_IMMUTABLE)
118 inode->i_flags |= S_IMMUTABLE;
119 if (ip->flags & BTRFS_INODE_APPEND)
120 inode->i_flags |= S_APPEND;
121 if (ip->flags & BTRFS_INODE_NOATIME)
122 inode->i_flags |= S_NOATIME;
123 if (ip->flags & BTRFS_INODE_DIRSYNC)
124 inode->i_flags |= S_DIRSYNC;
128 * Inherit flags from the parent inode.
130 * Currently only the compression flags and the cow flags are inherited.
132 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
139 flags = BTRFS_I(dir)->flags;
141 if (flags & BTRFS_INODE_NOCOMPRESS) {
142 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
143 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
144 } else if (flags & BTRFS_INODE_COMPRESS) {
145 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
146 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
149 if (flags & BTRFS_INODE_NODATACOW) {
150 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
151 if (S_ISREG(inode->i_mode))
152 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
155 btrfs_update_iflags(inode);
158 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
160 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
161 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
163 if (copy_to_user(arg, &flags, sizeof(flags)))
168 static int check_flags(unsigned int flags)
170 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
171 FS_NOATIME_FL | FS_NODUMP_FL | \
172 FS_SYNC_FL | FS_DIRSYNC_FL | \
173 FS_NOCOMP_FL | FS_COMPR_FL |
177 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
183 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
185 struct inode *inode = file_inode(file);
186 struct btrfs_inode *ip = BTRFS_I(inode);
187 struct btrfs_root *root = ip->root;
188 struct btrfs_trans_handle *trans;
189 unsigned int flags, oldflags;
192 unsigned int i_oldflags;
195 if (!inode_owner_or_capable(inode))
198 if (btrfs_root_readonly(root))
201 if (copy_from_user(&flags, arg, sizeof(flags)))
204 ret = check_flags(flags);
208 ret = mnt_want_write_file(file);
212 mutex_lock(&inode->i_mutex);
214 ip_oldflags = ip->flags;
215 i_oldflags = inode->i_flags;
216 mode = inode->i_mode;
218 flags = btrfs_mask_flags(inode->i_mode, flags);
219 oldflags = btrfs_flags_to_ioctl(ip->flags);
220 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
221 if (!capable(CAP_LINUX_IMMUTABLE)) {
227 if (flags & FS_SYNC_FL)
228 ip->flags |= BTRFS_INODE_SYNC;
230 ip->flags &= ~BTRFS_INODE_SYNC;
231 if (flags & FS_IMMUTABLE_FL)
232 ip->flags |= BTRFS_INODE_IMMUTABLE;
234 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
235 if (flags & FS_APPEND_FL)
236 ip->flags |= BTRFS_INODE_APPEND;
238 ip->flags &= ~BTRFS_INODE_APPEND;
239 if (flags & FS_NODUMP_FL)
240 ip->flags |= BTRFS_INODE_NODUMP;
242 ip->flags &= ~BTRFS_INODE_NODUMP;
243 if (flags & FS_NOATIME_FL)
244 ip->flags |= BTRFS_INODE_NOATIME;
246 ip->flags &= ~BTRFS_INODE_NOATIME;
247 if (flags & FS_DIRSYNC_FL)
248 ip->flags |= BTRFS_INODE_DIRSYNC;
250 ip->flags &= ~BTRFS_INODE_DIRSYNC;
251 if (flags & FS_NOCOW_FL) {
254 * It's safe to turn csums off here, no extents exist.
255 * Otherwise we want the flag to reflect the real COW
256 * status of the file and will not set it.
258 if (inode->i_size == 0)
259 ip->flags |= BTRFS_INODE_NODATACOW
260 | BTRFS_INODE_NODATASUM;
262 ip->flags |= BTRFS_INODE_NODATACOW;
266 * Revert back under same assuptions as above
269 if (inode->i_size == 0)
270 ip->flags &= ~(BTRFS_INODE_NODATACOW
271 | BTRFS_INODE_NODATASUM);
273 ip->flags &= ~BTRFS_INODE_NODATACOW;
278 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
279 * flag may be changed automatically if compression code won't make
282 if (flags & FS_NOCOMP_FL) {
283 ip->flags &= ~BTRFS_INODE_COMPRESS;
284 ip->flags |= BTRFS_INODE_NOCOMPRESS;
286 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
287 if (ret && ret != -ENODATA)
289 } else if (flags & FS_COMPR_FL) {
292 ip->flags |= BTRFS_INODE_COMPRESS;
293 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
295 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
299 ret = btrfs_set_prop(inode, "btrfs.compression",
300 comp, strlen(comp), 0);
305 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
306 if (ret && ret != -ENODATA)
308 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
311 trans = btrfs_start_transaction(root, 1);
313 ret = PTR_ERR(trans);
317 btrfs_update_iflags(inode);
318 inode_inc_iversion(inode);
319 inode->i_ctime = CURRENT_TIME;
320 ret = btrfs_update_inode(trans, root, inode);
322 btrfs_end_transaction(trans, root);
325 ip->flags = ip_oldflags;
326 inode->i_flags = i_oldflags;
330 mutex_unlock(&inode->i_mutex);
331 mnt_drop_write_file(file);
335 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
337 struct inode *inode = file_inode(file);
339 return put_user(inode->i_generation, arg);
342 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
344 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
345 struct btrfs_device *device;
346 struct request_queue *q;
347 struct fstrim_range range;
348 u64 minlen = ULLONG_MAX;
350 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
353 if (!capable(CAP_SYS_ADMIN))
357 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
361 q = bdev_get_queue(device->bdev);
362 if (blk_queue_discard(q)) {
364 minlen = min((u64)q->limits.discard_granularity,
372 if (copy_from_user(&range, arg, sizeof(range)))
374 if (range.start > total_bytes ||
375 range.len < fs_info->sb->s_blocksize)
378 range.len = min(range.len, total_bytes - range.start);
379 range.minlen = max(range.minlen, minlen);
380 ret = btrfs_trim_fs(fs_info->tree_root, &range);
384 if (copy_to_user(arg, &range, sizeof(range)))
390 int btrfs_is_empty_uuid(u8 *uuid)
394 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
401 static noinline int create_subvol(struct inode *dir,
402 struct dentry *dentry,
403 char *name, int namelen,
405 struct btrfs_qgroup_inherit *inherit)
407 struct btrfs_trans_handle *trans;
408 struct btrfs_key key;
409 struct btrfs_root_item root_item;
410 struct btrfs_inode_item *inode_item;
411 struct extent_buffer *leaf;
412 struct btrfs_root *root = BTRFS_I(dir)->root;
413 struct btrfs_root *new_root;
414 struct btrfs_block_rsv block_rsv;
415 struct timespec cur_time = CURRENT_TIME;
420 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
425 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
429 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
431 * The same as the snapshot creation, please see the comment
432 * of create_snapshot().
434 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
435 8, &qgroup_reserved, false);
439 trans = btrfs_start_transaction(root, 0);
441 ret = PTR_ERR(trans);
442 btrfs_subvolume_release_metadata(root, &block_rsv,
446 trans->block_rsv = &block_rsv;
447 trans->bytes_reserved = block_rsv.size;
449 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
453 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
454 0, objectid, NULL, 0, 0, 0);
460 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
461 btrfs_set_header_bytenr(leaf, leaf->start);
462 btrfs_set_header_generation(leaf, trans->transid);
463 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
464 btrfs_set_header_owner(leaf, objectid);
466 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
468 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
469 btrfs_header_chunk_tree_uuid(leaf),
471 btrfs_mark_buffer_dirty(leaf);
473 memset(&root_item, 0, sizeof(root_item));
475 inode_item = &root_item.inode;
476 btrfs_set_stack_inode_generation(inode_item, 1);
477 btrfs_set_stack_inode_size(inode_item, 3);
478 btrfs_set_stack_inode_nlink(inode_item, 1);
479 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
480 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
482 btrfs_set_root_flags(&root_item, 0);
483 btrfs_set_root_limit(&root_item, 0);
484 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
486 btrfs_set_root_bytenr(&root_item, leaf->start);
487 btrfs_set_root_generation(&root_item, trans->transid);
488 btrfs_set_root_level(&root_item, 0);
489 btrfs_set_root_refs(&root_item, 1);
490 btrfs_set_root_used(&root_item, leaf->len);
491 btrfs_set_root_last_snapshot(&root_item, 0);
493 btrfs_set_root_generation_v2(&root_item,
494 btrfs_root_generation(&root_item));
495 uuid_le_gen(&new_uuid);
496 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
497 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
498 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
499 root_item.ctime = root_item.otime;
500 btrfs_set_root_ctransid(&root_item, trans->transid);
501 btrfs_set_root_otransid(&root_item, trans->transid);
503 btrfs_tree_unlock(leaf);
504 free_extent_buffer(leaf);
507 btrfs_set_root_dirid(&root_item, new_dirid);
509 key.objectid = objectid;
511 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
512 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
517 key.offset = (u64)-1;
518 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
519 if (IS_ERR(new_root)) {
520 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
521 ret = PTR_ERR(new_root);
525 btrfs_record_root_in_trans(trans, new_root);
527 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
529 /* We potentially lose an unused inode item here */
530 btrfs_abort_transaction(trans, root, ret);
535 * insert the directory item
537 ret = btrfs_set_inode_index(dir, &index);
539 btrfs_abort_transaction(trans, root, ret);
543 ret = btrfs_insert_dir_item(trans, root,
544 name, namelen, dir, &key,
545 BTRFS_FT_DIR, index);
547 btrfs_abort_transaction(trans, root, ret);
551 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
552 ret = btrfs_update_inode(trans, root, dir);
555 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
556 objectid, root->root_key.objectid,
557 btrfs_ino(dir), index, name, namelen);
560 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
561 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
564 btrfs_abort_transaction(trans, root, ret);
567 trans->block_rsv = NULL;
568 trans->bytes_reserved = 0;
569 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
572 *async_transid = trans->transid;
573 err = btrfs_commit_transaction_async(trans, root, 1);
575 err = btrfs_commit_transaction(trans, root);
577 err = btrfs_commit_transaction(trans, root);
583 inode = btrfs_lookup_dentry(dir, dentry);
585 return PTR_ERR(inode);
586 d_instantiate(dentry, inode);
591 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
592 struct dentry *dentry, char *name, int namelen,
593 u64 *async_transid, bool readonly,
594 struct btrfs_qgroup_inherit *inherit)
597 struct btrfs_pending_snapshot *pending_snapshot;
598 struct btrfs_trans_handle *trans;
604 ret = btrfs_start_delalloc_inodes(root, 0);
608 btrfs_wait_ordered_extents(root, -1);
610 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
611 if (!pending_snapshot)
614 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
615 BTRFS_BLOCK_RSV_TEMP);
617 * 1 - parent dir inode
620 * 2 - root ref/backref
621 * 1 - root of snapshot
624 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
625 &pending_snapshot->block_rsv, 8,
626 &pending_snapshot->qgroup_reserved,
631 pending_snapshot->dentry = dentry;
632 pending_snapshot->root = root;
633 pending_snapshot->readonly = readonly;
634 pending_snapshot->dir = dir;
635 pending_snapshot->inherit = inherit;
637 trans = btrfs_start_transaction(root, 0);
639 ret = PTR_ERR(trans);
643 spin_lock(&root->fs_info->trans_lock);
644 list_add(&pending_snapshot->list,
645 &trans->transaction->pending_snapshots);
646 spin_unlock(&root->fs_info->trans_lock);
648 *async_transid = trans->transid;
649 ret = btrfs_commit_transaction_async(trans,
650 root->fs_info->extent_root, 1);
652 ret = btrfs_commit_transaction(trans, root);
654 ret = btrfs_commit_transaction(trans,
655 root->fs_info->extent_root);
660 ret = pending_snapshot->error;
664 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
668 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
670 ret = PTR_ERR(inode);
674 d_instantiate(dentry, inode);
677 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
678 &pending_snapshot->block_rsv,
679 pending_snapshot->qgroup_reserved);
681 kfree(pending_snapshot);
685 /* copy of check_sticky in fs/namei.c()
686 * It's inline, so penalty for filesystems that don't use sticky bit is
689 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
691 kuid_t fsuid = current_fsuid();
693 if (!(dir->i_mode & S_ISVTX))
695 if (uid_eq(inode->i_uid, fsuid))
697 if (uid_eq(dir->i_uid, fsuid))
699 return !capable(CAP_FOWNER);
702 /* copy of may_delete in fs/namei.c()
703 * Check whether we can remove a link victim from directory dir, check
704 * whether the type of victim is right.
705 * 1. We can't do it if dir is read-only (done in permission())
706 * 2. We should have write and exec permissions on dir
707 * 3. We can't remove anything from append-only dir
708 * 4. We can't do anything with immutable dir (done in permission())
709 * 5. If the sticky bit on dir is set we should either
710 * a. be owner of dir, or
711 * b. be owner of victim, or
712 * c. have CAP_FOWNER capability
713 * 6. If the victim is append-only or immutable we can't do antyhing with
714 * links pointing to it.
715 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
716 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
717 * 9. We can't remove a root or mountpoint.
718 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
719 * nfs_async_unlink().
722 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
726 if (!victim->d_inode)
729 BUG_ON(victim->d_parent->d_inode != dir);
730 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
732 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
737 if (btrfs_check_sticky(dir, victim->d_inode)||
738 IS_APPEND(victim->d_inode)||
739 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
742 if (!S_ISDIR(victim->d_inode->i_mode))
746 } else if (S_ISDIR(victim->d_inode->i_mode))
750 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
755 /* copy of may_create in fs/namei.c() */
756 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
762 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
766 * Create a new subvolume below @parent. This is largely modeled after
767 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
768 * inside this filesystem so it's quite a bit simpler.
770 static noinline int btrfs_mksubvol(struct path *parent,
771 char *name, int namelen,
772 struct btrfs_root *snap_src,
773 u64 *async_transid, bool readonly,
774 struct btrfs_qgroup_inherit *inherit)
776 struct inode *dir = parent->dentry->d_inode;
777 struct dentry *dentry;
780 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
784 dentry = lookup_one_len(name, parent->dentry, namelen);
785 error = PTR_ERR(dentry);
793 error = btrfs_may_create(dir, dentry);
798 * even if this name doesn't exist, we may get hash collisions.
799 * check for them now when we can safely fail
801 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
807 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
809 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
813 error = create_snapshot(snap_src, dir, dentry, name, namelen,
814 async_transid, readonly, inherit);
816 error = create_subvol(dir, dentry, name, namelen,
817 async_transid, inherit);
820 fsnotify_mkdir(dir, dentry);
822 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
826 mutex_unlock(&dir->i_mutex);
831 * When we're defragging a range, we don't want to kick it off again
832 * if it is really just waiting for delalloc to send it down.
833 * If we find a nice big extent or delalloc range for the bytes in the
834 * file you want to defrag, we return 0 to let you know to skip this
837 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
840 struct extent_map *em = NULL;
841 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
844 read_lock(&em_tree->lock);
845 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
846 read_unlock(&em_tree->lock);
849 end = extent_map_end(em);
851 if (end - offset > thresh)
854 /* if we already have a nice delalloc here, just stop */
856 end = count_range_bits(io_tree, &offset, offset + thresh,
857 thresh, EXTENT_DELALLOC, 1);
864 * helper function to walk through a file and find extents
865 * newer than a specific transid, and smaller than thresh.
867 * This is used by the defragging code to find new and small
870 static int find_new_extents(struct btrfs_root *root,
871 struct inode *inode, u64 newer_than,
872 u64 *off, int thresh)
874 struct btrfs_path *path;
875 struct btrfs_key min_key;
876 struct extent_buffer *leaf;
877 struct btrfs_file_extent_item *extent;
880 u64 ino = btrfs_ino(inode);
882 path = btrfs_alloc_path();
886 min_key.objectid = ino;
887 min_key.type = BTRFS_EXTENT_DATA_KEY;
888 min_key.offset = *off;
890 path->keep_locks = 1;
893 ret = btrfs_search_forward(root, &min_key, path, newer_than);
896 if (min_key.objectid != ino)
898 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
901 leaf = path->nodes[0];
902 extent = btrfs_item_ptr(leaf, path->slots[0],
903 struct btrfs_file_extent_item);
905 type = btrfs_file_extent_type(leaf, extent);
906 if (type == BTRFS_FILE_EXTENT_REG &&
907 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
908 check_defrag_in_cache(inode, min_key.offset, thresh)) {
909 *off = min_key.offset;
910 btrfs_free_path(path);
914 if (min_key.offset == (u64)-1)
918 btrfs_release_path(path);
921 btrfs_free_path(path);
925 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
927 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
929 struct extent_map *em;
930 u64 len = PAGE_CACHE_SIZE;
933 * hopefully we have this extent in the tree already, try without
934 * the full extent lock
936 read_lock(&em_tree->lock);
937 em = lookup_extent_mapping(em_tree, start, len);
938 read_unlock(&em_tree->lock);
941 /* get the big lock and read metadata off disk */
942 lock_extent(io_tree, start, start + len - 1);
943 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
944 unlock_extent(io_tree, start, start + len - 1);
953 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
955 struct extent_map *next;
958 /* this is the last extent */
959 if (em->start + em->len >= i_size_read(inode))
962 next = defrag_lookup_extent(inode, em->start + em->len);
963 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
966 free_extent_map(next);
970 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
971 u64 *last_len, u64 *skip, u64 *defrag_end,
974 struct extent_map *em;
976 bool next_mergeable = true;
979 * make sure that once we start defragging an extent, we keep on
982 if (start < *defrag_end)
987 em = defrag_lookup_extent(inode, start);
991 /* this will cover holes, and inline extents */
992 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
997 next_mergeable = defrag_check_next_extent(inode, em);
1000 * we hit a real extent, if it is big or the next extent is not a
1001 * real extent, don't bother defragging it
1003 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1004 (em->len >= thresh || !next_mergeable))
1008 * last_len ends up being a counter of how many bytes we've defragged.
1009 * every time we choose not to defrag an extent, we reset *last_len
1010 * so that the next tiny extent will force a defrag.
1012 * The end result of this is that tiny extents before a single big
1013 * extent will force at least part of that big extent to be defragged.
1016 *defrag_end = extent_map_end(em);
1019 *skip = extent_map_end(em);
1023 free_extent_map(em);
1028 * it doesn't do much good to defrag one or two pages
1029 * at a time. This pulls in a nice chunk of pages
1030 * to COW and defrag.
1032 * It also makes sure the delalloc code has enough
1033 * dirty data to avoid making new small extents as part
1036 * It's a good idea to start RA on this range
1037 * before calling this.
1039 static int cluster_pages_for_defrag(struct inode *inode,
1040 struct page **pages,
1041 unsigned long start_index,
1042 unsigned long num_pages)
1044 unsigned long file_end;
1045 u64 isize = i_size_read(inode);
1052 struct btrfs_ordered_extent *ordered;
1053 struct extent_state *cached_state = NULL;
1054 struct extent_io_tree *tree;
1055 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1057 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
1058 if (!isize || start_index > file_end)
1061 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1063 ret = btrfs_delalloc_reserve_space(inode,
1064 page_cnt << PAGE_CACHE_SHIFT);
1068 tree = &BTRFS_I(inode)->io_tree;
1070 /* step one, lock all the pages */
1071 for (i = 0; i < page_cnt; i++) {
1074 page = find_or_create_page(inode->i_mapping,
1075 start_index + i, mask);
1079 page_start = page_offset(page);
1080 page_end = page_start + PAGE_CACHE_SIZE - 1;
1082 lock_extent(tree, page_start, page_end);
1083 ordered = btrfs_lookup_ordered_extent(inode,
1085 unlock_extent(tree, page_start, page_end);
1090 btrfs_start_ordered_extent(inode, ordered, 1);
1091 btrfs_put_ordered_extent(ordered);
1094 * we unlocked the page above, so we need check if
1095 * it was released or not.
1097 if (page->mapping != inode->i_mapping) {
1099 page_cache_release(page);
1104 if (!PageUptodate(page)) {
1105 btrfs_readpage(NULL, page);
1107 if (!PageUptodate(page)) {
1109 page_cache_release(page);
1115 if (page->mapping != inode->i_mapping) {
1117 page_cache_release(page);
1127 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1131 * so now we have a nice long stream of locked
1132 * and up to date pages, lets wait on them
1134 for (i = 0; i < i_done; i++)
1135 wait_on_page_writeback(pages[i]);
1137 page_start = page_offset(pages[0]);
1138 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1140 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1141 page_start, page_end - 1, 0, &cached_state);
1142 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1143 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1144 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1145 &cached_state, GFP_NOFS);
1147 if (i_done != page_cnt) {
1148 spin_lock(&BTRFS_I(inode)->lock);
1149 BTRFS_I(inode)->outstanding_extents++;
1150 spin_unlock(&BTRFS_I(inode)->lock);
1151 btrfs_delalloc_release_space(inode,
1152 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1156 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1157 &cached_state, GFP_NOFS);
1159 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1160 page_start, page_end - 1, &cached_state,
1163 for (i = 0; i < i_done; i++) {
1164 clear_page_dirty_for_io(pages[i]);
1165 ClearPageChecked(pages[i]);
1166 set_page_extent_mapped(pages[i]);
1167 set_page_dirty(pages[i]);
1168 unlock_page(pages[i]);
1169 page_cache_release(pages[i]);
1173 for (i = 0; i < i_done; i++) {
1174 unlock_page(pages[i]);
1175 page_cache_release(pages[i]);
1177 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1182 int btrfs_defrag_file(struct inode *inode, struct file *file,
1183 struct btrfs_ioctl_defrag_range_args *range,
1184 u64 newer_than, unsigned long max_to_defrag)
1186 struct btrfs_root *root = BTRFS_I(inode)->root;
1187 struct file_ra_state *ra = NULL;
1188 unsigned long last_index;
1189 u64 isize = i_size_read(inode);
1193 u64 newer_off = range->start;
1195 unsigned long ra_index = 0;
1197 int defrag_count = 0;
1198 int compress_type = BTRFS_COMPRESS_ZLIB;
1199 int extent_thresh = range->extent_thresh;
1200 unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1201 unsigned long cluster = max_cluster;
1202 u64 new_align = ~((u64)128 * 1024 - 1);
1203 struct page **pages = NULL;
1208 if (range->start >= isize)
1211 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1212 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1214 if (range->compress_type)
1215 compress_type = range->compress_type;
1218 if (extent_thresh == 0)
1219 extent_thresh = 256 * 1024;
1222 * if we were not given a file, allocate a readahead
1226 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1229 file_ra_state_init(ra, inode->i_mapping);
1234 pages = kmalloc_array(max_cluster, sizeof(struct page *),
1241 /* find the last page to defrag */
1242 if (range->start + range->len > range->start) {
1243 last_index = min_t(u64, isize - 1,
1244 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1246 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1250 ret = find_new_extents(root, inode, newer_than,
1251 &newer_off, 64 * 1024);
1253 range->start = newer_off;
1255 * we always align our defrag to help keep
1256 * the extents in the file evenly spaced
1258 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1262 i = range->start >> PAGE_CACHE_SHIFT;
1265 max_to_defrag = last_index + 1;
1268 * make writeback starts from i, so the defrag range can be
1269 * written sequentially.
1271 if (i < inode->i_mapping->writeback_index)
1272 inode->i_mapping->writeback_index = i;
1274 while (i <= last_index && defrag_count < max_to_defrag &&
1275 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1276 PAGE_CACHE_SHIFT)) {
1278 * make sure we stop running if someone unmounts
1281 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1284 if (btrfs_defrag_cancelled(root->fs_info)) {
1285 printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
1290 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1291 extent_thresh, &last_len, &skip,
1292 &defrag_end, range->flags &
1293 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1296 * the should_defrag function tells us how much to skip
1297 * bump our counter by the suggested amount
1299 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1300 i = max(i + 1, next);
1305 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1306 PAGE_CACHE_SHIFT) - i;
1307 cluster = min(cluster, max_cluster);
1309 cluster = max_cluster;
1312 if (i + cluster > ra_index) {
1313 ra_index = max(i, ra_index);
1314 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1316 ra_index += max_cluster;
1319 mutex_lock(&inode->i_mutex);
1320 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1321 BTRFS_I(inode)->force_compress = compress_type;
1322 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1324 mutex_unlock(&inode->i_mutex);
1328 defrag_count += ret;
1329 balance_dirty_pages_ratelimited(inode->i_mapping);
1330 mutex_unlock(&inode->i_mutex);
1333 if (newer_off == (u64)-1)
1339 newer_off = max(newer_off + 1,
1340 (u64)i << PAGE_CACHE_SHIFT);
1342 ret = find_new_extents(root, inode,
1343 newer_than, &newer_off,
1346 range->start = newer_off;
1347 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1354 last_len += ret << PAGE_CACHE_SHIFT;
1362 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1363 filemap_flush(inode->i_mapping);
1365 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1366 /* the filemap_flush will queue IO into the worker threads, but
1367 * we have to make sure the IO is actually started and that
1368 * ordered extents get created before we return
1370 atomic_inc(&root->fs_info->async_submit_draining);
1371 while (atomic_read(&root->fs_info->nr_async_submits) ||
1372 atomic_read(&root->fs_info->async_delalloc_pages)) {
1373 wait_event(root->fs_info->async_submit_wait,
1374 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1375 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1377 atomic_dec(&root->fs_info->async_submit_draining);
1380 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1381 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1387 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1388 mutex_lock(&inode->i_mutex);
1389 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1390 mutex_unlock(&inode->i_mutex);
1398 static noinline int btrfs_ioctl_resize(struct file *file,
1404 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1405 struct btrfs_ioctl_vol_args *vol_args;
1406 struct btrfs_trans_handle *trans;
1407 struct btrfs_device *device = NULL;
1409 char *devstr = NULL;
1413 if (!capable(CAP_SYS_ADMIN))
1416 ret = mnt_want_write_file(file);
1420 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1422 mnt_drop_write_file(file);
1423 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1426 mutex_lock(&root->fs_info->volume_mutex);
1427 vol_args = memdup_user(arg, sizeof(*vol_args));
1428 if (IS_ERR(vol_args)) {
1429 ret = PTR_ERR(vol_args);
1433 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1435 sizestr = vol_args->name;
1436 devstr = strchr(sizestr, ':');
1439 sizestr = devstr + 1;
1441 devstr = vol_args->name;
1442 devid = simple_strtoull(devstr, &end, 10);
1447 btrfs_info(root->fs_info, "resizing devid %llu", devid);
1450 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1452 btrfs_info(root->fs_info, "resizer unable to find device %llu",
1458 if (!device->writeable) {
1459 btrfs_info(root->fs_info,
1460 "resizer unable to apply on readonly device %llu",
1466 if (!strcmp(sizestr, "max"))
1467 new_size = device->bdev->bd_inode->i_size;
1469 if (sizestr[0] == '-') {
1472 } else if (sizestr[0] == '+') {
1476 new_size = memparse(sizestr, NULL);
1477 if (new_size == 0) {
1483 if (device->is_tgtdev_for_dev_replace) {
1488 old_size = device->total_bytes;
1491 if (new_size > old_size) {
1495 new_size = old_size - new_size;
1496 } else if (mod > 0) {
1497 if (new_size > ULLONG_MAX - old_size) {
1501 new_size = old_size + new_size;
1504 if (new_size < 256 * 1024 * 1024) {
1508 if (new_size > device->bdev->bd_inode->i_size) {
1513 do_div(new_size, root->sectorsize);
1514 new_size *= root->sectorsize;
1516 printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
1517 rcu_str_deref(device->name), new_size);
1519 if (new_size > old_size) {
1520 trans = btrfs_start_transaction(root, 0);
1521 if (IS_ERR(trans)) {
1522 ret = PTR_ERR(trans);
1525 ret = btrfs_grow_device(trans, device, new_size);
1526 btrfs_commit_transaction(trans, root);
1527 } else if (new_size < old_size) {
1528 ret = btrfs_shrink_device(device, new_size);
1529 } /* equal, nothing need to do */
1534 mutex_unlock(&root->fs_info->volume_mutex);
1535 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1536 mnt_drop_write_file(file);
1540 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1541 char *name, unsigned long fd, int subvol,
1542 u64 *transid, bool readonly,
1543 struct btrfs_qgroup_inherit *inherit)
1548 ret = mnt_want_write_file(file);
1552 namelen = strlen(name);
1553 if (strchr(name, '/')) {
1555 goto out_drop_write;
1558 if (name[0] == '.' &&
1559 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1561 goto out_drop_write;
1565 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1566 NULL, transid, readonly, inherit);
1568 struct fd src = fdget(fd);
1569 struct inode *src_inode;
1572 goto out_drop_write;
1575 src_inode = file_inode(src.file);
1576 if (src_inode->i_sb != file_inode(file)->i_sb) {
1577 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1578 "Snapshot src from another FS");
1580 } else if (!inode_owner_or_capable(src_inode)) {
1582 * Subvolume creation is not restricted, but snapshots
1583 * are limited to own subvolumes only
1587 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1588 BTRFS_I(src_inode)->root,
1589 transid, readonly, inherit);
1594 mnt_drop_write_file(file);
1599 static noinline int btrfs_ioctl_snap_create(struct file *file,
1600 void __user *arg, int subvol)
1602 struct btrfs_ioctl_vol_args *vol_args;
1605 vol_args = memdup_user(arg, sizeof(*vol_args));
1606 if (IS_ERR(vol_args))
1607 return PTR_ERR(vol_args);
1608 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1610 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1611 vol_args->fd, subvol,
1618 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1619 void __user *arg, int subvol)
1621 struct btrfs_ioctl_vol_args_v2 *vol_args;
1625 bool readonly = false;
1626 struct btrfs_qgroup_inherit *inherit = NULL;
1628 vol_args = memdup_user(arg, sizeof(*vol_args));
1629 if (IS_ERR(vol_args))
1630 return PTR_ERR(vol_args);
1631 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1633 if (vol_args->flags &
1634 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1635 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1640 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1642 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1644 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1645 if (vol_args->size > PAGE_CACHE_SIZE) {
1649 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1650 if (IS_ERR(inherit)) {
1651 ret = PTR_ERR(inherit);
1656 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1657 vol_args->fd, subvol, ptr,
1660 if (ret == 0 && ptr &&
1662 offsetof(struct btrfs_ioctl_vol_args_v2,
1663 transid), ptr, sizeof(*ptr)))
1671 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1674 struct inode *inode = file_inode(file);
1675 struct btrfs_root *root = BTRFS_I(inode)->root;
1679 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1682 down_read(&root->fs_info->subvol_sem);
1683 if (btrfs_root_readonly(root))
1684 flags |= BTRFS_SUBVOL_RDONLY;
1685 up_read(&root->fs_info->subvol_sem);
1687 if (copy_to_user(arg, &flags, sizeof(flags)))
1693 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1696 struct inode *inode = file_inode(file);
1697 struct btrfs_root *root = BTRFS_I(inode)->root;
1698 struct btrfs_trans_handle *trans;
1703 if (!inode_owner_or_capable(inode))
1706 ret = mnt_want_write_file(file);
1710 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1712 goto out_drop_write;
1715 if (copy_from_user(&flags, arg, sizeof(flags))) {
1717 goto out_drop_write;
1720 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1722 goto out_drop_write;
1725 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1727 goto out_drop_write;
1730 down_write(&root->fs_info->subvol_sem);
1733 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1736 root_flags = btrfs_root_flags(&root->root_item);
1737 if (flags & BTRFS_SUBVOL_RDONLY) {
1738 btrfs_set_root_flags(&root->root_item,
1739 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1742 * Block RO -> RW transition if this subvolume is involved in
1745 spin_lock(&root->root_item_lock);
1746 if (root->send_in_progress == 0) {
1747 btrfs_set_root_flags(&root->root_item,
1748 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1749 spin_unlock(&root->root_item_lock);
1751 spin_unlock(&root->root_item_lock);
1752 btrfs_warn(root->fs_info,
1753 "Attempt to set subvolume %llu read-write during send",
1754 root->root_key.objectid);
1760 trans = btrfs_start_transaction(root, 1);
1761 if (IS_ERR(trans)) {
1762 ret = PTR_ERR(trans);
1766 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1767 &root->root_key, &root->root_item);
1769 btrfs_commit_transaction(trans, root);
1772 btrfs_set_root_flags(&root->root_item, root_flags);
1774 up_write(&root->fs_info->subvol_sem);
1776 mnt_drop_write_file(file);
1782 * helper to check if the subvolume references other subvolumes
1784 static noinline int may_destroy_subvol(struct btrfs_root *root)
1786 struct btrfs_path *path;
1787 struct btrfs_dir_item *di;
1788 struct btrfs_key key;
1792 path = btrfs_alloc_path();
1796 /* Make sure this root isn't set as the default subvol */
1797 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1798 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1799 dir_id, "default", 7, 0);
1800 if (di && !IS_ERR(di)) {
1801 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1802 if (key.objectid == root->root_key.objectid) {
1806 btrfs_release_path(path);
1809 key.objectid = root->root_key.objectid;
1810 key.type = BTRFS_ROOT_REF_KEY;
1811 key.offset = (u64)-1;
1813 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1820 if (path->slots[0] > 0) {
1822 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1823 if (key.objectid == root->root_key.objectid &&
1824 key.type == BTRFS_ROOT_REF_KEY)
1828 btrfs_free_path(path);
1832 static noinline int key_in_sk(struct btrfs_key *key,
1833 struct btrfs_ioctl_search_key *sk)
1835 struct btrfs_key test;
1838 test.objectid = sk->min_objectid;
1839 test.type = sk->min_type;
1840 test.offset = sk->min_offset;
1842 ret = btrfs_comp_cpu_keys(key, &test);
1846 test.objectid = sk->max_objectid;
1847 test.type = sk->max_type;
1848 test.offset = sk->max_offset;
1850 ret = btrfs_comp_cpu_keys(key, &test);
1856 static noinline int copy_to_sk(struct btrfs_root *root,
1857 struct btrfs_path *path,
1858 struct btrfs_key *key,
1859 struct btrfs_ioctl_search_key *sk,
1861 unsigned long *sk_offset,
1865 struct extent_buffer *leaf;
1866 struct btrfs_ioctl_search_header sh;
1867 unsigned long item_off;
1868 unsigned long item_len;
1874 leaf = path->nodes[0];
1875 slot = path->slots[0];
1876 nritems = btrfs_header_nritems(leaf);
1878 if (btrfs_header_generation(leaf) > sk->max_transid) {
1882 found_transid = btrfs_header_generation(leaf);
1884 for (i = slot; i < nritems; i++) {
1885 item_off = btrfs_item_ptr_offset(leaf, i);
1886 item_len = btrfs_item_size_nr(leaf, i);
1888 btrfs_item_key_to_cpu(leaf, key, i);
1889 if (!key_in_sk(key, sk))
1892 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1895 if (sizeof(sh) + item_len + *sk_offset >
1896 BTRFS_SEARCH_ARGS_BUFSIZE) {
1901 sh.objectid = key->objectid;
1902 sh.offset = key->offset;
1903 sh.type = key->type;
1905 sh.transid = found_transid;
1907 /* copy search result header */
1908 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1909 *sk_offset += sizeof(sh);
1912 char *p = buf + *sk_offset;
1914 read_extent_buffer(leaf, p,
1915 item_off, item_len);
1916 *sk_offset += item_len;
1920 if (*num_found >= sk->nr_items)
1925 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1927 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1930 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1940 static noinline int search_ioctl(struct inode *inode,
1941 struct btrfs_ioctl_search_args *args)
1943 struct btrfs_root *root;
1944 struct btrfs_key key;
1945 struct btrfs_path *path;
1946 struct btrfs_ioctl_search_key *sk = &args->key;
1947 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1950 unsigned long sk_offset = 0;
1952 path = btrfs_alloc_path();
1956 if (sk->tree_id == 0) {
1957 /* search the root of the inode that was passed */
1958 root = BTRFS_I(inode)->root;
1960 key.objectid = sk->tree_id;
1961 key.type = BTRFS_ROOT_ITEM_KEY;
1962 key.offset = (u64)-1;
1963 root = btrfs_read_fs_root_no_name(info, &key);
1965 printk(KERN_ERR "BTRFS: could not find root %llu\n",
1967 btrfs_free_path(path);
1972 key.objectid = sk->min_objectid;
1973 key.type = sk->min_type;
1974 key.offset = sk->min_offset;
1976 path->keep_locks = 1;
1979 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
1985 ret = copy_to_sk(root, path, &key, sk, args->buf,
1986 &sk_offset, &num_found);
1987 btrfs_release_path(path);
1988 if (ret || num_found >= sk->nr_items)
1994 sk->nr_items = num_found;
1995 btrfs_free_path(path);
1999 static noinline int btrfs_ioctl_tree_search(struct file *file,
2002 struct btrfs_ioctl_search_args *args;
2003 struct inode *inode;
2006 if (!capable(CAP_SYS_ADMIN))
2009 args = memdup_user(argp, sizeof(*args));
2011 return PTR_ERR(args);
2013 inode = file_inode(file);
2014 ret = search_ioctl(inode, args);
2015 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2022 * Search INODE_REFs to identify path name of 'dirid' directory
2023 * in a 'tree_id' tree. and sets path name to 'name'.
2025 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2026 u64 tree_id, u64 dirid, char *name)
2028 struct btrfs_root *root;
2029 struct btrfs_key key;
2035 struct btrfs_inode_ref *iref;
2036 struct extent_buffer *l;
2037 struct btrfs_path *path;
2039 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2044 path = btrfs_alloc_path();
2048 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
2050 key.objectid = tree_id;
2051 key.type = BTRFS_ROOT_ITEM_KEY;
2052 key.offset = (u64)-1;
2053 root = btrfs_read_fs_root_no_name(info, &key);
2055 printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
2060 key.objectid = dirid;
2061 key.type = BTRFS_INODE_REF_KEY;
2062 key.offset = (u64)-1;
2065 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2069 ret = btrfs_previous_item(root, path, dirid,
2070 BTRFS_INODE_REF_KEY);
2080 slot = path->slots[0];
2081 btrfs_item_key_to_cpu(l, &key, slot);
2083 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2084 len = btrfs_inode_ref_name_len(l, iref);
2086 total_len += len + 1;
2088 ret = -ENAMETOOLONG;
2093 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2095 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2098 btrfs_release_path(path);
2099 key.objectid = key.offset;
2100 key.offset = (u64)-1;
2101 dirid = key.objectid;
2103 memmove(name, ptr, total_len);
2104 name[total_len] = '\0';
2107 btrfs_free_path(path);
2111 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2114 struct btrfs_ioctl_ino_lookup_args *args;
2115 struct inode *inode;
2118 if (!capable(CAP_SYS_ADMIN))
2121 args = memdup_user(argp, sizeof(*args));
2123 return PTR_ERR(args);
2125 inode = file_inode(file);
2127 if (args->treeid == 0)
2128 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2130 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2131 args->treeid, args->objectid,
2134 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2141 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2144 struct dentry *parent = file->f_path.dentry;
2145 struct dentry *dentry;
2146 struct inode *dir = parent->d_inode;
2147 struct inode *inode;
2148 struct btrfs_root *root = BTRFS_I(dir)->root;
2149 struct btrfs_root *dest = NULL;
2150 struct btrfs_ioctl_vol_args *vol_args;
2151 struct btrfs_trans_handle *trans;
2152 struct btrfs_block_rsv block_rsv;
2153 u64 qgroup_reserved;
2158 vol_args = memdup_user(arg, sizeof(*vol_args));
2159 if (IS_ERR(vol_args))
2160 return PTR_ERR(vol_args);
2162 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2163 namelen = strlen(vol_args->name);
2164 if (strchr(vol_args->name, '/') ||
2165 strncmp(vol_args->name, "..", namelen) == 0) {
2170 err = mnt_want_write_file(file);
2174 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2176 goto out_drop_write;
2177 dentry = lookup_one_len(vol_args->name, parent, namelen);
2178 if (IS_ERR(dentry)) {
2179 err = PTR_ERR(dentry);
2180 goto out_unlock_dir;
2183 if (!dentry->d_inode) {
2188 inode = dentry->d_inode;
2189 dest = BTRFS_I(inode)->root;
2190 if (!capable(CAP_SYS_ADMIN)) {
2192 * Regular user. Only allow this with a special mount
2193 * option, when the user has write+exec access to the
2194 * subvol root, and when rmdir(2) would have been
2197 * Note that this is _not_ check that the subvol is
2198 * empty or doesn't contain data that we wouldn't
2199 * otherwise be able to delete.
2201 * Users who want to delete empty subvols should try
2205 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2209 * Do not allow deletion if the parent dir is the same
2210 * as the dir to be deleted. That means the ioctl
2211 * must be called on the dentry referencing the root
2212 * of the subvol, not a random directory contained
2219 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2224 /* check if subvolume may be deleted by a user */
2225 err = btrfs_may_delete(dir, dentry, 1);
2229 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2234 mutex_lock(&inode->i_mutex);
2235 err = d_invalidate(dentry);
2239 down_write(&root->fs_info->subvol_sem);
2241 err = may_destroy_subvol(dest);
2245 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2247 * One for dir inode, two for dir entries, two for root
2250 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2251 5, &qgroup_reserved, true);
2255 trans = btrfs_start_transaction(root, 0);
2256 if (IS_ERR(trans)) {
2257 err = PTR_ERR(trans);
2260 trans->block_rsv = &block_rsv;
2261 trans->bytes_reserved = block_rsv.size;
2263 ret = btrfs_unlink_subvol(trans, root, dir,
2264 dest->root_key.objectid,
2265 dentry->d_name.name,
2266 dentry->d_name.len);
2269 btrfs_abort_transaction(trans, root, ret);
2273 btrfs_record_root_in_trans(trans, dest);
2275 memset(&dest->root_item.drop_progress, 0,
2276 sizeof(dest->root_item.drop_progress));
2277 dest->root_item.drop_level = 0;
2278 btrfs_set_root_refs(&dest->root_item, 0);
2280 if (!xchg(&dest->orphan_item_inserted, 1)) {
2281 ret = btrfs_insert_orphan_item(trans,
2282 root->fs_info->tree_root,
2283 dest->root_key.objectid);
2285 btrfs_abort_transaction(trans, root, ret);
2291 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2292 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2293 dest->root_key.objectid);
2294 if (ret && ret != -ENOENT) {
2295 btrfs_abort_transaction(trans, root, ret);
2299 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2300 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2301 dest->root_item.received_uuid,
2302 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2303 dest->root_key.objectid);
2304 if (ret && ret != -ENOENT) {
2305 btrfs_abort_transaction(trans, root, ret);
2312 trans->block_rsv = NULL;
2313 trans->bytes_reserved = 0;
2314 ret = btrfs_end_transaction(trans, root);
2317 inode->i_flags |= S_DEAD;
2319 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
2321 up_write(&root->fs_info->subvol_sem);
2323 mutex_unlock(&inode->i_mutex);
2325 shrink_dcache_sb(root->fs_info->sb);
2326 btrfs_invalidate_inodes(dest);
2330 if (dest->cache_inode) {
2331 iput(dest->cache_inode);
2332 dest->cache_inode = NULL;
2338 mutex_unlock(&dir->i_mutex);
2340 mnt_drop_write_file(file);
2346 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2348 struct inode *inode = file_inode(file);
2349 struct btrfs_root *root = BTRFS_I(inode)->root;
2350 struct btrfs_ioctl_defrag_range_args *range;
2353 ret = mnt_want_write_file(file);
2357 if (btrfs_root_readonly(root)) {
2362 switch (inode->i_mode & S_IFMT) {
2364 if (!capable(CAP_SYS_ADMIN)) {
2368 ret = btrfs_defrag_root(root);
2371 ret = btrfs_defrag_root(root->fs_info->extent_root);
2374 if (!(file->f_mode & FMODE_WRITE)) {
2379 range = kzalloc(sizeof(*range), GFP_KERNEL);
2386 if (copy_from_user(range, argp,
2392 /* compression requires us to start the IO */
2393 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2394 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2395 range->extent_thresh = (u32)-1;
2398 /* the rest are all set to zero by kzalloc */
2399 range->len = (u64)-1;
2401 ret = btrfs_defrag_file(file_inode(file), file,
2411 mnt_drop_write_file(file);
2415 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2417 struct btrfs_ioctl_vol_args *vol_args;
2420 if (!capable(CAP_SYS_ADMIN))
2423 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2425 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2428 mutex_lock(&root->fs_info->volume_mutex);
2429 vol_args = memdup_user(arg, sizeof(*vol_args));
2430 if (IS_ERR(vol_args)) {
2431 ret = PTR_ERR(vol_args);
2435 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2436 ret = btrfs_init_new_device(root, vol_args->name);
2440 mutex_unlock(&root->fs_info->volume_mutex);
2441 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2445 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2447 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2448 struct btrfs_ioctl_vol_args *vol_args;
2451 if (!capable(CAP_SYS_ADMIN))
2454 ret = mnt_want_write_file(file);
2458 vol_args = memdup_user(arg, sizeof(*vol_args));
2459 if (IS_ERR(vol_args)) {
2460 ret = PTR_ERR(vol_args);
2464 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2466 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2468 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2472 mutex_lock(&root->fs_info->volume_mutex);
2473 ret = btrfs_rm_device(root, vol_args->name);
2474 mutex_unlock(&root->fs_info->volume_mutex);
2475 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2479 mnt_drop_write_file(file);
2483 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2485 struct btrfs_ioctl_fs_info_args *fi_args;
2486 struct btrfs_device *device;
2487 struct btrfs_device *next;
2488 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2491 if (!capable(CAP_SYS_ADMIN))
2494 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2498 mutex_lock(&fs_devices->device_list_mutex);
2499 fi_args->num_devices = fs_devices->num_devices;
2500 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2502 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2503 if (device->devid > fi_args->max_id)
2504 fi_args->max_id = device->devid;
2506 mutex_unlock(&fs_devices->device_list_mutex);
2508 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2515 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2517 struct btrfs_ioctl_dev_info_args *di_args;
2518 struct btrfs_device *dev;
2519 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2521 char *s_uuid = NULL;
2523 if (!capable(CAP_SYS_ADMIN))
2526 di_args = memdup_user(arg, sizeof(*di_args));
2527 if (IS_ERR(di_args))
2528 return PTR_ERR(di_args);
2530 if (!btrfs_is_empty_uuid(di_args->uuid))
2531 s_uuid = di_args->uuid;
2533 mutex_lock(&fs_devices->device_list_mutex);
2534 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2541 di_args->devid = dev->devid;
2542 di_args->bytes_used = dev->bytes_used;
2543 di_args->total_bytes = dev->total_bytes;
2544 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2546 struct rcu_string *name;
2549 name = rcu_dereference(dev->name);
2550 strncpy(di_args->path, name->str, sizeof(di_args->path));
2552 di_args->path[sizeof(di_args->path) - 1] = 0;
2554 di_args->path[0] = '\0';
2558 mutex_unlock(&fs_devices->device_list_mutex);
2559 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2566 static struct page *extent_same_get_page(struct inode *inode, u64 off)
2570 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2572 index = off >> PAGE_CACHE_SHIFT;
2574 page = grab_cache_page(inode->i_mapping, index);
2578 if (!PageUptodate(page)) {
2579 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2583 if (!PageUptodate(page)) {
2585 page_cache_release(page);
2594 static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2596 /* do any pending delalloc/csum calc on src, one way or
2597 another, and lock file content */
2599 struct btrfs_ordered_extent *ordered;
2600 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2601 ordered = btrfs_lookup_first_ordered_extent(inode,
2604 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2605 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2607 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2609 btrfs_put_ordered_extent(ordered);
2610 btrfs_wait_ordered_range(inode, off, len);
2614 static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2615 struct inode *inode2, u64 loff2, u64 len)
2617 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2618 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2620 mutex_unlock(&inode1->i_mutex);
2621 mutex_unlock(&inode2->i_mutex);
2624 static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2625 struct inode *inode2, u64 loff2, u64 len)
2627 if (inode1 < inode2) {
2628 swap(inode1, inode2);
2632 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2633 lock_extent_range(inode1, loff1, len);
2634 if (inode1 != inode2) {
2635 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2636 lock_extent_range(inode2, loff2, len);
2640 static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2641 u64 dst_loff, u64 len)
2644 struct page *src_page, *dst_page;
2645 unsigned int cmp_len = PAGE_CACHE_SIZE;
2646 void *addr, *dst_addr;
2649 if (len < PAGE_CACHE_SIZE)
2652 src_page = extent_same_get_page(src, loff);
2655 dst_page = extent_same_get_page(dst, dst_loff);
2657 page_cache_release(src_page);
2660 addr = kmap_atomic(src_page);
2661 dst_addr = kmap_atomic(dst_page);
2663 flush_dcache_page(src_page);
2664 flush_dcache_page(dst_page);
2666 if (memcmp(addr, dst_addr, cmp_len))
2667 ret = BTRFS_SAME_DATA_DIFFERS;
2669 kunmap_atomic(addr);
2670 kunmap_atomic(dst_addr);
2671 page_cache_release(src_page);
2672 page_cache_release(dst_page);
2678 dst_loff += cmp_len;
2685 static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2687 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2689 if (off + len > inode->i_size || off + len < off)
2691 /* Check that we are block aligned - btrfs_clone() requires this */
2692 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2698 static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2699 struct inode *dst, u64 dst_loff)
2704 * btrfs_clone() can't handle extents in the same file
2705 * yet. Once that works, we can drop this check and replace it
2706 * with a check for the same inode, but overlapping extents.
2711 btrfs_double_lock(src, loff, dst, dst_loff, len);
2713 ret = extent_same_check_offsets(src, loff, len);
2717 ret = extent_same_check_offsets(dst, dst_loff, len);
2721 /* don't make the dst file partly checksummed */
2722 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2723 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2728 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2730 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2733 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2738 #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2740 static long btrfs_ioctl_file_extent_same(struct file *file,
2741 struct btrfs_ioctl_same_args __user *argp)
2743 struct btrfs_ioctl_same_args *same;
2744 struct btrfs_ioctl_same_extent_info *info;
2745 struct inode *src = file_inode(file);
2751 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2752 bool is_admin = capable(CAP_SYS_ADMIN);
2755 if (!(file->f_mode & FMODE_READ))
2758 ret = mnt_want_write_file(file);
2762 if (get_user(count, &argp->dest_count)) {
2767 size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
2769 same = memdup_user(argp, size);
2772 ret = PTR_ERR(same);
2776 off = same->logical_offset;
2780 * Limit the total length we will dedupe for each operation.
2781 * This is intended to bound the total time spent in this
2782 * ioctl to something sane.
2784 if (len > BTRFS_MAX_DEDUPE_LEN)
2785 len = BTRFS_MAX_DEDUPE_LEN;
2787 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2789 * Btrfs does not support blocksize < page_size. As a
2790 * result, btrfs_cmp_data() won't correctly handle
2791 * this situation without an update.
2798 if (S_ISDIR(src->i_mode))
2802 if (!S_ISREG(src->i_mode))
2805 /* pre-format output fields to sane values */
2806 for (i = 0; i < count; i++) {
2807 same->info[i].bytes_deduped = 0ULL;
2808 same->info[i].status = 0;
2811 for (i = 0, info = same->info; i < count; i++, info++) {
2813 struct fd dst_file = fdget(info->fd);
2814 if (!dst_file.file) {
2815 info->status = -EBADF;
2818 dst = file_inode(dst_file.file);
2820 if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
2821 info->status = -EINVAL;
2822 } else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
2823 info->status = -EXDEV;
2824 } else if (S_ISDIR(dst->i_mode)) {
2825 info->status = -EISDIR;
2826 } else if (!S_ISREG(dst->i_mode)) {
2827 info->status = -EACCES;
2829 info->status = btrfs_extent_same(src, off, len, dst,
2830 info->logical_offset);
2831 if (info->status == 0)
2832 info->bytes_deduped += len;
2837 ret = copy_to_user(argp, same, size);
2842 mnt_drop_write_file(file);
2847 * btrfs_clone() - clone a range from inode file to another
2849 * @src: Inode to clone from
2850 * @inode: Inode to clone to
2851 * @off: Offset within source to start clone from
2852 * @olen: Original length, passed by user, of range to clone
2853 * @olen_aligned: Block-aligned value of olen, extent_same uses
2854 * identical values here
2855 * @destoff: Offset within @inode to start clone
2857 static int btrfs_clone(struct inode *src, struct inode *inode,
2858 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2860 struct btrfs_root *root = BTRFS_I(inode)->root;
2861 struct btrfs_path *path = NULL;
2862 struct extent_buffer *leaf;
2863 struct btrfs_trans_handle *trans;
2865 struct btrfs_key key;
2869 u64 len = olen_aligned;
2872 buf = vmalloc(btrfs_level_size(root, 0));
2876 path = btrfs_alloc_path();
2884 key.objectid = btrfs_ino(src);
2885 key.type = BTRFS_EXTENT_DATA_KEY;
2890 * note the key will change type as we walk through the
2893 path->leave_spinning = 1;
2894 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2899 nritems = btrfs_header_nritems(path->nodes[0]);
2901 if (path->slots[0] >= nritems) {
2902 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2907 nritems = btrfs_header_nritems(path->nodes[0]);
2909 leaf = path->nodes[0];
2910 slot = path->slots[0];
2912 btrfs_item_key_to_cpu(leaf, &key, slot);
2913 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2914 key.objectid != btrfs_ino(src))
2917 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2918 struct btrfs_file_extent_item *extent;
2921 struct btrfs_key new_key;
2922 u64 disko = 0, diskl = 0;
2923 u64 datao = 0, datal = 0;
2927 extent = btrfs_item_ptr(leaf, slot,
2928 struct btrfs_file_extent_item);
2929 comp = btrfs_file_extent_compression(leaf, extent);
2930 type = btrfs_file_extent_type(leaf, extent);
2931 if (type == BTRFS_FILE_EXTENT_REG ||
2932 type == BTRFS_FILE_EXTENT_PREALLOC) {
2933 disko = btrfs_file_extent_disk_bytenr(leaf,
2935 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2937 datao = btrfs_file_extent_offset(leaf, extent);
2938 datal = btrfs_file_extent_num_bytes(leaf,
2940 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2941 /* take upper bound, may be compressed */
2942 datal = btrfs_file_extent_ram_bytes(leaf,
2946 if (key.offset + datal <= off ||
2947 key.offset >= off + len - 1) {
2952 size = btrfs_item_size_nr(leaf, slot);
2953 read_extent_buffer(leaf, buf,
2954 btrfs_item_ptr_offset(leaf, slot),
2957 btrfs_release_path(path);
2958 path->leave_spinning = 0;
2960 memcpy(&new_key, &key, sizeof(new_key));
2961 new_key.objectid = btrfs_ino(inode);
2962 if (off <= key.offset)
2963 new_key.offset = key.offset + destoff - off;
2965 new_key.offset = destoff;
2968 * 1 - adjusting old extent (we may have to split it)
2969 * 1 - add new extent
2972 trans = btrfs_start_transaction(root, 3);
2973 if (IS_ERR(trans)) {
2974 ret = PTR_ERR(trans);
2978 if (type == BTRFS_FILE_EXTENT_REG ||
2979 type == BTRFS_FILE_EXTENT_PREALLOC) {
2981 * a | --- range to clone ---| b
2982 * | ------------- extent ------------- |
2985 /* substract range b */
2986 if (key.offset + datal > off + len)
2987 datal = off + len - key.offset;
2989 /* substract range a */
2990 if (off > key.offset) {
2991 datao += off - key.offset;
2992 datal -= off - key.offset;
2995 ret = btrfs_drop_extents(trans, root, inode,
2997 new_key.offset + datal,
3000 btrfs_abort_transaction(trans, root,
3002 btrfs_end_transaction(trans, root);
3006 ret = btrfs_insert_empty_item(trans, root, path,
3009 btrfs_abort_transaction(trans, root,
3011 btrfs_end_transaction(trans, root);
3015 leaf = path->nodes[0];
3016 slot = path->slots[0];
3017 write_extent_buffer(leaf, buf,
3018 btrfs_item_ptr_offset(leaf, slot),
3021 extent = btrfs_item_ptr(leaf, slot,
3022 struct btrfs_file_extent_item);
3024 /* disko == 0 means it's a hole */
3028 btrfs_set_file_extent_offset(leaf, extent,
3030 btrfs_set_file_extent_num_bytes(leaf, extent,
3033 inode_add_bytes(inode, datal);
3034 ret = btrfs_inc_extent_ref(trans, root,
3036 root->root_key.objectid,
3038 new_key.offset - datao,
3041 btrfs_abort_transaction(trans,
3044 btrfs_end_transaction(trans,
3050 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3053 if (off > key.offset) {
3054 skip = off - key.offset;
3055 new_key.offset += skip;
3058 if (key.offset + datal > off + len)
3059 trim = key.offset + datal - (off + len);
3061 if (comp && (skip || trim)) {
3063 btrfs_end_transaction(trans, root);
3066 size -= skip + trim;
3067 datal -= skip + trim;
3069 ret = btrfs_drop_extents(trans, root, inode,
3071 new_key.offset + datal,
3074 btrfs_abort_transaction(trans, root,
3076 btrfs_end_transaction(trans, root);
3080 ret = btrfs_insert_empty_item(trans, root, path,
3083 btrfs_abort_transaction(trans, root,
3085 btrfs_end_transaction(trans, root);
3091 btrfs_file_extent_calc_inline_size(0);
3092 memmove(buf+start, buf+start+skip,
3096 leaf = path->nodes[0];
3097 slot = path->slots[0];
3098 write_extent_buffer(leaf, buf,
3099 btrfs_item_ptr_offset(leaf, slot),
3101 inode_add_bytes(inode, datal);
3104 btrfs_mark_buffer_dirty(leaf);
3105 btrfs_release_path(path);
3107 inode_inc_iversion(inode);
3108 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3111 * we round up to the block size at eof when
3112 * determining which extents to clone above,
3113 * but shouldn't round up the file size
3115 endoff = new_key.offset + datal;
3116 if (endoff > destoff+olen)
3117 endoff = destoff+olen;
3118 if (endoff > inode->i_size)
3119 btrfs_i_size_write(inode, endoff);
3121 ret = btrfs_update_inode(trans, root, inode);
3123 btrfs_abort_transaction(trans, root, ret);
3124 btrfs_end_transaction(trans, root);
3127 ret = btrfs_end_transaction(trans, root);
3129 btrfs_release_path(path);
3135 btrfs_release_path(path);
3136 btrfs_free_path(path);
3141 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3142 u64 off, u64 olen, u64 destoff)
3144 struct inode *inode = file_inode(file);
3145 struct btrfs_root *root = BTRFS_I(inode)->root;
3150 u64 bs = root->fs_info->sb->s_blocksize;
3155 * - split compressed inline extents. annoying: we need to
3156 * decompress into destination's address_space (the file offset
3157 * may change, so source mapping won't do), then recompress (or
3158 * otherwise reinsert) a subrange.
3159 * - allow ranges within the same file to be cloned (provided
3160 * they don't overlap)?
3163 /* the destination must be opened for writing */
3164 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3167 if (btrfs_root_readonly(root))
3170 ret = mnt_want_write_file(file);
3174 src_file = fdget(srcfd);
3175 if (!src_file.file) {
3177 goto out_drop_write;
3181 if (src_file.file->f_path.mnt != file->f_path.mnt)
3184 src = file_inode(src_file.file);
3190 /* the src must be open for reading */
3191 if (!(src_file.file->f_mode & FMODE_READ))
3194 /* don't make the dst file partly checksummed */
3195 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3196 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3200 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3204 if (src->i_sb != inode->i_sb)
3209 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3210 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3212 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3213 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3216 mutex_lock(&src->i_mutex);
3219 /* determine range to clone */
3221 if (off + len > src->i_size || off + len < off)
3224 olen = len = src->i_size - off;
3225 /* if we extend to eof, continue to block boundary */
3226 if (off + len == src->i_size)
3227 len = ALIGN(src->i_size, bs) - off;
3229 /* verify the end result is block aligned */
3230 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3231 !IS_ALIGNED(destoff, bs))
3234 /* verify if ranges are overlapped within the same file */
3236 if (destoff + len > off && destoff < off + len)
3240 if (destoff > inode->i_size) {
3241 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3246 /* truncate page cache pages from target inode range */
3247 truncate_inode_pages_range(&inode->i_data, destoff,
3248 PAGE_CACHE_ALIGN(destoff + len) - 1);
3250 lock_extent_range(src, off, len);
3252 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3254 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3258 mutex_unlock(&src->i_mutex);
3259 mutex_unlock(&inode->i_mutex);
3261 mutex_unlock(&inode->i_mutex);
3262 mutex_unlock(&src->i_mutex);
3265 mutex_unlock(&src->i_mutex);
3270 mnt_drop_write_file(file);
3274 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3276 struct btrfs_ioctl_clone_range_args args;
3278 if (copy_from_user(&args, argp, sizeof(args)))
3280 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3281 args.src_length, args.dest_offset);
3285 * there are many ways the trans_start and trans_end ioctls can lead
3286 * to deadlocks. They should only be used by applications that
3287 * basically own the machine, and have a very in depth understanding
3288 * of all the possible deadlocks and enospc problems.
3290 static long btrfs_ioctl_trans_start(struct file *file)
3292 struct inode *inode = file_inode(file);
3293 struct btrfs_root *root = BTRFS_I(inode)->root;
3294 struct btrfs_trans_handle *trans;
3298 if (!capable(CAP_SYS_ADMIN))
3302 if (file->private_data)
3306 if (btrfs_root_readonly(root))
3309 ret = mnt_want_write_file(file);
3313 atomic_inc(&root->fs_info->open_ioctl_trans);
3316 trans = btrfs_start_ioctl_transaction(root);
3320 file->private_data = trans;
3324 atomic_dec(&root->fs_info->open_ioctl_trans);
3325 mnt_drop_write_file(file);
3330 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3332 struct inode *inode = file_inode(file);
3333 struct btrfs_root *root = BTRFS_I(inode)->root;
3334 struct btrfs_root *new_root;
3335 struct btrfs_dir_item *di;
3336 struct btrfs_trans_handle *trans;
3337 struct btrfs_path *path;
3338 struct btrfs_key location;
3339 struct btrfs_disk_key disk_key;
3344 if (!capable(CAP_SYS_ADMIN))
3347 ret = mnt_want_write_file(file);
3351 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3357 objectid = BTRFS_FS_TREE_OBJECTID;
3359 location.objectid = objectid;
3360 location.type = BTRFS_ROOT_ITEM_KEY;
3361 location.offset = (u64)-1;
3363 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
3364 if (IS_ERR(new_root)) {
3365 ret = PTR_ERR(new_root);
3369 path = btrfs_alloc_path();
3374 path->leave_spinning = 1;
3376 trans = btrfs_start_transaction(root, 1);
3377 if (IS_ERR(trans)) {
3378 btrfs_free_path(path);
3379 ret = PTR_ERR(trans);
3383 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
3384 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3385 dir_id, "default", 7, 1);
3386 if (IS_ERR_OR_NULL(di)) {
3387 btrfs_free_path(path);
3388 btrfs_end_transaction(trans, root);
3389 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3390 "item, this isn't going to work");
3395 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3396 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3397 btrfs_mark_buffer_dirty(path->nodes[0]);
3398 btrfs_free_path(path);
3400 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
3401 btrfs_end_transaction(trans, root);
3403 mnt_drop_write_file(file);
3407 void btrfs_get_block_group_info(struct list_head *groups_list,
3408 struct btrfs_ioctl_space_info *space)
3410 struct btrfs_block_group_cache *block_group;
3412 space->total_bytes = 0;
3413 space->used_bytes = 0;
3415 list_for_each_entry(block_group, groups_list, list) {
3416 space->flags = block_group->flags;
3417 space->total_bytes += block_group->key.offset;
3418 space->used_bytes +=
3419 btrfs_block_group_used(&block_group->item);
3423 static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
3425 struct btrfs_ioctl_space_args space_args;
3426 struct btrfs_ioctl_space_info space;
3427 struct btrfs_ioctl_space_info *dest;
3428 struct btrfs_ioctl_space_info *dest_orig;
3429 struct btrfs_ioctl_space_info __user *user_dest;
3430 struct btrfs_space_info *info;
3431 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3432 BTRFS_BLOCK_GROUP_SYSTEM,
3433 BTRFS_BLOCK_GROUP_METADATA,
3434 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3441 if (copy_from_user(&space_args,
3442 (struct btrfs_ioctl_space_args __user *)arg,
3443 sizeof(space_args)))
3446 for (i = 0; i < num_types; i++) {
3447 struct btrfs_space_info *tmp;
3451 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3453 if (tmp->flags == types[i]) {
3463 down_read(&info->groups_sem);
3464 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3465 if (!list_empty(&info->block_groups[c]))
3468 up_read(&info->groups_sem);
3471 /* space_slots == 0 means they are asking for a count */
3472 if (space_args.space_slots == 0) {
3473 space_args.total_spaces = slot_count;
3477 slot_count = min_t(u64, space_args.space_slots, slot_count);
3479 alloc_size = sizeof(*dest) * slot_count;
3481 /* we generally have at most 6 or so space infos, one for each raid
3482 * level. So, a whole page should be more than enough for everyone
3484 if (alloc_size > PAGE_CACHE_SIZE)
3487 space_args.total_spaces = 0;
3488 dest = kmalloc(alloc_size, GFP_NOFS);
3493 /* now we have a buffer to copy into */
3494 for (i = 0; i < num_types; i++) {
3495 struct btrfs_space_info *tmp;
3502 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3504 if (tmp->flags == types[i]) {
3513 down_read(&info->groups_sem);
3514 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3515 if (!list_empty(&info->block_groups[c])) {
3516 btrfs_get_block_group_info(
3517 &info->block_groups[c], &space);
3518 memcpy(dest, &space, sizeof(space));
3520 space_args.total_spaces++;
3526 up_read(&info->groups_sem);
3529 user_dest = (struct btrfs_ioctl_space_info __user *)
3530 (arg + sizeof(struct btrfs_ioctl_space_args));
3532 if (copy_to_user(user_dest, dest_orig, alloc_size))
3537 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3544 * there are many ways the trans_start and trans_end ioctls can lead
3545 * to deadlocks. They should only be used by applications that
3546 * basically own the machine, and have a very in depth understanding
3547 * of all the possible deadlocks and enospc problems.
3549 long btrfs_ioctl_trans_end(struct file *file)
3551 struct inode *inode = file_inode(file);
3552 struct btrfs_root *root = BTRFS_I(inode)->root;
3553 struct btrfs_trans_handle *trans;
3555 trans = file->private_data;
3558 file->private_data = NULL;
3560 btrfs_end_transaction(trans, root);
3562 atomic_dec(&root->fs_info->open_ioctl_trans);
3564 mnt_drop_write_file(file);
3568 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3571 struct btrfs_trans_handle *trans;
3575 trans = btrfs_attach_transaction_barrier(root);
3576 if (IS_ERR(trans)) {
3577 if (PTR_ERR(trans) != -ENOENT)
3578 return PTR_ERR(trans);
3580 /* No running transaction, don't bother */
3581 transid = root->fs_info->last_trans_committed;
3584 transid = trans->transid;
3585 ret = btrfs_commit_transaction_async(trans, root, 0);
3587 btrfs_end_transaction(trans, root);
3592 if (copy_to_user(argp, &transid, sizeof(transid)))
3597 static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3603 if (copy_from_user(&transid, argp, sizeof(transid)))
3606 transid = 0; /* current trans */
3608 return btrfs_wait_for_commit(root, transid);
3611 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3613 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3614 struct btrfs_ioctl_scrub_args *sa;
3617 if (!capable(CAP_SYS_ADMIN))
3620 sa = memdup_user(arg, sizeof(*sa));
3624 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3625 ret = mnt_want_write_file(file);
3630 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
3631 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3634 if (copy_to_user(arg, sa, sizeof(*sa)))
3637 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3638 mnt_drop_write_file(file);
3644 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3646 if (!capable(CAP_SYS_ADMIN))
3649 return btrfs_scrub_cancel(root->fs_info);
3652 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3655 struct btrfs_ioctl_scrub_args *sa;
3658 if (!capable(CAP_SYS_ADMIN))
3661 sa = memdup_user(arg, sizeof(*sa));
3665 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3667 if (copy_to_user(arg, sa, sizeof(*sa)))
3674 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3677 struct btrfs_ioctl_get_dev_stats *sa;
3680 sa = memdup_user(arg, sizeof(*sa));
3684 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3689 ret = btrfs_get_dev_stats(root, sa);
3691 if (copy_to_user(arg, sa, sizeof(*sa)))
3698 static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3700 struct btrfs_ioctl_dev_replace_args *p;
3703 if (!capable(CAP_SYS_ADMIN))
3706 p = memdup_user(arg, sizeof(*p));
3711 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3712 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3717 &root->fs_info->mutually_exclusive_operation_running,
3719 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3721 ret = btrfs_dev_replace_start(root, p);
3723 &root->fs_info->mutually_exclusive_operation_running,
3727 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3728 btrfs_dev_replace_status(root->fs_info, p);
3731 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3732 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3739 if (copy_to_user(arg, p, sizeof(*p)))
3746 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3752 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3753 struct inode_fs_paths *ipath = NULL;
3754 struct btrfs_path *path;
3756 if (!capable(CAP_DAC_READ_SEARCH))
3759 path = btrfs_alloc_path();
3765 ipa = memdup_user(arg, sizeof(*ipa));
3772 size = min_t(u32, ipa->size, 4096);
3773 ipath = init_ipath(size, root, path);
3774 if (IS_ERR(ipath)) {
3775 ret = PTR_ERR(ipath);
3780 ret = paths_from_inode(ipa->inum, ipath);
3784 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3785 rel_ptr = ipath->fspath->val[i] -
3786 (u64)(unsigned long)ipath->fspath->val;
3787 ipath->fspath->val[i] = rel_ptr;
3790 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3791 (void *)(unsigned long)ipath->fspath, size);
3798 btrfs_free_path(path);
3805 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3807 struct btrfs_data_container *inodes = ctx;
3808 const size_t c = 3 * sizeof(u64);
3810 if (inodes->bytes_left >= c) {
3811 inodes->bytes_left -= c;
3812 inodes->val[inodes->elem_cnt] = inum;
3813 inodes->val[inodes->elem_cnt + 1] = offset;
3814 inodes->val[inodes->elem_cnt + 2] = root;
3815 inodes->elem_cnt += 3;
3817 inodes->bytes_missing += c - inodes->bytes_left;
3818 inodes->bytes_left = 0;
3819 inodes->elem_missed += 3;
3825 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3830 struct btrfs_ioctl_logical_ino_args *loi;
3831 struct btrfs_data_container *inodes = NULL;
3832 struct btrfs_path *path = NULL;
3834 if (!capable(CAP_SYS_ADMIN))
3837 loi = memdup_user(arg, sizeof(*loi));
3844 path = btrfs_alloc_path();
3850 size = min_t(u32, loi->size, 64 * 1024);
3851 inodes = init_data_container(size);
3852 if (IS_ERR(inodes)) {
3853 ret = PTR_ERR(inodes);
3858 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3859 build_ino_list, inodes);
3865 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3866 (void *)(unsigned long)inodes, size);
3871 btrfs_free_path(path);
3878 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3879 struct btrfs_ioctl_balance_args *bargs)
3881 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3883 bargs->flags = bctl->flags;
3885 if (atomic_read(&fs_info->balance_running))
3886 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3887 if (atomic_read(&fs_info->balance_pause_req))
3888 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3889 if (atomic_read(&fs_info->balance_cancel_req))
3890 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3892 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3893 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3894 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3897 spin_lock(&fs_info->balance_lock);
3898 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3899 spin_unlock(&fs_info->balance_lock);
3901 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3905 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3907 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3908 struct btrfs_fs_info *fs_info = root->fs_info;
3909 struct btrfs_ioctl_balance_args *bargs;
3910 struct btrfs_balance_control *bctl;
3911 bool need_unlock; /* for mut. excl. ops lock */
3914 if (!capable(CAP_SYS_ADMIN))
3917 ret = mnt_want_write_file(file);
3922 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3923 mutex_lock(&fs_info->volume_mutex);
3924 mutex_lock(&fs_info->balance_mutex);
3930 * mut. excl. ops lock is locked. Three possibilites:
3931 * (1) some other op is running
3932 * (2) balance is running
3933 * (3) balance is paused -- special case (think resume)
3935 mutex_lock(&fs_info->balance_mutex);
3936 if (fs_info->balance_ctl) {
3937 /* this is either (2) or (3) */
3938 if (!atomic_read(&fs_info->balance_running)) {
3939 mutex_unlock(&fs_info->balance_mutex);
3940 if (!mutex_trylock(&fs_info->volume_mutex))
3942 mutex_lock(&fs_info->balance_mutex);
3944 if (fs_info->balance_ctl &&
3945 !atomic_read(&fs_info->balance_running)) {
3947 need_unlock = false;
3951 mutex_unlock(&fs_info->balance_mutex);
3952 mutex_unlock(&fs_info->volume_mutex);
3956 mutex_unlock(&fs_info->balance_mutex);
3962 mutex_unlock(&fs_info->balance_mutex);
3963 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3968 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3971 bargs = memdup_user(arg, sizeof(*bargs));
3972 if (IS_ERR(bargs)) {
3973 ret = PTR_ERR(bargs);
3977 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3978 if (!fs_info->balance_ctl) {
3983 bctl = fs_info->balance_ctl;
3984 spin_lock(&fs_info->balance_lock);
3985 bctl->flags |= BTRFS_BALANCE_RESUME;
3986 spin_unlock(&fs_info->balance_lock);
3994 if (fs_info->balance_ctl) {
3999 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4005 bctl->fs_info = fs_info;
4007 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4008 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4009 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4011 bctl->flags = bargs->flags;
4013 /* balance everything - no filters */
4014 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4019 * Ownership of bctl and mutually_exclusive_operation_running
4020 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4021 * or, if restriper was paused all the way until unmount, in
4022 * free_fs_info. mutually_exclusive_operation_running is
4023 * cleared in __cancel_balance.
4025 need_unlock = false;
4027 ret = btrfs_balance(bctl, bargs);
4030 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4037 mutex_unlock(&fs_info->balance_mutex);
4038 mutex_unlock(&fs_info->volume_mutex);
4040 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4042 mnt_drop_write_file(file);
4046 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4048 if (!capable(CAP_SYS_ADMIN))
4052 case BTRFS_BALANCE_CTL_PAUSE:
4053 return btrfs_pause_balance(root->fs_info);
4054 case BTRFS_BALANCE_CTL_CANCEL:
4055 return btrfs_cancel_balance(root->fs_info);
4061 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4064 struct btrfs_fs_info *fs_info = root->fs_info;
4065 struct btrfs_ioctl_balance_args *bargs;
4068 if (!capable(CAP_SYS_ADMIN))
4071 mutex_lock(&fs_info->balance_mutex);
4072 if (!fs_info->balance_ctl) {
4077 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4083 update_ioctl_balance_args(fs_info, 1, bargs);
4085 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4090 mutex_unlock(&fs_info->balance_mutex);
4094 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4096 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4097 struct btrfs_ioctl_quota_ctl_args *sa;
4098 struct btrfs_trans_handle *trans = NULL;
4102 if (!capable(CAP_SYS_ADMIN))
4105 ret = mnt_want_write_file(file);
4109 sa = memdup_user(arg, sizeof(*sa));
4115 down_write(&root->fs_info->subvol_sem);
4116 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4117 if (IS_ERR(trans)) {
4118 ret = PTR_ERR(trans);
4123 case BTRFS_QUOTA_CTL_ENABLE:
4124 ret = btrfs_quota_enable(trans, root->fs_info);
4126 case BTRFS_QUOTA_CTL_DISABLE:
4127 ret = btrfs_quota_disable(trans, root->fs_info);
4134 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4139 up_write(&root->fs_info->subvol_sem);
4141 mnt_drop_write_file(file);
4145 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4147 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4148 struct btrfs_ioctl_qgroup_assign_args *sa;
4149 struct btrfs_trans_handle *trans;
4153 if (!capable(CAP_SYS_ADMIN))
4156 ret = mnt_want_write_file(file);
4160 sa = memdup_user(arg, sizeof(*sa));
4166 trans = btrfs_join_transaction(root);
4167 if (IS_ERR(trans)) {
4168 ret = PTR_ERR(trans);
4172 /* FIXME: check if the IDs really exist */
4174 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4177 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4181 err = btrfs_end_transaction(trans, root);
4188 mnt_drop_write_file(file);
4192 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4194 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4195 struct btrfs_ioctl_qgroup_create_args *sa;
4196 struct btrfs_trans_handle *trans;
4200 if (!capable(CAP_SYS_ADMIN))
4203 ret = mnt_want_write_file(file);
4207 sa = memdup_user(arg, sizeof(*sa));
4213 if (!sa->qgroupid) {
4218 trans = btrfs_join_transaction(root);
4219 if (IS_ERR(trans)) {
4220 ret = PTR_ERR(trans);
4224 /* FIXME: check if the IDs really exist */
4226 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4229 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4232 err = btrfs_end_transaction(trans, root);
4239 mnt_drop_write_file(file);
4243 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4245 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4246 struct btrfs_ioctl_qgroup_limit_args *sa;
4247 struct btrfs_trans_handle *trans;
4252 if (!capable(CAP_SYS_ADMIN))
4255 ret = mnt_want_write_file(file);
4259 sa = memdup_user(arg, sizeof(*sa));
4265 trans = btrfs_join_transaction(root);
4266 if (IS_ERR(trans)) {
4267 ret = PTR_ERR(trans);
4271 qgroupid = sa->qgroupid;
4273 /* take the current subvol as qgroup */
4274 qgroupid = root->root_key.objectid;
4277 /* FIXME: check if the IDs really exist */
4278 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4280 err = btrfs_end_transaction(trans, root);
4287 mnt_drop_write_file(file);
4291 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4293 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4294 struct btrfs_ioctl_quota_rescan_args *qsa;
4297 if (!capable(CAP_SYS_ADMIN))
4300 ret = mnt_want_write_file(file);
4304 qsa = memdup_user(arg, sizeof(*qsa));
4315 ret = btrfs_qgroup_rescan(root->fs_info);
4320 mnt_drop_write_file(file);
4324 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4326 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4327 struct btrfs_ioctl_quota_rescan_args *qsa;
4330 if (!capable(CAP_SYS_ADMIN))
4333 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4337 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4339 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4342 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4349 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4351 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4353 if (!capable(CAP_SYS_ADMIN))
4356 return btrfs_qgroup_wait_for_completion(root->fs_info);
4359 static long btrfs_ioctl_set_received_subvol(struct file *file,
4362 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4363 struct inode *inode = file_inode(file);
4364 struct btrfs_root *root = BTRFS_I(inode)->root;
4365 struct btrfs_root_item *root_item = &root->root_item;
4366 struct btrfs_trans_handle *trans;
4367 struct timespec ct = CURRENT_TIME;
4369 int received_uuid_changed;
4371 if (!inode_owner_or_capable(inode))
4374 ret = mnt_want_write_file(file);
4378 down_write(&root->fs_info->subvol_sem);
4380 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4385 if (btrfs_root_readonly(root)) {
4390 sa = memdup_user(arg, sizeof(*sa));
4399 * 2 - uuid items (received uuid + subvol uuid)
4401 trans = btrfs_start_transaction(root, 3);
4402 if (IS_ERR(trans)) {
4403 ret = PTR_ERR(trans);
4408 sa->rtransid = trans->transid;
4409 sa->rtime.sec = ct.tv_sec;
4410 sa->rtime.nsec = ct.tv_nsec;
4412 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4414 if (received_uuid_changed &&
4415 !btrfs_is_empty_uuid(root_item->received_uuid))
4416 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4417 root_item->received_uuid,
4418 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4419 root->root_key.objectid);
4420 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4421 btrfs_set_root_stransid(root_item, sa->stransid);
4422 btrfs_set_root_rtransid(root_item, sa->rtransid);
4423 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4424 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4425 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4426 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4428 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4429 &root->root_key, &root->root_item);
4431 btrfs_end_transaction(trans, root);
4434 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4435 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4437 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4438 root->root_key.objectid);
4439 if (ret < 0 && ret != -EEXIST) {
4440 btrfs_abort_transaction(trans, root, ret);
4444 ret = btrfs_commit_transaction(trans, root);
4446 btrfs_abort_transaction(trans, root, ret);
4450 ret = copy_to_user(arg, sa, sizeof(*sa));
4456 up_write(&root->fs_info->subvol_sem);
4457 mnt_drop_write_file(file);
4461 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4463 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4466 char label[BTRFS_LABEL_SIZE];
4468 spin_lock(&root->fs_info->super_lock);
4469 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4470 spin_unlock(&root->fs_info->super_lock);
4472 len = strnlen(label, BTRFS_LABEL_SIZE);
4474 if (len == BTRFS_LABEL_SIZE) {
4475 btrfs_warn(root->fs_info,
4476 "label is too long, return the first %zu bytes", --len);
4479 ret = copy_to_user(arg, label, len);
4481 return ret ? -EFAULT : 0;
4484 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4486 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4487 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4488 struct btrfs_trans_handle *trans;
4489 char label[BTRFS_LABEL_SIZE];
4492 if (!capable(CAP_SYS_ADMIN))
4495 if (copy_from_user(label, arg, sizeof(label)))
4498 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4499 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
4500 BTRFS_LABEL_SIZE - 1);
4504 ret = mnt_want_write_file(file);
4508 trans = btrfs_start_transaction(root, 0);
4509 if (IS_ERR(trans)) {
4510 ret = PTR_ERR(trans);
4514 spin_lock(&root->fs_info->super_lock);
4515 strcpy(super_block->label, label);
4516 spin_unlock(&root->fs_info->super_lock);
4517 ret = btrfs_commit_transaction(trans, root);
4520 mnt_drop_write_file(file);
4524 #define INIT_FEATURE_FLAGS(suffix) \
4525 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4526 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4527 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4529 static int btrfs_ioctl_get_supported_features(struct file *file,
4532 static struct btrfs_ioctl_feature_flags features[3] = {
4533 INIT_FEATURE_FLAGS(SUPP),
4534 INIT_FEATURE_FLAGS(SAFE_SET),
4535 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4538 if (copy_to_user(arg, &features, sizeof(features)))
4544 static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
4546 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4547 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4548 struct btrfs_ioctl_feature_flags features;
4550 features.compat_flags = btrfs_super_compat_flags(super_block);
4551 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4552 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4554 if (copy_to_user(arg, &features, sizeof(features)))
4560 static int check_feature_bits(struct btrfs_root *root,
4561 enum btrfs_feature_set set,
4562 u64 change_mask, u64 flags, u64 supported_flags,
4563 u64 safe_set, u64 safe_clear)
4565 const char *type = btrfs_feature_set_names[set];
4567 u64 disallowed, unsupported;
4568 u64 set_mask = flags & change_mask;
4569 u64 clear_mask = ~flags & change_mask;
4571 unsupported = set_mask & ~supported_flags;
4573 names = btrfs_printable_features(set, unsupported);
4575 btrfs_warn(root->fs_info,
4576 "this kernel does not support the %s feature bit%s",
4577 names, strchr(names, ',') ? "s" : "");
4580 btrfs_warn(root->fs_info,
4581 "this kernel does not support %s bits 0x%llx",
4586 disallowed = set_mask & ~safe_set;
4588 names = btrfs_printable_features(set, disallowed);
4590 btrfs_warn(root->fs_info,
4591 "can't set the %s feature bit%s while mounted",
4592 names, strchr(names, ',') ? "s" : "");
4595 btrfs_warn(root->fs_info,
4596 "can't set %s bits 0x%llx while mounted",
4601 disallowed = clear_mask & ~safe_clear;
4603 names = btrfs_printable_features(set, disallowed);
4605 btrfs_warn(root->fs_info,
4606 "can't clear the %s feature bit%s while mounted",
4607 names, strchr(names, ',') ? "s" : "");
4610 btrfs_warn(root->fs_info,
4611 "can't clear %s bits 0x%llx while mounted",
4619 #define check_feature(root, change_mask, flags, mask_base) \
4620 check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
4621 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4622 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4623 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4625 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4627 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4628 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4629 struct btrfs_ioctl_feature_flags flags[2];
4630 struct btrfs_trans_handle *trans;
4634 if (!capable(CAP_SYS_ADMIN))
4637 if (copy_from_user(flags, arg, sizeof(flags)))
4641 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4642 !flags[0].incompat_flags)
4645 ret = check_feature(root, flags[0].compat_flags,
4646 flags[1].compat_flags, COMPAT);
4650 ret = check_feature(root, flags[0].compat_ro_flags,
4651 flags[1].compat_ro_flags, COMPAT_RO);
4655 ret = check_feature(root, flags[0].incompat_flags,
4656 flags[1].incompat_flags, INCOMPAT);
4660 trans = btrfs_start_transaction(root, 0);
4662 return PTR_ERR(trans);
4664 spin_lock(&root->fs_info->super_lock);
4665 newflags = btrfs_super_compat_flags(super_block);
4666 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4667 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4668 btrfs_set_super_compat_flags(super_block, newflags);
4670 newflags = btrfs_super_compat_ro_flags(super_block);
4671 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4672 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4673 btrfs_set_super_compat_ro_flags(super_block, newflags);
4675 newflags = btrfs_super_incompat_flags(super_block);
4676 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4677 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4678 btrfs_set_super_incompat_flags(super_block, newflags);
4679 spin_unlock(&root->fs_info->super_lock);
4681 return btrfs_commit_transaction(trans, root);
4684 long btrfs_ioctl(struct file *file, unsigned int
4685 cmd, unsigned long arg)
4687 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4688 void __user *argp = (void __user *)arg;
4691 case FS_IOC_GETFLAGS:
4692 return btrfs_ioctl_getflags(file, argp);
4693 case FS_IOC_SETFLAGS:
4694 return btrfs_ioctl_setflags(file, argp);
4695 case FS_IOC_GETVERSION:
4696 return btrfs_ioctl_getversion(file, argp);
4698 return btrfs_ioctl_fitrim(file, argp);
4699 case BTRFS_IOC_SNAP_CREATE:
4700 return btrfs_ioctl_snap_create(file, argp, 0);
4701 case BTRFS_IOC_SNAP_CREATE_V2:
4702 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4703 case BTRFS_IOC_SUBVOL_CREATE:
4704 return btrfs_ioctl_snap_create(file, argp, 1);
4705 case BTRFS_IOC_SUBVOL_CREATE_V2:
4706 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4707 case BTRFS_IOC_SNAP_DESTROY:
4708 return btrfs_ioctl_snap_destroy(file, argp);
4709 case BTRFS_IOC_SUBVOL_GETFLAGS:
4710 return btrfs_ioctl_subvol_getflags(file, argp);
4711 case BTRFS_IOC_SUBVOL_SETFLAGS:
4712 return btrfs_ioctl_subvol_setflags(file, argp);
4713 case BTRFS_IOC_DEFAULT_SUBVOL:
4714 return btrfs_ioctl_default_subvol(file, argp);
4715 case BTRFS_IOC_DEFRAG:
4716 return btrfs_ioctl_defrag(file, NULL);
4717 case BTRFS_IOC_DEFRAG_RANGE:
4718 return btrfs_ioctl_defrag(file, argp);
4719 case BTRFS_IOC_RESIZE:
4720 return btrfs_ioctl_resize(file, argp);
4721 case BTRFS_IOC_ADD_DEV:
4722 return btrfs_ioctl_add_dev(root, argp);
4723 case BTRFS_IOC_RM_DEV:
4724 return btrfs_ioctl_rm_dev(file, argp);
4725 case BTRFS_IOC_FS_INFO:
4726 return btrfs_ioctl_fs_info(root, argp);
4727 case BTRFS_IOC_DEV_INFO:
4728 return btrfs_ioctl_dev_info(root, argp);
4729 case BTRFS_IOC_BALANCE:
4730 return btrfs_ioctl_balance(file, NULL);
4731 case BTRFS_IOC_CLONE:
4732 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4733 case BTRFS_IOC_CLONE_RANGE:
4734 return btrfs_ioctl_clone_range(file, argp);
4735 case BTRFS_IOC_TRANS_START:
4736 return btrfs_ioctl_trans_start(file);
4737 case BTRFS_IOC_TRANS_END:
4738 return btrfs_ioctl_trans_end(file);
4739 case BTRFS_IOC_TREE_SEARCH:
4740 return btrfs_ioctl_tree_search(file, argp);
4741 case BTRFS_IOC_INO_LOOKUP:
4742 return btrfs_ioctl_ino_lookup(file, argp);
4743 case BTRFS_IOC_INO_PATHS:
4744 return btrfs_ioctl_ino_to_path(root, argp);
4745 case BTRFS_IOC_LOGICAL_INO:
4746 return btrfs_ioctl_logical_to_ino(root, argp);
4747 case BTRFS_IOC_SPACE_INFO:
4748 return btrfs_ioctl_space_info(root, argp);
4749 case BTRFS_IOC_SYNC: {
4752 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
4755 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4757 * The transaction thread may want to do more work,
4758 * namely it pokes the cleaner ktread that will start
4759 * processing uncleaned subvols.
4761 wake_up_process(root->fs_info->transaction_kthread);
4764 case BTRFS_IOC_START_SYNC:
4765 return btrfs_ioctl_start_sync(root, argp);
4766 case BTRFS_IOC_WAIT_SYNC:
4767 return btrfs_ioctl_wait_sync(root, argp);
4768 case BTRFS_IOC_SCRUB:
4769 return btrfs_ioctl_scrub(file, argp);
4770 case BTRFS_IOC_SCRUB_CANCEL:
4771 return btrfs_ioctl_scrub_cancel(root, argp);
4772 case BTRFS_IOC_SCRUB_PROGRESS:
4773 return btrfs_ioctl_scrub_progress(root, argp);
4774 case BTRFS_IOC_BALANCE_V2:
4775 return btrfs_ioctl_balance(file, argp);
4776 case BTRFS_IOC_BALANCE_CTL:
4777 return btrfs_ioctl_balance_ctl(root, arg);
4778 case BTRFS_IOC_BALANCE_PROGRESS:
4779 return btrfs_ioctl_balance_progress(root, argp);
4780 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4781 return btrfs_ioctl_set_received_subvol(file, argp);
4782 case BTRFS_IOC_SEND:
4783 return btrfs_ioctl_send(file, argp);
4784 case BTRFS_IOC_GET_DEV_STATS:
4785 return btrfs_ioctl_get_dev_stats(root, argp);
4786 case BTRFS_IOC_QUOTA_CTL:
4787 return btrfs_ioctl_quota_ctl(file, argp);
4788 case BTRFS_IOC_QGROUP_ASSIGN:
4789 return btrfs_ioctl_qgroup_assign(file, argp);
4790 case BTRFS_IOC_QGROUP_CREATE:
4791 return btrfs_ioctl_qgroup_create(file, argp);
4792 case BTRFS_IOC_QGROUP_LIMIT:
4793 return btrfs_ioctl_qgroup_limit(file, argp);
4794 case BTRFS_IOC_QUOTA_RESCAN:
4795 return btrfs_ioctl_quota_rescan(file, argp);
4796 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4797 return btrfs_ioctl_quota_rescan_status(file, argp);
4798 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4799 return btrfs_ioctl_quota_rescan_wait(file, argp);
4800 case BTRFS_IOC_DEV_REPLACE:
4801 return btrfs_ioctl_dev_replace(root, argp);
4802 case BTRFS_IOC_GET_FSLABEL:
4803 return btrfs_ioctl_get_fslabel(file, argp);
4804 case BTRFS_IOC_SET_FSLABEL:
4805 return btrfs_ioctl_set_fslabel(file, argp);
4806 case BTRFS_IOC_FILE_EXTENT_SAME:
4807 return btrfs_ioctl_file_extent_same(file, argp);
4808 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
4809 return btrfs_ioctl_get_supported_features(file, argp);
4810 case BTRFS_IOC_GET_FEATURES:
4811 return btrfs_ioctl_get_features(file, argp);
4812 case BTRFS_IOC_SET_FEATURES:
4813 return btrfs_ioctl_set_features(file, argp);