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"
48 #include "block-group.h"
51 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
52 * structures are incorrect, as the timespec structure from userspace
53 * is 4 bytes too small. We define these alternatives here to teach
54 * the kernel about the 32-bit struct packing.
56 struct btrfs_ioctl_timespec_32
{
59 } __attribute__ ((__packed__
));
61 struct btrfs_ioctl_received_subvol_args_32
{
62 char uuid
[BTRFS_UUID_SIZE
]; /* in */
63 __u64 stransid
; /* in */
64 __u64 rtransid
; /* out */
65 struct btrfs_ioctl_timespec_32 stime
; /* in */
66 struct btrfs_ioctl_timespec_32 rtime
; /* out */
68 __u64 reserved
[16]; /* in */
69 } __attribute__ ((__packed__
));
71 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
72 struct btrfs_ioctl_received_subvol_args_32)
75 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
76 struct btrfs_ioctl_send_args_32
{
77 __s64 send_fd
; /* in */
78 __u64 clone_sources_count
; /* in */
79 compat_uptr_t clone_sources
; /* in */
80 __u64 parent_root
; /* in */
82 __u64 reserved
[4]; /* in */
83 } __attribute__ ((__packed__
));
85 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
86 struct btrfs_ioctl_send_args_32)
89 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
90 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
,
93 /* Mask out flags that are inappropriate for the given type of inode. */
94 static unsigned int btrfs_mask_fsflags_for_type(struct inode
*inode
,
97 if (S_ISDIR(inode
->i_mode
))
99 else if (S_ISREG(inode
->i_mode
))
100 return flags
& ~FS_DIRSYNC_FL
;
102 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
106 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
109 static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags
)
111 unsigned int iflags
= 0;
113 if (flags
& BTRFS_INODE_SYNC
)
114 iflags
|= FS_SYNC_FL
;
115 if (flags
& BTRFS_INODE_IMMUTABLE
)
116 iflags
|= FS_IMMUTABLE_FL
;
117 if (flags
& BTRFS_INODE_APPEND
)
118 iflags
|= FS_APPEND_FL
;
119 if (flags
& BTRFS_INODE_NODUMP
)
120 iflags
|= FS_NODUMP_FL
;
121 if (flags
& BTRFS_INODE_NOATIME
)
122 iflags
|= FS_NOATIME_FL
;
123 if (flags
& BTRFS_INODE_DIRSYNC
)
124 iflags
|= FS_DIRSYNC_FL
;
125 if (flags
& BTRFS_INODE_NODATACOW
)
126 iflags
|= FS_NOCOW_FL
;
128 if (flags
& BTRFS_INODE_NOCOMPRESS
)
129 iflags
|= FS_NOCOMP_FL
;
130 else if (flags
& BTRFS_INODE_COMPRESS
)
131 iflags
|= FS_COMPR_FL
;
137 * Update inode->i_flags based on the btrfs internal flags.
139 void btrfs_sync_inode_flags_to_i_flags(struct inode
*inode
)
141 struct btrfs_inode
*binode
= BTRFS_I(inode
);
142 unsigned int new_fl
= 0;
144 if (binode
->flags
& BTRFS_INODE_SYNC
)
146 if (binode
->flags
& BTRFS_INODE_IMMUTABLE
)
147 new_fl
|= S_IMMUTABLE
;
148 if (binode
->flags
& BTRFS_INODE_APPEND
)
150 if (binode
->flags
& BTRFS_INODE_NOATIME
)
152 if (binode
->flags
& BTRFS_INODE_DIRSYNC
)
155 set_mask_bits(&inode
->i_flags
,
156 S_SYNC
| S_APPEND
| S_IMMUTABLE
| S_NOATIME
| S_DIRSYNC
,
160 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
162 struct btrfs_inode
*binode
= BTRFS_I(file_inode(file
));
163 unsigned int flags
= btrfs_inode_flags_to_fsflags(binode
->flags
);
165 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
170 /* Check if @flags are a supported and valid set of FS_*_FL flags */
171 static int check_fsflags(unsigned int flags
)
173 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
174 FS_NOATIME_FL
| FS_NODUMP_FL
| \
175 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
176 FS_NOCOMP_FL
| FS_COMPR_FL
|
180 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
186 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
188 struct inode
*inode
= file_inode(file
);
189 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
190 struct btrfs_inode
*binode
= BTRFS_I(inode
);
191 struct btrfs_root
*root
= binode
->root
;
192 struct btrfs_trans_handle
*trans
;
193 unsigned int fsflags
, old_fsflags
;
195 const char *comp
= NULL
;
196 u32 binode_flags
= binode
->flags
;
198 if (!inode_owner_or_capable(inode
))
201 if (btrfs_root_readonly(root
))
204 if (copy_from_user(&fsflags
, arg
, sizeof(fsflags
)))
207 ret
= check_fsflags(fsflags
);
211 ret
= mnt_want_write_file(file
);
217 fsflags
= btrfs_mask_fsflags_for_type(inode
, fsflags
);
218 old_fsflags
= btrfs_inode_flags_to_fsflags(binode
->flags
);
219 ret
= vfs_ioc_setflags_prepare(inode
, old_fsflags
, fsflags
);
223 if (fsflags
& FS_SYNC_FL
)
224 binode_flags
|= BTRFS_INODE_SYNC
;
226 binode_flags
&= ~BTRFS_INODE_SYNC
;
227 if (fsflags
& FS_IMMUTABLE_FL
)
228 binode_flags
|= BTRFS_INODE_IMMUTABLE
;
230 binode_flags
&= ~BTRFS_INODE_IMMUTABLE
;
231 if (fsflags
& FS_APPEND_FL
)
232 binode_flags
|= BTRFS_INODE_APPEND
;
234 binode_flags
&= ~BTRFS_INODE_APPEND
;
235 if (fsflags
& FS_NODUMP_FL
)
236 binode_flags
|= BTRFS_INODE_NODUMP
;
238 binode_flags
&= ~BTRFS_INODE_NODUMP
;
239 if (fsflags
& FS_NOATIME_FL
)
240 binode_flags
|= BTRFS_INODE_NOATIME
;
242 binode_flags
&= ~BTRFS_INODE_NOATIME
;
243 if (fsflags
& FS_DIRSYNC_FL
)
244 binode_flags
|= BTRFS_INODE_DIRSYNC
;
246 binode_flags
&= ~BTRFS_INODE_DIRSYNC
;
247 if (fsflags
& FS_NOCOW_FL
) {
248 if (S_ISREG(inode
->i_mode
)) {
250 * It's safe to turn csums off here, no extents exist.
251 * Otherwise we want the flag to reflect the real COW
252 * status of the file and will not set it.
254 if (inode
->i_size
== 0)
255 binode_flags
|= BTRFS_INODE_NODATACOW
|
256 BTRFS_INODE_NODATASUM
;
258 binode_flags
|= BTRFS_INODE_NODATACOW
;
262 * Revert back under same assumptions as above
264 if (S_ISREG(inode
->i_mode
)) {
265 if (inode
->i_size
== 0)
266 binode_flags
&= ~(BTRFS_INODE_NODATACOW
|
267 BTRFS_INODE_NODATASUM
);
269 binode_flags
&= ~BTRFS_INODE_NODATACOW
;
274 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
275 * flag may be changed automatically if compression code won't make
278 if (fsflags
& FS_NOCOMP_FL
) {
279 binode_flags
&= ~BTRFS_INODE_COMPRESS
;
280 binode_flags
|= BTRFS_INODE_NOCOMPRESS
;
281 } else if (fsflags
& FS_COMPR_FL
) {
283 if (IS_SWAPFILE(inode
)) {
288 binode_flags
|= BTRFS_INODE_COMPRESS
;
289 binode_flags
&= ~BTRFS_INODE_NOCOMPRESS
;
291 comp
= btrfs_compress_type2str(fs_info
->compress_type
);
292 if (!comp
|| comp
[0] == 0)
293 comp
= btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB
);
295 binode_flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
302 trans
= btrfs_start_transaction(root
, 3);
304 ret
= PTR_ERR(trans
);
309 ret
= btrfs_set_prop(trans
, inode
, "btrfs.compression", comp
,
312 btrfs_abort_transaction(trans
, ret
);
316 ret
= btrfs_set_prop(trans
, inode
, "btrfs.compression", NULL
,
318 if (ret
&& ret
!= -ENODATA
) {
319 btrfs_abort_transaction(trans
, ret
);
324 binode
->flags
= binode_flags
;
325 btrfs_sync_inode_flags_to_i_flags(inode
);
326 inode_inc_iversion(inode
);
327 inode
->i_ctime
= current_time(inode
);
328 ret
= btrfs_update_inode(trans
, root
, inode
);
331 btrfs_end_transaction(trans
);
334 mnt_drop_write_file(file
);
339 * Translate btrfs internal inode flags to xflags as expected by the
340 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
343 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags
)
345 unsigned int xflags
= 0;
347 if (flags
& BTRFS_INODE_APPEND
)
348 xflags
|= FS_XFLAG_APPEND
;
349 if (flags
& BTRFS_INODE_IMMUTABLE
)
350 xflags
|= FS_XFLAG_IMMUTABLE
;
351 if (flags
& BTRFS_INODE_NOATIME
)
352 xflags
|= FS_XFLAG_NOATIME
;
353 if (flags
& BTRFS_INODE_NODUMP
)
354 xflags
|= FS_XFLAG_NODUMP
;
355 if (flags
& BTRFS_INODE_SYNC
)
356 xflags
|= FS_XFLAG_SYNC
;
361 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
362 static int check_xflags(unsigned int flags
)
364 if (flags
& ~(FS_XFLAG_APPEND
| FS_XFLAG_IMMUTABLE
| FS_XFLAG_NOATIME
|
365 FS_XFLAG_NODUMP
| FS_XFLAG_SYNC
))
371 * Set the xflags from the internal inode flags. The remaining items of fsxattr
374 static int btrfs_ioctl_fsgetxattr(struct file
*file
, void __user
*arg
)
376 struct btrfs_inode
*binode
= BTRFS_I(file_inode(file
));
379 simple_fill_fsxattr(&fa
, btrfs_inode_flags_to_xflags(binode
->flags
));
380 if (copy_to_user(arg
, &fa
, sizeof(fa
)))
386 static int btrfs_ioctl_fssetxattr(struct file
*file
, void __user
*arg
)
388 struct inode
*inode
= file_inode(file
);
389 struct btrfs_inode
*binode
= BTRFS_I(inode
);
390 struct btrfs_root
*root
= binode
->root
;
391 struct btrfs_trans_handle
*trans
;
392 struct fsxattr fa
, old_fa
;
394 unsigned old_i_flags
;
397 if (!inode_owner_or_capable(inode
))
400 if (btrfs_root_readonly(root
))
403 if (copy_from_user(&fa
, arg
, sizeof(fa
)))
406 ret
= check_xflags(fa
.fsx_xflags
);
410 if (fa
.fsx_extsize
!= 0 || fa
.fsx_projid
!= 0 || fa
.fsx_cowextsize
!= 0)
413 ret
= mnt_want_write_file(file
);
419 old_flags
= binode
->flags
;
420 old_i_flags
= inode
->i_flags
;
422 simple_fill_fsxattr(&old_fa
,
423 btrfs_inode_flags_to_xflags(binode
->flags
));
424 ret
= vfs_ioc_fssetxattr_check(inode
, &old_fa
, &fa
);
428 if (fa
.fsx_xflags
& FS_XFLAG_SYNC
)
429 binode
->flags
|= BTRFS_INODE_SYNC
;
431 binode
->flags
&= ~BTRFS_INODE_SYNC
;
432 if (fa
.fsx_xflags
& FS_XFLAG_IMMUTABLE
)
433 binode
->flags
|= BTRFS_INODE_IMMUTABLE
;
435 binode
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
436 if (fa
.fsx_xflags
& FS_XFLAG_APPEND
)
437 binode
->flags
|= BTRFS_INODE_APPEND
;
439 binode
->flags
&= ~BTRFS_INODE_APPEND
;
440 if (fa
.fsx_xflags
& FS_XFLAG_NODUMP
)
441 binode
->flags
|= BTRFS_INODE_NODUMP
;
443 binode
->flags
&= ~BTRFS_INODE_NODUMP
;
444 if (fa
.fsx_xflags
& FS_XFLAG_NOATIME
)
445 binode
->flags
|= BTRFS_INODE_NOATIME
;
447 binode
->flags
&= ~BTRFS_INODE_NOATIME
;
449 /* 1 item for the inode */
450 trans
= btrfs_start_transaction(root
, 1);
452 ret
= PTR_ERR(trans
);
456 btrfs_sync_inode_flags_to_i_flags(inode
);
457 inode_inc_iversion(inode
);
458 inode
->i_ctime
= current_time(inode
);
459 ret
= btrfs_update_inode(trans
, root
, inode
);
461 btrfs_end_transaction(trans
);
465 binode
->flags
= old_flags
;
466 inode
->i_flags
= old_i_flags
;
470 mnt_drop_write_file(file
);
475 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
477 struct inode
*inode
= file_inode(file
);
479 return put_user(inode
->i_generation
, arg
);
482 static noinline
int btrfs_ioctl_fitrim(struct btrfs_fs_info
*fs_info
,
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 __pure
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
);
708 btrfs_abort_transaction(trans
, ret
);
712 ret
= btrfs_add_root_ref(trans
, objectid
, root
->root_key
.objectid
,
713 btrfs_ino(BTRFS_I(dir
)), index
, name
, namelen
);
715 btrfs_abort_transaction(trans
, ret
);
719 ret
= btrfs_uuid_tree_add(trans
, root_item
->uuid
,
720 BTRFS_UUID_KEY_SUBVOL
, objectid
);
722 btrfs_abort_transaction(trans
, ret
);
726 trans
->block_rsv
= NULL
;
727 trans
->bytes_reserved
= 0;
728 btrfs_subvolume_release_metadata(fs_info
, &block_rsv
);
731 *async_transid
= trans
->transid
;
732 err
= btrfs_commit_transaction_async(trans
, 1);
734 err
= btrfs_commit_transaction(trans
);
736 err
= btrfs_commit_transaction(trans
);
742 inode
= btrfs_lookup_dentry(dir
, dentry
);
744 return PTR_ERR(inode
);
745 d_instantiate(dentry
, inode
);
754 static int create_snapshot(struct btrfs_root
*root
, struct inode
*dir
,
755 struct dentry
*dentry
,
756 u64
*async_transid
, bool readonly
,
757 struct btrfs_qgroup_inherit
*inherit
)
759 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
761 struct btrfs_pending_snapshot
*pending_snapshot
;
762 struct btrfs_trans_handle
*trans
;
764 bool snapshot_force_cow
= false;
766 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
769 if (atomic_read(&root
->nr_swapfiles
)) {
771 "cannot snapshot subvolume with active swapfile");
775 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_KERNEL
);
776 if (!pending_snapshot
)
779 pending_snapshot
->root_item
= kzalloc(sizeof(struct btrfs_root_item
),
781 pending_snapshot
->path
= btrfs_alloc_path();
782 if (!pending_snapshot
->root_item
|| !pending_snapshot
->path
) {
788 * Force new buffered writes to reserve space even when NOCOW is
789 * possible. This is to avoid later writeback (running dealloc) to
790 * fallback to COW mode and unexpectedly fail with ENOSPC.
792 atomic_inc(&root
->will_be_snapshotted
);
793 smp_mb__after_atomic();
794 /* wait for no snapshot writes */
795 wait_event(root
->subv_writers
->wait
,
796 percpu_counter_sum(&root
->subv_writers
->counter
) == 0);
798 ret
= btrfs_start_delalloc_snapshot(root
);
803 * All previous writes have started writeback in NOCOW mode, so now
804 * we force future writes to fallback to COW mode during snapshot
807 atomic_inc(&root
->snapshot_force_cow
);
808 snapshot_force_cow
= true;
810 btrfs_wait_ordered_extents(root
, U64_MAX
, 0, (u64
)-1);
812 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
813 BTRFS_BLOCK_RSV_TEMP
);
815 * 1 - parent dir inode
818 * 2 - root ref/backref
819 * 1 - root of snapshot
822 ret
= btrfs_subvolume_reserve_metadata(BTRFS_I(dir
)->root
,
823 &pending_snapshot
->block_rsv
, 8,
828 pending_snapshot
->dentry
= dentry
;
829 pending_snapshot
->root
= root
;
830 pending_snapshot
->readonly
= readonly
;
831 pending_snapshot
->dir
= dir
;
832 pending_snapshot
->inherit
= inherit
;
834 trans
= btrfs_start_transaction(root
, 0);
836 ret
= PTR_ERR(trans
);
840 spin_lock(&fs_info
->trans_lock
);
841 list_add(&pending_snapshot
->list
,
842 &trans
->transaction
->pending_snapshots
);
843 spin_unlock(&fs_info
->trans_lock
);
845 *async_transid
= trans
->transid
;
846 ret
= btrfs_commit_transaction_async(trans
, 1);
848 ret
= btrfs_commit_transaction(trans
);
850 ret
= btrfs_commit_transaction(trans
);
855 ret
= pending_snapshot
->error
;
859 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
863 inode
= btrfs_lookup_dentry(d_inode(dentry
->d_parent
), dentry
);
865 ret
= PTR_ERR(inode
);
869 d_instantiate(dentry
, inode
);
872 btrfs_subvolume_release_metadata(fs_info
, &pending_snapshot
->block_rsv
);
874 if (snapshot_force_cow
)
875 atomic_dec(&root
->snapshot_force_cow
);
876 if (atomic_dec_and_test(&root
->will_be_snapshotted
))
877 wake_up_var(&root
->will_be_snapshotted
);
879 kfree(pending_snapshot
->root_item
);
880 btrfs_free_path(pending_snapshot
->path
);
881 kfree(pending_snapshot
);
886 /* copy of may_delete in fs/namei.c()
887 * Check whether we can remove a link victim from directory dir, check
888 * whether the type of victim is right.
889 * 1. We can't do it if dir is read-only (done in permission())
890 * 2. We should have write and exec permissions on dir
891 * 3. We can't remove anything from append-only dir
892 * 4. We can't do anything with immutable dir (done in permission())
893 * 5. If the sticky bit on dir is set we should either
894 * a. be owner of dir, or
895 * b. be owner of victim, or
896 * c. have CAP_FOWNER capability
897 * 6. If the victim is append-only or immutable we can't do anything with
898 * links pointing to it.
899 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
900 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
901 * 9. We can't remove a root or mountpoint.
902 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
903 * nfs_async_unlink().
906 static int btrfs_may_delete(struct inode
*dir
, struct dentry
*victim
, int isdir
)
910 if (d_really_is_negative(victim
))
913 BUG_ON(d_inode(victim
->d_parent
) != dir
);
914 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
916 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
921 if (check_sticky(dir
, d_inode(victim
)) || IS_APPEND(d_inode(victim
)) ||
922 IS_IMMUTABLE(d_inode(victim
)) || IS_SWAPFILE(d_inode(victim
)))
925 if (!d_is_dir(victim
))
929 } else if (d_is_dir(victim
))
933 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
938 /* copy of may_create in fs/namei.c() */
939 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
941 if (d_really_is_positive(child
))
945 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
949 * Create a new subvolume below @parent. This is largely modeled after
950 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
951 * inside this filesystem so it's quite a bit simpler.
953 static noinline
int btrfs_mksubvol(const struct path
*parent
,
954 const char *name
, int namelen
,
955 struct btrfs_root
*snap_src
,
956 u64
*async_transid
, bool readonly
,
957 struct btrfs_qgroup_inherit
*inherit
)
959 struct inode
*dir
= d_inode(parent
->dentry
);
960 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
961 struct dentry
*dentry
;
964 error
= down_write_killable_nested(&dir
->i_rwsem
, I_MUTEX_PARENT
);
968 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
969 error
= PTR_ERR(dentry
);
973 error
= btrfs_may_create(dir
, dentry
);
978 * even if this name doesn't exist, we may get hash collisions.
979 * check for them now when we can safely fail
981 error
= btrfs_check_dir_item_collision(BTRFS_I(dir
)->root
,
987 down_read(&fs_info
->subvol_sem
);
989 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
993 error
= create_snapshot(snap_src
, dir
, dentry
,
994 async_transid
, readonly
, inherit
);
996 error
= create_subvol(dir
, dentry
, name
, namelen
,
997 async_transid
, inherit
);
1000 fsnotify_mkdir(dir
, dentry
);
1002 up_read(&fs_info
->subvol_sem
);
1011 * When we're defragging a range, we don't want to kick it off again
1012 * if it is really just waiting for delalloc to send it down.
1013 * If we find a nice big extent or delalloc range for the bytes in the
1014 * file you want to defrag, we return 0 to let you know to skip this
1017 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, u32 thresh
)
1019 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1020 struct extent_map
*em
= NULL
;
1021 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1024 read_lock(&em_tree
->lock
);
1025 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_SIZE
);
1026 read_unlock(&em_tree
->lock
);
1029 end
= extent_map_end(em
);
1030 free_extent_map(em
);
1031 if (end
- offset
> thresh
)
1034 /* if we already have a nice delalloc here, just stop */
1036 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
1037 thresh
, EXTENT_DELALLOC
, 1);
1044 * helper function to walk through a file and find extents
1045 * newer than a specific transid, and smaller than thresh.
1047 * This is used by the defragging code to find new and small
1050 static int find_new_extents(struct btrfs_root
*root
,
1051 struct inode
*inode
, u64 newer_than
,
1052 u64
*off
, u32 thresh
)
1054 struct btrfs_path
*path
;
1055 struct btrfs_key min_key
;
1056 struct extent_buffer
*leaf
;
1057 struct btrfs_file_extent_item
*extent
;
1060 u64 ino
= btrfs_ino(BTRFS_I(inode
));
1062 path
= btrfs_alloc_path();
1066 min_key
.objectid
= ino
;
1067 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
1068 min_key
.offset
= *off
;
1071 ret
= btrfs_search_forward(root
, &min_key
, path
, newer_than
);
1075 if (min_key
.objectid
!= ino
)
1077 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1080 leaf
= path
->nodes
[0];
1081 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1082 struct btrfs_file_extent_item
);
1084 type
= btrfs_file_extent_type(leaf
, extent
);
1085 if (type
== BTRFS_FILE_EXTENT_REG
&&
1086 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
1087 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
1088 *off
= min_key
.offset
;
1089 btrfs_free_path(path
);
1094 if (path
->slots
[0] < btrfs_header_nritems(leaf
)) {
1095 btrfs_item_key_to_cpu(leaf
, &min_key
, path
->slots
[0]);
1099 if (min_key
.offset
== (u64
)-1)
1103 btrfs_release_path(path
);
1106 btrfs_free_path(path
);
1110 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
1112 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1113 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1114 struct extent_map
*em
;
1115 u64 len
= PAGE_SIZE
;
1118 * hopefully we have this extent in the tree already, try without
1119 * the full extent lock
1121 read_lock(&em_tree
->lock
);
1122 em
= lookup_extent_mapping(em_tree
, start
, len
);
1123 read_unlock(&em_tree
->lock
);
1126 struct extent_state
*cached
= NULL
;
1127 u64 end
= start
+ len
- 1;
1129 /* get the big lock and read metadata off disk */
1130 lock_extent_bits(io_tree
, start
, end
, &cached
);
1131 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, start
, len
);
1132 unlock_extent_cached(io_tree
, start
, end
, &cached
);
1141 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
1143 struct extent_map
*next
;
1146 /* this is the last extent */
1147 if (em
->start
+ em
->len
>= i_size_read(inode
))
1150 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
1151 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
1153 else if ((em
->block_start
+ em
->block_len
== next
->block_start
) &&
1154 (em
->block_len
> SZ_128K
&& next
->block_len
> SZ_128K
))
1157 free_extent_map(next
);
1161 static int should_defrag_range(struct inode
*inode
, u64 start
, u32 thresh
,
1162 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
1165 struct extent_map
*em
;
1167 bool next_mergeable
= true;
1168 bool prev_mergeable
= true;
1171 * make sure that once we start defragging an extent, we keep on
1174 if (start
< *defrag_end
)
1179 em
= defrag_lookup_extent(inode
, start
);
1183 /* this will cover holes, and inline extents */
1184 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
1190 prev_mergeable
= false;
1192 next_mergeable
= defrag_check_next_extent(inode
, em
);
1194 * we hit a real extent, if it is big or the next extent is not a
1195 * real extent, don't bother defragging it
1197 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
1198 (em
->len
>= thresh
|| (!next_mergeable
&& !prev_mergeable
)))
1202 * last_len ends up being a counter of how many bytes we've defragged.
1203 * every time we choose not to defrag an extent, we reset *last_len
1204 * so that the next tiny extent will force a defrag.
1206 * The end result of this is that tiny extents before a single big
1207 * extent will force at least part of that big extent to be defragged.
1210 *defrag_end
= extent_map_end(em
);
1213 *skip
= extent_map_end(em
);
1217 free_extent_map(em
);
1222 * it doesn't do much good to defrag one or two pages
1223 * at a time. This pulls in a nice chunk of pages
1224 * to COW and defrag.
1226 * It also makes sure the delalloc code has enough
1227 * dirty data to avoid making new small extents as part
1230 * It's a good idea to start RA on this range
1231 * before calling this.
1233 static int cluster_pages_for_defrag(struct inode
*inode
,
1234 struct page
**pages
,
1235 unsigned long start_index
,
1236 unsigned long num_pages
)
1238 unsigned long file_end
;
1239 u64 isize
= i_size_read(inode
);
1246 struct btrfs_ordered_extent
*ordered
;
1247 struct extent_state
*cached_state
= NULL
;
1248 struct extent_io_tree
*tree
;
1249 struct extent_changeset
*data_reserved
= NULL
;
1250 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
1252 file_end
= (isize
- 1) >> PAGE_SHIFT
;
1253 if (!isize
|| start_index
> file_end
)
1256 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
1258 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
,
1259 start_index
<< PAGE_SHIFT
,
1260 page_cnt
<< PAGE_SHIFT
);
1264 tree
= &BTRFS_I(inode
)->io_tree
;
1266 /* step one, lock all the pages */
1267 for (i
= 0; i
< page_cnt
; i
++) {
1270 page
= find_or_create_page(inode
->i_mapping
,
1271 start_index
+ i
, mask
);
1275 page_start
= page_offset(page
);
1276 page_end
= page_start
+ PAGE_SIZE
- 1;
1278 lock_extent_bits(tree
, page_start
, page_end
,
1280 ordered
= btrfs_lookup_ordered_extent(inode
,
1282 unlock_extent_cached(tree
, page_start
, page_end
,
1288 btrfs_start_ordered_extent(inode
, ordered
, 1);
1289 btrfs_put_ordered_extent(ordered
);
1292 * we unlocked the page above, so we need check if
1293 * it was released or not.
1295 if (page
->mapping
!= inode
->i_mapping
) {
1302 if (!PageUptodate(page
)) {
1303 btrfs_readpage(NULL
, page
);
1305 if (!PageUptodate(page
)) {
1313 if (page
->mapping
!= inode
->i_mapping
) {
1325 if (!(inode
->i_sb
->s_flags
& SB_ACTIVE
))
1329 * so now we have a nice long stream of locked
1330 * and up to date pages, lets wait on them
1332 for (i
= 0; i
< i_done
; i
++)
1333 wait_on_page_writeback(pages
[i
]);
1335 page_start
= page_offset(pages
[0]);
1336 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_SIZE
;
1338 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1339 page_start
, page_end
- 1, &cached_state
);
1340 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1341 page_end
- 1, EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
1342 EXTENT_DEFRAG
, 0, 0, &cached_state
);
1344 if (i_done
!= page_cnt
) {
1345 spin_lock(&BTRFS_I(inode
)->lock
);
1346 btrfs_mod_outstanding_extents(BTRFS_I(inode
), 1);
1347 spin_unlock(&BTRFS_I(inode
)->lock
);
1348 btrfs_delalloc_release_space(inode
, data_reserved
,
1349 start_index
<< PAGE_SHIFT
,
1350 (page_cnt
- i_done
) << PAGE_SHIFT
, true);
1354 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1357 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1358 page_start
, page_end
- 1, &cached_state
);
1360 for (i
= 0; i
< i_done
; i
++) {
1361 clear_page_dirty_for_io(pages
[i
]);
1362 ClearPageChecked(pages
[i
]);
1363 set_page_extent_mapped(pages
[i
]);
1364 set_page_dirty(pages
[i
]);
1365 unlock_page(pages
[i
]);
1368 btrfs_delalloc_release_extents(BTRFS_I(inode
), page_cnt
<< PAGE_SHIFT
);
1369 extent_changeset_free(data_reserved
);
1372 for (i
= 0; i
< i_done
; i
++) {
1373 unlock_page(pages
[i
]);
1376 btrfs_delalloc_release_space(inode
, data_reserved
,
1377 start_index
<< PAGE_SHIFT
,
1378 page_cnt
<< PAGE_SHIFT
, true);
1379 btrfs_delalloc_release_extents(BTRFS_I(inode
), page_cnt
<< PAGE_SHIFT
);
1380 extent_changeset_free(data_reserved
);
1385 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1386 struct btrfs_ioctl_defrag_range_args
*range
,
1387 u64 newer_than
, unsigned long max_to_defrag
)
1389 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1390 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1391 struct file_ra_state
*ra
= NULL
;
1392 unsigned long last_index
;
1393 u64 isize
= i_size_read(inode
);
1397 u64 newer_off
= range
->start
;
1399 unsigned long ra_index
= 0;
1401 int defrag_count
= 0;
1402 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1403 u32 extent_thresh
= range
->extent_thresh
;
1404 unsigned long max_cluster
= SZ_256K
>> PAGE_SHIFT
;
1405 unsigned long cluster
= max_cluster
;
1406 u64 new_align
= ~((u64
)SZ_128K
- 1);
1407 struct page
**pages
= NULL
;
1408 bool do_compress
= range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
;
1413 if (range
->start
>= isize
)
1417 if (range
->compress_type
>= BTRFS_NR_COMPRESS_TYPES
)
1419 if (range
->compress_type
)
1420 compress_type
= range
->compress_type
;
1423 if (extent_thresh
== 0)
1424 extent_thresh
= SZ_256K
;
1427 * If we were not given a file, allocate a readahead context. As
1428 * readahead is just an optimization, defrag will work without it so
1429 * we don't error out.
1432 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
1434 file_ra_state_init(ra
, inode
->i_mapping
);
1439 pages
= kmalloc_array(max_cluster
, sizeof(struct page
*), GFP_KERNEL
);
1445 /* find the last page to defrag */
1446 if (range
->start
+ range
->len
> range
->start
) {
1447 last_index
= min_t(u64
, isize
- 1,
1448 range
->start
+ range
->len
- 1) >> PAGE_SHIFT
;
1450 last_index
= (isize
- 1) >> PAGE_SHIFT
;
1454 ret
= find_new_extents(root
, inode
, newer_than
,
1455 &newer_off
, SZ_64K
);
1457 range
->start
= newer_off
;
1459 * we always align our defrag to help keep
1460 * the extents in the file evenly spaced
1462 i
= (newer_off
& new_align
) >> PAGE_SHIFT
;
1466 i
= range
->start
>> PAGE_SHIFT
;
1469 max_to_defrag
= last_index
- i
+ 1;
1472 * make writeback starts from i, so the defrag range can be
1473 * written sequentially.
1475 if (i
< inode
->i_mapping
->writeback_index
)
1476 inode
->i_mapping
->writeback_index
= i
;
1478 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1479 (i
< DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
))) {
1481 * make sure we stop running if someone unmounts
1484 if (!(inode
->i_sb
->s_flags
& SB_ACTIVE
))
1487 if (btrfs_defrag_cancelled(fs_info
)) {
1488 btrfs_debug(fs_info
, "defrag_file cancelled");
1493 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_SHIFT
,
1494 extent_thresh
, &last_len
, &skip
,
1495 &defrag_end
, do_compress
)){
1498 * the should_defrag function tells us how much to skip
1499 * bump our counter by the suggested amount
1501 next
= DIV_ROUND_UP(skip
, PAGE_SIZE
);
1502 i
= max(i
+ 1, next
);
1507 cluster
= (PAGE_ALIGN(defrag_end
) >>
1509 cluster
= min(cluster
, max_cluster
);
1511 cluster
= max_cluster
;
1514 if (i
+ cluster
> ra_index
) {
1515 ra_index
= max(i
, ra_index
);
1517 page_cache_sync_readahead(inode
->i_mapping
, ra
,
1518 file
, ra_index
, cluster
);
1519 ra_index
+= cluster
;
1523 if (IS_SWAPFILE(inode
)) {
1527 BTRFS_I(inode
)->defrag_compress
= compress_type
;
1528 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1531 inode_unlock(inode
);
1535 defrag_count
+= ret
;
1536 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1537 inode_unlock(inode
);
1540 if (newer_off
== (u64
)-1)
1546 newer_off
= max(newer_off
+ 1,
1547 (u64
)i
<< PAGE_SHIFT
);
1549 ret
= find_new_extents(root
, inode
, newer_than
,
1550 &newer_off
, SZ_64K
);
1552 range
->start
= newer_off
;
1553 i
= (newer_off
& new_align
) >> PAGE_SHIFT
;
1560 last_len
+= ret
<< PAGE_SHIFT
;
1568 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
)) {
1569 filemap_flush(inode
->i_mapping
);
1570 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1571 &BTRFS_I(inode
)->runtime_flags
))
1572 filemap_flush(inode
->i_mapping
);
1575 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1576 btrfs_set_fs_incompat(fs_info
, COMPRESS_LZO
);
1577 } else if (range
->compress_type
== BTRFS_COMPRESS_ZSTD
) {
1578 btrfs_set_fs_incompat(fs_info
, COMPRESS_ZSTD
);
1586 BTRFS_I(inode
)->defrag_compress
= BTRFS_COMPRESS_NONE
;
1587 inode_unlock(inode
);
1595 static noinline
int btrfs_ioctl_resize(struct file
*file
,
1598 struct inode
*inode
= file_inode(file
);
1599 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1603 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1604 struct btrfs_ioctl_vol_args
*vol_args
;
1605 struct btrfs_trans_handle
*trans
;
1606 struct btrfs_device
*device
= NULL
;
1609 char *devstr
= NULL
;
1613 if (!capable(CAP_SYS_ADMIN
))
1616 ret
= mnt_want_write_file(file
);
1620 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
1621 mnt_drop_write_file(file
);
1622 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
1625 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1626 if (IS_ERR(vol_args
)) {
1627 ret
= PTR_ERR(vol_args
);
1631 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1633 sizestr
= vol_args
->name
;
1634 devstr
= strchr(sizestr
, ':');
1636 sizestr
= devstr
+ 1;
1638 devstr
= vol_args
->name
;
1639 ret
= kstrtoull(devstr
, 10, &devid
);
1646 btrfs_info(fs_info
, "resizing devid %llu", devid
);
1649 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, NULL
, NULL
, true);
1651 btrfs_info(fs_info
, "resizer unable to find device %llu",
1657 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1659 "resizer unable to apply on readonly device %llu",
1665 if (!strcmp(sizestr
, "max"))
1666 new_size
= device
->bdev
->bd_inode
->i_size
;
1668 if (sizestr
[0] == '-') {
1671 } else if (sizestr
[0] == '+') {
1675 new_size
= memparse(sizestr
, &retptr
);
1676 if (*retptr
!= '\0' || new_size
== 0) {
1682 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1687 old_size
= btrfs_device_get_total_bytes(device
);
1690 if (new_size
> old_size
) {
1694 new_size
= old_size
- new_size
;
1695 } else if (mod
> 0) {
1696 if (new_size
> ULLONG_MAX
- old_size
) {
1700 new_size
= old_size
+ new_size
;
1703 if (new_size
< SZ_256M
) {
1707 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1712 new_size
= round_down(new_size
, fs_info
->sectorsize
);
1714 btrfs_info_in_rcu(fs_info
, "new size for %s is %llu",
1715 rcu_str_deref(device
->name
), new_size
);
1717 if (new_size
> old_size
) {
1718 trans
= btrfs_start_transaction(root
, 0);
1719 if (IS_ERR(trans
)) {
1720 ret
= PTR_ERR(trans
);
1723 ret
= btrfs_grow_device(trans
, device
, new_size
);
1724 btrfs_commit_transaction(trans
);
1725 } else if (new_size
< old_size
) {
1726 ret
= btrfs_shrink_device(device
, new_size
);
1727 } /* equal, nothing need to do */
1732 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
1733 mnt_drop_write_file(file
);
1737 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1738 const char *name
, unsigned long fd
, int subvol
,
1739 u64
*transid
, bool readonly
,
1740 struct btrfs_qgroup_inherit
*inherit
)
1745 if (!S_ISDIR(file_inode(file
)->i_mode
))
1748 ret
= mnt_want_write_file(file
);
1752 namelen
= strlen(name
);
1753 if (strchr(name
, '/')) {
1755 goto out_drop_write
;
1758 if (name
[0] == '.' &&
1759 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1761 goto out_drop_write
;
1765 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1766 NULL
, transid
, readonly
, inherit
);
1768 struct fd src
= fdget(fd
);
1769 struct inode
*src_inode
;
1772 goto out_drop_write
;
1775 src_inode
= file_inode(src
.file
);
1776 if (src_inode
->i_sb
!= file_inode(file
)->i_sb
) {
1777 btrfs_info(BTRFS_I(file_inode(file
))->root
->fs_info
,
1778 "Snapshot src from another FS");
1780 } else if (!inode_owner_or_capable(src_inode
)) {
1782 * Subvolume creation is not restricted, but snapshots
1783 * are limited to own subvolumes only
1787 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1788 BTRFS_I(src_inode
)->root
,
1789 transid
, readonly
, inherit
);
1794 mnt_drop_write_file(file
);
1799 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1800 void __user
*arg
, int subvol
)
1802 struct btrfs_ioctl_vol_args
*vol_args
;
1805 if (!S_ISDIR(file_inode(file
)->i_mode
))
1808 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1809 if (IS_ERR(vol_args
))
1810 return PTR_ERR(vol_args
);
1811 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1813 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1814 vol_args
->fd
, subvol
,
1821 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1822 void __user
*arg
, int subvol
)
1824 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1828 bool readonly
= false;
1829 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1831 if (!S_ISDIR(file_inode(file
)->i_mode
))
1834 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1835 if (IS_ERR(vol_args
))
1836 return PTR_ERR(vol_args
);
1837 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1839 if (vol_args
->flags
&
1840 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1841 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1846 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1847 struct inode
*inode
= file_inode(file
);
1848 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1851 "SNAP_CREATE_V2 ioctl with CREATE_ASYNC is deprecated and will be removed in kernel 5.7");
1855 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1857 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1858 if (vol_args
->size
> PAGE_SIZE
) {
1862 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1863 if (IS_ERR(inherit
)) {
1864 ret
= PTR_ERR(inherit
);
1869 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1870 vol_args
->fd
, subvol
, ptr
,
1875 if (ptr
&& copy_to_user(arg
+
1876 offsetof(struct btrfs_ioctl_vol_args_v2
,
1888 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1891 struct inode
*inode
= file_inode(file
);
1892 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1893 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1897 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
)
1900 down_read(&fs_info
->subvol_sem
);
1901 if (btrfs_root_readonly(root
))
1902 flags
|= BTRFS_SUBVOL_RDONLY
;
1903 up_read(&fs_info
->subvol_sem
);
1905 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1911 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1914 struct inode
*inode
= file_inode(file
);
1915 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1916 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1917 struct btrfs_trans_handle
*trans
;
1922 if (!inode_owner_or_capable(inode
))
1925 ret
= mnt_want_write_file(file
);
1929 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
1931 goto out_drop_write
;
1934 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1936 goto out_drop_write
;
1939 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1941 goto out_drop_write
;
1944 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1946 goto out_drop_write
;
1949 down_write(&fs_info
->subvol_sem
);
1952 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1955 root_flags
= btrfs_root_flags(&root
->root_item
);
1956 if (flags
& BTRFS_SUBVOL_RDONLY
) {
1957 btrfs_set_root_flags(&root
->root_item
,
1958 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1961 * Block RO -> RW transition if this subvolume is involved in
1964 spin_lock(&root
->root_item_lock
);
1965 if (root
->send_in_progress
== 0) {
1966 btrfs_set_root_flags(&root
->root_item
,
1967 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1968 spin_unlock(&root
->root_item_lock
);
1970 spin_unlock(&root
->root_item_lock
);
1972 "Attempt to set subvolume %llu read-write during send",
1973 root
->root_key
.objectid
);
1979 trans
= btrfs_start_transaction(root
, 1);
1980 if (IS_ERR(trans
)) {
1981 ret
= PTR_ERR(trans
);
1985 ret
= btrfs_update_root(trans
, fs_info
->tree_root
,
1986 &root
->root_key
, &root
->root_item
);
1988 btrfs_end_transaction(trans
);
1992 ret
= btrfs_commit_transaction(trans
);
1996 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1998 up_write(&fs_info
->subvol_sem
);
2000 mnt_drop_write_file(file
);
2005 static noinline
int key_in_sk(struct btrfs_key
*key
,
2006 struct btrfs_ioctl_search_key
*sk
)
2008 struct btrfs_key test
;
2011 test
.objectid
= sk
->min_objectid
;
2012 test
.type
= sk
->min_type
;
2013 test
.offset
= sk
->min_offset
;
2015 ret
= btrfs_comp_cpu_keys(key
, &test
);
2019 test
.objectid
= sk
->max_objectid
;
2020 test
.type
= sk
->max_type
;
2021 test
.offset
= sk
->max_offset
;
2023 ret
= btrfs_comp_cpu_keys(key
, &test
);
2029 static noinline
int copy_to_sk(struct btrfs_path
*path
,
2030 struct btrfs_key
*key
,
2031 struct btrfs_ioctl_search_key
*sk
,
2034 unsigned long *sk_offset
,
2038 struct extent_buffer
*leaf
;
2039 struct btrfs_ioctl_search_header sh
;
2040 struct btrfs_key test
;
2041 unsigned long item_off
;
2042 unsigned long item_len
;
2048 leaf
= path
->nodes
[0];
2049 slot
= path
->slots
[0];
2050 nritems
= btrfs_header_nritems(leaf
);
2052 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
2056 found_transid
= btrfs_header_generation(leaf
);
2058 for (i
= slot
; i
< nritems
; i
++) {
2059 item_off
= btrfs_item_ptr_offset(leaf
, i
);
2060 item_len
= btrfs_item_size_nr(leaf
, i
);
2062 btrfs_item_key_to_cpu(leaf
, key
, i
);
2063 if (!key_in_sk(key
, sk
))
2066 if (sizeof(sh
) + item_len
> *buf_size
) {
2073 * return one empty item back for v1, which does not
2077 *buf_size
= sizeof(sh
) + item_len
;
2082 if (sizeof(sh
) + item_len
+ *sk_offset
> *buf_size
) {
2087 sh
.objectid
= key
->objectid
;
2088 sh
.offset
= key
->offset
;
2089 sh
.type
= key
->type
;
2091 sh
.transid
= found_transid
;
2093 /* copy search result header */
2094 if (copy_to_user(ubuf
+ *sk_offset
, &sh
, sizeof(sh
))) {
2099 *sk_offset
+= sizeof(sh
);
2102 char __user
*up
= ubuf
+ *sk_offset
;
2104 if (read_extent_buffer_to_user(leaf
, up
,
2105 item_off
, item_len
)) {
2110 *sk_offset
+= item_len
;
2114 if (ret
) /* -EOVERFLOW from above */
2117 if (*num_found
>= sk
->nr_items
) {
2124 test
.objectid
= sk
->max_objectid
;
2125 test
.type
= sk
->max_type
;
2126 test
.offset
= sk
->max_offset
;
2127 if (btrfs_comp_cpu_keys(key
, &test
) >= 0)
2129 else if (key
->offset
< (u64
)-1)
2131 else if (key
->type
< (u8
)-1) {
2134 } else if (key
->objectid
< (u64
)-1) {
2142 * 0: all items from this leaf copied, continue with next
2143 * 1: * more items can be copied, but unused buffer is too small
2144 * * all items were found
2145 * Either way, it will stops the loop which iterates to the next
2147 * -EOVERFLOW: item was to large for buffer
2148 * -EFAULT: could not copy extent buffer back to userspace
2153 static noinline
int search_ioctl(struct inode
*inode
,
2154 struct btrfs_ioctl_search_key
*sk
,
2158 struct btrfs_fs_info
*info
= btrfs_sb(inode
->i_sb
);
2159 struct btrfs_root
*root
;
2160 struct btrfs_key key
;
2161 struct btrfs_path
*path
;
2164 unsigned long sk_offset
= 0;
2166 if (*buf_size
< sizeof(struct btrfs_ioctl_search_header
)) {
2167 *buf_size
= sizeof(struct btrfs_ioctl_search_header
);
2171 path
= btrfs_alloc_path();
2175 if (sk
->tree_id
== 0) {
2176 /* search the root of the inode that was passed */
2177 root
= BTRFS_I(inode
)->root
;
2179 key
.objectid
= sk
->tree_id
;
2180 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2181 key
.offset
= (u64
)-1;
2182 root
= btrfs_read_fs_root_no_name(info
, &key
);
2184 btrfs_free_path(path
);
2185 return PTR_ERR(root
);
2189 key
.objectid
= sk
->min_objectid
;
2190 key
.type
= sk
->min_type
;
2191 key
.offset
= sk
->min_offset
;
2194 ret
= btrfs_search_forward(root
, &key
, path
, sk
->min_transid
);
2200 ret
= copy_to_sk(path
, &key
, sk
, buf_size
, ubuf
,
2201 &sk_offset
, &num_found
);
2202 btrfs_release_path(path
);
2210 sk
->nr_items
= num_found
;
2211 btrfs_free_path(path
);
2215 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
2218 struct btrfs_ioctl_search_args __user
*uargs
;
2219 struct btrfs_ioctl_search_key sk
;
2220 struct inode
*inode
;
2224 if (!capable(CAP_SYS_ADMIN
))
2227 uargs
= (struct btrfs_ioctl_search_args __user
*)argp
;
2229 if (copy_from_user(&sk
, &uargs
->key
, sizeof(sk
)))
2232 buf_size
= sizeof(uargs
->buf
);
2234 inode
= file_inode(file
);
2235 ret
= search_ioctl(inode
, &sk
, &buf_size
, uargs
->buf
);
2238 * In the origin implementation an overflow is handled by returning a
2239 * search header with a len of zero, so reset ret.
2241 if (ret
== -EOVERFLOW
)
2244 if (ret
== 0 && copy_to_user(&uargs
->key
, &sk
, sizeof(sk
)))
2249 static noinline
int btrfs_ioctl_tree_search_v2(struct file
*file
,
2252 struct btrfs_ioctl_search_args_v2 __user
*uarg
;
2253 struct btrfs_ioctl_search_args_v2 args
;
2254 struct inode
*inode
;
2257 const size_t buf_limit
= SZ_16M
;
2259 if (!capable(CAP_SYS_ADMIN
))
2262 /* copy search header and buffer size */
2263 uarg
= (struct btrfs_ioctl_search_args_v2 __user
*)argp
;
2264 if (copy_from_user(&args
, uarg
, sizeof(args
)))
2267 buf_size
= args
.buf_size
;
2269 /* limit result size to 16MB */
2270 if (buf_size
> buf_limit
)
2271 buf_size
= buf_limit
;
2273 inode
= file_inode(file
);
2274 ret
= search_ioctl(inode
, &args
.key
, &buf_size
,
2275 (char __user
*)(&uarg
->buf
[0]));
2276 if (ret
== 0 && copy_to_user(&uarg
->key
, &args
.key
, sizeof(args
.key
)))
2278 else if (ret
== -EOVERFLOW
&&
2279 copy_to_user(&uarg
->buf_size
, &buf_size
, sizeof(buf_size
)))
2286 * Search INODE_REFs to identify path name of 'dirid' directory
2287 * in a 'tree_id' tree. and sets path name to 'name'.
2289 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
2290 u64 tree_id
, u64 dirid
, char *name
)
2292 struct btrfs_root
*root
;
2293 struct btrfs_key key
;
2299 struct btrfs_inode_ref
*iref
;
2300 struct extent_buffer
*l
;
2301 struct btrfs_path
*path
;
2303 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
2308 path
= btrfs_alloc_path();
2312 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
- 1];
2314 key
.objectid
= tree_id
;
2315 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2316 key
.offset
= (u64
)-1;
2317 root
= btrfs_read_fs_root_no_name(info
, &key
);
2319 ret
= PTR_ERR(root
);
2323 key
.objectid
= dirid
;
2324 key
.type
= BTRFS_INODE_REF_KEY
;
2325 key
.offset
= (u64
)-1;
2328 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2332 ret
= btrfs_previous_item(root
, path
, dirid
,
2333 BTRFS_INODE_REF_KEY
);
2343 slot
= path
->slots
[0];
2344 btrfs_item_key_to_cpu(l
, &key
, slot
);
2346 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
2347 len
= btrfs_inode_ref_name_len(l
, iref
);
2349 total_len
+= len
+ 1;
2351 ret
= -ENAMETOOLONG
;
2356 read_extent_buffer(l
, ptr
, (unsigned long)(iref
+ 1), len
);
2358 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
2361 btrfs_release_path(path
);
2362 key
.objectid
= key
.offset
;
2363 key
.offset
= (u64
)-1;
2364 dirid
= key
.objectid
;
2366 memmove(name
, ptr
, total_len
);
2367 name
[total_len
] = '\0';
2370 btrfs_free_path(path
);
2374 static int btrfs_search_path_in_tree_user(struct inode
*inode
,
2375 struct btrfs_ioctl_ino_lookup_user_args
*args
)
2377 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2378 struct super_block
*sb
= inode
->i_sb
;
2379 struct btrfs_key upper_limit
= BTRFS_I(inode
)->location
;
2380 u64 treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2381 u64 dirid
= args
->dirid
;
2382 unsigned long item_off
;
2383 unsigned long item_len
;
2384 struct btrfs_inode_ref
*iref
;
2385 struct btrfs_root_ref
*rref
;
2386 struct btrfs_root
*root
;
2387 struct btrfs_path
*path
;
2388 struct btrfs_key key
, key2
;
2389 struct extent_buffer
*leaf
;
2390 struct inode
*temp_inode
;
2397 path
= btrfs_alloc_path();
2402 * If the bottom subvolume does not exist directly under upper_limit,
2403 * construct the path in from the bottom up.
2405 if (dirid
!= upper_limit
.objectid
) {
2406 ptr
= &args
->path
[BTRFS_INO_LOOKUP_USER_PATH_MAX
- 1];
2408 key
.objectid
= treeid
;
2409 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2410 key
.offset
= (u64
)-1;
2411 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2413 ret
= PTR_ERR(root
);
2417 key
.objectid
= dirid
;
2418 key
.type
= BTRFS_INODE_REF_KEY
;
2419 key
.offset
= (u64
)-1;
2421 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2424 } else if (ret
> 0) {
2425 ret
= btrfs_previous_item(root
, path
, dirid
,
2426 BTRFS_INODE_REF_KEY
);
2429 } else if (ret
> 0) {
2435 leaf
= path
->nodes
[0];
2436 slot
= path
->slots
[0];
2437 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2439 iref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_inode_ref
);
2440 len
= btrfs_inode_ref_name_len(leaf
, iref
);
2442 total_len
+= len
+ 1;
2443 if (ptr
< args
->path
) {
2444 ret
= -ENAMETOOLONG
;
2449 read_extent_buffer(leaf
, ptr
,
2450 (unsigned long)(iref
+ 1), len
);
2452 /* Check the read+exec permission of this directory */
2453 ret
= btrfs_previous_item(root
, path
, dirid
,
2454 BTRFS_INODE_ITEM_KEY
);
2457 } else if (ret
> 0) {
2462 leaf
= path
->nodes
[0];
2463 slot
= path
->slots
[0];
2464 btrfs_item_key_to_cpu(leaf
, &key2
, slot
);
2465 if (key2
.objectid
!= dirid
) {
2470 temp_inode
= btrfs_iget(sb
, &key2
, root
);
2471 if (IS_ERR(temp_inode
)) {
2472 ret
= PTR_ERR(temp_inode
);
2475 ret
= inode_permission(temp_inode
, MAY_READ
| MAY_EXEC
);
2482 if (key
.offset
== upper_limit
.objectid
)
2484 if (key
.objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
2489 btrfs_release_path(path
);
2490 key
.objectid
= key
.offset
;
2491 key
.offset
= (u64
)-1;
2492 dirid
= key
.objectid
;
2495 memmove(args
->path
, ptr
, total_len
);
2496 args
->path
[total_len
] = '\0';
2497 btrfs_release_path(path
);
2500 /* Get the bottom subvolume's name from ROOT_REF */
2501 root
= fs_info
->tree_root
;
2502 key
.objectid
= treeid
;
2503 key
.type
= BTRFS_ROOT_REF_KEY
;
2504 key
.offset
= args
->treeid
;
2505 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2508 } else if (ret
> 0) {
2513 leaf
= path
->nodes
[0];
2514 slot
= path
->slots
[0];
2515 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2517 item_off
= btrfs_item_ptr_offset(leaf
, slot
);
2518 item_len
= btrfs_item_size_nr(leaf
, slot
);
2519 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2520 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2521 if (args
->dirid
!= btrfs_root_ref_dirid(leaf
, rref
)) {
2526 /* Copy subvolume's name */
2527 item_off
+= sizeof(struct btrfs_root_ref
);
2528 item_len
-= sizeof(struct btrfs_root_ref
);
2529 read_extent_buffer(leaf
, args
->name
, item_off
, item_len
);
2530 args
->name
[item_len
] = 0;
2533 btrfs_free_path(path
);
2537 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
2540 struct btrfs_ioctl_ino_lookup_args
*args
;
2541 struct inode
*inode
;
2544 args
= memdup_user(argp
, sizeof(*args
));
2546 return PTR_ERR(args
);
2548 inode
= file_inode(file
);
2551 * Unprivileged query to obtain the containing subvolume root id. The
2552 * path is reset so it's consistent with btrfs_search_path_in_tree.
2554 if (args
->treeid
== 0)
2555 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2557 if (args
->objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
2562 if (!capable(CAP_SYS_ADMIN
)) {
2567 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
2568 args
->treeid
, args
->objectid
,
2572 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2580 * Version of ino_lookup ioctl (unprivileged)
2582 * The main differences from ino_lookup ioctl are:
2584 * 1. Read + Exec permission will be checked using inode_permission() during
2585 * path construction. -EACCES will be returned in case of failure.
2586 * 2. Path construction will be stopped at the inode number which corresponds
2587 * to the fd with which this ioctl is called. If constructed path does not
2588 * exist under fd's inode, -EACCES will be returned.
2589 * 3. The name of bottom subvolume is also searched and filled.
2591 static int btrfs_ioctl_ino_lookup_user(struct file
*file
, void __user
*argp
)
2593 struct btrfs_ioctl_ino_lookup_user_args
*args
;
2594 struct inode
*inode
;
2597 args
= memdup_user(argp
, sizeof(*args
));
2599 return PTR_ERR(args
);
2601 inode
= file_inode(file
);
2603 if (args
->dirid
== BTRFS_FIRST_FREE_OBJECTID
&&
2604 BTRFS_I(inode
)->location
.objectid
!= BTRFS_FIRST_FREE_OBJECTID
) {
2606 * The subvolume does not exist under fd with which this is
2613 ret
= btrfs_search_path_in_tree_user(inode
, args
);
2615 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2622 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2623 static int btrfs_ioctl_get_subvol_info(struct file
*file
, void __user
*argp
)
2625 struct btrfs_ioctl_get_subvol_info_args
*subvol_info
;
2626 struct btrfs_fs_info
*fs_info
;
2627 struct btrfs_root
*root
;
2628 struct btrfs_path
*path
;
2629 struct btrfs_key key
;
2630 struct btrfs_root_item
*root_item
;
2631 struct btrfs_root_ref
*rref
;
2632 struct extent_buffer
*leaf
;
2633 unsigned long item_off
;
2634 unsigned long item_len
;
2635 struct inode
*inode
;
2639 path
= btrfs_alloc_path();
2643 subvol_info
= kzalloc(sizeof(*subvol_info
), GFP_KERNEL
);
2645 btrfs_free_path(path
);
2649 inode
= file_inode(file
);
2650 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2652 /* Get root_item of inode's subvolume */
2653 key
.objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2654 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2655 key
.offset
= (u64
)-1;
2656 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2658 ret
= PTR_ERR(root
);
2661 root_item
= &root
->root_item
;
2663 subvol_info
->treeid
= key
.objectid
;
2665 subvol_info
->generation
= btrfs_root_generation(root_item
);
2666 subvol_info
->flags
= btrfs_root_flags(root_item
);
2668 memcpy(subvol_info
->uuid
, root_item
->uuid
, BTRFS_UUID_SIZE
);
2669 memcpy(subvol_info
->parent_uuid
, root_item
->parent_uuid
,
2671 memcpy(subvol_info
->received_uuid
, root_item
->received_uuid
,
2674 subvol_info
->ctransid
= btrfs_root_ctransid(root_item
);
2675 subvol_info
->ctime
.sec
= btrfs_stack_timespec_sec(&root_item
->ctime
);
2676 subvol_info
->ctime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->ctime
);
2678 subvol_info
->otransid
= btrfs_root_otransid(root_item
);
2679 subvol_info
->otime
.sec
= btrfs_stack_timespec_sec(&root_item
->otime
);
2680 subvol_info
->otime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->otime
);
2682 subvol_info
->stransid
= btrfs_root_stransid(root_item
);
2683 subvol_info
->stime
.sec
= btrfs_stack_timespec_sec(&root_item
->stime
);
2684 subvol_info
->stime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->stime
);
2686 subvol_info
->rtransid
= btrfs_root_rtransid(root_item
);
2687 subvol_info
->rtime
.sec
= btrfs_stack_timespec_sec(&root_item
->rtime
);
2688 subvol_info
->rtime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->rtime
);
2690 if (key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) {
2691 /* Search root tree for ROOT_BACKREF of this subvolume */
2692 root
= fs_info
->tree_root
;
2694 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2696 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2699 } else if (path
->slots
[0] >=
2700 btrfs_header_nritems(path
->nodes
[0])) {
2701 ret
= btrfs_next_leaf(root
, path
);
2704 } else if (ret
> 0) {
2710 leaf
= path
->nodes
[0];
2711 slot
= path
->slots
[0];
2712 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2713 if (key
.objectid
== subvol_info
->treeid
&&
2714 key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
2715 subvol_info
->parent_id
= key
.offset
;
2717 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2718 subvol_info
->dirid
= btrfs_root_ref_dirid(leaf
, rref
);
2720 item_off
= btrfs_item_ptr_offset(leaf
, slot
)
2721 + sizeof(struct btrfs_root_ref
);
2722 item_len
= btrfs_item_size_nr(leaf
, slot
)
2723 - sizeof(struct btrfs_root_ref
);
2724 read_extent_buffer(leaf
, subvol_info
->name
,
2725 item_off
, item_len
);
2732 if (copy_to_user(argp
, subvol_info
, sizeof(*subvol_info
)))
2736 btrfs_free_path(path
);
2737 kzfree(subvol_info
);
2742 * Return ROOT_REF information of the subvolume containing this inode
2743 * except the subvolume name.
2745 static int btrfs_ioctl_get_subvol_rootref(struct file
*file
, void __user
*argp
)
2747 struct btrfs_ioctl_get_subvol_rootref_args
*rootrefs
;
2748 struct btrfs_root_ref
*rref
;
2749 struct btrfs_root
*root
;
2750 struct btrfs_path
*path
;
2751 struct btrfs_key key
;
2752 struct extent_buffer
*leaf
;
2753 struct inode
*inode
;
2759 path
= btrfs_alloc_path();
2763 rootrefs
= memdup_user(argp
, sizeof(*rootrefs
));
2764 if (IS_ERR(rootrefs
)) {
2765 btrfs_free_path(path
);
2766 return PTR_ERR(rootrefs
);
2769 inode
= file_inode(file
);
2770 root
= BTRFS_I(inode
)->root
->fs_info
->tree_root
;
2771 objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2773 key
.objectid
= objectid
;
2774 key
.type
= BTRFS_ROOT_REF_KEY
;
2775 key
.offset
= rootrefs
->min_treeid
;
2778 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2781 } else if (path
->slots
[0] >=
2782 btrfs_header_nritems(path
->nodes
[0])) {
2783 ret
= btrfs_next_leaf(root
, path
);
2786 } else if (ret
> 0) {
2792 leaf
= path
->nodes
[0];
2793 slot
= path
->slots
[0];
2795 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2796 if (key
.objectid
!= objectid
|| key
.type
!= BTRFS_ROOT_REF_KEY
) {
2801 if (found
== BTRFS_MAX_ROOTREF_BUFFER_NUM
) {
2806 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2807 rootrefs
->rootref
[found
].treeid
= key
.offset
;
2808 rootrefs
->rootref
[found
].dirid
=
2809 btrfs_root_ref_dirid(leaf
, rref
);
2812 ret
= btrfs_next_item(root
, path
);
2815 } else if (ret
> 0) {
2822 if (!ret
|| ret
== -EOVERFLOW
) {
2823 rootrefs
->num_items
= found
;
2824 /* update min_treeid for next search */
2826 rootrefs
->min_treeid
=
2827 rootrefs
->rootref
[found
- 1].treeid
+ 1;
2828 if (copy_to_user(argp
, rootrefs
, sizeof(*rootrefs
)))
2833 btrfs_free_path(path
);
2838 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
2841 struct dentry
*parent
= file
->f_path
.dentry
;
2842 struct btrfs_fs_info
*fs_info
= btrfs_sb(parent
->d_sb
);
2843 struct dentry
*dentry
;
2844 struct inode
*dir
= d_inode(parent
);
2845 struct inode
*inode
;
2846 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2847 struct btrfs_root
*dest
= NULL
;
2848 struct btrfs_ioctl_vol_args
*vol_args
;
2852 if (!S_ISDIR(dir
->i_mode
))
2855 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2856 if (IS_ERR(vol_args
))
2857 return PTR_ERR(vol_args
);
2859 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2860 namelen
= strlen(vol_args
->name
);
2861 if (strchr(vol_args
->name
, '/') ||
2862 strncmp(vol_args
->name
, "..", namelen
) == 0) {
2867 err
= mnt_want_write_file(file
);
2872 err
= down_write_killable_nested(&dir
->i_rwsem
, I_MUTEX_PARENT
);
2874 goto out_drop_write
;
2875 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
2876 if (IS_ERR(dentry
)) {
2877 err
= PTR_ERR(dentry
);
2878 goto out_unlock_dir
;
2881 if (d_really_is_negative(dentry
)) {
2886 inode
= d_inode(dentry
);
2887 dest
= BTRFS_I(inode
)->root
;
2888 if (!capable(CAP_SYS_ADMIN
)) {
2890 * Regular user. Only allow this with a special mount
2891 * option, when the user has write+exec access to the
2892 * subvol root, and when rmdir(2) would have been
2895 * Note that this is _not_ check that the subvol is
2896 * empty or doesn't contain data that we wouldn't
2897 * otherwise be able to delete.
2899 * Users who want to delete empty subvols should try
2903 if (!btrfs_test_opt(fs_info
, USER_SUBVOL_RM_ALLOWED
))
2907 * Do not allow deletion if the parent dir is the same
2908 * as the dir to be deleted. That means the ioctl
2909 * must be called on the dentry referencing the root
2910 * of the subvol, not a random directory contained
2917 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
2922 /* check if subvolume may be deleted by a user */
2923 err
= btrfs_may_delete(dir
, dentry
, 1);
2927 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
2933 err
= btrfs_delete_subvolume(dir
, dentry
);
2934 inode_unlock(inode
);
2936 fsnotify_rmdir(dir
, dentry
);
2945 mnt_drop_write_file(file
);
2951 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2953 struct inode
*inode
= file_inode(file
);
2954 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2955 struct btrfs_ioctl_defrag_range_args
*range
;
2958 ret
= mnt_want_write_file(file
);
2962 if (btrfs_root_readonly(root
)) {
2967 switch (inode
->i_mode
& S_IFMT
) {
2969 if (!capable(CAP_SYS_ADMIN
)) {
2973 ret
= btrfs_defrag_root(root
);
2977 * Note that this does not check the file descriptor for write
2978 * access. This prevents defragmenting executables that are
2979 * running and allows defrag on files open in read-only mode.
2981 if (!capable(CAP_SYS_ADMIN
) &&
2982 inode_permission(inode
, MAY_WRITE
)) {
2987 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2994 if (copy_from_user(range
, argp
,
3000 /* compression requires us to start the IO */
3001 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
3002 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
3003 range
->extent_thresh
= (u32
)-1;
3006 /* the rest are all set to zero by kzalloc */
3007 range
->len
= (u64
)-1;
3009 ret
= btrfs_defrag_file(file_inode(file
), file
,
3010 range
, BTRFS_OLDEST_GENERATION
, 0);
3019 mnt_drop_write_file(file
);
3023 static long btrfs_ioctl_add_dev(struct btrfs_fs_info
*fs_info
, void __user
*arg
)
3025 struct btrfs_ioctl_vol_args
*vol_args
;
3028 if (!capable(CAP_SYS_ADMIN
))
3031 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
))
3032 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3034 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
3035 if (IS_ERR(vol_args
)) {
3036 ret
= PTR_ERR(vol_args
);
3040 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
3041 ret
= btrfs_init_new_device(fs_info
, vol_args
->name
);
3044 btrfs_info(fs_info
, "disk added %s", vol_args
->name
);
3048 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3052 static long btrfs_ioctl_rm_dev_v2(struct file
*file
, void __user
*arg
)
3054 struct inode
*inode
= file_inode(file
);
3055 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3056 struct btrfs_ioctl_vol_args_v2
*vol_args
;
3059 if (!capable(CAP_SYS_ADMIN
))
3062 ret
= mnt_want_write_file(file
);
3066 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
3067 if (IS_ERR(vol_args
)) {
3068 ret
= PTR_ERR(vol_args
);
3072 /* Check for compatibility reject unknown flags */
3073 if (vol_args
->flags
& ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED
) {
3078 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
3079 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3083 if (vol_args
->flags
& BTRFS_DEVICE_SPEC_BY_ID
) {
3084 ret
= btrfs_rm_device(fs_info
, NULL
, vol_args
->devid
);
3086 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
3087 ret
= btrfs_rm_device(fs_info
, vol_args
->name
, 0);
3089 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3092 if (vol_args
->flags
& BTRFS_DEVICE_SPEC_BY_ID
)
3093 btrfs_info(fs_info
, "device deleted: id %llu",
3096 btrfs_info(fs_info
, "device deleted: %s",
3102 mnt_drop_write_file(file
);
3106 static long btrfs_ioctl_rm_dev(struct file
*file
, void __user
*arg
)
3108 struct inode
*inode
= file_inode(file
);
3109 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3110 struct btrfs_ioctl_vol_args
*vol_args
;
3113 if (!capable(CAP_SYS_ADMIN
))
3116 ret
= mnt_want_write_file(file
);
3120 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
3121 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3122 goto out_drop_write
;
3125 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
3126 if (IS_ERR(vol_args
)) {
3127 ret
= PTR_ERR(vol_args
);
3131 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
3132 ret
= btrfs_rm_device(fs_info
, vol_args
->name
, 0);
3135 btrfs_info(fs_info
, "disk deleted %s", vol_args
->name
);
3138 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3140 mnt_drop_write_file(file
);
3145 static long btrfs_ioctl_fs_info(struct btrfs_fs_info
*fs_info
,
3148 struct btrfs_ioctl_fs_info_args
*fi_args
;
3149 struct btrfs_device
*device
;
3150 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
3153 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
3158 fi_args
->num_devices
= fs_devices
->num_devices
;
3160 list_for_each_entry_rcu(device
, &fs_devices
->devices
, dev_list
) {
3161 if (device
->devid
> fi_args
->max_id
)
3162 fi_args
->max_id
= device
->devid
;
3166 memcpy(&fi_args
->fsid
, fs_devices
->fsid
, sizeof(fi_args
->fsid
));
3167 fi_args
->nodesize
= fs_info
->nodesize
;
3168 fi_args
->sectorsize
= fs_info
->sectorsize
;
3169 fi_args
->clone_alignment
= fs_info
->sectorsize
;
3171 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
3178 static long btrfs_ioctl_dev_info(struct btrfs_fs_info
*fs_info
,
3181 struct btrfs_ioctl_dev_info_args
*di_args
;
3182 struct btrfs_device
*dev
;
3184 char *s_uuid
= NULL
;
3186 di_args
= memdup_user(arg
, sizeof(*di_args
));
3187 if (IS_ERR(di_args
))
3188 return PTR_ERR(di_args
);
3190 if (!btrfs_is_empty_uuid(di_args
->uuid
))
3191 s_uuid
= di_args
->uuid
;
3194 dev
= btrfs_find_device(fs_info
->fs_devices
, di_args
->devid
, s_uuid
,
3202 di_args
->devid
= dev
->devid
;
3203 di_args
->bytes_used
= btrfs_device_get_bytes_used(dev
);
3204 di_args
->total_bytes
= btrfs_device_get_total_bytes(dev
);
3205 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
3207 strncpy(di_args
->path
, rcu_str_deref(dev
->name
),
3208 sizeof(di_args
->path
) - 1);
3209 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
3211 di_args
->path
[0] = '\0';
3216 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
3223 static void btrfs_double_extent_unlock(struct inode
*inode1
, u64 loff1
,
3224 struct inode
*inode2
, u64 loff2
, u64 len
)
3226 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
3227 unlock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
3230 static void btrfs_double_extent_lock(struct inode
*inode1
, u64 loff1
,
3231 struct inode
*inode2
, u64 loff2
, u64 len
)
3233 if (inode1
< inode2
) {
3234 swap(inode1
, inode2
);
3236 } else if (inode1
== inode2
&& loff2
< loff1
) {
3239 lock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
3240 lock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
3243 static int btrfs_extent_same_range(struct inode
*src
, u64 loff
, u64 len
,
3244 struct inode
*dst
, u64 dst_loff
)
3246 const u64 bs
= BTRFS_I(src
)->root
->fs_info
->sb
->s_blocksize
;
3250 * Lock destination range to serialize with concurrent readpages() and
3251 * source range to serialize with relocation.
3253 btrfs_double_extent_lock(src
, loff
, dst
, dst_loff
, len
);
3254 ret
= btrfs_clone(src
, dst
, loff
, len
, ALIGN(len
, bs
), dst_loff
, 1);
3255 btrfs_double_extent_unlock(src
, loff
, dst
, dst_loff
, len
);
3260 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3262 static int btrfs_extent_same(struct inode
*src
, u64 loff
, u64 olen
,
3263 struct inode
*dst
, u64 dst_loff
)
3266 u64 i
, tail_len
, chunk_count
;
3267 struct btrfs_root
*root_dst
= BTRFS_I(dst
)->root
;
3269 spin_lock(&root_dst
->root_item_lock
);
3270 if (root_dst
->send_in_progress
) {
3271 btrfs_warn_rl(root_dst
->fs_info
,
3272 "cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3273 root_dst
->root_key
.objectid
,
3274 root_dst
->send_in_progress
);
3275 spin_unlock(&root_dst
->root_item_lock
);
3278 root_dst
->dedupe_in_progress
++;
3279 spin_unlock(&root_dst
->root_item_lock
);
3281 tail_len
= olen
% BTRFS_MAX_DEDUPE_LEN
;
3282 chunk_count
= div_u64(olen
, BTRFS_MAX_DEDUPE_LEN
);
3284 for (i
= 0; i
< chunk_count
; i
++) {
3285 ret
= btrfs_extent_same_range(src
, loff
, BTRFS_MAX_DEDUPE_LEN
,
3290 loff
+= BTRFS_MAX_DEDUPE_LEN
;
3291 dst_loff
+= BTRFS_MAX_DEDUPE_LEN
;
3295 ret
= btrfs_extent_same_range(src
, loff
, tail_len
, dst
,
3298 spin_lock(&root_dst
->root_item_lock
);
3299 root_dst
->dedupe_in_progress
--;
3300 spin_unlock(&root_dst
->root_item_lock
);
3305 static int clone_finish_inode_update(struct btrfs_trans_handle
*trans
,
3306 struct inode
*inode
,
3312 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3315 inode_inc_iversion(inode
);
3316 if (!no_time_update
)
3317 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
3319 * We round up to the block size at eof when determining which
3320 * extents to clone above, but shouldn't round up the file size.
3322 if (endoff
> destoff
+ olen
)
3323 endoff
= destoff
+ olen
;
3324 if (endoff
> inode
->i_size
)
3325 btrfs_i_size_write(BTRFS_I(inode
), endoff
);
3327 ret
= btrfs_update_inode(trans
, root
, inode
);
3329 btrfs_abort_transaction(trans
, ret
);
3330 btrfs_end_transaction(trans
);
3333 ret
= btrfs_end_transaction(trans
);
3339 * Make sure we do not end up inserting an inline extent into a file that has
3340 * already other (non-inline) extents. If a file has an inline extent it can
3341 * not have any other extents and the (single) inline extent must start at the
3342 * file offset 0. Failing to respect these rules will lead to file corruption,
3343 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3345 * We can have extents that have been already written to disk or we can have
3346 * dirty ranges still in delalloc, in which case the extent maps and items are
3347 * created only when we run delalloc, and the delalloc ranges might fall outside
3348 * the range we are currently locking in the inode's io tree. So we check the
3349 * inode's i_size because of that (i_size updates are done while holding the
3350 * i_mutex, which we are holding here).
3351 * We also check to see if the inode has a size not greater than "datal" but has
3352 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3353 * protected against such concurrent fallocate calls by the i_mutex).
3355 * If the file has no extents but a size greater than datal, do not allow the
3356 * copy because we would need turn the inline extent into a non-inline one (even
3357 * with NO_HOLES enabled). If we find our destination inode only has one inline
3358 * extent, just overwrite it with the source inline extent if its size is less
3359 * than the source extent's size, or we could copy the source inline extent's
3360 * data into the destination inode's inline extent if the later is greater then
3363 static int clone_copy_inline_extent(struct inode
*dst
,
3364 struct btrfs_trans_handle
*trans
,
3365 struct btrfs_path
*path
,
3366 struct btrfs_key
*new_key
,
3367 const u64 drop_start
,
3373 struct btrfs_fs_info
*fs_info
= btrfs_sb(dst
->i_sb
);
3374 struct btrfs_root
*root
= BTRFS_I(dst
)->root
;
3375 const u64 aligned_end
= ALIGN(new_key
->offset
+ datal
,
3376 fs_info
->sectorsize
);
3378 struct btrfs_key key
;
3380 if (new_key
->offset
> 0)
3383 key
.objectid
= btrfs_ino(BTRFS_I(dst
));
3384 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3386 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3389 } else if (ret
> 0) {
3390 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
3391 ret
= btrfs_next_leaf(root
, path
);
3395 goto copy_inline_extent
;
3397 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3398 if (key
.objectid
== btrfs_ino(BTRFS_I(dst
)) &&
3399 key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3400 ASSERT(key
.offset
> 0);
3403 } else if (i_size_read(dst
) <= datal
) {
3404 struct btrfs_file_extent_item
*ei
;
3408 * If the file size is <= datal, make sure there are no other
3409 * extents following (can happen do to an fallocate call with
3410 * the flag FALLOC_FL_KEEP_SIZE).
3412 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3413 struct btrfs_file_extent_item
);
3415 * If it's an inline extent, it can not have other extents
3418 if (btrfs_file_extent_type(path
->nodes
[0], ei
) ==
3419 BTRFS_FILE_EXTENT_INLINE
)
3420 goto copy_inline_extent
;
3422 ext_len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
3423 if (ext_len
> aligned_end
)
3426 ret
= btrfs_next_item(root
, path
);
3429 } else if (ret
== 0) {
3430 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3432 if (key
.objectid
== btrfs_ino(BTRFS_I(dst
)) &&
3433 key
.type
== BTRFS_EXTENT_DATA_KEY
)
3440 * We have no extent items, or we have an extent at offset 0 which may
3441 * or may not be inlined. All these cases are dealt the same way.
3443 if (i_size_read(dst
) > datal
) {
3445 * If the destination inode has an inline extent...
3446 * This would require copying the data from the source inline
3447 * extent into the beginning of the destination's inline extent.
3448 * But this is really complex, both extents can be compressed
3449 * or just one of them, which would require decompressing and
3450 * re-compressing data (which could increase the new compressed
3451 * size, not allowing the compressed data to fit anymore in an
3453 * So just don't support this case for now (it should be rare,
3454 * we are not really saving space when cloning inline extents).
3459 btrfs_release_path(path
);
3460 ret
= btrfs_drop_extents(trans
, root
, dst
, drop_start
, aligned_end
, 1);
3463 ret
= btrfs_insert_empty_item(trans
, root
, path
, new_key
, size
);
3468 const u32 start
= btrfs_file_extent_calc_inline_size(0);
3470 memmove(inline_data
+ start
, inline_data
+ start
+ skip
, datal
);
3473 write_extent_buffer(path
->nodes
[0], inline_data
,
3474 btrfs_item_ptr_offset(path
->nodes
[0],
3477 inode_add_bytes(dst
, datal
);
3478 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(dst
)->runtime_flags
);
3484 * btrfs_clone() - clone a range from inode file to another
3486 * @src: Inode to clone from
3487 * @inode: Inode to clone to
3488 * @off: Offset within source to start clone from
3489 * @olen: Original length, passed by user, of range to clone
3490 * @olen_aligned: Block-aligned value of olen
3491 * @destoff: Offset within @inode to start clone
3492 * @no_time_update: Whether to update mtime/ctime on the target inode
3494 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
3495 const u64 off
, const u64 olen
, const u64 olen_aligned
,
3496 const u64 destoff
, int no_time_update
)
3498 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3499 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3500 struct btrfs_path
*path
= NULL
;
3501 struct extent_buffer
*leaf
;
3502 struct btrfs_trans_handle
*trans
;
3504 struct btrfs_key key
;
3508 const u64 len
= olen_aligned
;
3509 u64 last_dest_end
= destoff
;
3512 buf
= kvmalloc(fs_info
->nodesize
, GFP_KERNEL
);
3516 path
= btrfs_alloc_path();
3522 path
->reada
= READA_FORWARD
;
3524 key
.objectid
= btrfs_ino(BTRFS_I(src
));
3525 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3529 u64 next_key_min_offset
= key
.offset
+ 1;
3530 struct btrfs_file_extent_item
*extent
;
3533 struct btrfs_key new_key
;
3534 u64 disko
= 0, diskl
= 0;
3535 u64 datao
= 0, datal
= 0;
3540 * note the key will change type as we walk through the
3543 path
->leave_spinning
= 1;
3544 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
3549 * First search, if no extent item that starts at offset off was
3550 * found but the previous item is an extent item, it's possible
3551 * it might overlap our target range, therefore process it.
3553 if (key
.offset
== off
&& ret
> 0 && path
->slots
[0] > 0) {
3554 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3555 path
->slots
[0] - 1);
3556 if (key
.type
== BTRFS_EXTENT_DATA_KEY
)
3560 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3562 if (path
->slots
[0] >= nritems
) {
3563 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
3568 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3570 leaf
= path
->nodes
[0];
3571 slot
= path
->slots
[0];
3573 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3574 if (key
.type
> BTRFS_EXTENT_DATA_KEY
||
3575 key
.objectid
!= btrfs_ino(BTRFS_I(src
)))
3578 ASSERT(key
.type
== BTRFS_EXTENT_DATA_KEY
);
3580 extent
= btrfs_item_ptr(leaf
, slot
,
3581 struct btrfs_file_extent_item
);
3582 comp
= btrfs_file_extent_compression(leaf
, extent
);
3583 type
= btrfs_file_extent_type(leaf
, extent
);
3584 if (type
== BTRFS_FILE_EXTENT_REG
||
3585 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3586 disko
= btrfs_file_extent_disk_bytenr(leaf
, extent
);
3587 diskl
= btrfs_file_extent_disk_num_bytes(leaf
, extent
);
3588 datao
= btrfs_file_extent_offset(leaf
, extent
);
3589 datal
= btrfs_file_extent_num_bytes(leaf
, extent
);
3590 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3591 /* Take upper bound, may be compressed */
3592 datal
= btrfs_file_extent_ram_bytes(leaf
, extent
);
3596 * The first search might have left us at an extent item that
3597 * ends before our target range's start, can happen if we have
3598 * holes and NO_HOLES feature enabled.
3600 if (key
.offset
+ datal
<= off
) {
3603 } else if (key
.offset
>= off
+ len
) {
3606 next_key_min_offset
= key
.offset
+ datal
;
3607 size
= btrfs_item_size_nr(leaf
, slot
);
3608 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
, slot
),
3611 btrfs_release_path(path
);
3612 path
->leave_spinning
= 0;
3614 memcpy(&new_key
, &key
, sizeof(new_key
));
3615 new_key
.objectid
= btrfs_ino(BTRFS_I(inode
));
3616 if (off
<= key
.offset
)
3617 new_key
.offset
= key
.offset
+ destoff
- off
;
3619 new_key
.offset
= destoff
;
3622 * Deal with a hole that doesn't have an extent item that
3623 * represents it (NO_HOLES feature enabled).
3624 * This hole is either in the middle of the cloning range or at
3625 * the beginning (fully overlaps it or partially overlaps it).
3627 if (new_key
.offset
!= last_dest_end
)
3628 drop_start
= last_dest_end
;
3630 drop_start
= new_key
.offset
;
3632 if (type
== BTRFS_FILE_EXTENT_REG
||
3633 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3634 struct btrfs_clone_extent_info clone_info
;
3637 * a | --- range to clone ---| b
3638 * | ------------- extent ------------- |
3641 /* Subtract range b */
3642 if (key
.offset
+ datal
> off
+ len
)
3643 datal
= off
+ len
- key
.offset
;
3645 /* Subtract range a */
3646 if (off
> key
.offset
) {
3647 datao
+= off
- key
.offset
;
3648 datal
-= off
- key
.offset
;
3651 clone_info
.disk_offset
= disko
;
3652 clone_info
.disk_len
= diskl
;
3653 clone_info
.data_offset
= datao
;
3654 clone_info
.data_len
= datal
;
3655 clone_info
.file_offset
= new_key
.offset
;
3656 clone_info
.extent_buf
= buf
;
3657 clone_info
.item_size
= size
;
3658 ret
= btrfs_punch_hole_range(inode
, path
,
3660 new_key
.offset
+ datal
- 1,
3661 &clone_info
, &trans
);
3664 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3668 if (off
> key
.offset
) {
3669 skip
= off
- key
.offset
;
3670 new_key
.offset
+= skip
;
3673 if (key
.offset
+ datal
> off
+ len
)
3674 trim
= key
.offset
+ datal
- (off
+ len
);
3676 if (comp
&& (skip
|| trim
)) {
3680 size
-= skip
+ trim
;
3681 datal
-= skip
+ trim
;
3684 * If our extent is inline, we know we will drop or
3685 * adjust at most 1 extent item in the destination root.
3687 * 1 - adjusting old extent (we may have to split it)
3688 * 1 - add new extent
3691 trans
= btrfs_start_transaction(root
, 3);
3692 if (IS_ERR(trans
)) {
3693 ret
= PTR_ERR(trans
);
3697 ret
= clone_copy_inline_extent(inode
, trans
, path
,
3698 &new_key
, drop_start
,
3699 datal
, skip
, size
, buf
);
3701 if (ret
!= -EOPNOTSUPP
)
3702 btrfs_abort_transaction(trans
, ret
);
3703 btrfs_end_transaction(trans
);
3708 btrfs_release_path(path
);
3710 last_dest_end
= ALIGN(new_key
.offset
+ datal
,
3711 fs_info
->sectorsize
);
3712 ret
= clone_finish_inode_update(trans
, inode
, last_dest_end
,
3713 destoff
, olen
, no_time_update
);
3716 if (new_key
.offset
+ datal
>= destoff
+ len
)
3719 btrfs_release_path(path
);
3720 key
.offset
= next_key_min_offset
;
3722 if (fatal_signal_pending(current
)) {
3729 if (last_dest_end
< destoff
+ len
) {
3731 * We have an implicit hole that fully or partially overlaps our
3732 * cloning range at its end. This means that we either have the
3733 * NO_HOLES feature enabled or the implicit hole happened due to
3734 * mixing buffered and direct IO writes against this file.
3736 btrfs_release_path(path
);
3737 path
->leave_spinning
= 0;
3739 ret
= btrfs_punch_hole_range(inode
, path
,
3740 last_dest_end
, destoff
+ len
- 1,
3745 ret
= clone_finish_inode_update(trans
, inode
, destoff
+ len
,
3746 destoff
, olen
, no_time_update
);
3750 btrfs_free_path(path
);
3755 static noinline
int btrfs_clone_files(struct file
*file
, struct file
*file_src
,
3756 u64 off
, u64 olen
, u64 destoff
)
3758 struct inode
*inode
= file_inode(file
);
3759 struct inode
*src
= file_inode(file_src
);
3760 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3763 u64 bs
= fs_info
->sb
->s_blocksize
;
3767 * - split compressed inline extents. annoying: we need to
3768 * decompress into destination's address_space (the file offset
3769 * may change, so source mapping won't do), then recompress (or
3770 * otherwise reinsert) a subrange.
3772 * - split destination inode's inline extents. The inline extents can
3773 * be either compressed or non-compressed.
3777 * VFS's generic_remap_file_range_prep() protects us from cloning the
3778 * eof block into the middle of a file, which would result in corruption
3779 * if the file size is not blocksize aligned. So we don't need to check
3780 * for that case here.
3782 if (off
+ len
== src
->i_size
)
3783 len
= ALIGN(src
->i_size
, bs
) - off
;
3785 if (destoff
> inode
->i_size
) {
3786 const u64 wb_start
= ALIGN_DOWN(inode
->i_size
, bs
);
3788 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
3792 * We may have truncated the last block if the inode's size is
3793 * not sector size aligned, so we need to wait for writeback to
3794 * complete before proceeding further, otherwise we can race
3795 * with cloning and attempt to increment a reference to an
3796 * extent that no longer exists (writeback completed right after
3797 * we found the previous extent covering eof and before we
3798 * attempted to increment its reference count).
3800 ret
= btrfs_wait_ordered_range(inode
, wb_start
,
3801 destoff
- wb_start
);
3807 * Lock destination range to serialize with concurrent readpages() and
3808 * source range to serialize with relocation.
3810 btrfs_double_extent_lock(src
, off
, inode
, destoff
, len
);
3811 ret
= btrfs_clone(src
, inode
, off
, olen
, len
, destoff
, 0);
3812 btrfs_double_extent_unlock(src
, off
, inode
, destoff
, len
);
3814 * Truncate page cache pages so that future reads will see the cloned
3815 * data immediately and not the previous data.
3817 truncate_inode_pages_range(&inode
->i_data
,
3818 round_down(destoff
, PAGE_SIZE
),
3819 round_up(destoff
+ len
, PAGE_SIZE
) - 1);
3824 static int btrfs_remap_file_range_prep(struct file
*file_in
, loff_t pos_in
,
3825 struct file
*file_out
, loff_t pos_out
,
3826 loff_t
*len
, unsigned int remap_flags
)
3828 struct inode
*inode_in
= file_inode(file_in
);
3829 struct inode
*inode_out
= file_inode(file_out
);
3830 u64 bs
= BTRFS_I(inode_out
)->root
->fs_info
->sb
->s_blocksize
;
3831 bool same_inode
= inode_out
== inode_in
;
3835 if (!(remap_flags
& REMAP_FILE_DEDUP
)) {
3836 struct btrfs_root
*root_out
= BTRFS_I(inode_out
)->root
;
3838 if (btrfs_root_readonly(root_out
))
3841 if (file_in
->f_path
.mnt
!= file_out
->f_path
.mnt
||
3842 inode_in
->i_sb
!= inode_out
->i_sb
)
3846 /* don't make the dst file partly checksummed */
3847 if ((BTRFS_I(inode_in
)->flags
& BTRFS_INODE_NODATASUM
) !=
3848 (BTRFS_I(inode_out
)->flags
& BTRFS_INODE_NODATASUM
)) {
3853 * Now that the inodes are locked, we need to start writeback ourselves
3854 * and can not rely on the writeback from the VFS's generic helper
3855 * generic_remap_file_range_prep() because:
3857 * 1) For compression we must call filemap_fdatawrite_range() range
3858 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3859 * helper only calls it once;
3861 * 2) filemap_fdatawrite_range(), called by the generic helper only
3862 * waits for the writeback to complete, i.e. for IO to be done, and
3863 * not for the ordered extents to complete. We need to wait for them
3864 * to complete so that new file extent items are in the fs tree.
3866 if (*len
== 0 && !(remap_flags
& REMAP_FILE_DEDUP
))
3867 wb_len
= ALIGN(inode_in
->i_size
, bs
) - ALIGN_DOWN(pos_in
, bs
);
3869 wb_len
= ALIGN(*len
, bs
);
3872 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3873 * any in progress could create its ordered extents after we wait for
3874 * existing ordered extents below).
3876 inode_dio_wait(inode_in
);
3878 inode_dio_wait(inode_out
);
3881 * Workaround to make sure NOCOW buffered write reach disk as NOCOW.
3883 * Btrfs' back references do not have a block level granularity, they
3884 * work at the whole extent level.
3885 * NOCOW buffered write without data space reserved may not be able
3886 * to fall back to CoW due to lack of data space, thus could cause
3889 * Here we take a shortcut by flushing the whole inode, so that all
3890 * nocow write should reach disk as nocow before we increase the
3891 * reference of the extent. We could do better by only flushing NOCOW
3892 * data, but that needs extra accounting.
3894 * Also we don't need to check ASYNC_EXTENT, as async extent will be
3895 * CoWed anyway, not affecting nocow part.
3897 ret
= filemap_flush(inode_in
->i_mapping
);
3901 ret
= btrfs_wait_ordered_range(inode_in
, ALIGN_DOWN(pos_in
, bs
),
3905 ret
= btrfs_wait_ordered_range(inode_out
, ALIGN_DOWN(pos_out
, bs
),
3910 return generic_remap_file_range_prep(file_in
, pos_in
, file_out
, pos_out
,
3914 loff_t
btrfs_remap_file_range(struct file
*src_file
, loff_t off
,
3915 struct file
*dst_file
, loff_t destoff
, loff_t len
,
3916 unsigned int remap_flags
)
3918 struct inode
*src_inode
= file_inode(src_file
);
3919 struct inode
*dst_inode
= file_inode(dst_file
);
3920 bool same_inode
= dst_inode
== src_inode
;
3923 if (remap_flags
& ~(REMAP_FILE_DEDUP
| REMAP_FILE_ADVISORY
))
3927 inode_lock(src_inode
);
3929 lock_two_nondirectories(src_inode
, dst_inode
);
3931 ret
= btrfs_remap_file_range_prep(src_file
, off
, dst_file
, destoff
,
3933 if (ret
< 0 || len
== 0)
3936 if (remap_flags
& REMAP_FILE_DEDUP
)
3937 ret
= btrfs_extent_same(src_inode
, off
, len
, dst_inode
, destoff
);
3939 ret
= btrfs_clone_files(dst_file
, src_file
, off
, len
, destoff
);
3943 inode_unlock(src_inode
);
3945 unlock_two_nondirectories(src_inode
, dst_inode
);
3947 return ret
< 0 ? ret
: len
;
3950 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
3952 struct inode
*inode
= file_inode(file
);
3953 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3954 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3955 struct btrfs_root
*new_root
;
3956 struct btrfs_dir_item
*di
;
3957 struct btrfs_trans_handle
*trans
;
3958 struct btrfs_path
*path
;
3959 struct btrfs_key location
;
3960 struct btrfs_disk_key disk_key
;
3965 if (!capable(CAP_SYS_ADMIN
))
3968 ret
= mnt_want_write_file(file
);
3972 if (copy_from_user(&objectid
, argp
, sizeof(objectid
))) {
3978 objectid
= BTRFS_FS_TREE_OBJECTID
;
3980 location
.objectid
= objectid
;
3981 location
.type
= BTRFS_ROOT_ITEM_KEY
;
3982 location
.offset
= (u64
)-1;
3984 new_root
= btrfs_read_fs_root_no_name(fs_info
, &location
);
3985 if (IS_ERR(new_root
)) {
3986 ret
= PTR_ERR(new_root
);
3989 if (!is_fstree(new_root
->root_key
.objectid
)) {
3994 path
= btrfs_alloc_path();
3999 path
->leave_spinning
= 1;
4001 trans
= btrfs_start_transaction(root
, 1);
4002 if (IS_ERR(trans
)) {
4003 btrfs_free_path(path
);
4004 ret
= PTR_ERR(trans
);
4008 dir_id
= btrfs_super_root_dir(fs_info
->super_copy
);
4009 di
= btrfs_lookup_dir_item(trans
, fs_info
->tree_root
, path
,
4010 dir_id
, "default", 7, 1);
4011 if (IS_ERR_OR_NULL(di
)) {
4012 btrfs_free_path(path
);
4013 btrfs_end_transaction(trans
);
4015 "Umm, you don't have the default diritem, this isn't going to work");
4020 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
4021 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
4022 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4023 btrfs_free_path(path
);
4025 btrfs_set_fs_incompat(fs_info
, DEFAULT_SUBVOL
);
4026 btrfs_end_transaction(trans
);
4028 mnt_drop_write_file(file
);
4032 static void get_block_group_info(struct list_head
*groups_list
,
4033 struct btrfs_ioctl_space_info
*space
)
4035 struct btrfs_block_group
*block_group
;
4037 space
->total_bytes
= 0;
4038 space
->used_bytes
= 0;
4040 list_for_each_entry(block_group
, groups_list
, list
) {
4041 space
->flags
= block_group
->flags
;
4042 space
->total_bytes
+= block_group
->length
;
4043 space
->used_bytes
+= block_group
->used
;
4047 static long btrfs_ioctl_space_info(struct btrfs_fs_info
*fs_info
,
4050 struct btrfs_ioctl_space_args space_args
;
4051 struct btrfs_ioctl_space_info space
;
4052 struct btrfs_ioctl_space_info
*dest
;
4053 struct btrfs_ioctl_space_info
*dest_orig
;
4054 struct btrfs_ioctl_space_info __user
*user_dest
;
4055 struct btrfs_space_info
*info
;
4056 static const u64 types
[] = {
4057 BTRFS_BLOCK_GROUP_DATA
,
4058 BTRFS_BLOCK_GROUP_SYSTEM
,
4059 BTRFS_BLOCK_GROUP_METADATA
,
4060 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
4068 if (copy_from_user(&space_args
,
4069 (struct btrfs_ioctl_space_args __user
*)arg
,
4070 sizeof(space_args
)))
4073 for (i
= 0; i
< num_types
; i
++) {
4074 struct btrfs_space_info
*tmp
;
4078 list_for_each_entry_rcu(tmp
, &fs_info
->space_info
,
4080 if (tmp
->flags
== types
[i
]) {
4090 down_read(&info
->groups_sem
);
4091 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4092 if (!list_empty(&info
->block_groups
[c
]))
4095 up_read(&info
->groups_sem
);
4099 * Global block reserve, exported as a space_info
4103 /* space_slots == 0 means they are asking for a count */
4104 if (space_args
.space_slots
== 0) {
4105 space_args
.total_spaces
= slot_count
;
4109 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
4111 alloc_size
= sizeof(*dest
) * slot_count
;
4113 /* we generally have at most 6 or so space infos, one for each raid
4114 * level. So, a whole page should be more than enough for everyone
4116 if (alloc_size
> PAGE_SIZE
)
4119 space_args
.total_spaces
= 0;
4120 dest
= kmalloc(alloc_size
, GFP_KERNEL
);
4125 /* now we have a buffer to copy into */
4126 for (i
= 0; i
< num_types
; i
++) {
4127 struct btrfs_space_info
*tmp
;
4134 list_for_each_entry_rcu(tmp
, &fs_info
->space_info
,
4136 if (tmp
->flags
== types
[i
]) {
4145 down_read(&info
->groups_sem
);
4146 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4147 if (!list_empty(&info
->block_groups
[c
])) {
4148 get_block_group_info(&info
->block_groups
[c
],
4150 memcpy(dest
, &space
, sizeof(space
));
4152 space_args
.total_spaces
++;
4158 up_read(&info
->groups_sem
);
4162 * Add global block reserve
4165 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
4167 spin_lock(&block_rsv
->lock
);
4168 space
.total_bytes
= block_rsv
->size
;
4169 space
.used_bytes
= block_rsv
->size
- block_rsv
->reserved
;
4170 spin_unlock(&block_rsv
->lock
);
4171 space
.flags
= BTRFS_SPACE_INFO_GLOBAL_RSV
;
4172 memcpy(dest
, &space
, sizeof(space
));
4173 space_args
.total_spaces
++;
4176 user_dest
= (struct btrfs_ioctl_space_info __user
*)
4177 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
4179 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
4184 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
4190 static noinline
long btrfs_ioctl_start_sync(struct btrfs_root
*root
,
4193 struct btrfs_trans_handle
*trans
;
4197 trans
= btrfs_attach_transaction_barrier(root
);
4198 if (IS_ERR(trans
)) {
4199 if (PTR_ERR(trans
) != -ENOENT
)
4200 return PTR_ERR(trans
);
4202 /* No running transaction, don't bother */
4203 transid
= root
->fs_info
->last_trans_committed
;
4206 transid
= trans
->transid
;
4207 ret
= btrfs_commit_transaction_async(trans
, 0);
4209 btrfs_end_transaction(trans
);
4214 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
4219 static noinline
long btrfs_ioctl_wait_sync(struct btrfs_fs_info
*fs_info
,
4225 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
4228 transid
= 0; /* current trans */
4230 return btrfs_wait_for_commit(fs_info
, transid
);
4233 static long btrfs_ioctl_scrub(struct file
*file
, void __user
*arg
)
4235 struct btrfs_fs_info
*fs_info
= btrfs_sb(file_inode(file
)->i_sb
);
4236 struct btrfs_ioctl_scrub_args
*sa
;
4239 if (!capable(CAP_SYS_ADMIN
))
4242 sa
= memdup_user(arg
, sizeof(*sa
));
4246 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
)) {
4247 ret
= mnt_want_write_file(file
);
4252 ret
= btrfs_scrub_dev(fs_info
, sa
->devid
, sa
->start
, sa
->end
,
4253 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
,
4257 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
4258 * error. This is important as it allows user space to know how much
4259 * progress scrub has done. For example, if scrub is canceled we get
4260 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
4261 * space. Later user space can inspect the progress from the structure
4262 * btrfs_ioctl_scrub_args and resume scrub from where it left off
4263 * previously (btrfs-progs does this).
4264 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
4265 * then return -EFAULT to signal the structure was not copied or it may
4266 * be corrupt and unreliable due to a partial copy.
4268 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4271 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
))
4272 mnt_drop_write_file(file
);
4278 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info
*fs_info
)
4280 if (!capable(CAP_SYS_ADMIN
))
4283 return btrfs_scrub_cancel(fs_info
);
4286 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info
*fs_info
,
4289 struct btrfs_ioctl_scrub_args
*sa
;
4292 if (!capable(CAP_SYS_ADMIN
))
4295 sa
= memdup_user(arg
, sizeof(*sa
));
4299 ret
= btrfs_scrub_progress(fs_info
, sa
->devid
, &sa
->progress
);
4301 if (ret
== 0 && copy_to_user(arg
, sa
, sizeof(*sa
)))
4308 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info
*fs_info
,
4311 struct btrfs_ioctl_get_dev_stats
*sa
;
4314 sa
= memdup_user(arg
, sizeof(*sa
));
4318 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
4323 ret
= btrfs_get_dev_stats(fs_info
, sa
);
4325 if (ret
== 0 && copy_to_user(arg
, sa
, sizeof(*sa
)))
4332 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info
*fs_info
,
4335 struct btrfs_ioctl_dev_replace_args
*p
;
4338 if (!capable(CAP_SYS_ADMIN
))
4341 p
= memdup_user(arg
, sizeof(*p
));
4346 case BTRFS_IOCTL_DEV_REPLACE_CMD_START
:
4347 if (sb_rdonly(fs_info
->sb
)) {
4351 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
4352 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
4354 ret
= btrfs_dev_replace_by_ioctl(fs_info
, p
);
4355 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4358 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
:
4359 btrfs_dev_replace_status(fs_info
, p
);
4362 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
:
4363 p
->result
= btrfs_dev_replace_cancel(fs_info
);
4371 if ((ret
== 0 || ret
== -ECANCELED
) && copy_to_user(arg
, p
, sizeof(*p
)))
4378 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
4384 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
4385 struct inode_fs_paths
*ipath
= NULL
;
4386 struct btrfs_path
*path
;
4388 if (!capable(CAP_DAC_READ_SEARCH
))
4391 path
= btrfs_alloc_path();
4397 ipa
= memdup_user(arg
, sizeof(*ipa
));
4404 size
= min_t(u32
, ipa
->size
, 4096);
4405 ipath
= init_ipath(size
, root
, path
);
4406 if (IS_ERR(ipath
)) {
4407 ret
= PTR_ERR(ipath
);
4412 ret
= paths_from_inode(ipa
->inum
, ipath
);
4416 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
4417 rel_ptr
= ipath
->fspath
->val
[i
] -
4418 (u64
)(unsigned long)ipath
->fspath
->val
;
4419 ipath
->fspath
->val
[i
] = rel_ptr
;
4422 ret
= copy_to_user((void __user
*)(unsigned long)ipa
->fspath
,
4423 ipath
->fspath
, size
);
4430 btrfs_free_path(path
);
4437 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
4439 struct btrfs_data_container
*inodes
= ctx
;
4440 const size_t c
= 3 * sizeof(u64
);
4442 if (inodes
->bytes_left
>= c
) {
4443 inodes
->bytes_left
-= c
;
4444 inodes
->val
[inodes
->elem_cnt
] = inum
;
4445 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
4446 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
4447 inodes
->elem_cnt
+= 3;
4449 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
4450 inodes
->bytes_left
= 0;
4451 inodes
->elem_missed
+= 3;
4457 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info
*fs_info
,
4458 void __user
*arg
, int version
)
4462 struct btrfs_ioctl_logical_ino_args
*loi
;
4463 struct btrfs_data_container
*inodes
= NULL
;
4464 struct btrfs_path
*path
= NULL
;
4467 if (!capable(CAP_SYS_ADMIN
))
4470 loi
= memdup_user(arg
, sizeof(*loi
));
4472 return PTR_ERR(loi
);
4475 ignore_offset
= false;
4476 size
= min_t(u32
, loi
->size
, SZ_64K
);
4478 /* All reserved bits must be 0 for now */
4479 if (memchr_inv(loi
->reserved
, 0, sizeof(loi
->reserved
))) {
4483 /* Only accept flags we have defined so far */
4484 if (loi
->flags
& ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET
)) {
4488 ignore_offset
= loi
->flags
& BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET
;
4489 size
= min_t(u32
, loi
->size
, SZ_16M
);
4492 path
= btrfs_alloc_path();
4498 inodes
= init_data_container(size
);
4499 if (IS_ERR(inodes
)) {
4500 ret
= PTR_ERR(inodes
);
4505 ret
= iterate_inodes_from_logical(loi
->logical
, fs_info
, path
,
4506 build_ino_list
, inodes
, ignore_offset
);
4512 ret
= copy_to_user((void __user
*)(unsigned long)loi
->inodes
, inodes
,
4518 btrfs_free_path(path
);
4526 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
,
4527 struct btrfs_ioctl_balance_args
*bargs
)
4529 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
4531 bargs
->flags
= bctl
->flags
;
4533 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
))
4534 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
4535 if (atomic_read(&fs_info
->balance_pause_req
))
4536 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
4537 if (atomic_read(&fs_info
->balance_cancel_req
))
4538 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
4540 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
4541 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
4542 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
4544 spin_lock(&fs_info
->balance_lock
);
4545 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
4546 spin_unlock(&fs_info
->balance_lock
);
4549 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
4551 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4552 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4553 struct btrfs_ioctl_balance_args
*bargs
;
4554 struct btrfs_balance_control
*bctl
;
4555 bool need_unlock
; /* for mut. excl. ops lock */
4558 if (!capable(CAP_SYS_ADMIN
))
4561 ret
= mnt_want_write_file(file
);
4566 if (!test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
4567 mutex_lock(&fs_info
->balance_mutex
);
4573 * mut. excl. ops lock is locked. Three possibilities:
4574 * (1) some other op is running
4575 * (2) balance is running
4576 * (3) balance is paused -- special case (think resume)
4578 mutex_lock(&fs_info
->balance_mutex
);
4579 if (fs_info
->balance_ctl
) {
4580 /* this is either (2) or (3) */
4581 if (!test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4582 mutex_unlock(&fs_info
->balance_mutex
);
4584 * Lock released to allow other waiters to continue,
4585 * we'll reexamine the status again.
4587 mutex_lock(&fs_info
->balance_mutex
);
4589 if (fs_info
->balance_ctl
&&
4590 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4592 need_unlock
= false;
4596 mutex_unlock(&fs_info
->balance_mutex
);
4600 mutex_unlock(&fs_info
->balance_mutex
);
4606 mutex_unlock(&fs_info
->balance_mutex
);
4607 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
4612 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
));
4615 bargs
= memdup_user(arg
, sizeof(*bargs
));
4616 if (IS_ERR(bargs
)) {
4617 ret
= PTR_ERR(bargs
);
4621 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
4622 if (!fs_info
->balance_ctl
) {
4627 bctl
= fs_info
->balance_ctl
;
4628 spin_lock(&fs_info
->balance_lock
);
4629 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
4630 spin_unlock(&fs_info
->balance_lock
);
4638 if (fs_info
->balance_ctl
) {
4643 bctl
= kzalloc(sizeof(*bctl
), GFP_KERNEL
);
4650 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
4651 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
4652 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
4654 bctl
->flags
= bargs
->flags
;
4656 /* balance everything - no filters */
4657 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
4660 if (bctl
->flags
& ~(BTRFS_BALANCE_ARGS_MASK
| BTRFS_BALANCE_TYPE_MASK
)) {
4667 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4668 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4669 * restriper was paused all the way until unmount, in free_fs_info.
4670 * The flag should be cleared after reset_balance_state.
4672 need_unlock
= false;
4674 ret
= btrfs_balance(fs_info
, bctl
, bargs
);
4677 if ((ret
== 0 || ret
== -ECANCELED
) && arg
) {
4678 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4687 mutex_unlock(&fs_info
->balance_mutex
);
4689 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4691 mnt_drop_write_file(file
);
4695 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info
*fs_info
, int cmd
)
4697 if (!capable(CAP_SYS_ADMIN
))
4701 case BTRFS_BALANCE_CTL_PAUSE
:
4702 return btrfs_pause_balance(fs_info
);
4703 case BTRFS_BALANCE_CTL_CANCEL
:
4704 return btrfs_cancel_balance(fs_info
);
4710 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info
*fs_info
,
4713 struct btrfs_ioctl_balance_args
*bargs
;
4716 if (!capable(CAP_SYS_ADMIN
))
4719 mutex_lock(&fs_info
->balance_mutex
);
4720 if (!fs_info
->balance_ctl
) {
4725 bargs
= kzalloc(sizeof(*bargs
), GFP_KERNEL
);
4731 btrfs_update_ioctl_balance_args(fs_info
, bargs
);
4733 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4738 mutex_unlock(&fs_info
->balance_mutex
);
4742 static long btrfs_ioctl_quota_ctl(struct file
*file
, void __user
*arg
)
4744 struct inode
*inode
= file_inode(file
);
4745 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4746 struct btrfs_ioctl_quota_ctl_args
*sa
;
4749 if (!capable(CAP_SYS_ADMIN
))
4752 ret
= mnt_want_write_file(file
);
4756 sa
= memdup_user(arg
, sizeof(*sa
));
4762 down_write(&fs_info
->subvol_sem
);
4765 case BTRFS_QUOTA_CTL_ENABLE
:
4766 ret
= btrfs_quota_enable(fs_info
);
4768 case BTRFS_QUOTA_CTL_DISABLE
:
4769 ret
= btrfs_quota_disable(fs_info
);
4777 up_write(&fs_info
->subvol_sem
);
4779 mnt_drop_write_file(file
);
4783 static long btrfs_ioctl_qgroup_assign(struct file
*file
, void __user
*arg
)
4785 struct inode
*inode
= file_inode(file
);
4786 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4787 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4788 struct btrfs_ioctl_qgroup_assign_args
*sa
;
4789 struct btrfs_trans_handle
*trans
;
4793 if (!capable(CAP_SYS_ADMIN
))
4796 ret
= mnt_want_write_file(file
);
4800 sa
= memdup_user(arg
, sizeof(*sa
));
4806 trans
= btrfs_join_transaction(root
);
4807 if (IS_ERR(trans
)) {
4808 ret
= PTR_ERR(trans
);
4813 ret
= btrfs_add_qgroup_relation(trans
, sa
->src
, sa
->dst
);
4815 ret
= btrfs_del_qgroup_relation(trans
, sa
->src
, sa
->dst
);
4818 /* update qgroup status and info */
4819 err
= btrfs_run_qgroups(trans
);
4821 btrfs_handle_fs_error(fs_info
, err
,
4822 "failed to update qgroup status and info");
4823 err
= btrfs_end_transaction(trans
);
4830 mnt_drop_write_file(file
);
4834 static long btrfs_ioctl_qgroup_create(struct file
*file
, void __user
*arg
)
4836 struct inode
*inode
= file_inode(file
);
4837 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4838 struct btrfs_ioctl_qgroup_create_args
*sa
;
4839 struct btrfs_trans_handle
*trans
;
4843 if (!capable(CAP_SYS_ADMIN
))
4846 ret
= mnt_want_write_file(file
);
4850 sa
= memdup_user(arg
, sizeof(*sa
));
4856 if (!sa
->qgroupid
) {
4861 trans
= btrfs_join_transaction(root
);
4862 if (IS_ERR(trans
)) {
4863 ret
= PTR_ERR(trans
);
4868 ret
= btrfs_create_qgroup(trans
, sa
->qgroupid
);
4870 ret
= btrfs_remove_qgroup(trans
, sa
->qgroupid
);
4873 err
= btrfs_end_transaction(trans
);
4880 mnt_drop_write_file(file
);
4884 static long btrfs_ioctl_qgroup_limit(struct file
*file
, void __user
*arg
)
4886 struct inode
*inode
= file_inode(file
);
4887 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4888 struct btrfs_ioctl_qgroup_limit_args
*sa
;
4889 struct btrfs_trans_handle
*trans
;
4894 if (!capable(CAP_SYS_ADMIN
))
4897 ret
= mnt_want_write_file(file
);
4901 sa
= memdup_user(arg
, sizeof(*sa
));
4907 trans
= btrfs_join_transaction(root
);
4908 if (IS_ERR(trans
)) {
4909 ret
= PTR_ERR(trans
);
4913 qgroupid
= sa
->qgroupid
;
4915 /* take the current subvol as qgroup */
4916 qgroupid
= root
->root_key
.objectid
;
4919 ret
= btrfs_limit_qgroup(trans
, qgroupid
, &sa
->lim
);
4921 err
= btrfs_end_transaction(trans
);
4928 mnt_drop_write_file(file
);
4932 static long btrfs_ioctl_quota_rescan(struct file
*file
, void __user
*arg
)
4934 struct inode
*inode
= file_inode(file
);
4935 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4936 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4939 if (!capable(CAP_SYS_ADMIN
))
4942 ret
= mnt_want_write_file(file
);
4946 qsa
= memdup_user(arg
, sizeof(*qsa
));
4957 ret
= btrfs_qgroup_rescan(fs_info
);
4962 mnt_drop_write_file(file
);
4966 static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info
*fs_info
,
4969 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4972 if (!capable(CAP_SYS_ADMIN
))
4975 qsa
= kzalloc(sizeof(*qsa
), GFP_KERNEL
);
4979 if (fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
4981 qsa
->progress
= fs_info
->qgroup_rescan_progress
.objectid
;
4984 if (copy_to_user(arg
, qsa
, sizeof(*qsa
)))
4991 static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info
*fs_info
,
4994 if (!capable(CAP_SYS_ADMIN
))
4997 return btrfs_qgroup_wait_for_completion(fs_info
, true);
5000 static long _btrfs_ioctl_set_received_subvol(struct file
*file
,
5001 struct btrfs_ioctl_received_subvol_args
*sa
)
5003 struct inode
*inode
= file_inode(file
);
5004 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5005 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5006 struct btrfs_root_item
*root_item
= &root
->root_item
;
5007 struct btrfs_trans_handle
*trans
;
5008 struct timespec64 ct
= current_time(inode
);
5010 int received_uuid_changed
;
5012 if (!inode_owner_or_capable(inode
))
5015 ret
= mnt_want_write_file(file
);
5019 down_write(&fs_info
->subvol_sem
);
5021 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
5026 if (btrfs_root_readonly(root
)) {
5033 * 2 - uuid items (received uuid + subvol uuid)
5035 trans
= btrfs_start_transaction(root
, 3);
5036 if (IS_ERR(trans
)) {
5037 ret
= PTR_ERR(trans
);
5042 sa
->rtransid
= trans
->transid
;
5043 sa
->rtime
.sec
= ct
.tv_sec
;
5044 sa
->rtime
.nsec
= ct
.tv_nsec
;
5046 received_uuid_changed
= memcmp(root_item
->received_uuid
, sa
->uuid
,
5048 if (received_uuid_changed
&&
5049 !btrfs_is_empty_uuid(root_item
->received_uuid
)) {
5050 ret
= btrfs_uuid_tree_remove(trans
, root_item
->received_uuid
,
5051 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5052 root
->root_key
.objectid
);
5053 if (ret
&& ret
!= -ENOENT
) {
5054 btrfs_abort_transaction(trans
, ret
);
5055 btrfs_end_transaction(trans
);
5059 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
5060 btrfs_set_root_stransid(root_item
, sa
->stransid
);
5061 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
5062 btrfs_set_stack_timespec_sec(&root_item
->stime
, sa
->stime
.sec
);
5063 btrfs_set_stack_timespec_nsec(&root_item
->stime
, sa
->stime
.nsec
);
5064 btrfs_set_stack_timespec_sec(&root_item
->rtime
, sa
->rtime
.sec
);
5065 btrfs_set_stack_timespec_nsec(&root_item
->rtime
, sa
->rtime
.nsec
);
5067 ret
= btrfs_update_root(trans
, fs_info
->tree_root
,
5068 &root
->root_key
, &root
->root_item
);
5070 btrfs_end_transaction(trans
);
5073 if (received_uuid_changed
&& !btrfs_is_empty_uuid(sa
->uuid
)) {
5074 ret
= btrfs_uuid_tree_add(trans
, sa
->uuid
,
5075 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5076 root
->root_key
.objectid
);
5077 if (ret
< 0 && ret
!= -EEXIST
) {
5078 btrfs_abort_transaction(trans
, ret
);
5079 btrfs_end_transaction(trans
);
5083 ret
= btrfs_commit_transaction(trans
);
5085 up_write(&fs_info
->subvol_sem
);
5086 mnt_drop_write_file(file
);
5091 static long btrfs_ioctl_set_received_subvol_32(struct file
*file
,
5094 struct btrfs_ioctl_received_subvol_args_32
*args32
= NULL
;
5095 struct btrfs_ioctl_received_subvol_args
*args64
= NULL
;
5098 args32
= memdup_user(arg
, sizeof(*args32
));
5100 return PTR_ERR(args32
);
5102 args64
= kmalloc(sizeof(*args64
), GFP_KERNEL
);
5108 memcpy(args64
->uuid
, args32
->uuid
, BTRFS_UUID_SIZE
);
5109 args64
->stransid
= args32
->stransid
;
5110 args64
->rtransid
= args32
->rtransid
;
5111 args64
->stime
.sec
= args32
->stime
.sec
;
5112 args64
->stime
.nsec
= args32
->stime
.nsec
;
5113 args64
->rtime
.sec
= args32
->rtime
.sec
;
5114 args64
->rtime
.nsec
= args32
->rtime
.nsec
;
5115 args64
->flags
= args32
->flags
;
5117 ret
= _btrfs_ioctl_set_received_subvol(file
, args64
);
5121 memcpy(args32
->uuid
, args64
->uuid
, BTRFS_UUID_SIZE
);
5122 args32
->stransid
= args64
->stransid
;
5123 args32
->rtransid
= args64
->rtransid
;
5124 args32
->stime
.sec
= args64
->stime
.sec
;
5125 args32
->stime
.nsec
= args64
->stime
.nsec
;
5126 args32
->rtime
.sec
= args64
->rtime
.sec
;
5127 args32
->rtime
.nsec
= args64
->rtime
.nsec
;
5128 args32
->flags
= args64
->flags
;
5130 ret
= copy_to_user(arg
, args32
, sizeof(*args32
));
5141 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
5144 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
5147 sa
= memdup_user(arg
, sizeof(*sa
));
5151 ret
= _btrfs_ioctl_set_received_subvol(file
, sa
);
5156 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
5165 static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info
*fs_info
,
5170 char label
[BTRFS_LABEL_SIZE
];
5172 spin_lock(&fs_info
->super_lock
);
5173 memcpy(label
, fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
5174 spin_unlock(&fs_info
->super_lock
);
5176 len
= strnlen(label
, BTRFS_LABEL_SIZE
);
5178 if (len
== BTRFS_LABEL_SIZE
) {
5180 "label is too long, return the first %zu bytes",
5184 ret
= copy_to_user(arg
, label
, len
);
5186 return ret
? -EFAULT
: 0;
5189 static int btrfs_ioctl_set_fslabel(struct file
*file
, void __user
*arg
)
5191 struct inode
*inode
= file_inode(file
);
5192 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5193 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5194 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5195 struct btrfs_trans_handle
*trans
;
5196 char label
[BTRFS_LABEL_SIZE
];
5199 if (!capable(CAP_SYS_ADMIN
))
5202 if (copy_from_user(label
, arg
, sizeof(label
)))
5205 if (strnlen(label
, BTRFS_LABEL_SIZE
) == BTRFS_LABEL_SIZE
) {
5207 "unable to set label with more than %d bytes",
5208 BTRFS_LABEL_SIZE
- 1);
5212 ret
= mnt_want_write_file(file
);
5216 trans
= btrfs_start_transaction(root
, 0);
5217 if (IS_ERR(trans
)) {
5218 ret
= PTR_ERR(trans
);
5222 spin_lock(&fs_info
->super_lock
);
5223 strcpy(super_block
->label
, label
);
5224 spin_unlock(&fs_info
->super_lock
);
5225 ret
= btrfs_commit_transaction(trans
);
5228 mnt_drop_write_file(file
);
5232 #define INIT_FEATURE_FLAGS(suffix) \
5233 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5234 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5235 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5237 int btrfs_ioctl_get_supported_features(void __user
*arg
)
5239 static const struct btrfs_ioctl_feature_flags features
[3] = {
5240 INIT_FEATURE_FLAGS(SUPP
),
5241 INIT_FEATURE_FLAGS(SAFE_SET
),
5242 INIT_FEATURE_FLAGS(SAFE_CLEAR
)
5245 if (copy_to_user(arg
, &features
, sizeof(features
)))
5251 static int btrfs_ioctl_get_features(struct btrfs_fs_info
*fs_info
,
5254 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5255 struct btrfs_ioctl_feature_flags features
;
5257 features
.compat_flags
= btrfs_super_compat_flags(super_block
);
5258 features
.compat_ro_flags
= btrfs_super_compat_ro_flags(super_block
);
5259 features
.incompat_flags
= btrfs_super_incompat_flags(super_block
);
5261 if (copy_to_user(arg
, &features
, sizeof(features
)))
5267 static int check_feature_bits(struct btrfs_fs_info
*fs_info
,
5268 enum btrfs_feature_set set
,
5269 u64 change_mask
, u64 flags
, u64 supported_flags
,
5270 u64 safe_set
, u64 safe_clear
)
5272 const char *type
= btrfs_feature_set_name(set
);
5274 u64 disallowed
, unsupported
;
5275 u64 set_mask
= flags
& change_mask
;
5276 u64 clear_mask
= ~flags
& change_mask
;
5278 unsupported
= set_mask
& ~supported_flags
;
5280 names
= btrfs_printable_features(set
, unsupported
);
5283 "this kernel does not support the %s feature bit%s",
5284 names
, strchr(names
, ',') ? "s" : "");
5288 "this kernel does not support %s bits 0x%llx",
5293 disallowed
= set_mask
& ~safe_set
;
5295 names
= btrfs_printable_features(set
, disallowed
);
5298 "can't set the %s feature bit%s while mounted",
5299 names
, strchr(names
, ',') ? "s" : "");
5303 "can't set %s bits 0x%llx while mounted",
5308 disallowed
= clear_mask
& ~safe_clear
;
5310 names
= btrfs_printable_features(set
, disallowed
);
5313 "can't clear the %s feature bit%s while mounted",
5314 names
, strchr(names
, ',') ? "s" : "");
5318 "can't clear %s bits 0x%llx while mounted",
5326 #define check_feature(fs_info, change_mask, flags, mask_base) \
5327 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
5328 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5329 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5330 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5332 static int btrfs_ioctl_set_features(struct file
*file
, void __user
*arg
)
5334 struct inode
*inode
= file_inode(file
);
5335 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5336 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5337 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5338 struct btrfs_ioctl_feature_flags flags
[2];
5339 struct btrfs_trans_handle
*trans
;
5343 if (!capable(CAP_SYS_ADMIN
))
5346 if (copy_from_user(flags
, arg
, sizeof(flags
)))
5350 if (!flags
[0].compat_flags
&& !flags
[0].compat_ro_flags
&&
5351 !flags
[0].incompat_flags
)
5354 ret
= check_feature(fs_info
, flags
[0].compat_flags
,
5355 flags
[1].compat_flags
, COMPAT
);
5359 ret
= check_feature(fs_info
, flags
[0].compat_ro_flags
,
5360 flags
[1].compat_ro_flags
, COMPAT_RO
);
5364 ret
= check_feature(fs_info
, flags
[0].incompat_flags
,
5365 flags
[1].incompat_flags
, INCOMPAT
);
5369 ret
= mnt_want_write_file(file
);
5373 trans
= btrfs_start_transaction(root
, 0);
5374 if (IS_ERR(trans
)) {
5375 ret
= PTR_ERR(trans
);
5376 goto out_drop_write
;
5379 spin_lock(&fs_info
->super_lock
);
5380 newflags
= btrfs_super_compat_flags(super_block
);
5381 newflags
|= flags
[0].compat_flags
& flags
[1].compat_flags
;
5382 newflags
&= ~(flags
[0].compat_flags
& ~flags
[1].compat_flags
);
5383 btrfs_set_super_compat_flags(super_block
, newflags
);
5385 newflags
= btrfs_super_compat_ro_flags(super_block
);
5386 newflags
|= flags
[0].compat_ro_flags
& flags
[1].compat_ro_flags
;
5387 newflags
&= ~(flags
[0].compat_ro_flags
& ~flags
[1].compat_ro_flags
);
5388 btrfs_set_super_compat_ro_flags(super_block
, newflags
);
5390 newflags
= btrfs_super_incompat_flags(super_block
);
5391 newflags
|= flags
[0].incompat_flags
& flags
[1].incompat_flags
;
5392 newflags
&= ~(flags
[0].incompat_flags
& ~flags
[1].incompat_flags
);
5393 btrfs_set_super_incompat_flags(super_block
, newflags
);
5394 spin_unlock(&fs_info
->super_lock
);
5396 ret
= btrfs_commit_transaction(trans
);
5398 mnt_drop_write_file(file
);
5403 static int _btrfs_ioctl_send(struct file
*file
, void __user
*argp
, bool compat
)
5405 struct btrfs_ioctl_send_args
*arg
;
5409 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5410 struct btrfs_ioctl_send_args_32 args32
;
5412 ret
= copy_from_user(&args32
, argp
, sizeof(args32
));
5415 arg
= kzalloc(sizeof(*arg
), GFP_KERNEL
);
5418 arg
->send_fd
= args32
.send_fd
;
5419 arg
->clone_sources_count
= args32
.clone_sources_count
;
5420 arg
->clone_sources
= compat_ptr(args32
.clone_sources
);
5421 arg
->parent_root
= args32
.parent_root
;
5422 arg
->flags
= args32
.flags
;
5423 memcpy(arg
->reserved
, args32
.reserved
,
5424 sizeof(args32
.reserved
));
5429 arg
= memdup_user(argp
, sizeof(*arg
));
5431 return PTR_ERR(arg
);
5433 ret
= btrfs_ioctl_send(file
, arg
);
5438 long btrfs_ioctl(struct file
*file
, unsigned int
5439 cmd
, unsigned long arg
)
5441 struct inode
*inode
= file_inode(file
);
5442 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5443 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5444 void __user
*argp
= (void __user
*)arg
;
5447 case FS_IOC_GETFLAGS
:
5448 return btrfs_ioctl_getflags(file
, argp
);
5449 case FS_IOC_SETFLAGS
:
5450 return btrfs_ioctl_setflags(file
, argp
);
5451 case FS_IOC_GETVERSION
:
5452 return btrfs_ioctl_getversion(file
, argp
);
5453 case FS_IOC_GETFSLABEL
:
5454 return btrfs_ioctl_get_fslabel(fs_info
, argp
);
5455 case FS_IOC_SETFSLABEL
:
5456 return btrfs_ioctl_set_fslabel(file
, argp
);
5458 return btrfs_ioctl_fitrim(fs_info
, argp
);
5459 case BTRFS_IOC_SNAP_CREATE
:
5460 return btrfs_ioctl_snap_create(file
, argp
, 0);
5461 case BTRFS_IOC_SNAP_CREATE_V2
:
5462 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
5463 case BTRFS_IOC_SUBVOL_CREATE
:
5464 return btrfs_ioctl_snap_create(file
, argp
, 1);
5465 case BTRFS_IOC_SUBVOL_CREATE_V2
:
5466 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
5467 case BTRFS_IOC_SNAP_DESTROY
:
5468 return btrfs_ioctl_snap_destroy(file
, argp
);
5469 case BTRFS_IOC_SUBVOL_GETFLAGS
:
5470 return btrfs_ioctl_subvol_getflags(file
, argp
);
5471 case BTRFS_IOC_SUBVOL_SETFLAGS
:
5472 return btrfs_ioctl_subvol_setflags(file
, argp
);
5473 case BTRFS_IOC_DEFAULT_SUBVOL
:
5474 return btrfs_ioctl_default_subvol(file
, argp
);
5475 case BTRFS_IOC_DEFRAG
:
5476 return btrfs_ioctl_defrag(file
, NULL
);
5477 case BTRFS_IOC_DEFRAG_RANGE
:
5478 return btrfs_ioctl_defrag(file
, argp
);
5479 case BTRFS_IOC_RESIZE
:
5480 return btrfs_ioctl_resize(file
, argp
);
5481 case BTRFS_IOC_ADD_DEV
:
5482 return btrfs_ioctl_add_dev(fs_info
, argp
);
5483 case BTRFS_IOC_RM_DEV
:
5484 return btrfs_ioctl_rm_dev(file
, argp
);
5485 case BTRFS_IOC_RM_DEV_V2
:
5486 return btrfs_ioctl_rm_dev_v2(file
, argp
);
5487 case BTRFS_IOC_FS_INFO
:
5488 return btrfs_ioctl_fs_info(fs_info
, argp
);
5489 case BTRFS_IOC_DEV_INFO
:
5490 return btrfs_ioctl_dev_info(fs_info
, argp
);
5491 case BTRFS_IOC_BALANCE
:
5492 return btrfs_ioctl_balance(file
, NULL
);
5493 case BTRFS_IOC_TREE_SEARCH
:
5494 return btrfs_ioctl_tree_search(file
, argp
);
5495 case BTRFS_IOC_TREE_SEARCH_V2
:
5496 return btrfs_ioctl_tree_search_v2(file
, argp
);
5497 case BTRFS_IOC_INO_LOOKUP
:
5498 return btrfs_ioctl_ino_lookup(file
, argp
);
5499 case BTRFS_IOC_INO_PATHS
:
5500 return btrfs_ioctl_ino_to_path(root
, argp
);
5501 case BTRFS_IOC_LOGICAL_INO
:
5502 return btrfs_ioctl_logical_to_ino(fs_info
, argp
, 1);
5503 case BTRFS_IOC_LOGICAL_INO_V2
:
5504 return btrfs_ioctl_logical_to_ino(fs_info
, argp
, 2);
5505 case BTRFS_IOC_SPACE_INFO
:
5506 return btrfs_ioctl_space_info(fs_info
, argp
);
5507 case BTRFS_IOC_SYNC
: {
5510 ret
= btrfs_start_delalloc_roots(fs_info
, -1);
5513 ret
= btrfs_sync_fs(inode
->i_sb
, 1);
5515 * The transaction thread may want to do more work,
5516 * namely it pokes the cleaner kthread that will start
5517 * processing uncleaned subvols.
5519 wake_up_process(fs_info
->transaction_kthread
);
5522 case BTRFS_IOC_START_SYNC
:
5523 return btrfs_ioctl_start_sync(root
, argp
);
5524 case BTRFS_IOC_WAIT_SYNC
:
5525 return btrfs_ioctl_wait_sync(fs_info
, argp
);
5526 case BTRFS_IOC_SCRUB
:
5527 return btrfs_ioctl_scrub(file
, argp
);
5528 case BTRFS_IOC_SCRUB_CANCEL
:
5529 return btrfs_ioctl_scrub_cancel(fs_info
);
5530 case BTRFS_IOC_SCRUB_PROGRESS
:
5531 return btrfs_ioctl_scrub_progress(fs_info
, argp
);
5532 case BTRFS_IOC_BALANCE_V2
:
5533 return btrfs_ioctl_balance(file
, argp
);
5534 case BTRFS_IOC_BALANCE_CTL
:
5535 return btrfs_ioctl_balance_ctl(fs_info
, arg
);
5536 case BTRFS_IOC_BALANCE_PROGRESS
:
5537 return btrfs_ioctl_balance_progress(fs_info
, argp
);
5538 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
5539 return btrfs_ioctl_set_received_subvol(file
, argp
);
5541 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32
:
5542 return btrfs_ioctl_set_received_subvol_32(file
, argp
);
5544 case BTRFS_IOC_SEND
:
5545 return _btrfs_ioctl_send(file
, argp
, false);
5546 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5547 case BTRFS_IOC_SEND_32
:
5548 return _btrfs_ioctl_send(file
, argp
, true);
5550 case BTRFS_IOC_GET_DEV_STATS
:
5551 return btrfs_ioctl_get_dev_stats(fs_info
, argp
);
5552 case BTRFS_IOC_QUOTA_CTL
:
5553 return btrfs_ioctl_quota_ctl(file
, argp
);
5554 case BTRFS_IOC_QGROUP_ASSIGN
:
5555 return btrfs_ioctl_qgroup_assign(file
, argp
);
5556 case BTRFS_IOC_QGROUP_CREATE
:
5557 return btrfs_ioctl_qgroup_create(file
, argp
);
5558 case BTRFS_IOC_QGROUP_LIMIT
:
5559 return btrfs_ioctl_qgroup_limit(file
, argp
);
5560 case BTRFS_IOC_QUOTA_RESCAN
:
5561 return btrfs_ioctl_quota_rescan(file
, argp
);
5562 case BTRFS_IOC_QUOTA_RESCAN_STATUS
:
5563 return btrfs_ioctl_quota_rescan_status(fs_info
, argp
);
5564 case BTRFS_IOC_QUOTA_RESCAN_WAIT
:
5565 return btrfs_ioctl_quota_rescan_wait(fs_info
, argp
);
5566 case BTRFS_IOC_DEV_REPLACE
:
5567 return btrfs_ioctl_dev_replace(fs_info
, argp
);
5568 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
5569 return btrfs_ioctl_get_supported_features(argp
);
5570 case BTRFS_IOC_GET_FEATURES
:
5571 return btrfs_ioctl_get_features(fs_info
, argp
);
5572 case BTRFS_IOC_SET_FEATURES
:
5573 return btrfs_ioctl_set_features(file
, argp
);
5574 case FS_IOC_FSGETXATTR
:
5575 return btrfs_ioctl_fsgetxattr(file
, argp
);
5576 case FS_IOC_FSSETXATTR
:
5577 return btrfs_ioctl_fssetxattr(file
, argp
);
5578 case BTRFS_IOC_GET_SUBVOL_INFO
:
5579 return btrfs_ioctl_get_subvol_info(file
, argp
);
5580 case BTRFS_IOC_GET_SUBVOL_ROOTREF
:
5581 return btrfs_ioctl_get_subvol_rootref(file
, argp
);
5582 case BTRFS_IOC_INO_LOOKUP_USER
:
5583 return btrfs_ioctl_ino_lookup_user(file
, argp
);
5589 #ifdef CONFIG_COMPAT
5590 long btrfs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
5593 * These all access 32-bit values anyway so no further
5594 * handling is necessary.
5597 case FS_IOC32_GETFLAGS
:
5598 cmd
= FS_IOC_GETFLAGS
;
5600 case FS_IOC32_SETFLAGS
:
5601 cmd
= FS_IOC_SETFLAGS
;
5603 case FS_IOC32_GETVERSION
:
5604 cmd
= FS_IOC_GETVERSION
;
5608 return btrfs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));