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);
316 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
318 if (ret && ret != -ENODATA) {
319 btrfs_abort_transaction(trans, ret);
324 binode->flags = binode_flags;
325 btrfs_sync_inode_flags_to_i_flags(inode);
326 inode_inc_iversion(inode);
327 inode->i_ctime = current_time(inode);
328 ret = btrfs_update_inode(trans, root, inode);
331 btrfs_end_transaction(trans);
334 mnt_drop_write_file(file);
339 * Translate btrfs internal inode flags to xflags as expected by the
340 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
343 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
345 unsigned int xflags = 0;
347 if (flags & BTRFS_INODE_APPEND)
348 xflags |= FS_XFLAG_APPEND;
349 if (flags & BTRFS_INODE_IMMUTABLE)
350 xflags |= FS_XFLAG_IMMUTABLE;
351 if (flags & BTRFS_INODE_NOATIME)
352 xflags |= FS_XFLAG_NOATIME;
353 if (flags & BTRFS_INODE_NODUMP)
354 xflags |= FS_XFLAG_NODUMP;
355 if (flags & BTRFS_INODE_SYNC)
356 xflags |= FS_XFLAG_SYNC;
361 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
362 static int check_xflags(unsigned int flags)
364 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
365 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
371 * Set the xflags from the internal inode flags. The remaining items of fsxattr
374 static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
376 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
379 memset(&fa, 0, sizeof(fa));
380 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
382 if (copy_to_user(arg, &fa, sizeof(fa)))
388 static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
390 struct inode *inode = file_inode(file);
391 struct btrfs_inode *binode = BTRFS_I(inode);
392 struct btrfs_root *root = binode->root;
393 struct btrfs_trans_handle *trans;
396 unsigned old_i_flags;
399 if (!inode_owner_or_capable(inode))
402 if (btrfs_root_readonly(root))
405 memset(&fa, 0, sizeof(fa));
406 if (copy_from_user(&fa, arg, sizeof(fa)))
409 ret = check_xflags(fa.fsx_xflags);
413 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
416 ret = mnt_want_write_file(file);
422 old_flags = binode->flags;
423 old_i_flags = inode->i_flags;
425 /* We need the capabilities to change append-only or immutable inode */
426 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
427 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
428 !capable(CAP_LINUX_IMMUTABLE)) {
433 if (fa.fsx_xflags & FS_XFLAG_SYNC)
434 binode->flags |= BTRFS_INODE_SYNC;
436 binode->flags &= ~BTRFS_INODE_SYNC;
437 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
438 binode->flags |= BTRFS_INODE_IMMUTABLE;
440 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
441 if (fa.fsx_xflags & FS_XFLAG_APPEND)
442 binode->flags |= BTRFS_INODE_APPEND;
444 binode->flags &= ~BTRFS_INODE_APPEND;
445 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
446 binode->flags |= BTRFS_INODE_NODUMP;
448 binode->flags &= ~BTRFS_INODE_NODUMP;
449 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
450 binode->flags |= BTRFS_INODE_NOATIME;
452 binode->flags &= ~BTRFS_INODE_NOATIME;
454 /* 1 item for the inode */
455 trans = btrfs_start_transaction(root, 1);
457 ret = PTR_ERR(trans);
461 btrfs_sync_inode_flags_to_i_flags(inode);
462 inode_inc_iversion(inode);
463 inode->i_ctime = current_time(inode);
464 ret = btrfs_update_inode(trans, root, inode);
466 btrfs_end_transaction(trans);
470 binode->flags = old_flags;
471 inode->i_flags = old_i_flags;
475 mnt_drop_write_file(file);
480 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
482 struct inode *inode = file_inode(file);
484 return put_user(inode->i_generation, arg);
487 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
489 struct inode *inode = file_inode(file);
490 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
491 struct btrfs_device *device;
492 struct request_queue *q;
493 struct fstrim_range range;
494 u64 minlen = ULLONG_MAX;
498 if (!capable(CAP_SYS_ADMIN))
502 * If the fs is mounted with nologreplay, which requires it to be
503 * mounted in RO mode as well, we can not allow discard on free space
504 * inside block groups, because log trees refer to extents that are not
505 * pinned in a block group's free space cache (pinning the extents is
506 * precisely the first phase of replaying a log tree).
508 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
512 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
516 q = bdev_get_queue(device->bdev);
517 if (blk_queue_discard(q)) {
519 minlen = min_t(u64, q->limits.discard_granularity,
527 if (copy_from_user(&range, arg, sizeof(range)))
531 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
532 * block group is in the logical address space, which can be any
533 * sectorsize aligned bytenr in the range [0, U64_MAX].
535 if (range.len < fs_info->sb->s_blocksize)
538 range.minlen = max(range.minlen, minlen);
539 ret = btrfs_trim_fs(fs_info, &range);
543 if (copy_to_user(arg, &range, sizeof(range)))
549 int btrfs_is_empty_uuid(u8 *uuid)
553 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
560 static noinline int create_subvol(struct inode *dir,
561 struct dentry *dentry,
562 const char *name, int namelen,
564 struct btrfs_qgroup_inherit *inherit)
566 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
567 struct btrfs_trans_handle *trans;
568 struct btrfs_key key;
569 struct btrfs_root_item *root_item;
570 struct btrfs_inode_item *inode_item;
571 struct extent_buffer *leaf;
572 struct btrfs_root *root = BTRFS_I(dir)->root;
573 struct btrfs_root *new_root;
574 struct btrfs_block_rsv block_rsv;
575 struct timespec64 cur_time = current_time(dir);
580 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
584 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
588 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
593 * Don't create subvolume whose level is not zero. Or qgroup will be
594 * screwed up since it assumes subvolume qgroup's level to be 0.
596 if (btrfs_qgroup_level(objectid)) {
601 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
603 * The same as the snapshot creation, please see the comment
604 * of create_snapshot().
606 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
610 trans = btrfs_start_transaction(root, 0);
612 ret = PTR_ERR(trans);
613 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
616 trans->block_rsv = &block_rsv;
617 trans->bytes_reserved = block_rsv.size;
619 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
623 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
629 btrfs_mark_buffer_dirty(leaf);
631 inode_item = &root_item->inode;
632 btrfs_set_stack_inode_generation(inode_item, 1);
633 btrfs_set_stack_inode_size(inode_item, 3);
634 btrfs_set_stack_inode_nlink(inode_item, 1);
635 btrfs_set_stack_inode_nbytes(inode_item,
637 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
639 btrfs_set_root_flags(root_item, 0);
640 btrfs_set_root_limit(root_item, 0);
641 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
643 btrfs_set_root_bytenr(root_item, leaf->start);
644 btrfs_set_root_generation(root_item, trans->transid);
645 btrfs_set_root_level(root_item, 0);
646 btrfs_set_root_refs(root_item, 1);
647 btrfs_set_root_used(root_item, leaf->len);
648 btrfs_set_root_last_snapshot(root_item, 0);
650 btrfs_set_root_generation_v2(root_item,
651 btrfs_root_generation(root_item));
652 uuid_le_gen(&new_uuid);
653 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
654 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
655 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
656 root_item->ctime = root_item->otime;
657 btrfs_set_root_ctransid(root_item, trans->transid);
658 btrfs_set_root_otransid(root_item, trans->transid);
660 btrfs_tree_unlock(leaf);
661 free_extent_buffer(leaf);
664 btrfs_set_root_dirid(root_item, new_dirid);
666 key.objectid = objectid;
668 key.type = BTRFS_ROOT_ITEM_KEY;
669 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
674 key.offset = (u64)-1;
675 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
676 if (IS_ERR(new_root)) {
677 ret = PTR_ERR(new_root);
678 btrfs_abort_transaction(trans, ret);
682 btrfs_record_root_in_trans(trans, new_root);
684 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
686 /* We potentially lose an unused inode item here */
687 btrfs_abort_transaction(trans, ret);
691 mutex_lock(&new_root->objectid_mutex);
692 new_root->highest_objectid = new_dirid;
693 mutex_unlock(&new_root->objectid_mutex);
696 * insert the directory item
698 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
700 btrfs_abort_transaction(trans, ret);
704 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
705 BTRFS_FT_DIR, index);
707 btrfs_abort_transaction(trans, ret);
711 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
712 ret = btrfs_update_inode(trans, root, dir);
715 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
716 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
719 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
720 BTRFS_UUID_KEY_SUBVOL, objectid);
722 btrfs_abort_transaction(trans, ret);
726 trans->block_rsv = NULL;
727 trans->bytes_reserved = 0;
728 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
731 *async_transid = trans->transid;
732 err = btrfs_commit_transaction_async(trans, 1);
734 err = btrfs_commit_transaction(trans);
736 err = btrfs_commit_transaction(trans);
742 inode = btrfs_lookup_dentry(dir, dentry);
744 return PTR_ERR(inode);
745 d_instantiate(dentry, inode);
754 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
755 struct dentry *dentry,
756 u64 *async_transid, bool readonly,
757 struct btrfs_qgroup_inherit *inherit)
759 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
761 struct btrfs_pending_snapshot *pending_snapshot;
762 struct btrfs_trans_handle *trans;
764 bool snapshot_force_cow = false;
766 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
769 if (atomic_read(&root->nr_swapfiles)) {
771 "cannot snapshot subvolume with active swapfile");
775 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
776 if (!pending_snapshot)
779 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
781 pending_snapshot->path = btrfs_alloc_path();
782 if (!pending_snapshot->root_item || !pending_snapshot->path) {
788 * Force new buffered writes to reserve space even when NOCOW is
789 * possible. This is to avoid later writeback (running dealloc) to
790 * fallback to COW mode and unexpectedly fail with ENOSPC.
792 atomic_inc(&root->will_be_snapshotted);
793 smp_mb__after_atomic();
794 /* wait for no snapshot writes */
795 wait_event(root->subv_writers->wait,
796 percpu_counter_sum(&root->subv_writers->counter) == 0);
798 ret = btrfs_start_delalloc_snapshot(root);
803 * All previous writes have started writeback in NOCOW mode, so now
804 * we force future writes to fallback to COW mode during snapshot
807 atomic_inc(&root->snapshot_force_cow);
808 snapshot_force_cow = true;
810 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
812 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
813 BTRFS_BLOCK_RSV_TEMP);
815 * 1 - parent dir inode
818 * 2 - root ref/backref
819 * 1 - root of snapshot
822 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
823 &pending_snapshot->block_rsv, 8,
828 pending_snapshot->dentry = dentry;
829 pending_snapshot->root = root;
830 pending_snapshot->readonly = readonly;
831 pending_snapshot->dir = dir;
832 pending_snapshot->inherit = inherit;
834 trans = btrfs_start_transaction(root, 0);
836 ret = PTR_ERR(trans);
840 spin_lock(&fs_info->trans_lock);
841 list_add(&pending_snapshot->list,
842 &trans->transaction->pending_snapshots);
843 spin_unlock(&fs_info->trans_lock);
845 *async_transid = trans->transid;
846 ret = btrfs_commit_transaction_async(trans, 1);
848 ret = btrfs_commit_transaction(trans);
850 ret = btrfs_commit_transaction(trans);
855 ret = pending_snapshot->error;
859 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
863 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
865 ret = PTR_ERR(inode);
869 d_instantiate(dentry, inode);
872 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
874 if (snapshot_force_cow)
875 atomic_dec(&root->snapshot_force_cow);
876 if (atomic_dec_and_test(&root->will_be_snapshotted))
877 wake_up_var(&root->will_be_snapshotted);
879 kfree(pending_snapshot->root_item);
880 btrfs_free_path(pending_snapshot->path);
881 kfree(pending_snapshot);
886 /* copy of may_delete in fs/namei.c()
887 * Check whether we can remove a link victim from directory dir, check
888 * whether the type of victim is right.
889 * 1. We can't do it if dir is read-only (done in permission())
890 * 2. We should have write and exec permissions on dir
891 * 3. We can't remove anything from append-only dir
892 * 4. We can't do anything with immutable dir (done in permission())
893 * 5. If the sticky bit on dir is set we should either
894 * a. be owner of dir, or
895 * b. be owner of victim, or
896 * c. have CAP_FOWNER capability
897 * 6. If the victim is append-only or immutable we can't do anything with
898 * links pointing to it.
899 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
900 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
901 * 9. We can't remove a root or mountpoint.
902 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
903 * nfs_async_unlink().
906 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
910 if (d_really_is_negative(victim))
913 BUG_ON(d_inode(victim->d_parent) != dir);
914 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
916 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
921 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
922 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
925 if (!d_is_dir(victim))
929 } else if (d_is_dir(victim))
933 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
938 /* copy of may_create in fs/namei.c() */
939 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
941 if (d_really_is_positive(child))
945 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
949 * Create a new subvolume below @parent. This is largely modeled after
950 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
951 * inside this filesystem so it's quite a bit simpler.
953 static noinline int btrfs_mksubvol(const struct path *parent,
954 const char *name, int namelen,
955 struct btrfs_root *snap_src,
956 u64 *async_transid, bool readonly,
957 struct btrfs_qgroup_inherit *inherit)
959 struct inode *dir = d_inode(parent->dentry);
960 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
961 struct dentry *dentry;
964 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
968 dentry = lookup_one_len(name, parent->dentry, namelen);
969 error = PTR_ERR(dentry);
973 error = btrfs_may_create(dir, dentry);
978 * even if this name doesn't exist, we may get hash collisions.
979 * check for them now when we can safely fail
981 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
987 down_read(&fs_info->subvol_sem);
989 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
993 error = create_snapshot(snap_src, dir, dentry,
994 async_transid, readonly, inherit);
996 error = create_subvol(dir, dentry, name, namelen,
997 async_transid, inherit);
1000 fsnotify_mkdir(dir, dentry);
1002 up_read(&fs_info->subvol_sem);
1011 * When we're defragging a range, we don't want to kick it off again
1012 * if it is really just waiting for delalloc to send it down.
1013 * If we find a nice big extent or delalloc range for the bytes in the
1014 * file you want to defrag, we return 0 to let you know to skip this
1017 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1019 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1020 struct extent_map *em = NULL;
1021 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1024 read_lock(&em_tree->lock);
1025 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1026 read_unlock(&em_tree->lock);
1029 end = extent_map_end(em);
1030 free_extent_map(em);
1031 if (end - offset > thresh)
1034 /* if we already have a nice delalloc here, just stop */
1036 end = count_range_bits(io_tree, &offset, offset + thresh,
1037 thresh, EXTENT_DELALLOC, 1);
1044 * helper function to walk through a file and find extents
1045 * newer than a specific transid, and smaller than thresh.
1047 * This is used by the defragging code to find new and small
1050 static int find_new_extents(struct btrfs_root *root,
1051 struct inode *inode, u64 newer_than,
1052 u64 *off, u32 thresh)
1054 struct btrfs_path *path;
1055 struct btrfs_key min_key;
1056 struct extent_buffer *leaf;
1057 struct btrfs_file_extent_item *extent;
1060 u64 ino = btrfs_ino(BTRFS_I(inode));
1062 path = btrfs_alloc_path();
1066 min_key.objectid = ino;
1067 min_key.type = BTRFS_EXTENT_DATA_KEY;
1068 min_key.offset = *off;
1071 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1075 if (min_key.objectid != ino)
1077 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1080 leaf = path->nodes[0];
1081 extent = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_file_extent_item);
1084 type = btrfs_file_extent_type(leaf, extent);
1085 if (type == BTRFS_FILE_EXTENT_REG &&
1086 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1087 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1088 *off = min_key.offset;
1089 btrfs_free_path(path);
1094 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1095 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1099 if (min_key.offset == (u64)-1)
1103 btrfs_release_path(path);
1106 btrfs_free_path(path);
1110 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1112 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1113 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1114 struct extent_map *em;
1115 u64 len = PAGE_SIZE;
1118 * hopefully we have this extent in the tree already, try without
1119 * the full extent lock
1121 read_lock(&em_tree->lock);
1122 em = lookup_extent_mapping(em_tree, start, len);
1123 read_unlock(&em_tree->lock);
1126 struct extent_state *cached = NULL;
1127 u64 end = start + len - 1;
1129 /* get the big lock and read metadata off disk */
1130 lock_extent_bits(io_tree, start, end, &cached);
1131 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
1132 unlock_extent_cached(io_tree, start, end, &cached);
1141 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1143 struct extent_map *next;
1146 /* this is the last extent */
1147 if (em->start + em->len >= i_size_read(inode))
1150 next = defrag_lookup_extent(inode, em->start + em->len);
1151 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1153 else if ((em->block_start + em->block_len == next->block_start) &&
1154 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1157 free_extent_map(next);
1161 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1162 u64 *last_len, u64 *skip, u64 *defrag_end,
1165 struct extent_map *em;
1167 bool next_mergeable = true;
1168 bool prev_mergeable = true;
1171 * make sure that once we start defragging an extent, we keep on
1174 if (start < *defrag_end)
1179 em = defrag_lookup_extent(inode, start);
1183 /* this will cover holes, and inline extents */
1184 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1190 prev_mergeable = false;
1192 next_mergeable = defrag_check_next_extent(inode, em);
1194 * we hit a real extent, if it is big or the next extent is not a
1195 * real extent, don't bother defragging it
1197 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1198 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1202 * last_len ends up being a counter of how many bytes we've defragged.
1203 * every time we choose not to defrag an extent, we reset *last_len
1204 * so that the next tiny extent will force a defrag.
1206 * The end result of this is that tiny extents before a single big
1207 * extent will force at least part of that big extent to be defragged.
1210 *defrag_end = extent_map_end(em);
1213 *skip = extent_map_end(em);
1217 free_extent_map(em);
1222 * it doesn't do much good to defrag one or two pages
1223 * at a time. This pulls in a nice chunk of pages
1224 * to COW and defrag.
1226 * It also makes sure the delalloc code has enough
1227 * dirty data to avoid making new small extents as part
1230 * It's a good idea to start RA on this range
1231 * before calling this.
1233 static int cluster_pages_for_defrag(struct inode *inode,
1234 struct page **pages,
1235 unsigned long start_index,
1236 unsigned long num_pages)
1238 unsigned long file_end;
1239 u64 isize = i_size_read(inode);
1246 struct btrfs_ordered_extent *ordered;
1247 struct extent_state *cached_state = NULL;
1248 struct extent_io_tree *tree;
1249 struct extent_changeset *data_reserved = NULL;
1250 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1252 file_end = (isize - 1) >> PAGE_SHIFT;
1253 if (!isize || start_index > file_end)
1256 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1258 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
1259 start_index << PAGE_SHIFT,
1260 page_cnt << PAGE_SHIFT);
1264 tree = &BTRFS_I(inode)->io_tree;
1266 /* step one, lock all the pages */
1267 for (i = 0; i < page_cnt; i++) {
1270 page = find_or_create_page(inode->i_mapping,
1271 start_index + i, mask);
1275 page_start = page_offset(page);
1276 page_end = page_start + PAGE_SIZE - 1;
1278 lock_extent_bits(tree, page_start, page_end,
1280 ordered = btrfs_lookup_ordered_extent(inode,
1282 unlock_extent_cached(tree, page_start, page_end,
1288 btrfs_start_ordered_extent(inode, ordered, 1);
1289 btrfs_put_ordered_extent(ordered);
1292 * we unlocked the page above, so we need check if
1293 * it was released or not.
1295 if (page->mapping != inode->i_mapping) {
1302 if (!PageUptodate(page)) {
1303 btrfs_readpage(NULL, page);
1305 if (!PageUptodate(page)) {
1313 if (page->mapping != inode->i_mapping) {
1325 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1329 * so now we have a nice long stream of locked
1330 * and up to date pages, lets wait on them
1332 for (i = 0; i < i_done; i++)
1333 wait_on_page_writeback(pages[i]);
1335 page_start = page_offset(pages[0]);
1336 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1338 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1339 page_start, page_end - 1, &cached_state);
1340 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1341 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1342 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1345 if (i_done != page_cnt) {
1346 spin_lock(&BTRFS_I(inode)->lock);
1347 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1348 spin_unlock(&BTRFS_I(inode)->lock);
1349 btrfs_delalloc_release_space(inode, data_reserved,
1350 start_index << PAGE_SHIFT,
1351 (page_cnt - i_done) << PAGE_SHIFT, true);
1355 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1358 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1359 page_start, page_end - 1, &cached_state);
1361 for (i = 0; i < i_done; i++) {
1362 clear_page_dirty_for_io(pages[i]);
1363 ClearPageChecked(pages[i]);
1364 set_page_extent_mapped(pages[i]);
1365 set_page_dirty(pages[i]);
1366 unlock_page(pages[i]);
1369 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1371 extent_changeset_free(data_reserved);
1374 for (i = 0; i < i_done; i++) {
1375 unlock_page(pages[i]);
1378 btrfs_delalloc_release_space(inode, data_reserved,
1379 start_index << PAGE_SHIFT,
1380 page_cnt << PAGE_SHIFT, true);
1381 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1383 extent_changeset_free(data_reserved);
1388 int btrfs_defrag_file(struct inode *inode, struct file *file,
1389 struct btrfs_ioctl_defrag_range_args *range,
1390 u64 newer_than, unsigned long max_to_defrag)
1392 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1393 struct btrfs_root *root = BTRFS_I(inode)->root;
1394 struct file_ra_state *ra = NULL;
1395 unsigned long last_index;
1396 u64 isize = i_size_read(inode);
1400 u64 newer_off = range->start;
1402 unsigned long ra_index = 0;
1404 int defrag_count = 0;
1405 int compress_type = BTRFS_COMPRESS_ZLIB;
1406 u32 extent_thresh = range->extent_thresh;
1407 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1408 unsigned long cluster = max_cluster;
1409 u64 new_align = ~((u64)SZ_128K - 1);
1410 struct page **pages = NULL;
1411 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1416 if (range->start >= isize)
1420 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1422 if (range->compress_type)
1423 compress_type = range->compress_type;
1426 if (extent_thresh == 0)
1427 extent_thresh = SZ_256K;
1430 * If we were not given a file, allocate a readahead context. As
1431 * readahead is just an optimization, defrag will work without it so
1432 * we don't error out.
1435 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1437 file_ra_state_init(ra, inode->i_mapping);
1442 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1448 /* find the last page to defrag */
1449 if (range->start + range->len > range->start) {
1450 last_index = min_t(u64, isize - 1,
1451 range->start + range->len - 1) >> PAGE_SHIFT;
1453 last_index = (isize - 1) >> PAGE_SHIFT;
1457 ret = find_new_extents(root, inode, newer_than,
1458 &newer_off, SZ_64K);
1460 range->start = newer_off;
1462 * we always align our defrag to help keep
1463 * the extents in the file evenly spaced
1465 i = (newer_off & new_align) >> PAGE_SHIFT;
1469 i = range->start >> PAGE_SHIFT;
1472 max_to_defrag = last_index - i + 1;
1475 * make writeback starts from i, so the defrag range can be
1476 * written sequentially.
1478 if (i < inode->i_mapping->writeback_index)
1479 inode->i_mapping->writeback_index = i;
1481 while (i <= last_index && defrag_count < max_to_defrag &&
1482 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1484 * make sure we stop running if someone unmounts
1487 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1490 if (btrfs_defrag_cancelled(fs_info)) {
1491 btrfs_debug(fs_info, "defrag_file cancelled");
1496 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1497 extent_thresh, &last_len, &skip,
1498 &defrag_end, do_compress)){
1501 * the should_defrag function tells us how much to skip
1502 * bump our counter by the suggested amount
1504 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1505 i = max(i + 1, next);
1510 cluster = (PAGE_ALIGN(defrag_end) >>
1512 cluster = min(cluster, max_cluster);
1514 cluster = max_cluster;
1517 if (i + cluster > ra_index) {
1518 ra_index = max(i, ra_index);
1520 page_cache_sync_readahead(inode->i_mapping, ra,
1521 file, ra_index, cluster);
1522 ra_index += cluster;
1526 if (IS_SWAPFILE(inode)) {
1530 BTRFS_I(inode)->defrag_compress = compress_type;
1531 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1534 inode_unlock(inode);
1538 defrag_count += ret;
1539 balance_dirty_pages_ratelimited(inode->i_mapping);
1540 inode_unlock(inode);
1543 if (newer_off == (u64)-1)
1549 newer_off = max(newer_off + 1,
1550 (u64)i << PAGE_SHIFT);
1552 ret = find_new_extents(root, inode, newer_than,
1553 &newer_off, SZ_64K);
1555 range->start = newer_off;
1556 i = (newer_off & new_align) >> PAGE_SHIFT;
1563 last_len += ret << PAGE_SHIFT;
1571 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1572 filemap_flush(inode->i_mapping);
1573 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1574 &BTRFS_I(inode)->runtime_flags))
1575 filemap_flush(inode->i_mapping);
1578 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1579 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1580 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1581 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1589 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1590 inode_unlock(inode);
1598 static noinline int btrfs_ioctl_resize(struct file *file,
1601 struct inode *inode = file_inode(file);
1602 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1606 struct btrfs_root *root = BTRFS_I(inode)->root;
1607 struct btrfs_ioctl_vol_args *vol_args;
1608 struct btrfs_trans_handle *trans;
1609 struct btrfs_device *device = NULL;
1612 char *devstr = NULL;
1616 if (!capable(CAP_SYS_ADMIN))
1619 ret = mnt_want_write_file(file);
1623 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
1624 mnt_drop_write_file(file);
1625 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1628 vol_args = memdup_user(arg, sizeof(*vol_args));
1629 if (IS_ERR(vol_args)) {
1630 ret = PTR_ERR(vol_args);
1634 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1636 sizestr = vol_args->name;
1637 devstr = strchr(sizestr, ':');
1639 sizestr = devstr + 1;
1641 devstr = vol_args->name;
1642 ret = kstrtoull(devstr, 10, &devid);
1649 btrfs_info(fs_info, "resizing devid %llu", devid);
1652 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
1654 btrfs_info(fs_info, "resizer unable to find device %llu",
1660 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1662 "resizer unable to apply on readonly device %llu",
1668 if (!strcmp(sizestr, "max"))
1669 new_size = device->bdev->bd_inode->i_size;
1671 if (sizestr[0] == '-') {
1674 } else if (sizestr[0] == '+') {
1678 new_size = memparse(sizestr, &retptr);
1679 if (*retptr != '\0' || new_size == 0) {
1685 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1690 old_size = btrfs_device_get_total_bytes(device);
1693 if (new_size > old_size) {
1697 new_size = old_size - new_size;
1698 } else if (mod > 0) {
1699 if (new_size > ULLONG_MAX - old_size) {
1703 new_size = old_size + new_size;
1706 if (new_size < SZ_256M) {
1710 if (new_size > device->bdev->bd_inode->i_size) {
1715 new_size = round_down(new_size, fs_info->sectorsize);
1717 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1718 rcu_str_deref(device->name), new_size);
1720 if (new_size > old_size) {
1721 trans = btrfs_start_transaction(root, 0);
1722 if (IS_ERR(trans)) {
1723 ret = PTR_ERR(trans);
1726 ret = btrfs_grow_device(trans, device, new_size);
1727 btrfs_commit_transaction(trans);
1728 } else if (new_size < old_size) {
1729 ret = btrfs_shrink_device(device, new_size);
1730 } /* equal, nothing need to do */
1735 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
1736 mnt_drop_write_file(file);
1740 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1741 const char *name, unsigned long fd, int subvol,
1742 u64 *transid, bool readonly,
1743 struct btrfs_qgroup_inherit *inherit)
1748 if (!S_ISDIR(file_inode(file)->i_mode))
1751 ret = mnt_want_write_file(file);
1755 namelen = strlen(name);
1756 if (strchr(name, '/')) {
1758 goto out_drop_write;
1761 if (name[0] == '.' &&
1762 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1764 goto out_drop_write;
1768 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1769 NULL, transid, readonly, inherit);
1771 struct fd src = fdget(fd);
1772 struct inode *src_inode;
1775 goto out_drop_write;
1778 src_inode = file_inode(src.file);
1779 if (src_inode->i_sb != file_inode(file)->i_sb) {
1780 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1781 "Snapshot src from another FS");
1783 } else if (!inode_owner_or_capable(src_inode)) {
1785 * Subvolume creation is not restricted, but snapshots
1786 * are limited to own subvolumes only
1790 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1791 BTRFS_I(src_inode)->root,
1792 transid, readonly, inherit);
1797 mnt_drop_write_file(file);
1802 static noinline int btrfs_ioctl_snap_create(struct file *file,
1803 void __user *arg, int subvol)
1805 struct btrfs_ioctl_vol_args *vol_args;
1808 if (!S_ISDIR(file_inode(file)->i_mode))
1811 vol_args = memdup_user(arg, sizeof(*vol_args));
1812 if (IS_ERR(vol_args))
1813 return PTR_ERR(vol_args);
1814 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1816 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1817 vol_args->fd, subvol,
1824 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1825 void __user *arg, int subvol)
1827 struct btrfs_ioctl_vol_args_v2 *vol_args;
1831 bool readonly = false;
1832 struct btrfs_qgroup_inherit *inherit = NULL;
1834 if (!S_ISDIR(file_inode(file)->i_mode))
1837 vol_args = memdup_user(arg, sizeof(*vol_args));
1838 if (IS_ERR(vol_args))
1839 return PTR_ERR(vol_args);
1840 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1842 if (vol_args->flags &
1843 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1844 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1849 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1851 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1853 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1854 if (vol_args->size > PAGE_SIZE) {
1858 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1859 if (IS_ERR(inherit)) {
1860 ret = PTR_ERR(inherit);
1865 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1866 vol_args->fd, subvol, ptr,
1871 if (ptr && copy_to_user(arg +
1872 offsetof(struct btrfs_ioctl_vol_args_v2,
1884 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1887 struct inode *inode = file_inode(file);
1888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1889 struct btrfs_root *root = BTRFS_I(inode)->root;
1893 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1896 down_read(&fs_info->subvol_sem);
1897 if (btrfs_root_readonly(root))
1898 flags |= BTRFS_SUBVOL_RDONLY;
1899 up_read(&fs_info->subvol_sem);
1901 if (copy_to_user(arg, &flags, sizeof(flags)))
1907 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1910 struct inode *inode = file_inode(file);
1911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1912 struct btrfs_root *root = BTRFS_I(inode)->root;
1913 struct btrfs_trans_handle *trans;
1918 if (!inode_owner_or_capable(inode))
1921 ret = mnt_want_write_file(file);
1925 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1927 goto out_drop_write;
1930 if (copy_from_user(&flags, arg, sizeof(flags))) {
1932 goto out_drop_write;
1935 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1937 goto out_drop_write;
1940 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1942 goto out_drop_write;
1945 down_write(&fs_info->subvol_sem);
1948 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1951 root_flags = btrfs_root_flags(&root->root_item);
1952 if (flags & BTRFS_SUBVOL_RDONLY) {
1953 btrfs_set_root_flags(&root->root_item,
1954 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1957 * Block RO -> RW transition if this subvolume is involved in
1960 spin_lock(&root->root_item_lock);
1961 if (root->send_in_progress == 0) {
1962 btrfs_set_root_flags(&root->root_item,
1963 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1964 spin_unlock(&root->root_item_lock);
1966 spin_unlock(&root->root_item_lock);
1968 "Attempt to set subvolume %llu read-write during send",
1969 root->root_key.objectid);
1975 trans = btrfs_start_transaction(root, 1);
1976 if (IS_ERR(trans)) {
1977 ret = PTR_ERR(trans);
1981 ret = btrfs_update_root(trans, fs_info->tree_root,
1982 &root->root_key, &root->root_item);
1984 btrfs_end_transaction(trans);
1988 ret = btrfs_commit_transaction(trans);
1992 btrfs_set_root_flags(&root->root_item, root_flags);
1994 up_write(&fs_info->subvol_sem);
1996 mnt_drop_write_file(file);
2001 static noinline int key_in_sk(struct btrfs_key *key,
2002 struct btrfs_ioctl_search_key *sk)
2004 struct btrfs_key test;
2007 test.objectid = sk->min_objectid;
2008 test.type = sk->min_type;
2009 test.offset = sk->min_offset;
2011 ret = btrfs_comp_cpu_keys(key, &test);
2015 test.objectid = sk->max_objectid;
2016 test.type = sk->max_type;
2017 test.offset = sk->max_offset;
2019 ret = btrfs_comp_cpu_keys(key, &test);
2025 static noinline int copy_to_sk(struct btrfs_path *path,
2026 struct btrfs_key *key,
2027 struct btrfs_ioctl_search_key *sk,
2030 unsigned long *sk_offset,
2034 struct extent_buffer *leaf;
2035 struct btrfs_ioctl_search_header sh;
2036 struct btrfs_key test;
2037 unsigned long item_off;
2038 unsigned long item_len;
2044 leaf = path->nodes[0];
2045 slot = path->slots[0];
2046 nritems = btrfs_header_nritems(leaf);
2048 if (btrfs_header_generation(leaf) > sk->max_transid) {
2052 found_transid = btrfs_header_generation(leaf);
2054 for (i = slot; i < nritems; i++) {
2055 item_off = btrfs_item_ptr_offset(leaf, i);
2056 item_len = btrfs_item_size_nr(leaf, i);
2058 btrfs_item_key_to_cpu(leaf, key, i);
2059 if (!key_in_sk(key, sk))
2062 if (sizeof(sh) + item_len > *buf_size) {
2069 * return one empty item back for v1, which does not
2073 *buf_size = sizeof(sh) + item_len;
2078 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2083 sh.objectid = key->objectid;
2084 sh.offset = key->offset;
2085 sh.type = key->type;
2087 sh.transid = found_transid;
2089 /* copy search result header */
2090 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2095 *sk_offset += sizeof(sh);
2098 char __user *up = ubuf + *sk_offset;
2100 if (read_extent_buffer_to_user(leaf, up,
2101 item_off, item_len)) {
2106 *sk_offset += item_len;
2110 if (ret) /* -EOVERFLOW from above */
2113 if (*num_found >= sk->nr_items) {
2120 test.objectid = sk->max_objectid;
2121 test.type = sk->max_type;
2122 test.offset = sk->max_offset;
2123 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2125 else if (key->offset < (u64)-1)
2127 else if (key->type < (u8)-1) {
2130 } else if (key->objectid < (u64)-1) {
2138 * 0: all items from this leaf copied, continue with next
2139 * 1: * more items can be copied, but unused buffer is too small
2140 * * all items were found
2141 * Either way, it will stops the loop which iterates to the next
2143 * -EOVERFLOW: item was to large for buffer
2144 * -EFAULT: could not copy extent buffer back to userspace
2149 static noinline int search_ioctl(struct inode *inode,
2150 struct btrfs_ioctl_search_key *sk,
2154 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2155 struct btrfs_root *root;
2156 struct btrfs_key key;
2157 struct btrfs_path *path;
2160 unsigned long sk_offset = 0;
2162 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2163 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2167 path = btrfs_alloc_path();
2171 if (sk->tree_id == 0) {
2172 /* search the root of the inode that was passed */
2173 root = BTRFS_I(inode)->root;
2175 key.objectid = sk->tree_id;
2176 key.type = BTRFS_ROOT_ITEM_KEY;
2177 key.offset = (u64)-1;
2178 root = btrfs_read_fs_root_no_name(info, &key);
2180 btrfs_free_path(path);
2181 return PTR_ERR(root);
2185 key.objectid = sk->min_objectid;
2186 key.type = sk->min_type;
2187 key.offset = sk->min_offset;
2190 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2196 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2197 &sk_offset, &num_found);
2198 btrfs_release_path(path);
2206 sk->nr_items = num_found;
2207 btrfs_free_path(path);
2211 static noinline int btrfs_ioctl_tree_search(struct file *file,
2214 struct btrfs_ioctl_search_args __user *uargs;
2215 struct btrfs_ioctl_search_key sk;
2216 struct inode *inode;
2220 if (!capable(CAP_SYS_ADMIN))
2223 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2225 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2228 buf_size = sizeof(uargs->buf);
2230 inode = file_inode(file);
2231 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2234 * In the origin implementation an overflow is handled by returning a
2235 * search header with a len of zero, so reset ret.
2237 if (ret == -EOVERFLOW)
2240 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2245 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2248 struct btrfs_ioctl_search_args_v2 __user *uarg;
2249 struct btrfs_ioctl_search_args_v2 args;
2250 struct inode *inode;
2253 const size_t buf_limit = SZ_16M;
2255 if (!capable(CAP_SYS_ADMIN))
2258 /* copy search header and buffer size */
2259 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2260 if (copy_from_user(&args, uarg, sizeof(args)))
2263 buf_size = args.buf_size;
2265 /* limit result size to 16MB */
2266 if (buf_size > buf_limit)
2267 buf_size = buf_limit;
2269 inode = file_inode(file);
2270 ret = search_ioctl(inode, &args.key, &buf_size,
2271 (char __user *)(&uarg->buf[0]));
2272 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2274 else if (ret == -EOVERFLOW &&
2275 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2282 * Search INODE_REFs to identify path name of 'dirid' directory
2283 * in a 'tree_id' tree. and sets path name to 'name'.
2285 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2286 u64 tree_id, u64 dirid, char *name)
2288 struct btrfs_root *root;
2289 struct btrfs_key key;
2295 struct btrfs_inode_ref *iref;
2296 struct extent_buffer *l;
2297 struct btrfs_path *path;
2299 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2304 path = btrfs_alloc_path();
2308 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2310 key.objectid = tree_id;
2311 key.type = BTRFS_ROOT_ITEM_KEY;
2312 key.offset = (u64)-1;
2313 root = btrfs_read_fs_root_no_name(info, &key);
2315 ret = PTR_ERR(root);
2319 key.objectid = dirid;
2320 key.type = BTRFS_INODE_REF_KEY;
2321 key.offset = (u64)-1;
2324 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2328 ret = btrfs_previous_item(root, path, dirid,
2329 BTRFS_INODE_REF_KEY);
2339 slot = path->slots[0];
2340 btrfs_item_key_to_cpu(l, &key, slot);
2342 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2343 len = btrfs_inode_ref_name_len(l, iref);
2345 total_len += len + 1;
2347 ret = -ENAMETOOLONG;
2352 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2354 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2357 btrfs_release_path(path);
2358 key.objectid = key.offset;
2359 key.offset = (u64)-1;
2360 dirid = key.objectid;
2362 memmove(name, ptr, total_len);
2363 name[total_len] = '\0';
2366 btrfs_free_path(path);
2370 static int btrfs_search_path_in_tree_user(struct inode *inode,
2371 struct btrfs_ioctl_ino_lookup_user_args *args)
2373 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2374 struct super_block *sb = inode->i_sb;
2375 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2376 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2377 u64 dirid = args->dirid;
2378 unsigned long item_off;
2379 unsigned long item_len;
2380 struct btrfs_inode_ref *iref;
2381 struct btrfs_root_ref *rref;
2382 struct btrfs_root *root;
2383 struct btrfs_path *path;
2384 struct btrfs_key key, key2;
2385 struct extent_buffer *leaf;
2386 struct inode *temp_inode;
2393 path = btrfs_alloc_path();
2398 * If the bottom subvolume does not exist directly under upper_limit,
2399 * construct the path in from the bottom up.
2401 if (dirid != upper_limit.objectid) {
2402 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2404 key.objectid = treeid;
2405 key.type = BTRFS_ROOT_ITEM_KEY;
2406 key.offset = (u64)-1;
2407 root = btrfs_read_fs_root_no_name(fs_info, &key);
2409 ret = PTR_ERR(root);
2413 key.objectid = dirid;
2414 key.type = BTRFS_INODE_REF_KEY;
2415 key.offset = (u64)-1;
2417 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2420 } else if (ret > 0) {
2421 ret = btrfs_previous_item(root, path, dirid,
2422 BTRFS_INODE_REF_KEY);
2425 } else if (ret > 0) {
2431 leaf = path->nodes[0];
2432 slot = path->slots[0];
2433 btrfs_item_key_to_cpu(leaf, &key, slot);
2435 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2436 len = btrfs_inode_ref_name_len(leaf, iref);
2438 total_len += len + 1;
2439 if (ptr < args->path) {
2440 ret = -ENAMETOOLONG;
2445 read_extent_buffer(leaf, ptr,
2446 (unsigned long)(iref + 1), len);
2448 /* Check the read+exec permission of this directory */
2449 ret = btrfs_previous_item(root, path, dirid,
2450 BTRFS_INODE_ITEM_KEY);
2453 } else if (ret > 0) {
2458 leaf = path->nodes[0];
2459 slot = path->slots[0];
2460 btrfs_item_key_to_cpu(leaf, &key2, slot);
2461 if (key2.objectid != dirid) {
2466 temp_inode = btrfs_iget(sb, &key2, root, NULL);
2467 if (IS_ERR(temp_inode)) {
2468 ret = PTR_ERR(temp_inode);
2471 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2478 if (key.offset == upper_limit.objectid)
2480 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2485 btrfs_release_path(path);
2486 key.objectid = key.offset;
2487 key.offset = (u64)-1;
2488 dirid = key.objectid;
2491 memmove(args->path, ptr, total_len);
2492 args->path[total_len] = '\0';
2493 btrfs_release_path(path);
2496 /* Get the bottom subvolume's name from ROOT_REF */
2497 root = fs_info->tree_root;
2498 key.objectid = treeid;
2499 key.type = BTRFS_ROOT_REF_KEY;
2500 key.offset = args->treeid;
2501 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2504 } else if (ret > 0) {
2509 leaf = path->nodes[0];
2510 slot = path->slots[0];
2511 btrfs_item_key_to_cpu(leaf, &key, slot);
2513 item_off = btrfs_item_ptr_offset(leaf, slot);
2514 item_len = btrfs_item_size_nr(leaf, slot);
2515 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2516 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2517 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2522 /* Copy subvolume's name */
2523 item_off += sizeof(struct btrfs_root_ref);
2524 item_len -= sizeof(struct btrfs_root_ref);
2525 read_extent_buffer(leaf, args->name, item_off, item_len);
2526 args->name[item_len] = 0;
2529 btrfs_free_path(path);
2533 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2536 struct btrfs_ioctl_ino_lookup_args *args;
2537 struct inode *inode;
2540 args = memdup_user(argp, sizeof(*args));
2542 return PTR_ERR(args);
2544 inode = file_inode(file);
2547 * Unprivileged query to obtain the containing subvolume root id. The
2548 * path is reset so it's consistent with btrfs_search_path_in_tree.
2550 if (args->treeid == 0)
2551 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2553 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2558 if (!capable(CAP_SYS_ADMIN)) {
2563 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2564 args->treeid, args->objectid,
2568 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2576 * Version of ino_lookup ioctl (unprivileged)
2578 * The main differences from ino_lookup ioctl are:
2580 * 1. Read + Exec permission will be checked using inode_permission() during
2581 * path construction. -EACCES will be returned in case of failure.
2582 * 2. Path construction will be stopped at the inode number which corresponds
2583 * to the fd with which this ioctl is called. If constructed path does not
2584 * exist under fd's inode, -EACCES will be returned.
2585 * 3. The name of bottom subvolume is also searched and filled.
2587 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2589 struct btrfs_ioctl_ino_lookup_user_args *args;
2590 struct inode *inode;
2593 args = memdup_user(argp, sizeof(*args));
2595 return PTR_ERR(args);
2597 inode = file_inode(file);
2599 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2600 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2602 * The subvolume does not exist under fd with which this is
2609 ret = btrfs_search_path_in_tree_user(inode, args);
2611 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2618 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2619 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2621 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2622 struct btrfs_fs_info *fs_info;
2623 struct btrfs_root *root;
2624 struct btrfs_path *path;
2625 struct btrfs_key key;
2626 struct btrfs_root_item *root_item;
2627 struct btrfs_root_ref *rref;
2628 struct extent_buffer *leaf;
2629 unsigned long item_off;
2630 unsigned long item_len;
2631 struct inode *inode;
2635 path = btrfs_alloc_path();
2639 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2641 btrfs_free_path(path);
2645 inode = file_inode(file);
2646 fs_info = BTRFS_I(inode)->root->fs_info;
2648 /* Get root_item of inode's subvolume */
2649 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2650 key.type = BTRFS_ROOT_ITEM_KEY;
2651 key.offset = (u64)-1;
2652 root = btrfs_read_fs_root_no_name(fs_info, &key);
2654 ret = PTR_ERR(root);
2657 root_item = &root->root_item;
2659 subvol_info->treeid = key.objectid;
2661 subvol_info->generation = btrfs_root_generation(root_item);
2662 subvol_info->flags = btrfs_root_flags(root_item);
2664 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2665 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2667 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2670 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2671 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2672 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2674 subvol_info->otransid = btrfs_root_otransid(root_item);
2675 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2676 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2678 subvol_info->stransid = btrfs_root_stransid(root_item);
2679 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2680 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2682 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2683 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2684 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2686 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2687 /* Search root tree for ROOT_BACKREF of this subvolume */
2688 root = fs_info->tree_root;
2690 key.type = BTRFS_ROOT_BACKREF_KEY;
2692 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2695 } else if (path->slots[0] >=
2696 btrfs_header_nritems(path->nodes[0])) {
2697 ret = btrfs_next_leaf(root, path);
2700 } else if (ret > 0) {
2706 leaf = path->nodes[0];
2707 slot = path->slots[0];
2708 btrfs_item_key_to_cpu(leaf, &key, slot);
2709 if (key.objectid == subvol_info->treeid &&
2710 key.type == BTRFS_ROOT_BACKREF_KEY) {
2711 subvol_info->parent_id = key.offset;
2713 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2714 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2716 item_off = btrfs_item_ptr_offset(leaf, slot)
2717 + sizeof(struct btrfs_root_ref);
2718 item_len = btrfs_item_size_nr(leaf, slot)
2719 - sizeof(struct btrfs_root_ref);
2720 read_extent_buffer(leaf, subvol_info->name,
2721 item_off, item_len);
2728 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2732 btrfs_free_path(path);
2733 kzfree(subvol_info);
2738 * Return ROOT_REF information of the subvolume containing this inode
2739 * except the subvolume name.
2741 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2743 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2744 struct btrfs_root_ref *rref;
2745 struct btrfs_root *root;
2746 struct btrfs_path *path;
2747 struct btrfs_key key;
2748 struct extent_buffer *leaf;
2749 struct inode *inode;
2755 path = btrfs_alloc_path();
2759 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2760 if (IS_ERR(rootrefs)) {
2761 btrfs_free_path(path);
2762 return PTR_ERR(rootrefs);
2765 inode = file_inode(file);
2766 root = BTRFS_I(inode)->root->fs_info->tree_root;
2767 objectid = BTRFS_I(inode)->root->root_key.objectid;
2769 key.objectid = objectid;
2770 key.type = BTRFS_ROOT_REF_KEY;
2771 key.offset = rootrefs->min_treeid;
2774 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2777 } else if (path->slots[0] >=
2778 btrfs_header_nritems(path->nodes[0])) {
2779 ret = btrfs_next_leaf(root, path);
2782 } else if (ret > 0) {
2788 leaf = path->nodes[0];
2789 slot = path->slots[0];
2791 btrfs_item_key_to_cpu(leaf, &key, slot);
2792 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2797 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2802 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2803 rootrefs->rootref[found].treeid = key.offset;
2804 rootrefs->rootref[found].dirid =
2805 btrfs_root_ref_dirid(leaf, rref);
2808 ret = btrfs_next_item(root, path);
2811 } else if (ret > 0) {
2818 if (!ret || ret == -EOVERFLOW) {
2819 rootrefs->num_items = found;
2820 /* update min_treeid for next search */
2822 rootrefs->min_treeid =
2823 rootrefs->rootref[found - 1].treeid + 1;
2824 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2829 btrfs_free_path(path);
2834 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2837 struct dentry *parent = file->f_path.dentry;
2838 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2839 struct dentry *dentry;
2840 struct inode *dir = d_inode(parent);
2841 struct inode *inode;
2842 struct btrfs_root *root = BTRFS_I(dir)->root;
2843 struct btrfs_root *dest = NULL;
2844 struct btrfs_ioctl_vol_args *vol_args;
2848 if (!S_ISDIR(dir->i_mode))
2851 vol_args = memdup_user(arg, sizeof(*vol_args));
2852 if (IS_ERR(vol_args))
2853 return PTR_ERR(vol_args);
2855 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2856 namelen = strlen(vol_args->name);
2857 if (strchr(vol_args->name, '/') ||
2858 strncmp(vol_args->name, "..", namelen) == 0) {
2863 err = mnt_want_write_file(file);
2868 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2870 goto out_drop_write;
2871 dentry = lookup_one_len(vol_args->name, parent, namelen);
2872 if (IS_ERR(dentry)) {
2873 err = PTR_ERR(dentry);
2874 goto out_unlock_dir;
2877 if (d_really_is_negative(dentry)) {
2882 inode = d_inode(dentry);
2883 dest = BTRFS_I(inode)->root;
2884 if (!capable(CAP_SYS_ADMIN)) {
2886 * Regular user. Only allow this with a special mount
2887 * option, when the user has write+exec access to the
2888 * subvol root, and when rmdir(2) would have been
2891 * Note that this is _not_ check that the subvol is
2892 * empty or doesn't contain data that we wouldn't
2893 * otherwise be able to delete.
2895 * Users who want to delete empty subvols should try
2899 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
2903 * Do not allow deletion if the parent dir is the same
2904 * as the dir to be deleted. That means the ioctl
2905 * must be called on the dentry referencing the root
2906 * of the subvol, not a random directory contained
2913 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2918 /* check if subvolume may be deleted by a user */
2919 err = btrfs_may_delete(dir, dentry, 1);
2923 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2929 err = btrfs_delete_subvolume(dir, dentry);
2930 inode_unlock(inode);
2939 mnt_drop_write_file(file);
2945 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2947 struct inode *inode = file_inode(file);
2948 struct btrfs_root *root = BTRFS_I(inode)->root;
2949 struct btrfs_ioctl_defrag_range_args *range;
2952 ret = mnt_want_write_file(file);
2956 if (btrfs_root_readonly(root)) {
2961 switch (inode->i_mode & S_IFMT) {
2963 if (!capable(CAP_SYS_ADMIN)) {
2967 ret = btrfs_defrag_root(root);
2971 * Note that this does not check the file descriptor for write
2972 * access. This prevents defragmenting executables that are
2973 * running and allows defrag on files open in read-only mode.
2975 if (!capable(CAP_SYS_ADMIN) &&
2976 inode_permission(inode, MAY_WRITE)) {
2981 range = kzalloc(sizeof(*range), GFP_KERNEL);
2988 if (copy_from_user(range, argp,
2994 /* compression requires us to start the IO */
2995 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2996 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2997 range->extent_thresh = (u32)-1;
3000 /* the rest are all set to zero by kzalloc */
3001 range->len = (u64)-1;
3003 ret = btrfs_defrag_file(file_inode(file), file,
3004 range, BTRFS_OLDEST_GENERATION, 0);
3013 mnt_drop_write_file(file);
3017 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3019 struct btrfs_ioctl_vol_args *vol_args;
3022 if (!capable(CAP_SYS_ADMIN))
3025 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
3026 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3028 vol_args = memdup_user(arg, sizeof(*vol_args));
3029 if (IS_ERR(vol_args)) {
3030 ret = PTR_ERR(vol_args);
3034 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3035 ret = btrfs_init_new_device(fs_info, vol_args->name);
3038 btrfs_info(fs_info, "disk added %s", vol_args->name);
3042 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3046 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3048 struct inode *inode = file_inode(file);
3049 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3050 struct btrfs_ioctl_vol_args_v2 *vol_args;
3053 if (!capable(CAP_SYS_ADMIN))
3056 ret = mnt_want_write_file(file);
3060 vol_args = memdup_user(arg, sizeof(*vol_args));
3061 if (IS_ERR(vol_args)) {
3062 ret = PTR_ERR(vol_args);
3066 /* Check for compatibility reject unknown flags */
3067 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3072 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
3073 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3077 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3078 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3080 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3081 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3083 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3086 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3087 btrfs_info(fs_info, "device deleted: id %llu",
3090 btrfs_info(fs_info, "device deleted: %s",
3096 mnt_drop_write_file(file);
3100 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3102 struct inode *inode = file_inode(file);
3103 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3104 struct btrfs_ioctl_vol_args *vol_args;
3107 if (!capable(CAP_SYS_ADMIN))
3110 ret = mnt_want_write_file(file);
3114 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
3115 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3116 goto out_drop_write;
3119 vol_args = memdup_user(arg, sizeof(*vol_args));
3120 if (IS_ERR(vol_args)) {
3121 ret = PTR_ERR(vol_args);
3125 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3126 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3129 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3132 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3134 mnt_drop_write_file(file);
3139 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3142 struct btrfs_ioctl_fs_info_args *fi_args;
3143 struct btrfs_device *device;
3144 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3147 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3152 fi_args->num_devices = fs_devices->num_devices;
3154 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3155 if (device->devid > fi_args->max_id)
3156 fi_args->max_id = device->devid;
3160 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3161 fi_args->nodesize = fs_info->nodesize;
3162 fi_args->sectorsize = fs_info->sectorsize;
3163 fi_args->clone_alignment = fs_info->sectorsize;
3165 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3172 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3175 struct btrfs_ioctl_dev_info_args *di_args;
3176 struct btrfs_device *dev;
3178 char *s_uuid = NULL;
3180 di_args = memdup_user(arg, sizeof(*di_args));
3181 if (IS_ERR(di_args))
3182 return PTR_ERR(di_args);
3184 if (!btrfs_is_empty_uuid(di_args->uuid))
3185 s_uuid = di_args->uuid;
3188 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3196 di_args->devid = dev->devid;
3197 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3198 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3199 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3201 strncpy(di_args->path, rcu_str_deref(dev->name),
3202 sizeof(di_args->path) - 1);
3203 di_args->path[sizeof(di_args->path) - 1] = 0;
3205 di_args->path[0] = '\0';
3210 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3217 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3218 struct inode *inode2, u64 loff2, u64 len)
3220 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3221 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3224 static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3225 struct inode *inode2, u64 loff2, u64 len)
3227 if (inode1 < inode2) {
3228 swap(inode1, inode2);
3230 } else if (inode1 == inode2 && loff2 < loff1) {
3233 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3234 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3237 static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
3238 struct inode *dst, u64 dst_loff)
3243 * Lock destination range to serialize with concurrent readpages() and
3244 * source range to serialize with relocation.
3246 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
3247 ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
3248 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
3253 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3255 static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3256 struct inode *dst, u64 dst_loff)
3259 u64 i, tail_len, chunk_count;
3260 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3262 spin_lock(&root_dst->root_item_lock);
3263 if (root_dst->send_in_progress) {
3264 btrfs_warn_rl(root_dst->fs_info,
3265 "cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3266 root_dst->root_key.objectid,
3267 root_dst->send_in_progress);
3268 spin_unlock(&root_dst->root_item_lock);
3271 root_dst->dedupe_in_progress++;
3272 spin_unlock(&root_dst->root_item_lock);
3274 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3275 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
3277 for (i = 0; i < chunk_count; i++) {
3278 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
3283 loff += BTRFS_MAX_DEDUPE_LEN;
3284 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3288 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3291 spin_lock(&root_dst->root_item_lock);
3292 root_dst->dedupe_in_progress--;
3293 spin_unlock(&root_dst->root_item_lock);
3298 static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3299 struct inode *inode,
3305 struct btrfs_root *root = BTRFS_I(inode)->root;
3308 inode_inc_iversion(inode);
3309 if (!no_time_update)
3310 inode->i_mtime = inode->i_ctime = current_time(inode);
3312 * We round up to the block size at eof when determining which
3313 * extents to clone above, but shouldn't round up the file size.
3315 if (endoff > destoff + olen)
3316 endoff = destoff + olen;
3317 if (endoff > inode->i_size)
3318 btrfs_i_size_write(BTRFS_I(inode), endoff);
3320 ret = btrfs_update_inode(trans, root, inode);
3322 btrfs_abort_transaction(trans, ret);
3323 btrfs_end_transaction(trans);
3326 ret = btrfs_end_transaction(trans);
3331 static void clone_update_extent_map(struct btrfs_inode *inode,
3332 const struct btrfs_trans_handle *trans,
3333 const struct btrfs_path *path,
3334 const u64 hole_offset,
3337 struct extent_map_tree *em_tree = &inode->extent_tree;
3338 struct extent_map *em;
3341 em = alloc_extent_map();
3343 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
3348 struct btrfs_file_extent_item *fi;
3350 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3351 struct btrfs_file_extent_item);
3352 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3353 em->generation = -1;
3354 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3355 BTRFS_FILE_EXTENT_INLINE)
3356 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3357 &inode->runtime_flags);
3359 em->start = hole_offset;
3361 em->ram_bytes = em->len;
3362 em->orig_start = hole_offset;
3363 em->block_start = EXTENT_MAP_HOLE;
3365 em->orig_block_len = 0;
3366 em->compress_type = BTRFS_COMPRESS_NONE;
3367 em->generation = trans->transid;
3371 write_lock(&em_tree->lock);
3372 ret = add_extent_mapping(em_tree, em, 1);
3373 write_unlock(&em_tree->lock);
3374 if (ret != -EEXIST) {
3375 free_extent_map(em);
3378 btrfs_drop_extent_cache(inode, em->start,
3379 em->start + em->len - 1, 0);
3383 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
3387 * Make sure we do not end up inserting an inline extent into a file that has
3388 * already other (non-inline) extents. If a file has an inline extent it can
3389 * not have any other extents and the (single) inline extent must start at the
3390 * file offset 0. Failing to respect these rules will lead to file corruption,
3391 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3393 * We can have extents that have been already written to disk or we can have
3394 * dirty ranges still in delalloc, in which case the extent maps and items are
3395 * created only when we run delalloc, and the delalloc ranges might fall outside
3396 * the range we are currently locking in the inode's io tree. So we check the
3397 * inode's i_size because of that (i_size updates are done while holding the
3398 * i_mutex, which we are holding here).
3399 * We also check to see if the inode has a size not greater than "datal" but has
3400 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3401 * protected against such concurrent fallocate calls by the i_mutex).
3403 * If the file has no extents but a size greater than datal, do not allow the
3404 * copy because we would need turn the inline extent into a non-inline one (even
3405 * with NO_HOLES enabled). If we find our destination inode only has one inline
3406 * extent, just overwrite it with the source inline extent if its size is less
3407 * than the source extent's size, or we could copy the source inline extent's
3408 * data into the destination inode's inline extent if the later is greater then
3411 static int clone_copy_inline_extent(struct inode *dst,
3412 struct btrfs_trans_handle *trans,
3413 struct btrfs_path *path,
3414 struct btrfs_key *new_key,
3415 const u64 drop_start,
3421 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
3422 struct btrfs_root *root = BTRFS_I(dst)->root;
3423 const u64 aligned_end = ALIGN(new_key->offset + datal,
3424 fs_info->sectorsize);
3426 struct btrfs_key key;
3428 if (new_key->offset > 0)
3431 key.objectid = btrfs_ino(BTRFS_I(dst));
3432 key.type = BTRFS_EXTENT_DATA_KEY;
3434 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3437 } else if (ret > 0) {
3438 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3439 ret = btrfs_next_leaf(root, path);
3443 goto copy_inline_extent;
3445 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3446 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
3447 key.type == BTRFS_EXTENT_DATA_KEY) {
3448 ASSERT(key.offset > 0);
3451 } else if (i_size_read(dst) <= datal) {
3452 struct btrfs_file_extent_item *ei;
3456 * If the file size is <= datal, make sure there are no other
3457 * extents following (can happen do to an fallocate call with
3458 * the flag FALLOC_FL_KEEP_SIZE).
3460 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3461 struct btrfs_file_extent_item);
3463 * If it's an inline extent, it can not have other extents
3466 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3467 BTRFS_FILE_EXTENT_INLINE)
3468 goto copy_inline_extent;
3470 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3471 if (ext_len > aligned_end)
3474 ret = btrfs_next_item(root, path);
3477 } else if (ret == 0) {
3478 btrfs_item_key_to_cpu(path->nodes[0], &key,
3480 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
3481 key.type == BTRFS_EXTENT_DATA_KEY)
3488 * We have no extent items, or we have an extent at offset 0 which may
3489 * or may not be inlined. All these cases are dealt the same way.
3491 if (i_size_read(dst) > datal) {
3493 * If the destination inode has an inline extent...
3494 * This would require copying the data from the source inline
3495 * extent into the beginning of the destination's inline extent.
3496 * But this is really complex, both extents can be compressed
3497 * or just one of them, which would require decompressing and
3498 * re-compressing data (which could increase the new compressed
3499 * size, not allowing the compressed data to fit anymore in an
3501 * So just don't support this case for now (it should be rare,
3502 * we are not really saving space when cloning inline extents).
3507 btrfs_release_path(path);
3508 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3511 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3516 const u32 start = btrfs_file_extent_calc_inline_size(0);
3518 memmove(inline_data + start, inline_data + start + skip, datal);
3521 write_extent_buffer(path->nodes[0], inline_data,
3522 btrfs_item_ptr_offset(path->nodes[0],
3525 inode_add_bytes(dst, datal);
3531 * btrfs_clone() - clone a range from inode file to another
3533 * @src: Inode to clone from
3534 * @inode: Inode to clone to
3535 * @off: Offset within source to start clone from
3536 * @olen: Original length, passed by user, of range to clone
3537 * @olen_aligned: Block-aligned value of olen
3538 * @destoff: Offset within @inode to start clone
3539 * @no_time_update: Whether to update mtime/ctime on the target inode
3541 static int btrfs_clone(struct inode *src, struct inode *inode,
3542 const u64 off, const u64 olen, const u64 olen_aligned,
3543 const u64 destoff, int no_time_update)
3545 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3546 struct btrfs_root *root = BTRFS_I(inode)->root;
3547 struct btrfs_path *path = NULL;
3548 struct extent_buffer *leaf;
3549 struct btrfs_trans_handle *trans;
3551 struct btrfs_key key;
3555 const u64 len = olen_aligned;
3556 u64 last_dest_end = destoff;
3559 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3563 path = btrfs_alloc_path();
3569 path->reada = READA_FORWARD;
3571 key.objectid = btrfs_ino(BTRFS_I(src));
3572 key.type = BTRFS_EXTENT_DATA_KEY;
3576 u64 next_key_min_offset = key.offset + 1;
3579 * note the key will change type as we walk through the
3582 path->leave_spinning = 1;
3583 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3588 * First search, if no extent item that starts at offset off was
3589 * found but the previous item is an extent item, it's possible
3590 * it might overlap our target range, therefore process it.
3592 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3593 btrfs_item_key_to_cpu(path->nodes[0], &key,
3594 path->slots[0] - 1);
3595 if (key.type == BTRFS_EXTENT_DATA_KEY)
3599 nritems = btrfs_header_nritems(path->nodes[0]);
3601 if (path->slots[0] >= nritems) {
3602 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3607 nritems = btrfs_header_nritems(path->nodes[0]);
3609 leaf = path->nodes[0];
3610 slot = path->slots[0];
3612 btrfs_item_key_to_cpu(leaf, &key, slot);
3613 if (key.type > BTRFS_EXTENT_DATA_KEY ||
3614 key.objectid != btrfs_ino(BTRFS_I(src)))
3617 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3618 struct btrfs_file_extent_item *extent;
3621 struct btrfs_key new_key;
3622 u64 disko = 0, diskl = 0;
3623 u64 datao = 0, datal = 0;
3627 extent = btrfs_item_ptr(leaf, slot,
3628 struct btrfs_file_extent_item);
3629 comp = btrfs_file_extent_compression(leaf, extent);
3630 type = btrfs_file_extent_type(leaf, extent);
3631 if (type == BTRFS_FILE_EXTENT_REG ||
3632 type == BTRFS_FILE_EXTENT_PREALLOC) {
3633 disko = btrfs_file_extent_disk_bytenr(leaf,
3635 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3637 datao = btrfs_file_extent_offset(leaf, extent);
3638 datal = btrfs_file_extent_num_bytes(leaf,
3640 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3641 /* take upper bound, may be compressed */
3642 datal = btrfs_file_extent_ram_bytes(leaf,
3647 * The first search might have left us at an extent
3648 * item that ends before our target range's start, can
3649 * happen if we have holes and NO_HOLES feature enabled.
3651 if (key.offset + datal <= off) {
3654 } else if (key.offset >= off + len) {
3657 next_key_min_offset = key.offset + datal;
3658 size = btrfs_item_size_nr(leaf, slot);
3659 read_extent_buffer(leaf, buf,
3660 btrfs_item_ptr_offset(leaf, slot),
3663 btrfs_release_path(path);
3664 path->leave_spinning = 0;
3666 memcpy(&new_key, &key, sizeof(new_key));
3667 new_key.objectid = btrfs_ino(BTRFS_I(inode));
3668 if (off <= key.offset)
3669 new_key.offset = key.offset + destoff - off;
3671 new_key.offset = destoff;
3674 * Deal with a hole that doesn't have an extent item
3675 * that represents it (NO_HOLES feature enabled).
3676 * This hole is either in the middle of the cloning
3677 * range or at the beginning (fully overlaps it or
3678 * partially overlaps it).
3680 if (new_key.offset != last_dest_end)
3681 drop_start = last_dest_end;
3683 drop_start = new_key.offset;
3686 * 1 - adjusting old extent (we may have to split it)
3687 * 1 - add new extent
3690 trans = btrfs_start_transaction(root, 3);
3691 if (IS_ERR(trans)) {
3692 ret = PTR_ERR(trans);
3696 if (type == BTRFS_FILE_EXTENT_REG ||
3697 type == BTRFS_FILE_EXTENT_PREALLOC) {
3699 * a | --- range to clone ---| b
3700 * | ------------- extent ------------- |
3703 /* subtract range b */
3704 if (key.offset + datal > off + len)
3705 datal = off + len - key.offset;
3707 /* subtract range a */
3708 if (off > key.offset) {
3709 datao += off - key.offset;
3710 datal -= off - key.offset;
3713 ret = btrfs_drop_extents(trans, root, inode,
3715 new_key.offset + datal,
3718 if (ret != -EOPNOTSUPP)
3719 btrfs_abort_transaction(trans,
3721 btrfs_end_transaction(trans);
3725 ret = btrfs_insert_empty_item(trans, root, path,
3728 btrfs_abort_transaction(trans, ret);
3729 btrfs_end_transaction(trans);
3733 leaf = path->nodes[0];
3734 slot = path->slots[0];
3735 write_extent_buffer(leaf, buf,
3736 btrfs_item_ptr_offset(leaf, slot),
3739 extent = btrfs_item_ptr(leaf, slot,
3740 struct btrfs_file_extent_item);
3742 /* disko == 0 means it's a hole */
3746 btrfs_set_file_extent_offset(leaf, extent,
3748 btrfs_set_file_extent_num_bytes(leaf, extent,
3752 struct btrfs_ref ref = { 0 };
3753 inode_add_bytes(inode, datal);
3754 btrfs_init_generic_ref(&ref,
3755 BTRFS_ADD_DELAYED_REF, disko,
3757 btrfs_init_data_ref(&ref,
3758 root->root_key.objectid,
3759 btrfs_ino(BTRFS_I(inode)),
3760 new_key.offset - datao);
3761 ret = btrfs_inc_extent_ref(trans, &ref);
3763 btrfs_abort_transaction(trans,
3765 btrfs_end_transaction(trans);
3770 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3774 if (off > key.offset) {
3775 skip = off - key.offset;
3776 new_key.offset += skip;
3779 if (key.offset + datal > off + len)
3780 trim = key.offset + datal - (off + len);
3782 if (comp && (skip || trim)) {
3784 btrfs_end_transaction(trans);
3787 size -= skip + trim;
3788 datal -= skip + trim;
3790 ret = clone_copy_inline_extent(inode,
3797 if (ret != -EOPNOTSUPP)
3798 btrfs_abort_transaction(trans,
3800 btrfs_end_transaction(trans);
3803 leaf = path->nodes[0];
3804 slot = path->slots[0];
3807 /* If we have an implicit hole (NO_HOLES feature). */
3808 if (drop_start < new_key.offset)
3809 clone_update_extent_map(BTRFS_I(inode), trans,
3811 new_key.offset - drop_start);
3813 clone_update_extent_map(BTRFS_I(inode), trans,
3816 btrfs_mark_buffer_dirty(leaf);
3817 btrfs_release_path(path);
3819 last_dest_end = ALIGN(new_key.offset + datal,
3820 fs_info->sectorsize);
3821 ret = clone_finish_inode_update(trans, inode,
3827 if (new_key.offset + datal >= destoff + len)
3830 btrfs_release_path(path);
3831 key.offset = next_key_min_offset;
3833 if (fatal_signal_pending(current)) {
3840 if (last_dest_end < destoff + len) {
3842 * We have an implicit hole (NO_HOLES feature is enabled) that
3843 * fully or partially overlaps our cloning range at its end.
3845 btrfs_release_path(path);
3848 * 1 - remove extent(s)
3851 trans = btrfs_start_transaction(root, 2);
3852 if (IS_ERR(trans)) {
3853 ret = PTR_ERR(trans);
3856 ret = btrfs_drop_extents(trans, root, inode,
3857 last_dest_end, destoff + len, 1);
3859 if (ret != -EOPNOTSUPP)
3860 btrfs_abort_transaction(trans, ret);
3861 btrfs_end_transaction(trans);
3864 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3866 destoff + len - last_dest_end);
3867 ret = clone_finish_inode_update(trans, inode, destoff + len,
3868 destoff, olen, no_time_update);
3872 btrfs_free_path(path);
3877 static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3878 u64 off, u64 olen, u64 destoff)
3880 struct inode *inode = file_inode(file);
3881 struct inode *src = file_inode(file_src);
3882 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3885 u64 bs = fs_info->sb->s_blocksize;
3889 * - split compressed inline extents. annoying: we need to
3890 * decompress into destination's address_space (the file offset
3891 * may change, so source mapping won't do), then recompress (or
3892 * otherwise reinsert) a subrange.
3894 * - split destination inode's inline extents. The inline extents can
3895 * be either compressed or non-compressed.
3899 * VFS's generic_remap_file_range_prep() protects us from cloning the
3900 * eof block into the middle of a file, which would result in corruption
3901 * if the file size is not blocksize aligned. So we don't need to check
3902 * for that case here.
3904 if (off + len == src->i_size)
3905 len = ALIGN(src->i_size, bs) - off;
3907 if (destoff > inode->i_size) {
3908 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3910 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3914 * We may have truncated the last block if the inode's size is
3915 * not sector size aligned, so we need to wait for writeback to
3916 * complete before proceeding further, otherwise we can race
3917 * with cloning and attempt to increment a reference to an
3918 * extent that no longer exists (writeback completed right after
3919 * we found the previous extent covering eof and before we
3920 * attempted to increment its reference count).
3922 ret = btrfs_wait_ordered_range(inode, wb_start,
3923 destoff - wb_start);
3929 * Lock destination range to serialize with concurrent readpages() and
3930 * source range to serialize with relocation.
3932 btrfs_double_extent_lock(src, off, inode, destoff, len);
3933 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
3934 btrfs_double_extent_unlock(src, off, inode, destoff, len);
3936 * Truncate page cache pages so that future reads will see the cloned
3937 * data immediately and not the previous data.
3939 truncate_inode_pages_range(&inode->i_data,
3940 round_down(destoff, PAGE_SIZE),
3941 round_up(destoff + len, PAGE_SIZE) - 1);
3946 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3947 struct file *file_out, loff_t pos_out,
3948 loff_t *len, unsigned int remap_flags)
3950 struct inode *inode_in = file_inode(file_in);
3951 struct inode *inode_out = file_inode(file_out);
3952 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3953 bool same_inode = inode_out == inode_in;
3957 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3958 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3960 if (btrfs_root_readonly(root_out))
3963 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3964 inode_in->i_sb != inode_out->i_sb)
3968 /* don't make the dst file partly checksummed */
3969 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3970 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
3975 * Now that the inodes are locked, we need to start writeback ourselves
3976 * and can not rely on the writeback from the VFS's generic helper
3977 * generic_remap_file_range_prep() because:
3979 * 1) For compression we must call filemap_fdatawrite_range() range
3980 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3981 * helper only calls it once;
3983 * 2) filemap_fdatawrite_range(), called by the generic helper only
3984 * waits for the writeback to complete, i.e. for IO to be done, and
3985 * not for the ordered extents to complete. We need to wait for them
3986 * to complete so that new file extent items are in the fs tree.
3988 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3989 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3991 wb_len = ALIGN(*len, bs);
3994 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3995 * any in progress could create its ordered extents after we wait for
3996 * existing ordered extents below).
3998 inode_dio_wait(inode_in);
4000 inode_dio_wait(inode_out);
4002 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
4006 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
4011 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
4015 loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4016 struct file *dst_file, loff_t destoff, loff_t len,
4017 unsigned int remap_flags)
4019 struct inode *src_inode = file_inode(src_file);
4020 struct inode *dst_inode = file_inode(dst_file);
4021 bool same_inode = dst_inode == src_inode;
4024 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4028 inode_lock(src_inode);
4030 lock_two_nondirectories(src_inode, dst_inode);
4032 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4034 if (ret < 0 || len == 0)
4037 if (remap_flags & REMAP_FILE_DEDUP)
4038 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4040 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
4044 inode_unlock(src_inode);
4046 unlock_two_nondirectories(src_inode, dst_inode);
4048 return ret < 0 ? ret : len;
4051 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4053 struct inode *inode = file_inode(file);
4054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4055 struct btrfs_root *root = BTRFS_I(inode)->root;
4056 struct btrfs_root *new_root;
4057 struct btrfs_dir_item *di;
4058 struct btrfs_trans_handle *trans;
4059 struct btrfs_path *path;
4060 struct btrfs_key location;
4061 struct btrfs_disk_key disk_key;
4066 if (!capable(CAP_SYS_ADMIN))
4069 ret = mnt_want_write_file(file);
4073 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4079 objectid = BTRFS_FS_TREE_OBJECTID;
4081 location.objectid = objectid;
4082 location.type = BTRFS_ROOT_ITEM_KEY;
4083 location.offset = (u64)-1;
4085 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
4086 if (IS_ERR(new_root)) {
4087 ret = PTR_ERR(new_root);
4090 if (!is_fstree(new_root->root_key.objectid)) {
4095 path = btrfs_alloc_path();
4100 path->leave_spinning = 1;
4102 trans = btrfs_start_transaction(root, 1);
4103 if (IS_ERR(trans)) {
4104 btrfs_free_path(path);
4105 ret = PTR_ERR(trans);
4109 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4110 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
4111 dir_id, "default", 7, 1);
4112 if (IS_ERR_OR_NULL(di)) {
4113 btrfs_free_path(path);
4114 btrfs_end_transaction(trans);
4116 "Umm, you don't have the default diritem, this isn't going to work");
4121 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4122 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4123 btrfs_mark_buffer_dirty(path->nodes[0]);
4124 btrfs_free_path(path);
4126 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
4127 btrfs_end_transaction(trans);
4129 mnt_drop_write_file(file);
4133 static void get_block_group_info(struct list_head *groups_list,
4134 struct btrfs_ioctl_space_info *space)
4136 struct btrfs_block_group_cache *block_group;
4138 space->total_bytes = 0;
4139 space->used_bytes = 0;
4141 list_for_each_entry(block_group, groups_list, list) {
4142 space->flags = block_group->flags;
4143 space->total_bytes += block_group->key.offset;
4144 space->used_bytes +=
4145 btrfs_block_group_used(&block_group->item);
4149 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4152 struct btrfs_ioctl_space_args space_args;
4153 struct btrfs_ioctl_space_info space;
4154 struct btrfs_ioctl_space_info *dest;
4155 struct btrfs_ioctl_space_info *dest_orig;
4156 struct btrfs_ioctl_space_info __user *user_dest;
4157 struct btrfs_space_info *info;
4158 static const u64 types[] = {
4159 BTRFS_BLOCK_GROUP_DATA,
4160 BTRFS_BLOCK_GROUP_SYSTEM,
4161 BTRFS_BLOCK_GROUP_METADATA,
4162 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4170 if (copy_from_user(&space_args,
4171 (struct btrfs_ioctl_space_args __user *)arg,
4172 sizeof(space_args)))
4175 for (i = 0; i < num_types; i++) {
4176 struct btrfs_space_info *tmp;
4180 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4182 if (tmp->flags == types[i]) {
4192 down_read(&info->groups_sem);
4193 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4194 if (!list_empty(&info->block_groups[c]))
4197 up_read(&info->groups_sem);
4201 * Global block reserve, exported as a space_info
4205 /* space_slots == 0 means they are asking for a count */
4206 if (space_args.space_slots == 0) {
4207 space_args.total_spaces = slot_count;
4211 slot_count = min_t(u64, space_args.space_slots, slot_count);
4213 alloc_size = sizeof(*dest) * slot_count;
4215 /* we generally have at most 6 or so space infos, one for each raid
4216 * level. So, a whole page should be more than enough for everyone
4218 if (alloc_size > PAGE_SIZE)
4221 space_args.total_spaces = 0;
4222 dest = kmalloc(alloc_size, GFP_KERNEL);
4227 /* now we have a buffer to copy into */
4228 for (i = 0; i < num_types; i++) {
4229 struct btrfs_space_info *tmp;
4236 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4238 if (tmp->flags == types[i]) {
4247 down_read(&info->groups_sem);
4248 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4249 if (!list_empty(&info->block_groups[c])) {
4250 get_block_group_info(&info->block_groups[c],
4252 memcpy(dest, &space, sizeof(space));
4254 space_args.total_spaces++;
4260 up_read(&info->groups_sem);
4264 * Add global block reserve
4267 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4269 spin_lock(&block_rsv->lock);
4270 space.total_bytes = block_rsv->size;
4271 space.used_bytes = block_rsv->size - block_rsv->reserved;
4272 spin_unlock(&block_rsv->lock);
4273 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4274 memcpy(dest, &space, sizeof(space));
4275 space_args.total_spaces++;
4278 user_dest = (struct btrfs_ioctl_space_info __user *)
4279 (arg + sizeof(struct btrfs_ioctl_space_args));
4281 if (copy_to_user(user_dest, dest_orig, alloc_size))
4286 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
4292 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4295 struct btrfs_trans_handle *trans;
4299 trans = btrfs_attach_transaction_barrier(root);
4300 if (IS_ERR(trans)) {
4301 if (PTR_ERR(trans) != -ENOENT)
4302 return PTR_ERR(trans);
4304 /* No running transaction, don't bother */
4305 transid = root->fs_info->last_trans_committed;
4308 transid = trans->transid;
4309 ret = btrfs_commit_transaction_async(trans, 0);
4311 btrfs_end_transaction(trans);
4316 if (copy_to_user(argp, &transid, sizeof(transid)))
4321 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
4327 if (copy_from_user(&transid, argp, sizeof(transid)))
4330 transid = 0; /* current trans */
4332 return btrfs_wait_for_commit(fs_info, transid);
4335 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4337 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
4338 struct btrfs_ioctl_scrub_args *sa;
4341 if (!capable(CAP_SYS_ADMIN))
4344 sa = memdup_user(arg, sizeof(*sa));
4348 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4349 ret = mnt_want_write_file(file);
4354 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
4355 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4358 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4361 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4362 mnt_drop_write_file(file);
4368 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
4370 if (!capable(CAP_SYS_ADMIN))
4373 return btrfs_scrub_cancel(fs_info);
4376 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
4379 struct btrfs_ioctl_scrub_args *sa;
4382 if (!capable(CAP_SYS_ADMIN))
4385 sa = memdup_user(arg, sizeof(*sa));
4389 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
4391 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4398 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
4401 struct btrfs_ioctl_get_dev_stats *sa;
4404 sa = memdup_user(arg, sizeof(*sa));
4408 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4413 ret = btrfs_get_dev_stats(fs_info, sa);
4415 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4422 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4425 struct btrfs_ioctl_dev_replace_args *p;
4428 if (!capable(CAP_SYS_ADMIN))
4431 p = memdup_user(arg, sizeof(*p));
4436 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
4437 if (sb_rdonly(fs_info->sb)) {
4441 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
4442 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4444 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
4445 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4448 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4449 btrfs_dev_replace_status(fs_info, p);
4452 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4453 p->result = btrfs_dev_replace_cancel(fs_info);
4461 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
4468 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4474 struct btrfs_ioctl_ino_path_args *ipa = NULL;
4475 struct inode_fs_paths *ipath = NULL;
4476 struct btrfs_path *path;
4478 if (!capable(CAP_DAC_READ_SEARCH))
4481 path = btrfs_alloc_path();
4487 ipa = memdup_user(arg, sizeof(*ipa));
4494 size = min_t(u32, ipa->size, 4096);
4495 ipath = init_ipath(size, root, path);
4496 if (IS_ERR(ipath)) {
4497 ret = PTR_ERR(ipath);
4502 ret = paths_from_inode(ipa->inum, ipath);
4506 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
4507 rel_ptr = ipath->fspath->val[i] -
4508 (u64)(unsigned long)ipath->fspath->val;
4509 ipath->fspath->val[i] = rel_ptr;
4512 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4513 ipath->fspath, size);
4520 btrfs_free_path(path);
4527 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4529 struct btrfs_data_container *inodes = ctx;
4530 const size_t c = 3 * sizeof(u64);
4532 if (inodes->bytes_left >= c) {
4533 inodes->bytes_left -= c;
4534 inodes->val[inodes->elem_cnt] = inum;
4535 inodes->val[inodes->elem_cnt + 1] = offset;
4536 inodes->val[inodes->elem_cnt + 2] = root;
4537 inodes->elem_cnt += 3;
4539 inodes->bytes_missing += c - inodes->bytes_left;
4540 inodes->bytes_left = 0;
4541 inodes->elem_missed += 3;
4547 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
4548 void __user *arg, int version)
4552 struct btrfs_ioctl_logical_ino_args *loi;
4553 struct btrfs_data_container *inodes = NULL;
4554 struct btrfs_path *path = NULL;
4557 if (!capable(CAP_SYS_ADMIN))
4560 loi = memdup_user(arg, sizeof(*loi));
4562 return PTR_ERR(loi);
4565 ignore_offset = false;
4566 size = min_t(u32, loi->size, SZ_64K);
4568 /* All reserved bits must be 0 for now */
4569 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4573 /* Only accept flags we have defined so far */
4574 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4578 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
4579 size = min_t(u32, loi->size, SZ_16M);
4582 path = btrfs_alloc_path();
4588 inodes = init_data_container(size);
4589 if (IS_ERR(inodes)) {
4590 ret = PTR_ERR(inodes);
4595 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
4596 build_ino_list, inodes, ignore_offset);
4602 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4608 btrfs_free_path(path);
4616 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
4617 struct btrfs_ioctl_balance_args *bargs)
4619 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4621 bargs->flags = bctl->flags;
4623 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
4624 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4625 if (atomic_read(&fs_info->balance_pause_req))
4626 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4627 if (atomic_read(&fs_info->balance_cancel_req))
4628 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4630 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4631 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4632 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
4634 spin_lock(&fs_info->balance_lock);
4635 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4636 spin_unlock(&fs_info->balance_lock);
4639 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4641 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4642 struct btrfs_fs_info *fs_info = root->fs_info;
4643 struct btrfs_ioctl_balance_args *bargs;
4644 struct btrfs_balance_control *bctl;
4645 bool need_unlock; /* for mut. excl. ops lock */
4648 if (!capable(CAP_SYS_ADMIN))
4651 ret = mnt_want_write_file(file);
4656 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
4657 mutex_lock(&fs_info->balance_mutex);
4663 * mut. excl. ops lock is locked. Three possibilities:
4664 * (1) some other op is running
4665 * (2) balance is running
4666 * (3) balance is paused -- special case (think resume)
4668 mutex_lock(&fs_info->balance_mutex);
4669 if (fs_info->balance_ctl) {
4670 /* this is either (2) or (3) */
4671 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4672 mutex_unlock(&fs_info->balance_mutex);
4674 * Lock released to allow other waiters to continue,
4675 * we'll reexamine the status again.
4677 mutex_lock(&fs_info->balance_mutex);
4679 if (fs_info->balance_ctl &&
4680 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4682 need_unlock = false;
4686 mutex_unlock(&fs_info->balance_mutex);
4690 mutex_unlock(&fs_info->balance_mutex);
4696 mutex_unlock(&fs_info->balance_mutex);
4697 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4702 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
4705 bargs = memdup_user(arg, sizeof(*bargs));
4706 if (IS_ERR(bargs)) {
4707 ret = PTR_ERR(bargs);
4711 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4712 if (!fs_info->balance_ctl) {
4717 bctl = fs_info->balance_ctl;
4718 spin_lock(&fs_info->balance_lock);
4719 bctl->flags |= BTRFS_BALANCE_RESUME;
4720 spin_unlock(&fs_info->balance_lock);
4728 if (fs_info->balance_ctl) {
4733 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4740 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4741 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4742 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4744 bctl->flags = bargs->flags;
4746 /* balance everything - no filters */
4747 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4750 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4757 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4758 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4759 * restriper was paused all the way until unmount, in free_fs_info.
4760 * The flag should be cleared after reset_balance_state.
4762 need_unlock = false;
4764 ret = btrfs_balance(fs_info, bctl, bargs);
4767 if ((ret == 0 || ret == -ECANCELED) && arg) {
4768 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4777 mutex_unlock(&fs_info->balance_mutex);
4779 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4781 mnt_drop_write_file(file);
4785 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4787 if (!capable(CAP_SYS_ADMIN))
4791 case BTRFS_BALANCE_CTL_PAUSE:
4792 return btrfs_pause_balance(fs_info);
4793 case BTRFS_BALANCE_CTL_CANCEL:
4794 return btrfs_cancel_balance(fs_info);
4800 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4803 struct btrfs_ioctl_balance_args *bargs;
4806 if (!capable(CAP_SYS_ADMIN))
4809 mutex_lock(&fs_info->balance_mutex);
4810 if (!fs_info->balance_ctl) {
4815 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4821 btrfs_update_ioctl_balance_args(fs_info, bargs);
4823 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4828 mutex_unlock(&fs_info->balance_mutex);
4832 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4834 struct inode *inode = file_inode(file);
4835 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4836 struct btrfs_ioctl_quota_ctl_args *sa;
4839 if (!capable(CAP_SYS_ADMIN))
4842 ret = mnt_want_write_file(file);
4846 sa = memdup_user(arg, sizeof(*sa));
4852 down_write(&fs_info->subvol_sem);
4855 case BTRFS_QUOTA_CTL_ENABLE:
4856 ret = btrfs_quota_enable(fs_info);
4858 case BTRFS_QUOTA_CTL_DISABLE:
4859 ret = btrfs_quota_disable(fs_info);
4867 up_write(&fs_info->subvol_sem);
4869 mnt_drop_write_file(file);
4873 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4875 struct inode *inode = file_inode(file);
4876 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4877 struct btrfs_root *root = BTRFS_I(inode)->root;
4878 struct btrfs_ioctl_qgroup_assign_args *sa;
4879 struct btrfs_trans_handle *trans;
4883 if (!capable(CAP_SYS_ADMIN))
4886 ret = mnt_want_write_file(file);
4890 sa = memdup_user(arg, sizeof(*sa));
4896 trans = btrfs_join_transaction(root);
4897 if (IS_ERR(trans)) {
4898 ret = PTR_ERR(trans);
4903 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4905 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4908 /* update qgroup status and info */
4909 err = btrfs_run_qgroups(trans);
4911 btrfs_handle_fs_error(fs_info, err,
4912 "failed to update qgroup status and info");
4913 err = btrfs_end_transaction(trans);
4920 mnt_drop_write_file(file);
4924 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4926 struct inode *inode = file_inode(file);
4927 struct btrfs_root *root = BTRFS_I(inode)->root;
4928 struct btrfs_ioctl_qgroup_create_args *sa;
4929 struct btrfs_trans_handle *trans;
4933 if (!capable(CAP_SYS_ADMIN))
4936 ret = mnt_want_write_file(file);
4940 sa = memdup_user(arg, sizeof(*sa));
4946 if (!sa->qgroupid) {
4951 trans = btrfs_join_transaction(root);
4952 if (IS_ERR(trans)) {
4953 ret = PTR_ERR(trans);
4958 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4960 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4963 err = btrfs_end_transaction(trans);
4970 mnt_drop_write_file(file);
4974 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4976 struct inode *inode = file_inode(file);
4977 struct btrfs_root *root = BTRFS_I(inode)->root;
4978 struct btrfs_ioctl_qgroup_limit_args *sa;
4979 struct btrfs_trans_handle *trans;
4984 if (!capable(CAP_SYS_ADMIN))
4987 ret = mnt_want_write_file(file);
4991 sa = memdup_user(arg, sizeof(*sa));
4997 trans = btrfs_join_transaction(root);
4998 if (IS_ERR(trans)) {
4999 ret = PTR_ERR(trans);
5003 qgroupid = sa->qgroupid;
5005 /* take the current subvol as qgroup */
5006 qgroupid = root->root_key.objectid;
5009 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
5011 err = btrfs_end_transaction(trans);
5018 mnt_drop_write_file(file);
5022 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5024 struct inode *inode = file_inode(file);
5025 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5026 struct btrfs_ioctl_quota_rescan_args *qsa;
5029 if (!capable(CAP_SYS_ADMIN))
5032 ret = mnt_want_write_file(file);
5036 qsa = memdup_user(arg, sizeof(*qsa));
5047 ret = btrfs_qgroup_rescan(fs_info);
5052 mnt_drop_write_file(file);
5056 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5058 struct inode *inode = file_inode(file);
5059 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5060 struct btrfs_ioctl_quota_rescan_args *qsa;
5063 if (!capable(CAP_SYS_ADMIN))
5066 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
5070 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5072 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
5075 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5082 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5084 struct inode *inode = file_inode(file);
5085 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5087 if (!capable(CAP_SYS_ADMIN))
5090 return btrfs_qgroup_wait_for_completion(fs_info, true);
5093 static long _btrfs_ioctl_set_received_subvol(struct file *file,
5094 struct btrfs_ioctl_received_subvol_args *sa)
5096 struct inode *inode = file_inode(file);
5097 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5098 struct btrfs_root *root = BTRFS_I(inode)->root;
5099 struct btrfs_root_item *root_item = &root->root_item;
5100 struct btrfs_trans_handle *trans;
5101 struct timespec64 ct = current_time(inode);
5103 int received_uuid_changed;
5105 if (!inode_owner_or_capable(inode))
5108 ret = mnt_want_write_file(file);
5112 down_write(&fs_info->subvol_sem);
5114 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
5119 if (btrfs_root_readonly(root)) {
5126 * 2 - uuid items (received uuid + subvol uuid)
5128 trans = btrfs_start_transaction(root, 3);
5129 if (IS_ERR(trans)) {
5130 ret = PTR_ERR(trans);
5135 sa->rtransid = trans->transid;
5136 sa->rtime.sec = ct.tv_sec;
5137 sa->rtime.nsec = ct.tv_nsec;
5139 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5141 if (received_uuid_changed &&
5142 !btrfs_is_empty_uuid(root_item->received_uuid)) {
5143 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
5144 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5145 root->root_key.objectid);
5146 if (ret && ret != -ENOENT) {
5147 btrfs_abort_transaction(trans, ret);
5148 btrfs_end_transaction(trans);
5152 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5153 btrfs_set_root_stransid(root_item, sa->stransid);
5154 btrfs_set_root_rtransid(root_item, sa->rtransid);
5155 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5156 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5157 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5158 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
5160 ret = btrfs_update_root(trans, fs_info->tree_root,
5161 &root->root_key, &root->root_item);
5163 btrfs_end_transaction(trans);
5166 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5167 ret = btrfs_uuid_tree_add(trans, sa->uuid,
5168 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5169 root->root_key.objectid);
5170 if (ret < 0 && ret != -EEXIST) {
5171 btrfs_abort_transaction(trans, ret);
5172 btrfs_end_transaction(trans);
5176 ret = btrfs_commit_transaction(trans);
5178 up_write(&fs_info->subvol_sem);
5179 mnt_drop_write_file(file);
5184 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5187 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5188 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5191 args32 = memdup_user(arg, sizeof(*args32));
5193 return PTR_ERR(args32);
5195 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
5201 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5202 args64->stransid = args32->stransid;
5203 args64->rtransid = args32->rtransid;
5204 args64->stime.sec = args32->stime.sec;
5205 args64->stime.nsec = args32->stime.nsec;
5206 args64->rtime.sec = args32->rtime.sec;
5207 args64->rtime.nsec = args32->rtime.nsec;
5208 args64->flags = args32->flags;
5210 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5214 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5215 args32->stransid = args64->stransid;
5216 args32->rtransid = args64->rtransid;
5217 args32->stime.sec = args64->stime.sec;
5218 args32->stime.nsec = args64->stime.nsec;
5219 args32->rtime.sec = args64->rtime.sec;
5220 args32->rtime.nsec = args64->rtime.nsec;
5221 args32->flags = args64->flags;
5223 ret = copy_to_user(arg, args32, sizeof(*args32));
5234 static long btrfs_ioctl_set_received_subvol(struct file *file,
5237 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5240 sa = memdup_user(arg, sizeof(*sa));
5244 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5249 ret = copy_to_user(arg, sa, sizeof(*sa));
5258 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5260 struct inode *inode = file_inode(file);
5261 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5264 char label[BTRFS_LABEL_SIZE];
5266 spin_lock(&fs_info->super_lock);
5267 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5268 spin_unlock(&fs_info->super_lock);
5270 len = strnlen(label, BTRFS_LABEL_SIZE);
5272 if (len == BTRFS_LABEL_SIZE) {
5274 "label is too long, return the first %zu bytes",
5278 ret = copy_to_user(arg, label, len);
5280 return ret ? -EFAULT : 0;
5283 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5285 struct inode *inode = file_inode(file);
5286 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5287 struct btrfs_root *root = BTRFS_I(inode)->root;
5288 struct btrfs_super_block *super_block = fs_info->super_copy;
5289 struct btrfs_trans_handle *trans;
5290 char label[BTRFS_LABEL_SIZE];
5293 if (!capable(CAP_SYS_ADMIN))
5296 if (copy_from_user(label, arg, sizeof(label)))
5299 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
5301 "unable to set label with more than %d bytes",
5302 BTRFS_LABEL_SIZE - 1);
5306 ret = mnt_want_write_file(file);
5310 trans = btrfs_start_transaction(root, 0);
5311 if (IS_ERR(trans)) {
5312 ret = PTR_ERR(trans);
5316 spin_lock(&fs_info->super_lock);
5317 strcpy(super_block->label, label);
5318 spin_unlock(&fs_info->super_lock);
5319 ret = btrfs_commit_transaction(trans);
5322 mnt_drop_write_file(file);
5326 #define INIT_FEATURE_FLAGS(suffix) \
5327 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5328 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5329 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5331 int btrfs_ioctl_get_supported_features(void __user *arg)
5333 static const struct btrfs_ioctl_feature_flags features[3] = {
5334 INIT_FEATURE_FLAGS(SUPP),
5335 INIT_FEATURE_FLAGS(SAFE_SET),
5336 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5339 if (copy_to_user(arg, &features, sizeof(features)))
5345 static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5347 struct inode *inode = file_inode(file);
5348 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5349 struct btrfs_super_block *super_block = fs_info->super_copy;
5350 struct btrfs_ioctl_feature_flags features;
5352 features.compat_flags = btrfs_super_compat_flags(super_block);
5353 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5354 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5356 if (copy_to_user(arg, &features, sizeof(features)))
5362 static int check_feature_bits(struct btrfs_fs_info *fs_info,
5363 enum btrfs_feature_set set,
5364 u64 change_mask, u64 flags, u64 supported_flags,
5365 u64 safe_set, u64 safe_clear)
5367 const char *type = btrfs_feature_set_names[set];
5369 u64 disallowed, unsupported;
5370 u64 set_mask = flags & change_mask;
5371 u64 clear_mask = ~flags & change_mask;
5373 unsupported = set_mask & ~supported_flags;
5375 names = btrfs_printable_features(set, unsupported);
5378 "this kernel does not support the %s feature bit%s",
5379 names, strchr(names, ',') ? "s" : "");
5383 "this kernel does not support %s bits 0x%llx",
5388 disallowed = set_mask & ~safe_set;
5390 names = btrfs_printable_features(set, disallowed);
5393 "can't set the %s feature bit%s while mounted",
5394 names, strchr(names, ',') ? "s" : "");
5398 "can't set %s bits 0x%llx while mounted",
5403 disallowed = clear_mask & ~safe_clear;
5405 names = btrfs_printable_features(set, disallowed);
5408 "can't clear the %s feature bit%s while mounted",
5409 names, strchr(names, ',') ? "s" : "");
5413 "can't clear %s bits 0x%llx while mounted",
5421 #define check_feature(fs_info, change_mask, flags, mask_base) \
5422 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
5423 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5424 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5425 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5427 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5429 struct inode *inode = file_inode(file);
5430 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5431 struct btrfs_root *root = BTRFS_I(inode)->root;
5432 struct btrfs_super_block *super_block = fs_info->super_copy;
5433 struct btrfs_ioctl_feature_flags flags[2];
5434 struct btrfs_trans_handle *trans;
5438 if (!capable(CAP_SYS_ADMIN))
5441 if (copy_from_user(flags, arg, sizeof(flags)))
5445 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5446 !flags[0].incompat_flags)
5449 ret = check_feature(fs_info, flags[0].compat_flags,
5450 flags[1].compat_flags, COMPAT);
5454 ret = check_feature(fs_info, flags[0].compat_ro_flags,
5455 flags[1].compat_ro_flags, COMPAT_RO);
5459 ret = check_feature(fs_info, flags[0].incompat_flags,
5460 flags[1].incompat_flags, INCOMPAT);
5464 ret = mnt_want_write_file(file);
5468 trans = btrfs_start_transaction(root, 0);
5469 if (IS_ERR(trans)) {
5470 ret = PTR_ERR(trans);
5471 goto out_drop_write;
5474 spin_lock(&fs_info->super_lock);
5475 newflags = btrfs_super_compat_flags(super_block);
5476 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5477 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5478 btrfs_set_super_compat_flags(super_block, newflags);
5480 newflags = btrfs_super_compat_ro_flags(super_block);
5481 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5482 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5483 btrfs_set_super_compat_ro_flags(super_block, newflags);
5485 newflags = btrfs_super_incompat_flags(super_block);
5486 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5487 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5488 btrfs_set_super_incompat_flags(super_block, newflags);
5489 spin_unlock(&fs_info->super_lock);
5491 ret = btrfs_commit_transaction(trans);
5493 mnt_drop_write_file(file);
5498 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5500 struct btrfs_ioctl_send_args *arg;
5504 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5505 struct btrfs_ioctl_send_args_32 args32;
5507 ret = copy_from_user(&args32, argp, sizeof(args32));
5510 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5513 arg->send_fd = args32.send_fd;
5514 arg->clone_sources_count = args32.clone_sources_count;
5515 arg->clone_sources = compat_ptr(args32.clone_sources);
5516 arg->parent_root = args32.parent_root;
5517 arg->flags = args32.flags;
5518 memcpy(arg->reserved, args32.reserved,
5519 sizeof(args32.reserved));
5524 arg = memdup_user(argp, sizeof(*arg));
5526 return PTR_ERR(arg);
5528 ret = btrfs_ioctl_send(file, arg);
5533 long btrfs_ioctl(struct file *file, unsigned int
5534 cmd, unsigned long arg)
5536 struct inode *inode = file_inode(file);
5537 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5538 struct btrfs_root *root = BTRFS_I(inode)->root;
5539 void __user *argp = (void __user *)arg;
5542 case FS_IOC_GETFLAGS:
5543 return btrfs_ioctl_getflags(file, argp);
5544 case FS_IOC_SETFLAGS:
5545 return btrfs_ioctl_setflags(file, argp);
5546 case FS_IOC_GETVERSION:
5547 return btrfs_ioctl_getversion(file, argp);
5549 return btrfs_ioctl_fitrim(file, argp);
5550 case BTRFS_IOC_SNAP_CREATE:
5551 return btrfs_ioctl_snap_create(file, argp, 0);
5552 case BTRFS_IOC_SNAP_CREATE_V2:
5553 return btrfs_ioctl_snap_create_v2(file, argp, 0);
5554 case BTRFS_IOC_SUBVOL_CREATE:
5555 return btrfs_ioctl_snap_create(file, argp, 1);
5556 case BTRFS_IOC_SUBVOL_CREATE_V2:
5557 return btrfs_ioctl_snap_create_v2(file, argp, 1);
5558 case BTRFS_IOC_SNAP_DESTROY:
5559 return btrfs_ioctl_snap_destroy(file, argp);
5560 case BTRFS_IOC_SUBVOL_GETFLAGS:
5561 return btrfs_ioctl_subvol_getflags(file, argp);
5562 case BTRFS_IOC_SUBVOL_SETFLAGS:
5563 return btrfs_ioctl_subvol_setflags(file, argp);
5564 case BTRFS_IOC_DEFAULT_SUBVOL:
5565 return btrfs_ioctl_default_subvol(file, argp);
5566 case BTRFS_IOC_DEFRAG:
5567 return btrfs_ioctl_defrag(file, NULL);
5568 case BTRFS_IOC_DEFRAG_RANGE:
5569 return btrfs_ioctl_defrag(file, argp);
5570 case BTRFS_IOC_RESIZE:
5571 return btrfs_ioctl_resize(file, argp);
5572 case BTRFS_IOC_ADD_DEV:
5573 return btrfs_ioctl_add_dev(fs_info, argp);
5574 case BTRFS_IOC_RM_DEV:
5575 return btrfs_ioctl_rm_dev(file, argp);
5576 case BTRFS_IOC_RM_DEV_V2:
5577 return btrfs_ioctl_rm_dev_v2(file, argp);
5578 case BTRFS_IOC_FS_INFO:
5579 return btrfs_ioctl_fs_info(fs_info, argp);
5580 case BTRFS_IOC_DEV_INFO:
5581 return btrfs_ioctl_dev_info(fs_info, argp);
5582 case BTRFS_IOC_BALANCE:
5583 return btrfs_ioctl_balance(file, NULL);
5584 case BTRFS_IOC_TREE_SEARCH:
5585 return btrfs_ioctl_tree_search(file, argp);
5586 case BTRFS_IOC_TREE_SEARCH_V2:
5587 return btrfs_ioctl_tree_search_v2(file, argp);
5588 case BTRFS_IOC_INO_LOOKUP:
5589 return btrfs_ioctl_ino_lookup(file, argp);
5590 case BTRFS_IOC_INO_PATHS:
5591 return btrfs_ioctl_ino_to_path(root, argp);
5592 case BTRFS_IOC_LOGICAL_INO:
5593 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5594 case BTRFS_IOC_LOGICAL_INO_V2:
5595 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
5596 case BTRFS_IOC_SPACE_INFO:
5597 return btrfs_ioctl_space_info(fs_info, argp);
5598 case BTRFS_IOC_SYNC: {
5601 ret = btrfs_start_delalloc_roots(fs_info, -1);
5604 ret = btrfs_sync_fs(inode->i_sb, 1);
5606 * The transaction thread may want to do more work,
5607 * namely it pokes the cleaner kthread that will start
5608 * processing uncleaned subvols.
5610 wake_up_process(fs_info->transaction_kthread);
5613 case BTRFS_IOC_START_SYNC:
5614 return btrfs_ioctl_start_sync(root, argp);
5615 case BTRFS_IOC_WAIT_SYNC:
5616 return btrfs_ioctl_wait_sync(fs_info, argp);
5617 case BTRFS_IOC_SCRUB:
5618 return btrfs_ioctl_scrub(file, argp);
5619 case BTRFS_IOC_SCRUB_CANCEL:
5620 return btrfs_ioctl_scrub_cancel(fs_info);
5621 case BTRFS_IOC_SCRUB_PROGRESS:
5622 return btrfs_ioctl_scrub_progress(fs_info, argp);
5623 case BTRFS_IOC_BALANCE_V2:
5624 return btrfs_ioctl_balance(file, argp);
5625 case BTRFS_IOC_BALANCE_CTL:
5626 return btrfs_ioctl_balance_ctl(fs_info, arg);
5627 case BTRFS_IOC_BALANCE_PROGRESS:
5628 return btrfs_ioctl_balance_progress(fs_info, argp);
5629 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5630 return btrfs_ioctl_set_received_subvol(file, argp);
5632 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5633 return btrfs_ioctl_set_received_subvol_32(file, argp);
5635 case BTRFS_IOC_SEND:
5636 return _btrfs_ioctl_send(file, argp, false);
5637 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5638 case BTRFS_IOC_SEND_32:
5639 return _btrfs_ioctl_send(file, argp, true);
5641 case BTRFS_IOC_GET_DEV_STATS:
5642 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5643 case BTRFS_IOC_QUOTA_CTL:
5644 return btrfs_ioctl_quota_ctl(file, argp);
5645 case BTRFS_IOC_QGROUP_ASSIGN:
5646 return btrfs_ioctl_qgroup_assign(file, argp);
5647 case BTRFS_IOC_QGROUP_CREATE:
5648 return btrfs_ioctl_qgroup_create(file, argp);
5649 case BTRFS_IOC_QGROUP_LIMIT:
5650 return btrfs_ioctl_qgroup_limit(file, argp);
5651 case BTRFS_IOC_QUOTA_RESCAN:
5652 return btrfs_ioctl_quota_rescan(file, argp);
5653 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5654 return btrfs_ioctl_quota_rescan_status(file, argp);
5655 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5656 return btrfs_ioctl_quota_rescan_wait(file, argp);
5657 case BTRFS_IOC_DEV_REPLACE:
5658 return btrfs_ioctl_dev_replace(fs_info, argp);
5659 case BTRFS_IOC_GET_FSLABEL:
5660 return btrfs_ioctl_get_fslabel(file, argp);
5661 case BTRFS_IOC_SET_FSLABEL:
5662 return btrfs_ioctl_set_fslabel(file, argp);
5663 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5664 return btrfs_ioctl_get_supported_features(argp);
5665 case BTRFS_IOC_GET_FEATURES:
5666 return btrfs_ioctl_get_features(file, argp);
5667 case BTRFS_IOC_SET_FEATURES:
5668 return btrfs_ioctl_set_features(file, argp);
5669 case FS_IOC_FSGETXATTR:
5670 return btrfs_ioctl_fsgetxattr(file, argp);
5671 case FS_IOC_FSSETXATTR:
5672 return btrfs_ioctl_fssetxattr(file, argp);
5673 case BTRFS_IOC_GET_SUBVOL_INFO:
5674 return btrfs_ioctl_get_subvol_info(file, argp);
5675 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5676 return btrfs_ioctl_get_subvol_rootref(file, argp);
5677 case BTRFS_IOC_INO_LOOKUP_USER:
5678 return btrfs_ioctl_ino_lookup_user(file, argp);
5684 #ifdef CONFIG_COMPAT
5685 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5688 * These all access 32-bit values anyway so no further
5689 * handling is necessary.
5692 case FS_IOC32_GETFLAGS:
5693 cmd = FS_IOC_GETFLAGS;
5695 case FS_IOC32_SETFLAGS:
5696 cmd = FS_IOC_SETFLAGS;
5698 case FS_IOC32_GETVERSION:
5699 cmd = FS_IOC_GETVERSION;
5703 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));