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, old_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 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
216 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
220 if (fsflags & FS_SYNC_FL)
221 binode_flags |= BTRFS_INODE_SYNC;
223 binode_flags &= ~BTRFS_INODE_SYNC;
224 if (fsflags & FS_IMMUTABLE_FL)
225 binode_flags |= BTRFS_INODE_IMMUTABLE;
227 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
228 if (fsflags & FS_APPEND_FL)
229 binode_flags |= BTRFS_INODE_APPEND;
231 binode_flags &= ~BTRFS_INODE_APPEND;
232 if (fsflags & FS_NODUMP_FL)
233 binode_flags |= BTRFS_INODE_NODUMP;
235 binode_flags &= ~BTRFS_INODE_NODUMP;
236 if (fsflags & FS_NOATIME_FL)
237 binode_flags |= BTRFS_INODE_NOATIME;
239 binode_flags &= ~BTRFS_INODE_NOATIME;
240 if (fsflags & FS_DIRSYNC_FL)
241 binode_flags |= BTRFS_INODE_DIRSYNC;
243 binode_flags &= ~BTRFS_INODE_DIRSYNC;
244 if (fsflags & FS_NOCOW_FL) {
245 if (S_ISREG(inode->i_mode)) {
247 * It's safe to turn csums off here, no extents exist.
248 * Otherwise we want the flag to reflect the real COW
249 * status of the file and will not set it.
251 if (inode->i_size == 0)
252 binode_flags |= BTRFS_INODE_NODATACOW |
253 BTRFS_INODE_NODATASUM;
255 binode_flags |= BTRFS_INODE_NODATACOW;
259 * Revert back under same assumptions as above
261 if (S_ISREG(inode->i_mode)) {
262 if (inode->i_size == 0)
263 binode_flags &= ~(BTRFS_INODE_NODATACOW |
264 BTRFS_INODE_NODATASUM);
266 binode_flags &= ~BTRFS_INODE_NODATACOW;
271 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
272 * flag may be changed automatically if compression code won't make
275 if (fsflags & FS_NOCOMP_FL) {
276 binode_flags &= ~BTRFS_INODE_COMPRESS;
277 binode_flags |= BTRFS_INODE_NOCOMPRESS;
278 } else if (fsflags & FS_COMPR_FL) {
280 if (IS_SWAPFILE(inode)) {
285 binode_flags |= BTRFS_INODE_COMPRESS;
286 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
288 comp = btrfs_compress_type2str(fs_info->compress_type);
289 if (!comp || comp[0] == 0)
290 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
292 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
299 trans = btrfs_start_transaction(root, 3);
301 ret = PTR_ERR(trans);
306 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
309 btrfs_abort_transaction(trans, ret);
313 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
315 if (ret && ret != -ENODATA) {
316 btrfs_abort_transaction(trans, ret);
321 binode->flags = binode_flags;
322 btrfs_sync_inode_flags_to_i_flags(inode);
323 inode_inc_iversion(inode);
324 inode->i_ctime = current_time(inode);
325 ret = btrfs_update_inode(trans, root, inode);
328 btrfs_end_transaction(trans);
331 mnt_drop_write_file(file);
336 * Translate btrfs internal inode flags to xflags as expected by the
337 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
340 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
342 unsigned int xflags = 0;
344 if (flags & BTRFS_INODE_APPEND)
345 xflags |= FS_XFLAG_APPEND;
346 if (flags & BTRFS_INODE_IMMUTABLE)
347 xflags |= FS_XFLAG_IMMUTABLE;
348 if (flags & BTRFS_INODE_NOATIME)
349 xflags |= FS_XFLAG_NOATIME;
350 if (flags & BTRFS_INODE_NODUMP)
351 xflags |= FS_XFLAG_NODUMP;
352 if (flags & BTRFS_INODE_SYNC)
353 xflags |= FS_XFLAG_SYNC;
358 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
359 static int check_xflags(unsigned int flags)
361 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
362 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
368 * Set the xflags from the internal inode flags. The remaining items of fsxattr
371 static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
373 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
376 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
377 if (copy_to_user(arg, &fa, sizeof(fa)))
383 static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
385 struct inode *inode = file_inode(file);
386 struct btrfs_inode *binode = BTRFS_I(inode);
387 struct btrfs_root *root = binode->root;
388 struct btrfs_trans_handle *trans;
389 struct fsxattr fa, old_fa;
391 unsigned old_i_flags;
394 if (!inode_owner_or_capable(inode))
397 if (btrfs_root_readonly(root))
400 if (copy_from_user(&fa, arg, sizeof(fa)))
403 ret = check_xflags(fa.fsx_xflags);
407 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
410 ret = mnt_want_write_file(file);
416 old_flags = binode->flags;
417 old_i_flags = inode->i_flags;
419 simple_fill_fsxattr(&old_fa,
420 btrfs_inode_flags_to_xflags(binode->flags));
421 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
425 if (fa.fsx_xflags & FS_XFLAG_SYNC)
426 binode->flags |= BTRFS_INODE_SYNC;
428 binode->flags &= ~BTRFS_INODE_SYNC;
429 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
430 binode->flags |= BTRFS_INODE_IMMUTABLE;
432 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
433 if (fa.fsx_xflags & FS_XFLAG_APPEND)
434 binode->flags |= BTRFS_INODE_APPEND;
436 binode->flags &= ~BTRFS_INODE_APPEND;
437 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
438 binode->flags |= BTRFS_INODE_NODUMP;
440 binode->flags &= ~BTRFS_INODE_NODUMP;
441 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
442 binode->flags |= BTRFS_INODE_NOATIME;
444 binode->flags &= ~BTRFS_INODE_NOATIME;
446 /* 1 item for the inode */
447 trans = btrfs_start_transaction(root, 1);
449 ret = PTR_ERR(trans);
453 btrfs_sync_inode_flags_to_i_flags(inode);
454 inode_inc_iversion(inode);
455 inode->i_ctime = current_time(inode);
456 ret = btrfs_update_inode(trans, root, inode);
458 btrfs_end_transaction(trans);
462 binode->flags = old_flags;
463 inode->i_flags = old_i_flags;
467 mnt_drop_write_file(file);
472 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
474 struct inode *inode = file_inode(file);
476 return put_user(inode->i_generation, arg);
479 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
481 struct inode *inode = file_inode(file);
482 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
483 struct btrfs_device *device;
484 struct request_queue *q;
485 struct fstrim_range range;
486 u64 minlen = ULLONG_MAX;
490 if (!capable(CAP_SYS_ADMIN))
494 * If the fs is mounted with nologreplay, which requires it to be
495 * mounted in RO mode as well, we can not allow discard on free space
496 * inside block groups, because log trees refer to extents that are not
497 * pinned in a block group's free space cache (pinning the extents is
498 * precisely the first phase of replaying a log tree).
500 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
504 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
508 q = bdev_get_queue(device->bdev);
509 if (blk_queue_discard(q)) {
511 minlen = min_t(u64, q->limits.discard_granularity,
519 if (copy_from_user(&range, arg, sizeof(range)))
523 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
524 * block group is in the logical address space, which can be any
525 * sectorsize aligned bytenr in the range [0, U64_MAX].
527 if (range.len < fs_info->sb->s_blocksize)
530 range.minlen = max(range.minlen, minlen);
531 ret = btrfs_trim_fs(fs_info, &range);
535 if (copy_to_user(arg, &range, sizeof(range)))
541 int btrfs_is_empty_uuid(u8 *uuid)
545 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
552 static noinline int create_subvol(struct inode *dir,
553 struct dentry *dentry,
554 const char *name, int namelen,
556 struct btrfs_qgroup_inherit *inherit)
558 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
559 struct btrfs_trans_handle *trans;
560 struct btrfs_key key;
561 struct btrfs_root_item *root_item;
562 struct btrfs_inode_item *inode_item;
563 struct extent_buffer *leaf;
564 struct btrfs_root *root = BTRFS_I(dir)->root;
565 struct btrfs_root *new_root;
566 struct btrfs_block_rsv block_rsv;
567 struct timespec64 cur_time = current_time(dir);
572 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
576 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
580 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
585 * Don't create subvolume whose level is not zero. Or qgroup will be
586 * screwed up since it assumes subvolume qgroup's level to be 0.
588 if (btrfs_qgroup_level(objectid)) {
593 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
595 * The same as the snapshot creation, please see the comment
596 * of create_snapshot().
598 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
602 trans = btrfs_start_transaction(root, 0);
604 ret = PTR_ERR(trans);
605 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
608 trans->block_rsv = &block_rsv;
609 trans->bytes_reserved = block_rsv.size;
611 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
615 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
621 btrfs_mark_buffer_dirty(leaf);
623 inode_item = &root_item->inode;
624 btrfs_set_stack_inode_generation(inode_item, 1);
625 btrfs_set_stack_inode_size(inode_item, 3);
626 btrfs_set_stack_inode_nlink(inode_item, 1);
627 btrfs_set_stack_inode_nbytes(inode_item,
629 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
631 btrfs_set_root_flags(root_item, 0);
632 btrfs_set_root_limit(root_item, 0);
633 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
635 btrfs_set_root_bytenr(root_item, leaf->start);
636 btrfs_set_root_generation(root_item, trans->transid);
637 btrfs_set_root_level(root_item, 0);
638 btrfs_set_root_refs(root_item, 1);
639 btrfs_set_root_used(root_item, leaf->len);
640 btrfs_set_root_last_snapshot(root_item, 0);
642 btrfs_set_root_generation_v2(root_item,
643 btrfs_root_generation(root_item));
644 uuid_le_gen(&new_uuid);
645 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
646 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
647 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
648 root_item->ctime = root_item->otime;
649 btrfs_set_root_ctransid(root_item, trans->transid);
650 btrfs_set_root_otransid(root_item, trans->transid);
652 btrfs_tree_unlock(leaf);
653 free_extent_buffer(leaf);
656 btrfs_set_root_dirid(root_item, new_dirid);
658 key.objectid = objectid;
660 key.type = BTRFS_ROOT_ITEM_KEY;
661 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
666 key.offset = (u64)-1;
667 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
668 if (IS_ERR(new_root)) {
669 ret = PTR_ERR(new_root);
670 btrfs_abort_transaction(trans, ret);
674 btrfs_record_root_in_trans(trans, new_root);
676 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
678 /* We potentially lose an unused inode item here */
679 btrfs_abort_transaction(trans, ret);
683 mutex_lock(&new_root->objectid_mutex);
684 new_root->highest_objectid = new_dirid;
685 mutex_unlock(&new_root->objectid_mutex);
688 * insert the directory item
690 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
692 btrfs_abort_transaction(trans, ret);
696 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
697 BTRFS_FT_DIR, index);
699 btrfs_abort_transaction(trans, ret);
703 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
704 ret = btrfs_update_inode(trans, root, dir);
707 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
708 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
711 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
712 BTRFS_UUID_KEY_SUBVOL, objectid);
714 btrfs_abort_transaction(trans, ret);
718 trans->block_rsv = NULL;
719 trans->bytes_reserved = 0;
720 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
723 *async_transid = trans->transid;
724 err = btrfs_commit_transaction_async(trans, 1);
726 err = btrfs_commit_transaction(trans);
728 err = btrfs_commit_transaction(trans);
734 inode = btrfs_lookup_dentry(dir, dentry);
736 return PTR_ERR(inode);
737 d_instantiate(dentry, inode);
746 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
747 struct dentry *dentry,
748 u64 *async_transid, bool readonly,
749 struct btrfs_qgroup_inherit *inherit)
751 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
753 struct btrfs_pending_snapshot *pending_snapshot;
754 struct btrfs_trans_handle *trans;
756 bool snapshot_force_cow = false;
758 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
761 if (atomic_read(&root->nr_swapfiles)) {
763 "cannot snapshot subvolume with active swapfile");
767 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
768 if (!pending_snapshot)
771 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
773 pending_snapshot->path = btrfs_alloc_path();
774 if (!pending_snapshot->root_item || !pending_snapshot->path) {
780 * Force new buffered writes to reserve space even when NOCOW is
781 * possible. This is to avoid later writeback (running dealloc) to
782 * fallback to COW mode and unexpectedly fail with ENOSPC.
784 atomic_inc(&root->will_be_snapshotted);
785 smp_mb__after_atomic();
786 /* wait for no snapshot writes */
787 wait_event(root->subv_writers->wait,
788 percpu_counter_sum(&root->subv_writers->counter) == 0);
790 ret = btrfs_start_delalloc_snapshot(root);
795 * All previous writes have started writeback in NOCOW mode, so now
796 * we force future writes to fallback to COW mode during snapshot
799 atomic_inc(&root->snapshot_force_cow);
800 snapshot_force_cow = true;
802 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
804 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
805 BTRFS_BLOCK_RSV_TEMP);
807 * 1 - parent dir inode
810 * 2 - root ref/backref
811 * 1 - root of snapshot
814 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
815 &pending_snapshot->block_rsv, 8,
820 pending_snapshot->dentry = dentry;
821 pending_snapshot->root = root;
822 pending_snapshot->readonly = readonly;
823 pending_snapshot->dir = dir;
824 pending_snapshot->inherit = inherit;
826 trans = btrfs_start_transaction(root, 0);
828 ret = PTR_ERR(trans);
832 spin_lock(&fs_info->trans_lock);
833 list_add(&pending_snapshot->list,
834 &trans->transaction->pending_snapshots);
835 spin_unlock(&fs_info->trans_lock);
837 *async_transid = trans->transid;
838 ret = btrfs_commit_transaction_async(trans, 1);
840 ret = btrfs_commit_transaction(trans);
842 ret = btrfs_commit_transaction(trans);
847 ret = pending_snapshot->error;
851 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
855 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
857 ret = PTR_ERR(inode);
861 d_instantiate(dentry, inode);
864 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
866 if (snapshot_force_cow)
867 atomic_dec(&root->snapshot_force_cow);
868 if (atomic_dec_and_test(&root->will_be_snapshotted))
869 wake_up_var(&root->will_be_snapshotted);
871 kfree(pending_snapshot->root_item);
872 btrfs_free_path(pending_snapshot->path);
873 kfree(pending_snapshot);
878 /* copy of may_delete in fs/namei.c()
879 * Check whether we can remove a link victim from directory dir, check
880 * whether the type of victim is right.
881 * 1. We can't do it if dir is read-only (done in permission())
882 * 2. We should have write and exec permissions on dir
883 * 3. We can't remove anything from append-only dir
884 * 4. We can't do anything with immutable dir (done in permission())
885 * 5. If the sticky bit on dir is set we should either
886 * a. be owner of dir, or
887 * b. be owner of victim, or
888 * c. have CAP_FOWNER capability
889 * 6. If the victim is append-only or immutable we can't do anything with
890 * links pointing to it.
891 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
892 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
893 * 9. We can't remove a root or mountpoint.
894 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
895 * nfs_async_unlink().
898 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
902 if (d_really_is_negative(victim))
905 BUG_ON(d_inode(victim->d_parent) != dir);
906 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
908 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
913 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
914 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
917 if (!d_is_dir(victim))
921 } else if (d_is_dir(victim))
925 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
930 /* copy of may_create in fs/namei.c() */
931 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
933 if (d_really_is_positive(child))
937 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
941 * Create a new subvolume below @parent. This is largely modeled after
942 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
943 * inside this filesystem so it's quite a bit simpler.
945 static noinline int btrfs_mksubvol(const struct path *parent,
946 const char *name, int namelen,
947 struct btrfs_root *snap_src,
948 u64 *async_transid, bool readonly,
949 struct btrfs_qgroup_inherit *inherit)
951 struct inode *dir = d_inode(parent->dentry);
952 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
953 struct dentry *dentry;
956 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
960 dentry = lookup_one_len(name, parent->dentry, namelen);
961 error = PTR_ERR(dentry);
965 error = btrfs_may_create(dir, dentry);
970 * even if this name doesn't exist, we may get hash collisions.
971 * check for them now when we can safely fail
973 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
979 down_read(&fs_info->subvol_sem);
981 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
985 error = create_snapshot(snap_src, dir, dentry,
986 async_transid, readonly, inherit);
988 error = create_subvol(dir, dentry, name, namelen,
989 async_transid, inherit);
992 fsnotify_mkdir(dir, dentry);
994 up_read(&fs_info->subvol_sem);
1003 * When we're defragging a range, we don't want to kick it off again
1004 * if it is really just waiting for delalloc to send it down.
1005 * If we find a nice big extent or delalloc range for the bytes in the
1006 * file you want to defrag, we return 0 to let you know to skip this
1009 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1011 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1012 struct extent_map *em = NULL;
1013 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1016 read_lock(&em_tree->lock);
1017 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1018 read_unlock(&em_tree->lock);
1021 end = extent_map_end(em);
1022 free_extent_map(em);
1023 if (end - offset > thresh)
1026 /* if we already have a nice delalloc here, just stop */
1028 end = count_range_bits(io_tree, &offset, offset + thresh,
1029 thresh, EXTENT_DELALLOC, 1);
1036 * helper function to walk through a file and find extents
1037 * newer than a specific transid, and smaller than thresh.
1039 * This is used by the defragging code to find new and small
1042 static int find_new_extents(struct btrfs_root *root,
1043 struct inode *inode, u64 newer_than,
1044 u64 *off, u32 thresh)
1046 struct btrfs_path *path;
1047 struct btrfs_key min_key;
1048 struct extent_buffer *leaf;
1049 struct btrfs_file_extent_item *extent;
1052 u64 ino = btrfs_ino(BTRFS_I(inode));
1054 path = btrfs_alloc_path();
1058 min_key.objectid = ino;
1059 min_key.type = BTRFS_EXTENT_DATA_KEY;
1060 min_key.offset = *off;
1063 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1067 if (min_key.objectid != ino)
1069 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1072 leaf = path->nodes[0];
1073 extent = btrfs_item_ptr(leaf, path->slots[0],
1074 struct btrfs_file_extent_item);
1076 type = btrfs_file_extent_type(leaf, extent);
1077 if (type == BTRFS_FILE_EXTENT_REG &&
1078 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1079 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1080 *off = min_key.offset;
1081 btrfs_free_path(path);
1086 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1087 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1091 if (min_key.offset == (u64)-1)
1095 btrfs_release_path(path);
1098 btrfs_free_path(path);
1102 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1104 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1105 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1106 struct extent_map *em;
1107 u64 len = PAGE_SIZE;
1110 * hopefully we have this extent in the tree already, try without
1111 * the full extent lock
1113 read_lock(&em_tree->lock);
1114 em = lookup_extent_mapping(em_tree, start, len);
1115 read_unlock(&em_tree->lock);
1118 struct extent_state *cached = NULL;
1119 u64 end = start + len - 1;
1121 /* get the big lock and read metadata off disk */
1122 lock_extent_bits(io_tree, start, end, &cached);
1123 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
1124 unlock_extent_cached(io_tree, start, end, &cached);
1133 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1135 struct extent_map *next;
1138 /* this is the last extent */
1139 if (em->start + em->len >= i_size_read(inode))
1142 next = defrag_lookup_extent(inode, em->start + em->len);
1143 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1145 else if ((em->block_start + em->block_len == next->block_start) &&
1146 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1149 free_extent_map(next);
1153 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1154 u64 *last_len, u64 *skip, u64 *defrag_end,
1157 struct extent_map *em;
1159 bool next_mergeable = true;
1160 bool prev_mergeable = true;
1163 * make sure that once we start defragging an extent, we keep on
1166 if (start < *defrag_end)
1171 em = defrag_lookup_extent(inode, start);
1175 /* this will cover holes, and inline extents */
1176 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1182 prev_mergeable = false;
1184 next_mergeable = defrag_check_next_extent(inode, em);
1186 * we hit a real extent, if it is big or the next extent is not a
1187 * real extent, don't bother defragging it
1189 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1190 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1194 * last_len ends up being a counter of how many bytes we've defragged.
1195 * every time we choose not to defrag an extent, we reset *last_len
1196 * so that the next tiny extent will force a defrag.
1198 * The end result of this is that tiny extents before a single big
1199 * extent will force at least part of that big extent to be defragged.
1202 *defrag_end = extent_map_end(em);
1205 *skip = extent_map_end(em);
1209 free_extent_map(em);
1214 * it doesn't do much good to defrag one or two pages
1215 * at a time. This pulls in a nice chunk of pages
1216 * to COW and defrag.
1218 * It also makes sure the delalloc code has enough
1219 * dirty data to avoid making new small extents as part
1222 * It's a good idea to start RA on this range
1223 * before calling this.
1225 static int cluster_pages_for_defrag(struct inode *inode,
1226 struct page **pages,
1227 unsigned long start_index,
1228 unsigned long num_pages)
1230 unsigned long file_end;
1231 u64 isize = i_size_read(inode);
1238 struct btrfs_ordered_extent *ordered;
1239 struct extent_state *cached_state = NULL;
1240 struct extent_io_tree *tree;
1241 struct extent_changeset *data_reserved = NULL;
1242 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1244 file_end = (isize - 1) >> PAGE_SHIFT;
1245 if (!isize || start_index > file_end)
1248 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1250 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
1251 start_index << PAGE_SHIFT,
1252 page_cnt << PAGE_SHIFT);
1256 tree = &BTRFS_I(inode)->io_tree;
1258 /* step one, lock all the pages */
1259 for (i = 0; i < page_cnt; i++) {
1262 page = find_or_create_page(inode->i_mapping,
1263 start_index + i, mask);
1267 page_start = page_offset(page);
1268 page_end = page_start + PAGE_SIZE - 1;
1270 lock_extent_bits(tree, page_start, page_end,
1272 ordered = btrfs_lookup_ordered_extent(inode,
1274 unlock_extent_cached(tree, page_start, page_end,
1280 btrfs_start_ordered_extent(inode, ordered, 1);
1281 btrfs_put_ordered_extent(ordered);
1284 * we unlocked the page above, so we need check if
1285 * it was released or not.
1287 if (page->mapping != inode->i_mapping) {
1294 if (!PageUptodate(page)) {
1295 btrfs_readpage(NULL, page);
1297 if (!PageUptodate(page)) {
1305 if (page->mapping != inode->i_mapping) {
1317 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1321 * so now we have a nice long stream of locked
1322 * and up to date pages, lets wait on them
1324 for (i = 0; i < i_done; i++)
1325 wait_on_page_writeback(pages[i]);
1327 page_start = page_offset(pages[0]);
1328 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1330 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1331 page_start, page_end - 1, &cached_state);
1332 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1333 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1334 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1337 if (i_done != page_cnt) {
1338 spin_lock(&BTRFS_I(inode)->lock);
1339 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1340 spin_unlock(&BTRFS_I(inode)->lock);
1341 btrfs_delalloc_release_space(inode, data_reserved,
1342 start_index << PAGE_SHIFT,
1343 (page_cnt - i_done) << PAGE_SHIFT, true);
1347 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1350 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1351 page_start, page_end - 1, &cached_state);
1353 for (i = 0; i < i_done; i++) {
1354 clear_page_dirty_for_io(pages[i]);
1355 ClearPageChecked(pages[i]);
1356 set_page_extent_mapped(pages[i]);
1357 set_page_dirty(pages[i]);
1358 unlock_page(pages[i]);
1361 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1363 extent_changeset_free(data_reserved);
1366 for (i = 0; i < i_done; i++) {
1367 unlock_page(pages[i]);
1370 btrfs_delalloc_release_space(inode, data_reserved,
1371 start_index << PAGE_SHIFT,
1372 page_cnt << PAGE_SHIFT, true);
1373 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1375 extent_changeset_free(data_reserved);
1380 int btrfs_defrag_file(struct inode *inode, struct file *file,
1381 struct btrfs_ioctl_defrag_range_args *range,
1382 u64 newer_than, unsigned long max_to_defrag)
1384 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1385 struct btrfs_root *root = BTRFS_I(inode)->root;
1386 struct file_ra_state *ra = NULL;
1387 unsigned long last_index;
1388 u64 isize = i_size_read(inode);
1392 u64 newer_off = range->start;
1394 unsigned long ra_index = 0;
1396 int defrag_count = 0;
1397 int compress_type = BTRFS_COMPRESS_ZLIB;
1398 u32 extent_thresh = range->extent_thresh;
1399 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1400 unsigned long cluster = max_cluster;
1401 u64 new_align = ~((u64)SZ_128K - 1);
1402 struct page **pages = NULL;
1403 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1408 if (range->start >= isize)
1412 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1414 if (range->compress_type)
1415 compress_type = range->compress_type;
1418 if (extent_thresh == 0)
1419 extent_thresh = SZ_256K;
1422 * If we were not given a file, allocate a readahead context. As
1423 * readahead is just an optimization, defrag will work without it so
1424 * we don't error out.
1427 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1429 file_ra_state_init(ra, inode->i_mapping);
1434 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1440 /* find the last page to defrag */
1441 if (range->start + range->len > range->start) {
1442 last_index = min_t(u64, isize - 1,
1443 range->start + range->len - 1) >> PAGE_SHIFT;
1445 last_index = (isize - 1) >> PAGE_SHIFT;
1449 ret = find_new_extents(root, inode, newer_than,
1450 &newer_off, SZ_64K);
1452 range->start = newer_off;
1454 * we always align our defrag to help keep
1455 * the extents in the file evenly spaced
1457 i = (newer_off & new_align) >> PAGE_SHIFT;
1461 i = range->start >> PAGE_SHIFT;
1464 max_to_defrag = last_index - i + 1;
1467 * make writeback starts from i, so the defrag range can be
1468 * written sequentially.
1470 if (i < inode->i_mapping->writeback_index)
1471 inode->i_mapping->writeback_index = i;
1473 while (i <= last_index && defrag_count < max_to_defrag &&
1474 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1476 * make sure we stop running if someone unmounts
1479 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1482 if (btrfs_defrag_cancelled(fs_info)) {
1483 btrfs_debug(fs_info, "defrag_file cancelled");
1488 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1489 extent_thresh, &last_len, &skip,
1490 &defrag_end, do_compress)){
1493 * the should_defrag function tells us how much to skip
1494 * bump our counter by the suggested amount
1496 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1497 i = max(i + 1, next);
1502 cluster = (PAGE_ALIGN(defrag_end) >>
1504 cluster = min(cluster, max_cluster);
1506 cluster = max_cluster;
1509 if (i + cluster > ra_index) {
1510 ra_index = max(i, ra_index);
1512 page_cache_sync_readahead(inode->i_mapping, ra,
1513 file, ra_index, cluster);
1514 ra_index += cluster;
1518 if (IS_SWAPFILE(inode)) {
1522 BTRFS_I(inode)->defrag_compress = compress_type;
1523 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1526 inode_unlock(inode);
1530 defrag_count += ret;
1531 balance_dirty_pages_ratelimited(inode->i_mapping);
1532 inode_unlock(inode);
1535 if (newer_off == (u64)-1)
1541 newer_off = max(newer_off + 1,
1542 (u64)i << PAGE_SHIFT);
1544 ret = find_new_extents(root, inode, newer_than,
1545 &newer_off, SZ_64K);
1547 range->start = newer_off;
1548 i = (newer_off & new_align) >> PAGE_SHIFT;
1555 last_len += ret << PAGE_SHIFT;
1563 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1564 filemap_flush(inode->i_mapping);
1565 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1566 &BTRFS_I(inode)->runtime_flags))
1567 filemap_flush(inode->i_mapping);
1570 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1571 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1572 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1573 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1581 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1582 inode_unlock(inode);
1590 static noinline int btrfs_ioctl_resize(struct file *file,
1593 struct inode *inode = file_inode(file);
1594 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1598 struct btrfs_root *root = BTRFS_I(inode)->root;
1599 struct btrfs_ioctl_vol_args *vol_args;
1600 struct btrfs_trans_handle *trans;
1601 struct btrfs_device *device = NULL;
1604 char *devstr = NULL;
1608 if (!capable(CAP_SYS_ADMIN))
1611 ret = mnt_want_write_file(file);
1615 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
1616 mnt_drop_write_file(file);
1617 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1620 vol_args = memdup_user(arg, sizeof(*vol_args));
1621 if (IS_ERR(vol_args)) {
1622 ret = PTR_ERR(vol_args);
1626 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1628 sizestr = vol_args->name;
1629 devstr = strchr(sizestr, ':');
1631 sizestr = devstr + 1;
1633 devstr = vol_args->name;
1634 ret = kstrtoull(devstr, 10, &devid);
1641 btrfs_info(fs_info, "resizing devid %llu", devid);
1644 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
1646 btrfs_info(fs_info, "resizer unable to find device %llu",
1652 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1654 "resizer unable to apply on readonly device %llu",
1660 if (!strcmp(sizestr, "max"))
1661 new_size = device->bdev->bd_inode->i_size;
1663 if (sizestr[0] == '-') {
1666 } else if (sizestr[0] == '+') {
1670 new_size = memparse(sizestr, &retptr);
1671 if (*retptr != '\0' || new_size == 0) {
1677 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1682 old_size = btrfs_device_get_total_bytes(device);
1685 if (new_size > old_size) {
1689 new_size = old_size - new_size;
1690 } else if (mod > 0) {
1691 if (new_size > ULLONG_MAX - old_size) {
1695 new_size = old_size + new_size;
1698 if (new_size < SZ_256M) {
1702 if (new_size > device->bdev->bd_inode->i_size) {
1707 new_size = round_down(new_size, fs_info->sectorsize);
1709 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1710 rcu_str_deref(device->name), new_size);
1712 if (new_size > old_size) {
1713 trans = btrfs_start_transaction(root, 0);
1714 if (IS_ERR(trans)) {
1715 ret = PTR_ERR(trans);
1718 ret = btrfs_grow_device(trans, device, new_size);
1719 btrfs_commit_transaction(trans);
1720 } else if (new_size < old_size) {
1721 ret = btrfs_shrink_device(device, new_size);
1722 } /* equal, nothing need to do */
1727 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
1728 mnt_drop_write_file(file);
1732 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1733 const char *name, unsigned long fd, int subvol,
1734 u64 *transid, bool readonly,
1735 struct btrfs_qgroup_inherit *inherit)
1740 if (!S_ISDIR(file_inode(file)->i_mode))
1743 ret = mnt_want_write_file(file);
1747 namelen = strlen(name);
1748 if (strchr(name, '/')) {
1750 goto out_drop_write;
1753 if (name[0] == '.' &&
1754 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1756 goto out_drop_write;
1760 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1761 NULL, transid, readonly, inherit);
1763 struct fd src = fdget(fd);
1764 struct inode *src_inode;
1767 goto out_drop_write;
1770 src_inode = file_inode(src.file);
1771 if (src_inode->i_sb != file_inode(file)->i_sb) {
1772 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1773 "Snapshot src from another FS");
1775 } else if (!inode_owner_or_capable(src_inode)) {
1777 * Subvolume creation is not restricted, but snapshots
1778 * are limited to own subvolumes only
1782 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1783 BTRFS_I(src_inode)->root,
1784 transid, readonly, inherit);
1789 mnt_drop_write_file(file);
1794 static noinline int btrfs_ioctl_snap_create(struct file *file,
1795 void __user *arg, int subvol)
1797 struct btrfs_ioctl_vol_args *vol_args;
1800 if (!S_ISDIR(file_inode(file)->i_mode))
1803 vol_args = memdup_user(arg, sizeof(*vol_args));
1804 if (IS_ERR(vol_args))
1805 return PTR_ERR(vol_args);
1806 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1808 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1809 vol_args->fd, subvol,
1816 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1817 void __user *arg, int subvol)
1819 struct btrfs_ioctl_vol_args_v2 *vol_args;
1823 bool readonly = false;
1824 struct btrfs_qgroup_inherit *inherit = NULL;
1826 if (!S_ISDIR(file_inode(file)->i_mode))
1829 vol_args = memdup_user(arg, sizeof(*vol_args));
1830 if (IS_ERR(vol_args))
1831 return PTR_ERR(vol_args);
1832 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1834 if (vol_args->flags &
1835 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1836 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1841 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1843 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1845 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1846 if (vol_args->size > PAGE_SIZE) {
1850 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1851 if (IS_ERR(inherit)) {
1852 ret = PTR_ERR(inherit);
1857 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1858 vol_args->fd, subvol, ptr,
1863 if (ptr && copy_to_user(arg +
1864 offsetof(struct btrfs_ioctl_vol_args_v2,
1876 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1879 struct inode *inode = file_inode(file);
1880 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1881 struct btrfs_root *root = BTRFS_I(inode)->root;
1885 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1888 down_read(&fs_info->subvol_sem);
1889 if (btrfs_root_readonly(root))
1890 flags |= BTRFS_SUBVOL_RDONLY;
1891 up_read(&fs_info->subvol_sem);
1893 if (copy_to_user(arg, &flags, sizeof(flags)))
1899 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1902 struct inode *inode = file_inode(file);
1903 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1904 struct btrfs_root *root = BTRFS_I(inode)->root;
1905 struct btrfs_trans_handle *trans;
1910 if (!inode_owner_or_capable(inode))
1913 ret = mnt_want_write_file(file);
1917 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1919 goto out_drop_write;
1922 if (copy_from_user(&flags, arg, sizeof(flags))) {
1924 goto out_drop_write;
1927 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1929 goto out_drop_write;
1932 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1934 goto out_drop_write;
1937 down_write(&fs_info->subvol_sem);
1940 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1943 root_flags = btrfs_root_flags(&root->root_item);
1944 if (flags & BTRFS_SUBVOL_RDONLY) {
1945 btrfs_set_root_flags(&root->root_item,
1946 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1949 * Block RO -> RW transition if this subvolume is involved in
1952 spin_lock(&root->root_item_lock);
1953 if (root->send_in_progress == 0) {
1954 btrfs_set_root_flags(&root->root_item,
1955 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1956 spin_unlock(&root->root_item_lock);
1958 spin_unlock(&root->root_item_lock);
1960 "Attempt to set subvolume %llu read-write during send",
1961 root->root_key.objectid);
1967 trans = btrfs_start_transaction(root, 1);
1968 if (IS_ERR(trans)) {
1969 ret = PTR_ERR(trans);
1973 ret = btrfs_update_root(trans, fs_info->tree_root,
1974 &root->root_key, &root->root_item);
1976 btrfs_end_transaction(trans);
1980 ret = btrfs_commit_transaction(trans);
1984 btrfs_set_root_flags(&root->root_item, root_flags);
1986 up_write(&fs_info->subvol_sem);
1988 mnt_drop_write_file(file);
1993 static noinline int key_in_sk(struct btrfs_key *key,
1994 struct btrfs_ioctl_search_key *sk)
1996 struct btrfs_key test;
1999 test.objectid = sk->min_objectid;
2000 test.type = sk->min_type;
2001 test.offset = sk->min_offset;
2003 ret = btrfs_comp_cpu_keys(key, &test);
2007 test.objectid = sk->max_objectid;
2008 test.type = sk->max_type;
2009 test.offset = sk->max_offset;
2011 ret = btrfs_comp_cpu_keys(key, &test);
2017 static noinline int copy_to_sk(struct btrfs_path *path,
2018 struct btrfs_key *key,
2019 struct btrfs_ioctl_search_key *sk,
2022 unsigned long *sk_offset,
2026 struct extent_buffer *leaf;
2027 struct btrfs_ioctl_search_header sh;
2028 struct btrfs_key test;
2029 unsigned long item_off;
2030 unsigned long item_len;
2036 leaf = path->nodes[0];
2037 slot = path->slots[0];
2038 nritems = btrfs_header_nritems(leaf);
2040 if (btrfs_header_generation(leaf) > sk->max_transid) {
2044 found_transid = btrfs_header_generation(leaf);
2046 for (i = slot; i < nritems; i++) {
2047 item_off = btrfs_item_ptr_offset(leaf, i);
2048 item_len = btrfs_item_size_nr(leaf, i);
2050 btrfs_item_key_to_cpu(leaf, key, i);
2051 if (!key_in_sk(key, sk))
2054 if (sizeof(sh) + item_len > *buf_size) {
2061 * return one empty item back for v1, which does not
2065 *buf_size = sizeof(sh) + item_len;
2070 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2075 sh.objectid = key->objectid;
2076 sh.offset = key->offset;
2077 sh.type = key->type;
2079 sh.transid = found_transid;
2081 /* copy search result header */
2082 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2087 *sk_offset += sizeof(sh);
2090 char __user *up = ubuf + *sk_offset;
2092 if (read_extent_buffer_to_user(leaf, up,
2093 item_off, item_len)) {
2098 *sk_offset += item_len;
2102 if (ret) /* -EOVERFLOW from above */
2105 if (*num_found >= sk->nr_items) {
2112 test.objectid = sk->max_objectid;
2113 test.type = sk->max_type;
2114 test.offset = sk->max_offset;
2115 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2117 else if (key->offset < (u64)-1)
2119 else if (key->type < (u8)-1) {
2122 } else if (key->objectid < (u64)-1) {
2130 * 0: all items from this leaf copied, continue with next
2131 * 1: * more items can be copied, but unused buffer is too small
2132 * * all items were found
2133 * Either way, it will stops the loop which iterates to the next
2135 * -EOVERFLOW: item was to large for buffer
2136 * -EFAULT: could not copy extent buffer back to userspace
2141 static noinline int search_ioctl(struct inode *inode,
2142 struct btrfs_ioctl_search_key *sk,
2146 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2147 struct btrfs_root *root;
2148 struct btrfs_key key;
2149 struct btrfs_path *path;
2152 unsigned long sk_offset = 0;
2154 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2155 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2159 path = btrfs_alloc_path();
2163 if (sk->tree_id == 0) {
2164 /* search the root of the inode that was passed */
2165 root = BTRFS_I(inode)->root;
2167 key.objectid = sk->tree_id;
2168 key.type = BTRFS_ROOT_ITEM_KEY;
2169 key.offset = (u64)-1;
2170 root = btrfs_read_fs_root_no_name(info, &key);
2172 btrfs_free_path(path);
2173 return PTR_ERR(root);
2177 key.objectid = sk->min_objectid;
2178 key.type = sk->min_type;
2179 key.offset = sk->min_offset;
2182 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2188 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2189 &sk_offset, &num_found);
2190 btrfs_release_path(path);
2198 sk->nr_items = num_found;
2199 btrfs_free_path(path);
2203 static noinline int btrfs_ioctl_tree_search(struct file *file,
2206 struct btrfs_ioctl_search_args __user *uargs;
2207 struct btrfs_ioctl_search_key sk;
2208 struct inode *inode;
2212 if (!capable(CAP_SYS_ADMIN))
2215 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2217 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2220 buf_size = sizeof(uargs->buf);
2222 inode = file_inode(file);
2223 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2226 * In the origin implementation an overflow is handled by returning a
2227 * search header with a len of zero, so reset ret.
2229 if (ret == -EOVERFLOW)
2232 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2237 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2240 struct btrfs_ioctl_search_args_v2 __user *uarg;
2241 struct btrfs_ioctl_search_args_v2 args;
2242 struct inode *inode;
2245 const size_t buf_limit = SZ_16M;
2247 if (!capable(CAP_SYS_ADMIN))
2250 /* copy search header and buffer size */
2251 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2252 if (copy_from_user(&args, uarg, sizeof(args)))
2255 buf_size = args.buf_size;
2257 /* limit result size to 16MB */
2258 if (buf_size > buf_limit)
2259 buf_size = buf_limit;
2261 inode = file_inode(file);
2262 ret = search_ioctl(inode, &args.key, &buf_size,
2263 (char __user *)(&uarg->buf[0]));
2264 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2266 else if (ret == -EOVERFLOW &&
2267 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2274 * Search INODE_REFs to identify path name of 'dirid' directory
2275 * in a 'tree_id' tree. and sets path name to 'name'.
2277 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2278 u64 tree_id, u64 dirid, char *name)
2280 struct btrfs_root *root;
2281 struct btrfs_key key;
2287 struct btrfs_inode_ref *iref;
2288 struct extent_buffer *l;
2289 struct btrfs_path *path;
2291 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2296 path = btrfs_alloc_path();
2300 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2302 key.objectid = tree_id;
2303 key.type = BTRFS_ROOT_ITEM_KEY;
2304 key.offset = (u64)-1;
2305 root = btrfs_read_fs_root_no_name(info, &key);
2307 ret = PTR_ERR(root);
2311 key.objectid = dirid;
2312 key.type = BTRFS_INODE_REF_KEY;
2313 key.offset = (u64)-1;
2316 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2320 ret = btrfs_previous_item(root, path, dirid,
2321 BTRFS_INODE_REF_KEY);
2331 slot = path->slots[0];
2332 btrfs_item_key_to_cpu(l, &key, slot);
2334 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2335 len = btrfs_inode_ref_name_len(l, iref);
2337 total_len += len + 1;
2339 ret = -ENAMETOOLONG;
2344 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2346 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2349 btrfs_release_path(path);
2350 key.objectid = key.offset;
2351 key.offset = (u64)-1;
2352 dirid = key.objectid;
2354 memmove(name, ptr, total_len);
2355 name[total_len] = '\0';
2358 btrfs_free_path(path);
2362 static int btrfs_search_path_in_tree_user(struct inode *inode,
2363 struct btrfs_ioctl_ino_lookup_user_args *args)
2365 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2366 struct super_block *sb = inode->i_sb;
2367 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2368 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2369 u64 dirid = args->dirid;
2370 unsigned long item_off;
2371 unsigned long item_len;
2372 struct btrfs_inode_ref *iref;
2373 struct btrfs_root_ref *rref;
2374 struct btrfs_root *root;
2375 struct btrfs_path *path;
2376 struct btrfs_key key, key2;
2377 struct extent_buffer *leaf;
2378 struct inode *temp_inode;
2385 path = btrfs_alloc_path();
2390 * If the bottom subvolume does not exist directly under upper_limit,
2391 * construct the path in from the bottom up.
2393 if (dirid != upper_limit.objectid) {
2394 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2396 key.objectid = treeid;
2397 key.type = BTRFS_ROOT_ITEM_KEY;
2398 key.offset = (u64)-1;
2399 root = btrfs_read_fs_root_no_name(fs_info, &key);
2401 ret = PTR_ERR(root);
2405 key.objectid = dirid;
2406 key.type = BTRFS_INODE_REF_KEY;
2407 key.offset = (u64)-1;
2409 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2412 } else if (ret > 0) {
2413 ret = btrfs_previous_item(root, path, dirid,
2414 BTRFS_INODE_REF_KEY);
2417 } else if (ret > 0) {
2423 leaf = path->nodes[0];
2424 slot = path->slots[0];
2425 btrfs_item_key_to_cpu(leaf, &key, slot);
2427 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2428 len = btrfs_inode_ref_name_len(leaf, iref);
2430 total_len += len + 1;
2431 if (ptr < args->path) {
2432 ret = -ENAMETOOLONG;
2437 read_extent_buffer(leaf, ptr,
2438 (unsigned long)(iref + 1), len);
2440 /* Check the read+exec permission of this directory */
2441 ret = btrfs_previous_item(root, path, dirid,
2442 BTRFS_INODE_ITEM_KEY);
2445 } else if (ret > 0) {
2450 leaf = path->nodes[0];
2451 slot = path->slots[0];
2452 btrfs_item_key_to_cpu(leaf, &key2, slot);
2453 if (key2.objectid != dirid) {
2458 temp_inode = btrfs_iget(sb, &key2, root, NULL);
2459 if (IS_ERR(temp_inode)) {
2460 ret = PTR_ERR(temp_inode);
2463 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2470 if (key.offset == upper_limit.objectid)
2472 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2477 btrfs_release_path(path);
2478 key.objectid = key.offset;
2479 key.offset = (u64)-1;
2480 dirid = key.objectid;
2483 memmove(args->path, ptr, total_len);
2484 args->path[total_len] = '\0';
2485 btrfs_release_path(path);
2488 /* Get the bottom subvolume's name from ROOT_REF */
2489 root = fs_info->tree_root;
2490 key.objectid = treeid;
2491 key.type = BTRFS_ROOT_REF_KEY;
2492 key.offset = args->treeid;
2493 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2496 } else if (ret > 0) {
2501 leaf = path->nodes[0];
2502 slot = path->slots[0];
2503 btrfs_item_key_to_cpu(leaf, &key, slot);
2505 item_off = btrfs_item_ptr_offset(leaf, slot);
2506 item_len = btrfs_item_size_nr(leaf, slot);
2507 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2508 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2509 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2514 /* Copy subvolume's name */
2515 item_off += sizeof(struct btrfs_root_ref);
2516 item_len -= sizeof(struct btrfs_root_ref);
2517 read_extent_buffer(leaf, args->name, item_off, item_len);
2518 args->name[item_len] = 0;
2521 btrfs_free_path(path);
2525 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2528 struct btrfs_ioctl_ino_lookup_args *args;
2529 struct inode *inode;
2532 args = memdup_user(argp, sizeof(*args));
2534 return PTR_ERR(args);
2536 inode = file_inode(file);
2539 * Unprivileged query to obtain the containing subvolume root id. The
2540 * path is reset so it's consistent with btrfs_search_path_in_tree.
2542 if (args->treeid == 0)
2543 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2545 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2550 if (!capable(CAP_SYS_ADMIN)) {
2555 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2556 args->treeid, args->objectid,
2560 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2568 * Version of ino_lookup ioctl (unprivileged)
2570 * The main differences from ino_lookup ioctl are:
2572 * 1. Read + Exec permission will be checked using inode_permission() during
2573 * path construction. -EACCES will be returned in case of failure.
2574 * 2. Path construction will be stopped at the inode number which corresponds
2575 * to the fd with which this ioctl is called. If constructed path does not
2576 * exist under fd's inode, -EACCES will be returned.
2577 * 3. The name of bottom subvolume is also searched and filled.
2579 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2581 struct btrfs_ioctl_ino_lookup_user_args *args;
2582 struct inode *inode;
2585 args = memdup_user(argp, sizeof(*args));
2587 return PTR_ERR(args);
2589 inode = file_inode(file);
2591 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2592 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2594 * The subvolume does not exist under fd with which this is
2601 ret = btrfs_search_path_in_tree_user(inode, args);
2603 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2610 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2611 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2613 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2614 struct btrfs_fs_info *fs_info;
2615 struct btrfs_root *root;
2616 struct btrfs_path *path;
2617 struct btrfs_key key;
2618 struct btrfs_root_item *root_item;
2619 struct btrfs_root_ref *rref;
2620 struct extent_buffer *leaf;
2621 unsigned long item_off;
2622 unsigned long item_len;
2623 struct inode *inode;
2627 path = btrfs_alloc_path();
2631 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2633 btrfs_free_path(path);
2637 inode = file_inode(file);
2638 fs_info = BTRFS_I(inode)->root->fs_info;
2640 /* Get root_item of inode's subvolume */
2641 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2642 key.type = BTRFS_ROOT_ITEM_KEY;
2643 key.offset = (u64)-1;
2644 root = btrfs_read_fs_root_no_name(fs_info, &key);
2646 ret = PTR_ERR(root);
2649 root_item = &root->root_item;
2651 subvol_info->treeid = key.objectid;
2653 subvol_info->generation = btrfs_root_generation(root_item);
2654 subvol_info->flags = btrfs_root_flags(root_item);
2656 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2657 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2659 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2662 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2663 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2664 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2666 subvol_info->otransid = btrfs_root_otransid(root_item);
2667 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2668 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2670 subvol_info->stransid = btrfs_root_stransid(root_item);
2671 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2672 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2674 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2675 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2676 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2678 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2679 /* Search root tree for ROOT_BACKREF of this subvolume */
2680 root = fs_info->tree_root;
2682 key.type = BTRFS_ROOT_BACKREF_KEY;
2684 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2687 } else if (path->slots[0] >=
2688 btrfs_header_nritems(path->nodes[0])) {
2689 ret = btrfs_next_leaf(root, path);
2692 } else if (ret > 0) {
2698 leaf = path->nodes[0];
2699 slot = path->slots[0];
2700 btrfs_item_key_to_cpu(leaf, &key, slot);
2701 if (key.objectid == subvol_info->treeid &&
2702 key.type == BTRFS_ROOT_BACKREF_KEY) {
2703 subvol_info->parent_id = key.offset;
2705 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2706 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2708 item_off = btrfs_item_ptr_offset(leaf, slot)
2709 + sizeof(struct btrfs_root_ref);
2710 item_len = btrfs_item_size_nr(leaf, slot)
2711 - sizeof(struct btrfs_root_ref);
2712 read_extent_buffer(leaf, subvol_info->name,
2713 item_off, item_len);
2720 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2724 btrfs_free_path(path);
2725 kzfree(subvol_info);
2730 * Return ROOT_REF information of the subvolume containing this inode
2731 * except the subvolume name.
2733 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2735 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2736 struct btrfs_root_ref *rref;
2737 struct btrfs_root *root;
2738 struct btrfs_path *path;
2739 struct btrfs_key key;
2740 struct extent_buffer *leaf;
2741 struct inode *inode;
2747 path = btrfs_alloc_path();
2751 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2752 if (IS_ERR(rootrefs)) {
2753 btrfs_free_path(path);
2754 return PTR_ERR(rootrefs);
2757 inode = file_inode(file);
2758 root = BTRFS_I(inode)->root->fs_info->tree_root;
2759 objectid = BTRFS_I(inode)->root->root_key.objectid;
2761 key.objectid = objectid;
2762 key.type = BTRFS_ROOT_REF_KEY;
2763 key.offset = rootrefs->min_treeid;
2766 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2769 } else if (path->slots[0] >=
2770 btrfs_header_nritems(path->nodes[0])) {
2771 ret = btrfs_next_leaf(root, path);
2774 } else if (ret > 0) {
2780 leaf = path->nodes[0];
2781 slot = path->slots[0];
2783 btrfs_item_key_to_cpu(leaf, &key, slot);
2784 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2789 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2794 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2795 rootrefs->rootref[found].treeid = key.offset;
2796 rootrefs->rootref[found].dirid =
2797 btrfs_root_ref_dirid(leaf, rref);
2800 ret = btrfs_next_item(root, path);
2803 } else if (ret > 0) {
2810 if (!ret || ret == -EOVERFLOW) {
2811 rootrefs->num_items = found;
2812 /* update min_treeid for next search */
2814 rootrefs->min_treeid =
2815 rootrefs->rootref[found - 1].treeid + 1;
2816 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2821 btrfs_free_path(path);
2826 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2829 struct dentry *parent = file->f_path.dentry;
2830 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2831 struct dentry *dentry;
2832 struct inode *dir = d_inode(parent);
2833 struct inode *inode;
2834 struct btrfs_root *root = BTRFS_I(dir)->root;
2835 struct btrfs_root *dest = NULL;
2836 struct btrfs_ioctl_vol_args *vol_args;
2840 if (!S_ISDIR(dir->i_mode))
2843 vol_args = memdup_user(arg, sizeof(*vol_args));
2844 if (IS_ERR(vol_args))
2845 return PTR_ERR(vol_args);
2847 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2848 namelen = strlen(vol_args->name);
2849 if (strchr(vol_args->name, '/') ||
2850 strncmp(vol_args->name, "..", namelen) == 0) {
2855 err = mnt_want_write_file(file);
2860 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2862 goto out_drop_write;
2863 dentry = lookup_one_len(vol_args->name, parent, namelen);
2864 if (IS_ERR(dentry)) {
2865 err = PTR_ERR(dentry);
2866 goto out_unlock_dir;
2869 if (d_really_is_negative(dentry)) {
2874 inode = d_inode(dentry);
2875 dest = BTRFS_I(inode)->root;
2876 if (!capable(CAP_SYS_ADMIN)) {
2878 * Regular user. Only allow this with a special mount
2879 * option, when the user has write+exec access to the
2880 * subvol root, and when rmdir(2) would have been
2883 * Note that this is _not_ check that the subvol is
2884 * empty or doesn't contain data that we wouldn't
2885 * otherwise be able to delete.
2887 * Users who want to delete empty subvols should try
2891 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
2895 * Do not allow deletion if the parent dir is the same
2896 * as the dir to be deleted. That means the ioctl
2897 * must be called on the dentry referencing the root
2898 * of the subvol, not a random directory contained
2905 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2910 /* check if subvolume may be deleted by a user */
2911 err = btrfs_may_delete(dir, dentry, 1);
2915 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2921 err = btrfs_delete_subvolume(dir, dentry);
2922 inode_unlock(inode);
2924 fsnotify_rmdir(dir, dentry);
2933 mnt_drop_write_file(file);
2939 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2941 struct inode *inode = file_inode(file);
2942 struct btrfs_root *root = BTRFS_I(inode)->root;
2943 struct btrfs_ioctl_defrag_range_args *range;
2946 ret = mnt_want_write_file(file);
2950 if (btrfs_root_readonly(root)) {
2955 switch (inode->i_mode & S_IFMT) {
2957 if (!capable(CAP_SYS_ADMIN)) {
2961 ret = btrfs_defrag_root(root);
2965 * Note that this does not check the file descriptor for write
2966 * access. This prevents defragmenting executables that are
2967 * running and allows defrag on files open in read-only mode.
2969 if (!capable(CAP_SYS_ADMIN) &&
2970 inode_permission(inode, MAY_WRITE)) {
2975 range = kzalloc(sizeof(*range), GFP_KERNEL);
2982 if (copy_from_user(range, argp,
2988 /* compression requires us to start the IO */
2989 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2990 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2991 range->extent_thresh = (u32)-1;
2994 /* the rest are all set to zero by kzalloc */
2995 range->len = (u64)-1;
2997 ret = btrfs_defrag_file(file_inode(file), file,
2998 range, BTRFS_OLDEST_GENERATION, 0);
3007 mnt_drop_write_file(file);
3011 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3013 struct btrfs_ioctl_vol_args *vol_args;
3016 if (!capable(CAP_SYS_ADMIN))
3019 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
3020 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3022 vol_args = memdup_user(arg, sizeof(*vol_args));
3023 if (IS_ERR(vol_args)) {
3024 ret = PTR_ERR(vol_args);
3028 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3029 ret = btrfs_init_new_device(fs_info, vol_args->name);
3032 btrfs_info(fs_info, "disk added %s", vol_args->name);
3036 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3040 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3042 struct inode *inode = file_inode(file);
3043 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3044 struct btrfs_ioctl_vol_args_v2 *vol_args;
3047 if (!capable(CAP_SYS_ADMIN))
3050 ret = mnt_want_write_file(file);
3054 vol_args = memdup_user(arg, sizeof(*vol_args));
3055 if (IS_ERR(vol_args)) {
3056 ret = PTR_ERR(vol_args);
3060 /* Check for compatibility reject unknown flags */
3061 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3066 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
3067 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3071 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3072 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3074 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3075 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3077 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3080 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3081 btrfs_info(fs_info, "device deleted: id %llu",
3084 btrfs_info(fs_info, "device deleted: %s",
3090 mnt_drop_write_file(file);
3094 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3096 struct inode *inode = file_inode(file);
3097 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3098 struct btrfs_ioctl_vol_args *vol_args;
3101 if (!capable(CAP_SYS_ADMIN))
3104 ret = mnt_want_write_file(file);
3108 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
3109 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3110 goto out_drop_write;
3113 vol_args = memdup_user(arg, sizeof(*vol_args));
3114 if (IS_ERR(vol_args)) {
3115 ret = PTR_ERR(vol_args);
3119 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3120 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3123 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3126 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3128 mnt_drop_write_file(file);
3133 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3136 struct btrfs_ioctl_fs_info_args *fi_args;
3137 struct btrfs_device *device;
3138 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3141 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3146 fi_args->num_devices = fs_devices->num_devices;
3148 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3149 if (device->devid > fi_args->max_id)
3150 fi_args->max_id = device->devid;
3154 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3155 fi_args->nodesize = fs_info->nodesize;
3156 fi_args->sectorsize = fs_info->sectorsize;
3157 fi_args->clone_alignment = fs_info->sectorsize;
3159 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3166 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3169 struct btrfs_ioctl_dev_info_args *di_args;
3170 struct btrfs_device *dev;
3172 char *s_uuid = NULL;
3174 di_args = memdup_user(arg, sizeof(*di_args));
3175 if (IS_ERR(di_args))
3176 return PTR_ERR(di_args);
3178 if (!btrfs_is_empty_uuid(di_args->uuid))
3179 s_uuid = di_args->uuid;
3182 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3190 di_args->devid = dev->devid;
3191 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3192 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3193 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3195 strncpy(di_args->path, rcu_str_deref(dev->name),
3196 sizeof(di_args->path) - 1);
3197 di_args->path[sizeof(di_args->path) - 1] = 0;
3199 di_args->path[0] = '\0';
3204 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3211 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3212 struct inode *inode2, u64 loff2, u64 len)
3214 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3215 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3218 static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3219 struct inode *inode2, u64 loff2, u64 len)
3221 if (inode1 < inode2) {
3222 swap(inode1, inode2);
3224 } else if (inode1 == inode2 && loff2 < loff1) {
3227 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3228 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3231 static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
3232 struct inode *dst, u64 dst_loff)
3237 * Lock destination range to serialize with concurrent readpages() and
3238 * source range to serialize with relocation.
3240 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
3241 ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
3242 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
3247 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3249 static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3250 struct inode *dst, u64 dst_loff)
3253 u64 i, tail_len, chunk_count;
3254 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3256 spin_lock(&root_dst->root_item_lock);
3257 if (root_dst->send_in_progress) {
3258 btrfs_warn_rl(root_dst->fs_info,
3259 "cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3260 root_dst->root_key.objectid,
3261 root_dst->send_in_progress);
3262 spin_unlock(&root_dst->root_item_lock);
3265 root_dst->dedupe_in_progress++;
3266 spin_unlock(&root_dst->root_item_lock);
3268 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3269 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
3271 for (i = 0; i < chunk_count; i++) {
3272 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
3277 loff += BTRFS_MAX_DEDUPE_LEN;
3278 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3282 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3285 spin_lock(&root_dst->root_item_lock);
3286 root_dst->dedupe_in_progress--;
3287 spin_unlock(&root_dst->root_item_lock);
3292 static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3293 struct inode *inode,
3299 struct btrfs_root *root = BTRFS_I(inode)->root;
3302 inode_inc_iversion(inode);
3303 if (!no_time_update)
3304 inode->i_mtime = inode->i_ctime = current_time(inode);
3306 * We round up to the block size at eof when determining which
3307 * extents to clone above, but shouldn't round up the file size.
3309 if (endoff > destoff + olen)
3310 endoff = destoff + olen;
3311 if (endoff > inode->i_size)
3312 btrfs_i_size_write(BTRFS_I(inode), endoff);
3314 ret = btrfs_update_inode(trans, root, inode);
3316 btrfs_abort_transaction(trans, ret);
3317 btrfs_end_transaction(trans);
3320 ret = btrfs_end_transaction(trans);
3325 static void clone_update_extent_map(struct btrfs_inode *inode,
3326 const struct btrfs_trans_handle *trans,
3327 const struct btrfs_path *path,
3328 const u64 hole_offset,
3331 struct extent_map_tree *em_tree = &inode->extent_tree;
3332 struct extent_map *em;
3335 em = alloc_extent_map();
3337 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
3342 struct btrfs_file_extent_item *fi;
3344 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3345 struct btrfs_file_extent_item);
3346 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3347 em->generation = -1;
3348 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3349 BTRFS_FILE_EXTENT_INLINE)
3350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3351 &inode->runtime_flags);
3353 em->start = hole_offset;
3355 em->ram_bytes = em->len;
3356 em->orig_start = hole_offset;
3357 em->block_start = EXTENT_MAP_HOLE;
3359 em->orig_block_len = 0;
3360 em->compress_type = BTRFS_COMPRESS_NONE;
3361 em->generation = trans->transid;
3365 write_lock(&em_tree->lock);
3366 ret = add_extent_mapping(em_tree, em, 1);
3367 write_unlock(&em_tree->lock);
3368 if (ret != -EEXIST) {
3369 free_extent_map(em);
3372 btrfs_drop_extent_cache(inode, em->start,
3373 em->start + em->len - 1, 0);
3377 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
3381 * Make sure we do not end up inserting an inline extent into a file that has
3382 * already other (non-inline) extents. If a file has an inline extent it can
3383 * not have any other extents and the (single) inline extent must start at the
3384 * file offset 0. Failing to respect these rules will lead to file corruption,
3385 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3387 * We can have extents that have been already written to disk or we can have
3388 * dirty ranges still in delalloc, in which case the extent maps and items are
3389 * created only when we run delalloc, and the delalloc ranges might fall outside
3390 * the range we are currently locking in the inode's io tree. So we check the
3391 * inode's i_size because of that (i_size updates are done while holding the
3392 * i_mutex, which we are holding here).
3393 * We also check to see if the inode has a size not greater than "datal" but has
3394 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3395 * protected against such concurrent fallocate calls by the i_mutex).
3397 * If the file has no extents but a size greater than datal, do not allow the
3398 * copy because we would need turn the inline extent into a non-inline one (even
3399 * with NO_HOLES enabled). If we find our destination inode only has one inline
3400 * extent, just overwrite it with the source inline extent if its size is less
3401 * than the source extent's size, or we could copy the source inline extent's
3402 * data into the destination inode's inline extent if the later is greater then
3405 static int clone_copy_inline_extent(struct inode *dst,
3406 struct btrfs_trans_handle *trans,
3407 struct btrfs_path *path,
3408 struct btrfs_key *new_key,
3409 const u64 drop_start,
3415 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
3416 struct btrfs_root *root = BTRFS_I(dst)->root;
3417 const u64 aligned_end = ALIGN(new_key->offset + datal,
3418 fs_info->sectorsize);
3420 struct btrfs_key key;
3422 if (new_key->offset > 0)
3425 key.objectid = btrfs_ino(BTRFS_I(dst));
3426 key.type = BTRFS_EXTENT_DATA_KEY;
3428 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3431 } else if (ret > 0) {
3432 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3433 ret = btrfs_next_leaf(root, path);
3437 goto copy_inline_extent;
3439 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3440 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
3441 key.type == BTRFS_EXTENT_DATA_KEY) {
3442 ASSERT(key.offset > 0);
3445 } else if (i_size_read(dst) <= datal) {
3446 struct btrfs_file_extent_item *ei;
3450 * If the file size is <= datal, make sure there are no other
3451 * extents following (can happen do to an fallocate call with
3452 * the flag FALLOC_FL_KEEP_SIZE).
3454 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3455 struct btrfs_file_extent_item);
3457 * If it's an inline extent, it can not have other extents
3460 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3461 BTRFS_FILE_EXTENT_INLINE)
3462 goto copy_inline_extent;
3464 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3465 if (ext_len > aligned_end)
3468 ret = btrfs_next_item(root, path);
3471 } else if (ret == 0) {
3472 btrfs_item_key_to_cpu(path->nodes[0], &key,
3474 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
3475 key.type == BTRFS_EXTENT_DATA_KEY)
3482 * We have no extent items, or we have an extent at offset 0 which may
3483 * or may not be inlined. All these cases are dealt the same way.
3485 if (i_size_read(dst) > datal) {
3487 * If the destination inode has an inline extent...
3488 * This would require copying the data from the source inline
3489 * extent into the beginning of the destination's inline extent.
3490 * But this is really complex, both extents can be compressed
3491 * or just one of them, which would require decompressing and
3492 * re-compressing data (which could increase the new compressed
3493 * size, not allowing the compressed data to fit anymore in an
3495 * So just don't support this case for now (it should be rare,
3496 * we are not really saving space when cloning inline extents).
3501 btrfs_release_path(path);
3502 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3505 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3510 const u32 start = btrfs_file_extent_calc_inline_size(0);
3512 memmove(inline_data + start, inline_data + start + skip, datal);
3515 write_extent_buffer(path->nodes[0], inline_data,
3516 btrfs_item_ptr_offset(path->nodes[0],
3519 inode_add_bytes(dst, datal);
3525 * btrfs_clone() - clone a range from inode file to another
3527 * @src: Inode to clone from
3528 * @inode: Inode to clone to
3529 * @off: Offset within source to start clone from
3530 * @olen: Original length, passed by user, of range to clone
3531 * @olen_aligned: Block-aligned value of olen
3532 * @destoff: Offset within @inode to start clone
3533 * @no_time_update: Whether to update mtime/ctime on the target inode
3535 static int btrfs_clone(struct inode *src, struct inode *inode,
3536 const u64 off, const u64 olen, const u64 olen_aligned,
3537 const u64 destoff, int no_time_update)
3539 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3540 struct btrfs_root *root = BTRFS_I(inode)->root;
3541 struct btrfs_path *path = NULL;
3542 struct extent_buffer *leaf;
3543 struct btrfs_trans_handle *trans;
3545 struct btrfs_key key;
3549 const u64 len = olen_aligned;
3550 u64 last_dest_end = destoff;
3553 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3557 path = btrfs_alloc_path();
3563 path->reada = READA_FORWARD;
3565 key.objectid = btrfs_ino(BTRFS_I(src));
3566 key.type = BTRFS_EXTENT_DATA_KEY;
3570 u64 next_key_min_offset = key.offset + 1;
3573 * note the key will change type as we walk through the
3576 path->leave_spinning = 1;
3577 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3582 * First search, if no extent item that starts at offset off was
3583 * found but the previous item is an extent item, it's possible
3584 * it might overlap our target range, therefore process it.
3586 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3587 btrfs_item_key_to_cpu(path->nodes[0], &key,
3588 path->slots[0] - 1);
3589 if (key.type == BTRFS_EXTENT_DATA_KEY)
3593 nritems = btrfs_header_nritems(path->nodes[0]);
3595 if (path->slots[0] >= nritems) {
3596 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3601 nritems = btrfs_header_nritems(path->nodes[0]);
3603 leaf = path->nodes[0];
3604 slot = path->slots[0];
3606 btrfs_item_key_to_cpu(leaf, &key, slot);
3607 if (key.type > BTRFS_EXTENT_DATA_KEY ||
3608 key.objectid != btrfs_ino(BTRFS_I(src)))
3611 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3612 struct btrfs_file_extent_item *extent;
3615 struct btrfs_key new_key;
3616 u64 disko = 0, diskl = 0;
3617 u64 datao = 0, datal = 0;
3621 extent = btrfs_item_ptr(leaf, slot,
3622 struct btrfs_file_extent_item);
3623 comp = btrfs_file_extent_compression(leaf, extent);
3624 type = btrfs_file_extent_type(leaf, extent);
3625 if (type == BTRFS_FILE_EXTENT_REG ||
3626 type == BTRFS_FILE_EXTENT_PREALLOC) {
3627 disko = btrfs_file_extent_disk_bytenr(leaf,
3629 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3631 datao = btrfs_file_extent_offset(leaf, extent);
3632 datal = btrfs_file_extent_num_bytes(leaf,
3634 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3635 /* take upper bound, may be compressed */
3636 datal = btrfs_file_extent_ram_bytes(leaf,
3641 * The first search might have left us at an extent
3642 * item that ends before our target range's start, can
3643 * happen if we have holes and NO_HOLES feature enabled.
3645 if (key.offset + datal <= off) {
3648 } else if (key.offset >= off + len) {
3651 next_key_min_offset = key.offset + datal;
3652 size = btrfs_item_size_nr(leaf, slot);
3653 read_extent_buffer(leaf, buf,
3654 btrfs_item_ptr_offset(leaf, slot),
3657 btrfs_release_path(path);
3658 path->leave_spinning = 0;
3660 memcpy(&new_key, &key, sizeof(new_key));
3661 new_key.objectid = btrfs_ino(BTRFS_I(inode));
3662 if (off <= key.offset)
3663 new_key.offset = key.offset + destoff - off;
3665 new_key.offset = destoff;
3668 * Deal with a hole that doesn't have an extent item
3669 * that represents it (NO_HOLES feature enabled).
3670 * This hole is either in the middle of the cloning
3671 * range or at the beginning (fully overlaps it or
3672 * partially overlaps it).
3674 if (new_key.offset != last_dest_end)
3675 drop_start = last_dest_end;
3677 drop_start = new_key.offset;
3680 * 1 - adjusting old extent (we may have to split it)
3681 * 1 - add new extent
3684 trans = btrfs_start_transaction(root, 3);
3685 if (IS_ERR(trans)) {
3686 ret = PTR_ERR(trans);
3690 if (type == BTRFS_FILE_EXTENT_REG ||
3691 type == BTRFS_FILE_EXTENT_PREALLOC) {
3693 * a | --- range to clone ---| b
3694 * | ------------- extent ------------- |
3697 /* subtract range b */
3698 if (key.offset + datal > off + len)
3699 datal = off + len - key.offset;
3701 /* subtract range a */
3702 if (off > key.offset) {
3703 datao += off - key.offset;
3704 datal -= off - key.offset;
3707 ret = btrfs_drop_extents(trans, root, inode,
3709 new_key.offset + datal,
3712 if (ret != -EOPNOTSUPP)
3713 btrfs_abort_transaction(trans,
3715 btrfs_end_transaction(trans);
3719 ret = btrfs_insert_empty_item(trans, root, path,
3722 btrfs_abort_transaction(trans, ret);
3723 btrfs_end_transaction(trans);
3727 leaf = path->nodes[0];
3728 slot = path->slots[0];
3729 write_extent_buffer(leaf, buf,
3730 btrfs_item_ptr_offset(leaf, slot),
3733 extent = btrfs_item_ptr(leaf, slot,
3734 struct btrfs_file_extent_item);
3736 /* disko == 0 means it's a hole */
3740 btrfs_set_file_extent_offset(leaf, extent,
3742 btrfs_set_file_extent_num_bytes(leaf, extent,
3746 struct btrfs_ref ref = { 0 };
3747 inode_add_bytes(inode, datal);
3748 btrfs_init_generic_ref(&ref,
3749 BTRFS_ADD_DELAYED_REF, disko,
3751 btrfs_init_data_ref(&ref,
3752 root->root_key.objectid,
3753 btrfs_ino(BTRFS_I(inode)),
3754 new_key.offset - datao);
3755 ret = btrfs_inc_extent_ref(trans, &ref);
3757 btrfs_abort_transaction(trans,
3759 btrfs_end_transaction(trans);
3764 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3768 if (off > key.offset) {
3769 skip = off - key.offset;
3770 new_key.offset += skip;
3773 if (key.offset + datal > off + len)
3774 trim = key.offset + datal - (off + len);
3776 if (comp && (skip || trim)) {
3778 btrfs_end_transaction(trans);
3781 size -= skip + trim;
3782 datal -= skip + trim;
3784 ret = clone_copy_inline_extent(inode,
3791 if (ret != -EOPNOTSUPP)
3792 btrfs_abort_transaction(trans,
3794 btrfs_end_transaction(trans);
3797 leaf = path->nodes[0];
3798 slot = path->slots[0];
3801 /* If we have an implicit hole (NO_HOLES feature). */
3802 if (drop_start < new_key.offset)
3803 clone_update_extent_map(BTRFS_I(inode), trans,
3805 new_key.offset - drop_start);
3807 clone_update_extent_map(BTRFS_I(inode), trans,
3810 btrfs_mark_buffer_dirty(leaf);
3811 btrfs_release_path(path);
3813 last_dest_end = ALIGN(new_key.offset + datal,
3814 fs_info->sectorsize);
3815 ret = clone_finish_inode_update(trans, inode,
3821 if (new_key.offset + datal >= destoff + len)
3824 btrfs_release_path(path);
3825 key.offset = next_key_min_offset;
3827 if (fatal_signal_pending(current)) {
3834 if (last_dest_end < destoff + len) {
3836 * We have an implicit hole (NO_HOLES feature is enabled) that
3837 * fully or partially overlaps our cloning range at its end.
3839 btrfs_release_path(path);
3842 * 1 - remove extent(s)
3845 trans = btrfs_start_transaction(root, 2);
3846 if (IS_ERR(trans)) {
3847 ret = PTR_ERR(trans);
3850 ret = btrfs_drop_extents(trans, root, inode,
3851 last_dest_end, destoff + len, 1);
3853 if (ret != -EOPNOTSUPP)
3854 btrfs_abort_transaction(trans, ret);
3855 btrfs_end_transaction(trans);
3858 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3860 destoff + len - last_dest_end);
3861 ret = clone_finish_inode_update(trans, inode, destoff + len,
3862 destoff, olen, no_time_update);
3866 btrfs_free_path(path);
3871 static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3872 u64 off, u64 olen, u64 destoff)
3874 struct inode *inode = file_inode(file);
3875 struct inode *src = file_inode(file_src);
3876 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3879 u64 bs = fs_info->sb->s_blocksize;
3883 * - split compressed inline extents. annoying: we need to
3884 * decompress into destination's address_space (the file offset
3885 * may change, so source mapping won't do), then recompress (or
3886 * otherwise reinsert) a subrange.
3888 * - split destination inode's inline extents. The inline extents can
3889 * be either compressed or non-compressed.
3893 * VFS's generic_remap_file_range_prep() protects us from cloning the
3894 * eof block into the middle of a file, which would result in corruption
3895 * if the file size is not blocksize aligned. So we don't need to check
3896 * for that case here.
3898 if (off + len == src->i_size)
3899 len = ALIGN(src->i_size, bs) - off;
3901 if (destoff > inode->i_size) {
3902 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3904 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3908 * We may have truncated the last block if the inode's size is
3909 * not sector size aligned, so we need to wait for writeback to
3910 * complete before proceeding further, otherwise we can race
3911 * with cloning and attempt to increment a reference to an
3912 * extent that no longer exists (writeback completed right after
3913 * we found the previous extent covering eof and before we
3914 * attempted to increment its reference count).
3916 ret = btrfs_wait_ordered_range(inode, wb_start,
3917 destoff - wb_start);
3923 * Lock destination range to serialize with concurrent readpages() and
3924 * source range to serialize with relocation.
3926 btrfs_double_extent_lock(src, off, inode, destoff, len);
3927 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
3928 btrfs_double_extent_unlock(src, off, inode, destoff, len);
3930 * Truncate page cache pages so that future reads will see the cloned
3931 * data immediately and not the previous data.
3933 truncate_inode_pages_range(&inode->i_data,
3934 round_down(destoff, PAGE_SIZE),
3935 round_up(destoff + len, PAGE_SIZE) - 1);
3940 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3941 struct file *file_out, loff_t pos_out,
3942 loff_t *len, unsigned int remap_flags)
3944 struct inode *inode_in = file_inode(file_in);
3945 struct inode *inode_out = file_inode(file_out);
3946 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3947 bool same_inode = inode_out == inode_in;
3951 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3952 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3954 if (btrfs_root_readonly(root_out))
3957 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3958 inode_in->i_sb != inode_out->i_sb)
3962 /* don't make the dst file partly checksummed */
3963 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3964 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
3969 * Now that the inodes are locked, we need to start writeback ourselves
3970 * and can not rely on the writeback from the VFS's generic helper
3971 * generic_remap_file_range_prep() because:
3973 * 1) For compression we must call filemap_fdatawrite_range() range
3974 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3975 * helper only calls it once;
3977 * 2) filemap_fdatawrite_range(), called by the generic helper only
3978 * waits for the writeback to complete, i.e. for IO to be done, and
3979 * not for the ordered extents to complete. We need to wait for them
3980 * to complete so that new file extent items are in the fs tree.
3982 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3983 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3985 wb_len = ALIGN(*len, bs);
3988 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3989 * any in progress could create its ordered extents after we wait for
3990 * existing ordered extents below).
3992 inode_dio_wait(inode_in);
3994 inode_dio_wait(inode_out);
3996 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
4000 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
4005 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
4009 loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4010 struct file *dst_file, loff_t destoff, loff_t len,
4011 unsigned int remap_flags)
4013 struct inode *src_inode = file_inode(src_file);
4014 struct inode *dst_inode = file_inode(dst_file);
4015 bool same_inode = dst_inode == src_inode;
4018 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4022 inode_lock(src_inode);
4024 lock_two_nondirectories(src_inode, dst_inode);
4026 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4028 if (ret < 0 || len == 0)
4031 if (remap_flags & REMAP_FILE_DEDUP)
4032 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4034 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
4038 inode_unlock(src_inode);
4040 unlock_two_nondirectories(src_inode, dst_inode);
4042 return ret < 0 ? ret : len;
4045 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4047 struct inode *inode = file_inode(file);
4048 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4049 struct btrfs_root *root = BTRFS_I(inode)->root;
4050 struct btrfs_root *new_root;
4051 struct btrfs_dir_item *di;
4052 struct btrfs_trans_handle *trans;
4053 struct btrfs_path *path;
4054 struct btrfs_key location;
4055 struct btrfs_disk_key disk_key;
4060 if (!capable(CAP_SYS_ADMIN))
4063 ret = mnt_want_write_file(file);
4067 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4073 objectid = BTRFS_FS_TREE_OBJECTID;
4075 location.objectid = objectid;
4076 location.type = BTRFS_ROOT_ITEM_KEY;
4077 location.offset = (u64)-1;
4079 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
4080 if (IS_ERR(new_root)) {
4081 ret = PTR_ERR(new_root);
4084 if (!is_fstree(new_root->root_key.objectid)) {
4089 path = btrfs_alloc_path();
4094 path->leave_spinning = 1;
4096 trans = btrfs_start_transaction(root, 1);
4097 if (IS_ERR(trans)) {
4098 btrfs_free_path(path);
4099 ret = PTR_ERR(trans);
4103 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4104 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
4105 dir_id, "default", 7, 1);
4106 if (IS_ERR_OR_NULL(di)) {
4107 btrfs_free_path(path);
4108 btrfs_end_transaction(trans);
4110 "Umm, you don't have the default diritem, this isn't going to work");
4115 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4116 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4117 btrfs_mark_buffer_dirty(path->nodes[0]);
4118 btrfs_free_path(path);
4120 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
4121 btrfs_end_transaction(trans);
4123 mnt_drop_write_file(file);
4127 static void get_block_group_info(struct list_head *groups_list,
4128 struct btrfs_ioctl_space_info *space)
4130 struct btrfs_block_group_cache *block_group;
4132 space->total_bytes = 0;
4133 space->used_bytes = 0;
4135 list_for_each_entry(block_group, groups_list, list) {
4136 space->flags = block_group->flags;
4137 space->total_bytes += block_group->key.offset;
4138 space->used_bytes +=
4139 btrfs_block_group_used(&block_group->item);
4143 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4146 struct btrfs_ioctl_space_args space_args;
4147 struct btrfs_ioctl_space_info space;
4148 struct btrfs_ioctl_space_info *dest;
4149 struct btrfs_ioctl_space_info *dest_orig;
4150 struct btrfs_ioctl_space_info __user *user_dest;
4151 struct btrfs_space_info *info;
4152 static const u64 types[] = {
4153 BTRFS_BLOCK_GROUP_DATA,
4154 BTRFS_BLOCK_GROUP_SYSTEM,
4155 BTRFS_BLOCK_GROUP_METADATA,
4156 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4164 if (copy_from_user(&space_args,
4165 (struct btrfs_ioctl_space_args __user *)arg,
4166 sizeof(space_args)))
4169 for (i = 0; i < num_types; i++) {
4170 struct btrfs_space_info *tmp;
4174 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4176 if (tmp->flags == types[i]) {
4186 down_read(&info->groups_sem);
4187 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4188 if (!list_empty(&info->block_groups[c]))
4191 up_read(&info->groups_sem);
4195 * Global block reserve, exported as a space_info
4199 /* space_slots == 0 means they are asking for a count */
4200 if (space_args.space_slots == 0) {
4201 space_args.total_spaces = slot_count;
4205 slot_count = min_t(u64, space_args.space_slots, slot_count);
4207 alloc_size = sizeof(*dest) * slot_count;
4209 /* we generally have at most 6 or so space infos, one for each raid
4210 * level. So, a whole page should be more than enough for everyone
4212 if (alloc_size > PAGE_SIZE)
4215 space_args.total_spaces = 0;
4216 dest = kmalloc(alloc_size, GFP_KERNEL);
4221 /* now we have a buffer to copy into */
4222 for (i = 0; i < num_types; i++) {
4223 struct btrfs_space_info *tmp;
4230 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4232 if (tmp->flags == types[i]) {
4241 down_read(&info->groups_sem);
4242 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4243 if (!list_empty(&info->block_groups[c])) {
4244 get_block_group_info(&info->block_groups[c],
4246 memcpy(dest, &space, sizeof(space));
4248 space_args.total_spaces++;
4254 up_read(&info->groups_sem);
4258 * Add global block reserve
4261 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4263 spin_lock(&block_rsv->lock);
4264 space.total_bytes = block_rsv->size;
4265 space.used_bytes = block_rsv->size - block_rsv->reserved;
4266 spin_unlock(&block_rsv->lock);
4267 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4268 memcpy(dest, &space, sizeof(space));
4269 space_args.total_spaces++;
4272 user_dest = (struct btrfs_ioctl_space_info __user *)
4273 (arg + sizeof(struct btrfs_ioctl_space_args));
4275 if (copy_to_user(user_dest, dest_orig, alloc_size))
4280 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
4286 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4289 struct btrfs_trans_handle *trans;
4293 trans = btrfs_attach_transaction_barrier(root);
4294 if (IS_ERR(trans)) {
4295 if (PTR_ERR(trans) != -ENOENT)
4296 return PTR_ERR(trans);
4298 /* No running transaction, don't bother */
4299 transid = root->fs_info->last_trans_committed;
4302 transid = trans->transid;
4303 ret = btrfs_commit_transaction_async(trans, 0);
4305 btrfs_end_transaction(trans);
4310 if (copy_to_user(argp, &transid, sizeof(transid)))
4315 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
4321 if (copy_from_user(&transid, argp, sizeof(transid)))
4324 transid = 0; /* current trans */
4326 return btrfs_wait_for_commit(fs_info, transid);
4329 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4331 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
4332 struct btrfs_ioctl_scrub_args *sa;
4335 if (!capable(CAP_SYS_ADMIN))
4338 sa = memdup_user(arg, sizeof(*sa));
4342 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4343 ret = mnt_want_write_file(file);
4348 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
4349 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4352 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4355 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4356 mnt_drop_write_file(file);
4362 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
4364 if (!capable(CAP_SYS_ADMIN))
4367 return btrfs_scrub_cancel(fs_info);
4370 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
4373 struct btrfs_ioctl_scrub_args *sa;
4376 if (!capable(CAP_SYS_ADMIN))
4379 sa = memdup_user(arg, sizeof(*sa));
4383 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
4385 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4392 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
4395 struct btrfs_ioctl_get_dev_stats *sa;
4398 sa = memdup_user(arg, sizeof(*sa));
4402 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4407 ret = btrfs_get_dev_stats(fs_info, sa);
4409 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4416 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4419 struct btrfs_ioctl_dev_replace_args *p;
4422 if (!capable(CAP_SYS_ADMIN))
4425 p = memdup_user(arg, sizeof(*p));
4430 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
4431 if (sb_rdonly(fs_info->sb)) {
4435 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
4436 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4438 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
4439 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4442 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4443 btrfs_dev_replace_status(fs_info, p);
4446 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4447 p->result = btrfs_dev_replace_cancel(fs_info);
4455 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
4462 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4468 struct btrfs_ioctl_ino_path_args *ipa = NULL;
4469 struct inode_fs_paths *ipath = NULL;
4470 struct btrfs_path *path;
4472 if (!capable(CAP_DAC_READ_SEARCH))
4475 path = btrfs_alloc_path();
4481 ipa = memdup_user(arg, sizeof(*ipa));
4488 size = min_t(u32, ipa->size, 4096);
4489 ipath = init_ipath(size, root, path);
4490 if (IS_ERR(ipath)) {
4491 ret = PTR_ERR(ipath);
4496 ret = paths_from_inode(ipa->inum, ipath);
4500 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
4501 rel_ptr = ipath->fspath->val[i] -
4502 (u64)(unsigned long)ipath->fspath->val;
4503 ipath->fspath->val[i] = rel_ptr;
4506 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4507 ipath->fspath, size);
4514 btrfs_free_path(path);
4521 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4523 struct btrfs_data_container *inodes = ctx;
4524 const size_t c = 3 * sizeof(u64);
4526 if (inodes->bytes_left >= c) {
4527 inodes->bytes_left -= c;
4528 inodes->val[inodes->elem_cnt] = inum;
4529 inodes->val[inodes->elem_cnt + 1] = offset;
4530 inodes->val[inodes->elem_cnt + 2] = root;
4531 inodes->elem_cnt += 3;
4533 inodes->bytes_missing += c - inodes->bytes_left;
4534 inodes->bytes_left = 0;
4535 inodes->elem_missed += 3;
4541 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
4542 void __user *arg, int version)
4546 struct btrfs_ioctl_logical_ino_args *loi;
4547 struct btrfs_data_container *inodes = NULL;
4548 struct btrfs_path *path = NULL;
4551 if (!capable(CAP_SYS_ADMIN))
4554 loi = memdup_user(arg, sizeof(*loi));
4556 return PTR_ERR(loi);
4559 ignore_offset = false;
4560 size = min_t(u32, loi->size, SZ_64K);
4562 /* All reserved bits must be 0 for now */
4563 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4567 /* Only accept flags we have defined so far */
4568 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4572 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
4573 size = min_t(u32, loi->size, SZ_16M);
4576 path = btrfs_alloc_path();
4582 inodes = init_data_container(size);
4583 if (IS_ERR(inodes)) {
4584 ret = PTR_ERR(inodes);
4589 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
4590 build_ino_list, inodes, ignore_offset);
4596 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4602 btrfs_free_path(path);
4610 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
4611 struct btrfs_ioctl_balance_args *bargs)
4613 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4615 bargs->flags = bctl->flags;
4617 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
4618 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4619 if (atomic_read(&fs_info->balance_pause_req))
4620 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4621 if (atomic_read(&fs_info->balance_cancel_req))
4622 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4624 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4625 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4626 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
4628 spin_lock(&fs_info->balance_lock);
4629 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4630 spin_unlock(&fs_info->balance_lock);
4633 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4635 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4636 struct btrfs_fs_info *fs_info = root->fs_info;
4637 struct btrfs_ioctl_balance_args *bargs;
4638 struct btrfs_balance_control *bctl;
4639 bool need_unlock; /* for mut. excl. ops lock */
4642 if (!capable(CAP_SYS_ADMIN))
4645 ret = mnt_want_write_file(file);
4650 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
4651 mutex_lock(&fs_info->balance_mutex);
4657 * mut. excl. ops lock is locked. Three possibilities:
4658 * (1) some other op is running
4659 * (2) balance is running
4660 * (3) balance is paused -- special case (think resume)
4662 mutex_lock(&fs_info->balance_mutex);
4663 if (fs_info->balance_ctl) {
4664 /* this is either (2) or (3) */
4665 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4666 mutex_unlock(&fs_info->balance_mutex);
4668 * Lock released to allow other waiters to continue,
4669 * we'll reexamine the status again.
4671 mutex_lock(&fs_info->balance_mutex);
4673 if (fs_info->balance_ctl &&
4674 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4676 need_unlock = false;
4680 mutex_unlock(&fs_info->balance_mutex);
4684 mutex_unlock(&fs_info->balance_mutex);
4690 mutex_unlock(&fs_info->balance_mutex);
4691 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4696 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
4699 bargs = memdup_user(arg, sizeof(*bargs));
4700 if (IS_ERR(bargs)) {
4701 ret = PTR_ERR(bargs);
4705 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4706 if (!fs_info->balance_ctl) {
4711 bctl = fs_info->balance_ctl;
4712 spin_lock(&fs_info->balance_lock);
4713 bctl->flags |= BTRFS_BALANCE_RESUME;
4714 spin_unlock(&fs_info->balance_lock);
4722 if (fs_info->balance_ctl) {
4727 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4734 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4735 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4736 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4738 bctl->flags = bargs->flags;
4740 /* balance everything - no filters */
4741 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4744 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4751 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4752 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4753 * restriper was paused all the way until unmount, in free_fs_info.
4754 * The flag should be cleared after reset_balance_state.
4756 need_unlock = false;
4758 ret = btrfs_balance(fs_info, bctl, bargs);
4761 if ((ret == 0 || ret == -ECANCELED) && arg) {
4762 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4771 mutex_unlock(&fs_info->balance_mutex);
4773 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4775 mnt_drop_write_file(file);
4779 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4781 if (!capable(CAP_SYS_ADMIN))
4785 case BTRFS_BALANCE_CTL_PAUSE:
4786 return btrfs_pause_balance(fs_info);
4787 case BTRFS_BALANCE_CTL_CANCEL:
4788 return btrfs_cancel_balance(fs_info);
4794 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4797 struct btrfs_ioctl_balance_args *bargs;
4800 if (!capable(CAP_SYS_ADMIN))
4803 mutex_lock(&fs_info->balance_mutex);
4804 if (!fs_info->balance_ctl) {
4809 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4815 btrfs_update_ioctl_balance_args(fs_info, bargs);
4817 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4822 mutex_unlock(&fs_info->balance_mutex);
4826 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4828 struct inode *inode = file_inode(file);
4829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4830 struct btrfs_ioctl_quota_ctl_args *sa;
4833 if (!capable(CAP_SYS_ADMIN))
4836 ret = mnt_want_write_file(file);
4840 sa = memdup_user(arg, sizeof(*sa));
4846 down_write(&fs_info->subvol_sem);
4849 case BTRFS_QUOTA_CTL_ENABLE:
4850 ret = btrfs_quota_enable(fs_info);
4852 case BTRFS_QUOTA_CTL_DISABLE:
4853 ret = btrfs_quota_disable(fs_info);
4861 up_write(&fs_info->subvol_sem);
4863 mnt_drop_write_file(file);
4867 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4869 struct inode *inode = file_inode(file);
4870 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4871 struct btrfs_root *root = BTRFS_I(inode)->root;
4872 struct btrfs_ioctl_qgroup_assign_args *sa;
4873 struct btrfs_trans_handle *trans;
4877 if (!capable(CAP_SYS_ADMIN))
4880 ret = mnt_want_write_file(file);
4884 sa = memdup_user(arg, sizeof(*sa));
4890 trans = btrfs_join_transaction(root);
4891 if (IS_ERR(trans)) {
4892 ret = PTR_ERR(trans);
4897 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4899 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4902 /* update qgroup status and info */
4903 err = btrfs_run_qgroups(trans);
4905 btrfs_handle_fs_error(fs_info, err,
4906 "failed to update qgroup status and info");
4907 err = btrfs_end_transaction(trans);
4914 mnt_drop_write_file(file);
4918 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4920 struct inode *inode = file_inode(file);
4921 struct btrfs_root *root = BTRFS_I(inode)->root;
4922 struct btrfs_ioctl_qgroup_create_args *sa;
4923 struct btrfs_trans_handle *trans;
4927 if (!capable(CAP_SYS_ADMIN))
4930 ret = mnt_want_write_file(file);
4934 sa = memdup_user(arg, sizeof(*sa));
4940 if (!sa->qgroupid) {
4945 trans = btrfs_join_transaction(root);
4946 if (IS_ERR(trans)) {
4947 ret = PTR_ERR(trans);
4952 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4954 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4957 err = btrfs_end_transaction(trans);
4964 mnt_drop_write_file(file);
4968 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4970 struct inode *inode = file_inode(file);
4971 struct btrfs_root *root = BTRFS_I(inode)->root;
4972 struct btrfs_ioctl_qgroup_limit_args *sa;
4973 struct btrfs_trans_handle *trans;
4978 if (!capable(CAP_SYS_ADMIN))
4981 ret = mnt_want_write_file(file);
4985 sa = memdup_user(arg, sizeof(*sa));
4991 trans = btrfs_join_transaction(root);
4992 if (IS_ERR(trans)) {
4993 ret = PTR_ERR(trans);
4997 qgroupid = sa->qgroupid;
4999 /* take the current subvol as qgroup */
5000 qgroupid = root->root_key.objectid;
5003 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
5005 err = btrfs_end_transaction(trans);
5012 mnt_drop_write_file(file);
5016 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5018 struct inode *inode = file_inode(file);
5019 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5020 struct btrfs_ioctl_quota_rescan_args *qsa;
5023 if (!capable(CAP_SYS_ADMIN))
5026 ret = mnt_want_write_file(file);
5030 qsa = memdup_user(arg, sizeof(*qsa));
5041 ret = btrfs_qgroup_rescan(fs_info);
5046 mnt_drop_write_file(file);
5050 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5052 struct inode *inode = file_inode(file);
5053 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5054 struct btrfs_ioctl_quota_rescan_args *qsa;
5057 if (!capable(CAP_SYS_ADMIN))
5060 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
5064 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5066 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
5069 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5076 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5078 struct inode *inode = file_inode(file);
5079 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5081 if (!capable(CAP_SYS_ADMIN))
5084 return btrfs_qgroup_wait_for_completion(fs_info, true);
5087 static long _btrfs_ioctl_set_received_subvol(struct file *file,
5088 struct btrfs_ioctl_received_subvol_args *sa)
5090 struct inode *inode = file_inode(file);
5091 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5092 struct btrfs_root *root = BTRFS_I(inode)->root;
5093 struct btrfs_root_item *root_item = &root->root_item;
5094 struct btrfs_trans_handle *trans;
5095 struct timespec64 ct = current_time(inode);
5097 int received_uuid_changed;
5099 if (!inode_owner_or_capable(inode))
5102 ret = mnt_want_write_file(file);
5106 down_write(&fs_info->subvol_sem);
5108 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
5113 if (btrfs_root_readonly(root)) {
5120 * 2 - uuid items (received uuid + subvol uuid)
5122 trans = btrfs_start_transaction(root, 3);
5123 if (IS_ERR(trans)) {
5124 ret = PTR_ERR(trans);
5129 sa->rtransid = trans->transid;
5130 sa->rtime.sec = ct.tv_sec;
5131 sa->rtime.nsec = ct.tv_nsec;
5133 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5135 if (received_uuid_changed &&
5136 !btrfs_is_empty_uuid(root_item->received_uuid)) {
5137 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
5138 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5139 root->root_key.objectid);
5140 if (ret && ret != -ENOENT) {
5141 btrfs_abort_transaction(trans, ret);
5142 btrfs_end_transaction(trans);
5146 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5147 btrfs_set_root_stransid(root_item, sa->stransid);
5148 btrfs_set_root_rtransid(root_item, sa->rtransid);
5149 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5150 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5151 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5152 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
5154 ret = btrfs_update_root(trans, fs_info->tree_root,
5155 &root->root_key, &root->root_item);
5157 btrfs_end_transaction(trans);
5160 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5161 ret = btrfs_uuid_tree_add(trans, sa->uuid,
5162 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5163 root->root_key.objectid);
5164 if (ret < 0 && ret != -EEXIST) {
5165 btrfs_abort_transaction(trans, ret);
5166 btrfs_end_transaction(trans);
5170 ret = btrfs_commit_transaction(trans);
5172 up_write(&fs_info->subvol_sem);
5173 mnt_drop_write_file(file);
5178 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5181 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5182 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5185 args32 = memdup_user(arg, sizeof(*args32));
5187 return PTR_ERR(args32);
5189 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
5195 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5196 args64->stransid = args32->stransid;
5197 args64->rtransid = args32->rtransid;
5198 args64->stime.sec = args32->stime.sec;
5199 args64->stime.nsec = args32->stime.nsec;
5200 args64->rtime.sec = args32->rtime.sec;
5201 args64->rtime.nsec = args32->rtime.nsec;
5202 args64->flags = args32->flags;
5204 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5208 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5209 args32->stransid = args64->stransid;
5210 args32->rtransid = args64->rtransid;
5211 args32->stime.sec = args64->stime.sec;
5212 args32->stime.nsec = args64->stime.nsec;
5213 args32->rtime.sec = args64->rtime.sec;
5214 args32->rtime.nsec = args64->rtime.nsec;
5215 args32->flags = args64->flags;
5217 ret = copy_to_user(arg, args32, sizeof(*args32));
5228 static long btrfs_ioctl_set_received_subvol(struct file *file,
5231 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5234 sa = memdup_user(arg, sizeof(*sa));
5238 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5243 ret = copy_to_user(arg, sa, sizeof(*sa));
5252 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5254 struct inode *inode = file_inode(file);
5255 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5258 char label[BTRFS_LABEL_SIZE];
5260 spin_lock(&fs_info->super_lock);
5261 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5262 spin_unlock(&fs_info->super_lock);
5264 len = strnlen(label, BTRFS_LABEL_SIZE);
5266 if (len == BTRFS_LABEL_SIZE) {
5268 "label is too long, return the first %zu bytes",
5272 ret = copy_to_user(arg, label, len);
5274 return ret ? -EFAULT : 0;
5277 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5279 struct inode *inode = file_inode(file);
5280 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5281 struct btrfs_root *root = BTRFS_I(inode)->root;
5282 struct btrfs_super_block *super_block = fs_info->super_copy;
5283 struct btrfs_trans_handle *trans;
5284 char label[BTRFS_LABEL_SIZE];
5287 if (!capable(CAP_SYS_ADMIN))
5290 if (copy_from_user(label, arg, sizeof(label)))
5293 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
5295 "unable to set label with more than %d bytes",
5296 BTRFS_LABEL_SIZE - 1);
5300 ret = mnt_want_write_file(file);
5304 trans = btrfs_start_transaction(root, 0);
5305 if (IS_ERR(trans)) {
5306 ret = PTR_ERR(trans);
5310 spin_lock(&fs_info->super_lock);
5311 strcpy(super_block->label, label);
5312 spin_unlock(&fs_info->super_lock);
5313 ret = btrfs_commit_transaction(trans);
5316 mnt_drop_write_file(file);
5320 #define INIT_FEATURE_FLAGS(suffix) \
5321 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5322 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5323 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5325 int btrfs_ioctl_get_supported_features(void __user *arg)
5327 static const struct btrfs_ioctl_feature_flags features[3] = {
5328 INIT_FEATURE_FLAGS(SUPP),
5329 INIT_FEATURE_FLAGS(SAFE_SET),
5330 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5333 if (copy_to_user(arg, &features, sizeof(features)))
5339 static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5341 struct inode *inode = file_inode(file);
5342 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5343 struct btrfs_super_block *super_block = fs_info->super_copy;
5344 struct btrfs_ioctl_feature_flags features;
5346 features.compat_flags = btrfs_super_compat_flags(super_block);
5347 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5348 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5350 if (copy_to_user(arg, &features, sizeof(features)))
5356 static int check_feature_bits(struct btrfs_fs_info *fs_info,
5357 enum btrfs_feature_set set,
5358 u64 change_mask, u64 flags, u64 supported_flags,
5359 u64 safe_set, u64 safe_clear)
5361 const char *type = btrfs_feature_set_names[set];
5363 u64 disallowed, unsupported;
5364 u64 set_mask = flags & change_mask;
5365 u64 clear_mask = ~flags & change_mask;
5367 unsupported = set_mask & ~supported_flags;
5369 names = btrfs_printable_features(set, unsupported);
5372 "this kernel does not support the %s feature bit%s",
5373 names, strchr(names, ',') ? "s" : "");
5377 "this kernel does not support %s bits 0x%llx",
5382 disallowed = set_mask & ~safe_set;
5384 names = btrfs_printable_features(set, disallowed);
5387 "can't set the %s feature bit%s while mounted",
5388 names, strchr(names, ',') ? "s" : "");
5392 "can't set %s bits 0x%llx while mounted",
5397 disallowed = clear_mask & ~safe_clear;
5399 names = btrfs_printable_features(set, disallowed);
5402 "can't clear the %s feature bit%s while mounted",
5403 names, strchr(names, ',') ? "s" : "");
5407 "can't clear %s bits 0x%llx while mounted",
5415 #define check_feature(fs_info, change_mask, flags, mask_base) \
5416 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
5417 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5418 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5419 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5421 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5423 struct inode *inode = file_inode(file);
5424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5425 struct btrfs_root *root = BTRFS_I(inode)->root;
5426 struct btrfs_super_block *super_block = fs_info->super_copy;
5427 struct btrfs_ioctl_feature_flags flags[2];
5428 struct btrfs_trans_handle *trans;
5432 if (!capable(CAP_SYS_ADMIN))
5435 if (copy_from_user(flags, arg, sizeof(flags)))
5439 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5440 !flags[0].incompat_flags)
5443 ret = check_feature(fs_info, flags[0].compat_flags,
5444 flags[1].compat_flags, COMPAT);
5448 ret = check_feature(fs_info, flags[0].compat_ro_flags,
5449 flags[1].compat_ro_flags, COMPAT_RO);
5453 ret = check_feature(fs_info, flags[0].incompat_flags,
5454 flags[1].incompat_flags, INCOMPAT);
5458 ret = mnt_want_write_file(file);
5462 trans = btrfs_start_transaction(root, 0);
5463 if (IS_ERR(trans)) {
5464 ret = PTR_ERR(trans);
5465 goto out_drop_write;
5468 spin_lock(&fs_info->super_lock);
5469 newflags = btrfs_super_compat_flags(super_block);
5470 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5471 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5472 btrfs_set_super_compat_flags(super_block, newflags);
5474 newflags = btrfs_super_compat_ro_flags(super_block);
5475 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5476 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5477 btrfs_set_super_compat_ro_flags(super_block, newflags);
5479 newflags = btrfs_super_incompat_flags(super_block);
5480 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5481 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5482 btrfs_set_super_incompat_flags(super_block, newflags);
5483 spin_unlock(&fs_info->super_lock);
5485 ret = btrfs_commit_transaction(trans);
5487 mnt_drop_write_file(file);
5492 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5494 struct btrfs_ioctl_send_args *arg;
5498 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5499 struct btrfs_ioctl_send_args_32 args32;
5501 ret = copy_from_user(&args32, argp, sizeof(args32));
5504 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5507 arg->send_fd = args32.send_fd;
5508 arg->clone_sources_count = args32.clone_sources_count;
5509 arg->clone_sources = compat_ptr(args32.clone_sources);
5510 arg->parent_root = args32.parent_root;
5511 arg->flags = args32.flags;
5512 memcpy(arg->reserved, args32.reserved,
5513 sizeof(args32.reserved));
5518 arg = memdup_user(argp, sizeof(*arg));
5520 return PTR_ERR(arg);
5522 ret = btrfs_ioctl_send(file, arg);
5527 long btrfs_ioctl(struct file *file, unsigned int
5528 cmd, unsigned long arg)
5530 struct inode *inode = file_inode(file);
5531 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5532 struct btrfs_root *root = BTRFS_I(inode)->root;
5533 void __user *argp = (void __user *)arg;
5536 case FS_IOC_GETFLAGS:
5537 return btrfs_ioctl_getflags(file, argp);
5538 case FS_IOC_SETFLAGS:
5539 return btrfs_ioctl_setflags(file, argp);
5540 case FS_IOC_GETVERSION:
5541 return btrfs_ioctl_getversion(file, argp);
5543 return btrfs_ioctl_fitrim(file, argp);
5544 case BTRFS_IOC_SNAP_CREATE:
5545 return btrfs_ioctl_snap_create(file, argp, 0);
5546 case BTRFS_IOC_SNAP_CREATE_V2:
5547 return btrfs_ioctl_snap_create_v2(file, argp, 0);
5548 case BTRFS_IOC_SUBVOL_CREATE:
5549 return btrfs_ioctl_snap_create(file, argp, 1);
5550 case BTRFS_IOC_SUBVOL_CREATE_V2:
5551 return btrfs_ioctl_snap_create_v2(file, argp, 1);
5552 case BTRFS_IOC_SNAP_DESTROY:
5553 return btrfs_ioctl_snap_destroy(file, argp);
5554 case BTRFS_IOC_SUBVOL_GETFLAGS:
5555 return btrfs_ioctl_subvol_getflags(file, argp);
5556 case BTRFS_IOC_SUBVOL_SETFLAGS:
5557 return btrfs_ioctl_subvol_setflags(file, argp);
5558 case BTRFS_IOC_DEFAULT_SUBVOL:
5559 return btrfs_ioctl_default_subvol(file, argp);
5560 case BTRFS_IOC_DEFRAG:
5561 return btrfs_ioctl_defrag(file, NULL);
5562 case BTRFS_IOC_DEFRAG_RANGE:
5563 return btrfs_ioctl_defrag(file, argp);
5564 case BTRFS_IOC_RESIZE:
5565 return btrfs_ioctl_resize(file, argp);
5566 case BTRFS_IOC_ADD_DEV:
5567 return btrfs_ioctl_add_dev(fs_info, argp);
5568 case BTRFS_IOC_RM_DEV:
5569 return btrfs_ioctl_rm_dev(file, argp);
5570 case BTRFS_IOC_RM_DEV_V2:
5571 return btrfs_ioctl_rm_dev_v2(file, argp);
5572 case BTRFS_IOC_FS_INFO:
5573 return btrfs_ioctl_fs_info(fs_info, argp);
5574 case BTRFS_IOC_DEV_INFO:
5575 return btrfs_ioctl_dev_info(fs_info, argp);
5576 case BTRFS_IOC_BALANCE:
5577 return btrfs_ioctl_balance(file, NULL);
5578 case BTRFS_IOC_TREE_SEARCH:
5579 return btrfs_ioctl_tree_search(file, argp);
5580 case BTRFS_IOC_TREE_SEARCH_V2:
5581 return btrfs_ioctl_tree_search_v2(file, argp);
5582 case BTRFS_IOC_INO_LOOKUP:
5583 return btrfs_ioctl_ino_lookup(file, argp);
5584 case BTRFS_IOC_INO_PATHS:
5585 return btrfs_ioctl_ino_to_path(root, argp);
5586 case BTRFS_IOC_LOGICAL_INO:
5587 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5588 case BTRFS_IOC_LOGICAL_INO_V2:
5589 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
5590 case BTRFS_IOC_SPACE_INFO:
5591 return btrfs_ioctl_space_info(fs_info, argp);
5592 case BTRFS_IOC_SYNC: {
5595 ret = btrfs_start_delalloc_roots(fs_info, -1);
5598 ret = btrfs_sync_fs(inode->i_sb, 1);
5600 * The transaction thread may want to do more work,
5601 * namely it pokes the cleaner kthread that will start
5602 * processing uncleaned subvols.
5604 wake_up_process(fs_info->transaction_kthread);
5607 case BTRFS_IOC_START_SYNC:
5608 return btrfs_ioctl_start_sync(root, argp);
5609 case BTRFS_IOC_WAIT_SYNC:
5610 return btrfs_ioctl_wait_sync(fs_info, argp);
5611 case BTRFS_IOC_SCRUB:
5612 return btrfs_ioctl_scrub(file, argp);
5613 case BTRFS_IOC_SCRUB_CANCEL:
5614 return btrfs_ioctl_scrub_cancel(fs_info);
5615 case BTRFS_IOC_SCRUB_PROGRESS:
5616 return btrfs_ioctl_scrub_progress(fs_info, argp);
5617 case BTRFS_IOC_BALANCE_V2:
5618 return btrfs_ioctl_balance(file, argp);
5619 case BTRFS_IOC_BALANCE_CTL:
5620 return btrfs_ioctl_balance_ctl(fs_info, arg);
5621 case BTRFS_IOC_BALANCE_PROGRESS:
5622 return btrfs_ioctl_balance_progress(fs_info, argp);
5623 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5624 return btrfs_ioctl_set_received_subvol(file, argp);
5626 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5627 return btrfs_ioctl_set_received_subvol_32(file, argp);
5629 case BTRFS_IOC_SEND:
5630 return _btrfs_ioctl_send(file, argp, false);
5631 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5632 case BTRFS_IOC_SEND_32:
5633 return _btrfs_ioctl_send(file, argp, true);
5635 case BTRFS_IOC_GET_DEV_STATS:
5636 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5637 case BTRFS_IOC_QUOTA_CTL:
5638 return btrfs_ioctl_quota_ctl(file, argp);
5639 case BTRFS_IOC_QGROUP_ASSIGN:
5640 return btrfs_ioctl_qgroup_assign(file, argp);
5641 case BTRFS_IOC_QGROUP_CREATE:
5642 return btrfs_ioctl_qgroup_create(file, argp);
5643 case BTRFS_IOC_QGROUP_LIMIT:
5644 return btrfs_ioctl_qgroup_limit(file, argp);
5645 case BTRFS_IOC_QUOTA_RESCAN:
5646 return btrfs_ioctl_quota_rescan(file, argp);
5647 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5648 return btrfs_ioctl_quota_rescan_status(file, argp);
5649 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5650 return btrfs_ioctl_quota_rescan_wait(file, argp);
5651 case BTRFS_IOC_DEV_REPLACE:
5652 return btrfs_ioctl_dev_replace(fs_info, argp);
5653 case BTRFS_IOC_GET_FSLABEL:
5654 return btrfs_ioctl_get_fslabel(file, argp);
5655 case BTRFS_IOC_SET_FSLABEL:
5656 return btrfs_ioctl_set_fslabel(file, argp);
5657 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5658 return btrfs_ioctl_get_supported_features(argp);
5659 case BTRFS_IOC_GET_FEATURES:
5660 return btrfs_ioctl_get_features(file, argp);
5661 case BTRFS_IOC_SET_FEATURES:
5662 return btrfs_ioctl_set_features(file, argp);
5663 case FS_IOC_FSGETXATTR:
5664 return btrfs_ioctl_fsgetxattr(file, argp);
5665 case FS_IOC_FSSETXATTR:
5666 return btrfs_ioctl_fssetxattr(file, argp);
5667 case BTRFS_IOC_GET_SUBVOL_INFO:
5668 return btrfs_ioctl_get_subvol_info(file, argp);
5669 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5670 return btrfs_ioctl_get_subvol_rootref(file, argp);
5671 case BTRFS_IOC_INO_LOOKUP_USER:
5672 return btrfs_ioctl_ino_lookup_user(file, argp);
5678 #ifdef CONFIG_COMPAT
5679 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5682 * These all access 32-bit values anyway so no further
5683 * handling is necessary.
5686 case FS_IOC32_GETFLAGS:
5687 cmd = FS_IOC_GETFLAGS;
5689 case FS_IOC32_SETFLAGS:
5690 cmd = FS_IOC_SETFLAGS;
5692 case FS_IOC32_GETVERSION:
5693 cmd = FS_IOC_GETVERSION;
5697 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));