2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/fsnotify.h>
25 #include <linux/pagemap.h>
26 #include <linux/highmem.h>
27 #include <linux/time.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mount.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/swap.h>
35 #include <linux/writeback.h>
36 #include <linux/compat.h>
37 #include <linux/bit_spinlock.h>
38 #include <linux/security.h>
39 #include <linux/xattr.h>
40 #include <linux/vmalloc.h>
41 #include <linux/slab.h>
42 #include <linux/blkdev.h>
43 #include <linux/uuid.h>
44 #include <linux/btrfs.h>
45 #include <linux/uaccess.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
50 #include "print-tree.h"
53 #include "inode-map.h"
55 #include "rcu-string.h"
57 #include "dev-replace.h"
62 #include "compression.h"
65 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
66 * structures are incorrect, as the timespec structure from userspace
67 * is 4 bytes too small. We define these alternatives here to teach
68 * the kernel about the 32-bit struct packing.
70 struct btrfs_ioctl_timespec_32
{
73 } __attribute__ ((__packed__
));
75 struct btrfs_ioctl_received_subvol_args_32
{
76 char uuid
[BTRFS_UUID_SIZE
]; /* in */
77 __u64 stransid
; /* in */
78 __u64 rtransid
; /* out */
79 struct btrfs_ioctl_timespec_32 stime
; /* in */
80 struct btrfs_ioctl_timespec_32 rtime
; /* out */
82 __u64 reserved
[16]; /* in */
83 } __attribute__ ((__packed__
));
85 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
86 struct btrfs_ioctl_received_subvol_args_32)
90 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
91 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
,
94 /* Mask out flags that are inappropriate for the given type of inode. */
95 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
99 else if (S_ISREG(mode
))
100 return flags
& ~FS_DIRSYNC_FL
;
102 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
106 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
108 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
110 unsigned int iflags
= 0;
112 if (flags
& BTRFS_INODE_SYNC
)
113 iflags
|= FS_SYNC_FL
;
114 if (flags
& BTRFS_INODE_IMMUTABLE
)
115 iflags
|= FS_IMMUTABLE_FL
;
116 if (flags
& BTRFS_INODE_APPEND
)
117 iflags
|= FS_APPEND_FL
;
118 if (flags
& BTRFS_INODE_NODUMP
)
119 iflags
|= FS_NODUMP_FL
;
120 if (flags
& BTRFS_INODE_NOATIME
)
121 iflags
|= FS_NOATIME_FL
;
122 if (flags
& BTRFS_INODE_DIRSYNC
)
123 iflags
|= FS_DIRSYNC_FL
;
124 if (flags
& BTRFS_INODE_NODATACOW
)
125 iflags
|= FS_NOCOW_FL
;
127 if (flags
& BTRFS_INODE_NOCOMPRESS
)
128 iflags
|= FS_NOCOMP_FL
;
129 else if (flags
& BTRFS_INODE_COMPRESS
)
130 iflags
|= FS_COMPR_FL
;
136 * Update inode->i_flags based on the btrfs internal flags.
138 void btrfs_update_iflags(struct inode
*inode
)
140 struct btrfs_inode
*ip
= BTRFS_I(inode
);
141 unsigned int new_fl
= 0;
143 if (ip
->flags
& BTRFS_INODE_SYNC
)
145 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
146 new_fl
|= S_IMMUTABLE
;
147 if (ip
->flags
& BTRFS_INODE_APPEND
)
149 if (ip
->flags
& BTRFS_INODE_NOATIME
)
151 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
154 set_mask_bits(&inode
->i_flags
,
155 S_SYNC
| S_APPEND
| S_IMMUTABLE
| S_NOATIME
| S_DIRSYNC
,
160 * Inherit flags from the parent inode.
162 * Currently only the compression flags and the cow flags are inherited.
164 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
171 flags
= BTRFS_I(dir
)->flags
;
173 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
174 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
175 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
176 } else if (flags
& BTRFS_INODE_COMPRESS
) {
177 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
178 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
181 if (flags
& BTRFS_INODE_NODATACOW
) {
182 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
183 if (S_ISREG(inode
->i_mode
))
184 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
187 btrfs_update_iflags(inode
);
190 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
192 struct btrfs_inode
*ip
= BTRFS_I(file_inode(file
));
193 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
195 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
200 static int check_flags(unsigned int flags
)
202 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
203 FS_NOATIME_FL
| FS_NODUMP_FL
| \
204 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
205 FS_NOCOMP_FL
| FS_COMPR_FL
|
209 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
215 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
217 struct inode
*inode
= file_inode(file
);
218 struct btrfs_inode
*ip
= BTRFS_I(inode
);
219 struct btrfs_root
*root
= ip
->root
;
220 struct btrfs_trans_handle
*trans
;
221 unsigned int flags
, oldflags
;
224 unsigned int i_oldflags
;
227 if (!inode_owner_or_capable(inode
))
230 if (btrfs_root_readonly(root
))
233 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
236 ret
= check_flags(flags
);
240 ret
= mnt_want_write_file(file
);
246 ip_oldflags
= ip
->flags
;
247 i_oldflags
= inode
->i_flags
;
248 mode
= inode
->i_mode
;
250 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
251 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
252 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
253 if (!capable(CAP_LINUX_IMMUTABLE
)) {
259 if (flags
& FS_SYNC_FL
)
260 ip
->flags
|= BTRFS_INODE_SYNC
;
262 ip
->flags
&= ~BTRFS_INODE_SYNC
;
263 if (flags
& FS_IMMUTABLE_FL
)
264 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
266 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
267 if (flags
& FS_APPEND_FL
)
268 ip
->flags
|= BTRFS_INODE_APPEND
;
270 ip
->flags
&= ~BTRFS_INODE_APPEND
;
271 if (flags
& FS_NODUMP_FL
)
272 ip
->flags
|= BTRFS_INODE_NODUMP
;
274 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
275 if (flags
& FS_NOATIME_FL
)
276 ip
->flags
|= BTRFS_INODE_NOATIME
;
278 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
279 if (flags
& FS_DIRSYNC_FL
)
280 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
282 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
283 if (flags
& FS_NOCOW_FL
) {
286 * It's safe to turn csums off here, no extents exist.
287 * Otherwise we want the flag to reflect the real COW
288 * status of the file and will not set it.
290 if (inode
->i_size
== 0)
291 ip
->flags
|= BTRFS_INODE_NODATACOW
292 | BTRFS_INODE_NODATASUM
;
294 ip
->flags
|= BTRFS_INODE_NODATACOW
;
298 * Revert back under same assumptions as above
301 if (inode
->i_size
== 0)
302 ip
->flags
&= ~(BTRFS_INODE_NODATACOW
303 | BTRFS_INODE_NODATASUM
);
305 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
310 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
311 * flag may be changed automatically if compression code won't make
314 if (flags
& FS_NOCOMP_FL
) {
315 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
316 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
318 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
319 if (ret
&& ret
!= -ENODATA
)
321 } else if (flags
& FS_COMPR_FL
) {
324 ip
->flags
|= BTRFS_INODE_COMPRESS
;
325 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
327 if (root
->fs_info
->compress_type
== BTRFS_COMPRESS_LZO
)
331 ret
= btrfs_set_prop(inode
, "btrfs.compression",
332 comp
, strlen(comp
), 0);
337 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
338 if (ret
&& ret
!= -ENODATA
)
340 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
343 trans
= btrfs_start_transaction(root
, 1);
345 ret
= PTR_ERR(trans
);
349 btrfs_update_iflags(inode
);
350 inode_inc_iversion(inode
);
351 inode
->i_ctime
= current_time(inode
);
352 ret
= btrfs_update_inode(trans
, root
, inode
);
354 btrfs_end_transaction(trans
, root
);
357 ip
->flags
= ip_oldflags
;
358 inode
->i_flags
= i_oldflags
;
363 mnt_drop_write_file(file
);
367 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
369 struct inode
*inode
= file_inode(file
);
371 return put_user(inode
->i_generation
, arg
);
374 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
376 struct btrfs_fs_info
*fs_info
= btrfs_sb(file_inode(file
)->i_sb
);
377 struct btrfs_device
*device
;
378 struct request_queue
*q
;
379 struct fstrim_range range
;
380 u64 minlen
= ULLONG_MAX
;
382 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
385 if (!capable(CAP_SYS_ADMIN
))
389 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
393 q
= bdev_get_queue(device
->bdev
);
394 if (blk_queue_discard(q
)) {
396 minlen
= min((u64
)q
->limits
.discard_granularity
,
404 if (copy_from_user(&range
, arg
, sizeof(range
)))
406 if (range
.start
> total_bytes
||
407 range
.len
< fs_info
->sb
->s_blocksize
)
410 range
.len
= min(range
.len
, total_bytes
- range
.start
);
411 range
.minlen
= max(range
.minlen
, minlen
);
412 ret
= btrfs_trim_fs(fs_info
->tree_root
, &range
);
416 if (copy_to_user(arg
, &range
, sizeof(range
)))
422 int btrfs_is_empty_uuid(u8
*uuid
)
426 for (i
= 0; i
< BTRFS_UUID_SIZE
; i
++) {
433 static noinline
int create_subvol(struct inode
*dir
,
434 struct dentry
*dentry
,
435 char *name
, int namelen
,
437 struct btrfs_qgroup_inherit
*inherit
)
439 struct btrfs_trans_handle
*trans
;
440 struct btrfs_key key
;
441 struct btrfs_root_item
*root_item
;
442 struct btrfs_inode_item
*inode_item
;
443 struct extent_buffer
*leaf
;
444 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
445 struct btrfs_root
*new_root
;
446 struct btrfs_block_rsv block_rsv
;
447 struct timespec cur_time
= current_time(dir
);
452 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
457 root_item
= kzalloc(sizeof(*root_item
), GFP_KERNEL
);
461 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
466 * Don't create subvolume whose level is not zero. Or qgroup will be
467 * screwed up since it assumes subvolume qgroup's level to be 0.
469 if (btrfs_qgroup_level(objectid
)) {
474 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
476 * The same as the snapshot creation, please see the comment
477 * of create_snapshot().
479 ret
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
480 8, &qgroup_reserved
, false);
484 trans
= btrfs_start_transaction(root
, 0);
486 ret
= PTR_ERR(trans
);
487 btrfs_subvolume_release_metadata(root
, &block_rsv
,
491 trans
->block_rsv
= &block_rsv
;
492 trans
->bytes_reserved
= block_rsv
.size
;
494 ret
= btrfs_qgroup_inherit(trans
, root
->fs_info
, 0, objectid
, inherit
);
498 leaf
= btrfs_alloc_tree_block(trans
, root
, 0, objectid
, NULL
, 0, 0, 0);
504 memzero_extent_buffer(leaf
, 0, sizeof(struct btrfs_header
));
505 btrfs_set_header_bytenr(leaf
, leaf
->start
);
506 btrfs_set_header_generation(leaf
, trans
->transid
);
507 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
508 btrfs_set_header_owner(leaf
, objectid
);
510 write_extent_buffer_fsid(leaf
, root
->fs_info
->fsid
);
511 write_extent_buffer_chunk_tree_uuid(leaf
,
512 root
->fs_info
->chunk_tree_uuid
);
513 btrfs_mark_buffer_dirty(leaf
);
515 inode_item
= &root_item
->inode
;
516 btrfs_set_stack_inode_generation(inode_item
, 1);
517 btrfs_set_stack_inode_size(inode_item
, 3);
518 btrfs_set_stack_inode_nlink(inode_item
, 1);
519 btrfs_set_stack_inode_nbytes(inode_item
,
520 root
->fs_info
->nodesize
);
521 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
523 btrfs_set_root_flags(root_item
, 0);
524 btrfs_set_root_limit(root_item
, 0);
525 btrfs_set_stack_inode_flags(inode_item
, BTRFS_INODE_ROOT_ITEM_INIT
);
527 btrfs_set_root_bytenr(root_item
, leaf
->start
);
528 btrfs_set_root_generation(root_item
, trans
->transid
);
529 btrfs_set_root_level(root_item
, 0);
530 btrfs_set_root_refs(root_item
, 1);
531 btrfs_set_root_used(root_item
, leaf
->len
);
532 btrfs_set_root_last_snapshot(root_item
, 0);
534 btrfs_set_root_generation_v2(root_item
,
535 btrfs_root_generation(root_item
));
536 uuid_le_gen(&new_uuid
);
537 memcpy(root_item
->uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
538 btrfs_set_stack_timespec_sec(&root_item
->otime
, cur_time
.tv_sec
);
539 btrfs_set_stack_timespec_nsec(&root_item
->otime
, cur_time
.tv_nsec
);
540 root_item
->ctime
= root_item
->otime
;
541 btrfs_set_root_ctransid(root_item
, trans
->transid
);
542 btrfs_set_root_otransid(root_item
, trans
->transid
);
544 btrfs_tree_unlock(leaf
);
545 free_extent_buffer(leaf
);
548 btrfs_set_root_dirid(root_item
, new_dirid
);
550 key
.objectid
= objectid
;
552 key
.type
= BTRFS_ROOT_ITEM_KEY
;
553 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
558 key
.offset
= (u64
)-1;
559 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
560 if (IS_ERR(new_root
)) {
561 ret
= PTR_ERR(new_root
);
562 btrfs_abort_transaction(trans
, ret
);
566 btrfs_record_root_in_trans(trans
, new_root
);
568 ret
= btrfs_create_subvol_root(trans
, new_root
, root
, new_dirid
);
570 /* We potentially lose an unused inode item here */
571 btrfs_abort_transaction(trans
, ret
);
575 mutex_lock(&new_root
->objectid_mutex
);
576 new_root
->highest_objectid
= new_dirid
;
577 mutex_unlock(&new_root
->objectid_mutex
);
580 * insert the directory item
582 ret
= btrfs_set_inode_index(dir
, &index
);
584 btrfs_abort_transaction(trans
, ret
);
588 ret
= btrfs_insert_dir_item(trans
, root
,
589 name
, namelen
, dir
, &key
,
590 BTRFS_FT_DIR
, index
);
592 btrfs_abort_transaction(trans
, ret
);
596 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
597 ret
= btrfs_update_inode(trans
, root
, dir
);
600 ret
= btrfs_add_root_ref(trans
, root
->fs_info
,
601 objectid
, root
->root_key
.objectid
,
602 btrfs_ino(dir
), index
, name
, namelen
);
605 ret
= btrfs_uuid_tree_add(trans
, root
->fs_info
, root_item
->uuid
,
606 BTRFS_UUID_KEY_SUBVOL
, objectid
);
608 btrfs_abort_transaction(trans
, ret
);
612 trans
->block_rsv
= NULL
;
613 trans
->bytes_reserved
= 0;
614 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
617 *async_transid
= trans
->transid
;
618 err
= btrfs_commit_transaction_async(trans
, root
, 1);
620 err
= btrfs_commit_transaction(trans
, root
);
622 err
= btrfs_commit_transaction(trans
, root
);
628 inode
= btrfs_lookup_dentry(dir
, dentry
);
630 return PTR_ERR(inode
);
631 d_instantiate(dentry
, inode
);
640 static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root
*root
)
646 prepare_to_wait(&root
->subv_writers
->wait
, &wait
,
647 TASK_UNINTERRUPTIBLE
);
649 writers
= percpu_counter_sum(&root
->subv_writers
->counter
);
653 finish_wait(&root
->subv_writers
->wait
, &wait
);
657 static int create_snapshot(struct btrfs_root
*root
, struct inode
*dir
,
658 struct dentry
*dentry
, char *name
, int namelen
,
659 u64
*async_transid
, bool readonly
,
660 struct btrfs_qgroup_inherit
*inherit
)
663 struct btrfs_pending_snapshot
*pending_snapshot
;
664 struct btrfs_trans_handle
*trans
;
667 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
670 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
671 if (!pending_snapshot
)
674 pending_snapshot
->root_item
= kzalloc(sizeof(struct btrfs_root_item
),
676 pending_snapshot
->path
= btrfs_alloc_path();
677 if (!pending_snapshot
->root_item
|| !pending_snapshot
->path
) {
682 atomic_inc(&root
->will_be_snapshoted
);
683 smp_mb__after_atomic();
684 btrfs_wait_for_no_snapshoting_writes(root
);
686 ret
= btrfs_start_delalloc_inodes(root
, 0);
690 btrfs_wait_ordered_extents(root
, -1, 0, (u64
)-1);
692 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
693 BTRFS_BLOCK_RSV_TEMP
);
695 * 1 - parent dir inode
698 * 2 - root ref/backref
699 * 1 - root of snapshot
702 ret
= btrfs_subvolume_reserve_metadata(BTRFS_I(dir
)->root
,
703 &pending_snapshot
->block_rsv
, 8,
704 &pending_snapshot
->qgroup_reserved
,
709 pending_snapshot
->dentry
= dentry
;
710 pending_snapshot
->root
= root
;
711 pending_snapshot
->readonly
= readonly
;
712 pending_snapshot
->dir
= dir
;
713 pending_snapshot
->inherit
= inherit
;
715 trans
= btrfs_start_transaction(root
, 0);
717 ret
= PTR_ERR(trans
);
721 spin_lock(&root
->fs_info
->trans_lock
);
722 list_add(&pending_snapshot
->list
,
723 &trans
->transaction
->pending_snapshots
);
724 spin_unlock(&root
->fs_info
->trans_lock
);
726 *async_transid
= trans
->transid
;
727 ret
= btrfs_commit_transaction_async(trans
,
728 root
->fs_info
->extent_root
, 1);
730 ret
= btrfs_commit_transaction(trans
, root
);
732 ret
= btrfs_commit_transaction(trans
,
733 root
->fs_info
->extent_root
);
738 ret
= pending_snapshot
->error
;
742 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
746 inode
= btrfs_lookup_dentry(d_inode(dentry
->d_parent
), dentry
);
748 ret
= PTR_ERR(inode
);
752 d_instantiate(dentry
, inode
);
755 btrfs_subvolume_release_metadata(BTRFS_I(dir
)->root
,
756 &pending_snapshot
->block_rsv
,
757 pending_snapshot
->qgroup_reserved
);
759 if (atomic_dec_and_test(&root
->will_be_snapshoted
))
760 wake_up_atomic_t(&root
->will_be_snapshoted
);
762 kfree(pending_snapshot
->root_item
);
763 btrfs_free_path(pending_snapshot
->path
);
764 kfree(pending_snapshot
);
769 /* copy of may_delete in fs/namei.c()
770 * Check whether we can remove a link victim from directory dir, check
771 * whether the type of victim is right.
772 * 1. We can't do it if dir is read-only (done in permission())
773 * 2. We should have write and exec permissions on dir
774 * 3. We can't remove anything from append-only dir
775 * 4. We can't do anything with immutable dir (done in permission())
776 * 5. If the sticky bit on dir is set we should either
777 * a. be owner of dir, or
778 * b. be owner of victim, or
779 * c. have CAP_FOWNER capability
780 * 6. If the victim is append-only or immutable we can't do anything with
781 * links pointing to it.
782 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
783 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
784 * 9. We can't remove a root or mountpoint.
785 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
786 * nfs_async_unlink().
789 static int btrfs_may_delete(struct inode
*dir
, struct dentry
*victim
, int isdir
)
793 if (d_really_is_negative(victim
))
796 BUG_ON(d_inode(victim
->d_parent
) != dir
);
797 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
799 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
804 if (check_sticky(dir
, d_inode(victim
)) || IS_APPEND(d_inode(victim
)) ||
805 IS_IMMUTABLE(d_inode(victim
)) || IS_SWAPFILE(d_inode(victim
)))
808 if (!d_is_dir(victim
))
812 } else if (d_is_dir(victim
))
816 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
821 /* copy of may_create in fs/namei.c() */
822 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
824 if (d_really_is_positive(child
))
828 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
832 * Create a new subvolume below @parent. This is largely modeled after
833 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
834 * inside this filesystem so it's quite a bit simpler.
836 static noinline
int btrfs_mksubvol(struct path
*parent
,
837 char *name
, int namelen
,
838 struct btrfs_root
*snap_src
,
839 u64
*async_transid
, bool readonly
,
840 struct btrfs_qgroup_inherit
*inherit
)
842 struct inode
*dir
= d_inode(parent
->dentry
);
843 struct dentry
*dentry
;
846 error
= down_write_killable_nested(&dir
->i_rwsem
, I_MUTEX_PARENT
);
850 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
851 error
= PTR_ERR(dentry
);
855 error
= btrfs_may_create(dir
, dentry
);
860 * even if this name doesn't exist, we may get hash collisions.
861 * check for them now when we can safely fail
863 error
= btrfs_check_dir_item_collision(BTRFS_I(dir
)->root
,
869 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
871 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
875 error
= create_snapshot(snap_src
, dir
, dentry
, name
, namelen
,
876 async_transid
, readonly
, inherit
);
878 error
= create_subvol(dir
, dentry
, name
, namelen
,
879 async_transid
, inherit
);
882 fsnotify_mkdir(dir
, dentry
);
884 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
893 * When we're defragging a range, we don't want to kick it off again
894 * if it is really just waiting for delalloc to send it down.
895 * If we find a nice big extent or delalloc range for the bytes in the
896 * file you want to defrag, we return 0 to let you know to skip this
899 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, u32 thresh
)
901 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
902 struct extent_map
*em
= NULL
;
903 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
906 read_lock(&em_tree
->lock
);
907 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_SIZE
);
908 read_unlock(&em_tree
->lock
);
911 end
= extent_map_end(em
);
913 if (end
- offset
> thresh
)
916 /* if we already have a nice delalloc here, just stop */
918 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
919 thresh
, EXTENT_DELALLOC
, 1);
926 * helper function to walk through a file and find extents
927 * newer than a specific transid, and smaller than thresh.
929 * This is used by the defragging code to find new and small
932 static int find_new_extents(struct btrfs_root
*root
,
933 struct inode
*inode
, u64 newer_than
,
934 u64
*off
, u32 thresh
)
936 struct btrfs_path
*path
;
937 struct btrfs_key min_key
;
938 struct extent_buffer
*leaf
;
939 struct btrfs_file_extent_item
*extent
;
942 u64 ino
= btrfs_ino(inode
);
944 path
= btrfs_alloc_path();
948 min_key
.objectid
= ino
;
949 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
950 min_key
.offset
= *off
;
953 ret
= btrfs_search_forward(root
, &min_key
, path
, newer_than
);
957 if (min_key
.objectid
!= ino
)
959 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
962 leaf
= path
->nodes
[0];
963 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
964 struct btrfs_file_extent_item
);
966 type
= btrfs_file_extent_type(leaf
, extent
);
967 if (type
== BTRFS_FILE_EXTENT_REG
&&
968 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
969 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
970 *off
= min_key
.offset
;
971 btrfs_free_path(path
);
976 if (path
->slots
[0] < btrfs_header_nritems(leaf
)) {
977 btrfs_item_key_to_cpu(leaf
, &min_key
, path
->slots
[0]);
981 if (min_key
.offset
== (u64
)-1)
985 btrfs_release_path(path
);
988 btrfs_free_path(path
);
992 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
994 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
995 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
996 struct extent_map
*em
;
1000 * hopefully we have this extent in the tree already, try without
1001 * the full extent lock
1003 read_lock(&em_tree
->lock
);
1004 em
= lookup_extent_mapping(em_tree
, start
, len
);
1005 read_unlock(&em_tree
->lock
);
1008 struct extent_state
*cached
= NULL
;
1009 u64 end
= start
+ len
- 1;
1011 /* get the big lock and read metadata off disk */
1012 lock_extent_bits(io_tree
, start
, end
, &cached
);
1013 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
1014 unlock_extent_cached(io_tree
, start
, end
, &cached
, GFP_NOFS
);
1023 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
1025 struct extent_map
*next
;
1028 /* this is the last extent */
1029 if (em
->start
+ em
->len
>= i_size_read(inode
))
1032 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
1033 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
1035 else if ((em
->block_start
+ em
->block_len
== next
->block_start
) &&
1036 (em
->block_len
> SZ_128K
&& next
->block_len
> SZ_128K
))
1039 free_extent_map(next
);
1043 static int should_defrag_range(struct inode
*inode
, u64 start
, u32 thresh
,
1044 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
1047 struct extent_map
*em
;
1049 bool next_mergeable
= true;
1050 bool prev_mergeable
= true;
1053 * make sure that once we start defragging an extent, we keep on
1056 if (start
< *defrag_end
)
1061 em
= defrag_lookup_extent(inode
, start
);
1065 /* this will cover holes, and inline extents */
1066 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
1072 prev_mergeable
= false;
1074 next_mergeable
= defrag_check_next_extent(inode
, em
);
1076 * we hit a real extent, if it is big or the next extent is not a
1077 * real extent, don't bother defragging it
1079 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
1080 (em
->len
>= thresh
|| (!next_mergeable
&& !prev_mergeable
)))
1084 * last_len ends up being a counter of how many bytes we've defragged.
1085 * every time we choose not to defrag an extent, we reset *last_len
1086 * so that the next tiny extent will force a defrag.
1088 * The end result of this is that tiny extents before a single big
1089 * extent will force at least part of that big extent to be defragged.
1092 *defrag_end
= extent_map_end(em
);
1095 *skip
= extent_map_end(em
);
1099 free_extent_map(em
);
1104 * it doesn't do much good to defrag one or two pages
1105 * at a time. This pulls in a nice chunk of pages
1106 * to COW and defrag.
1108 * It also makes sure the delalloc code has enough
1109 * dirty data to avoid making new small extents as part
1112 * It's a good idea to start RA on this range
1113 * before calling this.
1115 static int cluster_pages_for_defrag(struct inode
*inode
,
1116 struct page
**pages
,
1117 unsigned long start_index
,
1118 unsigned long num_pages
)
1120 unsigned long file_end
;
1121 u64 isize
= i_size_read(inode
);
1128 struct btrfs_ordered_extent
*ordered
;
1129 struct extent_state
*cached_state
= NULL
;
1130 struct extent_io_tree
*tree
;
1131 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
1133 file_end
= (isize
- 1) >> PAGE_SHIFT
;
1134 if (!isize
|| start_index
> file_end
)
1137 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
1139 ret
= btrfs_delalloc_reserve_space(inode
,
1140 start_index
<< PAGE_SHIFT
,
1141 page_cnt
<< PAGE_SHIFT
);
1145 tree
= &BTRFS_I(inode
)->io_tree
;
1147 /* step one, lock all the pages */
1148 for (i
= 0; i
< page_cnt
; i
++) {
1151 page
= find_or_create_page(inode
->i_mapping
,
1152 start_index
+ i
, mask
);
1156 page_start
= page_offset(page
);
1157 page_end
= page_start
+ PAGE_SIZE
- 1;
1159 lock_extent_bits(tree
, page_start
, page_end
,
1161 ordered
= btrfs_lookup_ordered_extent(inode
,
1163 unlock_extent_cached(tree
, page_start
, page_end
,
1164 &cached_state
, GFP_NOFS
);
1169 btrfs_start_ordered_extent(inode
, ordered
, 1);
1170 btrfs_put_ordered_extent(ordered
);
1173 * we unlocked the page above, so we need check if
1174 * it was released or not.
1176 if (page
->mapping
!= inode
->i_mapping
) {
1183 if (!PageUptodate(page
)) {
1184 btrfs_readpage(NULL
, page
);
1186 if (!PageUptodate(page
)) {
1194 if (page
->mapping
!= inode
->i_mapping
) {
1206 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1210 * so now we have a nice long stream of locked
1211 * and up to date pages, lets wait on them
1213 for (i
= 0; i
< i_done
; i
++)
1214 wait_on_page_writeback(pages
[i
]);
1216 page_start
= page_offset(pages
[0]);
1217 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_SIZE
;
1219 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1220 page_start
, page_end
- 1, &cached_state
);
1221 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1222 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1223 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 0, 0,
1224 &cached_state
, GFP_NOFS
);
1226 if (i_done
!= page_cnt
) {
1227 spin_lock(&BTRFS_I(inode
)->lock
);
1228 BTRFS_I(inode
)->outstanding_extents
++;
1229 spin_unlock(&BTRFS_I(inode
)->lock
);
1230 btrfs_delalloc_release_space(inode
,
1231 start_index
<< PAGE_SHIFT
,
1232 (page_cnt
- i_done
) << PAGE_SHIFT
);
1236 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1239 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1240 page_start
, page_end
- 1, &cached_state
,
1243 for (i
= 0; i
< i_done
; i
++) {
1244 clear_page_dirty_for_io(pages
[i
]);
1245 ClearPageChecked(pages
[i
]);
1246 set_page_extent_mapped(pages
[i
]);
1247 set_page_dirty(pages
[i
]);
1248 unlock_page(pages
[i
]);
1253 for (i
= 0; i
< i_done
; i
++) {
1254 unlock_page(pages
[i
]);
1257 btrfs_delalloc_release_space(inode
,
1258 start_index
<< PAGE_SHIFT
,
1259 page_cnt
<< PAGE_SHIFT
);
1264 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1265 struct btrfs_ioctl_defrag_range_args
*range
,
1266 u64 newer_than
, unsigned long max_to_defrag
)
1268 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1269 struct file_ra_state
*ra
= NULL
;
1270 unsigned long last_index
;
1271 u64 isize
= i_size_read(inode
);
1275 u64 newer_off
= range
->start
;
1277 unsigned long ra_index
= 0;
1279 int defrag_count
= 0;
1280 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1281 u32 extent_thresh
= range
->extent_thresh
;
1282 unsigned long max_cluster
= SZ_256K
>> PAGE_SHIFT
;
1283 unsigned long cluster
= max_cluster
;
1284 u64 new_align
= ~((u64
)SZ_128K
- 1);
1285 struct page
**pages
= NULL
;
1290 if (range
->start
>= isize
)
1293 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1294 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1296 if (range
->compress_type
)
1297 compress_type
= range
->compress_type
;
1300 if (extent_thresh
== 0)
1301 extent_thresh
= SZ_256K
;
1304 * if we were not given a file, allocate a readahead
1308 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1311 file_ra_state_init(ra
, inode
->i_mapping
);
1316 pages
= kmalloc_array(max_cluster
, sizeof(struct page
*),
1323 /* find the last page to defrag */
1324 if (range
->start
+ range
->len
> range
->start
) {
1325 last_index
= min_t(u64
, isize
- 1,
1326 range
->start
+ range
->len
- 1) >> PAGE_SHIFT
;
1328 last_index
= (isize
- 1) >> PAGE_SHIFT
;
1332 ret
= find_new_extents(root
, inode
, newer_than
,
1333 &newer_off
, SZ_64K
);
1335 range
->start
= newer_off
;
1337 * we always align our defrag to help keep
1338 * the extents in the file evenly spaced
1340 i
= (newer_off
& new_align
) >> PAGE_SHIFT
;
1344 i
= range
->start
>> PAGE_SHIFT
;
1347 max_to_defrag
= last_index
- i
+ 1;
1350 * make writeback starts from i, so the defrag range can be
1351 * written sequentially.
1353 if (i
< inode
->i_mapping
->writeback_index
)
1354 inode
->i_mapping
->writeback_index
= i
;
1356 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1357 (i
< DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
))) {
1359 * make sure we stop running if someone unmounts
1362 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1365 if (btrfs_defrag_cancelled(root
->fs_info
)) {
1366 btrfs_debug(root
->fs_info
, "defrag_file cancelled");
1371 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_SHIFT
,
1372 extent_thresh
, &last_len
, &skip
,
1373 &defrag_end
, range
->flags
&
1374 BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1377 * the should_defrag function tells us how much to skip
1378 * bump our counter by the suggested amount
1380 next
= DIV_ROUND_UP(skip
, PAGE_SIZE
);
1381 i
= max(i
+ 1, next
);
1386 cluster
= (PAGE_ALIGN(defrag_end
) >>
1388 cluster
= min(cluster
, max_cluster
);
1390 cluster
= max_cluster
;
1393 if (i
+ cluster
> ra_index
) {
1394 ra_index
= max(i
, ra_index
);
1395 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1397 ra_index
+= cluster
;
1401 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1402 BTRFS_I(inode
)->force_compress
= compress_type
;
1403 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1405 inode_unlock(inode
);
1409 defrag_count
+= ret
;
1410 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1411 inode_unlock(inode
);
1414 if (newer_off
== (u64
)-1)
1420 newer_off
= max(newer_off
+ 1,
1421 (u64
)i
<< PAGE_SHIFT
);
1423 ret
= find_new_extents(root
, inode
, newer_than
,
1424 &newer_off
, SZ_64K
);
1426 range
->start
= newer_off
;
1427 i
= (newer_off
& new_align
) >> PAGE_SHIFT
;
1434 last_len
+= ret
<< PAGE_SHIFT
;
1442 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
)) {
1443 filemap_flush(inode
->i_mapping
);
1444 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1445 &BTRFS_I(inode
)->runtime_flags
))
1446 filemap_flush(inode
->i_mapping
);
1449 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1450 /* the filemap_flush will queue IO into the worker threads, but
1451 * we have to make sure the IO is actually started and that
1452 * ordered extents get created before we return
1454 atomic_inc(&root
->fs_info
->async_submit_draining
);
1455 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1456 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1457 wait_event(root
->fs_info
->async_submit_wait
,
1458 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1459 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1461 atomic_dec(&root
->fs_info
->async_submit_draining
);
1464 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1465 btrfs_set_fs_incompat(root
->fs_info
, COMPRESS_LZO
);
1471 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1473 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1474 inode_unlock(inode
);
1482 static noinline
int btrfs_ioctl_resize(struct file
*file
,
1488 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
1489 struct btrfs_ioctl_vol_args
*vol_args
;
1490 struct btrfs_trans_handle
*trans
;
1491 struct btrfs_device
*device
= NULL
;
1494 char *devstr
= NULL
;
1498 if (!capable(CAP_SYS_ADMIN
))
1501 ret
= mnt_want_write_file(file
);
1505 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
1507 mnt_drop_write_file(file
);
1508 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
1511 mutex_lock(&root
->fs_info
->volume_mutex
);
1512 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1513 if (IS_ERR(vol_args
)) {
1514 ret
= PTR_ERR(vol_args
);
1518 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1520 sizestr
= vol_args
->name
;
1521 devstr
= strchr(sizestr
, ':');
1523 sizestr
= devstr
+ 1;
1525 devstr
= vol_args
->name
;
1526 ret
= kstrtoull(devstr
, 10, &devid
);
1533 btrfs_info(root
->fs_info
, "resizing devid %llu", devid
);
1536 device
= btrfs_find_device(root
->fs_info
, devid
, NULL
, NULL
);
1538 btrfs_info(root
->fs_info
, "resizer unable to find device %llu",
1544 if (!device
->writeable
) {
1545 btrfs_info(root
->fs_info
,
1546 "resizer unable to apply on readonly device %llu",
1552 if (!strcmp(sizestr
, "max"))
1553 new_size
= device
->bdev
->bd_inode
->i_size
;
1555 if (sizestr
[0] == '-') {
1558 } else if (sizestr
[0] == '+') {
1562 new_size
= memparse(sizestr
, &retptr
);
1563 if (*retptr
!= '\0' || new_size
== 0) {
1569 if (device
->is_tgtdev_for_dev_replace
) {
1574 old_size
= btrfs_device_get_total_bytes(device
);
1577 if (new_size
> old_size
) {
1581 new_size
= old_size
- new_size
;
1582 } else if (mod
> 0) {
1583 if (new_size
> ULLONG_MAX
- old_size
) {
1587 new_size
= old_size
+ new_size
;
1590 if (new_size
< SZ_256M
) {
1594 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1599 new_size
= div_u64(new_size
, root
->fs_info
->sectorsize
);
1600 new_size
*= root
->fs_info
->sectorsize
;
1602 btrfs_info_in_rcu(root
->fs_info
, "new size for %s is %llu",
1603 rcu_str_deref(device
->name
), new_size
);
1605 if (new_size
> old_size
) {
1606 trans
= btrfs_start_transaction(root
, 0);
1607 if (IS_ERR(trans
)) {
1608 ret
= PTR_ERR(trans
);
1611 ret
= btrfs_grow_device(trans
, device
, new_size
);
1612 btrfs_commit_transaction(trans
, root
);
1613 } else if (new_size
< old_size
) {
1614 ret
= btrfs_shrink_device(device
, new_size
);
1615 } /* equal, nothing need to do */
1620 mutex_unlock(&root
->fs_info
->volume_mutex
);
1621 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
1622 mnt_drop_write_file(file
);
1626 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1627 char *name
, unsigned long fd
, int subvol
,
1628 u64
*transid
, bool readonly
,
1629 struct btrfs_qgroup_inherit
*inherit
)
1634 if (!S_ISDIR(file_inode(file
)->i_mode
))
1637 ret
= mnt_want_write_file(file
);
1641 namelen
= strlen(name
);
1642 if (strchr(name
, '/')) {
1644 goto out_drop_write
;
1647 if (name
[0] == '.' &&
1648 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1650 goto out_drop_write
;
1654 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1655 NULL
, transid
, readonly
, inherit
);
1657 struct fd src
= fdget(fd
);
1658 struct inode
*src_inode
;
1661 goto out_drop_write
;
1664 src_inode
= file_inode(src
.file
);
1665 if (src_inode
->i_sb
!= file_inode(file
)->i_sb
) {
1666 btrfs_info(BTRFS_I(file_inode(file
))->root
->fs_info
,
1667 "Snapshot src from another FS");
1669 } else if (!inode_owner_or_capable(src_inode
)) {
1671 * Subvolume creation is not restricted, but snapshots
1672 * are limited to own subvolumes only
1676 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1677 BTRFS_I(src_inode
)->root
,
1678 transid
, readonly
, inherit
);
1683 mnt_drop_write_file(file
);
1688 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1689 void __user
*arg
, int subvol
)
1691 struct btrfs_ioctl_vol_args
*vol_args
;
1694 if (!S_ISDIR(file_inode(file
)->i_mode
))
1697 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1698 if (IS_ERR(vol_args
))
1699 return PTR_ERR(vol_args
);
1700 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1702 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1703 vol_args
->fd
, subvol
,
1710 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1711 void __user
*arg
, int subvol
)
1713 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1717 bool readonly
= false;
1718 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1720 if (!S_ISDIR(file_inode(file
)->i_mode
))
1723 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1724 if (IS_ERR(vol_args
))
1725 return PTR_ERR(vol_args
);
1726 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1728 if (vol_args
->flags
&
1729 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1730 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1735 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1737 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1739 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1740 if (vol_args
->size
> PAGE_SIZE
) {
1744 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1745 if (IS_ERR(inherit
)) {
1746 ret
= PTR_ERR(inherit
);
1751 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1752 vol_args
->fd
, subvol
, ptr
,
1757 if (ptr
&& copy_to_user(arg
+
1758 offsetof(struct btrfs_ioctl_vol_args_v2
,
1770 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1773 struct inode
*inode
= file_inode(file
);
1774 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1778 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1781 down_read(&root
->fs_info
->subvol_sem
);
1782 if (btrfs_root_readonly(root
))
1783 flags
|= BTRFS_SUBVOL_RDONLY
;
1784 up_read(&root
->fs_info
->subvol_sem
);
1786 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1792 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1795 struct inode
*inode
= file_inode(file
);
1796 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1797 struct btrfs_trans_handle
*trans
;
1802 if (!inode_owner_or_capable(inode
))
1805 ret
= mnt_want_write_file(file
);
1809 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1811 goto out_drop_write
;
1814 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1816 goto out_drop_write
;
1819 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1821 goto out_drop_write
;
1824 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1826 goto out_drop_write
;
1829 down_write(&root
->fs_info
->subvol_sem
);
1832 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1835 root_flags
= btrfs_root_flags(&root
->root_item
);
1836 if (flags
& BTRFS_SUBVOL_RDONLY
) {
1837 btrfs_set_root_flags(&root
->root_item
,
1838 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1841 * Block RO -> RW transition if this subvolume is involved in
1844 spin_lock(&root
->root_item_lock
);
1845 if (root
->send_in_progress
== 0) {
1846 btrfs_set_root_flags(&root
->root_item
,
1847 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1848 spin_unlock(&root
->root_item_lock
);
1850 spin_unlock(&root
->root_item_lock
);
1851 btrfs_warn(root
->fs_info
,
1852 "Attempt to set subvolume %llu read-write during send",
1853 root
->root_key
.objectid
);
1859 trans
= btrfs_start_transaction(root
, 1);
1860 if (IS_ERR(trans
)) {
1861 ret
= PTR_ERR(trans
);
1865 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1866 &root
->root_key
, &root
->root_item
);
1868 btrfs_commit_transaction(trans
, root
);
1871 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1873 up_write(&root
->fs_info
->subvol_sem
);
1875 mnt_drop_write_file(file
);
1881 * helper to check if the subvolume references other subvolumes
1883 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1885 struct btrfs_path
*path
;
1886 struct btrfs_dir_item
*di
;
1887 struct btrfs_key key
;
1891 path
= btrfs_alloc_path();
1895 /* Make sure this root isn't set as the default subvol */
1896 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
1897 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, path
,
1898 dir_id
, "default", 7, 0);
1899 if (di
&& !IS_ERR(di
)) {
1900 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1901 if (key
.objectid
== root
->root_key
.objectid
) {
1903 btrfs_err(root
->fs_info
,
1904 "deleting default subvolume %llu is not allowed",
1908 btrfs_release_path(path
);
1911 key
.objectid
= root
->root_key
.objectid
;
1912 key
.type
= BTRFS_ROOT_REF_KEY
;
1913 key
.offset
= (u64
)-1;
1915 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1922 if (path
->slots
[0] > 0) {
1924 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1925 if (key
.objectid
== root
->root_key
.objectid
&&
1926 key
.type
== BTRFS_ROOT_REF_KEY
)
1930 btrfs_free_path(path
);
1934 static noinline
int key_in_sk(struct btrfs_key
*key
,
1935 struct btrfs_ioctl_search_key
*sk
)
1937 struct btrfs_key test
;
1940 test
.objectid
= sk
->min_objectid
;
1941 test
.type
= sk
->min_type
;
1942 test
.offset
= sk
->min_offset
;
1944 ret
= btrfs_comp_cpu_keys(key
, &test
);
1948 test
.objectid
= sk
->max_objectid
;
1949 test
.type
= sk
->max_type
;
1950 test
.offset
= sk
->max_offset
;
1952 ret
= btrfs_comp_cpu_keys(key
, &test
);
1958 static noinline
int copy_to_sk(struct btrfs_path
*path
,
1959 struct btrfs_key
*key
,
1960 struct btrfs_ioctl_search_key
*sk
,
1963 unsigned long *sk_offset
,
1967 struct extent_buffer
*leaf
;
1968 struct btrfs_ioctl_search_header sh
;
1969 struct btrfs_key test
;
1970 unsigned long item_off
;
1971 unsigned long item_len
;
1977 leaf
= path
->nodes
[0];
1978 slot
= path
->slots
[0];
1979 nritems
= btrfs_header_nritems(leaf
);
1981 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1985 found_transid
= btrfs_header_generation(leaf
);
1987 for (i
= slot
; i
< nritems
; i
++) {
1988 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1989 item_len
= btrfs_item_size_nr(leaf
, i
);
1991 btrfs_item_key_to_cpu(leaf
, key
, i
);
1992 if (!key_in_sk(key
, sk
))
1995 if (sizeof(sh
) + item_len
> *buf_size
) {
2002 * return one empty item back for v1, which does not
2006 *buf_size
= sizeof(sh
) + item_len
;
2011 if (sizeof(sh
) + item_len
+ *sk_offset
> *buf_size
) {
2016 sh
.objectid
= key
->objectid
;
2017 sh
.offset
= key
->offset
;
2018 sh
.type
= key
->type
;
2020 sh
.transid
= found_transid
;
2022 /* copy search result header */
2023 if (copy_to_user(ubuf
+ *sk_offset
, &sh
, sizeof(sh
))) {
2028 *sk_offset
+= sizeof(sh
);
2031 char __user
*up
= ubuf
+ *sk_offset
;
2033 if (read_extent_buffer_to_user(leaf
, up
,
2034 item_off
, item_len
)) {
2039 *sk_offset
+= item_len
;
2043 if (ret
) /* -EOVERFLOW from above */
2046 if (*num_found
>= sk
->nr_items
) {
2053 test
.objectid
= sk
->max_objectid
;
2054 test
.type
= sk
->max_type
;
2055 test
.offset
= sk
->max_offset
;
2056 if (btrfs_comp_cpu_keys(key
, &test
) >= 0)
2058 else if (key
->offset
< (u64
)-1)
2060 else if (key
->type
< (u8
)-1) {
2063 } else if (key
->objectid
< (u64
)-1) {
2071 * 0: all items from this leaf copied, continue with next
2072 * 1: * more items can be copied, but unused buffer is too small
2073 * * all items were found
2074 * Either way, it will stops the loop which iterates to the next
2076 * -EOVERFLOW: item was to large for buffer
2077 * -EFAULT: could not copy extent buffer back to userspace
2082 static noinline
int search_ioctl(struct inode
*inode
,
2083 struct btrfs_ioctl_search_key
*sk
,
2087 struct btrfs_root
*root
;
2088 struct btrfs_key key
;
2089 struct btrfs_path
*path
;
2090 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
2093 unsigned long sk_offset
= 0;
2095 if (*buf_size
< sizeof(struct btrfs_ioctl_search_header
)) {
2096 *buf_size
= sizeof(struct btrfs_ioctl_search_header
);
2100 path
= btrfs_alloc_path();
2104 if (sk
->tree_id
== 0) {
2105 /* search the root of the inode that was passed */
2106 root
= BTRFS_I(inode
)->root
;
2108 key
.objectid
= sk
->tree_id
;
2109 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2110 key
.offset
= (u64
)-1;
2111 root
= btrfs_read_fs_root_no_name(info
, &key
);
2113 btrfs_free_path(path
);
2118 key
.objectid
= sk
->min_objectid
;
2119 key
.type
= sk
->min_type
;
2120 key
.offset
= sk
->min_offset
;
2123 ret
= btrfs_search_forward(root
, &key
, path
, sk
->min_transid
);
2129 ret
= copy_to_sk(path
, &key
, sk
, buf_size
, ubuf
,
2130 &sk_offset
, &num_found
);
2131 btrfs_release_path(path
);
2139 sk
->nr_items
= num_found
;
2140 btrfs_free_path(path
);
2144 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
2147 struct btrfs_ioctl_search_args __user
*uargs
;
2148 struct btrfs_ioctl_search_key sk
;
2149 struct inode
*inode
;
2153 if (!capable(CAP_SYS_ADMIN
))
2156 uargs
= (struct btrfs_ioctl_search_args __user
*)argp
;
2158 if (copy_from_user(&sk
, &uargs
->key
, sizeof(sk
)))
2161 buf_size
= sizeof(uargs
->buf
);
2163 inode
= file_inode(file
);
2164 ret
= search_ioctl(inode
, &sk
, &buf_size
, uargs
->buf
);
2167 * In the origin implementation an overflow is handled by returning a
2168 * search header with a len of zero, so reset ret.
2170 if (ret
== -EOVERFLOW
)
2173 if (ret
== 0 && copy_to_user(&uargs
->key
, &sk
, sizeof(sk
)))
2178 static noinline
int btrfs_ioctl_tree_search_v2(struct file
*file
,
2181 struct btrfs_ioctl_search_args_v2 __user
*uarg
;
2182 struct btrfs_ioctl_search_args_v2 args
;
2183 struct inode
*inode
;
2186 const size_t buf_limit
= SZ_16M
;
2188 if (!capable(CAP_SYS_ADMIN
))
2191 /* copy search header and buffer size */
2192 uarg
= (struct btrfs_ioctl_search_args_v2 __user
*)argp
;
2193 if (copy_from_user(&args
, uarg
, sizeof(args
)))
2196 buf_size
= args
.buf_size
;
2198 if (buf_size
< sizeof(struct btrfs_ioctl_search_header
))
2201 /* limit result size to 16MB */
2202 if (buf_size
> buf_limit
)
2203 buf_size
= buf_limit
;
2205 inode
= file_inode(file
);
2206 ret
= search_ioctl(inode
, &args
.key
, &buf_size
,
2207 (char *)(&uarg
->buf
[0]));
2208 if (ret
== 0 && copy_to_user(&uarg
->key
, &args
.key
, sizeof(args
.key
)))
2210 else if (ret
== -EOVERFLOW
&&
2211 copy_to_user(&uarg
->buf_size
, &buf_size
, sizeof(buf_size
)))
2218 * Search INODE_REFs to identify path name of 'dirid' directory
2219 * in a 'tree_id' tree. and sets path name to 'name'.
2221 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
2222 u64 tree_id
, u64 dirid
, char *name
)
2224 struct btrfs_root
*root
;
2225 struct btrfs_key key
;
2231 struct btrfs_inode_ref
*iref
;
2232 struct extent_buffer
*l
;
2233 struct btrfs_path
*path
;
2235 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
2240 path
= btrfs_alloc_path();
2244 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
2246 key
.objectid
= tree_id
;
2247 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2248 key
.offset
= (u64
)-1;
2249 root
= btrfs_read_fs_root_no_name(info
, &key
);
2251 btrfs_err(info
, "could not find root %llu", tree_id
);
2256 key
.objectid
= dirid
;
2257 key
.type
= BTRFS_INODE_REF_KEY
;
2258 key
.offset
= (u64
)-1;
2261 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2265 ret
= btrfs_previous_item(root
, path
, dirid
,
2266 BTRFS_INODE_REF_KEY
);
2276 slot
= path
->slots
[0];
2277 btrfs_item_key_to_cpu(l
, &key
, slot
);
2279 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
2280 len
= btrfs_inode_ref_name_len(l
, iref
);
2282 total_len
+= len
+ 1;
2284 ret
= -ENAMETOOLONG
;
2289 read_extent_buffer(l
, ptr
, (unsigned long)(iref
+ 1), len
);
2291 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
2294 btrfs_release_path(path
);
2295 key
.objectid
= key
.offset
;
2296 key
.offset
= (u64
)-1;
2297 dirid
= key
.objectid
;
2299 memmove(name
, ptr
, total_len
);
2300 name
[total_len
] = '\0';
2303 btrfs_free_path(path
);
2307 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
2310 struct btrfs_ioctl_ino_lookup_args
*args
;
2311 struct inode
*inode
;
2314 args
= memdup_user(argp
, sizeof(*args
));
2316 return PTR_ERR(args
);
2318 inode
= file_inode(file
);
2321 * Unprivileged query to obtain the containing subvolume root id. The
2322 * path is reset so it's consistent with btrfs_search_path_in_tree.
2324 if (args
->treeid
== 0)
2325 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2327 if (args
->objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
2332 if (!capable(CAP_SYS_ADMIN
)) {
2337 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
2338 args
->treeid
, args
->objectid
,
2342 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2349 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
2352 struct dentry
*parent
= file
->f_path
.dentry
;
2353 struct dentry
*dentry
;
2354 struct inode
*dir
= d_inode(parent
);
2355 struct inode
*inode
;
2356 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2357 struct btrfs_root
*dest
= NULL
;
2358 struct btrfs_ioctl_vol_args
*vol_args
;
2359 struct btrfs_trans_handle
*trans
;
2360 struct btrfs_block_rsv block_rsv
;
2362 u64 qgroup_reserved
;
2367 if (!S_ISDIR(dir
->i_mode
))
2370 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2371 if (IS_ERR(vol_args
))
2372 return PTR_ERR(vol_args
);
2374 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2375 namelen
= strlen(vol_args
->name
);
2376 if (strchr(vol_args
->name
, '/') ||
2377 strncmp(vol_args
->name
, "..", namelen
) == 0) {
2382 err
= mnt_want_write_file(file
);
2387 err
= down_write_killable_nested(&dir
->i_rwsem
, I_MUTEX_PARENT
);
2389 goto out_drop_write
;
2390 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
2391 if (IS_ERR(dentry
)) {
2392 err
= PTR_ERR(dentry
);
2393 goto out_unlock_dir
;
2396 if (d_really_is_negative(dentry
)) {
2401 inode
= d_inode(dentry
);
2402 dest
= BTRFS_I(inode
)->root
;
2403 if (!capable(CAP_SYS_ADMIN
)) {
2405 * Regular user. Only allow this with a special mount
2406 * option, when the user has write+exec access to the
2407 * subvol root, and when rmdir(2) would have been
2410 * Note that this is _not_ check that the subvol is
2411 * empty or doesn't contain data that we wouldn't
2412 * otherwise be able to delete.
2414 * Users who want to delete empty subvols should try
2418 if (!btrfs_test_opt(root
->fs_info
, USER_SUBVOL_RM_ALLOWED
))
2422 * Do not allow deletion if the parent dir is the same
2423 * as the dir to be deleted. That means the ioctl
2424 * must be called on the dentry referencing the root
2425 * of the subvol, not a random directory contained
2432 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
2437 /* check if subvolume may be deleted by a user */
2438 err
= btrfs_may_delete(dir
, dentry
, 1);
2442 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
2450 * Don't allow to delete a subvolume with send in progress. This is
2451 * inside the i_mutex so the error handling that has to drop the bit
2452 * again is not run concurrently.
2454 spin_lock(&dest
->root_item_lock
);
2455 root_flags
= btrfs_root_flags(&dest
->root_item
);
2456 if (dest
->send_in_progress
== 0) {
2457 btrfs_set_root_flags(&dest
->root_item
,
2458 root_flags
| BTRFS_ROOT_SUBVOL_DEAD
);
2459 spin_unlock(&dest
->root_item_lock
);
2461 spin_unlock(&dest
->root_item_lock
);
2462 btrfs_warn(root
->fs_info
,
2463 "Attempt to delete subvolume %llu during send",
2464 dest
->root_key
.objectid
);
2466 goto out_unlock_inode
;
2469 down_write(&root
->fs_info
->subvol_sem
);
2471 err
= may_destroy_subvol(dest
);
2475 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
2477 * One for dir inode, two for dir entries, two for root
2480 err
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
2481 5, &qgroup_reserved
, true);
2485 trans
= btrfs_start_transaction(root
, 0);
2486 if (IS_ERR(trans
)) {
2487 err
= PTR_ERR(trans
);
2490 trans
->block_rsv
= &block_rsv
;
2491 trans
->bytes_reserved
= block_rsv
.size
;
2493 btrfs_record_snapshot_destroy(trans
, dir
);
2495 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
2496 dest
->root_key
.objectid
,
2497 dentry
->d_name
.name
,
2498 dentry
->d_name
.len
);
2501 btrfs_abort_transaction(trans
, ret
);
2505 btrfs_record_root_in_trans(trans
, dest
);
2507 memset(&dest
->root_item
.drop_progress
, 0,
2508 sizeof(dest
->root_item
.drop_progress
));
2509 dest
->root_item
.drop_level
= 0;
2510 btrfs_set_root_refs(&dest
->root_item
, 0);
2512 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &dest
->state
)) {
2513 ret
= btrfs_insert_orphan_item(trans
,
2514 root
->fs_info
->tree_root
,
2515 dest
->root_key
.objectid
);
2517 btrfs_abort_transaction(trans
, ret
);
2523 ret
= btrfs_uuid_tree_rem(trans
, root
->fs_info
, dest
->root_item
.uuid
,
2524 BTRFS_UUID_KEY_SUBVOL
,
2525 dest
->root_key
.objectid
);
2526 if (ret
&& ret
!= -ENOENT
) {
2527 btrfs_abort_transaction(trans
, ret
);
2531 if (!btrfs_is_empty_uuid(dest
->root_item
.received_uuid
)) {
2532 ret
= btrfs_uuid_tree_rem(trans
, root
->fs_info
,
2533 dest
->root_item
.received_uuid
,
2534 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
2535 dest
->root_key
.objectid
);
2536 if (ret
&& ret
!= -ENOENT
) {
2537 btrfs_abort_transaction(trans
, ret
);
2544 trans
->block_rsv
= NULL
;
2545 trans
->bytes_reserved
= 0;
2546 ret
= btrfs_end_transaction(trans
, root
);
2549 inode
->i_flags
|= S_DEAD
;
2551 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
2553 up_write(&root
->fs_info
->subvol_sem
);
2555 spin_lock(&dest
->root_item_lock
);
2556 root_flags
= btrfs_root_flags(&dest
->root_item
);
2557 btrfs_set_root_flags(&dest
->root_item
,
2558 root_flags
& ~BTRFS_ROOT_SUBVOL_DEAD
);
2559 spin_unlock(&dest
->root_item_lock
);
2562 inode_unlock(inode
);
2564 d_invalidate(dentry
);
2565 btrfs_invalidate_inodes(dest
);
2567 ASSERT(dest
->send_in_progress
== 0);
2570 if (dest
->ino_cache_inode
) {
2571 iput(dest
->ino_cache_inode
);
2572 dest
->ino_cache_inode
= NULL
;
2580 mnt_drop_write_file(file
);
2586 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2588 struct inode
*inode
= file_inode(file
);
2589 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2590 struct btrfs_ioctl_defrag_range_args
*range
;
2593 ret
= mnt_want_write_file(file
);
2597 if (btrfs_root_readonly(root
)) {
2602 switch (inode
->i_mode
& S_IFMT
) {
2604 if (!capable(CAP_SYS_ADMIN
)) {
2608 ret
= btrfs_defrag_root(root
);
2611 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
);
2614 if (!(file
->f_mode
& FMODE_WRITE
)) {
2619 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2626 if (copy_from_user(range
, argp
,
2632 /* compression requires us to start the IO */
2633 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2634 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2635 range
->extent_thresh
= (u32
)-1;
2638 /* the rest are all set to zero by kzalloc */
2639 range
->len
= (u64
)-1;
2641 ret
= btrfs_defrag_file(file_inode(file
), file
,
2651 mnt_drop_write_file(file
);
2655 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2657 struct btrfs_ioctl_vol_args
*vol_args
;
2660 if (!capable(CAP_SYS_ADMIN
))
2663 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2665 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2668 mutex_lock(&root
->fs_info
->volume_mutex
);
2669 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2670 if (IS_ERR(vol_args
)) {
2671 ret
= PTR_ERR(vol_args
);
2675 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2676 ret
= btrfs_init_new_device(root
->fs_info
, vol_args
->name
);
2679 btrfs_info(root
->fs_info
, "disk added %s",vol_args
->name
);
2683 mutex_unlock(&root
->fs_info
->volume_mutex
);
2684 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2688 static long btrfs_ioctl_rm_dev_v2(struct file
*file
, void __user
*arg
)
2690 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
2691 struct btrfs_ioctl_vol_args_v2
*vol_args
;
2694 if (!capable(CAP_SYS_ADMIN
))
2697 ret
= mnt_want_write_file(file
);
2701 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2702 if (IS_ERR(vol_args
)) {
2703 ret
= PTR_ERR(vol_args
);
2707 /* Check for compatibility reject unknown flags */
2708 if (vol_args
->flags
& ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED
)
2711 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2713 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2717 mutex_lock(&root
->fs_info
->volume_mutex
);
2718 if (vol_args
->flags
& BTRFS_DEVICE_SPEC_BY_ID
) {
2719 ret
= btrfs_rm_device(root
, NULL
, vol_args
->devid
);
2721 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
2722 ret
= btrfs_rm_device(root
, vol_args
->name
, 0);
2724 mutex_unlock(&root
->fs_info
->volume_mutex
);
2725 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2728 if (vol_args
->flags
& BTRFS_DEVICE_SPEC_BY_ID
)
2729 btrfs_info(root
->fs_info
, "device deleted: id %llu",
2732 btrfs_info(root
->fs_info
, "device deleted: %s",
2738 mnt_drop_write_file(file
);
2742 static long btrfs_ioctl_rm_dev(struct file
*file
, void __user
*arg
)
2744 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
2745 struct btrfs_ioctl_vol_args
*vol_args
;
2748 if (!capable(CAP_SYS_ADMIN
))
2751 ret
= mnt_want_write_file(file
);
2755 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2757 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2758 goto out_drop_write
;
2761 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2762 if (IS_ERR(vol_args
)) {
2763 ret
= PTR_ERR(vol_args
);
2767 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2768 mutex_lock(&root
->fs_info
->volume_mutex
);
2769 ret
= btrfs_rm_device(root
, vol_args
->name
, 0);
2770 mutex_unlock(&root
->fs_info
->volume_mutex
);
2773 btrfs_info(root
->fs_info
, "disk deleted %s",vol_args
->name
);
2776 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2778 mnt_drop_write_file(file
);
2783 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2785 struct btrfs_ioctl_fs_info_args
*fi_args
;
2786 struct btrfs_device
*device
;
2787 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2790 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2794 mutex_lock(&fs_devices
->device_list_mutex
);
2795 fi_args
->num_devices
= fs_devices
->num_devices
;
2796 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2798 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
2799 if (device
->devid
> fi_args
->max_id
)
2800 fi_args
->max_id
= device
->devid
;
2802 mutex_unlock(&fs_devices
->device_list_mutex
);
2804 fi_args
->nodesize
= root
->fs_info
->super_copy
->nodesize
;
2805 fi_args
->sectorsize
= root
->fs_info
->super_copy
->sectorsize
;
2806 fi_args
->clone_alignment
= root
->fs_info
->super_copy
->sectorsize
;
2808 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2815 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2817 struct btrfs_ioctl_dev_info_args
*di_args
;
2818 struct btrfs_device
*dev
;
2819 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2821 char *s_uuid
= NULL
;
2823 di_args
= memdup_user(arg
, sizeof(*di_args
));
2824 if (IS_ERR(di_args
))
2825 return PTR_ERR(di_args
);
2827 if (!btrfs_is_empty_uuid(di_args
->uuid
))
2828 s_uuid
= di_args
->uuid
;
2830 mutex_lock(&fs_devices
->device_list_mutex
);
2831 dev
= btrfs_find_device(root
->fs_info
, di_args
->devid
, s_uuid
, NULL
);
2838 di_args
->devid
= dev
->devid
;
2839 di_args
->bytes_used
= btrfs_device_get_bytes_used(dev
);
2840 di_args
->total_bytes
= btrfs_device_get_total_bytes(dev
);
2841 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2843 struct rcu_string
*name
;
2846 name
= rcu_dereference(dev
->name
);
2847 strncpy(di_args
->path
, name
->str
, sizeof(di_args
->path
));
2849 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
2851 di_args
->path
[0] = '\0';
2855 mutex_unlock(&fs_devices
->device_list_mutex
);
2856 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2863 static struct page
*extent_same_get_page(struct inode
*inode
, pgoff_t index
)
2867 page
= grab_cache_page(inode
->i_mapping
, index
);
2869 return ERR_PTR(-ENOMEM
);
2871 if (!PageUptodate(page
)) {
2874 ret
= btrfs_readpage(NULL
, page
);
2876 return ERR_PTR(ret
);
2878 if (!PageUptodate(page
)) {
2881 return ERR_PTR(-EIO
);
2883 if (page
->mapping
!= inode
->i_mapping
) {
2886 return ERR_PTR(-EAGAIN
);
2893 static int gather_extent_pages(struct inode
*inode
, struct page
**pages
,
2894 int num_pages
, u64 off
)
2897 pgoff_t index
= off
>> PAGE_SHIFT
;
2899 for (i
= 0; i
< num_pages
; i
++) {
2901 pages
[i
] = extent_same_get_page(inode
, index
+ i
);
2902 if (IS_ERR(pages
[i
])) {
2903 int err
= PTR_ERR(pages
[i
]);
2914 static int lock_extent_range(struct inode
*inode
, u64 off
, u64 len
,
2915 bool retry_range_locking
)
2918 * Do any pending delalloc/csum calculations on inode, one way or
2919 * another, and lock file content.
2920 * The locking order is:
2923 * 2) range in the inode's io tree
2926 struct btrfs_ordered_extent
*ordered
;
2927 lock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2928 ordered
= btrfs_lookup_first_ordered_extent(inode
,
2931 ordered
->file_offset
+ ordered
->len
<= off
||
2932 ordered
->file_offset
>= off
+ len
) &&
2933 !test_range_bit(&BTRFS_I(inode
)->io_tree
, off
,
2934 off
+ len
- 1, EXTENT_DELALLOC
, 0, NULL
)) {
2936 btrfs_put_ordered_extent(ordered
);
2939 unlock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2941 btrfs_put_ordered_extent(ordered
);
2942 if (!retry_range_locking
)
2944 btrfs_wait_ordered_range(inode
, off
, len
);
2949 static void btrfs_double_inode_unlock(struct inode
*inode1
, struct inode
*inode2
)
2951 inode_unlock(inode1
);
2952 inode_unlock(inode2
);
2955 static void btrfs_double_inode_lock(struct inode
*inode1
, struct inode
*inode2
)
2957 if (inode1
< inode2
)
2958 swap(inode1
, inode2
);
2960 inode_lock_nested(inode1
, I_MUTEX_PARENT
);
2961 inode_lock_nested(inode2
, I_MUTEX_CHILD
);
2964 static void btrfs_double_extent_unlock(struct inode
*inode1
, u64 loff1
,
2965 struct inode
*inode2
, u64 loff2
, u64 len
)
2967 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
2968 unlock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
2971 static int btrfs_double_extent_lock(struct inode
*inode1
, u64 loff1
,
2972 struct inode
*inode2
, u64 loff2
, u64 len
,
2973 bool retry_range_locking
)
2977 if (inode1
< inode2
) {
2978 swap(inode1
, inode2
);
2981 ret
= lock_extent_range(inode1
, loff1
, len
, retry_range_locking
);
2984 ret
= lock_extent_range(inode2
, loff2
, len
, retry_range_locking
);
2986 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
,
2993 struct page
**src_pages
;
2994 struct page
**dst_pages
;
2997 static void btrfs_cmp_data_free(struct cmp_pages
*cmp
)
3002 for (i
= 0; i
< cmp
->num_pages
; i
++) {
3003 pg
= cmp
->src_pages
[i
];
3008 pg
= cmp
->dst_pages
[i
];
3014 kfree(cmp
->src_pages
);
3015 kfree(cmp
->dst_pages
);
3018 static int btrfs_cmp_data_prepare(struct inode
*src
, u64 loff
,
3019 struct inode
*dst
, u64 dst_loff
,
3020 u64 len
, struct cmp_pages
*cmp
)
3023 int num_pages
= PAGE_ALIGN(len
) >> PAGE_SHIFT
;
3024 struct page
**src_pgarr
, **dst_pgarr
;
3027 * We must gather up all the pages before we initiate our
3028 * extent locking. We use an array for the page pointers. Size
3029 * of the array is bounded by len, which is in turn bounded by
3030 * BTRFS_MAX_DEDUPE_LEN.
3032 src_pgarr
= kcalloc(num_pages
, sizeof(struct page
*), GFP_KERNEL
);
3033 dst_pgarr
= kcalloc(num_pages
, sizeof(struct page
*), GFP_KERNEL
);
3034 if (!src_pgarr
|| !dst_pgarr
) {
3039 cmp
->num_pages
= num_pages
;
3040 cmp
->src_pages
= src_pgarr
;
3041 cmp
->dst_pages
= dst_pgarr
;
3043 ret
= gather_extent_pages(src
, cmp
->src_pages
, cmp
->num_pages
, loff
);
3047 ret
= gather_extent_pages(dst
, cmp
->dst_pages
, cmp
->num_pages
, dst_loff
);
3051 btrfs_cmp_data_free(cmp
);
3055 static int btrfs_cmp_data(struct inode
*src
, u64 loff
, struct inode
*dst
,
3056 u64 dst_loff
, u64 len
, struct cmp_pages
*cmp
)
3060 struct page
*src_page
, *dst_page
;
3061 unsigned int cmp_len
= PAGE_SIZE
;
3062 void *addr
, *dst_addr
;
3066 if (len
< PAGE_SIZE
)
3069 BUG_ON(i
>= cmp
->num_pages
);
3071 src_page
= cmp
->src_pages
[i
];
3072 dst_page
= cmp
->dst_pages
[i
];
3073 ASSERT(PageLocked(src_page
));
3074 ASSERT(PageLocked(dst_page
));
3076 addr
= kmap_atomic(src_page
);
3077 dst_addr
= kmap_atomic(dst_page
);
3079 flush_dcache_page(src_page
);
3080 flush_dcache_page(dst_page
);
3082 if (memcmp(addr
, dst_addr
, cmp_len
))
3085 kunmap_atomic(addr
);
3086 kunmap_atomic(dst_addr
);
3098 static int extent_same_check_offsets(struct inode
*inode
, u64 off
, u64
*plen
,
3102 u64 bs
= BTRFS_I(inode
)->root
->fs_info
->sb
->s_blocksize
;
3104 if (off
+ olen
> inode
->i_size
|| off
+ olen
< off
)
3107 /* if we extend to eof, continue to block boundary */
3108 if (off
+ len
== inode
->i_size
)
3109 *plen
= len
= ALIGN(inode
->i_size
, bs
) - off
;
3111 /* Check that we are block aligned - btrfs_clone() requires this */
3112 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
))
3118 static int btrfs_extent_same(struct inode
*src
, u64 loff
, u64 olen
,
3119 struct inode
*dst
, u64 dst_loff
)
3123 struct cmp_pages cmp
;
3125 u64 same_lock_start
= 0;
3126 u64 same_lock_len
= 0;
3137 ret
= extent_same_check_offsets(src
, loff
, &len
, olen
);
3140 ret
= extent_same_check_offsets(src
, dst_loff
, &len
, olen
);
3145 * Single inode case wants the same checks, except we
3146 * don't want our length pushed out past i_size as
3147 * comparing that data range makes no sense.
3149 * extent_same_check_offsets() will do this for an
3150 * unaligned length at i_size, so catch it here and
3151 * reject the request.
3153 * This effectively means we require aligned extents
3154 * for the single-inode case, whereas the other cases
3155 * allow an unaligned length so long as it ends at
3163 /* Check for overlapping ranges */
3164 if (dst_loff
+ len
> loff
&& dst_loff
< loff
+ len
) {
3169 same_lock_start
= min_t(u64
, loff
, dst_loff
);
3170 same_lock_len
= max_t(u64
, loff
, dst_loff
) + len
- same_lock_start
;
3172 btrfs_double_inode_lock(src
, dst
);
3174 ret
= extent_same_check_offsets(src
, loff
, &len
, olen
);
3178 ret
= extent_same_check_offsets(dst
, dst_loff
, &len
, olen
);
3183 /* don't make the dst file partly checksummed */
3184 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3185 (BTRFS_I(dst
)->flags
& BTRFS_INODE_NODATASUM
)) {
3191 ret
= btrfs_cmp_data_prepare(src
, loff
, dst
, dst_loff
, olen
, &cmp
);
3196 ret
= lock_extent_range(src
, same_lock_start
, same_lock_len
,
3199 ret
= btrfs_double_extent_lock(src
, loff
, dst
, dst_loff
, len
,
3202 * If one of the inodes has dirty pages in the respective range or
3203 * ordered extents, we need to flush dellaloc and wait for all ordered
3204 * extents in the range. We must unlock the pages and the ranges in the
3205 * io trees to avoid deadlocks when flushing delalloc (requires locking
3206 * pages) and when waiting for ordered extents to complete (they require
3209 if (ret
== -EAGAIN
) {
3211 * Ranges in the io trees already unlocked. Now unlock all
3212 * pages before waiting for all IO to complete.
3214 btrfs_cmp_data_free(&cmp
);
3216 btrfs_wait_ordered_range(src
, same_lock_start
,
3219 btrfs_wait_ordered_range(src
, loff
, len
);
3220 btrfs_wait_ordered_range(dst
, dst_loff
, len
);
3226 /* ranges in the io trees already unlocked */
3227 btrfs_cmp_data_free(&cmp
);
3231 /* pass original length for comparison so we stay within i_size */
3232 ret
= btrfs_cmp_data(src
, loff
, dst
, dst_loff
, olen
, &cmp
);
3234 ret
= btrfs_clone(src
, dst
, loff
, olen
, len
, dst_loff
, 1);
3237 unlock_extent(&BTRFS_I(src
)->io_tree
, same_lock_start
,
3238 same_lock_start
+ same_lock_len
- 1);
3240 btrfs_double_extent_unlock(src
, loff
, dst
, dst_loff
, len
);
3242 btrfs_cmp_data_free(&cmp
);
3247 btrfs_double_inode_unlock(src
, dst
);
3252 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3254 ssize_t
btrfs_dedupe_file_range(struct file
*src_file
, u64 loff
, u64 olen
,
3255 struct file
*dst_file
, u64 dst_loff
)
3257 struct inode
*src
= file_inode(src_file
);
3258 struct inode
*dst
= file_inode(dst_file
);
3259 u64 bs
= BTRFS_I(src
)->root
->fs_info
->sb
->s_blocksize
;
3262 if (olen
> BTRFS_MAX_DEDUPE_LEN
)
3263 olen
= BTRFS_MAX_DEDUPE_LEN
;
3265 if (WARN_ON_ONCE(bs
< PAGE_SIZE
)) {
3267 * Btrfs does not support blocksize < page_size. As a
3268 * result, btrfs_cmp_data() won't correctly handle
3269 * this situation without an update.
3274 res
= btrfs_extent_same(src
, loff
, olen
, dst
, dst_loff
);
3280 static int clone_finish_inode_update(struct btrfs_trans_handle
*trans
,
3281 struct inode
*inode
,
3287 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3290 inode_inc_iversion(inode
);
3291 if (!no_time_update
)
3292 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
3294 * We round up to the block size at eof when determining which
3295 * extents to clone above, but shouldn't round up the file size.
3297 if (endoff
> destoff
+ olen
)
3298 endoff
= destoff
+ olen
;
3299 if (endoff
> inode
->i_size
)
3300 btrfs_i_size_write(inode
, endoff
);
3302 ret
= btrfs_update_inode(trans
, root
, inode
);
3304 btrfs_abort_transaction(trans
, ret
);
3305 btrfs_end_transaction(trans
, root
);
3308 ret
= btrfs_end_transaction(trans
, root
);
3313 static void clone_update_extent_map(struct inode
*inode
,
3314 const struct btrfs_trans_handle
*trans
,
3315 const struct btrfs_path
*path
,
3316 const u64 hole_offset
,
3319 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
3320 struct extent_map
*em
;
3323 em
= alloc_extent_map();
3325 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3326 &BTRFS_I(inode
)->runtime_flags
);
3331 struct btrfs_file_extent_item
*fi
;
3333 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3334 struct btrfs_file_extent_item
);
3335 btrfs_extent_item_to_extent_map(inode
, path
, fi
, false, em
);
3336 em
->generation
= -1;
3337 if (btrfs_file_extent_type(path
->nodes
[0], fi
) ==
3338 BTRFS_FILE_EXTENT_INLINE
)
3339 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3340 &BTRFS_I(inode
)->runtime_flags
);
3342 em
->start
= hole_offset
;
3344 em
->ram_bytes
= em
->len
;
3345 em
->orig_start
= hole_offset
;
3346 em
->block_start
= EXTENT_MAP_HOLE
;
3348 em
->orig_block_len
= 0;
3349 em
->compress_type
= BTRFS_COMPRESS_NONE
;
3350 em
->generation
= trans
->transid
;
3354 write_lock(&em_tree
->lock
);
3355 ret
= add_extent_mapping(em_tree
, em
, 1);
3356 write_unlock(&em_tree
->lock
);
3357 if (ret
!= -EEXIST
) {
3358 free_extent_map(em
);
3361 btrfs_drop_extent_cache(inode
, em
->start
,
3362 em
->start
+ em
->len
- 1, 0);
3366 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3367 &BTRFS_I(inode
)->runtime_flags
);
3371 * Make sure we do not end up inserting an inline extent into a file that has
3372 * already other (non-inline) extents. If a file has an inline extent it can
3373 * not have any other extents and the (single) inline extent must start at the
3374 * file offset 0. Failing to respect these rules will lead to file corruption,
3375 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3377 * We can have extents that have been already written to disk or we can have
3378 * dirty ranges still in delalloc, in which case the extent maps and items are
3379 * created only when we run delalloc, and the delalloc ranges might fall outside
3380 * the range we are currently locking in the inode's io tree. So we check the
3381 * inode's i_size because of that (i_size updates are done while holding the
3382 * i_mutex, which we are holding here).
3383 * We also check to see if the inode has a size not greater than "datal" but has
3384 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3385 * protected against such concurrent fallocate calls by the i_mutex).
3387 * If the file has no extents but a size greater than datal, do not allow the
3388 * copy because we would need turn the inline extent into a non-inline one (even
3389 * with NO_HOLES enabled). If we find our destination inode only has one inline
3390 * extent, just overwrite it with the source inline extent if its size is less
3391 * than the source extent's size, or we could copy the source inline extent's
3392 * data into the destination inode's inline extent if the later is greater then
3395 static int clone_copy_inline_extent(struct inode
*src
,
3397 struct btrfs_trans_handle
*trans
,
3398 struct btrfs_path
*path
,
3399 struct btrfs_key
*new_key
,
3400 const u64 drop_start
,
3406 struct btrfs_root
*root
= BTRFS_I(dst
)->root
;
3407 const u64 aligned_end
= ALIGN(new_key
->offset
+ datal
,
3408 root
->fs_info
->sectorsize
);
3410 struct btrfs_key key
;
3412 if (new_key
->offset
> 0)
3415 key
.objectid
= btrfs_ino(dst
);
3416 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3418 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3421 } else if (ret
> 0) {
3422 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
3423 ret
= btrfs_next_leaf(root
, path
);
3427 goto copy_inline_extent
;
3429 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3430 if (key
.objectid
== btrfs_ino(dst
) &&
3431 key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3432 ASSERT(key
.offset
> 0);
3435 } else if (i_size_read(dst
) <= datal
) {
3436 struct btrfs_file_extent_item
*ei
;
3440 * If the file size is <= datal, make sure there are no other
3441 * extents following (can happen do to an fallocate call with
3442 * the flag FALLOC_FL_KEEP_SIZE).
3444 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3445 struct btrfs_file_extent_item
);
3447 * If it's an inline extent, it can not have other extents
3450 if (btrfs_file_extent_type(path
->nodes
[0], ei
) ==
3451 BTRFS_FILE_EXTENT_INLINE
)
3452 goto copy_inline_extent
;
3454 ext_len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
3455 if (ext_len
> aligned_end
)
3458 ret
= btrfs_next_item(root
, path
);
3461 } else if (ret
== 0) {
3462 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3464 if (key
.objectid
== btrfs_ino(dst
) &&
3465 key
.type
== BTRFS_EXTENT_DATA_KEY
)
3472 * We have no extent items, or we have an extent at offset 0 which may
3473 * or may not be inlined. All these cases are dealt the same way.
3475 if (i_size_read(dst
) > datal
) {
3477 * If the destination inode has an inline extent...
3478 * This would require copying the data from the source inline
3479 * extent into the beginning of the destination's inline extent.
3480 * But this is really complex, both extents can be compressed
3481 * or just one of them, which would require decompressing and
3482 * re-compressing data (which could increase the new compressed
3483 * size, not allowing the compressed data to fit anymore in an
3485 * So just don't support this case for now (it should be rare,
3486 * we are not really saving space when cloning inline extents).
3491 btrfs_release_path(path
);
3492 ret
= btrfs_drop_extents(trans
, root
, dst
, drop_start
, aligned_end
, 1);
3495 ret
= btrfs_insert_empty_item(trans
, root
, path
, new_key
, size
);
3500 const u32 start
= btrfs_file_extent_calc_inline_size(0);
3502 memmove(inline_data
+ start
, inline_data
+ start
+ skip
, datal
);
3505 write_extent_buffer(path
->nodes
[0], inline_data
,
3506 btrfs_item_ptr_offset(path
->nodes
[0],
3509 inode_add_bytes(dst
, datal
);
3515 * btrfs_clone() - clone a range from inode file to another
3517 * @src: Inode to clone from
3518 * @inode: Inode to clone to
3519 * @off: Offset within source to start clone from
3520 * @olen: Original length, passed by user, of range to clone
3521 * @olen_aligned: Block-aligned value of olen
3522 * @destoff: Offset within @inode to start clone
3523 * @no_time_update: Whether to update mtime/ctime on the target inode
3525 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
3526 const u64 off
, const u64 olen
, const u64 olen_aligned
,
3527 const u64 destoff
, int no_time_update
)
3529 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3530 struct btrfs_path
*path
= NULL
;
3531 struct extent_buffer
*leaf
;
3532 struct btrfs_trans_handle
*trans
;
3534 struct btrfs_key key
;
3538 const u64 len
= olen_aligned
;
3539 u64 last_dest_end
= destoff
;
3542 buf
= kmalloc(root
->fs_info
->nodesize
,
3543 GFP_KERNEL
| __GFP_NOWARN
);
3545 buf
= vmalloc(root
->fs_info
->nodesize
);
3550 path
= btrfs_alloc_path();
3556 path
->reada
= READA_FORWARD
;
3558 key
.objectid
= btrfs_ino(src
);
3559 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3563 u64 next_key_min_offset
= key
.offset
+ 1;
3566 * note the key will change type as we walk through the
3569 path
->leave_spinning
= 1;
3570 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
3575 * First search, if no extent item that starts at offset off was
3576 * found but the previous item is an extent item, it's possible
3577 * it might overlap our target range, therefore process it.
3579 if (key
.offset
== off
&& ret
> 0 && path
->slots
[0] > 0) {
3580 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3581 path
->slots
[0] - 1);
3582 if (key
.type
== BTRFS_EXTENT_DATA_KEY
)
3586 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3588 if (path
->slots
[0] >= nritems
) {
3589 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
3594 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3596 leaf
= path
->nodes
[0];
3597 slot
= path
->slots
[0];
3599 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3600 if (key
.type
> BTRFS_EXTENT_DATA_KEY
||
3601 key
.objectid
!= btrfs_ino(src
))
3604 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3605 struct btrfs_file_extent_item
*extent
;
3608 struct btrfs_key new_key
;
3609 u64 disko
= 0, diskl
= 0;
3610 u64 datao
= 0, datal
= 0;
3614 extent
= btrfs_item_ptr(leaf
, slot
,
3615 struct btrfs_file_extent_item
);
3616 comp
= btrfs_file_extent_compression(leaf
, extent
);
3617 type
= btrfs_file_extent_type(leaf
, extent
);
3618 if (type
== BTRFS_FILE_EXTENT_REG
||
3619 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3620 disko
= btrfs_file_extent_disk_bytenr(leaf
,
3622 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
3624 datao
= btrfs_file_extent_offset(leaf
, extent
);
3625 datal
= btrfs_file_extent_num_bytes(leaf
,
3627 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3628 /* take upper bound, may be compressed */
3629 datal
= btrfs_file_extent_ram_bytes(leaf
,
3634 * The first search might have left us at an extent
3635 * item that ends before our target range's start, can
3636 * happen if we have holes and NO_HOLES feature enabled.
3638 if (key
.offset
+ datal
<= off
) {
3641 } else if (key
.offset
>= off
+ len
) {
3644 next_key_min_offset
= key
.offset
+ datal
;
3645 size
= btrfs_item_size_nr(leaf
, slot
);
3646 read_extent_buffer(leaf
, buf
,
3647 btrfs_item_ptr_offset(leaf
, slot
),
3650 btrfs_release_path(path
);
3651 path
->leave_spinning
= 0;
3653 memcpy(&new_key
, &key
, sizeof(new_key
));
3654 new_key
.objectid
= btrfs_ino(inode
);
3655 if (off
<= key
.offset
)
3656 new_key
.offset
= key
.offset
+ destoff
- off
;
3658 new_key
.offset
= destoff
;
3661 * Deal with a hole that doesn't have an extent item
3662 * that represents it (NO_HOLES feature enabled).
3663 * This hole is either in the middle of the cloning
3664 * range or at the beginning (fully overlaps it or
3665 * partially overlaps it).
3667 if (new_key
.offset
!= last_dest_end
)
3668 drop_start
= last_dest_end
;
3670 drop_start
= new_key
.offset
;
3673 * 1 - adjusting old extent (we may have to split it)
3674 * 1 - add new extent
3677 trans
= btrfs_start_transaction(root
, 3);
3678 if (IS_ERR(trans
)) {
3679 ret
= PTR_ERR(trans
);
3683 if (type
== BTRFS_FILE_EXTENT_REG
||
3684 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3686 * a | --- range to clone ---| b
3687 * | ------------- extent ------------- |
3690 /* subtract range b */
3691 if (key
.offset
+ datal
> off
+ len
)
3692 datal
= off
+ len
- key
.offset
;
3694 /* subtract range a */
3695 if (off
> key
.offset
) {
3696 datao
+= off
- key
.offset
;
3697 datal
-= off
- key
.offset
;
3700 ret
= btrfs_drop_extents(trans
, root
, inode
,
3702 new_key
.offset
+ datal
,
3705 if (ret
!= -EOPNOTSUPP
)
3706 btrfs_abort_transaction(trans
,
3708 btrfs_end_transaction(trans
, root
);
3712 ret
= btrfs_insert_empty_item(trans
, root
, path
,
3715 btrfs_abort_transaction(trans
, ret
);
3716 btrfs_end_transaction(trans
, root
);
3720 leaf
= path
->nodes
[0];
3721 slot
= path
->slots
[0];
3722 write_extent_buffer(leaf
, buf
,
3723 btrfs_item_ptr_offset(leaf
, slot
),
3726 extent
= btrfs_item_ptr(leaf
, slot
,
3727 struct btrfs_file_extent_item
);
3729 /* disko == 0 means it's a hole */
3733 btrfs_set_file_extent_offset(leaf
, extent
,
3735 btrfs_set_file_extent_num_bytes(leaf
, extent
,
3739 inode_add_bytes(inode
, datal
);
3740 ret
= btrfs_inc_extent_ref(trans
, root
,
3742 root
->root_key
.objectid
,
3744 new_key
.offset
- datao
);
3746 btrfs_abort_transaction(trans
,
3748 btrfs_end_transaction(trans
,
3754 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3758 if (off
> key
.offset
) {
3759 skip
= off
- key
.offset
;
3760 new_key
.offset
+= skip
;
3763 if (key
.offset
+ datal
> off
+ len
)
3764 trim
= key
.offset
+ datal
- (off
+ len
);
3766 if (comp
&& (skip
|| trim
)) {
3768 btrfs_end_transaction(trans
, root
);
3771 size
-= skip
+ trim
;
3772 datal
-= skip
+ trim
;
3774 ret
= clone_copy_inline_extent(src
, inode
,
3781 if (ret
!= -EOPNOTSUPP
)
3782 btrfs_abort_transaction(trans
,
3784 btrfs_end_transaction(trans
, root
);
3787 leaf
= path
->nodes
[0];
3788 slot
= path
->slots
[0];
3791 /* If we have an implicit hole (NO_HOLES feature). */
3792 if (drop_start
< new_key
.offset
)
3793 clone_update_extent_map(inode
, trans
,
3795 new_key
.offset
- drop_start
);
3797 clone_update_extent_map(inode
, trans
, path
, 0, 0);
3799 btrfs_mark_buffer_dirty(leaf
);
3800 btrfs_release_path(path
);
3802 last_dest_end
= ALIGN(new_key
.offset
+ datal
,
3803 root
->fs_info
->sectorsize
);
3804 ret
= clone_finish_inode_update(trans
, inode
,
3810 if (new_key
.offset
+ datal
>= destoff
+ len
)
3813 btrfs_release_path(path
);
3814 key
.offset
= next_key_min_offset
;
3816 if (fatal_signal_pending(current
)) {
3823 if (last_dest_end
< destoff
+ len
) {
3825 * We have an implicit hole (NO_HOLES feature is enabled) that
3826 * fully or partially overlaps our cloning range at its end.
3828 btrfs_release_path(path
);
3831 * 1 - remove extent(s)
3834 trans
= btrfs_start_transaction(root
, 2);
3835 if (IS_ERR(trans
)) {
3836 ret
= PTR_ERR(trans
);
3839 ret
= btrfs_drop_extents(trans
, root
, inode
,
3840 last_dest_end
, destoff
+ len
, 1);
3842 if (ret
!= -EOPNOTSUPP
)
3843 btrfs_abort_transaction(trans
, ret
);
3844 btrfs_end_transaction(trans
, root
);
3847 clone_update_extent_map(inode
, trans
, NULL
, last_dest_end
,
3848 destoff
+ len
- last_dest_end
);
3849 ret
= clone_finish_inode_update(trans
, inode
, destoff
+ len
,
3850 destoff
, olen
, no_time_update
);
3854 btrfs_free_path(path
);
3859 static noinline
int btrfs_clone_files(struct file
*file
, struct file
*file_src
,
3860 u64 off
, u64 olen
, u64 destoff
)
3862 struct inode
*inode
= file_inode(file
);
3863 struct inode
*src
= file_inode(file_src
);
3864 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3867 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
3868 int same_inode
= src
== inode
;
3872 * - split compressed inline extents. annoying: we need to
3873 * decompress into destination's address_space (the file offset
3874 * may change, so source mapping won't do), then recompress (or
3875 * otherwise reinsert) a subrange.
3877 * - split destination inode's inline extents. The inline extents can
3878 * be either compressed or non-compressed.
3881 if (btrfs_root_readonly(root
))
3884 if (file_src
->f_path
.mnt
!= file
->f_path
.mnt
||
3885 src
->i_sb
!= inode
->i_sb
)
3888 /* don't make the dst file partly checksummed */
3889 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3890 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
3893 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
3897 btrfs_double_inode_lock(src
, inode
);
3902 /* determine range to clone */
3904 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
3907 olen
= len
= src
->i_size
- off
;
3908 /* if we extend to eof, continue to block boundary */
3909 if (off
+ len
== src
->i_size
)
3910 len
= ALIGN(src
->i_size
, bs
) - off
;
3917 /* verify the end result is block aligned */
3918 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
3919 !IS_ALIGNED(destoff
, bs
))
3922 /* verify if ranges are overlapped within the same file */
3924 if (destoff
+ len
> off
&& destoff
< off
+ len
)
3928 if (destoff
> inode
->i_size
) {
3929 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
3935 * Lock the target range too. Right after we replace the file extent
3936 * items in the fs tree (which now point to the cloned data), we might
3937 * have a worker replace them with extent items relative to a write
3938 * operation that was issued before this clone operation (i.e. confront
3939 * with inode.c:btrfs_finish_ordered_io).
3942 u64 lock_start
= min_t(u64
, off
, destoff
);
3943 u64 lock_len
= max_t(u64
, off
, destoff
) + len
- lock_start
;
3945 ret
= lock_extent_range(src
, lock_start
, lock_len
, true);
3947 ret
= btrfs_double_extent_lock(src
, off
, inode
, destoff
, len
,
3952 /* ranges in the io trees already unlocked */
3956 ret
= btrfs_clone(src
, inode
, off
, olen
, len
, destoff
, 0);
3959 u64 lock_start
= min_t(u64
, off
, destoff
);
3960 u64 lock_end
= max_t(u64
, off
, destoff
) + len
- 1;
3962 unlock_extent(&BTRFS_I(src
)->io_tree
, lock_start
, lock_end
);
3964 btrfs_double_extent_unlock(src
, off
, inode
, destoff
, len
);
3967 * Truncate page cache pages so that future reads will see the cloned
3968 * data immediately and not the previous data.
3970 truncate_inode_pages_range(&inode
->i_data
,
3971 round_down(destoff
, PAGE_SIZE
),
3972 round_up(destoff
+ len
, PAGE_SIZE
) - 1);
3975 btrfs_double_inode_unlock(src
, inode
);
3981 ssize_t
btrfs_copy_file_range(struct file
*file_in
, loff_t pos_in
,
3982 struct file
*file_out
, loff_t pos_out
,
3983 size_t len
, unsigned int flags
)
3987 ret
= btrfs_clone_files(file_out
, file_in
, pos_in
, len
, pos_out
);
3993 int btrfs_clone_file_range(struct file
*src_file
, loff_t off
,
3994 struct file
*dst_file
, loff_t destoff
, u64 len
)
3996 return btrfs_clone_files(dst_file
, src_file
, off
, len
, destoff
);
4000 * there are many ways the trans_start and trans_end ioctls can lead
4001 * to deadlocks. They should only be used by applications that
4002 * basically own the machine, and have a very in depth understanding
4003 * of all the possible deadlocks and enospc problems.
4005 static long btrfs_ioctl_trans_start(struct file
*file
)
4007 struct inode
*inode
= file_inode(file
);
4008 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4009 struct btrfs_trans_handle
*trans
;
4013 if (!capable(CAP_SYS_ADMIN
))
4017 if (file
->private_data
)
4021 if (btrfs_root_readonly(root
))
4024 ret
= mnt_want_write_file(file
);
4028 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
4031 trans
= btrfs_start_ioctl_transaction(root
);
4035 file
->private_data
= trans
;
4039 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
4040 mnt_drop_write_file(file
);
4045 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
4047 struct inode
*inode
= file_inode(file
);
4048 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4049 struct btrfs_root
*new_root
;
4050 struct btrfs_dir_item
*di
;
4051 struct btrfs_trans_handle
*trans
;
4052 struct btrfs_path
*path
;
4053 struct btrfs_key location
;
4054 struct btrfs_disk_key disk_key
;
4059 if (!capable(CAP_SYS_ADMIN
))
4062 ret
= mnt_want_write_file(file
);
4066 if (copy_from_user(&objectid
, argp
, sizeof(objectid
))) {
4072 objectid
= BTRFS_FS_TREE_OBJECTID
;
4074 location
.objectid
= objectid
;
4075 location
.type
= BTRFS_ROOT_ITEM_KEY
;
4076 location
.offset
= (u64
)-1;
4078 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
4079 if (IS_ERR(new_root
)) {
4080 ret
= PTR_ERR(new_root
);
4084 path
= btrfs_alloc_path();
4089 path
->leave_spinning
= 1;
4091 trans
= btrfs_start_transaction(root
, 1);
4092 if (IS_ERR(trans
)) {
4093 btrfs_free_path(path
);
4094 ret
= PTR_ERR(trans
);
4098 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
4099 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
4100 dir_id
, "default", 7, 1);
4101 if (IS_ERR_OR_NULL(di
)) {
4102 btrfs_free_path(path
);
4103 btrfs_end_transaction(trans
, root
);
4104 btrfs_err(new_root
->fs_info
,
4105 "Umm, you don't have the default diritem, this isn't going to work");
4110 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
4111 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
4112 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4113 btrfs_free_path(path
);
4115 btrfs_set_fs_incompat(root
->fs_info
, DEFAULT_SUBVOL
);
4116 btrfs_end_transaction(trans
, root
);
4118 mnt_drop_write_file(file
);
4122 void btrfs_get_block_group_info(struct list_head
*groups_list
,
4123 struct btrfs_ioctl_space_info
*space
)
4125 struct btrfs_block_group_cache
*block_group
;
4127 space
->total_bytes
= 0;
4128 space
->used_bytes
= 0;
4130 list_for_each_entry(block_group
, groups_list
, list
) {
4131 space
->flags
= block_group
->flags
;
4132 space
->total_bytes
+= block_group
->key
.offset
;
4133 space
->used_bytes
+=
4134 btrfs_block_group_used(&block_group
->item
);
4138 static long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
4140 struct btrfs_ioctl_space_args space_args
;
4141 struct btrfs_ioctl_space_info space
;
4142 struct btrfs_ioctl_space_info
*dest
;
4143 struct btrfs_ioctl_space_info
*dest_orig
;
4144 struct btrfs_ioctl_space_info __user
*user_dest
;
4145 struct btrfs_space_info
*info
;
4146 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
4147 BTRFS_BLOCK_GROUP_SYSTEM
,
4148 BTRFS_BLOCK_GROUP_METADATA
,
4149 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
4156 if (copy_from_user(&space_args
,
4157 (struct btrfs_ioctl_space_args __user
*)arg
,
4158 sizeof(space_args
)))
4161 for (i
= 0; i
< num_types
; i
++) {
4162 struct btrfs_space_info
*tmp
;
4166 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
4168 if (tmp
->flags
== types
[i
]) {
4178 down_read(&info
->groups_sem
);
4179 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4180 if (!list_empty(&info
->block_groups
[c
]))
4183 up_read(&info
->groups_sem
);
4187 * Global block reserve, exported as a space_info
4191 /* space_slots == 0 means they are asking for a count */
4192 if (space_args
.space_slots
== 0) {
4193 space_args
.total_spaces
= slot_count
;
4197 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
4199 alloc_size
= sizeof(*dest
) * slot_count
;
4201 /* we generally have at most 6 or so space infos, one for each raid
4202 * level. So, a whole page should be more than enough for everyone
4204 if (alloc_size
> PAGE_SIZE
)
4207 space_args
.total_spaces
= 0;
4208 dest
= kmalloc(alloc_size
, GFP_KERNEL
);
4213 /* now we have a buffer to copy into */
4214 for (i
= 0; i
< num_types
; i
++) {
4215 struct btrfs_space_info
*tmp
;
4222 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
4224 if (tmp
->flags
== types
[i
]) {
4233 down_read(&info
->groups_sem
);
4234 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4235 if (!list_empty(&info
->block_groups
[c
])) {
4236 btrfs_get_block_group_info(
4237 &info
->block_groups
[c
], &space
);
4238 memcpy(dest
, &space
, sizeof(space
));
4240 space_args
.total_spaces
++;
4246 up_read(&info
->groups_sem
);
4250 * Add global block reserve
4253 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->global_block_rsv
;
4255 spin_lock(&block_rsv
->lock
);
4256 space
.total_bytes
= block_rsv
->size
;
4257 space
.used_bytes
= block_rsv
->size
- block_rsv
->reserved
;
4258 spin_unlock(&block_rsv
->lock
);
4259 space
.flags
= BTRFS_SPACE_INFO_GLOBAL_RSV
;
4260 memcpy(dest
, &space
, sizeof(space
));
4261 space_args
.total_spaces
++;
4264 user_dest
= (struct btrfs_ioctl_space_info __user
*)
4265 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
4267 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
4272 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
4279 * there are many ways the trans_start and trans_end ioctls can lead
4280 * to deadlocks. They should only be used by applications that
4281 * basically own the machine, and have a very in depth understanding
4282 * of all the possible deadlocks and enospc problems.
4284 long btrfs_ioctl_trans_end(struct file
*file
)
4286 struct inode
*inode
= file_inode(file
);
4287 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4288 struct btrfs_trans_handle
*trans
;
4290 trans
= file
->private_data
;
4293 file
->private_data
= NULL
;
4295 btrfs_end_transaction(trans
, root
);
4297 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
4299 mnt_drop_write_file(file
);
4303 static noinline
long btrfs_ioctl_start_sync(struct btrfs_root
*root
,
4306 struct btrfs_trans_handle
*trans
;
4310 trans
= btrfs_attach_transaction_barrier(root
);
4311 if (IS_ERR(trans
)) {
4312 if (PTR_ERR(trans
) != -ENOENT
)
4313 return PTR_ERR(trans
);
4315 /* No running transaction, don't bother */
4316 transid
= root
->fs_info
->last_trans_committed
;
4319 transid
= trans
->transid
;
4320 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
4322 btrfs_end_transaction(trans
, root
);
4327 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
4332 static noinline
long btrfs_ioctl_wait_sync(struct btrfs_root
*root
,
4338 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
4341 transid
= 0; /* current trans */
4343 return btrfs_wait_for_commit(root
, transid
);
4346 static long btrfs_ioctl_scrub(struct file
*file
, void __user
*arg
)
4348 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4349 struct btrfs_ioctl_scrub_args
*sa
;
4352 if (!capable(CAP_SYS_ADMIN
))
4355 sa
= memdup_user(arg
, sizeof(*sa
));
4359 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
)) {
4360 ret
= mnt_want_write_file(file
);
4365 ret
= btrfs_scrub_dev(root
->fs_info
, sa
->devid
, sa
->start
, sa
->end
,
4366 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
,
4369 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4372 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
))
4373 mnt_drop_write_file(file
);
4379 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
4381 if (!capable(CAP_SYS_ADMIN
))
4384 return btrfs_scrub_cancel(root
->fs_info
);
4387 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
4390 struct btrfs_ioctl_scrub_args
*sa
;
4393 if (!capable(CAP_SYS_ADMIN
))
4396 sa
= memdup_user(arg
, sizeof(*sa
));
4400 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
4402 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4409 static long btrfs_ioctl_get_dev_stats(struct btrfs_root
*root
,
4412 struct btrfs_ioctl_get_dev_stats
*sa
;
4415 sa
= memdup_user(arg
, sizeof(*sa
));
4419 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
4424 ret
= btrfs_get_dev_stats(root
, sa
);
4426 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4433 static long btrfs_ioctl_dev_replace(struct btrfs_root
*root
, void __user
*arg
)
4435 struct btrfs_ioctl_dev_replace_args
*p
;
4438 if (!capable(CAP_SYS_ADMIN
))
4441 p
= memdup_user(arg
, sizeof(*p
));
4446 case BTRFS_IOCTL_DEV_REPLACE_CMD_START
:
4447 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
) {
4452 &root
->fs_info
->mutually_exclusive_operation_running
,
4454 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
4456 ret
= btrfs_dev_replace_by_ioctl(root
, p
);
4458 &root
->fs_info
->mutually_exclusive_operation_running
,
4462 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
:
4463 btrfs_dev_replace_status(root
->fs_info
, p
);
4466 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
:
4467 ret
= btrfs_dev_replace_cancel(root
->fs_info
, p
);
4474 if (copy_to_user(arg
, p
, sizeof(*p
)))
4481 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
4487 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
4488 struct inode_fs_paths
*ipath
= NULL
;
4489 struct btrfs_path
*path
;
4491 if (!capable(CAP_DAC_READ_SEARCH
))
4494 path
= btrfs_alloc_path();
4500 ipa
= memdup_user(arg
, sizeof(*ipa
));
4507 size
= min_t(u32
, ipa
->size
, 4096);
4508 ipath
= init_ipath(size
, root
, path
);
4509 if (IS_ERR(ipath
)) {
4510 ret
= PTR_ERR(ipath
);
4515 ret
= paths_from_inode(ipa
->inum
, ipath
);
4519 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
4520 rel_ptr
= ipath
->fspath
->val
[i
] -
4521 (u64
)(unsigned long)ipath
->fspath
->val
;
4522 ipath
->fspath
->val
[i
] = rel_ptr
;
4525 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
4526 (void *)(unsigned long)ipath
->fspath
, size
);
4533 btrfs_free_path(path
);
4540 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
4542 struct btrfs_data_container
*inodes
= ctx
;
4543 const size_t c
= 3 * sizeof(u64
);
4545 if (inodes
->bytes_left
>= c
) {
4546 inodes
->bytes_left
-= c
;
4547 inodes
->val
[inodes
->elem_cnt
] = inum
;
4548 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
4549 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
4550 inodes
->elem_cnt
+= 3;
4552 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
4553 inodes
->bytes_left
= 0;
4554 inodes
->elem_missed
+= 3;
4560 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
4565 struct btrfs_ioctl_logical_ino_args
*loi
;
4566 struct btrfs_data_container
*inodes
= NULL
;
4567 struct btrfs_path
*path
= NULL
;
4569 if (!capable(CAP_SYS_ADMIN
))
4572 loi
= memdup_user(arg
, sizeof(*loi
));
4574 return PTR_ERR(loi
);
4576 path
= btrfs_alloc_path();
4582 size
= min_t(u32
, loi
->size
, SZ_64K
);
4583 inodes
= init_data_container(size
);
4584 if (IS_ERR(inodes
)) {
4585 ret
= PTR_ERR(inodes
);
4590 ret
= iterate_inodes_from_logical(loi
->logical
, root
->fs_info
, path
,
4591 build_ino_list
, inodes
);
4597 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
4598 (void *)(unsigned long)inodes
, size
);
4603 btrfs_free_path(path
);
4610 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
4611 struct btrfs_ioctl_balance_args
*bargs
)
4613 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
4615 bargs
->flags
= bctl
->flags
;
4617 if (atomic_read(&fs_info
->balance_running
))
4618 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
4619 if (atomic_read(&fs_info
->balance_pause_req
))
4620 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
4621 if (atomic_read(&fs_info
->balance_cancel_req
))
4622 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
4624 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
4625 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
4626 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
4629 spin_lock(&fs_info
->balance_lock
);
4630 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
4631 spin_unlock(&fs_info
->balance_lock
);
4633 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
4637 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
4639 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4640 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4641 struct btrfs_ioctl_balance_args
*bargs
;
4642 struct btrfs_balance_control
*bctl
;
4643 bool need_unlock
; /* for mut. excl. ops lock */
4646 if (!capable(CAP_SYS_ADMIN
))
4649 ret
= mnt_want_write_file(file
);
4654 if (!atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1)) {
4655 mutex_lock(&fs_info
->volume_mutex
);
4656 mutex_lock(&fs_info
->balance_mutex
);
4662 * mut. excl. ops lock is locked. Three possibilities:
4663 * (1) some other op is running
4664 * (2) balance is running
4665 * (3) balance is paused -- special case (think resume)
4667 mutex_lock(&fs_info
->balance_mutex
);
4668 if (fs_info
->balance_ctl
) {
4669 /* this is either (2) or (3) */
4670 if (!atomic_read(&fs_info
->balance_running
)) {
4671 mutex_unlock(&fs_info
->balance_mutex
);
4672 if (!mutex_trylock(&fs_info
->volume_mutex
))
4674 mutex_lock(&fs_info
->balance_mutex
);
4676 if (fs_info
->balance_ctl
&&
4677 !atomic_read(&fs_info
->balance_running
)) {
4679 need_unlock
= false;
4683 mutex_unlock(&fs_info
->balance_mutex
);
4684 mutex_unlock(&fs_info
->volume_mutex
);
4688 mutex_unlock(&fs_info
->balance_mutex
);
4694 mutex_unlock(&fs_info
->balance_mutex
);
4695 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
4700 BUG_ON(!atomic_read(&fs_info
->mutually_exclusive_operation_running
));
4703 bargs
= memdup_user(arg
, sizeof(*bargs
));
4704 if (IS_ERR(bargs
)) {
4705 ret
= PTR_ERR(bargs
);
4709 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
4710 if (!fs_info
->balance_ctl
) {
4715 bctl
= fs_info
->balance_ctl
;
4716 spin_lock(&fs_info
->balance_lock
);
4717 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
4718 spin_unlock(&fs_info
->balance_lock
);
4726 if (fs_info
->balance_ctl
) {
4731 bctl
= kzalloc(sizeof(*bctl
), GFP_KERNEL
);
4737 bctl
->fs_info
= fs_info
;
4739 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
4740 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
4741 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
4743 bctl
->flags
= bargs
->flags
;
4745 /* balance everything - no filters */
4746 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
4749 if (bctl
->flags
& ~(BTRFS_BALANCE_ARGS_MASK
| BTRFS_BALANCE_TYPE_MASK
)) {
4756 * Ownership of bctl and mutually_exclusive_operation_running
4757 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4758 * or, if restriper was paused all the way until unmount, in
4759 * free_fs_info. mutually_exclusive_operation_running is
4760 * cleared in __cancel_balance.
4762 need_unlock
= false;
4764 ret
= btrfs_balance(bctl
, bargs
);
4768 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4777 mutex_unlock(&fs_info
->balance_mutex
);
4778 mutex_unlock(&fs_info
->volume_mutex
);
4780 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
4782 mnt_drop_write_file(file
);
4786 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
4788 if (!capable(CAP_SYS_ADMIN
))
4792 case BTRFS_BALANCE_CTL_PAUSE
:
4793 return btrfs_pause_balance(root
->fs_info
);
4794 case BTRFS_BALANCE_CTL_CANCEL
:
4795 return btrfs_cancel_balance(root
->fs_info
);
4801 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
4804 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4805 struct btrfs_ioctl_balance_args
*bargs
;
4808 if (!capable(CAP_SYS_ADMIN
))
4811 mutex_lock(&fs_info
->balance_mutex
);
4812 if (!fs_info
->balance_ctl
) {
4817 bargs
= kzalloc(sizeof(*bargs
), GFP_KERNEL
);
4823 update_ioctl_balance_args(fs_info
, 1, bargs
);
4825 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4830 mutex_unlock(&fs_info
->balance_mutex
);
4834 static long btrfs_ioctl_quota_ctl(struct file
*file
, void __user
*arg
)
4836 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4837 struct btrfs_ioctl_quota_ctl_args
*sa
;
4838 struct btrfs_trans_handle
*trans
= NULL
;
4842 if (!capable(CAP_SYS_ADMIN
))
4845 ret
= mnt_want_write_file(file
);
4849 sa
= memdup_user(arg
, sizeof(*sa
));
4855 down_write(&root
->fs_info
->subvol_sem
);
4856 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 2);
4857 if (IS_ERR(trans
)) {
4858 ret
= PTR_ERR(trans
);
4863 case BTRFS_QUOTA_CTL_ENABLE
:
4864 ret
= btrfs_quota_enable(trans
, root
->fs_info
);
4866 case BTRFS_QUOTA_CTL_DISABLE
:
4867 ret
= btrfs_quota_disable(trans
, root
->fs_info
);
4874 err
= btrfs_commit_transaction(trans
, root
->fs_info
->tree_root
);
4879 up_write(&root
->fs_info
->subvol_sem
);
4881 mnt_drop_write_file(file
);
4885 static long btrfs_ioctl_qgroup_assign(struct file
*file
, void __user
*arg
)
4887 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4888 struct btrfs_ioctl_qgroup_assign_args
*sa
;
4889 struct btrfs_trans_handle
*trans
;
4893 if (!capable(CAP_SYS_ADMIN
))
4896 ret
= mnt_want_write_file(file
);
4900 sa
= memdup_user(arg
, sizeof(*sa
));
4906 trans
= btrfs_join_transaction(root
);
4907 if (IS_ERR(trans
)) {
4908 ret
= PTR_ERR(trans
);
4912 /* FIXME: check if the IDs really exist */
4914 ret
= btrfs_add_qgroup_relation(trans
, root
->fs_info
,
4917 ret
= btrfs_del_qgroup_relation(trans
, root
->fs_info
,
4921 /* update qgroup status and info */
4922 err
= btrfs_run_qgroups(trans
, root
->fs_info
);
4924 btrfs_handle_fs_error(root
->fs_info
, err
,
4925 "failed to update qgroup status and info");
4926 err
= btrfs_end_transaction(trans
, root
);
4933 mnt_drop_write_file(file
);
4937 static long btrfs_ioctl_qgroup_create(struct file
*file
, void __user
*arg
)
4939 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4940 struct btrfs_ioctl_qgroup_create_args
*sa
;
4941 struct btrfs_trans_handle
*trans
;
4945 if (!capable(CAP_SYS_ADMIN
))
4948 ret
= mnt_want_write_file(file
);
4952 sa
= memdup_user(arg
, sizeof(*sa
));
4958 if (!sa
->qgroupid
) {
4963 trans
= btrfs_join_transaction(root
);
4964 if (IS_ERR(trans
)) {
4965 ret
= PTR_ERR(trans
);
4969 /* FIXME: check if the IDs really exist */
4971 ret
= btrfs_create_qgroup(trans
, root
->fs_info
, sa
->qgroupid
);
4973 ret
= btrfs_remove_qgroup(trans
, root
->fs_info
, sa
->qgroupid
);
4976 err
= btrfs_end_transaction(trans
, root
);
4983 mnt_drop_write_file(file
);
4987 static long btrfs_ioctl_qgroup_limit(struct file
*file
, void __user
*arg
)
4989 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4990 struct btrfs_ioctl_qgroup_limit_args
*sa
;
4991 struct btrfs_trans_handle
*trans
;
4996 if (!capable(CAP_SYS_ADMIN
))
4999 ret
= mnt_want_write_file(file
);
5003 sa
= memdup_user(arg
, sizeof(*sa
));
5009 trans
= btrfs_join_transaction(root
);
5010 if (IS_ERR(trans
)) {
5011 ret
= PTR_ERR(trans
);
5015 qgroupid
= sa
->qgroupid
;
5017 /* take the current subvol as qgroup */
5018 qgroupid
= root
->root_key
.objectid
;
5021 /* FIXME: check if the IDs really exist */
5022 ret
= btrfs_limit_qgroup(trans
, root
->fs_info
, qgroupid
, &sa
->lim
);
5024 err
= btrfs_end_transaction(trans
, root
);
5031 mnt_drop_write_file(file
);
5035 static long btrfs_ioctl_quota_rescan(struct file
*file
, void __user
*arg
)
5037 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5038 struct btrfs_ioctl_quota_rescan_args
*qsa
;
5041 if (!capable(CAP_SYS_ADMIN
))
5044 ret
= mnt_want_write_file(file
);
5048 qsa
= memdup_user(arg
, sizeof(*qsa
));
5059 ret
= btrfs_qgroup_rescan(root
->fs_info
);
5064 mnt_drop_write_file(file
);
5068 static long btrfs_ioctl_quota_rescan_status(struct file
*file
, void __user
*arg
)
5070 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5071 struct btrfs_ioctl_quota_rescan_args
*qsa
;
5074 if (!capable(CAP_SYS_ADMIN
))
5077 qsa
= kzalloc(sizeof(*qsa
), GFP_KERNEL
);
5081 if (root
->fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
5083 qsa
->progress
= root
->fs_info
->qgroup_rescan_progress
.objectid
;
5086 if (copy_to_user(arg
, qsa
, sizeof(*qsa
)))
5093 static long btrfs_ioctl_quota_rescan_wait(struct file
*file
, void __user
*arg
)
5095 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5097 if (!capable(CAP_SYS_ADMIN
))
5100 return btrfs_qgroup_wait_for_completion(root
->fs_info
, true);
5103 static long _btrfs_ioctl_set_received_subvol(struct file
*file
,
5104 struct btrfs_ioctl_received_subvol_args
*sa
)
5106 struct inode
*inode
= file_inode(file
);
5107 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5108 struct btrfs_root_item
*root_item
= &root
->root_item
;
5109 struct btrfs_trans_handle
*trans
;
5110 struct timespec ct
= current_time(inode
);
5112 int received_uuid_changed
;
5114 if (!inode_owner_or_capable(inode
))
5117 ret
= mnt_want_write_file(file
);
5121 down_write(&root
->fs_info
->subvol_sem
);
5123 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
5128 if (btrfs_root_readonly(root
)) {
5135 * 2 - uuid items (received uuid + subvol uuid)
5137 trans
= btrfs_start_transaction(root
, 3);
5138 if (IS_ERR(trans
)) {
5139 ret
= PTR_ERR(trans
);
5144 sa
->rtransid
= trans
->transid
;
5145 sa
->rtime
.sec
= ct
.tv_sec
;
5146 sa
->rtime
.nsec
= ct
.tv_nsec
;
5148 received_uuid_changed
= memcmp(root_item
->received_uuid
, sa
->uuid
,
5150 if (received_uuid_changed
&&
5151 !btrfs_is_empty_uuid(root_item
->received_uuid
))
5152 btrfs_uuid_tree_rem(trans
, root
->fs_info
,
5153 root_item
->received_uuid
,
5154 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5155 root
->root_key
.objectid
);
5156 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
5157 btrfs_set_root_stransid(root_item
, sa
->stransid
);
5158 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
5159 btrfs_set_stack_timespec_sec(&root_item
->stime
, sa
->stime
.sec
);
5160 btrfs_set_stack_timespec_nsec(&root_item
->stime
, sa
->stime
.nsec
);
5161 btrfs_set_stack_timespec_sec(&root_item
->rtime
, sa
->rtime
.sec
);
5162 btrfs_set_stack_timespec_nsec(&root_item
->rtime
, sa
->rtime
.nsec
);
5164 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
5165 &root
->root_key
, &root
->root_item
);
5167 btrfs_end_transaction(trans
, root
);
5170 if (received_uuid_changed
&& !btrfs_is_empty_uuid(sa
->uuid
)) {
5171 ret
= btrfs_uuid_tree_add(trans
, root
->fs_info
, sa
->uuid
,
5172 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5173 root
->root_key
.objectid
);
5174 if (ret
< 0 && ret
!= -EEXIST
) {
5175 btrfs_abort_transaction(trans
, ret
);
5179 ret
= btrfs_commit_transaction(trans
, root
);
5181 btrfs_abort_transaction(trans
, ret
);
5186 up_write(&root
->fs_info
->subvol_sem
);
5187 mnt_drop_write_file(file
);
5192 static long btrfs_ioctl_set_received_subvol_32(struct file
*file
,
5195 struct btrfs_ioctl_received_subvol_args_32
*args32
= NULL
;
5196 struct btrfs_ioctl_received_subvol_args
*args64
= NULL
;
5199 args32
= memdup_user(arg
, sizeof(*args32
));
5201 return PTR_ERR(args32
);
5203 args64
= kmalloc(sizeof(*args64
), GFP_KERNEL
);
5209 memcpy(args64
->uuid
, args32
->uuid
, BTRFS_UUID_SIZE
);
5210 args64
->stransid
= args32
->stransid
;
5211 args64
->rtransid
= args32
->rtransid
;
5212 args64
->stime
.sec
= args32
->stime
.sec
;
5213 args64
->stime
.nsec
= args32
->stime
.nsec
;
5214 args64
->rtime
.sec
= args32
->rtime
.sec
;
5215 args64
->rtime
.nsec
= args32
->rtime
.nsec
;
5216 args64
->flags
= args32
->flags
;
5218 ret
= _btrfs_ioctl_set_received_subvol(file
, args64
);
5222 memcpy(args32
->uuid
, args64
->uuid
, BTRFS_UUID_SIZE
);
5223 args32
->stransid
= args64
->stransid
;
5224 args32
->rtransid
= args64
->rtransid
;
5225 args32
->stime
.sec
= args64
->stime
.sec
;
5226 args32
->stime
.nsec
= args64
->stime
.nsec
;
5227 args32
->rtime
.sec
= args64
->rtime
.sec
;
5228 args32
->rtime
.nsec
= args64
->rtime
.nsec
;
5229 args32
->flags
= args64
->flags
;
5231 ret
= copy_to_user(arg
, args32
, sizeof(*args32
));
5242 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
5245 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
5248 sa
= memdup_user(arg
, sizeof(*sa
));
5252 ret
= _btrfs_ioctl_set_received_subvol(file
, sa
);
5257 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
5266 static int btrfs_ioctl_get_fslabel(struct file
*file
, void __user
*arg
)
5268 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5271 char label
[BTRFS_LABEL_SIZE
];
5273 spin_lock(&root
->fs_info
->super_lock
);
5274 memcpy(label
, root
->fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
5275 spin_unlock(&root
->fs_info
->super_lock
);
5277 len
= strnlen(label
, BTRFS_LABEL_SIZE
);
5279 if (len
== BTRFS_LABEL_SIZE
) {
5280 btrfs_warn(root
->fs_info
,
5281 "label is too long, return the first %zu bytes", --len
);
5284 ret
= copy_to_user(arg
, label
, len
);
5286 return ret
? -EFAULT
: 0;
5289 static int btrfs_ioctl_set_fslabel(struct file
*file
, void __user
*arg
)
5291 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5292 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
5293 struct btrfs_trans_handle
*trans
;
5294 char label
[BTRFS_LABEL_SIZE
];
5297 if (!capable(CAP_SYS_ADMIN
))
5300 if (copy_from_user(label
, arg
, sizeof(label
)))
5303 if (strnlen(label
, BTRFS_LABEL_SIZE
) == BTRFS_LABEL_SIZE
) {
5304 btrfs_err(root
->fs_info
,
5305 "unable to set label with more than %d bytes",
5306 BTRFS_LABEL_SIZE
- 1);
5310 ret
= mnt_want_write_file(file
);
5314 trans
= btrfs_start_transaction(root
, 0);
5315 if (IS_ERR(trans
)) {
5316 ret
= PTR_ERR(trans
);
5320 spin_lock(&root
->fs_info
->super_lock
);
5321 strcpy(super_block
->label
, label
);
5322 spin_unlock(&root
->fs_info
->super_lock
);
5323 ret
= btrfs_commit_transaction(trans
, root
);
5326 mnt_drop_write_file(file
);
5330 #define INIT_FEATURE_FLAGS(suffix) \
5331 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5332 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5333 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5335 int btrfs_ioctl_get_supported_features(void __user
*arg
)
5337 static const struct btrfs_ioctl_feature_flags features
[3] = {
5338 INIT_FEATURE_FLAGS(SUPP
),
5339 INIT_FEATURE_FLAGS(SAFE_SET
),
5340 INIT_FEATURE_FLAGS(SAFE_CLEAR
)
5343 if (copy_to_user(arg
, &features
, sizeof(features
)))
5349 static int btrfs_ioctl_get_features(struct file
*file
, void __user
*arg
)
5351 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5352 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
5353 struct btrfs_ioctl_feature_flags features
;
5355 features
.compat_flags
= btrfs_super_compat_flags(super_block
);
5356 features
.compat_ro_flags
= btrfs_super_compat_ro_flags(super_block
);
5357 features
.incompat_flags
= btrfs_super_incompat_flags(super_block
);
5359 if (copy_to_user(arg
, &features
, sizeof(features
)))
5365 static int check_feature_bits(struct btrfs_root
*root
,
5366 enum btrfs_feature_set set
,
5367 u64 change_mask
, u64 flags
, u64 supported_flags
,
5368 u64 safe_set
, u64 safe_clear
)
5370 const char *type
= btrfs_feature_set_names
[set
];
5372 u64 disallowed
, unsupported
;
5373 u64 set_mask
= flags
& change_mask
;
5374 u64 clear_mask
= ~flags
& change_mask
;
5376 unsupported
= set_mask
& ~supported_flags
;
5378 names
= btrfs_printable_features(set
, unsupported
);
5380 btrfs_warn(root
->fs_info
,
5381 "this kernel does not support the %s feature bit%s",
5382 names
, strchr(names
, ',') ? "s" : "");
5385 btrfs_warn(root
->fs_info
,
5386 "this kernel does not support %s bits 0x%llx",
5391 disallowed
= set_mask
& ~safe_set
;
5393 names
= btrfs_printable_features(set
, disallowed
);
5395 btrfs_warn(root
->fs_info
,
5396 "can't set the %s feature bit%s while mounted",
5397 names
, strchr(names
, ',') ? "s" : "");
5400 btrfs_warn(root
->fs_info
,
5401 "can't set %s bits 0x%llx while mounted",
5406 disallowed
= clear_mask
& ~safe_clear
;
5408 names
= btrfs_printable_features(set
, disallowed
);
5410 btrfs_warn(root
->fs_info
,
5411 "can't clear the %s feature bit%s while mounted",
5412 names
, strchr(names
, ',') ? "s" : "");
5415 btrfs_warn(root
->fs_info
,
5416 "can't clear %s bits 0x%llx while mounted",
5424 #define check_feature(root, change_mask, flags, mask_base) \
5425 check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
5426 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5427 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5428 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5430 static int btrfs_ioctl_set_features(struct file
*file
, void __user
*arg
)
5432 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5433 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
5434 struct btrfs_ioctl_feature_flags flags
[2];
5435 struct btrfs_trans_handle
*trans
;
5439 if (!capable(CAP_SYS_ADMIN
))
5442 if (copy_from_user(flags
, arg
, sizeof(flags
)))
5446 if (!flags
[0].compat_flags
&& !flags
[0].compat_ro_flags
&&
5447 !flags
[0].incompat_flags
)
5450 ret
= check_feature(root
, flags
[0].compat_flags
,
5451 flags
[1].compat_flags
, COMPAT
);
5455 ret
= check_feature(root
, flags
[0].compat_ro_flags
,
5456 flags
[1].compat_ro_flags
, COMPAT_RO
);
5460 ret
= check_feature(root
, flags
[0].incompat_flags
,
5461 flags
[1].incompat_flags
, INCOMPAT
);
5465 ret
= mnt_want_write_file(file
);
5469 trans
= btrfs_start_transaction(root
, 0);
5470 if (IS_ERR(trans
)) {
5471 ret
= PTR_ERR(trans
);
5472 goto out_drop_write
;
5475 spin_lock(&root
->fs_info
->super_lock
);
5476 newflags
= btrfs_super_compat_flags(super_block
);
5477 newflags
|= flags
[0].compat_flags
& flags
[1].compat_flags
;
5478 newflags
&= ~(flags
[0].compat_flags
& ~flags
[1].compat_flags
);
5479 btrfs_set_super_compat_flags(super_block
, newflags
);
5481 newflags
= btrfs_super_compat_ro_flags(super_block
);
5482 newflags
|= flags
[0].compat_ro_flags
& flags
[1].compat_ro_flags
;
5483 newflags
&= ~(flags
[0].compat_ro_flags
& ~flags
[1].compat_ro_flags
);
5484 btrfs_set_super_compat_ro_flags(super_block
, newflags
);
5486 newflags
= btrfs_super_incompat_flags(super_block
);
5487 newflags
|= flags
[0].incompat_flags
& flags
[1].incompat_flags
;
5488 newflags
&= ~(flags
[0].incompat_flags
& ~flags
[1].incompat_flags
);
5489 btrfs_set_super_incompat_flags(super_block
, newflags
);
5490 spin_unlock(&root
->fs_info
->super_lock
);
5492 ret
= btrfs_commit_transaction(trans
, root
);
5494 mnt_drop_write_file(file
);
5499 long btrfs_ioctl(struct file
*file
, unsigned int
5500 cmd
, unsigned long arg
)
5502 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5503 void __user
*argp
= (void __user
*)arg
;
5506 case FS_IOC_GETFLAGS
:
5507 return btrfs_ioctl_getflags(file
, argp
);
5508 case FS_IOC_SETFLAGS
:
5509 return btrfs_ioctl_setflags(file
, argp
);
5510 case FS_IOC_GETVERSION
:
5511 return btrfs_ioctl_getversion(file
, argp
);
5513 return btrfs_ioctl_fitrim(file
, argp
);
5514 case BTRFS_IOC_SNAP_CREATE
:
5515 return btrfs_ioctl_snap_create(file
, argp
, 0);
5516 case BTRFS_IOC_SNAP_CREATE_V2
:
5517 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
5518 case BTRFS_IOC_SUBVOL_CREATE
:
5519 return btrfs_ioctl_snap_create(file
, argp
, 1);
5520 case BTRFS_IOC_SUBVOL_CREATE_V2
:
5521 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
5522 case BTRFS_IOC_SNAP_DESTROY
:
5523 return btrfs_ioctl_snap_destroy(file
, argp
);
5524 case BTRFS_IOC_SUBVOL_GETFLAGS
:
5525 return btrfs_ioctl_subvol_getflags(file
, argp
);
5526 case BTRFS_IOC_SUBVOL_SETFLAGS
:
5527 return btrfs_ioctl_subvol_setflags(file
, argp
);
5528 case BTRFS_IOC_DEFAULT_SUBVOL
:
5529 return btrfs_ioctl_default_subvol(file
, argp
);
5530 case BTRFS_IOC_DEFRAG
:
5531 return btrfs_ioctl_defrag(file
, NULL
);
5532 case BTRFS_IOC_DEFRAG_RANGE
:
5533 return btrfs_ioctl_defrag(file
, argp
);
5534 case BTRFS_IOC_RESIZE
:
5535 return btrfs_ioctl_resize(file
, argp
);
5536 case BTRFS_IOC_ADD_DEV
:
5537 return btrfs_ioctl_add_dev(root
, argp
);
5538 case BTRFS_IOC_RM_DEV
:
5539 return btrfs_ioctl_rm_dev(file
, argp
);
5540 case BTRFS_IOC_RM_DEV_V2
:
5541 return btrfs_ioctl_rm_dev_v2(file
, argp
);
5542 case BTRFS_IOC_FS_INFO
:
5543 return btrfs_ioctl_fs_info(root
, argp
);
5544 case BTRFS_IOC_DEV_INFO
:
5545 return btrfs_ioctl_dev_info(root
, argp
);
5546 case BTRFS_IOC_BALANCE
:
5547 return btrfs_ioctl_balance(file
, NULL
);
5548 case BTRFS_IOC_TRANS_START
:
5549 return btrfs_ioctl_trans_start(file
);
5550 case BTRFS_IOC_TRANS_END
:
5551 return btrfs_ioctl_trans_end(file
);
5552 case BTRFS_IOC_TREE_SEARCH
:
5553 return btrfs_ioctl_tree_search(file
, argp
);
5554 case BTRFS_IOC_TREE_SEARCH_V2
:
5555 return btrfs_ioctl_tree_search_v2(file
, argp
);
5556 case BTRFS_IOC_INO_LOOKUP
:
5557 return btrfs_ioctl_ino_lookup(file
, argp
);
5558 case BTRFS_IOC_INO_PATHS
:
5559 return btrfs_ioctl_ino_to_path(root
, argp
);
5560 case BTRFS_IOC_LOGICAL_INO
:
5561 return btrfs_ioctl_logical_to_ino(root
, argp
);
5562 case BTRFS_IOC_SPACE_INFO
:
5563 return btrfs_ioctl_space_info(root
, argp
);
5564 case BTRFS_IOC_SYNC
: {
5567 ret
= btrfs_start_delalloc_roots(root
->fs_info
, 0, -1);
5570 ret
= btrfs_sync_fs(file_inode(file
)->i_sb
, 1);
5572 * The transaction thread may want to do more work,
5573 * namely it pokes the cleaner kthread that will start
5574 * processing uncleaned subvols.
5576 wake_up_process(root
->fs_info
->transaction_kthread
);
5579 case BTRFS_IOC_START_SYNC
:
5580 return btrfs_ioctl_start_sync(root
, argp
);
5581 case BTRFS_IOC_WAIT_SYNC
:
5582 return btrfs_ioctl_wait_sync(root
, argp
);
5583 case BTRFS_IOC_SCRUB
:
5584 return btrfs_ioctl_scrub(file
, argp
);
5585 case BTRFS_IOC_SCRUB_CANCEL
:
5586 return btrfs_ioctl_scrub_cancel(root
, argp
);
5587 case BTRFS_IOC_SCRUB_PROGRESS
:
5588 return btrfs_ioctl_scrub_progress(root
, argp
);
5589 case BTRFS_IOC_BALANCE_V2
:
5590 return btrfs_ioctl_balance(file
, argp
);
5591 case BTRFS_IOC_BALANCE_CTL
:
5592 return btrfs_ioctl_balance_ctl(root
, arg
);
5593 case BTRFS_IOC_BALANCE_PROGRESS
:
5594 return btrfs_ioctl_balance_progress(root
, argp
);
5595 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
5596 return btrfs_ioctl_set_received_subvol(file
, argp
);
5598 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32
:
5599 return btrfs_ioctl_set_received_subvol_32(file
, argp
);
5601 case BTRFS_IOC_SEND
:
5602 return btrfs_ioctl_send(file
, argp
);
5603 case BTRFS_IOC_GET_DEV_STATS
:
5604 return btrfs_ioctl_get_dev_stats(root
, argp
);
5605 case BTRFS_IOC_QUOTA_CTL
:
5606 return btrfs_ioctl_quota_ctl(file
, argp
);
5607 case BTRFS_IOC_QGROUP_ASSIGN
:
5608 return btrfs_ioctl_qgroup_assign(file
, argp
);
5609 case BTRFS_IOC_QGROUP_CREATE
:
5610 return btrfs_ioctl_qgroup_create(file
, argp
);
5611 case BTRFS_IOC_QGROUP_LIMIT
:
5612 return btrfs_ioctl_qgroup_limit(file
, argp
);
5613 case BTRFS_IOC_QUOTA_RESCAN
:
5614 return btrfs_ioctl_quota_rescan(file
, argp
);
5615 case BTRFS_IOC_QUOTA_RESCAN_STATUS
:
5616 return btrfs_ioctl_quota_rescan_status(file
, argp
);
5617 case BTRFS_IOC_QUOTA_RESCAN_WAIT
:
5618 return btrfs_ioctl_quota_rescan_wait(file
, argp
);
5619 case BTRFS_IOC_DEV_REPLACE
:
5620 return btrfs_ioctl_dev_replace(root
, argp
);
5621 case BTRFS_IOC_GET_FSLABEL
:
5622 return btrfs_ioctl_get_fslabel(file
, argp
);
5623 case BTRFS_IOC_SET_FSLABEL
:
5624 return btrfs_ioctl_set_fslabel(file
, argp
);
5625 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
5626 return btrfs_ioctl_get_supported_features(argp
);
5627 case BTRFS_IOC_GET_FEATURES
:
5628 return btrfs_ioctl_get_features(file
, argp
);
5629 case BTRFS_IOC_SET_FEATURES
:
5630 return btrfs_ioctl_set_features(file
, argp
);
5636 #ifdef CONFIG_COMPAT
5637 long btrfs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
5640 case FS_IOC32_GETFLAGS
:
5641 cmd
= FS_IOC_GETFLAGS
;
5643 case FS_IOC32_SETFLAGS
:
5644 cmd
= FS_IOC_SETFLAGS
;
5646 case FS_IOC32_GETVERSION
:
5647 cmd
= FS_IOC_GETVERSION
;
5650 return -ENOIOCTLCMD
;
5653 return btrfs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));