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(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(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 * If the fs is mounted with nologreplay, which requires it to be
532 * mounted in RO mode as well, we can not allow discard on free space
533 * inside block groups, because log trees refer to extents that are not
534 * pinned in a block group's free space cache (pinning the extents is
535 * precisely the first phase of replaying a log tree).
537 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
541 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
545 q = bdev_get_queue(device->bdev);
546 if (blk_queue_discard(q)) {
548 minlen = min_t(u64, q->limits.discard_granularity,
556 if (copy_from_user(&range, arg, sizeof(range)))
560 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
561 * block group is in the logical address space, which can be any
562 * sectorsize aligned bytenr in the range [0, U64_MAX].
564 if (range.len < fs_info->sb->s_blocksize)
567 range.minlen = max(range.minlen, minlen);
568 ret = btrfs_trim_fs(fs_info, &range);
572 if (copy_to_user(arg, &range, sizeof(range)))
578 int __pure btrfs_is_empty_uuid(u8 *uuid)
582 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
589 static noinline int create_subvol(struct inode *dir,
590 struct dentry *dentry,
591 const char *name, int namelen,
592 struct btrfs_qgroup_inherit *inherit)
594 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
595 struct btrfs_trans_handle *trans;
596 struct btrfs_key key;
597 struct btrfs_root_item *root_item;
598 struct btrfs_inode_item *inode_item;
599 struct extent_buffer *leaf;
600 struct btrfs_root *root = BTRFS_I(dir)->root;
601 struct btrfs_root *new_root;
602 struct btrfs_block_rsv block_rsv;
603 struct timespec64 cur_time = current_time(dir);
611 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
615 ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
619 ret = get_anon_bdev(&anon_dev);
624 * Don't create subvolume whose level is not zero. Or qgroup will be
625 * screwed up since it assumes subvolume qgroup's level to be 0.
627 if (btrfs_qgroup_level(objectid)) {
632 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
634 * The same as the snapshot creation, please see the comment
635 * of create_snapshot().
637 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
641 trans = btrfs_start_transaction(root, 0);
643 ret = PTR_ERR(trans);
644 btrfs_subvolume_release_metadata(root, &block_rsv);
647 trans->block_rsv = &block_rsv;
648 trans->bytes_reserved = block_rsv.size;
650 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
654 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
655 BTRFS_NESTING_NORMAL);
661 btrfs_mark_buffer_dirty(leaf);
663 inode_item = &root_item->inode;
664 btrfs_set_stack_inode_generation(inode_item, 1);
665 btrfs_set_stack_inode_size(inode_item, 3);
666 btrfs_set_stack_inode_nlink(inode_item, 1);
667 btrfs_set_stack_inode_nbytes(inode_item,
669 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
671 btrfs_set_root_flags(root_item, 0);
672 btrfs_set_root_limit(root_item, 0);
673 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
675 btrfs_set_root_bytenr(root_item, leaf->start);
676 btrfs_set_root_generation(root_item, trans->transid);
677 btrfs_set_root_level(root_item, 0);
678 btrfs_set_root_refs(root_item, 1);
679 btrfs_set_root_used(root_item, leaf->len);
680 btrfs_set_root_last_snapshot(root_item, 0);
682 btrfs_set_root_generation_v2(root_item,
683 btrfs_root_generation(root_item));
684 generate_random_guid(root_item->uuid);
685 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
686 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
687 root_item->ctime = root_item->otime;
688 btrfs_set_root_ctransid(root_item, trans->transid);
689 btrfs_set_root_otransid(root_item, trans->transid);
691 btrfs_tree_unlock(leaf);
692 free_extent_buffer(leaf);
695 btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
697 key.objectid = objectid;
699 key.type = BTRFS_ROOT_ITEM_KEY;
700 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
705 key.offset = (u64)-1;
706 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
707 if (IS_ERR(new_root)) {
708 free_anon_bdev(anon_dev);
709 ret = PTR_ERR(new_root);
710 btrfs_abort_transaction(trans, ret);
713 /* Freeing will be done in btrfs_put_root() of new_root */
716 btrfs_record_root_in_trans(trans, new_root);
718 ret = btrfs_create_subvol_root(trans, new_root, root);
719 btrfs_put_root(new_root);
721 /* We potentially lose an unused inode item here */
722 btrfs_abort_transaction(trans, ret);
727 * insert the directory item
729 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
731 btrfs_abort_transaction(trans, ret);
735 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
736 BTRFS_FT_DIR, index);
738 btrfs_abort_transaction(trans, ret);
742 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
743 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
745 btrfs_abort_transaction(trans, ret);
749 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
750 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
752 btrfs_abort_transaction(trans, ret);
756 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
757 BTRFS_UUID_KEY_SUBVOL, objectid);
759 btrfs_abort_transaction(trans, ret);
763 trans->block_rsv = NULL;
764 trans->bytes_reserved = 0;
765 btrfs_subvolume_release_metadata(root, &block_rsv);
767 err = btrfs_commit_transaction(trans);
772 inode = btrfs_lookup_dentry(dir, dentry);
774 return PTR_ERR(inode);
775 d_instantiate(dentry, inode);
781 free_anon_bdev(anon_dev);
786 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
787 struct dentry *dentry, bool readonly,
788 struct btrfs_qgroup_inherit *inherit)
790 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
792 struct btrfs_pending_snapshot *pending_snapshot;
793 struct btrfs_trans_handle *trans;
796 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
799 if (atomic_read(&root->nr_swapfiles)) {
801 "cannot snapshot subvolume with active swapfile");
805 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
806 if (!pending_snapshot)
809 ret = get_anon_bdev(&pending_snapshot->anon_dev);
812 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
814 pending_snapshot->path = btrfs_alloc_path();
815 if (!pending_snapshot->root_item || !pending_snapshot->path) {
820 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
821 BTRFS_BLOCK_RSV_TEMP);
823 * 1 - parent dir inode
826 * 2 - root ref/backref
827 * 1 - root of snapshot
830 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
831 &pending_snapshot->block_rsv, 8,
836 pending_snapshot->dentry = dentry;
837 pending_snapshot->root = root;
838 pending_snapshot->readonly = readonly;
839 pending_snapshot->dir = dir;
840 pending_snapshot->inherit = inherit;
842 trans = btrfs_start_transaction(root, 0);
844 ret = PTR_ERR(trans);
848 spin_lock(&fs_info->trans_lock);
849 list_add(&pending_snapshot->list,
850 &trans->transaction->pending_snapshots);
851 spin_unlock(&fs_info->trans_lock);
853 ret = btrfs_commit_transaction(trans);
857 ret = pending_snapshot->error;
861 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
865 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
867 ret = PTR_ERR(inode);
871 d_instantiate(dentry, inode);
873 pending_snapshot->anon_dev = 0;
875 /* Prevent double freeing of anon_dev */
876 if (ret && pending_snapshot->snap)
877 pending_snapshot->snap->anon_dev = 0;
878 btrfs_put_root(pending_snapshot->snap);
879 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
881 if (pending_snapshot->anon_dev)
882 free_anon_bdev(pending_snapshot->anon_dev);
883 kfree(pending_snapshot->root_item);
884 btrfs_free_path(pending_snapshot->path);
885 kfree(pending_snapshot);
890 /* copy of may_delete in fs/namei.c()
891 * Check whether we can remove a link victim from directory dir, check
892 * whether the type of victim is right.
893 * 1. We can't do it if dir is read-only (done in permission())
894 * 2. We should have write and exec permissions on dir
895 * 3. We can't remove anything from append-only dir
896 * 4. We can't do anything with immutable dir (done in permission())
897 * 5. If the sticky bit on dir is set we should either
898 * a. be owner of dir, or
899 * b. be owner of victim, or
900 * c. have CAP_FOWNER capability
901 * 6. If the victim is append-only or immutable we can't do anything with
902 * links pointing to it.
903 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
904 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
905 * 9. We can't remove a root or mountpoint.
906 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
907 * nfs_async_unlink().
910 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
914 if (d_really_is_negative(victim))
917 BUG_ON(d_inode(victim->d_parent) != dir);
918 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
920 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
925 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
926 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
929 if (!d_is_dir(victim))
933 } else if (d_is_dir(victim))
937 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
942 /* copy of may_create in fs/namei.c() */
943 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
945 if (d_really_is_positive(child))
949 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
953 * Create a new subvolume below @parent. This is largely modeled after
954 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
955 * inside this filesystem so it's quite a bit simpler.
957 static noinline int btrfs_mksubvol(const struct path *parent,
958 const char *name, int namelen,
959 struct btrfs_root *snap_src,
961 struct btrfs_qgroup_inherit *inherit)
963 struct inode *dir = d_inode(parent->dentry);
964 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
965 struct dentry *dentry;
968 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
972 dentry = lookup_one_len(name, parent->dentry, namelen);
973 error = PTR_ERR(dentry);
977 error = btrfs_may_create(dir, dentry);
982 * even if this name doesn't exist, we may get hash collisions.
983 * check for them now when we can safely fail
985 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
991 down_read(&fs_info->subvol_sem);
993 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
997 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
999 error = create_subvol(dir, dentry, name, namelen, inherit);
1002 fsnotify_mkdir(dir, dentry);
1004 up_read(&fs_info->subvol_sem);
1012 static noinline int btrfs_mksnapshot(const struct path *parent,
1013 const char *name, int namelen,
1014 struct btrfs_root *root,
1016 struct btrfs_qgroup_inherit *inherit)
1019 bool snapshot_force_cow = false;
1022 * Force new buffered writes to reserve space even when NOCOW is
1023 * possible. This is to avoid later writeback (running dealloc) to
1024 * fallback to COW mode and unexpectedly fail with ENOSPC.
1026 btrfs_drew_read_lock(&root->snapshot_lock);
1028 ret = btrfs_start_delalloc_snapshot(root);
1033 * All previous writes have started writeback in NOCOW mode, so now
1034 * we force future writes to fallback to COW mode during snapshot
1037 atomic_inc(&root->snapshot_force_cow);
1038 snapshot_force_cow = true;
1040 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
1042 ret = btrfs_mksubvol(parent, name, namelen,
1043 root, readonly, inherit);
1045 if (snapshot_force_cow)
1046 atomic_dec(&root->snapshot_force_cow);
1047 btrfs_drew_read_unlock(&root->snapshot_lock);
1052 * When we're defragging a range, we don't want to kick it off again
1053 * if it is really just waiting for delalloc to send it down.
1054 * If we find a nice big extent or delalloc range for the bytes in the
1055 * file you want to defrag, we return 0 to let you know to skip this
1058 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1060 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1061 struct extent_map *em = NULL;
1062 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1065 read_lock(&em_tree->lock);
1066 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1067 read_unlock(&em_tree->lock);
1070 end = extent_map_end(em);
1071 free_extent_map(em);
1072 if (end - offset > thresh)
1075 /* if we already have a nice delalloc here, just stop */
1077 end = count_range_bits(io_tree, &offset, offset + thresh,
1078 thresh, EXTENT_DELALLOC, 1);
1085 * helper function to walk through a file and find extents
1086 * newer than a specific transid, and smaller than thresh.
1088 * This is used by the defragging code to find new and small
1091 static int find_new_extents(struct btrfs_root *root,
1092 struct inode *inode, u64 newer_than,
1093 u64 *off, u32 thresh)
1095 struct btrfs_path *path;
1096 struct btrfs_key min_key;
1097 struct extent_buffer *leaf;
1098 struct btrfs_file_extent_item *extent;
1101 u64 ino = btrfs_ino(BTRFS_I(inode));
1103 path = btrfs_alloc_path();
1107 min_key.objectid = ino;
1108 min_key.type = BTRFS_EXTENT_DATA_KEY;
1109 min_key.offset = *off;
1112 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1116 if (min_key.objectid != ino)
1118 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1121 leaf = path->nodes[0];
1122 extent = btrfs_item_ptr(leaf, path->slots[0],
1123 struct btrfs_file_extent_item);
1125 type = btrfs_file_extent_type(leaf, extent);
1126 if (type == BTRFS_FILE_EXTENT_REG &&
1127 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1128 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1129 *off = min_key.offset;
1130 btrfs_free_path(path);
1135 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1136 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1140 if (min_key.offset == (u64)-1)
1144 btrfs_release_path(path);
1147 btrfs_free_path(path);
1151 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1153 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1154 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1155 struct extent_map *em;
1156 u64 len = PAGE_SIZE;
1159 * hopefully we have this extent in the tree already, try without
1160 * the full extent lock
1162 read_lock(&em_tree->lock);
1163 em = lookup_extent_mapping(em_tree, start, len);
1164 read_unlock(&em_tree->lock);
1167 struct extent_state *cached = NULL;
1168 u64 end = start + len - 1;
1170 /* get the big lock and read metadata off disk */
1171 lock_extent_bits(io_tree, start, end, &cached);
1172 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
1173 unlock_extent_cached(io_tree, start, end, &cached);
1182 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1184 struct extent_map *next;
1187 /* this is the last extent */
1188 if (em->start + em->len >= i_size_read(inode))
1191 next = defrag_lookup_extent(inode, em->start + em->len);
1192 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1194 else if ((em->block_start + em->block_len == next->block_start) &&
1195 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1198 free_extent_map(next);
1202 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1203 u64 *last_len, u64 *skip, u64 *defrag_end,
1206 struct extent_map *em;
1208 bool next_mergeable = true;
1209 bool prev_mergeable = true;
1212 * make sure that once we start defragging an extent, we keep on
1215 if (start < *defrag_end)
1220 em = defrag_lookup_extent(inode, start);
1224 /* this will cover holes, and inline extents */
1225 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1231 prev_mergeable = false;
1233 next_mergeable = defrag_check_next_extent(inode, em);
1235 * we hit a real extent, if it is big or the next extent is not a
1236 * real extent, don't bother defragging it
1238 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1239 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1243 * last_len ends up being a counter of how many bytes we've defragged.
1244 * every time we choose not to defrag an extent, we reset *last_len
1245 * so that the next tiny extent will force a defrag.
1247 * The end result of this is that tiny extents before a single big
1248 * extent will force at least part of that big extent to be defragged.
1251 *defrag_end = extent_map_end(em);
1254 *skip = extent_map_end(em);
1258 free_extent_map(em);
1263 * it doesn't do much good to defrag one or two pages
1264 * at a time. This pulls in a nice chunk of pages
1265 * to COW and defrag.
1267 * It also makes sure the delalloc code has enough
1268 * dirty data to avoid making new small extents as part
1271 * It's a good idea to start RA on this range
1272 * before calling this.
1274 static int cluster_pages_for_defrag(struct inode *inode,
1275 struct page **pages,
1276 unsigned long start_index,
1277 unsigned long num_pages)
1279 unsigned long file_end;
1280 u64 isize = i_size_read(inode);
1284 u64 start = (u64)start_index << PAGE_SHIFT;
1289 struct btrfs_ordered_extent *ordered;
1290 struct extent_state *cached_state = NULL;
1291 struct extent_io_tree *tree;
1292 struct extent_changeset *data_reserved = NULL;
1293 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1295 file_end = (isize - 1) >> PAGE_SHIFT;
1296 if (!isize || start_index > file_end)
1299 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1301 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
1302 start, page_cnt << PAGE_SHIFT);
1306 tree = &BTRFS_I(inode)->io_tree;
1308 /* step one, lock all the pages */
1309 for (i = 0; i < page_cnt; i++) {
1312 page = find_or_create_page(inode->i_mapping,
1313 start_index + i, mask);
1317 page_start = page_offset(page);
1318 page_end = page_start + PAGE_SIZE - 1;
1320 lock_extent_bits(tree, page_start, page_end,
1322 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode),
1324 unlock_extent_cached(tree, page_start, page_end,
1330 btrfs_start_ordered_extent(ordered, 1);
1331 btrfs_put_ordered_extent(ordered);
1334 * we unlocked the page above, so we need check if
1335 * it was released or not.
1337 if (page->mapping != inode->i_mapping) {
1344 if (!PageUptodate(page)) {
1345 btrfs_readpage(NULL, page);
1347 if (!PageUptodate(page)) {
1355 if (page->mapping != inode->i_mapping) {
1367 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1371 * so now we have a nice long stream of locked
1372 * and up to date pages, lets wait on them
1374 for (i = 0; i < i_done; i++)
1375 wait_on_page_writeback(pages[i]);
1377 page_start = page_offset(pages[0]);
1378 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1380 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1381 page_start, page_end - 1, &cached_state);
1384 * When defragmenting we skip ranges that have holes or inline extents,
1385 * (check should_defrag_range()), to avoid unnecessary IO and wasting
1386 * space. At btrfs_defrag_file(), we check if a range should be defragged
1387 * before locking the inode and then, if it should, we trigger a sync
1388 * page cache readahead - we lock the inode only after that to avoid
1389 * blocking for too long other tasks that possibly want to operate on
1390 * other file ranges. But before we were able to get the inode lock,
1391 * some other task may have punched a hole in the range, or we may have
1392 * now an inline extent, in which case we should not defrag. So check
1393 * for that here, where we have the inode and the range locked, and bail
1394 * out if that happened.
1396 search_start = page_start;
1397 while (search_start < page_end) {
1398 struct extent_map *em;
1400 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, search_start,
1401 page_end - search_start);
1404 goto out_unlock_range;
1406 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1407 free_extent_map(em);
1408 /* Ok, 0 means we did not defrag anything */
1410 goto out_unlock_range;
1412 search_start = extent_map_end(em);
1413 free_extent_map(em);
1416 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1417 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1418 EXTENT_DEFRAG, 0, 0, &cached_state);
1420 if (i_done != page_cnt) {
1421 spin_lock(&BTRFS_I(inode)->lock);
1422 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1423 spin_unlock(&BTRFS_I(inode)->lock);
1424 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1425 start, (page_cnt - i_done) << PAGE_SHIFT, true);
1429 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1432 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1433 page_start, page_end - 1, &cached_state);
1435 for (i = 0; i < i_done; i++) {
1436 clear_page_dirty_for_io(pages[i]);
1437 ClearPageChecked(pages[i]);
1438 set_page_extent_mapped(pages[i]);
1439 set_page_dirty(pages[i]);
1440 unlock_page(pages[i]);
1443 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1444 extent_changeset_free(data_reserved);
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 unlock_page(pages[i]);
1455 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1456 start, page_cnt << PAGE_SHIFT, true);
1457 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1458 extent_changeset_free(data_reserved);
1463 int btrfs_defrag_file(struct inode *inode, struct file *file,
1464 struct btrfs_ioctl_defrag_range_args *range,
1465 u64 newer_than, unsigned long max_to_defrag)
1467 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1468 struct btrfs_root *root = BTRFS_I(inode)->root;
1469 struct file_ra_state *ra = NULL;
1470 unsigned long last_index;
1471 u64 isize = i_size_read(inode);
1475 u64 newer_off = range->start;
1477 unsigned long ra_index = 0;
1479 int defrag_count = 0;
1480 int compress_type = BTRFS_COMPRESS_ZLIB;
1481 u32 extent_thresh = range->extent_thresh;
1482 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1483 unsigned long cluster = max_cluster;
1484 u64 new_align = ~((u64)SZ_128K - 1);
1485 struct page **pages = NULL;
1486 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1491 if (range->start >= isize)
1495 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
1497 if (range->compress_type)
1498 compress_type = range->compress_type;
1501 if (extent_thresh == 0)
1502 extent_thresh = SZ_256K;
1505 * If we were not given a file, allocate a readahead context. As
1506 * readahead is just an optimization, defrag will work without it so
1507 * we don't error out.
1510 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1512 file_ra_state_init(ra, inode->i_mapping);
1517 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1523 /* find the last page to defrag */
1524 if (range->start + range->len > range->start) {
1525 last_index = min_t(u64, isize - 1,
1526 range->start + range->len - 1) >> PAGE_SHIFT;
1528 last_index = (isize - 1) >> PAGE_SHIFT;
1532 ret = find_new_extents(root, inode, newer_than,
1533 &newer_off, SZ_64K);
1535 range->start = newer_off;
1537 * we always align our defrag to help keep
1538 * the extents in the file evenly spaced
1540 i = (newer_off & new_align) >> PAGE_SHIFT;
1544 i = range->start >> PAGE_SHIFT;
1547 max_to_defrag = last_index - i + 1;
1550 * make writeback starts from i, so the defrag range can be
1551 * written sequentially.
1553 if (i < inode->i_mapping->writeback_index)
1554 inode->i_mapping->writeback_index = i;
1556 while (i <= last_index && defrag_count < max_to_defrag &&
1557 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1559 * make sure we stop running if someone unmounts
1562 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1565 if (btrfs_defrag_cancelled(fs_info)) {
1566 btrfs_debug(fs_info, "defrag_file cancelled");
1571 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1572 extent_thresh, &last_len, &skip,
1573 &defrag_end, do_compress)){
1576 * the should_defrag function tells us how much to skip
1577 * bump our counter by the suggested amount
1579 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1580 i = max(i + 1, next);
1585 cluster = (PAGE_ALIGN(defrag_end) >>
1587 cluster = min(cluster, max_cluster);
1589 cluster = max_cluster;
1592 if (i + cluster > ra_index) {
1593 ra_index = max(i, ra_index);
1595 page_cache_sync_readahead(inode->i_mapping, ra,
1596 file, ra_index, cluster);
1597 ra_index += cluster;
1601 if (IS_SWAPFILE(inode)) {
1605 BTRFS_I(inode)->defrag_compress = compress_type;
1606 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1609 inode_unlock(inode);
1613 defrag_count += ret;
1614 balance_dirty_pages_ratelimited(inode->i_mapping);
1615 inode_unlock(inode);
1618 if (newer_off == (u64)-1)
1624 newer_off = max(newer_off + 1,
1625 (u64)i << PAGE_SHIFT);
1627 ret = find_new_extents(root, inode, newer_than,
1628 &newer_off, SZ_64K);
1630 range->start = newer_off;
1631 i = (newer_off & new_align) >> PAGE_SHIFT;
1638 last_len += ret << PAGE_SHIFT;
1646 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1647 filemap_flush(inode->i_mapping);
1648 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1649 &BTRFS_I(inode)->runtime_flags))
1650 filemap_flush(inode->i_mapping);
1653 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1654 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1655 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1656 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1664 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1665 inode_unlock(inode);
1673 static noinline int btrfs_ioctl_resize(struct file *file,
1676 struct inode *inode = file_inode(file);
1677 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1681 struct btrfs_root *root = BTRFS_I(inode)->root;
1682 struct btrfs_ioctl_vol_args *vol_args;
1683 struct btrfs_trans_handle *trans;
1684 struct btrfs_device *device = NULL;
1687 char *devstr = NULL;
1691 if (!capable(CAP_SYS_ADMIN))
1694 ret = mnt_want_write_file(file);
1698 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_RESIZE)) {
1699 mnt_drop_write_file(file);
1700 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1703 vol_args = memdup_user(arg, sizeof(*vol_args));
1704 if (IS_ERR(vol_args)) {
1705 ret = PTR_ERR(vol_args);
1709 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1711 sizestr = vol_args->name;
1712 devstr = strchr(sizestr, ':');
1714 sizestr = devstr + 1;
1716 devstr = vol_args->name;
1717 ret = kstrtoull(devstr, 10, &devid);
1724 btrfs_info(fs_info, "resizing devid %llu", devid);
1727 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
1729 btrfs_info(fs_info, "resizer unable to find device %llu",
1735 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1737 "resizer unable to apply on readonly device %llu",
1743 if (!strcmp(sizestr, "max"))
1744 new_size = device->bdev->bd_inode->i_size;
1746 if (sizestr[0] == '-') {
1749 } else if (sizestr[0] == '+') {
1753 new_size = memparse(sizestr, &retptr);
1754 if (*retptr != '\0' || new_size == 0) {
1760 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1765 old_size = btrfs_device_get_total_bytes(device);
1768 if (new_size > old_size) {
1772 new_size = old_size - new_size;
1773 } else if (mod > 0) {
1774 if (new_size > ULLONG_MAX - old_size) {
1778 new_size = old_size + new_size;
1781 if (new_size < SZ_256M) {
1785 if (new_size > device->bdev->bd_inode->i_size) {
1790 new_size = round_down(new_size, fs_info->sectorsize);
1792 if (new_size > old_size) {
1793 trans = btrfs_start_transaction(root, 0);
1794 if (IS_ERR(trans)) {
1795 ret = PTR_ERR(trans);
1798 ret = btrfs_grow_device(trans, device, new_size);
1799 btrfs_commit_transaction(trans);
1800 } else if (new_size < old_size) {
1801 ret = btrfs_shrink_device(device, new_size);
1802 } /* equal, nothing need to do */
1804 if (ret == 0 && new_size != old_size)
1805 btrfs_info_in_rcu(fs_info,
1806 "resize device %s (devid %llu) from %llu to %llu",
1807 rcu_str_deref(device->name), device->devid,
1808 old_size, new_size);
1812 btrfs_exclop_finish(fs_info);
1813 mnt_drop_write_file(file);
1817 static noinline int __btrfs_ioctl_snap_create(struct file *file,
1818 const char *name, unsigned long fd, int subvol,
1820 struct btrfs_qgroup_inherit *inherit)
1825 if (!S_ISDIR(file_inode(file)->i_mode))
1828 ret = mnt_want_write_file(file);
1832 namelen = strlen(name);
1833 if (strchr(name, '/')) {
1835 goto out_drop_write;
1838 if (name[0] == '.' &&
1839 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1841 goto out_drop_write;
1845 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1846 NULL, readonly, inherit);
1848 struct fd src = fdget(fd);
1849 struct inode *src_inode;
1852 goto out_drop_write;
1855 src_inode = file_inode(src.file);
1856 if (src_inode->i_sb != file_inode(file)->i_sb) {
1857 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1858 "Snapshot src from another FS");
1860 } else if (!inode_owner_or_capable(src_inode)) {
1862 * Subvolume creation is not restricted, but snapshots
1863 * are limited to own subvolumes only
1867 ret = btrfs_mksnapshot(&file->f_path, name, namelen,
1868 BTRFS_I(src_inode)->root,
1874 mnt_drop_write_file(file);
1879 static noinline int btrfs_ioctl_snap_create(struct file *file,
1880 void __user *arg, int subvol)
1882 struct btrfs_ioctl_vol_args *vol_args;
1885 if (!S_ISDIR(file_inode(file)->i_mode))
1888 vol_args = memdup_user(arg, sizeof(*vol_args));
1889 if (IS_ERR(vol_args))
1890 return PTR_ERR(vol_args);
1891 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1893 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1894 subvol, false, NULL);
1900 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1901 void __user *arg, int subvol)
1903 struct btrfs_ioctl_vol_args_v2 *vol_args;
1905 bool readonly = false;
1906 struct btrfs_qgroup_inherit *inherit = NULL;
1908 if (!S_ISDIR(file_inode(file)->i_mode))
1911 vol_args = memdup_user(arg, sizeof(*vol_args));
1912 if (IS_ERR(vol_args))
1913 return PTR_ERR(vol_args);
1914 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1916 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
1921 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1923 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1924 if (vol_args->size > PAGE_SIZE) {
1928 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1929 if (IS_ERR(inherit)) {
1930 ret = PTR_ERR(inherit);
1935 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1936 subvol, readonly, inherit);
1946 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1949 struct inode *inode = file_inode(file);
1950 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1951 struct btrfs_root *root = BTRFS_I(inode)->root;
1955 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1958 down_read(&fs_info->subvol_sem);
1959 if (btrfs_root_readonly(root))
1960 flags |= BTRFS_SUBVOL_RDONLY;
1961 up_read(&fs_info->subvol_sem);
1963 if (copy_to_user(arg, &flags, sizeof(flags)))
1969 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1972 struct inode *inode = file_inode(file);
1973 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1974 struct btrfs_root *root = BTRFS_I(inode)->root;
1975 struct btrfs_trans_handle *trans;
1980 if (!inode_owner_or_capable(inode))
1983 ret = mnt_want_write_file(file);
1987 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1989 goto out_drop_write;
1992 if (copy_from_user(&flags, arg, sizeof(flags))) {
1994 goto out_drop_write;
1997 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1999 goto out_drop_write;
2002 down_write(&fs_info->subvol_sem);
2005 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
2008 root_flags = btrfs_root_flags(&root->root_item);
2009 if (flags & BTRFS_SUBVOL_RDONLY) {
2010 btrfs_set_root_flags(&root->root_item,
2011 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
2014 * Block RO -> RW transition if this subvolume is involved in
2017 spin_lock(&root->root_item_lock);
2018 if (root->send_in_progress == 0) {
2019 btrfs_set_root_flags(&root->root_item,
2020 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
2021 spin_unlock(&root->root_item_lock);
2023 spin_unlock(&root->root_item_lock);
2025 "Attempt to set subvolume %llu read-write during send",
2026 root->root_key.objectid);
2032 trans = btrfs_start_transaction(root, 1);
2033 if (IS_ERR(trans)) {
2034 ret = PTR_ERR(trans);
2038 ret = btrfs_update_root(trans, fs_info->tree_root,
2039 &root->root_key, &root->root_item);
2041 btrfs_end_transaction(trans);
2045 ret = btrfs_commit_transaction(trans);
2049 btrfs_set_root_flags(&root->root_item, root_flags);
2051 up_write(&fs_info->subvol_sem);
2053 mnt_drop_write_file(file);
2058 static noinline int key_in_sk(struct btrfs_key *key,
2059 struct btrfs_ioctl_search_key *sk)
2061 struct btrfs_key test;
2064 test.objectid = sk->min_objectid;
2065 test.type = sk->min_type;
2066 test.offset = sk->min_offset;
2068 ret = btrfs_comp_cpu_keys(key, &test);
2072 test.objectid = sk->max_objectid;
2073 test.type = sk->max_type;
2074 test.offset = sk->max_offset;
2076 ret = btrfs_comp_cpu_keys(key, &test);
2082 static noinline int copy_to_sk(struct btrfs_path *path,
2083 struct btrfs_key *key,
2084 struct btrfs_ioctl_search_key *sk,
2087 unsigned long *sk_offset,
2091 struct extent_buffer *leaf;
2092 struct btrfs_ioctl_search_header sh;
2093 struct btrfs_key test;
2094 unsigned long item_off;
2095 unsigned long item_len;
2101 leaf = path->nodes[0];
2102 slot = path->slots[0];
2103 nritems = btrfs_header_nritems(leaf);
2105 if (btrfs_header_generation(leaf) > sk->max_transid) {
2109 found_transid = btrfs_header_generation(leaf);
2111 for (i = slot; i < nritems; i++) {
2112 item_off = btrfs_item_ptr_offset(leaf, i);
2113 item_len = btrfs_item_size_nr(leaf, i);
2115 btrfs_item_key_to_cpu(leaf, key, i);
2116 if (!key_in_sk(key, sk))
2119 if (sizeof(sh) + item_len > *buf_size) {
2126 * return one empty item back for v1, which does not
2130 *buf_size = sizeof(sh) + item_len;
2135 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2140 sh.objectid = key->objectid;
2141 sh.offset = key->offset;
2142 sh.type = key->type;
2144 sh.transid = found_transid;
2147 * Copy search result header. If we fault then loop again so we
2148 * can fault in the pages and -EFAULT there if there's a
2149 * problem. Otherwise we'll fault and then copy the buffer in
2150 * properly this next time through
2152 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2157 *sk_offset += sizeof(sh);
2160 char __user *up = ubuf + *sk_offset;
2162 * Copy the item, same behavior as above, but reset the
2163 * * sk_offset so we copy the full thing again.
2165 if (read_extent_buffer_to_user_nofault(leaf, up,
2166 item_off, item_len)) {
2168 *sk_offset -= sizeof(sh);
2172 *sk_offset += item_len;
2176 if (ret) /* -EOVERFLOW from above */
2179 if (*num_found >= sk->nr_items) {
2186 test.objectid = sk->max_objectid;
2187 test.type = sk->max_type;
2188 test.offset = sk->max_offset;
2189 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2191 else if (key->offset < (u64)-1)
2193 else if (key->type < (u8)-1) {
2196 } else if (key->objectid < (u64)-1) {
2204 * 0: all items from this leaf copied, continue with next
2205 * 1: * more items can be copied, but unused buffer is too small
2206 * * all items were found
2207 * Either way, it will stops the loop which iterates to the next
2209 * -EOVERFLOW: item was to large for buffer
2210 * -EFAULT: could not copy extent buffer back to userspace
2215 static noinline int search_ioctl(struct inode *inode,
2216 struct btrfs_ioctl_search_key *sk,
2220 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2221 struct btrfs_root *root;
2222 struct btrfs_key key;
2223 struct btrfs_path *path;
2226 unsigned long sk_offset = 0;
2228 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2229 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2233 path = btrfs_alloc_path();
2237 if (sk->tree_id == 0) {
2238 /* search the root of the inode that was passed */
2239 root = btrfs_grab_root(BTRFS_I(inode)->root);
2241 root = btrfs_get_fs_root(info, sk->tree_id, true);
2243 btrfs_free_path(path);
2244 return PTR_ERR(root);
2248 key.objectid = sk->min_objectid;
2249 key.type = sk->min_type;
2250 key.offset = sk->min_offset;
2253 ret = fault_in_pages_writeable(ubuf + sk_offset,
2254 *buf_size - sk_offset);
2258 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2264 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2265 &sk_offset, &num_found);
2266 btrfs_release_path(path);
2274 sk->nr_items = num_found;
2275 btrfs_put_root(root);
2276 btrfs_free_path(path);
2280 static noinline int btrfs_ioctl_tree_search(struct file *file,
2283 struct btrfs_ioctl_search_args __user *uargs;
2284 struct btrfs_ioctl_search_key sk;
2285 struct inode *inode;
2289 if (!capable(CAP_SYS_ADMIN))
2292 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2294 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2297 buf_size = sizeof(uargs->buf);
2299 inode = file_inode(file);
2300 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2303 * In the origin implementation an overflow is handled by returning a
2304 * search header with a len of zero, so reset ret.
2306 if (ret == -EOVERFLOW)
2309 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2314 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2317 struct btrfs_ioctl_search_args_v2 __user *uarg;
2318 struct btrfs_ioctl_search_args_v2 args;
2319 struct inode *inode;
2322 const size_t buf_limit = SZ_16M;
2324 if (!capable(CAP_SYS_ADMIN))
2327 /* copy search header and buffer size */
2328 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2329 if (copy_from_user(&args, uarg, sizeof(args)))
2332 buf_size = args.buf_size;
2334 /* limit result size to 16MB */
2335 if (buf_size > buf_limit)
2336 buf_size = buf_limit;
2338 inode = file_inode(file);
2339 ret = search_ioctl(inode, &args.key, &buf_size,
2340 (char __user *)(&uarg->buf[0]));
2341 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2343 else if (ret == -EOVERFLOW &&
2344 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2351 * Search INODE_REFs to identify path name of 'dirid' directory
2352 * in a 'tree_id' tree. and sets path name to 'name'.
2354 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2355 u64 tree_id, u64 dirid, char *name)
2357 struct btrfs_root *root;
2358 struct btrfs_key key;
2364 struct btrfs_inode_ref *iref;
2365 struct extent_buffer *l;
2366 struct btrfs_path *path;
2368 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2373 path = btrfs_alloc_path();
2377 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2379 root = btrfs_get_fs_root(info, tree_id, true);
2381 ret = PTR_ERR(root);
2386 key.objectid = dirid;
2387 key.type = BTRFS_INODE_REF_KEY;
2388 key.offset = (u64)-1;
2391 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2395 ret = btrfs_previous_item(root, path, dirid,
2396 BTRFS_INODE_REF_KEY);
2406 slot = path->slots[0];
2407 btrfs_item_key_to_cpu(l, &key, slot);
2409 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2410 len = btrfs_inode_ref_name_len(l, iref);
2412 total_len += len + 1;
2414 ret = -ENAMETOOLONG;
2419 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2421 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2424 btrfs_release_path(path);
2425 key.objectid = key.offset;
2426 key.offset = (u64)-1;
2427 dirid = key.objectid;
2429 memmove(name, ptr, total_len);
2430 name[total_len] = '\0';
2433 btrfs_put_root(root);
2434 btrfs_free_path(path);
2438 static int btrfs_search_path_in_tree_user(struct inode *inode,
2439 struct btrfs_ioctl_ino_lookup_user_args *args)
2441 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2442 struct super_block *sb = inode->i_sb;
2443 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2444 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2445 u64 dirid = args->dirid;
2446 unsigned long item_off;
2447 unsigned long item_len;
2448 struct btrfs_inode_ref *iref;
2449 struct btrfs_root_ref *rref;
2450 struct btrfs_root *root = NULL;
2451 struct btrfs_path *path;
2452 struct btrfs_key key, key2;
2453 struct extent_buffer *leaf;
2454 struct inode *temp_inode;
2461 path = btrfs_alloc_path();
2466 * If the bottom subvolume does not exist directly under upper_limit,
2467 * construct the path in from the bottom up.
2469 if (dirid != upper_limit.objectid) {
2470 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2472 root = btrfs_get_fs_root(fs_info, treeid, true);
2474 ret = PTR_ERR(root);
2478 key.objectid = dirid;
2479 key.type = BTRFS_INODE_REF_KEY;
2480 key.offset = (u64)-1;
2482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2485 } else if (ret > 0) {
2486 ret = btrfs_previous_item(root, path, dirid,
2487 BTRFS_INODE_REF_KEY);
2490 } else if (ret > 0) {
2496 leaf = path->nodes[0];
2497 slot = path->slots[0];
2498 btrfs_item_key_to_cpu(leaf, &key, slot);
2500 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2501 len = btrfs_inode_ref_name_len(leaf, iref);
2503 total_len += len + 1;
2504 if (ptr < args->path) {
2505 ret = -ENAMETOOLONG;
2510 read_extent_buffer(leaf, ptr,
2511 (unsigned long)(iref + 1), len);
2513 /* Check the read+exec permission of this directory */
2514 ret = btrfs_previous_item(root, path, dirid,
2515 BTRFS_INODE_ITEM_KEY);
2518 } else if (ret > 0) {
2523 leaf = path->nodes[0];
2524 slot = path->slots[0];
2525 btrfs_item_key_to_cpu(leaf, &key2, slot);
2526 if (key2.objectid != dirid) {
2531 temp_inode = btrfs_iget(sb, key2.objectid, root);
2532 if (IS_ERR(temp_inode)) {
2533 ret = PTR_ERR(temp_inode);
2536 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2543 if (key.offset == upper_limit.objectid)
2545 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2550 btrfs_release_path(path);
2551 key.objectid = key.offset;
2552 key.offset = (u64)-1;
2553 dirid = key.objectid;
2556 memmove(args->path, ptr, total_len);
2557 args->path[total_len] = '\0';
2558 btrfs_put_root(root);
2560 btrfs_release_path(path);
2563 /* Get the bottom subvolume's name from ROOT_REF */
2564 key.objectid = treeid;
2565 key.type = BTRFS_ROOT_REF_KEY;
2566 key.offset = args->treeid;
2567 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2570 } else if (ret > 0) {
2575 leaf = path->nodes[0];
2576 slot = path->slots[0];
2577 btrfs_item_key_to_cpu(leaf, &key, slot);
2579 item_off = btrfs_item_ptr_offset(leaf, slot);
2580 item_len = btrfs_item_size_nr(leaf, slot);
2581 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2582 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2583 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2588 /* Copy subvolume's name */
2589 item_off += sizeof(struct btrfs_root_ref);
2590 item_len -= sizeof(struct btrfs_root_ref);
2591 read_extent_buffer(leaf, args->name, item_off, item_len);
2592 args->name[item_len] = 0;
2595 btrfs_put_root(root);
2597 btrfs_free_path(path);
2601 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2604 struct btrfs_ioctl_ino_lookup_args *args;
2605 struct inode *inode;
2608 args = memdup_user(argp, sizeof(*args));
2610 return PTR_ERR(args);
2612 inode = file_inode(file);
2615 * Unprivileged query to obtain the containing subvolume root id. The
2616 * path is reset so it's consistent with btrfs_search_path_in_tree.
2618 if (args->treeid == 0)
2619 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2621 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2626 if (!capable(CAP_SYS_ADMIN)) {
2631 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2632 args->treeid, args->objectid,
2636 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2644 * Version of ino_lookup ioctl (unprivileged)
2646 * The main differences from ino_lookup ioctl are:
2648 * 1. Read + Exec permission will be checked using inode_permission() during
2649 * path construction. -EACCES will be returned in case of failure.
2650 * 2. Path construction will be stopped at the inode number which corresponds
2651 * to the fd with which this ioctl is called. If constructed path does not
2652 * exist under fd's inode, -EACCES will be returned.
2653 * 3. The name of bottom subvolume is also searched and filled.
2655 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2657 struct btrfs_ioctl_ino_lookup_user_args *args;
2658 struct inode *inode;
2661 args = memdup_user(argp, sizeof(*args));
2663 return PTR_ERR(args);
2665 inode = file_inode(file);
2667 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2668 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2670 * The subvolume does not exist under fd with which this is
2677 ret = btrfs_search_path_in_tree_user(inode, args);
2679 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2686 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2687 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2689 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2690 struct btrfs_fs_info *fs_info;
2691 struct btrfs_root *root;
2692 struct btrfs_path *path;
2693 struct btrfs_key key;
2694 struct btrfs_root_item *root_item;
2695 struct btrfs_root_ref *rref;
2696 struct extent_buffer *leaf;
2697 unsigned long item_off;
2698 unsigned long item_len;
2699 struct inode *inode;
2703 path = btrfs_alloc_path();
2707 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2709 btrfs_free_path(path);
2713 inode = file_inode(file);
2714 fs_info = BTRFS_I(inode)->root->fs_info;
2716 /* Get root_item of inode's subvolume */
2717 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2718 root = btrfs_get_fs_root(fs_info, key.objectid, true);
2720 ret = PTR_ERR(root);
2723 root_item = &root->root_item;
2725 subvol_info->treeid = key.objectid;
2727 subvol_info->generation = btrfs_root_generation(root_item);
2728 subvol_info->flags = btrfs_root_flags(root_item);
2730 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2731 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2733 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2736 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2737 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2738 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2740 subvol_info->otransid = btrfs_root_otransid(root_item);
2741 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2742 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2744 subvol_info->stransid = btrfs_root_stransid(root_item);
2745 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2746 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2748 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2749 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2750 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2752 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2753 /* Search root tree for ROOT_BACKREF of this subvolume */
2754 key.type = BTRFS_ROOT_BACKREF_KEY;
2756 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2759 } else if (path->slots[0] >=
2760 btrfs_header_nritems(path->nodes[0])) {
2761 ret = btrfs_next_leaf(fs_info->tree_root, path);
2764 } else if (ret > 0) {
2770 leaf = path->nodes[0];
2771 slot = path->slots[0];
2772 btrfs_item_key_to_cpu(leaf, &key, slot);
2773 if (key.objectid == subvol_info->treeid &&
2774 key.type == BTRFS_ROOT_BACKREF_KEY) {
2775 subvol_info->parent_id = key.offset;
2777 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2778 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2780 item_off = btrfs_item_ptr_offset(leaf, slot)
2781 + sizeof(struct btrfs_root_ref);
2782 item_len = btrfs_item_size_nr(leaf, slot)
2783 - sizeof(struct btrfs_root_ref);
2784 read_extent_buffer(leaf, subvol_info->name,
2785 item_off, item_len);
2792 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2796 btrfs_put_root(root);
2798 btrfs_free_path(path);
2804 * Return ROOT_REF information of the subvolume containing this inode
2805 * except the subvolume name.
2807 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2809 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2810 struct btrfs_root_ref *rref;
2811 struct btrfs_root *root;
2812 struct btrfs_path *path;
2813 struct btrfs_key key;
2814 struct extent_buffer *leaf;
2815 struct inode *inode;
2821 path = btrfs_alloc_path();
2825 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2826 if (IS_ERR(rootrefs)) {
2827 btrfs_free_path(path);
2828 return PTR_ERR(rootrefs);
2831 inode = file_inode(file);
2832 root = BTRFS_I(inode)->root->fs_info->tree_root;
2833 objectid = BTRFS_I(inode)->root->root_key.objectid;
2835 key.objectid = objectid;
2836 key.type = BTRFS_ROOT_REF_KEY;
2837 key.offset = rootrefs->min_treeid;
2840 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2843 } else if (path->slots[0] >=
2844 btrfs_header_nritems(path->nodes[0])) {
2845 ret = btrfs_next_leaf(root, path);
2848 } else if (ret > 0) {
2854 leaf = path->nodes[0];
2855 slot = path->slots[0];
2857 btrfs_item_key_to_cpu(leaf, &key, slot);
2858 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2863 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2868 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2869 rootrefs->rootref[found].treeid = key.offset;
2870 rootrefs->rootref[found].dirid =
2871 btrfs_root_ref_dirid(leaf, rref);
2874 ret = btrfs_next_item(root, path);
2877 } else if (ret > 0) {
2884 if (!ret || ret == -EOVERFLOW) {
2885 rootrefs->num_items = found;
2886 /* update min_treeid for next search */
2888 rootrefs->min_treeid =
2889 rootrefs->rootref[found - 1].treeid + 1;
2890 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2895 btrfs_free_path(path);
2900 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2904 struct dentry *parent = file->f_path.dentry;
2905 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2906 struct dentry *dentry;
2907 struct inode *dir = d_inode(parent);
2908 struct inode *inode;
2909 struct btrfs_root *root = BTRFS_I(dir)->root;
2910 struct btrfs_root *dest = NULL;
2911 struct btrfs_ioctl_vol_args *vol_args = NULL;
2912 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2913 char *subvol_name, *subvol_name_ptr = NULL;
2916 bool destroy_parent = false;
2919 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2920 if (IS_ERR(vol_args2))
2921 return PTR_ERR(vol_args2);
2923 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2929 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2930 * name, same as v1 currently does.
2932 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2933 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2934 subvol_name = vol_args2->name;
2936 err = mnt_want_write_file(file);
2940 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2945 err = mnt_want_write_file(file);
2949 dentry = btrfs_get_dentry(fs_info->sb,
2950 BTRFS_FIRST_FREE_OBJECTID,
2951 vol_args2->subvolid, 0, 0);
2952 if (IS_ERR(dentry)) {
2953 err = PTR_ERR(dentry);
2954 goto out_drop_write;
2958 * Change the default parent since the subvolume being
2959 * deleted can be outside of the current mount point.
2961 parent = btrfs_get_parent(dentry);
2964 * At this point dentry->d_name can point to '/' if the
2965 * subvolume we want to destroy is outsite of the
2966 * current mount point, so we need to release the
2967 * current dentry and execute the lookup to return a new
2968 * one with ->d_name pointing to the
2969 * <mount point>/subvol_name.
2972 if (IS_ERR(parent)) {
2973 err = PTR_ERR(parent);
2974 goto out_drop_write;
2976 dir = d_inode(parent);
2979 * If v2 was used with SPEC_BY_ID, a new parent was
2980 * allocated since the subvolume can be outside of the
2981 * current mount point. Later on we need to release this
2982 * new parent dentry.
2984 destroy_parent = true;
2986 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2987 fs_info, vol_args2->subvolid);
2988 if (IS_ERR(subvol_name_ptr)) {
2989 err = PTR_ERR(subvol_name_ptr);
2992 /* subvol_name_ptr is already NULL termined */
2993 subvol_name = (char *)kbasename(subvol_name_ptr);
2996 vol_args = memdup_user(arg, sizeof(*vol_args));
2997 if (IS_ERR(vol_args))
2998 return PTR_ERR(vol_args);
3000 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
3001 subvol_name = vol_args->name;
3003 err = mnt_want_write_file(file);
3008 subvol_namelen = strlen(subvol_name);
3010 if (strchr(subvol_name, '/') ||
3011 strncmp(subvol_name, "..", subvol_namelen) == 0) {
3013 goto free_subvol_name;
3016 if (!S_ISDIR(dir->i_mode)) {
3018 goto free_subvol_name;
3021 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
3023 goto free_subvol_name;
3024 dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
3025 if (IS_ERR(dentry)) {
3026 err = PTR_ERR(dentry);
3027 goto out_unlock_dir;
3030 if (d_really_is_negative(dentry)) {
3035 inode = d_inode(dentry);
3036 dest = BTRFS_I(inode)->root;
3037 if (!capable(CAP_SYS_ADMIN)) {
3039 * Regular user. Only allow this with a special mount
3040 * option, when the user has write+exec access to the
3041 * subvol root, and when rmdir(2) would have been
3044 * Note that this is _not_ check that the subvol is
3045 * empty or doesn't contain data that we wouldn't
3046 * otherwise be able to delete.
3048 * Users who want to delete empty subvols should try
3052 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
3056 * Do not allow deletion if the parent dir is the same
3057 * as the dir to be deleted. That means the ioctl
3058 * must be called on the dentry referencing the root
3059 * of the subvol, not a random directory contained
3066 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
3071 /* check if subvolume may be deleted by a user */
3072 err = btrfs_may_delete(dir, dentry, 1);
3076 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
3082 err = btrfs_delete_subvolume(dir, dentry);
3083 inode_unlock(inode);
3085 fsnotify_rmdir(dir, dentry);
3094 kfree(subvol_name_ptr);
3099 mnt_drop_write_file(file);
3106 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
3108 struct inode *inode = file_inode(file);
3109 struct btrfs_root *root = BTRFS_I(inode)->root;
3110 struct btrfs_ioctl_defrag_range_args *range;
3113 ret = mnt_want_write_file(file);
3117 if (btrfs_root_readonly(root)) {
3122 switch (inode->i_mode & S_IFMT) {
3124 if (!capable(CAP_SYS_ADMIN)) {
3128 ret = btrfs_defrag_root(root);
3132 * Note that this does not check the file descriptor for write
3133 * access. This prevents defragmenting executables that are
3134 * running and allows defrag on files open in read-only mode.
3136 if (!capable(CAP_SYS_ADMIN) &&
3137 inode_permission(inode, MAY_WRITE)) {
3142 range = kzalloc(sizeof(*range), GFP_KERNEL);
3149 if (copy_from_user(range, argp,
3155 /* compression requires us to start the IO */
3156 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3157 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3158 range->extent_thresh = (u32)-1;
3161 /* the rest are all set to zero by kzalloc */
3162 range->len = (u64)-1;
3164 ret = btrfs_defrag_file(file_inode(file), file,
3165 range, BTRFS_OLDEST_GENERATION, 0);
3174 mnt_drop_write_file(file);
3178 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3180 struct btrfs_ioctl_vol_args *vol_args;
3183 if (!capable(CAP_SYS_ADMIN))
3186 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
3187 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3189 vol_args = memdup_user(arg, sizeof(*vol_args));
3190 if (IS_ERR(vol_args)) {
3191 ret = PTR_ERR(vol_args);
3195 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3196 ret = btrfs_init_new_device(fs_info, vol_args->name);
3199 btrfs_info(fs_info, "disk added %s", vol_args->name);
3203 btrfs_exclop_finish(fs_info);
3207 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3209 struct inode *inode = file_inode(file);
3210 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3211 struct btrfs_ioctl_vol_args_v2 *vol_args;
3214 if (!capable(CAP_SYS_ADMIN))
3217 ret = mnt_want_write_file(file);
3221 vol_args = memdup_user(arg, sizeof(*vol_args));
3222 if (IS_ERR(vol_args)) {
3223 ret = PTR_ERR(vol_args);
3227 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
3232 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3233 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3237 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3238 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3240 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3241 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3243 btrfs_exclop_finish(fs_info);
3246 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3247 btrfs_info(fs_info, "device deleted: id %llu",
3250 btrfs_info(fs_info, "device deleted: %s",
3256 mnt_drop_write_file(file);
3260 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3262 struct inode *inode = file_inode(file);
3263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3264 struct btrfs_ioctl_vol_args *vol_args;
3267 if (!capable(CAP_SYS_ADMIN))
3270 ret = mnt_want_write_file(file);
3274 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3275 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3276 goto out_drop_write;
3279 vol_args = memdup_user(arg, sizeof(*vol_args));
3280 if (IS_ERR(vol_args)) {
3281 ret = PTR_ERR(vol_args);
3285 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3286 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3289 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3292 btrfs_exclop_finish(fs_info);
3294 mnt_drop_write_file(file);
3299 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3302 struct btrfs_ioctl_fs_info_args *fi_args;
3303 struct btrfs_device *device;
3304 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3308 fi_args = memdup_user(arg, sizeof(*fi_args));
3309 if (IS_ERR(fi_args))
3310 return PTR_ERR(fi_args);
3312 flags_in = fi_args->flags;
3313 memset(fi_args, 0, sizeof(*fi_args));
3316 fi_args->num_devices = fs_devices->num_devices;
3318 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3319 if (device->devid > fi_args->max_id)
3320 fi_args->max_id = device->devid;
3324 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3325 fi_args->nodesize = fs_info->nodesize;
3326 fi_args->sectorsize = fs_info->sectorsize;
3327 fi_args->clone_alignment = fs_info->sectorsize;
3329 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3330 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3331 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3332 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3335 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3336 fi_args->generation = fs_info->generation;
3337 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3340 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3341 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3342 sizeof(fi_args->metadata_uuid));
3343 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3346 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3353 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3356 struct btrfs_ioctl_dev_info_args *di_args;
3357 struct btrfs_device *dev;
3359 char *s_uuid = NULL;
3361 di_args = memdup_user(arg, sizeof(*di_args));
3362 if (IS_ERR(di_args))
3363 return PTR_ERR(di_args);
3365 if (!btrfs_is_empty_uuid(di_args->uuid))
3366 s_uuid = di_args->uuid;
3369 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3377 di_args->devid = dev->devid;
3378 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3379 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3380 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3382 strncpy(di_args->path, rcu_str_deref(dev->name),
3383 sizeof(di_args->path) - 1);
3384 di_args->path[sizeof(di_args->path) - 1] = 0;
3386 di_args->path[0] = '\0';
3391 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3398 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3400 struct inode *inode = file_inode(file);
3401 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3402 struct btrfs_root *root = BTRFS_I(inode)->root;
3403 struct btrfs_root *new_root;
3404 struct btrfs_dir_item *di;
3405 struct btrfs_trans_handle *trans;
3406 struct btrfs_path *path = NULL;
3407 struct btrfs_disk_key disk_key;
3412 if (!capable(CAP_SYS_ADMIN))
3415 ret = mnt_want_write_file(file);
3419 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3425 objectid = BTRFS_FS_TREE_OBJECTID;
3427 new_root = btrfs_get_fs_root(fs_info, objectid, true);
3428 if (IS_ERR(new_root)) {
3429 ret = PTR_ERR(new_root);
3432 if (!is_fstree(new_root->root_key.objectid)) {
3437 path = btrfs_alloc_path();
3443 trans = btrfs_start_transaction(root, 1);
3444 if (IS_ERR(trans)) {
3445 ret = PTR_ERR(trans);
3449 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3450 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
3451 dir_id, "default", 7, 1);
3452 if (IS_ERR_OR_NULL(di)) {
3453 btrfs_release_path(path);
3454 btrfs_end_transaction(trans);
3456 "Umm, you don't have the default diritem, this isn't going to work");
3461 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3462 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3463 btrfs_mark_buffer_dirty(path->nodes[0]);
3464 btrfs_release_path(path);
3466 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
3467 btrfs_end_transaction(trans);
3469 btrfs_put_root(new_root);
3470 btrfs_free_path(path);
3472 mnt_drop_write_file(file);
3476 static void get_block_group_info(struct list_head *groups_list,
3477 struct btrfs_ioctl_space_info *space)
3479 struct btrfs_block_group *block_group;
3481 space->total_bytes = 0;
3482 space->used_bytes = 0;
3484 list_for_each_entry(block_group, groups_list, list) {
3485 space->flags = block_group->flags;
3486 space->total_bytes += block_group->length;
3487 space->used_bytes += block_group->used;
3491 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3494 struct btrfs_ioctl_space_args space_args;
3495 struct btrfs_ioctl_space_info space;
3496 struct btrfs_ioctl_space_info *dest;
3497 struct btrfs_ioctl_space_info *dest_orig;
3498 struct btrfs_ioctl_space_info __user *user_dest;
3499 struct btrfs_space_info *info;
3500 static const u64 types[] = {
3501 BTRFS_BLOCK_GROUP_DATA,
3502 BTRFS_BLOCK_GROUP_SYSTEM,
3503 BTRFS_BLOCK_GROUP_METADATA,
3504 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3512 if (copy_from_user(&space_args,
3513 (struct btrfs_ioctl_space_args __user *)arg,
3514 sizeof(space_args)))
3517 for (i = 0; i < num_types; i++) {
3518 struct btrfs_space_info *tmp;
3521 list_for_each_entry(tmp, &fs_info->space_info, list) {
3522 if (tmp->flags == types[i]) {
3531 down_read(&info->groups_sem);
3532 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3533 if (!list_empty(&info->block_groups[c]))
3536 up_read(&info->groups_sem);
3540 * Global block reserve, exported as a space_info
3544 /* space_slots == 0 means they are asking for a count */
3545 if (space_args.space_slots == 0) {
3546 space_args.total_spaces = slot_count;
3550 slot_count = min_t(u64, space_args.space_slots, slot_count);
3552 alloc_size = sizeof(*dest) * slot_count;
3554 /* we generally have at most 6 or so space infos, one for each raid
3555 * level. So, a whole page should be more than enough for everyone
3557 if (alloc_size > PAGE_SIZE)
3560 space_args.total_spaces = 0;
3561 dest = kmalloc(alloc_size, GFP_KERNEL);
3566 /* now we have a buffer to copy into */
3567 for (i = 0; i < num_types; i++) {
3568 struct btrfs_space_info *tmp;
3574 list_for_each_entry(tmp, &fs_info->space_info, list) {
3575 if (tmp->flags == types[i]) {
3583 down_read(&info->groups_sem);
3584 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3585 if (!list_empty(&info->block_groups[c])) {
3586 get_block_group_info(&info->block_groups[c],
3588 memcpy(dest, &space, sizeof(space));
3590 space_args.total_spaces++;
3596 up_read(&info->groups_sem);
3600 * Add global block reserve
3603 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3605 spin_lock(&block_rsv->lock);
3606 space.total_bytes = block_rsv->size;
3607 space.used_bytes = block_rsv->size - block_rsv->reserved;
3608 spin_unlock(&block_rsv->lock);
3609 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3610 memcpy(dest, &space, sizeof(space));
3611 space_args.total_spaces++;
3614 user_dest = (struct btrfs_ioctl_space_info __user *)
3615 (arg + sizeof(struct btrfs_ioctl_space_args));
3617 if (copy_to_user(user_dest, dest_orig, alloc_size))
3622 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3628 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3631 struct btrfs_trans_handle *trans;
3635 trans = btrfs_attach_transaction_barrier(root);
3636 if (IS_ERR(trans)) {
3637 if (PTR_ERR(trans) != -ENOENT)
3638 return PTR_ERR(trans);
3640 /* No running transaction, don't bother */
3641 transid = root->fs_info->last_trans_committed;
3644 transid = trans->transid;
3645 ret = btrfs_commit_transaction_async(trans, 0);
3647 btrfs_end_transaction(trans);
3652 if (copy_to_user(argp, &transid, sizeof(transid)))
3657 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
3663 if (copy_from_user(&transid, argp, sizeof(transid)))
3666 transid = 0; /* current trans */
3668 return btrfs_wait_for_commit(fs_info, transid);
3671 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3673 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
3674 struct btrfs_ioctl_scrub_args *sa;
3677 if (!capable(CAP_SYS_ADMIN))
3680 sa = memdup_user(arg, sizeof(*sa));
3684 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3685 ret = mnt_want_write_file(file);
3690 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
3691 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3695 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3696 * error. This is important as it allows user space to know how much
3697 * progress scrub has done. For example, if scrub is canceled we get
3698 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3699 * space. Later user space can inspect the progress from the structure
3700 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3701 * previously (btrfs-progs does this).
3702 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3703 * then return -EFAULT to signal the structure was not copied or it may
3704 * be corrupt and unreliable due to a partial copy.
3706 if (copy_to_user(arg, sa, sizeof(*sa)))
3709 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3710 mnt_drop_write_file(file);
3716 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
3718 if (!capable(CAP_SYS_ADMIN))
3721 return btrfs_scrub_cancel(fs_info);
3724 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
3727 struct btrfs_ioctl_scrub_args *sa;
3730 if (!capable(CAP_SYS_ADMIN))
3733 sa = memdup_user(arg, sizeof(*sa));
3737 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
3739 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3746 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
3749 struct btrfs_ioctl_get_dev_stats *sa;
3752 sa = memdup_user(arg, sizeof(*sa));
3756 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3761 ret = btrfs_get_dev_stats(fs_info, sa);
3763 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3770 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3773 struct btrfs_ioctl_dev_replace_args *p;
3776 if (!capable(CAP_SYS_ADMIN))
3779 p = memdup_user(arg, sizeof(*p));
3784 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3785 if (sb_rdonly(fs_info->sb)) {
3789 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
3790 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3792 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
3793 btrfs_exclop_finish(fs_info);
3796 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3797 btrfs_dev_replace_status(fs_info, p);
3800 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3801 p->result = btrfs_dev_replace_cancel(fs_info);
3809 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
3816 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3822 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3823 struct inode_fs_paths *ipath = NULL;
3824 struct btrfs_path *path;
3826 if (!capable(CAP_DAC_READ_SEARCH))
3829 path = btrfs_alloc_path();
3835 ipa = memdup_user(arg, sizeof(*ipa));
3842 size = min_t(u32, ipa->size, 4096);
3843 ipath = init_ipath(size, root, path);
3844 if (IS_ERR(ipath)) {
3845 ret = PTR_ERR(ipath);
3850 ret = paths_from_inode(ipa->inum, ipath);
3854 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3855 rel_ptr = ipath->fspath->val[i] -
3856 (u64)(unsigned long)ipath->fspath->val;
3857 ipath->fspath->val[i] = rel_ptr;
3860 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3861 ipath->fspath, size);
3868 btrfs_free_path(path);
3875 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3877 struct btrfs_data_container *inodes = ctx;
3878 const size_t c = 3 * sizeof(u64);
3880 if (inodes->bytes_left >= c) {
3881 inodes->bytes_left -= c;
3882 inodes->val[inodes->elem_cnt] = inum;
3883 inodes->val[inodes->elem_cnt + 1] = offset;
3884 inodes->val[inodes->elem_cnt + 2] = root;
3885 inodes->elem_cnt += 3;
3887 inodes->bytes_missing += c - inodes->bytes_left;
3888 inodes->bytes_left = 0;
3889 inodes->elem_missed += 3;
3895 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
3896 void __user *arg, int version)
3900 struct btrfs_ioctl_logical_ino_args *loi;
3901 struct btrfs_data_container *inodes = NULL;
3902 struct btrfs_path *path = NULL;
3905 if (!capable(CAP_SYS_ADMIN))
3908 loi = memdup_user(arg, sizeof(*loi));
3910 return PTR_ERR(loi);
3913 ignore_offset = false;
3914 size = min_t(u32, loi->size, SZ_64K);
3916 /* All reserved bits must be 0 for now */
3917 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3921 /* Only accept flags we have defined so far */
3922 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3926 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
3927 size = min_t(u32, loi->size, SZ_16M);
3930 path = btrfs_alloc_path();
3936 inodes = init_data_container(size);
3937 if (IS_ERR(inodes)) {
3938 ret = PTR_ERR(inodes);
3943 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
3944 build_ino_list, inodes, ignore_offset);
3950 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3956 btrfs_free_path(path);
3964 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3965 struct btrfs_ioctl_balance_args *bargs)
3967 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3969 bargs->flags = bctl->flags;
3971 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
3972 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3973 if (atomic_read(&fs_info->balance_pause_req))
3974 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3975 if (atomic_read(&fs_info->balance_cancel_req))
3976 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3978 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3979 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3980 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3982 spin_lock(&fs_info->balance_lock);
3983 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3984 spin_unlock(&fs_info->balance_lock);
3987 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3989 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3990 struct btrfs_fs_info *fs_info = root->fs_info;
3991 struct btrfs_ioctl_balance_args *bargs;
3992 struct btrfs_balance_control *bctl;
3993 bool need_unlock; /* for mut. excl. ops lock */
3996 if (!capable(CAP_SYS_ADMIN))
3999 ret = mnt_want_write_file(file);
4004 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
4005 mutex_lock(&fs_info->balance_mutex);
4011 * mut. excl. ops lock is locked. Three possibilities:
4012 * (1) some other op is running
4013 * (2) balance is running
4014 * (3) balance is paused -- special case (think resume)
4016 mutex_lock(&fs_info->balance_mutex);
4017 if (fs_info->balance_ctl) {
4018 /* this is either (2) or (3) */
4019 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4020 mutex_unlock(&fs_info->balance_mutex);
4022 * Lock released to allow other waiters to continue,
4023 * we'll reexamine the status again.
4025 mutex_lock(&fs_info->balance_mutex);
4027 if (fs_info->balance_ctl &&
4028 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4030 need_unlock = false;
4034 mutex_unlock(&fs_info->balance_mutex);
4038 mutex_unlock(&fs_info->balance_mutex);
4044 mutex_unlock(&fs_info->balance_mutex);
4045 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4052 bargs = memdup_user(arg, sizeof(*bargs));
4053 if (IS_ERR(bargs)) {
4054 ret = PTR_ERR(bargs);
4058 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4059 if (!fs_info->balance_ctl) {
4064 bctl = fs_info->balance_ctl;
4065 spin_lock(&fs_info->balance_lock);
4066 bctl->flags |= BTRFS_BALANCE_RESUME;
4067 spin_unlock(&fs_info->balance_lock);
4075 if (fs_info->balance_ctl) {
4080 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4087 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4088 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4089 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4091 bctl->flags = bargs->flags;
4093 /* balance everything - no filters */
4094 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4097 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4104 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4105 * bctl is freed in reset_balance_state, or, if restriper was paused
4106 * all the way until unmount, in free_fs_info. The flag should be
4107 * cleared after reset_balance_state.
4109 need_unlock = false;
4111 ret = btrfs_balance(fs_info, bctl, bargs);
4114 if ((ret == 0 || ret == -ECANCELED) && arg) {
4115 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4124 mutex_unlock(&fs_info->balance_mutex);
4126 btrfs_exclop_finish(fs_info);
4128 mnt_drop_write_file(file);
4132 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4134 if (!capable(CAP_SYS_ADMIN))
4138 case BTRFS_BALANCE_CTL_PAUSE:
4139 return btrfs_pause_balance(fs_info);
4140 case BTRFS_BALANCE_CTL_CANCEL:
4141 return btrfs_cancel_balance(fs_info);
4147 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4150 struct btrfs_ioctl_balance_args *bargs;
4153 if (!capable(CAP_SYS_ADMIN))
4156 mutex_lock(&fs_info->balance_mutex);
4157 if (!fs_info->balance_ctl) {
4162 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4168 btrfs_update_ioctl_balance_args(fs_info, bargs);
4170 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4175 mutex_unlock(&fs_info->balance_mutex);
4179 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4181 struct inode *inode = file_inode(file);
4182 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4183 struct btrfs_ioctl_quota_ctl_args *sa;
4186 if (!capable(CAP_SYS_ADMIN))
4189 ret = mnt_want_write_file(file);
4193 sa = memdup_user(arg, sizeof(*sa));
4199 down_write(&fs_info->subvol_sem);
4202 case BTRFS_QUOTA_CTL_ENABLE:
4203 ret = btrfs_quota_enable(fs_info);
4205 case BTRFS_QUOTA_CTL_DISABLE:
4206 ret = btrfs_quota_disable(fs_info);
4214 up_write(&fs_info->subvol_sem);
4216 mnt_drop_write_file(file);
4220 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4222 struct inode *inode = file_inode(file);
4223 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4224 struct btrfs_root *root = BTRFS_I(inode)->root;
4225 struct btrfs_ioctl_qgroup_assign_args *sa;
4226 struct btrfs_trans_handle *trans;
4230 if (!capable(CAP_SYS_ADMIN))
4233 ret = mnt_want_write_file(file);
4237 sa = memdup_user(arg, sizeof(*sa));
4243 trans = btrfs_join_transaction(root);
4244 if (IS_ERR(trans)) {
4245 ret = PTR_ERR(trans);
4250 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4252 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4255 /* update qgroup status and info */
4256 err = btrfs_run_qgroups(trans);
4258 btrfs_handle_fs_error(fs_info, err,
4259 "failed to update qgroup status and info");
4260 err = btrfs_end_transaction(trans);
4267 mnt_drop_write_file(file);
4271 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4273 struct inode *inode = file_inode(file);
4274 struct btrfs_root *root = BTRFS_I(inode)->root;
4275 struct btrfs_ioctl_qgroup_create_args *sa;
4276 struct btrfs_trans_handle *trans;
4280 if (!capable(CAP_SYS_ADMIN))
4283 ret = mnt_want_write_file(file);
4287 sa = memdup_user(arg, sizeof(*sa));
4293 if (!sa->qgroupid) {
4298 trans = btrfs_join_transaction(root);
4299 if (IS_ERR(trans)) {
4300 ret = PTR_ERR(trans);
4305 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4307 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4310 err = btrfs_end_transaction(trans);
4317 mnt_drop_write_file(file);
4321 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4323 struct inode *inode = file_inode(file);
4324 struct btrfs_root *root = BTRFS_I(inode)->root;
4325 struct btrfs_ioctl_qgroup_limit_args *sa;
4326 struct btrfs_trans_handle *trans;
4331 if (!capable(CAP_SYS_ADMIN))
4334 ret = mnt_want_write_file(file);
4338 sa = memdup_user(arg, sizeof(*sa));
4344 trans = btrfs_join_transaction(root);
4345 if (IS_ERR(trans)) {
4346 ret = PTR_ERR(trans);
4350 qgroupid = sa->qgroupid;
4352 /* take the current subvol as qgroup */
4353 qgroupid = root->root_key.objectid;
4356 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
4358 err = btrfs_end_transaction(trans);
4365 mnt_drop_write_file(file);
4369 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4371 struct inode *inode = file_inode(file);
4372 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4373 struct btrfs_ioctl_quota_rescan_args *qsa;
4376 if (!capable(CAP_SYS_ADMIN))
4379 ret = mnt_want_write_file(file);
4383 qsa = memdup_user(arg, sizeof(*qsa));
4394 ret = btrfs_qgroup_rescan(fs_info);
4399 mnt_drop_write_file(file);
4403 static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4406 struct btrfs_ioctl_quota_rescan_args *qsa;
4409 if (!capable(CAP_SYS_ADMIN))
4412 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
4416 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4418 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
4421 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4428 static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4431 if (!capable(CAP_SYS_ADMIN))
4434 return btrfs_qgroup_wait_for_completion(fs_info, true);
4437 static long _btrfs_ioctl_set_received_subvol(struct file *file,
4438 struct btrfs_ioctl_received_subvol_args *sa)
4440 struct inode *inode = file_inode(file);
4441 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4442 struct btrfs_root *root = BTRFS_I(inode)->root;
4443 struct btrfs_root_item *root_item = &root->root_item;
4444 struct btrfs_trans_handle *trans;
4445 struct timespec64 ct = current_time(inode);
4447 int received_uuid_changed;
4449 if (!inode_owner_or_capable(inode))
4452 ret = mnt_want_write_file(file);
4456 down_write(&fs_info->subvol_sem);
4458 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
4463 if (btrfs_root_readonly(root)) {
4470 * 2 - uuid items (received uuid + subvol uuid)
4472 trans = btrfs_start_transaction(root, 3);
4473 if (IS_ERR(trans)) {
4474 ret = PTR_ERR(trans);
4479 sa->rtransid = trans->transid;
4480 sa->rtime.sec = ct.tv_sec;
4481 sa->rtime.nsec = ct.tv_nsec;
4483 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4485 if (received_uuid_changed &&
4486 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4487 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
4488 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4489 root->root_key.objectid);
4490 if (ret && ret != -ENOENT) {
4491 btrfs_abort_transaction(trans, ret);
4492 btrfs_end_transaction(trans);
4496 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4497 btrfs_set_root_stransid(root_item, sa->stransid);
4498 btrfs_set_root_rtransid(root_item, sa->rtransid);
4499 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4500 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4501 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4502 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4504 ret = btrfs_update_root(trans, fs_info->tree_root,
4505 &root->root_key, &root->root_item);
4507 btrfs_end_transaction(trans);
4510 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4511 ret = btrfs_uuid_tree_add(trans, sa->uuid,
4512 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4513 root->root_key.objectid);
4514 if (ret < 0 && ret != -EEXIST) {
4515 btrfs_abort_transaction(trans, ret);
4516 btrfs_end_transaction(trans);
4520 ret = btrfs_commit_transaction(trans);
4522 up_write(&fs_info->subvol_sem);
4523 mnt_drop_write_file(file);
4528 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4531 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4532 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4535 args32 = memdup_user(arg, sizeof(*args32));
4537 return PTR_ERR(args32);
4539 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
4545 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4546 args64->stransid = args32->stransid;
4547 args64->rtransid = args32->rtransid;
4548 args64->stime.sec = args32->stime.sec;
4549 args64->stime.nsec = args32->stime.nsec;
4550 args64->rtime.sec = args32->rtime.sec;
4551 args64->rtime.nsec = args32->rtime.nsec;
4552 args64->flags = args32->flags;
4554 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4558 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4559 args32->stransid = args64->stransid;
4560 args32->rtransid = args64->rtransid;
4561 args32->stime.sec = args64->stime.sec;
4562 args32->stime.nsec = args64->stime.nsec;
4563 args32->rtime.sec = args64->rtime.sec;
4564 args32->rtime.nsec = args64->rtime.nsec;
4565 args32->flags = args64->flags;
4567 ret = copy_to_user(arg, args32, sizeof(*args32));
4578 static long btrfs_ioctl_set_received_subvol(struct file *file,
4581 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4584 sa = memdup_user(arg, sizeof(*sa));
4588 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4593 ret = copy_to_user(arg, sa, sizeof(*sa));
4602 static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4607 char label[BTRFS_LABEL_SIZE];
4609 spin_lock(&fs_info->super_lock);
4610 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4611 spin_unlock(&fs_info->super_lock);
4613 len = strnlen(label, BTRFS_LABEL_SIZE);
4615 if (len == BTRFS_LABEL_SIZE) {
4617 "label is too long, return the first %zu bytes",
4621 ret = copy_to_user(arg, label, len);
4623 return ret ? -EFAULT : 0;
4626 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4628 struct inode *inode = file_inode(file);
4629 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4630 struct btrfs_root *root = BTRFS_I(inode)->root;
4631 struct btrfs_super_block *super_block = fs_info->super_copy;
4632 struct btrfs_trans_handle *trans;
4633 char label[BTRFS_LABEL_SIZE];
4636 if (!capable(CAP_SYS_ADMIN))
4639 if (copy_from_user(label, arg, sizeof(label)))
4642 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4644 "unable to set label with more than %d bytes",
4645 BTRFS_LABEL_SIZE - 1);
4649 ret = mnt_want_write_file(file);
4653 trans = btrfs_start_transaction(root, 0);
4654 if (IS_ERR(trans)) {
4655 ret = PTR_ERR(trans);
4659 spin_lock(&fs_info->super_lock);
4660 strcpy(super_block->label, label);
4661 spin_unlock(&fs_info->super_lock);
4662 ret = btrfs_commit_transaction(trans);
4665 mnt_drop_write_file(file);
4669 #define INIT_FEATURE_FLAGS(suffix) \
4670 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4671 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4672 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4674 int btrfs_ioctl_get_supported_features(void __user *arg)
4676 static const struct btrfs_ioctl_feature_flags features[3] = {
4677 INIT_FEATURE_FLAGS(SUPP),
4678 INIT_FEATURE_FLAGS(SAFE_SET),
4679 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4682 if (copy_to_user(arg, &features, sizeof(features)))
4688 static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4691 struct btrfs_super_block *super_block = fs_info->super_copy;
4692 struct btrfs_ioctl_feature_flags features;
4694 features.compat_flags = btrfs_super_compat_flags(super_block);
4695 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4696 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4698 if (copy_to_user(arg, &features, sizeof(features)))
4704 static int check_feature_bits(struct btrfs_fs_info *fs_info,
4705 enum btrfs_feature_set set,
4706 u64 change_mask, u64 flags, u64 supported_flags,
4707 u64 safe_set, u64 safe_clear)
4709 const char *type = btrfs_feature_set_name(set);
4711 u64 disallowed, unsupported;
4712 u64 set_mask = flags & change_mask;
4713 u64 clear_mask = ~flags & change_mask;
4715 unsupported = set_mask & ~supported_flags;
4717 names = btrfs_printable_features(set, unsupported);
4720 "this kernel does not support the %s feature bit%s",
4721 names, strchr(names, ',') ? "s" : "");
4725 "this kernel does not support %s bits 0x%llx",
4730 disallowed = set_mask & ~safe_set;
4732 names = btrfs_printable_features(set, disallowed);
4735 "can't set the %s feature bit%s while mounted",
4736 names, strchr(names, ',') ? "s" : "");
4740 "can't set %s bits 0x%llx while mounted",
4745 disallowed = clear_mask & ~safe_clear;
4747 names = btrfs_printable_features(set, disallowed);
4750 "can't clear the %s feature bit%s while mounted",
4751 names, strchr(names, ',') ? "s" : "");
4755 "can't clear %s bits 0x%llx while mounted",
4763 #define check_feature(fs_info, change_mask, flags, mask_base) \
4764 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
4765 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4766 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4767 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4769 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4771 struct inode *inode = file_inode(file);
4772 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4773 struct btrfs_root *root = BTRFS_I(inode)->root;
4774 struct btrfs_super_block *super_block = fs_info->super_copy;
4775 struct btrfs_ioctl_feature_flags flags[2];
4776 struct btrfs_trans_handle *trans;
4780 if (!capable(CAP_SYS_ADMIN))
4783 if (copy_from_user(flags, arg, sizeof(flags)))
4787 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4788 !flags[0].incompat_flags)
4791 ret = check_feature(fs_info, flags[0].compat_flags,
4792 flags[1].compat_flags, COMPAT);
4796 ret = check_feature(fs_info, flags[0].compat_ro_flags,
4797 flags[1].compat_ro_flags, COMPAT_RO);
4801 ret = check_feature(fs_info, flags[0].incompat_flags,
4802 flags[1].incompat_flags, INCOMPAT);
4806 ret = mnt_want_write_file(file);
4810 trans = btrfs_start_transaction(root, 0);
4811 if (IS_ERR(trans)) {
4812 ret = PTR_ERR(trans);
4813 goto out_drop_write;
4816 spin_lock(&fs_info->super_lock);
4817 newflags = btrfs_super_compat_flags(super_block);
4818 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4819 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4820 btrfs_set_super_compat_flags(super_block, newflags);
4822 newflags = btrfs_super_compat_ro_flags(super_block);
4823 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4824 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4825 btrfs_set_super_compat_ro_flags(super_block, newflags);
4827 newflags = btrfs_super_incompat_flags(super_block);
4828 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4829 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4830 btrfs_set_super_incompat_flags(super_block, newflags);
4831 spin_unlock(&fs_info->super_lock);
4833 ret = btrfs_commit_transaction(trans);
4835 mnt_drop_write_file(file);
4840 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4842 struct btrfs_ioctl_send_args *arg;
4846 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4847 struct btrfs_ioctl_send_args_32 args32;
4849 ret = copy_from_user(&args32, argp, sizeof(args32));
4852 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4855 arg->send_fd = args32.send_fd;
4856 arg->clone_sources_count = args32.clone_sources_count;
4857 arg->clone_sources = compat_ptr(args32.clone_sources);
4858 arg->parent_root = args32.parent_root;
4859 arg->flags = args32.flags;
4860 memcpy(arg->reserved, args32.reserved,
4861 sizeof(args32.reserved));
4866 arg = memdup_user(argp, sizeof(*arg));
4868 return PTR_ERR(arg);
4870 ret = btrfs_ioctl_send(file, arg);
4875 long btrfs_ioctl(struct file *file, unsigned int
4876 cmd, unsigned long arg)
4878 struct inode *inode = file_inode(file);
4879 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4880 struct btrfs_root *root = BTRFS_I(inode)->root;
4881 void __user *argp = (void __user *)arg;
4884 case FS_IOC_GETFLAGS:
4885 return btrfs_ioctl_getflags(file, argp);
4886 case FS_IOC_SETFLAGS:
4887 return btrfs_ioctl_setflags(file, argp);
4888 case FS_IOC_GETVERSION:
4889 return btrfs_ioctl_getversion(file, argp);
4890 case FS_IOC_GETFSLABEL:
4891 return btrfs_ioctl_get_fslabel(fs_info, argp);
4892 case FS_IOC_SETFSLABEL:
4893 return btrfs_ioctl_set_fslabel(file, argp);
4895 return btrfs_ioctl_fitrim(fs_info, argp);
4896 case BTRFS_IOC_SNAP_CREATE:
4897 return btrfs_ioctl_snap_create(file, argp, 0);
4898 case BTRFS_IOC_SNAP_CREATE_V2:
4899 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4900 case BTRFS_IOC_SUBVOL_CREATE:
4901 return btrfs_ioctl_snap_create(file, argp, 1);
4902 case BTRFS_IOC_SUBVOL_CREATE_V2:
4903 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4904 case BTRFS_IOC_SNAP_DESTROY:
4905 return btrfs_ioctl_snap_destroy(file, argp, false);
4906 case BTRFS_IOC_SNAP_DESTROY_V2:
4907 return btrfs_ioctl_snap_destroy(file, argp, true);
4908 case BTRFS_IOC_SUBVOL_GETFLAGS:
4909 return btrfs_ioctl_subvol_getflags(file, argp);
4910 case BTRFS_IOC_SUBVOL_SETFLAGS:
4911 return btrfs_ioctl_subvol_setflags(file, argp);
4912 case BTRFS_IOC_DEFAULT_SUBVOL:
4913 return btrfs_ioctl_default_subvol(file, argp);
4914 case BTRFS_IOC_DEFRAG:
4915 return btrfs_ioctl_defrag(file, NULL);
4916 case BTRFS_IOC_DEFRAG_RANGE:
4917 return btrfs_ioctl_defrag(file, argp);
4918 case BTRFS_IOC_RESIZE:
4919 return btrfs_ioctl_resize(file, argp);
4920 case BTRFS_IOC_ADD_DEV:
4921 return btrfs_ioctl_add_dev(fs_info, argp);
4922 case BTRFS_IOC_RM_DEV:
4923 return btrfs_ioctl_rm_dev(file, argp);
4924 case BTRFS_IOC_RM_DEV_V2:
4925 return btrfs_ioctl_rm_dev_v2(file, argp);
4926 case BTRFS_IOC_FS_INFO:
4927 return btrfs_ioctl_fs_info(fs_info, argp);
4928 case BTRFS_IOC_DEV_INFO:
4929 return btrfs_ioctl_dev_info(fs_info, argp);
4930 case BTRFS_IOC_BALANCE:
4931 return btrfs_ioctl_balance(file, NULL);
4932 case BTRFS_IOC_TREE_SEARCH:
4933 return btrfs_ioctl_tree_search(file, argp);
4934 case BTRFS_IOC_TREE_SEARCH_V2:
4935 return btrfs_ioctl_tree_search_v2(file, argp);
4936 case BTRFS_IOC_INO_LOOKUP:
4937 return btrfs_ioctl_ino_lookup(file, argp);
4938 case BTRFS_IOC_INO_PATHS:
4939 return btrfs_ioctl_ino_to_path(root, argp);
4940 case BTRFS_IOC_LOGICAL_INO:
4941 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4942 case BTRFS_IOC_LOGICAL_INO_V2:
4943 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
4944 case BTRFS_IOC_SPACE_INFO:
4945 return btrfs_ioctl_space_info(fs_info, argp);
4946 case BTRFS_IOC_SYNC: {
4949 ret = btrfs_start_delalloc_roots(fs_info, U64_MAX, false);
4952 ret = btrfs_sync_fs(inode->i_sb, 1);
4954 * The transaction thread may want to do more work,
4955 * namely it pokes the cleaner kthread that will start
4956 * processing uncleaned subvols.
4958 wake_up_process(fs_info->transaction_kthread);
4961 case BTRFS_IOC_START_SYNC:
4962 return btrfs_ioctl_start_sync(root, argp);
4963 case BTRFS_IOC_WAIT_SYNC:
4964 return btrfs_ioctl_wait_sync(fs_info, argp);
4965 case BTRFS_IOC_SCRUB:
4966 return btrfs_ioctl_scrub(file, argp);
4967 case BTRFS_IOC_SCRUB_CANCEL:
4968 return btrfs_ioctl_scrub_cancel(fs_info);
4969 case BTRFS_IOC_SCRUB_PROGRESS:
4970 return btrfs_ioctl_scrub_progress(fs_info, argp);
4971 case BTRFS_IOC_BALANCE_V2:
4972 return btrfs_ioctl_balance(file, argp);
4973 case BTRFS_IOC_BALANCE_CTL:
4974 return btrfs_ioctl_balance_ctl(fs_info, arg);
4975 case BTRFS_IOC_BALANCE_PROGRESS:
4976 return btrfs_ioctl_balance_progress(fs_info, argp);
4977 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4978 return btrfs_ioctl_set_received_subvol(file, argp);
4980 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4981 return btrfs_ioctl_set_received_subvol_32(file, argp);
4983 case BTRFS_IOC_SEND:
4984 return _btrfs_ioctl_send(file, argp, false);
4985 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4986 case BTRFS_IOC_SEND_32:
4987 return _btrfs_ioctl_send(file, argp, true);
4989 case BTRFS_IOC_GET_DEV_STATS:
4990 return btrfs_ioctl_get_dev_stats(fs_info, argp);
4991 case BTRFS_IOC_QUOTA_CTL:
4992 return btrfs_ioctl_quota_ctl(file, argp);
4993 case BTRFS_IOC_QGROUP_ASSIGN:
4994 return btrfs_ioctl_qgroup_assign(file, argp);
4995 case BTRFS_IOC_QGROUP_CREATE:
4996 return btrfs_ioctl_qgroup_create(file, argp);
4997 case BTRFS_IOC_QGROUP_LIMIT:
4998 return btrfs_ioctl_qgroup_limit(file, argp);
4999 case BTRFS_IOC_QUOTA_RESCAN:
5000 return btrfs_ioctl_quota_rescan(file, argp);
5001 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5002 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
5003 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5004 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
5005 case BTRFS_IOC_DEV_REPLACE:
5006 return btrfs_ioctl_dev_replace(fs_info, argp);
5007 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5008 return btrfs_ioctl_get_supported_features(argp);
5009 case BTRFS_IOC_GET_FEATURES:
5010 return btrfs_ioctl_get_features(fs_info, argp);
5011 case BTRFS_IOC_SET_FEATURES:
5012 return btrfs_ioctl_set_features(file, argp);
5013 case FS_IOC_FSGETXATTR:
5014 return btrfs_ioctl_fsgetxattr(file, argp);
5015 case FS_IOC_FSSETXATTR:
5016 return btrfs_ioctl_fssetxattr(file, argp);
5017 case BTRFS_IOC_GET_SUBVOL_INFO:
5018 return btrfs_ioctl_get_subvol_info(file, argp);
5019 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5020 return btrfs_ioctl_get_subvol_rootref(file, argp);
5021 case BTRFS_IOC_INO_LOOKUP_USER:
5022 return btrfs_ioctl_ino_lookup_user(file, argp);
5028 #ifdef CONFIG_COMPAT
5029 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5032 * These all access 32-bit values anyway so no further
5033 * handling is necessary.
5036 case FS_IOC32_GETFLAGS:
5037 cmd = FS_IOC_GETFLAGS;
5039 case FS_IOC32_SETFLAGS:
5040 cmd = FS_IOC_SETFLAGS;
5042 case FS_IOC32_GETVERSION:
5043 cmd = FS_IOC_GETVERSION;
5047 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));