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>
29 #include <linux/fileattr.h>
33 #include "transaction.h"
34 #include "btrfs_inode.h"
35 #include "print-tree.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,
158 * Check if @flags are a supported and valid set of FS_*_FL flags and that
159 * the old and new flags are not conflicting
161 static int check_fsflags(unsigned int old_flags, unsigned int flags)
163 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
164 FS_NOATIME_FL | FS_NODUMP_FL | \
165 FS_SYNC_FL | FS_DIRSYNC_FL | \
166 FS_NOCOMP_FL | FS_COMPR_FL |
170 /* COMPR and NOCOMP on new/old are valid */
171 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
174 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
177 /* NOCOW and compression options are mutually exclusive */
178 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
180 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
186 static int check_fsflags_compatible(struct btrfs_fs_info *fs_info,
189 if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL))
196 * Set flags/xflags from the internal inode flags. The remaining items of
197 * fsxattr are zeroed.
199 int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
201 struct btrfs_inode *binode = BTRFS_I(d_inode(dentry));
203 fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(binode->flags));
207 int btrfs_fileattr_set(struct user_namespace *mnt_userns,
208 struct dentry *dentry, struct fileattr *fa)
210 struct inode *inode = d_inode(dentry);
211 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
212 struct btrfs_inode *binode = BTRFS_I(inode);
213 struct btrfs_root *root = binode->root;
214 struct btrfs_trans_handle *trans;
215 unsigned int fsflags, old_fsflags;
217 const char *comp = NULL;
220 if (btrfs_root_readonly(root))
223 if (fileattr_has_fsx(fa))
226 fsflags = btrfs_mask_fsflags_for_type(inode, fa->flags);
227 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
228 ret = check_fsflags(old_fsflags, fsflags);
232 ret = check_fsflags_compatible(fs_info, fsflags);
236 binode_flags = binode->flags;
237 if (fsflags & FS_SYNC_FL)
238 binode_flags |= BTRFS_INODE_SYNC;
240 binode_flags &= ~BTRFS_INODE_SYNC;
241 if (fsflags & FS_IMMUTABLE_FL)
242 binode_flags |= BTRFS_INODE_IMMUTABLE;
244 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
245 if (fsflags & FS_APPEND_FL)
246 binode_flags |= BTRFS_INODE_APPEND;
248 binode_flags &= ~BTRFS_INODE_APPEND;
249 if (fsflags & FS_NODUMP_FL)
250 binode_flags |= BTRFS_INODE_NODUMP;
252 binode_flags &= ~BTRFS_INODE_NODUMP;
253 if (fsflags & FS_NOATIME_FL)
254 binode_flags |= BTRFS_INODE_NOATIME;
256 binode_flags &= ~BTRFS_INODE_NOATIME;
258 /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */
259 if (!fa->flags_valid) {
260 /* 1 item for the inode */
261 trans = btrfs_start_transaction(root, 1);
265 if (fsflags & FS_DIRSYNC_FL)
266 binode_flags |= BTRFS_INODE_DIRSYNC;
268 binode_flags &= ~BTRFS_INODE_DIRSYNC;
269 if (fsflags & FS_NOCOW_FL) {
270 if (S_ISREG(inode->i_mode)) {
272 * It's safe to turn csums off here, no extents exist.
273 * Otherwise we want the flag to reflect the real COW
274 * status of the file and will not set it.
276 if (inode->i_size == 0)
277 binode_flags |= BTRFS_INODE_NODATACOW |
278 BTRFS_INODE_NODATASUM;
280 binode_flags |= BTRFS_INODE_NODATACOW;
284 * Revert back under same assumptions as above
286 if (S_ISREG(inode->i_mode)) {
287 if (inode->i_size == 0)
288 binode_flags &= ~(BTRFS_INODE_NODATACOW |
289 BTRFS_INODE_NODATASUM);
291 binode_flags &= ~BTRFS_INODE_NODATACOW;
296 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
297 * flag may be changed automatically if compression code won't make
300 if (fsflags & FS_NOCOMP_FL) {
301 binode_flags &= ~BTRFS_INODE_COMPRESS;
302 binode_flags |= BTRFS_INODE_NOCOMPRESS;
303 } else if (fsflags & FS_COMPR_FL) {
305 if (IS_SWAPFILE(inode))
308 binode_flags |= BTRFS_INODE_COMPRESS;
309 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
311 comp = btrfs_compress_type2str(fs_info->compress_type);
312 if (!comp || comp[0] == 0)
313 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
315 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
322 trans = btrfs_start_transaction(root, 3);
324 return PTR_ERR(trans);
327 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
330 btrfs_abort_transaction(trans, ret);
334 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
336 if (ret && ret != -ENODATA) {
337 btrfs_abort_transaction(trans, ret);
343 binode->flags = binode_flags;
344 btrfs_sync_inode_flags_to_i_flags(inode);
345 inode_inc_iversion(inode);
346 inode->i_ctime = current_time(inode);
347 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
350 btrfs_end_transaction(trans);
354 bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
355 enum btrfs_exclusive_operation type)
357 return !cmpxchg(&fs_info->exclusive_operation, BTRFS_EXCLOP_NONE, type);
360 void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
362 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
363 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
366 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
368 struct inode *inode = file_inode(file);
370 return put_user(inode->i_generation, arg);
373 static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
376 struct btrfs_device *device;
377 struct request_queue *q;
378 struct fstrim_range range;
379 u64 minlen = ULLONG_MAX;
383 if (!capable(CAP_SYS_ADMIN))
387 * btrfs_trim_block_group() depends on space cache, which is not
388 * available in zoned filesystem. So, disallow fitrim on a zoned
389 * filesystem for now.
391 if (btrfs_is_zoned(fs_info))
395 * If the fs is mounted with nologreplay, which requires it to be
396 * mounted in RO mode as well, we can not allow discard on free space
397 * inside block groups, because log trees refer to extents that are not
398 * pinned in a block group's free space cache (pinning the extents is
399 * precisely the first phase of replaying a log tree).
401 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
405 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
409 q = bdev_get_queue(device->bdev);
410 if (blk_queue_discard(q)) {
412 minlen = min_t(u64, q->limits.discard_granularity,
420 if (copy_from_user(&range, arg, sizeof(range)))
424 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
425 * block group is in the logical address space, which can be any
426 * sectorsize aligned bytenr in the range [0, U64_MAX].
428 if (range.len < fs_info->sb->s_blocksize)
431 range.minlen = max(range.minlen, minlen);
432 ret = btrfs_trim_fs(fs_info, &range);
436 if (copy_to_user(arg, &range, sizeof(range)))
442 int __pure btrfs_is_empty_uuid(u8 *uuid)
446 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
453 static noinline int create_subvol(struct inode *dir,
454 struct dentry *dentry,
455 const char *name, int namelen,
456 struct btrfs_qgroup_inherit *inherit)
458 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
459 struct btrfs_trans_handle *trans;
460 struct btrfs_key key;
461 struct btrfs_root_item *root_item;
462 struct btrfs_inode_item *inode_item;
463 struct extent_buffer *leaf;
464 struct btrfs_root *root = BTRFS_I(dir)->root;
465 struct btrfs_root *new_root;
466 struct btrfs_block_rsv block_rsv;
467 struct timespec64 cur_time = current_time(dir);
475 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
479 ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
483 ret = get_anon_bdev(&anon_dev);
488 * Don't create subvolume whose level is not zero. Or qgroup will be
489 * screwed up since it assumes subvolume qgroup's level to be 0.
491 if (btrfs_qgroup_level(objectid)) {
496 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
498 * The same as the snapshot creation, please see the comment
499 * of create_snapshot().
501 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
505 trans = btrfs_start_transaction(root, 0);
507 ret = PTR_ERR(trans);
508 btrfs_subvolume_release_metadata(root, &block_rsv);
511 trans->block_rsv = &block_rsv;
512 trans->bytes_reserved = block_rsv.size;
514 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
518 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
519 BTRFS_NESTING_NORMAL);
525 btrfs_mark_buffer_dirty(leaf);
527 inode_item = &root_item->inode;
528 btrfs_set_stack_inode_generation(inode_item, 1);
529 btrfs_set_stack_inode_size(inode_item, 3);
530 btrfs_set_stack_inode_nlink(inode_item, 1);
531 btrfs_set_stack_inode_nbytes(inode_item,
533 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
535 btrfs_set_root_flags(root_item, 0);
536 btrfs_set_root_limit(root_item, 0);
537 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
539 btrfs_set_root_bytenr(root_item, leaf->start);
540 btrfs_set_root_generation(root_item, trans->transid);
541 btrfs_set_root_level(root_item, 0);
542 btrfs_set_root_refs(root_item, 1);
543 btrfs_set_root_used(root_item, leaf->len);
544 btrfs_set_root_last_snapshot(root_item, 0);
546 btrfs_set_root_generation_v2(root_item,
547 btrfs_root_generation(root_item));
548 generate_random_guid(root_item->uuid);
549 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
550 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
551 root_item->ctime = root_item->otime;
552 btrfs_set_root_ctransid(root_item, trans->transid);
553 btrfs_set_root_otransid(root_item, trans->transid);
555 btrfs_tree_unlock(leaf);
557 btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
559 key.objectid = objectid;
561 key.type = BTRFS_ROOT_ITEM_KEY;
562 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
566 * Since we don't abort the transaction in this case, free the
567 * tree block so that we don't leak space and leave the
568 * filesystem in an inconsistent state (an extent item in the
569 * extent tree without backreferences). Also no need to have
570 * the tree block locked since it is not in any tree at this
571 * point, so no other task can find it and use it.
573 btrfs_free_tree_block(trans, root, leaf, 0, 1);
574 free_extent_buffer(leaf);
578 free_extent_buffer(leaf);
581 key.offset = (u64)-1;
582 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
583 if (IS_ERR(new_root)) {
584 free_anon_bdev(anon_dev);
585 ret = PTR_ERR(new_root);
586 btrfs_abort_transaction(trans, ret);
589 /* Freeing will be done in btrfs_put_root() of new_root */
592 ret = btrfs_record_root_in_trans(trans, new_root);
594 btrfs_put_root(new_root);
595 btrfs_abort_transaction(trans, ret);
599 ret = btrfs_create_subvol_root(trans, new_root, root);
600 btrfs_put_root(new_root);
602 /* We potentially lose an unused inode item here */
603 btrfs_abort_transaction(trans, ret);
608 * insert the directory item
610 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
612 btrfs_abort_transaction(trans, ret);
616 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
617 BTRFS_FT_DIR, index);
619 btrfs_abort_transaction(trans, ret);
623 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
624 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
626 btrfs_abort_transaction(trans, ret);
630 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
631 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
633 btrfs_abort_transaction(trans, ret);
637 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
638 BTRFS_UUID_KEY_SUBVOL, objectid);
640 btrfs_abort_transaction(trans, ret);
644 trans->block_rsv = NULL;
645 trans->bytes_reserved = 0;
646 btrfs_subvolume_release_metadata(root, &block_rsv);
648 err = btrfs_commit_transaction(trans);
653 inode = btrfs_lookup_dentry(dir, dentry);
655 return PTR_ERR(inode);
656 d_instantiate(dentry, inode);
662 free_anon_bdev(anon_dev);
667 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
668 struct dentry *dentry, bool readonly,
669 struct btrfs_qgroup_inherit *inherit)
671 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
673 struct btrfs_pending_snapshot *pending_snapshot;
674 struct btrfs_trans_handle *trans;
677 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
680 if (atomic_read(&root->nr_swapfiles)) {
682 "cannot snapshot subvolume with active swapfile");
686 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
687 if (!pending_snapshot)
690 ret = get_anon_bdev(&pending_snapshot->anon_dev);
693 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
695 pending_snapshot->path = btrfs_alloc_path();
696 if (!pending_snapshot->root_item || !pending_snapshot->path) {
701 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
702 BTRFS_BLOCK_RSV_TEMP);
704 * 1 - parent dir inode
707 * 2 - root ref/backref
708 * 1 - root of snapshot
711 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
712 &pending_snapshot->block_rsv, 8,
717 pending_snapshot->dentry = dentry;
718 pending_snapshot->root = root;
719 pending_snapshot->readonly = readonly;
720 pending_snapshot->dir = dir;
721 pending_snapshot->inherit = inherit;
723 trans = btrfs_start_transaction(root, 0);
725 ret = PTR_ERR(trans);
729 spin_lock(&fs_info->trans_lock);
730 list_add(&pending_snapshot->list,
731 &trans->transaction->pending_snapshots);
732 spin_unlock(&fs_info->trans_lock);
734 ret = btrfs_commit_transaction(trans);
738 ret = pending_snapshot->error;
742 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
746 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
748 ret = PTR_ERR(inode);
752 d_instantiate(dentry, inode);
754 pending_snapshot->anon_dev = 0;
756 /* Prevent double freeing of anon_dev */
757 if (ret && pending_snapshot->snap)
758 pending_snapshot->snap->anon_dev = 0;
759 btrfs_put_root(pending_snapshot->snap);
760 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
762 if (pending_snapshot->anon_dev)
763 free_anon_bdev(pending_snapshot->anon_dev);
764 kfree(pending_snapshot->root_item);
765 btrfs_free_path(pending_snapshot->path);
766 kfree(pending_snapshot);
771 /* copy of may_delete in fs/namei.c()
772 * Check whether we can remove a link victim from directory dir, check
773 * whether the type of victim is right.
774 * 1. We can't do it if dir is read-only (done in permission())
775 * 2. We should have write and exec permissions on dir
776 * 3. We can't remove anything from append-only dir
777 * 4. We can't do anything with immutable dir (done in permission())
778 * 5. If the sticky bit on dir is set we should either
779 * a. be owner of dir, or
780 * b. be owner of victim, or
781 * c. have CAP_FOWNER capability
782 * 6. If the victim is append-only or immutable we can't do anything with
783 * links pointing to it.
784 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
785 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
786 * 9. We can't remove a root or mountpoint.
787 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
788 * nfs_async_unlink().
791 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
795 if (d_really_is_negative(victim))
798 BUG_ON(d_inode(victim->d_parent) != dir);
799 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
801 error = inode_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
806 if (check_sticky(&init_user_ns, dir, d_inode(victim)) ||
807 IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) ||
808 IS_SWAPFILE(d_inode(victim)))
811 if (!d_is_dir(victim))
815 } else if (d_is_dir(victim))
819 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
824 /* copy of may_create in fs/namei.c() */
825 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
827 if (d_really_is_positive(child))
831 return inode_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
835 * Create a new subvolume below @parent. This is largely modeled after
836 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
837 * inside this filesystem so it's quite a bit simpler.
839 static noinline int btrfs_mksubvol(const struct path *parent,
840 const char *name, int namelen,
841 struct btrfs_root *snap_src,
843 struct btrfs_qgroup_inherit *inherit)
845 struct inode *dir = d_inode(parent->dentry);
846 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
847 struct dentry *dentry;
850 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
854 dentry = lookup_one_len(name, parent->dentry, namelen);
855 error = PTR_ERR(dentry);
859 error = btrfs_may_create(dir, dentry);
864 * even if this name doesn't exist, we may get hash collisions.
865 * check for them now when we can safely fail
867 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
873 down_read(&fs_info->subvol_sem);
875 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
879 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
881 error = create_subvol(dir, dentry, name, namelen, inherit);
884 fsnotify_mkdir(dir, dentry);
886 up_read(&fs_info->subvol_sem);
890 btrfs_inode_unlock(dir, 0);
894 static noinline int btrfs_mksnapshot(const struct path *parent,
895 const char *name, int namelen,
896 struct btrfs_root *root,
898 struct btrfs_qgroup_inherit *inherit)
901 bool snapshot_force_cow = false;
904 * Force new buffered writes to reserve space even when NOCOW is
905 * possible. This is to avoid later writeback (running dealloc) to
906 * fallback to COW mode and unexpectedly fail with ENOSPC.
908 btrfs_drew_read_lock(&root->snapshot_lock);
910 ret = btrfs_start_delalloc_snapshot(root);
915 * All previous writes have started writeback in NOCOW mode, so now
916 * we force future writes to fallback to COW mode during snapshot
919 atomic_inc(&root->snapshot_force_cow);
920 snapshot_force_cow = true;
922 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
924 ret = btrfs_mksubvol(parent, name, namelen,
925 root, readonly, inherit);
927 if (snapshot_force_cow)
928 atomic_dec(&root->snapshot_force_cow);
929 btrfs_drew_read_unlock(&root->snapshot_lock);
934 * When we're defragging a range, we don't want to kick it off again
935 * if it is really just waiting for delalloc to send it down.
936 * If we find a nice big extent or delalloc range for the bytes in the
937 * file you want to defrag, we return 0 to let you know to skip this
940 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
942 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
943 struct extent_map *em = NULL;
944 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
947 read_lock(&em_tree->lock);
948 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
949 read_unlock(&em_tree->lock);
952 end = extent_map_end(em);
954 if (end - offset > thresh)
957 /* if we already have a nice delalloc here, just stop */
959 end = count_range_bits(io_tree, &offset, offset + thresh,
960 thresh, EXTENT_DELALLOC, 1);
967 * helper function to walk through a file and find extents
968 * newer than a specific transid, and smaller than thresh.
970 * This is used by the defragging code to find new and small
973 static int find_new_extents(struct btrfs_root *root,
974 struct inode *inode, u64 newer_than,
975 u64 *off, u32 thresh)
977 struct btrfs_path *path;
978 struct btrfs_key min_key;
979 struct extent_buffer *leaf;
980 struct btrfs_file_extent_item *extent;
983 u64 ino = btrfs_ino(BTRFS_I(inode));
985 path = btrfs_alloc_path();
989 min_key.objectid = ino;
990 min_key.type = BTRFS_EXTENT_DATA_KEY;
991 min_key.offset = *off;
994 ret = btrfs_search_forward(root, &min_key, path, newer_than);
998 if (min_key.objectid != ino)
1000 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1003 leaf = path->nodes[0];
1004 extent = btrfs_item_ptr(leaf, path->slots[0],
1005 struct btrfs_file_extent_item);
1007 type = btrfs_file_extent_type(leaf, extent);
1008 if (type == BTRFS_FILE_EXTENT_REG &&
1009 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1010 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1011 *off = min_key.offset;
1012 btrfs_free_path(path);
1017 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1018 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1022 if (min_key.offset == (u64)-1)
1026 btrfs_release_path(path);
1029 btrfs_free_path(path);
1033 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1035 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1036 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1037 struct extent_map *em;
1038 u64 len = PAGE_SIZE;
1041 * hopefully we have this extent in the tree already, try without
1042 * the full extent lock
1044 read_lock(&em_tree->lock);
1045 em = lookup_extent_mapping(em_tree, start, len);
1046 read_unlock(&em_tree->lock);
1049 struct extent_state *cached = NULL;
1050 u64 end = start + len - 1;
1052 /* get the big lock and read metadata off disk */
1053 lock_extent_bits(io_tree, start, end, &cached);
1054 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
1055 unlock_extent_cached(io_tree, start, end, &cached);
1064 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1066 struct extent_map *next;
1069 /* this is the last extent */
1070 if (em->start + em->len >= i_size_read(inode))
1073 next = defrag_lookup_extent(inode, em->start + em->len);
1074 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1076 else if ((em->block_start + em->block_len == next->block_start) &&
1077 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1080 free_extent_map(next);
1084 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1085 u64 *last_len, u64 *skip, u64 *defrag_end,
1088 struct extent_map *em;
1090 bool next_mergeable = true;
1091 bool prev_mergeable = true;
1094 * make sure that once we start defragging an extent, we keep on
1097 if (start < *defrag_end)
1102 em = defrag_lookup_extent(inode, start);
1106 /* this will cover holes, and inline extents */
1107 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1113 prev_mergeable = false;
1115 next_mergeable = defrag_check_next_extent(inode, em);
1117 * we hit a real extent, if it is big or the next extent is not a
1118 * real extent, don't bother defragging it
1120 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1121 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1125 * last_len ends up being a counter of how many bytes we've defragged.
1126 * every time we choose not to defrag an extent, we reset *last_len
1127 * so that the next tiny extent will force a defrag.
1129 * The end result of this is that tiny extents before a single big
1130 * extent will force at least part of that big extent to be defragged.
1133 *defrag_end = extent_map_end(em);
1136 *skip = extent_map_end(em);
1140 free_extent_map(em);
1145 * it doesn't do much good to defrag one or two pages
1146 * at a time. This pulls in a nice chunk of pages
1147 * to COW and defrag.
1149 * It also makes sure the delalloc code has enough
1150 * dirty data to avoid making new small extents as part
1153 * It's a good idea to start RA on this range
1154 * before calling this.
1156 static int cluster_pages_for_defrag(struct inode *inode,
1157 struct page **pages,
1158 unsigned long start_index,
1159 unsigned long num_pages)
1161 unsigned long file_end;
1162 u64 isize = i_size_read(inode);
1166 u64 start = (u64)start_index << PAGE_SHIFT;
1171 struct btrfs_ordered_extent *ordered;
1172 struct extent_state *cached_state = NULL;
1173 struct extent_io_tree *tree;
1174 struct extent_changeset *data_reserved = NULL;
1175 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1177 file_end = (isize - 1) >> PAGE_SHIFT;
1178 if (!isize || start_index > file_end)
1181 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1183 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
1184 start, page_cnt << PAGE_SHIFT);
1188 tree = &BTRFS_I(inode)->io_tree;
1190 /* step one, lock all the pages */
1191 for (i = 0; i < page_cnt; i++) {
1194 page = find_or_create_page(inode->i_mapping,
1195 start_index + i, mask);
1199 ret = set_page_extent_mapped(page);
1206 page_start = page_offset(page);
1207 page_end = page_start + PAGE_SIZE - 1;
1209 lock_extent_bits(tree, page_start, page_end,
1211 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode),
1213 unlock_extent_cached(tree, page_start, page_end,
1219 btrfs_start_ordered_extent(ordered, 1);
1220 btrfs_put_ordered_extent(ordered);
1223 * we unlocked the page above, so we need check if
1224 * it was released or not.
1226 if (page->mapping != inode->i_mapping) {
1233 if (!PageUptodate(page)) {
1234 btrfs_readpage(NULL, page);
1236 if (!PageUptodate(page)) {
1244 if (page->mapping != inode->i_mapping) {
1256 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1260 * so now we have a nice long stream of locked
1261 * and up to date pages, lets wait on them
1263 for (i = 0; i < i_done; i++)
1264 wait_on_page_writeback(pages[i]);
1266 page_start = page_offset(pages[0]);
1267 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1269 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1270 page_start, page_end - 1, &cached_state);
1273 * When defragmenting we skip ranges that have holes or inline extents,
1274 * (check should_defrag_range()), to avoid unnecessary IO and wasting
1275 * space. At btrfs_defrag_file(), we check if a range should be defragged
1276 * before locking the inode and then, if it should, we trigger a sync
1277 * page cache readahead - we lock the inode only after that to avoid
1278 * blocking for too long other tasks that possibly want to operate on
1279 * other file ranges. But before we were able to get the inode lock,
1280 * some other task may have punched a hole in the range, or we may have
1281 * now an inline extent, in which case we should not defrag. So check
1282 * for that here, where we have the inode and the range locked, and bail
1283 * out if that happened.
1285 search_start = page_start;
1286 while (search_start < page_end) {
1287 struct extent_map *em;
1289 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, search_start,
1290 page_end - search_start);
1293 goto out_unlock_range;
1295 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1296 free_extent_map(em);
1297 /* Ok, 0 means we did not defrag anything */
1299 goto out_unlock_range;
1301 search_start = extent_map_end(em);
1302 free_extent_map(em);
1305 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1306 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1307 EXTENT_DEFRAG, 0, 0, &cached_state);
1309 if (i_done != page_cnt) {
1310 spin_lock(&BTRFS_I(inode)->lock);
1311 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1312 spin_unlock(&BTRFS_I(inode)->lock);
1313 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1314 start, (page_cnt - i_done) << PAGE_SHIFT, true);
1318 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1321 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1322 page_start, page_end - 1, &cached_state);
1324 for (i = 0; i < i_done; i++) {
1325 clear_page_dirty_for_io(pages[i]);
1326 ClearPageChecked(pages[i]);
1327 set_page_dirty(pages[i]);
1328 unlock_page(pages[i]);
1331 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1332 extent_changeset_free(data_reserved);
1336 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1337 page_start, page_end - 1, &cached_state);
1339 for (i = 0; i < i_done; i++) {
1340 unlock_page(pages[i]);
1343 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
1344 start, page_cnt << PAGE_SHIFT, true);
1345 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
1346 extent_changeset_free(data_reserved);
1351 int btrfs_defrag_file(struct inode *inode, struct file *file,
1352 struct btrfs_ioctl_defrag_range_args *range,
1353 u64 newer_than, unsigned long max_to_defrag)
1355 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1356 struct btrfs_root *root = BTRFS_I(inode)->root;
1357 struct file_ra_state *ra = NULL;
1358 unsigned long last_index;
1359 u64 isize = i_size_read(inode);
1363 u64 newer_off = range->start;
1365 unsigned long ra_index = 0;
1367 int defrag_count = 0;
1368 int compress_type = BTRFS_COMPRESS_ZLIB;
1369 u32 extent_thresh = range->extent_thresh;
1370 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1371 unsigned long cluster = max_cluster;
1372 u64 new_align = ~((u64)SZ_128K - 1);
1373 struct page **pages = NULL;
1374 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1379 if (range->start >= isize)
1383 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
1385 if (range->compress_type)
1386 compress_type = range->compress_type;
1389 if (extent_thresh == 0)
1390 extent_thresh = SZ_256K;
1393 * If we were not given a file, allocate a readahead context. As
1394 * readahead is just an optimization, defrag will work without it so
1395 * we don't error out.
1398 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1400 file_ra_state_init(ra, inode->i_mapping);
1405 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1411 /* find the last page to defrag */
1412 if (range->start + range->len > range->start) {
1413 last_index = min_t(u64, isize - 1,
1414 range->start + range->len - 1) >> PAGE_SHIFT;
1416 last_index = (isize - 1) >> PAGE_SHIFT;
1420 ret = find_new_extents(root, inode, newer_than,
1421 &newer_off, SZ_64K);
1423 range->start = newer_off;
1425 * we always align our defrag to help keep
1426 * the extents in the file evenly spaced
1428 i = (newer_off & new_align) >> PAGE_SHIFT;
1432 i = range->start >> PAGE_SHIFT;
1435 max_to_defrag = last_index - i + 1;
1438 * make writeback starts from i, so the defrag range can be
1439 * written sequentially.
1441 if (i < inode->i_mapping->writeback_index)
1442 inode->i_mapping->writeback_index = i;
1444 while (i <= last_index && defrag_count < max_to_defrag &&
1445 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1447 * make sure we stop running if someone unmounts
1450 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1453 if (btrfs_defrag_cancelled(fs_info)) {
1454 btrfs_debug(fs_info, "defrag_file cancelled");
1459 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1460 extent_thresh, &last_len, &skip,
1461 &defrag_end, do_compress)){
1464 * the should_defrag function tells us how much to skip
1465 * bump our counter by the suggested amount
1467 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1468 i = max(i + 1, next);
1473 cluster = (PAGE_ALIGN(defrag_end) >>
1475 cluster = min(cluster, max_cluster);
1477 cluster = max_cluster;
1480 if (i + cluster > ra_index) {
1481 ra_index = max(i, ra_index);
1483 page_cache_sync_readahead(inode->i_mapping, ra,
1484 file, ra_index, cluster);
1485 ra_index += cluster;
1488 btrfs_inode_lock(inode, 0);
1489 if (IS_SWAPFILE(inode)) {
1493 BTRFS_I(inode)->defrag_compress = compress_type;
1494 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1497 btrfs_inode_unlock(inode, 0);
1501 defrag_count += ret;
1502 balance_dirty_pages_ratelimited(inode->i_mapping);
1503 btrfs_inode_unlock(inode, 0);
1506 if (newer_off == (u64)-1)
1512 newer_off = max(newer_off + 1,
1513 (u64)i << PAGE_SHIFT);
1515 ret = find_new_extents(root, inode, newer_than,
1516 &newer_off, SZ_64K);
1518 range->start = newer_off;
1519 i = (newer_off & new_align) >> PAGE_SHIFT;
1526 last_len += ret << PAGE_SHIFT;
1534 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1535 filemap_flush(inode->i_mapping);
1536 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1537 &BTRFS_I(inode)->runtime_flags))
1538 filemap_flush(inode->i_mapping);
1541 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1542 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1543 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1544 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1551 btrfs_inode_lock(inode, 0);
1552 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1553 btrfs_inode_unlock(inode, 0);
1561 static noinline int btrfs_ioctl_resize(struct file *file,
1564 struct inode *inode = file_inode(file);
1565 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1569 struct btrfs_root *root = BTRFS_I(inode)->root;
1570 struct btrfs_ioctl_vol_args *vol_args;
1571 struct btrfs_trans_handle *trans;
1572 struct btrfs_device *device = NULL;
1575 char *devstr = NULL;
1579 if (!capable(CAP_SYS_ADMIN))
1582 ret = mnt_want_write_file(file);
1586 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_RESIZE)) {
1587 mnt_drop_write_file(file);
1588 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1591 vol_args = memdup_user(arg, sizeof(*vol_args));
1592 if (IS_ERR(vol_args)) {
1593 ret = PTR_ERR(vol_args);
1597 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1599 sizestr = vol_args->name;
1600 devstr = strchr(sizestr, ':');
1602 sizestr = devstr + 1;
1604 devstr = vol_args->name;
1605 ret = kstrtoull(devstr, 10, &devid);
1612 btrfs_info(fs_info, "resizing devid %llu", devid);
1615 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
1617 btrfs_info(fs_info, "resizer unable to find device %llu",
1623 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1625 "resizer unable to apply on readonly device %llu",
1631 if (!strcmp(sizestr, "max"))
1632 new_size = device->bdev->bd_inode->i_size;
1634 if (sizestr[0] == '-') {
1637 } else if (sizestr[0] == '+') {
1641 new_size = memparse(sizestr, &retptr);
1642 if (*retptr != '\0' || new_size == 0) {
1648 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1653 old_size = btrfs_device_get_total_bytes(device);
1656 if (new_size > old_size) {
1660 new_size = old_size - new_size;
1661 } else if (mod > 0) {
1662 if (new_size > ULLONG_MAX - old_size) {
1666 new_size = old_size + new_size;
1669 if (new_size < SZ_256M) {
1673 if (new_size > device->bdev->bd_inode->i_size) {
1678 new_size = round_down(new_size, fs_info->sectorsize);
1680 if (new_size > old_size) {
1681 trans = btrfs_start_transaction(root, 0);
1682 if (IS_ERR(trans)) {
1683 ret = PTR_ERR(trans);
1686 ret = btrfs_grow_device(trans, device, new_size);
1687 btrfs_commit_transaction(trans);
1688 } else if (new_size < old_size) {
1689 ret = btrfs_shrink_device(device, new_size);
1690 } /* equal, nothing need to do */
1692 if (ret == 0 && new_size != old_size)
1693 btrfs_info_in_rcu(fs_info,
1694 "resize device %s (devid %llu) from %llu to %llu",
1695 rcu_str_deref(device->name), device->devid,
1696 old_size, new_size);
1700 btrfs_exclop_finish(fs_info);
1701 mnt_drop_write_file(file);
1705 static noinline int __btrfs_ioctl_snap_create(struct file *file,
1706 const char *name, unsigned long fd, int subvol,
1708 struct btrfs_qgroup_inherit *inherit)
1713 if (!S_ISDIR(file_inode(file)->i_mode))
1716 ret = mnt_want_write_file(file);
1720 namelen = strlen(name);
1721 if (strchr(name, '/')) {
1723 goto out_drop_write;
1726 if (name[0] == '.' &&
1727 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1729 goto out_drop_write;
1733 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1734 NULL, readonly, inherit);
1736 struct fd src = fdget(fd);
1737 struct inode *src_inode;
1740 goto out_drop_write;
1743 src_inode = file_inode(src.file);
1744 if (src_inode->i_sb != file_inode(file)->i_sb) {
1745 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1746 "Snapshot src from another FS");
1748 } else if (!inode_owner_or_capable(&init_user_ns, src_inode)) {
1750 * Subvolume creation is not restricted, but snapshots
1751 * are limited to own subvolumes only
1755 ret = btrfs_mksnapshot(&file->f_path, name, namelen,
1756 BTRFS_I(src_inode)->root,
1762 mnt_drop_write_file(file);
1767 static noinline int btrfs_ioctl_snap_create(struct file *file,
1768 void __user *arg, int subvol)
1770 struct btrfs_ioctl_vol_args *vol_args;
1773 if (!S_ISDIR(file_inode(file)->i_mode))
1776 vol_args = memdup_user(arg, sizeof(*vol_args));
1777 if (IS_ERR(vol_args))
1778 return PTR_ERR(vol_args);
1779 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1781 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1782 subvol, false, NULL);
1788 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1789 void __user *arg, int subvol)
1791 struct btrfs_ioctl_vol_args_v2 *vol_args;
1793 bool readonly = false;
1794 struct btrfs_qgroup_inherit *inherit = NULL;
1796 if (!S_ISDIR(file_inode(file)->i_mode))
1799 vol_args = memdup_user(arg, sizeof(*vol_args));
1800 if (IS_ERR(vol_args))
1801 return PTR_ERR(vol_args);
1802 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1804 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
1809 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1811 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1814 if (vol_args->size < sizeof(*inherit) ||
1815 vol_args->size > PAGE_SIZE) {
1819 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1820 if (IS_ERR(inherit)) {
1821 ret = PTR_ERR(inherit);
1825 if (inherit->num_qgroups > PAGE_SIZE ||
1826 inherit->num_ref_copies > PAGE_SIZE ||
1827 inherit->num_excl_copies > PAGE_SIZE) {
1832 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1833 2 * inherit->num_excl_copies;
1834 if (vol_args->size != struct_size(inherit, qgroups, nums)) {
1840 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1841 subvol, readonly, inherit);
1851 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1854 struct inode *inode = file_inode(file);
1855 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1856 struct btrfs_root *root = BTRFS_I(inode)->root;
1860 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1863 down_read(&fs_info->subvol_sem);
1864 if (btrfs_root_readonly(root))
1865 flags |= BTRFS_SUBVOL_RDONLY;
1866 up_read(&fs_info->subvol_sem);
1868 if (copy_to_user(arg, &flags, sizeof(flags)))
1874 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1877 struct inode *inode = file_inode(file);
1878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1879 struct btrfs_root *root = BTRFS_I(inode)->root;
1880 struct btrfs_trans_handle *trans;
1885 if (!inode_owner_or_capable(&init_user_ns, inode))
1888 ret = mnt_want_write_file(file);
1892 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1894 goto out_drop_write;
1897 if (copy_from_user(&flags, arg, sizeof(flags))) {
1899 goto out_drop_write;
1902 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1904 goto out_drop_write;
1907 down_write(&fs_info->subvol_sem);
1910 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1913 root_flags = btrfs_root_flags(&root->root_item);
1914 if (flags & BTRFS_SUBVOL_RDONLY) {
1915 btrfs_set_root_flags(&root->root_item,
1916 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1919 * Block RO -> RW transition if this subvolume is involved in
1922 spin_lock(&root->root_item_lock);
1923 if (root->send_in_progress == 0) {
1924 btrfs_set_root_flags(&root->root_item,
1925 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1926 spin_unlock(&root->root_item_lock);
1928 spin_unlock(&root->root_item_lock);
1930 "Attempt to set subvolume %llu read-write during send",
1931 root->root_key.objectid);
1937 trans = btrfs_start_transaction(root, 1);
1938 if (IS_ERR(trans)) {
1939 ret = PTR_ERR(trans);
1943 ret = btrfs_update_root(trans, fs_info->tree_root,
1944 &root->root_key, &root->root_item);
1946 btrfs_end_transaction(trans);
1950 ret = btrfs_commit_transaction(trans);
1954 btrfs_set_root_flags(&root->root_item, root_flags);
1956 up_write(&fs_info->subvol_sem);
1958 mnt_drop_write_file(file);
1963 static noinline int key_in_sk(struct btrfs_key *key,
1964 struct btrfs_ioctl_search_key *sk)
1966 struct btrfs_key test;
1969 test.objectid = sk->min_objectid;
1970 test.type = sk->min_type;
1971 test.offset = sk->min_offset;
1973 ret = btrfs_comp_cpu_keys(key, &test);
1977 test.objectid = sk->max_objectid;
1978 test.type = sk->max_type;
1979 test.offset = sk->max_offset;
1981 ret = btrfs_comp_cpu_keys(key, &test);
1987 static noinline int copy_to_sk(struct btrfs_path *path,
1988 struct btrfs_key *key,
1989 struct btrfs_ioctl_search_key *sk,
1992 unsigned long *sk_offset,
1996 struct extent_buffer *leaf;
1997 struct btrfs_ioctl_search_header sh;
1998 struct btrfs_key test;
1999 unsigned long item_off;
2000 unsigned long item_len;
2006 leaf = path->nodes[0];
2007 slot = path->slots[0];
2008 nritems = btrfs_header_nritems(leaf);
2010 if (btrfs_header_generation(leaf) > sk->max_transid) {
2014 found_transid = btrfs_header_generation(leaf);
2016 for (i = slot; i < nritems; i++) {
2017 item_off = btrfs_item_ptr_offset(leaf, i);
2018 item_len = btrfs_item_size_nr(leaf, i);
2020 btrfs_item_key_to_cpu(leaf, key, i);
2021 if (!key_in_sk(key, sk))
2024 if (sizeof(sh) + item_len > *buf_size) {
2031 * return one empty item back for v1, which does not
2035 *buf_size = sizeof(sh) + item_len;
2040 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2045 sh.objectid = key->objectid;
2046 sh.offset = key->offset;
2047 sh.type = key->type;
2049 sh.transid = found_transid;
2052 * Copy search result header. If we fault then loop again so we
2053 * can fault in the pages and -EFAULT there if there's a
2054 * problem. Otherwise we'll fault and then copy the buffer in
2055 * properly this next time through
2057 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2062 *sk_offset += sizeof(sh);
2065 char __user *up = ubuf + *sk_offset;
2067 * Copy the item, same behavior as above, but reset the
2068 * * sk_offset so we copy the full thing again.
2070 if (read_extent_buffer_to_user_nofault(leaf, up,
2071 item_off, item_len)) {
2073 *sk_offset -= sizeof(sh);
2077 *sk_offset += item_len;
2081 if (ret) /* -EOVERFLOW from above */
2084 if (*num_found >= sk->nr_items) {
2091 test.objectid = sk->max_objectid;
2092 test.type = sk->max_type;
2093 test.offset = sk->max_offset;
2094 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2096 else if (key->offset < (u64)-1)
2098 else if (key->type < (u8)-1) {
2101 } else if (key->objectid < (u64)-1) {
2109 * 0: all items from this leaf copied, continue with next
2110 * 1: * more items can be copied, but unused buffer is too small
2111 * * all items were found
2112 * Either way, it will stops the loop which iterates to the next
2114 * -EOVERFLOW: item was to large for buffer
2115 * -EFAULT: could not copy extent buffer back to userspace
2120 static noinline int search_ioctl(struct inode *inode,
2121 struct btrfs_ioctl_search_key *sk,
2125 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2126 struct btrfs_root *root;
2127 struct btrfs_key key;
2128 struct btrfs_path *path;
2131 unsigned long sk_offset = 0;
2133 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2134 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2138 path = btrfs_alloc_path();
2142 if (sk->tree_id == 0) {
2143 /* search the root of the inode that was passed */
2144 root = btrfs_grab_root(BTRFS_I(inode)->root);
2146 root = btrfs_get_fs_root(info, sk->tree_id, true);
2148 btrfs_free_path(path);
2149 return PTR_ERR(root);
2153 key.objectid = sk->min_objectid;
2154 key.type = sk->min_type;
2155 key.offset = sk->min_offset;
2158 ret = fault_in_pages_writeable(ubuf + sk_offset,
2159 *buf_size - sk_offset);
2163 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2169 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2170 &sk_offset, &num_found);
2171 btrfs_release_path(path);
2179 sk->nr_items = num_found;
2180 btrfs_put_root(root);
2181 btrfs_free_path(path);
2185 static noinline int btrfs_ioctl_tree_search(struct file *file,
2188 struct btrfs_ioctl_search_args __user *uargs;
2189 struct btrfs_ioctl_search_key sk;
2190 struct inode *inode;
2194 if (!capable(CAP_SYS_ADMIN))
2197 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2199 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2202 buf_size = sizeof(uargs->buf);
2204 inode = file_inode(file);
2205 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2208 * In the origin implementation an overflow is handled by returning a
2209 * search header with a len of zero, so reset ret.
2211 if (ret == -EOVERFLOW)
2214 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2219 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2222 struct btrfs_ioctl_search_args_v2 __user *uarg;
2223 struct btrfs_ioctl_search_args_v2 args;
2224 struct inode *inode;
2227 const size_t buf_limit = SZ_16M;
2229 if (!capable(CAP_SYS_ADMIN))
2232 /* copy search header and buffer size */
2233 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2234 if (copy_from_user(&args, uarg, sizeof(args)))
2237 buf_size = args.buf_size;
2239 /* limit result size to 16MB */
2240 if (buf_size > buf_limit)
2241 buf_size = buf_limit;
2243 inode = file_inode(file);
2244 ret = search_ioctl(inode, &args.key, &buf_size,
2245 (char __user *)(&uarg->buf[0]));
2246 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2248 else if (ret == -EOVERFLOW &&
2249 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2256 * Search INODE_REFs to identify path name of 'dirid' directory
2257 * in a 'tree_id' tree. and sets path name to 'name'.
2259 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2260 u64 tree_id, u64 dirid, char *name)
2262 struct btrfs_root *root;
2263 struct btrfs_key key;
2269 struct btrfs_inode_ref *iref;
2270 struct extent_buffer *l;
2271 struct btrfs_path *path;
2273 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2278 path = btrfs_alloc_path();
2282 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2284 root = btrfs_get_fs_root(info, tree_id, true);
2286 ret = PTR_ERR(root);
2291 key.objectid = dirid;
2292 key.type = BTRFS_INODE_REF_KEY;
2293 key.offset = (u64)-1;
2296 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2300 ret = btrfs_previous_item(root, path, dirid,
2301 BTRFS_INODE_REF_KEY);
2311 slot = path->slots[0];
2312 btrfs_item_key_to_cpu(l, &key, slot);
2314 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2315 len = btrfs_inode_ref_name_len(l, iref);
2317 total_len += len + 1;
2319 ret = -ENAMETOOLONG;
2324 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2326 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2329 btrfs_release_path(path);
2330 key.objectid = key.offset;
2331 key.offset = (u64)-1;
2332 dirid = key.objectid;
2334 memmove(name, ptr, total_len);
2335 name[total_len] = '\0';
2338 btrfs_put_root(root);
2339 btrfs_free_path(path);
2343 static int btrfs_search_path_in_tree_user(struct inode *inode,
2344 struct btrfs_ioctl_ino_lookup_user_args *args)
2346 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2347 struct super_block *sb = inode->i_sb;
2348 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2349 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2350 u64 dirid = args->dirid;
2351 unsigned long item_off;
2352 unsigned long item_len;
2353 struct btrfs_inode_ref *iref;
2354 struct btrfs_root_ref *rref;
2355 struct btrfs_root *root = NULL;
2356 struct btrfs_path *path;
2357 struct btrfs_key key, key2;
2358 struct extent_buffer *leaf;
2359 struct inode *temp_inode;
2366 path = btrfs_alloc_path();
2371 * If the bottom subvolume does not exist directly under upper_limit,
2372 * construct the path in from the bottom up.
2374 if (dirid != upper_limit.objectid) {
2375 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2377 root = btrfs_get_fs_root(fs_info, treeid, true);
2379 ret = PTR_ERR(root);
2383 key.objectid = dirid;
2384 key.type = BTRFS_INODE_REF_KEY;
2385 key.offset = (u64)-1;
2387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2390 } else if (ret > 0) {
2391 ret = btrfs_previous_item(root, path, dirid,
2392 BTRFS_INODE_REF_KEY);
2395 } else if (ret > 0) {
2401 leaf = path->nodes[0];
2402 slot = path->slots[0];
2403 btrfs_item_key_to_cpu(leaf, &key, slot);
2405 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2406 len = btrfs_inode_ref_name_len(leaf, iref);
2408 total_len += len + 1;
2409 if (ptr < args->path) {
2410 ret = -ENAMETOOLONG;
2415 read_extent_buffer(leaf, ptr,
2416 (unsigned long)(iref + 1), len);
2418 /* Check the read+exec permission of this directory */
2419 ret = btrfs_previous_item(root, path, dirid,
2420 BTRFS_INODE_ITEM_KEY);
2423 } else if (ret > 0) {
2428 leaf = path->nodes[0];
2429 slot = path->slots[0];
2430 btrfs_item_key_to_cpu(leaf, &key2, slot);
2431 if (key2.objectid != dirid) {
2436 temp_inode = btrfs_iget(sb, key2.objectid, root);
2437 if (IS_ERR(temp_inode)) {
2438 ret = PTR_ERR(temp_inode);
2441 ret = inode_permission(&init_user_ns, temp_inode,
2442 MAY_READ | MAY_EXEC);
2449 if (key.offset == upper_limit.objectid)
2451 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2456 btrfs_release_path(path);
2457 key.objectid = key.offset;
2458 key.offset = (u64)-1;
2459 dirid = key.objectid;
2462 memmove(args->path, ptr, total_len);
2463 args->path[total_len] = '\0';
2464 btrfs_put_root(root);
2466 btrfs_release_path(path);
2469 /* Get the bottom subvolume's name from ROOT_REF */
2470 key.objectid = treeid;
2471 key.type = BTRFS_ROOT_REF_KEY;
2472 key.offset = args->treeid;
2473 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2476 } else if (ret > 0) {
2481 leaf = path->nodes[0];
2482 slot = path->slots[0];
2483 btrfs_item_key_to_cpu(leaf, &key, slot);
2485 item_off = btrfs_item_ptr_offset(leaf, slot);
2486 item_len = btrfs_item_size_nr(leaf, slot);
2487 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2488 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2489 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2494 /* Copy subvolume's name */
2495 item_off += sizeof(struct btrfs_root_ref);
2496 item_len -= sizeof(struct btrfs_root_ref);
2497 read_extent_buffer(leaf, args->name, item_off, item_len);
2498 args->name[item_len] = 0;
2501 btrfs_put_root(root);
2503 btrfs_free_path(path);
2507 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2510 struct btrfs_ioctl_ino_lookup_args *args;
2511 struct inode *inode;
2514 args = memdup_user(argp, sizeof(*args));
2516 return PTR_ERR(args);
2518 inode = file_inode(file);
2521 * Unprivileged query to obtain the containing subvolume root id. The
2522 * path is reset so it's consistent with btrfs_search_path_in_tree.
2524 if (args->treeid == 0)
2525 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2527 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2532 if (!capable(CAP_SYS_ADMIN)) {
2537 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2538 args->treeid, args->objectid,
2542 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2550 * Version of ino_lookup ioctl (unprivileged)
2552 * The main differences from ino_lookup ioctl are:
2554 * 1. Read + Exec permission will be checked using inode_permission() during
2555 * path construction. -EACCES will be returned in case of failure.
2556 * 2. Path construction will be stopped at the inode number which corresponds
2557 * to the fd with which this ioctl is called. If constructed path does not
2558 * exist under fd's inode, -EACCES will be returned.
2559 * 3. The name of bottom subvolume is also searched and filled.
2561 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2563 struct btrfs_ioctl_ino_lookup_user_args *args;
2564 struct inode *inode;
2567 args = memdup_user(argp, sizeof(*args));
2569 return PTR_ERR(args);
2571 inode = file_inode(file);
2573 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2574 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2576 * The subvolume does not exist under fd with which this is
2583 ret = btrfs_search_path_in_tree_user(inode, args);
2585 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2592 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2593 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2595 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2596 struct btrfs_fs_info *fs_info;
2597 struct btrfs_root *root;
2598 struct btrfs_path *path;
2599 struct btrfs_key key;
2600 struct btrfs_root_item *root_item;
2601 struct btrfs_root_ref *rref;
2602 struct extent_buffer *leaf;
2603 unsigned long item_off;
2604 unsigned long item_len;
2605 struct inode *inode;
2609 path = btrfs_alloc_path();
2613 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2615 btrfs_free_path(path);
2619 inode = file_inode(file);
2620 fs_info = BTRFS_I(inode)->root->fs_info;
2622 /* Get root_item of inode's subvolume */
2623 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2624 root = btrfs_get_fs_root(fs_info, key.objectid, true);
2626 ret = PTR_ERR(root);
2629 root_item = &root->root_item;
2631 subvol_info->treeid = key.objectid;
2633 subvol_info->generation = btrfs_root_generation(root_item);
2634 subvol_info->flags = btrfs_root_flags(root_item);
2636 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2637 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2639 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2642 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2643 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2644 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2646 subvol_info->otransid = btrfs_root_otransid(root_item);
2647 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2648 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2650 subvol_info->stransid = btrfs_root_stransid(root_item);
2651 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2652 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2654 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2655 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2656 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2658 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2659 /* Search root tree for ROOT_BACKREF of this subvolume */
2660 key.type = BTRFS_ROOT_BACKREF_KEY;
2662 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2665 } else if (path->slots[0] >=
2666 btrfs_header_nritems(path->nodes[0])) {
2667 ret = btrfs_next_leaf(fs_info->tree_root, path);
2670 } else if (ret > 0) {
2676 leaf = path->nodes[0];
2677 slot = path->slots[0];
2678 btrfs_item_key_to_cpu(leaf, &key, slot);
2679 if (key.objectid == subvol_info->treeid &&
2680 key.type == BTRFS_ROOT_BACKREF_KEY) {
2681 subvol_info->parent_id = key.offset;
2683 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2684 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2686 item_off = btrfs_item_ptr_offset(leaf, slot)
2687 + sizeof(struct btrfs_root_ref);
2688 item_len = btrfs_item_size_nr(leaf, slot)
2689 - sizeof(struct btrfs_root_ref);
2690 read_extent_buffer(leaf, subvol_info->name,
2691 item_off, item_len);
2698 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2702 btrfs_put_root(root);
2704 btrfs_free_path(path);
2710 * Return ROOT_REF information of the subvolume containing this inode
2711 * except the subvolume name.
2713 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2715 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2716 struct btrfs_root_ref *rref;
2717 struct btrfs_root *root;
2718 struct btrfs_path *path;
2719 struct btrfs_key key;
2720 struct extent_buffer *leaf;
2721 struct inode *inode;
2727 path = btrfs_alloc_path();
2731 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2732 if (IS_ERR(rootrefs)) {
2733 btrfs_free_path(path);
2734 return PTR_ERR(rootrefs);
2737 inode = file_inode(file);
2738 root = BTRFS_I(inode)->root->fs_info->tree_root;
2739 objectid = BTRFS_I(inode)->root->root_key.objectid;
2741 key.objectid = objectid;
2742 key.type = BTRFS_ROOT_REF_KEY;
2743 key.offset = rootrefs->min_treeid;
2746 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2749 } else if (path->slots[0] >=
2750 btrfs_header_nritems(path->nodes[0])) {
2751 ret = btrfs_next_leaf(root, path);
2754 } else if (ret > 0) {
2760 leaf = path->nodes[0];
2761 slot = path->slots[0];
2763 btrfs_item_key_to_cpu(leaf, &key, slot);
2764 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2769 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2774 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2775 rootrefs->rootref[found].treeid = key.offset;
2776 rootrefs->rootref[found].dirid =
2777 btrfs_root_ref_dirid(leaf, rref);
2780 ret = btrfs_next_item(root, path);
2783 } else if (ret > 0) {
2790 if (!ret || ret == -EOVERFLOW) {
2791 rootrefs->num_items = found;
2792 /* update min_treeid for next search */
2794 rootrefs->min_treeid =
2795 rootrefs->rootref[found - 1].treeid + 1;
2796 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2801 btrfs_free_path(path);
2806 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2810 struct dentry *parent = file->f_path.dentry;
2811 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2812 struct dentry *dentry;
2813 struct inode *dir = d_inode(parent);
2814 struct inode *inode;
2815 struct btrfs_root *root = BTRFS_I(dir)->root;
2816 struct btrfs_root *dest = NULL;
2817 struct btrfs_ioctl_vol_args *vol_args = NULL;
2818 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2819 char *subvol_name, *subvol_name_ptr = NULL;
2822 bool destroy_parent = false;
2825 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2826 if (IS_ERR(vol_args2))
2827 return PTR_ERR(vol_args2);
2829 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2835 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2836 * name, same as v1 currently does.
2838 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2839 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2840 subvol_name = vol_args2->name;
2842 err = mnt_want_write_file(file);
2846 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2851 err = mnt_want_write_file(file);
2855 dentry = btrfs_get_dentry(fs_info->sb,
2856 BTRFS_FIRST_FREE_OBJECTID,
2857 vol_args2->subvolid, 0, 0);
2858 if (IS_ERR(dentry)) {
2859 err = PTR_ERR(dentry);
2860 goto out_drop_write;
2864 * Change the default parent since the subvolume being
2865 * deleted can be outside of the current mount point.
2867 parent = btrfs_get_parent(dentry);
2870 * At this point dentry->d_name can point to '/' if the
2871 * subvolume we want to destroy is outsite of the
2872 * current mount point, so we need to release the
2873 * current dentry and execute the lookup to return a new
2874 * one with ->d_name pointing to the
2875 * <mount point>/subvol_name.
2878 if (IS_ERR(parent)) {
2879 err = PTR_ERR(parent);
2880 goto out_drop_write;
2882 dir = d_inode(parent);
2885 * If v2 was used with SPEC_BY_ID, a new parent was
2886 * allocated since the subvolume can be outside of the
2887 * current mount point. Later on we need to release this
2888 * new parent dentry.
2890 destroy_parent = true;
2892 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2893 fs_info, vol_args2->subvolid);
2894 if (IS_ERR(subvol_name_ptr)) {
2895 err = PTR_ERR(subvol_name_ptr);
2898 /* subvol_name_ptr is already NULL termined */
2899 subvol_name = (char *)kbasename(subvol_name_ptr);
2902 vol_args = memdup_user(arg, sizeof(*vol_args));
2903 if (IS_ERR(vol_args))
2904 return PTR_ERR(vol_args);
2906 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
2907 subvol_name = vol_args->name;
2909 err = mnt_want_write_file(file);
2914 subvol_namelen = strlen(subvol_name);
2916 if (strchr(subvol_name, '/') ||
2917 strncmp(subvol_name, "..", subvol_namelen) == 0) {
2919 goto free_subvol_name;
2922 if (!S_ISDIR(dir->i_mode)) {
2924 goto free_subvol_name;
2927 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2929 goto free_subvol_name;
2930 dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
2931 if (IS_ERR(dentry)) {
2932 err = PTR_ERR(dentry);
2933 goto out_unlock_dir;
2936 if (d_really_is_negative(dentry)) {
2941 inode = d_inode(dentry);
2942 dest = BTRFS_I(inode)->root;
2943 if (!capable(CAP_SYS_ADMIN)) {
2945 * Regular user. Only allow this with a special mount
2946 * option, when the user has write+exec access to the
2947 * subvol root, and when rmdir(2) would have been
2950 * Note that this is _not_ check that the subvol is
2951 * empty or doesn't contain data that we wouldn't
2952 * otherwise be able to delete.
2954 * Users who want to delete empty subvols should try
2958 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
2962 * Do not allow deletion if the parent dir is the same
2963 * as the dir to be deleted. That means the ioctl
2964 * must be called on the dentry referencing the root
2965 * of the subvol, not a random directory contained
2972 err = inode_permission(&init_user_ns, inode,
2973 MAY_WRITE | MAY_EXEC);
2978 /* check if subvolume may be deleted by a user */
2979 err = btrfs_may_delete(dir, dentry, 1);
2983 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2988 btrfs_inode_lock(inode, 0);
2989 err = btrfs_delete_subvolume(dir, dentry);
2990 btrfs_inode_unlock(inode, 0);
2992 fsnotify_rmdir(dir, dentry);
2999 btrfs_inode_unlock(dir, 0);
3001 kfree(subvol_name_ptr);
3006 mnt_drop_write_file(file);
3013 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
3015 struct inode *inode = file_inode(file);
3016 struct btrfs_root *root = BTRFS_I(inode)->root;
3017 struct btrfs_ioctl_defrag_range_args *range;
3020 ret = mnt_want_write_file(file);
3024 if (btrfs_root_readonly(root)) {
3029 switch (inode->i_mode & S_IFMT) {
3031 if (!capable(CAP_SYS_ADMIN)) {
3035 ret = btrfs_defrag_root(root);
3039 * Note that this does not check the file descriptor for write
3040 * access. This prevents defragmenting executables that are
3041 * running and allows defrag on files open in read-only mode.
3043 if (!capable(CAP_SYS_ADMIN) &&
3044 inode_permission(&init_user_ns, inode, MAY_WRITE)) {
3049 range = kzalloc(sizeof(*range), GFP_KERNEL);
3056 if (copy_from_user(range, argp,
3062 /* compression requires us to start the IO */
3063 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3064 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3065 range->extent_thresh = (u32)-1;
3068 /* the rest are all set to zero by kzalloc */
3069 range->len = (u64)-1;
3071 ret = btrfs_defrag_file(file_inode(file), file,
3072 range, BTRFS_OLDEST_GENERATION, 0);
3081 mnt_drop_write_file(file);
3085 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3087 struct btrfs_ioctl_vol_args *vol_args;
3090 if (!capable(CAP_SYS_ADMIN))
3093 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
3094 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3096 vol_args = memdup_user(arg, sizeof(*vol_args));
3097 if (IS_ERR(vol_args)) {
3098 ret = PTR_ERR(vol_args);
3102 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3103 ret = btrfs_init_new_device(fs_info, vol_args->name);
3106 btrfs_info(fs_info, "disk added %s", vol_args->name);
3110 btrfs_exclop_finish(fs_info);
3114 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3116 struct inode *inode = file_inode(file);
3117 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3118 struct btrfs_ioctl_vol_args_v2 *vol_args;
3121 if (!capable(CAP_SYS_ADMIN))
3124 ret = mnt_want_write_file(file);
3128 vol_args = memdup_user(arg, sizeof(*vol_args));
3129 if (IS_ERR(vol_args)) {
3130 ret = PTR_ERR(vol_args);
3134 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
3139 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3140 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3144 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3145 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3147 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3148 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3150 btrfs_exclop_finish(fs_info);
3153 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3154 btrfs_info(fs_info, "device deleted: id %llu",
3157 btrfs_info(fs_info, "device deleted: %s",
3163 mnt_drop_write_file(file);
3167 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3169 struct inode *inode = file_inode(file);
3170 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3171 struct btrfs_ioctl_vol_args *vol_args;
3174 if (!capable(CAP_SYS_ADMIN))
3177 ret = mnt_want_write_file(file);
3181 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
3182 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3183 goto out_drop_write;
3186 vol_args = memdup_user(arg, sizeof(*vol_args));
3187 if (IS_ERR(vol_args)) {
3188 ret = PTR_ERR(vol_args);
3192 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3193 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3196 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3199 btrfs_exclop_finish(fs_info);
3201 mnt_drop_write_file(file);
3206 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3209 struct btrfs_ioctl_fs_info_args *fi_args;
3210 struct btrfs_device *device;
3211 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3215 fi_args = memdup_user(arg, sizeof(*fi_args));
3216 if (IS_ERR(fi_args))
3217 return PTR_ERR(fi_args);
3219 flags_in = fi_args->flags;
3220 memset(fi_args, 0, sizeof(*fi_args));
3223 fi_args->num_devices = fs_devices->num_devices;
3225 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3226 if (device->devid > fi_args->max_id)
3227 fi_args->max_id = device->devid;
3231 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3232 fi_args->nodesize = fs_info->nodesize;
3233 fi_args->sectorsize = fs_info->sectorsize;
3234 fi_args->clone_alignment = fs_info->sectorsize;
3236 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3237 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3238 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3239 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3242 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3243 fi_args->generation = fs_info->generation;
3244 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3247 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3248 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3249 sizeof(fi_args->metadata_uuid));
3250 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3253 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3260 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3263 struct btrfs_ioctl_dev_info_args *di_args;
3264 struct btrfs_device *dev;
3266 char *s_uuid = NULL;
3268 di_args = memdup_user(arg, sizeof(*di_args));
3269 if (IS_ERR(di_args))
3270 return PTR_ERR(di_args);
3272 if (!btrfs_is_empty_uuid(di_args->uuid))
3273 s_uuid = di_args->uuid;
3276 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3284 di_args->devid = dev->devid;
3285 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3286 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3287 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3289 strncpy(di_args->path, rcu_str_deref(dev->name),
3290 sizeof(di_args->path) - 1);
3291 di_args->path[sizeof(di_args->path) - 1] = 0;
3293 di_args->path[0] = '\0';
3298 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3305 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3307 struct inode *inode = file_inode(file);
3308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3309 struct btrfs_root *root = BTRFS_I(inode)->root;
3310 struct btrfs_root *new_root;
3311 struct btrfs_dir_item *di;
3312 struct btrfs_trans_handle *trans;
3313 struct btrfs_path *path = NULL;
3314 struct btrfs_disk_key disk_key;
3319 if (!capable(CAP_SYS_ADMIN))
3322 ret = mnt_want_write_file(file);
3326 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3332 objectid = BTRFS_FS_TREE_OBJECTID;
3334 new_root = btrfs_get_fs_root(fs_info, objectid, true);
3335 if (IS_ERR(new_root)) {
3336 ret = PTR_ERR(new_root);
3339 if (!is_fstree(new_root->root_key.objectid)) {
3344 path = btrfs_alloc_path();
3350 trans = btrfs_start_transaction(root, 1);
3351 if (IS_ERR(trans)) {
3352 ret = PTR_ERR(trans);
3356 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3357 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
3358 dir_id, "default", 7, 1);
3359 if (IS_ERR_OR_NULL(di)) {
3360 btrfs_release_path(path);
3361 btrfs_end_transaction(trans);
3363 "Umm, you don't have the default diritem, this isn't going to work");
3368 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3369 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3370 btrfs_mark_buffer_dirty(path->nodes[0]);
3371 btrfs_release_path(path);
3373 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
3374 btrfs_end_transaction(trans);
3376 btrfs_put_root(new_root);
3377 btrfs_free_path(path);
3379 mnt_drop_write_file(file);
3383 static void get_block_group_info(struct list_head *groups_list,
3384 struct btrfs_ioctl_space_info *space)
3386 struct btrfs_block_group *block_group;
3388 space->total_bytes = 0;
3389 space->used_bytes = 0;
3391 list_for_each_entry(block_group, groups_list, list) {
3392 space->flags = block_group->flags;
3393 space->total_bytes += block_group->length;
3394 space->used_bytes += block_group->used;
3398 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3401 struct btrfs_ioctl_space_args space_args;
3402 struct btrfs_ioctl_space_info space;
3403 struct btrfs_ioctl_space_info *dest;
3404 struct btrfs_ioctl_space_info *dest_orig;
3405 struct btrfs_ioctl_space_info __user *user_dest;
3406 struct btrfs_space_info *info;
3407 static const u64 types[] = {
3408 BTRFS_BLOCK_GROUP_DATA,
3409 BTRFS_BLOCK_GROUP_SYSTEM,
3410 BTRFS_BLOCK_GROUP_METADATA,
3411 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3419 if (copy_from_user(&space_args,
3420 (struct btrfs_ioctl_space_args __user *)arg,
3421 sizeof(space_args)))
3424 for (i = 0; i < num_types; i++) {
3425 struct btrfs_space_info *tmp;
3428 list_for_each_entry(tmp, &fs_info->space_info, list) {
3429 if (tmp->flags == types[i]) {
3438 down_read(&info->groups_sem);
3439 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3440 if (!list_empty(&info->block_groups[c]))
3443 up_read(&info->groups_sem);
3447 * Global block reserve, exported as a space_info
3451 /* space_slots == 0 means they are asking for a count */
3452 if (space_args.space_slots == 0) {
3453 space_args.total_spaces = slot_count;
3457 slot_count = min_t(u64, space_args.space_slots, slot_count);
3459 alloc_size = sizeof(*dest) * slot_count;
3461 /* we generally have at most 6 or so space infos, one for each raid
3462 * level. So, a whole page should be more than enough for everyone
3464 if (alloc_size > PAGE_SIZE)
3467 space_args.total_spaces = 0;
3468 dest = kmalloc(alloc_size, GFP_KERNEL);
3473 /* now we have a buffer to copy into */
3474 for (i = 0; i < num_types; i++) {
3475 struct btrfs_space_info *tmp;
3481 list_for_each_entry(tmp, &fs_info->space_info, list) {
3482 if (tmp->flags == types[i]) {
3490 down_read(&info->groups_sem);
3491 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3492 if (!list_empty(&info->block_groups[c])) {
3493 get_block_group_info(&info->block_groups[c],
3495 memcpy(dest, &space, sizeof(space));
3497 space_args.total_spaces++;
3503 up_read(&info->groups_sem);
3507 * Add global block reserve
3510 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3512 spin_lock(&block_rsv->lock);
3513 space.total_bytes = block_rsv->size;
3514 space.used_bytes = block_rsv->size - block_rsv->reserved;
3515 spin_unlock(&block_rsv->lock);
3516 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3517 memcpy(dest, &space, sizeof(space));
3518 space_args.total_spaces++;
3521 user_dest = (struct btrfs_ioctl_space_info __user *)
3522 (arg + sizeof(struct btrfs_ioctl_space_args));
3524 if (copy_to_user(user_dest, dest_orig, alloc_size))
3529 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3535 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3538 struct btrfs_trans_handle *trans;
3542 trans = btrfs_attach_transaction_barrier(root);
3543 if (IS_ERR(trans)) {
3544 if (PTR_ERR(trans) != -ENOENT)
3545 return PTR_ERR(trans);
3547 /* No running transaction, don't bother */
3548 transid = root->fs_info->last_trans_committed;
3551 transid = trans->transid;
3552 ret = btrfs_commit_transaction_async(trans, 0);
3554 btrfs_end_transaction(trans);
3559 if (copy_to_user(argp, &transid, sizeof(transid)))
3564 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
3570 if (copy_from_user(&transid, argp, sizeof(transid)))
3573 transid = 0; /* current trans */
3575 return btrfs_wait_for_commit(fs_info, transid);
3578 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3580 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
3581 struct btrfs_ioctl_scrub_args *sa;
3584 if (!capable(CAP_SYS_ADMIN))
3587 sa = memdup_user(arg, sizeof(*sa));
3591 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3592 ret = mnt_want_write_file(file);
3597 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
3598 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3602 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3603 * error. This is important as it allows user space to know how much
3604 * progress scrub has done. For example, if scrub is canceled we get
3605 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3606 * space. Later user space can inspect the progress from the structure
3607 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3608 * previously (btrfs-progs does this).
3609 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3610 * then return -EFAULT to signal the structure was not copied or it may
3611 * be corrupt and unreliable due to a partial copy.
3613 if (copy_to_user(arg, sa, sizeof(*sa)))
3616 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3617 mnt_drop_write_file(file);
3623 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
3625 if (!capable(CAP_SYS_ADMIN))
3628 return btrfs_scrub_cancel(fs_info);
3631 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
3634 struct btrfs_ioctl_scrub_args *sa;
3637 if (!capable(CAP_SYS_ADMIN))
3640 sa = memdup_user(arg, sizeof(*sa));
3644 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
3646 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3653 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
3656 struct btrfs_ioctl_get_dev_stats *sa;
3659 sa = memdup_user(arg, sizeof(*sa));
3663 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3668 ret = btrfs_get_dev_stats(fs_info, sa);
3670 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3677 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3680 struct btrfs_ioctl_dev_replace_args *p;
3683 if (!capable(CAP_SYS_ADMIN))
3686 p = memdup_user(arg, sizeof(*p));
3691 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3692 if (sb_rdonly(fs_info->sb)) {
3696 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
3697 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3699 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
3700 btrfs_exclop_finish(fs_info);
3703 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3704 btrfs_dev_replace_status(fs_info, p);
3707 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3708 p->result = btrfs_dev_replace_cancel(fs_info);
3716 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
3723 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3729 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3730 struct inode_fs_paths *ipath = NULL;
3731 struct btrfs_path *path;
3733 if (!capable(CAP_DAC_READ_SEARCH))
3736 path = btrfs_alloc_path();
3742 ipa = memdup_user(arg, sizeof(*ipa));
3749 size = min_t(u32, ipa->size, 4096);
3750 ipath = init_ipath(size, root, path);
3751 if (IS_ERR(ipath)) {
3752 ret = PTR_ERR(ipath);
3757 ret = paths_from_inode(ipa->inum, ipath);
3761 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3762 rel_ptr = ipath->fspath->val[i] -
3763 (u64)(unsigned long)ipath->fspath->val;
3764 ipath->fspath->val[i] = rel_ptr;
3767 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3768 ipath->fspath, size);
3775 btrfs_free_path(path);
3782 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3784 struct btrfs_data_container *inodes = ctx;
3785 const size_t c = 3 * sizeof(u64);
3787 if (inodes->bytes_left >= c) {
3788 inodes->bytes_left -= c;
3789 inodes->val[inodes->elem_cnt] = inum;
3790 inodes->val[inodes->elem_cnt + 1] = offset;
3791 inodes->val[inodes->elem_cnt + 2] = root;
3792 inodes->elem_cnt += 3;
3794 inodes->bytes_missing += c - inodes->bytes_left;
3795 inodes->bytes_left = 0;
3796 inodes->elem_missed += 3;
3802 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
3803 void __user *arg, int version)
3807 struct btrfs_ioctl_logical_ino_args *loi;
3808 struct btrfs_data_container *inodes = NULL;
3809 struct btrfs_path *path = NULL;
3812 if (!capable(CAP_SYS_ADMIN))
3815 loi = memdup_user(arg, sizeof(*loi));
3817 return PTR_ERR(loi);
3820 ignore_offset = false;
3821 size = min_t(u32, loi->size, SZ_64K);
3823 /* All reserved bits must be 0 for now */
3824 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3828 /* Only accept flags we have defined so far */
3829 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3833 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
3834 size = min_t(u32, loi->size, SZ_16M);
3837 path = btrfs_alloc_path();
3843 inodes = init_data_container(size);
3844 if (IS_ERR(inodes)) {
3845 ret = PTR_ERR(inodes);
3850 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
3851 build_ino_list, inodes, ignore_offset);
3857 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3863 btrfs_free_path(path);
3871 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3872 struct btrfs_ioctl_balance_args *bargs)
3874 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3876 bargs->flags = bctl->flags;
3878 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
3879 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3880 if (atomic_read(&fs_info->balance_pause_req))
3881 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3882 if (atomic_read(&fs_info->balance_cancel_req))
3883 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3885 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3886 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3887 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3889 spin_lock(&fs_info->balance_lock);
3890 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3891 spin_unlock(&fs_info->balance_lock);
3894 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3896 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3897 struct btrfs_fs_info *fs_info = root->fs_info;
3898 struct btrfs_ioctl_balance_args *bargs;
3899 struct btrfs_balance_control *bctl;
3900 bool need_unlock; /* for mut. excl. ops lock */
3903 if (!capable(CAP_SYS_ADMIN))
3906 ret = mnt_want_write_file(file);
3911 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
3912 mutex_lock(&fs_info->balance_mutex);
3918 * mut. excl. ops lock is locked. Three possibilities:
3919 * (1) some other op is running
3920 * (2) balance is running
3921 * (3) balance is paused -- special case (think resume)
3923 mutex_lock(&fs_info->balance_mutex);
3924 if (fs_info->balance_ctl) {
3925 /* this is either (2) or (3) */
3926 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
3927 mutex_unlock(&fs_info->balance_mutex);
3929 * Lock released to allow other waiters to continue,
3930 * we'll reexamine the status again.
3932 mutex_lock(&fs_info->balance_mutex);
3934 if (fs_info->balance_ctl &&
3935 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
3937 need_unlock = false;
3941 mutex_unlock(&fs_info->balance_mutex);
3945 mutex_unlock(&fs_info->balance_mutex);
3951 mutex_unlock(&fs_info->balance_mutex);
3952 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3959 bargs = memdup_user(arg, sizeof(*bargs));
3960 if (IS_ERR(bargs)) {
3961 ret = PTR_ERR(bargs);
3965 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3966 if (!fs_info->balance_ctl) {
3971 bctl = fs_info->balance_ctl;
3972 spin_lock(&fs_info->balance_lock);
3973 bctl->flags |= BTRFS_BALANCE_RESUME;
3974 spin_unlock(&fs_info->balance_lock);
3982 if (fs_info->balance_ctl) {
3987 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
3994 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3995 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3996 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3998 bctl->flags = bargs->flags;
4000 /* balance everything - no filters */
4001 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4004 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4011 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4012 * bctl is freed in reset_balance_state, or, if restriper was paused
4013 * all the way until unmount, in free_fs_info. The flag should be
4014 * cleared after reset_balance_state.
4016 need_unlock = false;
4018 ret = btrfs_balance(fs_info, bctl, bargs);
4021 if ((ret == 0 || ret == -ECANCELED) && arg) {
4022 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4031 mutex_unlock(&fs_info->balance_mutex);
4033 btrfs_exclop_finish(fs_info);
4035 mnt_drop_write_file(file);
4039 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4041 if (!capable(CAP_SYS_ADMIN))
4045 case BTRFS_BALANCE_CTL_PAUSE:
4046 return btrfs_pause_balance(fs_info);
4047 case BTRFS_BALANCE_CTL_CANCEL:
4048 return btrfs_cancel_balance(fs_info);
4054 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4057 struct btrfs_ioctl_balance_args *bargs;
4060 if (!capable(CAP_SYS_ADMIN))
4063 mutex_lock(&fs_info->balance_mutex);
4064 if (!fs_info->balance_ctl) {
4069 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4075 btrfs_update_ioctl_balance_args(fs_info, bargs);
4077 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4082 mutex_unlock(&fs_info->balance_mutex);
4086 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4088 struct inode *inode = file_inode(file);
4089 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4090 struct btrfs_ioctl_quota_ctl_args *sa;
4093 if (!capable(CAP_SYS_ADMIN))
4096 ret = mnt_want_write_file(file);
4100 sa = memdup_user(arg, sizeof(*sa));
4106 down_write(&fs_info->subvol_sem);
4109 case BTRFS_QUOTA_CTL_ENABLE:
4110 ret = btrfs_quota_enable(fs_info);
4112 case BTRFS_QUOTA_CTL_DISABLE:
4113 ret = btrfs_quota_disable(fs_info);
4121 up_write(&fs_info->subvol_sem);
4123 mnt_drop_write_file(file);
4127 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4129 struct inode *inode = file_inode(file);
4130 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4131 struct btrfs_root *root = BTRFS_I(inode)->root;
4132 struct btrfs_ioctl_qgroup_assign_args *sa;
4133 struct btrfs_trans_handle *trans;
4137 if (!capable(CAP_SYS_ADMIN))
4140 ret = mnt_want_write_file(file);
4144 sa = memdup_user(arg, sizeof(*sa));
4150 trans = btrfs_join_transaction(root);
4151 if (IS_ERR(trans)) {
4152 ret = PTR_ERR(trans);
4157 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4159 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4162 /* update qgroup status and info */
4163 err = btrfs_run_qgroups(trans);
4165 btrfs_handle_fs_error(fs_info, err,
4166 "failed to update qgroup status and info");
4167 err = btrfs_end_transaction(trans);
4174 mnt_drop_write_file(file);
4178 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4180 struct inode *inode = file_inode(file);
4181 struct btrfs_root *root = BTRFS_I(inode)->root;
4182 struct btrfs_ioctl_qgroup_create_args *sa;
4183 struct btrfs_trans_handle *trans;
4187 if (!capable(CAP_SYS_ADMIN))
4190 ret = mnt_want_write_file(file);
4194 sa = memdup_user(arg, sizeof(*sa));
4200 if (!sa->qgroupid) {
4205 trans = btrfs_join_transaction(root);
4206 if (IS_ERR(trans)) {
4207 ret = PTR_ERR(trans);
4212 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4214 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4217 err = btrfs_end_transaction(trans);
4224 mnt_drop_write_file(file);
4228 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4230 struct inode *inode = file_inode(file);
4231 struct btrfs_root *root = BTRFS_I(inode)->root;
4232 struct btrfs_ioctl_qgroup_limit_args *sa;
4233 struct btrfs_trans_handle *trans;
4238 if (!capable(CAP_SYS_ADMIN))
4241 ret = mnt_want_write_file(file);
4245 sa = memdup_user(arg, sizeof(*sa));
4251 trans = btrfs_join_transaction(root);
4252 if (IS_ERR(trans)) {
4253 ret = PTR_ERR(trans);
4257 qgroupid = sa->qgroupid;
4259 /* take the current subvol as qgroup */
4260 qgroupid = root->root_key.objectid;
4263 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
4265 err = btrfs_end_transaction(trans);
4272 mnt_drop_write_file(file);
4276 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4278 struct inode *inode = file_inode(file);
4279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4280 struct btrfs_ioctl_quota_rescan_args *qsa;
4283 if (!capable(CAP_SYS_ADMIN))
4286 ret = mnt_want_write_file(file);
4290 qsa = memdup_user(arg, sizeof(*qsa));
4301 ret = btrfs_qgroup_rescan(fs_info);
4306 mnt_drop_write_file(file);
4310 static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4313 struct btrfs_ioctl_quota_rescan_args *qsa;
4316 if (!capable(CAP_SYS_ADMIN))
4319 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
4323 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4325 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
4328 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4335 static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4338 if (!capable(CAP_SYS_ADMIN))
4341 return btrfs_qgroup_wait_for_completion(fs_info, true);
4344 static long _btrfs_ioctl_set_received_subvol(struct file *file,
4345 struct btrfs_ioctl_received_subvol_args *sa)
4347 struct inode *inode = file_inode(file);
4348 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4349 struct btrfs_root *root = BTRFS_I(inode)->root;
4350 struct btrfs_root_item *root_item = &root->root_item;
4351 struct btrfs_trans_handle *trans;
4352 struct timespec64 ct = current_time(inode);
4354 int received_uuid_changed;
4356 if (!inode_owner_or_capable(&init_user_ns, inode))
4359 ret = mnt_want_write_file(file);
4363 down_write(&fs_info->subvol_sem);
4365 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
4370 if (btrfs_root_readonly(root)) {
4377 * 2 - uuid items (received uuid + subvol uuid)
4379 trans = btrfs_start_transaction(root, 3);
4380 if (IS_ERR(trans)) {
4381 ret = PTR_ERR(trans);
4386 sa->rtransid = trans->transid;
4387 sa->rtime.sec = ct.tv_sec;
4388 sa->rtime.nsec = ct.tv_nsec;
4390 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4392 if (received_uuid_changed &&
4393 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4394 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
4395 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4396 root->root_key.objectid);
4397 if (ret && ret != -ENOENT) {
4398 btrfs_abort_transaction(trans, ret);
4399 btrfs_end_transaction(trans);
4403 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4404 btrfs_set_root_stransid(root_item, sa->stransid);
4405 btrfs_set_root_rtransid(root_item, sa->rtransid);
4406 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4407 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4408 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4409 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4411 ret = btrfs_update_root(trans, fs_info->tree_root,
4412 &root->root_key, &root->root_item);
4414 btrfs_end_transaction(trans);
4417 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4418 ret = btrfs_uuid_tree_add(trans, sa->uuid,
4419 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4420 root->root_key.objectid);
4421 if (ret < 0 && ret != -EEXIST) {
4422 btrfs_abort_transaction(trans, ret);
4423 btrfs_end_transaction(trans);
4427 ret = btrfs_commit_transaction(trans);
4429 up_write(&fs_info->subvol_sem);
4430 mnt_drop_write_file(file);
4435 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4438 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4439 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4442 args32 = memdup_user(arg, sizeof(*args32));
4444 return PTR_ERR(args32);
4446 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
4452 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4453 args64->stransid = args32->stransid;
4454 args64->rtransid = args32->rtransid;
4455 args64->stime.sec = args32->stime.sec;
4456 args64->stime.nsec = args32->stime.nsec;
4457 args64->rtime.sec = args32->rtime.sec;
4458 args64->rtime.nsec = args32->rtime.nsec;
4459 args64->flags = args32->flags;
4461 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4465 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4466 args32->stransid = args64->stransid;
4467 args32->rtransid = args64->rtransid;
4468 args32->stime.sec = args64->stime.sec;
4469 args32->stime.nsec = args64->stime.nsec;
4470 args32->rtime.sec = args64->rtime.sec;
4471 args32->rtime.nsec = args64->rtime.nsec;
4472 args32->flags = args64->flags;
4474 ret = copy_to_user(arg, args32, sizeof(*args32));
4485 static long btrfs_ioctl_set_received_subvol(struct file *file,
4488 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4491 sa = memdup_user(arg, sizeof(*sa));
4495 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4500 ret = copy_to_user(arg, sa, sizeof(*sa));
4509 static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4514 char label[BTRFS_LABEL_SIZE];
4516 spin_lock(&fs_info->super_lock);
4517 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4518 spin_unlock(&fs_info->super_lock);
4520 len = strnlen(label, BTRFS_LABEL_SIZE);
4522 if (len == BTRFS_LABEL_SIZE) {
4524 "label is too long, return the first %zu bytes",
4528 ret = copy_to_user(arg, label, len);
4530 return ret ? -EFAULT : 0;
4533 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4535 struct inode *inode = file_inode(file);
4536 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4537 struct btrfs_root *root = BTRFS_I(inode)->root;
4538 struct btrfs_super_block *super_block = fs_info->super_copy;
4539 struct btrfs_trans_handle *trans;
4540 char label[BTRFS_LABEL_SIZE];
4543 if (!capable(CAP_SYS_ADMIN))
4546 if (copy_from_user(label, arg, sizeof(label)))
4549 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4551 "unable to set label with more than %d bytes",
4552 BTRFS_LABEL_SIZE - 1);
4556 ret = mnt_want_write_file(file);
4560 trans = btrfs_start_transaction(root, 0);
4561 if (IS_ERR(trans)) {
4562 ret = PTR_ERR(trans);
4566 spin_lock(&fs_info->super_lock);
4567 strcpy(super_block->label, label);
4568 spin_unlock(&fs_info->super_lock);
4569 ret = btrfs_commit_transaction(trans);
4572 mnt_drop_write_file(file);
4576 #define INIT_FEATURE_FLAGS(suffix) \
4577 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4578 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4579 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4581 int btrfs_ioctl_get_supported_features(void __user *arg)
4583 static const struct btrfs_ioctl_feature_flags features[3] = {
4584 INIT_FEATURE_FLAGS(SUPP),
4585 INIT_FEATURE_FLAGS(SAFE_SET),
4586 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4589 if (copy_to_user(arg, &features, sizeof(features)))
4595 static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4598 struct btrfs_super_block *super_block = fs_info->super_copy;
4599 struct btrfs_ioctl_feature_flags features;
4601 features.compat_flags = btrfs_super_compat_flags(super_block);
4602 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4603 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4605 if (copy_to_user(arg, &features, sizeof(features)))
4611 static int check_feature_bits(struct btrfs_fs_info *fs_info,
4612 enum btrfs_feature_set set,
4613 u64 change_mask, u64 flags, u64 supported_flags,
4614 u64 safe_set, u64 safe_clear)
4616 const char *type = btrfs_feature_set_name(set);
4618 u64 disallowed, unsupported;
4619 u64 set_mask = flags & change_mask;
4620 u64 clear_mask = ~flags & change_mask;
4622 unsupported = set_mask & ~supported_flags;
4624 names = btrfs_printable_features(set, unsupported);
4627 "this kernel does not support the %s feature bit%s",
4628 names, strchr(names, ',') ? "s" : "");
4632 "this kernel does not support %s bits 0x%llx",
4637 disallowed = set_mask & ~safe_set;
4639 names = btrfs_printable_features(set, disallowed);
4642 "can't set the %s feature bit%s while mounted",
4643 names, strchr(names, ',') ? "s" : "");
4647 "can't set %s bits 0x%llx while mounted",
4652 disallowed = clear_mask & ~safe_clear;
4654 names = btrfs_printable_features(set, disallowed);
4657 "can't clear the %s feature bit%s while mounted",
4658 names, strchr(names, ',') ? "s" : "");
4662 "can't clear %s bits 0x%llx while mounted",
4670 #define check_feature(fs_info, change_mask, flags, mask_base) \
4671 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
4672 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4673 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4674 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4676 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4678 struct inode *inode = file_inode(file);
4679 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4680 struct btrfs_root *root = BTRFS_I(inode)->root;
4681 struct btrfs_super_block *super_block = fs_info->super_copy;
4682 struct btrfs_ioctl_feature_flags flags[2];
4683 struct btrfs_trans_handle *trans;
4687 if (!capable(CAP_SYS_ADMIN))
4690 if (copy_from_user(flags, arg, sizeof(flags)))
4694 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4695 !flags[0].incompat_flags)
4698 ret = check_feature(fs_info, flags[0].compat_flags,
4699 flags[1].compat_flags, COMPAT);
4703 ret = check_feature(fs_info, flags[0].compat_ro_flags,
4704 flags[1].compat_ro_flags, COMPAT_RO);
4708 ret = check_feature(fs_info, flags[0].incompat_flags,
4709 flags[1].incompat_flags, INCOMPAT);
4713 ret = mnt_want_write_file(file);
4717 trans = btrfs_start_transaction(root, 0);
4718 if (IS_ERR(trans)) {
4719 ret = PTR_ERR(trans);
4720 goto out_drop_write;
4723 spin_lock(&fs_info->super_lock);
4724 newflags = btrfs_super_compat_flags(super_block);
4725 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4726 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4727 btrfs_set_super_compat_flags(super_block, newflags);
4729 newflags = btrfs_super_compat_ro_flags(super_block);
4730 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4731 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4732 btrfs_set_super_compat_ro_flags(super_block, newflags);
4734 newflags = btrfs_super_incompat_flags(super_block);
4735 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4736 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4737 btrfs_set_super_incompat_flags(super_block, newflags);
4738 spin_unlock(&fs_info->super_lock);
4740 ret = btrfs_commit_transaction(trans);
4742 mnt_drop_write_file(file);
4747 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4749 struct btrfs_ioctl_send_args *arg;
4753 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4754 struct btrfs_ioctl_send_args_32 args32;
4756 ret = copy_from_user(&args32, argp, sizeof(args32));
4759 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4762 arg->send_fd = args32.send_fd;
4763 arg->clone_sources_count = args32.clone_sources_count;
4764 arg->clone_sources = compat_ptr(args32.clone_sources);
4765 arg->parent_root = args32.parent_root;
4766 arg->flags = args32.flags;
4767 memcpy(arg->reserved, args32.reserved,
4768 sizeof(args32.reserved));
4773 arg = memdup_user(argp, sizeof(*arg));
4775 return PTR_ERR(arg);
4777 ret = btrfs_ioctl_send(file, arg);
4782 long btrfs_ioctl(struct file *file, unsigned int
4783 cmd, unsigned long arg)
4785 struct inode *inode = file_inode(file);
4786 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4787 struct btrfs_root *root = BTRFS_I(inode)->root;
4788 void __user *argp = (void __user *)arg;
4791 case FS_IOC_GETVERSION:
4792 return btrfs_ioctl_getversion(file, argp);
4793 case FS_IOC_GETFSLABEL:
4794 return btrfs_ioctl_get_fslabel(fs_info, argp);
4795 case FS_IOC_SETFSLABEL:
4796 return btrfs_ioctl_set_fslabel(file, argp);
4798 return btrfs_ioctl_fitrim(fs_info, argp);
4799 case BTRFS_IOC_SNAP_CREATE:
4800 return btrfs_ioctl_snap_create(file, argp, 0);
4801 case BTRFS_IOC_SNAP_CREATE_V2:
4802 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4803 case BTRFS_IOC_SUBVOL_CREATE:
4804 return btrfs_ioctl_snap_create(file, argp, 1);
4805 case BTRFS_IOC_SUBVOL_CREATE_V2:
4806 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4807 case BTRFS_IOC_SNAP_DESTROY:
4808 return btrfs_ioctl_snap_destroy(file, argp, false);
4809 case BTRFS_IOC_SNAP_DESTROY_V2:
4810 return btrfs_ioctl_snap_destroy(file, argp, true);
4811 case BTRFS_IOC_SUBVOL_GETFLAGS:
4812 return btrfs_ioctl_subvol_getflags(file, argp);
4813 case BTRFS_IOC_SUBVOL_SETFLAGS:
4814 return btrfs_ioctl_subvol_setflags(file, argp);
4815 case BTRFS_IOC_DEFAULT_SUBVOL:
4816 return btrfs_ioctl_default_subvol(file, argp);
4817 case BTRFS_IOC_DEFRAG:
4818 return btrfs_ioctl_defrag(file, NULL);
4819 case BTRFS_IOC_DEFRAG_RANGE:
4820 return btrfs_ioctl_defrag(file, argp);
4821 case BTRFS_IOC_RESIZE:
4822 return btrfs_ioctl_resize(file, argp);
4823 case BTRFS_IOC_ADD_DEV:
4824 return btrfs_ioctl_add_dev(fs_info, argp);
4825 case BTRFS_IOC_RM_DEV:
4826 return btrfs_ioctl_rm_dev(file, argp);
4827 case BTRFS_IOC_RM_DEV_V2:
4828 return btrfs_ioctl_rm_dev_v2(file, argp);
4829 case BTRFS_IOC_FS_INFO:
4830 return btrfs_ioctl_fs_info(fs_info, argp);
4831 case BTRFS_IOC_DEV_INFO:
4832 return btrfs_ioctl_dev_info(fs_info, argp);
4833 case BTRFS_IOC_BALANCE:
4834 return btrfs_ioctl_balance(file, NULL);
4835 case BTRFS_IOC_TREE_SEARCH:
4836 return btrfs_ioctl_tree_search(file, argp);
4837 case BTRFS_IOC_TREE_SEARCH_V2:
4838 return btrfs_ioctl_tree_search_v2(file, argp);
4839 case BTRFS_IOC_INO_LOOKUP:
4840 return btrfs_ioctl_ino_lookup(file, argp);
4841 case BTRFS_IOC_INO_PATHS:
4842 return btrfs_ioctl_ino_to_path(root, argp);
4843 case BTRFS_IOC_LOGICAL_INO:
4844 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4845 case BTRFS_IOC_LOGICAL_INO_V2:
4846 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
4847 case BTRFS_IOC_SPACE_INFO:
4848 return btrfs_ioctl_space_info(fs_info, argp);
4849 case BTRFS_IOC_SYNC: {
4852 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
4855 ret = btrfs_sync_fs(inode->i_sb, 1);
4857 * The transaction thread may want to do more work,
4858 * namely it pokes the cleaner kthread that will start
4859 * processing uncleaned subvols.
4861 wake_up_process(fs_info->transaction_kthread);
4864 case BTRFS_IOC_START_SYNC:
4865 return btrfs_ioctl_start_sync(root, argp);
4866 case BTRFS_IOC_WAIT_SYNC:
4867 return btrfs_ioctl_wait_sync(fs_info, argp);
4868 case BTRFS_IOC_SCRUB:
4869 return btrfs_ioctl_scrub(file, argp);
4870 case BTRFS_IOC_SCRUB_CANCEL:
4871 return btrfs_ioctl_scrub_cancel(fs_info);
4872 case BTRFS_IOC_SCRUB_PROGRESS:
4873 return btrfs_ioctl_scrub_progress(fs_info, argp);
4874 case BTRFS_IOC_BALANCE_V2:
4875 return btrfs_ioctl_balance(file, argp);
4876 case BTRFS_IOC_BALANCE_CTL:
4877 return btrfs_ioctl_balance_ctl(fs_info, arg);
4878 case BTRFS_IOC_BALANCE_PROGRESS:
4879 return btrfs_ioctl_balance_progress(fs_info, argp);
4880 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4881 return btrfs_ioctl_set_received_subvol(file, argp);
4883 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4884 return btrfs_ioctl_set_received_subvol_32(file, argp);
4886 case BTRFS_IOC_SEND:
4887 return _btrfs_ioctl_send(file, argp, false);
4888 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4889 case BTRFS_IOC_SEND_32:
4890 return _btrfs_ioctl_send(file, argp, true);
4892 case BTRFS_IOC_GET_DEV_STATS:
4893 return btrfs_ioctl_get_dev_stats(fs_info, argp);
4894 case BTRFS_IOC_QUOTA_CTL:
4895 return btrfs_ioctl_quota_ctl(file, argp);
4896 case BTRFS_IOC_QGROUP_ASSIGN:
4897 return btrfs_ioctl_qgroup_assign(file, argp);
4898 case BTRFS_IOC_QGROUP_CREATE:
4899 return btrfs_ioctl_qgroup_create(file, argp);
4900 case BTRFS_IOC_QGROUP_LIMIT:
4901 return btrfs_ioctl_qgroup_limit(file, argp);
4902 case BTRFS_IOC_QUOTA_RESCAN:
4903 return btrfs_ioctl_quota_rescan(file, argp);
4904 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4905 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
4906 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4907 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
4908 case BTRFS_IOC_DEV_REPLACE:
4909 return btrfs_ioctl_dev_replace(fs_info, argp);
4910 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
4911 return btrfs_ioctl_get_supported_features(argp);
4912 case BTRFS_IOC_GET_FEATURES:
4913 return btrfs_ioctl_get_features(fs_info, argp);
4914 case BTRFS_IOC_SET_FEATURES:
4915 return btrfs_ioctl_set_features(file, argp);
4916 case BTRFS_IOC_GET_SUBVOL_INFO:
4917 return btrfs_ioctl_get_subvol_info(file, argp);
4918 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
4919 return btrfs_ioctl_get_subvol_rootref(file, argp);
4920 case BTRFS_IOC_INO_LOOKUP_USER:
4921 return btrfs_ioctl_ino_lookup_user(file, argp);
4927 #ifdef CONFIG_COMPAT
4928 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4931 * These all access 32-bit values anyway so no further
4932 * handling is necessary.
4935 case FS_IOC32_GETVERSION:
4936 cmd = FS_IOC_GETVERSION;
4940 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));