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>
31 #include "transaction.h"
32 #include "btrfs_inode.h"
33 #include "print-tree.h"
36 #include "inode-map.h"
38 #include "rcu-string.h"
40 #include "dev-replace.h"
45 #include "compression.h"
48 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
49 * structures are incorrect, as the timespec structure from userspace
50 * is 4 bytes too small. We define these alternatives here to teach
51 * the kernel about the 32-bit struct packing.
53 struct btrfs_ioctl_timespec_32 {
56 } __attribute__ ((__packed__));
58 struct btrfs_ioctl_received_subvol_args_32 {
59 char uuid[BTRFS_UUID_SIZE]; /* in */
60 __u64 stransid; /* in */
61 __u64 rtransid; /* out */
62 struct btrfs_ioctl_timespec_32 stime; /* in */
63 struct btrfs_ioctl_timespec_32 rtime; /* out */
65 __u64 reserved[16]; /* in */
66 } __attribute__ ((__packed__));
68 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
69 struct btrfs_ioctl_received_subvol_args_32)
72 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
73 struct btrfs_ioctl_send_args_32 {
74 __s64 send_fd; /* in */
75 __u64 clone_sources_count; /* in */
76 compat_uptr_t clone_sources; /* in */
77 __u64 parent_root; /* in */
79 __u64 reserved[4]; /* in */
80 } __attribute__ ((__packed__));
82 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
83 struct btrfs_ioctl_send_args_32)
86 static int btrfs_clone(struct inode *src, struct inode *inode,
87 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
90 /* Mask out flags that are inappropriate for the given type of inode. */
91 static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
94 if (S_ISDIR(inode->i_mode))
96 else if (S_ISREG(inode->i_mode))
97 return flags & ~FS_DIRSYNC_FL;
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
106 static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
108 unsigned int iflags = 0;
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
125 if (flags & BTRFS_INODE_NOCOMPRESS)
126 iflags |= FS_NOCOMP_FL;
127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
134 * Update inode->i_flags based on the btrfs internal flags.
136 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
138 struct btrfs_inode *binode = BTRFS_I(inode);
139 unsigned int new_fl = 0;
141 if (binode->flags & BTRFS_INODE_SYNC)
143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
144 new_fl |= S_IMMUTABLE;
145 if (binode->flags & BTRFS_INODE_APPEND)
147 if (binode->flags & BTRFS_INODE_NOATIME)
149 if (binode->flags & BTRFS_INODE_DIRSYNC)
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
157 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
162 if (copy_to_user(arg, &flags, sizeof(flags)))
167 /* Check if @flags are a supported and valid set of FS_*_FL flags */
168 static int check_fsflags(unsigned int flags)
170 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
171 FS_NOATIME_FL | FS_NODUMP_FL | \
172 FS_SYNC_FL | FS_DIRSYNC_FL | \
173 FS_NOCOMP_FL | FS_COMPR_FL |
177 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
183 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
185 struct inode *inode = file_inode(file);
186 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
187 struct btrfs_inode *binode = BTRFS_I(inode);
188 struct btrfs_root *root = binode->root;
189 struct btrfs_trans_handle *trans;
190 unsigned int fsflags;
192 const char *comp = NULL;
193 u32 binode_flags = binode->flags;
195 if (!inode_owner_or_capable(inode))
198 if (btrfs_root_readonly(root))
201 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
204 ret = check_fsflags(fsflags);
208 ret = mnt_want_write_file(file);
214 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
215 if ((fsflags ^ btrfs_inode_flags_to_fsflags(binode->flags)) &
216 (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
217 if (!capable(CAP_LINUX_IMMUTABLE)) {
223 if (fsflags & FS_SYNC_FL)
224 binode_flags |= BTRFS_INODE_SYNC;
226 binode_flags &= ~BTRFS_INODE_SYNC;
227 if (fsflags & FS_IMMUTABLE_FL)
228 binode_flags |= BTRFS_INODE_IMMUTABLE;
230 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
231 if (fsflags & FS_APPEND_FL)
232 binode_flags |= BTRFS_INODE_APPEND;
234 binode_flags &= ~BTRFS_INODE_APPEND;
235 if (fsflags & FS_NODUMP_FL)
236 binode_flags |= BTRFS_INODE_NODUMP;
238 binode_flags &= ~BTRFS_INODE_NODUMP;
239 if (fsflags & FS_NOATIME_FL)
240 binode_flags |= BTRFS_INODE_NOATIME;
242 binode_flags &= ~BTRFS_INODE_NOATIME;
243 if (fsflags & FS_DIRSYNC_FL)
244 binode_flags |= BTRFS_INODE_DIRSYNC;
246 binode_flags &= ~BTRFS_INODE_DIRSYNC;
247 if (fsflags & FS_NOCOW_FL) {
248 if (S_ISREG(inode->i_mode)) {
250 * It's safe to turn csums off here, no extents exist.
251 * Otherwise we want the flag to reflect the real COW
252 * status of the file and will not set it.
254 if (inode->i_size == 0)
255 binode_flags |= BTRFS_INODE_NODATACOW |
256 BTRFS_INODE_NODATASUM;
258 binode_flags |= BTRFS_INODE_NODATACOW;
262 * Revert back under same assumptions as above
264 if (S_ISREG(inode->i_mode)) {
265 if (inode->i_size == 0)
266 binode_flags &= ~(BTRFS_INODE_NODATACOW |
267 BTRFS_INODE_NODATASUM);
269 binode_flags &= ~BTRFS_INODE_NODATACOW;
274 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
275 * flag may be changed automatically if compression code won't make
278 if (fsflags & FS_NOCOMP_FL) {
279 binode_flags &= ~BTRFS_INODE_COMPRESS;
280 binode_flags |= BTRFS_INODE_NOCOMPRESS;
281 } else if (fsflags & FS_COMPR_FL) {
283 if (IS_SWAPFILE(inode)) {
288 binode_flags |= BTRFS_INODE_COMPRESS;
289 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
291 comp = btrfs_compress_type2str(fs_info->compress_type);
292 if (!comp || comp[0] == 0)
293 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
295 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
302 trans = btrfs_start_transaction(root, 3);
304 ret = PTR_ERR(trans);
309 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
312 btrfs_abort_transaction(trans, ret);
315 set_bit(BTRFS_INODE_COPY_EVERYTHING,
316 &BTRFS_I(inode)->runtime_flags);
318 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
320 if (ret && ret != -ENODATA) {
321 btrfs_abort_transaction(trans, ret);
326 binode->flags = binode_flags;
327 btrfs_sync_inode_flags_to_i_flags(inode);
328 inode_inc_iversion(inode);
329 inode->i_ctime = current_time(inode);
330 ret = btrfs_update_inode(trans, root, inode);
333 btrfs_end_transaction(trans);
336 mnt_drop_write_file(file);
341 * Translate btrfs internal inode flags to xflags as expected by the
342 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
345 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
347 unsigned int xflags = 0;
349 if (flags & BTRFS_INODE_APPEND)
350 xflags |= FS_XFLAG_APPEND;
351 if (flags & BTRFS_INODE_IMMUTABLE)
352 xflags |= FS_XFLAG_IMMUTABLE;
353 if (flags & BTRFS_INODE_NOATIME)
354 xflags |= FS_XFLAG_NOATIME;
355 if (flags & BTRFS_INODE_NODUMP)
356 xflags |= FS_XFLAG_NODUMP;
357 if (flags & BTRFS_INODE_SYNC)
358 xflags |= FS_XFLAG_SYNC;
363 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
364 static int check_xflags(unsigned int flags)
366 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
367 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
373 * Set the xflags from the internal inode flags. The remaining items of fsxattr
376 static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
378 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
381 memset(&fa, 0, sizeof(fa));
382 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
384 if (copy_to_user(arg, &fa, sizeof(fa)))
390 static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
392 struct inode *inode = file_inode(file);
393 struct btrfs_inode *binode = BTRFS_I(inode);
394 struct btrfs_root *root = binode->root;
395 struct btrfs_trans_handle *trans;
398 unsigned old_i_flags;
401 if (!inode_owner_or_capable(inode))
404 if (btrfs_root_readonly(root))
407 memset(&fa, 0, sizeof(fa));
408 if (copy_from_user(&fa, arg, sizeof(fa)))
411 ret = check_xflags(fa.fsx_xflags);
415 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
418 ret = mnt_want_write_file(file);
424 old_flags = binode->flags;
425 old_i_flags = inode->i_flags;
427 /* We need the capabilities to change append-only or immutable inode */
428 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
429 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
430 !capable(CAP_LINUX_IMMUTABLE)) {
435 if (fa.fsx_xflags & FS_XFLAG_SYNC)
436 binode->flags |= BTRFS_INODE_SYNC;
438 binode->flags &= ~BTRFS_INODE_SYNC;
439 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
440 binode->flags |= BTRFS_INODE_IMMUTABLE;
442 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
443 if (fa.fsx_xflags & FS_XFLAG_APPEND)
444 binode->flags |= BTRFS_INODE_APPEND;
446 binode->flags &= ~BTRFS_INODE_APPEND;
447 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
448 binode->flags |= BTRFS_INODE_NODUMP;
450 binode->flags &= ~BTRFS_INODE_NODUMP;
451 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
452 binode->flags |= BTRFS_INODE_NOATIME;
454 binode->flags &= ~BTRFS_INODE_NOATIME;
456 /* 1 item for the inode */
457 trans = btrfs_start_transaction(root, 1);
459 ret = PTR_ERR(trans);
463 btrfs_sync_inode_flags_to_i_flags(inode);
464 inode_inc_iversion(inode);
465 inode->i_ctime = current_time(inode);
466 ret = btrfs_update_inode(trans, root, inode);
468 btrfs_end_transaction(trans);
472 binode->flags = old_flags;
473 inode->i_flags = old_i_flags;
477 mnt_drop_write_file(file);
482 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
484 struct inode *inode = file_inode(file);
486 return put_user(inode->i_generation, arg);
489 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
491 struct inode *inode = file_inode(file);
492 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
493 struct btrfs_device *device;
494 struct request_queue *q;
495 struct fstrim_range range;
496 u64 minlen = ULLONG_MAX;
500 if (!capable(CAP_SYS_ADMIN))
504 * If the fs is mounted with nologreplay, which requires it to be
505 * mounted in RO mode as well, we can not allow discard on free space
506 * inside block groups, because log trees refer to extents that are not
507 * pinned in a block group's free space cache (pinning the extents is
508 * precisely the first phase of replaying a log tree).
510 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
514 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
518 q = bdev_get_queue(device->bdev);
519 if (blk_queue_discard(q)) {
521 minlen = min_t(u64, q->limits.discard_granularity,
529 if (copy_from_user(&range, arg, sizeof(range)))
533 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
534 * block group is in the logical address space, which can be any
535 * sectorsize aligned bytenr in the range [0, U64_MAX].
537 if (range.len < fs_info->sb->s_blocksize)
540 range.minlen = max(range.minlen, minlen);
541 ret = btrfs_trim_fs(fs_info, &range);
545 if (copy_to_user(arg, &range, sizeof(range)))
551 int btrfs_is_empty_uuid(u8 *uuid)
555 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
562 static noinline int create_subvol(struct inode *dir,
563 struct dentry *dentry,
564 const char *name, int namelen,
566 struct btrfs_qgroup_inherit *inherit)
568 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
569 struct btrfs_trans_handle *trans;
570 struct btrfs_key key;
571 struct btrfs_root_item *root_item;
572 struct btrfs_inode_item *inode_item;
573 struct extent_buffer *leaf;
574 struct btrfs_root *root = BTRFS_I(dir)->root;
575 struct btrfs_root *new_root;
576 struct btrfs_block_rsv block_rsv;
577 struct timespec64 cur_time = current_time(dir);
582 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
586 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
590 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
595 * Don't create subvolume whose level is not zero. Or qgroup will be
596 * screwed up since it assumes subvolume qgroup's level to be 0.
598 if (btrfs_qgroup_level(objectid)) {
603 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
605 * The same as the snapshot creation, please see the comment
606 * of create_snapshot().
608 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
612 trans = btrfs_start_transaction(root, 0);
614 ret = PTR_ERR(trans);
615 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
618 trans->block_rsv = &block_rsv;
619 trans->bytes_reserved = block_rsv.size;
621 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
625 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
631 btrfs_mark_buffer_dirty(leaf);
633 inode_item = &root_item->inode;
634 btrfs_set_stack_inode_generation(inode_item, 1);
635 btrfs_set_stack_inode_size(inode_item, 3);
636 btrfs_set_stack_inode_nlink(inode_item, 1);
637 btrfs_set_stack_inode_nbytes(inode_item,
639 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
641 btrfs_set_root_flags(root_item, 0);
642 btrfs_set_root_limit(root_item, 0);
643 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
645 btrfs_set_root_bytenr(root_item, leaf->start);
646 btrfs_set_root_generation(root_item, trans->transid);
647 btrfs_set_root_level(root_item, 0);
648 btrfs_set_root_refs(root_item, 1);
649 btrfs_set_root_used(root_item, leaf->len);
650 btrfs_set_root_last_snapshot(root_item, 0);
652 btrfs_set_root_generation_v2(root_item,
653 btrfs_root_generation(root_item));
654 uuid_le_gen(&new_uuid);
655 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
656 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
657 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
658 root_item->ctime = root_item->otime;
659 btrfs_set_root_ctransid(root_item, trans->transid);
660 btrfs_set_root_otransid(root_item, trans->transid);
662 btrfs_tree_unlock(leaf);
663 free_extent_buffer(leaf);
666 btrfs_set_root_dirid(root_item, new_dirid);
668 key.objectid = objectid;
670 key.type = BTRFS_ROOT_ITEM_KEY;
671 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
676 key.offset = (u64)-1;
677 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
678 if (IS_ERR(new_root)) {
679 ret = PTR_ERR(new_root);
680 btrfs_abort_transaction(trans, ret);
684 btrfs_record_root_in_trans(trans, new_root);
686 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
688 /* We potentially lose an unused inode item here */
689 btrfs_abort_transaction(trans, ret);
693 mutex_lock(&new_root->objectid_mutex);
694 new_root->highest_objectid = new_dirid;
695 mutex_unlock(&new_root->objectid_mutex);
698 * insert the directory item
700 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
702 btrfs_abort_transaction(trans, ret);
706 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
707 BTRFS_FT_DIR, index);
709 btrfs_abort_transaction(trans, ret);
713 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
714 ret = btrfs_update_inode(trans, root, dir);
717 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
718 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
721 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
722 BTRFS_UUID_KEY_SUBVOL, objectid);
724 btrfs_abort_transaction(trans, ret);
728 trans->block_rsv = NULL;
729 trans->bytes_reserved = 0;
730 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
733 *async_transid = trans->transid;
734 err = btrfs_commit_transaction_async(trans, 1);
736 err = btrfs_commit_transaction(trans);
738 err = btrfs_commit_transaction(trans);
744 inode = btrfs_lookup_dentry(dir, dentry);
746 return PTR_ERR(inode);
747 d_instantiate(dentry, inode);
756 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
757 struct dentry *dentry,
758 u64 *async_transid, bool readonly,
759 struct btrfs_qgroup_inherit *inherit)
761 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
763 struct btrfs_pending_snapshot *pending_snapshot;
764 struct btrfs_trans_handle *trans;
766 bool snapshot_force_cow = false;
768 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
771 if (atomic_read(&root->nr_swapfiles)) {
773 "cannot snapshot subvolume with active swapfile");
777 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
778 if (!pending_snapshot)
781 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
783 pending_snapshot->path = btrfs_alloc_path();
784 if (!pending_snapshot->root_item || !pending_snapshot->path) {
790 * Force new buffered writes to reserve space even when NOCOW is
791 * possible. This is to avoid later writeback (running dealloc) to
792 * fallback to COW mode and unexpectedly fail with ENOSPC.
794 atomic_inc(&root->will_be_snapshotted);
795 smp_mb__after_atomic();
796 /* wait for no snapshot writes */
797 wait_event(root->subv_writers->wait,
798 percpu_counter_sum(&root->subv_writers->counter) == 0);
800 ret = btrfs_start_delalloc_snapshot(root);
805 * All previous writes have started writeback in NOCOW mode, so now
806 * we force future writes to fallback to COW mode during snapshot
809 atomic_inc(&root->snapshot_force_cow);
810 snapshot_force_cow = true;
812 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
814 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
815 BTRFS_BLOCK_RSV_TEMP);
817 * 1 - parent dir inode
820 * 2 - root ref/backref
821 * 1 - root of snapshot
824 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
825 &pending_snapshot->block_rsv, 8,
830 pending_snapshot->dentry = dentry;
831 pending_snapshot->root = root;
832 pending_snapshot->readonly = readonly;
833 pending_snapshot->dir = dir;
834 pending_snapshot->inherit = inherit;
836 trans = btrfs_start_transaction(root, 0);
838 ret = PTR_ERR(trans);
842 spin_lock(&fs_info->trans_lock);
843 list_add(&pending_snapshot->list,
844 &trans->transaction->pending_snapshots);
845 spin_unlock(&fs_info->trans_lock);
847 *async_transid = trans->transid;
848 ret = btrfs_commit_transaction_async(trans, 1);
850 ret = btrfs_commit_transaction(trans);
852 ret = btrfs_commit_transaction(trans);
857 ret = pending_snapshot->error;
861 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
865 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
867 ret = PTR_ERR(inode);
871 d_instantiate(dentry, inode);
874 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
876 if (snapshot_force_cow)
877 atomic_dec(&root->snapshot_force_cow);
878 if (atomic_dec_and_test(&root->will_be_snapshotted))
879 wake_up_var(&root->will_be_snapshotted);
881 kfree(pending_snapshot->root_item);
882 btrfs_free_path(pending_snapshot->path);
883 kfree(pending_snapshot);
888 /* copy of may_delete in fs/namei.c()
889 * Check whether we can remove a link victim from directory dir, check
890 * whether the type of victim is right.
891 * 1. We can't do it if dir is read-only (done in permission())
892 * 2. We should have write and exec permissions on dir
893 * 3. We can't remove anything from append-only dir
894 * 4. We can't do anything with immutable dir (done in permission())
895 * 5. If the sticky bit on dir is set we should either
896 * a. be owner of dir, or
897 * b. be owner of victim, or
898 * c. have CAP_FOWNER capability
899 * 6. If the victim is append-only or immutable we can't do anything with
900 * links pointing to it.
901 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
902 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
903 * 9. We can't remove a root or mountpoint.
904 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
905 * nfs_async_unlink().
908 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
912 if (d_really_is_negative(victim))
915 BUG_ON(d_inode(victim->d_parent) != dir);
916 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
918 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
923 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
924 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
927 if (!d_is_dir(victim))
931 } else if (d_is_dir(victim))
935 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
940 /* copy of may_create in fs/namei.c() */
941 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
943 if (d_really_is_positive(child))
947 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
951 * Create a new subvolume below @parent. This is largely modeled after
952 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
953 * inside this filesystem so it's quite a bit simpler.
955 static noinline int btrfs_mksubvol(const struct path *parent,
956 const char *name, int namelen,
957 struct btrfs_root *snap_src,
958 u64 *async_transid, bool readonly,
959 struct btrfs_qgroup_inherit *inherit)
961 struct inode *dir = d_inode(parent->dentry);
962 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
963 struct dentry *dentry;
966 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
970 dentry = lookup_one_len(name, parent->dentry, namelen);
971 error = PTR_ERR(dentry);
975 error = btrfs_may_create(dir, dentry);
980 * even if this name doesn't exist, we may get hash collisions.
981 * check for them now when we can safely fail
983 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
989 down_read(&fs_info->subvol_sem);
991 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
995 error = create_snapshot(snap_src, dir, dentry,
996 async_transid, readonly, inherit);
998 error = create_subvol(dir, dentry, name, namelen,
999 async_transid, inherit);
1002 fsnotify_mkdir(dir, dentry);
1004 up_read(&fs_info->subvol_sem);
1013 * When we're defragging a range, we don't want to kick it off again
1014 * if it is really just waiting for delalloc to send it down.
1015 * If we find a nice big extent or delalloc range for the bytes in the
1016 * file you want to defrag, we return 0 to let you know to skip this
1019 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1021 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1022 struct extent_map *em = NULL;
1023 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1026 read_lock(&em_tree->lock);
1027 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1028 read_unlock(&em_tree->lock);
1031 end = extent_map_end(em);
1032 free_extent_map(em);
1033 if (end - offset > thresh)
1036 /* if we already have a nice delalloc here, just stop */
1038 end = count_range_bits(io_tree, &offset, offset + thresh,
1039 thresh, EXTENT_DELALLOC, 1);
1046 * helper function to walk through a file and find extents
1047 * newer than a specific transid, and smaller than thresh.
1049 * This is used by the defragging code to find new and small
1052 static int find_new_extents(struct btrfs_root *root,
1053 struct inode *inode, u64 newer_than,
1054 u64 *off, u32 thresh)
1056 struct btrfs_path *path;
1057 struct btrfs_key min_key;
1058 struct extent_buffer *leaf;
1059 struct btrfs_file_extent_item *extent;
1062 u64 ino = btrfs_ino(BTRFS_I(inode));
1064 path = btrfs_alloc_path();
1068 min_key.objectid = ino;
1069 min_key.type = BTRFS_EXTENT_DATA_KEY;
1070 min_key.offset = *off;
1073 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1077 if (min_key.objectid != ino)
1079 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1082 leaf = path->nodes[0];
1083 extent = btrfs_item_ptr(leaf, path->slots[0],
1084 struct btrfs_file_extent_item);
1086 type = btrfs_file_extent_type(leaf, extent);
1087 if (type == BTRFS_FILE_EXTENT_REG &&
1088 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1089 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1090 *off = min_key.offset;
1091 btrfs_free_path(path);
1096 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1097 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1101 if (min_key.offset == (u64)-1)
1105 btrfs_release_path(path);
1108 btrfs_free_path(path);
1112 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1114 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1115 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1116 struct extent_map *em;
1117 u64 len = PAGE_SIZE;
1120 * hopefully we have this extent in the tree already, try without
1121 * the full extent lock
1123 read_lock(&em_tree->lock);
1124 em = lookup_extent_mapping(em_tree, start, len);
1125 read_unlock(&em_tree->lock);
1128 struct extent_state *cached = NULL;
1129 u64 end = start + len - 1;
1131 /* get the big lock and read metadata off disk */
1132 lock_extent_bits(io_tree, start, end, &cached);
1133 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
1134 unlock_extent_cached(io_tree, start, end, &cached);
1143 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1145 struct extent_map *next;
1148 /* this is the last extent */
1149 if (em->start + em->len >= i_size_read(inode))
1152 next = defrag_lookup_extent(inode, em->start + em->len);
1153 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1155 else if ((em->block_start + em->block_len == next->block_start) &&
1156 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1159 free_extent_map(next);
1163 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1164 u64 *last_len, u64 *skip, u64 *defrag_end,
1167 struct extent_map *em;
1169 bool next_mergeable = true;
1170 bool prev_mergeable = true;
1173 * make sure that once we start defragging an extent, we keep on
1176 if (start < *defrag_end)
1181 em = defrag_lookup_extent(inode, start);
1185 /* this will cover holes, and inline extents */
1186 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1192 prev_mergeable = false;
1194 next_mergeable = defrag_check_next_extent(inode, em);
1196 * we hit a real extent, if it is big or the next extent is not a
1197 * real extent, don't bother defragging it
1199 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1200 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1204 * last_len ends up being a counter of how many bytes we've defragged.
1205 * every time we choose not to defrag an extent, we reset *last_len
1206 * so that the next tiny extent will force a defrag.
1208 * The end result of this is that tiny extents before a single big
1209 * extent will force at least part of that big extent to be defragged.
1212 *defrag_end = extent_map_end(em);
1215 *skip = extent_map_end(em);
1219 free_extent_map(em);
1224 * it doesn't do much good to defrag one or two pages
1225 * at a time. This pulls in a nice chunk of pages
1226 * to COW and defrag.
1228 * It also makes sure the delalloc code has enough
1229 * dirty data to avoid making new small extents as part
1232 * It's a good idea to start RA on this range
1233 * before calling this.
1235 static int cluster_pages_for_defrag(struct inode *inode,
1236 struct page **pages,
1237 unsigned long start_index,
1238 unsigned long num_pages)
1240 unsigned long file_end;
1241 u64 isize = i_size_read(inode);
1248 struct btrfs_ordered_extent *ordered;
1249 struct extent_state *cached_state = NULL;
1250 struct extent_io_tree *tree;
1251 struct extent_changeset *data_reserved = NULL;
1252 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1254 file_end = (isize - 1) >> PAGE_SHIFT;
1255 if (!isize || start_index > file_end)
1258 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1260 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
1261 start_index << PAGE_SHIFT,
1262 page_cnt << PAGE_SHIFT);
1266 tree = &BTRFS_I(inode)->io_tree;
1268 /* step one, lock all the pages */
1269 for (i = 0; i < page_cnt; i++) {
1272 page = find_or_create_page(inode->i_mapping,
1273 start_index + i, mask);
1277 page_start = page_offset(page);
1278 page_end = page_start + PAGE_SIZE - 1;
1280 lock_extent_bits(tree, page_start, page_end,
1282 ordered = btrfs_lookup_ordered_extent(inode,
1284 unlock_extent_cached(tree, page_start, page_end,
1290 btrfs_start_ordered_extent(inode, ordered, 1);
1291 btrfs_put_ordered_extent(ordered);
1294 * we unlocked the page above, so we need check if
1295 * it was released or not.
1297 if (page->mapping != inode->i_mapping) {
1304 if (!PageUptodate(page)) {
1305 btrfs_readpage(NULL, page);
1307 if (!PageUptodate(page)) {
1315 if (page->mapping != inode->i_mapping) {
1327 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1331 * so now we have a nice long stream of locked
1332 * and up to date pages, lets wait on them
1334 for (i = 0; i < i_done; i++)
1335 wait_on_page_writeback(pages[i]);
1337 page_start = page_offset(pages[0]);
1338 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1340 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1341 page_start, page_end - 1, &cached_state);
1342 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1343 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1344 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1347 if (i_done != page_cnt) {
1348 spin_lock(&BTRFS_I(inode)->lock);
1349 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1350 spin_unlock(&BTRFS_I(inode)->lock);
1351 btrfs_delalloc_release_space(inode, data_reserved,
1352 start_index << PAGE_SHIFT,
1353 (page_cnt - i_done) << PAGE_SHIFT, true);
1357 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1360 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1361 page_start, page_end - 1, &cached_state);
1363 for (i = 0; i < i_done; i++) {
1364 clear_page_dirty_for_io(pages[i]);
1365 ClearPageChecked(pages[i]);
1366 set_page_extent_mapped(pages[i]);
1367 set_page_dirty(pages[i]);
1368 unlock_page(pages[i]);
1371 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1373 extent_changeset_free(data_reserved);
1376 for (i = 0; i < i_done; i++) {
1377 unlock_page(pages[i]);
1380 btrfs_delalloc_release_space(inode, data_reserved,
1381 start_index << PAGE_SHIFT,
1382 page_cnt << PAGE_SHIFT, true);
1383 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1385 extent_changeset_free(data_reserved);
1390 int btrfs_defrag_file(struct inode *inode, struct file *file,
1391 struct btrfs_ioctl_defrag_range_args *range,
1392 u64 newer_than, unsigned long max_to_defrag)
1394 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1395 struct btrfs_root *root = BTRFS_I(inode)->root;
1396 struct file_ra_state *ra = NULL;
1397 unsigned long last_index;
1398 u64 isize = i_size_read(inode);
1402 u64 newer_off = range->start;
1404 unsigned long ra_index = 0;
1406 int defrag_count = 0;
1407 int compress_type = BTRFS_COMPRESS_ZLIB;
1408 u32 extent_thresh = range->extent_thresh;
1409 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1410 unsigned long cluster = max_cluster;
1411 u64 new_align = ~((u64)SZ_128K - 1);
1412 struct page **pages = NULL;
1413 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1418 if (range->start >= isize)
1422 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1424 if (range->compress_type)
1425 compress_type = range->compress_type;
1428 if (extent_thresh == 0)
1429 extent_thresh = SZ_256K;
1432 * If we were not given a file, allocate a readahead context. As
1433 * readahead is just an optimization, defrag will work without it so
1434 * we don't error out.
1437 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1439 file_ra_state_init(ra, inode->i_mapping);
1444 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1450 /* find the last page to defrag */
1451 if (range->start + range->len > range->start) {
1452 last_index = min_t(u64, isize - 1,
1453 range->start + range->len - 1) >> PAGE_SHIFT;
1455 last_index = (isize - 1) >> PAGE_SHIFT;
1459 ret = find_new_extents(root, inode, newer_than,
1460 &newer_off, SZ_64K);
1462 range->start = newer_off;
1464 * we always align our defrag to help keep
1465 * the extents in the file evenly spaced
1467 i = (newer_off & new_align) >> PAGE_SHIFT;
1471 i = range->start >> PAGE_SHIFT;
1474 max_to_defrag = last_index - i + 1;
1477 * make writeback starts from i, so the defrag range can be
1478 * written sequentially.
1480 if (i < inode->i_mapping->writeback_index)
1481 inode->i_mapping->writeback_index = i;
1483 while (i <= last_index && defrag_count < max_to_defrag &&
1484 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1486 * make sure we stop running if someone unmounts
1489 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1492 if (btrfs_defrag_cancelled(fs_info)) {
1493 btrfs_debug(fs_info, "defrag_file cancelled");
1498 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1499 extent_thresh, &last_len, &skip,
1500 &defrag_end, do_compress)){
1503 * the should_defrag function tells us how much to skip
1504 * bump our counter by the suggested amount
1506 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1507 i = max(i + 1, next);
1512 cluster = (PAGE_ALIGN(defrag_end) >>
1514 cluster = min(cluster, max_cluster);
1516 cluster = max_cluster;
1519 if (i + cluster > ra_index) {
1520 ra_index = max(i, ra_index);
1522 page_cache_sync_readahead(inode->i_mapping, ra,
1523 file, ra_index, cluster);
1524 ra_index += cluster;
1528 if (IS_SWAPFILE(inode)) {
1532 BTRFS_I(inode)->defrag_compress = compress_type;
1533 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1536 inode_unlock(inode);
1540 defrag_count += ret;
1541 balance_dirty_pages_ratelimited(inode->i_mapping);
1542 inode_unlock(inode);
1545 if (newer_off == (u64)-1)
1551 newer_off = max(newer_off + 1,
1552 (u64)i << PAGE_SHIFT);
1554 ret = find_new_extents(root, inode, newer_than,
1555 &newer_off, SZ_64K);
1557 range->start = newer_off;
1558 i = (newer_off & new_align) >> PAGE_SHIFT;
1565 last_len += ret << PAGE_SHIFT;
1573 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1574 filemap_flush(inode->i_mapping);
1575 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1576 &BTRFS_I(inode)->runtime_flags))
1577 filemap_flush(inode->i_mapping);
1580 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1581 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1582 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1583 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1591 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1592 inode_unlock(inode);
1600 static noinline int btrfs_ioctl_resize(struct file *file,
1603 struct inode *inode = file_inode(file);
1604 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1608 struct btrfs_root *root = BTRFS_I(inode)->root;
1609 struct btrfs_ioctl_vol_args *vol_args;
1610 struct btrfs_trans_handle *trans;
1611 struct btrfs_device *device = NULL;
1614 char *devstr = NULL;
1618 if (!capable(CAP_SYS_ADMIN))
1621 ret = mnt_want_write_file(file);
1625 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
1626 mnt_drop_write_file(file);
1627 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1630 vol_args = memdup_user(arg, sizeof(*vol_args));
1631 if (IS_ERR(vol_args)) {
1632 ret = PTR_ERR(vol_args);
1636 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1638 sizestr = vol_args->name;
1639 devstr = strchr(sizestr, ':');
1641 sizestr = devstr + 1;
1643 devstr = vol_args->name;
1644 ret = kstrtoull(devstr, 10, &devid);
1651 btrfs_info(fs_info, "resizing devid %llu", devid);
1654 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
1656 btrfs_info(fs_info, "resizer unable to find device %llu",
1662 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1664 "resizer unable to apply on readonly device %llu",
1670 if (!strcmp(sizestr, "max"))
1671 new_size = device->bdev->bd_inode->i_size;
1673 if (sizestr[0] == '-') {
1676 } else if (sizestr[0] == '+') {
1680 new_size = memparse(sizestr, &retptr);
1681 if (*retptr != '\0' || new_size == 0) {
1687 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1692 old_size = btrfs_device_get_total_bytes(device);
1695 if (new_size > old_size) {
1699 new_size = old_size - new_size;
1700 } else if (mod > 0) {
1701 if (new_size > ULLONG_MAX - old_size) {
1705 new_size = old_size + new_size;
1708 if (new_size < SZ_256M) {
1712 if (new_size > device->bdev->bd_inode->i_size) {
1717 new_size = round_down(new_size, fs_info->sectorsize);
1719 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1720 rcu_str_deref(device->name), new_size);
1722 if (new_size > old_size) {
1723 trans = btrfs_start_transaction(root, 0);
1724 if (IS_ERR(trans)) {
1725 ret = PTR_ERR(trans);
1728 ret = btrfs_grow_device(trans, device, new_size);
1729 btrfs_commit_transaction(trans);
1730 } else if (new_size < old_size) {
1731 ret = btrfs_shrink_device(device, new_size);
1732 } /* equal, nothing need to do */
1737 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
1738 mnt_drop_write_file(file);
1742 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1743 const char *name, unsigned long fd, int subvol,
1744 u64 *transid, bool readonly,
1745 struct btrfs_qgroup_inherit *inherit)
1750 if (!S_ISDIR(file_inode(file)->i_mode))
1753 ret = mnt_want_write_file(file);
1757 namelen = strlen(name);
1758 if (strchr(name, '/')) {
1760 goto out_drop_write;
1763 if (name[0] == '.' &&
1764 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1766 goto out_drop_write;
1770 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1771 NULL, transid, readonly, inherit);
1773 struct fd src = fdget(fd);
1774 struct inode *src_inode;
1777 goto out_drop_write;
1780 src_inode = file_inode(src.file);
1781 if (src_inode->i_sb != file_inode(file)->i_sb) {
1782 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1783 "Snapshot src from another FS");
1785 } else if (!inode_owner_or_capable(src_inode)) {
1787 * Subvolume creation is not restricted, but snapshots
1788 * are limited to own subvolumes only
1792 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1793 BTRFS_I(src_inode)->root,
1794 transid, readonly, inherit);
1799 mnt_drop_write_file(file);
1804 static noinline int btrfs_ioctl_snap_create(struct file *file,
1805 void __user *arg, int subvol)
1807 struct btrfs_ioctl_vol_args *vol_args;
1810 if (!S_ISDIR(file_inode(file)->i_mode))
1813 vol_args = memdup_user(arg, sizeof(*vol_args));
1814 if (IS_ERR(vol_args))
1815 return PTR_ERR(vol_args);
1816 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1818 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1819 vol_args->fd, subvol,
1826 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1827 void __user *arg, int subvol)
1829 struct btrfs_ioctl_vol_args_v2 *vol_args;
1833 bool readonly = false;
1834 struct btrfs_qgroup_inherit *inherit = NULL;
1836 if (!S_ISDIR(file_inode(file)->i_mode))
1839 vol_args = memdup_user(arg, sizeof(*vol_args));
1840 if (IS_ERR(vol_args))
1841 return PTR_ERR(vol_args);
1842 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1844 if (vol_args->flags &
1845 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1846 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1851 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1853 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1855 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1856 if (vol_args->size > PAGE_SIZE) {
1860 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1861 if (IS_ERR(inherit)) {
1862 ret = PTR_ERR(inherit);
1867 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1868 vol_args->fd, subvol, ptr,
1873 if (ptr && copy_to_user(arg +
1874 offsetof(struct btrfs_ioctl_vol_args_v2,
1886 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1889 struct inode *inode = file_inode(file);
1890 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1891 struct btrfs_root *root = BTRFS_I(inode)->root;
1895 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1898 down_read(&fs_info->subvol_sem);
1899 if (btrfs_root_readonly(root))
1900 flags |= BTRFS_SUBVOL_RDONLY;
1901 up_read(&fs_info->subvol_sem);
1903 if (copy_to_user(arg, &flags, sizeof(flags)))
1909 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1912 struct inode *inode = file_inode(file);
1913 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1914 struct btrfs_root *root = BTRFS_I(inode)->root;
1915 struct btrfs_trans_handle *trans;
1920 if (!inode_owner_or_capable(inode))
1923 ret = mnt_want_write_file(file);
1927 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1929 goto out_drop_write;
1932 if (copy_from_user(&flags, arg, sizeof(flags))) {
1934 goto out_drop_write;
1937 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1939 goto out_drop_write;
1942 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1944 goto out_drop_write;
1947 down_write(&fs_info->subvol_sem);
1950 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1953 root_flags = btrfs_root_flags(&root->root_item);
1954 if (flags & BTRFS_SUBVOL_RDONLY) {
1955 btrfs_set_root_flags(&root->root_item,
1956 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1959 * Block RO -> RW transition if this subvolume is involved in
1962 spin_lock(&root->root_item_lock);
1963 if (root->send_in_progress == 0) {
1964 btrfs_set_root_flags(&root->root_item,
1965 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1966 spin_unlock(&root->root_item_lock);
1968 spin_unlock(&root->root_item_lock);
1970 "Attempt to set subvolume %llu read-write during send",
1971 root->root_key.objectid);
1977 trans = btrfs_start_transaction(root, 1);
1978 if (IS_ERR(trans)) {
1979 ret = PTR_ERR(trans);
1983 ret = btrfs_update_root(trans, fs_info->tree_root,
1984 &root->root_key, &root->root_item);
1986 btrfs_end_transaction(trans);
1990 ret = btrfs_commit_transaction(trans);
1994 btrfs_set_root_flags(&root->root_item, root_flags);
1996 up_write(&fs_info->subvol_sem);
1998 mnt_drop_write_file(file);
2003 static noinline int key_in_sk(struct btrfs_key *key,
2004 struct btrfs_ioctl_search_key *sk)
2006 struct btrfs_key test;
2009 test.objectid = sk->min_objectid;
2010 test.type = sk->min_type;
2011 test.offset = sk->min_offset;
2013 ret = btrfs_comp_cpu_keys(key, &test);
2017 test.objectid = sk->max_objectid;
2018 test.type = sk->max_type;
2019 test.offset = sk->max_offset;
2021 ret = btrfs_comp_cpu_keys(key, &test);
2027 static noinline int copy_to_sk(struct btrfs_path *path,
2028 struct btrfs_key *key,
2029 struct btrfs_ioctl_search_key *sk,
2032 unsigned long *sk_offset,
2036 struct extent_buffer *leaf;
2037 struct btrfs_ioctl_search_header sh;
2038 struct btrfs_key test;
2039 unsigned long item_off;
2040 unsigned long item_len;
2046 leaf = path->nodes[0];
2047 slot = path->slots[0];
2048 nritems = btrfs_header_nritems(leaf);
2050 if (btrfs_header_generation(leaf) > sk->max_transid) {
2054 found_transid = btrfs_header_generation(leaf);
2056 for (i = slot; i < nritems; i++) {
2057 item_off = btrfs_item_ptr_offset(leaf, i);
2058 item_len = btrfs_item_size_nr(leaf, i);
2060 btrfs_item_key_to_cpu(leaf, key, i);
2061 if (!key_in_sk(key, sk))
2064 if (sizeof(sh) + item_len > *buf_size) {
2071 * return one empty item back for v1, which does not
2075 *buf_size = sizeof(sh) + item_len;
2080 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2085 sh.objectid = key->objectid;
2086 sh.offset = key->offset;
2087 sh.type = key->type;
2089 sh.transid = found_transid;
2091 /* copy search result header */
2092 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2097 *sk_offset += sizeof(sh);
2100 char __user *up = ubuf + *sk_offset;
2102 if (read_extent_buffer_to_user(leaf, up,
2103 item_off, item_len)) {
2108 *sk_offset += item_len;
2112 if (ret) /* -EOVERFLOW from above */
2115 if (*num_found >= sk->nr_items) {
2122 test.objectid = sk->max_objectid;
2123 test.type = sk->max_type;
2124 test.offset = sk->max_offset;
2125 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2127 else if (key->offset < (u64)-1)
2129 else if (key->type < (u8)-1) {
2132 } else if (key->objectid < (u64)-1) {
2140 * 0: all items from this leaf copied, continue with next
2141 * 1: * more items can be copied, but unused buffer is too small
2142 * * all items were found
2143 * Either way, it will stops the loop which iterates to the next
2145 * -EOVERFLOW: item was to large for buffer
2146 * -EFAULT: could not copy extent buffer back to userspace
2151 static noinline int search_ioctl(struct inode *inode,
2152 struct btrfs_ioctl_search_key *sk,
2156 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2157 struct btrfs_root *root;
2158 struct btrfs_key key;
2159 struct btrfs_path *path;
2162 unsigned long sk_offset = 0;
2164 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2165 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2169 path = btrfs_alloc_path();
2173 if (sk->tree_id == 0) {
2174 /* search the root of the inode that was passed */
2175 root = BTRFS_I(inode)->root;
2177 key.objectid = sk->tree_id;
2178 key.type = BTRFS_ROOT_ITEM_KEY;
2179 key.offset = (u64)-1;
2180 root = btrfs_read_fs_root_no_name(info, &key);
2182 btrfs_free_path(path);
2183 return PTR_ERR(root);
2187 key.objectid = sk->min_objectid;
2188 key.type = sk->min_type;
2189 key.offset = sk->min_offset;
2192 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2198 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2199 &sk_offset, &num_found);
2200 btrfs_release_path(path);
2208 sk->nr_items = num_found;
2209 btrfs_free_path(path);
2213 static noinline int btrfs_ioctl_tree_search(struct file *file,
2216 struct btrfs_ioctl_search_args __user *uargs;
2217 struct btrfs_ioctl_search_key sk;
2218 struct inode *inode;
2222 if (!capable(CAP_SYS_ADMIN))
2225 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2227 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2230 buf_size = sizeof(uargs->buf);
2232 inode = file_inode(file);
2233 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2236 * In the origin implementation an overflow is handled by returning a
2237 * search header with a len of zero, so reset ret.
2239 if (ret == -EOVERFLOW)
2242 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2247 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2250 struct btrfs_ioctl_search_args_v2 __user *uarg;
2251 struct btrfs_ioctl_search_args_v2 args;
2252 struct inode *inode;
2255 const size_t buf_limit = SZ_16M;
2257 if (!capable(CAP_SYS_ADMIN))
2260 /* copy search header and buffer size */
2261 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2262 if (copy_from_user(&args, uarg, sizeof(args)))
2265 buf_size = args.buf_size;
2267 /* limit result size to 16MB */
2268 if (buf_size > buf_limit)
2269 buf_size = buf_limit;
2271 inode = file_inode(file);
2272 ret = search_ioctl(inode, &args.key, &buf_size,
2273 (char __user *)(&uarg->buf[0]));
2274 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2276 else if (ret == -EOVERFLOW &&
2277 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2284 * Search INODE_REFs to identify path name of 'dirid' directory
2285 * in a 'tree_id' tree. and sets path name to 'name'.
2287 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2288 u64 tree_id, u64 dirid, char *name)
2290 struct btrfs_root *root;
2291 struct btrfs_key key;
2297 struct btrfs_inode_ref *iref;
2298 struct extent_buffer *l;
2299 struct btrfs_path *path;
2301 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2306 path = btrfs_alloc_path();
2310 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2312 key.objectid = tree_id;
2313 key.type = BTRFS_ROOT_ITEM_KEY;
2314 key.offset = (u64)-1;
2315 root = btrfs_read_fs_root_no_name(info, &key);
2317 ret = PTR_ERR(root);
2321 key.objectid = dirid;
2322 key.type = BTRFS_INODE_REF_KEY;
2323 key.offset = (u64)-1;
2326 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2330 ret = btrfs_previous_item(root, path, dirid,
2331 BTRFS_INODE_REF_KEY);
2341 slot = path->slots[0];
2342 btrfs_item_key_to_cpu(l, &key, slot);
2344 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2345 len = btrfs_inode_ref_name_len(l, iref);
2347 total_len += len + 1;
2349 ret = -ENAMETOOLONG;
2354 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2356 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2359 btrfs_release_path(path);
2360 key.objectid = key.offset;
2361 key.offset = (u64)-1;
2362 dirid = key.objectid;
2364 memmove(name, ptr, total_len);
2365 name[total_len] = '\0';
2368 btrfs_free_path(path);
2372 static int btrfs_search_path_in_tree_user(struct inode *inode,
2373 struct btrfs_ioctl_ino_lookup_user_args *args)
2375 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2376 struct super_block *sb = inode->i_sb;
2377 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2378 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2379 u64 dirid = args->dirid;
2380 unsigned long item_off;
2381 unsigned long item_len;
2382 struct btrfs_inode_ref *iref;
2383 struct btrfs_root_ref *rref;
2384 struct btrfs_root *root;
2385 struct btrfs_path *path;
2386 struct btrfs_key key, key2;
2387 struct extent_buffer *leaf;
2388 struct inode *temp_inode;
2395 path = btrfs_alloc_path();
2400 * If the bottom subvolume does not exist directly under upper_limit,
2401 * construct the path in from the bottom up.
2403 if (dirid != upper_limit.objectid) {
2404 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2406 key.objectid = treeid;
2407 key.type = BTRFS_ROOT_ITEM_KEY;
2408 key.offset = (u64)-1;
2409 root = btrfs_read_fs_root_no_name(fs_info, &key);
2411 ret = PTR_ERR(root);
2415 key.objectid = dirid;
2416 key.type = BTRFS_INODE_REF_KEY;
2417 key.offset = (u64)-1;
2419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2422 } else if (ret > 0) {
2423 ret = btrfs_previous_item(root, path, dirid,
2424 BTRFS_INODE_REF_KEY);
2427 } else if (ret > 0) {
2433 leaf = path->nodes[0];
2434 slot = path->slots[0];
2435 btrfs_item_key_to_cpu(leaf, &key, slot);
2437 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2438 len = btrfs_inode_ref_name_len(leaf, iref);
2440 total_len += len + 1;
2441 if (ptr < args->path) {
2442 ret = -ENAMETOOLONG;
2447 read_extent_buffer(leaf, ptr,
2448 (unsigned long)(iref + 1), len);
2450 /* Check the read+exec permission of this directory */
2451 ret = btrfs_previous_item(root, path, dirid,
2452 BTRFS_INODE_ITEM_KEY);
2455 } else if (ret > 0) {
2460 leaf = path->nodes[0];
2461 slot = path->slots[0];
2462 btrfs_item_key_to_cpu(leaf, &key2, slot);
2463 if (key2.objectid != dirid) {
2468 temp_inode = btrfs_iget(sb, &key2, root, NULL);
2469 if (IS_ERR(temp_inode)) {
2470 ret = PTR_ERR(temp_inode);
2473 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2480 if (key.offset == upper_limit.objectid)
2482 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2487 btrfs_release_path(path);
2488 key.objectid = key.offset;
2489 key.offset = (u64)-1;
2490 dirid = key.objectid;
2493 memmove(args->path, ptr, total_len);
2494 args->path[total_len] = '\0';
2495 btrfs_release_path(path);
2498 /* Get the bottom subvolume's name from ROOT_REF */
2499 root = fs_info->tree_root;
2500 key.objectid = treeid;
2501 key.type = BTRFS_ROOT_REF_KEY;
2502 key.offset = args->treeid;
2503 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2506 } else if (ret > 0) {
2511 leaf = path->nodes[0];
2512 slot = path->slots[0];
2513 btrfs_item_key_to_cpu(leaf, &key, slot);
2515 item_off = btrfs_item_ptr_offset(leaf, slot);
2516 item_len = btrfs_item_size_nr(leaf, slot);
2517 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2518 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2519 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2524 /* Copy subvolume's name */
2525 item_off += sizeof(struct btrfs_root_ref);
2526 item_len -= sizeof(struct btrfs_root_ref);
2527 read_extent_buffer(leaf, args->name, item_off, item_len);
2528 args->name[item_len] = 0;
2531 btrfs_free_path(path);
2535 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2538 struct btrfs_ioctl_ino_lookup_args *args;
2539 struct inode *inode;
2542 args = memdup_user(argp, sizeof(*args));
2544 return PTR_ERR(args);
2546 inode = file_inode(file);
2549 * Unprivileged query to obtain the containing subvolume root id. The
2550 * path is reset so it's consistent with btrfs_search_path_in_tree.
2552 if (args->treeid == 0)
2553 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2555 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2560 if (!capable(CAP_SYS_ADMIN)) {
2565 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2566 args->treeid, args->objectid,
2570 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2578 * Version of ino_lookup ioctl (unprivileged)
2580 * The main differences from ino_lookup ioctl are:
2582 * 1. Read + Exec permission will be checked using inode_permission() during
2583 * path construction. -EACCES will be returned in case of failure.
2584 * 2. Path construction will be stopped at the inode number which corresponds
2585 * to the fd with which this ioctl is called. If constructed path does not
2586 * exist under fd's inode, -EACCES will be returned.
2587 * 3. The name of bottom subvolume is also searched and filled.
2589 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2591 struct btrfs_ioctl_ino_lookup_user_args *args;
2592 struct inode *inode;
2595 args = memdup_user(argp, sizeof(*args));
2597 return PTR_ERR(args);
2599 inode = file_inode(file);
2601 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2602 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2604 * The subvolume does not exist under fd with which this is
2611 ret = btrfs_search_path_in_tree_user(inode, args);
2613 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2620 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2621 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2623 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2624 struct btrfs_fs_info *fs_info;
2625 struct btrfs_root *root;
2626 struct btrfs_path *path;
2627 struct btrfs_key key;
2628 struct btrfs_root_item *root_item;
2629 struct btrfs_root_ref *rref;
2630 struct extent_buffer *leaf;
2631 unsigned long item_off;
2632 unsigned long item_len;
2633 struct inode *inode;
2637 path = btrfs_alloc_path();
2641 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2643 btrfs_free_path(path);
2647 inode = file_inode(file);
2648 fs_info = BTRFS_I(inode)->root->fs_info;
2650 /* Get root_item of inode's subvolume */
2651 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2652 key.type = BTRFS_ROOT_ITEM_KEY;
2653 key.offset = (u64)-1;
2654 root = btrfs_read_fs_root_no_name(fs_info, &key);
2656 ret = PTR_ERR(root);
2659 root_item = &root->root_item;
2661 subvol_info->treeid = key.objectid;
2663 subvol_info->generation = btrfs_root_generation(root_item);
2664 subvol_info->flags = btrfs_root_flags(root_item);
2666 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2667 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2669 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2672 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2673 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2674 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2676 subvol_info->otransid = btrfs_root_otransid(root_item);
2677 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2678 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2680 subvol_info->stransid = btrfs_root_stransid(root_item);
2681 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2682 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2684 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2685 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2686 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2688 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2689 /* Search root tree for ROOT_BACKREF of this subvolume */
2690 root = fs_info->tree_root;
2692 key.type = BTRFS_ROOT_BACKREF_KEY;
2694 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2697 } else if (path->slots[0] >=
2698 btrfs_header_nritems(path->nodes[0])) {
2699 ret = btrfs_next_leaf(root, path);
2702 } else if (ret > 0) {
2708 leaf = path->nodes[0];
2709 slot = path->slots[0];
2710 btrfs_item_key_to_cpu(leaf, &key, slot);
2711 if (key.objectid == subvol_info->treeid &&
2712 key.type == BTRFS_ROOT_BACKREF_KEY) {
2713 subvol_info->parent_id = key.offset;
2715 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2716 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2718 item_off = btrfs_item_ptr_offset(leaf, slot)
2719 + sizeof(struct btrfs_root_ref);
2720 item_len = btrfs_item_size_nr(leaf, slot)
2721 - sizeof(struct btrfs_root_ref);
2722 read_extent_buffer(leaf, subvol_info->name,
2723 item_off, item_len);
2730 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2734 btrfs_free_path(path);
2735 kzfree(subvol_info);
2740 * Return ROOT_REF information of the subvolume containing this inode
2741 * except the subvolume name.
2743 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2745 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2746 struct btrfs_root_ref *rref;
2747 struct btrfs_root *root;
2748 struct btrfs_path *path;
2749 struct btrfs_key key;
2750 struct extent_buffer *leaf;
2751 struct inode *inode;
2757 path = btrfs_alloc_path();
2761 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2762 if (IS_ERR(rootrefs)) {
2763 btrfs_free_path(path);
2764 return PTR_ERR(rootrefs);
2767 inode = file_inode(file);
2768 root = BTRFS_I(inode)->root->fs_info->tree_root;
2769 objectid = BTRFS_I(inode)->root->root_key.objectid;
2771 key.objectid = objectid;
2772 key.type = BTRFS_ROOT_REF_KEY;
2773 key.offset = rootrefs->min_treeid;
2776 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2779 } else if (path->slots[0] >=
2780 btrfs_header_nritems(path->nodes[0])) {
2781 ret = btrfs_next_leaf(root, path);
2784 } else if (ret > 0) {
2790 leaf = path->nodes[0];
2791 slot = path->slots[0];
2793 btrfs_item_key_to_cpu(leaf, &key, slot);
2794 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2799 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2804 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2805 rootrefs->rootref[found].treeid = key.offset;
2806 rootrefs->rootref[found].dirid =
2807 btrfs_root_ref_dirid(leaf, rref);
2810 ret = btrfs_next_item(root, path);
2813 } else if (ret > 0) {
2820 if (!ret || ret == -EOVERFLOW) {
2821 rootrefs->num_items = found;
2822 /* update min_treeid for next search */
2824 rootrefs->min_treeid =
2825 rootrefs->rootref[found - 1].treeid + 1;
2826 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2831 btrfs_free_path(path);
2836 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2839 struct dentry *parent = file->f_path.dentry;
2840 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2841 struct dentry *dentry;
2842 struct inode *dir = d_inode(parent);
2843 struct inode *inode;
2844 struct btrfs_root *root = BTRFS_I(dir)->root;
2845 struct btrfs_root *dest = NULL;
2846 struct btrfs_ioctl_vol_args *vol_args;
2850 if (!S_ISDIR(dir->i_mode))
2853 vol_args = memdup_user(arg, sizeof(*vol_args));
2854 if (IS_ERR(vol_args))
2855 return PTR_ERR(vol_args);
2857 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2858 namelen = strlen(vol_args->name);
2859 if (strchr(vol_args->name, '/') ||
2860 strncmp(vol_args->name, "..", namelen) == 0) {
2865 err = mnt_want_write_file(file);
2870 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2872 goto out_drop_write;
2873 dentry = lookup_one_len(vol_args->name, parent, namelen);
2874 if (IS_ERR(dentry)) {
2875 err = PTR_ERR(dentry);
2876 goto out_unlock_dir;
2879 if (d_really_is_negative(dentry)) {
2884 inode = d_inode(dentry);
2885 dest = BTRFS_I(inode)->root;
2886 if (!capable(CAP_SYS_ADMIN)) {
2888 * Regular user. Only allow this with a special mount
2889 * option, when the user has write+exec access to the
2890 * subvol root, and when rmdir(2) would have been
2893 * Note that this is _not_ check that the subvol is
2894 * empty or doesn't contain data that we wouldn't
2895 * otherwise be able to delete.
2897 * Users who want to delete empty subvols should try
2901 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
2905 * Do not allow deletion if the parent dir is the same
2906 * as the dir to be deleted. That means the ioctl
2907 * must be called on the dentry referencing the root
2908 * of the subvol, not a random directory contained
2915 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2920 /* check if subvolume may be deleted by a user */
2921 err = btrfs_may_delete(dir, dentry, 1);
2925 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2931 err = btrfs_delete_subvolume(dir, dentry);
2932 inode_unlock(inode);
2941 mnt_drop_write_file(file);
2947 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2949 struct inode *inode = file_inode(file);
2950 struct btrfs_root *root = BTRFS_I(inode)->root;
2951 struct btrfs_ioctl_defrag_range_args *range;
2954 ret = mnt_want_write_file(file);
2958 if (btrfs_root_readonly(root)) {
2963 switch (inode->i_mode & S_IFMT) {
2965 if (!capable(CAP_SYS_ADMIN)) {
2969 ret = btrfs_defrag_root(root);
2973 * Note that this does not check the file descriptor for write
2974 * access. This prevents defragmenting executables that are
2975 * running and allows defrag on files open in read-only mode.
2977 if (!capable(CAP_SYS_ADMIN) &&
2978 inode_permission(inode, MAY_WRITE)) {
2983 range = kzalloc(sizeof(*range), GFP_KERNEL);
2990 if (copy_from_user(range, argp,
2996 /* compression requires us to start the IO */
2997 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2998 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2999 range->extent_thresh = (u32)-1;
3002 /* the rest are all set to zero by kzalloc */
3003 range->len = (u64)-1;
3005 ret = btrfs_defrag_file(file_inode(file), file,
3006 range, BTRFS_OLDEST_GENERATION, 0);
3015 mnt_drop_write_file(file);
3019 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3021 struct btrfs_ioctl_vol_args *vol_args;
3024 if (!capable(CAP_SYS_ADMIN))
3027 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
3028 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3030 vol_args = memdup_user(arg, sizeof(*vol_args));
3031 if (IS_ERR(vol_args)) {
3032 ret = PTR_ERR(vol_args);
3036 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3037 ret = btrfs_init_new_device(fs_info, vol_args->name);
3040 btrfs_info(fs_info, "disk added %s", vol_args->name);
3044 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3048 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3050 struct inode *inode = file_inode(file);
3051 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3052 struct btrfs_ioctl_vol_args_v2 *vol_args;
3055 if (!capable(CAP_SYS_ADMIN))
3058 ret = mnt_want_write_file(file);
3062 vol_args = memdup_user(arg, sizeof(*vol_args));
3063 if (IS_ERR(vol_args)) {
3064 ret = PTR_ERR(vol_args);
3068 /* Check for compatibility reject unknown flags */
3069 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3074 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
3075 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3079 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3080 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3082 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3083 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3085 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3088 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3089 btrfs_info(fs_info, "device deleted: id %llu",
3092 btrfs_info(fs_info, "device deleted: %s",
3098 mnt_drop_write_file(file);
3102 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3104 struct inode *inode = file_inode(file);
3105 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3106 struct btrfs_ioctl_vol_args *vol_args;
3109 if (!capable(CAP_SYS_ADMIN))
3112 ret = mnt_want_write_file(file);
3116 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
3117 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3118 goto out_drop_write;
3121 vol_args = memdup_user(arg, sizeof(*vol_args));
3122 if (IS_ERR(vol_args)) {
3123 ret = PTR_ERR(vol_args);
3127 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3128 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3131 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3134 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3136 mnt_drop_write_file(file);
3141 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3144 struct btrfs_ioctl_fs_info_args *fi_args;
3145 struct btrfs_device *device;
3146 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3149 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3154 fi_args->num_devices = fs_devices->num_devices;
3156 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3157 if (device->devid > fi_args->max_id)
3158 fi_args->max_id = device->devid;
3162 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3163 fi_args->nodesize = fs_info->nodesize;
3164 fi_args->sectorsize = fs_info->sectorsize;
3165 fi_args->clone_alignment = fs_info->sectorsize;
3167 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3174 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3177 struct btrfs_ioctl_dev_info_args *di_args;
3178 struct btrfs_device *dev;
3180 char *s_uuid = NULL;
3182 di_args = memdup_user(arg, sizeof(*di_args));
3183 if (IS_ERR(di_args))
3184 return PTR_ERR(di_args);
3186 if (!btrfs_is_empty_uuid(di_args->uuid))
3187 s_uuid = di_args->uuid;
3190 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3198 di_args->devid = dev->devid;
3199 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3200 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3201 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3203 strncpy(di_args->path, rcu_str_deref(dev->name),
3204 sizeof(di_args->path) - 1);
3205 di_args->path[sizeof(di_args->path) - 1] = 0;
3207 di_args->path[0] = '\0';
3212 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3219 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3220 struct inode *inode2, u64 loff2, u64 len)
3222 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3223 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3226 static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3227 struct inode *inode2, u64 loff2, u64 len)
3229 if (inode1 < inode2) {
3230 swap(inode1, inode2);
3232 } else if (inode1 == inode2 && loff2 < loff1) {
3235 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3236 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3239 static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
3240 struct inode *dst, u64 dst_loff)
3245 * Lock destination range to serialize with concurrent readpages() and
3246 * source range to serialize with relocation.
3248 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
3249 ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
3250 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
3255 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3257 static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3258 struct inode *dst, u64 dst_loff)
3261 u64 i, tail_len, chunk_count;
3262 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3264 spin_lock(&root_dst->root_item_lock);
3265 if (root_dst->send_in_progress) {
3266 btrfs_warn_rl(root_dst->fs_info,
3267 "cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3268 root_dst->root_key.objectid,
3269 root_dst->send_in_progress);
3270 spin_unlock(&root_dst->root_item_lock);
3273 root_dst->dedupe_in_progress++;
3274 spin_unlock(&root_dst->root_item_lock);
3276 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3277 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
3279 for (i = 0; i < chunk_count; i++) {
3280 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
3285 loff += BTRFS_MAX_DEDUPE_LEN;
3286 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3290 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3293 spin_lock(&root_dst->root_item_lock);
3294 root_dst->dedupe_in_progress--;
3295 spin_unlock(&root_dst->root_item_lock);
3300 static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3301 struct inode *inode,
3307 struct btrfs_root *root = BTRFS_I(inode)->root;
3310 inode_inc_iversion(inode);
3311 if (!no_time_update)
3312 inode->i_mtime = inode->i_ctime = current_time(inode);
3314 * We round up to the block size at eof when determining which
3315 * extents to clone above, but shouldn't round up the file size.
3317 if (endoff > destoff + olen)
3318 endoff = destoff + olen;
3319 if (endoff > inode->i_size)
3320 btrfs_i_size_write(BTRFS_I(inode), endoff);
3322 ret = btrfs_update_inode(trans, root, inode);
3324 btrfs_abort_transaction(trans, ret);
3325 btrfs_end_transaction(trans);
3328 ret = btrfs_end_transaction(trans);
3333 static void clone_update_extent_map(struct btrfs_inode *inode,
3334 const struct btrfs_trans_handle *trans,
3335 const struct btrfs_path *path,
3336 const u64 hole_offset,
3339 struct extent_map_tree *em_tree = &inode->extent_tree;
3340 struct extent_map *em;
3343 em = alloc_extent_map();
3345 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
3350 struct btrfs_file_extent_item *fi;
3352 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3353 struct btrfs_file_extent_item);
3354 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3355 em->generation = -1;
3356 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3357 BTRFS_FILE_EXTENT_INLINE)
3358 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3359 &inode->runtime_flags);
3361 em->start = hole_offset;
3363 em->ram_bytes = em->len;
3364 em->orig_start = hole_offset;
3365 em->block_start = EXTENT_MAP_HOLE;
3367 em->orig_block_len = 0;
3368 em->compress_type = BTRFS_COMPRESS_NONE;
3369 em->generation = trans->transid;
3373 write_lock(&em_tree->lock);
3374 ret = add_extent_mapping(em_tree, em, 1);
3375 write_unlock(&em_tree->lock);
3376 if (ret != -EEXIST) {
3377 free_extent_map(em);
3380 btrfs_drop_extent_cache(inode, em->start,
3381 em->start + em->len - 1, 0);
3385 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
3389 * Make sure we do not end up inserting an inline extent into a file that has
3390 * already other (non-inline) extents. If a file has an inline extent it can
3391 * not have any other extents and the (single) inline extent must start at the
3392 * file offset 0. Failing to respect these rules will lead to file corruption,
3393 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3395 * We can have extents that have been already written to disk or we can have
3396 * dirty ranges still in delalloc, in which case the extent maps and items are
3397 * created only when we run delalloc, and the delalloc ranges might fall outside
3398 * the range we are currently locking in the inode's io tree. So we check the
3399 * inode's i_size because of that (i_size updates are done while holding the
3400 * i_mutex, which we are holding here).
3401 * We also check to see if the inode has a size not greater than "datal" but has
3402 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3403 * protected against such concurrent fallocate calls by the i_mutex).
3405 * If the file has no extents but a size greater than datal, do not allow the
3406 * copy because we would need turn the inline extent into a non-inline one (even
3407 * with NO_HOLES enabled). If we find our destination inode only has one inline
3408 * extent, just overwrite it with the source inline extent if its size is less
3409 * than the source extent's size, or we could copy the source inline extent's
3410 * data into the destination inode's inline extent if the later is greater then
3413 static int clone_copy_inline_extent(struct inode *dst,
3414 struct btrfs_trans_handle *trans,
3415 struct btrfs_path *path,
3416 struct btrfs_key *new_key,
3417 const u64 drop_start,
3423 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
3424 struct btrfs_root *root = BTRFS_I(dst)->root;
3425 const u64 aligned_end = ALIGN(new_key->offset + datal,
3426 fs_info->sectorsize);
3428 struct btrfs_key key;
3430 if (new_key->offset > 0)
3433 key.objectid = btrfs_ino(BTRFS_I(dst));
3434 key.type = BTRFS_EXTENT_DATA_KEY;
3436 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3439 } else if (ret > 0) {
3440 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3441 ret = btrfs_next_leaf(root, path);
3445 goto copy_inline_extent;
3447 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3448 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
3449 key.type == BTRFS_EXTENT_DATA_KEY) {
3450 ASSERT(key.offset > 0);
3453 } else if (i_size_read(dst) <= datal) {
3454 struct btrfs_file_extent_item *ei;
3458 * If the file size is <= datal, make sure there are no other
3459 * extents following (can happen do to an fallocate call with
3460 * the flag FALLOC_FL_KEEP_SIZE).
3462 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3463 struct btrfs_file_extent_item);
3465 * If it's an inline extent, it can not have other extents
3468 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3469 BTRFS_FILE_EXTENT_INLINE)
3470 goto copy_inline_extent;
3472 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3473 if (ext_len > aligned_end)
3476 ret = btrfs_next_item(root, path);
3479 } else if (ret == 0) {
3480 btrfs_item_key_to_cpu(path->nodes[0], &key,
3482 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
3483 key.type == BTRFS_EXTENT_DATA_KEY)
3490 * We have no extent items, or we have an extent at offset 0 which may
3491 * or may not be inlined. All these cases are dealt the same way.
3493 if (i_size_read(dst) > datal) {
3495 * If the destination inode has an inline extent...
3496 * This would require copying the data from the source inline
3497 * extent into the beginning of the destination's inline extent.
3498 * But this is really complex, both extents can be compressed
3499 * or just one of them, which would require decompressing and
3500 * re-compressing data (which could increase the new compressed
3501 * size, not allowing the compressed data to fit anymore in an
3503 * So just don't support this case for now (it should be rare,
3504 * we are not really saving space when cloning inline extents).
3509 btrfs_release_path(path);
3510 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3513 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3518 const u32 start = btrfs_file_extent_calc_inline_size(0);
3520 memmove(inline_data + start, inline_data + start + skip, datal);
3523 write_extent_buffer(path->nodes[0], inline_data,
3524 btrfs_item_ptr_offset(path->nodes[0],
3527 inode_add_bytes(dst, datal);
3533 * btrfs_clone() - clone a range from inode file to another
3535 * @src: Inode to clone from
3536 * @inode: Inode to clone to
3537 * @off: Offset within source to start clone from
3538 * @olen: Original length, passed by user, of range to clone
3539 * @olen_aligned: Block-aligned value of olen
3540 * @destoff: Offset within @inode to start clone
3541 * @no_time_update: Whether to update mtime/ctime on the target inode
3543 static int btrfs_clone(struct inode *src, struct inode *inode,
3544 const u64 off, const u64 olen, const u64 olen_aligned,
3545 const u64 destoff, int no_time_update)
3547 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3548 struct btrfs_root *root = BTRFS_I(inode)->root;
3549 struct btrfs_path *path = NULL;
3550 struct extent_buffer *leaf;
3551 struct btrfs_trans_handle *trans;
3553 struct btrfs_key key;
3557 const u64 len = olen_aligned;
3558 u64 last_dest_end = destoff;
3561 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3565 path = btrfs_alloc_path();
3571 path->reada = READA_FORWARD;
3573 key.objectid = btrfs_ino(BTRFS_I(src));
3574 key.type = BTRFS_EXTENT_DATA_KEY;
3578 u64 next_key_min_offset = key.offset + 1;
3581 * note the key will change type as we walk through the
3584 path->leave_spinning = 1;
3585 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3590 * First search, if no extent item that starts at offset off was
3591 * found but the previous item is an extent item, it's possible
3592 * it might overlap our target range, therefore process it.
3594 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3595 btrfs_item_key_to_cpu(path->nodes[0], &key,
3596 path->slots[0] - 1);
3597 if (key.type == BTRFS_EXTENT_DATA_KEY)
3601 nritems = btrfs_header_nritems(path->nodes[0]);
3603 if (path->slots[0] >= nritems) {
3604 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3609 nritems = btrfs_header_nritems(path->nodes[0]);
3611 leaf = path->nodes[0];
3612 slot = path->slots[0];
3614 btrfs_item_key_to_cpu(leaf, &key, slot);
3615 if (key.type > BTRFS_EXTENT_DATA_KEY ||
3616 key.objectid != btrfs_ino(BTRFS_I(src)))
3619 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3620 struct btrfs_file_extent_item *extent;
3623 struct btrfs_key new_key;
3624 u64 disko = 0, diskl = 0;
3625 u64 datao = 0, datal = 0;
3629 extent = btrfs_item_ptr(leaf, slot,
3630 struct btrfs_file_extent_item);
3631 comp = btrfs_file_extent_compression(leaf, extent);
3632 type = btrfs_file_extent_type(leaf, extent);
3633 if (type == BTRFS_FILE_EXTENT_REG ||
3634 type == BTRFS_FILE_EXTENT_PREALLOC) {
3635 disko = btrfs_file_extent_disk_bytenr(leaf,
3637 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3639 datao = btrfs_file_extent_offset(leaf, extent);
3640 datal = btrfs_file_extent_num_bytes(leaf,
3642 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3643 /* take upper bound, may be compressed */
3644 datal = btrfs_file_extent_ram_bytes(leaf,
3649 * The first search might have left us at an extent
3650 * item that ends before our target range's start, can
3651 * happen if we have holes and NO_HOLES feature enabled.
3653 if (key.offset + datal <= off) {
3656 } else if (key.offset >= off + len) {
3659 next_key_min_offset = key.offset + datal;
3660 size = btrfs_item_size_nr(leaf, slot);
3661 read_extent_buffer(leaf, buf,
3662 btrfs_item_ptr_offset(leaf, slot),
3665 btrfs_release_path(path);
3666 path->leave_spinning = 0;
3668 memcpy(&new_key, &key, sizeof(new_key));
3669 new_key.objectid = btrfs_ino(BTRFS_I(inode));
3670 if (off <= key.offset)
3671 new_key.offset = key.offset + destoff - off;
3673 new_key.offset = destoff;
3676 * Deal with a hole that doesn't have an extent item
3677 * that represents it (NO_HOLES feature enabled).
3678 * This hole is either in the middle of the cloning
3679 * range or at the beginning (fully overlaps it or
3680 * partially overlaps it).
3682 if (new_key.offset != last_dest_end)
3683 drop_start = last_dest_end;
3685 drop_start = new_key.offset;
3688 * 1 - adjusting old extent (we may have to split it)
3689 * 1 - add new extent
3692 trans = btrfs_start_transaction(root, 3);
3693 if (IS_ERR(trans)) {
3694 ret = PTR_ERR(trans);
3698 if (type == BTRFS_FILE_EXTENT_REG ||
3699 type == BTRFS_FILE_EXTENT_PREALLOC) {
3701 * a | --- range to clone ---| b
3702 * | ------------- extent ------------- |
3705 /* subtract range b */
3706 if (key.offset + datal > off + len)
3707 datal = off + len - key.offset;
3709 /* subtract range a */
3710 if (off > key.offset) {
3711 datao += off - key.offset;
3712 datal -= off - key.offset;
3715 ret = btrfs_drop_extents(trans, root, inode,
3717 new_key.offset + datal,
3720 if (ret != -EOPNOTSUPP)
3721 btrfs_abort_transaction(trans,
3723 btrfs_end_transaction(trans);
3727 ret = btrfs_insert_empty_item(trans, root, path,
3730 btrfs_abort_transaction(trans, ret);
3731 btrfs_end_transaction(trans);
3735 leaf = path->nodes[0];
3736 slot = path->slots[0];
3737 write_extent_buffer(leaf, buf,
3738 btrfs_item_ptr_offset(leaf, slot),
3741 extent = btrfs_item_ptr(leaf, slot,
3742 struct btrfs_file_extent_item);
3744 /* disko == 0 means it's a hole */
3748 btrfs_set_file_extent_offset(leaf, extent,
3750 btrfs_set_file_extent_num_bytes(leaf, extent,
3754 struct btrfs_ref ref = { 0 };
3755 inode_add_bytes(inode, datal);
3756 btrfs_init_generic_ref(&ref,
3757 BTRFS_ADD_DELAYED_REF, disko,
3759 btrfs_init_data_ref(&ref,
3760 root->root_key.objectid,
3761 btrfs_ino(BTRFS_I(inode)),
3762 new_key.offset - datao);
3763 ret = btrfs_inc_extent_ref(trans, &ref);
3765 btrfs_abort_transaction(trans,
3767 btrfs_end_transaction(trans);
3772 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3776 if (off > key.offset) {
3777 skip = off - key.offset;
3778 new_key.offset += skip;
3781 if (key.offset + datal > off + len)
3782 trim = key.offset + datal - (off + len);
3784 if (comp && (skip || trim)) {
3786 btrfs_end_transaction(trans);
3789 size -= skip + trim;
3790 datal -= skip + trim;
3792 ret = clone_copy_inline_extent(inode,
3799 if (ret != -EOPNOTSUPP)
3800 btrfs_abort_transaction(trans,
3802 btrfs_end_transaction(trans);
3805 leaf = path->nodes[0];
3806 slot = path->slots[0];
3809 /* If we have an implicit hole (NO_HOLES feature). */
3810 if (drop_start < new_key.offset)
3811 clone_update_extent_map(BTRFS_I(inode), trans,
3813 new_key.offset - drop_start);
3815 clone_update_extent_map(BTRFS_I(inode), trans,
3818 btrfs_mark_buffer_dirty(leaf);
3819 btrfs_release_path(path);
3821 last_dest_end = ALIGN(new_key.offset + datal,
3822 fs_info->sectorsize);
3823 ret = clone_finish_inode_update(trans, inode,
3829 if (new_key.offset + datal >= destoff + len)
3832 btrfs_release_path(path);
3833 key.offset = next_key_min_offset;
3835 if (fatal_signal_pending(current)) {
3842 if (last_dest_end < destoff + len) {
3844 * We have an implicit hole (NO_HOLES feature is enabled) that
3845 * fully or partially overlaps our cloning range at its end.
3847 btrfs_release_path(path);
3850 * 1 - remove extent(s)
3853 trans = btrfs_start_transaction(root, 2);
3854 if (IS_ERR(trans)) {
3855 ret = PTR_ERR(trans);
3858 ret = btrfs_drop_extents(trans, root, inode,
3859 last_dest_end, destoff + len, 1);
3861 if (ret != -EOPNOTSUPP)
3862 btrfs_abort_transaction(trans, ret);
3863 btrfs_end_transaction(trans);
3866 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3868 destoff + len - last_dest_end);
3869 ret = clone_finish_inode_update(trans, inode, destoff + len,
3870 destoff, olen, no_time_update);
3874 btrfs_free_path(path);
3879 static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3880 u64 off, u64 olen, u64 destoff)
3882 struct inode *inode = file_inode(file);
3883 struct inode *src = file_inode(file_src);
3884 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3887 u64 bs = fs_info->sb->s_blocksize;
3891 * - split compressed inline extents. annoying: we need to
3892 * decompress into destination's address_space (the file offset
3893 * may change, so source mapping won't do), then recompress (or
3894 * otherwise reinsert) a subrange.
3896 * - split destination inode's inline extents. The inline extents can
3897 * be either compressed or non-compressed.
3901 * VFS's generic_remap_file_range_prep() protects us from cloning the
3902 * eof block into the middle of a file, which would result in corruption
3903 * if the file size is not blocksize aligned. So we don't need to check
3904 * for that case here.
3906 if (off + len == src->i_size)
3907 len = ALIGN(src->i_size, bs) - off;
3909 if (destoff > inode->i_size) {
3910 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3912 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3916 * We may have truncated the last block if the inode's size is
3917 * not sector size aligned, so we need to wait for writeback to
3918 * complete before proceeding further, otherwise we can race
3919 * with cloning and attempt to increment a reference to an
3920 * extent that no longer exists (writeback completed right after
3921 * we found the previous extent covering eof and before we
3922 * attempted to increment its reference count).
3924 ret = btrfs_wait_ordered_range(inode, wb_start,
3925 destoff - wb_start);
3931 * Lock destination range to serialize with concurrent readpages() and
3932 * source range to serialize with relocation.
3934 btrfs_double_extent_lock(src, off, inode, destoff, len);
3935 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
3936 btrfs_double_extent_unlock(src, off, inode, destoff, len);
3938 * Truncate page cache pages so that future reads will see the cloned
3939 * data immediately and not the previous data.
3941 truncate_inode_pages_range(&inode->i_data,
3942 round_down(destoff, PAGE_SIZE),
3943 round_up(destoff + len, PAGE_SIZE) - 1);
3948 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3949 struct file *file_out, loff_t pos_out,
3950 loff_t *len, unsigned int remap_flags)
3952 struct inode *inode_in = file_inode(file_in);
3953 struct inode *inode_out = file_inode(file_out);
3954 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3955 bool same_inode = inode_out == inode_in;
3959 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3960 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3962 if (btrfs_root_readonly(root_out))
3965 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3966 inode_in->i_sb != inode_out->i_sb)
3970 /* don't make the dst file partly checksummed */
3971 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3972 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
3977 * Now that the inodes are locked, we need to start writeback ourselves
3978 * and can not rely on the writeback from the VFS's generic helper
3979 * generic_remap_file_range_prep() because:
3981 * 1) For compression we must call filemap_fdatawrite_range() range
3982 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3983 * helper only calls it once;
3985 * 2) filemap_fdatawrite_range(), called by the generic helper only
3986 * waits for the writeback to complete, i.e. for IO to be done, and
3987 * not for the ordered extents to complete. We need to wait for them
3988 * to complete so that new file extent items are in the fs tree.
3990 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3991 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3993 wb_len = ALIGN(*len, bs);
3996 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3997 * any in progress could create its ordered extents after we wait for
3998 * existing ordered extents below).
4000 inode_dio_wait(inode_in);
4002 inode_dio_wait(inode_out);
4004 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
4008 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
4013 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
4017 loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4018 struct file *dst_file, loff_t destoff, loff_t len,
4019 unsigned int remap_flags)
4021 struct inode *src_inode = file_inode(src_file);
4022 struct inode *dst_inode = file_inode(dst_file);
4023 bool same_inode = dst_inode == src_inode;
4026 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4030 inode_lock(src_inode);
4032 lock_two_nondirectories(src_inode, dst_inode);
4034 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4036 if (ret < 0 || len == 0)
4039 if (remap_flags & REMAP_FILE_DEDUP)
4040 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4042 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
4046 inode_unlock(src_inode);
4048 unlock_two_nondirectories(src_inode, dst_inode);
4050 return ret < 0 ? ret : len;
4053 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4055 struct inode *inode = file_inode(file);
4056 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4057 struct btrfs_root *root = BTRFS_I(inode)->root;
4058 struct btrfs_root *new_root;
4059 struct btrfs_dir_item *di;
4060 struct btrfs_trans_handle *trans;
4061 struct btrfs_path *path;
4062 struct btrfs_key location;
4063 struct btrfs_disk_key disk_key;
4068 if (!capable(CAP_SYS_ADMIN))
4071 ret = mnt_want_write_file(file);
4075 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4081 objectid = BTRFS_FS_TREE_OBJECTID;
4083 location.objectid = objectid;
4084 location.type = BTRFS_ROOT_ITEM_KEY;
4085 location.offset = (u64)-1;
4087 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
4088 if (IS_ERR(new_root)) {
4089 ret = PTR_ERR(new_root);
4092 if (!is_fstree(new_root->root_key.objectid)) {
4097 path = btrfs_alloc_path();
4102 path->leave_spinning = 1;
4104 trans = btrfs_start_transaction(root, 1);
4105 if (IS_ERR(trans)) {
4106 btrfs_free_path(path);
4107 ret = PTR_ERR(trans);
4111 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4112 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
4113 dir_id, "default", 7, 1);
4114 if (IS_ERR_OR_NULL(di)) {
4115 btrfs_free_path(path);
4116 btrfs_end_transaction(trans);
4118 "Umm, you don't have the default diritem, this isn't going to work");
4123 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4124 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4125 btrfs_mark_buffer_dirty(path->nodes[0]);
4126 btrfs_free_path(path);
4128 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
4129 btrfs_end_transaction(trans);
4131 mnt_drop_write_file(file);
4135 static void get_block_group_info(struct list_head *groups_list,
4136 struct btrfs_ioctl_space_info *space)
4138 struct btrfs_block_group_cache *block_group;
4140 space->total_bytes = 0;
4141 space->used_bytes = 0;
4143 list_for_each_entry(block_group, groups_list, list) {
4144 space->flags = block_group->flags;
4145 space->total_bytes += block_group->key.offset;
4146 space->used_bytes +=
4147 btrfs_block_group_used(&block_group->item);
4151 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4154 struct btrfs_ioctl_space_args space_args;
4155 struct btrfs_ioctl_space_info space;
4156 struct btrfs_ioctl_space_info *dest;
4157 struct btrfs_ioctl_space_info *dest_orig;
4158 struct btrfs_ioctl_space_info __user *user_dest;
4159 struct btrfs_space_info *info;
4160 static const u64 types[] = {
4161 BTRFS_BLOCK_GROUP_DATA,
4162 BTRFS_BLOCK_GROUP_SYSTEM,
4163 BTRFS_BLOCK_GROUP_METADATA,
4164 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4172 if (copy_from_user(&space_args,
4173 (struct btrfs_ioctl_space_args __user *)arg,
4174 sizeof(space_args)))
4177 for (i = 0; i < num_types; i++) {
4178 struct btrfs_space_info *tmp;
4182 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4184 if (tmp->flags == types[i]) {
4194 down_read(&info->groups_sem);
4195 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4196 if (!list_empty(&info->block_groups[c]))
4199 up_read(&info->groups_sem);
4203 * Global block reserve, exported as a space_info
4207 /* space_slots == 0 means they are asking for a count */
4208 if (space_args.space_slots == 0) {
4209 space_args.total_spaces = slot_count;
4213 slot_count = min_t(u64, space_args.space_slots, slot_count);
4215 alloc_size = sizeof(*dest) * slot_count;
4217 /* we generally have at most 6 or so space infos, one for each raid
4218 * level. So, a whole page should be more than enough for everyone
4220 if (alloc_size > PAGE_SIZE)
4223 space_args.total_spaces = 0;
4224 dest = kmalloc(alloc_size, GFP_KERNEL);
4229 /* now we have a buffer to copy into */
4230 for (i = 0; i < num_types; i++) {
4231 struct btrfs_space_info *tmp;
4238 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4240 if (tmp->flags == types[i]) {
4249 down_read(&info->groups_sem);
4250 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4251 if (!list_empty(&info->block_groups[c])) {
4252 get_block_group_info(&info->block_groups[c],
4254 memcpy(dest, &space, sizeof(space));
4256 space_args.total_spaces++;
4262 up_read(&info->groups_sem);
4266 * Add global block reserve
4269 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4271 spin_lock(&block_rsv->lock);
4272 space.total_bytes = block_rsv->size;
4273 space.used_bytes = block_rsv->size - block_rsv->reserved;
4274 spin_unlock(&block_rsv->lock);
4275 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4276 memcpy(dest, &space, sizeof(space));
4277 space_args.total_spaces++;
4280 user_dest = (struct btrfs_ioctl_space_info __user *)
4281 (arg + sizeof(struct btrfs_ioctl_space_args));
4283 if (copy_to_user(user_dest, dest_orig, alloc_size))
4288 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
4294 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4297 struct btrfs_trans_handle *trans;
4301 trans = btrfs_attach_transaction_barrier(root);
4302 if (IS_ERR(trans)) {
4303 if (PTR_ERR(trans) != -ENOENT)
4304 return PTR_ERR(trans);
4306 /* No running transaction, don't bother */
4307 transid = root->fs_info->last_trans_committed;
4310 transid = trans->transid;
4311 ret = btrfs_commit_transaction_async(trans, 0);
4313 btrfs_end_transaction(trans);
4318 if (copy_to_user(argp, &transid, sizeof(transid)))
4323 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
4329 if (copy_from_user(&transid, argp, sizeof(transid)))
4332 transid = 0; /* current trans */
4334 return btrfs_wait_for_commit(fs_info, transid);
4337 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4339 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
4340 struct btrfs_ioctl_scrub_args *sa;
4343 if (!capable(CAP_SYS_ADMIN))
4346 sa = memdup_user(arg, sizeof(*sa));
4350 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4351 ret = mnt_want_write_file(file);
4356 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
4357 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4360 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4363 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4364 mnt_drop_write_file(file);
4370 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
4372 if (!capable(CAP_SYS_ADMIN))
4375 return btrfs_scrub_cancel(fs_info);
4378 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
4381 struct btrfs_ioctl_scrub_args *sa;
4384 if (!capable(CAP_SYS_ADMIN))
4387 sa = memdup_user(arg, sizeof(*sa));
4391 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
4393 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4400 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
4403 struct btrfs_ioctl_get_dev_stats *sa;
4406 sa = memdup_user(arg, sizeof(*sa));
4410 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4415 ret = btrfs_get_dev_stats(fs_info, sa);
4417 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4424 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4427 struct btrfs_ioctl_dev_replace_args *p;
4430 if (!capable(CAP_SYS_ADMIN))
4433 p = memdup_user(arg, sizeof(*p));
4438 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
4439 if (sb_rdonly(fs_info->sb)) {
4443 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
4444 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4446 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
4447 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4450 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4451 btrfs_dev_replace_status(fs_info, p);
4454 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4455 p->result = btrfs_dev_replace_cancel(fs_info);
4463 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
4470 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4476 struct btrfs_ioctl_ino_path_args *ipa = NULL;
4477 struct inode_fs_paths *ipath = NULL;
4478 struct btrfs_path *path;
4480 if (!capable(CAP_DAC_READ_SEARCH))
4483 path = btrfs_alloc_path();
4489 ipa = memdup_user(arg, sizeof(*ipa));
4496 size = min_t(u32, ipa->size, 4096);
4497 ipath = init_ipath(size, root, path);
4498 if (IS_ERR(ipath)) {
4499 ret = PTR_ERR(ipath);
4504 ret = paths_from_inode(ipa->inum, ipath);
4508 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
4509 rel_ptr = ipath->fspath->val[i] -
4510 (u64)(unsigned long)ipath->fspath->val;
4511 ipath->fspath->val[i] = rel_ptr;
4514 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4515 ipath->fspath, size);
4522 btrfs_free_path(path);
4529 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4531 struct btrfs_data_container *inodes = ctx;
4532 const size_t c = 3 * sizeof(u64);
4534 if (inodes->bytes_left >= c) {
4535 inodes->bytes_left -= c;
4536 inodes->val[inodes->elem_cnt] = inum;
4537 inodes->val[inodes->elem_cnt + 1] = offset;
4538 inodes->val[inodes->elem_cnt + 2] = root;
4539 inodes->elem_cnt += 3;
4541 inodes->bytes_missing += c - inodes->bytes_left;
4542 inodes->bytes_left = 0;
4543 inodes->elem_missed += 3;
4549 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
4550 void __user *arg, int version)
4554 struct btrfs_ioctl_logical_ino_args *loi;
4555 struct btrfs_data_container *inodes = NULL;
4556 struct btrfs_path *path = NULL;
4559 if (!capable(CAP_SYS_ADMIN))
4562 loi = memdup_user(arg, sizeof(*loi));
4564 return PTR_ERR(loi);
4567 ignore_offset = false;
4568 size = min_t(u32, loi->size, SZ_64K);
4570 /* All reserved bits must be 0 for now */
4571 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4575 /* Only accept flags we have defined so far */
4576 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4580 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
4581 size = min_t(u32, loi->size, SZ_16M);
4584 path = btrfs_alloc_path();
4590 inodes = init_data_container(size);
4591 if (IS_ERR(inodes)) {
4592 ret = PTR_ERR(inodes);
4597 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
4598 build_ino_list, inodes, ignore_offset);
4604 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4610 btrfs_free_path(path);
4618 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
4619 struct btrfs_ioctl_balance_args *bargs)
4621 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4623 bargs->flags = bctl->flags;
4625 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
4626 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4627 if (atomic_read(&fs_info->balance_pause_req))
4628 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4629 if (atomic_read(&fs_info->balance_cancel_req))
4630 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4632 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4633 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4634 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
4636 spin_lock(&fs_info->balance_lock);
4637 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4638 spin_unlock(&fs_info->balance_lock);
4641 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4643 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4644 struct btrfs_fs_info *fs_info = root->fs_info;
4645 struct btrfs_ioctl_balance_args *bargs;
4646 struct btrfs_balance_control *bctl;
4647 bool need_unlock; /* for mut. excl. ops lock */
4650 if (!capable(CAP_SYS_ADMIN))
4653 ret = mnt_want_write_file(file);
4658 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
4659 mutex_lock(&fs_info->balance_mutex);
4665 * mut. excl. ops lock is locked. Three possibilities:
4666 * (1) some other op is running
4667 * (2) balance is running
4668 * (3) balance is paused -- special case (think resume)
4670 mutex_lock(&fs_info->balance_mutex);
4671 if (fs_info->balance_ctl) {
4672 /* this is either (2) or (3) */
4673 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4674 mutex_unlock(&fs_info->balance_mutex);
4676 * Lock released to allow other waiters to continue,
4677 * we'll reexamine the status again.
4679 mutex_lock(&fs_info->balance_mutex);
4681 if (fs_info->balance_ctl &&
4682 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4684 need_unlock = false;
4688 mutex_unlock(&fs_info->balance_mutex);
4692 mutex_unlock(&fs_info->balance_mutex);
4698 mutex_unlock(&fs_info->balance_mutex);
4699 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4704 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
4707 bargs = memdup_user(arg, sizeof(*bargs));
4708 if (IS_ERR(bargs)) {
4709 ret = PTR_ERR(bargs);
4713 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4714 if (!fs_info->balance_ctl) {
4719 bctl = fs_info->balance_ctl;
4720 spin_lock(&fs_info->balance_lock);
4721 bctl->flags |= BTRFS_BALANCE_RESUME;
4722 spin_unlock(&fs_info->balance_lock);
4730 if (fs_info->balance_ctl) {
4735 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4742 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4743 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4744 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4746 bctl->flags = bargs->flags;
4748 /* balance everything - no filters */
4749 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4752 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4759 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4760 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4761 * restriper was paused all the way until unmount, in free_fs_info.
4762 * The flag should be cleared after reset_balance_state.
4764 need_unlock = false;
4766 ret = btrfs_balance(fs_info, bctl, bargs);
4769 if ((ret == 0 || ret == -ECANCELED) && arg) {
4770 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4779 mutex_unlock(&fs_info->balance_mutex);
4781 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4783 mnt_drop_write_file(file);
4787 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4789 if (!capable(CAP_SYS_ADMIN))
4793 case BTRFS_BALANCE_CTL_PAUSE:
4794 return btrfs_pause_balance(fs_info);
4795 case BTRFS_BALANCE_CTL_CANCEL:
4796 return btrfs_cancel_balance(fs_info);
4802 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4805 struct btrfs_ioctl_balance_args *bargs;
4808 if (!capable(CAP_SYS_ADMIN))
4811 mutex_lock(&fs_info->balance_mutex);
4812 if (!fs_info->balance_ctl) {
4817 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4823 btrfs_update_ioctl_balance_args(fs_info, bargs);
4825 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4830 mutex_unlock(&fs_info->balance_mutex);
4834 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4836 struct inode *inode = file_inode(file);
4837 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4838 struct btrfs_ioctl_quota_ctl_args *sa;
4841 if (!capable(CAP_SYS_ADMIN))
4844 ret = mnt_want_write_file(file);
4848 sa = memdup_user(arg, sizeof(*sa));
4854 down_write(&fs_info->subvol_sem);
4857 case BTRFS_QUOTA_CTL_ENABLE:
4858 ret = btrfs_quota_enable(fs_info);
4860 case BTRFS_QUOTA_CTL_DISABLE:
4861 ret = btrfs_quota_disable(fs_info);
4869 up_write(&fs_info->subvol_sem);
4871 mnt_drop_write_file(file);
4875 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4877 struct inode *inode = file_inode(file);
4878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4879 struct btrfs_root *root = BTRFS_I(inode)->root;
4880 struct btrfs_ioctl_qgroup_assign_args *sa;
4881 struct btrfs_trans_handle *trans;
4885 if (!capable(CAP_SYS_ADMIN))
4888 ret = mnt_want_write_file(file);
4892 sa = memdup_user(arg, sizeof(*sa));
4898 trans = btrfs_join_transaction(root);
4899 if (IS_ERR(trans)) {
4900 ret = PTR_ERR(trans);
4905 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4907 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4910 /* update qgroup status and info */
4911 err = btrfs_run_qgroups(trans);
4913 btrfs_handle_fs_error(fs_info, err,
4914 "failed to update qgroup status and info");
4915 err = btrfs_end_transaction(trans);
4922 mnt_drop_write_file(file);
4926 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4928 struct inode *inode = file_inode(file);
4929 struct btrfs_root *root = BTRFS_I(inode)->root;
4930 struct btrfs_ioctl_qgroup_create_args *sa;
4931 struct btrfs_trans_handle *trans;
4935 if (!capable(CAP_SYS_ADMIN))
4938 ret = mnt_want_write_file(file);
4942 sa = memdup_user(arg, sizeof(*sa));
4948 if (!sa->qgroupid) {
4953 trans = btrfs_join_transaction(root);
4954 if (IS_ERR(trans)) {
4955 ret = PTR_ERR(trans);
4960 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4962 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4965 err = btrfs_end_transaction(trans);
4972 mnt_drop_write_file(file);
4976 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4978 struct inode *inode = file_inode(file);
4979 struct btrfs_root *root = BTRFS_I(inode)->root;
4980 struct btrfs_ioctl_qgroup_limit_args *sa;
4981 struct btrfs_trans_handle *trans;
4986 if (!capable(CAP_SYS_ADMIN))
4989 ret = mnt_want_write_file(file);
4993 sa = memdup_user(arg, sizeof(*sa));
4999 trans = btrfs_join_transaction(root);
5000 if (IS_ERR(trans)) {
5001 ret = PTR_ERR(trans);
5005 qgroupid = sa->qgroupid;
5007 /* take the current subvol as qgroup */
5008 qgroupid = root->root_key.objectid;
5011 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
5013 err = btrfs_end_transaction(trans);
5020 mnt_drop_write_file(file);
5024 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5026 struct inode *inode = file_inode(file);
5027 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5028 struct btrfs_ioctl_quota_rescan_args *qsa;
5031 if (!capable(CAP_SYS_ADMIN))
5034 ret = mnt_want_write_file(file);
5038 qsa = memdup_user(arg, sizeof(*qsa));
5049 ret = btrfs_qgroup_rescan(fs_info);
5054 mnt_drop_write_file(file);
5058 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5060 struct inode *inode = file_inode(file);
5061 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5062 struct btrfs_ioctl_quota_rescan_args *qsa;
5065 if (!capable(CAP_SYS_ADMIN))
5068 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
5072 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5074 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
5077 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5084 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5086 struct inode *inode = file_inode(file);
5087 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5089 if (!capable(CAP_SYS_ADMIN))
5092 return btrfs_qgroup_wait_for_completion(fs_info, true);
5095 static long _btrfs_ioctl_set_received_subvol(struct file *file,
5096 struct btrfs_ioctl_received_subvol_args *sa)
5098 struct inode *inode = file_inode(file);
5099 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5100 struct btrfs_root *root = BTRFS_I(inode)->root;
5101 struct btrfs_root_item *root_item = &root->root_item;
5102 struct btrfs_trans_handle *trans;
5103 struct timespec64 ct = current_time(inode);
5105 int received_uuid_changed;
5107 if (!inode_owner_or_capable(inode))
5110 ret = mnt_want_write_file(file);
5114 down_write(&fs_info->subvol_sem);
5116 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
5121 if (btrfs_root_readonly(root)) {
5128 * 2 - uuid items (received uuid + subvol uuid)
5130 trans = btrfs_start_transaction(root, 3);
5131 if (IS_ERR(trans)) {
5132 ret = PTR_ERR(trans);
5137 sa->rtransid = trans->transid;
5138 sa->rtime.sec = ct.tv_sec;
5139 sa->rtime.nsec = ct.tv_nsec;
5141 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5143 if (received_uuid_changed &&
5144 !btrfs_is_empty_uuid(root_item->received_uuid)) {
5145 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
5146 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5147 root->root_key.objectid);
5148 if (ret && ret != -ENOENT) {
5149 btrfs_abort_transaction(trans, ret);
5150 btrfs_end_transaction(trans);
5154 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5155 btrfs_set_root_stransid(root_item, sa->stransid);
5156 btrfs_set_root_rtransid(root_item, sa->rtransid);
5157 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5158 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5159 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5160 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
5162 ret = btrfs_update_root(trans, fs_info->tree_root,
5163 &root->root_key, &root->root_item);
5165 btrfs_end_transaction(trans);
5168 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5169 ret = btrfs_uuid_tree_add(trans, sa->uuid,
5170 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5171 root->root_key.objectid);
5172 if (ret < 0 && ret != -EEXIST) {
5173 btrfs_abort_transaction(trans, ret);
5174 btrfs_end_transaction(trans);
5178 ret = btrfs_commit_transaction(trans);
5180 up_write(&fs_info->subvol_sem);
5181 mnt_drop_write_file(file);
5186 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5189 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5190 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5193 args32 = memdup_user(arg, sizeof(*args32));
5195 return PTR_ERR(args32);
5197 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
5203 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5204 args64->stransid = args32->stransid;
5205 args64->rtransid = args32->rtransid;
5206 args64->stime.sec = args32->stime.sec;
5207 args64->stime.nsec = args32->stime.nsec;
5208 args64->rtime.sec = args32->rtime.sec;
5209 args64->rtime.nsec = args32->rtime.nsec;
5210 args64->flags = args32->flags;
5212 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5216 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5217 args32->stransid = args64->stransid;
5218 args32->rtransid = args64->rtransid;
5219 args32->stime.sec = args64->stime.sec;
5220 args32->stime.nsec = args64->stime.nsec;
5221 args32->rtime.sec = args64->rtime.sec;
5222 args32->rtime.nsec = args64->rtime.nsec;
5223 args32->flags = args64->flags;
5225 ret = copy_to_user(arg, args32, sizeof(*args32));
5236 static long btrfs_ioctl_set_received_subvol(struct file *file,
5239 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5242 sa = memdup_user(arg, sizeof(*sa));
5246 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5251 ret = copy_to_user(arg, sa, sizeof(*sa));
5260 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5262 struct inode *inode = file_inode(file);
5263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5266 char label[BTRFS_LABEL_SIZE];
5268 spin_lock(&fs_info->super_lock);
5269 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5270 spin_unlock(&fs_info->super_lock);
5272 len = strnlen(label, BTRFS_LABEL_SIZE);
5274 if (len == BTRFS_LABEL_SIZE) {
5276 "label is too long, return the first %zu bytes",
5280 ret = copy_to_user(arg, label, len);
5282 return ret ? -EFAULT : 0;
5285 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5287 struct inode *inode = file_inode(file);
5288 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5289 struct btrfs_root *root = BTRFS_I(inode)->root;
5290 struct btrfs_super_block *super_block = fs_info->super_copy;
5291 struct btrfs_trans_handle *trans;
5292 char label[BTRFS_LABEL_SIZE];
5295 if (!capable(CAP_SYS_ADMIN))
5298 if (copy_from_user(label, arg, sizeof(label)))
5301 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
5303 "unable to set label with more than %d bytes",
5304 BTRFS_LABEL_SIZE - 1);
5308 ret = mnt_want_write_file(file);
5312 trans = btrfs_start_transaction(root, 0);
5313 if (IS_ERR(trans)) {
5314 ret = PTR_ERR(trans);
5318 spin_lock(&fs_info->super_lock);
5319 strcpy(super_block->label, label);
5320 spin_unlock(&fs_info->super_lock);
5321 ret = btrfs_commit_transaction(trans);
5324 mnt_drop_write_file(file);
5328 #define INIT_FEATURE_FLAGS(suffix) \
5329 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5330 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5331 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5333 int btrfs_ioctl_get_supported_features(void __user *arg)
5335 static const struct btrfs_ioctl_feature_flags features[3] = {
5336 INIT_FEATURE_FLAGS(SUPP),
5337 INIT_FEATURE_FLAGS(SAFE_SET),
5338 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5341 if (copy_to_user(arg, &features, sizeof(features)))
5347 static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5349 struct inode *inode = file_inode(file);
5350 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5351 struct btrfs_super_block *super_block = fs_info->super_copy;
5352 struct btrfs_ioctl_feature_flags features;
5354 features.compat_flags = btrfs_super_compat_flags(super_block);
5355 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5356 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5358 if (copy_to_user(arg, &features, sizeof(features)))
5364 static int check_feature_bits(struct btrfs_fs_info *fs_info,
5365 enum btrfs_feature_set set,
5366 u64 change_mask, u64 flags, u64 supported_flags,
5367 u64 safe_set, u64 safe_clear)
5369 const char *type = btrfs_feature_set_names[set];
5371 u64 disallowed, unsupported;
5372 u64 set_mask = flags & change_mask;
5373 u64 clear_mask = ~flags & change_mask;
5375 unsupported = set_mask & ~supported_flags;
5377 names = btrfs_printable_features(set, unsupported);
5380 "this kernel does not support the %s feature bit%s",
5381 names, strchr(names, ',') ? "s" : "");
5385 "this kernel does not support %s bits 0x%llx",
5390 disallowed = set_mask & ~safe_set;
5392 names = btrfs_printable_features(set, disallowed);
5395 "can't set the %s feature bit%s while mounted",
5396 names, strchr(names, ',') ? "s" : "");
5400 "can't set %s bits 0x%llx while mounted",
5405 disallowed = clear_mask & ~safe_clear;
5407 names = btrfs_printable_features(set, disallowed);
5410 "can't clear the %s feature bit%s while mounted",
5411 names, strchr(names, ',') ? "s" : "");
5415 "can't clear %s bits 0x%llx while mounted",
5423 #define check_feature(fs_info, change_mask, flags, mask_base) \
5424 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
5425 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5426 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5427 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5429 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5431 struct inode *inode = file_inode(file);
5432 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5433 struct btrfs_root *root = BTRFS_I(inode)->root;
5434 struct btrfs_super_block *super_block = fs_info->super_copy;
5435 struct btrfs_ioctl_feature_flags flags[2];
5436 struct btrfs_trans_handle *trans;
5440 if (!capable(CAP_SYS_ADMIN))
5443 if (copy_from_user(flags, arg, sizeof(flags)))
5447 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5448 !flags[0].incompat_flags)
5451 ret = check_feature(fs_info, flags[0].compat_flags,
5452 flags[1].compat_flags, COMPAT);
5456 ret = check_feature(fs_info, flags[0].compat_ro_flags,
5457 flags[1].compat_ro_flags, COMPAT_RO);
5461 ret = check_feature(fs_info, flags[0].incompat_flags,
5462 flags[1].incompat_flags, INCOMPAT);
5466 ret = mnt_want_write_file(file);
5470 trans = btrfs_start_transaction(root, 0);
5471 if (IS_ERR(trans)) {
5472 ret = PTR_ERR(trans);
5473 goto out_drop_write;
5476 spin_lock(&fs_info->super_lock);
5477 newflags = btrfs_super_compat_flags(super_block);
5478 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5479 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5480 btrfs_set_super_compat_flags(super_block, newflags);
5482 newflags = btrfs_super_compat_ro_flags(super_block);
5483 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5484 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5485 btrfs_set_super_compat_ro_flags(super_block, newflags);
5487 newflags = btrfs_super_incompat_flags(super_block);
5488 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5489 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5490 btrfs_set_super_incompat_flags(super_block, newflags);
5491 spin_unlock(&fs_info->super_lock);
5493 ret = btrfs_commit_transaction(trans);
5495 mnt_drop_write_file(file);
5500 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5502 struct btrfs_ioctl_send_args *arg;
5506 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5507 struct btrfs_ioctl_send_args_32 args32;
5509 ret = copy_from_user(&args32, argp, sizeof(args32));
5512 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5515 arg->send_fd = args32.send_fd;
5516 arg->clone_sources_count = args32.clone_sources_count;
5517 arg->clone_sources = compat_ptr(args32.clone_sources);
5518 arg->parent_root = args32.parent_root;
5519 arg->flags = args32.flags;
5520 memcpy(arg->reserved, args32.reserved,
5521 sizeof(args32.reserved));
5526 arg = memdup_user(argp, sizeof(*arg));
5528 return PTR_ERR(arg);
5530 ret = btrfs_ioctl_send(file, arg);
5535 long btrfs_ioctl(struct file *file, unsigned int
5536 cmd, unsigned long arg)
5538 struct inode *inode = file_inode(file);
5539 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5540 struct btrfs_root *root = BTRFS_I(inode)->root;
5541 void __user *argp = (void __user *)arg;
5544 case FS_IOC_GETFLAGS:
5545 return btrfs_ioctl_getflags(file, argp);
5546 case FS_IOC_SETFLAGS:
5547 return btrfs_ioctl_setflags(file, argp);
5548 case FS_IOC_GETVERSION:
5549 return btrfs_ioctl_getversion(file, argp);
5551 return btrfs_ioctl_fitrim(file, argp);
5552 case BTRFS_IOC_SNAP_CREATE:
5553 return btrfs_ioctl_snap_create(file, argp, 0);
5554 case BTRFS_IOC_SNAP_CREATE_V2:
5555 return btrfs_ioctl_snap_create_v2(file, argp, 0);
5556 case BTRFS_IOC_SUBVOL_CREATE:
5557 return btrfs_ioctl_snap_create(file, argp, 1);
5558 case BTRFS_IOC_SUBVOL_CREATE_V2:
5559 return btrfs_ioctl_snap_create_v2(file, argp, 1);
5560 case BTRFS_IOC_SNAP_DESTROY:
5561 return btrfs_ioctl_snap_destroy(file, argp);
5562 case BTRFS_IOC_SUBVOL_GETFLAGS:
5563 return btrfs_ioctl_subvol_getflags(file, argp);
5564 case BTRFS_IOC_SUBVOL_SETFLAGS:
5565 return btrfs_ioctl_subvol_setflags(file, argp);
5566 case BTRFS_IOC_DEFAULT_SUBVOL:
5567 return btrfs_ioctl_default_subvol(file, argp);
5568 case BTRFS_IOC_DEFRAG:
5569 return btrfs_ioctl_defrag(file, NULL);
5570 case BTRFS_IOC_DEFRAG_RANGE:
5571 return btrfs_ioctl_defrag(file, argp);
5572 case BTRFS_IOC_RESIZE:
5573 return btrfs_ioctl_resize(file, argp);
5574 case BTRFS_IOC_ADD_DEV:
5575 return btrfs_ioctl_add_dev(fs_info, argp);
5576 case BTRFS_IOC_RM_DEV:
5577 return btrfs_ioctl_rm_dev(file, argp);
5578 case BTRFS_IOC_RM_DEV_V2:
5579 return btrfs_ioctl_rm_dev_v2(file, argp);
5580 case BTRFS_IOC_FS_INFO:
5581 return btrfs_ioctl_fs_info(fs_info, argp);
5582 case BTRFS_IOC_DEV_INFO:
5583 return btrfs_ioctl_dev_info(fs_info, argp);
5584 case BTRFS_IOC_BALANCE:
5585 return btrfs_ioctl_balance(file, NULL);
5586 case BTRFS_IOC_TREE_SEARCH:
5587 return btrfs_ioctl_tree_search(file, argp);
5588 case BTRFS_IOC_TREE_SEARCH_V2:
5589 return btrfs_ioctl_tree_search_v2(file, argp);
5590 case BTRFS_IOC_INO_LOOKUP:
5591 return btrfs_ioctl_ino_lookup(file, argp);
5592 case BTRFS_IOC_INO_PATHS:
5593 return btrfs_ioctl_ino_to_path(root, argp);
5594 case BTRFS_IOC_LOGICAL_INO:
5595 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5596 case BTRFS_IOC_LOGICAL_INO_V2:
5597 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
5598 case BTRFS_IOC_SPACE_INFO:
5599 return btrfs_ioctl_space_info(fs_info, argp);
5600 case BTRFS_IOC_SYNC: {
5603 ret = btrfs_start_delalloc_roots(fs_info, -1);
5606 ret = btrfs_sync_fs(inode->i_sb, 1);
5608 * The transaction thread may want to do more work,
5609 * namely it pokes the cleaner kthread that will start
5610 * processing uncleaned subvols.
5612 wake_up_process(fs_info->transaction_kthread);
5615 case BTRFS_IOC_START_SYNC:
5616 return btrfs_ioctl_start_sync(root, argp);
5617 case BTRFS_IOC_WAIT_SYNC:
5618 return btrfs_ioctl_wait_sync(fs_info, argp);
5619 case BTRFS_IOC_SCRUB:
5620 return btrfs_ioctl_scrub(file, argp);
5621 case BTRFS_IOC_SCRUB_CANCEL:
5622 return btrfs_ioctl_scrub_cancel(fs_info);
5623 case BTRFS_IOC_SCRUB_PROGRESS:
5624 return btrfs_ioctl_scrub_progress(fs_info, argp);
5625 case BTRFS_IOC_BALANCE_V2:
5626 return btrfs_ioctl_balance(file, argp);
5627 case BTRFS_IOC_BALANCE_CTL:
5628 return btrfs_ioctl_balance_ctl(fs_info, arg);
5629 case BTRFS_IOC_BALANCE_PROGRESS:
5630 return btrfs_ioctl_balance_progress(fs_info, argp);
5631 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5632 return btrfs_ioctl_set_received_subvol(file, argp);
5634 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5635 return btrfs_ioctl_set_received_subvol_32(file, argp);
5637 case BTRFS_IOC_SEND:
5638 return _btrfs_ioctl_send(file, argp, false);
5639 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5640 case BTRFS_IOC_SEND_32:
5641 return _btrfs_ioctl_send(file, argp, true);
5643 case BTRFS_IOC_GET_DEV_STATS:
5644 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5645 case BTRFS_IOC_QUOTA_CTL:
5646 return btrfs_ioctl_quota_ctl(file, argp);
5647 case BTRFS_IOC_QGROUP_ASSIGN:
5648 return btrfs_ioctl_qgroup_assign(file, argp);
5649 case BTRFS_IOC_QGROUP_CREATE:
5650 return btrfs_ioctl_qgroup_create(file, argp);
5651 case BTRFS_IOC_QGROUP_LIMIT:
5652 return btrfs_ioctl_qgroup_limit(file, argp);
5653 case BTRFS_IOC_QUOTA_RESCAN:
5654 return btrfs_ioctl_quota_rescan(file, argp);
5655 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5656 return btrfs_ioctl_quota_rescan_status(file, argp);
5657 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5658 return btrfs_ioctl_quota_rescan_wait(file, argp);
5659 case BTRFS_IOC_DEV_REPLACE:
5660 return btrfs_ioctl_dev_replace(fs_info, argp);
5661 case BTRFS_IOC_GET_FSLABEL:
5662 return btrfs_ioctl_get_fslabel(file, argp);
5663 case BTRFS_IOC_SET_FSLABEL:
5664 return btrfs_ioctl_set_fslabel(file, argp);
5665 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5666 return btrfs_ioctl_get_supported_features(argp);
5667 case BTRFS_IOC_GET_FEATURES:
5668 return btrfs_ioctl_get_features(file, argp);
5669 case BTRFS_IOC_SET_FEATURES:
5670 return btrfs_ioctl_set_features(file, argp);
5671 case FS_IOC_FSGETXATTR:
5672 return btrfs_ioctl_fsgetxattr(file, argp);
5673 case FS_IOC_FSSETXATTR:
5674 return btrfs_ioctl_fssetxattr(file, argp);
5675 case BTRFS_IOC_GET_SUBVOL_INFO:
5676 return btrfs_ioctl_get_subvol_info(file, argp);
5677 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5678 return btrfs_ioctl_get_subvol_rootref(file, argp);
5679 case BTRFS_IOC_INO_LOOKUP_USER:
5680 return btrfs_ioctl_ino_lookup_user(file, argp);
5686 #ifdef CONFIG_COMPAT
5687 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5690 * These all access 32-bit values anyway so no further
5691 * handling is necessary.
5694 case FS_IOC32_GETFLAGS:
5695 cmd = FS_IOC_GETFLAGS;
5697 case FS_IOC32_SETFLAGS:
5698 cmd = FS_IOC_SETFLAGS;
5700 case FS_IOC32_GETVERSION:
5701 cmd = FS_IOC_GETVERSION;
5705 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));