1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/kernel.h>
8 #include <linux/file.h>
10 #include <linux/fsnotify.h>
11 #include <linux/pagemap.h>
12 #include <linux/highmem.h>
13 #include <linux/time.h>
14 #include <linux/string.h>
15 #include <linux/backing-dev.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/writeback.h>
19 #include <linux/compat.h>
20 #include <linux/security.h>
21 #include <linux/xattr.h>
23 #include <linux/slab.h>
24 #include <linux/blkdev.h>
25 #include <linux/uuid.h>
26 #include <linux/btrfs.h>
27 #include <linux/uaccess.h>
28 #include <linux/iversion.h>
32 #include "transaction.h"
33 #include "btrfs_inode.h"
34 #include "print-tree.h"
38 #include "rcu-string.h"
40 #include "dev-replace.h"
45 #include "compression.h"
46 #include "space-info.h"
47 #include "delalloc-space.h"
48 #include "block-group.h"
51 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
52 * structures are incorrect, as the timespec structure from userspace
53 * is 4 bytes too small. We define these alternatives here to teach
54 * the kernel about the 32-bit struct packing.
56 struct btrfs_ioctl_timespec_32 {
59 } __attribute__ ((__packed__));
61 struct btrfs_ioctl_received_subvol_args_32 {
62 char uuid[BTRFS_UUID_SIZE]; /* in */
63 __u64 stransid; /* in */
64 __u64 rtransid; /* out */
65 struct btrfs_ioctl_timespec_32 stime; /* in */
66 struct btrfs_ioctl_timespec_32 rtime; /* out */
68 __u64 reserved[16]; /* in */
69 } __attribute__ ((__packed__));
71 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
72 struct btrfs_ioctl_received_subvol_args_32)
75 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
76 struct btrfs_ioctl_send_args_32 {
77 __s64 send_fd; /* in */
78 __u64 clone_sources_count; /* in */
79 compat_uptr_t clone_sources; /* in */
80 __u64 parent_root; /* in */
82 __u64 reserved[4]; /* in */
83 } __attribute__ ((__packed__));
85 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
86 struct btrfs_ioctl_send_args_32)
89 /* Mask out flags that are inappropriate for the given type of inode. */
90 static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
93 if (S_ISDIR(inode->i_mode))
95 else if (S_ISREG(inode->i_mode))
96 return flags & ~FS_DIRSYNC_FL;
98 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
102 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
105 static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
107 unsigned int iflags = 0;
109 if (flags & BTRFS_INODE_SYNC)
110 iflags |= FS_SYNC_FL;
111 if (flags & BTRFS_INODE_IMMUTABLE)
112 iflags |= FS_IMMUTABLE_FL;
113 if (flags & BTRFS_INODE_APPEND)
114 iflags |= FS_APPEND_FL;
115 if (flags & BTRFS_INODE_NODUMP)
116 iflags |= FS_NODUMP_FL;
117 if (flags & BTRFS_INODE_NOATIME)
118 iflags |= FS_NOATIME_FL;
119 if (flags & BTRFS_INODE_DIRSYNC)
120 iflags |= FS_DIRSYNC_FL;
121 if (flags & BTRFS_INODE_NODATACOW)
122 iflags |= FS_NOCOW_FL;
124 if (flags & BTRFS_INODE_NOCOMPRESS)
125 iflags |= FS_NOCOMP_FL;
126 else if (flags & BTRFS_INODE_COMPRESS)
127 iflags |= FS_COMPR_FL;
133 * Update inode->i_flags based on the btrfs internal flags.
135 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
137 struct btrfs_inode *binode = BTRFS_I(inode);
138 unsigned int new_fl = 0;
140 if (binode->flags & BTRFS_INODE_SYNC)
142 if (binode->flags & BTRFS_INODE_IMMUTABLE)
143 new_fl |= S_IMMUTABLE;
144 if (binode->flags & BTRFS_INODE_APPEND)
146 if (binode->flags & BTRFS_INODE_NOATIME)
148 if (binode->flags & BTRFS_INODE_DIRSYNC)
151 set_mask_bits(&inode->i_flags,
152 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
156 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
159 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
161 if (copy_to_user(arg, &flags, sizeof(flags)))
167 * Check if @flags are a supported and valid set of FS_*_FL flags and that
168 * the old and new flags are not conflicting
170 static int check_fsflags(unsigned int old_flags, unsigned int flags)
172 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
173 FS_NOATIME_FL | FS_NODUMP_FL | \
174 FS_SYNC_FL | FS_DIRSYNC_FL | \
175 FS_NOCOMP_FL | FS_COMPR_FL |
179 /* COMPR and NOCOMP on new/old are valid */
180 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
183 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
186 /* NOCOW and compression options are mutually exclusive */
187 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
189 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
195 static int check_fsflags_compatible(struct btrfs_fs_info *fs_info,
198 if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL))
204 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
206 struct inode *inode = file_inode(file);
207 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
208 struct btrfs_inode *binode = BTRFS_I(inode);
209 struct btrfs_root *root = binode->root;
210 struct btrfs_trans_handle *trans;
211 unsigned int fsflags, old_fsflags;
213 const char *comp = NULL;
216 if (!inode_owner_or_capable(&init_user_ns, inode))
219 if (btrfs_root_readonly(root))
222 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
225 ret = mnt_want_write_file(file);
230 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
231 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
233 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
237 ret = check_fsflags(old_fsflags, fsflags);
241 ret = check_fsflags_compatible(fs_info, fsflags);
245 binode_flags = binode->flags;
246 if (fsflags & FS_SYNC_FL)
247 binode_flags |= BTRFS_INODE_SYNC;
249 binode_flags &= ~BTRFS_INODE_SYNC;
250 if (fsflags & FS_IMMUTABLE_FL)
251 binode_flags |= BTRFS_INODE_IMMUTABLE;
253 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
254 if (fsflags & FS_APPEND_FL)
255 binode_flags |= BTRFS_INODE_APPEND;
257 binode_flags &= ~BTRFS_INODE_APPEND;
258 if (fsflags & FS_NODUMP_FL)
259 binode_flags |= BTRFS_INODE_NODUMP;
261 binode_flags &= ~BTRFS_INODE_NODUMP;
262 if (fsflags & FS_NOATIME_FL)
263 binode_flags |= BTRFS_INODE_NOATIME;
265 binode_flags &= ~BTRFS_INODE_NOATIME;
266 if (fsflags & FS_DIRSYNC_FL)
267 binode_flags |= BTRFS_INODE_DIRSYNC;
269 binode_flags &= ~BTRFS_INODE_DIRSYNC;
270 if (fsflags & FS_NOCOW_FL) {
271 if (S_ISREG(inode->i_mode)) {
273 * It's safe to turn csums off here, no extents exist.
274 * Otherwise we want the flag to reflect the real COW
275 * status of the file and will not set it.
277 if (inode->i_size == 0)
278 binode_flags |= BTRFS_INODE_NODATACOW |
279 BTRFS_INODE_NODATASUM;
281 binode_flags |= BTRFS_INODE_NODATACOW;
285 * Revert back under same assumptions as above
287 if (S_ISREG(inode->i_mode)) {
288 if (inode->i_size == 0)
289 binode_flags &= ~(BTRFS_INODE_NODATACOW |
290 BTRFS_INODE_NODATASUM);
292 binode_flags &= ~BTRFS_INODE_NODATACOW;
297 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
298 * flag may be changed automatically if compression code won't make
301 if (fsflags & FS_NOCOMP_FL) {
302 binode_flags &= ~BTRFS_INODE_COMPRESS;
303 binode_flags |= BTRFS_INODE_NOCOMPRESS;
304 } else if (fsflags & FS_COMPR_FL) {
306 if (IS_SWAPFILE(inode)) {
311 binode_flags |= BTRFS_INODE_COMPRESS;
312 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
314 comp = btrfs_compress_type2str(fs_info->compress_type);
315 if (!comp || comp[0] == 0)
316 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
318 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
325 trans = btrfs_start_transaction(root, 3);
327 ret = PTR_ERR(trans);
332 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
335 btrfs_abort_transaction(trans, ret);
339 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
341 if (ret && ret != -ENODATA) {
342 btrfs_abort_transaction(trans, ret);
347 binode->flags = binode_flags;
348 btrfs_sync_inode_flags_to_i_flags(inode);
349 inode_inc_iversion(inode);
350 inode->i_ctime = current_time(inode);
351 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
354 btrfs_end_transaction(trans);
357 mnt_drop_write_file(file);
362 * Translate btrfs internal inode flags to xflags as expected by the
363 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
366 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
368 unsigned int xflags = 0;
370 if (flags & BTRFS_INODE_APPEND)
371 xflags |= FS_XFLAG_APPEND;
372 if (flags & BTRFS_INODE_IMMUTABLE)
373 xflags |= FS_XFLAG_IMMUTABLE;
374 if (flags & BTRFS_INODE_NOATIME)
375 xflags |= FS_XFLAG_NOATIME;
376 if (flags & BTRFS_INODE_NODUMP)
377 xflags |= FS_XFLAG_NODUMP;
378 if (flags & BTRFS_INODE_SYNC)
379 xflags |= FS_XFLAG_SYNC;
384 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
385 static int check_xflags(unsigned int flags)
387 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
388 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
393 bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
394 enum btrfs_exclusive_operation type)
396 return !cmpxchg(&fs_info->exclusive_operation, BTRFS_EXCLOP_NONE, type);
399 void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
401 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
402 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
406 * Set the xflags from the internal inode flags. The remaining items of fsxattr
409 static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
411 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
414 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
415 if (copy_to_user(arg, &fa, sizeof(fa)))
421 static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
423 struct inode *inode = file_inode(file);
424 struct btrfs_inode *binode = BTRFS_I(inode);
425 struct btrfs_root *root = binode->root;
426 struct btrfs_trans_handle *trans;
427 struct fsxattr fa, old_fa;
429 unsigned old_i_flags;
432 if (!inode_owner_or_capable(&init_user_ns, inode))
435 if (btrfs_root_readonly(root))
438 if (copy_from_user(&fa, arg, sizeof(fa)))
441 ret = check_xflags(fa.fsx_xflags);
445 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
448 ret = mnt_want_write_file(file);
454 old_flags = binode->flags;
455 old_i_flags = inode->i_flags;
457 simple_fill_fsxattr(&old_fa,
458 btrfs_inode_flags_to_xflags(binode->flags));
459 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
463 if (fa.fsx_xflags & FS_XFLAG_SYNC)
464 binode->flags |= BTRFS_INODE_SYNC;
466 binode->flags &= ~BTRFS_INODE_SYNC;
467 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
468 binode->flags |= BTRFS_INODE_IMMUTABLE;
470 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
471 if (fa.fsx_xflags & FS_XFLAG_APPEND)
472 binode->flags |= BTRFS_INODE_APPEND;
474 binode->flags &= ~BTRFS_INODE_APPEND;
475 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
476 binode->flags |= BTRFS_INODE_NODUMP;
478 binode->flags &= ~BTRFS_INODE_NODUMP;
479 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
480 binode->flags |= BTRFS_INODE_NOATIME;
482 binode->flags &= ~BTRFS_INODE_NOATIME;
484 /* 1 item for the inode */
485 trans = btrfs_start_transaction(root, 1);
487 ret = PTR_ERR(trans);
491 btrfs_sync_inode_flags_to_i_flags(inode);
492 inode_inc_iversion(inode);
493 inode->i_ctime = current_time(inode);
494 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
496 btrfs_end_transaction(trans);
500 binode->flags = old_flags;
501 inode->i_flags = old_i_flags;
505 mnt_drop_write_file(file);
510 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
512 struct inode *inode = file_inode(file);
514 return put_user(inode->i_generation, arg);
517 static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
520 struct btrfs_device *device;
521 struct request_queue *q;
522 struct fstrim_range range;
523 u64 minlen = ULLONG_MAX;
527 if (!capable(CAP_SYS_ADMIN))
531 * btrfs_trim_block_group() depends on space cache, which is not
532 * available in zoned filesystem. So, disallow fitrim on a zoned
533 * filesystem for now.
535 if (btrfs_is_zoned(fs_info))
539 * If the fs is mounted with nologreplay, which requires it to be
540 * mounted in RO mode as well, we can not allow discard on free space
541 * inside block groups, because log trees refer to extents that are not
542 * pinned in a block group's free space cache (pinning the extents is
543 * precisely the first phase of replaying a log tree).
545 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
549 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
553 q = bdev_get_queue(device->bdev);
554 if (blk_queue_discard(q)) {
556 minlen = min_t(u64, q->limits.discard_granularity,
564 if (copy_from_user(&range, arg, sizeof(range)))
568 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
569 * block group is in the logical address space, which can be any
570 * sectorsize aligned bytenr in the range [0, U64_MAX].
572 if (range.len < fs_info->sb->s_blocksize)
575 range.minlen = max(range.minlen, minlen);
576 ret = btrfs_trim_fs(fs_info, &range);
580 if (copy_to_user(arg, &range, sizeof(range)))
586 int __pure btrfs_is_empty_uuid(u8 *uuid)
590 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
597 static noinline int create_subvol(struct inode *dir,
598 struct dentry *dentry,
599 const char *name, int namelen,
600 struct btrfs_qgroup_inherit *inherit)
602 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
603 struct btrfs_trans_handle *trans;
604 struct btrfs_key key;
605 struct btrfs_root_item *root_item;
606 struct btrfs_inode_item *inode_item;
607 struct extent_buffer *leaf;
608 struct btrfs_root *root = BTRFS_I(dir)->root;
609 struct btrfs_root *new_root;
610 struct btrfs_block_rsv block_rsv;
611 struct timespec64 cur_time = current_time(dir);
619 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
623 ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
627 ret = get_anon_bdev(&anon_dev);
632 * Don't create subvolume whose level is not zero. Or qgroup will be
633 * screwed up since it assumes subvolume qgroup's level to be 0.
635 if (btrfs_qgroup_level(objectid)) {
640 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
642 * The same as the snapshot creation, please see the comment
643 * of create_snapshot().
645 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
649 trans = btrfs_start_transaction(root, 0);
651 ret = PTR_ERR(trans);
652 btrfs_subvolume_release_metadata(root, &block_rsv);
655 trans->block_rsv = &block_rsv;
656 trans->bytes_reserved = block_rsv.size;
658 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
662 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
663 BTRFS_NESTING_NORMAL);
669 btrfs_mark_buffer_dirty(leaf);
671 inode_item = &root_item->inode;
672 btrfs_set_stack_inode_generation(inode_item, 1);
673 btrfs_set_stack_inode_size(inode_item, 3);
674 btrfs_set_stack_inode_nlink(inode_item, 1);
675 btrfs_set_stack_inode_nbytes(inode_item,
677 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
679 btrfs_set_root_flags(root_item, 0);
680 btrfs_set_root_limit(root_item, 0);
681 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
683 btrfs_set_root_bytenr(root_item, leaf->start);
684 btrfs_set_root_generation(root_item, trans->transid);
685 btrfs_set_root_level(root_item, 0);
686 btrfs_set_root_refs(root_item, 1);
687 btrfs_set_root_used(root_item, leaf->len);
688 btrfs_set_root_last_snapshot(root_item, 0);
690 btrfs_set_root_generation_v2(root_item,
691 btrfs_root_generation(root_item));
692 generate_random_guid(root_item->uuid);
693 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
694 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
695 root_item->ctime = root_item->otime;
696 btrfs_set_root_ctransid(root_item, trans->transid);
697 btrfs_set_root_otransid(root_item, trans->transid);
699 btrfs_tree_unlock(leaf);
700 free_extent_buffer(leaf);
703 btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
705 key.objectid = objectid;
707 key.type = BTRFS_ROOT_ITEM_KEY;
708 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
713 key.offset = (u64)-1;
714 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
715 if (IS_ERR(new_root)) {
716 free_anon_bdev(anon_dev);
717 ret = PTR_ERR(new_root);
718 btrfs_abort_transaction(trans, ret);
721 /* Freeing will be done in btrfs_put_root() of new_root */
724 btrfs_record_root_in_trans(trans, new_root);
726 ret = btrfs_create_subvol_root(trans, new_root, root);
727 btrfs_put_root(new_root);
729 /* We potentially lose an unused inode item here */
730 btrfs_abort_transaction(trans, ret);
735 * insert the directory item
737 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
739 btrfs_abort_transaction(trans, ret);
743 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
744 BTRFS_FT_DIR, index);
746 btrfs_abort_transaction(trans, ret);
750 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
751 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
753 btrfs_abort_transaction(trans, ret);
757 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
758 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
760 btrfs_abort_transaction(trans, ret);
764 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
765 BTRFS_UUID_KEY_SUBVOL, objectid);
767 btrfs_abort_transaction(trans, ret);
771 trans->block_rsv = NULL;
772 trans->bytes_reserved = 0;
773 btrfs_subvolume_release_metadata(root, &block_rsv);
775 err = btrfs_commit_transaction(trans);
780 inode = btrfs_lookup_dentry(dir, dentry);
782 return PTR_ERR(inode);
783 d_instantiate(dentry, inode);
789 free_anon_bdev(anon_dev);
794 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
795 struct dentry *dentry, bool readonly,
796 struct btrfs_qgroup_inherit *inherit)
798 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
800 struct btrfs_pending_snapshot *pending_snapshot;
801 struct btrfs_trans_handle *trans;
804 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
807 if (atomic_read(&root->nr_swapfiles)) {
809 "cannot snapshot subvolume with active swapfile");
813 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
814 if (!pending_snapshot)
817 ret = get_anon_bdev(&pending_snapshot->anon_dev);
820 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
822 pending_snapshot->path = btrfs_alloc_path();
823 if (!pending_snapshot->root_item || !pending_snapshot->path) {
828 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
829 BTRFS_BLOCK_RSV_TEMP);
831 * 1 - parent dir inode
834 * 2 - root ref/backref
835 * 1 - root of snapshot
838 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
839 &pending_snapshot->block_rsv, 8,
844 pending_snapshot->dentry = dentry;
845 pending_snapshot->root = root;
846 pending_snapshot->readonly = readonly;
847 pending_snapshot->dir = dir;
848 pending_snapshot->inherit = inherit;
850 trans = btrfs_start_transaction(root, 0);
852 ret = PTR_ERR(trans);
856 spin_lock(&fs_info->trans_lock);
857 list_add(&pending_snapshot->list,
858 &trans->transaction->pending_snapshots);
859 spin_unlock(&fs_info->trans_lock);
861 ret = btrfs_commit_transaction(trans);
865 ret = pending_snapshot->error;
869 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
873 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
875 ret = PTR_ERR(inode);
879 d_instantiate(dentry, inode);
881 pending_snapshot->anon_dev = 0;
883 /* Prevent double freeing of anon_dev */
884 if (ret && pending_snapshot->snap)
885 pending_snapshot->snap->anon_dev = 0;
886 btrfs_put_root(pending_snapshot->snap);
887 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
889 if (pending_snapshot->anon_dev)
890 free_anon_bdev(pending_snapshot->anon_dev);
891 kfree(pending_snapshot->root_item);
892 btrfs_free_path(pending_snapshot->path);
893 kfree(pending_snapshot);
898 /* copy of may_delete in fs/namei.c()
899 * Check whether we can remove a link victim from directory dir, check
900 * whether the type of victim is right.
901 * 1. We can't do it if dir is read-only (done in permission())
902 * 2. We should have write and exec permissions on dir
903 * 3. We can't remove anything from append-only dir
904 * 4. We can't do anything with immutable dir (done in permission())
905 * 5. If the sticky bit on dir is set we should either
906 * a. be owner of dir, or
907 * b. be owner of victim, or
908 * c. have CAP_FOWNER capability
909 * 6. If the victim is append-only or immutable we can't do anything with
910 * links pointing to it.
911 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
912 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
913 * 9. We can't remove a root or mountpoint.
914 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
915 * nfs_async_unlink().
918 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
922 if (d_really_is_negative(victim))
925 BUG_ON(d_inode(victim->d_parent) != dir);
926 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
928 error = inode_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
933 if (check_sticky(&init_user_ns, dir, d_inode(victim)) ||
934 IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) ||
935 IS_SWAPFILE(d_inode(victim)))
938 if (!d_is_dir(victim))
942 } else if (d_is_dir(victim))
946 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
951 /* copy of may_create in fs/namei.c() */
952 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
954 if (d_really_is_positive(child))
958 return inode_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
962 * Create a new subvolume below @parent. This is largely modeled after
963 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
964 * inside this filesystem so it's quite a bit simpler.
966 static noinline int btrfs_mksubvol(const struct path *parent,
967 const char *name, int namelen,
968 struct btrfs_root *snap_src,
970 struct btrfs_qgroup_inherit *inherit)
972 struct inode *dir = d_inode(parent->dentry);
973 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
974 struct dentry *dentry;
977 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
981 dentry = lookup_one_len(name, parent->dentry, namelen);
982 error = PTR_ERR(dentry);
986 error = btrfs_may_create(dir, dentry);
991 * even if this name doesn't exist, we may get hash collisions.
992 * check for them now when we can safely fail
994 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
1000 down_read(&fs_info->subvol_sem);
1002 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
1006 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
1008 error = create_subvol(dir, dentry, name, namelen, inherit);
1011 fsnotify_mkdir(dir, dentry);
1013 up_read(&fs_info->subvol_sem);
1021 static noinline int btrfs_mksnapshot(const struct path *parent,
1022 const char *name, int namelen,
1023 struct btrfs_root *root,
1025 struct btrfs_qgroup_inherit *inherit)
1028 bool snapshot_force_cow = false;
1031 * Force new buffered writes to reserve space even when NOCOW is
1032 * possible. This is to avoid later writeback (running dealloc) to
1033 * fallback to COW mode and unexpectedly fail with ENOSPC.
1035 btrfs_drew_read_lock(&root->snapshot_lock);
1037 ret = btrfs_start_delalloc_snapshot(root);
1042 * All previous writes have started writeback in NOCOW mode, so now
1043 * we force future writes to fallback to COW mode during snapshot
1046 atomic_inc(&root->snapshot_force_cow);
1047 snapshot_force_cow = true;
1049 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
1051 ret = btrfs_mksubvol(parent, name, namelen,
1052 root, readonly, inherit);
1054 if (snapshot_force_cow)
1055 atomic_dec(&root->snapshot_force_cow);
1056 btrfs_drew_read_unlock(&root->snapshot_lock);
1061 * When we're defragging a range, we don't want to kick it off again
1062 * if it is really just waiting for delalloc to send it down.
1063 * If we find a nice big extent or delalloc range for the bytes in the
1064 * file you want to defrag, we return 0 to let you know to skip this
1067 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1069 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1070 struct extent_map *em = NULL;
1071 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1074 read_lock(&em_tree->lock);
1075 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1076 read_unlock(&em_tree->lock);
1079 end = extent_map_end(em);
1080 free_extent_map(em);
1081 if (end - offset > thresh)
1084 /* if we already have a nice delalloc here, just stop */
1086 end = count_range_bits(io_tree, &offset, offset + thresh,
1087 thresh, EXTENT_DELALLOC, 1);
1094 * helper function to walk through a file and find extents
1095 * newer than a specific transid, and smaller than thresh.
1097 * This is used by the defragging code to find new and small
1100 static int find_new_extents(struct btrfs_root *root,
1101 struct inode *inode, u64 newer_than,
1102 u64 *off, u32 thresh)
1104 struct btrfs_path *path;
1105 struct btrfs_key min_key;
1106 struct extent_buffer *leaf;
1107 struct btrfs_file_extent_item *extent;
1110 u64 ino = btrfs_ino(BTRFS_I(inode));
1112 path = btrfs_alloc_path();
1116 min_key.objectid = ino;
1117 min_key.type = BTRFS_EXTENT_DATA_KEY;
1118 min_key.offset = *off;
1121 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1125 if (min_key.objectid != ino)
1127 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1130 leaf = path->nodes[0];
1131 extent = btrfs_item_ptr(leaf, path->slots[0],
1132 struct btrfs_file_extent_item);
1134 type = btrfs_file_extent_type(leaf, extent);
1135 if (type == BTRFS_FILE_EXTENT_REG &&
1136 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1137 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1138 *off = min_key.offset;
1139 btrfs_free_path(path);
1144 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1145 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1149 if (min_key.offset == (u64)-1)
1153 btrfs_release_path(path);
1156 btrfs_free_path(path);
1160 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1162 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1163 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1164 struct extent_map *em;
1165 u64 len = PAGE_SIZE;
1168 * hopefully we have this extent in the tree already, try without
1169 * the full extent lock
1171 read_lock(&em_tree->lock);
1172 em = lookup_extent_mapping(em_tree, start, len);
1173 read_unlock(&em_tree->lock);
1176 struct extent_state *cached = NULL;
1177 u64 end = start + len - 1;
1179 /* get the big lock and read metadata off disk */
1180 lock_extent_bits(io_tree, start, end, &cached);
1181 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
1182 unlock_extent_cached(io_tree, start, end, &cached);
1191 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1193 struct extent_map *next;
1196 /* this is the last extent */
1197 if (em->start + em->len >= i_size_read(inode))
1200 next = defrag_lookup_extent(inode, em->start + em->len);
1201 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1203 else if ((em->block_start + em->block_len == next->block_start) &&
1204 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1207 free_extent_map(next);
1211 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1212 u64 *last_len, u64 *skip, u64 *defrag_end,
1215 struct extent_map *em;
1217 bool next_mergeable = true;
1218 bool prev_mergeable = true;
1221 * make sure that once we start defragging an extent, we keep on
1224 if (start < *defrag_end)
1229 em = defrag_lookup_extent(inode, start);
1233 /* this will cover holes, and inline extents */
1234 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1240 prev_mergeable = false;
1242 next_mergeable = defrag_check_next_extent(inode, em);
1244 * we hit a real extent, if it is big or the next extent is not a
1245 * real extent, don't bother defragging it
1247 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1248 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1252 * last_len ends up being a counter of how many bytes we've defragged.
1253 * every time we choose not to defrag an extent, we reset *last_len
1254 * so that the next tiny extent will force a defrag.
1256 * The end result of this is that tiny extents before a single big
1257 * extent will force at least part of that big extent to be defragged.
1260 *defrag_end = extent_map_end(em);
1263 *skip = extent_map_end(em);
1267 free_extent_map(em);
1272 * it doesn't do much good to defrag one or two pages
1273 * at a time. This pulls in a nice chunk of pages
1274 * to COW and defrag.
1276 * It also makes sure the delalloc code has enough
1277 * dirty data to avoid making new small extents as part
1280 * It's a good idea to start RA on this range
1281 * before calling this.
1283 static int cluster_pages_for_defrag(struct inode *inode,
1284 struct page **pages,
1285 unsigned long start_index,
1286 unsigned long num_pages)
1288 unsigned long file_end;
1289 u64 isize = i_size_read(inode);
1293 u64 start = (u64)start_index << PAGE_SHIFT;
1298 struct btrfs_ordered_extent *ordered;
1299 struct extent_state *cached_state = NULL;
1300 struct extent_io_tree *tree;
1301 struct extent_changeset *data_reserved = NULL;
1302 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1304 file_end = (isize - 1) >> PAGE_SHIFT;
1305 if (!isize || start_index > file_end)
1308 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1310 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
1311 start, page_cnt << PAGE_SHIFT);
1315 tree = &BTRFS_I(inode)->io_tree;
1317 /* step one, lock all the pages */
1318 for (i = 0; i < page_cnt; i++) {
1321 page = find_or_create_page(inode->i_mapping,
1322 start_index + i, mask);
1326 ret = set_page_extent_mapped(page);
1333 page_start = page_offset(page);
1334 page_end = page_start + PAGE_SIZE - 1;
1336 lock_extent_bits(tree, page_start, page_end,
1338 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode),
1340 unlock_extent_cached(tree, page_start, page_end,
1346 btrfs_start_ordered_extent(ordered, 1);
1347 btrfs_put_ordered_extent(ordered);
1350 * we unlocked the page above, so we need check if
1351 * it was released or not.
1353 if (page->mapping != inode->i_mapping) {
1360 if (!PageUptodate(page)) {
1361 btrfs_readpage(NULL, page);
1363 if (!PageUptodate(page)) {
1371 if (page->mapping != inode->i_mapping) {
1383 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1387 * so now we have a nice long stream of locked
1388 * and up to date pages, lets wait on them
1390 for (i = 0; i < i_done; i++)
1391 wait_on_page_writeback(pages[i]);
1393 page_start = page_offset(pages[0]);
1394 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1396 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1397 page_start, page_end - 1, &cached_state);
1400 * When defragmenting we skip ranges that have holes or inline extents,
1401 * (check should_defrag_range()), to avoid unnecessary IO and wasting
1402 * space. At btrfs_defrag_file(), we check if a range should be defragged
1403 * before locking the inode and then, if it should, we trigger a sync
1404 * page cache readahead - we lock the inode only after that to avoid
1405 * blocking for too long other tasks that possibly want to operate on
1406 * other file ranges. But before we were able to get the inode lock,
1407 * some other task may have punched a hole in the range, or we may have
1408 * now an inline extent, in which case we should not defrag. So check
1409 * for that here, where we have the inode and the range locked, and bail
1410 * out if that happened.
1412 search_start = page_start;
1413 while (search_start < page_end) {
1414 struct extent_map *em;
1416 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, search_start,
1417 page_end - search_start);
1420 goto out_unlock_range;
1422 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1423 free_extent_map(em);
1424 /* Ok, 0 means we did not defrag anything */
1426 goto out_unlock_range;
1428 search_start = extent_map_end(em);
1429 free_extent_map(em);
1432 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1433 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1434 EXTENT_DEFRAG, 0, 0, &cached_state);
1436 if (i_done != page_cnt) {
1437 spin_lock(&BTRFS_I(inode)->lock);
1438 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1439 spin_unlock(&BTRFS_I(inode)->lock);
1440 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1441 start, (page_cnt - i_done) << PAGE_SHIFT, true);
1445 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1448 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1449 page_start, page_end - 1, &cached_state);
1451 for (i = 0; i < i_done; i++) {
1452 clear_page_dirty_for_io(pages[i]);
1453 ClearPageChecked(pages[i]);
1454 set_page_dirty(pages[i]);
1455 unlock_page(pages[i]);
1458 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1459 extent_changeset_free(data_reserved);
1463 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1464 page_start, page_end - 1, &cached_state);
1466 for (i = 0; i < i_done; i++) {
1467 unlock_page(pages[i]);
1470 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1471 start, page_cnt << PAGE_SHIFT, true);
1472 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1473 extent_changeset_free(data_reserved);
1478 int btrfs_defrag_file(struct inode *inode, struct file *file,
1479 struct btrfs_ioctl_defrag_range_args *range,
1480 u64 newer_than, unsigned long max_to_defrag)
1482 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1483 struct btrfs_root *root = BTRFS_I(inode)->root;
1484 struct file_ra_state *ra = NULL;
1485 unsigned long last_index;
1486 u64 isize = i_size_read(inode);
1490 u64 newer_off = range->start;
1492 unsigned long ra_index = 0;
1494 int defrag_count = 0;
1495 int compress_type = BTRFS_COMPRESS_ZLIB;
1496 u32 extent_thresh = range->extent_thresh;
1497 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1498 unsigned long cluster = max_cluster;
1499 u64 new_align = ~((u64)SZ_128K - 1);
1500 struct page **pages = NULL;
1501 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1506 if (range->start >= isize)
1510 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
1512 if (range->compress_type)
1513 compress_type = range->compress_type;
1516 if (extent_thresh == 0)
1517 extent_thresh = SZ_256K;
1520 * If we were not given a file, allocate a readahead context. As
1521 * readahead is just an optimization, defrag will work without it so
1522 * we don't error out.
1525 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1527 file_ra_state_init(ra, inode->i_mapping);
1532 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1538 /* find the last page to defrag */
1539 if (range->start + range->len > range->start) {
1540 last_index = min_t(u64, isize - 1,
1541 range->start + range->len - 1) >> PAGE_SHIFT;
1543 last_index = (isize - 1) >> PAGE_SHIFT;
1547 ret = find_new_extents(root, inode, newer_than,
1548 &newer_off, SZ_64K);
1550 range->start = newer_off;
1552 * we always align our defrag to help keep
1553 * the extents in the file evenly spaced
1555 i = (newer_off & new_align) >> PAGE_SHIFT;
1559 i = range->start >> PAGE_SHIFT;
1562 max_to_defrag = last_index - i + 1;
1565 * make writeback starts from i, so the defrag range can be
1566 * written sequentially.
1568 if (i < inode->i_mapping->writeback_index)
1569 inode->i_mapping->writeback_index = i;
1571 while (i <= last_index && defrag_count < max_to_defrag &&
1572 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1574 * make sure we stop running if someone unmounts
1577 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1580 if (btrfs_defrag_cancelled(fs_info)) {
1581 btrfs_debug(fs_info, "defrag_file cancelled");
1586 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1587 extent_thresh, &last_len, &skip,
1588 &defrag_end, do_compress)){
1591 * the should_defrag function tells us how much to skip
1592 * bump our counter by the suggested amount
1594 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1595 i = max(i + 1, next);
1600 cluster = (PAGE_ALIGN(defrag_end) >>
1602 cluster = min(cluster, max_cluster);
1604 cluster = max_cluster;
1607 if (i + cluster > ra_index) {
1608 ra_index = max(i, ra_index);
1610 page_cache_sync_readahead(inode->i_mapping, ra,
1611 file, ra_index, cluster);
1612 ra_index += cluster;
1616 if (IS_SWAPFILE(inode)) {
1620 BTRFS_I(inode)->defrag_compress = compress_type;
1621 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1624 inode_unlock(inode);
1628 defrag_count += ret;
1629 balance_dirty_pages_ratelimited(inode->i_mapping);
1630 inode_unlock(inode);
1633 if (newer_off == (u64)-1)
1639 newer_off = max(newer_off + 1,
1640 (u64)i << PAGE_SHIFT);
1642 ret = find_new_extents(root, inode, newer_than,
1643 &newer_off, SZ_64K);
1645 range->start = newer_off;
1646 i = (newer_off & new_align) >> PAGE_SHIFT;
1653 last_len += ret << PAGE_SHIFT;
1661 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1662 filemap_flush(inode->i_mapping);
1663 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1664 &BTRFS_I(inode)->runtime_flags))
1665 filemap_flush(inode->i_mapping);
1668 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1669 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1670 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1671 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1679 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1680 inode_unlock(inode);
1688 static noinline int btrfs_ioctl_resize(struct file *file,
1691 struct inode *inode = file_inode(file);
1692 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1696 struct btrfs_root *root = BTRFS_I(inode)->root;
1697 struct btrfs_ioctl_vol_args *vol_args;
1698 struct btrfs_trans_handle *trans;
1699 struct btrfs_device *device = NULL;
1702 char *devstr = NULL;
1706 if (!capable(CAP_SYS_ADMIN))
1709 ret = mnt_want_write_file(file);
1713 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_RESIZE)) {
1714 mnt_drop_write_file(file);
1715 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1718 vol_args = memdup_user(arg, sizeof(*vol_args));
1719 if (IS_ERR(vol_args)) {
1720 ret = PTR_ERR(vol_args);
1724 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1726 sizestr = vol_args->name;
1727 devstr = strchr(sizestr, ':');
1729 sizestr = devstr + 1;
1731 devstr = vol_args->name;
1732 ret = kstrtoull(devstr, 10, &devid);
1739 btrfs_info(fs_info, "resizing devid %llu", devid);
1742 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
1744 btrfs_info(fs_info, "resizer unable to find device %llu",
1750 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1752 "resizer unable to apply on readonly device %llu",
1758 if (!strcmp(sizestr, "max"))
1759 new_size = device->bdev->bd_inode->i_size;
1761 if (sizestr[0] == '-') {
1764 } else if (sizestr[0] == '+') {
1768 new_size = memparse(sizestr, &retptr);
1769 if (*retptr != '\0' || new_size == 0) {
1775 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1780 old_size = btrfs_device_get_total_bytes(device);
1783 if (new_size > old_size) {
1787 new_size = old_size - new_size;
1788 } else if (mod > 0) {
1789 if (new_size > ULLONG_MAX - old_size) {
1793 new_size = old_size + new_size;
1796 if (new_size < SZ_256M) {
1800 if (new_size > device->bdev->bd_inode->i_size) {
1805 new_size = round_down(new_size, fs_info->sectorsize);
1807 if (new_size > old_size) {
1808 trans = btrfs_start_transaction(root, 0);
1809 if (IS_ERR(trans)) {
1810 ret = PTR_ERR(trans);
1813 ret = btrfs_grow_device(trans, device, new_size);
1814 btrfs_commit_transaction(trans);
1815 } else if (new_size < old_size) {
1816 ret = btrfs_shrink_device(device, new_size);
1817 } /* equal, nothing need to do */
1819 if (ret == 0 && new_size != old_size)
1820 btrfs_info_in_rcu(fs_info,
1821 "resize device %s (devid %llu) from %llu to %llu",
1822 rcu_str_deref(device->name), device->devid,
1823 old_size, new_size);
1827 btrfs_exclop_finish(fs_info);
1828 mnt_drop_write_file(file);
1832 static noinline int __btrfs_ioctl_snap_create(struct file *file,
1833 const char *name, unsigned long fd, int subvol,
1835 struct btrfs_qgroup_inherit *inherit)
1840 if (!S_ISDIR(file_inode(file)->i_mode))
1843 ret = mnt_want_write_file(file);
1847 namelen = strlen(name);
1848 if (strchr(name, '/')) {
1850 goto out_drop_write;
1853 if (name[0] == '.' &&
1854 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1856 goto out_drop_write;
1860 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1861 NULL, readonly, inherit);
1863 struct fd src = fdget(fd);
1864 struct inode *src_inode;
1867 goto out_drop_write;
1870 src_inode = file_inode(src.file);
1871 if (src_inode->i_sb != file_inode(file)->i_sb) {
1872 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1873 "Snapshot src from another FS");
1875 } else if (!inode_owner_or_capable(&init_user_ns, src_inode)) {
1877 * Subvolume creation is not restricted, but snapshots
1878 * are limited to own subvolumes only
1882 ret = btrfs_mksnapshot(&file->f_path, name, namelen,
1883 BTRFS_I(src_inode)->root,
1889 mnt_drop_write_file(file);
1894 static noinline int btrfs_ioctl_snap_create(struct file *file,
1895 void __user *arg, int subvol)
1897 struct btrfs_ioctl_vol_args *vol_args;
1900 if (!S_ISDIR(file_inode(file)->i_mode))
1903 vol_args = memdup_user(arg, sizeof(*vol_args));
1904 if (IS_ERR(vol_args))
1905 return PTR_ERR(vol_args);
1906 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1908 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1909 subvol, false, NULL);
1915 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1916 void __user *arg, int subvol)
1918 struct btrfs_ioctl_vol_args_v2 *vol_args;
1920 bool readonly = false;
1921 struct btrfs_qgroup_inherit *inherit = NULL;
1923 if (!S_ISDIR(file_inode(file)->i_mode))
1926 vol_args = memdup_user(arg, sizeof(*vol_args));
1927 if (IS_ERR(vol_args))
1928 return PTR_ERR(vol_args);
1929 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1931 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
1936 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1938 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1939 if (vol_args->size > PAGE_SIZE) {
1943 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1944 if (IS_ERR(inherit)) {
1945 ret = PTR_ERR(inherit);
1950 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1951 subvol, readonly, inherit);
1961 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1964 struct inode *inode = file_inode(file);
1965 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1966 struct btrfs_root *root = BTRFS_I(inode)->root;
1970 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1973 down_read(&fs_info->subvol_sem);
1974 if (btrfs_root_readonly(root))
1975 flags |= BTRFS_SUBVOL_RDONLY;
1976 up_read(&fs_info->subvol_sem);
1978 if (copy_to_user(arg, &flags, sizeof(flags)))
1984 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1987 struct inode *inode = file_inode(file);
1988 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1989 struct btrfs_root *root = BTRFS_I(inode)->root;
1990 struct btrfs_trans_handle *trans;
1995 if (!inode_owner_or_capable(&init_user_ns, inode))
1998 ret = mnt_want_write_file(file);
2002 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2004 goto out_drop_write;
2007 if (copy_from_user(&flags, arg, sizeof(flags))) {
2009 goto out_drop_write;
2012 if (flags & ~BTRFS_SUBVOL_RDONLY) {
2014 goto out_drop_write;
2017 down_write(&fs_info->subvol_sem);
2020 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
2023 root_flags = btrfs_root_flags(&root->root_item);
2024 if (flags & BTRFS_SUBVOL_RDONLY) {
2025 btrfs_set_root_flags(&root->root_item,
2026 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
2029 * Block RO -> RW transition if this subvolume is involved in
2032 spin_lock(&root->root_item_lock);
2033 if (root->send_in_progress == 0) {
2034 btrfs_set_root_flags(&root->root_item,
2035 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
2036 spin_unlock(&root->root_item_lock);
2038 spin_unlock(&root->root_item_lock);
2040 "Attempt to set subvolume %llu read-write during send",
2041 root->root_key.objectid);
2047 trans = btrfs_start_transaction(root, 1);
2048 if (IS_ERR(trans)) {
2049 ret = PTR_ERR(trans);
2053 ret = btrfs_update_root(trans, fs_info->tree_root,
2054 &root->root_key, &root->root_item);
2056 btrfs_end_transaction(trans);
2060 ret = btrfs_commit_transaction(trans);
2064 btrfs_set_root_flags(&root->root_item, root_flags);
2066 up_write(&fs_info->subvol_sem);
2068 mnt_drop_write_file(file);
2073 static noinline int key_in_sk(struct btrfs_key *key,
2074 struct btrfs_ioctl_search_key *sk)
2076 struct btrfs_key test;
2079 test.objectid = sk->min_objectid;
2080 test.type = sk->min_type;
2081 test.offset = sk->min_offset;
2083 ret = btrfs_comp_cpu_keys(key, &test);
2087 test.objectid = sk->max_objectid;
2088 test.type = sk->max_type;
2089 test.offset = sk->max_offset;
2091 ret = btrfs_comp_cpu_keys(key, &test);
2097 static noinline int copy_to_sk(struct btrfs_path *path,
2098 struct btrfs_key *key,
2099 struct btrfs_ioctl_search_key *sk,
2102 unsigned long *sk_offset,
2106 struct extent_buffer *leaf;
2107 struct btrfs_ioctl_search_header sh;
2108 struct btrfs_key test;
2109 unsigned long item_off;
2110 unsigned long item_len;
2116 leaf = path->nodes[0];
2117 slot = path->slots[0];
2118 nritems = btrfs_header_nritems(leaf);
2120 if (btrfs_header_generation(leaf) > sk->max_transid) {
2124 found_transid = btrfs_header_generation(leaf);
2126 for (i = slot; i < nritems; i++) {
2127 item_off = btrfs_item_ptr_offset(leaf, i);
2128 item_len = btrfs_item_size_nr(leaf, i);
2130 btrfs_item_key_to_cpu(leaf, key, i);
2131 if (!key_in_sk(key, sk))
2134 if (sizeof(sh) + item_len > *buf_size) {
2141 * return one empty item back for v1, which does not
2145 *buf_size = sizeof(sh) + item_len;
2150 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2155 sh.objectid = key->objectid;
2156 sh.offset = key->offset;
2157 sh.type = key->type;
2159 sh.transid = found_transid;
2162 * Copy search result header. If we fault then loop again so we
2163 * can fault in the pages and -EFAULT there if there's a
2164 * problem. Otherwise we'll fault and then copy the buffer in
2165 * properly this next time through
2167 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2172 *sk_offset += sizeof(sh);
2175 char __user *up = ubuf + *sk_offset;
2177 * Copy the item, same behavior as above, but reset the
2178 * * sk_offset so we copy the full thing again.
2180 if (read_extent_buffer_to_user_nofault(leaf, up,
2181 item_off, item_len)) {
2183 *sk_offset -= sizeof(sh);
2187 *sk_offset += item_len;
2191 if (ret) /* -EOVERFLOW from above */
2194 if (*num_found >= sk->nr_items) {
2201 test.objectid = sk->max_objectid;
2202 test.type = sk->max_type;
2203 test.offset = sk->max_offset;
2204 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2206 else if (key->offset < (u64)-1)
2208 else if (key->type < (u8)-1) {
2211 } else if (key->objectid < (u64)-1) {
2219 * 0: all items from this leaf copied, continue with next
2220 * 1: * more items can be copied, but unused buffer is too small
2221 * * all items were found
2222 * Either way, it will stops the loop which iterates to the next
2224 * -EOVERFLOW: item was to large for buffer
2225 * -EFAULT: could not copy extent buffer back to userspace
2230 static noinline int search_ioctl(struct inode *inode,
2231 struct btrfs_ioctl_search_key *sk,
2235 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2236 struct btrfs_root *root;
2237 struct btrfs_key key;
2238 struct btrfs_path *path;
2241 unsigned long sk_offset = 0;
2243 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2244 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2248 path = btrfs_alloc_path();
2252 if (sk->tree_id == 0) {
2253 /* search the root of the inode that was passed */
2254 root = btrfs_grab_root(BTRFS_I(inode)->root);
2256 root = btrfs_get_fs_root(info, sk->tree_id, true);
2258 btrfs_free_path(path);
2259 return PTR_ERR(root);
2263 key.objectid = sk->min_objectid;
2264 key.type = sk->min_type;
2265 key.offset = sk->min_offset;
2268 ret = fault_in_pages_writeable(ubuf + sk_offset,
2269 *buf_size - sk_offset);
2273 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2279 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2280 &sk_offset, &num_found);
2281 btrfs_release_path(path);
2289 sk->nr_items = num_found;
2290 btrfs_put_root(root);
2291 btrfs_free_path(path);
2295 static noinline int btrfs_ioctl_tree_search(struct file *file,
2298 struct btrfs_ioctl_search_args __user *uargs;
2299 struct btrfs_ioctl_search_key sk;
2300 struct inode *inode;
2304 if (!capable(CAP_SYS_ADMIN))
2307 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2309 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2312 buf_size = sizeof(uargs->buf);
2314 inode = file_inode(file);
2315 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2318 * In the origin implementation an overflow is handled by returning a
2319 * search header with a len of zero, so reset ret.
2321 if (ret == -EOVERFLOW)
2324 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2329 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2332 struct btrfs_ioctl_search_args_v2 __user *uarg;
2333 struct btrfs_ioctl_search_args_v2 args;
2334 struct inode *inode;
2337 const size_t buf_limit = SZ_16M;
2339 if (!capable(CAP_SYS_ADMIN))
2342 /* copy search header and buffer size */
2343 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2344 if (copy_from_user(&args, uarg, sizeof(args)))
2347 buf_size = args.buf_size;
2349 /* limit result size to 16MB */
2350 if (buf_size > buf_limit)
2351 buf_size = buf_limit;
2353 inode = file_inode(file);
2354 ret = search_ioctl(inode, &args.key, &buf_size,
2355 (char __user *)(&uarg->buf[0]));
2356 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2358 else if (ret == -EOVERFLOW &&
2359 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2366 * Search INODE_REFs to identify path name of 'dirid' directory
2367 * in a 'tree_id' tree. and sets path name to 'name'.
2369 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2370 u64 tree_id, u64 dirid, char *name)
2372 struct btrfs_root *root;
2373 struct btrfs_key key;
2379 struct btrfs_inode_ref *iref;
2380 struct extent_buffer *l;
2381 struct btrfs_path *path;
2383 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2388 path = btrfs_alloc_path();
2392 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2394 root = btrfs_get_fs_root(info, tree_id, true);
2396 ret = PTR_ERR(root);
2401 key.objectid = dirid;
2402 key.type = BTRFS_INODE_REF_KEY;
2403 key.offset = (u64)-1;
2406 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2410 ret = btrfs_previous_item(root, path, dirid,
2411 BTRFS_INODE_REF_KEY);
2421 slot = path->slots[0];
2422 btrfs_item_key_to_cpu(l, &key, slot);
2424 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2425 len = btrfs_inode_ref_name_len(l, iref);
2427 total_len += len + 1;
2429 ret = -ENAMETOOLONG;
2434 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2436 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2439 btrfs_release_path(path);
2440 key.objectid = key.offset;
2441 key.offset = (u64)-1;
2442 dirid = key.objectid;
2444 memmove(name, ptr, total_len);
2445 name[total_len] = '\0';
2448 btrfs_put_root(root);
2449 btrfs_free_path(path);
2453 static int btrfs_search_path_in_tree_user(struct inode *inode,
2454 struct btrfs_ioctl_ino_lookup_user_args *args)
2456 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2457 struct super_block *sb = inode->i_sb;
2458 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2459 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2460 u64 dirid = args->dirid;
2461 unsigned long item_off;
2462 unsigned long item_len;
2463 struct btrfs_inode_ref *iref;
2464 struct btrfs_root_ref *rref;
2465 struct btrfs_root *root = NULL;
2466 struct btrfs_path *path;
2467 struct btrfs_key key, key2;
2468 struct extent_buffer *leaf;
2469 struct inode *temp_inode;
2476 path = btrfs_alloc_path();
2481 * If the bottom subvolume does not exist directly under upper_limit,
2482 * construct the path in from the bottom up.
2484 if (dirid != upper_limit.objectid) {
2485 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2487 root = btrfs_get_fs_root(fs_info, treeid, true);
2489 ret = PTR_ERR(root);
2493 key.objectid = dirid;
2494 key.type = BTRFS_INODE_REF_KEY;
2495 key.offset = (u64)-1;
2497 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2500 } else if (ret > 0) {
2501 ret = btrfs_previous_item(root, path, dirid,
2502 BTRFS_INODE_REF_KEY);
2505 } else if (ret > 0) {
2511 leaf = path->nodes[0];
2512 slot = path->slots[0];
2513 btrfs_item_key_to_cpu(leaf, &key, slot);
2515 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2516 len = btrfs_inode_ref_name_len(leaf, iref);
2518 total_len += len + 1;
2519 if (ptr < args->path) {
2520 ret = -ENAMETOOLONG;
2525 read_extent_buffer(leaf, ptr,
2526 (unsigned long)(iref + 1), len);
2528 /* Check the read+exec permission of this directory */
2529 ret = btrfs_previous_item(root, path, dirid,
2530 BTRFS_INODE_ITEM_KEY);
2533 } else if (ret > 0) {
2538 leaf = path->nodes[0];
2539 slot = path->slots[0];
2540 btrfs_item_key_to_cpu(leaf, &key2, slot);
2541 if (key2.objectid != dirid) {
2546 temp_inode = btrfs_iget(sb, key2.objectid, root);
2547 if (IS_ERR(temp_inode)) {
2548 ret = PTR_ERR(temp_inode);
2551 ret = inode_permission(&init_user_ns, temp_inode,
2552 MAY_READ | MAY_EXEC);
2559 if (key.offset == upper_limit.objectid)
2561 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2566 btrfs_release_path(path);
2567 key.objectid = key.offset;
2568 key.offset = (u64)-1;
2569 dirid = key.objectid;
2572 memmove(args->path, ptr, total_len);
2573 args->path[total_len] = '\0';
2574 btrfs_put_root(root);
2576 btrfs_release_path(path);
2579 /* Get the bottom subvolume's name from ROOT_REF */
2580 key.objectid = treeid;
2581 key.type = BTRFS_ROOT_REF_KEY;
2582 key.offset = args->treeid;
2583 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2586 } else if (ret > 0) {
2591 leaf = path->nodes[0];
2592 slot = path->slots[0];
2593 btrfs_item_key_to_cpu(leaf, &key, slot);
2595 item_off = btrfs_item_ptr_offset(leaf, slot);
2596 item_len = btrfs_item_size_nr(leaf, slot);
2597 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2598 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2599 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2604 /* Copy subvolume's name */
2605 item_off += sizeof(struct btrfs_root_ref);
2606 item_len -= sizeof(struct btrfs_root_ref);
2607 read_extent_buffer(leaf, args->name, item_off, item_len);
2608 args->name[item_len] = 0;
2611 btrfs_put_root(root);
2613 btrfs_free_path(path);
2617 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2620 struct btrfs_ioctl_ino_lookup_args *args;
2621 struct inode *inode;
2624 args = memdup_user(argp, sizeof(*args));
2626 return PTR_ERR(args);
2628 inode = file_inode(file);
2631 * Unprivileged query to obtain the containing subvolume root id. The
2632 * path is reset so it's consistent with btrfs_search_path_in_tree.
2634 if (args->treeid == 0)
2635 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2637 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2642 if (!capable(CAP_SYS_ADMIN)) {
2647 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2648 args->treeid, args->objectid,
2652 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2660 * Version of ino_lookup ioctl (unprivileged)
2662 * The main differences from ino_lookup ioctl are:
2664 * 1. Read + Exec permission will be checked using inode_permission() during
2665 * path construction. -EACCES will be returned in case of failure.
2666 * 2. Path construction will be stopped at the inode number which corresponds
2667 * to the fd with which this ioctl is called. If constructed path does not
2668 * exist under fd's inode, -EACCES will be returned.
2669 * 3. The name of bottom subvolume is also searched and filled.
2671 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2673 struct btrfs_ioctl_ino_lookup_user_args *args;
2674 struct inode *inode;
2677 args = memdup_user(argp, sizeof(*args));
2679 return PTR_ERR(args);
2681 inode = file_inode(file);
2683 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2684 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2686 * The subvolume does not exist under fd with which this is
2693 ret = btrfs_search_path_in_tree_user(inode, args);
2695 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2702 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2703 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2705 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2706 struct btrfs_fs_info *fs_info;
2707 struct btrfs_root *root;
2708 struct btrfs_path *path;
2709 struct btrfs_key key;
2710 struct btrfs_root_item *root_item;
2711 struct btrfs_root_ref *rref;
2712 struct extent_buffer *leaf;
2713 unsigned long item_off;
2714 unsigned long item_len;
2715 struct inode *inode;
2719 path = btrfs_alloc_path();
2723 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2725 btrfs_free_path(path);
2729 inode = file_inode(file);
2730 fs_info = BTRFS_I(inode)->root->fs_info;
2732 /* Get root_item of inode's subvolume */
2733 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2734 root = btrfs_get_fs_root(fs_info, key.objectid, true);
2736 ret = PTR_ERR(root);
2739 root_item = &root->root_item;
2741 subvol_info->treeid = key.objectid;
2743 subvol_info->generation = btrfs_root_generation(root_item);
2744 subvol_info->flags = btrfs_root_flags(root_item);
2746 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2747 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2749 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2752 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2753 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2754 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2756 subvol_info->otransid = btrfs_root_otransid(root_item);
2757 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2758 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2760 subvol_info->stransid = btrfs_root_stransid(root_item);
2761 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2762 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2764 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2765 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2766 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2768 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2769 /* Search root tree for ROOT_BACKREF of this subvolume */
2770 key.type = BTRFS_ROOT_BACKREF_KEY;
2772 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2775 } else if (path->slots[0] >=
2776 btrfs_header_nritems(path->nodes[0])) {
2777 ret = btrfs_next_leaf(fs_info->tree_root, path);
2780 } else if (ret > 0) {
2786 leaf = path->nodes[0];
2787 slot = path->slots[0];
2788 btrfs_item_key_to_cpu(leaf, &key, slot);
2789 if (key.objectid == subvol_info->treeid &&
2790 key.type == BTRFS_ROOT_BACKREF_KEY) {
2791 subvol_info->parent_id = key.offset;
2793 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2794 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2796 item_off = btrfs_item_ptr_offset(leaf, slot)
2797 + sizeof(struct btrfs_root_ref);
2798 item_len = btrfs_item_size_nr(leaf, slot)
2799 - sizeof(struct btrfs_root_ref);
2800 read_extent_buffer(leaf, subvol_info->name,
2801 item_off, item_len);
2808 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2812 btrfs_put_root(root);
2814 btrfs_free_path(path);
2820 * Return ROOT_REF information of the subvolume containing this inode
2821 * except the subvolume name.
2823 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2825 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2826 struct btrfs_root_ref *rref;
2827 struct btrfs_root *root;
2828 struct btrfs_path *path;
2829 struct btrfs_key key;
2830 struct extent_buffer *leaf;
2831 struct inode *inode;
2837 path = btrfs_alloc_path();
2841 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2842 if (IS_ERR(rootrefs)) {
2843 btrfs_free_path(path);
2844 return PTR_ERR(rootrefs);
2847 inode = file_inode(file);
2848 root = BTRFS_I(inode)->root->fs_info->tree_root;
2849 objectid = BTRFS_I(inode)->root->root_key.objectid;
2851 key.objectid = objectid;
2852 key.type = BTRFS_ROOT_REF_KEY;
2853 key.offset = rootrefs->min_treeid;
2856 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2859 } else if (path->slots[0] >=
2860 btrfs_header_nritems(path->nodes[0])) {
2861 ret = btrfs_next_leaf(root, path);
2864 } else if (ret > 0) {
2870 leaf = path->nodes[0];
2871 slot = path->slots[0];
2873 btrfs_item_key_to_cpu(leaf, &key, slot);
2874 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2879 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2884 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2885 rootrefs->rootref[found].treeid = key.offset;
2886 rootrefs->rootref[found].dirid =
2887 btrfs_root_ref_dirid(leaf, rref);
2890 ret = btrfs_next_item(root, path);
2893 } else if (ret > 0) {
2900 if (!ret || ret == -EOVERFLOW) {
2901 rootrefs->num_items = found;
2902 /* update min_treeid for next search */
2904 rootrefs->min_treeid =
2905 rootrefs->rootref[found - 1].treeid + 1;
2906 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2911 btrfs_free_path(path);
2916 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2920 struct dentry *parent = file->f_path.dentry;
2921 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2922 struct dentry *dentry;
2923 struct inode *dir = d_inode(parent);
2924 struct inode *inode;
2925 struct btrfs_root *root = BTRFS_I(dir)->root;
2926 struct btrfs_root *dest = NULL;
2927 struct btrfs_ioctl_vol_args *vol_args = NULL;
2928 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2929 char *subvol_name, *subvol_name_ptr = NULL;
2932 bool destroy_parent = false;
2935 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2936 if (IS_ERR(vol_args2))
2937 return PTR_ERR(vol_args2);
2939 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2945 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2946 * name, same as v1 currently does.
2948 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2949 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2950 subvol_name = vol_args2->name;
2952 err = mnt_want_write_file(file);
2956 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2961 err = mnt_want_write_file(file);
2965 dentry = btrfs_get_dentry(fs_info->sb,
2966 BTRFS_FIRST_FREE_OBJECTID,
2967 vol_args2->subvolid, 0, 0);
2968 if (IS_ERR(dentry)) {
2969 err = PTR_ERR(dentry);
2970 goto out_drop_write;
2974 * Change the default parent since the subvolume being
2975 * deleted can be outside of the current mount point.
2977 parent = btrfs_get_parent(dentry);
2980 * At this point dentry->d_name can point to '/' if the
2981 * subvolume we want to destroy is outsite of the
2982 * current mount point, so we need to release the
2983 * current dentry and execute the lookup to return a new
2984 * one with ->d_name pointing to the
2985 * <mount point>/subvol_name.
2988 if (IS_ERR(parent)) {
2989 err = PTR_ERR(parent);
2990 goto out_drop_write;
2992 dir = d_inode(parent);
2995 * If v2 was used with SPEC_BY_ID, a new parent was
2996 * allocated since the subvolume can be outside of the
2997 * current mount point. Later on we need to release this
2998 * new parent dentry.
3000 destroy_parent = true;
3002 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
3003 fs_info, vol_args2->subvolid);
3004 if (IS_ERR(subvol_name_ptr)) {
3005 err = PTR_ERR(subvol_name_ptr);
3008 /* subvol_name_ptr is already NULL termined */
3009 subvol_name = (char *)kbasename(subvol_name_ptr);
3012 vol_args = memdup_user(arg, sizeof(*vol_args));
3013 if (IS_ERR(vol_args))
3014 return PTR_ERR(vol_args);
3016 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
3017 subvol_name = vol_args->name;
3019 err = mnt_want_write_file(file);
3024 subvol_namelen = strlen(subvol_name);
3026 if (strchr(subvol_name, '/') ||
3027 strncmp(subvol_name, "..", subvol_namelen) == 0) {
3029 goto free_subvol_name;
3032 if (!S_ISDIR(dir->i_mode)) {
3034 goto free_subvol_name;
3037 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
3039 goto free_subvol_name;
3040 dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
3041 if (IS_ERR(dentry)) {
3042 err = PTR_ERR(dentry);
3043 goto out_unlock_dir;
3046 if (d_really_is_negative(dentry)) {
3051 inode = d_inode(dentry);
3052 dest = BTRFS_I(inode)->root;
3053 if (!capable(CAP_SYS_ADMIN)) {
3055 * Regular user. Only allow this with a special mount
3056 * option, when the user has write+exec access to the
3057 * subvol root, and when rmdir(2) would have been
3060 * Note that this is _not_ check that the subvol is
3061 * empty or doesn't contain data that we wouldn't
3062 * otherwise be able to delete.
3064 * Users who want to delete empty subvols should try
3068 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
3072 * Do not allow deletion if the parent dir is the same
3073 * as the dir to be deleted. That means the ioctl
3074 * must be called on the dentry referencing the root
3075 * of the subvol, not a random directory contained
3082 err = inode_permission(&init_user_ns, inode,
3083 MAY_WRITE | MAY_EXEC);
3088 /* check if subvolume may be deleted by a user */
3089 err = btrfs_may_delete(dir, dentry, 1);
3093 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
3099 err = btrfs_delete_subvolume(dir, dentry);
3100 inode_unlock(inode);
3102 fsnotify_rmdir(dir, dentry);
3111 kfree(subvol_name_ptr);
3116 mnt_drop_write_file(file);
3123 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
3125 struct inode *inode = file_inode(file);
3126 struct btrfs_root *root = BTRFS_I(inode)->root;
3127 struct btrfs_ioctl_defrag_range_args *range;
3130 ret = mnt_want_write_file(file);
3134 if (btrfs_root_readonly(root)) {
3139 switch (inode->i_mode & S_IFMT) {
3141 if (!capable(CAP_SYS_ADMIN)) {
3145 ret = btrfs_defrag_root(root);
3149 * Note that this does not check the file descriptor for write
3150 * access. This prevents defragmenting executables that are
3151 * running and allows defrag on files open in read-only mode.
3153 if (!capable(CAP_SYS_ADMIN) &&
3154 inode_permission(&init_user_ns, inode, MAY_WRITE)) {
3159 range = kzalloc(sizeof(*range), GFP_KERNEL);
3166 if (copy_from_user(range, argp,
3172 /* compression requires us to start the IO */
3173 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3174 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3175 range->extent_thresh = (u32)-1;
3178 /* the rest are all set to zero by kzalloc */
3179 range->len = (u64)-1;
3181 ret = btrfs_defrag_file(file_inode(file), file,
3182 range, BTRFS_OLDEST_GENERATION, 0);
3191 mnt_drop_write_file(file);
3195 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3197 struct btrfs_ioctl_vol_args *vol_args;
3200 if (!capable(CAP_SYS_ADMIN))
3203 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
3204 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3206 vol_args = memdup_user(arg, sizeof(*vol_args));
3207 if (IS_ERR(vol_args)) {
3208 ret = PTR_ERR(vol_args);
3212 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3213 ret = btrfs_init_new_device(fs_info, vol_args->name);
3216 btrfs_info(fs_info, "disk added %s", vol_args->name);
3220 btrfs_exclop_finish(fs_info);
3224 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3226 struct inode *inode = file_inode(file);
3227 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3228 struct btrfs_ioctl_vol_args_v2 *vol_args;
3231 if (!capable(CAP_SYS_ADMIN))
3234 ret = mnt_want_write_file(file);
3238 vol_args = memdup_user(arg, sizeof(*vol_args));
3239 if (IS_ERR(vol_args)) {
3240 ret = PTR_ERR(vol_args);
3244 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
3249 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3250 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3254 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3255 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3257 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3258 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3260 btrfs_exclop_finish(fs_info);
3263 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3264 btrfs_info(fs_info, "device deleted: id %llu",
3267 btrfs_info(fs_info, "device deleted: %s",
3273 mnt_drop_write_file(file);
3277 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3279 struct inode *inode = file_inode(file);
3280 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3281 struct btrfs_ioctl_vol_args *vol_args;
3284 if (!capable(CAP_SYS_ADMIN))
3287 ret = mnt_want_write_file(file);
3291 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3292 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3293 goto out_drop_write;
3296 vol_args = memdup_user(arg, sizeof(*vol_args));
3297 if (IS_ERR(vol_args)) {
3298 ret = PTR_ERR(vol_args);
3302 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3303 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3306 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3309 btrfs_exclop_finish(fs_info);
3311 mnt_drop_write_file(file);
3316 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3319 struct btrfs_ioctl_fs_info_args *fi_args;
3320 struct btrfs_device *device;
3321 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3325 fi_args = memdup_user(arg, sizeof(*fi_args));
3326 if (IS_ERR(fi_args))
3327 return PTR_ERR(fi_args);
3329 flags_in = fi_args->flags;
3330 memset(fi_args, 0, sizeof(*fi_args));
3333 fi_args->num_devices = fs_devices->num_devices;
3335 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3336 if (device->devid > fi_args->max_id)
3337 fi_args->max_id = device->devid;
3341 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3342 fi_args->nodesize = fs_info->nodesize;
3343 fi_args->sectorsize = fs_info->sectorsize;
3344 fi_args->clone_alignment = fs_info->sectorsize;
3346 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3347 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3348 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3349 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3352 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3353 fi_args->generation = fs_info->generation;
3354 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3357 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3358 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3359 sizeof(fi_args->metadata_uuid));
3360 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3363 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3370 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3373 struct btrfs_ioctl_dev_info_args *di_args;
3374 struct btrfs_device *dev;
3376 char *s_uuid = NULL;
3378 di_args = memdup_user(arg, sizeof(*di_args));
3379 if (IS_ERR(di_args))
3380 return PTR_ERR(di_args);
3382 if (!btrfs_is_empty_uuid(di_args->uuid))
3383 s_uuid = di_args->uuid;
3386 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3394 di_args->devid = dev->devid;
3395 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3396 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3397 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3399 strncpy(di_args->path, rcu_str_deref(dev->name),
3400 sizeof(di_args->path) - 1);
3401 di_args->path[sizeof(di_args->path) - 1] = 0;
3403 di_args->path[0] = '\0';
3408 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3415 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3417 struct inode *inode = file_inode(file);
3418 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3419 struct btrfs_root *root = BTRFS_I(inode)->root;
3420 struct btrfs_root *new_root;
3421 struct btrfs_dir_item *di;
3422 struct btrfs_trans_handle *trans;
3423 struct btrfs_path *path = NULL;
3424 struct btrfs_disk_key disk_key;
3429 if (!capable(CAP_SYS_ADMIN))
3432 ret = mnt_want_write_file(file);
3436 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3442 objectid = BTRFS_FS_TREE_OBJECTID;
3444 new_root = btrfs_get_fs_root(fs_info, objectid, true);
3445 if (IS_ERR(new_root)) {
3446 ret = PTR_ERR(new_root);
3449 if (!is_fstree(new_root->root_key.objectid)) {
3454 path = btrfs_alloc_path();
3460 trans = btrfs_start_transaction(root, 1);
3461 if (IS_ERR(trans)) {
3462 ret = PTR_ERR(trans);
3466 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3467 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
3468 dir_id, "default", 7, 1);
3469 if (IS_ERR_OR_NULL(di)) {
3470 btrfs_release_path(path);
3471 btrfs_end_transaction(trans);
3473 "Umm, you don't have the default diritem, this isn't going to work");
3478 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3479 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3480 btrfs_mark_buffer_dirty(path->nodes[0]);
3481 btrfs_release_path(path);
3483 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
3484 btrfs_end_transaction(trans);
3486 btrfs_put_root(new_root);
3487 btrfs_free_path(path);
3489 mnt_drop_write_file(file);
3493 static void get_block_group_info(struct list_head *groups_list,
3494 struct btrfs_ioctl_space_info *space)
3496 struct btrfs_block_group *block_group;
3498 space->total_bytes = 0;
3499 space->used_bytes = 0;
3501 list_for_each_entry(block_group, groups_list, list) {
3502 space->flags = block_group->flags;
3503 space->total_bytes += block_group->length;
3504 space->used_bytes += block_group->used;
3508 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3511 struct btrfs_ioctl_space_args space_args;
3512 struct btrfs_ioctl_space_info space;
3513 struct btrfs_ioctl_space_info *dest;
3514 struct btrfs_ioctl_space_info *dest_orig;
3515 struct btrfs_ioctl_space_info __user *user_dest;
3516 struct btrfs_space_info *info;
3517 static const u64 types[] = {
3518 BTRFS_BLOCK_GROUP_DATA,
3519 BTRFS_BLOCK_GROUP_SYSTEM,
3520 BTRFS_BLOCK_GROUP_METADATA,
3521 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3529 if (copy_from_user(&space_args,
3530 (struct btrfs_ioctl_space_args __user *)arg,
3531 sizeof(space_args)))
3534 for (i = 0; i < num_types; i++) {
3535 struct btrfs_space_info *tmp;
3538 list_for_each_entry(tmp, &fs_info->space_info, list) {
3539 if (tmp->flags == types[i]) {
3548 down_read(&info->groups_sem);
3549 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3550 if (!list_empty(&info->block_groups[c]))
3553 up_read(&info->groups_sem);
3557 * Global block reserve, exported as a space_info
3561 /* space_slots == 0 means they are asking for a count */
3562 if (space_args.space_slots == 0) {
3563 space_args.total_spaces = slot_count;
3567 slot_count = min_t(u64, space_args.space_slots, slot_count);
3569 alloc_size = sizeof(*dest) * slot_count;
3571 /* we generally have at most 6 or so space infos, one for each raid
3572 * level. So, a whole page should be more than enough for everyone
3574 if (alloc_size > PAGE_SIZE)
3577 space_args.total_spaces = 0;
3578 dest = kmalloc(alloc_size, GFP_KERNEL);
3583 /* now we have a buffer to copy into */
3584 for (i = 0; i < num_types; i++) {
3585 struct btrfs_space_info *tmp;
3591 list_for_each_entry(tmp, &fs_info->space_info, list) {
3592 if (tmp->flags == types[i]) {
3600 down_read(&info->groups_sem);
3601 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3602 if (!list_empty(&info->block_groups[c])) {
3603 get_block_group_info(&info->block_groups[c],
3605 memcpy(dest, &space, sizeof(space));
3607 space_args.total_spaces++;
3613 up_read(&info->groups_sem);
3617 * Add global block reserve
3620 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3622 spin_lock(&block_rsv->lock);
3623 space.total_bytes = block_rsv->size;
3624 space.used_bytes = block_rsv->size - block_rsv->reserved;
3625 spin_unlock(&block_rsv->lock);
3626 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3627 memcpy(dest, &space, sizeof(space));
3628 space_args.total_spaces++;
3631 user_dest = (struct btrfs_ioctl_space_info __user *)
3632 (arg + sizeof(struct btrfs_ioctl_space_args));
3634 if (copy_to_user(user_dest, dest_orig, alloc_size))
3639 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3645 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3648 struct btrfs_trans_handle *trans;
3652 trans = btrfs_attach_transaction_barrier(root);
3653 if (IS_ERR(trans)) {
3654 if (PTR_ERR(trans) != -ENOENT)
3655 return PTR_ERR(trans);
3657 /* No running transaction, don't bother */
3658 transid = root->fs_info->last_trans_committed;
3661 transid = trans->transid;
3662 ret = btrfs_commit_transaction_async(trans, 0);
3664 btrfs_end_transaction(trans);
3669 if (copy_to_user(argp, &transid, sizeof(transid)))
3674 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
3680 if (copy_from_user(&transid, argp, sizeof(transid)))
3683 transid = 0; /* current trans */
3685 return btrfs_wait_for_commit(fs_info, transid);
3688 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3690 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
3691 struct btrfs_ioctl_scrub_args *sa;
3694 if (!capable(CAP_SYS_ADMIN))
3697 sa = memdup_user(arg, sizeof(*sa));
3701 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3702 ret = mnt_want_write_file(file);
3707 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
3708 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3712 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3713 * error. This is important as it allows user space to know how much
3714 * progress scrub has done. For example, if scrub is canceled we get
3715 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3716 * space. Later user space can inspect the progress from the structure
3717 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3718 * previously (btrfs-progs does this).
3719 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3720 * then return -EFAULT to signal the structure was not copied or it may
3721 * be corrupt and unreliable due to a partial copy.
3723 if (copy_to_user(arg, sa, sizeof(*sa)))
3726 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3727 mnt_drop_write_file(file);
3733 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
3735 if (!capable(CAP_SYS_ADMIN))
3738 return btrfs_scrub_cancel(fs_info);
3741 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
3744 struct btrfs_ioctl_scrub_args *sa;
3747 if (!capable(CAP_SYS_ADMIN))
3750 sa = memdup_user(arg, sizeof(*sa));
3754 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
3756 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3763 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
3766 struct btrfs_ioctl_get_dev_stats *sa;
3769 sa = memdup_user(arg, sizeof(*sa));
3773 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3778 ret = btrfs_get_dev_stats(fs_info, sa);
3780 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3787 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3790 struct btrfs_ioctl_dev_replace_args *p;
3793 if (!capable(CAP_SYS_ADMIN))
3796 p = memdup_user(arg, sizeof(*p));
3801 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3802 if (sb_rdonly(fs_info->sb)) {
3806 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
3807 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3809 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
3810 btrfs_exclop_finish(fs_info);
3813 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3814 btrfs_dev_replace_status(fs_info, p);
3817 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3818 p->result = btrfs_dev_replace_cancel(fs_info);
3826 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
3833 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3839 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3840 struct inode_fs_paths *ipath = NULL;
3841 struct btrfs_path *path;
3843 if (!capable(CAP_DAC_READ_SEARCH))
3846 path = btrfs_alloc_path();
3852 ipa = memdup_user(arg, sizeof(*ipa));
3859 size = min_t(u32, ipa->size, 4096);
3860 ipath = init_ipath(size, root, path);
3861 if (IS_ERR(ipath)) {
3862 ret = PTR_ERR(ipath);
3867 ret = paths_from_inode(ipa->inum, ipath);
3871 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3872 rel_ptr = ipath->fspath->val[i] -
3873 (u64)(unsigned long)ipath->fspath->val;
3874 ipath->fspath->val[i] = rel_ptr;
3877 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3878 ipath->fspath, size);
3885 btrfs_free_path(path);
3892 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3894 struct btrfs_data_container *inodes = ctx;
3895 const size_t c = 3 * sizeof(u64);
3897 if (inodes->bytes_left >= c) {
3898 inodes->bytes_left -= c;
3899 inodes->val[inodes->elem_cnt] = inum;
3900 inodes->val[inodes->elem_cnt + 1] = offset;
3901 inodes->val[inodes->elem_cnt + 2] = root;
3902 inodes->elem_cnt += 3;
3904 inodes->bytes_missing += c - inodes->bytes_left;
3905 inodes->bytes_left = 0;
3906 inodes->elem_missed += 3;
3912 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
3913 void __user *arg, int version)
3917 struct btrfs_ioctl_logical_ino_args *loi;
3918 struct btrfs_data_container *inodes = NULL;
3919 struct btrfs_path *path = NULL;
3922 if (!capable(CAP_SYS_ADMIN))
3925 loi = memdup_user(arg, sizeof(*loi));
3927 return PTR_ERR(loi);
3930 ignore_offset = false;
3931 size = min_t(u32, loi->size, SZ_64K);
3933 /* All reserved bits must be 0 for now */
3934 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3938 /* Only accept flags we have defined so far */
3939 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3943 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
3944 size = min_t(u32, loi->size, SZ_16M);
3947 path = btrfs_alloc_path();
3953 inodes = init_data_container(size);
3954 if (IS_ERR(inodes)) {
3955 ret = PTR_ERR(inodes);
3960 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
3961 build_ino_list, inodes, ignore_offset);
3967 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3973 btrfs_free_path(path);
3981 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3982 struct btrfs_ioctl_balance_args *bargs)
3984 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3986 bargs->flags = bctl->flags;
3988 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
3989 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3990 if (atomic_read(&fs_info->balance_pause_req))
3991 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3992 if (atomic_read(&fs_info->balance_cancel_req))
3993 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3995 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3996 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3997 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3999 spin_lock(&fs_info->balance_lock);
4000 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4001 spin_unlock(&fs_info->balance_lock);
4004 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4006 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4007 struct btrfs_fs_info *fs_info = root->fs_info;
4008 struct btrfs_ioctl_balance_args *bargs;
4009 struct btrfs_balance_control *bctl;
4010 bool need_unlock; /* for mut. excl. ops lock */
4013 if (!capable(CAP_SYS_ADMIN))
4016 ret = mnt_want_write_file(file);
4021 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
4022 mutex_lock(&fs_info->balance_mutex);
4028 * mut. excl. ops lock is locked. Three possibilities:
4029 * (1) some other op is running
4030 * (2) balance is running
4031 * (3) balance is paused -- special case (think resume)
4033 mutex_lock(&fs_info->balance_mutex);
4034 if (fs_info->balance_ctl) {
4035 /* this is either (2) or (3) */
4036 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4037 mutex_unlock(&fs_info->balance_mutex);
4039 * Lock released to allow other waiters to continue,
4040 * we'll reexamine the status again.
4042 mutex_lock(&fs_info->balance_mutex);
4044 if (fs_info->balance_ctl &&
4045 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4047 need_unlock = false;
4051 mutex_unlock(&fs_info->balance_mutex);
4055 mutex_unlock(&fs_info->balance_mutex);
4061 mutex_unlock(&fs_info->balance_mutex);
4062 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4069 bargs = memdup_user(arg, sizeof(*bargs));
4070 if (IS_ERR(bargs)) {
4071 ret = PTR_ERR(bargs);
4075 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4076 if (!fs_info->balance_ctl) {
4081 bctl = fs_info->balance_ctl;
4082 spin_lock(&fs_info->balance_lock);
4083 bctl->flags |= BTRFS_BALANCE_RESUME;
4084 spin_unlock(&fs_info->balance_lock);
4092 if (fs_info->balance_ctl) {
4097 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4104 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4105 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4106 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4108 bctl->flags = bargs->flags;
4110 /* balance everything - no filters */
4111 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4114 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4121 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4122 * bctl is freed in reset_balance_state, or, if restriper was paused
4123 * all the way until unmount, in free_fs_info. The flag should be
4124 * cleared after reset_balance_state.
4126 need_unlock = false;
4128 ret = btrfs_balance(fs_info, bctl, bargs);
4131 if ((ret == 0 || ret == -ECANCELED) && arg) {
4132 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4141 mutex_unlock(&fs_info->balance_mutex);
4143 btrfs_exclop_finish(fs_info);
4145 mnt_drop_write_file(file);
4149 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4151 if (!capable(CAP_SYS_ADMIN))
4155 case BTRFS_BALANCE_CTL_PAUSE:
4156 return btrfs_pause_balance(fs_info);
4157 case BTRFS_BALANCE_CTL_CANCEL:
4158 return btrfs_cancel_balance(fs_info);
4164 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4167 struct btrfs_ioctl_balance_args *bargs;
4170 if (!capable(CAP_SYS_ADMIN))
4173 mutex_lock(&fs_info->balance_mutex);
4174 if (!fs_info->balance_ctl) {
4179 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4185 btrfs_update_ioctl_balance_args(fs_info, bargs);
4187 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4192 mutex_unlock(&fs_info->balance_mutex);
4196 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4198 struct inode *inode = file_inode(file);
4199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4200 struct btrfs_ioctl_quota_ctl_args *sa;
4203 if (!capable(CAP_SYS_ADMIN))
4206 ret = mnt_want_write_file(file);
4210 sa = memdup_user(arg, sizeof(*sa));
4216 down_write(&fs_info->subvol_sem);
4219 case BTRFS_QUOTA_CTL_ENABLE:
4220 ret = btrfs_quota_enable(fs_info);
4222 case BTRFS_QUOTA_CTL_DISABLE:
4223 ret = btrfs_quota_disable(fs_info);
4231 up_write(&fs_info->subvol_sem);
4233 mnt_drop_write_file(file);
4237 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4239 struct inode *inode = file_inode(file);
4240 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4241 struct btrfs_root *root = BTRFS_I(inode)->root;
4242 struct btrfs_ioctl_qgroup_assign_args *sa;
4243 struct btrfs_trans_handle *trans;
4247 if (!capable(CAP_SYS_ADMIN))
4250 ret = mnt_want_write_file(file);
4254 sa = memdup_user(arg, sizeof(*sa));
4260 trans = btrfs_join_transaction(root);
4261 if (IS_ERR(trans)) {
4262 ret = PTR_ERR(trans);
4267 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4269 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4272 /* update qgroup status and info */
4273 err = btrfs_run_qgroups(trans);
4275 btrfs_handle_fs_error(fs_info, err,
4276 "failed to update qgroup status and info");
4277 err = btrfs_end_transaction(trans);
4284 mnt_drop_write_file(file);
4288 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4290 struct inode *inode = file_inode(file);
4291 struct btrfs_root *root = BTRFS_I(inode)->root;
4292 struct btrfs_ioctl_qgroup_create_args *sa;
4293 struct btrfs_trans_handle *trans;
4297 if (!capable(CAP_SYS_ADMIN))
4300 ret = mnt_want_write_file(file);
4304 sa = memdup_user(arg, sizeof(*sa));
4310 if (!sa->qgroupid) {
4315 trans = btrfs_join_transaction(root);
4316 if (IS_ERR(trans)) {
4317 ret = PTR_ERR(trans);
4322 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4324 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4327 err = btrfs_end_transaction(trans);
4334 mnt_drop_write_file(file);
4338 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4340 struct inode *inode = file_inode(file);
4341 struct btrfs_root *root = BTRFS_I(inode)->root;
4342 struct btrfs_ioctl_qgroup_limit_args *sa;
4343 struct btrfs_trans_handle *trans;
4348 if (!capable(CAP_SYS_ADMIN))
4351 ret = mnt_want_write_file(file);
4355 sa = memdup_user(arg, sizeof(*sa));
4361 trans = btrfs_join_transaction(root);
4362 if (IS_ERR(trans)) {
4363 ret = PTR_ERR(trans);
4367 qgroupid = sa->qgroupid;
4369 /* take the current subvol as qgroup */
4370 qgroupid = root->root_key.objectid;
4373 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
4375 err = btrfs_end_transaction(trans);
4382 mnt_drop_write_file(file);
4386 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4388 struct inode *inode = file_inode(file);
4389 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4390 struct btrfs_ioctl_quota_rescan_args *qsa;
4393 if (!capable(CAP_SYS_ADMIN))
4396 ret = mnt_want_write_file(file);
4400 qsa = memdup_user(arg, sizeof(*qsa));
4411 ret = btrfs_qgroup_rescan(fs_info);
4416 mnt_drop_write_file(file);
4420 static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4423 struct btrfs_ioctl_quota_rescan_args *qsa;
4426 if (!capable(CAP_SYS_ADMIN))
4429 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
4433 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4435 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
4438 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4445 static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4448 if (!capable(CAP_SYS_ADMIN))
4451 return btrfs_qgroup_wait_for_completion(fs_info, true);
4454 static long _btrfs_ioctl_set_received_subvol(struct file *file,
4455 struct btrfs_ioctl_received_subvol_args *sa)
4457 struct inode *inode = file_inode(file);
4458 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4459 struct btrfs_root *root = BTRFS_I(inode)->root;
4460 struct btrfs_root_item *root_item = &root->root_item;
4461 struct btrfs_trans_handle *trans;
4462 struct timespec64 ct = current_time(inode);
4464 int received_uuid_changed;
4466 if (!inode_owner_or_capable(&init_user_ns, inode))
4469 ret = mnt_want_write_file(file);
4473 down_write(&fs_info->subvol_sem);
4475 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
4480 if (btrfs_root_readonly(root)) {
4487 * 2 - uuid items (received uuid + subvol uuid)
4489 trans = btrfs_start_transaction(root, 3);
4490 if (IS_ERR(trans)) {
4491 ret = PTR_ERR(trans);
4496 sa->rtransid = trans->transid;
4497 sa->rtime.sec = ct.tv_sec;
4498 sa->rtime.nsec = ct.tv_nsec;
4500 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4502 if (received_uuid_changed &&
4503 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4504 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
4505 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4506 root->root_key.objectid);
4507 if (ret && ret != -ENOENT) {
4508 btrfs_abort_transaction(trans, ret);
4509 btrfs_end_transaction(trans);
4513 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4514 btrfs_set_root_stransid(root_item, sa->stransid);
4515 btrfs_set_root_rtransid(root_item, sa->rtransid);
4516 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4517 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4518 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4519 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4521 ret = btrfs_update_root(trans, fs_info->tree_root,
4522 &root->root_key, &root->root_item);
4524 btrfs_end_transaction(trans);
4527 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4528 ret = btrfs_uuid_tree_add(trans, sa->uuid,
4529 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4530 root->root_key.objectid);
4531 if (ret < 0 && ret != -EEXIST) {
4532 btrfs_abort_transaction(trans, ret);
4533 btrfs_end_transaction(trans);
4537 ret = btrfs_commit_transaction(trans);
4539 up_write(&fs_info->subvol_sem);
4540 mnt_drop_write_file(file);
4545 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4548 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4549 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4552 args32 = memdup_user(arg, sizeof(*args32));
4554 return PTR_ERR(args32);
4556 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
4562 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4563 args64->stransid = args32->stransid;
4564 args64->rtransid = args32->rtransid;
4565 args64->stime.sec = args32->stime.sec;
4566 args64->stime.nsec = args32->stime.nsec;
4567 args64->rtime.sec = args32->rtime.sec;
4568 args64->rtime.nsec = args32->rtime.nsec;
4569 args64->flags = args32->flags;
4571 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4575 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4576 args32->stransid = args64->stransid;
4577 args32->rtransid = args64->rtransid;
4578 args32->stime.sec = args64->stime.sec;
4579 args32->stime.nsec = args64->stime.nsec;
4580 args32->rtime.sec = args64->rtime.sec;
4581 args32->rtime.nsec = args64->rtime.nsec;
4582 args32->flags = args64->flags;
4584 ret = copy_to_user(arg, args32, sizeof(*args32));
4595 static long btrfs_ioctl_set_received_subvol(struct file *file,
4598 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4601 sa = memdup_user(arg, sizeof(*sa));
4605 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4610 ret = copy_to_user(arg, sa, sizeof(*sa));
4619 static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4624 char label[BTRFS_LABEL_SIZE];
4626 spin_lock(&fs_info->super_lock);
4627 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4628 spin_unlock(&fs_info->super_lock);
4630 len = strnlen(label, BTRFS_LABEL_SIZE);
4632 if (len == BTRFS_LABEL_SIZE) {
4634 "label is too long, return the first %zu bytes",
4638 ret = copy_to_user(arg, label, len);
4640 return ret ? -EFAULT : 0;
4643 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4645 struct inode *inode = file_inode(file);
4646 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4647 struct btrfs_root *root = BTRFS_I(inode)->root;
4648 struct btrfs_super_block *super_block = fs_info->super_copy;
4649 struct btrfs_trans_handle *trans;
4650 char label[BTRFS_LABEL_SIZE];
4653 if (!capable(CAP_SYS_ADMIN))
4656 if (copy_from_user(label, arg, sizeof(label)))
4659 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4661 "unable to set label with more than %d bytes",
4662 BTRFS_LABEL_SIZE - 1);
4666 ret = mnt_want_write_file(file);
4670 trans = btrfs_start_transaction(root, 0);
4671 if (IS_ERR(trans)) {
4672 ret = PTR_ERR(trans);
4676 spin_lock(&fs_info->super_lock);
4677 strcpy(super_block->label, label);
4678 spin_unlock(&fs_info->super_lock);
4679 ret = btrfs_commit_transaction(trans);
4682 mnt_drop_write_file(file);
4686 #define INIT_FEATURE_FLAGS(suffix) \
4687 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4688 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4689 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4691 int btrfs_ioctl_get_supported_features(void __user *arg)
4693 static const struct btrfs_ioctl_feature_flags features[3] = {
4694 INIT_FEATURE_FLAGS(SUPP),
4695 INIT_FEATURE_FLAGS(SAFE_SET),
4696 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4699 if (copy_to_user(arg, &features, sizeof(features)))
4705 static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4708 struct btrfs_super_block *super_block = fs_info->super_copy;
4709 struct btrfs_ioctl_feature_flags features;
4711 features.compat_flags = btrfs_super_compat_flags(super_block);
4712 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4713 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4715 if (copy_to_user(arg, &features, sizeof(features)))
4721 static int check_feature_bits(struct btrfs_fs_info *fs_info,
4722 enum btrfs_feature_set set,
4723 u64 change_mask, u64 flags, u64 supported_flags,
4724 u64 safe_set, u64 safe_clear)
4726 const char *type = btrfs_feature_set_name(set);
4728 u64 disallowed, unsupported;
4729 u64 set_mask = flags & change_mask;
4730 u64 clear_mask = ~flags & change_mask;
4732 unsupported = set_mask & ~supported_flags;
4734 names = btrfs_printable_features(set, unsupported);
4737 "this kernel does not support the %s feature bit%s",
4738 names, strchr(names, ',') ? "s" : "");
4742 "this kernel does not support %s bits 0x%llx",
4747 disallowed = set_mask & ~safe_set;
4749 names = btrfs_printable_features(set, disallowed);
4752 "can't set the %s feature bit%s while mounted",
4753 names, strchr(names, ',') ? "s" : "");
4757 "can't set %s bits 0x%llx while mounted",
4762 disallowed = clear_mask & ~safe_clear;
4764 names = btrfs_printable_features(set, disallowed);
4767 "can't clear the %s feature bit%s while mounted",
4768 names, strchr(names, ',') ? "s" : "");
4772 "can't clear %s bits 0x%llx while mounted",
4780 #define check_feature(fs_info, change_mask, flags, mask_base) \
4781 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
4782 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4783 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4784 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4786 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4788 struct inode *inode = file_inode(file);
4789 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4790 struct btrfs_root *root = BTRFS_I(inode)->root;
4791 struct btrfs_super_block *super_block = fs_info->super_copy;
4792 struct btrfs_ioctl_feature_flags flags[2];
4793 struct btrfs_trans_handle *trans;
4797 if (!capable(CAP_SYS_ADMIN))
4800 if (copy_from_user(flags, arg, sizeof(flags)))
4804 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4805 !flags[0].incompat_flags)
4808 ret = check_feature(fs_info, flags[0].compat_flags,
4809 flags[1].compat_flags, COMPAT);
4813 ret = check_feature(fs_info, flags[0].compat_ro_flags,
4814 flags[1].compat_ro_flags, COMPAT_RO);
4818 ret = check_feature(fs_info, flags[0].incompat_flags,
4819 flags[1].incompat_flags, INCOMPAT);
4823 ret = mnt_want_write_file(file);
4827 trans = btrfs_start_transaction(root, 0);
4828 if (IS_ERR(trans)) {
4829 ret = PTR_ERR(trans);
4830 goto out_drop_write;
4833 spin_lock(&fs_info->super_lock);
4834 newflags = btrfs_super_compat_flags(super_block);
4835 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4836 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4837 btrfs_set_super_compat_flags(super_block, newflags);
4839 newflags = btrfs_super_compat_ro_flags(super_block);
4840 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4841 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4842 btrfs_set_super_compat_ro_flags(super_block, newflags);
4844 newflags = btrfs_super_incompat_flags(super_block);
4845 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4846 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4847 btrfs_set_super_incompat_flags(super_block, newflags);
4848 spin_unlock(&fs_info->super_lock);
4850 ret = btrfs_commit_transaction(trans);
4852 mnt_drop_write_file(file);
4857 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4859 struct btrfs_ioctl_send_args *arg;
4863 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4864 struct btrfs_ioctl_send_args_32 args32;
4866 ret = copy_from_user(&args32, argp, sizeof(args32));
4869 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4872 arg->send_fd = args32.send_fd;
4873 arg->clone_sources_count = args32.clone_sources_count;
4874 arg->clone_sources = compat_ptr(args32.clone_sources);
4875 arg->parent_root = args32.parent_root;
4876 arg->flags = args32.flags;
4877 memcpy(arg->reserved, args32.reserved,
4878 sizeof(args32.reserved));
4883 arg = memdup_user(argp, sizeof(*arg));
4885 return PTR_ERR(arg);
4887 ret = btrfs_ioctl_send(file, arg);
4892 long btrfs_ioctl(struct file *file, unsigned int
4893 cmd, unsigned long arg)
4895 struct inode *inode = file_inode(file);
4896 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4897 struct btrfs_root *root = BTRFS_I(inode)->root;
4898 void __user *argp = (void __user *)arg;
4901 case FS_IOC_GETFLAGS:
4902 return btrfs_ioctl_getflags(file, argp);
4903 case FS_IOC_SETFLAGS:
4904 return btrfs_ioctl_setflags(file, argp);
4905 case FS_IOC_GETVERSION:
4906 return btrfs_ioctl_getversion(file, argp);
4907 case FS_IOC_GETFSLABEL:
4908 return btrfs_ioctl_get_fslabel(fs_info, argp);
4909 case FS_IOC_SETFSLABEL:
4910 return btrfs_ioctl_set_fslabel(file, argp);
4912 return btrfs_ioctl_fitrim(fs_info, argp);
4913 case BTRFS_IOC_SNAP_CREATE:
4914 return btrfs_ioctl_snap_create(file, argp, 0);
4915 case BTRFS_IOC_SNAP_CREATE_V2:
4916 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4917 case BTRFS_IOC_SUBVOL_CREATE:
4918 return btrfs_ioctl_snap_create(file, argp, 1);
4919 case BTRFS_IOC_SUBVOL_CREATE_V2:
4920 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4921 case BTRFS_IOC_SNAP_DESTROY:
4922 return btrfs_ioctl_snap_destroy(file, argp, false);
4923 case BTRFS_IOC_SNAP_DESTROY_V2:
4924 return btrfs_ioctl_snap_destroy(file, argp, true);
4925 case BTRFS_IOC_SUBVOL_GETFLAGS:
4926 return btrfs_ioctl_subvol_getflags(file, argp);
4927 case BTRFS_IOC_SUBVOL_SETFLAGS:
4928 return btrfs_ioctl_subvol_setflags(file, argp);
4929 case BTRFS_IOC_DEFAULT_SUBVOL:
4930 return btrfs_ioctl_default_subvol(file, argp);
4931 case BTRFS_IOC_DEFRAG:
4932 return btrfs_ioctl_defrag(file, NULL);
4933 case BTRFS_IOC_DEFRAG_RANGE:
4934 return btrfs_ioctl_defrag(file, argp);
4935 case BTRFS_IOC_RESIZE:
4936 return btrfs_ioctl_resize(file, argp);
4937 case BTRFS_IOC_ADD_DEV:
4938 return btrfs_ioctl_add_dev(fs_info, argp);
4939 case BTRFS_IOC_RM_DEV:
4940 return btrfs_ioctl_rm_dev(file, argp);
4941 case BTRFS_IOC_RM_DEV_V2:
4942 return btrfs_ioctl_rm_dev_v2(file, argp);
4943 case BTRFS_IOC_FS_INFO:
4944 return btrfs_ioctl_fs_info(fs_info, argp);
4945 case BTRFS_IOC_DEV_INFO:
4946 return btrfs_ioctl_dev_info(fs_info, argp);
4947 case BTRFS_IOC_BALANCE:
4948 return btrfs_ioctl_balance(file, NULL);
4949 case BTRFS_IOC_TREE_SEARCH:
4950 return btrfs_ioctl_tree_search(file, argp);
4951 case BTRFS_IOC_TREE_SEARCH_V2:
4952 return btrfs_ioctl_tree_search_v2(file, argp);
4953 case BTRFS_IOC_INO_LOOKUP:
4954 return btrfs_ioctl_ino_lookup(file, argp);
4955 case BTRFS_IOC_INO_PATHS:
4956 return btrfs_ioctl_ino_to_path(root, argp);
4957 case BTRFS_IOC_LOGICAL_INO:
4958 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4959 case BTRFS_IOC_LOGICAL_INO_V2:
4960 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
4961 case BTRFS_IOC_SPACE_INFO:
4962 return btrfs_ioctl_space_info(fs_info, argp);
4963 case BTRFS_IOC_SYNC: {
4966 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
4969 ret = btrfs_sync_fs(inode->i_sb, 1);
4971 * The transaction thread may want to do more work,
4972 * namely it pokes the cleaner kthread that will start
4973 * processing uncleaned subvols.
4975 wake_up_process(fs_info->transaction_kthread);
4978 case BTRFS_IOC_START_SYNC:
4979 return btrfs_ioctl_start_sync(root, argp);
4980 case BTRFS_IOC_WAIT_SYNC:
4981 return btrfs_ioctl_wait_sync(fs_info, argp);
4982 case BTRFS_IOC_SCRUB:
4983 return btrfs_ioctl_scrub(file, argp);
4984 case BTRFS_IOC_SCRUB_CANCEL:
4985 return btrfs_ioctl_scrub_cancel(fs_info);
4986 case BTRFS_IOC_SCRUB_PROGRESS:
4987 return btrfs_ioctl_scrub_progress(fs_info, argp);
4988 case BTRFS_IOC_BALANCE_V2:
4989 return btrfs_ioctl_balance(file, argp);
4990 case BTRFS_IOC_BALANCE_CTL:
4991 return btrfs_ioctl_balance_ctl(fs_info, arg);
4992 case BTRFS_IOC_BALANCE_PROGRESS:
4993 return btrfs_ioctl_balance_progress(fs_info, argp);
4994 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4995 return btrfs_ioctl_set_received_subvol(file, argp);
4997 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4998 return btrfs_ioctl_set_received_subvol_32(file, argp);
5000 case BTRFS_IOC_SEND:
5001 return _btrfs_ioctl_send(file, argp, false);
5002 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5003 case BTRFS_IOC_SEND_32:
5004 return _btrfs_ioctl_send(file, argp, true);
5006 case BTRFS_IOC_GET_DEV_STATS:
5007 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5008 case BTRFS_IOC_QUOTA_CTL:
5009 return btrfs_ioctl_quota_ctl(file, argp);
5010 case BTRFS_IOC_QGROUP_ASSIGN:
5011 return btrfs_ioctl_qgroup_assign(file, argp);
5012 case BTRFS_IOC_QGROUP_CREATE:
5013 return btrfs_ioctl_qgroup_create(file, argp);
5014 case BTRFS_IOC_QGROUP_LIMIT:
5015 return btrfs_ioctl_qgroup_limit(file, argp);
5016 case BTRFS_IOC_QUOTA_RESCAN:
5017 return btrfs_ioctl_quota_rescan(file, argp);
5018 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5019 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
5020 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5021 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
5022 case BTRFS_IOC_DEV_REPLACE:
5023 return btrfs_ioctl_dev_replace(fs_info, argp);
5024 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5025 return btrfs_ioctl_get_supported_features(argp);
5026 case BTRFS_IOC_GET_FEATURES:
5027 return btrfs_ioctl_get_features(fs_info, argp);
5028 case BTRFS_IOC_SET_FEATURES:
5029 return btrfs_ioctl_set_features(file, argp);
5030 case FS_IOC_FSGETXATTR:
5031 return btrfs_ioctl_fsgetxattr(file, argp);
5032 case FS_IOC_FSSETXATTR:
5033 return btrfs_ioctl_fssetxattr(file, argp);
5034 case BTRFS_IOC_GET_SUBVOL_INFO:
5035 return btrfs_ioctl_get_subvol_info(file, argp);
5036 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5037 return btrfs_ioctl_get_subvol_rootref(file, argp);
5038 case BTRFS_IOC_INO_LOOKUP_USER:
5039 return btrfs_ioctl_ino_lookup_user(file, argp);
5045 #ifdef CONFIG_COMPAT
5046 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5049 * These all access 32-bit values anyway so no further
5050 * handling is necessary.
5053 case FS_IOC32_GETFLAGS:
5054 cmd = FS_IOC_GETFLAGS;
5056 case FS_IOC32_SETFLAGS:
5057 cmd = FS_IOC_SETFLAGS;
5059 case FS_IOC32_GETVERSION:
5060 cmd = FS_IOC_GETVERSION;
5064 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));