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"
46 #include "space-info.h"
47 #include "delalloc-space.h"
50 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
51 * structures are incorrect, as the timespec structure from userspace
52 * is 4 bytes too small. We define these alternatives here to teach
53 * the kernel about the 32-bit struct packing.
55 struct btrfs_ioctl_timespec_32 {
58 } __attribute__ ((__packed__));
60 struct btrfs_ioctl_received_subvol_args_32 {
61 char uuid[BTRFS_UUID_SIZE]; /* in */
62 __u64 stransid; /* in */
63 __u64 rtransid; /* out */
64 struct btrfs_ioctl_timespec_32 stime; /* in */
65 struct btrfs_ioctl_timespec_32 rtime; /* out */
67 __u64 reserved[16]; /* in */
68 } __attribute__ ((__packed__));
70 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
71 struct btrfs_ioctl_received_subvol_args_32)
74 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
75 struct btrfs_ioctl_send_args_32 {
76 __s64 send_fd; /* in */
77 __u64 clone_sources_count; /* in */
78 compat_uptr_t clone_sources; /* in */
79 __u64 parent_root; /* in */
81 __u64 reserved[4]; /* in */
82 } __attribute__ ((__packed__));
84 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
85 struct btrfs_ioctl_send_args_32)
88 static int btrfs_clone(struct inode *src, struct inode *inode,
89 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
92 /* Mask out flags that are inappropriate for the given type of inode. */
93 static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
96 if (S_ISDIR(inode->i_mode))
98 else if (S_ISREG(inode->i_mode))
99 return flags & ~FS_DIRSYNC_FL;
101 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
105 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
108 static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
110 unsigned int iflags = 0;
112 if (flags & BTRFS_INODE_SYNC)
113 iflags |= FS_SYNC_FL;
114 if (flags & BTRFS_INODE_IMMUTABLE)
115 iflags |= FS_IMMUTABLE_FL;
116 if (flags & BTRFS_INODE_APPEND)
117 iflags |= FS_APPEND_FL;
118 if (flags & BTRFS_INODE_NODUMP)
119 iflags |= FS_NODUMP_FL;
120 if (flags & BTRFS_INODE_NOATIME)
121 iflags |= FS_NOATIME_FL;
122 if (flags & BTRFS_INODE_DIRSYNC)
123 iflags |= FS_DIRSYNC_FL;
124 if (flags & BTRFS_INODE_NODATACOW)
125 iflags |= FS_NOCOW_FL;
127 if (flags & BTRFS_INODE_NOCOMPRESS)
128 iflags |= FS_NOCOMP_FL;
129 else if (flags & BTRFS_INODE_COMPRESS)
130 iflags |= FS_COMPR_FL;
136 * Update inode->i_flags based on the btrfs internal flags.
138 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
140 struct btrfs_inode *binode = BTRFS_I(inode);
141 unsigned int new_fl = 0;
143 if (binode->flags & BTRFS_INODE_SYNC)
145 if (binode->flags & BTRFS_INODE_IMMUTABLE)
146 new_fl |= S_IMMUTABLE;
147 if (binode->flags & BTRFS_INODE_APPEND)
149 if (binode->flags & BTRFS_INODE_NOATIME)
151 if (binode->flags & BTRFS_INODE_DIRSYNC)
154 set_mask_bits(&inode->i_flags,
155 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
159 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
161 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
162 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
164 if (copy_to_user(arg, &flags, sizeof(flags)))
169 /* Check if @flags are a supported and valid set of FS_*_FL flags */
170 static int check_fsflags(unsigned int flags)
172 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
173 FS_NOATIME_FL | FS_NODUMP_FL | \
174 FS_SYNC_FL | FS_DIRSYNC_FL | \
175 FS_NOCOMP_FL | FS_COMPR_FL |
179 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
185 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
187 struct inode *inode = file_inode(file);
188 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
189 struct btrfs_inode *binode = BTRFS_I(inode);
190 struct btrfs_root *root = binode->root;
191 struct btrfs_trans_handle *trans;
192 unsigned int fsflags, old_fsflags;
194 const char *comp = NULL;
195 u32 binode_flags = binode->flags;
197 if (!inode_owner_or_capable(inode))
200 if (btrfs_root_readonly(root))
203 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
206 ret = check_fsflags(fsflags);
210 ret = mnt_want_write_file(file);
216 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
217 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
218 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
222 if (fsflags & FS_SYNC_FL)
223 binode_flags |= BTRFS_INODE_SYNC;
225 binode_flags &= ~BTRFS_INODE_SYNC;
226 if (fsflags & FS_IMMUTABLE_FL)
227 binode_flags |= BTRFS_INODE_IMMUTABLE;
229 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
230 if (fsflags & FS_APPEND_FL)
231 binode_flags |= BTRFS_INODE_APPEND;
233 binode_flags &= ~BTRFS_INODE_APPEND;
234 if (fsflags & FS_NODUMP_FL)
235 binode_flags |= BTRFS_INODE_NODUMP;
237 binode_flags &= ~BTRFS_INODE_NODUMP;
238 if (fsflags & FS_NOATIME_FL)
239 binode_flags |= BTRFS_INODE_NOATIME;
241 binode_flags &= ~BTRFS_INODE_NOATIME;
242 if (fsflags & FS_DIRSYNC_FL)
243 binode_flags |= BTRFS_INODE_DIRSYNC;
245 binode_flags &= ~BTRFS_INODE_DIRSYNC;
246 if (fsflags & FS_NOCOW_FL) {
247 if (S_ISREG(inode->i_mode)) {
249 * It's safe to turn csums off here, no extents exist.
250 * Otherwise we want the flag to reflect the real COW
251 * status of the file and will not set it.
253 if (inode->i_size == 0)
254 binode_flags |= BTRFS_INODE_NODATACOW |
255 BTRFS_INODE_NODATASUM;
257 binode_flags |= BTRFS_INODE_NODATACOW;
261 * Revert back under same assumptions as above
263 if (S_ISREG(inode->i_mode)) {
264 if (inode->i_size == 0)
265 binode_flags &= ~(BTRFS_INODE_NODATACOW |
266 BTRFS_INODE_NODATASUM);
268 binode_flags &= ~BTRFS_INODE_NODATACOW;
273 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
274 * flag may be changed automatically if compression code won't make
277 if (fsflags & FS_NOCOMP_FL) {
278 binode_flags &= ~BTRFS_INODE_COMPRESS;
279 binode_flags |= BTRFS_INODE_NOCOMPRESS;
280 } else if (fsflags & FS_COMPR_FL) {
282 if (IS_SWAPFILE(inode)) {
287 binode_flags |= BTRFS_INODE_COMPRESS;
288 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
290 comp = btrfs_compress_type2str(fs_info->compress_type);
291 if (!comp || comp[0] == 0)
292 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
294 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
301 trans = btrfs_start_transaction(root, 3);
303 ret = PTR_ERR(trans);
308 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
311 btrfs_abort_transaction(trans, ret);
315 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
317 if (ret && ret != -ENODATA) {
318 btrfs_abort_transaction(trans, ret);
323 binode->flags = binode_flags;
324 btrfs_sync_inode_flags_to_i_flags(inode);
325 inode_inc_iversion(inode);
326 inode->i_ctime = current_time(inode);
327 ret = btrfs_update_inode(trans, root, inode);
330 btrfs_end_transaction(trans);
333 mnt_drop_write_file(file);
338 * Translate btrfs internal inode flags to xflags as expected by the
339 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
342 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
344 unsigned int xflags = 0;
346 if (flags & BTRFS_INODE_APPEND)
347 xflags |= FS_XFLAG_APPEND;
348 if (flags & BTRFS_INODE_IMMUTABLE)
349 xflags |= FS_XFLAG_IMMUTABLE;
350 if (flags & BTRFS_INODE_NOATIME)
351 xflags |= FS_XFLAG_NOATIME;
352 if (flags & BTRFS_INODE_NODUMP)
353 xflags |= FS_XFLAG_NODUMP;
354 if (flags & BTRFS_INODE_SYNC)
355 xflags |= FS_XFLAG_SYNC;
360 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
361 static int check_xflags(unsigned int flags)
363 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
364 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
370 * Set the xflags from the internal inode flags. The remaining items of fsxattr
373 static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
375 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
378 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
379 if (copy_to_user(arg, &fa, sizeof(fa)))
385 static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
387 struct inode *inode = file_inode(file);
388 struct btrfs_inode *binode = BTRFS_I(inode);
389 struct btrfs_root *root = binode->root;
390 struct btrfs_trans_handle *trans;
391 struct fsxattr fa, old_fa;
393 unsigned old_i_flags;
396 if (!inode_owner_or_capable(inode))
399 if (btrfs_root_readonly(root))
402 if (copy_from_user(&fa, arg, sizeof(fa)))
405 ret = check_xflags(fa.fsx_xflags);
409 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
412 ret = mnt_want_write_file(file);
418 old_flags = binode->flags;
419 old_i_flags = inode->i_flags;
421 simple_fill_fsxattr(&old_fa,
422 btrfs_inode_flags_to_xflags(binode->flags));
423 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
427 if (fa.fsx_xflags & FS_XFLAG_SYNC)
428 binode->flags |= BTRFS_INODE_SYNC;
430 binode->flags &= ~BTRFS_INODE_SYNC;
431 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
432 binode->flags |= BTRFS_INODE_IMMUTABLE;
434 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
435 if (fa.fsx_xflags & FS_XFLAG_APPEND)
436 binode->flags |= BTRFS_INODE_APPEND;
438 binode->flags &= ~BTRFS_INODE_APPEND;
439 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
440 binode->flags |= BTRFS_INODE_NODUMP;
442 binode->flags &= ~BTRFS_INODE_NODUMP;
443 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
444 binode->flags |= BTRFS_INODE_NOATIME;
446 binode->flags &= ~BTRFS_INODE_NOATIME;
448 /* 1 item for the inode */
449 trans = btrfs_start_transaction(root, 1);
451 ret = PTR_ERR(trans);
455 btrfs_sync_inode_flags_to_i_flags(inode);
456 inode_inc_iversion(inode);
457 inode->i_ctime = current_time(inode);
458 ret = btrfs_update_inode(trans, root, inode);
460 btrfs_end_transaction(trans);
464 binode->flags = old_flags;
465 inode->i_flags = old_i_flags;
469 mnt_drop_write_file(file);
474 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
476 struct inode *inode = file_inode(file);
478 return put_user(inode->i_generation, arg);
481 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
483 struct inode *inode = file_inode(file);
484 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
485 struct btrfs_device *device;
486 struct request_queue *q;
487 struct fstrim_range range;
488 u64 minlen = ULLONG_MAX;
492 if (!capable(CAP_SYS_ADMIN))
496 * If the fs is mounted with nologreplay, which requires it to be
497 * mounted in RO mode as well, we can not allow discard on free space
498 * inside block groups, because log trees refer to extents that are not
499 * pinned in a block group's free space cache (pinning the extents is
500 * precisely the first phase of replaying a log tree).
502 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
506 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
510 q = bdev_get_queue(device->bdev);
511 if (blk_queue_discard(q)) {
513 minlen = min_t(u64, q->limits.discard_granularity,
521 if (copy_from_user(&range, arg, sizeof(range)))
525 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
526 * block group is in the logical address space, which can be any
527 * sectorsize aligned bytenr in the range [0, U64_MAX].
529 if (range.len < fs_info->sb->s_blocksize)
532 range.minlen = max(range.minlen, minlen);
533 ret = btrfs_trim_fs(fs_info, &range);
537 if (copy_to_user(arg, &range, sizeof(range)))
543 int btrfs_is_empty_uuid(u8 *uuid)
547 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
554 static noinline int create_subvol(struct inode *dir,
555 struct dentry *dentry,
556 const char *name, int namelen,
558 struct btrfs_qgroup_inherit *inherit)
560 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
561 struct btrfs_trans_handle *trans;
562 struct btrfs_key key;
563 struct btrfs_root_item *root_item;
564 struct btrfs_inode_item *inode_item;
565 struct extent_buffer *leaf;
566 struct btrfs_root *root = BTRFS_I(dir)->root;
567 struct btrfs_root *new_root;
568 struct btrfs_block_rsv block_rsv;
569 struct timespec64 cur_time = current_time(dir);
574 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
578 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
582 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
587 * Don't create subvolume whose level is not zero. Or qgroup will be
588 * screwed up since it assumes subvolume qgroup's level to be 0.
590 if (btrfs_qgroup_level(objectid)) {
595 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
597 * The same as the snapshot creation, please see the comment
598 * of create_snapshot().
600 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
604 trans = btrfs_start_transaction(root, 0);
606 ret = PTR_ERR(trans);
607 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
610 trans->block_rsv = &block_rsv;
611 trans->bytes_reserved = block_rsv.size;
613 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
617 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
623 btrfs_mark_buffer_dirty(leaf);
625 inode_item = &root_item->inode;
626 btrfs_set_stack_inode_generation(inode_item, 1);
627 btrfs_set_stack_inode_size(inode_item, 3);
628 btrfs_set_stack_inode_nlink(inode_item, 1);
629 btrfs_set_stack_inode_nbytes(inode_item,
631 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
633 btrfs_set_root_flags(root_item, 0);
634 btrfs_set_root_limit(root_item, 0);
635 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
637 btrfs_set_root_bytenr(root_item, leaf->start);
638 btrfs_set_root_generation(root_item, trans->transid);
639 btrfs_set_root_level(root_item, 0);
640 btrfs_set_root_refs(root_item, 1);
641 btrfs_set_root_used(root_item, leaf->len);
642 btrfs_set_root_last_snapshot(root_item, 0);
644 btrfs_set_root_generation_v2(root_item,
645 btrfs_root_generation(root_item));
646 uuid_le_gen(&new_uuid);
647 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
648 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
649 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
650 root_item->ctime = root_item->otime;
651 btrfs_set_root_ctransid(root_item, trans->transid);
652 btrfs_set_root_otransid(root_item, trans->transid);
654 btrfs_tree_unlock(leaf);
655 free_extent_buffer(leaf);
658 btrfs_set_root_dirid(root_item, new_dirid);
660 key.objectid = objectid;
662 key.type = BTRFS_ROOT_ITEM_KEY;
663 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
668 key.offset = (u64)-1;
669 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
670 if (IS_ERR(new_root)) {
671 ret = PTR_ERR(new_root);
672 btrfs_abort_transaction(trans, ret);
676 btrfs_record_root_in_trans(trans, new_root);
678 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
680 /* We potentially lose an unused inode item here */
681 btrfs_abort_transaction(trans, ret);
685 mutex_lock(&new_root->objectid_mutex);
686 new_root->highest_objectid = new_dirid;
687 mutex_unlock(&new_root->objectid_mutex);
690 * insert the directory item
692 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
694 btrfs_abort_transaction(trans, ret);
698 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
699 BTRFS_FT_DIR, index);
701 btrfs_abort_transaction(trans, ret);
705 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
706 ret = btrfs_update_inode(trans, root, dir);
709 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
710 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
713 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
714 BTRFS_UUID_KEY_SUBVOL, objectid);
716 btrfs_abort_transaction(trans, ret);
720 trans->block_rsv = NULL;
721 trans->bytes_reserved = 0;
722 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
725 *async_transid = trans->transid;
726 err = btrfs_commit_transaction_async(trans, 1);
728 err = btrfs_commit_transaction(trans);
730 err = btrfs_commit_transaction(trans);
736 inode = btrfs_lookup_dentry(dir, dentry);
738 return PTR_ERR(inode);
739 d_instantiate(dentry, inode);
748 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
749 struct dentry *dentry,
750 u64 *async_transid, bool readonly,
751 struct btrfs_qgroup_inherit *inherit)
753 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
755 struct btrfs_pending_snapshot *pending_snapshot;
756 struct btrfs_trans_handle *trans;
758 bool snapshot_force_cow = false;
760 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
763 if (atomic_read(&root->nr_swapfiles)) {
765 "cannot snapshot subvolume with active swapfile");
769 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
770 if (!pending_snapshot)
773 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
775 pending_snapshot->path = btrfs_alloc_path();
776 if (!pending_snapshot->root_item || !pending_snapshot->path) {
782 * Force new buffered writes to reserve space even when NOCOW is
783 * possible. This is to avoid later writeback (running dealloc) to
784 * fallback to COW mode and unexpectedly fail with ENOSPC.
786 atomic_inc(&root->will_be_snapshotted);
787 smp_mb__after_atomic();
788 /* wait for no snapshot writes */
789 wait_event(root->subv_writers->wait,
790 percpu_counter_sum(&root->subv_writers->counter) == 0);
792 ret = btrfs_start_delalloc_snapshot(root);
797 * All previous writes have started writeback in NOCOW mode, so now
798 * we force future writes to fallback to COW mode during snapshot
801 atomic_inc(&root->snapshot_force_cow);
802 snapshot_force_cow = true;
804 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
806 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
807 BTRFS_BLOCK_RSV_TEMP);
809 * 1 - parent dir inode
812 * 2 - root ref/backref
813 * 1 - root of snapshot
816 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
817 &pending_snapshot->block_rsv, 8,
822 pending_snapshot->dentry = dentry;
823 pending_snapshot->root = root;
824 pending_snapshot->readonly = readonly;
825 pending_snapshot->dir = dir;
826 pending_snapshot->inherit = inherit;
828 trans = btrfs_start_transaction(root, 0);
830 ret = PTR_ERR(trans);
834 spin_lock(&fs_info->trans_lock);
835 list_add(&pending_snapshot->list,
836 &trans->transaction->pending_snapshots);
837 spin_unlock(&fs_info->trans_lock);
839 *async_transid = trans->transid;
840 ret = btrfs_commit_transaction_async(trans, 1);
842 ret = btrfs_commit_transaction(trans);
844 ret = btrfs_commit_transaction(trans);
849 ret = pending_snapshot->error;
853 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
857 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
859 ret = PTR_ERR(inode);
863 d_instantiate(dentry, inode);
866 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
868 if (snapshot_force_cow)
869 atomic_dec(&root->snapshot_force_cow);
870 if (atomic_dec_and_test(&root->will_be_snapshotted))
871 wake_up_var(&root->will_be_snapshotted);
873 kfree(pending_snapshot->root_item);
874 btrfs_free_path(pending_snapshot->path);
875 kfree(pending_snapshot);
880 /* copy of may_delete in fs/namei.c()
881 * Check whether we can remove a link victim from directory dir, check
882 * whether the type of victim is right.
883 * 1. We can't do it if dir is read-only (done in permission())
884 * 2. We should have write and exec permissions on dir
885 * 3. We can't remove anything from append-only dir
886 * 4. We can't do anything with immutable dir (done in permission())
887 * 5. If the sticky bit on dir is set we should either
888 * a. be owner of dir, or
889 * b. be owner of victim, or
890 * c. have CAP_FOWNER capability
891 * 6. If the victim is append-only or immutable we can't do anything with
892 * links pointing to it.
893 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
894 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
895 * 9. We can't remove a root or mountpoint.
896 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
897 * nfs_async_unlink().
900 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
904 if (d_really_is_negative(victim))
907 BUG_ON(d_inode(victim->d_parent) != dir);
908 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
910 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
915 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
916 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
919 if (!d_is_dir(victim))
923 } else if (d_is_dir(victim))
927 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
932 /* copy of may_create in fs/namei.c() */
933 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
935 if (d_really_is_positive(child))
939 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
943 * Create a new subvolume below @parent. This is largely modeled after
944 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
945 * inside this filesystem so it's quite a bit simpler.
947 static noinline int btrfs_mksubvol(const struct path *parent,
948 const char *name, int namelen,
949 struct btrfs_root *snap_src,
950 u64 *async_transid, bool readonly,
951 struct btrfs_qgroup_inherit *inherit)
953 struct inode *dir = d_inode(parent->dentry);
954 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
955 struct dentry *dentry;
958 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
962 dentry = lookup_one_len(name, parent->dentry, namelen);
963 error = PTR_ERR(dentry);
967 error = btrfs_may_create(dir, dentry);
972 * even if this name doesn't exist, we may get hash collisions.
973 * check for them now when we can safely fail
975 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
981 down_read(&fs_info->subvol_sem);
983 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
987 error = create_snapshot(snap_src, dir, dentry,
988 async_transid, readonly, inherit);
990 error = create_subvol(dir, dentry, name, namelen,
991 async_transid, inherit);
994 fsnotify_mkdir(dir, dentry);
996 up_read(&fs_info->subvol_sem);
1005 * When we're defragging a range, we don't want to kick it off again
1006 * if it is really just waiting for delalloc to send it down.
1007 * If we find a nice big extent or delalloc range for the bytes in the
1008 * file you want to defrag, we return 0 to let you know to skip this
1011 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1013 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1014 struct extent_map *em = NULL;
1015 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1018 read_lock(&em_tree->lock);
1019 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1020 read_unlock(&em_tree->lock);
1023 end = extent_map_end(em);
1024 free_extent_map(em);
1025 if (end - offset > thresh)
1028 /* if we already have a nice delalloc here, just stop */
1030 end = count_range_bits(io_tree, &offset, offset + thresh,
1031 thresh, EXTENT_DELALLOC, 1);
1038 * helper function to walk through a file and find extents
1039 * newer than a specific transid, and smaller than thresh.
1041 * This is used by the defragging code to find new and small
1044 static int find_new_extents(struct btrfs_root *root,
1045 struct inode *inode, u64 newer_than,
1046 u64 *off, u32 thresh)
1048 struct btrfs_path *path;
1049 struct btrfs_key min_key;
1050 struct extent_buffer *leaf;
1051 struct btrfs_file_extent_item *extent;
1054 u64 ino = btrfs_ino(BTRFS_I(inode));
1056 path = btrfs_alloc_path();
1060 min_key.objectid = ino;
1061 min_key.type = BTRFS_EXTENT_DATA_KEY;
1062 min_key.offset = *off;
1065 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1069 if (min_key.objectid != ino)
1071 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1074 leaf = path->nodes[0];
1075 extent = btrfs_item_ptr(leaf, path->slots[0],
1076 struct btrfs_file_extent_item);
1078 type = btrfs_file_extent_type(leaf, extent);
1079 if (type == BTRFS_FILE_EXTENT_REG &&
1080 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1081 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1082 *off = min_key.offset;
1083 btrfs_free_path(path);
1088 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1089 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1093 if (min_key.offset == (u64)-1)
1097 btrfs_release_path(path);
1100 btrfs_free_path(path);
1104 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1106 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1107 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1108 struct extent_map *em;
1109 u64 len = PAGE_SIZE;
1112 * hopefully we have this extent in the tree already, try without
1113 * the full extent lock
1115 read_lock(&em_tree->lock);
1116 em = lookup_extent_mapping(em_tree, start, len);
1117 read_unlock(&em_tree->lock);
1120 struct extent_state *cached = NULL;
1121 u64 end = start + len - 1;
1123 /* get the big lock and read metadata off disk */
1124 lock_extent_bits(io_tree, start, end, &cached);
1125 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
1126 unlock_extent_cached(io_tree, start, end, &cached);
1135 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1137 struct extent_map *next;
1140 /* this is the last extent */
1141 if (em->start + em->len >= i_size_read(inode))
1144 next = defrag_lookup_extent(inode, em->start + em->len);
1145 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1147 else if ((em->block_start + em->block_len == next->block_start) &&
1148 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1151 free_extent_map(next);
1155 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1156 u64 *last_len, u64 *skip, u64 *defrag_end,
1159 struct extent_map *em;
1161 bool next_mergeable = true;
1162 bool prev_mergeable = true;
1165 * make sure that once we start defragging an extent, we keep on
1168 if (start < *defrag_end)
1173 em = defrag_lookup_extent(inode, start);
1177 /* this will cover holes, and inline extents */
1178 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1184 prev_mergeable = false;
1186 next_mergeable = defrag_check_next_extent(inode, em);
1188 * we hit a real extent, if it is big or the next extent is not a
1189 * real extent, don't bother defragging it
1191 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1192 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1196 * last_len ends up being a counter of how many bytes we've defragged.
1197 * every time we choose not to defrag an extent, we reset *last_len
1198 * so that the next tiny extent will force a defrag.
1200 * The end result of this is that tiny extents before a single big
1201 * extent will force at least part of that big extent to be defragged.
1204 *defrag_end = extent_map_end(em);
1207 *skip = extent_map_end(em);
1211 free_extent_map(em);
1216 * it doesn't do much good to defrag one or two pages
1217 * at a time. This pulls in a nice chunk of pages
1218 * to COW and defrag.
1220 * It also makes sure the delalloc code has enough
1221 * dirty data to avoid making new small extents as part
1224 * It's a good idea to start RA on this range
1225 * before calling this.
1227 static int cluster_pages_for_defrag(struct inode *inode,
1228 struct page **pages,
1229 unsigned long start_index,
1230 unsigned long num_pages)
1232 unsigned long file_end;
1233 u64 isize = i_size_read(inode);
1240 struct btrfs_ordered_extent *ordered;
1241 struct extent_state *cached_state = NULL;
1242 struct extent_io_tree *tree;
1243 struct extent_changeset *data_reserved = NULL;
1244 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1246 file_end = (isize - 1) >> PAGE_SHIFT;
1247 if (!isize || start_index > file_end)
1250 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1252 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
1253 start_index << PAGE_SHIFT,
1254 page_cnt << PAGE_SHIFT);
1258 tree = &BTRFS_I(inode)->io_tree;
1260 /* step one, lock all the pages */
1261 for (i = 0; i < page_cnt; i++) {
1264 page = find_or_create_page(inode->i_mapping,
1265 start_index + i, mask);
1269 page_start = page_offset(page);
1270 page_end = page_start + PAGE_SIZE - 1;
1272 lock_extent_bits(tree, page_start, page_end,
1274 ordered = btrfs_lookup_ordered_extent(inode,
1276 unlock_extent_cached(tree, page_start, page_end,
1282 btrfs_start_ordered_extent(inode, ordered, 1);
1283 btrfs_put_ordered_extent(ordered);
1286 * we unlocked the page above, so we need check if
1287 * it was released or not.
1289 if (page->mapping != inode->i_mapping) {
1296 if (!PageUptodate(page)) {
1297 btrfs_readpage(NULL, page);
1299 if (!PageUptodate(page)) {
1307 if (page->mapping != inode->i_mapping) {
1319 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1323 * so now we have a nice long stream of locked
1324 * and up to date pages, lets wait on them
1326 for (i = 0; i < i_done; i++)
1327 wait_on_page_writeback(pages[i]);
1329 page_start = page_offset(pages[0]);
1330 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1332 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1333 page_start, page_end - 1, &cached_state);
1334 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1335 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1336 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1339 if (i_done != page_cnt) {
1340 spin_lock(&BTRFS_I(inode)->lock);
1341 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1342 spin_unlock(&BTRFS_I(inode)->lock);
1343 btrfs_delalloc_release_space(inode, data_reserved,
1344 start_index << PAGE_SHIFT,
1345 (page_cnt - i_done) << PAGE_SHIFT, true);
1349 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1352 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1353 page_start, page_end - 1, &cached_state);
1355 for (i = 0; i < i_done; i++) {
1356 clear_page_dirty_for_io(pages[i]);
1357 ClearPageChecked(pages[i]);
1358 set_page_extent_mapped(pages[i]);
1359 set_page_dirty(pages[i]);
1360 unlock_page(pages[i]);
1363 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1365 extent_changeset_free(data_reserved);
1368 for (i = 0; i < i_done; i++) {
1369 unlock_page(pages[i]);
1372 btrfs_delalloc_release_space(inode, data_reserved,
1373 start_index << PAGE_SHIFT,
1374 page_cnt << PAGE_SHIFT, true);
1375 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1377 extent_changeset_free(data_reserved);
1382 int btrfs_defrag_file(struct inode *inode, struct file *file,
1383 struct btrfs_ioctl_defrag_range_args *range,
1384 u64 newer_than, unsigned long max_to_defrag)
1386 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1387 struct btrfs_root *root = BTRFS_I(inode)->root;
1388 struct file_ra_state *ra = NULL;
1389 unsigned long last_index;
1390 u64 isize = i_size_read(inode);
1394 u64 newer_off = range->start;
1396 unsigned long ra_index = 0;
1398 int defrag_count = 0;
1399 int compress_type = BTRFS_COMPRESS_ZLIB;
1400 u32 extent_thresh = range->extent_thresh;
1401 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1402 unsigned long cluster = max_cluster;
1403 u64 new_align = ~((u64)SZ_128K - 1);
1404 struct page **pages = NULL;
1405 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1410 if (range->start >= isize)
1414 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1416 if (range->compress_type)
1417 compress_type = range->compress_type;
1420 if (extent_thresh == 0)
1421 extent_thresh = SZ_256K;
1424 * If we were not given a file, allocate a readahead context. As
1425 * readahead is just an optimization, defrag will work without it so
1426 * we don't error out.
1429 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1431 file_ra_state_init(ra, inode->i_mapping);
1436 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1442 /* find the last page to defrag */
1443 if (range->start + range->len > range->start) {
1444 last_index = min_t(u64, isize - 1,
1445 range->start + range->len - 1) >> PAGE_SHIFT;
1447 last_index = (isize - 1) >> PAGE_SHIFT;
1451 ret = find_new_extents(root, inode, newer_than,
1452 &newer_off, SZ_64K);
1454 range->start = newer_off;
1456 * we always align our defrag to help keep
1457 * the extents in the file evenly spaced
1459 i = (newer_off & new_align) >> PAGE_SHIFT;
1463 i = range->start >> PAGE_SHIFT;
1466 max_to_defrag = last_index - i + 1;
1469 * make writeback starts from i, so the defrag range can be
1470 * written sequentially.
1472 if (i < inode->i_mapping->writeback_index)
1473 inode->i_mapping->writeback_index = i;
1475 while (i <= last_index && defrag_count < max_to_defrag &&
1476 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1478 * make sure we stop running if someone unmounts
1481 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1484 if (btrfs_defrag_cancelled(fs_info)) {
1485 btrfs_debug(fs_info, "defrag_file cancelled");
1490 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1491 extent_thresh, &last_len, &skip,
1492 &defrag_end, do_compress)){
1495 * the should_defrag function tells us how much to skip
1496 * bump our counter by the suggested amount
1498 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1499 i = max(i + 1, next);
1504 cluster = (PAGE_ALIGN(defrag_end) >>
1506 cluster = min(cluster, max_cluster);
1508 cluster = max_cluster;
1511 if (i + cluster > ra_index) {
1512 ra_index = max(i, ra_index);
1514 page_cache_sync_readahead(inode->i_mapping, ra,
1515 file, ra_index, cluster);
1516 ra_index += cluster;
1520 if (IS_SWAPFILE(inode)) {
1524 BTRFS_I(inode)->defrag_compress = compress_type;
1525 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1528 inode_unlock(inode);
1532 defrag_count += ret;
1533 balance_dirty_pages_ratelimited(inode->i_mapping);
1534 inode_unlock(inode);
1537 if (newer_off == (u64)-1)
1543 newer_off = max(newer_off + 1,
1544 (u64)i << PAGE_SHIFT);
1546 ret = find_new_extents(root, inode, newer_than,
1547 &newer_off, SZ_64K);
1549 range->start = newer_off;
1550 i = (newer_off & new_align) >> PAGE_SHIFT;
1557 last_len += ret << PAGE_SHIFT;
1565 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1566 filemap_flush(inode->i_mapping);
1567 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1568 &BTRFS_I(inode)->runtime_flags))
1569 filemap_flush(inode->i_mapping);
1572 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1573 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1574 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1575 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1583 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1584 inode_unlock(inode);
1592 static noinline int btrfs_ioctl_resize(struct file *file,
1595 struct inode *inode = file_inode(file);
1596 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1600 struct btrfs_root *root = BTRFS_I(inode)->root;
1601 struct btrfs_ioctl_vol_args *vol_args;
1602 struct btrfs_trans_handle *trans;
1603 struct btrfs_device *device = NULL;
1606 char *devstr = NULL;
1610 if (!capable(CAP_SYS_ADMIN))
1613 ret = mnt_want_write_file(file);
1617 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
1618 mnt_drop_write_file(file);
1619 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1622 vol_args = memdup_user(arg, sizeof(*vol_args));
1623 if (IS_ERR(vol_args)) {
1624 ret = PTR_ERR(vol_args);
1628 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1630 sizestr = vol_args->name;
1631 devstr = strchr(sizestr, ':');
1633 sizestr = devstr + 1;
1635 devstr = vol_args->name;
1636 ret = kstrtoull(devstr, 10, &devid);
1643 btrfs_info(fs_info, "resizing devid %llu", devid);
1646 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
1648 btrfs_info(fs_info, "resizer unable to find device %llu",
1654 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1656 "resizer unable to apply on readonly device %llu",
1662 if (!strcmp(sizestr, "max"))
1663 new_size = device->bdev->bd_inode->i_size;
1665 if (sizestr[0] == '-') {
1668 } else if (sizestr[0] == '+') {
1672 new_size = memparse(sizestr, &retptr);
1673 if (*retptr != '\0' || new_size == 0) {
1679 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1684 old_size = btrfs_device_get_total_bytes(device);
1687 if (new_size > old_size) {
1691 new_size = old_size - new_size;
1692 } else if (mod > 0) {
1693 if (new_size > ULLONG_MAX - old_size) {
1697 new_size = old_size + new_size;
1700 if (new_size < SZ_256M) {
1704 if (new_size > device->bdev->bd_inode->i_size) {
1709 new_size = round_down(new_size, fs_info->sectorsize);
1711 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1712 rcu_str_deref(device->name), new_size);
1714 if (new_size > old_size) {
1715 trans = btrfs_start_transaction(root, 0);
1716 if (IS_ERR(trans)) {
1717 ret = PTR_ERR(trans);
1720 ret = btrfs_grow_device(trans, device, new_size);
1721 btrfs_commit_transaction(trans);
1722 } else if (new_size < old_size) {
1723 ret = btrfs_shrink_device(device, new_size);
1724 } /* equal, nothing need to do */
1729 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
1730 mnt_drop_write_file(file);
1734 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1735 const char *name, unsigned long fd, int subvol,
1736 u64 *transid, bool readonly,
1737 struct btrfs_qgroup_inherit *inherit)
1742 if (!S_ISDIR(file_inode(file)->i_mode))
1745 ret = mnt_want_write_file(file);
1749 namelen = strlen(name);
1750 if (strchr(name, '/')) {
1752 goto out_drop_write;
1755 if (name[0] == '.' &&
1756 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1758 goto out_drop_write;
1762 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1763 NULL, transid, readonly, inherit);
1765 struct fd src = fdget(fd);
1766 struct inode *src_inode;
1769 goto out_drop_write;
1772 src_inode = file_inode(src.file);
1773 if (src_inode->i_sb != file_inode(file)->i_sb) {
1774 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1775 "Snapshot src from another FS");
1777 } else if (!inode_owner_or_capable(src_inode)) {
1779 * Subvolume creation is not restricted, but snapshots
1780 * are limited to own subvolumes only
1784 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1785 BTRFS_I(src_inode)->root,
1786 transid, readonly, inherit);
1791 mnt_drop_write_file(file);
1796 static noinline int btrfs_ioctl_snap_create(struct file *file,
1797 void __user *arg, int subvol)
1799 struct btrfs_ioctl_vol_args *vol_args;
1802 if (!S_ISDIR(file_inode(file)->i_mode))
1805 vol_args = memdup_user(arg, sizeof(*vol_args));
1806 if (IS_ERR(vol_args))
1807 return PTR_ERR(vol_args);
1808 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1810 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1811 vol_args->fd, subvol,
1818 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1819 void __user *arg, int subvol)
1821 struct btrfs_ioctl_vol_args_v2 *vol_args;
1825 bool readonly = false;
1826 struct btrfs_qgroup_inherit *inherit = NULL;
1828 if (!S_ISDIR(file_inode(file)->i_mode))
1831 vol_args = memdup_user(arg, sizeof(*vol_args));
1832 if (IS_ERR(vol_args))
1833 return PTR_ERR(vol_args);
1834 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1836 if (vol_args->flags &
1837 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1838 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1843 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1845 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1847 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1848 if (vol_args->size > PAGE_SIZE) {
1852 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1853 if (IS_ERR(inherit)) {
1854 ret = PTR_ERR(inherit);
1859 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1860 vol_args->fd, subvol, ptr,
1865 if (ptr && copy_to_user(arg +
1866 offsetof(struct btrfs_ioctl_vol_args_v2,
1878 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1881 struct inode *inode = file_inode(file);
1882 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1883 struct btrfs_root *root = BTRFS_I(inode)->root;
1887 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1890 down_read(&fs_info->subvol_sem);
1891 if (btrfs_root_readonly(root))
1892 flags |= BTRFS_SUBVOL_RDONLY;
1893 up_read(&fs_info->subvol_sem);
1895 if (copy_to_user(arg, &flags, sizeof(flags)))
1901 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1904 struct inode *inode = file_inode(file);
1905 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1906 struct btrfs_root *root = BTRFS_I(inode)->root;
1907 struct btrfs_trans_handle *trans;
1912 if (!inode_owner_or_capable(inode))
1915 ret = mnt_want_write_file(file);
1919 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1921 goto out_drop_write;
1924 if (copy_from_user(&flags, arg, sizeof(flags))) {
1926 goto out_drop_write;
1929 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1931 goto out_drop_write;
1934 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1936 goto out_drop_write;
1939 down_write(&fs_info->subvol_sem);
1942 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1945 root_flags = btrfs_root_flags(&root->root_item);
1946 if (flags & BTRFS_SUBVOL_RDONLY) {
1947 btrfs_set_root_flags(&root->root_item,
1948 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1951 * Block RO -> RW transition if this subvolume is involved in
1954 spin_lock(&root->root_item_lock);
1955 if (root->send_in_progress == 0) {
1956 btrfs_set_root_flags(&root->root_item,
1957 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1958 spin_unlock(&root->root_item_lock);
1960 spin_unlock(&root->root_item_lock);
1962 "Attempt to set subvolume %llu read-write during send",
1963 root->root_key.objectid);
1969 trans = btrfs_start_transaction(root, 1);
1970 if (IS_ERR(trans)) {
1971 ret = PTR_ERR(trans);
1975 ret = btrfs_update_root(trans, fs_info->tree_root,
1976 &root->root_key, &root->root_item);
1978 btrfs_end_transaction(trans);
1982 ret = btrfs_commit_transaction(trans);
1986 btrfs_set_root_flags(&root->root_item, root_flags);
1988 up_write(&fs_info->subvol_sem);
1990 mnt_drop_write_file(file);
1995 static noinline int key_in_sk(struct btrfs_key *key,
1996 struct btrfs_ioctl_search_key *sk)
1998 struct btrfs_key test;
2001 test.objectid = sk->min_objectid;
2002 test.type = sk->min_type;
2003 test.offset = sk->min_offset;
2005 ret = btrfs_comp_cpu_keys(key, &test);
2009 test.objectid = sk->max_objectid;
2010 test.type = sk->max_type;
2011 test.offset = sk->max_offset;
2013 ret = btrfs_comp_cpu_keys(key, &test);
2019 static noinline int copy_to_sk(struct btrfs_path *path,
2020 struct btrfs_key *key,
2021 struct btrfs_ioctl_search_key *sk,
2024 unsigned long *sk_offset,
2028 struct extent_buffer *leaf;
2029 struct btrfs_ioctl_search_header sh;
2030 struct btrfs_key test;
2031 unsigned long item_off;
2032 unsigned long item_len;
2038 leaf = path->nodes[0];
2039 slot = path->slots[0];
2040 nritems = btrfs_header_nritems(leaf);
2042 if (btrfs_header_generation(leaf) > sk->max_transid) {
2046 found_transid = btrfs_header_generation(leaf);
2048 for (i = slot; i < nritems; i++) {
2049 item_off = btrfs_item_ptr_offset(leaf, i);
2050 item_len = btrfs_item_size_nr(leaf, i);
2052 btrfs_item_key_to_cpu(leaf, key, i);
2053 if (!key_in_sk(key, sk))
2056 if (sizeof(sh) + item_len > *buf_size) {
2063 * return one empty item back for v1, which does not
2067 *buf_size = sizeof(sh) + item_len;
2072 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2077 sh.objectid = key->objectid;
2078 sh.offset = key->offset;
2079 sh.type = key->type;
2081 sh.transid = found_transid;
2083 /* copy search result header */
2084 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2089 *sk_offset += sizeof(sh);
2092 char __user *up = ubuf + *sk_offset;
2094 if (read_extent_buffer_to_user(leaf, up,
2095 item_off, item_len)) {
2100 *sk_offset += item_len;
2104 if (ret) /* -EOVERFLOW from above */
2107 if (*num_found >= sk->nr_items) {
2114 test.objectid = sk->max_objectid;
2115 test.type = sk->max_type;
2116 test.offset = sk->max_offset;
2117 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2119 else if (key->offset < (u64)-1)
2121 else if (key->type < (u8)-1) {
2124 } else if (key->objectid < (u64)-1) {
2132 * 0: all items from this leaf copied, continue with next
2133 * 1: * more items can be copied, but unused buffer is too small
2134 * * all items were found
2135 * Either way, it will stops the loop which iterates to the next
2137 * -EOVERFLOW: item was to large for buffer
2138 * -EFAULT: could not copy extent buffer back to userspace
2143 static noinline int search_ioctl(struct inode *inode,
2144 struct btrfs_ioctl_search_key *sk,
2148 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2149 struct btrfs_root *root;
2150 struct btrfs_key key;
2151 struct btrfs_path *path;
2154 unsigned long sk_offset = 0;
2156 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2157 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2161 path = btrfs_alloc_path();
2165 if (sk->tree_id == 0) {
2166 /* search the root of the inode that was passed */
2167 root = BTRFS_I(inode)->root;
2169 key.objectid = sk->tree_id;
2170 key.type = BTRFS_ROOT_ITEM_KEY;
2171 key.offset = (u64)-1;
2172 root = btrfs_read_fs_root_no_name(info, &key);
2174 btrfs_free_path(path);
2175 return PTR_ERR(root);
2179 key.objectid = sk->min_objectid;
2180 key.type = sk->min_type;
2181 key.offset = sk->min_offset;
2184 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2190 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2191 &sk_offset, &num_found);
2192 btrfs_release_path(path);
2200 sk->nr_items = num_found;
2201 btrfs_free_path(path);
2205 static noinline int btrfs_ioctl_tree_search(struct file *file,
2208 struct btrfs_ioctl_search_args __user *uargs;
2209 struct btrfs_ioctl_search_key sk;
2210 struct inode *inode;
2214 if (!capable(CAP_SYS_ADMIN))
2217 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2219 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2222 buf_size = sizeof(uargs->buf);
2224 inode = file_inode(file);
2225 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2228 * In the origin implementation an overflow is handled by returning a
2229 * search header with a len of zero, so reset ret.
2231 if (ret == -EOVERFLOW)
2234 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2239 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2242 struct btrfs_ioctl_search_args_v2 __user *uarg;
2243 struct btrfs_ioctl_search_args_v2 args;
2244 struct inode *inode;
2247 const size_t buf_limit = SZ_16M;
2249 if (!capable(CAP_SYS_ADMIN))
2252 /* copy search header and buffer size */
2253 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2254 if (copy_from_user(&args, uarg, sizeof(args)))
2257 buf_size = args.buf_size;
2259 /* limit result size to 16MB */
2260 if (buf_size > buf_limit)
2261 buf_size = buf_limit;
2263 inode = file_inode(file);
2264 ret = search_ioctl(inode, &args.key, &buf_size,
2265 (char __user *)(&uarg->buf[0]));
2266 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2268 else if (ret == -EOVERFLOW &&
2269 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2276 * Search INODE_REFs to identify path name of 'dirid' directory
2277 * in a 'tree_id' tree. and sets path name to 'name'.
2279 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2280 u64 tree_id, u64 dirid, char *name)
2282 struct btrfs_root *root;
2283 struct btrfs_key key;
2289 struct btrfs_inode_ref *iref;
2290 struct extent_buffer *l;
2291 struct btrfs_path *path;
2293 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2298 path = btrfs_alloc_path();
2302 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2304 key.objectid = tree_id;
2305 key.type = BTRFS_ROOT_ITEM_KEY;
2306 key.offset = (u64)-1;
2307 root = btrfs_read_fs_root_no_name(info, &key);
2309 ret = PTR_ERR(root);
2313 key.objectid = dirid;
2314 key.type = BTRFS_INODE_REF_KEY;
2315 key.offset = (u64)-1;
2318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2322 ret = btrfs_previous_item(root, path, dirid,
2323 BTRFS_INODE_REF_KEY);
2333 slot = path->slots[0];
2334 btrfs_item_key_to_cpu(l, &key, slot);
2336 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2337 len = btrfs_inode_ref_name_len(l, iref);
2339 total_len += len + 1;
2341 ret = -ENAMETOOLONG;
2346 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2348 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2351 btrfs_release_path(path);
2352 key.objectid = key.offset;
2353 key.offset = (u64)-1;
2354 dirid = key.objectid;
2356 memmove(name, ptr, total_len);
2357 name[total_len] = '\0';
2360 btrfs_free_path(path);
2364 static int btrfs_search_path_in_tree_user(struct inode *inode,
2365 struct btrfs_ioctl_ino_lookup_user_args *args)
2367 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2368 struct super_block *sb = inode->i_sb;
2369 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2370 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2371 u64 dirid = args->dirid;
2372 unsigned long item_off;
2373 unsigned long item_len;
2374 struct btrfs_inode_ref *iref;
2375 struct btrfs_root_ref *rref;
2376 struct btrfs_root *root;
2377 struct btrfs_path *path;
2378 struct btrfs_key key, key2;
2379 struct extent_buffer *leaf;
2380 struct inode *temp_inode;
2387 path = btrfs_alloc_path();
2392 * If the bottom subvolume does not exist directly under upper_limit,
2393 * construct the path in from the bottom up.
2395 if (dirid != upper_limit.objectid) {
2396 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2398 key.objectid = treeid;
2399 key.type = BTRFS_ROOT_ITEM_KEY;
2400 key.offset = (u64)-1;
2401 root = btrfs_read_fs_root_no_name(fs_info, &key);
2403 ret = PTR_ERR(root);
2407 key.objectid = dirid;
2408 key.type = BTRFS_INODE_REF_KEY;
2409 key.offset = (u64)-1;
2411 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2414 } else if (ret > 0) {
2415 ret = btrfs_previous_item(root, path, dirid,
2416 BTRFS_INODE_REF_KEY);
2419 } else if (ret > 0) {
2425 leaf = path->nodes[0];
2426 slot = path->slots[0];
2427 btrfs_item_key_to_cpu(leaf, &key, slot);
2429 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2430 len = btrfs_inode_ref_name_len(leaf, iref);
2432 total_len += len + 1;
2433 if (ptr < args->path) {
2434 ret = -ENAMETOOLONG;
2439 read_extent_buffer(leaf, ptr,
2440 (unsigned long)(iref + 1), len);
2442 /* Check the read+exec permission of this directory */
2443 ret = btrfs_previous_item(root, path, dirid,
2444 BTRFS_INODE_ITEM_KEY);
2447 } else if (ret > 0) {
2452 leaf = path->nodes[0];
2453 slot = path->slots[0];
2454 btrfs_item_key_to_cpu(leaf, &key2, slot);
2455 if (key2.objectid != dirid) {
2460 temp_inode = btrfs_iget(sb, &key2, root, NULL);
2461 if (IS_ERR(temp_inode)) {
2462 ret = PTR_ERR(temp_inode);
2465 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2472 if (key.offset == upper_limit.objectid)
2474 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2479 btrfs_release_path(path);
2480 key.objectid = key.offset;
2481 key.offset = (u64)-1;
2482 dirid = key.objectid;
2485 memmove(args->path, ptr, total_len);
2486 args->path[total_len] = '\0';
2487 btrfs_release_path(path);
2490 /* Get the bottom subvolume's name from ROOT_REF */
2491 root = fs_info->tree_root;
2492 key.objectid = treeid;
2493 key.type = BTRFS_ROOT_REF_KEY;
2494 key.offset = args->treeid;
2495 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2498 } else if (ret > 0) {
2503 leaf = path->nodes[0];
2504 slot = path->slots[0];
2505 btrfs_item_key_to_cpu(leaf, &key, slot);
2507 item_off = btrfs_item_ptr_offset(leaf, slot);
2508 item_len = btrfs_item_size_nr(leaf, slot);
2509 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2510 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2511 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2516 /* Copy subvolume's name */
2517 item_off += sizeof(struct btrfs_root_ref);
2518 item_len -= sizeof(struct btrfs_root_ref);
2519 read_extent_buffer(leaf, args->name, item_off, item_len);
2520 args->name[item_len] = 0;
2523 btrfs_free_path(path);
2527 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2530 struct btrfs_ioctl_ino_lookup_args *args;
2531 struct inode *inode;
2534 args = memdup_user(argp, sizeof(*args));
2536 return PTR_ERR(args);
2538 inode = file_inode(file);
2541 * Unprivileged query to obtain the containing subvolume root id. The
2542 * path is reset so it's consistent with btrfs_search_path_in_tree.
2544 if (args->treeid == 0)
2545 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2547 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2552 if (!capable(CAP_SYS_ADMIN)) {
2557 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2558 args->treeid, args->objectid,
2562 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2570 * Version of ino_lookup ioctl (unprivileged)
2572 * The main differences from ino_lookup ioctl are:
2574 * 1. Read + Exec permission will be checked using inode_permission() during
2575 * path construction. -EACCES will be returned in case of failure.
2576 * 2. Path construction will be stopped at the inode number which corresponds
2577 * to the fd with which this ioctl is called. If constructed path does not
2578 * exist under fd's inode, -EACCES will be returned.
2579 * 3. The name of bottom subvolume is also searched and filled.
2581 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2583 struct btrfs_ioctl_ino_lookup_user_args *args;
2584 struct inode *inode;
2587 args = memdup_user(argp, sizeof(*args));
2589 return PTR_ERR(args);
2591 inode = file_inode(file);
2593 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2594 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2596 * The subvolume does not exist under fd with which this is
2603 ret = btrfs_search_path_in_tree_user(inode, args);
2605 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2612 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2613 static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2615 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2616 struct btrfs_fs_info *fs_info;
2617 struct btrfs_root *root;
2618 struct btrfs_path *path;
2619 struct btrfs_key key;
2620 struct btrfs_root_item *root_item;
2621 struct btrfs_root_ref *rref;
2622 struct extent_buffer *leaf;
2623 unsigned long item_off;
2624 unsigned long item_len;
2625 struct inode *inode;
2629 path = btrfs_alloc_path();
2633 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2635 btrfs_free_path(path);
2639 inode = file_inode(file);
2640 fs_info = BTRFS_I(inode)->root->fs_info;
2642 /* Get root_item of inode's subvolume */
2643 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2644 key.type = BTRFS_ROOT_ITEM_KEY;
2645 key.offset = (u64)-1;
2646 root = btrfs_read_fs_root_no_name(fs_info, &key);
2648 ret = PTR_ERR(root);
2651 root_item = &root->root_item;
2653 subvol_info->treeid = key.objectid;
2655 subvol_info->generation = btrfs_root_generation(root_item);
2656 subvol_info->flags = btrfs_root_flags(root_item);
2658 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2659 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2661 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2664 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2665 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2666 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2668 subvol_info->otransid = btrfs_root_otransid(root_item);
2669 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2670 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2672 subvol_info->stransid = btrfs_root_stransid(root_item);
2673 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2674 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2676 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2677 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2678 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2680 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2681 /* Search root tree for ROOT_BACKREF of this subvolume */
2682 root = fs_info->tree_root;
2684 key.type = BTRFS_ROOT_BACKREF_KEY;
2686 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2689 } else if (path->slots[0] >=
2690 btrfs_header_nritems(path->nodes[0])) {
2691 ret = btrfs_next_leaf(root, path);
2694 } else if (ret > 0) {
2700 leaf = path->nodes[0];
2701 slot = path->slots[0];
2702 btrfs_item_key_to_cpu(leaf, &key, slot);
2703 if (key.objectid == subvol_info->treeid &&
2704 key.type == BTRFS_ROOT_BACKREF_KEY) {
2705 subvol_info->parent_id = key.offset;
2707 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2708 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2710 item_off = btrfs_item_ptr_offset(leaf, slot)
2711 + sizeof(struct btrfs_root_ref);
2712 item_len = btrfs_item_size_nr(leaf, slot)
2713 - sizeof(struct btrfs_root_ref);
2714 read_extent_buffer(leaf, subvol_info->name,
2715 item_off, item_len);
2722 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2726 btrfs_free_path(path);
2727 kzfree(subvol_info);
2732 * Return ROOT_REF information of the subvolume containing this inode
2733 * except the subvolume name.
2735 static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2737 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2738 struct btrfs_root_ref *rref;
2739 struct btrfs_root *root;
2740 struct btrfs_path *path;
2741 struct btrfs_key key;
2742 struct extent_buffer *leaf;
2743 struct inode *inode;
2749 path = btrfs_alloc_path();
2753 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2754 if (IS_ERR(rootrefs)) {
2755 btrfs_free_path(path);
2756 return PTR_ERR(rootrefs);
2759 inode = file_inode(file);
2760 root = BTRFS_I(inode)->root->fs_info->tree_root;
2761 objectid = BTRFS_I(inode)->root->root_key.objectid;
2763 key.objectid = objectid;
2764 key.type = BTRFS_ROOT_REF_KEY;
2765 key.offset = rootrefs->min_treeid;
2768 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2771 } else if (path->slots[0] >=
2772 btrfs_header_nritems(path->nodes[0])) {
2773 ret = btrfs_next_leaf(root, path);
2776 } else if (ret > 0) {
2782 leaf = path->nodes[0];
2783 slot = path->slots[0];
2785 btrfs_item_key_to_cpu(leaf, &key, slot);
2786 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2791 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2796 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2797 rootrefs->rootref[found].treeid = key.offset;
2798 rootrefs->rootref[found].dirid =
2799 btrfs_root_ref_dirid(leaf, rref);
2802 ret = btrfs_next_item(root, path);
2805 } else if (ret > 0) {
2812 if (!ret || ret == -EOVERFLOW) {
2813 rootrefs->num_items = found;
2814 /* update min_treeid for next search */
2816 rootrefs->min_treeid =
2817 rootrefs->rootref[found - 1].treeid + 1;
2818 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2823 btrfs_free_path(path);
2828 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2831 struct dentry *parent = file->f_path.dentry;
2832 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2833 struct dentry *dentry;
2834 struct inode *dir = d_inode(parent);
2835 struct inode *inode;
2836 struct btrfs_root *root = BTRFS_I(dir)->root;
2837 struct btrfs_root *dest = NULL;
2838 struct btrfs_ioctl_vol_args *vol_args;
2842 if (!S_ISDIR(dir->i_mode))
2845 vol_args = memdup_user(arg, sizeof(*vol_args));
2846 if (IS_ERR(vol_args))
2847 return PTR_ERR(vol_args);
2849 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2850 namelen = strlen(vol_args->name);
2851 if (strchr(vol_args->name, '/') ||
2852 strncmp(vol_args->name, "..", namelen) == 0) {
2857 err = mnt_want_write_file(file);
2862 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2864 goto out_drop_write;
2865 dentry = lookup_one_len(vol_args->name, parent, namelen);
2866 if (IS_ERR(dentry)) {
2867 err = PTR_ERR(dentry);
2868 goto out_unlock_dir;
2871 if (d_really_is_negative(dentry)) {
2876 inode = d_inode(dentry);
2877 dest = BTRFS_I(inode)->root;
2878 if (!capable(CAP_SYS_ADMIN)) {
2880 * Regular user. Only allow this with a special mount
2881 * option, when the user has write+exec access to the
2882 * subvol root, and when rmdir(2) would have been
2885 * Note that this is _not_ check that the subvol is
2886 * empty or doesn't contain data that we wouldn't
2887 * otherwise be able to delete.
2889 * Users who want to delete empty subvols should try
2893 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
2897 * Do not allow deletion if the parent dir is the same
2898 * as the dir to be deleted. That means the ioctl
2899 * must be called on the dentry referencing the root
2900 * of the subvol, not a random directory contained
2907 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2912 /* check if subvolume may be deleted by a user */
2913 err = btrfs_may_delete(dir, dentry, 1);
2917 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2923 err = btrfs_delete_subvolume(dir, dentry);
2924 inode_unlock(inode);
2926 fsnotify_rmdir(dir, dentry);
2935 mnt_drop_write_file(file);
2941 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2943 struct inode *inode = file_inode(file);
2944 struct btrfs_root *root = BTRFS_I(inode)->root;
2945 struct btrfs_ioctl_defrag_range_args *range;
2948 ret = mnt_want_write_file(file);
2952 if (btrfs_root_readonly(root)) {
2957 switch (inode->i_mode & S_IFMT) {
2959 if (!capable(CAP_SYS_ADMIN)) {
2963 ret = btrfs_defrag_root(root);
2967 * Note that this does not check the file descriptor for write
2968 * access. This prevents defragmenting executables that are
2969 * running and allows defrag on files open in read-only mode.
2971 if (!capable(CAP_SYS_ADMIN) &&
2972 inode_permission(inode, MAY_WRITE)) {
2977 range = kzalloc(sizeof(*range), GFP_KERNEL);
2984 if (copy_from_user(range, argp,
2990 /* compression requires us to start the IO */
2991 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2992 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2993 range->extent_thresh = (u32)-1;
2996 /* the rest are all set to zero by kzalloc */
2997 range->len = (u64)-1;
2999 ret = btrfs_defrag_file(file_inode(file), file,
3000 range, BTRFS_OLDEST_GENERATION, 0);
3009 mnt_drop_write_file(file);
3013 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3015 struct btrfs_ioctl_vol_args *vol_args;
3018 if (!capable(CAP_SYS_ADMIN))
3021 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
3022 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3024 vol_args = memdup_user(arg, sizeof(*vol_args));
3025 if (IS_ERR(vol_args)) {
3026 ret = PTR_ERR(vol_args);
3030 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3031 ret = btrfs_init_new_device(fs_info, vol_args->name);
3034 btrfs_info(fs_info, "disk added %s", vol_args->name);
3038 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3042 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3044 struct inode *inode = file_inode(file);
3045 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3046 struct btrfs_ioctl_vol_args_v2 *vol_args;
3049 if (!capable(CAP_SYS_ADMIN))
3052 ret = mnt_want_write_file(file);
3056 vol_args = memdup_user(arg, sizeof(*vol_args));
3057 if (IS_ERR(vol_args)) {
3058 ret = PTR_ERR(vol_args);
3062 /* Check for compatibility reject unknown flags */
3063 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3068 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
3069 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3073 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3074 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3076 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3077 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3079 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3082 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3083 btrfs_info(fs_info, "device deleted: id %llu",
3086 btrfs_info(fs_info, "device deleted: %s",
3092 mnt_drop_write_file(file);
3096 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3098 struct inode *inode = file_inode(file);
3099 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3100 struct btrfs_ioctl_vol_args *vol_args;
3103 if (!capable(CAP_SYS_ADMIN))
3106 ret = mnt_want_write_file(file);
3110 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
3111 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3112 goto out_drop_write;
3115 vol_args = memdup_user(arg, sizeof(*vol_args));
3116 if (IS_ERR(vol_args)) {
3117 ret = PTR_ERR(vol_args);
3121 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3122 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3125 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3128 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3130 mnt_drop_write_file(file);
3135 static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3138 struct btrfs_ioctl_fs_info_args *fi_args;
3139 struct btrfs_device *device;
3140 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3143 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3148 fi_args->num_devices = fs_devices->num_devices;
3150 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3151 if (device->devid > fi_args->max_id)
3152 fi_args->max_id = device->devid;
3156 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
3157 fi_args->nodesize = fs_info->nodesize;
3158 fi_args->sectorsize = fs_info->sectorsize;
3159 fi_args->clone_alignment = fs_info->sectorsize;
3161 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3168 static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3171 struct btrfs_ioctl_dev_info_args *di_args;
3172 struct btrfs_device *dev;
3174 char *s_uuid = NULL;
3176 di_args = memdup_user(arg, sizeof(*di_args));
3177 if (IS_ERR(di_args))
3178 return PTR_ERR(di_args);
3180 if (!btrfs_is_empty_uuid(di_args->uuid))
3181 s_uuid = di_args->uuid;
3184 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3192 di_args->devid = dev->devid;
3193 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3194 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3195 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3197 strncpy(di_args->path, rcu_str_deref(dev->name),
3198 sizeof(di_args->path) - 1);
3199 di_args->path[sizeof(di_args->path) - 1] = 0;
3201 di_args->path[0] = '\0';
3206 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3213 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3214 struct inode *inode2, u64 loff2, u64 len)
3216 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3217 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3220 static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3221 struct inode *inode2, u64 loff2, u64 len)
3223 if (inode1 < inode2) {
3224 swap(inode1, inode2);
3226 } else if (inode1 == inode2 && loff2 < loff1) {
3229 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3230 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3233 static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
3234 struct inode *dst, u64 dst_loff)
3239 * Lock destination range to serialize with concurrent readpages() and
3240 * source range to serialize with relocation.
3242 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
3243 ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
3244 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
3249 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3251 static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3252 struct inode *dst, u64 dst_loff)
3255 u64 i, tail_len, chunk_count;
3256 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3258 spin_lock(&root_dst->root_item_lock);
3259 if (root_dst->send_in_progress) {
3260 btrfs_warn_rl(root_dst->fs_info,
3261 "cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3262 root_dst->root_key.objectid,
3263 root_dst->send_in_progress);
3264 spin_unlock(&root_dst->root_item_lock);
3267 root_dst->dedupe_in_progress++;
3268 spin_unlock(&root_dst->root_item_lock);
3270 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3271 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
3273 for (i = 0; i < chunk_count; i++) {
3274 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
3279 loff += BTRFS_MAX_DEDUPE_LEN;
3280 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3284 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3287 spin_lock(&root_dst->root_item_lock);
3288 root_dst->dedupe_in_progress--;
3289 spin_unlock(&root_dst->root_item_lock);
3294 static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3295 struct inode *inode,
3301 struct btrfs_root *root = BTRFS_I(inode)->root;
3304 inode_inc_iversion(inode);
3305 if (!no_time_update)
3306 inode->i_mtime = inode->i_ctime = current_time(inode);
3308 * We round up to the block size at eof when determining which
3309 * extents to clone above, but shouldn't round up the file size.
3311 if (endoff > destoff + olen)
3312 endoff = destoff + olen;
3313 if (endoff > inode->i_size)
3314 btrfs_i_size_write(BTRFS_I(inode), endoff);
3316 ret = btrfs_update_inode(trans, root, inode);
3318 btrfs_abort_transaction(trans, ret);
3319 btrfs_end_transaction(trans);
3322 ret = btrfs_end_transaction(trans);
3327 static void clone_update_extent_map(struct btrfs_inode *inode,
3328 const struct btrfs_trans_handle *trans,
3329 const struct btrfs_path *path,
3330 const u64 hole_offset,
3333 struct extent_map_tree *em_tree = &inode->extent_tree;
3334 struct extent_map *em;
3337 em = alloc_extent_map();
3339 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
3344 struct btrfs_file_extent_item *fi;
3346 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3347 struct btrfs_file_extent_item);
3348 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3349 em->generation = -1;
3350 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3351 BTRFS_FILE_EXTENT_INLINE)
3352 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3353 &inode->runtime_flags);
3355 em->start = hole_offset;
3357 em->ram_bytes = em->len;
3358 em->orig_start = hole_offset;
3359 em->block_start = EXTENT_MAP_HOLE;
3361 em->orig_block_len = 0;
3362 em->compress_type = BTRFS_COMPRESS_NONE;
3363 em->generation = trans->transid;
3367 write_lock(&em_tree->lock);
3368 ret = add_extent_mapping(em_tree, em, 1);
3369 write_unlock(&em_tree->lock);
3370 if (ret != -EEXIST) {
3371 free_extent_map(em);
3374 btrfs_drop_extent_cache(inode, em->start,
3375 em->start + em->len - 1, 0);
3379 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
3383 * Make sure we do not end up inserting an inline extent into a file that has
3384 * already other (non-inline) extents. If a file has an inline extent it can
3385 * not have any other extents and the (single) inline extent must start at the
3386 * file offset 0. Failing to respect these rules will lead to file corruption,
3387 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3389 * We can have extents that have been already written to disk or we can have
3390 * dirty ranges still in delalloc, in which case the extent maps and items are
3391 * created only when we run delalloc, and the delalloc ranges might fall outside
3392 * the range we are currently locking in the inode's io tree. So we check the
3393 * inode's i_size because of that (i_size updates are done while holding the
3394 * i_mutex, which we are holding here).
3395 * We also check to see if the inode has a size not greater than "datal" but has
3396 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3397 * protected against such concurrent fallocate calls by the i_mutex).
3399 * If the file has no extents but a size greater than datal, do not allow the
3400 * copy because we would need turn the inline extent into a non-inline one (even
3401 * with NO_HOLES enabled). If we find our destination inode only has one inline
3402 * extent, just overwrite it with the source inline extent if its size is less
3403 * than the source extent's size, or we could copy the source inline extent's
3404 * data into the destination inode's inline extent if the later is greater then
3407 static int clone_copy_inline_extent(struct inode *dst,
3408 struct btrfs_trans_handle *trans,
3409 struct btrfs_path *path,
3410 struct btrfs_key *new_key,
3411 const u64 drop_start,
3417 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
3418 struct btrfs_root *root = BTRFS_I(dst)->root;
3419 const u64 aligned_end = ALIGN(new_key->offset + datal,
3420 fs_info->sectorsize);
3422 struct btrfs_key key;
3424 if (new_key->offset > 0)
3427 key.objectid = btrfs_ino(BTRFS_I(dst));
3428 key.type = BTRFS_EXTENT_DATA_KEY;
3430 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3433 } else if (ret > 0) {
3434 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3435 ret = btrfs_next_leaf(root, path);
3439 goto copy_inline_extent;
3441 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3442 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
3443 key.type == BTRFS_EXTENT_DATA_KEY) {
3444 ASSERT(key.offset > 0);
3447 } else if (i_size_read(dst) <= datal) {
3448 struct btrfs_file_extent_item *ei;
3452 * If the file size is <= datal, make sure there are no other
3453 * extents following (can happen do to an fallocate call with
3454 * the flag FALLOC_FL_KEEP_SIZE).
3456 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3457 struct btrfs_file_extent_item);
3459 * If it's an inline extent, it can not have other extents
3462 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3463 BTRFS_FILE_EXTENT_INLINE)
3464 goto copy_inline_extent;
3466 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3467 if (ext_len > aligned_end)
3470 ret = btrfs_next_item(root, path);
3473 } else if (ret == 0) {
3474 btrfs_item_key_to_cpu(path->nodes[0], &key,
3476 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
3477 key.type == BTRFS_EXTENT_DATA_KEY)
3484 * We have no extent items, or we have an extent at offset 0 which may
3485 * or may not be inlined. All these cases are dealt the same way.
3487 if (i_size_read(dst) > datal) {
3489 * If the destination inode has an inline extent...
3490 * This would require copying the data from the source inline
3491 * extent into the beginning of the destination's inline extent.
3492 * But this is really complex, both extents can be compressed
3493 * or just one of them, which would require decompressing and
3494 * re-compressing data (which could increase the new compressed
3495 * size, not allowing the compressed data to fit anymore in an
3497 * So just don't support this case for now (it should be rare,
3498 * we are not really saving space when cloning inline extents).
3503 btrfs_release_path(path);
3504 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3507 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3512 const u32 start = btrfs_file_extent_calc_inline_size(0);
3514 memmove(inline_data + start, inline_data + start + skip, datal);
3517 write_extent_buffer(path->nodes[0], inline_data,
3518 btrfs_item_ptr_offset(path->nodes[0],
3521 inode_add_bytes(dst, datal);
3527 * btrfs_clone() - clone a range from inode file to another
3529 * @src: Inode to clone from
3530 * @inode: Inode to clone to
3531 * @off: Offset within source to start clone from
3532 * @olen: Original length, passed by user, of range to clone
3533 * @olen_aligned: Block-aligned value of olen
3534 * @destoff: Offset within @inode to start clone
3535 * @no_time_update: Whether to update mtime/ctime on the target inode
3537 static int btrfs_clone(struct inode *src, struct inode *inode,
3538 const u64 off, const u64 olen, const u64 olen_aligned,
3539 const u64 destoff, int no_time_update)
3541 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3542 struct btrfs_root *root = BTRFS_I(inode)->root;
3543 struct btrfs_path *path = NULL;
3544 struct extent_buffer *leaf;
3545 struct btrfs_trans_handle *trans;
3547 struct btrfs_key key;
3551 const u64 len = olen_aligned;
3552 u64 last_dest_end = destoff;
3555 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3559 path = btrfs_alloc_path();
3565 path->reada = READA_FORWARD;
3567 key.objectid = btrfs_ino(BTRFS_I(src));
3568 key.type = BTRFS_EXTENT_DATA_KEY;
3572 u64 next_key_min_offset = key.offset + 1;
3575 * note the key will change type as we walk through the
3578 path->leave_spinning = 1;
3579 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3584 * First search, if no extent item that starts at offset off was
3585 * found but the previous item is an extent item, it's possible
3586 * it might overlap our target range, therefore process it.
3588 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3589 btrfs_item_key_to_cpu(path->nodes[0], &key,
3590 path->slots[0] - 1);
3591 if (key.type == BTRFS_EXTENT_DATA_KEY)
3595 nritems = btrfs_header_nritems(path->nodes[0]);
3597 if (path->slots[0] >= nritems) {
3598 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3603 nritems = btrfs_header_nritems(path->nodes[0]);
3605 leaf = path->nodes[0];
3606 slot = path->slots[0];
3608 btrfs_item_key_to_cpu(leaf, &key, slot);
3609 if (key.type > BTRFS_EXTENT_DATA_KEY ||
3610 key.objectid != btrfs_ino(BTRFS_I(src)))
3613 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3614 struct btrfs_file_extent_item *extent;
3617 struct btrfs_key new_key;
3618 u64 disko = 0, diskl = 0;
3619 u64 datao = 0, datal = 0;
3623 extent = btrfs_item_ptr(leaf, slot,
3624 struct btrfs_file_extent_item);
3625 comp = btrfs_file_extent_compression(leaf, extent);
3626 type = btrfs_file_extent_type(leaf, extent);
3627 if (type == BTRFS_FILE_EXTENT_REG ||
3628 type == BTRFS_FILE_EXTENT_PREALLOC) {
3629 disko = btrfs_file_extent_disk_bytenr(leaf,
3631 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3633 datao = btrfs_file_extent_offset(leaf, extent);
3634 datal = btrfs_file_extent_num_bytes(leaf,
3636 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3637 /* take upper bound, may be compressed */
3638 datal = btrfs_file_extent_ram_bytes(leaf,
3643 * The first search might have left us at an extent
3644 * item that ends before our target range's start, can
3645 * happen if we have holes and NO_HOLES feature enabled.
3647 if (key.offset + datal <= off) {
3650 } else if (key.offset >= off + len) {
3653 next_key_min_offset = key.offset + datal;
3654 size = btrfs_item_size_nr(leaf, slot);
3655 read_extent_buffer(leaf, buf,
3656 btrfs_item_ptr_offset(leaf, slot),
3659 btrfs_release_path(path);
3660 path->leave_spinning = 0;
3662 memcpy(&new_key, &key, sizeof(new_key));
3663 new_key.objectid = btrfs_ino(BTRFS_I(inode));
3664 if (off <= key.offset)
3665 new_key.offset = key.offset + destoff - off;
3667 new_key.offset = destoff;
3670 * Deal with a hole that doesn't have an extent item
3671 * that represents it (NO_HOLES feature enabled).
3672 * This hole is either in the middle of the cloning
3673 * range or at the beginning (fully overlaps it or
3674 * partially overlaps it).
3676 if (new_key.offset != last_dest_end)
3677 drop_start = last_dest_end;
3679 drop_start = new_key.offset;
3682 * 1 - adjusting old extent (we may have to split it)
3683 * 1 - add new extent
3686 trans = btrfs_start_transaction(root, 3);
3687 if (IS_ERR(trans)) {
3688 ret = PTR_ERR(trans);
3692 if (type == BTRFS_FILE_EXTENT_REG ||
3693 type == BTRFS_FILE_EXTENT_PREALLOC) {
3695 * a | --- range to clone ---| b
3696 * | ------------- extent ------------- |
3699 /* subtract range b */
3700 if (key.offset + datal > off + len)
3701 datal = off + len - key.offset;
3703 /* subtract range a */
3704 if (off > key.offset) {
3705 datao += off - key.offset;
3706 datal -= off - key.offset;
3709 ret = btrfs_drop_extents(trans, root, inode,
3711 new_key.offset + datal,
3714 if (ret != -EOPNOTSUPP)
3715 btrfs_abort_transaction(trans,
3717 btrfs_end_transaction(trans);
3721 ret = btrfs_insert_empty_item(trans, root, path,
3724 btrfs_abort_transaction(trans, ret);
3725 btrfs_end_transaction(trans);
3729 leaf = path->nodes[0];
3730 slot = path->slots[0];
3731 write_extent_buffer(leaf, buf,
3732 btrfs_item_ptr_offset(leaf, slot),
3735 extent = btrfs_item_ptr(leaf, slot,
3736 struct btrfs_file_extent_item);
3738 /* disko == 0 means it's a hole */
3742 btrfs_set_file_extent_offset(leaf, extent,
3744 btrfs_set_file_extent_num_bytes(leaf, extent,
3748 struct btrfs_ref ref = { 0 };
3749 inode_add_bytes(inode, datal);
3750 btrfs_init_generic_ref(&ref,
3751 BTRFS_ADD_DELAYED_REF, disko,
3753 btrfs_init_data_ref(&ref,
3754 root->root_key.objectid,
3755 btrfs_ino(BTRFS_I(inode)),
3756 new_key.offset - datao);
3757 ret = btrfs_inc_extent_ref(trans, &ref);
3759 btrfs_abort_transaction(trans,
3761 btrfs_end_transaction(trans);
3766 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3770 if (off > key.offset) {
3771 skip = off - key.offset;
3772 new_key.offset += skip;
3775 if (key.offset + datal > off + len)
3776 trim = key.offset + datal - (off + len);
3778 if (comp && (skip || trim)) {
3780 btrfs_end_transaction(trans);
3783 size -= skip + trim;
3784 datal -= skip + trim;
3786 ret = clone_copy_inline_extent(inode,
3793 if (ret != -EOPNOTSUPP)
3794 btrfs_abort_transaction(trans,
3796 btrfs_end_transaction(trans);
3799 leaf = path->nodes[0];
3800 slot = path->slots[0];
3803 /* If we have an implicit hole (NO_HOLES feature). */
3804 if (drop_start < new_key.offset)
3805 clone_update_extent_map(BTRFS_I(inode), trans,
3807 new_key.offset - drop_start);
3809 clone_update_extent_map(BTRFS_I(inode), trans,
3812 btrfs_mark_buffer_dirty(leaf);
3813 btrfs_release_path(path);
3815 last_dest_end = ALIGN(new_key.offset + datal,
3816 fs_info->sectorsize);
3817 ret = clone_finish_inode_update(trans, inode,
3823 if (new_key.offset + datal >= destoff + len)
3826 btrfs_release_path(path);
3827 key.offset = next_key_min_offset;
3829 if (fatal_signal_pending(current)) {
3836 if (last_dest_end < destoff + len) {
3838 * We have an implicit hole (NO_HOLES feature is enabled) that
3839 * fully or partially overlaps our cloning range at its end.
3841 btrfs_release_path(path);
3844 * 1 - remove extent(s)
3847 trans = btrfs_start_transaction(root, 2);
3848 if (IS_ERR(trans)) {
3849 ret = PTR_ERR(trans);
3852 ret = btrfs_drop_extents(trans, root, inode,
3853 last_dest_end, destoff + len, 1);
3855 if (ret != -EOPNOTSUPP)
3856 btrfs_abort_transaction(trans, ret);
3857 btrfs_end_transaction(trans);
3860 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3862 destoff + len - last_dest_end);
3863 ret = clone_finish_inode_update(trans, inode, destoff + len,
3864 destoff, olen, no_time_update);
3868 btrfs_free_path(path);
3873 static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3874 u64 off, u64 olen, u64 destoff)
3876 struct inode *inode = file_inode(file);
3877 struct inode *src = file_inode(file_src);
3878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3881 u64 bs = fs_info->sb->s_blocksize;
3885 * - split compressed inline extents. annoying: we need to
3886 * decompress into destination's address_space (the file offset
3887 * may change, so source mapping won't do), then recompress (or
3888 * otherwise reinsert) a subrange.
3890 * - split destination inode's inline extents. The inline extents can
3891 * be either compressed or non-compressed.
3895 * VFS's generic_remap_file_range_prep() protects us from cloning the
3896 * eof block into the middle of a file, which would result in corruption
3897 * if the file size is not blocksize aligned. So we don't need to check
3898 * for that case here.
3900 if (off + len == src->i_size)
3901 len = ALIGN(src->i_size, bs) - off;
3903 if (destoff > inode->i_size) {
3904 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3906 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3910 * We may have truncated the last block if the inode's size is
3911 * not sector size aligned, so we need to wait for writeback to
3912 * complete before proceeding further, otherwise we can race
3913 * with cloning and attempt to increment a reference to an
3914 * extent that no longer exists (writeback completed right after
3915 * we found the previous extent covering eof and before we
3916 * attempted to increment its reference count).
3918 ret = btrfs_wait_ordered_range(inode, wb_start,
3919 destoff - wb_start);
3925 * Lock destination range to serialize with concurrent readpages() and
3926 * source range to serialize with relocation.
3928 btrfs_double_extent_lock(src, off, inode, destoff, len);
3929 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
3930 btrfs_double_extent_unlock(src, off, inode, destoff, len);
3932 * Truncate page cache pages so that future reads will see the cloned
3933 * data immediately and not the previous data.
3935 truncate_inode_pages_range(&inode->i_data,
3936 round_down(destoff, PAGE_SIZE),
3937 round_up(destoff + len, PAGE_SIZE) - 1);
3942 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3943 struct file *file_out, loff_t pos_out,
3944 loff_t *len, unsigned int remap_flags)
3946 struct inode *inode_in = file_inode(file_in);
3947 struct inode *inode_out = file_inode(file_out);
3948 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3949 bool same_inode = inode_out == inode_in;
3953 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3954 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3956 if (btrfs_root_readonly(root_out))
3959 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3960 inode_in->i_sb != inode_out->i_sb)
3964 /* don't make the dst file partly checksummed */
3965 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3966 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
3971 * Now that the inodes are locked, we need to start writeback ourselves
3972 * and can not rely on the writeback from the VFS's generic helper
3973 * generic_remap_file_range_prep() because:
3975 * 1) For compression we must call filemap_fdatawrite_range() range
3976 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3977 * helper only calls it once;
3979 * 2) filemap_fdatawrite_range(), called by the generic helper only
3980 * waits for the writeback to complete, i.e. for IO to be done, and
3981 * not for the ordered extents to complete. We need to wait for them
3982 * to complete so that new file extent items are in the fs tree.
3984 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3985 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3987 wb_len = ALIGN(*len, bs);
3990 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3991 * any in progress could create its ordered extents after we wait for
3992 * existing ordered extents below).
3994 inode_dio_wait(inode_in);
3996 inode_dio_wait(inode_out);
3999 * Workaround to make sure NOCOW buffered write reach disk as NOCOW.
4001 * Btrfs' back references do not have a block level granularity, they
4002 * work at the whole extent level.
4003 * NOCOW buffered write without data space reserved may not be able
4004 * to fall back to CoW due to lack of data space, thus could cause
4007 * Here we take a shortcut by flushing the whole inode, so that all
4008 * nocow write should reach disk as nocow before we increase the
4009 * reference of the extent. We could do better by only flushing NOCOW
4010 * data, but that needs extra accounting.
4012 * Also we don't need to check ASYNC_EXTENT, as async extent will be
4013 * CoWed anyway, not affecting nocow part.
4015 ret = filemap_flush(inode_in->i_mapping);
4019 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
4023 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
4028 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
4032 loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4033 struct file *dst_file, loff_t destoff, loff_t len,
4034 unsigned int remap_flags)
4036 struct inode *src_inode = file_inode(src_file);
4037 struct inode *dst_inode = file_inode(dst_file);
4038 bool same_inode = dst_inode == src_inode;
4041 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4045 inode_lock(src_inode);
4047 lock_two_nondirectories(src_inode, dst_inode);
4049 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4051 if (ret < 0 || len == 0)
4054 if (remap_flags & REMAP_FILE_DEDUP)
4055 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4057 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
4061 inode_unlock(src_inode);
4063 unlock_two_nondirectories(src_inode, dst_inode);
4065 return ret < 0 ? ret : len;
4068 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4070 struct inode *inode = file_inode(file);
4071 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4072 struct btrfs_root *root = BTRFS_I(inode)->root;
4073 struct btrfs_root *new_root;
4074 struct btrfs_dir_item *di;
4075 struct btrfs_trans_handle *trans;
4076 struct btrfs_path *path;
4077 struct btrfs_key location;
4078 struct btrfs_disk_key disk_key;
4083 if (!capable(CAP_SYS_ADMIN))
4086 ret = mnt_want_write_file(file);
4090 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4096 objectid = BTRFS_FS_TREE_OBJECTID;
4098 location.objectid = objectid;
4099 location.type = BTRFS_ROOT_ITEM_KEY;
4100 location.offset = (u64)-1;
4102 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
4103 if (IS_ERR(new_root)) {
4104 ret = PTR_ERR(new_root);
4107 if (!is_fstree(new_root->root_key.objectid)) {
4112 path = btrfs_alloc_path();
4117 path->leave_spinning = 1;
4119 trans = btrfs_start_transaction(root, 1);
4120 if (IS_ERR(trans)) {
4121 btrfs_free_path(path);
4122 ret = PTR_ERR(trans);
4126 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4127 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
4128 dir_id, "default", 7, 1);
4129 if (IS_ERR_OR_NULL(di)) {
4130 btrfs_free_path(path);
4131 btrfs_end_transaction(trans);
4133 "Umm, you don't have the default diritem, this isn't going to work");
4138 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4139 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4140 btrfs_mark_buffer_dirty(path->nodes[0]);
4141 btrfs_free_path(path);
4143 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
4144 btrfs_end_transaction(trans);
4146 mnt_drop_write_file(file);
4150 static void get_block_group_info(struct list_head *groups_list,
4151 struct btrfs_ioctl_space_info *space)
4153 struct btrfs_block_group_cache *block_group;
4155 space->total_bytes = 0;
4156 space->used_bytes = 0;
4158 list_for_each_entry(block_group, groups_list, list) {
4159 space->flags = block_group->flags;
4160 space->total_bytes += block_group->key.offset;
4161 space->used_bytes +=
4162 btrfs_block_group_used(&block_group->item);
4166 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4169 struct btrfs_ioctl_space_args space_args;
4170 struct btrfs_ioctl_space_info space;
4171 struct btrfs_ioctl_space_info *dest;
4172 struct btrfs_ioctl_space_info *dest_orig;
4173 struct btrfs_ioctl_space_info __user *user_dest;
4174 struct btrfs_space_info *info;
4175 static const u64 types[] = {
4176 BTRFS_BLOCK_GROUP_DATA,
4177 BTRFS_BLOCK_GROUP_SYSTEM,
4178 BTRFS_BLOCK_GROUP_METADATA,
4179 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4187 if (copy_from_user(&space_args,
4188 (struct btrfs_ioctl_space_args __user *)arg,
4189 sizeof(space_args)))
4192 for (i = 0; i < num_types; i++) {
4193 struct btrfs_space_info *tmp;
4197 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4199 if (tmp->flags == types[i]) {
4209 down_read(&info->groups_sem);
4210 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4211 if (!list_empty(&info->block_groups[c]))
4214 up_read(&info->groups_sem);
4218 * Global block reserve, exported as a space_info
4222 /* space_slots == 0 means they are asking for a count */
4223 if (space_args.space_slots == 0) {
4224 space_args.total_spaces = slot_count;
4228 slot_count = min_t(u64, space_args.space_slots, slot_count);
4230 alloc_size = sizeof(*dest) * slot_count;
4232 /* we generally have at most 6 or so space infos, one for each raid
4233 * level. So, a whole page should be more than enough for everyone
4235 if (alloc_size > PAGE_SIZE)
4238 space_args.total_spaces = 0;
4239 dest = kmalloc(alloc_size, GFP_KERNEL);
4244 /* now we have a buffer to copy into */
4245 for (i = 0; i < num_types; i++) {
4246 struct btrfs_space_info *tmp;
4253 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4255 if (tmp->flags == types[i]) {
4264 down_read(&info->groups_sem);
4265 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4266 if (!list_empty(&info->block_groups[c])) {
4267 get_block_group_info(&info->block_groups[c],
4269 memcpy(dest, &space, sizeof(space));
4271 space_args.total_spaces++;
4277 up_read(&info->groups_sem);
4281 * Add global block reserve
4284 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4286 spin_lock(&block_rsv->lock);
4287 space.total_bytes = block_rsv->size;
4288 space.used_bytes = block_rsv->size - block_rsv->reserved;
4289 spin_unlock(&block_rsv->lock);
4290 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4291 memcpy(dest, &space, sizeof(space));
4292 space_args.total_spaces++;
4295 user_dest = (struct btrfs_ioctl_space_info __user *)
4296 (arg + sizeof(struct btrfs_ioctl_space_args));
4298 if (copy_to_user(user_dest, dest_orig, alloc_size))
4303 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
4309 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4312 struct btrfs_trans_handle *trans;
4316 trans = btrfs_attach_transaction_barrier(root);
4317 if (IS_ERR(trans)) {
4318 if (PTR_ERR(trans) != -ENOENT)
4319 return PTR_ERR(trans);
4321 /* No running transaction, don't bother */
4322 transid = root->fs_info->last_trans_committed;
4325 transid = trans->transid;
4326 ret = btrfs_commit_transaction_async(trans, 0);
4328 btrfs_end_transaction(trans);
4333 if (copy_to_user(argp, &transid, sizeof(transid)))
4338 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
4344 if (copy_from_user(&transid, argp, sizeof(transid)))
4347 transid = 0; /* current trans */
4349 return btrfs_wait_for_commit(fs_info, transid);
4352 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4354 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
4355 struct btrfs_ioctl_scrub_args *sa;
4358 if (!capable(CAP_SYS_ADMIN))
4361 sa = memdup_user(arg, sizeof(*sa));
4365 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4366 ret = mnt_want_write_file(file);
4371 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
4372 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4375 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4378 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4379 mnt_drop_write_file(file);
4385 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
4387 if (!capable(CAP_SYS_ADMIN))
4390 return btrfs_scrub_cancel(fs_info);
4393 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
4396 struct btrfs_ioctl_scrub_args *sa;
4399 if (!capable(CAP_SYS_ADMIN))
4402 sa = memdup_user(arg, sizeof(*sa));
4406 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
4408 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4415 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
4418 struct btrfs_ioctl_get_dev_stats *sa;
4421 sa = memdup_user(arg, sizeof(*sa));
4425 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4430 ret = btrfs_get_dev_stats(fs_info, sa);
4432 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
4439 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4442 struct btrfs_ioctl_dev_replace_args *p;
4445 if (!capable(CAP_SYS_ADMIN))
4448 p = memdup_user(arg, sizeof(*p));
4453 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
4454 if (sb_rdonly(fs_info->sb)) {
4458 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
4459 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4461 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
4462 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4465 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4466 btrfs_dev_replace_status(fs_info, p);
4469 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4470 p->result = btrfs_dev_replace_cancel(fs_info);
4478 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
4485 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4491 struct btrfs_ioctl_ino_path_args *ipa = NULL;
4492 struct inode_fs_paths *ipath = NULL;
4493 struct btrfs_path *path;
4495 if (!capable(CAP_DAC_READ_SEARCH))
4498 path = btrfs_alloc_path();
4504 ipa = memdup_user(arg, sizeof(*ipa));
4511 size = min_t(u32, ipa->size, 4096);
4512 ipath = init_ipath(size, root, path);
4513 if (IS_ERR(ipath)) {
4514 ret = PTR_ERR(ipath);
4519 ret = paths_from_inode(ipa->inum, ipath);
4523 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
4524 rel_ptr = ipath->fspath->val[i] -
4525 (u64)(unsigned long)ipath->fspath->val;
4526 ipath->fspath->val[i] = rel_ptr;
4529 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4530 ipath->fspath, size);
4537 btrfs_free_path(path);
4544 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4546 struct btrfs_data_container *inodes = ctx;
4547 const size_t c = 3 * sizeof(u64);
4549 if (inodes->bytes_left >= c) {
4550 inodes->bytes_left -= c;
4551 inodes->val[inodes->elem_cnt] = inum;
4552 inodes->val[inodes->elem_cnt + 1] = offset;
4553 inodes->val[inodes->elem_cnt + 2] = root;
4554 inodes->elem_cnt += 3;
4556 inodes->bytes_missing += c - inodes->bytes_left;
4557 inodes->bytes_left = 0;
4558 inodes->elem_missed += 3;
4564 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
4565 void __user *arg, int version)
4569 struct btrfs_ioctl_logical_ino_args *loi;
4570 struct btrfs_data_container *inodes = NULL;
4571 struct btrfs_path *path = NULL;
4574 if (!capable(CAP_SYS_ADMIN))
4577 loi = memdup_user(arg, sizeof(*loi));
4579 return PTR_ERR(loi);
4582 ignore_offset = false;
4583 size = min_t(u32, loi->size, SZ_64K);
4585 /* All reserved bits must be 0 for now */
4586 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4590 /* Only accept flags we have defined so far */
4591 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4595 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
4596 size = min_t(u32, loi->size, SZ_16M);
4599 path = btrfs_alloc_path();
4605 inodes = init_data_container(size);
4606 if (IS_ERR(inodes)) {
4607 ret = PTR_ERR(inodes);
4612 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
4613 build_ino_list, inodes, ignore_offset);
4619 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4625 btrfs_free_path(path);
4633 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
4634 struct btrfs_ioctl_balance_args *bargs)
4636 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4638 bargs->flags = bctl->flags;
4640 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
4641 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4642 if (atomic_read(&fs_info->balance_pause_req))
4643 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4644 if (atomic_read(&fs_info->balance_cancel_req))
4645 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4647 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4648 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4649 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
4651 spin_lock(&fs_info->balance_lock);
4652 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4653 spin_unlock(&fs_info->balance_lock);
4656 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4658 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4659 struct btrfs_fs_info *fs_info = root->fs_info;
4660 struct btrfs_ioctl_balance_args *bargs;
4661 struct btrfs_balance_control *bctl;
4662 bool need_unlock; /* for mut. excl. ops lock */
4665 if (!capable(CAP_SYS_ADMIN))
4668 ret = mnt_want_write_file(file);
4673 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
4674 mutex_lock(&fs_info->balance_mutex);
4680 * mut. excl. ops lock is locked. Three possibilities:
4681 * (1) some other op is running
4682 * (2) balance is running
4683 * (3) balance is paused -- special case (think resume)
4685 mutex_lock(&fs_info->balance_mutex);
4686 if (fs_info->balance_ctl) {
4687 /* this is either (2) or (3) */
4688 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4689 mutex_unlock(&fs_info->balance_mutex);
4691 * Lock released to allow other waiters to continue,
4692 * we'll reexamine the status again.
4694 mutex_lock(&fs_info->balance_mutex);
4696 if (fs_info->balance_ctl &&
4697 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4699 need_unlock = false;
4703 mutex_unlock(&fs_info->balance_mutex);
4707 mutex_unlock(&fs_info->balance_mutex);
4713 mutex_unlock(&fs_info->balance_mutex);
4714 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4719 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
4722 bargs = memdup_user(arg, sizeof(*bargs));
4723 if (IS_ERR(bargs)) {
4724 ret = PTR_ERR(bargs);
4728 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4729 if (!fs_info->balance_ctl) {
4734 bctl = fs_info->balance_ctl;
4735 spin_lock(&fs_info->balance_lock);
4736 bctl->flags |= BTRFS_BALANCE_RESUME;
4737 spin_unlock(&fs_info->balance_lock);
4745 if (fs_info->balance_ctl) {
4750 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4757 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4758 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4759 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4761 bctl->flags = bargs->flags;
4763 /* balance everything - no filters */
4764 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4767 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4774 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4775 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4776 * restriper was paused all the way until unmount, in free_fs_info.
4777 * The flag should be cleared after reset_balance_state.
4779 need_unlock = false;
4781 ret = btrfs_balance(fs_info, bctl, bargs);
4784 if ((ret == 0 || ret == -ECANCELED) && arg) {
4785 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4794 mutex_unlock(&fs_info->balance_mutex);
4796 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4798 mnt_drop_write_file(file);
4802 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4804 if (!capable(CAP_SYS_ADMIN))
4808 case BTRFS_BALANCE_CTL_PAUSE:
4809 return btrfs_pause_balance(fs_info);
4810 case BTRFS_BALANCE_CTL_CANCEL:
4811 return btrfs_cancel_balance(fs_info);
4817 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4820 struct btrfs_ioctl_balance_args *bargs;
4823 if (!capable(CAP_SYS_ADMIN))
4826 mutex_lock(&fs_info->balance_mutex);
4827 if (!fs_info->balance_ctl) {
4832 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4838 btrfs_update_ioctl_balance_args(fs_info, bargs);
4840 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4845 mutex_unlock(&fs_info->balance_mutex);
4849 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4851 struct inode *inode = file_inode(file);
4852 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4853 struct btrfs_ioctl_quota_ctl_args *sa;
4856 if (!capable(CAP_SYS_ADMIN))
4859 ret = mnt_want_write_file(file);
4863 sa = memdup_user(arg, sizeof(*sa));
4869 down_write(&fs_info->subvol_sem);
4872 case BTRFS_QUOTA_CTL_ENABLE:
4873 ret = btrfs_quota_enable(fs_info);
4875 case BTRFS_QUOTA_CTL_DISABLE:
4876 ret = btrfs_quota_disable(fs_info);
4884 up_write(&fs_info->subvol_sem);
4886 mnt_drop_write_file(file);
4890 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4892 struct inode *inode = file_inode(file);
4893 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4894 struct btrfs_root *root = BTRFS_I(inode)->root;
4895 struct btrfs_ioctl_qgroup_assign_args *sa;
4896 struct btrfs_trans_handle *trans;
4900 if (!capable(CAP_SYS_ADMIN))
4903 ret = mnt_want_write_file(file);
4907 sa = memdup_user(arg, sizeof(*sa));
4913 trans = btrfs_join_transaction(root);
4914 if (IS_ERR(trans)) {
4915 ret = PTR_ERR(trans);
4920 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4922 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4925 /* update qgroup status and info */
4926 err = btrfs_run_qgroups(trans);
4928 btrfs_handle_fs_error(fs_info, err,
4929 "failed to update qgroup status and info");
4930 err = btrfs_end_transaction(trans);
4937 mnt_drop_write_file(file);
4941 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4943 struct inode *inode = file_inode(file);
4944 struct btrfs_root *root = BTRFS_I(inode)->root;
4945 struct btrfs_ioctl_qgroup_create_args *sa;
4946 struct btrfs_trans_handle *trans;
4950 if (!capable(CAP_SYS_ADMIN))
4953 ret = mnt_want_write_file(file);
4957 sa = memdup_user(arg, sizeof(*sa));
4963 if (!sa->qgroupid) {
4968 trans = btrfs_join_transaction(root);
4969 if (IS_ERR(trans)) {
4970 ret = PTR_ERR(trans);
4975 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4977 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4980 err = btrfs_end_transaction(trans);
4987 mnt_drop_write_file(file);
4991 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4993 struct inode *inode = file_inode(file);
4994 struct btrfs_root *root = BTRFS_I(inode)->root;
4995 struct btrfs_ioctl_qgroup_limit_args *sa;
4996 struct btrfs_trans_handle *trans;
5001 if (!capable(CAP_SYS_ADMIN))
5004 ret = mnt_want_write_file(file);
5008 sa = memdup_user(arg, sizeof(*sa));
5014 trans = btrfs_join_transaction(root);
5015 if (IS_ERR(trans)) {
5016 ret = PTR_ERR(trans);
5020 qgroupid = sa->qgroupid;
5022 /* take the current subvol as qgroup */
5023 qgroupid = root->root_key.objectid;
5026 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
5028 err = btrfs_end_transaction(trans);
5035 mnt_drop_write_file(file);
5039 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5041 struct inode *inode = file_inode(file);
5042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5043 struct btrfs_ioctl_quota_rescan_args *qsa;
5046 if (!capable(CAP_SYS_ADMIN))
5049 ret = mnt_want_write_file(file);
5053 qsa = memdup_user(arg, sizeof(*qsa));
5064 ret = btrfs_qgroup_rescan(fs_info);
5069 mnt_drop_write_file(file);
5073 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5075 struct inode *inode = file_inode(file);
5076 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5077 struct btrfs_ioctl_quota_rescan_args *qsa;
5080 if (!capable(CAP_SYS_ADMIN))
5083 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
5087 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5089 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
5092 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5099 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5101 struct inode *inode = file_inode(file);
5102 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5104 if (!capable(CAP_SYS_ADMIN))
5107 return btrfs_qgroup_wait_for_completion(fs_info, true);
5110 static long _btrfs_ioctl_set_received_subvol(struct file *file,
5111 struct btrfs_ioctl_received_subvol_args *sa)
5113 struct inode *inode = file_inode(file);
5114 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5115 struct btrfs_root *root = BTRFS_I(inode)->root;
5116 struct btrfs_root_item *root_item = &root->root_item;
5117 struct btrfs_trans_handle *trans;
5118 struct timespec64 ct = current_time(inode);
5120 int received_uuid_changed;
5122 if (!inode_owner_or_capable(inode))
5125 ret = mnt_want_write_file(file);
5129 down_write(&fs_info->subvol_sem);
5131 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
5136 if (btrfs_root_readonly(root)) {
5143 * 2 - uuid items (received uuid + subvol uuid)
5145 trans = btrfs_start_transaction(root, 3);
5146 if (IS_ERR(trans)) {
5147 ret = PTR_ERR(trans);
5152 sa->rtransid = trans->transid;
5153 sa->rtime.sec = ct.tv_sec;
5154 sa->rtime.nsec = ct.tv_nsec;
5156 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5158 if (received_uuid_changed &&
5159 !btrfs_is_empty_uuid(root_item->received_uuid)) {
5160 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
5161 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5162 root->root_key.objectid);
5163 if (ret && ret != -ENOENT) {
5164 btrfs_abort_transaction(trans, ret);
5165 btrfs_end_transaction(trans);
5169 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5170 btrfs_set_root_stransid(root_item, sa->stransid);
5171 btrfs_set_root_rtransid(root_item, sa->rtransid);
5172 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5173 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5174 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5175 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
5177 ret = btrfs_update_root(trans, fs_info->tree_root,
5178 &root->root_key, &root->root_item);
5180 btrfs_end_transaction(trans);
5183 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5184 ret = btrfs_uuid_tree_add(trans, sa->uuid,
5185 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5186 root->root_key.objectid);
5187 if (ret < 0 && ret != -EEXIST) {
5188 btrfs_abort_transaction(trans, ret);
5189 btrfs_end_transaction(trans);
5193 ret = btrfs_commit_transaction(trans);
5195 up_write(&fs_info->subvol_sem);
5196 mnt_drop_write_file(file);
5201 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5204 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5205 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5208 args32 = memdup_user(arg, sizeof(*args32));
5210 return PTR_ERR(args32);
5212 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
5218 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5219 args64->stransid = args32->stransid;
5220 args64->rtransid = args32->rtransid;
5221 args64->stime.sec = args32->stime.sec;
5222 args64->stime.nsec = args32->stime.nsec;
5223 args64->rtime.sec = args32->rtime.sec;
5224 args64->rtime.nsec = args32->rtime.nsec;
5225 args64->flags = args32->flags;
5227 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5231 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5232 args32->stransid = args64->stransid;
5233 args32->rtransid = args64->rtransid;
5234 args32->stime.sec = args64->stime.sec;
5235 args32->stime.nsec = args64->stime.nsec;
5236 args32->rtime.sec = args64->rtime.sec;
5237 args32->rtime.nsec = args64->rtime.nsec;
5238 args32->flags = args64->flags;
5240 ret = copy_to_user(arg, args32, sizeof(*args32));
5251 static long btrfs_ioctl_set_received_subvol(struct file *file,
5254 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5257 sa = memdup_user(arg, sizeof(*sa));
5261 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5266 ret = copy_to_user(arg, sa, sizeof(*sa));
5275 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5277 struct inode *inode = file_inode(file);
5278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5281 char label[BTRFS_LABEL_SIZE];
5283 spin_lock(&fs_info->super_lock);
5284 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5285 spin_unlock(&fs_info->super_lock);
5287 len = strnlen(label, BTRFS_LABEL_SIZE);
5289 if (len == BTRFS_LABEL_SIZE) {
5291 "label is too long, return the first %zu bytes",
5295 ret = copy_to_user(arg, label, len);
5297 return ret ? -EFAULT : 0;
5300 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5302 struct inode *inode = file_inode(file);
5303 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5304 struct btrfs_root *root = BTRFS_I(inode)->root;
5305 struct btrfs_super_block *super_block = fs_info->super_copy;
5306 struct btrfs_trans_handle *trans;
5307 char label[BTRFS_LABEL_SIZE];
5310 if (!capable(CAP_SYS_ADMIN))
5313 if (copy_from_user(label, arg, sizeof(label)))
5316 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
5318 "unable to set label with more than %d bytes",
5319 BTRFS_LABEL_SIZE - 1);
5323 ret = mnt_want_write_file(file);
5327 trans = btrfs_start_transaction(root, 0);
5328 if (IS_ERR(trans)) {
5329 ret = PTR_ERR(trans);
5333 spin_lock(&fs_info->super_lock);
5334 strcpy(super_block->label, label);
5335 spin_unlock(&fs_info->super_lock);
5336 ret = btrfs_commit_transaction(trans);
5339 mnt_drop_write_file(file);
5343 #define INIT_FEATURE_FLAGS(suffix) \
5344 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5345 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5346 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5348 int btrfs_ioctl_get_supported_features(void __user *arg)
5350 static const struct btrfs_ioctl_feature_flags features[3] = {
5351 INIT_FEATURE_FLAGS(SUPP),
5352 INIT_FEATURE_FLAGS(SAFE_SET),
5353 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5356 if (copy_to_user(arg, &features, sizeof(features)))
5362 static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5364 struct inode *inode = file_inode(file);
5365 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5366 struct btrfs_super_block *super_block = fs_info->super_copy;
5367 struct btrfs_ioctl_feature_flags features;
5369 features.compat_flags = btrfs_super_compat_flags(super_block);
5370 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5371 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5373 if (copy_to_user(arg, &features, sizeof(features)))
5379 static int check_feature_bits(struct btrfs_fs_info *fs_info,
5380 enum btrfs_feature_set set,
5381 u64 change_mask, u64 flags, u64 supported_flags,
5382 u64 safe_set, u64 safe_clear)
5384 const char *type = btrfs_feature_set_names[set];
5386 u64 disallowed, unsupported;
5387 u64 set_mask = flags & change_mask;
5388 u64 clear_mask = ~flags & change_mask;
5390 unsupported = set_mask & ~supported_flags;
5392 names = btrfs_printable_features(set, unsupported);
5395 "this kernel does not support the %s feature bit%s",
5396 names, strchr(names, ',') ? "s" : "");
5400 "this kernel does not support %s bits 0x%llx",
5405 disallowed = set_mask & ~safe_set;
5407 names = btrfs_printable_features(set, disallowed);
5410 "can't set the %s feature bit%s while mounted",
5411 names, strchr(names, ',') ? "s" : "");
5415 "can't set %s bits 0x%llx while mounted",
5420 disallowed = clear_mask & ~safe_clear;
5422 names = btrfs_printable_features(set, disallowed);
5425 "can't clear the %s feature bit%s while mounted",
5426 names, strchr(names, ',') ? "s" : "");
5430 "can't clear %s bits 0x%llx while mounted",
5438 #define check_feature(fs_info, change_mask, flags, mask_base) \
5439 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
5440 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5441 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5442 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5444 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5446 struct inode *inode = file_inode(file);
5447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5448 struct btrfs_root *root = BTRFS_I(inode)->root;
5449 struct btrfs_super_block *super_block = fs_info->super_copy;
5450 struct btrfs_ioctl_feature_flags flags[2];
5451 struct btrfs_trans_handle *trans;
5455 if (!capable(CAP_SYS_ADMIN))
5458 if (copy_from_user(flags, arg, sizeof(flags)))
5462 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5463 !flags[0].incompat_flags)
5466 ret = check_feature(fs_info, flags[0].compat_flags,
5467 flags[1].compat_flags, COMPAT);
5471 ret = check_feature(fs_info, flags[0].compat_ro_flags,
5472 flags[1].compat_ro_flags, COMPAT_RO);
5476 ret = check_feature(fs_info, flags[0].incompat_flags,
5477 flags[1].incompat_flags, INCOMPAT);
5481 ret = mnt_want_write_file(file);
5485 trans = btrfs_start_transaction(root, 0);
5486 if (IS_ERR(trans)) {
5487 ret = PTR_ERR(trans);
5488 goto out_drop_write;
5491 spin_lock(&fs_info->super_lock);
5492 newflags = btrfs_super_compat_flags(super_block);
5493 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5494 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5495 btrfs_set_super_compat_flags(super_block, newflags);
5497 newflags = btrfs_super_compat_ro_flags(super_block);
5498 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5499 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5500 btrfs_set_super_compat_ro_flags(super_block, newflags);
5502 newflags = btrfs_super_incompat_flags(super_block);
5503 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5504 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5505 btrfs_set_super_incompat_flags(super_block, newflags);
5506 spin_unlock(&fs_info->super_lock);
5508 ret = btrfs_commit_transaction(trans);
5510 mnt_drop_write_file(file);
5515 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5517 struct btrfs_ioctl_send_args *arg;
5521 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5522 struct btrfs_ioctl_send_args_32 args32;
5524 ret = copy_from_user(&args32, argp, sizeof(args32));
5527 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5530 arg->send_fd = args32.send_fd;
5531 arg->clone_sources_count = args32.clone_sources_count;
5532 arg->clone_sources = compat_ptr(args32.clone_sources);
5533 arg->parent_root = args32.parent_root;
5534 arg->flags = args32.flags;
5535 memcpy(arg->reserved, args32.reserved,
5536 sizeof(args32.reserved));
5541 arg = memdup_user(argp, sizeof(*arg));
5543 return PTR_ERR(arg);
5545 ret = btrfs_ioctl_send(file, arg);
5550 long btrfs_ioctl(struct file *file, unsigned int
5551 cmd, unsigned long arg)
5553 struct inode *inode = file_inode(file);
5554 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5555 struct btrfs_root *root = BTRFS_I(inode)->root;
5556 void __user *argp = (void __user *)arg;
5559 case FS_IOC_GETFLAGS:
5560 return btrfs_ioctl_getflags(file, argp);
5561 case FS_IOC_SETFLAGS:
5562 return btrfs_ioctl_setflags(file, argp);
5563 case FS_IOC_GETVERSION:
5564 return btrfs_ioctl_getversion(file, argp);
5566 return btrfs_ioctl_fitrim(file, argp);
5567 case BTRFS_IOC_SNAP_CREATE:
5568 return btrfs_ioctl_snap_create(file, argp, 0);
5569 case BTRFS_IOC_SNAP_CREATE_V2:
5570 return btrfs_ioctl_snap_create_v2(file, argp, 0);
5571 case BTRFS_IOC_SUBVOL_CREATE:
5572 return btrfs_ioctl_snap_create(file, argp, 1);
5573 case BTRFS_IOC_SUBVOL_CREATE_V2:
5574 return btrfs_ioctl_snap_create_v2(file, argp, 1);
5575 case BTRFS_IOC_SNAP_DESTROY:
5576 return btrfs_ioctl_snap_destroy(file, argp);
5577 case BTRFS_IOC_SUBVOL_GETFLAGS:
5578 return btrfs_ioctl_subvol_getflags(file, argp);
5579 case BTRFS_IOC_SUBVOL_SETFLAGS:
5580 return btrfs_ioctl_subvol_setflags(file, argp);
5581 case BTRFS_IOC_DEFAULT_SUBVOL:
5582 return btrfs_ioctl_default_subvol(file, argp);
5583 case BTRFS_IOC_DEFRAG:
5584 return btrfs_ioctl_defrag(file, NULL);
5585 case BTRFS_IOC_DEFRAG_RANGE:
5586 return btrfs_ioctl_defrag(file, argp);
5587 case BTRFS_IOC_RESIZE:
5588 return btrfs_ioctl_resize(file, argp);
5589 case BTRFS_IOC_ADD_DEV:
5590 return btrfs_ioctl_add_dev(fs_info, argp);
5591 case BTRFS_IOC_RM_DEV:
5592 return btrfs_ioctl_rm_dev(file, argp);
5593 case BTRFS_IOC_RM_DEV_V2:
5594 return btrfs_ioctl_rm_dev_v2(file, argp);
5595 case BTRFS_IOC_FS_INFO:
5596 return btrfs_ioctl_fs_info(fs_info, argp);
5597 case BTRFS_IOC_DEV_INFO:
5598 return btrfs_ioctl_dev_info(fs_info, argp);
5599 case BTRFS_IOC_BALANCE:
5600 return btrfs_ioctl_balance(file, NULL);
5601 case BTRFS_IOC_TREE_SEARCH:
5602 return btrfs_ioctl_tree_search(file, argp);
5603 case BTRFS_IOC_TREE_SEARCH_V2:
5604 return btrfs_ioctl_tree_search_v2(file, argp);
5605 case BTRFS_IOC_INO_LOOKUP:
5606 return btrfs_ioctl_ino_lookup(file, argp);
5607 case BTRFS_IOC_INO_PATHS:
5608 return btrfs_ioctl_ino_to_path(root, argp);
5609 case BTRFS_IOC_LOGICAL_INO:
5610 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5611 case BTRFS_IOC_LOGICAL_INO_V2:
5612 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
5613 case BTRFS_IOC_SPACE_INFO:
5614 return btrfs_ioctl_space_info(fs_info, argp);
5615 case BTRFS_IOC_SYNC: {
5618 ret = btrfs_start_delalloc_roots(fs_info, -1);
5621 ret = btrfs_sync_fs(inode->i_sb, 1);
5623 * The transaction thread may want to do more work,
5624 * namely it pokes the cleaner kthread that will start
5625 * processing uncleaned subvols.
5627 wake_up_process(fs_info->transaction_kthread);
5630 case BTRFS_IOC_START_SYNC:
5631 return btrfs_ioctl_start_sync(root, argp);
5632 case BTRFS_IOC_WAIT_SYNC:
5633 return btrfs_ioctl_wait_sync(fs_info, argp);
5634 case BTRFS_IOC_SCRUB:
5635 return btrfs_ioctl_scrub(file, argp);
5636 case BTRFS_IOC_SCRUB_CANCEL:
5637 return btrfs_ioctl_scrub_cancel(fs_info);
5638 case BTRFS_IOC_SCRUB_PROGRESS:
5639 return btrfs_ioctl_scrub_progress(fs_info, argp);
5640 case BTRFS_IOC_BALANCE_V2:
5641 return btrfs_ioctl_balance(file, argp);
5642 case BTRFS_IOC_BALANCE_CTL:
5643 return btrfs_ioctl_balance_ctl(fs_info, arg);
5644 case BTRFS_IOC_BALANCE_PROGRESS:
5645 return btrfs_ioctl_balance_progress(fs_info, argp);
5646 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5647 return btrfs_ioctl_set_received_subvol(file, argp);
5649 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5650 return btrfs_ioctl_set_received_subvol_32(file, argp);
5652 case BTRFS_IOC_SEND:
5653 return _btrfs_ioctl_send(file, argp, false);
5654 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5655 case BTRFS_IOC_SEND_32:
5656 return _btrfs_ioctl_send(file, argp, true);
5658 case BTRFS_IOC_GET_DEV_STATS:
5659 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5660 case BTRFS_IOC_QUOTA_CTL:
5661 return btrfs_ioctl_quota_ctl(file, argp);
5662 case BTRFS_IOC_QGROUP_ASSIGN:
5663 return btrfs_ioctl_qgroup_assign(file, argp);
5664 case BTRFS_IOC_QGROUP_CREATE:
5665 return btrfs_ioctl_qgroup_create(file, argp);
5666 case BTRFS_IOC_QGROUP_LIMIT:
5667 return btrfs_ioctl_qgroup_limit(file, argp);
5668 case BTRFS_IOC_QUOTA_RESCAN:
5669 return btrfs_ioctl_quota_rescan(file, argp);
5670 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5671 return btrfs_ioctl_quota_rescan_status(file, argp);
5672 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5673 return btrfs_ioctl_quota_rescan_wait(file, argp);
5674 case BTRFS_IOC_DEV_REPLACE:
5675 return btrfs_ioctl_dev_replace(fs_info, argp);
5676 case BTRFS_IOC_GET_FSLABEL:
5677 return btrfs_ioctl_get_fslabel(file, argp);
5678 case BTRFS_IOC_SET_FSLABEL:
5679 return btrfs_ioctl_set_fslabel(file, argp);
5680 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5681 return btrfs_ioctl_get_supported_features(argp);
5682 case BTRFS_IOC_GET_FEATURES:
5683 return btrfs_ioctl_get_features(file, argp);
5684 case BTRFS_IOC_SET_FEATURES:
5685 return btrfs_ioctl_set_features(file, argp);
5686 case FS_IOC_FSGETXATTR:
5687 return btrfs_ioctl_fsgetxattr(file, argp);
5688 case FS_IOC_FSSETXATTR:
5689 return btrfs_ioctl_fssetxattr(file, argp);
5690 case BTRFS_IOC_GET_SUBVOL_INFO:
5691 return btrfs_ioctl_get_subvol_info(file, argp);
5692 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5693 return btrfs_ioctl_get_subvol_rootref(file, argp);
5694 case BTRFS_IOC_INO_LOOKUP_USER:
5695 return btrfs_ioctl_ino_lookup_user(file, argp);
5701 #ifdef CONFIG_COMPAT
5702 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5705 * These all access 32-bit values anyway so no further
5706 * handling is necessary.
5709 case FS_IOC32_GETFLAGS:
5710 cmd = FS_IOC_GETFLAGS;
5712 case FS_IOC32_SETFLAGS:
5713 cmd = FS_IOC_SETFLAGS;
5715 case FS_IOC32_GETVERSION:
5716 cmd = FS_IOC_GETVERSION;
5720 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));