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"
37 #include "inode-map.h"
39 #include "rcu-string.h"
41 #include "dev-replace.h"
46 #include "compression.h"
47 #include "space-info.h"
48 #include "delalloc-space.h"
49 #include "block-group.h"
52 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
53 * structures are incorrect, as the timespec structure from userspace
54 * is 4 bytes too small. We define these alternatives here to teach
55 * the kernel about the 32-bit struct packing.
57 struct btrfs_ioctl_timespec_32 {
60 } __attribute__ ((__packed__));
62 struct btrfs_ioctl_received_subvol_args_32 {
63 char uuid[BTRFS_UUID_SIZE]; /* in */
64 __u64 stransid; /* in */
65 __u64 rtransid; /* out */
66 struct btrfs_ioctl_timespec_32 stime; /* in */
67 struct btrfs_ioctl_timespec_32 rtime; /* out */
69 __u64 reserved[16]; /* in */
70 } __attribute__ ((__packed__));
72 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
73 struct btrfs_ioctl_received_subvol_args_32)
76 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
77 struct btrfs_ioctl_send_args_32 {
78 __s64 send_fd; /* in */
79 __u64 clone_sources_count; /* in */
80 compat_uptr_t clone_sources; /* in */
81 __u64 parent_root; /* in */
83 __u64 reserved[4]; /* in */
84 } __attribute__ ((__packed__));
86 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
87 struct btrfs_ioctl_send_args_32)
90 /* Mask out flags that are inappropriate for the given type of inode. */
91 static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
94 if (S_ISDIR(inode->i_mode))
96 else if (S_ISREG(inode->i_mode))
97 return flags & ~FS_DIRSYNC_FL;
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
106 static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
108 unsigned int iflags = 0;
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
125 if (flags & BTRFS_INODE_NOCOMPRESS)
126 iflags |= FS_NOCOMP_FL;
127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
134 * Update inode->i_flags based on the btrfs internal flags.
136 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
138 struct btrfs_inode *binode = BTRFS_I(inode);
139 unsigned int new_fl = 0;
141 if (binode->flags & BTRFS_INODE_SYNC)
143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
144 new_fl |= S_IMMUTABLE;
145 if (binode->flags & BTRFS_INODE_APPEND)
147 if (binode->flags & BTRFS_INODE_NOATIME)
149 if (binode->flags & BTRFS_INODE_DIRSYNC)
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
157 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
162 if (copy_to_user(arg, &flags, sizeof(flags)))
168 * Check if @flags are a supported and valid set of FS_*_FL flags and that
169 * the old and new flags are not conflicting
171 static int check_fsflags(unsigned int old_flags, unsigned int flags)
173 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
174 FS_NOATIME_FL | FS_NODUMP_FL | \
175 FS_SYNC_FL | FS_DIRSYNC_FL | \
176 FS_NOCOMP_FL | FS_COMPR_FL |
180 /* COMPR and NOCOMP on new/old are valid */
181 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
184 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
187 /* NOCOW and compression options are mutually exclusive */
188 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
190 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
196 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
198 struct inode *inode = file_inode(file);
199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
200 struct btrfs_inode *binode = BTRFS_I(inode);
201 struct btrfs_root *root = binode->root;
202 struct btrfs_trans_handle *trans;
203 unsigned int fsflags, old_fsflags;
205 const char *comp = NULL;
208 if (!inode_owner_or_capable(inode))
211 if (btrfs_root_readonly(root))
214 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
217 ret = mnt_want_write_file(file);
222 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
223 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
225 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
229 ret = check_fsflags(old_fsflags, fsflags);
233 binode_flags = binode->flags;
234 if (fsflags & FS_SYNC_FL)
235 binode_flags |= BTRFS_INODE_SYNC;
237 binode_flags &= ~BTRFS_INODE_SYNC;
238 if (fsflags & FS_IMMUTABLE_FL)
239 binode_flags |= BTRFS_INODE_IMMUTABLE;
241 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
242 if (fsflags & FS_APPEND_FL)
243 binode_flags |= BTRFS_INODE_APPEND;
245 binode_flags &= ~BTRFS_INODE_APPEND;
246 if (fsflags & FS_NODUMP_FL)
247 binode_flags |= BTRFS_INODE_NODUMP;
249 binode_flags &= ~BTRFS_INODE_NODUMP;
250 if (fsflags & FS_NOATIME_FL)
251 binode_flags |= BTRFS_INODE_NOATIME;
253 binode_flags &= ~BTRFS_INODE_NOATIME;
254 if (fsflags & FS_DIRSYNC_FL)
255 binode_flags |= BTRFS_INODE_DIRSYNC;
257 binode_flags &= ~BTRFS_INODE_DIRSYNC;
258 if (fsflags & FS_NOCOW_FL) {
259 if (S_ISREG(inode->i_mode)) {
261 * It's safe to turn csums off here, no extents exist.
262 * Otherwise we want the flag to reflect the real COW
263 * status of the file and will not set it.
265 if (inode->i_size == 0)
266 binode_flags |= BTRFS_INODE_NODATACOW |
267 BTRFS_INODE_NODATASUM;
269 binode_flags |= BTRFS_INODE_NODATACOW;
273 * Revert back under same assumptions as above
275 if (S_ISREG(inode->i_mode)) {
276 if (inode->i_size == 0)
277 binode_flags &= ~(BTRFS_INODE_NODATACOW |
278 BTRFS_INODE_NODATASUM);
280 binode_flags &= ~BTRFS_INODE_NODATACOW;
285 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
286 * flag may be changed automatically if compression code won't make
289 if (fsflags & FS_NOCOMP_FL) {
290 binode_flags &= ~BTRFS_INODE_COMPRESS;
291 binode_flags |= BTRFS_INODE_NOCOMPRESS;
292 } else if (fsflags & FS_COMPR_FL) {
294 if (IS_SWAPFILE(inode)) {
299 binode_flags |= BTRFS_INODE_COMPRESS;
300 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
302 comp = btrfs_compress_type2str(fs_info->compress_type);
303 if (!comp || comp[0] == 0)
304 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
306 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
313 trans = btrfs_start_transaction(root, 3);
315 ret = PTR_ERR(trans);
320 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
323 btrfs_abort_transaction(trans, ret);
327 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
329 if (ret && ret != -ENODATA) {
330 btrfs_abort_transaction(trans, ret);
335 binode->flags = binode_flags;
336 btrfs_sync_inode_flags_to_i_flags(inode);
337 inode_inc_iversion(inode);
338 inode->i_ctime = current_time(inode);
339 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
342 btrfs_end_transaction(trans);
345 mnt_drop_write_file(file);
350 * Translate btrfs internal inode flags to xflags as expected by the
351 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
354 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
356 unsigned int xflags = 0;
358 if (flags & BTRFS_INODE_APPEND)
359 xflags |= FS_XFLAG_APPEND;
360 if (flags & BTRFS_INODE_IMMUTABLE)
361 xflags |= FS_XFLAG_IMMUTABLE;
362 if (flags & BTRFS_INODE_NOATIME)
363 xflags |= FS_XFLAG_NOATIME;
364 if (flags & BTRFS_INODE_NODUMP)
365 xflags |= FS_XFLAG_NODUMP;
366 if (flags & BTRFS_INODE_SYNC)
367 xflags |= FS_XFLAG_SYNC;
372 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
373 static int check_xflags(unsigned int flags)
375 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
376 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
381 bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
382 enum btrfs_exclusive_operation type)
384 return !cmpxchg(&fs_info->exclusive_operation, BTRFS_EXCLOP_NONE, type);
387 void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
389 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
390 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
394 * Set the xflags from the internal inode flags. The remaining items of fsxattr
397 static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
399 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
402 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
403 if (copy_to_user(arg, &fa, sizeof(fa)))
409 static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
411 struct inode *inode = file_inode(file);
412 struct btrfs_inode *binode = BTRFS_I(inode);
413 struct btrfs_root *root = binode->root;
414 struct btrfs_trans_handle *trans;
415 struct fsxattr fa, old_fa;
417 unsigned old_i_flags;
420 if (!inode_owner_or_capable(inode))
423 if (btrfs_root_readonly(root))
426 if (copy_from_user(&fa, arg, sizeof(fa)))
429 ret = check_xflags(fa.fsx_xflags);
433 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
436 ret = mnt_want_write_file(file);
442 old_flags = binode->flags;
443 old_i_flags = inode->i_flags;
445 simple_fill_fsxattr(&old_fa,
446 btrfs_inode_flags_to_xflags(binode->flags));
447 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
451 if (fa.fsx_xflags & FS_XFLAG_SYNC)
452 binode->flags |= BTRFS_INODE_SYNC;
454 binode->flags &= ~BTRFS_INODE_SYNC;
455 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
456 binode->flags |= BTRFS_INODE_IMMUTABLE;
458 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
459 if (fa.fsx_xflags & FS_XFLAG_APPEND)
460 binode->flags |= BTRFS_INODE_APPEND;
462 binode->flags &= ~BTRFS_INODE_APPEND;
463 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
464 binode->flags |= BTRFS_INODE_NODUMP;
466 binode->flags &= ~BTRFS_INODE_NODUMP;
467 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
468 binode->flags |= BTRFS_INODE_NOATIME;
470 binode->flags &= ~BTRFS_INODE_NOATIME;
472 /* 1 item for the inode */
473 trans = btrfs_start_transaction(root, 1);
475 ret = PTR_ERR(trans);
479 btrfs_sync_inode_flags_to_i_flags(inode);
480 inode_inc_iversion(inode);
481 inode->i_ctime = current_time(inode);
482 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
484 btrfs_end_transaction(trans);
488 binode->flags = old_flags;
489 inode->i_flags = old_i_flags;
493 mnt_drop_write_file(file);
498 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
500 struct inode *inode = file_inode(file);
502 return put_user(inode->i_generation, arg);
505 static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
508 struct btrfs_device *device;
509 struct request_queue *q;
510 struct fstrim_range range;
511 u64 minlen = ULLONG_MAX;
515 if (!capable(CAP_SYS_ADMIN))
519 * If the fs is mounted with nologreplay, which requires it to be
520 * mounted in RO mode as well, we can not allow discard on free space
521 * inside block groups, because log trees refer to extents that are not
522 * pinned in a block group's free space cache (pinning the extents is
523 * precisely the first phase of replaying a log tree).
525 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
529 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
533 q = bdev_get_queue(device->bdev);
534 if (blk_queue_discard(q)) {
536 minlen = min_t(u64, q->limits.discard_granularity,
544 if (copy_from_user(&range, arg, sizeof(range)))
548 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
549 * block group is in the logical address space, which can be any
550 * sectorsize aligned bytenr in the range [0, U64_MAX].
552 if (range.len < fs_info->sb->s_blocksize)
555 range.minlen = max(range.minlen, minlen);
556 ret = btrfs_trim_fs(fs_info, &range);
560 if (copy_to_user(arg, &range, sizeof(range)))
566 int __pure btrfs_is_empty_uuid(u8 *uuid)
570 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
577 static noinline int create_subvol(struct inode *dir,
578 struct dentry *dentry,
579 const char *name, int namelen,
580 struct btrfs_qgroup_inherit *inherit)
582 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
583 struct btrfs_trans_handle *trans;
584 struct btrfs_key key;
585 struct btrfs_root_item *root_item;
586 struct btrfs_inode_item *inode_item;
587 struct extent_buffer *leaf;
588 struct btrfs_root *root = BTRFS_I(dir)->root;
589 struct btrfs_root *new_root;
590 struct btrfs_block_rsv block_rsv;
591 struct timespec64 cur_time = current_time(dir);
597 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
600 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
604 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
608 ret = get_anon_bdev(&anon_dev);
613 * Don't create subvolume whose level is not zero. Or qgroup will be
614 * screwed up since it assumes subvolume qgroup's level to be 0.
616 if (btrfs_qgroup_level(objectid)) {
621 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
623 * The same as the snapshot creation, please see the comment
624 * of create_snapshot().
626 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
630 trans = btrfs_start_transaction(root, 0);
632 ret = PTR_ERR(trans);
633 btrfs_subvolume_release_metadata(root, &block_rsv);
636 trans->block_rsv = &block_rsv;
637 trans->bytes_reserved = block_rsv.size;
639 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
643 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
644 BTRFS_NESTING_NORMAL);
650 btrfs_mark_buffer_dirty(leaf);
652 inode_item = &root_item->inode;
653 btrfs_set_stack_inode_generation(inode_item, 1);
654 btrfs_set_stack_inode_size(inode_item, 3);
655 btrfs_set_stack_inode_nlink(inode_item, 1);
656 btrfs_set_stack_inode_nbytes(inode_item,
658 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
660 btrfs_set_root_flags(root_item, 0);
661 btrfs_set_root_limit(root_item, 0);
662 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
664 btrfs_set_root_bytenr(root_item, leaf->start);
665 btrfs_set_root_generation(root_item, trans->transid);
666 btrfs_set_root_level(root_item, 0);
667 btrfs_set_root_refs(root_item, 1);
668 btrfs_set_root_used(root_item, leaf->len);
669 btrfs_set_root_last_snapshot(root_item, 0);
671 btrfs_set_root_generation_v2(root_item,
672 btrfs_root_generation(root_item));
673 generate_random_guid(root_item->uuid);
674 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
675 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
676 root_item->ctime = root_item->otime;
677 btrfs_set_root_ctransid(root_item, trans->transid);
678 btrfs_set_root_otransid(root_item, trans->transid);
680 btrfs_tree_unlock(leaf);
681 free_extent_buffer(leaf);
684 btrfs_set_root_dirid(root_item, new_dirid);
686 key.objectid = objectid;
688 key.type = BTRFS_ROOT_ITEM_KEY;
689 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
694 key.offset = (u64)-1;
695 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
696 if (IS_ERR(new_root)) {
697 free_anon_bdev(anon_dev);
698 ret = PTR_ERR(new_root);
699 btrfs_abort_transaction(trans, ret);
702 /* Freeing will be done in btrfs_put_root() of new_root */
705 btrfs_record_root_in_trans(trans, new_root);
707 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
708 btrfs_put_root(new_root);
710 /* We potentially lose an unused inode item here */
711 btrfs_abort_transaction(trans, ret);
715 mutex_lock(&new_root->objectid_mutex);
716 new_root->highest_objectid = new_dirid;
717 mutex_unlock(&new_root->objectid_mutex);
720 * insert the directory item
722 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
724 btrfs_abort_transaction(trans, ret);
728 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
729 BTRFS_FT_DIR, index);
731 btrfs_abort_transaction(trans, ret);
735 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
736 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
738 btrfs_abort_transaction(trans, ret);
742 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
743 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
745 btrfs_abort_transaction(trans, ret);
749 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
750 BTRFS_UUID_KEY_SUBVOL, objectid);
752 btrfs_abort_transaction(trans, ret);
756 trans->block_rsv = NULL;
757 trans->bytes_reserved = 0;
758 btrfs_subvolume_release_metadata(root, &block_rsv);
760 err = btrfs_commit_transaction(trans);
765 inode = btrfs_lookup_dentry(dir, dentry);
767 return PTR_ERR(inode);
768 d_instantiate(dentry, inode);
774 free_anon_bdev(anon_dev);
779 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
780 struct dentry *dentry, bool readonly,
781 struct btrfs_qgroup_inherit *inherit)
783 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
785 struct btrfs_pending_snapshot *pending_snapshot;
786 struct btrfs_trans_handle *trans;
789 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
792 if (atomic_read(&root->nr_swapfiles)) {
794 "cannot snapshot subvolume with active swapfile");
798 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
799 if (!pending_snapshot)
802 ret = get_anon_bdev(&pending_snapshot->anon_dev);
805 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
807 pending_snapshot->path = btrfs_alloc_path();
808 if (!pending_snapshot->root_item || !pending_snapshot->path) {
813 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
814 BTRFS_BLOCK_RSV_TEMP);
816 * 1 - parent dir inode
819 * 2 - root ref/backref
820 * 1 - root of snapshot
823 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
824 &pending_snapshot->block_rsv, 8,
829 pending_snapshot->dentry = dentry;
830 pending_snapshot->root = root;
831 pending_snapshot->readonly = readonly;
832 pending_snapshot->dir = dir;
833 pending_snapshot->inherit = inherit;
835 trans = btrfs_start_transaction(root, 0);
837 ret = PTR_ERR(trans);
841 spin_lock(&fs_info->trans_lock);
842 list_add(&pending_snapshot->list,
843 &trans->transaction->pending_snapshots);
844 spin_unlock(&fs_info->trans_lock);
846 ret = btrfs_commit_transaction(trans);
850 ret = pending_snapshot->error;
854 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
858 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
860 ret = PTR_ERR(inode);
864 d_instantiate(dentry, inode);
866 pending_snapshot->anon_dev = 0;
868 /* Prevent double freeing of anon_dev */
869 if (ret && pending_snapshot->snap)
870 pending_snapshot->snap->anon_dev = 0;
871 btrfs_put_root(pending_snapshot->snap);
872 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
874 if (pending_snapshot->anon_dev)
875 free_anon_bdev(pending_snapshot->anon_dev);
876 kfree(pending_snapshot->root_item);
877 btrfs_free_path(pending_snapshot->path);
878 kfree(pending_snapshot);
883 /* copy of may_delete in fs/namei.c()
884 * Check whether we can remove a link victim from directory dir, check
885 * whether the type of victim is right.
886 * 1. We can't do it if dir is read-only (done in permission())
887 * 2. We should have write and exec permissions on dir
888 * 3. We can't remove anything from append-only dir
889 * 4. We can't do anything with immutable dir (done in permission())
890 * 5. If the sticky bit on dir is set we should either
891 * a. be owner of dir, or
892 * b. be owner of victim, or
893 * c. have CAP_FOWNER capability
894 * 6. If the victim is append-only or immutable we can't do anything with
895 * links pointing to it.
896 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
897 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
898 * 9. We can't remove a root or mountpoint.
899 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
900 * nfs_async_unlink().
903 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
907 if (d_really_is_negative(victim))
910 BUG_ON(d_inode(victim->d_parent) != dir);
911 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
913 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
918 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
919 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
922 if (!d_is_dir(victim))
926 } else if (d_is_dir(victim))
930 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
935 /* copy of may_create in fs/namei.c() */
936 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
938 if (d_really_is_positive(child))
942 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
946 * Create a new subvolume below @parent. This is largely modeled after
947 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
948 * inside this filesystem so it's quite a bit simpler.
950 static noinline int btrfs_mksubvol(const struct path *parent,
951 const char *name, int namelen,
952 struct btrfs_root *snap_src,
954 struct btrfs_qgroup_inherit *inherit)
956 struct inode *dir = d_inode(parent->dentry);
957 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
958 struct dentry *dentry;
961 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
965 dentry = lookup_one_len(name, parent->dentry, namelen);
966 error = PTR_ERR(dentry);
970 error = btrfs_may_create(dir, dentry);
975 * even if this name doesn't exist, we may get hash collisions.
976 * check for them now when we can safely fail
978 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
984 down_read(&fs_info->subvol_sem);
986 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
990 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
992 error = create_subvol(dir, dentry, name, namelen, inherit);
995 fsnotify_mkdir(dir, dentry);
997 up_read(&fs_info->subvol_sem);
1005 static noinline int btrfs_mksnapshot(const struct path *parent,
1006 const char *name, int namelen,
1007 struct btrfs_root *root,
1009 struct btrfs_qgroup_inherit *inherit)
1012 bool snapshot_force_cow = false;
1015 * Force new buffered writes to reserve space even when NOCOW is
1016 * possible. This is to avoid later writeback (running dealloc) to
1017 * fallback to COW mode and unexpectedly fail with ENOSPC.
1019 btrfs_drew_read_lock(&root->snapshot_lock);
1021 ret = btrfs_start_delalloc_snapshot(root);
1026 * All previous writes have started writeback in NOCOW mode, so now
1027 * we force future writes to fallback to COW mode during snapshot
1030 atomic_inc(&root->snapshot_force_cow);
1031 snapshot_force_cow = true;
1033 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
1035 ret = btrfs_mksubvol(parent, name, namelen,
1036 root, readonly, inherit);
1038 if (snapshot_force_cow)
1039 atomic_dec(&root->snapshot_force_cow);
1040 btrfs_drew_read_unlock(&root->snapshot_lock);
1045 * When we're defragging a range, we don't want to kick it off again
1046 * if it is really just waiting for delalloc to send it down.
1047 * If we find a nice big extent or delalloc range for the bytes in the
1048 * file you want to defrag, we return 0 to let you know to skip this
1051 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1053 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1054 struct extent_map *em = NULL;
1055 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1058 read_lock(&em_tree->lock);
1059 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1060 read_unlock(&em_tree->lock);
1063 end = extent_map_end(em);
1064 free_extent_map(em);
1065 if (end - offset > thresh)
1068 /* if we already have a nice delalloc here, just stop */
1070 end = count_range_bits(io_tree, &offset, offset + thresh,
1071 thresh, EXTENT_DELALLOC, 1);
1078 * helper function to walk through a file and find extents
1079 * newer than a specific transid, and smaller than thresh.
1081 * This is used by the defragging code to find new and small
1084 static int find_new_extents(struct btrfs_root *root,
1085 struct inode *inode, u64 newer_than,
1086 u64 *off, u32 thresh)
1088 struct btrfs_path *path;
1089 struct btrfs_key min_key;
1090 struct extent_buffer *leaf;
1091 struct btrfs_file_extent_item *extent;
1094 u64 ino = btrfs_ino(BTRFS_I(inode));
1096 path = btrfs_alloc_path();
1100 min_key.objectid = ino;
1101 min_key.type = BTRFS_EXTENT_DATA_KEY;
1102 min_key.offset = *off;
1105 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1109 if (min_key.objectid != ino)
1111 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1114 leaf = path->nodes[0];
1115 extent = btrfs_item_ptr(leaf, path->slots[0],
1116 struct btrfs_file_extent_item);
1118 type = btrfs_file_extent_type(leaf, extent);
1119 if (type == BTRFS_FILE_EXTENT_REG &&
1120 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1121 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1122 *off = min_key.offset;
1123 btrfs_free_path(path);
1128 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1129 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1133 if (min_key.offset == (u64)-1)
1137 btrfs_release_path(path);
1140 btrfs_free_path(path);
1144 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1146 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1147 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1148 struct extent_map *em;
1149 u64 len = PAGE_SIZE;
1152 * hopefully we have this extent in the tree already, try without
1153 * the full extent lock
1155 read_lock(&em_tree->lock);
1156 em = lookup_extent_mapping(em_tree, start, len);
1157 read_unlock(&em_tree->lock);
1160 struct extent_state *cached = NULL;
1161 u64 end = start + len - 1;
1163 /* get the big lock and read metadata off disk */
1164 lock_extent_bits(io_tree, start, end, &cached);
1165 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
1166 unlock_extent_cached(io_tree, start, end, &cached);
1175 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1177 struct extent_map *next;
1180 /* this is the last extent */
1181 if (em->start + em->len >= i_size_read(inode))
1184 next = defrag_lookup_extent(inode, em->start + em->len);
1185 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1187 else if ((em->block_start + em->block_len == next->block_start) &&
1188 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1191 free_extent_map(next);
1195 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1196 u64 *last_len, u64 *skip, u64 *defrag_end,
1199 struct extent_map *em;
1201 bool next_mergeable = true;
1202 bool prev_mergeable = true;
1205 * make sure that once we start defragging an extent, we keep on
1208 if (start < *defrag_end)
1213 em = defrag_lookup_extent(inode, start);
1217 /* this will cover holes, and inline extents */
1218 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1224 prev_mergeable = false;
1226 next_mergeable = defrag_check_next_extent(inode, em);
1228 * we hit a real extent, if it is big or the next extent is not a
1229 * real extent, don't bother defragging it
1231 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1232 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1236 * last_len ends up being a counter of how many bytes we've defragged.
1237 * every time we choose not to defrag an extent, we reset *last_len
1238 * so that the next tiny extent will force a defrag.
1240 * The end result of this is that tiny extents before a single big
1241 * extent will force at least part of that big extent to be defragged.
1244 *defrag_end = extent_map_end(em);
1247 *skip = extent_map_end(em);
1251 free_extent_map(em);
1256 * it doesn't do much good to defrag one or two pages
1257 * at a time. This pulls in a nice chunk of pages
1258 * to COW and defrag.
1260 * It also makes sure the delalloc code has enough
1261 * dirty data to avoid making new small extents as part
1264 * It's a good idea to start RA on this range
1265 * before calling this.
1267 static int cluster_pages_for_defrag(struct inode *inode,
1268 struct page **pages,
1269 unsigned long start_index,
1270 unsigned long num_pages)
1272 unsigned long file_end;
1273 u64 isize = i_size_read(inode);
1277 u64 start = (u64)start_index << PAGE_SHIFT;
1282 struct btrfs_ordered_extent *ordered;
1283 struct extent_state *cached_state = NULL;
1284 struct extent_io_tree *tree;
1285 struct extent_changeset *data_reserved = NULL;
1286 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1288 file_end = (isize - 1) >> PAGE_SHIFT;
1289 if (!isize || start_index > file_end)
1292 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1294 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
1295 start, page_cnt << PAGE_SHIFT);
1299 tree = &BTRFS_I(inode)->io_tree;
1301 /* step one, lock all the pages */
1302 for (i = 0; i < page_cnt; i++) {
1305 page = find_or_create_page(inode->i_mapping,
1306 start_index + i, mask);
1310 page_start = page_offset(page);
1311 page_end = page_start + PAGE_SIZE - 1;
1313 lock_extent_bits(tree, page_start, page_end,
1315 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode),
1317 unlock_extent_cached(tree, page_start, page_end,
1323 btrfs_start_ordered_extent(ordered, 1);
1324 btrfs_put_ordered_extent(ordered);
1327 * we unlocked the page above, so we need check if
1328 * it was released or not.
1330 if (page->mapping != inode->i_mapping) {
1337 if (!PageUptodate(page)) {
1338 btrfs_readpage(NULL, page);
1340 if (!PageUptodate(page)) {
1348 if (page->mapping != inode->i_mapping) {
1360 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1364 * so now we have a nice long stream of locked
1365 * and up to date pages, lets wait on them
1367 for (i = 0; i < i_done; i++)
1368 wait_on_page_writeback(pages[i]);
1370 page_start = page_offset(pages[0]);
1371 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1373 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1374 page_start, page_end - 1, &cached_state);
1377 * When defragmenting we skip ranges that have holes or inline extents,
1378 * (check should_defrag_range()), to avoid unnecessary IO and wasting
1379 * space. At btrfs_defrag_file(), we check if a range should be defragged
1380 * before locking the inode and then, if it should, we trigger a sync
1381 * page cache readahead - we lock the inode only after that to avoid
1382 * blocking for too long other tasks that possibly want to operate on
1383 * other file ranges. But before we were able to get the inode lock,
1384 * some other task may have punched a hole in the range, or we may have
1385 * now an inline extent, in which case we should not defrag. So check
1386 * for that here, where we have the inode and the range locked, and bail
1387 * out if that happened.
1389 search_start = page_start;
1390 while (search_start < page_end) {
1391 struct extent_map *em;
1393 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, search_start,
1394 page_end - search_start);
1397 goto out_unlock_range;
1399 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1400 free_extent_map(em);
1401 /* Ok, 0 means we did not defrag anything */
1403 goto out_unlock_range;
1405 search_start = extent_map_end(em);
1406 free_extent_map(em);
1409 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1410 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1411 EXTENT_DEFRAG, 0, 0, &cached_state);
1413 if (i_done != page_cnt) {
1414 spin_lock(&BTRFS_I(inode)->lock);
1415 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1416 spin_unlock(&BTRFS_I(inode)->lock);
1417 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1418 start, (page_cnt - i_done) << PAGE_SHIFT, true);
1422 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1425 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1426 page_start, page_end - 1, &cached_state);
1428 for (i = 0; i < i_done; i++) {
1429 clear_page_dirty_for_io(pages[i]);
1430 ClearPageChecked(pages[i]);
1431 set_page_extent_mapped(pages[i]);
1432 set_page_dirty(pages[i]);
1433 unlock_page(pages[i]);
1436 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1437 extent_changeset_free(data_reserved);
1441 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1442 page_start, page_end - 1, &cached_state);
1444 for (i = 0; i < i_done; i++) {
1445 unlock_page(pages[i]);
1448 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1449 start, page_cnt << PAGE_SHIFT, true);
1450 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1451 extent_changeset_free(data_reserved);
1456 int btrfs_defrag_file(struct inode *inode, struct file *file,
1457 struct btrfs_ioctl_defrag_range_args *range,
1458 u64 newer_than, unsigned long max_to_defrag)
1460 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1461 struct btrfs_root *root = BTRFS_I(inode)->root;
1462 struct file_ra_state *ra = NULL;
1463 unsigned long last_index;
1464 u64 isize = i_size_read(inode);
1468 u64 newer_off = range->start;
1470 unsigned long ra_index = 0;
1472 int defrag_count = 0;
1473 int compress_type = BTRFS_COMPRESS_ZLIB;
1474 u32 extent_thresh = range->extent_thresh;
1475 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1476 unsigned long cluster = max_cluster;
1477 u64 new_align = ~((u64)SZ_128K - 1);
1478 struct page **pages = NULL;
1479 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1484 if (range->start >= isize)
1488 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
1490 if (range->compress_type)
1491 compress_type = range->compress_type;
1494 if (extent_thresh == 0)
1495 extent_thresh = SZ_256K;
1498 * If we were not given a file, allocate a readahead context. As
1499 * readahead is just an optimization, defrag will work without it so
1500 * we don't error out.
1503 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1505 file_ra_state_init(ra, inode->i_mapping);
1510 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1516 /* find the last page to defrag */
1517 if (range->start + range->len > range->start) {
1518 last_index = min_t(u64, isize - 1,
1519 range->start + range->len - 1) >> PAGE_SHIFT;
1521 last_index = (isize - 1) >> PAGE_SHIFT;
1525 ret = find_new_extents(root, inode, newer_than,
1526 &newer_off, SZ_64K);
1528 range->start = newer_off;
1530 * we always align our defrag to help keep
1531 * the extents in the file evenly spaced
1533 i = (newer_off & new_align) >> PAGE_SHIFT;
1537 i = range->start >> PAGE_SHIFT;
1540 max_to_defrag = last_index - i + 1;
1543 * make writeback starts from i, so the defrag range can be
1544 * written sequentially.
1546 if (i < inode->i_mapping->writeback_index)
1547 inode->i_mapping->writeback_index = i;
1549 while (i <= last_index && defrag_count < max_to_defrag &&
1550 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1552 * make sure we stop running if someone unmounts
1555 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1558 if (btrfs_defrag_cancelled(fs_info)) {
1559 btrfs_debug(fs_info, "defrag_file cancelled");
1564 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1565 extent_thresh, &last_len, &skip,
1566 &defrag_end, do_compress)){
1569 * the should_defrag function tells us how much to skip
1570 * bump our counter by the suggested amount
1572 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1573 i = max(i + 1, next);
1578 cluster = (PAGE_ALIGN(defrag_end) >>
1580 cluster = min(cluster, max_cluster);
1582 cluster = max_cluster;
1585 if (i + cluster > ra_index) {
1586 ra_index = max(i, ra_index);
1588 page_cache_sync_readahead(inode->i_mapping, ra,
1589 file, ra_index, cluster);
1590 ra_index += cluster;
1594 if (IS_SWAPFILE(inode)) {
1598 BTRFS_I(inode)->defrag_compress = compress_type;
1599 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1602 inode_unlock(inode);
1606 defrag_count += ret;
1607 balance_dirty_pages_ratelimited(inode->i_mapping);
1608 inode_unlock(inode);
1611 if (newer_off == (u64)-1)
1617 newer_off = max(newer_off + 1,
1618 (u64)i << PAGE_SHIFT);
1620 ret = find_new_extents(root, inode, newer_than,
1621 &newer_off, SZ_64K);
1623 range->start = newer_off;
1624 i = (newer_off & new_align) >> PAGE_SHIFT;
1631 last_len += ret << PAGE_SHIFT;
1639 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1640 filemap_flush(inode->i_mapping);
1641 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1642 &BTRFS_I(inode)->runtime_flags))
1643 filemap_flush(inode->i_mapping);
1646 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1647 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1648 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1649 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1657 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1658 inode_unlock(inode);
1666 static noinline int btrfs_ioctl_resize(struct file *file,
1669 struct inode *inode = file_inode(file);
1670 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1674 struct btrfs_root *root = BTRFS_I(inode)->root;
1675 struct btrfs_ioctl_vol_args *vol_args;
1676 struct btrfs_trans_handle *trans;
1677 struct btrfs_device *device = NULL;
1680 char *devstr = NULL;
1684 if (!capable(CAP_SYS_ADMIN))
1687 ret = mnt_want_write_file(file);
1691 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_RESIZE)) {
1692 mnt_drop_write_file(file);
1693 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1696 vol_args = memdup_user(arg, sizeof(*vol_args));
1697 if (IS_ERR(vol_args)) {
1698 ret = PTR_ERR(vol_args);
1702 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1704 sizestr = vol_args->name;
1705 devstr = strchr(sizestr, ':');
1707 sizestr = devstr + 1;
1709 devstr = vol_args->name;
1710 ret = kstrtoull(devstr, 10, &devid);
1717 btrfs_info(fs_info, "resizing devid %llu", devid);
1720 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
1722 btrfs_info(fs_info, "resizer unable to find device %llu",
1728 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1730 "resizer unable to apply on readonly device %llu",
1736 if (!strcmp(sizestr, "max"))
1737 new_size = device->bdev->bd_inode->i_size;
1739 if (sizestr[0] == '-') {
1742 } else if (sizestr[0] == '+') {
1746 new_size = memparse(sizestr, &retptr);
1747 if (*retptr != '\0' || new_size == 0) {
1753 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1758 old_size = btrfs_device_get_total_bytes(device);
1761 if (new_size > old_size) {
1765 new_size = old_size - new_size;
1766 } else if (mod > 0) {
1767 if (new_size > ULLONG_MAX - old_size) {
1771 new_size = old_size + new_size;
1774 if (new_size < SZ_256M) {
1778 if (new_size > device->bdev->bd_inode->i_size) {
1783 new_size = round_down(new_size, fs_info->sectorsize);
1785 if (new_size > old_size) {
1786 trans = btrfs_start_transaction(root, 0);
1787 if (IS_ERR(trans)) {
1788 ret = PTR_ERR(trans);
1791 ret = btrfs_grow_device(trans, device, new_size);
1792 btrfs_commit_transaction(trans);
1793 } else if (new_size < old_size) {
1794 ret = btrfs_shrink_device(device, new_size);
1795 } /* equal, nothing need to do */
1797 if (ret == 0 && new_size != old_size)
1798 btrfs_info_in_rcu(fs_info,
1799 "resize device %s (devid %llu) from %llu to %llu",
1800 rcu_str_deref(device->name), device->devid,
1801 old_size, new_size);
1805 btrfs_exclop_finish(fs_info);
1806 mnt_drop_write_file(file);
1810 static noinline int __btrfs_ioctl_snap_create(struct file *file,
1811 const char *name, unsigned long fd, int subvol,
1813 struct btrfs_qgroup_inherit *inherit)
1818 if (!S_ISDIR(file_inode(file)->i_mode))
1821 ret = mnt_want_write_file(file);
1825 namelen = strlen(name);
1826 if (strchr(name, '/')) {
1828 goto out_drop_write;
1831 if (name[0] == '.' &&
1832 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1834 goto out_drop_write;
1838 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1839 NULL, readonly, inherit);
1841 struct fd src = fdget(fd);
1842 struct inode *src_inode;
1845 goto out_drop_write;
1848 src_inode = file_inode(src.file);
1849 if (src_inode->i_sb != file_inode(file)->i_sb) {
1850 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1851 "Snapshot src from another FS");
1853 } else if (!inode_owner_or_capable(src_inode)) {
1855 * Subvolume creation is not restricted, but snapshots
1856 * are limited to own subvolumes only
1860 ret = btrfs_mksnapshot(&file->f_path, name, namelen,
1861 BTRFS_I(src_inode)->root,
1867 mnt_drop_write_file(file);
1872 static noinline int btrfs_ioctl_snap_create(struct file *file,
1873 void __user *arg, int subvol)
1875 struct btrfs_ioctl_vol_args *vol_args;
1878 if (!S_ISDIR(file_inode(file)->i_mode))
1881 vol_args = memdup_user(arg, sizeof(*vol_args));
1882 if (IS_ERR(vol_args))
1883 return PTR_ERR(vol_args);
1884 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1886 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1887 subvol, false, NULL);
1893 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1894 void __user *arg, int subvol)
1896 struct btrfs_ioctl_vol_args_v2 *vol_args;
1898 bool readonly = false;
1899 struct btrfs_qgroup_inherit *inherit = NULL;
1901 if (!S_ISDIR(file_inode(file)->i_mode))
1904 vol_args = memdup_user(arg, sizeof(*vol_args));
1905 if (IS_ERR(vol_args))
1906 return PTR_ERR(vol_args);
1907 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1909 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
1914 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1916 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1917 if (vol_args->size > PAGE_SIZE) {
1921 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1922 if (IS_ERR(inherit)) {
1923 ret = PTR_ERR(inherit);
1928 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1929 subvol, readonly, inherit);
1939 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1942 struct inode *inode = file_inode(file);
1943 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1944 struct btrfs_root *root = BTRFS_I(inode)->root;
1948 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1951 down_read(&fs_info->subvol_sem);
1952 if (btrfs_root_readonly(root))
1953 flags |= BTRFS_SUBVOL_RDONLY;
1954 up_read(&fs_info->subvol_sem);
1956 if (copy_to_user(arg, &flags, sizeof(flags)))
1962 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1965 struct inode *inode = file_inode(file);
1966 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1967 struct btrfs_root *root = BTRFS_I(inode)->root;
1968 struct btrfs_trans_handle *trans;
1973 if (!inode_owner_or_capable(inode))
1976 ret = mnt_want_write_file(file);
1980 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1982 goto out_drop_write;
1985 if (copy_from_user(&flags, arg, sizeof(flags))) {
1987 goto out_drop_write;
1990 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1992 goto out_drop_write;
1995 down_write(&fs_info->subvol_sem);
1998 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
2001 root_flags = btrfs_root_flags(&root->root_item);
2002 if (flags & BTRFS_SUBVOL_RDONLY) {
2003 btrfs_set_root_flags(&root->root_item,
2004 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
2007 * Block RO -> RW transition if this subvolume is involved in
2010 spin_lock(&root->root_item_lock);
2011 if (root->send_in_progress == 0) {
2012 btrfs_set_root_flags(&root->root_item,
2013 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
2014 spin_unlock(&root->root_item_lock);
2016 spin_unlock(&root->root_item_lock);
2018 "Attempt to set subvolume %llu read-write during send",
2019 root->root_key.objectid);
2025 trans = btrfs_start_transaction(root, 1);
2026 if (IS_ERR(trans)) {
2027 ret = PTR_ERR(trans);
2031 ret = btrfs_update_root(trans, fs_info->tree_root,
2032 &root->root_key, &root->root_item);
2034 btrfs_end_transaction(trans);
2038 ret = btrfs_commit_transaction(trans);
2042 btrfs_set_root_flags(&root->root_item, root_flags);
2044 up_write(&fs_info->subvol_sem);
2046 mnt_drop_write_file(file);
2051 static noinline int key_in_sk(struct btrfs_key *key,
2052 struct btrfs_ioctl_search_key *sk)
2054 struct btrfs_key test;
2057 test.objectid = sk->min_objectid;
2058 test.type = sk->min_type;
2059 test.offset = sk->min_offset;
2061 ret = btrfs_comp_cpu_keys(key, &test);
2065 test.objectid = sk->max_objectid;
2066 test.type = sk->max_type;
2067 test.offset = sk->max_offset;
2069 ret = btrfs_comp_cpu_keys(key, &test);
2075 static noinline int copy_to_sk(struct btrfs_path *path,
2076 struct btrfs_key *key,
2077 struct btrfs_ioctl_search_key *sk,
2080 unsigned long *sk_offset,
2084 struct extent_buffer *leaf;
2085 struct btrfs_ioctl_search_header sh;
2086 struct btrfs_key test;
2087 unsigned long item_off;
2088 unsigned long item_len;
2094 leaf = path->nodes[0];
2095 slot = path->slots[0];
2096 nritems = btrfs_header_nritems(leaf);
2098 if (btrfs_header_generation(leaf) > sk->max_transid) {
2102 found_transid = btrfs_header_generation(leaf);
2104 for (i = slot; i < nritems; i++) {
2105 item_off = btrfs_item_ptr_offset(leaf, i);
2106 item_len = btrfs_item_size_nr(leaf, i);
2108 btrfs_item_key_to_cpu(leaf, key, i);
2109 if (!key_in_sk(key, sk))
2112 if (sizeof(sh) + item_len > *buf_size) {
2119 * return one empty item back for v1, which does not
2123 *buf_size = sizeof(sh) + item_len;
2128 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2133 sh.objectid = key->objectid;
2134 sh.offset = key->offset;
2135 sh.type = key->type;
2137 sh.transid = found_transid;
2140 * Copy search result header. If we fault then loop again so we
2141 * can fault in the pages and -EFAULT there if there's a
2142 * problem. Otherwise we'll fault and then copy the buffer in
2143 * properly this next time through
2145 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2150 *sk_offset += sizeof(sh);
2153 char __user *up = ubuf + *sk_offset;
2155 * Copy the item, same behavior as above, but reset the
2156 * * sk_offset so we copy the full thing again.
2158 if (read_extent_buffer_to_user_nofault(leaf, up,
2159 item_off, item_len)) {
2161 *sk_offset -= sizeof(sh);
2165 *sk_offset += item_len;
2169 if (ret) /* -EOVERFLOW from above */
2172 if (*num_found >= sk->nr_items) {
2179 test.objectid = sk->max_objectid;
2180 test.type = sk->max_type;
2181 test.offset = sk->max_offset;
2182 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2184 else if (key->offset < (u64)-1)
2186 else if (key->type < (u8)-1) {
2189 } else if (key->objectid < (u64)-1) {
2197 * 0: all items from this leaf copied, continue with next
2198 * 1: * more items can be copied, but unused buffer is too small
2199 * * all items were found
2200 * Either way, it will stops the loop which iterates to the next
2202 * -EOVERFLOW: item was to large for buffer
2203 * -EFAULT: could not copy extent buffer back to userspace
2208 static noinline int search_ioctl(struct inode *inode,
2209 struct btrfs_ioctl_search_key *sk,
2213 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2214 struct btrfs_root *root;
2215 struct btrfs_key key;
2216 struct btrfs_path *path;
2219 unsigned long sk_offset = 0;
2221 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2222 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2226 path = btrfs_alloc_path();
2230 if (sk->tree_id == 0) {
2231 /* search the root of the inode that was passed */
2232 root = btrfs_grab_root(BTRFS_I(inode)->root);
2234 root = btrfs_get_fs_root(info, sk->tree_id, true);
2236 btrfs_free_path(path);
2237 return PTR_ERR(root);
2241 key.objectid = sk->min_objectid;
2242 key.type = sk->min_type;
2243 key.offset = sk->min_offset;
2246 ret = fault_in_pages_writeable(ubuf + sk_offset,
2247 *buf_size - sk_offset);
2251 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2257 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2258 &sk_offset, &num_found);
2259 btrfs_release_path(path);
2267 sk->nr_items = num_found;
2268 btrfs_put_root(root);
2269 btrfs_free_path(path);
2273 static noinline int btrfs_ioctl_tree_search(struct file *file,
2276 struct btrfs_ioctl_search_args __user *uargs;
2277 struct btrfs_ioctl_search_key sk;
2278 struct inode *inode;
2282 if (!capable(CAP_SYS_ADMIN))
2285 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2287 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2290 buf_size = sizeof(uargs->buf);
2292 inode = file_inode(file);
2293 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2296 * In the origin implementation an overflow is handled by returning a
2297 * search header with a len of zero, so reset ret.
2299 if (ret == -EOVERFLOW)
2302 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2307 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2310 struct btrfs_ioctl_search_args_v2 __user *uarg;
2311 struct btrfs_ioctl_search_args_v2 args;
2312 struct inode *inode;
2315 const size_t buf_limit = SZ_16M;
2317 if (!capable(CAP_SYS_ADMIN))
2320 /* copy search header and buffer size */
2321 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2322 if (copy_from_user(&args, uarg, sizeof(args)))
2325 buf_size = args.buf_size;
2327 /* limit result size to 16MB */
2328 if (buf_size > buf_limit)
2329 buf_size = buf_limit;
2331 inode = file_inode(file);
2332 ret = search_ioctl(inode, &args.key, &buf_size,
2333 (char __user *)(&uarg->buf[0]));
2334 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2336 else if (ret == -EOVERFLOW &&
2337 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2344 * Search INODE_REFs to identify path name of 'dirid' directory
2345 * in a 'tree_id' tree. and sets path name to 'name'.
2347 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2348 u64 tree_id, u64 dirid, char *name)
2350 struct btrfs_root *root;
2351 struct btrfs_key key;
2357 struct btrfs_inode_ref *iref;
2358 struct extent_buffer *l;
2359 struct btrfs_path *path;
2361 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2366 path = btrfs_alloc_path();
2370 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2372 root = btrfs_get_fs_root(info, tree_id, true);
2374 ret = PTR_ERR(root);
2379 key.objectid = dirid;
2380 key.type = BTRFS_INODE_REF_KEY;
2381 key.offset = (u64)-1;
2384 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2388 ret = btrfs_previous_item(root, path, dirid,
2389 BTRFS_INODE_REF_KEY);
2399 slot = path->slots[0];
2400 btrfs_item_key_to_cpu(l, &key, slot);
2402 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2403 len = btrfs_inode_ref_name_len(l, iref);
2405 total_len += len + 1;
2407 ret = -ENAMETOOLONG;
2412 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2414 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2417 btrfs_release_path(path);
2418 key.objectid = key.offset;
2419 key.offset = (u64)-1;
2420 dirid = key.objectid;
2422 memmove(name, ptr, total_len);
2423 name[total_len] = '\0';
2426 btrfs_put_root(root);
2427 btrfs_free_path(path);
2431 static int btrfs_search_path_in_tree_user(struct inode *inode,
2432 struct btrfs_ioctl_ino_lookup_user_args *args)
2434 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2435 struct super_block *sb = inode->i_sb;
2436 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2437 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2438 u64 dirid = args->dirid;
2439 unsigned long item_off;
2440 unsigned long item_len;
2441 struct btrfs_inode_ref *iref;
2442 struct btrfs_root_ref *rref;
2443 struct btrfs_root *root = NULL;
2444 struct btrfs_path *path;
2445 struct btrfs_key key, key2;
2446 struct extent_buffer *leaf;
2447 struct inode *temp_inode;
2454 path = btrfs_alloc_path();
2459 * If the bottom subvolume does not exist directly under upper_limit,
2460 * construct the path in from the bottom up.
2462 if (dirid != upper_limit.objectid) {
2463 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2465 root = btrfs_get_fs_root(fs_info, treeid, true);
2467 ret = PTR_ERR(root);
2471 key.objectid = dirid;
2472 key.type = BTRFS_INODE_REF_KEY;
2473 key.offset = (u64)-1;
2475 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2478 } else if (ret > 0) {
2479 ret = btrfs_previous_item(root, path, dirid,
2480 BTRFS_INODE_REF_KEY);
2483 } else if (ret > 0) {
2489 leaf = path->nodes[0];
2490 slot = path->slots[0];
2491 btrfs_item_key_to_cpu(leaf, &key, slot);
2493 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2494 len = btrfs_inode_ref_name_len(leaf, iref);
2496 total_len += len + 1;
2497 if (ptr < args->path) {
2498 ret = -ENAMETOOLONG;
2503 read_extent_buffer(leaf, ptr,
2504 (unsigned long)(iref + 1), len);
2506 /* Check the read+exec permission of this directory */
2507 ret = btrfs_previous_item(root, path, dirid,
2508 BTRFS_INODE_ITEM_KEY);
2511 } else if (ret > 0) {
2516 leaf = path->nodes[0];
2517 slot = path->slots[0];
2518 btrfs_item_key_to_cpu(leaf, &key2, slot);
2519 if (key2.objectid != dirid) {
2524 temp_inode = btrfs_iget(sb, key2.objectid, root);
2525 if (IS_ERR(temp_inode)) {
2526 ret = PTR_ERR(temp_inode);
2529 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2536 if (key.offset == upper_limit.objectid)
2538 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2543 btrfs_release_path(path);
2544 key.objectid = key.offset;
2545 key.offset = (u64)-1;
2546 dirid = key.objectid;
2549 memmove(args->path, ptr, total_len);
2550 args->path[total_len] = '\0';
2551 btrfs_put_root(root);
2553 btrfs_release_path(path);
2556 /* Get the bottom subvolume's name from ROOT_REF */
2557 key.objectid = treeid;
2558 key.type = BTRFS_ROOT_REF_KEY;
2559 key.offset = args->treeid;
2560 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2563 } else if (ret > 0) {
2568 leaf = path->nodes[0];
2569 slot = path->slots[0];
2570 btrfs_item_key_to_cpu(leaf, &key, slot);
2572 item_off = btrfs_item_ptr_offset(leaf, slot);
2573 item_len = btrfs_item_size_nr(leaf, slot);
2574 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2575 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2576 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2581 /* Copy subvolume's name */
2582 item_off += sizeof(struct btrfs_root_ref);
2583 item_len -= sizeof(struct btrfs_root_ref);
2584 read_extent_buffer(leaf, args->name, item_off, item_len);
2585 args->name[item_len] = 0;
2588 btrfs_put_root(root);
2590 btrfs_free_path(path);
2594 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2597 struct btrfs_ioctl_ino_lookup_args *args;
2598 struct inode *inode;
2601 args = memdup_user(argp, sizeof(*args));
2603 return PTR_ERR(args);
2605 inode = file_inode(file);
2608 * Unprivileged query to obtain the containing subvolume root id. The
2609 * path is reset so it's consistent with btrfs_search_path_in_tree.
2611 if (args->treeid == 0)
2612 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2614 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2619 if (!capable(CAP_SYS_ADMIN)) {
2624 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2625 args->treeid, args->objectid,
2629 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2637 * Version of ino_lookup ioctl (unprivileged)
2639 * The main differences from ino_lookup ioctl are:
2641 * 1. Read + Exec permission will be checked using inode_permission() during
2642 * path construction. -EACCES will be returned in case of failure.
2643 * 2. Path construction will be stopped at the inode number which corresponds
2644 * to the fd with which this ioctl is called. If constructed path does not
2645 * exist under fd's inode, -EACCES will be returned.
2646 * 3. The name of bottom subvolume is also searched and filled.
2648 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2650 struct btrfs_ioctl_ino_lookup_user_args *args;
2651 struct inode *inode;
2654 args = memdup_user(argp, sizeof(*args));
2656 return PTR_ERR(args);
2658 inode = file_inode(file);
2660 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2661 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2663 * The subvolume does not exist under fd with which this is
2670 ret = btrfs_search_path_in_tree_user(inode, args);
2672 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2679 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2680 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2682 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2683 struct btrfs_fs_info *fs_info;
2684 struct btrfs_root *root;
2685 struct btrfs_path *path;
2686 struct btrfs_key key;
2687 struct btrfs_root_item *root_item;
2688 struct btrfs_root_ref *rref;
2689 struct extent_buffer *leaf;
2690 unsigned long item_off;
2691 unsigned long item_len;
2692 struct inode *inode;
2696 path = btrfs_alloc_path();
2700 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2702 btrfs_free_path(path);
2706 inode = file_inode(file);
2707 fs_info = BTRFS_I(inode)->root->fs_info;
2709 /* Get root_item of inode's subvolume */
2710 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2711 root = btrfs_get_fs_root(fs_info, key.objectid, true);
2713 ret = PTR_ERR(root);
2716 root_item = &root->root_item;
2718 subvol_info->treeid = key.objectid;
2720 subvol_info->generation = btrfs_root_generation(root_item);
2721 subvol_info->flags = btrfs_root_flags(root_item);
2723 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2724 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2726 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2729 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2730 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2731 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2733 subvol_info->otransid = btrfs_root_otransid(root_item);
2734 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2735 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2737 subvol_info->stransid = btrfs_root_stransid(root_item);
2738 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2739 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2741 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2742 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2743 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2745 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2746 /* Search root tree for ROOT_BACKREF of this subvolume */
2747 key.type = BTRFS_ROOT_BACKREF_KEY;
2749 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2752 } else if (path->slots[0] >=
2753 btrfs_header_nritems(path->nodes[0])) {
2754 ret = btrfs_next_leaf(fs_info->tree_root, path);
2757 } else if (ret > 0) {
2763 leaf = path->nodes[0];
2764 slot = path->slots[0];
2765 btrfs_item_key_to_cpu(leaf, &key, slot);
2766 if (key.objectid == subvol_info->treeid &&
2767 key.type == BTRFS_ROOT_BACKREF_KEY) {
2768 subvol_info->parent_id = key.offset;
2770 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2771 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2773 item_off = btrfs_item_ptr_offset(leaf, slot)
2774 + sizeof(struct btrfs_root_ref);
2775 item_len = btrfs_item_size_nr(leaf, slot)
2776 - sizeof(struct btrfs_root_ref);
2777 read_extent_buffer(leaf, subvol_info->name,
2778 item_off, item_len);
2785 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2789 btrfs_put_root(root);
2791 btrfs_free_path(path);
2797 * Return ROOT_REF information of the subvolume containing this inode
2798 * except the subvolume name.
2800 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2802 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2803 struct btrfs_root_ref *rref;
2804 struct btrfs_root *root;
2805 struct btrfs_path *path;
2806 struct btrfs_key key;
2807 struct extent_buffer *leaf;
2808 struct inode *inode;
2814 path = btrfs_alloc_path();
2818 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2819 if (IS_ERR(rootrefs)) {
2820 btrfs_free_path(path);
2821 return PTR_ERR(rootrefs);
2824 inode = file_inode(file);
2825 root = BTRFS_I(inode)->root->fs_info->tree_root;
2826 objectid = BTRFS_I(inode)->root->root_key.objectid;
2828 key.objectid = objectid;
2829 key.type = BTRFS_ROOT_REF_KEY;
2830 key.offset = rootrefs->min_treeid;
2833 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2836 } else if (path->slots[0] >=
2837 btrfs_header_nritems(path->nodes[0])) {
2838 ret = btrfs_next_leaf(root, path);
2841 } else if (ret > 0) {
2847 leaf = path->nodes[0];
2848 slot = path->slots[0];
2850 btrfs_item_key_to_cpu(leaf, &key, slot);
2851 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2856 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2861 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2862 rootrefs->rootref[found].treeid = key.offset;
2863 rootrefs->rootref[found].dirid =
2864 btrfs_root_ref_dirid(leaf, rref);
2867 ret = btrfs_next_item(root, path);
2870 } else if (ret > 0) {
2877 if (!ret || ret == -EOVERFLOW) {
2878 rootrefs->num_items = found;
2879 /* update min_treeid for next search */
2881 rootrefs->min_treeid =
2882 rootrefs->rootref[found - 1].treeid + 1;
2883 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2888 btrfs_free_path(path);
2893 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2897 struct dentry *parent = file->f_path.dentry;
2898 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2899 struct dentry *dentry;
2900 struct inode *dir = d_inode(parent);
2901 struct inode *inode;
2902 struct btrfs_root *root = BTRFS_I(dir)->root;
2903 struct btrfs_root *dest = NULL;
2904 struct btrfs_ioctl_vol_args *vol_args = NULL;
2905 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2906 char *subvol_name, *subvol_name_ptr = NULL;
2909 bool destroy_parent = false;
2912 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2913 if (IS_ERR(vol_args2))
2914 return PTR_ERR(vol_args2);
2916 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2922 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2923 * name, same as v1 currently does.
2925 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2926 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2927 subvol_name = vol_args2->name;
2929 err = mnt_want_write_file(file);
2933 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2938 err = mnt_want_write_file(file);
2942 dentry = btrfs_get_dentry(fs_info->sb,
2943 BTRFS_FIRST_FREE_OBJECTID,
2944 vol_args2->subvolid, 0, 0);
2945 if (IS_ERR(dentry)) {
2946 err = PTR_ERR(dentry);
2947 goto out_drop_write;
2951 * Change the default parent since the subvolume being
2952 * deleted can be outside of the current mount point.
2954 parent = btrfs_get_parent(dentry);
2957 * At this point dentry->d_name can point to '/' if the
2958 * subvolume we want to destroy is outsite of the
2959 * current mount point, so we need to release the
2960 * current dentry and execute the lookup to return a new
2961 * one with ->d_name pointing to the
2962 * <mount point>/subvol_name.
2965 if (IS_ERR(parent)) {
2966 err = PTR_ERR(parent);
2967 goto out_drop_write;
2969 dir = d_inode(parent);
2972 * If v2 was used with SPEC_BY_ID, a new parent was
2973 * allocated since the subvolume can be outside of the
2974 * current mount point. Later on we need to release this
2975 * new parent dentry.
2977 destroy_parent = true;
2979 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2980 fs_info, vol_args2->subvolid);
2981 if (IS_ERR(subvol_name_ptr)) {
2982 err = PTR_ERR(subvol_name_ptr);
2985 /* subvol_name_ptr is already NULL termined */
2986 subvol_name = (char *)kbasename(subvol_name_ptr);
2989 vol_args = memdup_user(arg, sizeof(*vol_args));
2990 if (IS_ERR(vol_args))
2991 return PTR_ERR(vol_args);
2993 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
2994 subvol_name = vol_args->name;
2996 err = mnt_want_write_file(file);
3001 subvol_namelen = strlen(subvol_name);
3003 if (strchr(subvol_name, '/') ||
3004 strncmp(subvol_name, "..", subvol_namelen) == 0) {
3006 goto free_subvol_name;
3009 if (!S_ISDIR(dir->i_mode)) {
3011 goto free_subvol_name;
3014 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
3016 goto free_subvol_name;
3017 dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
3018 if (IS_ERR(dentry)) {
3019 err = PTR_ERR(dentry);
3020 goto out_unlock_dir;
3023 if (d_really_is_negative(dentry)) {
3028 inode = d_inode(dentry);
3029 dest = BTRFS_I(inode)->root;
3030 if (!capable(CAP_SYS_ADMIN)) {
3032 * Regular user. Only allow this with a special mount
3033 * option, when the user has write+exec access to the
3034 * subvol root, and when rmdir(2) would have been
3037 * Note that this is _not_ check that the subvol is
3038 * empty or doesn't contain data that we wouldn't
3039 * otherwise be able to delete.
3041 * Users who want to delete empty subvols should try
3045 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
3049 * Do not allow deletion if the parent dir is the same
3050 * as the dir to be deleted. That means the ioctl
3051 * must be called on the dentry referencing the root
3052 * of the subvol, not a random directory contained
3059 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
3064 /* check if subvolume may be deleted by a user */
3065 err = btrfs_may_delete(dir, dentry, 1);
3069 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
3075 err = btrfs_delete_subvolume(dir, dentry);
3076 inode_unlock(inode);
3078 fsnotify_rmdir(dir, dentry);
3087 kfree(subvol_name_ptr);
3092 mnt_drop_write_file(file);
3099 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
3101 struct inode *inode = file_inode(file);
3102 struct btrfs_root *root = BTRFS_I(inode)->root;
3103 struct btrfs_ioctl_defrag_range_args *range;
3106 ret = mnt_want_write_file(file);
3110 if (btrfs_root_readonly(root)) {
3115 switch (inode->i_mode & S_IFMT) {
3117 if (!capable(CAP_SYS_ADMIN)) {
3121 ret = btrfs_defrag_root(root);
3125 * Note that this does not check the file descriptor for write
3126 * access. This prevents defragmenting executables that are
3127 * running and allows defrag on files open in read-only mode.
3129 if (!capable(CAP_SYS_ADMIN) &&
3130 inode_permission(inode, MAY_WRITE)) {
3135 range = kzalloc(sizeof(*range), GFP_KERNEL);
3142 if (copy_from_user(range, argp,
3148 /* compression requires us to start the IO */
3149 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3150 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3151 range->extent_thresh = (u32)-1;
3154 /* the rest are all set to zero by kzalloc */
3155 range->len = (u64)-1;
3157 ret = btrfs_defrag_file(file_inode(file), file,
3158 range, BTRFS_OLDEST_GENERATION, 0);
3167 mnt_drop_write_file(file);
3171 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3173 struct btrfs_ioctl_vol_args *vol_args;
3176 if (!capable(CAP_SYS_ADMIN))
3179 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
3180 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3182 vol_args = memdup_user(arg, sizeof(*vol_args));
3183 if (IS_ERR(vol_args)) {
3184 ret = PTR_ERR(vol_args);
3188 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3189 ret = btrfs_init_new_device(fs_info, vol_args->name);
3192 btrfs_info(fs_info, "disk added %s", vol_args->name);
3196 btrfs_exclop_finish(fs_info);
3200 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3202 struct inode *inode = file_inode(file);
3203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3204 struct btrfs_ioctl_vol_args_v2 *vol_args;
3207 if (!capable(CAP_SYS_ADMIN))
3210 ret = mnt_want_write_file(file);
3214 vol_args = memdup_user(arg, sizeof(*vol_args));
3215 if (IS_ERR(vol_args)) {
3216 ret = PTR_ERR(vol_args);
3220 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
3225 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3226 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3230 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3231 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3233 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3234 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3236 btrfs_exclop_finish(fs_info);
3239 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3240 btrfs_info(fs_info, "device deleted: id %llu",
3243 btrfs_info(fs_info, "device deleted: %s",
3249 mnt_drop_write_file(file);
3253 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3255 struct inode *inode = file_inode(file);
3256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3257 struct btrfs_ioctl_vol_args *vol_args;
3260 if (!capable(CAP_SYS_ADMIN))
3263 ret = mnt_want_write_file(file);
3267 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3268 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3269 goto out_drop_write;
3272 vol_args = memdup_user(arg, sizeof(*vol_args));
3273 if (IS_ERR(vol_args)) {
3274 ret = PTR_ERR(vol_args);
3278 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3279 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3282 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3285 btrfs_exclop_finish(fs_info);
3287 mnt_drop_write_file(file);
3292 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3295 struct btrfs_ioctl_fs_info_args *fi_args;
3296 struct btrfs_device *device;
3297 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3301 fi_args = memdup_user(arg, sizeof(*fi_args));
3302 if (IS_ERR(fi_args))
3303 return PTR_ERR(fi_args);
3305 flags_in = fi_args->flags;
3306 memset(fi_args, 0, sizeof(*fi_args));
3309 fi_args->num_devices = fs_devices->num_devices;
3311 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3312 if (device->devid > fi_args->max_id)
3313 fi_args->max_id = device->devid;
3317 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3318 fi_args->nodesize = fs_info->nodesize;
3319 fi_args->sectorsize = fs_info->sectorsize;
3320 fi_args->clone_alignment = fs_info->sectorsize;
3322 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3323 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3324 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3325 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3328 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3329 fi_args->generation = fs_info->generation;
3330 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3333 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3334 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3335 sizeof(fi_args->metadata_uuid));
3336 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3339 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3346 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3349 struct btrfs_ioctl_dev_info_args *di_args;
3350 struct btrfs_device *dev;
3352 char *s_uuid = NULL;
3354 di_args = memdup_user(arg, sizeof(*di_args));
3355 if (IS_ERR(di_args))
3356 return PTR_ERR(di_args);
3358 if (!btrfs_is_empty_uuid(di_args->uuid))
3359 s_uuid = di_args->uuid;
3362 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3370 di_args->devid = dev->devid;
3371 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3372 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3373 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3375 strncpy(di_args->path, rcu_str_deref(dev->name),
3376 sizeof(di_args->path) - 1);
3377 di_args->path[sizeof(di_args->path) - 1] = 0;
3379 di_args->path[0] = '\0';
3384 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3391 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3393 struct inode *inode = file_inode(file);
3394 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3395 struct btrfs_root *root = BTRFS_I(inode)->root;
3396 struct btrfs_root *new_root;
3397 struct btrfs_dir_item *di;
3398 struct btrfs_trans_handle *trans;
3399 struct btrfs_path *path = NULL;
3400 struct btrfs_disk_key disk_key;
3405 if (!capable(CAP_SYS_ADMIN))
3408 ret = mnt_want_write_file(file);
3412 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3418 objectid = BTRFS_FS_TREE_OBJECTID;
3420 new_root = btrfs_get_fs_root(fs_info, objectid, true);
3421 if (IS_ERR(new_root)) {
3422 ret = PTR_ERR(new_root);
3425 if (!is_fstree(new_root->root_key.objectid)) {
3430 path = btrfs_alloc_path();
3436 trans = btrfs_start_transaction(root, 1);
3437 if (IS_ERR(trans)) {
3438 ret = PTR_ERR(trans);
3442 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3443 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
3444 dir_id, "default", 7, 1);
3445 if (IS_ERR_OR_NULL(di)) {
3446 btrfs_release_path(path);
3447 btrfs_end_transaction(trans);
3449 "Umm, you don't have the default diritem, this isn't going to work");
3454 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3455 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3456 btrfs_mark_buffer_dirty(path->nodes[0]);
3457 btrfs_release_path(path);
3459 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
3460 btrfs_end_transaction(trans);
3462 btrfs_put_root(new_root);
3463 btrfs_free_path(path);
3465 mnt_drop_write_file(file);
3469 static void get_block_group_info(struct list_head *groups_list,
3470 struct btrfs_ioctl_space_info *space)
3472 struct btrfs_block_group *block_group;
3474 space->total_bytes = 0;
3475 space->used_bytes = 0;
3477 list_for_each_entry(block_group, groups_list, list) {
3478 space->flags = block_group->flags;
3479 space->total_bytes += block_group->length;
3480 space->used_bytes += block_group->used;
3484 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3487 struct btrfs_ioctl_space_args space_args;
3488 struct btrfs_ioctl_space_info space;
3489 struct btrfs_ioctl_space_info *dest;
3490 struct btrfs_ioctl_space_info *dest_orig;
3491 struct btrfs_ioctl_space_info __user *user_dest;
3492 struct btrfs_space_info *info;
3493 static const u64 types[] = {
3494 BTRFS_BLOCK_GROUP_DATA,
3495 BTRFS_BLOCK_GROUP_SYSTEM,
3496 BTRFS_BLOCK_GROUP_METADATA,
3497 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3505 if (copy_from_user(&space_args,
3506 (struct btrfs_ioctl_space_args __user *)arg,
3507 sizeof(space_args)))
3510 for (i = 0; i < num_types; i++) {
3511 struct btrfs_space_info *tmp;
3514 list_for_each_entry(tmp, &fs_info->space_info, list) {
3515 if (tmp->flags == types[i]) {
3524 down_read(&info->groups_sem);
3525 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3526 if (!list_empty(&info->block_groups[c]))
3529 up_read(&info->groups_sem);
3533 * Global block reserve, exported as a space_info
3537 /* space_slots == 0 means they are asking for a count */
3538 if (space_args.space_slots == 0) {
3539 space_args.total_spaces = slot_count;
3543 slot_count = min_t(u64, space_args.space_slots, slot_count);
3545 alloc_size = sizeof(*dest) * slot_count;
3547 /* we generally have at most 6 or so space infos, one for each raid
3548 * level. So, a whole page should be more than enough for everyone
3550 if (alloc_size > PAGE_SIZE)
3553 space_args.total_spaces = 0;
3554 dest = kmalloc(alloc_size, GFP_KERNEL);
3559 /* now we have a buffer to copy into */
3560 for (i = 0; i < num_types; i++) {
3561 struct btrfs_space_info *tmp;
3567 list_for_each_entry(tmp, &fs_info->space_info, list) {
3568 if (tmp->flags == types[i]) {
3576 down_read(&info->groups_sem);
3577 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3578 if (!list_empty(&info->block_groups[c])) {
3579 get_block_group_info(&info->block_groups[c],
3581 memcpy(dest, &space, sizeof(space));
3583 space_args.total_spaces++;
3589 up_read(&info->groups_sem);
3593 * Add global block reserve
3596 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3598 spin_lock(&block_rsv->lock);
3599 space.total_bytes = block_rsv->size;
3600 space.used_bytes = block_rsv->size - block_rsv->reserved;
3601 spin_unlock(&block_rsv->lock);
3602 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3603 memcpy(dest, &space, sizeof(space));
3604 space_args.total_spaces++;
3607 user_dest = (struct btrfs_ioctl_space_info __user *)
3608 (arg + sizeof(struct btrfs_ioctl_space_args));
3610 if (copy_to_user(user_dest, dest_orig, alloc_size))
3615 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3621 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3624 struct btrfs_trans_handle *trans;
3628 trans = btrfs_attach_transaction_barrier(root);
3629 if (IS_ERR(trans)) {
3630 if (PTR_ERR(trans) != -ENOENT)
3631 return PTR_ERR(trans);
3633 /* No running transaction, don't bother */
3634 transid = root->fs_info->last_trans_committed;
3637 transid = trans->transid;
3638 ret = btrfs_commit_transaction_async(trans, 0);
3640 btrfs_end_transaction(trans);
3645 if (copy_to_user(argp, &transid, sizeof(transid)))
3650 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
3656 if (copy_from_user(&transid, argp, sizeof(transid)))
3659 transid = 0; /* current trans */
3661 return btrfs_wait_for_commit(fs_info, transid);
3664 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3666 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
3667 struct btrfs_ioctl_scrub_args *sa;
3670 if (!capable(CAP_SYS_ADMIN))
3673 sa = memdup_user(arg, sizeof(*sa));
3677 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3678 ret = mnt_want_write_file(file);
3683 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
3684 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3688 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3689 * error. This is important as it allows user space to know how much
3690 * progress scrub has done. For example, if scrub is canceled we get
3691 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3692 * space. Later user space can inspect the progress from the structure
3693 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3694 * previously (btrfs-progs does this).
3695 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3696 * then return -EFAULT to signal the structure was not copied or it may
3697 * be corrupt and unreliable due to a partial copy.
3699 if (copy_to_user(arg, sa, sizeof(*sa)))
3702 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3703 mnt_drop_write_file(file);
3709 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
3711 if (!capable(CAP_SYS_ADMIN))
3714 return btrfs_scrub_cancel(fs_info);
3717 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
3720 struct btrfs_ioctl_scrub_args *sa;
3723 if (!capable(CAP_SYS_ADMIN))
3726 sa = memdup_user(arg, sizeof(*sa));
3730 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
3732 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3739 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
3742 struct btrfs_ioctl_get_dev_stats *sa;
3745 sa = memdup_user(arg, sizeof(*sa));
3749 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3754 ret = btrfs_get_dev_stats(fs_info, sa);
3756 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3763 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3766 struct btrfs_ioctl_dev_replace_args *p;
3769 if (!capable(CAP_SYS_ADMIN))
3772 p = memdup_user(arg, sizeof(*p));
3777 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3778 if (sb_rdonly(fs_info->sb)) {
3782 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
3783 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3785 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
3786 btrfs_exclop_finish(fs_info);
3789 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3790 btrfs_dev_replace_status(fs_info, p);
3793 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3794 p->result = btrfs_dev_replace_cancel(fs_info);
3802 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
3809 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3815 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3816 struct inode_fs_paths *ipath = NULL;
3817 struct btrfs_path *path;
3819 if (!capable(CAP_DAC_READ_SEARCH))
3822 path = btrfs_alloc_path();
3828 ipa = memdup_user(arg, sizeof(*ipa));
3835 size = min_t(u32, ipa->size, 4096);
3836 ipath = init_ipath(size, root, path);
3837 if (IS_ERR(ipath)) {
3838 ret = PTR_ERR(ipath);
3843 ret = paths_from_inode(ipa->inum, ipath);
3847 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3848 rel_ptr = ipath->fspath->val[i] -
3849 (u64)(unsigned long)ipath->fspath->val;
3850 ipath->fspath->val[i] = rel_ptr;
3853 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3854 ipath->fspath, size);
3861 btrfs_free_path(path);
3868 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3870 struct btrfs_data_container *inodes = ctx;
3871 const size_t c = 3 * sizeof(u64);
3873 if (inodes->bytes_left >= c) {
3874 inodes->bytes_left -= c;
3875 inodes->val[inodes->elem_cnt] = inum;
3876 inodes->val[inodes->elem_cnt + 1] = offset;
3877 inodes->val[inodes->elem_cnt + 2] = root;
3878 inodes->elem_cnt += 3;
3880 inodes->bytes_missing += c - inodes->bytes_left;
3881 inodes->bytes_left = 0;
3882 inodes->elem_missed += 3;
3888 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
3889 void __user *arg, int version)
3893 struct btrfs_ioctl_logical_ino_args *loi;
3894 struct btrfs_data_container *inodes = NULL;
3895 struct btrfs_path *path = NULL;
3898 if (!capable(CAP_SYS_ADMIN))
3901 loi = memdup_user(arg, sizeof(*loi));
3903 return PTR_ERR(loi);
3906 ignore_offset = false;
3907 size = min_t(u32, loi->size, SZ_64K);
3909 /* All reserved bits must be 0 for now */
3910 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3914 /* Only accept flags we have defined so far */
3915 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3919 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
3920 size = min_t(u32, loi->size, SZ_16M);
3923 path = btrfs_alloc_path();
3929 inodes = init_data_container(size);
3930 if (IS_ERR(inodes)) {
3931 ret = PTR_ERR(inodes);
3936 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
3937 build_ino_list, inodes, ignore_offset);
3943 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3949 btrfs_free_path(path);
3957 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3958 struct btrfs_ioctl_balance_args *bargs)
3960 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3962 bargs->flags = bctl->flags;
3964 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
3965 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3966 if (atomic_read(&fs_info->balance_pause_req))
3967 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3968 if (atomic_read(&fs_info->balance_cancel_req))
3969 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3971 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3972 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3973 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3975 spin_lock(&fs_info->balance_lock);
3976 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3977 spin_unlock(&fs_info->balance_lock);
3980 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3982 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3983 struct btrfs_fs_info *fs_info = root->fs_info;
3984 struct btrfs_ioctl_balance_args *bargs;
3985 struct btrfs_balance_control *bctl;
3986 bool need_unlock; /* for mut. excl. ops lock */
3989 if (!capable(CAP_SYS_ADMIN))
3992 ret = mnt_want_write_file(file);
3997 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
3998 mutex_lock(&fs_info->balance_mutex);
4004 * mut. excl. ops lock is locked. Three possibilities:
4005 * (1) some other op is running
4006 * (2) balance is running
4007 * (3) balance is paused -- special case (think resume)
4009 mutex_lock(&fs_info->balance_mutex);
4010 if (fs_info->balance_ctl) {
4011 /* this is either (2) or (3) */
4012 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4013 mutex_unlock(&fs_info->balance_mutex);
4015 * Lock released to allow other waiters to continue,
4016 * we'll reexamine the status again.
4018 mutex_lock(&fs_info->balance_mutex);
4020 if (fs_info->balance_ctl &&
4021 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4023 need_unlock = false;
4027 mutex_unlock(&fs_info->balance_mutex);
4031 mutex_unlock(&fs_info->balance_mutex);
4037 mutex_unlock(&fs_info->balance_mutex);
4038 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4045 bargs = memdup_user(arg, sizeof(*bargs));
4046 if (IS_ERR(bargs)) {
4047 ret = PTR_ERR(bargs);
4051 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4052 if (!fs_info->balance_ctl) {
4057 bctl = fs_info->balance_ctl;
4058 spin_lock(&fs_info->balance_lock);
4059 bctl->flags |= BTRFS_BALANCE_RESUME;
4060 spin_unlock(&fs_info->balance_lock);
4068 if (fs_info->balance_ctl) {
4073 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4080 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4081 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4082 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4084 bctl->flags = bargs->flags;
4086 /* balance everything - no filters */
4087 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4090 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4097 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4098 * bctl is freed in reset_balance_state, or, if restriper was paused
4099 * all the way until unmount, in free_fs_info. The flag should be
4100 * cleared after reset_balance_state.
4102 need_unlock = false;
4104 ret = btrfs_balance(fs_info, bctl, bargs);
4107 if ((ret == 0 || ret == -ECANCELED) && arg) {
4108 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4117 mutex_unlock(&fs_info->balance_mutex);
4119 btrfs_exclop_finish(fs_info);
4121 mnt_drop_write_file(file);
4125 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4127 if (!capable(CAP_SYS_ADMIN))
4131 case BTRFS_BALANCE_CTL_PAUSE:
4132 return btrfs_pause_balance(fs_info);
4133 case BTRFS_BALANCE_CTL_CANCEL:
4134 return btrfs_cancel_balance(fs_info);
4140 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4143 struct btrfs_ioctl_balance_args *bargs;
4146 if (!capable(CAP_SYS_ADMIN))
4149 mutex_lock(&fs_info->balance_mutex);
4150 if (!fs_info->balance_ctl) {
4155 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4161 btrfs_update_ioctl_balance_args(fs_info, bargs);
4163 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4168 mutex_unlock(&fs_info->balance_mutex);
4172 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4174 struct inode *inode = file_inode(file);
4175 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4176 struct btrfs_ioctl_quota_ctl_args *sa;
4179 if (!capable(CAP_SYS_ADMIN))
4182 ret = mnt_want_write_file(file);
4186 sa = memdup_user(arg, sizeof(*sa));
4192 down_write(&fs_info->subvol_sem);
4195 case BTRFS_QUOTA_CTL_ENABLE:
4196 ret = btrfs_quota_enable(fs_info);
4198 case BTRFS_QUOTA_CTL_DISABLE:
4199 ret = btrfs_quota_disable(fs_info);
4207 up_write(&fs_info->subvol_sem);
4209 mnt_drop_write_file(file);
4213 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4215 struct inode *inode = file_inode(file);
4216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4217 struct btrfs_root *root = BTRFS_I(inode)->root;
4218 struct btrfs_ioctl_qgroup_assign_args *sa;
4219 struct btrfs_trans_handle *trans;
4223 if (!capable(CAP_SYS_ADMIN))
4226 ret = mnt_want_write_file(file);
4230 sa = memdup_user(arg, sizeof(*sa));
4236 trans = btrfs_join_transaction(root);
4237 if (IS_ERR(trans)) {
4238 ret = PTR_ERR(trans);
4243 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4245 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4248 /* update qgroup status and info */
4249 err = btrfs_run_qgroups(trans);
4251 btrfs_handle_fs_error(fs_info, err,
4252 "failed to update qgroup status and info");
4253 err = btrfs_end_transaction(trans);
4260 mnt_drop_write_file(file);
4264 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4266 struct inode *inode = file_inode(file);
4267 struct btrfs_root *root = BTRFS_I(inode)->root;
4268 struct btrfs_ioctl_qgroup_create_args *sa;
4269 struct btrfs_trans_handle *trans;
4273 if (!capable(CAP_SYS_ADMIN))
4276 ret = mnt_want_write_file(file);
4280 sa = memdup_user(arg, sizeof(*sa));
4286 if (!sa->qgroupid) {
4291 trans = btrfs_join_transaction(root);
4292 if (IS_ERR(trans)) {
4293 ret = PTR_ERR(trans);
4298 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4300 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4303 err = btrfs_end_transaction(trans);
4310 mnt_drop_write_file(file);
4314 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4316 struct inode *inode = file_inode(file);
4317 struct btrfs_root *root = BTRFS_I(inode)->root;
4318 struct btrfs_ioctl_qgroup_limit_args *sa;
4319 struct btrfs_trans_handle *trans;
4324 if (!capable(CAP_SYS_ADMIN))
4327 ret = mnt_want_write_file(file);
4331 sa = memdup_user(arg, sizeof(*sa));
4337 trans = btrfs_join_transaction(root);
4338 if (IS_ERR(trans)) {
4339 ret = PTR_ERR(trans);
4343 qgroupid = sa->qgroupid;
4345 /* take the current subvol as qgroup */
4346 qgroupid = root->root_key.objectid;
4349 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
4351 err = btrfs_end_transaction(trans);
4358 mnt_drop_write_file(file);
4362 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4364 struct inode *inode = file_inode(file);
4365 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4366 struct btrfs_ioctl_quota_rescan_args *qsa;
4369 if (!capable(CAP_SYS_ADMIN))
4372 ret = mnt_want_write_file(file);
4376 qsa = memdup_user(arg, sizeof(*qsa));
4387 ret = btrfs_qgroup_rescan(fs_info);
4392 mnt_drop_write_file(file);
4396 static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4399 struct btrfs_ioctl_quota_rescan_args *qsa;
4402 if (!capable(CAP_SYS_ADMIN))
4405 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
4409 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4411 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
4414 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4421 static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4424 if (!capable(CAP_SYS_ADMIN))
4427 return btrfs_qgroup_wait_for_completion(fs_info, true);
4430 static long _btrfs_ioctl_set_received_subvol(struct file *file,
4431 struct btrfs_ioctl_received_subvol_args *sa)
4433 struct inode *inode = file_inode(file);
4434 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4435 struct btrfs_root *root = BTRFS_I(inode)->root;
4436 struct btrfs_root_item *root_item = &root->root_item;
4437 struct btrfs_trans_handle *trans;
4438 struct timespec64 ct = current_time(inode);
4440 int received_uuid_changed;
4442 if (!inode_owner_or_capable(inode))
4445 ret = mnt_want_write_file(file);
4449 down_write(&fs_info->subvol_sem);
4451 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
4456 if (btrfs_root_readonly(root)) {
4463 * 2 - uuid items (received uuid + subvol uuid)
4465 trans = btrfs_start_transaction(root, 3);
4466 if (IS_ERR(trans)) {
4467 ret = PTR_ERR(trans);
4472 sa->rtransid = trans->transid;
4473 sa->rtime.sec = ct.tv_sec;
4474 sa->rtime.nsec = ct.tv_nsec;
4476 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4478 if (received_uuid_changed &&
4479 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4480 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
4481 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4482 root->root_key.objectid);
4483 if (ret && ret != -ENOENT) {
4484 btrfs_abort_transaction(trans, ret);
4485 btrfs_end_transaction(trans);
4489 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4490 btrfs_set_root_stransid(root_item, sa->stransid);
4491 btrfs_set_root_rtransid(root_item, sa->rtransid);
4492 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4493 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4494 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4495 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4497 ret = btrfs_update_root(trans, fs_info->tree_root,
4498 &root->root_key, &root->root_item);
4500 btrfs_end_transaction(trans);
4503 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4504 ret = btrfs_uuid_tree_add(trans, sa->uuid,
4505 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4506 root->root_key.objectid);
4507 if (ret < 0 && ret != -EEXIST) {
4508 btrfs_abort_transaction(trans, ret);
4509 btrfs_end_transaction(trans);
4513 ret = btrfs_commit_transaction(trans);
4515 up_write(&fs_info->subvol_sem);
4516 mnt_drop_write_file(file);
4521 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4524 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4525 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4528 args32 = memdup_user(arg, sizeof(*args32));
4530 return PTR_ERR(args32);
4532 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
4538 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4539 args64->stransid = args32->stransid;
4540 args64->rtransid = args32->rtransid;
4541 args64->stime.sec = args32->stime.sec;
4542 args64->stime.nsec = args32->stime.nsec;
4543 args64->rtime.sec = args32->rtime.sec;
4544 args64->rtime.nsec = args32->rtime.nsec;
4545 args64->flags = args32->flags;
4547 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4551 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4552 args32->stransid = args64->stransid;
4553 args32->rtransid = args64->rtransid;
4554 args32->stime.sec = args64->stime.sec;
4555 args32->stime.nsec = args64->stime.nsec;
4556 args32->rtime.sec = args64->rtime.sec;
4557 args32->rtime.nsec = args64->rtime.nsec;
4558 args32->flags = args64->flags;
4560 ret = copy_to_user(arg, args32, sizeof(*args32));
4571 static long btrfs_ioctl_set_received_subvol(struct file *file,
4574 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4577 sa = memdup_user(arg, sizeof(*sa));
4581 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4586 ret = copy_to_user(arg, sa, sizeof(*sa));
4595 static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4600 char label[BTRFS_LABEL_SIZE];
4602 spin_lock(&fs_info->super_lock);
4603 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4604 spin_unlock(&fs_info->super_lock);
4606 len = strnlen(label, BTRFS_LABEL_SIZE);
4608 if (len == BTRFS_LABEL_SIZE) {
4610 "label is too long, return the first %zu bytes",
4614 ret = copy_to_user(arg, label, len);
4616 return ret ? -EFAULT : 0;
4619 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4621 struct inode *inode = file_inode(file);
4622 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4623 struct btrfs_root *root = BTRFS_I(inode)->root;
4624 struct btrfs_super_block *super_block = fs_info->super_copy;
4625 struct btrfs_trans_handle *trans;
4626 char label[BTRFS_LABEL_SIZE];
4629 if (!capable(CAP_SYS_ADMIN))
4632 if (copy_from_user(label, arg, sizeof(label)))
4635 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4637 "unable to set label with more than %d bytes",
4638 BTRFS_LABEL_SIZE - 1);
4642 ret = mnt_want_write_file(file);
4646 trans = btrfs_start_transaction(root, 0);
4647 if (IS_ERR(trans)) {
4648 ret = PTR_ERR(trans);
4652 spin_lock(&fs_info->super_lock);
4653 strcpy(super_block->label, label);
4654 spin_unlock(&fs_info->super_lock);
4655 ret = btrfs_commit_transaction(trans);
4658 mnt_drop_write_file(file);
4662 #define INIT_FEATURE_FLAGS(suffix) \
4663 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4664 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4665 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4667 int btrfs_ioctl_get_supported_features(void __user *arg)
4669 static const struct btrfs_ioctl_feature_flags features[3] = {
4670 INIT_FEATURE_FLAGS(SUPP),
4671 INIT_FEATURE_FLAGS(SAFE_SET),
4672 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4675 if (copy_to_user(arg, &features, sizeof(features)))
4681 static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4684 struct btrfs_super_block *super_block = fs_info->super_copy;
4685 struct btrfs_ioctl_feature_flags features;
4687 features.compat_flags = btrfs_super_compat_flags(super_block);
4688 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4689 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4691 if (copy_to_user(arg, &features, sizeof(features)))
4697 static int check_feature_bits(struct btrfs_fs_info *fs_info,
4698 enum btrfs_feature_set set,
4699 u64 change_mask, u64 flags, u64 supported_flags,
4700 u64 safe_set, u64 safe_clear)
4702 const char *type = btrfs_feature_set_name(set);
4704 u64 disallowed, unsupported;
4705 u64 set_mask = flags & change_mask;
4706 u64 clear_mask = ~flags & change_mask;
4708 unsupported = set_mask & ~supported_flags;
4710 names = btrfs_printable_features(set, unsupported);
4713 "this kernel does not support the %s feature bit%s",
4714 names, strchr(names, ',') ? "s" : "");
4718 "this kernel does not support %s bits 0x%llx",
4723 disallowed = set_mask & ~safe_set;
4725 names = btrfs_printable_features(set, disallowed);
4728 "can't set the %s feature bit%s while mounted",
4729 names, strchr(names, ',') ? "s" : "");
4733 "can't set %s bits 0x%llx while mounted",
4738 disallowed = clear_mask & ~safe_clear;
4740 names = btrfs_printable_features(set, disallowed);
4743 "can't clear the %s feature bit%s while mounted",
4744 names, strchr(names, ',') ? "s" : "");
4748 "can't clear %s bits 0x%llx while mounted",
4756 #define check_feature(fs_info, change_mask, flags, mask_base) \
4757 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
4758 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4759 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4760 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4762 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4764 struct inode *inode = file_inode(file);
4765 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4766 struct btrfs_root *root = BTRFS_I(inode)->root;
4767 struct btrfs_super_block *super_block = fs_info->super_copy;
4768 struct btrfs_ioctl_feature_flags flags[2];
4769 struct btrfs_trans_handle *trans;
4773 if (!capable(CAP_SYS_ADMIN))
4776 if (copy_from_user(flags, arg, sizeof(flags)))
4780 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4781 !flags[0].incompat_flags)
4784 ret = check_feature(fs_info, flags[0].compat_flags,
4785 flags[1].compat_flags, COMPAT);
4789 ret = check_feature(fs_info, flags[0].compat_ro_flags,
4790 flags[1].compat_ro_flags, COMPAT_RO);
4794 ret = check_feature(fs_info, flags[0].incompat_flags,
4795 flags[1].incompat_flags, INCOMPAT);
4799 ret = mnt_want_write_file(file);
4803 trans = btrfs_start_transaction(root, 0);
4804 if (IS_ERR(trans)) {
4805 ret = PTR_ERR(trans);
4806 goto out_drop_write;
4809 spin_lock(&fs_info->super_lock);
4810 newflags = btrfs_super_compat_flags(super_block);
4811 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4812 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4813 btrfs_set_super_compat_flags(super_block, newflags);
4815 newflags = btrfs_super_compat_ro_flags(super_block);
4816 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4817 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4818 btrfs_set_super_compat_ro_flags(super_block, newflags);
4820 newflags = btrfs_super_incompat_flags(super_block);
4821 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4822 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4823 btrfs_set_super_incompat_flags(super_block, newflags);
4824 spin_unlock(&fs_info->super_lock);
4826 ret = btrfs_commit_transaction(trans);
4828 mnt_drop_write_file(file);
4833 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4835 struct btrfs_ioctl_send_args *arg;
4839 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4840 struct btrfs_ioctl_send_args_32 args32;
4842 ret = copy_from_user(&args32, argp, sizeof(args32));
4845 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4848 arg->send_fd = args32.send_fd;
4849 arg->clone_sources_count = args32.clone_sources_count;
4850 arg->clone_sources = compat_ptr(args32.clone_sources);
4851 arg->parent_root = args32.parent_root;
4852 arg->flags = args32.flags;
4853 memcpy(arg->reserved, args32.reserved,
4854 sizeof(args32.reserved));
4859 arg = memdup_user(argp, sizeof(*arg));
4861 return PTR_ERR(arg);
4863 ret = btrfs_ioctl_send(file, arg);
4868 long btrfs_ioctl(struct file *file, unsigned int
4869 cmd, unsigned long arg)
4871 struct inode *inode = file_inode(file);
4872 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4873 struct btrfs_root *root = BTRFS_I(inode)->root;
4874 void __user *argp = (void __user *)arg;
4877 case FS_IOC_GETFLAGS:
4878 return btrfs_ioctl_getflags(file, argp);
4879 case FS_IOC_SETFLAGS:
4880 return btrfs_ioctl_setflags(file, argp);
4881 case FS_IOC_GETVERSION:
4882 return btrfs_ioctl_getversion(file, argp);
4883 case FS_IOC_GETFSLABEL:
4884 return btrfs_ioctl_get_fslabel(fs_info, argp);
4885 case FS_IOC_SETFSLABEL:
4886 return btrfs_ioctl_set_fslabel(file, argp);
4888 return btrfs_ioctl_fitrim(fs_info, argp);
4889 case BTRFS_IOC_SNAP_CREATE:
4890 return btrfs_ioctl_snap_create(file, argp, 0);
4891 case BTRFS_IOC_SNAP_CREATE_V2:
4892 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4893 case BTRFS_IOC_SUBVOL_CREATE:
4894 return btrfs_ioctl_snap_create(file, argp, 1);
4895 case BTRFS_IOC_SUBVOL_CREATE_V2:
4896 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4897 case BTRFS_IOC_SNAP_DESTROY:
4898 return btrfs_ioctl_snap_destroy(file, argp, false);
4899 case BTRFS_IOC_SNAP_DESTROY_V2:
4900 return btrfs_ioctl_snap_destroy(file, argp, true);
4901 case BTRFS_IOC_SUBVOL_GETFLAGS:
4902 return btrfs_ioctl_subvol_getflags(file, argp);
4903 case BTRFS_IOC_SUBVOL_SETFLAGS:
4904 return btrfs_ioctl_subvol_setflags(file, argp);
4905 case BTRFS_IOC_DEFAULT_SUBVOL:
4906 return btrfs_ioctl_default_subvol(file, argp);
4907 case BTRFS_IOC_DEFRAG:
4908 return btrfs_ioctl_defrag(file, NULL);
4909 case BTRFS_IOC_DEFRAG_RANGE:
4910 return btrfs_ioctl_defrag(file, argp);
4911 case BTRFS_IOC_RESIZE:
4912 return btrfs_ioctl_resize(file, argp);
4913 case BTRFS_IOC_ADD_DEV:
4914 return btrfs_ioctl_add_dev(fs_info, argp);
4915 case BTRFS_IOC_RM_DEV:
4916 return btrfs_ioctl_rm_dev(file, argp);
4917 case BTRFS_IOC_RM_DEV_V2:
4918 return btrfs_ioctl_rm_dev_v2(file, argp);
4919 case BTRFS_IOC_FS_INFO:
4920 return btrfs_ioctl_fs_info(fs_info, argp);
4921 case BTRFS_IOC_DEV_INFO:
4922 return btrfs_ioctl_dev_info(fs_info, argp);
4923 case BTRFS_IOC_BALANCE:
4924 return btrfs_ioctl_balance(file, NULL);
4925 case BTRFS_IOC_TREE_SEARCH:
4926 return btrfs_ioctl_tree_search(file, argp);
4927 case BTRFS_IOC_TREE_SEARCH_V2:
4928 return btrfs_ioctl_tree_search_v2(file, argp);
4929 case BTRFS_IOC_INO_LOOKUP:
4930 return btrfs_ioctl_ino_lookup(file, argp);
4931 case BTRFS_IOC_INO_PATHS:
4932 return btrfs_ioctl_ino_to_path(root, argp);
4933 case BTRFS_IOC_LOGICAL_INO:
4934 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4935 case BTRFS_IOC_LOGICAL_INO_V2:
4936 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
4937 case BTRFS_IOC_SPACE_INFO:
4938 return btrfs_ioctl_space_info(fs_info, argp);
4939 case BTRFS_IOC_SYNC: {
4942 ret = btrfs_start_delalloc_roots(fs_info, U64_MAX);
4945 ret = btrfs_sync_fs(inode->i_sb, 1);
4947 * The transaction thread may want to do more work,
4948 * namely it pokes the cleaner kthread that will start
4949 * processing uncleaned subvols.
4951 wake_up_process(fs_info->transaction_kthread);
4954 case BTRFS_IOC_START_SYNC:
4955 return btrfs_ioctl_start_sync(root, argp);
4956 case BTRFS_IOC_WAIT_SYNC:
4957 return btrfs_ioctl_wait_sync(fs_info, argp);
4958 case BTRFS_IOC_SCRUB:
4959 return btrfs_ioctl_scrub(file, argp);
4960 case BTRFS_IOC_SCRUB_CANCEL:
4961 return btrfs_ioctl_scrub_cancel(fs_info);
4962 case BTRFS_IOC_SCRUB_PROGRESS:
4963 return btrfs_ioctl_scrub_progress(fs_info, argp);
4964 case BTRFS_IOC_BALANCE_V2:
4965 return btrfs_ioctl_balance(file, argp);
4966 case BTRFS_IOC_BALANCE_CTL:
4967 return btrfs_ioctl_balance_ctl(fs_info, arg);
4968 case BTRFS_IOC_BALANCE_PROGRESS:
4969 return btrfs_ioctl_balance_progress(fs_info, argp);
4970 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4971 return btrfs_ioctl_set_received_subvol(file, argp);
4973 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4974 return btrfs_ioctl_set_received_subvol_32(file, argp);
4976 case BTRFS_IOC_SEND:
4977 return _btrfs_ioctl_send(file, argp, false);
4978 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4979 case BTRFS_IOC_SEND_32:
4980 return _btrfs_ioctl_send(file, argp, true);
4982 case BTRFS_IOC_GET_DEV_STATS:
4983 return btrfs_ioctl_get_dev_stats(fs_info, argp);
4984 case BTRFS_IOC_QUOTA_CTL:
4985 return btrfs_ioctl_quota_ctl(file, argp);
4986 case BTRFS_IOC_QGROUP_ASSIGN:
4987 return btrfs_ioctl_qgroup_assign(file, argp);
4988 case BTRFS_IOC_QGROUP_CREATE:
4989 return btrfs_ioctl_qgroup_create(file, argp);
4990 case BTRFS_IOC_QGROUP_LIMIT:
4991 return btrfs_ioctl_qgroup_limit(file, argp);
4992 case BTRFS_IOC_QUOTA_RESCAN:
4993 return btrfs_ioctl_quota_rescan(file, argp);
4994 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4995 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
4996 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4997 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
4998 case BTRFS_IOC_DEV_REPLACE:
4999 return btrfs_ioctl_dev_replace(fs_info, argp);
5000 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5001 return btrfs_ioctl_get_supported_features(argp);
5002 case BTRFS_IOC_GET_FEATURES:
5003 return btrfs_ioctl_get_features(fs_info, argp);
5004 case BTRFS_IOC_SET_FEATURES:
5005 return btrfs_ioctl_set_features(file, argp);
5006 case FS_IOC_FSGETXATTR:
5007 return btrfs_ioctl_fsgetxattr(file, argp);
5008 case FS_IOC_FSSETXATTR:
5009 return btrfs_ioctl_fssetxattr(file, argp);
5010 case BTRFS_IOC_GET_SUBVOL_INFO:
5011 return btrfs_ioctl_get_subvol_info(file, argp);
5012 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5013 return btrfs_ioctl_get_subvol_rootref(file, argp);
5014 case BTRFS_IOC_INO_LOOKUP_USER:
5015 return btrfs_ioctl_ino_lookup_user(file, argp);
5021 #ifdef CONFIG_COMPAT
5022 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5025 * These all access 32-bit values anyway so no further
5026 * handling is necessary.
5029 case FS_IOC32_GETFLAGS:
5030 cmd = FS_IOC_GETFLAGS;
5032 case FS_IOC32_SETFLAGS:
5033 cmd = FS_IOC_SETFLAGS;
5035 case FS_IOC32_GETVERSION:
5036 cmd = FS_IOC_GETVERSION;
5040 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));