1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/kernel.h>
8 #include <linux/file.h>
10 #include <linux/fsnotify.h>
11 #include <linux/pagemap.h>
12 #include <linux/highmem.h>
13 #include <linux/time.h>
14 #include <linux/string.h>
15 #include <linux/backing-dev.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/writeback.h>
19 #include <linux/compat.h>
20 #include <linux/security.h>
21 #include <linux/xattr.h>
23 #include <linux/slab.h>
24 #include <linux/blkdev.h>
25 #include <linux/uuid.h>
26 #include <linux/btrfs.h>
27 #include <linux/uaccess.h>
28 #include <linux/iversion.h>
31 #include "transaction.h"
32 #include "btrfs_inode.h"
33 #include "print-tree.h"
36 #include "inode-map.h"
38 #include "rcu-string.h"
40 #include "dev-replace.h"
45 #include "compression.h"
48 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
49 * structures are incorrect, as the timespec structure from userspace
50 * is 4 bytes too small. We define these alternatives here to teach
51 * the kernel about the 32-bit struct packing.
53 struct btrfs_ioctl_timespec_32
{
56 } __attribute__ ((__packed__
));
58 struct btrfs_ioctl_received_subvol_args_32
{
59 char uuid
[BTRFS_UUID_SIZE
]; /* in */
60 __u64 stransid
; /* in */
61 __u64 rtransid
; /* out */
62 struct btrfs_ioctl_timespec_32 stime
; /* in */
63 struct btrfs_ioctl_timespec_32 rtime
; /* out */
65 __u64 reserved
[16]; /* in */
66 } __attribute__ ((__packed__
));
68 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
69 struct btrfs_ioctl_received_subvol_args_32)
72 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
73 struct btrfs_ioctl_send_args_32
{
74 __s64 send_fd
; /* in */
75 __u64 clone_sources_count
; /* in */
76 compat_uptr_t clone_sources
; /* in */
77 __u64 parent_root
; /* in */
79 __u64 reserved
[4]; /* in */
80 } __attribute__ ((__packed__
));
82 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
83 struct btrfs_ioctl_send_args_32)
86 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
87 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
,
90 /* Mask out flags that are inappropriate for the given type of inode. */
91 static unsigned int btrfs_mask_fsflags_for_type(struct inode
*inode
,
94 if (S_ISDIR(inode
->i_mode
))
96 else if (S_ISREG(inode
->i_mode
))
97 return flags
& ~FS_DIRSYNC_FL
;
99 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
106 static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags
)
108 unsigned int iflags
= 0;
110 if (flags
& BTRFS_INODE_SYNC
)
111 iflags
|= FS_SYNC_FL
;
112 if (flags
& BTRFS_INODE_IMMUTABLE
)
113 iflags
|= FS_IMMUTABLE_FL
;
114 if (flags
& BTRFS_INODE_APPEND
)
115 iflags
|= FS_APPEND_FL
;
116 if (flags
& BTRFS_INODE_NODUMP
)
117 iflags
|= FS_NODUMP_FL
;
118 if (flags
& BTRFS_INODE_NOATIME
)
119 iflags
|= FS_NOATIME_FL
;
120 if (flags
& BTRFS_INODE_DIRSYNC
)
121 iflags
|= FS_DIRSYNC_FL
;
122 if (flags
& BTRFS_INODE_NODATACOW
)
123 iflags
|= FS_NOCOW_FL
;
125 if (flags
& BTRFS_INODE_NOCOMPRESS
)
126 iflags
|= FS_NOCOMP_FL
;
127 else if (flags
& BTRFS_INODE_COMPRESS
)
128 iflags
|= FS_COMPR_FL
;
134 * Update inode->i_flags based on the btrfs internal flags.
136 void btrfs_sync_inode_flags_to_i_flags(struct inode
*inode
)
138 struct btrfs_inode
*binode
= BTRFS_I(inode
);
139 unsigned int new_fl
= 0;
141 if (binode
->flags
& BTRFS_INODE_SYNC
)
143 if (binode
->flags
& BTRFS_INODE_IMMUTABLE
)
144 new_fl
|= S_IMMUTABLE
;
145 if (binode
->flags
& BTRFS_INODE_APPEND
)
147 if (binode
->flags
& BTRFS_INODE_NOATIME
)
149 if (binode
->flags
& BTRFS_INODE_DIRSYNC
)
152 set_mask_bits(&inode
->i_flags
,
153 S_SYNC
| S_APPEND
| S_IMMUTABLE
| S_NOATIME
| S_DIRSYNC
,
157 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
159 struct btrfs_inode
*binode
= BTRFS_I(file_inode(file
));
160 unsigned int flags
= btrfs_inode_flags_to_fsflags(binode
->flags
);
162 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
167 /* Check if @flags are a supported and valid set of FS_*_FL flags */
168 static int check_fsflags(unsigned int flags
)
170 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
171 FS_NOATIME_FL
| FS_NODUMP_FL
| \
172 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
173 FS_NOCOMP_FL
| FS_COMPR_FL
|
177 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
183 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
185 struct inode
*inode
= file_inode(file
);
186 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
187 struct btrfs_inode
*binode
= BTRFS_I(inode
);
188 struct btrfs_root
*root
= binode
->root
;
189 struct btrfs_trans_handle
*trans
;
190 unsigned int fsflags
, old_fsflags
;
193 unsigned int old_i_flags
;
196 if (!inode_owner_or_capable(inode
))
199 if (btrfs_root_readonly(root
))
202 if (copy_from_user(&fsflags
, arg
, sizeof(fsflags
)))
205 ret
= check_fsflags(fsflags
);
209 ret
= mnt_want_write_file(file
);
215 old_flags
= binode
->flags
;
216 old_i_flags
= inode
->i_flags
;
217 mode
= inode
->i_mode
;
219 fsflags
= btrfs_mask_fsflags_for_type(inode
, fsflags
);
220 old_fsflags
= btrfs_inode_flags_to_fsflags(binode
->flags
);
221 if ((fsflags
^ old_fsflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
222 if (!capable(CAP_LINUX_IMMUTABLE
)) {
228 if (fsflags
& FS_SYNC_FL
)
229 binode
->flags
|= BTRFS_INODE_SYNC
;
231 binode
->flags
&= ~BTRFS_INODE_SYNC
;
232 if (fsflags
& FS_IMMUTABLE_FL
)
233 binode
->flags
|= BTRFS_INODE_IMMUTABLE
;
235 binode
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
236 if (fsflags
& FS_APPEND_FL
)
237 binode
->flags
|= BTRFS_INODE_APPEND
;
239 binode
->flags
&= ~BTRFS_INODE_APPEND
;
240 if (fsflags
& FS_NODUMP_FL
)
241 binode
->flags
|= BTRFS_INODE_NODUMP
;
243 binode
->flags
&= ~BTRFS_INODE_NODUMP
;
244 if (fsflags
& FS_NOATIME_FL
)
245 binode
->flags
|= BTRFS_INODE_NOATIME
;
247 binode
->flags
&= ~BTRFS_INODE_NOATIME
;
248 if (fsflags
& FS_DIRSYNC_FL
)
249 binode
->flags
|= BTRFS_INODE_DIRSYNC
;
251 binode
->flags
&= ~BTRFS_INODE_DIRSYNC
;
252 if (fsflags
& FS_NOCOW_FL
) {
255 * It's safe to turn csums off here, no extents exist.
256 * Otherwise we want the flag to reflect the real COW
257 * status of the file and will not set it.
259 if (inode
->i_size
== 0)
260 binode
->flags
|= BTRFS_INODE_NODATACOW
261 | BTRFS_INODE_NODATASUM
;
263 binode
->flags
|= BTRFS_INODE_NODATACOW
;
267 * Revert back under same assumptions as above
270 if (inode
->i_size
== 0)
271 binode
->flags
&= ~(BTRFS_INODE_NODATACOW
272 | BTRFS_INODE_NODATASUM
);
274 binode
->flags
&= ~BTRFS_INODE_NODATACOW
;
279 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
280 * flag may be changed automatically if compression code won't make
283 if (fsflags
& FS_NOCOMP_FL
) {
284 binode
->flags
&= ~BTRFS_INODE_COMPRESS
;
285 binode
->flags
|= BTRFS_INODE_NOCOMPRESS
;
287 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
288 if (ret
&& ret
!= -ENODATA
)
290 } else if (fsflags
& FS_COMPR_FL
) {
293 binode
->flags
|= BTRFS_INODE_COMPRESS
;
294 binode
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
296 comp
= btrfs_compress_type2str(fs_info
->compress_type
);
297 if (!comp
|| comp
[0] == 0)
298 comp
= btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB
);
300 ret
= btrfs_set_prop(inode
, "btrfs.compression",
301 comp
, strlen(comp
), 0);
306 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
307 if (ret
&& ret
!= -ENODATA
)
309 binode
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
312 trans
= btrfs_start_transaction(root
, 1);
314 ret
= PTR_ERR(trans
);
318 btrfs_sync_inode_flags_to_i_flags(inode
);
319 inode_inc_iversion(inode
);
320 inode
->i_ctime
= current_time(inode
);
321 ret
= btrfs_update_inode(trans
, root
, inode
);
323 btrfs_end_transaction(trans
);
326 binode
->flags
= old_flags
;
327 inode
->i_flags
= old_i_flags
;
332 mnt_drop_write_file(file
);
337 * Translate btrfs internal inode flags to xflags as expected by the
338 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
341 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags
)
343 unsigned int xflags
= 0;
345 if (flags
& BTRFS_INODE_APPEND
)
346 xflags
|= FS_XFLAG_APPEND
;
347 if (flags
& BTRFS_INODE_IMMUTABLE
)
348 xflags
|= FS_XFLAG_IMMUTABLE
;
349 if (flags
& BTRFS_INODE_NOATIME
)
350 xflags
|= FS_XFLAG_NOATIME
;
351 if (flags
& BTRFS_INODE_NODUMP
)
352 xflags
|= FS_XFLAG_NODUMP
;
353 if (flags
& BTRFS_INODE_SYNC
)
354 xflags
|= FS_XFLAG_SYNC
;
359 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
360 static int check_xflags(unsigned int flags
)
362 if (flags
& ~(FS_XFLAG_APPEND
| FS_XFLAG_IMMUTABLE
| FS_XFLAG_NOATIME
|
363 FS_XFLAG_NODUMP
| FS_XFLAG_SYNC
))
369 * Set the xflags from the internal inode flags. The remaining items of fsxattr
372 static int btrfs_ioctl_fsgetxattr(struct file
*file
, void __user
*arg
)
374 struct btrfs_inode
*binode
= BTRFS_I(file_inode(file
));
377 memset(&fa
, 0, sizeof(fa
));
378 fa
.fsx_xflags
= 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
;
394 unsigned old_i_flags
;
397 if (!inode_owner_or_capable(inode
))
400 if (btrfs_root_readonly(root
))
403 memset(&fa
, 0, sizeof(fa
));
404 if (copy_from_user(&fa
, arg
, sizeof(fa
)))
407 ret
= check_xflags(fa
.fsx_xflags
);
411 if (fa
.fsx_extsize
!= 0 || fa
.fsx_projid
!= 0 || fa
.fsx_cowextsize
!= 0)
414 ret
= mnt_want_write_file(file
);
420 old_flags
= binode
->flags
;
421 old_i_flags
= inode
->i_flags
;
423 /* We need the capabilities to change append-only or immutable inode */
424 if (((old_flags
& (BTRFS_INODE_APPEND
| BTRFS_INODE_IMMUTABLE
)) ||
425 (fa
.fsx_xflags
& (FS_XFLAG_APPEND
| FS_XFLAG_IMMUTABLE
))) &&
426 !capable(CAP_LINUX_IMMUTABLE
)) {
431 if (fa
.fsx_xflags
& FS_XFLAG_SYNC
)
432 binode
->flags
|= BTRFS_INODE_SYNC
;
434 binode
->flags
&= ~BTRFS_INODE_SYNC
;
435 if (fa
.fsx_xflags
& FS_XFLAG_IMMUTABLE
)
436 binode
->flags
|= BTRFS_INODE_IMMUTABLE
;
438 binode
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
439 if (fa
.fsx_xflags
& FS_XFLAG_APPEND
)
440 binode
->flags
|= BTRFS_INODE_APPEND
;
442 binode
->flags
&= ~BTRFS_INODE_APPEND
;
443 if (fa
.fsx_xflags
& FS_XFLAG_NODUMP
)
444 binode
->flags
|= BTRFS_INODE_NODUMP
;
446 binode
->flags
&= ~BTRFS_INODE_NODUMP
;
447 if (fa
.fsx_xflags
& FS_XFLAG_NOATIME
)
448 binode
->flags
|= BTRFS_INODE_NOATIME
;
450 binode
->flags
&= ~BTRFS_INODE_NOATIME
;
452 /* 1 item for the inode */
453 trans
= btrfs_start_transaction(root
, 1);
455 ret
= PTR_ERR(trans
);
459 btrfs_sync_inode_flags_to_i_flags(inode
);
460 inode_inc_iversion(inode
);
461 inode
->i_ctime
= current_time(inode
);
462 ret
= btrfs_update_inode(trans
, root
, inode
);
464 btrfs_end_transaction(trans
);
468 binode
->flags
= old_flags
;
469 inode
->i_flags
= old_i_flags
;
473 mnt_drop_write_file(file
);
478 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
480 struct inode
*inode
= file_inode(file
);
482 return put_user(inode
->i_generation
, arg
);
485 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
487 struct inode
*inode
= file_inode(file
);
488 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
489 struct btrfs_device
*device
;
490 struct request_queue
*q
;
491 struct fstrim_range range
;
492 u64 minlen
= ULLONG_MAX
;
496 if (!capable(CAP_SYS_ADMIN
))
500 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
504 q
= bdev_get_queue(device
->bdev
);
505 if (blk_queue_discard(q
)) {
507 minlen
= min_t(u64
, q
->limits
.discard_granularity
,
515 if (copy_from_user(&range
, arg
, sizeof(range
)))
519 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
520 * block group is in the logical address space, which can be any
521 * sectorsize aligned bytenr in the range [0, U64_MAX].
523 if (range
.len
< fs_info
->sb
->s_blocksize
)
526 range
.minlen
= max(range
.minlen
, minlen
);
527 ret
= btrfs_trim_fs(fs_info
, &range
);
531 if (copy_to_user(arg
, &range
, sizeof(range
)))
537 int btrfs_is_empty_uuid(u8
*uuid
)
541 for (i
= 0; i
< BTRFS_UUID_SIZE
; i
++) {
548 static noinline
int create_subvol(struct inode
*dir
,
549 struct dentry
*dentry
,
550 const char *name
, int namelen
,
552 struct btrfs_qgroup_inherit
*inherit
)
554 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
555 struct btrfs_trans_handle
*trans
;
556 struct btrfs_key key
;
557 struct btrfs_root_item
*root_item
;
558 struct btrfs_inode_item
*inode_item
;
559 struct extent_buffer
*leaf
;
560 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
561 struct btrfs_root
*new_root
;
562 struct btrfs_block_rsv block_rsv
;
563 struct timespec64 cur_time
= current_time(dir
);
568 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
572 root_item
= kzalloc(sizeof(*root_item
), GFP_KERNEL
);
576 ret
= btrfs_find_free_objectid(fs_info
->tree_root
, &objectid
);
581 * Don't create subvolume whose level is not zero. Or qgroup will be
582 * screwed up since it assumes subvolume qgroup's level to be 0.
584 if (btrfs_qgroup_level(objectid
)) {
589 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
591 * The same as the snapshot creation, please see the comment
592 * of create_snapshot().
594 ret
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
, 8, false);
598 trans
= btrfs_start_transaction(root
, 0);
600 ret
= PTR_ERR(trans
);
601 btrfs_subvolume_release_metadata(fs_info
, &block_rsv
);
604 trans
->block_rsv
= &block_rsv
;
605 trans
->bytes_reserved
= block_rsv
.size
;
607 ret
= btrfs_qgroup_inherit(trans
, 0, objectid
, inherit
);
611 leaf
= btrfs_alloc_tree_block(trans
, root
, 0, objectid
, NULL
, 0, 0, 0);
617 btrfs_mark_buffer_dirty(leaf
);
619 inode_item
= &root_item
->inode
;
620 btrfs_set_stack_inode_generation(inode_item
, 1);
621 btrfs_set_stack_inode_size(inode_item
, 3);
622 btrfs_set_stack_inode_nlink(inode_item
, 1);
623 btrfs_set_stack_inode_nbytes(inode_item
,
625 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
627 btrfs_set_root_flags(root_item
, 0);
628 btrfs_set_root_limit(root_item
, 0);
629 btrfs_set_stack_inode_flags(inode_item
, BTRFS_INODE_ROOT_ITEM_INIT
);
631 btrfs_set_root_bytenr(root_item
, leaf
->start
);
632 btrfs_set_root_generation(root_item
, trans
->transid
);
633 btrfs_set_root_level(root_item
, 0);
634 btrfs_set_root_refs(root_item
, 1);
635 btrfs_set_root_used(root_item
, leaf
->len
);
636 btrfs_set_root_last_snapshot(root_item
, 0);
638 btrfs_set_root_generation_v2(root_item
,
639 btrfs_root_generation(root_item
));
640 uuid_le_gen(&new_uuid
);
641 memcpy(root_item
->uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
642 btrfs_set_stack_timespec_sec(&root_item
->otime
, cur_time
.tv_sec
);
643 btrfs_set_stack_timespec_nsec(&root_item
->otime
, cur_time
.tv_nsec
);
644 root_item
->ctime
= root_item
->otime
;
645 btrfs_set_root_ctransid(root_item
, trans
->transid
);
646 btrfs_set_root_otransid(root_item
, trans
->transid
);
648 btrfs_tree_unlock(leaf
);
649 free_extent_buffer(leaf
);
652 btrfs_set_root_dirid(root_item
, new_dirid
);
654 key
.objectid
= objectid
;
656 key
.type
= BTRFS_ROOT_ITEM_KEY
;
657 ret
= btrfs_insert_root(trans
, fs_info
->tree_root
, &key
,
662 key
.offset
= (u64
)-1;
663 new_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
664 if (IS_ERR(new_root
)) {
665 ret
= PTR_ERR(new_root
);
666 btrfs_abort_transaction(trans
, ret
);
670 btrfs_record_root_in_trans(trans
, new_root
);
672 ret
= btrfs_create_subvol_root(trans
, new_root
, root
, new_dirid
);
674 /* We potentially lose an unused inode item here */
675 btrfs_abort_transaction(trans
, ret
);
679 mutex_lock(&new_root
->objectid_mutex
);
680 new_root
->highest_objectid
= new_dirid
;
681 mutex_unlock(&new_root
->objectid_mutex
);
684 * insert the directory item
686 ret
= btrfs_set_inode_index(BTRFS_I(dir
), &index
);
688 btrfs_abort_transaction(trans
, ret
);
692 ret
= btrfs_insert_dir_item(trans
, name
, namelen
, BTRFS_I(dir
), &key
,
693 BTRFS_FT_DIR
, index
);
695 btrfs_abort_transaction(trans
, ret
);
699 btrfs_i_size_write(BTRFS_I(dir
), dir
->i_size
+ namelen
* 2);
700 ret
= btrfs_update_inode(trans
, root
, dir
);
703 ret
= btrfs_add_root_ref(trans
, objectid
, root
->root_key
.objectid
,
704 btrfs_ino(BTRFS_I(dir
)), index
, name
, namelen
);
707 ret
= btrfs_uuid_tree_add(trans
, root_item
->uuid
,
708 BTRFS_UUID_KEY_SUBVOL
, objectid
);
710 btrfs_abort_transaction(trans
, ret
);
714 trans
->block_rsv
= NULL
;
715 trans
->bytes_reserved
= 0;
716 btrfs_subvolume_release_metadata(fs_info
, &block_rsv
);
719 *async_transid
= trans
->transid
;
720 err
= btrfs_commit_transaction_async(trans
, 1);
722 err
= btrfs_commit_transaction(trans
);
724 err
= btrfs_commit_transaction(trans
);
730 inode
= btrfs_lookup_dentry(dir
, dentry
);
732 return PTR_ERR(inode
);
733 d_instantiate(dentry
, inode
);
742 static int create_snapshot(struct btrfs_root
*root
, struct inode
*dir
,
743 struct dentry
*dentry
,
744 u64
*async_transid
, bool readonly
,
745 struct btrfs_qgroup_inherit
*inherit
)
747 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
749 struct btrfs_pending_snapshot
*pending_snapshot
;
750 struct btrfs_trans_handle
*trans
;
752 bool snapshot_force_cow
= false;
754 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
757 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_KERNEL
);
758 if (!pending_snapshot
)
761 pending_snapshot
->root_item
= kzalloc(sizeof(struct btrfs_root_item
),
763 pending_snapshot
->path
= btrfs_alloc_path();
764 if (!pending_snapshot
->root_item
|| !pending_snapshot
->path
) {
770 * Force new buffered writes to reserve space even when NOCOW is
771 * possible. This is to avoid later writeback (running dealloc) to
772 * fallback to COW mode and unexpectedly fail with ENOSPC.
774 atomic_inc(&root
->will_be_snapshotted
);
775 smp_mb__after_atomic();
776 /* wait for no snapshot writes */
777 wait_event(root
->subv_writers
->wait
,
778 percpu_counter_sum(&root
->subv_writers
->counter
) == 0);
780 ret
= btrfs_start_delalloc_inodes(root
);
785 * All previous writes have started writeback in NOCOW mode, so now
786 * we force future writes to fallback to COW mode during snapshot
789 atomic_inc(&root
->snapshot_force_cow
);
790 snapshot_force_cow
= true;
792 btrfs_wait_ordered_extents(root
, U64_MAX
, 0, (u64
)-1);
794 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
795 BTRFS_BLOCK_RSV_TEMP
);
797 * 1 - parent dir inode
800 * 2 - root ref/backref
801 * 1 - root of snapshot
804 ret
= btrfs_subvolume_reserve_metadata(BTRFS_I(dir
)->root
,
805 &pending_snapshot
->block_rsv
, 8,
810 pending_snapshot
->dentry
= dentry
;
811 pending_snapshot
->root
= root
;
812 pending_snapshot
->readonly
= readonly
;
813 pending_snapshot
->dir
= dir
;
814 pending_snapshot
->inherit
= inherit
;
816 trans
= btrfs_start_transaction(root
, 0);
818 ret
= PTR_ERR(trans
);
822 spin_lock(&fs_info
->trans_lock
);
823 list_add(&pending_snapshot
->list
,
824 &trans
->transaction
->pending_snapshots
);
825 spin_unlock(&fs_info
->trans_lock
);
827 *async_transid
= trans
->transid
;
828 ret
= btrfs_commit_transaction_async(trans
, 1);
830 ret
= btrfs_commit_transaction(trans
);
832 ret
= btrfs_commit_transaction(trans
);
837 ret
= pending_snapshot
->error
;
841 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
845 inode
= btrfs_lookup_dentry(d_inode(dentry
->d_parent
), dentry
);
847 ret
= PTR_ERR(inode
);
851 d_instantiate(dentry
, inode
);
854 btrfs_subvolume_release_metadata(fs_info
, &pending_snapshot
->block_rsv
);
856 if (snapshot_force_cow
)
857 atomic_dec(&root
->snapshot_force_cow
);
858 if (atomic_dec_and_test(&root
->will_be_snapshotted
))
859 wake_up_var(&root
->will_be_snapshotted
);
861 kfree(pending_snapshot
->root_item
);
862 btrfs_free_path(pending_snapshot
->path
);
863 kfree(pending_snapshot
);
868 /* copy of may_delete in fs/namei.c()
869 * Check whether we can remove a link victim from directory dir, check
870 * whether the type of victim is right.
871 * 1. We can't do it if dir is read-only (done in permission())
872 * 2. We should have write and exec permissions on dir
873 * 3. We can't remove anything from append-only dir
874 * 4. We can't do anything with immutable dir (done in permission())
875 * 5. If the sticky bit on dir is set we should either
876 * a. be owner of dir, or
877 * b. be owner of victim, or
878 * c. have CAP_FOWNER capability
879 * 6. If the victim is append-only or immutable we can't do anything with
880 * links pointing to it.
881 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
882 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
883 * 9. We can't remove a root or mountpoint.
884 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
885 * nfs_async_unlink().
888 static int btrfs_may_delete(struct inode
*dir
, struct dentry
*victim
, int isdir
)
892 if (d_really_is_negative(victim
))
895 BUG_ON(d_inode(victim
->d_parent
) != dir
);
896 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
898 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
903 if (check_sticky(dir
, d_inode(victim
)) || IS_APPEND(d_inode(victim
)) ||
904 IS_IMMUTABLE(d_inode(victim
)) || IS_SWAPFILE(d_inode(victim
)))
907 if (!d_is_dir(victim
))
911 } else if (d_is_dir(victim
))
915 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
920 /* copy of may_create in fs/namei.c() */
921 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
923 if (d_really_is_positive(child
))
927 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
931 * Create a new subvolume below @parent. This is largely modeled after
932 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
933 * inside this filesystem so it's quite a bit simpler.
935 static noinline
int btrfs_mksubvol(const struct path
*parent
,
936 const char *name
, int namelen
,
937 struct btrfs_root
*snap_src
,
938 u64
*async_transid
, bool readonly
,
939 struct btrfs_qgroup_inherit
*inherit
)
941 struct inode
*dir
= d_inode(parent
->dentry
);
942 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
943 struct dentry
*dentry
;
946 error
= down_write_killable_nested(&dir
->i_rwsem
, I_MUTEX_PARENT
);
950 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
951 error
= PTR_ERR(dentry
);
955 error
= btrfs_may_create(dir
, dentry
);
960 * even if this name doesn't exist, we may get hash collisions.
961 * check for them now when we can safely fail
963 error
= btrfs_check_dir_item_collision(BTRFS_I(dir
)->root
,
969 down_read(&fs_info
->subvol_sem
);
971 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
975 error
= create_snapshot(snap_src
, dir
, dentry
,
976 async_transid
, readonly
, inherit
);
978 error
= create_subvol(dir
, dentry
, name
, namelen
,
979 async_transid
, inherit
);
982 fsnotify_mkdir(dir
, dentry
);
984 up_read(&fs_info
->subvol_sem
);
993 * When we're defragging a range, we don't want to kick it off again
994 * if it is really just waiting for delalloc to send it down.
995 * If we find a nice big extent or delalloc range for the bytes in the
996 * file you want to defrag, we return 0 to let you know to skip this
999 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, u32 thresh
)
1001 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1002 struct extent_map
*em
= NULL
;
1003 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1006 read_lock(&em_tree
->lock
);
1007 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_SIZE
);
1008 read_unlock(&em_tree
->lock
);
1011 end
= extent_map_end(em
);
1012 free_extent_map(em
);
1013 if (end
- offset
> thresh
)
1016 /* if we already have a nice delalloc here, just stop */
1018 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
1019 thresh
, EXTENT_DELALLOC
, 1);
1026 * helper function to walk through a file and find extents
1027 * newer than a specific transid, and smaller than thresh.
1029 * This is used by the defragging code to find new and small
1032 static int find_new_extents(struct btrfs_root
*root
,
1033 struct inode
*inode
, u64 newer_than
,
1034 u64
*off
, u32 thresh
)
1036 struct btrfs_path
*path
;
1037 struct btrfs_key min_key
;
1038 struct extent_buffer
*leaf
;
1039 struct btrfs_file_extent_item
*extent
;
1042 u64 ino
= btrfs_ino(BTRFS_I(inode
));
1044 path
= btrfs_alloc_path();
1048 min_key
.objectid
= ino
;
1049 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
1050 min_key
.offset
= *off
;
1053 ret
= btrfs_search_forward(root
, &min_key
, path
, newer_than
);
1057 if (min_key
.objectid
!= ino
)
1059 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1062 leaf
= path
->nodes
[0];
1063 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1064 struct btrfs_file_extent_item
);
1066 type
= btrfs_file_extent_type(leaf
, extent
);
1067 if (type
== BTRFS_FILE_EXTENT_REG
&&
1068 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
1069 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
1070 *off
= min_key
.offset
;
1071 btrfs_free_path(path
);
1076 if (path
->slots
[0] < btrfs_header_nritems(leaf
)) {
1077 btrfs_item_key_to_cpu(leaf
, &min_key
, path
->slots
[0]);
1081 if (min_key
.offset
== (u64
)-1)
1085 btrfs_release_path(path
);
1088 btrfs_free_path(path
);
1092 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
1094 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1095 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1096 struct extent_map
*em
;
1097 u64 len
= PAGE_SIZE
;
1100 * hopefully we have this extent in the tree already, try without
1101 * the full extent lock
1103 read_lock(&em_tree
->lock
);
1104 em
= lookup_extent_mapping(em_tree
, start
, len
);
1105 read_unlock(&em_tree
->lock
);
1108 struct extent_state
*cached
= NULL
;
1109 u64 end
= start
+ len
- 1;
1111 /* get the big lock and read metadata off disk */
1112 lock_extent_bits(io_tree
, start
, end
, &cached
);
1113 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, start
, len
, 0);
1114 unlock_extent_cached(io_tree
, start
, end
, &cached
);
1123 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
1125 struct extent_map
*next
;
1128 /* this is the last extent */
1129 if (em
->start
+ em
->len
>= i_size_read(inode
))
1132 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
1133 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
1135 else if ((em
->block_start
+ em
->block_len
== next
->block_start
) &&
1136 (em
->block_len
> SZ_128K
&& next
->block_len
> SZ_128K
))
1139 free_extent_map(next
);
1143 static int should_defrag_range(struct inode
*inode
, u64 start
, u32 thresh
,
1144 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
1147 struct extent_map
*em
;
1149 bool next_mergeable
= true;
1150 bool prev_mergeable
= true;
1153 * make sure that once we start defragging an extent, we keep on
1156 if (start
< *defrag_end
)
1161 em
= defrag_lookup_extent(inode
, start
);
1165 /* this will cover holes, and inline extents */
1166 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
1172 prev_mergeable
= false;
1174 next_mergeable
= defrag_check_next_extent(inode
, em
);
1176 * we hit a real extent, if it is big or the next extent is not a
1177 * real extent, don't bother defragging it
1179 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
1180 (em
->len
>= thresh
|| (!next_mergeable
&& !prev_mergeable
)))
1184 * last_len ends up being a counter of how many bytes we've defragged.
1185 * every time we choose not to defrag an extent, we reset *last_len
1186 * so that the next tiny extent will force a defrag.
1188 * The end result of this is that tiny extents before a single big
1189 * extent will force at least part of that big extent to be defragged.
1192 *defrag_end
= extent_map_end(em
);
1195 *skip
= extent_map_end(em
);
1199 free_extent_map(em
);
1204 * it doesn't do much good to defrag one or two pages
1205 * at a time. This pulls in a nice chunk of pages
1206 * to COW and defrag.
1208 * It also makes sure the delalloc code has enough
1209 * dirty data to avoid making new small extents as part
1212 * It's a good idea to start RA on this range
1213 * before calling this.
1215 static int cluster_pages_for_defrag(struct inode
*inode
,
1216 struct page
**pages
,
1217 unsigned long start_index
,
1218 unsigned long num_pages
)
1220 unsigned long file_end
;
1221 u64 isize
= i_size_read(inode
);
1228 struct btrfs_ordered_extent
*ordered
;
1229 struct extent_state
*cached_state
= NULL
;
1230 struct extent_io_tree
*tree
;
1231 struct extent_changeset
*data_reserved
= NULL
;
1232 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
1234 file_end
= (isize
- 1) >> PAGE_SHIFT
;
1235 if (!isize
|| start_index
> file_end
)
1238 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
1240 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
,
1241 start_index
<< PAGE_SHIFT
,
1242 page_cnt
<< PAGE_SHIFT
);
1246 tree
= &BTRFS_I(inode
)->io_tree
;
1248 /* step one, lock all the pages */
1249 for (i
= 0; i
< page_cnt
; i
++) {
1252 page
= find_or_create_page(inode
->i_mapping
,
1253 start_index
+ i
, mask
);
1257 page_start
= page_offset(page
);
1258 page_end
= page_start
+ PAGE_SIZE
- 1;
1260 lock_extent_bits(tree
, page_start
, page_end
,
1262 ordered
= btrfs_lookup_ordered_extent(inode
,
1264 unlock_extent_cached(tree
, page_start
, page_end
,
1270 btrfs_start_ordered_extent(inode
, ordered
, 1);
1271 btrfs_put_ordered_extent(ordered
);
1274 * we unlocked the page above, so we need check if
1275 * it was released or not.
1277 if (page
->mapping
!= inode
->i_mapping
) {
1284 if (!PageUptodate(page
)) {
1285 btrfs_readpage(NULL
, page
);
1287 if (!PageUptodate(page
)) {
1295 if (page
->mapping
!= inode
->i_mapping
) {
1307 if (!(inode
->i_sb
->s_flags
& SB_ACTIVE
))
1311 * so now we have a nice long stream of locked
1312 * and up to date pages, lets wait on them
1314 for (i
= 0; i
< i_done
; i
++)
1315 wait_on_page_writeback(pages
[i
]);
1317 page_start
= page_offset(pages
[0]);
1318 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_SIZE
;
1320 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1321 page_start
, page_end
- 1, &cached_state
);
1322 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1323 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1324 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 0, 0,
1327 if (i_done
!= page_cnt
) {
1328 spin_lock(&BTRFS_I(inode
)->lock
);
1329 btrfs_mod_outstanding_extents(BTRFS_I(inode
), 1);
1330 spin_unlock(&BTRFS_I(inode
)->lock
);
1331 btrfs_delalloc_release_space(inode
, data_reserved
,
1332 start_index
<< PAGE_SHIFT
,
1333 (page_cnt
- i_done
) << PAGE_SHIFT
, true);
1337 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1340 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1341 page_start
, page_end
- 1, &cached_state
);
1343 for (i
= 0; i
< i_done
; i
++) {
1344 clear_page_dirty_for_io(pages
[i
]);
1345 ClearPageChecked(pages
[i
]);
1346 set_page_extent_mapped(pages
[i
]);
1347 set_page_dirty(pages
[i
]);
1348 unlock_page(pages
[i
]);
1351 btrfs_delalloc_release_extents(BTRFS_I(inode
), page_cnt
<< PAGE_SHIFT
,
1353 extent_changeset_free(data_reserved
);
1356 for (i
= 0; i
< i_done
; i
++) {
1357 unlock_page(pages
[i
]);
1360 btrfs_delalloc_release_space(inode
, data_reserved
,
1361 start_index
<< PAGE_SHIFT
,
1362 page_cnt
<< PAGE_SHIFT
, true);
1363 btrfs_delalloc_release_extents(BTRFS_I(inode
), page_cnt
<< PAGE_SHIFT
,
1365 extent_changeset_free(data_reserved
);
1370 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1371 struct btrfs_ioctl_defrag_range_args
*range
,
1372 u64 newer_than
, unsigned long max_to_defrag
)
1374 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1375 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1376 struct file_ra_state
*ra
= NULL
;
1377 unsigned long last_index
;
1378 u64 isize
= i_size_read(inode
);
1382 u64 newer_off
= range
->start
;
1384 unsigned long ra_index
= 0;
1386 int defrag_count
= 0;
1387 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1388 u32 extent_thresh
= range
->extent_thresh
;
1389 unsigned long max_cluster
= SZ_256K
>> PAGE_SHIFT
;
1390 unsigned long cluster
= max_cluster
;
1391 u64 new_align
= ~((u64
)SZ_128K
- 1);
1392 struct page
**pages
= NULL
;
1393 bool do_compress
= range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
;
1398 if (range
->start
>= isize
)
1402 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1404 if (range
->compress_type
)
1405 compress_type
= range
->compress_type
;
1408 if (extent_thresh
== 0)
1409 extent_thresh
= SZ_256K
;
1412 * If we were not given a file, allocate a readahead context. As
1413 * readahead is just an optimization, defrag will work without it so
1414 * we don't error out.
1417 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
1419 file_ra_state_init(ra
, inode
->i_mapping
);
1424 pages
= kmalloc_array(max_cluster
, sizeof(struct page
*), GFP_KERNEL
);
1430 /* find the last page to defrag */
1431 if (range
->start
+ range
->len
> range
->start
) {
1432 last_index
= min_t(u64
, isize
- 1,
1433 range
->start
+ range
->len
- 1) >> PAGE_SHIFT
;
1435 last_index
= (isize
- 1) >> PAGE_SHIFT
;
1439 ret
= find_new_extents(root
, inode
, newer_than
,
1440 &newer_off
, SZ_64K
);
1442 range
->start
= newer_off
;
1444 * we always align our defrag to help keep
1445 * the extents in the file evenly spaced
1447 i
= (newer_off
& new_align
) >> PAGE_SHIFT
;
1451 i
= range
->start
>> PAGE_SHIFT
;
1454 max_to_defrag
= last_index
- i
+ 1;
1457 * make writeback starts from i, so the defrag range can be
1458 * written sequentially.
1460 if (i
< inode
->i_mapping
->writeback_index
)
1461 inode
->i_mapping
->writeback_index
= i
;
1463 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1464 (i
< DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
))) {
1466 * make sure we stop running if someone unmounts
1469 if (!(inode
->i_sb
->s_flags
& SB_ACTIVE
))
1472 if (btrfs_defrag_cancelled(fs_info
)) {
1473 btrfs_debug(fs_info
, "defrag_file cancelled");
1478 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_SHIFT
,
1479 extent_thresh
, &last_len
, &skip
,
1480 &defrag_end
, do_compress
)){
1483 * the should_defrag function tells us how much to skip
1484 * bump our counter by the suggested amount
1486 next
= DIV_ROUND_UP(skip
, PAGE_SIZE
);
1487 i
= max(i
+ 1, next
);
1492 cluster
= (PAGE_ALIGN(defrag_end
) >>
1494 cluster
= min(cluster
, max_cluster
);
1496 cluster
= max_cluster
;
1499 if (i
+ cluster
> ra_index
) {
1500 ra_index
= max(i
, ra_index
);
1502 page_cache_sync_readahead(inode
->i_mapping
, ra
,
1503 file
, ra_index
, cluster
);
1504 ra_index
+= cluster
;
1509 BTRFS_I(inode
)->defrag_compress
= compress_type
;
1510 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1512 inode_unlock(inode
);
1516 defrag_count
+= ret
;
1517 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1518 inode_unlock(inode
);
1521 if (newer_off
== (u64
)-1)
1527 newer_off
= max(newer_off
+ 1,
1528 (u64
)i
<< PAGE_SHIFT
);
1530 ret
= find_new_extents(root
, inode
, newer_than
,
1531 &newer_off
, SZ_64K
);
1533 range
->start
= newer_off
;
1534 i
= (newer_off
& new_align
) >> PAGE_SHIFT
;
1541 last_len
+= ret
<< PAGE_SHIFT
;
1549 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
)) {
1550 filemap_flush(inode
->i_mapping
);
1551 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1552 &BTRFS_I(inode
)->runtime_flags
))
1553 filemap_flush(inode
->i_mapping
);
1556 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1557 btrfs_set_fs_incompat(fs_info
, COMPRESS_LZO
);
1558 } else if (range
->compress_type
== BTRFS_COMPRESS_ZSTD
) {
1559 btrfs_set_fs_incompat(fs_info
, COMPRESS_ZSTD
);
1567 BTRFS_I(inode
)->defrag_compress
= BTRFS_COMPRESS_NONE
;
1568 inode_unlock(inode
);
1576 static noinline
int btrfs_ioctl_resize(struct file
*file
,
1579 struct inode
*inode
= file_inode(file
);
1580 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1584 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1585 struct btrfs_ioctl_vol_args
*vol_args
;
1586 struct btrfs_trans_handle
*trans
;
1587 struct btrfs_device
*device
= NULL
;
1590 char *devstr
= NULL
;
1594 if (!capable(CAP_SYS_ADMIN
))
1597 ret
= mnt_want_write_file(file
);
1601 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
1602 mnt_drop_write_file(file
);
1603 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
1606 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1607 if (IS_ERR(vol_args
)) {
1608 ret
= PTR_ERR(vol_args
);
1612 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1614 sizestr
= vol_args
->name
;
1615 devstr
= strchr(sizestr
, ':');
1617 sizestr
= devstr
+ 1;
1619 devstr
= vol_args
->name
;
1620 ret
= kstrtoull(devstr
, 10, &devid
);
1627 btrfs_info(fs_info
, "resizing devid %llu", devid
);
1630 device
= btrfs_find_device(fs_info
, devid
, NULL
, NULL
);
1632 btrfs_info(fs_info
, "resizer unable to find device %llu",
1638 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1640 "resizer unable to apply on readonly device %llu",
1646 if (!strcmp(sizestr
, "max"))
1647 new_size
= device
->bdev
->bd_inode
->i_size
;
1649 if (sizestr
[0] == '-') {
1652 } else if (sizestr
[0] == '+') {
1656 new_size
= memparse(sizestr
, &retptr
);
1657 if (*retptr
!= '\0' || new_size
== 0) {
1663 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1668 old_size
= btrfs_device_get_total_bytes(device
);
1671 if (new_size
> old_size
) {
1675 new_size
= old_size
- new_size
;
1676 } else if (mod
> 0) {
1677 if (new_size
> ULLONG_MAX
- old_size
) {
1681 new_size
= old_size
+ new_size
;
1684 if (new_size
< SZ_256M
) {
1688 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1693 new_size
= round_down(new_size
, fs_info
->sectorsize
);
1695 btrfs_info_in_rcu(fs_info
, "new size for %s is %llu",
1696 rcu_str_deref(device
->name
), new_size
);
1698 if (new_size
> old_size
) {
1699 trans
= btrfs_start_transaction(root
, 0);
1700 if (IS_ERR(trans
)) {
1701 ret
= PTR_ERR(trans
);
1704 ret
= btrfs_grow_device(trans
, device
, new_size
);
1705 btrfs_commit_transaction(trans
);
1706 } else if (new_size
< old_size
) {
1707 ret
= btrfs_shrink_device(device
, new_size
);
1708 } /* equal, nothing need to do */
1713 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
1714 mnt_drop_write_file(file
);
1718 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1719 const char *name
, unsigned long fd
, int subvol
,
1720 u64
*transid
, bool readonly
,
1721 struct btrfs_qgroup_inherit
*inherit
)
1726 if (!S_ISDIR(file_inode(file
)->i_mode
))
1729 ret
= mnt_want_write_file(file
);
1733 namelen
= strlen(name
);
1734 if (strchr(name
, '/')) {
1736 goto out_drop_write
;
1739 if (name
[0] == '.' &&
1740 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1742 goto out_drop_write
;
1746 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1747 NULL
, transid
, readonly
, inherit
);
1749 struct fd src
= fdget(fd
);
1750 struct inode
*src_inode
;
1753 goto out_drop_write
;
1756 src_inode
= file_inode(src
.file
);
1757 if (src_inode
->i_sb
!= file_inode(file
)->i_sb
) {
1758 btrfs_info(BTRFS_I(file_inode(file
))->root
->fs_info
,
1759 "Snapshot src from another FS");
1761 } else if (!inode_owner_or_capable(src_inode
)) {
1763 * Subvolume creation is not restricted, but snapshots
1764 * are limited to own subvolumes only
1768 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1769 BTRFS_I(src_inode
)->root
,
1770 transid
, readonly
, inherit
);
1775 mnt_drop_write_file(file
);
1780 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1781 void __user
*arg
, int subvol
)
1783 struct btrfs_ioctl_vol_args
*vol_args
;
1786 if (!S_ISDIR(file_inode(file
)->i_mode
))
1789 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1790 if (IS_ERR(vol_args
))
1791 return PTR_ERR(vol_args
);
1792 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1794 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1795 vol_args
->fd
, subvol
,
1802 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1803 void __user
*arg
, int subvol
)
1805 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1809 bool readonly
= false;
1810 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1812 if (!S_ISDIR(file_inode(file
)->i_mode
))
1815 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1816 if (IS_ERR(vol_args
))
1817 return PTR_ERR(vol_args
);
1818 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1820 if (vol_args
->flags
&
1821 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1822 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1827 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1829 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1831 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1832 if (vol_args
->size
> PAGE_SIZE
) {
1836 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1837 if (IS_ERR(inherit
)) {
1838 ret
= PTR_ERR(inherit
);
1843 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1844 vol_args
->fd
, subvol
, ptr
,
1849 if (ptr
&& copy_to_user(arg
+
1850 offsetof(struct btrfs_ioctl_vol_args_v2
,
1862 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1865 struct inode
*inode
= file_inode(file
);
1866 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1867 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1871 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
)
1874 down_read(&fs_info
->subvol_sem
);
1875 if (btrfs_root_readonly(root
))
1876 flags
|= BTRFS_SUBVOL_RDONLY
;
1877 up_read(&fs_info
->subvol_sem
);
1879 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1885 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1888 struct inode
*inode
= file_inode(file
);
1889 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1890 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1891 struct btrfs_trans_handle
*trans
;
1896 if (!inode_owner_or_capable(inode
))
1899 ret
= mnt_want_write_file(file
);
1903 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
1905 goto out_drop_write
;
1908 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1910 goto out_drop_write
;
1913 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1915 goto out_drop_write
;
1918 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1920 goto out_drop_write
;
1923 down_write(&fs_info
->subvol_sem
);
1926 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1929 root_flags
= btrfs_root_flags(&root
->root_item
);
1930 if (flags
& BTRFS_SUBVOL_RDONLY
) {
1931 btrfs_set_root_flags(&root
->root_item
,
1932 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1935 * Block RO -> RW transition if this subvolume is involved in
1938 spin_lock(&root
->root_item_lock
);
1939 if (root
->send_in_progress
== 0) {
1940 btrfs_set_root_flags(&root
->root_item
,
1941 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1942 spin_unlock(&root
->root_item_lock
);
1944 spin_unlock(&root
->root_item_lock
);
1946 "Attempt to set subvolume %llu read-write during send",
1947 root
->root_key
.objectid
);
1953 trans
= btrfs_start_transaction(root
, 1);
1954 if (IS_ERR(trans
)) {
1955 ret
= PTR_ERR(trans
);
1959 ret
= btrfs_update_root(trans
, fs_info
->tree_root
,
1960 &root
->root_key
, &root
->root_item
);
1962 btrfs_end_transaction(trans
);
1966 ret
= btrfs_commit_transaction(trans
);
1970 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1972 up_write(&fs_info
->subvol_sem
);
1974 mnt_drop_write_file(file
);
1979 static noinline
int key_in_sk(struct btrfs_key
*key
,
1980 struct btrfs_ioctl_search_key
*sk
)
1982 struct btrfs_key test
;
1985 test
.objectid
= sk
->min_objectid
;
1986 test
.type
= sk
->min_type
;
1987 test
.offset
= sk
->min_offset
;
1989 ret
= btrfs_comp_cpu_keys(key
, &test
);
1993 test
.objectid
= sk
->max_objectid
;
1994 test
.type
= sk
->max_type
;
1995 test
.offset
= sk
->max_offset
;
1997 ret
= btrfs_comp_cpu_keys(key
, &test
);
2003 static noinline
int copy_to_sk(struct btrfs_path
*path
,
2004 struct btrfs_key
*key
,
2005 struct btrfs_ioctl_search_key
*sk
,
2008 unsigned long *sk_offset
,
2012 struct extent_buffer
*leaf
;
2013 struct btrfs_ioctl_search_header sh
;
2014 struct btrfs_key test
;
2015 unsigned long item_off
;
2016 unsigned long item_len
;
2022 leaf
= path
->nodes
[0];
2023 slot
= path
->slots
[0];
2024 nritems
= btrfs_header_nritems(leaf
);
2026 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
2030 found_transid
= btrfs_header_generation(leaf
);
2032 for (i
= slot
; i
< nritems
; i
++) {
2033 item_off
= btrfs_item_ptr_offset(leaf
, i
);
2034 item_len
= btrfs_item_size_nr(leaf
, i
);
2036 btrfs_item_key_to_cpu(leaf
, key
, i
);
2037 if (!key_in_sk(key
, sk
))
2040 if (sizeof(sh
) + item_len
> *buf_size
) {
2047 * return one empty item back for v1, which does not
2051 *buf_size
= sizeof(sh
) + item_len
;
2056 if (sizeof(sh
) + item_len
+ *sk_offset
> *buf_size
) {
2061 sh
.objectid
= key
->objectid
;
2062 sh
.offset
= key
->offset
;
2063 sh
.type
= key
->type
;
2065 sh
.transid
= found_transid
;
2067 /* copy search result header */
2068 if (copy_to_user(ubuf
+ *sk_offset
, &sh
, sizeof(sh
))) {
2073 *sk_offset
+= sizeof(sh
);
2076 char __user
*up
= ubuf
+ *sk_offset
;
2078 if (read_extent_buffer_to_user(leaf
, up
,
2079 item_off
, item_len
)) {
2084 *sk_offset
+= item_len
;
2088 if (ret
) /* -EOVERFLOW from above */
2091 if (*num_found
>= sk
->nr_items
) {
2098 test
.objectid
= sk
->max_objectid
;
2099 test
.type
= sk
->max_type
;
2100 test
.offset
= sk
->max_offset
;
2101 if (btrfs_comp_cpu_keys(key
, &test
) >= 0)
2103 else if (key
->offset
< (u64
)-1)
2105 else if (key
->type
< (u8
)-1) {
2108 } else if (key
->objectid
< (u64
)-1) {
2116 * 0: all items from this leaf copied, continue with next
2117 * 1: * more items can be copied, but unused buffer is too small
2118 * * all items were found
2119 * Either way, it will stops the loop which iterates to the next
2121 * -EOVERFLOW: item was to large for buffer
2122 * -EFAULT: could not copy extent buffer back to userspace
2127 static noinline
int search_ioctl(struct inode
*inode
,
2128 struct btrfs_ioctl_search_key
*sk
,
2132 struct btrfs_fs_info
*info
= btrfs_sb(inode
->i_sb
);
2133 struct btrfs_root
*root
;
2134 struct btrfs_key key
;
2135 struct btrfs_path
*path
;
2138 unsigned long sk_offset
= 0;
2140 if (*buf_size
< sizeof(struct btrfs_ioctl_search_header
)) {
2141 *buf_size
= sizeof(struct btrfs_ioctl_search_header
);
2145 path
= btrfs_alloc_path();
2149 if (sk
->tree_id
== 0) {
2150 /* search the root of the inode that was passed */
2151 root
= BTRFS_I(inode
)->root
;
2153 key
.objectid
= sk
->tree_id
;
2154 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2155 key
.offset
= (u64
)-1;
2156 root
= btrfs_read_fs_root_no_name(info
, &key
);
2158 btrfs_free_path(path
);
2159 return PTR_ERR(root
);
2163 key
.objectid
= sk
->min_objectid
;
2164 key
.type
= sk
->min_type
;
2165 key
.offset
= sk
->min_offset
;
2168 ret
= btrfs_search_forward(root
, &key
, path
, sk
->min_transid
);
2174 ret
= copy_to_sk(path
, &key
, sk
, buf_size
, ubuf
,
2175 &sk_offset
, &num_found
);
2176 btrfs_release_path(path
);
2184 sk
->nr_items
= num_found
;
2185 btrfs_free_path(path
);
2189 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
2192 struct btrfs_ioctl_search_args __user
*uargs
;
2193 struct btrfs_ioctl_search_key sk
;
2194 struct inode
*inode
;
2198 if (!capable(CAP_SYS_ADMIN
))
2201 uargs
= (struct btrfs_ioctl_search_args __user
*)argp
;
2203 if (copy_from_user(&sk
, &uargs
->key
, sizeof(sk
)))
2206 buf_size
= sizeof(uargs
->buf
);
2208 inode
= file_inode(file
);
2209 ret
= search_ioctl(inode
, &sk
, &buf_size
, uargs
->buf
);
2212 * In the origin implementation an overflow is handled by returning a
2213 * search header with a len of zero, so reset ret.
2215 if (ret
== -EOVERFLOW
)
2218 if (ret
== 0 && copy_to_user(&uargs
->key
, &sk
, sizeof(sk
)))
2223 static noinline
int btrfs_ioctl_tree_search_v2(struct file
*file
,
2226 struct btrfs_ioctl_search_args_v2 __user
*uarg
;
2227 struct btrfs_ioctl_search_args_v2 args
;
2228 struct inode
*inode
;
2231 const size_t buf_limit
= SZ_16M
;
2233 if (!capable(CAP_SYS_ADMIN
))
2236 /* copy search header and buffer size */
2237 uarg
= (struct btrfs_ioctl_search_args_v2 __user
*)argp
;
2238 if (copy_from_user(&args
, uarg
, sizeof(args
)))
2241 buf_size
= args
.buf_size
;
2243 /* limit result size to 16MB */
2244 if (buf_size
> buf_limit
)
2245 buf_size
= buf_limit
;
2247 inode
= file_inode(file
);
2248 ret
= search_ioctl(inode
, &args
.key
, &buf_size
,
2249 (char __user
*)(&uarg
->buf
[0]));
2250 if (ret
== 0 && copy_to_user(&uarg
->key
, &args
.key
, sizeof(args
.key
)))
2252 else if (ret
== -EOVERFLOW
&&
2253 copy_to_user(&uarg
->buf_size
, &buf_size
, sizeof(buf_size
)))
2260 * Search INODE_REFs to identify path name of 'dirid' directory
2261 * in a 'tree_id' tree. and sets path name to 'name'.
2263 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
2264 u64 tree_id
, u64 dirid
, char *name
)
2266 struct btrfs_root
*root
;
2267 struct btrfs_key key
;
2273 struct btrfs_inode_ref
*iref
;
2274 struct extent_buffer
*l
;
2275 struct btrfs_path
*path
;
2277 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
2282 path
= btrfs_alloc_path();
2286 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
- 1];
2288 key
.objectid
= tree_id
;
2289 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2290 key
.offset
= (u64
)-1;
2291 root
= btrfs_read_fs_root_no_name(info
, &key
);
2293 ret
= PTR_ERR(root
);
2297 key
.objectid
= dirid
;
2298 key
.type
= BTRFS_INODE_REF_KEY
;
2299 key
.offset
= (u64
)-1;
2302 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2306 ret
= btrfs_previous_item(root
, path
, dirid
,
2307 BTRFS_INODE_REF_KEY
);
2317 slot
= path
->slots
[0];
2318 btrfs_item_key_to_cpu(l
, &key
, slot
);
2320 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
2321 len
= btrfs_inode_ref_name_len(l
, iref
);
2323 total_len
+= len
+ 1;
2325 ret
= -ENAMETOOLONG
;
2330 read_extent_buffer(l
, ptr
, (unsigned long)(iref
+ 1), len
);
2332 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
2335 btrfs_release_path(path
);
2336 key
.objectid
= key
.offset
;
2337 key
.offset
= (u64
)-1;
2338 dirid
= key
.objectid
;
2340 memmove(name
, ptr
, total_len
);
2341 name
[total_len
] = '\0';
2344 btrfs_free_path(path
);
2348 static int btrfs_search_path_in_tree_user(struct inode
*inode
,
2349 struct btrfs_ioctl_ino_lookup_user_args
*args
)
2351 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2352 struct super_block
*sb
= inode
->i_sb
;
2353 struct btrfs_key upper_limit
= BTRFS_I(inode
)->location
;
2354 u64 treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2355 u64 dirid
= args
->dirid
;
2356 unsigned long item_off
;
2357 unsigned long item_len
;
2358 struct btrfs_inode_ref
*iref
;
2359 struct btrfs_root_ref
*rref
;
2360 struct btrfs_root
*root
;
2361 struct btrfs_path
*path
;
2362 struct btrfs_key key
, key2
;
2363 struct extent_buffer
*leaf
;
2364 struct inode
*temp_inode
;
2371 path
= btrfs_alloc_path();
2376 * If the bottom subvolume does not exist directly under upper_limit,
2377 * construct the path in from the bottom up.
2379 if (dirid
!= upper_limit
.objectid
) {
2380 ptr
= &args
->path
[BTRFS_INO_LOOKUP_USER_PATH_MAX
- 1];
2382 key
.objectid
= treeid
;
2383 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2384 key
.offset
= (u64
)-1;
2385 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2387 ret
= PTR_ERR(root
);
2391 key
.objectid
= dirid
;
2392 key
.type
= BTRFS_INODE_REF_KEY
;
2393 key
.offset
= (u64
)-1;
2395 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2398 } else if (ret
> 0) {
2399 ret
= btrfs_previous_item(root
, path
, dirid
,
2400 BTRFS_INODE_REF_KEY
);
2403 } else if (ret
> 0) {
2409 leaf
= path
->nodes
[0];
2410 slot
= path
->slots
[0];
2411 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2413 iref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_inode_ref
);
2414 len
= btrfs_inode_ref_name_len(leaf
, iref
);
2416 total_len
+= len
+ 1;
2417 if (ptr
< args
->path
) {
2418 ret
= -ENAMETOOLONG
;
2423 read_extent_buffer(leaf
, ptr
,
2424 (unsigned long)(iref
+ 1), len
);
2426 /* Check the read+exec permission of this directory */
2427 ret
= btrfs_previous_item(root
, path
, dirid
,
2428 BTRFS_INODE_ITEM_KEY
);
2431 } else if (ret
> 0) {
2436 leaf
= path
->nodes
[0];
2437 slot
= path
->slots
[0];
2438 btrfs_item_key_to_cpu(leaf
, &key2
, slot
);
2439 if (key2
.objectid
!= dirid
) {
2444 temp_inode
= btrfs_iget(sb
, &key2
, root
, NULL
);
2445 if (IS_ERR(temp_inode
)) {
2446 ret
= PTR_ERR(temp_inode
);
2449 ret
= inode_permission(temp_inode
, MAY_READ
| MAY_EXEC
);
2456 if (key
.offset
== upper_limit
.objectid
)
2458 if (key
.objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
2463 btrfs_release_path(path
);
2464 key
.objectid
= key
.offset
;
2465 key
.offset
= (u64
)-1;
2466 dirid
= key
.objectid
;
2469 memmove(args
->path
, ptr
, total_len
);
2470 args
->path
[total_len
] = '\0';
2471 btrfs_release_path(path
);
2474 /* Get the bottom subvolume's name from ROOT_REF */
2475 root
= fs_info
->tree_root
;
2476 key
.objectid
= treeid
;
2477 key
.type
= BTRFS_ROOT_REF_KEY
;
2478 key
.offset
= args
->treeid
;
2479 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2482 } else if (ret
> 0) {
2487 leaf
= path
->nodes
[0];
2488 slot
= path
->slots
[0];
2489 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2491 item_off
= btrfs_item_ptr_offset(leaf
, slot
);
2492 item_len
= btrfs_item_size_nr(leaf
, slot
);
2493 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2494 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2495 if (args
->dirid
!= btrfs_root_ref_dirid(leaf
, rref
)) {
2500 /* Copy subvolume's name */
2501 item_off
+= sizeof(struct btrfs_root_ref
);
2502 item_len
-= sizeof(struct btrfs_root_ref
);
2503 read_extent_buffer(leaf
, args
->name
, item_off
, item_len
);
2504 args
->name
[item_len
] = 0;
2507 btrfs_free_path(path
);
2511 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
2514 struct btrfs_ioctl_ino_lookup_args
*args
;
2515 struct inode
*inode
;
2518 args
= memdup_user(argp
, sizeof(*args
));
2520 return PTR_ERR(args
);
2522 inode
= file_inode(file
);
2525 * Unprivileged query to obtain the containing subvolume root id. The
2526 * path is reset so it's consistent with btrfs_search_path_in_tree.
2528 if (args
->treeid
== 0)
2529 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2531 if (args
->objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
2536 if (!capable(CAP_SYS_ADMIN
)) {
2541 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
2542 args
->treeid
, args
->objectid
,
2546 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2554 * Version of ino_lookup ioctl (unprivileged)
2556 * The main differences from ino_lookup ioctl are:
2558 * 1. Read + Exec permission will be checked using inode_permission() during
2559 * path construction. -EACCES will be returned in case of failure.
2560 * 2. Path construction will be stopped at the inode number which corresponds
2561 * to the fd with which this ioctl is called. If constructed path does not
2562 * exist under fd's inode, -EACCES will be returned.
2563 * 3. The name of bottom subvolume is also searched and filled.
2565 static int btrfs_ioctl_ino_lookup_user(struct file
*file
, void __user
*argp
)
2567 struct btrfs_ioctl_ino_lookup_user_args
*args
;
2568 struct inode
*inode
;
2571 args
= memdup_user(argp
, sizeof(*args
));
2573 return PTR_ERR(args
);
2575 inode
= file_inode(file
);
2577 if (args
->dirid
== BTRFS_FIRST_FREE_OBJECTID
&&
2578 BTRFS_I(inode
)->location
.objectid
!= BTRFS_FIRST_FREE_OBJECTID
) {
2580 * The subvolume does not exist under fd with which this is
2587 ret
= btrfs_search_path_in_tree_user(inode
, args
);
2589 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2596 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2597 static int btrfs_ioctl_get_subvol_info(struct file
*file
, void __user
*argp
)
2599 struct btrfs_ioctl_get_subvol_info_args
*subvol_info
;
2600 struct btrfs_fs_info
*fs_info
;
2601 struct btrfs_root
*root
;
2602 struct btrfs_path
*path
;
2603 struct btrfs_key key
;
2604 struct btrfs_root_item
*root_item
;
2605 struct btrfs_root_ref
*rref
;
2606 struct extent_buffer
*leaf
;
2607 unsigned long item_off
;
2608 unsigned long item_len
;
2609 struct inode
*inode
;
2613 path
= btrfs_alloc_path();
2617 subvol_info
= kzalloc(sizeof(*subvol_info
), GFP_KERNEL
);
2619 btrfs_free_path(path
);
2623 inode
= file_inode(file
);
2624 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2626 /* Get root_item of inode's subvolume */
2627 key
.objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2628 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2629 key
.offset
= (u64
)-1;
2630 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2632 ret
= PTR_ERR(root
);
2635 root_item
= &root
->root_item
;
2637 subvol_info
->treeid
= key
.objectid
;
2639 subvol_info
->generation
= btrfs_root_generation(root_item
);
2640 subvol_info
->flags
= btrfs_root_flags(root_item
);
2642 memcpy(subvol_info
->uuid
, root_item
->uuid
, BTRFS_UUID_SIZE
);
2643 memcpy(subvol_info
->parent_uuid
, root_item
->parent_uuid
,
2645 memcpy(subvol_info
->received_uuid
, root_item
->received_uuid
,
2648 subvol_info
->ctransid
= btrfs_root_ctransid(root_item
);
2649 subvol_info
->ctime
.sec
= btrfs_stack_timespec_sec(&root_item
->ctime
);
2650 subvol_info
->ctime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->ctime
);
2652 subvol_info
->otransid
= btrfs_root_otransid(root_item
);
2653 subvol_info
->otime
.sec
= btrfs_stack_timespec_sec(&root_item
->otime
);
2654 subvol_info
->otime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->otime
);
2656 subvol_info
->stransid
= btrfs_root_stransid(root_item
);
2657 subvol_info
->stime
.sec
= btrfs_stack_timespec_sec(&root_item
->stime
);
2658 subvol_info
->stime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->stime
);
2660 subvol_info
->rtransid
= btrfs_root_rtransid(root_item
);
2661 subvol_info
->rtime
.sec
= btrfs_stack_timespec_sec(&root_item
->rtime
);
2662 subvol_info
->rtime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->rtime
);
2664 if (key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) {
2665 /* Search root tree for ROOT_BACKREF of this subvolume */
2666 root
= fs_info
->tree_root
;
2668 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2670 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2673 } else if (path
->slots
[0] >=
2674 btrfs_header_nritems(path
->nodes
[0])) {
2675 ret
= btrfs_next_leaf(root
, path
);
2678 } else if (ret
> 0) {
2684 leaf
= path
->nodes
[0];
2685 slot
= path
->slots
[0];
2686 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2687 if (key
.objectid
== subvol_info
->treeid
&&
2688 key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
2689 subvol_info
->parent_id
= key
.offset
;
2691 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2692 subvol_info
->dirid
= btrfs_root_ref_dirid(leaf
, rref
);
2694 item_off
= btrfs_item_ptr_offset(leaf
, slot
)
2695 + sizeof(struct btrfs_root_ref
);
2696 item_len
= btrfs_item_size_nr(leaf
, slot
)
2697 - sizeof(struct btrfs_root_ref
);
2698 read_extent_buffer(leaf
, subvol_info
->name
,
2699 item_off
, item_len
);
2706 if (copy_to_user(argp
, subvol_info
, sizeof(*subvol_info
)))
2710 btrfs_free_path(path
);
2711 kzfree(subvol_info
);
2716 * Return ROOT_REF information of the subvolume containing this inode
2717 * except the subvolume name.
2719 static int btrfs_ioctl_get_subvol_rootref(struct file
*file
, void __user
*argp
)
2721 struct btrfs_ioctl_get_subvol_rootref_args
*rootrefs
;
2722 struct btrfs_root_ref
*rref
;
2723 struct btrfs_root
*root
;
2724 struct btrfs_path
*path
;
2725 struct btrfs_key key
;
2726 struct extent_buffer
*leaf
;
2727 struct inode
*inode
;
2733 path
= btrfs_alloc_path();
2737 rootrefs
= memdup_user(argp
, sizeof(*rootrefs
));
2738 if (IS_ERR(rootrefs
)) {
2739 btrfs_free_path(path
);
2740 return PTR_ERR(rootrefs
);
2743 inode
= file_inode(file
);
2744 root
= BTRFS_I(inode
)->root
->fs_info
->tree_root
;
2745 objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2747 key
.objectid
= objectid
;
2748 key
.type
= BTRFS_ROOT_REF_KEY
;
2749 key
.offset
= rootrefs
->min_treeid
;
2752 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2755 } else if (path
->slots
[0] >=
2756 btrfs_header_nritems(path
->nodes
[0])) {
2757 ret
= btrfs_next_leaf(root
, path
);
2760 } else if (ret
> 0) {
2766 leaf
= path
->nodes
[0];
2767 slot
= path
->slots
[0];
2769 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2770 if (key
.objectid
!= objectid
|| key
.type
!= BTRFS_ROOT_REF_KEY
) {
2775 if (found
== BTRFS_MAX_ROOTREF_BUFFER_NUM
) {
2780 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2781 rootrefs
->rootref
[found
].treeid
= key
.offset
;
2782 rootrefs
->rootref
[found
].dirid
=
2783 btrfs_root_ref_dirid(leaf
, rref
);
2786 ret
= btrfs_next_item(root
, path
);
2789 } else if (ret
> 0) {
2796 if (!ret
|| ret
== -EOVERFLOW
) {
2797 rootrefs
->num_items
= found
;
2798 /* update min_treeid for next search */
2800 rootrefs
->min_treeid
=
2801 rootrefs
->rootref
[found
- 1].treeid
+ 1;
2802 if (copy_to_user(argp
, rootrefs
, sizeof(*rootrefs
)))
2807 btrfs_free_path(path
);
2812 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
2815 struct dentry
*parent
= file
->f_path
.dentry
;
2816 struct btrfs_fs_info
*fs_info
= btrfs_sb(parent
->d_sb
);
2817 struct dentry
*dentry
;
2818 struct inode
*dir
= d_inode(parent
);
2819 struct inode
*inode
;
2820 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2821 struct btrfs_root
*dest
= NULL
;
2822 struct btrfs_ioctl_vol_args
*vol_args
;
2826 if (!S_ISDIR(dir
->i_mode
))
2829 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2830 if (IS_ERR(vol_args
))
2831 return PTR_ERR(vol_args
);
2833 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2834 namelen
= strlen(vol_args
->name
);
2835 if (strchr(vol_args
->name
, '/') ||
2836 strncmp(vol_args
->name
, "..", namelen
) == 0) {
2841 err
= mnt_want_write_file(file
);
2846 err
= down_write_killable_nested(&dir
->i_rwsem
, I_MUTEX_PARENT
);
2848 goto out_drop_write
;
2849 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
2850 if (IS_ERR(dentry
)) {
2851 err
= PTR_ERR(dentry
);
2852 goto out_unlock_dir
;
2855 if (d_really_is_negative(dentry
)) {
2860 inode
= d_inode(dentry
);
2861 dest
= BTRFS_I(inode
)->root
;
2862 if (!capable(CAP_SYS_ADMIN
)) {
2864 * Regular user. Only allow this with a special mount
2865 * option, when the user has write+exec access to the
2866 * subvol root, and when rmdir(2) would have been
2869 * Note that this is _not_ check that the subvol is
2870 * empty or doesn't contain data that we wouldn't
2871 * otherwise be able to delete.
2873 * Users who want to delete empty subvols should try
2877 if (!btrfs_test_opt(fs_info
, USER_SUBVOL_RM_ALLOWED
))
2881 * Do not allow deletion if the parent dir is the same
2882 * as the dir to be deleted. That means the ioctl
2883 * must be called on the dentry referencing the root
2884 * of the subvol, not a random directory contained
2891 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
2896 /* check if subvolume may be deleted by a user */
2897 err
= btrfs_may_delete(dir
, dentry
, 1);
2901 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
2907 err
= btrfs_delete_subvolume(dir
, dentry
);
2908 inode_unlock(inode
);
2917 mnt_drop_write_file(file
);
2923 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2925 struct inode
*inode
= file_inode(file
);
2926 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2927 struct btrfs_ioctl_defrag_range_args
*range
;
2930 ret
= mnt_want_write_file(file
);
2934 if (btrfs_root_readonly(root
)) {
2939 switch (inode
->i_mode
& S_IFMT
) {
2941 if (!capable(CAP_SYS_ADMIN
)) {
2945 ret
= btrfs_defrag_root(root
);
2949 * Note that this does not check the file descriptor for write
2950 * access. This prevents defragmenting executables that are
2951 * running and allows defrag on files open in read-only mode.
2953 if (!capable(CAP_SYS_ADMIN
) &&
2954 inode_permission(inode
, MAY_WRITE
)) {
2959 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2966 if (copy_from_user(range
, argp
,
2972 /* compression requires us to start the IO */
2973 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2974 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2975 range
->extent_thresh
= (u32
)-1;
2978 /* the rest are all set to zero by kzalloc */
2979 range
->len
= (u64
)-1;
2981 ret
= btrfs_defrag_file(file_inode(file
), file
,
2982 range
, BTRFS_OLDEST_GENERATION
, 0);
2991 mnt_drop_write_file(file
);
2995 static long btrfs_ioctl_add_dev(struct btrfs_fs_info
*fs_info
, void __user
*arg
)
2997 struct btrfs_ioctl_vol_args
*vol_args
;
3000 if (!capable(CAP_SYS_ADMIN
))
3003 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
))
3004 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3006 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
3007 if (IS_ERR(vol_args
)) {
3008 ret
= PTR_ERR(vol_args
);
3012 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
3013 ret
= btrfs_init_new_device(fs_info
, vol_args
->name
);
3016 btrfs_info(fs_info
, "disk added %s", vol_args
->name
);
3020 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3024 static long btrfs_ioctl_rm_dev_v2(struct file
*file
, void __user
*arg
)
3026 struct inode
*inode
= file_inode(file
);
3027 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3028 struct btrfs_ioctl_vol_args_v2
*vol_args
;
3031 if (!capable(CAP_SYS_ADMIN
))
3034 ret
= mnt_want_write_file(file
);
3038 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
3039 if (IS_ERR(vol_args
)) {
3040 ret
= PTR_ERR(vol_args
);
3044 /* Check for compatibility reject unknown flags */
3045 if (vol_args
->flags
& ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED
) {
3050 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
3051 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3055 if (vol_args
->flags
& BTRFS_DEVICE_SPEC_BY_ID
) {
3056 ret
= btrfs_rm_device(fs_info
, NULL
, vol_args
->devid
);
3058 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
3059 ret
= btrfs_rm_device(fs_info
, vol_args
->name
, 0);
3061 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3064 if (vol_args
->flags
& BTRFS_DEVICE_SPEC_BY_ID
)
3065 btrfs_info(fs_info
, "device deleted: id %llu",
3068 btrfs_info(fs_info
, "device deleted: %s",
3074 mnt_drop_write_file(file
);
3078 static long btrfs_ioctl_rm_dev(struct file
*file
, void __user
*arg
)
3080 struct inode
*inode
= file_inode(file
);
3081 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3082 struct btrfs_ioctl_vol_args
*vol_args
;
3085 if (!capable(CAP_SYS_ADMIN
))
3088 ret
= mnt_want_write_file(file
);
3092 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
3093 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3094 goto out_drop_write
;
3097 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
3098 if (IS_ERR(vol_args
)) {
3099 ret
= PTR_ERR(vol_args
);
3103 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
3104 ret
= btrfs_rm_device(fs_info
, vol_args
->name
, 0);
3107 btrfs_info(fs_info
, "disk deleted %s", vol_args
->name
);
3110 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3112 mnt_drop_write_file(file
);
3117 static long btrfs_ioctl_fs_info(struct btrfs_fs_info
*fs_info
,
3120 struct btrfs_ioctl_fs_info_args
*fi_args
;
3121 struct btrfs_device
*device
;
3122 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
3125 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
3130 fi_args
->num_devices
= fs_devices
->num_devices
;
3132 list_for_each_entry_rcu(device
, &fs_devices
->devices
, dev_list
) {
3133 if (device
->devid
> fi_args
->max_id
)
3134 fi_args
->max_id
= device
->devid
;
3138 memcpy(&fi_args
->fsid
, fs_info
->fsid
, sizeof(fi_args
->fsid
));
3139 fi_args
->nodesize
= fs_info
->nodesize
;
3140 fi_args
->sectorsize
= fs_info
->sectorsize
;
3141 fi_args
->clone_alignment
= fs_info
->sectorsize
;
3143 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
3150 static long btrfs_ioctl_dev_info(struct btrfs_fs_info
*fs_info
,
3153 struct btrfs_ioctl_dev_info_args
*di_args
;
3154 struct btrfs_device
*dev
;
3156 char *s_uuid
= NULL
;
3158 di_args
= memdup_user(arg
, sizeof(*di_args
));
3159 if (IS_ERR(di_args
))
3160 return PTR_ERR(di_args
);
3162 if (!btrfs_is_empty_uuid(di_args
->uuid
))
3163 s_uuid
= di_args
->uuid
;
3166 dev
= btrfs_find_device(fs_info
, di_args
->devid
, s_uuid
, NULL
);
3173 di_args
->devid
= dev
->devid
;
3174 di_args
->bytes_used
= btrfs_device_get_bytes_used(dev
);
3175 di_args
->total_bytes
= btrfs_device_get_total_bytes(dev
);
3176 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
3178 strncpy(di_args
->path
, rcu_str_deref(dev
->name
),
3179 sizeof(di_args
->path
) - 1);
3180 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
3182 di_args
->path
[0] = '\0';
3187 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
3194 static struct page
*extent_same_get_page(struct inode
*inode
, pgoff_t index
)
3198 page
= grab_cache_page(inode
->i_mapping
, index
);
3200 return ERR_PTR(-ENOMEM
);
3202 if (!PageUptodate(page
)) {
3205 ret
= btrfs_readpage(NULL
, page
);
3207 return ERR_PTR(ret
);
3209 if (!PageUptodate(page
)) {
3212 return ERR_PTR(-EIO
);
3214 if (page
->mapping
!= inode
->i_mapping
) {
3217 return ERR_PTR(-EAGAIN
);
3224 static int gather_extent_pages(struct inode
*inode
, struct page
**pages
,
3225 int num_pages
, u64 off
)
3228 pgoff_t index
= off
>> PAGE_SHIFT
;
3230 for (i
= 0; i
< num_pages
; i
++) {
3232 pages
[i
] = extent_same_get_page(inode
, index
+ i
);
3233 if (IS_ERR(pages
[i
])) {
3234 int err
= PTR_ERR(pages
[i
]);
3245 static int lock_extent_range(struct inode
*inode
, u64 off
, u64 len
,
3246 bool retry_range_locking
)
3249 * Do any pending delalloc/csum calculations on inode, one way or
3250 * another, and lock file content.
3251 * The locking order is:
3254 * 2) range in the inode's io tree
3257 struct btrfs_ordered_extent
*ordered
;
3258 lock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
3259 ordered
= btrfs_lookup_first_ordered_extent(inode
,
3262 ordered
->file_offset
+ ordered
->len
<= off
||
3263 ordered
->file_offset
>= off
+ len
) &&
3264 !test_range_bit(&BTRFS_I(inode
)->io_tree
, off
,
3265 off
+ len
- 1, EXTENT_DELALLOC
, 0, NULL
)) {
3267 btrfs_put_ordered_extent(ordered
);
3270 unlock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
3272 btrfs_put_ordered_extent(ordered
);
3273 if (!retry_range_locking
)
3275 btrfs_wait_ordered_range(inode
, off
, len
);
3280 static void btrfs_double_inode_unlock(struct inode
*inode1
, struct inode
*inode2
)
3282 inode_unlock(inode1
);
3283 inode_unlock(inode2
);
3286 static void btrfs_double_inode_lock(struct inode
*inode1
, struct inode
*inode2
)
3288 if (inode1
< inode2
)
3289 swap(inode1
, inode2
);
3291 inode_lock_nested(inode1
, I_MUTEX_PARENT
);
3292 inode_lock_nested(inode2
, I_MUTEX_CHILD
);
3295 static void btrfs_double_extent_unlock(struct inode
*inode1
, u64 loff1
,
3296 struct inode
*inode2
, u64 loff2
, u64 len
)
3298 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
3299 unlock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
3302 static int btrfs_double_extent_lock(struct inode
*inode1
, u64 loff1
,
3303 struct inode
*inode2
, u64 loff2
, u64 len
,
3304 bool retry_range_locking
)
3308 if (inode1
< inode2
) {
3309 swap(inode1
, inode2
);
3312 ret
= lock_extent_range(inode1
, loff1
, len
, retry_range_locking
);
3315 ret
= lock_extent_range(inode2
, loff2
, len
, retry_range_locking
);
3317 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
,
3324 struct page
**src_pages
;
3325 struct page
**dst_pages
;
3328 static void btrfs_cmp_data_free(struct cmp_pages
*cmp
)
3333 for (i
= 0; i
< cmp
->num_pages
; i
++) {
3334 pg
= cmp
->src_pages
[i
];
3338 cmp
->src_pages
[i
] = NULL
;
3340 pg
= cmp
->dst_pages
[i
];
3344 cmp
->dst_pages
[i
] = NULL
;
3349 static int btrfs_cmp_data_prepare(struct inode
*src
, u64 loff
,
3350 struct inode
*dst
, u64 dst_loff
,
3351 u64 len
, struct cmp_pages
*cmp
)
3354 int num_pages
= PAGE_ALIGN(len
) >> PAGE_SHIFT
;
3356 cmp
->num_pages
= num_pages
;
3358 ret
= gather_extent_pages(src
, cmp
->src_pages
, num_pages
, loff
);
3362 ret
= gather_extent_pages(dst
, cmp
->dst_pages
, num_pages
, dst_loff
);
3366 btrfs_cmp_data_free(cmp
);
3370 static int btrfs_cmp_data(u64 len
, struct cmp_pages
*cmp
)
3374 struct page
*src_page
, *dst_page
;
3375 unsigned int cmp_len
= PAGE_SIZE
;
3376 void *addr
, *dst_addr
;
3380 if (len
< PAGE_SIZE
)
3383 BUG_ON(i
>= cmp
->num_pages
);
3385 src_page
= cmp
->src_pages
[i
];
3386 dst_page
= cmp
->dst_pages
[i
];
3387 ASSERT(PageLocked(src_page
));
3388 ASSERT(PageLocked(dst_page
));
3390 addr
= kmap_atomic(src_page
);
3391 dst_addr
= kmap_atomic(dst_page
);
3393 flush_dcache_page(src_page
);
3394 flush_dcache_page(dst_page
);
3396 if (memcmp(addr
, dst_addr
, cmp_len
))
3399 kunmap_atomic(addr
);
3400 kunmap_atomic(dst_addr
);
3412 static int extent_same_check_offsets(struct inode
*inode
, u64 off
, u64
*plen
,
3416 u64 bs
= BTRFS_I(inode
)->root
->fs_info
->sb
->s_blocksize
;
3418 if (off
+ olen
> inode
->i_size
|| off
+ olen
< off
)
3421 /* if we extend to eof, continue to block boundary */
3422 if (off
+ len
== inode
->i_size
)
3423 *plen
= len
= ALIGN(inode
->i_size
, bs
) - off
;
3425 /* Check that we are block aligned - btrfs_clone() requires this */
3426 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
))
3432 static int btrfs_extent_same_range(struct inode
*src
, u64 loff
, u64 olen
,
3433 struct inode
*dst
, u64 dst_loff
,
3434 struct cmp_pages
*cmp
)
3438 bool same_inode
= (src
== dst
);
3439 u64 same_lock_start
= 0;
3440 u64 same_lock_len
= 0;
3442 ret
= extent_same_check_offsets(src
, loff
, &len
, olen
);
3446 ret
= extent_same_check_offsets(dst
, dst_loff
, &len
, olen
);
3452 * Single inode case wants the same checks, except we
3453 * don't want our length pushed out past i_size as
3454 * comparing that data range makes no sense.
3456 * extent_same_check_offsets() will do this for an
3457 * unaligned length at i_size, so catch it here and
3458 * reject the request.
3460 * This effectively means we require aligned extents
3461 * for the single-inode case, whereas the other cases
3462 * allow an unaligned length so long as it ends at
3468 /* Check for overlapping ranges */
3469 if (dst_loff
+ len
> loff
&& dst_loff
< loff
+ len
)
3472 same_lock_start
= min_t(u64
, loff
, dst_loff
);
3473 same_lock_len
= max_t(u64
, loff
, dst_loff
) + len
- same_lock_start
;
3476 * If the source and destination inodes are different, the
3477 * source's range end offset matches the source's i_size, that
3478 * i_size is not a multiple of the sector size, and the
3479 * destination range does not go past the destination's i_size,
3480 * we must round down the length to the nearest sector size
3481 * multiple. If we don't do this adjustment we end replacing
3482 * with zeroes the bytes in the range that starts at the
3483 * deduplication range's end offset and ends at the next sector
3486 if (loff
+ olen
== i_size_read(src
) &&
3487 dst_loff
+ len
< i_size_read(dst
)) {
3488 const u64 sz
= BTRFS_I(src
)->root
->fs_info
->sectorsize
;
3490 len
= round_down(i_size_read(src
), sz
) - loff
;
3498 ret
= btrfs_cmp_data_prepare(src
, loff
, dst
, dst_loff
, olen
, cmp
);
3503 ret
= lock_extent_range(src
, same_lock_start
, same_lock_len
,
3506 ret
= btrfs_double_extent_lock(src
, loff
, dst
, dst_loff
, len
,
3509 * If one of the inodes has dirty pages in the respective range or
3510 * ordered extents, we need to flush dellaloc and wait for all ordered
3511 * extents in the range. We must unlock the pages and the ranges in the
3512 * io trees to avoid deadlocks when flushing delalloc (requires locking
3513 * pages) and when waiting for ordered extents to complete (they require
3516 if (ret
== -EAGAIN
) {
3518 * Ranges in the io trees already unlocked. Now unlock all
3519 * pages before waiting for all IO to complete.
3521 btrfs_cmp_data_free(cmp
);
3523 btrfs_wait_ordered_range(src
, same_lock_start
,
3526 btrfs_wait_ordered_range(src
, loff
, len
);
3527 btrfs_wait_ordered_range(dst
, dst_loff
, len
);
3533 /* ranges in the io trees already unlocked */
3534 btrfs_cmp_data_free(cmp
);
3538 /* pass original length for comparison so we stay within i_size */
3539 ret
= btrfs_cmp_data(olen
, cmp
);
3541 ret
= btrfs_clone(src
, dst
, loff
, olen
, len
, dst_loff
, 1);
3544 unlock_extent(&BTRFS_I(src
)->io_tree
, same_lock_start
,
3545 same_lock_start
+ same_lock_len
- 1);
3547 btrfs_double_extent_unlock(src
, loff
, dst
, dst_loff
, len
);
3549 btrfs_cmp_data_free(cmp
);
3554 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3556 static int btrfs_extent_same(struct inode
*src
, u64 loff
, u64 olen
,
3557 struct inode
*dst
, u64 dst_loff
)
3560 struct cmp_pages cmp
;
3561 int num_pages
= PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN
) >> PAGE_SHIFT
;
3562 bool same_inode
= (src
== dst
);
3563 u64 i
, tail_len
, chunk_count
;
3571 btrfs_double_inode_lock(src
, dst
);
3573 /* don't make the dst file partly checksummed */
3574 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3575 (BTRFS_I(dst
)->flags
& BTRFS_INODE_NODATASUM
)) {
3580 tail_len
= olen
% BTRFS_MAX_DEDUPE_LEN
;
3581 chunk_count
= div_u64(olen
, BTRFS_MAX_DEDUPE_LEN
);
3582 if (chunk_count
== 0)
3583 num_pages
= PAGE_ALIGN(tail_len
) >> PAGE_SHIFT
;
3586 * If deduping ranges in the same inode, locking rules make it
3587 * mandatory to always lock pages in ascending order to avoid deadlocks
3588 * with concurrent tasks (such as starting writeback/delalloc).
3590 if (same_inode
&& dst_loff
< loff
)
3591 swap(loff
, dst_loff
);
3594 * We must gather up all the pages before we initiate our extent
3595 * locking. We use an array for the page pointers. Size of the array is
3596 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
3598 cmp
.src_pages
= kvmalloc_array(num_pages
, sizeof(struct page
*),
3599 GFP_KERNEL
| __GFP_ZERO
);
3600 cmp
.dst_pages
= kvmalloc_array(num_pages
, sizeof(struct page
*),
3601 GFP_KERNEL
| __GFP_ZERO
);
3602 if (!cmp
.src_pages
|| !cmp
.dst_pages
) {
3607 for (i
= 0; i
< chunk_count
; i
++) {
3608 ret
= btrfs_extent_same_range(src
, loff
, BTRFS_MAX_DEDUPE_LEN
,
3609 dst
, dst_loff
, &cmp
);
3613 loff
+= BTRFS_MAX_DEDUPE_LEN
;
3614 dst_loff
+= BTRFS_MAX_DEDUPE_LEN
;
3618 ret
= btrfs_extent_same_range(src
, loff
, tail_len
, dst
,
3622 kvfree(cmp
.src_pages
);
3623 kvfree(cmp
.dst_pages
);
3629 btrfs_double_inode_unlock(src
, dst
);
3634 static int clone_finish_inode_update(struct btrfs_trans_handle
*trans
,
3635 struct inode
*inode
,
3641 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3644 inode_inc_iversion(inode
);
3645 if (!no_time_update
)
3646 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
3648 * We round up to the block size at eof when determining which
3649 * extents to clone above, but shouldn't round up the file size.
3651 if (endoff
> destoff
+ olen
)
3652 endoff
= destoff
+ olen
;
3653 if (endoff
> inode
->i_size
)
3654 btrfs_i_size_write(BTRFS_I(inode
), endoff
);
3656 ret
= btrfs_update_inode(trans
, root
, inode
);
3658 btrfs_abort_transaction(trans
, ret
);
3659 btrfs_end_transaction(trans
);
3662 ret
= btrfs_end_transaction(trans
);
3667 static void clone_update_extent_map(struct btrfs_inode
*inode
,
3668 const struct btrfs_trans_handle
*trans
,
3669 const struct btrfs_path
*path
,
3670 const u64 hole_offset
,
3673 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
3674 struct extent_map
*em
;
3677 em
= alloc_extent_map();
3679 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &inode
->runtime_flags
);
3684 struct btrfs_file_extent_item
*fi
;
3686 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3687 struct btrfs_file_extent_item
);
3688 btrfs_extent_item_to_extent_map(inode
, path
, fi
, false, em
);
3689 em
->generation
= -1;
3690 if (btrfs_file_extent_type(path
->nodes
[0], fi
) ==
3691 BTRFS_FILE_EXTENT_INLINE
)
3692 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3693 &inode
->runtime_flags
);
3695 em
->start
= hole_offset
;
3697 em
->ram_bytes
= em
->len
;
3698 em
->orig_start
= hole_offset
;
3699 em
->block_start
= EXTENT_MAP_HOLE
;
3701 em
->orig_block_len
= 0;
3702 em
->compress_type
= BTRFS_COMPRESS_NONE
;
3703 em
->generation
= trans
->transid
;
3707 write_lock(&em_tree
->lock
);
3708 ret
= add_extent_mapping(em_tree
, em
, 1);
3709 write_unlock(&em_tree
->lock
);
3710 if (ret
!= -EEXIST
) {
3711 free_extent_map(em
);
3714 btrfs_drop_extent_cache(inode
, em
->start
,
3715 em
->start
+ em
->len
- 1, 0);
3719 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &inode
->runtime_flags
);
3723 * Make sure we do not end up inserting an inline extent into a file that has
3724 * already other (non-inline) extents. If a file has an inline extent it can
3725 * not have any other extents and the (single) inline extent must start at the
3726 * file offset 0. Failing to respect these rules will lead to file corruption,
3727 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3729 * We can have extents that have been already written to disk or we can have
3730 * dirty ranges still in delalloc, in which case the extent maps and items are
3731 * created only when we run delalloc, and the delalloc ranges might fall outside
3732 * the range we are currently locking in the inode's io tree. So we check the
3733 * inode's i_size because of that (i_size updates are done while holding the
3734 * i_mutex, which we are holding here).
3735 * We also check to see if the inode has a size not greater than "datal" but has
3736 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3737 * protected against such concurrent fallocate calls by the i_mutex).
3739 * If the file has no extents but a size greater than datal, do not allow the
3740 * copy because we would need turn the inline extent into a non-inline one (even
3741 * with NO_HOLES enabled). If we find our destination inode only has one inline
3742 * extent, just overwrite it with the source inline extent if its size is less
3743 * than the source extent's size, or we could copy the source inline extent's
3744 * data into the destination inode's inline extent if the later is greater then
3747 static int clone_copy_inline_extent(struct inode
*dst
,
3748 struct btrfs_trans_handle
*trans
,
3749 struct btrfs_path
*path
,
3750 struct btrfs_key
*new_key
,
3751 const u64 drop_start
,
3757 struct btrfs_fs_info
*fs_info
= btrfs_sb(dst
->i_sb
);
3758 struct btrfs_root
*root
= BTRFS_I(dst
)->root
;
3759 const u64 aligned_end
= ALIGN(new_key
->offset
+ datal
,
3760 fs_info
->sectorsize
);
3762 struct btrfs_key key
;
3764 if (new_key
->offset
> 0)
3767 key
.objectid
= btrfs_ino(BTRFS_I(dst
));
3768 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3770 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3773 } else if (ret
> 0) {
3774 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
3775 ret
= btrfs_next_leaf(root
, path
);
3779 goto copy_inline_extent
;
3781 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3782 if (key
.objectid
== btrfs_ino(BTRFS_I(dst
)) &&
3783 key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3784 ASSERT(key
.offset
> 0);
3787 } else if (i_size_read(dst
) <= datal
) {
3788 struct btrfs_file_extent_item
*ei
;
3792 * If the file size is <= datal, make sure there are no other
3793 * extents following (can happen do to an fallocate call with
3794 * the flag FALLOC_FL_KEEP_SIZE).
3796 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3797 struct btrfs_file_extent_item
);
3799 * If it's an inline extent, it can not have other extents
3802 if (btrfs_file_extent_type(path
->nodes
[0], ei
) ==
3803 BTRFS_FILE_EXTENT_INLINE
)
3804 goto copy_inline_extent
;
3806 ext_len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
3807 if (ext_len
> aligned_end
)
3810 ret
= btrfs_next_item(root
, path
);
3813 } else if (ret
== 0) {
3814 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3816 if (key
.objectid
== btrfs_ino(BTRFS_I(dst
)) &&
3817 key
.type
== BTRFS_EXTENT_DATA_KEY
)
3824 * We have no extent items, or we have an extent at offset 0 which may
3825 * or may not be inlined. All these cases are dealt the same way.
3827 if (i_size_read(dst
) > datal
) {
3829 * If the destination inode has an inline extent...
3830 * This would require copying the data from the source inline
3831 * extent into the beginning of the destination's inline extent.
3832 * But this is really complex, both extents can be compressed
3833 * or just one of them, which would require decompressing and
3834 * re-compressing data (which could increase the new compressed
3835 * size, not allowing the compressed data to fit anymore in an
3837 * So just don't support this case for now (it should be rare,
3838 * we are not really saving space when cloning inline extents).
3843 btrfs_release_path(path
);
3844 ret
= btrfs_drop_extents(trans
, root
, dst
, drop_start
, aligned_end
, 1);
3847 ret
= btrfs_insert_empty_item(trans
, root
, path
, new_key
, size
);
3852 const u32 start
= btrfs_file_extent_calc_inline_size(0);
3854 memmove(inline_data
+ start
, inline_data
+ start
+ skip
, datal
);
3857 write_extent_buffer(path
->nodes
[0], inline_data
,
3858 btrfs_item_ptr_offset(path
->nodes
[0],
3861 inode_add_bytes(dst
, datal
);
3867 * btrfs_clone() - clone a range from inode file to another
3869 * @src: Inode to clone from
3870 * @inode: Inode to clone to
3871 * @off: Offset within source to start clone from
3872 * @olen: Original length, passed by user, of range to clone
3873 * @olen_aligned: Block-aligned value of olen
3874 * @destoff: Offset within @inode to start clone
3875 * @no_time_update: Whether to update mtime/ctime on the target inode
3877 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
3878 const u64 off
, const u64 olen
, const u64 olen_aligned
,
3879 const u64 destoff
, int no_time_update
)
3881 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3882 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3883 struct btrfs_path
*path
= NULL
;
3884 struct extent_buffer
*leaf
;
3885 struct btrfs_trans_handle
*trans
;
3887 struct btrfs_key key
;
3891 const u64 len
= olen_aligned
;
3892 u64 last_dest_end
= destoff
;
3895 buf
= kvmalloc(fs_info
->nodesize
, GFP_KERNEL
);
3899 path
= btrfs_alloc_path();
3905 path
->reada
= READA_FORWARD
;
3907 key
.objectid
= btrfs_ino(BTRFS_I(src
));
3908 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3912 u64 next_key_min_offset
= key
.offset
+ 1;
3915 * note the key will change type as we walk through the
3918 path
->leave_spinning
= 1;
3919 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
3924 * First search, if no extent item that starts at offset off was
3925 * found but the previous item is an extent item, it's possible
3926 * it might overlap our target range, therefore process it.
3928 if (key
.offset
== off
&& ret
> 0 && path
->slots
[0] > 0) {
3929 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3930 path
->slots
[0] - 1);
3931 if (key
.type
== BTRFS_EXTENT_DATA_KEY
)
3935 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3937 if (path
->slots
[0] >= nritems
) {
3938 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
3943 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3945 leaf
= path
->nodes
[0];
3946 slot
= path
->slots
[0];
3948 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3949 if (key
.type
> BTRFS_EXTENT_DATA_KEY
||
3950 key
.objectid
!= btrfs_ino(BTRFS_I(src
)))
3953 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3954 struct btrfs_file_extent_item
*extent
;
3957 struct btrfs_key new_key
;
3958 u64 disko
= 0, diskl
= 0;
3959 u64 datao
= 0, datal
= 0;
3963 extent
= btrfs_item_ptr(leaf
, slot
,
3964 struct btrfs_file_extent_item
);
3965 comp
= btrfs_file_extent_compression(leaf
, extent
);
3966 type
= btrfs_file_extent_type(leaf
, extent
);
3967 if (type
== BTRFS_FILE_EXTENT_REG
||
3968 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3969 disko
= btrfs_file_extent_disk_bytenr(leaf
,
3971 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
3973 datao
= btrfs_file_extent_offset(leaf
, extent
);
3974 datal
= btrfs_file_extent_num_bytes(leaf
,
3976 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3977 /* take upper bound, may be compressed */
3978 datal
= btrfs_file_extent_ram_bytes(leaf
,
3983 * The first search might have left us at an extent
3984 * item that ends before our target range's start, can
3985 * happen if we have holes and NO_HOLES feature enabled.
3987 if (key
.offset
+ datal
<= off
) {
3990 } else if (key
.offset
>= off
+ len
) {
3993 next_key_min_offset
= key
.offset
+ datal
;
3994 size
= btrfs_item_size_nr(leaf
, slot
);
3995 read_extent_buffer(leaf
, buf
,
3996 btrfs_item_ptr_offset(leaf
, slot
),
3999 btrfs_release_path(path
);
4000 path
->leave_spinning
= 0;
4002 memcpy(&new_key
, &key
, sizeof(new_key
));
4003 new_key
.objectid
= btrfs_ino(BTRFS_I(inode
));
4004 if (off
<= key
.offset
)
4005 new_key
.offset
= key
.offset
+ destoff
- off
;
4007 new_key
.offset
= destoff
;
4010 * Deal with a hole that doesn't have an extent item
4011 * that represents it (NO_HOLES feature enabled).
4012 * This hole is either in the middle of the cloning
4013 * range or at the beginning (fully overlaps it or
4014 * partially overlaps it).
4016 if (new_key
.offset
!= last_dest_end
)
4017 drop_start
= last_dest_end
;
4019 drop_start
= new_key
.offset
;
4022 * 1 - adjusting old extent (we may have to split it)
4023 * 1 - add new extent
4026 trans
= btrfs_start_transaction(root
, 3);
4027 if (IS_ERR(trans
)) {
4028 ret
= PTR_ERR(trans
);
4032 if (type
== BTRFS_FILE_EXTENT_REG
||
4033 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
4035 * a | --- range to clone ---| b
4036 * | ------------- extent ------------- |
4039 /* subtract range b */
4040 if (key
.offset
+ datal
> off
+ len
)
4041 datal
= off
+ len
- key
.offset
;
4043 /* subtract range a */
4044 if (off
> key
.offset
) {
4045 datao
+= off
- key
.offset
;
4046 datal
-= off
- key
.offset
;
4049 ret
= btrfs_drop_extents(trans
, root
, inode
,
4051 new_key
.offset
+ datal
,
4054 if (ret
!= -EOPNOTSUPP
)
4055 btrfs_abort_transaction(trans
,
4057 btrfs_end_transaction(trans
);
4061 ret
= btrfs_insert_empty_item(trans
, root
, path
,
4064 btrfs_abort_transaction(trans
, ret
);
4065 btrfs_end_transaction(trans
);
4069 leaf
= path
->nodes
[0];
4070 slot
= path
->slots
[0];
4071 write_extent_buffer(leaf
, buf
,
4072 btrfs_item_ptr_offset(leaf
, slot
),
4075 extent
= btrfs_item_ptr(leaf
, slot
,
4076 struct btrfs_file_extent_item
);
4078 /* disko == 0 means it's a hole */
4082 btrfs_set_file_extent_offset(leaf
, extent
,
4084 btrfs_set_file_extent_num_bytes(leaf
, extent
,
4088 inode_add_bytes(inode
, datal
);
4089 ret
= btrfs_inc_extent_ref(trans
,
4092 root
->root_key
.objectid
,
4093 btrfs_ino(BTRFS_I(inode
)),
4094 new_key
.offset
- datao
);
4096 btrfs_abort_transaction(trans
,
4098 btrfs_end_transaction(trans
);
4103 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4107 if (off
> key
.offset
) {
4108 skip
= off
- key
.offset
;
4109 new_key
.offset
+= skip
;
4112 if (key
.offset
+ datal
> off
+ len
)
4113 trim
= key
.offset
+ datal
- (off
+ len
);
4115 if (comp
&& (skip
|| trim
)) {
4117 btrfs_end_transaction(trans
);
4120 size
-= skip
+ trim
;
4121 datal
-= skip
+ trim
;
4123 ret
= clone_copy_inline_extent(inode
,
4130 if (ret
!= -EOPNOTSUPP
)
4131 btrfs_abort_transaction(trans
,
4133 btrfs_end_transaction(trans
);
4136 leaf
= path
->nodes
[0];
4137 slot
= path
->slots
[0];
4140 /* If we have an implicit hole (NO_HOLES feature). */
4141 if (drop_start
< new_key
.offset
)
4142 clone_update_extent_map(BTRFS_I(inode
), trans
,
4144 new_key
.offset
- drop_start
);
4146 clone_update_extent_map(BTRFS_I(inode
), trans
,
4149 btrfs_mark_buffer_dirty(leaf
);
4150 btrfs_release_path(path
);
4152 last_dest_end
= ALIGN(new_key
.offset
+ datal
,
4153 fs_info
->sectorsize
);
4154 ret
= clone_finish_inode_update(trans
, inode
,
4160 if (new_key
.offset
+ datal
>= destoff
+ len
)
4163 btrfs_release_path(path
);
4164 key
.offset
= next_key_min_offset
;
4166 if (fatal_signal_pending(current
)) {
4173 if (last_dest_end
< destoff
+ len
) {
4175 * We have an implicit hole (NO_HOLES feature is enabled) that
4176 * fully or partially overlaps our cloning range at its end.
4178 btrfs_release_path(path
);
4181 * 1 - remove extent(s)
4184 trans
= btrfs_start_transaction(root
, 2);
4185 if (IS_ERR(trans
)) {
4186 ret
= PTR_ERR(trans
);
4189 ret
= btrfs_drop_extents(trans
, root
, inode
,
4190 last_dest_end
, destoff
+ len
, 1);
4192 if (ret
!= -EOPNOTSUPP
)
4193 btrfs_abort_transaction(trans
, ret
);
4194 btrfs_end_transaction(trans
);
4197 clone_update_extent_map(BTRFS_I(inode
), trans
, NULL
,
4199 destoff
+ len
- last_dest_end
);
4200 ret
= clone_finish_inode_update(trans
, inode
, destoff
+ len
,
4201 destoff
, olen
, no_time_update
);
4205 btrfs_free_path(path
);
4210 static noinline
int btrfs_clone_files(struct file
*file
, struct file
*file_src
,
4211 u64 off
, u64 olen
, u64 destoff
)
4213 struct inode
*inode
= file_inode(file
);
4214 struct inode
*src
= file_inode(file_src
);
4215 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4216 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4219 u64 bs
= fs_info
->sb
->s_blocksize
;
4220 int same_inode
= src
== inode
;
4224 * - split compressed inline extents. annoying: we need to
4225 * decompress into destination's address_space (the file offset
4226 * may change, so source mapping won't do), then recompress (or
4227 * otherwise reinsert) a subrange.
4229 * - split destination inode's inline extents. The inline extents can
4230 * be either compressed or non-compressed.
4233 if (btrfs_root_readonly(root
))
4236 if (file_src
->f_path
.mnt
!= file
->f_path
.mnt
||
4237 src
->i_sb
!= inode
->i_sb
)
4240 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
4244 btrfs_double_inode_lock(src
, inode
);
4249 /* don't make the dst file partly checksummed */
4250 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
4251 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
4256 /* determine range to clone */
4258 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
4261 olen
= len
= src
->i_size
- off
;
4263 * If we extend to eof, continue to block boundary if and only if the
4264 * destination end offset matches the destination file's size, otherwise
4265 * we would be corrupting data by placing the eof block into the middle
4268 if (off
+ len
== src
->i_size
) {
4269 if (!IS_ALIGNED(len
, bs
) && destoff
+ len
< inode
->i_size
)
4271 len
= ALIGN(src
->i_size
, bs
) - off
;
4279 /* verify the end result is block aligned */
4280 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
4281 !IS_ALIGNED(destoff
, bs
))
4284 /* verify if ranges are overlapped within the same file */
4286 if (destoff
+ len
> off
&& destoff
< off
+ len
)
4290 if (destoff
> inode
->i_size
) {
4291 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
4297 * Lock the target range too. Right after we replace the file extent
4298 * items in the fs tree (which now point to the cloned data), we might
4299 * have a worker replace them with extent items relative to a write
4300 * operation that was issued before this clone operation (i.e. confront
4301 * with inode.c:btrfs_finish_ordered_io).
4304 u64 lock_start
= min_t(u64
, off
, destoff
);
4305 u64 lock_len
= max_t(u64
, off
, destoff
) + len
- lock_start
;
4307 ret
= lock_extent_range(src
, lock_start
, lock_len
, true);
4309 ret
= btrfs_double_extent_lock(src
, off
, inode
, destoff
, len
,
4314 /* ranges in the io trees already unlocked */
4318 ret
= btrfs_clone(src
, inode
, off
, olen
, len
, destoff
, 0);
4321 u64 lock_start
= min_t(u64
, off
, destoff
);
4322 u64 lock_end
= max_t(u64
, off
, destoff
) + len
- 1;
4324 unlock_extent(&BTRFS_I(src
)->io_tree
, lock_start
, lock_end
);
4326 btrfs_double_extent_unlock(src
, off
, inode
, destoff
, len
);
4329 * Truncate page cache pages so that future reads will see the cloned
4330 * data immediately and not the previous data.
4332 truncate_inode_pages_range(&inode
->i_data
,
4333 round_down(destoff
, PAGE_SIZE
),
4334 round_up(destoff
+ len
, PAGE_SIZE
) - 1);
4337 btrfs_double_inode_unlock(src
, inode
);
4343 loff_t
btrfs_remap_file_range(struct file
*src_file
, loff_t off
,
4344 struct file
*dst_file
, loff_t destoff
, loff_t len
,
4345 unsigned int remap_flags
)
4349 if (remap_flags
& ~(REMAP_FILE_DEDUP
| REMAP_FILE_ADVISORY
))
4352 if (remap_flags
& REMAP_FILE_DEDUP
) {
4353 struct inode
*src
= file_inode(src_file
);
4354 struct inode
*dst
= file_inode(dst_file
);
4355 u64 bs
= BTRFS_I(src
)->root
->fs_info
->sb
->s_blocksize
;
4357 if (WARN_ON_ONCE(bs
< PAGE_SIZE
)) {
4359 * Btrfs does not support blocksize < page_size. As a
4360 * result, btrfs_cmp_data() won't correctly handle
4361 * this situation without an update.
4366 ret
= btrfs_extent_same(src
, off
, len
, dst
, destoff
);
4368 ret
= btrfs_clone_files(dst_file
, src_file
, off
, len
, destoff
);
4370 return ret
< 0 ? ret
: len
;
4373 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
4375 struct inode
*inode
= file_inode(file
);
4376 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4377 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4378 struct btrfs_root
*new_root
;
4379 struct btrfs_dir_item
*di
;
4380 struct btrfs_trans_handle
*trans
;
4381 struct btrfs_path
*path
;
4382 struct btrfs_key location
;
4383 struct btrfs_disk_key disk_key
;
4388 if (!capable(CAP_SYS_ADMIN
))
4391 ret
= mnt_want_write_file(file
);
4395 if (copy_from_user(&objectid
, argp
, sizeof(objectid
))) {
4401 objectid
= BTRFS_FS_TREE_OBJECTID
;
4403 location
.objectid
= objectid
;
4404 location
.type
= BTRFS_ROOT_ITEM_KEY
;
4405 location
.offset
= (u64
)-1;
4407 new_root
= btrfs_read_fs_root_no_name(fs_info
, &location
);
4408 if (IS_ERR(new_root
)) {
4409 ret
= PTR_ERR(new_root
);
4412 if (!is_fstree(new_root
->root_key
.objectid
)) {
4417 path
= btrfs_alloc_path();
4422 path
->leave_spinning
= 1;
4424 trans
= btrfs_start_transaction(root
, 1);
4425 if (IS_ERR(trans
)) {
4426 btrfs_free_path(path
);
4427 ret
= PTR_ERR(trans
);
4431 dir_id
= btrfs_super_root_dir(fs_info
->super_copy
);
4432 di
= btrfs_lookup_dir_item(trans
, fs_info
->tree_root
, path
,
4433 dir_id
, "default", 7, 1);
4434 if (IS_ERR_OR_NULL(di
)) {
4435 btrfs_free_path(path
);
4436 btrfs_end_transaction(trans
);
4438 "Umm, you don't have the default diritem, this isn't going to work");
4443 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
4444 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
4445 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4446 btrfs_free_path(path
);
4448 btrfs_set_fs_incompat(fs_info
, DEFAULT_SUBVOL
);
4449 btrfs_end_transaction(trans
);
4451 mnt_drop_write_file(file
);
4455 static void get_block_group_info(struct list_head
*groups_list
,
4456 struct btrfs_ioctl_space_info
*space
)
4458 struct btrfs_block_group_cache
*block_group
;
4460 space
->total_bytes
= 0;
4461 space
->used_bytes
= 0;
4463 list_for_each_entry(block_group
, groups_list
, list
) {
4464 space
->flags
= block_group
->flags
;
4465 space
->total_bytes
+= block_group
->key
.offset
;
4466 space
->used_bytes
+=
4467 btrfs_block_group_used(&block_group
->item
);
4471 static long btrfs_ioctl_space_info(struct btrfs_fs_info
*fs_info
,
4474 struct btrfs_ioctl_space_args space_args
;
4475 struct btrfs_ioctl_space_info space
;
4476 struct btrfs_ioctl_space_info
*dest
;
4477 struct btrfs_ioctl_space_info
*dest_orig
;
4478 struct btrfs_ioctl_space_info __user
*user_dest
;
4479 struct btrfs_space_info
*info
;
4480 static const u64 types
[] = {
4481 BTRFS_BLOCK_GROUP_DATA
,
4482 BTRFS_BLOCK_GROUP_SYSTEM
,
4483 BTRFS_BLOCK_GROUP_METADATA
,
4484 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
4492 if (copy_from_user(&space_args
,
4493 (struct btrfs_ioctl_space_args __user
*)arg
,
4494 sizeof(space_args
)))
4497 for (i
= 0; i
< num_types
; i
++) {
4498 struct btrfs_space_info
*tmp
;
4502 list_for_each_entry_rcu(tmp
, &fs_info
->space_info
,
4504 if (tmp
->flags
== types
[i
]) {
4514 down_read(&info
->groups_sem
);
4515 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4516 if (!list_empty(&info
->block_groups
[c
]))
4519 up_read(&info
->groups_sem
);
4523 * Global block reserve, exported as a space_info
4527 /* space_slots == 0 means they are asking for a count */
4528 if (space_args
.space_slots
== 0) {
4529 space_args
.total_spaces
= slot_count
;
4533 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
4535 alloc_size
= sizeof(*dest
) * slot_count
;
4537 /* we generally have at most 6 or so space infos, one for each raid
4538 * level. So, a whole page should be more than enough for everyone
4540 if (alloc_size
> PAGE_SIZE
)
4543 space_args
.total_spaces
= 0;
4544 dest
= kmalloc(alloc_size
, GFP_KERNEL
);
4549 /* now we have a buffer to copy into */
4550 for (i
= 0; i
< num_types
; i
++) {
4551 struct btrfs_space_info
*tmp
;
4558 list_for_each_entry_rcu(tmp
, &fs_info
->space_info
,
4560 if (tmp
->flags
== types
[i
]) {
4569 down_read(&info
->groups_sem
);
4570 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4571 if (!list_empty(&info
->block_groups
[c
])) {
4572 get_block_group_info(&info
->block_groups
[c
],
4574 memcpy(dest
, &space
, sizeof(space
));
4576 space_args
.total_spaces
++;
4582 up_read(&info
->groups_sem
);
4586 * Add global block reserve
4589 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
4591 spin_lock(&block_rsv
->lock
);
4592 space
.total_bytes
= block_rsv
->size
;
4593 space
.used_bytes
= block_rsv
->size
- block_rsv
->reserved
;
4594 spin_unlock(&block_rsv
->lock
);
4595 space
.flags
= BTRFS_SPACE_INFO_GLOBAL_RSV
;
4596 memcpy(dest
, &space
, sizeof(space
));
4597 space_args
.total_spaces
++;
4600 user_dest
= (struct btrfs_ioctl_space_info __user
*)
4601 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
4603 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
4608 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
4614 static noinline
long btrfs_ioctl_start_sync(struct btrfs_root
*root
,
4617 struct btrfs_trans_handle
*trans
;
4621 trans
= btrfs_attach_transaction_barrier(root
);
4622 if (IS_ERR(trans
)) {
4623 if (PTR_ERR(trans
) != -ENOENT
)
4624 return PTR_ERR(trans
);
4626 /* No running transaction, don't bother */
4627 transid
= root
->fs_info
->last_trans_committed
;
4630 transid
= trans
->transid
;
4631 ret
= btrfs_commit_transaction_async(trans
, 0);
4633 btrfs_end_transaction(trans
);
4638 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
4643 static noinline
long btrfs_ioctl_wait_sync(struct btrfs_fs_info
*fs_info
,
4649 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
4652 transid
= 0; /* current trans */
4654 return btrfs_wait_for_commit(fs_info
, transid
);
4657 static long btrfs_ioctl_scrub(struct file
*file
, void __user
*arg
)
4659 struct btrfs_fs_info
*fs_info
= btrfs_sb(file_inode(file
)->i_sb
);
4660 struct btrfs_ioctl_scrub_args
*sa
;
4663 if (!capable(CAP_SYS_ADMIN
))
4666 sa
= memdup_user(arg
, sizeof(*sa
));
4670 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
)) {
4671 ret
= mnt_want_write_file(file
);
4676 ret
= btrfs_scrub_dev(fs_info
, sa
->devid
, sa
->start
, sa
->end
,
4677 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
,
4680 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4683 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
))
4684 mnt_drop_write_file(file
);
4690 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info
*fs_info
)
4692 if (!capable(CAP_SYS_ADMIN
))
4695 return btrfs_scrub_cancel(fs_info
);
4698 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info
*fs_info
,
4701 struct btrfs_ioctl_scrub_args
*sa
;
4704 if (!capable(CAP_SYS_ADMIN
))
4707 sa
= memdup_user(arg
, sizeof(*sa
));
4711 ret
= btrfs_scrub_progress(fs_info
, sa
->devid
, &sa
->progress
);
4713 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4720 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info
*fs_info
,
4723 struct btrfs_ioctl_get_dev_stats
*sa
;
4726 sa
= memdup_user(arg
, sizeof(*sa
));
4730 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
4735 ret
= btrfs_get_dev_stats(fs_info
, sa
);
4737 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4744 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info
*fs_info
,
4747 struct btrfs_ioctl_dev_replace_args
*p
;
4750 if (!capable(CAP_SYS_ADMIN
))
4753 p
= memdup_user(arg
, sizeof(*p
));
4758 case BTRFS_IOCTL_DEV_REPLACE_CMD_START
:
4759 if (sb_rdonly(fs_info
->sb
)) {
4763 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
4764 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
4766 ret
= btrfs_dev_replace_by_ioctl(fs_info
, p
);
4767 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4770 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
:
4771 btrfs_dev_replace_status(fs_info
, p
);
4774 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
:
4775 p
->result
= btrfs_dev_replace_cancel(fs_info
);
4783 if (copy_to_user(arg
, p
, sizeof(*p
)))
4790 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
4796 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
4797 struct inode_fs_paths
*ipath
= NULL
;
4798 struct btrfs_path
*path
;
4800 if (!capable(CAP_DAC_READ_SEARCH
))
4803 path
= btrfs_alloc_path();
4809 ipa
= memdup_user(arg
, sizeof(*ipa
));
4816 size
= min_t(u32
, ipa
->size
, 4096);
4817 ipath
= init_ipath(size
, root
, path
);
4818 if (IS_ERR(ipath
)) {
4819 ret
= PTR_ERR(ipath
);
4824 ret
= paths_from_inode(ipa
->inum
, ipath
);
4828 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
4829 rel_ptr
= ipath
->fspath
->val
[i
] -
4830 (u64
)(unsigned long)ipath
->fspath
->val
;
4831 ipath
->fspath
->val
[i
] = rel_ptr
;
4834 ret
= copy_to_user((void __user
*)(unsigned long)ipa
->fspath
,
4835 ipath
->fspath
, size
);
4842 btrfs_free_path(path
);
4849 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
4851 struct btrfs_data_container
*inodes
= ctx
;
4852 const size_t c
= 3 * sizeof(u64
);
4854 if (inodes
->bytes_left
>= c
) {
4855 inodes
->bytes_left
-= c
;
4856 inodes
->val
[inodes
->elem_cnt
] = inum
;
4857 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
4858 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
4859 inodes
->elem_cnt
+= 3;
4861 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
4862 inodes
->bytes_left
= 0;
4863 inodes
->elem_missed
+= 3;
4869 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info
*fs_info
,
4870 void __user
*arg
, int version
)
4874 struct btrfs_ioctl_logical_ino_args
*loi
;
4875 struct btrfs_data_container
*inodes
= NULL
;
4876 struct btrfs_path
*path
= NULL
;
4879 if (!capable(CAP_SYS_ADMIN
))
4882 loi
= memdup_user(arg
, sizeof(*loi
));
4884 return PTR_ERR(loi
);
4887 ignore_offset
= false;
4888 size
= min_t(u32
, loi
->size
, SZ_64K
);
4890 /* All reserved bits must be 0 for now */
4891 if (memchr_inv(loi
->reserved
, 0, sizeof(loi
->reserved
))) {
4895 /* Only accept flags we have defined so far */
4896 if (loi
->flags
& ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET
)) {
4900 ignore_offset
= loi
->flags
& BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET
;
4901 size
= min_t(u32
, loi
->size
, SZ_16M
);
4904 path
= btrfs_alloc_path();
4910 inodes
= init_data_container(size
);
4911 if (IS_ERR(inodes
)) {
4912 ret
= PTR_ERR(inodes
);
4917 ret
= iterate_inodes_from_logical(loi
->logical
, fs_info
, path
,
4918 build_ino_list
, inodes
, ignore_offset
);
4924 ret
= copy_to_user((void __user
*)(unsigned long)loi
->inodes
, inodes
,
4930 btrfs_free_path(path
);
4938 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
,
4939 struct btrfs_ioctl_balance_args
*bargs
)
4941 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
4943 bargs
->flags
= bctl
->flags
;
4945 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
))
4946 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
4947 if (atomic_read(&fs_info
->balance_pause_req
))
4948 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
4949 if (atomic_read(&fs_info
->balance_cancel_req
))
4950 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
4952 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
4953 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
4954 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
4956 spin_lock(&fs_info
->balance_lock
);
4957 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
4958 spin_unlock(&fs_info
->balance_lock
);
4961 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
4963 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4964 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4965 struct btrfs_ioctl_balance_args
*bargs
;
4966 struct btrfs_balance_control
*bctl
;
4967 bool need_unlock
; /* for mut. excl. ops lock */
4970 if (!capable(CAP_SYS_ADMIN
))
4973 ret
= mnt_want_write_file(file
);
4978 if (!test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
4979 mutex_lock(&fs_info
->balance_mutex
);
4985 * mut. excl. ops lock is locked. Three possibilities:
4986 * (1) some other op is running
4987 * (2) balance is running
4988 * (3) balance is paused -- special case (think resume)
4990 mutex_lock(&fs_info
->balance_mutex
);
4991 if (fs_info
->balance_ctl
) {
4992 /* this is either (2) or (3) */
4993 if (!test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4994 mutex_unlock(&fs_info
->balance_mutex
);
4996 * Lock released to allow other waiters to continue,
4997 * we'll reexamine the status again.
4999 mutex_lock(&fs_info
->balance_mutex
);
5001 if (fs_info
->balance_ctl
&&
5002 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
5004 need_unlock
= false;
5008 mutex_unlock(&fs_info
->balance_mutex
);
5012 mutex_unlock(&fs_info
->balance_mutex
);
5018 mutex_unlock(&fs_info
->balance_mutex
);
5019 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
5024 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
));
5027 bargs
= memdup_user(arg
, sizeof(*bargs
));
5028 if (IS_ERR(bargs
)) {
5029 ret
= PTR_ERR(bargs
);
5033 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
5034 if (!fs_info
->balance_ctl
) {
5039 bctl
= fs_info
->balance_ctl
;
5040 spin_lock(&fs_info
->balance_lock
);
5041 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
5042 spin_unlock(&fs_info
->balance_lock
);
5050 if (fs_info
->balance_ctl
) {
5055 bctl
= kzalloc(sizeof(*bctl
), GFP_KERNEL
);
5062 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
5063 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
5064 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
5066 bctl
->flags
= bargs
->flags
;
5068 /* balance everything - no filters */
5069 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
5072 if (bctl
->flags
& ~(BTRFS_BALANCE_ARGS_MASK
| BTRFS_BALANCE_TYPE_MASK
)) {
5079 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
5080 * btrfs_balance. bctl is freed in reset_balance_state, or, if
5081 * restriper was paused all the way until unmount, in free_fs_info.
5082 * The flag should be cleared after reset_balance_state.
5084 need_unlock
= false;
5086 ret
= btrfs_balance(fs_info
, bctl
, bargs
);
5090 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
5099 mutex_unlock(&fs_info
->balance_mutex
);
5101 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
5103 mnt_drop_write_file(file
);
5107 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info
*fs_info
, int cmd
)
5109 if (!capable(CAP_SYS_ADMIN
))
5113 case BTRFS_BALANCE_CTL_PAUSE
:
5114 return btrfs_pause_balance(fs_info
);
5115 case BTRFS_BALANCE_CTL_CANCEL
:
5116 return btrfs_cancel_balance(fs_info
);
5122 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info
*fs_info
,
5125 struct btrfs_ioctl_balance_args
*bargs
;
5128 if (!capable(CAP_SYS_ADMIN
))
5131 mutex_lock(&fs_info
->balance_mutex
);
5132 if (!fs_info
->balance_ctl
) {
5137 bargs
= kzalloc(sizeof(*bargs
), GFP_KERNEL
);
5143 btrfs_update_ioctl_balance_args(fs_info
, bargs
);
5145 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
5150 mutex_unlock(&fs_info
->balance_mutex
);
5154 static long btrfs_ioctl_quota_ctl(struct file
*file
, void __user
*arg
)
5156 struct inode
*inode
= file_inode(file
);
5157 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5158 struct btrfs_ioctl_quota_ctl_args
*sa
;
5161 if (!capable(CAP_SYS_ADMIN
))
5164 ret
= mnt_want_write_file(file
);
5168 sa
= memdup_user(arg
, sizeof(*sa
));
5174 down_write(&fs_info
->subvol_sem
);
5177 case BTRFS_QUOTA_CTL_ENABLE
:
5178 ret
= btrfs_quota_enable(fs_info
);
5180 case BTRFS_QUOTA_CTL_DISABLE
:
5181 ret
= btrfs_quota_disable(fs_info
);
5189 up_write(&fs_info
->subvol_sem
);
5191 mnt_drop_write_file(file
);
5195 static long btrfs_ioctl_qgroup_assign(struct file
*file
, void __user
*arg
)
5197 struct inode
*inode
= file_inode(file
);
5198 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5199 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5200 struct btrfs_ioctl_qgroup_assign_args
*sa
;
5201 struct btrfs_trans_handle
*trans
;
5205 if (!capable(CAP_SYS_ADMIN
))
5208 ret
= mnt_want_write_file(file
);
5212 sa
= memdup_user(arg
, sizeof(*sa
));
5218 trans
= btrfs_join_transaction(root
);
5219 if (IS_ERR(trans
)) {
5220 ret
= PTR_ERR(trans
);
5225 ret
= btrfs_add_qgroup_relation(trans
, sa
->src
, sa
->dst
);
5227 ret
= btrfs_del_qgroup_relation(trans
, sa
->src
, sa
->dst
);
5230 /* update qgroup status and info */
5231 err
= btrfs_run_qgroups(trans
);
5233 btrfs_handle_fs_error(fs_info
, err
,
5234 "failed to update qgroup status and info");
5235 err
= btrfs_end_transaction(trans
);
5242 mnt_drop_write_file(file
);
5246 static long btrfs_ioctl_qgroup_create(struct file
*file
, void __user
*arg
)
5248 struct inode
*inode
= file_inode(file
);
5249 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5250 struct btrfs_ioctl_qgroup_create_args
*sa
;
5251 struct btrfs_trans_handle
*trans
;
5255 if (!capable(CAP_SYS_ADMIN
))
5258 ret
= mnt_want_write_file(file
);
5262 sa
= memdup_user(arg
, sizeof(*sa
));
5268 if (!sa
->qgroupid
) {
5273 trans
= btrfs_join_transaction(root
);
5274 if (IS_ERR(trans
)) {
5275 ret
= PTR_ERR(trans
);
5280 ret
= btrfs_create_qgroup(trans
, sa
->qgroupid
);
5282 ret
= btrfs_remove_qgroup(trans
, sa
->qgroupid
);
5285 err
= btrfs_end_transaction(trans
);
5292 mnt_drop_write_file(file
);
5296 static long btrfs_ioctl_qgroup_limit(struct file
*file
, void __user
*arg
)
5298 struct inode
*inode
= file_inode(file
);
5299 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5300 struct btrfs_ioctl_qgroup_limit_args
*sa
;
5301 struct btrfs_trans_handle
*trans
;
5306 if (!capable(CAP_SYS_ADMIN
))
5309 ret
= mnt_want_write_file(file
);
5313 sa
= memdup_user(arg
, sizeof(*sa
));
5319 trans
= btrfs_join_transaction(root
);
5320 if (IS_ERR(trans
)) {
5321 ret
= PTR_ERR(trans
);
5325 qgroupid
= sa
->qgroupid
;
5327 /* take the current subvol as qgroup */
5328 qgroupid
= root
->root_key
.objectid
;
5331 ret
= btrfs_limit_qgroup(trans
, qgroupid
, &sa
->lim
);
5333 err
= btrfs_end_transaction(trans
);
5340 mnt_drop_write_file(file
);
5344 static long btrfs_ioctl_quota_rescan(struct file
*file
, void __user
*arg
)
5346 struct inode
*inode
= file_inode(file
);
5347 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5348 struct btrfs_ioctl_quota_rescan_args
*qsa
;
5351 if (!capable(CAP_SYS_ADMIN
))
5354 ret
= mnt_want_write_file(file
);
5358 qsa
= memdup_user(arg
, sizeof(*qsa
));
5369 ret
= btrfs_qgroup_rescan(fs_info
);
5374 mnt_drop_write_file(file
);
5378 static long btrfs_ioctl_quota_rescan_status(struct file
*file
, void __user
*arg
)
5380 struct inode
*inode
= file_inode(file
);
5381 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5382 struct btrfs_ioctl_quota_rescan_args
*qsa
;
5385 if (!capable(CAP_SYS_ADMIN
))
5388 qsa
= kzalloc(sizeof(*qsa
), GFP_KERNEL
);
5392 if (fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
5394 qsa
->progress
= fs_info
->qgroup_rescan_progress
.objectid
;
5397 if (copy_to_user(arg
, qsa
, sizeof(*qsa
)))
5404 static long btrfs_ioctl_quota_rescan_wait(struct file
*file
, void __user
*arg
)
5406 struct inode
*inode
= file_inode(file
);
5407 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5409 if (!capable(CAP_SYS_ADMIN
))
5412 return btrfs_qgroup_wait_for_completion(fs_info
, true);
5415 static long _btrfs_ioctl_set_received_subvol(struct file
*file
,
5416 struct btrfs_ioctl_received_subvol_args
*sa
)
5418 struct inode
*inode
= file_inode(file
);
5419 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5420 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5421 struct btrfs_root_item
*root_item
= &root
->root_item
;
5422 struct btrfs_trans_handle
*trans
;
5423 struct timespec64 ct
= current_time(inode
);
5425 int received_uuid_changed
;
5427 if (!inode_owner_or_capable(inode
))
5430 ret
= mnt_want_write_file(file
);
5434 down_write(&fs_info
->subvol_sem
);
5436 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
5441 if (btrfs_root_readonly(root
)) {
5448 * 2 - uuid items (received uuid + subvol uuid)
5450 trans
= btrfs_start_transaction(root
, 3);
5451 if (IS_ERR(trans
)) {
5452 ret
= PTR_ERR(trans
);
5457 sa
->rtransid
= trans
->transid
;
5458 sa
->rtime
.sec
= ct
.tv_sec
;
5459 sa
->rtime
.nsec
= ct
.tv_nsec
;
5461 received_uuid_changed
= memcmp(root_item
->received_uuid
, sa
->uuid
,
5463 if (received_uuid_changed
&&
5464 !btrfs_is_empty_uuid(root_item
->received_uuid
)) {
5465 ret
= btrfs_uuid_tree_remove(trans
, root_item
->received_uuid
,
5466 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5467 root
->root_key
.objectid
);
5468 if (ret
&& ret
!= -ENOENT
) {
5469 btrfs_abort_transaction(trans
, ret
);
5470 btrfs_end_transaction(trans
);
5474 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
5475 btrfs_set_root_stransid(root_item
, sa
->stransid
);
5476 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
5477 btrfs_set_stack_timespec_sec(&root_item
->stime
, sa
->stime
.sec
);
5478 btrfs_set_stack_timespec_nsec(&root_item
->stime
, sa
->stime
.nsec
);
5479 btrfs_set_stack_timespec_sec(&root_item
->rtime
, sa
->rtime
.sec
);
5480 btrfs_set_stack_timespec_nsec(&root_item
->rtime
, sa
->rtime
.nsec
);
5482 ret
= btrfs_update_root(trans
, fs_info
->tree_root
,
5483 &root
->root_key
, &root
->root_item
);
5485 btrfs_end_transaction(trans
);
5488 if (received_uuid_changed
&& !btrfs_is_empty_uuid(sa
->uuid
)) {
5489 ret
= btrfs_uuid_tree_add(trans
, sa
->uuid
,
5490 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5491 root
->root_key
.objectid
);
5492 if (ret
< 0 && ret
!= -EEXIST
) {
5493 btrfs_abort_transaction(trans
, ret
);
5494 btrfs_end_transaction(trans
);
5498 ret
= btrfs_commit_transaction(trans
);
5500 up_write(&fs_info
->subvol_sem
);
5501 mnt_drop_write_file(file
);
5506 static long btrfs_ioctl_set_received_subvol_32(struct file
*file
,
5509 struct btrfs_ioctl_received_subvol_args_32
*args32
= NULL
;
5510 struct btrfs_ioctl_received_subvol_args
*args64
= NULL
;
5513 args32
= memdup_user(arg
, sizeof(*args32
));
5515 return PTR_ERR(args32
);
5517 args64
= kmalloc(sizeof(*args64
), GFP_KERNEL
);
5523 memcpy(args64
->uuid
, args32
->uuid
, BTRFS_UUID_SIZE
);
5524 args64
->stransid
= args32
->stransid
;
5525 args64
->rtransid
= args32
->rtransid
;
5526 args64
->stime
.sec
= args32
->stime
.sec
;
5527 args64
->stime
.nsec
= args32
->stime
.nsec
;
5528 args64
->rtime
.sec
= args32
->rtime
.sec
;
5529 args64
->rtime
.nsec
= args32
->rtime
.nsec
;
5530 args64
->flags
= args32
->flags
;
5532 ret
= _btrfs_ioctl_set_received_subvol(file
, args64
);
5536 memcpy(args32
->uuid
, args64
->uuid
, BTRFS_UUID_SIZE
);
5537 args32
->stransid
= args64
->stransid
;
5538 args32
->rtransid
= args64
->rtransid
;
5539 args32
->stime
.sec
= args64
->stime
.sec
;
5540 args32
->stime
.nsec
= args64
->stime
.nsec
;
5541 args32
->rtime
.sec
= args64
->rtime
.sec
;
5542 args32
->rtime
.nsec
= args64
->rtime
.nsec
;
5543 args32
->flags
= args64
->flags
;
5545 ret
= copy_to_user(arg
, args32
, sizeof(*args32
));
5556 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
5559 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
5562 sa
= memdup_user(arg
, sizeof(*sa
));
5566 ret
= _btrfs_ioctl_set_received_subvol(file
, sa
);
5571 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
5580 static int btrfs_ioctl_get_fslabel(struct file
*file
, void __user
*arg
)
5582 struct inode
*inode
= file_inode(file
);
5583 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5586 char label
[BTRFS_LABEL_SIZE
];
5588 spin_lock(&fs_info
->super_lock
);
5589 memcpy(label
, fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
5590 spin_unlock(&fs_info
->super_lock
);
5592 len
= strnlen(label
, BTRFS_LABEL_SIZE
);
5594 if (len
== BTRFS_LABEL_SIZE
) {
5596 "label is too long, return the first %zu bytes",
5600 ret
= copy_to_user(arg
, label
, len
);
5602 return ret
? -EFAULT
: 0;
5605 static int btrfs_ioctl_set_fslabel(struct file
*file
, void __user
*arg
)
5607 struct inode
*inode
= file_inode(file
);
5608 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5609 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5610 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5611 struct btrfs_trans_handle
*trans
;
5612 char label
[BTRFS_LABEL_SIZE
];
5615 if (!capable(CAP_SYS_ADMIN
))
5618 if (copy_from_user(label
, arg
, sizeof(label
)))
5621 if (strnlen(label
, BTRFS_LABEL_SIZE
) == BTRFS_LABEL_SIZE
) {
5623 "unable to set label with more than %d bytes",
5624 BTRFS_LABEL_SIZE
- 1);
5628 ret
= mnt_want_write_file(file
);
5632 trans
= btrfs_start_transaction(root
, 0);
5633 if (IS_ERR(trans
)) {
5634 ret
= PTR_ERR(trans
);
5638 spin_lock(&fs_info
->super_lock
);
5639 strcpy(super_block
->label
, label
);
5640 spin_unlock(&fs_info
->super_lock
);
5641 ret
= btrfs_commit_transaction(trans
);
5644 mnt_drop_write_file(file
);
5648 #define INIT_FEATURE_FLAGS(suffix) \
5649 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5650 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5651 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5653 int btrfs_ioctl_get_supported_features(void __user
*arg
)
5655 static const struct btrfs_ioctl_feature_flags features
[3] = {
5656 INIT_FEATURE_FLAGS(SUPP
),
5657 INIT_FEATURE_FLAGS(SAFE_SET
),
5658 INIT_FEATURE_FLAGS(SAFE_CLEAR
)
5661 if (copy_to_user(arg
, &features
, sizeof(features
)))
5667 static int btrfs_ioctl_get_features(struct file
*file
, void __user
*arg
)
5669 struct inode
*inode
= file_inode(file
);
5670 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5671 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5672 struct btrfs_ioctl_feature_flags features
;
5674 features
.compat_flags
= btrfs_super_compat_flags(super_block
);
5675 features
.compat_ro_flags
= btrfs_super_compat_ro_flags(super_block
);
5676 features
.incompat_flags
= btrfs_super_incompat_flags(super_block
);
5678 if (copy_to_user(arg
, &features
, sizeof(features
)))
5684 static int check_feature_bits(struct btrfs_fs_info
*fs_info
,
5685 enum btrfs_feature_set set
,
5686 u64 change_mask
, u64 flags
, u64 supported_flags
,
5687 u64 safe_set
, u64 safe_clear
)
5689 const char *type
= btrfs_feature_set_names
[set
];
5691 u64 disallowed
, unsupported
;
5692 u64 set_mask
= flags
& change_mask
;
5693 u64 clear_mask
= ~flags
& change_mask
;
5695 unsupported
= set_mask
& ~supported_flags
;
5697 names
= btrfs_printable_features(set
, unsupported
);
5700 "this kernel does not support the %s feature bit%s",
5701 names
, strchr(names
, ',') ? "s" : "");
5705 "this kernel does not support %s bits 0x%llx",
5710 disallowed
= set_mask
& ~safe_set
;
5712 names
= btrfs_printable_features(set
, disallowed
);
5715 "can't set the %s feature bit%s while mounted",
5716 names
, strchr(names
, ',') ? "s" : "");
5720 "can't set %s bits 0x%llx while mounted",
5725 disallowed
= clear_mask
& ~safe_clear
;
5727 names
= btrfs_printable_features(set
, disallowed
);
5730 "can't clear the %s feature bit%s while mounted",
5731 names
, strchr(names
, ',') ? "s" : "");
5735 "can't clear %s bits 0x%llx while mounted",
5743 #define check_feature(fs_info, change_mask, flags, mask_base) \
5744 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
5745 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5746 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5747 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5749 static int btrfs_ioctl_set_features(struct file
*file
, void __user
*arg
)
5751 struct inode
*inode
= file_inode(file
);
5752 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5753 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5754 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5755 struct btrfs_ioctl_feature_flags flags
[2];
5756 struct btrfs_trans_handle
*trans
;
5760 if (!capable(CAP_SYS_ADMIN
))
5763 if (copy_from_user(flags
, arg
, sizeof(flags
)))
5767 if (!flags
[0].compat_flags
&& !flags
[0].compat_ro_flags
&&
5768 !flags
[0].incompat_flags
)
5771 ret
= check_feature(fs_info
, flags
[0].compat_flags
,
5772 flags
[1].compat_flags
, COMPAT
);
5776 ret
= check_feature(fs_info
, flags
[0].compat_ro_flags
,
5777 flags
[1].compat_ro_flags
, COMPAT_RO
);
5781 ret
= check_feature(fs_info
, flags
[0].incompat_flags
,
5782 flags
[1].incompat_flags
, INCOMPAT
);
5786 ret
= mnt_want_write_file(file
);
5790 trans
= btrfs_start_transaction(root
, 0);
5791 if (IS_ERR(trans
)) {
5792 ret
= PTR_ERR(trans
);
5793 goto out_drop_write
;
5796 spin_lock(&fs_info
->super_lock
);
5797 newflags
= btrfs_super_compat_flags(super_block
);
5798 newflags
|= flags
[0].compat_flags
& flags
[1].compat_flags
;
5799 newflags
&= ~(flags
[0].compat_flags
& ~flags
[1].compat_flags
);
5800 btrfs_set_super_compat_flags(super_block
, newflags
);
5802 newflags
= btrfs_super_compat_ro_flags(super_block
);
5803 newflags
|= flags
[0].compat_ro_flags
& flags
[1].compat_ro_flags
;
5804 newflags
&= ~(flags
[0].compat_ro_flags
& ~flags
[1].compat_ro_flags
);
5805 btrfs_set_super_compat_ro_flags(super_block
, newflags
);
5807 newflags
= btrfs_super_incompat_flags(super_block
);
5808 newflags
|= flags
[0].incompat_flags
& flags
[1].incompat_flags
;
5809 newflags
&= ~(flags
[0].incompat_flags
& ~flags
[1].incompat_flags
);
5810 btrfs_set_super_incompat_flags(super_block
, newflags
);
5811 spin_unlock(&fs_info
->super_lock
);
5813 ret
= btrfs_commit_transaction(trans
);
5815 mnt_drop_write_file(file
);
5820 static int _btrfs_ioctl_send(struct file
*file
, void __user
*argp
, bool compat
)
5822 struct btrfs_ioctl_send_args
*arg
;
5826 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5827 struct btrfs_ioctl_send_args_32 args32
;
5829 ret
= copy_from_user(&args32
, argp
, sizeof(args32
));
5832 arg
= kzalloc(sizeof(*arg
), GFP_KERNEL
);
5835 arg
->send_fd
= args32
.send_fd
;
5836 arg
->clone_sources_count
= args32
.clone_sources_count
;
5837 arg
->clone_sources
= compat_ptr(args32
.clone_sources
);
5838 arg
->parent_root
= args32
.parent_root
;
5839 arg
->flags
= args32
.flags
;
5840 memcpy(arg
->reserved
, args32
.reserved
,
5841 sizeof(args32
.reserved
));
5846 arg
= memdup_user(argp
, sizeof(*arg
));
5848 return PTR_ERR(arg
);
5850 ret
= btrfs_ioctl_send(file
, arg
);
5855 long btrfs_ioctl(struct file
*file
, unsigned int
5856 cmd
, unsigned long arg
)
5858 struct inode
*inode
= file_inode(file
);
5859 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5860 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5861 void __user
*argp
= (void __user
*)arg
;
5864 case FS_IOC_GETFLAGS
:
5865 return btrfs_ioctl_getflags(file
, argp
);
5866 case FS_IOC_SETFLAGS
:
5867 return btrfs_ioctl_setflags(file
, argp
);
5868 case FS_IOC_GETVERSION
:
5869 return btrfs_ioctl_getversion(file
, argp
);
5871 return btrfs_ioctl_fitrim(file
, argp
);
5872 case BTRFS_IOC_SNAP_CREATE
:
5873 return btrfs_ioctl_snap_create(file
, argp
, 0);
5874 case BTRFS_IOC_SNAP_CREATE_V2
:
5875 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
5876 case BTRFS_IOC_SUBVOL_CREATE
:
5877 return btrfs_ioctl_snap_create(file
, argp
, 1);
5878 case BTRFS_IOC_SUBVOL_CREATE_V2
:
5879 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
5880 case BTRFS_IOC_SNAP_DESTROY
:
5881 return btrfs_ioctl_snap_destroy(file
, argp
);
5882 case BTRFS_IOC_SUBVOL_GETFLAGS
:
5883 return btrfs_ioctl_subvol_getflags(file
, argp
);
5884 case BTRFS_IOC_SUBVOL_SETFLAGS
:
5885 return btrfs_ioctl_subvol_setflags(file
, argp
);
5886 case BTRFS_IOC_DEFAULT_SUBVOL
:
5887 return btrfs_ioctl_default_subvol(file
, argp
);
5888 case BTRFS_IOC_DEFRAG
:
5889 return btrfs_ioctl_defrag(file
, NULL
);
5890 case BTRFS_IOC_DEFRAG_RANGE
:
5891 return btrfs_ioctl_defrag(file
, argp
);
5892 case BTRFS_IOC_RESIZE
:
5893 return btrfs_ioctl_resize(file
, argp
);
5894 case BTRFS_IOC_ADD_DEV
:
5895 return btrfs_ioctl_add_dev(fs_info
, argp
);
5896 case BTRFS_IOC_RM_DEV
:
5897 return btrfs_ioctl_rm_dev(file
, argp
);
5898 case BTRFS_IOC_RM_DEV_V2
:
5899 return btrfs_ioctl_rm_dev_v2(file
, argp
);
5900 case BTRFS_IOC_FS_INFO
:
5901 return btrfs_ioctl_fs_info(fs_info
, argp
);
5902 case BTRFS_IOC_DEV_INFO
:
5903 return btrfs_ioctl_dev_info(fs_info
, argp
);
5904 case BTRFS_IOC_BALANCE
:
5905 return btrfs_ioctl_balance(file
, NULL
);
5906 case BTRFS_IOC_TREE_SEARCH
:
5907 return btrfs_ioctl_tree_search(file
, argp
);
5908 case BTRFS_IOC_TREE_SEARCH_V2
:
5909 return btrfs_ioctl_tree_search_v2(file
, argp
);
5910 case BTRFS_IOC_INO_LOOKUP
:
5911 return btrfs_ioctl_ino_lookup(file
, argp
);
5912 case BTRFS_IOC_INO_PATHS
:
5913 return btrfs_ioctl_ino_to_path(root
, argp
);
5914 case BTRFS_IOC_LOGICAL_INO
:
5915 return btrfs_ioctl_logical_to_ino(fs_info
, argp
, 1);
5916 case BTRFS_IOC_LOGICAL_INO_V2
:
5917 return btrfs_ioctl_logical_to_ino(fs_info
, argp
, 2);
5918 case BTRFS_IOC_SPACE_INFO
:
5919 return btrfs_ioctl_space_info(fs_info
, argp
);
5920 case BTRFS_IOC_SYNC
: {
5923 ret
= btrfs_start_delalloc_roots(fs_info
, -1);
5926 ret
= btrfs_sync_fs(inode
->i_sb
, 1);
5928 * The transaction thread may want to do more work,
5929 * namely it pokes the cleaner kthread that will start
5930 * processing uncleaned subvols.
5932 wake_up_process(fs_info
->transaction_kthread
);
5935 case BTRFS_IOC_START_SYNC
:
5936 return btrfs_ioctl_start_sync(root
, argp
);
5937 case BTRFS_IOC_WAIT_SYNC
:
5938 return btrfs_ioctl_wait_sync(fs_info
, argp
);
5939 case BTRFS_IOC_SCRUB
:
5940 return btrfs_ioctl_scrub(file
, argp
);
5941 case BTRFS_IOC_SCRUB_CANCEL
:
5942 return btrfs_ioctl_scrub_cancel(fs_info
);
5943 case BTRFS_IOC_SCRUB_PROGRESS
:
5944 return btrfs_ioctl_scrub_progress(fs_info
, argp
);
5945 case BTRFS_IOC_BALANCE_V2
:
5946 return btrfs_ioctl_balance(file
, argp
);
5947 case BTRFS_IOC_BALANCE_CTL
:
5948 return btrfs_ioctl_balance_ctl(fs_info
, arg
);
5949 case BTRFS_IOC_BALANCE_PROGRESS
:
5950 return btrfs_ioctl_balance_progress(fs_info
, argp
);
5951 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
5952 return btrfs_ioctl_set_received_subvol(file
, argp
);
5954 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32
:
5955 return btrfs_ioctl_set_received_subvol_32(file
, argp
);
5957 case BTRFS_IOC_SEND
:
5958 return _btrfs_ioctl_send(file
, argp
, false);
5959 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5960 case BTRFS_IOC_SEND_32
:
5961 return _btrfs_ioctl_send(file
, argp
, true);
5963 case BTRFS_IOC_GET_DEV_STATS
:
5964 return btrfs_ioctl_get_dev_stats(fs_info
, argp
);
5965 case BTRFS_IOC_QUOTA_CTL
:
5966 return btrfs_ioctl_quota_ctl(file
, argp
);
5967 case BTRFS_IOC_QGROUP_ASSIGN
:
5968 return btrfs_ioctl_qgroup_assign(file
, argp
);
5969 case BTRFS_IOC_QGROUP_CREATE
:
5970 return btrfs_ioctl_qgroup_create(file
, argp
);
5971 case BTRFS_IOC_QGROUP_LIMIT
:
5972 return btrfs_ioctl_qgroup_limit(file
, argp
);
5973 case BTRFS_IOC_QUOTA_RESCAN
:
5974 return btrfs_ioctl_quota_rescan(file
, argp
);
5975 case BTRFS_IOC_QUOTA_RESCAN_STATUS
:
5976 return btrfs_ioctl_quota_rescan_status(file
, argp
);
5977 case BTRFS_IOC_QUOTA_RESCAN_WAIT
:
5978 return btrfs_ioctl_quota_rescan_wait(file
, argp
);
5979 case BTRFS_IOC_DEV_REPLACE
:
5980 return btrfs_ioctl_dev_replace(fs_info
, argp
);
5981 case BTRFS_IOC_GET_FSLABEL
:
5982 return btrfs_ioctl_get_fslabel(file
, argp
);
5983 case BTRFS_IOC_SET_FSLABEL
:
5984 return btrfs_ioctl_set_fslabel(file
, argp
);
5985 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
5986 return btrfs_ioctl_get_supported_features(argp
);
5987 case BTRFS_IOC_GET_FEATURES
:
5988 return btrfs_ioctl_get_features(file
, argp
);
5989 case BTRFS_IOC_SET_FEATURES
:
5990 return btrfs_ioctl_set_features(file
, argp
);
5991 case FS_IOC_FSGETXATTR
:
5992 return btrfs_ioctl_fsgetxattr(file
, argp
);
5993 case FS_IOC_FSSETXATTR
:
5994 return btrfs_ioctl_fssetxattr(file
, argp
);
5995 case BTRFS_IOC_GET_SUBVOL_INFO
:
5996 return btrfs_ioctl_get_subvol_info(file
, argp
);
5997 case BTRFS_IOC_GET_SUBVOL_ROOTREF
:
5998 return btrfs_ioctl_get_subvol_rootref(file
, argp
);
5999 case BTRFS_IOC_INO_LOOKUP_USER
:
6000 return btrfs_ioctl_ino_lookup_user(file
, argp
);
6006 #ifdef CONFIG_COMPAT
6007 long btrfs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
6010 * These all access 32-bit values anyway so no further
6011 * handling is necessary.
6014 case FS_IOC32_GETFLAGS
:
6015 cmd
= FS_IOC_GETFLAGS
;
6017 case FS_IOC32_SETFLAGS
:
6018 cmd
= FS_IOC_SETFLAGS
;
6020 case FS_IOC32_GETVERSION
:
6021 cmd
= FS_IOC_GETVERSION
;
6025 return btrfs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));