1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/kernel.h>
8 #include <linux/buffer_head.h>
9 #include <linux/file.h>
11 #include <linux/fsnotify.h>
12 #include <linux/pagemap.h>
13 #include <linux/highmem.h>
14 #include <linux/time.h>
15 #include <linux/init.h>
16 #include <linux/string.h>
17 #include <linux/backing-dev.h>
18 #include <linux/mount.h>
19 #include <linux/mpage.h>
20 #include <linux/namei.h>
21 #include <linux/swap.h>
22 #include <linux/writeback.h>
23 #include <linux/compat.h>
24 #include <linux/bit_spinlock.h>
25 #include <linux/security.h>
26 #include <linux/xattr.h>
28 #include <linux/slab.h>
29 #include <linux/blkdev.h>
30 #include <linux/uuid.h>
31 #include <linux/btrfs.h>
32 #include <linux/uaccess.h>
33 #include <linux/iversion.h>
36 #include "transaction.h"
37 #include "btrfs_inode.h"
38 #include "print-tree.h"
41 #include "inode-map.h"
43 #include "rcu-string.h"
45 #include "dev-replace.h"
50 #include "compression.h"
53 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
54 * structures are incorrect, as the timespec structure from userspace
55 * is 4 bytes too small. We define these alternatives here to teach
56 * the kernel about the 32-bit struct packing.
58 struct btrfs_ioctl_timespec_32
{
61 } __attribute__ ((__packed__
));
63 struct btrfs_ioctl_received_subvol_args_32
{
64 char uuid
[BTRFS_UUID_SIZE
]; /* in */
65 __u64 stransid
; /* in */
66 __u64 rtransid
; /* out */
67 struct btrfs_ioctl_timespec_32 stime
; /* in */
68 struct btrfs_ioctl_timespec_32 rtime
; /* out */
70 __u64 reserved
[16]; /* in */
71 } __attribute__ ((__packed__
));
73 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
74 struct btrfs_ioctl_received_subvol_args_32)
77 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
78 struct btrfs_ioctl_send_args_32
{
79 __s64 send_fd
; /* in */
80 __u64 clone_sources_count
; /* in */
81 compat_uptr_t clone_sources
; /* in */
82 __u64 parent_root
; /* in */
84 __u64 reserved
[4]; /* in */
85 } __attribute__ ((__packed__
));
87 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
88 struct btrfs_ioctl_send_args_32)
91 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
92 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
,
95 /* Mask out flags that are inappropriate for the given type of inode. */
96 static unsigned int btrfs_mask_fsflags_for_type(struct inode
*inode
,
99 if (S_ISDIR(inode
->i_mode
))
101 else if (S_ISREG(inode
->i_mode
))
102 return flags
& ~FS_DIRSYNC_FL
;
104 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
108 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
111 static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags
)
113 unsigned int iflags
= 0;
115 if (flags
& BTRFS_INODE_SYNC
)
116 iflags
|= FS_SYNC_FL
;
117 if (flags
& BTRFS_INODE_IMMUTABLE
)
118 iflags
|= FS_IMMUTABLE_FL
;
119 if (flags
& BTRFS_INODE_APPEND
)
120 iflags
|= FS_APPEND_FL
;
121 if (flags
& BTRFS_INODE_NODUMP
)
122 iflags
|= FS_NODUMP_FL
;
123 if (flags
& BTRFS_INODE_NOATIME
)
124 iflags
|= FS_NOATIME_FL
;
125 if (flags
& BTRFS_INODE_DIRSYNC
)
126 iflags
|= FS_DIRSYNC_FL
;
127 if (flags
& BTRFS_INODE_NODATACOW
)
128 iflags
|= FS_NOCOW_FL
;
130 if (flags
& BTRFS_INODE_NOCOMPRESS
)
131 iflags
|= FS_NOCOMP_FL
;
132 else if (flags
& BTRFS_INODE_COMPRESS
)
133 iflags
|= FS_COMPR_FL
;
139 * Update inode->i_flags based on the btrfs internal flags.
141 void btrfs_sync_inode_flags_to_i_flags(struct inode
*inode
)
143 struct btrfs_inode
*binode
= BTRFS_I(inode
);
144 unsigned int new_fl
= 0;
146 if (binode
->flags
& BTRFS_INODE_SYNC
)
148 if (binode
->flags
& BTRFS_INODE_IMMUTABLE
)
149 new_fl
|= S_IMMUTABLE
;
150 if (binode
->flags
& BTRFS_INODE_APPEND
)
152 if (binode
->flags
& BTRFS_INODE_NOATIME
)
154 if (binode
->flags
& BTRFS_INODE_DIRSYNC
)
157 set_mask_bits(&inode
->i_flags
,
158 S_SYNC
| S_APPEND
| S_IMMUTABLE
| S_NOATIME
| S_DIRSYNC
,
162 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
164 struct btrfs_inode
*binode
= BTRFS_I(file_inode(file
));
165 unsigned int flags
= btrfs_inode_flags_to_fsflags(binode
->flags
);
167 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
172 /* Check if @flags are a supported and valid set of FS_*_FL flags */
173 static int check_fsflags(unsigned int flags
)
175 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
176 FS_NOATIME_FL
| FS_NODUMP_FL
| \
177 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
178 FS_NOCOMP_FL
| FS_COMPR_FL
|
182 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
188 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
190 struct inode
*inode
= file_inode(file
);
191 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
192 struct btrfs_inode
*binode
= BTRFS_I(inode
);
193 struct btrfs_root
*root
= binode
->root
;
194 struct btrfs_trans_handle
*trans
;
195 unsigned int fsflags
, old_fsflags
;
198 unsigned int old_i_flags
;
201 if (!inode_owner_or_capable(inode
))
204 if (btrfs_root_readonly(root
))
207 if (copy_from_user(&fsflags
, arg
, sizeof(fsflags
)))
210 ret
= check_fsflags(fsflags
);
214 ret
= mnt_want_write_file(file
);
220 old_flags
= binode
->flags
;
221 old_i_flags
= inode
->i_flags
;
222 mode
= inode
->i_mode
;
224 fsflags
= btrfs_mask_fsflags_for_type(inode
, fsflags
);
225 old_fsflags
= btrfs_inode_flags_to_fsflags(binode
->flags
);
226 if ((fsflags
^ old_fsflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
227 if (!capable(CAP_LINUX_IMMUTABLE
)) {
233 if (fsflags
& FS_SYNC_FL
)
234 binode
->flags
|= BTRFS_INODE_SYNC
;
236 binode
->flags
&= ~BTRFS_INODE_SYNC
;
237 if (fsflags
& FS_IMMUTABLE_FL
)
238 binode
->flags
|= BTRFS_INODE_IMMUTABLE
;
240 binode
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
241 if (fsflags
& FS_APPEND_FL
)
242 binode
->flags
|= BTRFS_INODE_APPEND
;
244 binode
->flags
&= ~BTRFS_INODE_APPEND
;
245 if (fsflags
& FS_NODUMP_FL
)
246 binode
->flags
|= BTRFS_INODE_NODUMP
;
248 binode
->flags
&= ~BTRFS_INODE_NODUMP
;
249 if (fsflags
& FS_NOATIME_FL
)
250 binode
->flags
|= BTRFS_INODE_NOATIME
;
252 binode
->flags
&= ~BTRFS_INODE_NOATIME
;
253 if (fsflags
& FS_DIRSYNC_FL
)
254 binode
->flags
|= BTRFS_INODE_DIRSYNC
;
256 binode
->flags
&= ~BTRFS_INODE_DIRSYNC
;
257 if (fsflags
& FS_NOCOW_FL
) {
260 * It's safe to turn csums off here, no extents exist.
261 * Otherwise we want the flag to reflect the real COW
262 * status of the file and will not set it.
264 if (inode
->i_size
== 0)
265 binode
->flags
|= BTRFS_INODE_NODATACOW
266 | BTRFS_INODE_NODATASUM
;
268 binode
->flags
|= BTRFS_INODE_NODATACOW
;
272 * Revert back under same assumptions as above
275 if (inode
->i_size
== 0)
276 binode
->flags
&= ~(BTRFS_INODE_NODATACOW
277 | BTRFS_INODE_NODATASUM
);
279 binode
->flags
&= ~BTRFS_INODE_NODATACOW
;
284 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
285 * flag may be changed automatically if compression code won't make
288 if (fsflags
& FS_NOCOMP_FL
) {
289 binode
->flags
&= ~BTRFS_INODE_COMPRESS
;
290 binode
->flags
|= BTRFS_INODE_NOCOMPRESS
;
292 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
293 if (ret
&& ret
!= -ENODATA
)
295 } else if (fsflags
& FS_COMPR_FL
) {
298 binode
->flags
|= BTRFS_INODE_COMPRESS
;
299 binode
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
301 comp
= btrfs_compress_type2str(fs_info
->compress_type
);
302 if (!comp
|| comp
[0] == 0)
303 comp
= btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB
);
305 ret
= btrfs_set_prop(inode
, "btrfs.compression",
306 comp
, strlen(comp
), 0);
311 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
312 if (ret
&& ret
!= -ENODATA
)
314 binode
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
317 trans
= btrfs_start_transaction(root
, 1);
319 ret
= PTR_ERR(trans
);
323 btrfs_sync_inode_flags_to_i_flags(inode
);
324 inode_inc_iversion(inode
);
325 inode
->i_ctime
= current_time(inode
);
326 ret
= btrfs_update_inode(trans
, root
, inode
);
328 btrfs_end_transaction(trans
);
331 binode
->flags
= old_flags
;
332 inode
->i_flags
= old_i_flags
;
337 mnt_drop_write_file(file
);
342 * Translate btrfs internal inode flags to xflags as expected by the
343 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
346 static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags
)
348 unsigned int xflags
= 0;
350 if (flags
& BTRFS_INODE_APPEND
)
351 xflags
|= FS_XFLAG_APPEND
;
352 if (flags
& BTRFS_INODE_IMMUTABLE
)
353 xflags
|= FS_XFLAG_IMMUTABLE
;
354 if (flags
& BTRFS_INODE_NOATIME
)
355 xflags
|= FS_XFLAG_NOATIME
;
356 if (flags
& BTRFS_INODE_NODUMP
)
357 xflags
|= FS_XFLAG_NODUMP
;
358 if (flags
& BTRFS_INODE_SYNC
)
359 xflags
|= FS_XFLAG_SYNC
;
364 /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
365 static int check_xflags(unsigned int flags
)
367 if (flags
& ~(FS_XFLAG_APPEND
| FS_XFLAG_IMMUTABLE
| FS_XFLAG_NOATIME
|
368 FS_XFLAG_NODUMP
| FS_XFLAG_SYNC
))
374 * Set the xflags from the internal inode flags. The remaining items of fsxattr
377 static int btrfs_ioctl_fsgetxattr(struct file
*file
, void __user
*arg
)
379 struct btrfs_inode
*binode
= BTRFS_I(file_inode(file
));
382 memset(&fa
, 0, sizeof(fa
));
383 fa
.fsx_xflags
= btrfs_inode_flags_to_xflags(binode
->flags
);
385 if (copy_to_user(arg
, &fa
, sizeof(fa
)))
391 static int btrfs_ioctl_fssetxattr(struct file
*file
, void __user
*arg
)
393 struct inode
*inode
= file_inode(file
);
394 struct btrfs_inode
*binode
= BTRFS_I(inode
);
395 struct btrfs_root
*root
= binode
->root
;
396 struct btrfs_trans_handle
*trans
;
399 unsigned old_i_flags
;
402 if (!inode_owner_or_capable(inode
))
405 if (btrfs_root_readonly(root
))
408 memset(&fa
, 0, sizeof(fa
));
409 if (copy_from_user(&fa
, arg
, sizeof(fa
)))
412 ret
= check_xflags(fa
.fsx_xflags
);
416 if (fa
.fsx_extsize
!= 0 || fa
.fsx_projid
!= 0 || fa
.fsx_cowextsize
!= 0)
419 ret
= mnt_want_write_file(file
);
425 old_flags
= binode
->flags
;
426 old_i_flags
= inode
->i_flags
;
428 /* We need the capabilities to change append-only or immutable inode */
429 if (((old_flags
& (BTRFS_INODE_APPEND
| BTRFS_INODE_IMMUTABLE
)) ||
430 (fa
.fsx_xflags
& (FS_XFLAG_APPEND
| FS_XFLAG_IMMUTABLE
))) &&
431 !capable(CAP_LINUX_IMMUTABLE
)) {
436 if (fa
.fsx_xflags
& FS_XFLAG_SYNC
)
437 binode
->flags
|= BTRFS_INODE_SYNC
;
439 binode
->flags
&= ~BTRFS_INODE_SYNC
;
440 if (fa
.fsx_xflags
& FS_XFLAG_IMMUTABLE
)
441 binode
->flags
|= BTRFS_INODE_IMMUTABLE
;
443 binode
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
444 if (fa
.fsx_xflags
& FS_XFLAG_APPEND
)
445 binode
->flags
|= BTRFS_INODE_APPEND
;
447 binode
->flags
&= ~BTRFS_INODE_APPEND
;
448 if (fa
.fsx_xflags
& FS_XFLAG_NODUMP
)
449 binode
->flags
|= BTRFS_INODE_NODUMP
;
451 binode
->flags
&= ~BTRFS_INODE_NODUMP
;
452 if (fa
.fsx_xflags
& FS_XFLAG_NOATIME
)
453 binode
->flags
|= BTRFS_INODE_NOATIME
;
455 binode
->flags
&= ~BTRFS_INODE_NOATIME
;
457 /* 1 item for the inode */
458 trans
= btrfs_start_transaction(root
, 1);
460 ret
= PTR_ERR(trans
);
464 btrfs_sync_inode_flags_to_i_flags(inode
);
465 inode_inc_iversion(inode
);
466 inode
->i_ctime
= current_time(inode
);
467 ret
= btrfs_update_inode(trans
, root
, inode
);
469 btrfs_end_transaction(trans
);
473 binode
->flags
= old_flags
;
474 inode
->i_flags
= old_i_flags
;
478 mnt_drop_write_file(file
);
483 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
485 struct inode
*inode
= file_inode(file
);
487 return put_user(inode
->i_generation
, arg
);
490 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
492 struct inode
*inode
= file_inode(file
);
493 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
494 struct btrfs_device
*device
;
495 struct request_queue
*q
;
496 struct fstrim_range range
;
497 u64 minlen
= ULLONG_MAX
;
499 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
502 if (!capable(CAP_SYS_ADMIN
))
506 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
510 q
= bdev_get_queue(device
->bdev
);
511 if (blk_queue_discard(q
)) {
513 minlen
= min_t(u64
, q
->limits
.discard_granularity
,
521 if (copy_from_user(&range
, arg
, sizeof(range
)))
523 if (range
.start
> total_bytes
||
524 range
.len
< fs_info
->sb
->s_blocksize
)
527 range
.len
= min(range
.len
, total_bytes
- range
.start
);
528 range
.minlen
= max(range
.minlen
, minlen
);
529 ret
= btrfs_trim_fs(fs_info
, &range
);
533 if (copy_to_user(arg
, &range
, sizeof(range
)))
539 int btrfs_is_empty_uuid(u8
*uuid
)
543 for (i
= 0; i
< BTRFS_UUID_SIZE
; i
++) {
550 static noinline
int create_subvol(struct inode
*dir
,
551 struct dentry
*dentry
,
552 const char *name
, int namelen
,
554 struct btrfs_qgroup_inherit
*inherit
)
556 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
557 struct btrfs_trans_handle
*trans
;
558 struct btrfs_key key
;
559 struct btrfs_root_item
*root_item
;
560 struct btrfs_inode_item
*inode_item
;
561 struct extent_buffer
*leaf
;
562 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
563 struct btrfs_root
*new_root
;
564 struct btrfs_block_rsv block_rsv
;
565 struct timespec64 cur_time
= current_time(dir
);
570 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
574 root_item
= kzalloc(sizeof(*root_item
), GFP_KERNEL
);
578 ret
= btrfs_find_free_objectid(fs_info
->tree_root
, &objectid
);
583 * Don't create subvolume whose level is not zero. Or qgroup will be
584 * screwed up since it assumes subvolume qgroup's level to be 0.
586 if (btrfs_qgroup_level(objectid
)) {
591 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
593 * The same as the snapshot creation, please see the comment
594 * of create_snapshot().
596 ret
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
, 8, false);
600 trans
= btrfs_start_transaction(root
, 0);
602 ret
= PTR_ERR(trans
);
603 btrfs_subvolume_release_metadata(fs_info
, &block_rsv
);
606 trans
->block_rsv
= &block_rsv
;
607 trans
->bytes_reserved
= block_rsv
.size
;
609 ret
= btrfs_qgroup_inherit(trans
, fs_info
, 0, objectid
, inherit
);
613 leaf
= btrfs_alloc_tree_block(trans
, root
, 0, objectid
, NULL
, 0, 0, 0);
619 memzero_extent_buffer(leaf
, 0, sizeof(struct btrfs_header
));
620 btrfs_set_header_bytenr(leaf
, leaf
->start
);
621 btrfs_set_header_generation(leaf
, trans
->transid
);
622 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
623 btrfs_set_header_owner(leaf
, objectid
);
625 write_extent_buffer_fsid(leaf
, fs_info
->fsid
);
626 write_extent_buffer_chunk_tree_uuid(leaf
, fs_info
->chunk_tree_uuid
);
627 btrfs_mark_buffer_dirty(leaf
);
629 inode_item
= &root_item
->inode
;
630 btrfs_set_stack_inode_generation(inode_item
, 1);
631 btrfs_set_stack_inode_size(inode_item
, 3);
632 btrfs_set_stack_inode_nlink(inode_item
, 1);
633 btrfs_set_stack_inode_nbytes(inode_item
,
635 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
637 btrfs_set_root_flags(root_item
, 0);
638 btrfs_set_root_limit(root_item
, 0);
639 btrfs_set_stack_inode_flags(inode_item
, BTRFS_INODE_ROOT_ITEM_INIT
);
641 btrfs_set_root_bytenr(root_item
, leaf
->start
);
642 btrfs_set_root_generation(root_item
, trans
->transid
);
643 btrfs_set_root_level(root_item
, 0);
644 btrfs_set_root_refs(root_item
, 1);
645 btrfs_set_root_used(root_item
, leaf
->len
);
646 btrfs_set_root_last_snapshot(root_item
, 0);
648 btrfs_set_root_generation_v2(root_item
,
649 btrfs_root_generation(root_item
));
650 uuid_le_gen(&new_uuid
);
651 memcpy(root_item
->uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
652 btrfs_set_stack_timespec_sec(&root_item
->otime
, cur_time
.tv_sec
);
653 btrfs_set_stack_timespec_nsec(&root_item
->otime
, cur_time
.tv_nsec
);
654 root_item
->ctime
= root_item
->otime
;
655 btrfs_set_root_ctransid(root_item
, trans
->transid
);
656 btrfs_set_root_otransid(root_item
, trans
->transid
);
658 btrfs_tree_unlock(leaf
);
659 free_extent_buffer(leaf
);
662 btrfs_set_root_dirid(root_item
, new_dirid
);
664 key
.objectid
= objectid
;
666 key
.type
= BTRFS_ROOT_ITEM_KEY
;
667 ret
= btrfs_insert_root(trans
, fs_info
->tree_root
, &key
,
672 key
.offset
= (u64
)-1;
673 new_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
674 if (IS_ERR(new_root
)) {
675 ret
= PTR_ERR(new_root
);
676 btrfs_abort_transaction(trans
, ret
);
680 btrfs_record_root_in_trans(trans
, new_root
);
682 ret
= btrfs_create_subvol_root(trans
, new_root
, root
, new_dirid
);
684 /* We potentially lose an unused inode item here */
685 btrfs_abort_transaction(trans
, ret
);
689 mutex_lock(&new_root
->objectid_mutex
);
690 new_root
->highest_objectid
= new_dirid
;
691 mutex_unlock(&new_root
->objectid_mutex
);
694 * insert the directory item
696 ret
= btrfs_set_inode_index(BTRFS_I(dir
), &index
);
698 btrfs_abort_transaction(trans
, ret
);
702 ret
= btrfs_insert_dir_item(trans
, root
,
703 name
, namelen
, BTRFS_I(dir
), &key
,
704 BTRFS_FT_DIR
, index
);
706 btrfs_abort_transaction(trans
, ret
);
710 btrfs_i_size_write(BTRFS_I(dir
), dir
->i_size
+ namelen
* 2);
711 ret
= btrfs_update_inode(trans
, root
, dir
);
714 ret
= btrfs_add_root_ref(trans
, fs_info
,
715 objectid
, root
->root_key
.objectid
,
716 btrfs_ino(BTRFS_I(dir
)), index
, name
, namelen
);
719 ret
= btrfs_uuid_tree_add(trans
, root_item
->uuid
,
720 BTRFS_UUID_KEY_SUBVOL
, objectid
);
722 btrfs_abort_transaction(trans
, ret
);
726 trans
->block_rsv
= NULL
;
727 trans
->bytes_reserved
= 0;
728 btrfs_subvolume_release_metadata(fs_info
, &block_rsv
);
731 *async_transid
= trans
->transid
;
732 err
= btrfs_commit_transaction_async(trans
, 1);
734 err
= btrfs_commit_transaction(trans
);
736 err
= btrfs_commit_transaction(trans
);
742 inode
= btrfs_lookup_dentry(dir
, dentry
);
744 return PTR_ERR(inode
);
745 d_instantiate(dentry
, inode
);
754 static int create_snapshot(struct btrfs_root
*root
, struct inode
*dir
,
755 struct dentry
*dentry
,
756 u64
*async_transid
, bool readonly
,
757 struct btrfs_qgroup_inherit
*inherit
)
759 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
761 struct btrfs_pending_snapshot
*pending_snapshot
;
762 struct btrfs_trans_handle
*trans
;
765 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
768 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_KERNEL
);
769 if (!pending_snapshot
)
772 pending_snapshot
->root_item
= kzalloc(sizeof(struct btrfs_root_item
),
774 pending_snapshot
->path
= btrfs_alloc_path();
775 if (!pending_snapshot
->root_item
|| !pending_snapshot
->path
) {
780 atomic_inc(&root
->will_be_snapshotted
);
781 smp_mb__after_atomic();
782 /* wait for no snapshot writes */
783 wait_event(root
->subv_writers
->wait
,
784 percpu_counter_sum(&root
->subv_writers
->counter
) == 0);
786 ret
= btrfs_start_delalloc_inodes(root
);
790 btrfs_wait_ordered_extents(root
, U64_MAX
, 0, (u64
)-1);
792 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
793 BTRFS_BLOCK_RSV_TEMP
);
795 * 1 - parent dir inode
798 * 2 - root ref/backref
799 * 1 - root of snapshot
802 ret
= btrfs_subvolume_reserve_metadata(BTRFS_I(dir
)->root
,
803 &pending_snapshot
->block_rsv
, 8,
808 pending_snapshot
->dentry
= dentry
;
809 pending_snapshot
->root
= root
;
810 pending_snapshot
->readonly
= readonly
;
811 pending_snapshot
->dir
= dir
;
812 pending_snapshot
->inherit
= inherit
;
814 trans
= btrfs_start_transaction(root
, 0);
816 ret
= PTR_ERR(trans
);
820 spin_lock(&fs_info
->trans_lock
);
821 list_add(&pending_snapshot
->list
,
822 &trans
->transaction
->pending_snapshots
);
823 spin_unlock(&fs_info
->trans_lock
);
825 *async_transid
= trans
->transid
;
826 ret
= btrfs_commit_transaction_async(trans
, 1);
828 ret
= btrfs_commit_transaction(trans
);
830 ret
= btrfs_commit_transaction(trans
);
835 ret
= pending_snapshot
->error
;
839 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
843 inode
= btrfs_lookup_dentry(d_inode(dentry
->d_parent
), dentry
);
845 ret
= PTR_ERR(inode
);
849 d_instantiate(dentry
, inode
);
852 btrfs_subvolume_release_metadata(fs_info
, &pending_snapshot
->block_rsv
);
854 if (atomic_dec_and_test(&root
->will_be_snapshotted
))
855 wake_up_var(&root
->will_be_snapshotted
);
857 kfree(pending_snapshot
->root_item
);
858 btrfs_free_path(pending_snapshot
->path
);
859 kfree(pending_snapshot
);
864 /* copy of may_delete in fs/namei.c()
865 * Check whether we can remove a link victim from directory dir, check
866 * whether the type of victim is right.
867 * 1. We can't do it if dir is read-only (done in permission())
868 * 2. We should have write and exec permissions on dir
869 * 3. We can't remove anything from append-only dir
870 * 4. We can't do anything with immutable dir (done in permission())
871 * 5. If the sticky bit on dir is set we should either
872 * a. be owner of dir, or
873 * b. be owner of victim, or
874 * c. have CAP_FOWNER capability
875 * 6. If the victim is append-only or immutable we can't do anything with
876 * links pointing to it.
877 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
878 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
879 * 9. We can't remove a root or mountpoint.
880 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
881 * nfs_async_unlink().
884 static int btrfs_may_delete(struct inode
*dir
, struct dentry
*victim
, int isdir
)
888 if (d_really_is_negative(victim
))
891 BUG_ON(d_inode(victim
->d_parent
) != dir
);
892 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
894 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
899 if (check_sticky(dir
, d_inode(victim
)) || IS_APPEND(d_inode(victim
)) ||
900 IS_IMMUTABLE(d_inode(victim
)) || IS_SWAPFILE(d_inode(victim
)))
903 if (!d_is_dir(victim
))
907 } else if (d_is_dir(victim
))
911 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
916 /* copy of may_create in fs/namei.c() */
917 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
919 if (d_really_is_positive(child
))
923 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
927 * Create a new subvolume below @parent. This is largely modeled after
928 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
929 * inside this filesystem so it's quite a bit simpler.
931 static noinline
int btrfs_mksubvol(const struct path
*parent
,
932 const char *name
, int namelen
,
933 struct btrfs_root
*snap_src
,
934 u64
*async_transid
, bool readonly
,
935 struct btrfs_qgroup_inherit
*inherit
)
937 struct inode
*dir
= d_inode(parent
->dentry
);
938 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
939 struct dentry
*dentry
;
942 error
= down_write_killable_nested(&dir
->i_rwsem
, I_MUTEX_PARENT
);
946 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
947 error
= PTR_ERR(dentry
);
951 error
= btrfs_may_create(dir
, dentry
);
956 * even if this name doesn't exist, we may get hash collisions.
957 * check for them now when we can safely fail
959 error
= btrfs_check_dir_item_collision(BTRFS_I(dir
)->root
,
965 down_read(&fs_info
->subvol_sem
);
967 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
971 error
= create_snapshot(snap_src
, dir
, dentry
,
972 async_transid
, readonly
, inherit
);
974 error
= create_subvol(dir
, dentry
, name
, namelen
,
975 async_transid
, inherit
);
978 fsnotify_mkdir(dir
, dentry
);
980 up_read(&fs_info
->subvol_sem
);
989 * When we're defragging a range, we don't want to kick it off again
990 * if it is really just waiting for delalloc to send it down.
991 * If we find a nice big extent or delalloc range for the bytes in the
992 * file you want to defrag, we return 0 to let you know to skip this
995 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, u32 thresh
)
997 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
998 struct extent_map
*em
= NULL
;
999 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1002 read_lock(&em_tree
->lock
);
1003 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_SIZE
);
1004 read_unlock(&em_tree
->lock
);
1007 end
= extent_map_end(em
);
1008 free_extent_map(em
);
1009 if (end
- offset
> thresh
)
1012 /* if we already have a nice delalloc here, just stop */
1014 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
1015 thresh
, EXTENT_DELALLOC
, 1);
1022 * helper function to walk through a file and find extents
1023 * newer than a specific transid, and smaller than thresh.
1025 * This is used by the defragging code to find new and small
1028 static int find_new_extents(struct btrfs_root
*root
,
1029 struct inode
*inode
, u64 newer_than
,
1030 u64
*off
, u32 thresh
)
1032 struct btrfs_path
*path
;
1033 struct btrfs_key min_key
;
1034 struct extent_buffer
*leaf
;
1035 struct btrfs_file_extent_item
*extent
;
1038 u64 ino
= btrfs_ino(BTRFS_I(inode
));
1040 path
= btrfs_alloc_path();
1044 min_key
.objectid
= ino
;
1045 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
1046 min_key
.offset
= *off
;
1049 ret
= btrfs_search_forward(root
, &min_key
, path
, newer_than
);
1053 if (min_key
.objectid
!= ino
)
1055 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1058 leaf
= path
->nodes
[0];
1059 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1060 struct btrfs_file_extent_item
);
1062 type
= btrfs_file_extent_type(leaf
, extent
);
1063 if (type
== BTRFS_FILE_EXTENT_REG
&&
1064 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
1065 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
1066 *off
= min_key
.offset
;
1067 btrfs_free_path(path
);
1072 if (path
->slots
[0] < btrfs_header_nritems(leaf
)) {
1073 btrfs_item_key_to_cpu(leaf
, &min_key
, path
->slots
[0]);
1077 if (min_key
.offset
== (u64
)-1)
1081 btrfs_release_path(path
);
1084 btrfs_free_path(path
);
1088 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
1090 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1091 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1092 struct extent_map
*em
;
1093 u64 len
= PAGE_SIZE
;
1096 * hopefully we have this extent in the tree already, try without
1097 * the full extent lock
1099 read_lock(&em_tree
->lock
);
1100 em
= lookup_extent_mapping(em_tree
, start
, len
);
1101 read_unlock(&em_tree
->lock
);
1104 struct extent_state
*cached
= NULL
;
1105 u64 end
= start
+ len
- 1;
1107 /* get the big lock and read metadata off disk */
1108 lock_extent_bits(io_tree
, start
, end
, &cached
);
1109 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, start
, len
, 0);
1110 unlock_extent_cached(io_tree
, start
, end
, &cached
);
1119 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
1121 struct extent_map
*next
;
1124 /* this is the last extent */
1125 if (em
->start
+ em
->len
>= i_size_read(inode
))
1128 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
1129 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
1131 else if ((em
->block_start
+ em
->block_len
== next
->block_start
) &&
1132 (em
->block_len
> SZ_128K
&& next
->block_len
> SZ_128K
))
1135 free_extent_map(next
);
1139 static int should_defrag_range(struct inode
*inode
, u64 start
, u32 thresh
,
1140 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
1143 struct extent_map
*em
;
1145 bool next_mergeable
= true;
1146 bool prev_mergeable
= true;
1149 * make sure that once we start defragging an extent, we keep on
1152 if (start
< *defrag_end
)
1157 em
= defrag_lookup_extent(inode
, start
);
1161 /* this will cover holes, and inline extents */
1162 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
1168 prev_mergeable
= false;
1170 next_mergeable
= defrag_check_next_extent(inode
, em
);
1172 * we hit a real extent, if it is big or the next extent is not a
1173 * real extent, don't bother defragging it
1175 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
1176 (em
->len
>= thresh
|| (!next_mergeable
&& !prev_mergeable
)))
1180 * last_len ends up being a counter of how many bytes we've defragged.
1181 * every time we choose not to defrag an extent, we reset *last_len
1182 * so that the next tiny extent will force a defrag.
1184 * The end result of this is that tiny extents before a single big
1185 * extent will force at least part of that big extent to be defragged.
1188 *defrag_end
= extent_map_end(em
);
1191 *skip
= extent_map_end(em
);
1195 free_extent_map(em
);
1200 * it doesn't do much good to defrag one or two pages
1201 * at a time. This pulls in a nice chunk of pages
1202 * to COW and defrag.
1204 * It also makes sure the delalloc code has enough
1205 * dirty data to avoid making new small extents as part
1208 * It's a good idea to start RA on this range
1209 * before calling this.
1211 static int cluster_pages_for_defrag(struct inode
*inode
,
1212 struct page
**pages
,
1213 unsigned long start_index
,
1214 unsigned long num_pages
)
1216 unsigned long file_end
;
1217 u64 isize
= i_size_read(inode
);
1224 struct btrfs_ordered_extent
*ordered
;
1225 struct extent_state
*cached_state
= NULL
;
1226 struct extent_io_tree
*tree
;
1227 struct extent_changeset
*data_reserved
= NULL
;
1228 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
1230 file_end
= (isize
- 1) >> PAGE_SHIFT
;
1231 if (!isize
|| start_index
> file_end
)
1234 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
1236 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
,
1237 start_index
<< PAGE_SHIFT
,
1238 page_cnt
<< PAGE_SHIFT
);
1242 tree
= &BTRFS_I(inode
)->io_tree
;
1244 /* step one, lock all the pages */
1245 for (i
= 0; i
< page_cnt
; i
++) {
1248 page
= find_or_create_page(inode
->i_mapping
,
1249 start_index
+ i
, mask
);
1253 page_start
= page_offset(page
);
1254 page_end
= page_start
+ PAGE_SIZE
- 1;
1256 lock_extent_bits(tree
, page_start
, page_end
,
1258 ordered
= btrfs_lookup_ordered_extent(inode
,
1260 unlock_extent_cached(tree
, page_start
, page_end
,
1266 btrfs_start_ordered_extent(inode
, ordered
, 1);
1267 btrfs_put_ordered_extent(ordered
);
1270 * we unlocked the page above, so we need check if
1271 * it was released or not.
1273 if (page
->mapping
!= inode
->i_mapping
) {
1280 if (!PageUptodate(page
)) {
1281 btrfs_readpage(NULL
, page
);
1283 if (!PageUptodate(page
)) {
1291 if (page
->mapping
!= inode
->i_mapping
) {
1303 if (!(inode
->i_sb
->s_flags
& SB_ACTIVE
))
1307 * so now we have a nice long stream of locked
1308 * and up to date pages, lets wait on them
1310 for (i
= 0; i
< i_done
; i
++)
1311 wait_on_page_writeback(pages
[i
]);
1313 page_start
= page_offset(pages
[0]);
1314 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_SIZE
;
1316 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1317 page_start
, page_end
- 1, &cached_state
);
1318 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1319 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1320 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 0, 0,
1323 if (i_done
!= page_cnt
) {
1324 spin_lock(&BTRFS_I(inode
)->lock
);
1325 BTRFS_I(inode
)->outstanding_extents
++;
1326 spin_unlock(&BTRFS_I(inode
)->lock
);
1327 btrfs_delalloc_release_space(inode
, data_reserved
,
1328 start_index
<< PAGE_SHIFT
,
1329 (page_cnt
- i_done
) << PAGE_SHIFT
, true);
1333 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1336 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1337 page_start
, page_end
- 1, &cached_state
);
1339 for (i
= 0; i
< i_done
; i
++) {
1340 clear_page_dirty_for_io(pages
[i
]);
1341 ClearPageChecked(pages
[i
]);
1342 set_page_extent_mapped(pages
[i
]);
1343 set_page_dirty(pages
[i
]);
1344 unlock_page(pages
[i
]);
1347 btrfs_delalloc_release_extents(BTRFS_I(inode
), page_cnt
<< PAGE_SHIFT
,
1349 extent_changeset_free(data_reserved
);
1352 for (i
= 0; i
< i_done
; i
++) {
1353 unlock_page(pages
[i
]);
1356 btrfs_delalloc_release_space(inode
, data_reserved
,
1357 start_index
<< PAGE_SHIFT
,
1358 page_cnt
<< PAGE_SHIFT
, true);
1359 btrfs_delalloc_release_extents(BTRFS_I(inode
), page_cnt
<< PAGE_SHIFT
,
1361 extent_changeset_free(data_reserved
);
1366 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1367 struct btrfs_ioctl_defrag_range_args
*range
,
1368 u64 newer_than
, unsigned long max_to_defrag
)
1370 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1371 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1372 struct file_ra_state
*ra
= NULL
;
1373 unsigned long last_index
;
1374 u64 isize
= i_size_read(inode
);
1378 u64 newer_off
= range
->start
;
1380 unsigned long ra_index
= 0;
1382 int defrag_count
= 0;
1383 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1384 u32 extent_thresh
= range
->extent_thresh
;
1385 unsigned long max_cluster
= SZ_256K
>> PAGE_SHIFT
;
1386 unsigned long cluster
= max_cluster
;
1387 u64 new_align
= ~((u64
)SZ_128K
- 1);
1388 struct page
**pages
= NULL
;
1389 bool do_compress
= range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
;
1394 if (range
->start
>= isize
)
1398 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1400 if (range
->compress_type
)
1401 compress_type
= range
->compress_type
;
1404 if (extent_thresh
== 0)
1405 extent_thresh
= SZ_256K
;
1408 * If we were not given a file, allocate a readahead context. As
1409 * readahead is just an optimization, defrag will work without it so
1410 * we don't error out.
1413 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
1415 file_ra_state_init(ra
, inode
->i_mapping
);
1420 pages
= kmalloc_array(max_cluster
, sizeof(struct page
*), GFP_KERNEL
);
1426 /* find the last page to defrag */
1427 if (range
->start
+ range
->len
> range
->start
) {
1428 last_index
= min_t(u64
, isize
- 1,
1429 range
->start
+ range
->len
- 1) >> PAGE_SHIFT
;
1431 last_index
= (isize
- 1) >> PAGE_SHIFT
;
1435 ret
= find_new_extents(root
, inode
, newer_than
,
1436 &newer_off
, SZ_64K
);
1438 range
->start
= newer_off
;
1440 * we always align our defrag to help keep
1441 * the extents in the file evenly spaced
1443 i
= (newer_off
& new_align
) >> PAGE_SHIFT
;
1447 i
= range
->start
>> PAGE_SHIFT
;
1450 max_to_defrag
= last_index
- i
+ 1;
1453 * make writeback starts from i, so the defrag range can be
1454 * written sequentially.
1456 if (i
< inode
->i_mapping
->writeback_index
)
1457 inode
->i_mapping
->writeback_index
= i
;
1459 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1460 (i
< DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
))) {
1462 * make sure we stop running if someone unmounts
1465 if (!(inode
->i_sb
->s_flags
& SB_ACTIVE
))
1468 if (btrfs_defrag_cancelled(fs_info
)) {
1469 btrfs_debug(fs_info
, "defrag_file cancelled");
1474 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_SHIFT
,
1475 extent_thresh
, &last_len
, &skip
,
1476 &defrag_end
, do_compress
)){
1479 * the should_defrag function tells us how much to skip
1480 * bump our counter by the suggested amount
1482 next
= DIV_ROUND_UP(skip
, PAGE_SIZE
);
1483 i
= max(i
+ 1, next
);
1488 cluster
= (PAGE_ALIGN(defrag_end
) >>
1490 cluster
= min(cluster
, max_cluster
);
1492 cluster
= max_cluster
;
1495 if (i
+ cluster
> ra_index
) {
1496 ra_index
= max(i
, ra_index
);
1498 page_cache_sync_readahead(inode
->i_mapping
, ra
,
1499 file
, ra_index
, cluster
);
1500 ra_index
+= cluster
;
1505 BTRFS_I(inode
)->defrag_compress
= compress_type
;
1506 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1508 inode_unlock(inode
);
1512 defrag_count
+= ret
;
1513 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1514 inode_unlock(inode
);
1517 if (newer_off
== (u64
)-1)
1523 newer_off
= max(newer_off
+ 1,
1524 (u64
)i
<< PAGE_SHIFT
);
1526 ret
= find_new_extents(root
, inode
, newer_than
,
1527 &newer_off
, SZ_64K
);
1529 range
->start
= newer_off
;
1530 i
= (newer_off
& new_align
) >> PAGE_SHIFT
;
1537 last_len
+= ret
<< PAGE_SHIFT
;
1545 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
)) {
1546 filemap_flush(inode
->i_mapping
);
1547 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1548 &BTRFS_I(inode
)->runtime_flags
))
1549 filemap_flush(inode
->i_mapping
);
1552 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1553 btrfs_set_fs_incompat(fs_info
, COMPRESS_LZO
);
1554 } else if (range
->compress_type
== BTRFS_COMPRESS_ZSTD
) {
1555 btrfs_set_fs_incompat(fs_info
, COMPRESS_ZSTD
);
1563 BTRFS_I(inode
)->defrag_compress
= BTRFS_COMPRESS_NONE
;
1564 inode_unlock(inode
);
1572 static noinline
int btrfs_ioctl_resize(struct file
*file
,
1575 struct inode
*inode
= file_inode(file
);
1576 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1580 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1581 struct btrfs_ioctl_vol_args
*vol_args
;
1582 struct btrfs_trans_handle
*trans
;
1583 struct btrfs_device
*device
= NULL
;
1586 char *devstr
= NULL
;
1590 if (!capable(CAP_SYS_ADMIN
))
1593 ret
= mnt_want_write_file(file
);
1597 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
1598 mnt_drop_write_file(file
);
1599 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
1602 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1603 if (IS_ERR(vol_args
)) {
1604 ret
= PTR_ERR(vol_args
);
1608 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1610 sizestr
= vol_args
->name
;
1611 devstr
= strchr(sizestr
, ':');
1613 sizestr
= devstr
+ 1;
1615 devstr
= vol_args
->name
;
1616 ret
= kstrtoull(devstr
, 10, &devid
);
1623 btrfs_info(fs_info
, "resizing devid %llu", devid
);
1626 device
= btrfs_find_device(fs_info
, devid
, NULL
, NULL
);
1628 btrfs_info(fs_info
, "resizer unable to find device %llu",
1634 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1636 "resizer unable to apply on readonly device %llu",
1642 if (!strcmp(sizestr
, "max"))
1643 new_size
= device
->bdev
->bd_inode
->i_size
;
1645 if (sizestr
[0] == '-') {
1648 } else if (sizestr
[0] == '+') {
1652 new_size
= memparse(sizestr
, &retptr
);
1653 if (*retptr
!= '\0' || new_size
== 0) {
1659 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1664 old_size
= btrfs_device_get_total_bytes(device
);
1667 if (new_size
> old_size
) {
1671 new_size
= old_size
- new_size
;
1672 } else if (mod
> 0) {
1673 if (new_size
> ULLONG_MAX
- old_size
) {
1677 new_size
= old_size
+ new_size
;
1680 if (new_size
< SZ_256M
) {
1684 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1689 new_size
= round_down(new_size
, fs_info
->sectorsize
);
1691 btrfs_info_in_rcu(fs_info
, "new size for %s is %llu",
1692 rcu_str_deref(device
->name
), new_size
);
1694 if (new_size
> old_size
) {
1695 trans
= btrfs_start_transaction(root
, 0);
1696 if (IS_ERR(trans
)) {
1697 ret
= PTR_ERR(trans
);
1700 ret
= btrfs_grow_device(trans
, device
, new_size
);
1701 btrfs_commit_transaction(trans
);
1702 } else if (new_size
< old_size
) {
1703 ret
= btrfs_shrink_device(device
, new_size
);
1704 } /* equal, nothing need to do */
1709 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
1710 mnt_drop_write_file(file
);
1714 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1715 const char *name
, unsigned long fd
, int subvol
,
1716 u64
*transid
, bool readonly
,
1717 struct btrfs_qgroup_inherit
*inherit
)
1722 if (!S_ISDIR(file_inode(file
)->i_mode
))
1725 ret
= mnt_want_write_file(file
);
1729 namelen
= strlen(name
);
1730 if (strchr(name
, '/')) {
1732 goto out_drop_write
;
1735 if (name
[0] == '.' &&
1736 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1738 goto out_drop_write
;
1742 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1743 NULL
, transid
, readonly
, inherit
);
1745 struct fd src
= fdget(fd
);
1746 struct inode
*src_inode
;
1749 goto out_drop_write
;
1752 src_inode
= file_inode(src
.file
);
1753 if (src_inode
->i_sb
!= file_inode(file
)->i_sb
) {
1754 btrfs_info(BTRFS_I(file_inode(file
))->root
->fs_info
,
1755 "Snapshot src from another FS");
1757 } else if (!inode_owner_or_capable(src_inode
)) {
1759 * Subvolume creation is not restricted, but snapshots
1760 * are limited to own subvolumes only
1764 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1765 BTRFS_I(src_inode
)->root
,
1766 transid
, readonly
, inherit
);
1771 mnt_drop_write_file(file
);
1776 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1777 void __user
*arg
, int subvol
)
1779 struct btrfs_ioctl_vol_args
*vol_args
;
1782 if (!S_ISDIR(file_inode(file
)->i_mode
))
1785 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1786 if (IS_ERR(vol_args
))
1787 return PTR_ERR(vol_args
);
1788 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1790 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1791 vol_args
->fd
, subvol
,
1798 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1799 void __user
*arg
, int subvol
)
1801 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1805 bool readonly
= false;
1806 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1808 if (!S_ISDIR(file_inode(file
)->i_mode
))
1811 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1812 if (IS_ERR(vol_args
))
1813 return PTR_ERR(vol_args
);
1814 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1816 if (vol_args
->flags
&
1817 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1818 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1823 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1825 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1827 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1828 if (vol_args
->size
> PAGE_SIZE
) {
1832 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1833 if (IS_ERR(inherit
)) {
1834 ret
= PTR_ERR(inherit
);
1839 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1840 vol_args
->fd
, subvol
, ptr
,
1845 if (ptr
&& copy_to_user(arg
+
1846 offsetof(struct btrfs_ioctl_vol_args_v2
,
1858 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1861 struct inode
*inode
= file_inode(file
);
1862 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1863 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1867 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
)
1870 down_read(&fs_info
->subvol_sem
);
1871 if (btrfs_root_readonly(root
))
1872 flags
|= BTRFS_SUBVOL_RDONLY
;
1873 up_read(&fs_info
->subvol_sem
);
1875 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1881 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1884 struct inode
*inode
= file_inode(file
);
1885 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1886 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1887 struct btrfs_trans_handle
*trans
;
1892 if (!inode_owner_or_capable(inode
))
1895 ret
= mnt_want_write_file(file
);
1899 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
1901 goto out_drop_write
;
1904 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1906 goto out_drop_write
;
1909 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1911 goto out_drop_write
;
1914 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1916 goto out_drop_write
;
1919 down_write(&fs_info
->subvol_sem
);
1922 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1925 root_flags
= btrfs_root_flags(&root
->root_item
);
1926 if (flags
& BTRFS_SUBVOL_RDONLY
) {
1927 btrfs_set_root_flags(&root
->root_item
,
1928 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1931 * Block RO -> RW transition if this subvolume is involved in
1934 spin_lock(&root
->root_item_lock
);
1935 if (root
->send_in_progress
== 0) {
1936 btrfs_set_root_flags(&root
->root_item
,
1937 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1938 spin_unlock(&root
->root_item_lock
);
1940 spin_unlock(&root
->root_item_lock
);
1942 "Attempt to set subvolume %llu read-write during send",
1943 root
->root_key
.objectid
);
1949 trans
= btrfs_start_transaction(root
, 1);
1950 if (IS_ERR(trans
)) {
1951 ret
= PTR_ERR(trans
);
1955 ret
= btrfs_update_root(trans
, fs_info
->tree_root
,
1956 &root
->root_key
, &root
->root_item
);
1958 btrfs_end_transaction(trans
);
1962 ret
= btrfs_commit_transaction(trans
);
1966 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1968 up_write(&fs_info
->subvol_sem
);
1970 mnt_drop_write_file(file
);
1975 static noinline
int key_in_sk(struct btrfs_key
*key
,
1976 struct btrfs_ioctl_search_key
*sk
)
1978 struct btrfs_key test
;
1981 test
.objectid
= sk
->min_objectid
;
1982 test
.type
= sk
->min_type
;
1983 test
.offset
= sk
->min_offset
;
1985 ret
= btrfs_comp_cpu_keys(key
, &test
);
1989 test
.objectid
= sk
->max_objectid
;
1990 test
.type
= sk
->max_type
;
1991 test
.offset
= sk
->max_offset
;
1993 ret
= btrfs_comp_cpu_keys(key
, &test
);
1999 static noinline
int copy_to_sk(struct btrfs_path
*path
,
2000 struct btrfs_key
*key
,
2001 struct btrfs_ioctl_search_key
*sk
,
2004 unsigned long *sk_offset
,
2008 struct extent_buffer
*leaf
;
2009 struct btrfs_ioctl_search_header sh
;
2010 struct btrfs_key test
;
2011 unsigned long item_off
;
2012 unsigned long item_len
;
2018 leaf
= path
->nodes
[0];
2019 slot
= path
->slots
[0];
2020 nritems
= btrfs_header_nritems(leaf
);
2022 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
2026 found_transid
= btrfs_header_generation(leaf
);
2028 for (i
= slot
; i
< nritems
; i
++) {
2029 item_off
= btrfs_item_ptr_offset(leaf
, i
);
2030 item_len
= btrfs_item_size_nr(leaf
, i
);
2032 btrfs_item_key_to_cpu(leaf
, key
, i
);
2033 if (!key_in_sk(key
, sk
))
2036 if (sizeof(sh
) + item_len
> *buf_size
) {
2043 * return one empty item back for v1, which does not
2047 *buf_size
= sizeof(sh
) + item_len
;
2052 if (sizeof(sh
) + item_len
+ *sk_offset
> *buf_size
) {
2057 sh
.objectid
= key
->objectid
;
2058 sh
.offset
= key
->offset
;
2059 sh
.type
= key
->type
;
2061 sh
.transid
= found_transid
;
2063 /* copy search result header */
2064 if (copy_to_user(ubuf
+ *sk_offset
, &sh
, sizeof(sh
))) {
2069 *sk_offset
+= sizeof(sh
);
2072 char __user
*up
= ubuf
+ *sk_offset
;
2074 if (read_extent_buffer_to_user(leaf
, up
,
2075 item_off
, item_len
)) {
2080 *sk_offset
+= item_len
;
2084 if (ret
) /* -EOVERFLOW from above */
2087 if (*num_found
>= sk
->nr_items
) {
2094 test
.objectid
= sk
->max_objectid
;
2095 test
.type
= sk
->max_type
;
2096 test
.offset
= sk
->max_offset
;
2097 if (btrfs_comp_cpu_keys(key
, &test
) >= 0)
2099 else if (key
->offset
< (u64
)-1)
2101 else if (key
->type
< (u8
)-1) {
2104 } else if (key
->objectid
< (u64
)-1) {
2112 * 0: all items from this leaf copied, continue with next
2113 * 1: * more items can be copied, but unused buffer is too small
2114 * * all items were found
2115 * Either way, it will stops the loop which iterates to the next
2117 * -EOVERFLOW: item was to large for buffer
2118 * -EFAULT: could not copy extent buffer back to userspace
2123 static noinline
int search_ioctl(struct inode
*inode
,
2124 struct btrfs_ioctl_search_key
*sk
,
2128 struct btrfs_fs_info
*info
= btrfs_sb(inode
->i_sb
);
2129 struct btrfs_root
*root
;
2130 struct btrfs_key key
;
2131 struct btrfs_path
*path
;
2134 unsigned long sk_offset
= 0;
2136 if (*buf_size
< sizeof(struct btrfs_ioctl_search_header
)) {
2137 *buf_size
= sizeof(struct btrfs_ioctl_search_header
);
2141 path
= btrfs_alloc_path();
2145 if (sk
->tree_id
== 0) {
2146 /* search the root of the inode that was passed */
2147 root
= BTRFS_I(inode
)->root
;
2149 key
.objectid
= sk
->tree_id
;
2150 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2151 key
.offset
= (u64
)-1;
2152 root
= btrfs_read_fs_root_no_name(info
, &key
);
2154 btrfs_free_path(path
);
2155 return PTR_ERR(root
);
2159 key
.objectid
= sk
->min_objectid
;
2160 key
.type
= sk
->min_type
;
2161 key
.offset
= sk
->min_offset
;
2164 ret
= btrfs_search_forward(root
, &key
, path
, sk
->min_transid
);
2170 ret
= copy_to_sk(path
, &key
, sk
, buf_size
, ubuf
,
2171 &sk_offset
, &num_found
);
2172 btrfs_release_path(path
);
2180 sk
->nr_items
= num_found
;
2181 btrfs_free_path(path
);
2185 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
2188 struct btrfs_ioctl_search_args __user
*uargs
;
2189 struct btrfs_ioctl_search_key sk
;
2190 struct inode
*inode
;
2194 if (!capable(CAP_SYS_ADMIN
))
2197 uargs
= (struct btrfs_ioctl_search_args __user
*)argp
;
2199 if (copy_from_user(&sk
, &uargs
->key
, sizeof(sk
)))
2202 buf_size
= sizeof(uargs
->buf
);
2204 inode
= file_inode(file
);
2205 ret
= search_ioctl(inode
, &sk
, &buf_size
, uargs
->buf
);
2208 * In the origin implementation an overflow is handled by returning a
2209 * search header with a len of zero, so reset ret.
2211 if (ret
== -EOVERFLOW
)
2214 if (ret
== 0 && copy_to_user(&uargs
->key
, &sk
, sizeof(sk
)))
2219 static noinline
int btrfs_ioctl_tree_search_v2(struct file
*file
,
2222 struct btrfs_ioctl_search_args_v2 __user
*uarg
;
2223 struct btrfs_ioctl_search_args_v2 args
;
2224 struct inode
*inode
;
2227 const size_t buf_limit
= SZ_16M
;
2229 if (!capable(CAP_SYS_ADMIN
))
2232 /* copy search header and buffer size */
2233 uarg
= (struct btrfs_ioctl_search_args_v2 __user
*)argp
;
2234 if (copy_from_user(&args
, uarg
, sizeof(args
)))
2237 buf_size
= args
.buf_size
;
2239 /* limit result size to 16MB */
2240 if (buf_size
> buf_limit
)
2241 buf_size
= buf_limit
;
2243 inode
= file_inode(file
);
2244 ret
= search_ioctl(inode
, &args
.key
, &buf_size
,
2245 (char __user
*)(&uarg
->buf
[0]));
2246 if (ret
== 0 && copy_to_user(&uarg
->key
, &args
.key
, sizeof(args
.key
)))
2248 else if (ret
== -EOVERFLOW
&&
2249 copy_to_user(&uarg
->buf_size
, &buf_size
, sizeof(buf_size
)))
2256 * Search INODE_REFs to identify path name of 'dirid' directory
2257 * in a 'tree_id' tree. and sets path name to 'name'.
2259 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
2260 u64 tree_id
, u64 dirid
, char *name
)
2262 struct btrfs_root
*root
;
2263 struct btrfs_key key
;
2269 struct btrfs_inode_ref
*iref
;
2270 struct extent_buffer
*l
;
2271 struct btrfs_path
*path
;
2273 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
2278 path
= btrfs_alloc_path();
2282 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
- 1];
2284 key
.objectid
= tree_id
;
2285 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2286 key
.offset
= (u64
)-1;
2287 root
= btrfs_read_fs_root_no_name(info
, &key
);
2289 ret
= PTR_ERR(root
);
2293 key
.objectid
= dirid
;
2294 key
.type
= BTRFS_INODE_REF_KEY
;
2295 key
.offset
= (u64
)-1;
2298 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2302 ret
= btrfs_previous_item(root
, path
, dirid
,
2303 BTRFS_INODE_REF_KEY
);
2313 slot
= path
->slots
[0];
2314 btrfs_item_key_to_cpu(l
, &key
, slot
);
2316 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
2317 len
= btrfs_inode_ref_name_len(l
, iref
);
2319 total_len
+= len
+ 1;
2321 ret
= -ENAMETOOLONG
;
2326 read_extent_buffer(l
, ptr
, (unsigned long)(iref
+ 1), len
);
2328 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
2331 btrfs_release_path(path
);
2332 key
.objectid
= key
.offset
;
2333 key
.offset
= (u64
)-1;
2334 dirid
= key
.objectid
;
2336 memmove(name
, ptr
, total_len
);
2337 name
[total_len
] = '\0';
2340 btrfs_free_path(path
);
2344 static int btrfs_search_path_in_tree_user(struct inode
*inode
,
2345 struct btrfs_ioctl_ino_lookup_user_args
*args
)
2347 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2348 struct super_block
*sb
= inode
->i_sb
;
2349 struct btrfs_key upper_limit
= BTRFS_I(inode
)->location
;
2350 u64 treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2351 u64 dirid
= args
->dirid
;
2352 unsigned long item_off
;
2353 unsigned long item_len
;
2354 struct btrfs_inode_ref
*iref
;
2355 struct btrfs_root_ref
*rref
;
2356 struct btrfs_root
*root
;
2357 struct btrfs_path
*path
;
2358 struct btrfs_key key
, key2
;
2359 struct extent_buffer
*leaf
;
2360 struct inode
*temp_inode
;
2367 path
= btrfs_alloc_path();
2372 * If the bottom subvolume does not exist directly under upper_limit,
2373 * construct the path in from the bottom up.
2375 if (dirid
!= upper_limit
.objectid
) {
2376 ptr
= &args
->path
[BTRFS_INO_LOOKUP_USER_PATH_MAX
- 1];
2378 key
.objectid
= treeid
;
2379 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2380 key
.offset
= (u64
)-1;
2381 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2383 ret
= PTR_ERR(root
);
2387 key
.objectid
= dirid
;
2388 key
.type
= BTRFS_INODE_REF_KEY
;
2389 key
.offset
= (u64
)-1;
2391 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2394 } else if (ret
> 0) {
2395 ret
= btrfs_previous_item(root
, path
, dirid
,
2396 BTRFS_INODE_REF_KEY
);
2399 } else if (ret
> 0) {
2405 leaf
= path
->nodes
[0];
2406 slot
= path
->slots
[0];
2407 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2409 iref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_inode_ref
);
2410 len
= btrfs_inode_ref_name_len(leaf
, iref
);
2412 total_len
+= len
+ 1;
2413 if (ptr
< args
->path
) {
2414 ret
= -ENAMETOOLONG
;
2419 read_extent_buffer(leaf
, ptr
,
2420 (unsigned long)(iref
+ 1), len
);
2422 /* Check the read+exec permission of this directory */
2423 ret
= btrfs_previous_item(root
, path
, dirid
,
2424 BTRFS_INODE_ITEM_KEY
);
2427 } else if (ret
> 0) {
2432 leaf
= path
->nodes
[0];
2433 slot
= path
->slots
[0];
2434 btrfs_item_key_to_cpu(leaf
, &key2
, slot
);
2435 if (key2
.objectid
!= dirid
) {
2440 temp_inode
= btrfs_iget(sb
, &key2
, root
, NULL
);
2441 if (IS_ERR(temp_inode
)) {
2442 ret
= PTR_ERR(temp_inode
);
2445 ret
= inode_permission(temp_inode
, MAY_READ
| MAY_EXEC
);
2452 if (key
.offset
== upper_limit
.objectid
)
2454 if (key
.objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
2459 btrfs_release_path(path
);
2460 key
.objectid
= key
.offset
;
2461 key
.offset
= (u64
)-1;
2462 dirid
= key
.objectid
;
2465 memmove(args
->path
, ptr
, total_len
);
2466 args
->path
[total_len
] = '\0';
2467 btrfs_release_path(path
);
2470 /* Get the bottom subvolume's name from ROOT_REF */
2471 root
= fs_info
->tree_root
;
2472 key
.objectid
= treeid
;
2473 key
.type
= BTRFS_ROOT_REF_KEY
;
2474 key
.offset
= args
->treeid
;
2475 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2478 } else if (ret
> 0) {
2483 leaf
= path
->nodes
[0];
2484 slot
= path
->slots
[0];
2485 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2487 item_off
= btrfs_item_ptr_offset(leaf
, slot
);
2488 item_len
= btrfs_item_size_nr(leaf
, slot
);
2489 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2490 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2491 if (args
->dirid
!= btrfs_root_ref_dirid(leaf
, rref
)) {
2496 /* Copy subvolume's name */
2497 item_off
+= sizeof(struct btrfs_root_ref
);
2498 item_len
-= sizeof(struct btrfs_root_ref
);
2499 read_extent_buffer(leaf
, args
->name
, item_off
, item_len
);
2500 args
->name
[item_len
] = 0;
2503 btrfs_free_path(path
);
2507 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
2510 struct btrfs_ioctl_ino_lookup_args
*args
;
2511 struct inode
*inode
;
2514 args
= memdup_user(argp
, sizeof(*args
));
2516 return PTR_ERR(args
);
2518 inode
= file_inode(file
);
2521 * Unprivileged query to obtain the containing subvolume root id. The
2522 * path is reset so it's consistent with btrfs_search_path_in_tree.
2524 if (args
->treeid
== 0)
2525 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2527 if (args
->objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
2532 if (!capable(CAP_SYS_ADMIN
)) {
2537 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
2538 args
->treeid
, args
->objectid
,
2542 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2550 * Version of ino_lookup ioctl (unprivileged)
2552 * The main differences from ino_lookup ioctl are:
2554 * 1. Read + Exec permission will be checked using inode_permission() during
2555 * path construction. -EACCES will be returned in case of failure.
2556 * 2. Path construction will be stopped at the inode number which corresponds
2557 * to the fd with which this ioctl is called. If constructed path does not
2558 * exist under fd's inode, -EACCES will be returned.
2559 * 3. The name of bottom subvolume is also searched and filled.
2561 static int btrfs_ioctl_ino_lookup_user(struct file
*file
, void __user
*argp
)
2563 struct btrfs_ioctl_ino_lookup_user_args
*args
;
2564 struct inode
*inode
;
2567 args
= memdup_user(argp
, sizeof(*args
));
2569 return PTR_ERR(args
);
2571 inode
= file_inode(file
);
2573 if (args
->dirid
== BTRFS_FIRST_FREE_OBJECTID
&&
2574 BTRFS_I(inode
)->location
.objectid
!= BTRFS_FIRST_FREE_OBJECTID
) {
2576 * The subvolume does not exist under fd with which this is
2583 ret
= btrfs_search_path_in_tree_user(inode
, args
);
2585 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2592 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2593 static int btrfs_ioctl_get_subvol_info(struct file
*file
, void __user
*argp
)
2595 struct btrfs_ioctl_get_subvol_info_args
*subvol_info
;
2596 struct btrfs_fs_info
*fs_info
;
2597 struct btrfs_root
*root
;
2598 struct btrfs_path
*path
;
2599 struct btrfs_key key
;
2600 struct btrfs_root_item
*root_item
;
2601 struct btrfs_root_ref
*rref
;
2602 struct extent_buffer
*leaf
;
2603 unsigned long item_off
;
2604 unsigned long item_len
;
2605 struct inode
*inode
;
2609 path
= btrfs_alloc_path();
2613 subvol_info
= kzalloc(sizeof(*subvol_info
), GFP_KERNEL
);
2615 btrfs_free_path(path
);
2619 inode
= file_inode(file
);
2620 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2622 /* Get root_item of inode's subvolume */
2623 key
.objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2624 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2625 key
.offset
= (u64
)-1;
2626 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2628 ret
= PTR_ERR(root
);
2631 root_item
= &root
->root_item
;
2633 subvol_info
->treeid
= key
.objectid
;
2635 subvol_info
->generation
= btrfs_root_generation(root_item
);
2636 subvol_info
->flags
= btrfs_root_flags(root_item
);
2638 memcpy(subvol_info
->uuid
, root_item
->uuid
, BTRFS_UUID_SIZE
);
2639 memcpy(subvol_info
->parent_uuid
, root_item
->parent_uuid
,
2641 memcpy(subvol_info
->received_uuid
, root_item
->received_uuid
,
2644 subvol_info
->ctransid
= btrfs_root_ctransid(root_item
);
2645 subvol_info
->ctime
.sec
= btrfs_stack_timespec_sec(&root_item
->ctime
);
2646 subvol_info
->ctime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->ctime
);
2648 subvol_info
->otransid
= btrfs_root_otransid(root_item
);
2649 subvol_info
->otime
.sec
= btrfs_stack_timespec_sec(&root_item
->otime
);
2650 subvol_info
->otime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->otime
);
2652 subvol_info
->stransid
= btrfs_root_stransid(root_item
);
2653 subvol_info
->stime
.sec
= btrfs_stack_timespec_sec(&root_item
->stime
);
2654 subvol_info
->stime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->stime
);
2656 subvol_info
->rtransid
= btrfs_root_rtransid(root_item
);
2657 subvol_info
->rtime
.sec
= btrfs_stack_timespec_sec(&root_item
->rtime
);
2658 subvol_info
->rtime
.nsec
= btrfs_stack_timespec_nsec(&root_item
->rtime
);
2660 if (key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) {
2661 /* Search root tree for ROOT_BACKREF of this subvolume */
2662 root
= fs_info
->tree_root
;
2664 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2666 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2669 } else if (path
->slots
[0] >=
2670 btrfs_header_nritems(path
->nodes
[0])) {
2671 ret
= btrfs_next_leaf(root
, path
);
2674 } else if (ret
> 0) {
2680 leaf
= path
->nodes
[0];
2681 slot
= path
->slots
[0];
2682 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2683 if (key
.objectid
== subvol_info
->treeid
&&
2684 key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
2685 subvol_info
->parent_id
= key
.offset
;
2687 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2688 subvol_info
->dirid
= btrfs_root_ref_dirid(leaf
, rref
);
2690 item_off
= btrfs_item_ptr_offset(leaf
, slot
)
2691 + sizeof(struct btrfs_root_ref
);
2692 item_len
= btrfs_item_size_nr(leaf
, slot
)
2693 - sizeof(struct btrfs_root_ref
);
2694 read_extent_buffer(leaf
, subvol_info
->name
,
2695 item_off
, item_len
);
2702 if (copy_to_user(argp
, subvol_info
, sizeof(*subvol_info
)))
2706 btrfs_free_path(path
);
2707 kzfree(subvol_info
);
2712 * Return ROOT_REF information of the subvolume containing this inode
2713 * except the subvolume name.
2715 static int btrfs_ioctl_get_subvol_rootref(struct file
*file
, void __user
*argp
)
2717 struct btrfs_ioctl_get_subvol_rootref_args
*rootrefs
;
2718 struct btrfs_root_ref
*rref
;
2719 struct btrfs_root
*root
;
2720 struct btrfs_path
*path
;
2721 struct btrfs_key key
;
2722 struct extent_buffer
*leaf
;
2723 struct inode
*inode
;
2729 path
= btrfs_alloc_path();
2733 rootrefs
= memdup_user(argp
, sizeof(*rootrefs
));
2734 if (IS_ERR(rootrefs
)) {
2735 btrfs_free_path(path
);
2736 return PTR_ERR(rootrefs
);
2739 inode
= file_inode(file
);
2740 root
= BTRFS_I(inode
)->root
->fs_info
->tree_root
;
2741 objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2743 key
.objectid
= objectid
;
2744 key
.type
= BTRFS_ROOT_REF_KEY
;
2745 key
.offset
= rootrefs
->min_treeid
;
2748 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2751 } else if (path
->slots
[0] >=
2752 btrfs_header_nritems(path
->nodes
[0])) {
2753 ret
= btrfs_next_leaf(root
, path
);
2756 } else if (ret
> 0) {
2762 leaf
= path
->nodes
[0];
2763 slot
= path
->slots
[0];
2765 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2766 if (key
.objectid
!= objectid
|| key
.type
!= BTRFS_ROOT_REF_KEY
) {
2771 if (found
== BTRFS_MAX_ROOTREF_BUFFER_NUM
) {
2776 rref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_root_ref
);
2777 rootrefs
->rootref
[found
].treeid
= key
.offset
;
2778 rootrefs
->rootref
[found
].dirid
=
2779 btrfs_root_ref_dirid(leaf
, rref
);
2782 ret
= btrfs_next_item(root
, path
);
2785 } else if (ret
> 0) {
2792 if (!ret
|| ret
== -EOVERFLOW
) {
2793 rootrefs
->num_items
= found
;
2794 /* update min_treeid for next search */
2796 rootrefs
->min_treeid
=
2797 rootrefs
->rootref
[found
- 1].treeid
+ 1;
2798 if (copy_to_user(argp
, rootrefs
, sizeof(*rootrefs
)))
2803 btrfs_free_path(path
);
2808 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
2811 struct dentry
*parent
= file
->f_path
.dentry
;
2812 struct btrfs_fs_info
*fs_info
= btrfs_sb(parent
->d_sb
);
2813 struct dentry
*dentry
;
2814 struct inode
*dir
= d_inode(parent
);
2815 struct inode
*inode
;
2816 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2817 struct btrfs_root
*dest
= NULL
;
2818 struct btrfs_ioctl_vol_args
*vol_args
;
2822 if (!S_ISDIR(dir
->i_mode
))
2825 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2826 if (IS_ERR(vol_args
))
2827 return PTR_ERR(vol_args
);
2829 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2830 namelen
= strlen(vol_args
->name
);
2831 if (strchr(vol_args
->name
, '/') ||
2832 strncmp(vol_args
->name
, "..", namelen
) == 0) {
2837 err
= mnt_want_write_file(file
);
2842 err
= down_write_killable_nested(&dir
->i_rwsem
, I_MUTEX_PARENT
);
2844 goto out_drop_write
;
2845 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
2846 if (IS_ERR(dentry
)) {
2847 err
= PTR_ERR(dentry
);
2848 goto out_unlock_dir
;
2851 if (d_really_is_negative(dentry
)) {
2856 inode
= d_inode(dentry
);
2857 dest
= BTRFS_I(inode
)->root
;
2858 if (!capable(CAP_SYS_ADMIN
)) {
2860 * Regular user. Only allow this with a special mount
2861 * option, when the user has write+exec access to the
2862 * subvol root, and when rmdir(2) would have been
2865 * Note that this is _not_ check that the subvol is
2866 * empty or doesn't contain data that we wouldn't
2867 * otherwise be able to delete.
2869 * Users who want to delete empty subvols should try
2873 if (!btrfs_test_opt(fs_info
, USER_SUBVOL_RM_ALLOWED
))
2877 * Do not allow deletion if the parent dir is the same
2878 * as the dir to be deleted. That means the ioctl
2879 * must be called on the dentry referencing the root
2880 * of the subvol, not a random directory contained
2887 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
2892 /* check if subvolume may be deleted by a user */
2893 err
= btrfs_may_delete(dir
, dentry
, 1);
2897 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
2903 err
= btrfs_delete_subvolume(dir
, dentry
);
2904 inode_unlock(inode
);
2913 mnt_drop_write_file(file
);
2919 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2921 struct inode
*inode
= file_inode(file
);
2922 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2923 struct btrfs_ioctl_defrag_range_args
*range
;
2926 ret
= mnt_want_write_file(file
);
2930 if (btrfs_root_readonly(root
)) {
2935 switch (inode
->i_mode
& S_IFMT
) {
2937 if (!capable(CAP_SYS_ADMIN
)) {
2941 ret
= btrfs_defrag_root(root
);
2944 if (!(file
->f_mode
& FMODE_WRITE
)) {
2949 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2956 if (copy_from_user(range
, argp
,
2962 /* compression requires us to start the IO */
2963 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2964 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2965 range
->extent_thresh
= (u32
)-1;
2968 /* the rest are all set to zero by kzalloc */
2969 range
->len
= (u64
)-1;
2971 ret
= btrfs_defrag_file(file_inode(file
), file
,
2972 range
, BTRFS_OLDEST_GENERATION
, 0);
2981 mnt_drop_write_file(file
);
2985 static long btrfs_ioctl_add_dev(struct btrfs_fs_info
*fs_info
, void __user
*arg
)
2987 struct btrfs_ioctl_vol_args
*vol_args
;
2990 if (!capable(CAP_SYS_ADMIN
))
2993 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
))
2994 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2996 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2997 if (IS_ERR(vol_args
)) {
2998 ret
= PTR_ERR(vol_args
);
3002 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
3003 ret
= btrfs_init_new_device(fs_info
, vol_args
->name
);
3006 btrfs_info(fs_info
, "disk added %s", vol_args
->name
);
3010 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3014 static long btrfs_ioctl_rm_dev_v2(struct file
*file
, void __user
*arg
)
3016 struct inode
*inode
= file_inode(file
);
3017 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3018 struct btrfs_ioctl_vol_args_v2
*vol_args
;
3021 if (!capable(CAP_SYS_ADMIN
))
3024 ret
= mnt_want_write_file(file
);
3028 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
3029 if (IS_ERR(vol_args
)) {
3030 ret
= PTR_ERR(vol_args
);
3034 /* Check for compatibility reject unknown flags */
3035 if (vol_args
->flags
& ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED
) {
3040 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
3041 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3045 if (vol_args
->flags
& BTRFS_DEVICE_SPEC_BY_ID
) {
3046 ret
= btrfs_rm_device(fs_info
, NULL
, vol_args
->devid
);
3048 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
3049 ret
= btrfs_rm_device(fs_info
, vol_args
->name
, 0);
3051 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3054 if (vol_args
->flags
& BTRFS_DEVICE_SPEC_BY_ID
)
3055 btrfs_info(fs_info
, "device deleted: id %llu",
3058 btrfs_info(fs_info
, "device deleted: %s",
3064 mnt_drop_write_file(file
);
3068 static long btrfs_ioctl_rm_dev(struct file
*file
, void __user
*arg
)
3070 struct inode
*inode
= file_inode(file
);
3071 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3072 struct btrfs_ioctl_vol_args
*vol_args
;
3075 if (!capable(CAP_SYS_ADMIN
))
3078 ret
= mnt_want_write_file(file
);
3082 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
3083 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3084 goto out_drop_write
;
3087 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
3088 if (IS_ERR(vol_args
)) {
3089 ret
= PTR_ERR(vol_args
);
3093 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
3094 ret
= btrfs_rm_device(fs_info
, vol_args
->name
, 0);
3097 btrfs_info(fs_info
, "disk deleted %s", vol_args
->name
);
3100 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3102 mnt_drop_write_file(file
);
3107 static long btrfs_ioctl_fs_info(struct btrfs_fs_info
*fs_info
,
3110 struct btrfs_ioctl_fs_info_args
*fi_args
;
3111 struct btrfs_device
*device
;
3112 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
3115 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
3120 fi_args
->num_devices
= fs_devices
->num_devices
;
3122 list_for_each_entry_rcu(device
, &fs_devices
->devices
, dev_list
) {
3123 if (device
->devid
> fi_args
->max_id
)
3124 fi_args
->max_id
= device
->devid
;
3128 memcpy(&fi_args
->fsid
, fs_info
->fsid
, sizeof(fi_args
->fsid
));
3129 fi_args
->nodesize
= fs_info
->nodesize
;
3130 fi_args
->sectorsize
= fs_info
->sectorsize
;
3131 fi_args
->clone_alignment
= fs_info
->sectorsize
;
3133 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
3140 static long btrfs_ioctl_dev_info(struct btrfs_fs_info
*fs_info
,
3143 struct btrfs_ioctl_dev_info_args
*di_args
;
3144 struct btrfs_device
*dev
;
3146 char *s_uuid
= NULL
;
3148 di_args
= memdup_user(arg
, sizeof(*di_args
));
3149 if (IS_ERR(di_args
))
3150 return PTR_ERR(di_args
);
3152 if (!btrfs_is_empty_uuid(di_args
->uuid
))
3153 s_uuid
= di_args
->uuid
;
3156 dev
= btrfs_find_device(fs_info
, di_args
->devid
, s_uuid
, NULL
);
3163 di_args
->devid
= dev
->devid
;
3164 di_args
->bytes_used
= btrfs_device_get_bytes_used(dev
);
3165 di_args
->total_bytes
= btrfs_device_get_total_bytes(dev
);
3166 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
3168 struct rcu_string
*name
;
3170 name
= rcu_dereference(dev
->name
);
3171 strncpy(di_args
->path
, name
->str
, sizeof(di_args
->path
) - 1);
3172 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
3174 di_args
->path
[0] = '\0';
3179 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
3186 static struct page
*extent_same_get_page(struct inode
*inode
, pgoff_t index
)
3190 page
= grab_cache_page(inode
->i_mapping
, index
);
3192 return ERR_PTR(-ENOMEM
);
3194 if (!PageUptodate(page
)) {
3197 ret
= btrfs_readpage(NULL
, page
);
3199 return ERR_PTR(ret
);
3201 if (!PageUptodate(page
)) {
3204 return ERR_PTR(-EIO
);
3206 if (page
->mapping
!= inode
->i_mapping
) {
3209 return ERR_PTR(-EAGAIN
);
3216 static int gather_extent_pages(struct inode
*inode
, struct page
**pages
,
3217 int num_pages
, u64 off
)
3220 pgoff_t index
= off
>> PAGE_SHIFT
;
3222 for (i
= 0; i
< num_pages
; i
++) {
3224 pages
[i
] = extent_same_get_page(inode
, index
+ i
);
3225 if (IS_ERR(pages
[i
])) {
3226 int err
= PTR_ERR(pages
[i
]);
3237 static int lock_extent_range(struct inode
*inode
, u64 off
, u64 len
,
3238 bool retry_range_locking
)
3241 * Do any pending delalloc/csum calculations on inode, one way or
3242 * another, and lock file content.
3243 * The locking order is:
3246 * 2) range in the inode's io tree
3249 struct btrfs_ordered_extent
*ordered
;
3250 lock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
3251 ordered
= btrfs_lookup_first_ordered_extent(inode
,
3254 ordered
->file_offset
+ ordered
->len
<= off
||
3255 ordered
->file_offset
>= off
+ len
) &&
3256 !test_range_bit(&BTRFS_I(inode
)->io_tree
, off
,
3257 off
+ len
- 1, EXTENT_DELALLOC
, 0, NULL
)) {
3259 btrfs_put_ordered_extent(ordered
);
3262 unlock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
3264 btrfs_put_ordered_extent(ordered
);
3265 if (!retry_range_locking
)
3267 btrfs_wait_ordered_range(inode
, off
, len
);
3272 static void btrfs_double_inode_unlock(struct inode
*inode1
, struct inode
*inode2
)
3274 inode_unlock(inode1
);
3275 inode_unlock(inode2
);
3278 static void btrfs_double_inode_lock(struct inode
*inode1
, struct inode
*inode2
)
3280 if (inode1
< inode2
)
3281 swap(inode1
, inode2
);
3283 inode_lock_nested(inode1
, I_MUTEX_PARENT
);
3284 inode_lock_nested(inode2
, I_MUTEX_CHILD
);
3287 static void btrfs_double_extent_unlock(struct inode
*inode1
, u64 loff1
,
3288 struct inode
*inode2
, u64 loff2
, u64 len
)
3290 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
3291 unlock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
3294 static int btrfs_double_extent_lock(struct inode
*inode1
, u64 loff1
,
3295 struct inode
*inode2
, u64 loff2
, u64 len
,
3296 bool retry_range_locking
)
3300 if (inode1
< inode2
) {
3301 swap(inode1
, inode2
);
3304 ret
= lock_extent_range(inode1
, loff1
, len
, retry_range_locking
);
3307 ret
= lock_extent_range(inode2
, loff2
, len
, retry_range_locking
);
3309 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
,
3316 struct page
**src_pages
;
3317 struct page
**dst_pages
;
3320 static void btrfs_cmp_data_free(struct cmp_pages
*cmp
)
3325 for (i
= 0; i
< cmp
->num_pages
; i
++) {
3326 pg
= cmp
->src_pages
[i
];
3330 cmp
->src_pages
[i
] = NULL
;
3332 pg
= cmp
->dst_pages
[i
];
3336 cmp
->dst_pages
[i
] = NULL
;
3341 static int btrfs_cmp_data_prepare(struct inode
*src
, u64 loff
,
3342 struct inode
*dst
, u64 dst_loff
,
3343 u64 len
, struct cmp_pages
*cmp
)
3346 int num_pages
= PAGE_ALIGN(len
) >> PAGE_SHIFT
;
3348 cmp
->num_pages
= num_pages
;
3350 ret
= gather_extent_pages(src
, cmp
->src_pages
, num_pages
, loff
);
3354 ret
= gather_extent_pages(dst
, cmp
->dst_pages
, num_pages
, dst_loff
);
3358 btrfs_cmp_data_free(cmp
);
3362 static int btrfs_cmp_data(u64 len
, struct cmp_pages
*cmp
)
3366 struct page
*src_page
, *dst_page
;
3367 unsigned int cmp_len
= PAGE_SIZE
;
3368 void *addr
, *dst_addr
;
3372 if (len
< PAGE_SIZE
)
3375 BUG_ON(i
>= cmp
->num_pages
);
3377 src_page
= cmp
->src_pages
[i
];
3378 dst_page
= cmp
->dst_pages
[i
];
3379 ASSERT(PageLocked(src_page
));
3380 ASSERT(PageLocked(dst_page
));
3382 addr
= kmap_atomic(src_page
);
3383 dst_addr
= kmap_atomic(dst_page
);
3385 flush_dcache_page(src_page
);
3386 flush_dcache_page(dst_page
);
3388 if (memcmp(addr
, dst_addr
, cmp_len
))
3391 kunmap_atomic(addr
);
3392 kunmap_atomic(dst_addr
);
3404 static int extent_same_check_offsets(struct inode
*inode
, u64 off
, u64
*plen
,
3408 u64 bs
= BTRFS_I(inode
)->root
->fs_info
->sb
->s_blocksize
;
3410 if (off
+ olen
> inode
->i_size
|| off
+ olen
< off
)
3413 /* if we extend to eof, continue to block boundary */
3414 if (off
+ len
== inode
->i_size
)
3415 *plen
= len
= ALIGN(inode
->i_size
, bs
) - off
;
3417 /* Check that we are block aligned - btrfs_clone() requires this */
3418 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
))
3424 static int btrfs_extent_same_range(struct inode
*src
, u64 loff
, u64 olen
,
3425 struct inode
*dst
, u64 dst_loff
,
3426 struct cmp_pages
*cmp
)
3430 bool same_inode
= (src
== dst
);
3431 u64 same_lock_start
= 0;
3432 u64 same_lock_len
= 0;
3434 ret
= extent_same_check_offsets(src
, loff
, &len
, olen
);
3438 ret
= extent_same_check_offsets(dst
, dst_loff
, &len
, olen
);
3444 * Single inode case wants the same checks, except we
3445 * don't want our length pushed out past i_size as
3446 * comparing that data range makes no sense.
3448 * extent_same_check_offsets() will do this for an
3449 * unaligned length at i_size, so catch it here and
3450 * reject the request.
3452 * This effectively means we require aligned extents
3453 * for the single-inode case, whereas the other cases
3454 * allow an unaligned length so long as it ends at
3460 /* Check for overlapping ranges */
3461 if (dst_loff
+ len
> loff
&& dst_loff
< loff
+ len
)
3464 same_lock_start
= min_t(u64
, loff
, dst_loff
);
3465 same_lock_len
= max_t(u64
, loff
, dst_loff
) + len
- same_lock_start
;
3468 * If the source and destination inodes are different, the
3469 * source's range end offset matches the source's i_size, that
3470 * i_size is not a multiple of the sector size, and the
3471 * destination range does not go past the destination's i_size,
3472 * we must round down the length to the nearest sector size
3473 * multiple. If we don't do this adjustment we end replacing
3474 * with zeroes the bytes in the range that starts at the
3475 * deduplication range's end offset and ends at the next sector
3478 if (loff
+ olen
== i_size_read(src
) &&
3479 dst_loff
+ len
< i_size_read(dst
)) {
3480 const u64 sz
= BTRFS_I(src
)->root
->fs_info
->sectorsize
;
3482 len
= round_down(i_size_read(src
), sz
) - loff
;
3488 ret
= btrfs_cmp_data_prepare(src
, loff
, dst
, dst_loff
, olen
, cmp
);
3493 ret
= lock_extent_range(src
, same_lock_start
, same_lock_len
,
3496 ret
= btrfs_double_extent_lock(src
, loff
, dst
, dst_loff
, len
,
3499 * If one of the inodes has dirty pages in the respective range or
3500 * ordered extents, we need to flush dellaloc and wait for all ordered
3501 * extents in the range. We must unlock the pages and the ranges in the
3502 * io trees to avoid deadlocks when flushing delalloc (requires locking
3503 * pages) and when waiting for ordered extents to complete (they require
3506 if (ret
== -EAGAIN
) {
3508 * Ranges in the io trees already unlocked. Now unlock all
3509 * pages before waiting for all IO to complete.
3511 btrfs_cmp_data_free(cmp
);
3513 btrfs_wait_ordered_range(src
, same_lock_start
,
3516 btrfs_wait_ordered_range(src
, loff
, len
);
3517 btrfs_wait_ordered_range(dst
, dst_loff
, len
);
3523 /* ranges in the io trees already unlocked */
3524 btrfs_cmp_data_free(cmp
);
3528 /* pass original length for comparison so we stay within i_size */
3529 ret
= btrfs_cmp_data(olen
, cmp
);
3531 ret
= btrfs_clone(src
, dst
, loff
, olen
, len
, dst_loff
, 1);
3534 unlock_extent(&BTRFS_I(src
)->io_tree
, same_lock_start
,
3535 same_lock_start
+ same_lock_len
- 1);
3537 btrfs_double_extent_unlock(src
, loff
, dst
, dst_loff
, len
);
3539 btrfs_cmp_data_free(cmp
);
3544 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3546 static int btrfs_extent_same(struct inode
*src
, u64 loff
, u64 olen
,
3547 struct inode
*dst
, u64 dst_loff
)
3550 struct cmp_pages cmp
;
3551 int num_pages
= PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN
) >> PAGE_SHIFT
;
3552 bool same_inode
= (src
== dst
);
3553 u64 i
, tail_len
, chunk_count
;
3561 btrfs_double_inode_lock(src
, dst
);
3563 /* don't make the dst file partly checksummed */
3564 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3565 (BTRFS_I(dst
)->flags
& BTRFS_INODE_NODATASUM
)) {
3570 tail_len
= olen
% BTRFS_MAX_DEDUPE_LEN
;
3571 chunk_count
= div_u64(olen
, BTRFS_MAX_DEDUPE_LEN
);
3572 if (chunk_count
== 0)
3573 num_pages
= PAGE_ALIGN(tail_len
) >> PAGE_SHIFT
;
3576 * If deduping ranges in the same inode, locking rules make it
3577 * mandatory to always lock pages in ascending order to avoid deadlocks
3578 * with concurrent tasks (such as starting writeback/delalloc).
3580 if (same_inode
&& dst_loff
< loff
)
3581 swap(loff
, dst_loff
);
3584 * We must gather up all the pages before we initiate our extent
3585 * locking. We use an array for the page pointers. Size of the array is
3586 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
3588 cmp
.src_pages
= kvmalloc_array(num_pages
, sizeof(struct page
*),
3589 GFP_KERNEL
| __GFP_ZERO
);
3590 cmp
.dst_pages
= kvmalloc_array(num_pages
, sizeof(struct page
*),
3591 GFP_KERNEL
| __GFP_ZERO
);
3592 if (!cmp
.src_pages
|| !cmp
.dst_pages
) {
3597 for (i
= 0; i
< chunk_count
; i
++) {
3598 ret
= btrfs_extent_same_range(src
, loff
, BTRFS_MAX_DEDUPE_LEN
,
3599 dst
, dst_loff
, &cmp
);
3603 loff
+= BTRFS_MAX_DEDUPE_LEN
;
3604 dst_loff
+= BTRFS_MAX_DEDUPE_LEN
;
3608 ret
= btrfs_extent_same_range(src
, loff
, tail_len
, dst
,
3612 kvfree(cmp
.src_pages
);
3613 kvfree(cmp
.dst_pages
);
3619 btrfs_double_inode_unlock(src
, dst
);
3624 ssize_t
btrfs_dedupe_file_range(struct file
*src_file
, u64 loff
, u64 olen
,
3625 struct file
*dst_file
, u64 dst_loff
)
3627 struct inode
*src
= file_inode(src_file
);
3628 struct inode
*dst
= file_inode(dst_file
);
3629 u64 bs
= BTRFS_I(src
)->root
->fs_info
->sb
->s_blocksize
;
3632 if (WARN_ON_ONCE(bs
< PAGE_SIZE
)) {
3634 * Btrfs does not support blocksize < page_size. As a
3635 * result, btrfs_cmp_data() won't correctly handle
3636 * this situation without an update.
3641 res
= btrfs_extent_same(src
, loff
, olen
, dst
, dst_loff
);
3647 static int clone_finish_inode_update(struct btrfs_trans_handle
*trans
,
3648 struct inode
*inode
,
3654 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3657 inode_inc_iversion(inode
);
3658 if (!no_time_update
)
3659 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
3661 * We round up to the block size at eof when determining which
3662 * extents to clone above, but shouldn't round up the file size.
3664 if (endoff
> destoff
+ olen
)
3665 endoff
= destoff
+ olen
;
3666 if (endoff
> inode
->i_size
)
3667 btrfs_i_size_write(BTRFS_I(inode
), endoff
);
3669 ret
= btrfs_update_inode(trans
, root
, inode
);
3671 btrfs_abort_transaction(trans
, ret
);
3672 btrfs_end_transaction(trans
);
3675 ret
= btrfs_end_transaction(trans
);
3680 static void clone_update_extent_map(struct btrfs_inode
*inode
,
3681 const struct btrfs_trans_handle
*trans
,
3682 const struct btrfs_path
*path
,
3683 const u64 hole_offset
,
3686 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
3687 struct extent_map
*em
;
3690 em
= alloc_extent_map();
3692 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &inode
->runtime_flags
);
3697 struct btrfs_file_extent_item
*fi
;
3699 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3700 struct btrfs_file_extent_item
);
3701 btrfs_extent_item_to_extent_map(inode
, path
, fi
, false, em
);
3702 em
->generation
= -1;
3703 if (btrfs_file_extent_type(path
->nodes
[0], fi
) ==
3704 BTRFS_FILE_EXTENT_INLINE
)
3705 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3706 &inode
->runtime_flags
);
3708 em
->start
= hole_offset
;
3710 em
->ram_bytes
= em
->len
;
3711 em
->orig_start
= hole_offset
;
3712 em
->block_start
= EXTENT_MAP_HOLE
;
3714 em
->orig_block_len
= 0;
3715 em
->compress_type
= BTRFS_COMPRESS_NONE
;
3716 em
->generation
= trans
->transid
;
3720 write_lock(&em_tree
->lock
);
3721 ret
= add_extent_mapping(em_tree
, em
, 1);
3722 write_unlock(&em_tree
->lock
);
3723 if (ret
!= -EEXIST
) {
3724 free_extent_map(em
);
3727 btrfs_drop_extent_cache(inode
, em
->start
,
3728 em
->start
+ em
->len
- 1, 0);
3732 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &inode
->runtime_flags
);
3736 * Make sure we do not end up inserting an inline extent into a file that has
3737 * already other (non-inline) extents. If a file has an inline extent it can
3738 * not have any other extents and the (single) inline extent must start at the
3739 * file offset 0. Failing to respect these rules will lead to file corruption,
3740 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3742 * We can have extents that have been already written to disk or we can have
3743 * dirty ranges still in delalloc, in which case the extent maps and items are
3744 * created only when we run delalloc, and the delalloc ranges might fall outside
3745 * the range we are currently locking in the inode's io tree. So we check the
3746 * inode's i_size because of that (i_size updates are done while holding the
3747 * i_mutex, which we are holding here).
3748 * We also check to see if the inode has a size not greater than "datal" but has
3749 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3750 * protected against such concurrent fallocate calls by the i_mutex).
3752 * If the file has no extents but a size greater than datal, do not allow the
3753 * copy because we would need turn the inline extent into a non-inline one (even
3754 * with NO_HOLES enabled). If we find our destination inode only has one inline
3755 * extent, just overwrite it with the source inline extent if its size is less
3756 * than the source extent's size, or we could copy the source inline extent's
3757 * data into the destination inode's inline extent if the later is greater then
3760 static int clone_copy_inline_extent(struct inode
*dst
,
3761 struct btrfs_trans_handle
*trans
,
3762 struct btrfs_path
*path
,
3763 struct btrfs_key
*new_key
,
3764 const u64 drop_start
,
3770 struct btrfs_fs_info
*fs_info
= btrfs_sb(dst
->i_sb
);
3771 struct btrfs_root
*root
= BTRFS_I(dst
)->root
;
3772 const u64 aligned_end
= ALIGN(new_key
->offset
+ datal
,
3773 fs_info
->sectorsize
);
3775 struct btrfs_key key
;
3777 if (new_key
->offset
> 0)
3780 key
.objectid
= btrfs_ino(BTRFS_I(dst
));
3781 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3783 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3786 } else if (ret
> 0) {
3787 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
3788 ret
= btrfs_next_leaf(root
, path
);
3792 goto copy_inline_extent
;
3794 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3795 if (key
.objectid
== btrfs_ino(BTRFS_I(dst
)) &&
3796 key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3797 ASSERT(key
.offset
> 0);
3800 } else if (i_size_read(dst
) <= datal
) {
3801 struct btrfs_file_extent_item
*ei
;
3805 * If the file size is <= datal, make sure there are no other
3806 * extents following (can happen do to an fallocate call with
3807 * the flag FALLOC_FL_KEEP_SIZE).
3809 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3810 struct btrfs_file_extent_item
);
3812 * If it's an inline extent, it can not have other extents
3815 if (btrfs_file_extent_type(path
->nodes
[0], ei
) ==
3816 BTRFS_FILE_EXTENT_INLINE
)
3817 goto copy_inline_extent
;
3819 ext_len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
3820 if (ext_len
> aligned_end
)
3823 ret
= btrfs_next_item(root
, path
);
3826 } else if (ret
== 0) {
3827 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3829 if (key
.objectid
== btrfs_ino(BTRFS_I(dst
)) &&
3830 key
.type
== BTRFS_EXTENT_DATA_KEY
)
3837 * We have no extent items, or we have an extent at offset 0 which may
3838 * or may not be inlined. All these cases are dealt the same way.
3840 if (i_size_read(dst
) > datal
) {
3842 * If the destination inode has an inline extent...
3843 * This would require copying the data from the source inline
3844 * extent into the beginning of the destination's inline extent.
3845 * But this is really complex, both extents can be compressed
3846 * or just one of them, which would require decompressing and
3847 * re-compressing data (which could increase the new compressed
3848 * size, not allowing the compressed data to fit anymore in an
3850 * So just don't support this case for now (it should be rare,
3851 * we are not really saving space when cloning inline extents).
3856 btrfs_release_path(path
);
3857 ret
= btrfs_drop_extents(trans
, root
, dst
, drop_start
, aligned_end
, 1);
3860 ret
= btrfs_insert_empty_item(trans
, root
, path
, new_key
, size
);
3865 const u32 start
= btrfs_file_extent_calc_inline_size(0);
3867 memmove(inline_data
+ start
, inline_data
+ start
+ skip
, datal
);
3870 write_extent_buffer(path
->nodes
[0], inline_data
,
3871 btrfs_item_ptr_offset(path
->nodes
[0],
3874 inode_add_bytes(dst
, datal
);
3880 * btrfs_clone() - clone a range from inode file to another
3882 * @src: Inode to clone from
3883 * @inode: Inode to clone to
3884 * @off: Offset within source to start clone from
3885 * @olen: Original length, passed by user, of range to clone
3886 * @olen_aligned: Block-aligned value of olen
3887 * @destoff: Offset within @inode to start clone
3888 * @no_time_update: Whether to update mtime/ctime on the target inode
3890 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
3891 const u64 off
, const u64 olen
, const u64 olen_aligned
,
3892 const u64 destoff
, int no_time_update
)
3894 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3895 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3896 struct btrfs_path
*path
= NULL
;
3897 struct extent_buffer
*leaf
;
3898 struct btrfs_trans_handle
*trans
;
3900 struct btrfs_key key
;
3904 const u64 len
= olen_aligned
;
3905 u64 last_dest_end
= destoff
;
3908 buf
= kvmalloc(fs_info
->nodesize
, GFP_KERNEL
);
3912 path
= btrfs_alloc_path();
3918 path
->reada
= READA_FORWARD
;
3920 key
.objectid
= btrfs_ino(BTRFS_I(src
));
3921 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3925 u64 next_key_min_offset
= key
.offset
+ 1;
3928 * note the key will change type as we walk through the
3931 path
->leave_spinning
= 1;
3932 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
3937 * First search, if no extent item that starts at offset off was
3938 * found but the previous item is an extent item, it's possible
3939 * it might overlap our target range, therefore process it.
3941 if (key
.offset
== off
&& ret
> 0 && path
->slots
[0] > 0) {
3942 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3943 path
->slots
[0] - 1);
3944 if (key
.type
== BTRFS_EXTENT_DATA_KEY
)
3948 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3950 if (path
->slots
[0] >= nritems
) {
3951 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
3956 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3958 leaf
= path
->nodes
[0];
3959 slot
= path
->slots
[0];
3961 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3962 if (key
.type
> BTRFS_EXTENT_DATA_KEY
||
3963 key
.objectid
!= btrfs_ino(BTRFS_I(src
)))
3966 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3967 struct btrfs_file_extent_item
*extent
;
3970 struct btrfs_key new_key
;
3971 u64 disko
= 0, diskl
= 0;
3972 u64 datao
= 0, datal
= 0;
3976 extent
= btrfs_item_ptr(leaf
, slot
,
3977 struct btrfs_file_extent_item
);
3978 comp
= btrfs_file_extent_compression(leaf
, extent
);
3979 type
= btrfs_file_extent_type(leaf
, extent
);
3980 if (type
== BTRFS_FILE_EXTENT_REG
||
3981 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3982 disko
= btrfs_file_extent_disk_bytenr(leaf
,
3984 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
3986 datao
= btrfs_file_extent_offset(leaf
, extent
);
3987 datal
= btrfs_file_extent_num_bytes(leaf
,
3989 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3990 /* take upper bound, may be compressed */
3991 datal
= btrfs_file_extent_ram_bytes(leaf
,
3996 * The first search might have left us at an extent
3997 * item that ends before our target range's start, can
3998 * happen if we have holes and NO_HOLES feature enabled.
4000 if (key
.offset
+ datal
<= off
) {
4003 } else if (key
.offset
>= off
+ len
) {
4006 next_key_min_offset
= key
.offset
+ datal
;
4007 size
= btrfs_item_size_nr(leaf
, slot
);
4008 read_extent_buffer(leaf
, buf
,
4009 btrfs_item_ptr_offset(leaf
, slot
),
4012 btrfs_release_path(path
);
4013 path
->leave_spinning
= 0;
4015 memcpy(&new_key
, &key
, sizeof(new_key
));
4016 new_key
.objectid
= btrfs_ino(BTRFS_I(inode
));
4017 if (off
<= key
.offset
)
4018 new_key
.offset
= key
.offset
+ destoff
- off
;
4020 new_key
.offset
= destoff
;
4023 * Deal with a hole that doesn't have an extent item
4024 * that represents it (NO_HOLES feature enabled).
4025 * This hole is either in the middle of the cloning
4026 * range or at the beginning (fully overlaps it or
4027 * partially overlaps it).
4029 if (new_key
.offset
!= last_dest_end
)
4030 drop_start
= last_dest_end
;
4032 drop_start
= new_key
.offset
;
4035 * 1 - adjusting old extent (we may have to split it)
4036 * 1 - add new extent
4039 trans
= btrfs_start_transaction(root
, 3);
4040 if (IS_ERR(trans
)) {
4041 ret
= PTR_ERR(trans
);
4045 if (type
== BTRFS_FILE_EXTENT_REG
||
4046 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
4048 * a | --- range to clone ---| b
4049 * | ------------- extent ------------- |
4052 /* subtract range b */
4053 if (key
.offset
+ datal
> off
+ len
)
4054 datal
= off
+ len
- key
.offset
;
4056 /* subtract range a */
4057 if (off
> key
.offset
) {
4058 datao
+= off
- key
.offset
;
4059 datal
-= off
- key
.offset
;
4062 ret
= btrfs_drop_extents(trans
, root
, inode
,
4064 new_key
.offset
+ datal
,
4067 if (ret
!= -EOPNOTSUPP
)
4068 btrfs_abort_transaction(trans
,
4070 btrfs_end_transaction(trans
);
4074 ret
= btrfs_insert_empty_item(trans
, root
, path
,
4077 btrfs_abort_transaction(trans
, ret
);
4078 btrfs_end_transaction(trans
);
4082 leaf
= path
->nodes
[0];
4083 slot
= path
->slots
[0];
4084 write_extent_buffer(leaf
, buf
,
4085 btrfs_item_ptr_offset(leaf
, slot
),
4088 extent
= btrfs_item_ptr(leaf
, slot
,
4089 struct btrfs_file_extent_item
);
4091 /* disko == 0 means it's a hole */
4095 btrfs_set_file_extent_offset(leaf
, extent
,
4097 btrfs_set_file_extent_num_bytes(leaf
, extent
,
4101 inode_add_bytes(inode
, datal
);
4102 ret
= btrfs_inc_extent_ref(trans
,
4105 root
->root_key
.objectid
,
4106 btrfs_ino(BTRFS_I(inode
)),
4107 new_key
.offset
- datao
);
4109 btrfs_abort_transaction(trans
,
4111 btrfs_end_transaction(trans
);
4116 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4120 if (off
> key
.offset
) {
4121 skip
= off
- key
.offset
;
4122 new_key
.offset
+= skip
;
4125 if (key
.offset
+ datal
> off
+ len
)
4126 trim
= key
.offset
+ datal
- (off
+ len
);
4128 if (comp
&& (skip
|| trim
)) {
4130 btrfs_end_transaction(trans
);
4133 size
-= skip
+ trim
;
4134 datal
-= skip
+ trim
;
4136 ret
= clone_copy_inline_extent(inode
,
4143 if (ret
!= -EOPNOTSUPP
)
4144 btrfs_abort_transaction(trans
,
4146 btrfs_end_transaction(trans
);
4149 leaf
= path
->nodes
[0];
4150 slot
= path
->slots
[0];
4153 /* If we have an implicit hole (NO_HOLES feature). */
4154 if (drop_start
< new_key
.offset
)
4155 clone_update_extent_map(BTRFS_I(inode
), trans
,
4157 new_key
.offset
- drop_start
);
4159 clone_update_extent_map(BTRFS_I(inode
), trans
,
4162 btrfs_mark_buffer_dirty(leaf
);
4163 btrfs_release_path(path
);
4165 last_dest_end
= ALIGN(new_key
.offset
+ datal
,
4166 fs_info
->sectorsize
);
4167 ret
= clone_finish_inode_update(trans
, inode
,
4173 if (new_key
.offset
+ datal
>= destoff
+ len
)
4176 btrfs_release_path(path
);
4177 key
.offset
= next_key_min_offset
;
4179 if (fatal_signal_pending(current
)) {
4186 if (last_dest_end
< destoff
+ len
) {
4188 * We have an implicit hole (NO_HOLES feature is enabled) that
4189 * fully or partially overlaps our cloning range at its end.
4191 btrfs_release_path(path
);
4194 * 1 - remove extent(s)
4197 trans
= btrfs_start_transaction(root
, 2);
4198 if (IS_ERR(trans
)) {
4199 ret
= PTR_ERR(trans
);
4202 ret
= btrfs_drop_extents(trans
, root
, inode
,
4203 last_dest_end
, destoff
+ len
, 1);
4205 if (ret
!= -EOPNOTSUPP
)
4206 btrfs_abort_transaction(trans
, ret
);
4207 btrfs_end_transaction(trans
);
4210 clone_update_extent_map(BTRFS_I(inode
), trans
, NULL
,
4212 destoff
+ len
- last_dest_end
);
4213 ret
= clone_finish_inode_update(trans
, inode
, destoff
+ len
,
4214 destoff
, olen
, no_time_update
);
4218 btrfs_free_path(path
);
4223 static noinline
int btrfs_clone_files(struct file
*file
, struct file
*file_src
,
4224 u64 off
, u64 olen
, u64 destoff
)
4226 struct inode
*inode
= file_inode(file
);
4227 struct inode
*src
= file_inode(file_src
);
4228 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4229 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4232 u64 bs
= fs_info
->sb
->s_blocksize
;
4233 int same_inode
= src
== inode
;
4237 * - split compressed inline extents. annoying: we need to
4238 * decompress into destination's address_space (the file offset
4239 * may change, so source mapping won't do), then recompress (or
4240 * otherwise reinsert) a subrange.
4242 * - split destination inode's inline extents. The inline extents can
4243 * be either compressed or non-compressed.
4246 if (btrfs_root_readonly(root
))
4249 if (file_src
->f_path
.mnt
!= file
->f_path
.mnt
||
4250 src
->i_sb
!= inode
->i_sb
)
4253 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
4257 btrfs_double_inode_lock(src
, inode
);
4262 /* don't make the dst file partly checksummed */
4263 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
4264 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
4269 /* determine range to clone */
4271 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
4274 olen
= len
= src
->i_size
- off
;
4275 /* if we extend to eof, continue to block boundary */
4276 if (off
+ len
== src
->i_size
)
4277 len
= ALIGN(src
->i_size
, bs
) - off
;
4284 /* verify the end result is block aligned */
4285 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
4286 !IS_ALIGNED(destoff
, bs
))
4289 /* verify if ranges are overlapped within the same file */
4291 if (destoff
+ len
> off
&& destoff
< off
+ len
)
4295 if (destoff
> inode
->i_size
) {
4296 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
4302 * Lock the target range too. Right after we replace the file extent
4303 * items in the fs tree (which now point to the cloned data), we might
4304 * have a worker replace them with extent items relative to a write
4305 * operation that was issued before this clone operation (i.e. confront
4306 * with inode.c:btrfs_finish_ordered_io).
4309 u64 lock_start
= min_t(u64
, off
, destoff
);
4310 u64 lock_len
= max_t(u64
, off
, destoff
) + len
- lock_start
;
4312 ret
= lock_extent_range(src
, lock_start
, lock_len
, true);
4314 ret
= btrfs_double_extent_lock(src
, off
, inode
, destoff
, len
,
4319 /* ranges in the io trees already unlocked */
4323 ret
= btrfs_clone(src
, inode
, off
, olen
, len
, destoff
, 0);
4326 u64 lock_start
= min_t(u64
, off
, destoff
);
4327 u64 lock_end
= max_t(u64
, off
, destoff
) + len
- 1;
4329 unlock_extent(&BTRFS_I(src
)->io_tree
, lock_start
, lock_end
);
4331 btrfs_double_extent_unlock(src
, off
, inode
, destoff
, len
);
4334 * Truncate page cache pages so that future reads will see the cloned
4335 * data immediately and not the previous data.
4337 truncate_inode_pages_range(&inode
->i_data
,
4338 round_down(destoff
, PAGE_SIZE
),
4339 round_up(destoff
+ len
, PAGE_SIZE
) - 1);
4342 btrfs_double_inode_unlock(src
, inode
);
4348 int btrfs_clone_file_range(struct file
*src_file
, loff_t off
,
4349 struct file
*dst_file
, loff_t destoff
, u64 len
)
4351 return btrfs_clone_files(dst_file
, src_file
, off
, len
, destoff
);
4354 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
4356 struct inode
*inode
= file_inode(file
);
4357 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4358 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4359 struct btrfs_root
*new_root
;
4360 struct btrfs_dir_item
*di
;
4361 struct btrfs_trans_handle
*trans
;
4362 struct btrfs_path
*path
;
4363 struct btrfs_key location
;
4364 struct btrfs_disk_key disk_key
;
4369 if (!capable(CAP_SYS_ADMIN
))
4372 ret
= mnt_want_write_file(file
);
4376 if (copy_from_user(&objectid
, argp
, sizeof(objectid
))) {
4382 objectid
= BTRFS_FS_TREE_OBJECTID
;
4384 location
.objectid
= objectid
;
4385 location
.type
= BTRFS_ROOT_ITEM_KEY
;
4386 location
.offset
= (u64
)-1;
4388 new_root
= btrfs_read_fs_root_no_name(fs_info
, &location
);
4389 if (IS_ERR(new_root
)) {
4390 ret
= PTR_ERR(new_root
);
4393 if (!is_fstree(new_root
->objectid
)) {
4398 path
= btrfs_alloc_path();
4403 path
->leave_spinning
= 1;
4405 trans
= btrfs_start_transaction(root
, 1);
4406 if (IS_ERR(trans
)) {
4407 btrfs_free_path(path
);
4408 ret
= PTR_ERR(trans
);
4412 dir_id
= btrfs_super_root_dir(fs_info
->super_copy
);
4413 di
= btrfs_lookup_dir_item(trans
, fs_info
->tree_root
, path
,
4414 dir_id
, "default", 7, 1);
4415 if (IS_ERR_OR_NULL(di
)) {
4416 btrfs_free_path(path
);
4417 btrfs_end_transaction(trans
);
4419 "Umm, you don't have the default diritem, this isn't going to work");
4424 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
4425 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
4426 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4427 btrfs_free_path(path
);
4429 btrfs_set_fs_incompat(fs_info
, DEFAULT_SUBVOL
);
4430 btrfs_end_transaction(trans
);
4432 mnt_drop_write_file(file
);
4436 static void get_block_group_info(struct list_head
*groups_list
,
4437 struct btrfs_ioctl_space_info
*space
)
4439 struct btrfs_block_group_cache
*block_group
;
4441 space
->total_bytes
= 0;
4442 space
->used_bytes
= 0;
4444 list_for_each_entry(block_group
, groups_list
, list
) {
4445 space
->flags
= block_group
->flags
;
4446 space
->total_bytes
+= block_group
->key
.offset
;
4447 space
->used_bytes
+=
4448 btrfs_block_group_used(&block_group
->item
);
4452 static long btrfs_ioctl_space_info(struct btrfs_fs_info
*fs_info
,
4455 struct btrfs_ioctl_space_args space_args
;
4456 struct btrfs_ioctl_space_info space
;
4457 struct btrfs_ioctl_space_info
*dest
;
4458 struct btrfs_ioctl_space_info
*dest_orig
;
4459 struct btrfs_ioctl_space_info __user
*user_dest
;
4460 struct btrfs_space_info
*info
;
4461 static const u64 types
[] = {
4462 BTRFS_BLOCK_GROUP_DATA
,
4463 BTRFS_BLOCK_GROUP_SYSTEM
,
4464 BTRFS_BLOCK_GROUP_METADATA
,
4465 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
4473 if (copy_from_user(&space_args
,
4474 (struct btrfs_ioctl_space_args __user
*)arg
,
4475 sizeof(space_args
)))
4478 for (i
= 0; i
< num_types
; i
++) {
4479 struct btrfs_space_info
*tmp
;
4483 list_for_each_entry_rcu(tmp
, &fs_info
->space_info
,
4485 if (tmp
->flags
== types
[i
]) {
4495 down_read(&info
->groups_sem
);
4496 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4497 if (!list_empty(&info
->block_groups
[c
]))
4500 up_read(&info
->groups_sem
);
4504 * Global block reserve, exported as a space_info
4508 /* space_slots == 0 means they are asking for a count */
4509 if (space_args
.space_slots
== 0) {
4510 space_args
.total_spaces
= slot_count
;
4514 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
4516 alloc_size
= sizeof(*dest
) * slot_count
;
4518 /* we generally have at most 6 or so space infos, one for each raid
4519 * level. So, a whole page should be more than enough for everyone
4521 if (alloc_size
> PAGE_SIZE
)
4524 space_args
.total_spaces
= 0;
4525 dest
= kmalloc(alloc_size
, GFP_KERNEL
);
4530 /* now we have a buffer to copy into */
4531 for (i
= 0; i
< num_types
; i
++) {
4532 struct btrfs_space_info
*tmp
;
4539 list_for_each_entry_rcu(tmp
, &fs_info
->space_info
,
4541 if (tmp
->flags
== types
[i
]) {
4550 down_read(&info
->groups_sem
);
4551 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4552 if (!list_empty(&info
->block_groups
[c
])) {
4553 get_block_group_info(&info
->block_groups
[c
],
4555 memcpy(dest
, &space
, sizeof(space
));
4557 space_args
.total_spaces
++;
4563 up_read(&info
->groups_sem
);
4567 * Add global block reserve
4570 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
4572 spin_lock(&block_rsv
->lock
);
4573 space
.total_bytes
= block_rsv
->size
;
4574 space
.used_bytes
= block_rsv
->size
- block_rsv
->reserved
;
4575 spin_unlock(&block_rsv
->lock
);
4576 space
.flags
= BTRFS_SPACE_INFO_GLOBAL_RSV
;
4577 memcpy(dest
, &space
, sizeof(space
));
4578 space_args
.total_spaces
++;
4581 user_dest
= (struct btrfs_ioctl_space_info __user
*)
4582 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
4584 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
4589 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
4595 static noinline
long btrfs_ioctl_start_sync(struct btrfs_root
*root
,
4598 struct btrfs_trans_handle
*trans
;
4602 trans
= btrfs_attach_transaction_barrier(root
);
4603 if (IS_ERR(trans
)) {
4604 if (PTR_ERR(trans
) != -ENOENT
)
4605 return PTR_ERR(trans
);
4607 /* No running transaction, don't bother */
4608 transid
= root
->fs_info
->last_trans_committed
;
4611 transid
= trans
->transid
;
4612 ret
= btrfs_commit_transaction_async(trans
, 0);
4614 btrfs_end_transaction(trans
);
4619 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
4624 static noinline
long btrfs_ioctl_wait_sync(struct btrfs_fs_info
*fs_info
,
4630 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
4633 transid
= 0; /* current trans */
4635 return btrfs_wait_for_commit(fs_info
, transid
);
4638 static long btrfs_ioctl_scrub(struct file
*file
, void __user
*arg
)
4640 struct btrfs_fs_info
*fs_info
= btrfs_sb(file_inode(file
)->i_sb
);
4641 struct btrfs_ioctl_scrub_args
*sa
;
4644 if (!capable(CAP_SYS_ADMIN
))
4647 sa
= memdup_user(arg
, sizeof(*sa
));
4651 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
)) {
4652 ret
= mnt_want_write_file(file
);
4657 ret
= btrfs_scrub_dev(fs_info
, sa
->devid
, sa
->start
, sa
->end
,
4658 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
,
4661 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4664 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
))
4665 mnt_drop_write_file(file
);
4671 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info
*fs_info
)
4673 if (!capable(CAP_SYS_ADMIN
))
4676 return btrfs_scrub_cancel(fs_info
);
4679 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info
*fs_info
,
4682 struct btrfs_ioctl_scrub_args
*sa
;
4685 if (!capable(CAP_SYS_ADMIN
))
4688 sa
= memdup_user(arg
, sizeof(*sa
));
4692 ret
= btrfs_scrub_progress(fs_info
, sa
->devid
, &sa
->progress
);
4694 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4701 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info
*fs_info
,
4704 struct btrfs_ioctl_get_dev_stats
*sa
;
4707 sa
= memdup_user(arg
, sizeof(*sa
));
4711 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
4716 ret
= btrfs_get_dev_stats(fs_info
, sa
);
4718 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4725 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info
*fs_info
,
4728 struct btrfs_ioctl_dev_replace_args
*p
;
4731 if (!capable(CAP_SYS_ADMIN
))
4734 p
= memdup_user(arg
, sizeof(*p
));
4739 case BTRFS_IOCTL_DEV_REPLACE_CMD_START
:
4740 if (sb_rdonly(fs_info
->sb
)) {
4744 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
4745 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
4747 ret
= btrfs_dev_replace_by_ioctl(fs_info
, p
);
4748 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4751 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
:
4752 btrfs_dev_replace_status(fs_info
, p
);
4755 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
:
4756 p
->result
= btrfs_dev_replace_cancel(fs_info
);
4764 if (copy_to_user(arg
, p
, sizeof(*p
)))
4771 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
4777 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
4778 struct inode_fs_paths
*ipath
= NULL
;
4779 struct btrfs_path
*path
;
4781 if (!capable(CAP_DAC_READ_SEARCH
))
4784 path
= btrfs_alloc_path();
4790 ipa
= memdup_user(arg
, sizeof(*ipa
));
4797 size
= min_t(u32
, ipa
->size
, 4096);
4798 ipath
= init_ipath(size
, root
, path
);
4799 if (IS_ERR(ipath
)) {
4800 ret
= PTR_ERR(ipath
);
4805 ret
= paths_from_inode(ipa
->inum
, ipath
);
4809 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
4810 rel_ptr
= ipath
->fspath
->val
[i
] -
4811 (u64
)(unsigned long)ipath
->fspath
->val
;
4812 ipath
->fspath
->val
[i
] = rel_ptr
;
4815 ret
= copy_to_user((void __user
*)(unsigned long)ipa
->fspath
,
4816 ipath
->fspath
, size
);
4823 btrfs_free_path(path
);
4830 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
4832 struct btrfs_data_container
*inodes
= ctx
;
4833 const size_t c
= 3 * sizeof(u64
);
4835 if (inodes
->bytes_left
>= c
) {
4836 inodes
->bytes_left
-= c
;
4837 inodes
->val
[inodes
->elem_cnt
] = inum
;
4838 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
4839 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
4840 inodes
->elem_cnt
+= 3;
4842 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
4843 inodes
->bytes_left
= 0;
4844 inodes
->elem_missed
+= 3;
4850 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info
*fs_info
,
4851 void __user
*arg
, int version
)
4855 struct btrfs_ioctl_logical_ino_args
*loi
;
4856 struct btrfs_data_container
*inodes
= NULL
;
4857 struct btrfs_path
*path
= NULL
;
4860 if (!capable(CAP_SYS_ADMIN
))
4863 loi
= memdup_user(arg
, sizeof(*loi
));
4865 return PTR_ERR(loi
);
4868 ignore_offset
= false;
4869 size
= min_t(u32
, loi
->size
, SZ_64K
);
4871 /* All reserved bits must be 0 for now */
4872 if (memchr_inv(loi
->reserved
, 0, sizeof(loi
->reserved
))) {
4876 /* Only accept flags we have defined so far */
4877 if (loi
->flags
& ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET
)) {
4881 ignore_offset
= loi
->flags
& BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET
;
4882 size
= min_t(u32
, loi
->size
, SZ_16M
);
4885 path
= btrfs_alloc_path();
4891 inodes
= init_data_container(size
);
4892 if (IS_ERR(inodes
)) {
4893 ret
= PTR_ERR(inodes
);
4898 ret
= iterate_inodes_from_logical(loi
->logical
, fs_info
, path
,
4899 build_ino_list
, inodes
, ignore_offset
);
4905 ret
= copy_to_user((void __user
*)(unsigned long)loi
->inodes
, inodes
,
4911 btrfs_free_path(path
);
4919 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
,
4920 struct btrfs_ioctl_balance_args
*bargs
)
4922 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
4924 bargs
->flags
= bctl
->flags
;
4926 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
))
4927 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
4928 if (atomic_read(&fs_info
->balance_pause_req
))
4929 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
4930 if (atomic_read(&fs_info
->balance_cancel_req
))
4931 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
4933 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
4934 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
4935 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
4937 spin_lock(&fs_info
->balance_lock
);
4938 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
4939 spin_unlock(&fs_info
->balance_lock
);
4942 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
4944 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4945 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4946 struct btrfs_ioctl_balance_args
*bargs
;
4947 struct btrfs_balance_control
*bctl
;
4948 bool need_unlock
; /* for mut. excl. ops lock */
4951 if (!capable(CAP_SYS_ADMIN
))
4954 ret
= mnt_want_write_file(file
);
4959 if (!test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
4960 mutex_lock(&fs_info
->balance_mutex
);
4966 * mut. excl. ops lock is locked. Three possibilities:
4967 * (1) some other op is running
4968 * (2) balance is running
4969 * (3) balance is paused -- special case (think resume)
4971 mutex_lock(&fs_info
->balance_mutex
);
4972 if (fs_info
->balance_ctl
) {
4973 /* this is either (2) or (3) */
4974 if (!test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4975 mutex_unlock(&fs_info
->balance_mutex
);
4977 * Lock released to allow other waiters to continue,
4978 * we'll reexamine the status again.
4980 mutex_lock(&fs_info
->balance_mutex
);
4982 if (fs_info
->balance_ctl
&&
4983 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4985 need_unlock
= false;
4989 mutex_unlock(&fs_info
->balance_mutex
);
4993 mutex_unlock(&fs_info
->balance_mutex
);
4999 mutex_unlock(&fs_info
->balance_mutex
);
5000 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
5005 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
));
5008 bargs
= memdup_user(arg
, sizeof(*bargs
));
5009 if (IS_ERR(bargs
)) {
5010 ret
= PTR_ERR(bargs
);
5014 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
5015 if (!fs_info
->balance_ctl
) {
5020 bctl
= fs_info
->balance_ctl
;
5021 spin_lock(&fs_info
->balance_lock
);
5022 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
5023 spin_unlock(&fs_info
->balance_lock
);
5031 if (fs_info
->balance_ctl
) {
5036 bctl
= kzalloc(sizeof(*bctl
), GFP_KERNEL
);
5043 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
5044 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
5045 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
5047 bctl
->flags
= bargs
->flags
;
5049 /* balance everything - no filters */
5050 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
5053 if (bctl
->flags
& ~(BTRFS_BALANCE_ARGS_MASK
| BTRFS_BALANCE_TYPE_MASK
)) {
5060 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
5061 * btrfs_balance. bctl is freed in reset_balance_state, or, if
5062 * restriper was paused all the way until unmount, in free_fs_info.
5063 * The flag should be cleared after reset_balance_state.
5065 need_unlock
= false;
5067 ret
= btrfs_balance(fs_info
, bctl
, bargs
);
5071 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
5080 mutex_unlock(&fs_info
->balance_mutex
);
5082 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
5084 mnt_drop_write_file(file
);
5088 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info
*fs_info
, int cmd
)
5090 if (!capable(CAP_SYS_ADMIN
))
5094 case BTRFS_BALANCE_CTL_PAUSE
:
5095 return btrfs_pause_balance(fs_info
);
5096 case BTRFS_BALANCE_CTL_CANCEL
:
5097 return btrfs_cancel_balance(fs_info
);
5103 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info
*fs_info
,
5106 struct btrfs_ioctl_balance_args
*bargs
;
5109 if (!capable(CAP_SYS_ADMIN
))
5112 mutex_lock(&fs_info
->balance_mutex
);
5113 if (!fs_info
->balance_ctl
) {
5118 bargs
= kzalloc(sizeof(*bargs
), GFP_KERNEL
);
5124 btrfs_update_ioctl_balance_args(fs_info
, bargs
);
5126 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
5131 mutex_unlock(&fs_info
->balance_mutex
);
5135 static long btrfs_ioctl_quota_ctl(struct file
*file
, void __user
*arg
)
5137 struct inode
*inode
= file_inode(file
);
5138 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5139 struct btrfs_ioctl_quota_ctl_args
*sa
;
5140 struct btrfs_trans_handle
*trans
= NULL
;
5144 if (!capable(CAP_SYS_ADMIN
))
5147 ret
= mnt_want_write_file(file
);
5151 sa
= memdup_user(arg
, sizeof(*sa
));
5157 down_write(&fs_info
->subvol_sem
);
5158 trans
= btrfs_start_transaction(fs_info
->tree_root
, 2);
5159 if (IS_ERR(trans
)) {
5160 ret
= PTR_ERR(trans
);
5165 case BTRFS_QUOTA_CTL_ENABLE
:
5166 ret
= btrfs_quota_enable(trans
, fs_info
);
5168 case BTRFS_QUOTA_CTL_DISABLE
:
5169 ret
= btrfs_quota_disable(trans
, fs_info
);
5176 err
= btrfs_commit_transaction(trans
);
5181 up_write(&fs_info
->subvol_sem
);
5183 mnt_drop_write_file(file
);
5187 static long btrfs_ioctl_qgroup_assign(struct file
*file
, void __user
*arg
)
5189 struct inode
*inode
= file_inode(file
);
5190 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5191 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5192 struct btrfs_ioctl_qgroup_assign_args
*sa
;
5193 struct btrfs_trans_handle
*trans
;
5197 if (!capable(CAP_SYS_ADMIN
))
5200 ret
= mnt_want_write_file(file
);
5204 sa
= memdup_user(arg
, sizeof(*sa
));
5210 trans
= btrfs_join_transaction(root
);
5211 if (IS_ERR(trans
)) {
5212 ret
= PTR_ERR(trans
);
5217 ret
= btrfs_add_qgroup_relation(trans
, fs_info
,
5220 ret
= btrfs_del_qgroup_relation(trans
, fs_info
,
5224 /* update qgroup status and info */
5225 err
= btrfs_run_qgroups(trans
, fs_info
);
5227 btrfs_handle_fs_error(fs_info
, err
,
5228 "failed to update qgroup status and info");
5229 err
= btrfs_end_transaction(trans
);
5236 mnt_drop_write_file(file
);
5240 static long btrfs_ioctl_qgroup_create(struct file
*file
, void __user
*arg
)
5242 struct inode
*inode
= file_inode(file
);
5243 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5244 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5245 struct btrfs_ioctl_qgroup_create_args
*sa
;
5246 struct btrfs_trans_handle
*trans
;
5250 if (!capable(CAP_SYS_ADMIN
))
5253 ret
= mnt_want_write_file(file
);
5257 sa
= memdup_user(arg
, sizeof(*sa
));
5263 if (!sa
->qgroupid
) {
5268 trans
= btrfs_join_transaction(root
);
5269 if (IS_ERR(trans
)) {
5270 ret
= PTR_ERR(trans
);
5275 ret
= btrfs_create_qgroup(trans
, fs_info
, sa
->qgroupid
);
5277 ret
= btrfs_remove_qgroup(trans
, fs_info
, sa
->qgroupid
);
5280 err
= btrfs_end_transaction(trans
);
5287 mnt_drop_write_file(file
);
5291 static long btrfs_ioctl_qgroup_limit(struct file
*file
, void __user
*arg
)
5293 struct inode
*inode
= file_inode(file
);
5294 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5295 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5296 struct btrfs_ioctl_qgroup_limit_args
*sa
;
5297 struct btrfs_trans_handle
*trans
;
5302 if (!capable(CAP_SYS_ADMIN
))
5305 ret
= mnt_want_write_file(file
);
5309 sa
= memdup_user(arg
, sizeof(*sa
));
5315 trans
= btrfs_join_transaction(root
);
5316 if (IS_ERR(trans
)) {
5317 ret
= PTR_ERR(trans
);
5321 qgroupid
= sa
->qgroupid
;
5323 /* take the current subvol as qgroup */
5324 qgroupid
= root
->root_key
.objectid
;
5327 ret
= btrfs_limit_qgroup(trans
, fs_info
, qgroupid
, &sa
->lim
);
5329 err
= btrfs_end_transaction(trans
);
5336 mnt_drop_write_file(file
);
5340 static long btrfs_ioctl_quota_rescan(struct file
*file
, void __user
*arg
)
5342 struct inode
*inode
= file_inode(file
);
5343 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5344 struct btrfs_ioctl_quota_rescan_args
*qsa
;
5347 if (!capable(CAP_SYS_ADMIN
))
5350 ret
= mnt_want_write_file(file
);
5354 qsa
= memdup_user(arg
, sizeof(*qsa
));
5365 ret
= btrfs_qgroup_rescan(fs_info
);
5370 mnt_drop_write_file(file
);
5374 static long btrfs_ioctl_quota_rescan_status(struct file
*file
, void __user
*arg
)
5376 struct inode
*inode
= file_inode(file
);
5377 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5378 struct btrfs_ioctl_quota_rescan_args
*qsa
;
5381 if (!capable(CAP_SYS_ADMIN
))
5384 qsa
= kzalloc(sizeof(*qsa
), GFP_KERNEL
);
5388 if (fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
5390 qsa
->progress
= fs_info
->qgroup_rescan_progress
.objectid
;
5393 if (copy_to_user(arg
, qsa
, sizeof(*qsa
)))
5400 static long btrfs_ioctl_quota_rescan_wait(struct file
*file
, void __user
*arg
)
5402 struct inode
*inode
= file_inode(file
);
5403 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5405 if (!capable(CAP_SYS_ADMIN
))
5408 return btrfs_qgroup_wait_for_completion(fs_info
, true);
5411 static long _btrfs_ioctl_set_received_subvol(struct file
*file
,
5412 struct btrfs_ioctl_received_subvol_args
*sa
)
5414 struct inode
*inode
= file_inode(file
);
5415 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5416 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5417 struct btrfs_root_item
*root_item
= &root
->root_item
;
5418 struct btrfs_trans_handle
*trans
;
5419 struct timespec64 ct
= current_time(inode
);
5421 int received_uuid_changed
;
5423 if (!inode_owner_or_capable(inode
))
5426 ret
= mnt_want_write_file(file
);
5430 down_write(&fs_info
->subvol_sem
);
5432 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FIRST_FREE_OBJECTID
) {
5437 if (btrfs_root_readonly(root
)) {
5444 * 2 - uuid items (received uuid + subvol uuid)
5446 trans
= btrfs_start_transaction(root
, 3);
5447 if (IS_ERR(trans
)) {
5448 ret
= PTR_ERR(trans
);
5453 sa
->rtransid
= trans
->transid
;
5454 sa
->rtime
.sec
= ct
.tv_sec
;
5455 sa
->rtime
.nsec
= ct
.tv_nsec
;
5457 received_uuid_changed
= memcmp(root_item
->received_uuid
, sa
->uuid
,
5459 if (received_uuid_changed
&&
5460 !btrfs_is_empty_uuid(root_item
->received_uuid
)) {
5461 ret
= btrfs_uuid_tree_remove(trans
, root_item
->received_uuid
,
5462 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5463 root
->root_key
.objectid
);
5464 if (ret
&& ret
!= -ENOENT
) {
5465 btrfs_abort_transaction(trans
, ret
);
5466 btrfs_end_transaction(trans
);
5470 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
5471 btrfs_set_root_stransid(root_item
, sa
->stransid
);
5472 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
5473 btrfs_set_stack_timespec_sec(&root_item
->stime
, sa
->stime
.sec
);
5474 btrfs_set_stack_timespec_nsec(&root_item
->stime
, sa
->stime
.nsec
);
5475 btrfs_set_stack_timespec_sec(&root_item
->rtime
, sa
->rtime
.sec
);
5476 btrfs_set_stack_timespec_nsec(&root_item
->rtime
, sa
->rtime
.nsec
);
5478 ret
= btrfs_update_root(trans
, fs_info
->tree_root
,
5479 &root
->root_key
, &root
->root_item
);
5481 btrfs_end_transaction(trans
);
5484 if (received_uuid_changed
&& !btrfs_is_empty_uuid(sa
->uuid
)) {
5485 ret
= btrfs_uuid_tree_add(trans
, sa
->uuid
,
5486 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5487 root
->root_key
.objectid
);
5488 if (ret
< 0 && ret
!= -EEXIST
) {
5489 btrfs_abort_transaction(trans
, ret
);
5490 btrfs_end_transaction(trans
);
5494 ret
= btrfs_commit_transaction(trans
);
5496 up_write(&fs_info
->subvol_sem
);
5497 mnt_drop_write_file(file
);
5502 static long btrfs_ioctl_set_received_subvol_32(struct file
*file
,
5505 struct btrfs_ioctl_received_subvol_args_32
*args32
= NULL
;
5506 struct btrfs_ioctl_received_subvol_args
*args64
= NULL
;
5509 args32
= memdup_user(arg
, sizeof(*args32
));
5511 return PTR_ERR(args32
);
5513 args64
= kmalloc(sizeof(*args64
), GFP_KERNEL
);
5519 memcpy(args64
->uuid
, args32
->uuid
, BTRFS_UUID_SIZE
);
5520 args64
->stransid
= args32
->stransid
;
5521 args64
->rtransid
= args32
->rtransid
;
5522 args64
->stime
.sec
= args32
->stime
.sec
;
5523 args64
->stime
.nsec
= args32
->stime
.nsec
;
5524 args64
->rtime
.sec
= args32
->rtime
.sec
;
5525 args64
->rtime
.nsec
= args32
->rtime
.nsec
;
5526 args64
->flags
= args32
->flags
;
5528 ret
= _btrfs_ioctl_set_received_subvol(file
, args64
);
5532 memcpy(args32
->uuid
, args64
->uuid
, BTRFS_UUID_SIZE
);
5533 args32
->stransid
= args64
->stransid
;
5534 args32
->rtransid
= args64
->rtransid
;
5535 args32
->stime
.sec
= args64
->stime
.sec
;
5536 args32
->stime
.nsec
= args64
->stime
.nsec
;
5537 args32
->rtime
.sec
= args64
->rtime
.sec
;
5538 args32
->rtime
.nsec
= args64
->rtime
.nsec
;
5539 args32
->flags
= args64
->flags
;
5541 ret
= copy_to_user(arg
, args32
, sizeof(*args32
));
5552 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
5555 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
5558 sa
= memdup_user(arg
, sizeof(*sa
));
5562 ret
= _btrfs_ioctl_set_received_subvol(file
, sa
);
5567 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
5576 static int btrfs_ioctl_get_fslabel(struct file
*file
, void __user
*arg
)
5578 struct inode
*inode
= file_inode(file
);
5579 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5582 char label
[BTRFS_LABEL_SIZE
];
5584 spin_lock(&fs_info
->super_lock
);
5585 memcpy(label
, fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
5586 spin_unlock(&fs_info
->super_lock
);
5588 len
= strnlen(label
, BTRFS_LABEL_SIZE
);
5590 if (len
== BTRFS_LABEL_SIZE
) {
5592 "label is too long, return the first %zu bytes",
5596 ret
= copy_to_user(arg
, label
, len
);
5598 return ret
? -EFAULT
: 0;
5601 static int btrfs_ioctl_set_fslabel(struct file
*file
, void __user
*arg
)
5603 struct inode
*inode
= file_inode(file
);
5604 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5605 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5606 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5607 struct btrfs_trans_handle
*trans
;
5608 char label
[BTRFS_LABEL_SIZE
];
5611 if (!capable(CAP_SYS_ADMIN
))
5614 if (copy_from_user(label
, arg
, sizeof(label
)))
5617 if (strnlen(label
, BTRFS_LABEL_SIZE
) == BTRFS_LABEL_SIZE
) {
5619 "unable to set label with more than %d bytes",
5620 BTRFS_LABEL_SIZE
- 1);
5624 ret
= mnt_want_write_file(file
);
5628 trans
= btrfs_start_transaction(root
, 0);
5629 if (IS_ERR(trans
)) {
5630 ret
= PTR_ERR(trans
);
5634 spin_lock(&fs_info
->super_lock
);
5635 strcpy(super_block
->label
, label
);
5636 spin_unlock(&fs_info
->super_lock
);
5637 ret
= btrfs_commit_transaction(trans
);
5640 mnt_drop_write_file(file
);
5644 #define INIT_FEATURE_FLAGS(suffix) \
5645 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5646 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5647 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5649 int btrfs_ioctl_get_supported_features(void __user
*arg
)
5651 static const struct btrfs_ioctl_feature_flags features
[3] = {
5652 INIT_FEATURE_FLAGS(SUPP
),
5653 INIT_FEATURE_FLAGS(SAFE_SET
),
5654 INIT_FEATURE_FLAGS(SAFE_CLEAR
)
5657 if (copy_to_user(arg
, &features
, sizeof(features
)))
5663 static int btrfs_ioctl_get_features(struct file
*file
, void __user
*arg
)
5665 struct inode
*inode
= file_inode(file
);
5666 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5667 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5668 struct btrfs_ioctl_feature_flags features
;
5670 features
.compat_flags
= btrfs_super_compat_flags(super_block
);
5671 features
.compat_ro_flags
= btrfs_super_compat_ro_flags(super_block
);
5672 features
.incompat_flags
= btrfs_super_incompat_flags(super_block
);
5674 if (copy_to_user(arg
, &features
, sizeof(features
)))
5680 static int check_feature_bits(struct btrfs_fs_info
*fs_info
,
5681 enum btrfs_feature_set set
,
5682 u64 change_mask
, u64 flags
, u64 supported_flags
,
5683 u64 safe_set
, u64 safe_clear
)
5685 const char *type
= btrfs_feature_set_names
[set
];
5687 u64 disallowed
, unsupported
;
5688 u64 set_mask
= flags
& change_mask
;
5689 u64 clear_mask
= ~flags
& change_mask
;
5691 unsupported
= set_mask
& ~supported_flags
;
5693 names
= btrfs_printable_features(set
, unsupported
);
5696 "this kernel does not support the %s feature bit%s",
5697 names
, strchr(names
, ',') ? "s" : "");
5701 "this kernel does not support %s bits 0x%llx",
5706 disallowed
= set_mask
& ~safe_set
;
5708 names
= btrfs_printable_features(set
, disallowed
);
5711 "can't set the %s feature bit%s while mounted",
5712 names
, strchr(names
, ',') ? "s" : "");
5716 "can't set %s bits 0x%llx while mounted",
5721 disallowed
= clear_mask
& ~safe_clear
;
5723 names
= btrfs_printable_features(set
, disallowed
);
5726 "can't clear the %s feature bit%s while mounted",
5727 names
, strchr(names
, ',') ? "s" : "");
5731 "can't clear %s bits 0x%llx while mounted",
5739 #define check_feature(fs_info, change_mask, flags, mask_base) \
5740 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
5741 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5742 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5743 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5745 static int btrfs_ioctl_set_features(struct file
*file
, void __user
*arg
)
5747 struct inode
*inode
= file_inode(file
);
5748 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5749 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5750 struct btrfs_super_block
*super_block
= fs_info
->super_copy
;
5751 struct btrfs_ioctl_feature_flags flags
[2];
5752 struct btrfs_trans_handle
*trans
;
5756 if (!capable(CAP_SYS_ADMIN
))
5759 if (copy_from_user(flags
, arg
, sizeof(flags
)))
5763 if (!flags
[0].compat_flags
&& !flags
[0].compat_ro_flags
&&
5764 !flags
[0].incompat_flags
)
5767 ret
= check_feature(fs_info
, flags
[0].compat_flags
,
5768 flags
[1].compat_flags
, COMPAT
);
5772 ret
= check_feature(fs_info
, flags
[0].compat_ro_flags
,
5773 flags
[1].compat_ro_flags
, COMPAT_RO
);
5777 ret
= check_feature(fs_info
, flags
[0].incompat_flags
,
5778 flags
[1].incompat_flags
, INCOMPAT
);
5782 ret
= mnt_want_write_file(file
);
5786 trans
= btrfs_start_transaction(root
, 0);
5787 if (IS_ERR(trans
)) {
5788 ret
= PTR_ERR(trans
);
5789 goto out_drop_write
;
5792 spin_lock(&fs_info
->super_lock
);
5793 newflags
= btrfs_super_compat_flags(super_block
);
5794 newflags
|= flags
[0].compat_flags
& flags
[1].compat_flags
;
5795 newflags
&= ~(flags
[0].compat_flags
& ~flags
[1].compat_flags
);
5796 btrfs_set_super_compat_flags(super_block
, newflags
);
5798 newflags
= btrfs_super_compat_ro_flags(super_block
);
5799 newflags
|= flags
[0].compat_ro_flags
& flags
[1].compat_ro_flags
;
5800 newflags
&= ~(flags
[0].compat_ro_flags
& ~flags
[1].compat_ro_flags
);
5801 btrfs_set_super_compat_ro_flags(super_block
, newflags
);
5803 newflags
= btrfs_super_incompat_flags(super_block
);
5804 newflags
|= flags
[0].incompat_flags
& flags
[1].incompat_flags
;
5805 newflags
&= ~(flags
[0].incompat_flags
& ~flags
[1].incompat_flags
);
5806 btrfs_set_super_incompat_flags(super_block
, newflags
);
5807 spin_unlock(&fs_info
->super_lock
);
5809 ret
= btrfs_commit_transaction(trans
);
5811 mnt_drop_write_file(file
);
5816 static int _btrfs_ioctl_send(struct file
*file
, void __user
*argp
, bool compat
)
5818 struct btrfs_ioctl_send_args
*arg
;
5822 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5823 struct btrfs_ioctl_send_args_32 args32
;
5825 ret
= copy_from_user(&args32
, argp
, sizeof(args32
));
5828 arg
= kzalloc(sizeof(*arg
), GFP_KERNEL
);
5831 arg
->send_fd
= args32
.send_fd
;
5832 arg
->clone_sources_count
= args32
.clone_sources_count
;
5833 arg
->clone_sources
= compat_ptr(args32
.clone_sources
);
5834 arg
->parent_root
= args32
.parent_root
;
5835 arg
->flags
= args32
.flags
;
5836 memcpy(arg
->reserved
, args32
.reserved
,
5837 sizeof(args32
.reserved
));
5842 arg
= memdup_user(argp
, sizeof(*arg
));
5844 return PTR_ERR(arg
);
5846 ret
= btrfs_ioctl_send(file
, arg
);
5851 long btrfs_ioctl(struct file
*file
, unsigned int
5852 cmd
, unsigned long arg
)
5854 struct inode
*inode
= file_inode(file
);
5855 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5856 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5857 void __user
*argp
= (void __user
*)arg
;
5860 case FS_IOC_GETFLAGS
:
5861 return btrfs_ioctl_getflags(file
, argp
);
5862 case FS_IOC_SETFLAGS
:
5863 return btrfs_ioctl_setflags(file
, argp
);
5864 case FS_IOC_GETVERSION
:
5865 return btrfs_ioctl_getversion(file
, argp
);
5867 return btrfs_ioctl_fitrim(file
, argp
);
5868 case BTRFS_IOC_SNAP_CREATE
:
5869 return btrfs_ioctl_snap_create(file
, argp
, 0);
5870 case BTRFS_IOC_SNAP_CREATE_V2
:
5871 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
5872 case BTRFS_IOC_SUBVOL_CREATE
:
5873 return btrfs_ioctl_snap_create(file
, argp
, 1);
5874 case BTRFS_IOC_SUBVOL_CREATE_V2
:
5875 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
5876 case BTRFS_IOC_SNAP_DESTROY
:
5877 return btrfs_ioctl_snap_destroy(file
, argp
);
5878 case BTRFS_IOC_SUBVOL_GETFLAGS
:
5879 return btrfs_ioctl_subvol_getflags(file
, argp
);
5880 case BTRFS_IOC_SUBVOL_SETFLAGS
:
5881 return btrfs_ioctl_subvol_setflags(file
, argp
);
5882 case BTRFS_IOC_DEFAULT_SUBVOL
:
5883 return btrfs_ioctl_default_subvol(file
, argp
);
5884 case BTRFS_IOC_DEFRAG
:
5885 return btrfs_ioctl_defrag(file
, NULL
);
5886 case BTRFS_IOC_DEFRAG_RANGE
:
5887 return btrfs_ioctl_defrag(file
, argp
);
5888 case BTRFS_IOC_RESIZE
:
5889 return btrfs_ioctl_resize(file
, argp
);
5890 case BTRFS_IOC_ADD_DEV
:
5891 return btrfs_ioctl_add_dev(fs_info
, argp
);
5892 case BTRFS_IOC_RM_DEV
:
5893 return btrfs_ioctl_rm_dev(file
, argp
);
5894 case BTRFS_IOC_RM_DEV_V2
:
5895 return btrfs_ioctl_rm_dev_v2(file
, argp
);
5896 case BTRFS_IOC_FS_INFO
:
5897 return btrfs_ioctl_fs_info(fs_info
, argp
);
5898 case BTRFS_IOC_DEV_INFO
:
5899 return btrfs_ioctl_dev_info(fs_info
, argp
);
5900 case BTRFS_IOC_BALANCE
:
5901 return btrfs_ioctl_balance(file
, NULL
);
5902 case BTRFS_IOC_TREE_SEARCH
:
5903 return btrfs_ioctl_tree_search(file
, argp
);
5904 case BTRFS_IOC_TREE_SEARCH_V2
:
5905 return btrfs_ioctl_tree_search_v2(file
, argp
);
5906 case BTRFS_IOC_INO_LOOKUP
:
5907 return btrfs_ioctl_ino_lookup(file
, argp
);
5908 case BTRFS_IOC_INO_PATHS
:
5909 return btrfs_ioctl_ino_to_path(root
, argp
);
5910 case BTRFS_IOC_LOGICAL_INO
:
5911 return btrfs_ioctl_logical_to_ino(fs_info
, argp
, 1);
5912 case BTRFS_IOC_LOGICAL_INO_V2
:
5913 return btrfs_ioctl_logical_to_ino(fs_info
, argp
, 2);
5914 case BTRFS_IOC_SPACE_INFO
:
5915 return btrfs_ioctl_space_info(fs_info
, argp
);
5916 case BTRFS_IOC_SYNC
: {
5919 ret
= btrfs_start_delalloc_roots(fs_info
, -1);
5922 ret
= btrfs_sync_fs(inode
->i_sb
, 1);
5924 * The transaction thread may want to do more work,
5925 * namely it pokes the cleaner kthread that will start
5926 * processing uncleaned subvols.
5928 wake_up_process(fs_info
->transaction_kthread
);
5931 case BTRFS_IOC_START_SYNC
:
5932 return btrfs_ioctl_start_sync(root
, argp
);
5933 case BTRFS_IOC_WAIT_SYNC
:
5934 return btrfs_ioctl_wait_sync(fs_info
, argp
);
5935 case BTRFS_IOC_SCRUB
:
5936 return btrfs_ioctl_scrub(file
, argp
);
5937 case BTRFS_IOC_SCRUB_CANCEL
:
5938 return btrfs_ioctl_scrub_cancel(fs_info
);
5939 case BTRFS_IOC_SCRUB_PROGRESS
:
5940 return btrfs_ioctl_scrub_progress(fs_info
, argp
);
5941 case BTRFS_IOC_BALANCE_V2
:
5942 return btrfs_ioctl_balance(file
, argp
);
5943 case BTRFS_IOC_BALANCE_CTL
:
5944 return btrfs_ioctl_balance_ctl(fs_info
, arg
);
5945 case BTRFS_IOC_BALANCE_PROGRESS
:
5946 return btrfs_ioctl_balance_progress(fs_info
, argp
);
5947 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
5948 return btrfs_ioctl_set_received_subvol(file
, argp
);
5950 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32
:
5951 return btrfs_ioctl_set_received_subvol_32(file
, argp
);
5953 case BTRFS_IOC_SEND
:
5954 return _btrfs_ioctl_send(file
, argp
, false);
5955 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5956 case BTRFS_IOC_SEND_32
:
5957 return _btrfs_ioctl_send(file
, argp
, true);
5959 case BTRFS_IOC_GET_DEV_STATS
:
5960 return btrfs_ioctl_get_dev_stats(fs_info
, argp
);
5961 case BTRFS_IOC_QUOTA_CTL
:
5962 return btrfs_ioctl_quota_ctl(file
, argp
);
5963 case BTRFS_IOC_QGROUP_ASSIGN
:
5964 return btrfs_ioctl_qgroup_assign(file
, argp
);
5965 case BTRFS_IOC_QGROUP_CREATE
:
5966 return btrfs_ioctl_qgroup_create(file
, argp
);
5967 case BTRFS_IOC_QGROUP_LIMIT
:
5968 return btrfs_ioctl_qgroup_limit(file
, argp
);
5969 case BTRFS_IOC_QUOTA_RESCAN
:
5970 return btrfs_ioctl_quota_rescan(file
, argp
);
5971 case BTRFS_IOC_QUOTA_RESCAN_STATUS
:
5972 return btrfs_ioctl_quota_rescan_status(file
, argp
);
5973 case BTRFS_IOC_QUOTA_RESCAN_WAIT
:
5974 return btrfs_ioctl_quota_rescan_wait(file
, argp
);
5975 case BTRFS_IOC_DEV_REPLACE
:
5976 return btrfs_ioctl_dev_replace(fs_info
, argp
);
5977 case BTRFS_IOC_GET_FSLABEL
:
5978 return btrfs_ioctl_get_fslabel(file
, argp
);
5979 case BTRFS_IOC_SET_FSLABEL
:
5980 return btrfs_ioctl_set_fslabel(file
, argp
);
5981 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
5982 return btrfs_ioctl_get_supported_features(argp
);
5983 case BTRFS_IOC_GET_FEATURES
:
5984 return btrfs_ioctl_get_features(file
, argp
);
5985 case BTRFS_IOC_SET_FEATURES
:
5986 return btrfs_ioctl_set_features(file
, argp
);
5987 case FS_IOC_FSGETXATTR
:
5988 return btrfs_ioctl_fsgetxattr(file
, argp
);
5989 case FS_IOC_FSSETXATTR
:
5990 return btrfs_ioctl_fssetxattr(file
, argp
);
5991 case BTRFS_IOC_GET_SUBVOL_INFO
:
5992 return btrfs_ioctl_get_subvol_info(file
, argp
);
5993 case BTRFS_IOC_GET_SUBVOL_ROOTREF
:
5994 return btrfs_ioctl_get_subvol_rootref(file
, argp
);
5995 case BTRFS_IOC_INO_LOOKUP_USER
:
5996 return btrfs_ioctl_ino_lookup_user(file
, argp
);
6002 #ifdef CONFIG_COMPAT
6003 long btrfs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
6006 * These all access 32-bit values anyway so no further
6007 * handling is necessary.
6010 case FS_IOC32_GETFLAGS
:
6011 cmd
= FS_IOC_GETFLAGS
;
6013 case FS_IOC32_SETFLAGS
:
6014 cmd
= FS_IOC_SETFLAGS
;
6016 case FS_IOC32_GETVERSION
:
6017 cmd
= FS_IOC_GETVERSION
;
6021 return btrfs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));