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/statfs.h>
37 #include <linux/compat.h>
38 #include <linux/bit_spinlock.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/vmalloc.h>
42 #include <linux/slab.h>
43 #include <linux/blkdev.h>
44 #include <linux/uuid.h>
45 #include <linux/btrfs.h>
46 #include <linux/uaccess.h>
49 #include "transaction.h"
50 #include "btrfs_inode.h"
51 #include "print-tree.h"
54 #include "inode-map.h"
56 #include "rcu-string.h"
58 #include "dev-replace.h"
64 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
65 * structures are incorrect, as the timespec structure from userspace
66 * is 4 bytes too small. We define these alternatives here to teach
67 * the kernel about the 32-bit struct packing.
69 struct btrfs_ioctl_timespec_32
{
72 } __attribute__ ((__packed__
));
74 struct btrfs_ioctl_received_subvol_args_32
{
75 char uuid
[BTRFS_UUID_SIZE
]; /* in */
76 __u64 stransid
; /* in */
77 __u64 rtransid
; /* out */
78 struct btrfs_ioctl_timespec_32 stime
; /* in */
79 struct btrfs_ioctl_timespec_32 rtime
; /* out */
81 __u64 reserved
[16]; /* in */
82 } __attribute__ ((__packed__
));
84 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
85 struct btrfs_ioctl_received_subvol_args_32)
89 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
90 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
,
93 /* Mask out flags that are inappropriate for the given type of inode. */
94 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
98 else if (S_ISREG(mode
))
99 return flags
& ~FS_DIRSYNC_FL
;
101 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
105 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
107 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
109 unsigned int iflags
= 0;
111 if (flags
& BTRFS_INODE_SYNC
)
112 iflags
|= FS_SYNC_FL
;
113 if (flags
& BTRFS_INODE_IMMUTABLE
)
114 iflags
|= FS_IMMUTABLE_FL
;
115 if (flags
& BTRFS_INODE_APPEND
)
116 iflags
|= FS_APPEND_FL
;
117 if (flags
& BTRFS_INODE_NODUMP
)
118 iflags
|= FS_NODUMP_FL
;
119 if (flags
& BTRFS_INODE_NOATIME
)
120 iflags
|= FS_NOATIME_FL
;
121 if (flags
& BTRFS_INODE_DIRSYNC
)
122 iflags
|= FS_DIRSYNC_FL
;
123 if (flags
& BTRFS_INODE_NODATACOW
)
124 iflags
|= FS_NOCOW_FL
;
126 if ((flags
& BTRFS_INODE_COMPRESS
) && !(flags
& BTRFS_INODE_NOCOMPRESS
))
127 iflags
|= FS_COMPR_FL
;
128 else if (flags
& BTRFS_INODE_NOCOMPRESS
)
129 iflags
|= FS_NOCOMP_FL
;
135 * Update inode->i_flags based on the btrfs internal flags.
137 void btrfs_update_iflags(struct inode
*inode
)
139 struct btrfs_inode
*ip
= BTRFS_I(inode
);
140 unsigned int new_fl
= 0;
142 if (ip
->flags
& BTRFS_INODE_SYNC
)
144 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
145 new_fl
|= S_IMMUTABLE
;
146 if (ip
->flags
& BTRFS_INODE_APPEND
)
148 if (ip
->flags
& BTRFS_INODE_NOATIME
)
150 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
153 set_mask_bits(&inode
->i_flags
,
154 S_SYNC
| S_APPEND
| S_IMMUTABLE
| S_NOATIME
| S_DIRSYNC
,
159 * Inherit flags from the parent inode.
161 * Currently only the compression flags and the cow flags are inherited.
163 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
170 flags
= BTRFS_I(dir
)->flags
;
172 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
173 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
174 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
175 } else if (flags
& BTRFS_INODE_COMPRESS
) {
176 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
177 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
180 if (flags
& BTRFS_INODE_NODATACOW
) {
181 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
182 if (S_ISREG(inode
->i_mode
))
183 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
186 btrfs_update_iflags(inode
);
189 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
191 struct btrfs_inode
*ip
= BTRFS_I(file_inode(file
));
192 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
194 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
199 static int check_flags(unsigned int flags
)
201 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
202 FS_NOATIME_FL
| FS_NODUMP_FL
| \
203 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
204 FS_NOCOMP_FL
| FS_COMPR_FL
|
208 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
214 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
216 struct inode
*inode
= file_inode(file
);
217 struct btrfs_inode
*ip
= BTRFS_I(inode
);
218 struct btrfs_root
*root
= ip
->root
;
219 struct btrfs_trans_handle
*trans
;
220 unsigned int flags
, oldflags
;
223 unsigned int i_oldflags
;
226 if (!inode_owner_or_capable(inode
))
229 if (btrfs_root_readonly(root
))
232 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
235 ret
= check_flags(flags
);
239 ret
= mnt_want_write_file(file
);
243 mutex_lock(&inode
->i_mutex
);
245 ip_oldflags
= ip
->flags
;
246 i_oldflags
= inode
->i_flags
;
247 mode
= inode
->i_mode
;
249 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
250 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
251 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
252 if (!capable(CAP_LINUX_IMMUTABLE
)) {
258 if (flags
& FS_SYNC_FL
)
259 ip
->flags
|= BTRFS_INODE_SYNC
;
261 ip
->flags
&= ~BTRFS_INODE_SYNC
;
262 if (flags
& FS_IMMUTABLE_FL
)
263 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
265 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
266 if (flags
& FS_APPEND_FL
)
267 ip
->flags
|= BTRFS_INODE_APPEND
;
269 ip
->flags
&= ~BTRFS_INODE_APPEND
;
270 if (flags
& FS_NODUMP_FL
)
271 ip
->flags
|= BTRFS_INODE_NODUMP
;
273 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
274 if (flags
& FS_NOATIME_FL
)
275 ip
->flags
|= BTRFS_INODE_NOATIME
;
277 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
278 if (flags
& FS_DIRSYNC_FL
)
279 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
281 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
282 if (flags
& FS_NOCOW_FL
) {
285 * It's safe to turn csums off here, no extents exist.
286 * Otherwise we want the flag to reflect the real COW
287 * status of the file and will not set it.
289 if (inode
->i_size
== 0)
290 ip
->flags
|= BTRFS_INODE_NODATACOW
291 | BTRFS_INODE_NODATASUM
;
293 ip
->flags
|= BTRFS_INODE_NODATACOW
;
297 * Revert back under same assuptions as above
300 if (inode
->i_size
== 0)
301 ip
->flags
&= ~(BTRFS_INODE_NODATACOW
302 | BTRFS_INODE_NODATASUM
);
304 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
309 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
310 * flag may be changed automatically if compression code won't make
313 if (flags
& FS_NOCOMP_FL
) {
314 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
315 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
317 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
318 if (ret
&& ret
!= -ENODATA
)
320 } else if (flags
& FS_COMPR_FL
) {
323 ip
->flags
|= BTRFS_INODE_COMPRESS
;
324 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
326 if (root
->fs_info
->compress_type
== BTRFS_COMPRESS_LZO
)
330 ret
= btrfs_set_prop(inode
, "btrfs.compression",
331 comp
, strlen(comp
), 0);
336 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
337 if (ret
&& ret
!= -ENODATA
)
339 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
342 trans
= btrfs_start_transaction(root
, 1);
344 ret
= PTR_ERR(trans
);
348 btrfs_update_iflags(inode
);
349 inode_inc_iversion(inode
);
350 inode
->i_ctime
= CURRENT_TIME
;
351 ret
= btrfs_update_inode(trans
, root
, inode
);
353 btrfs_end_transaction(trans
, root
);
356 ip
->flags
= ip_oldflags
;
357 inode
->i_flags
= i_oldflags
;
361 mutex_unlock(&inode
->i_mutex
);
362 mnt_drop_write_file(file
);
366 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
368 struct inode
*inode
= file_inode(file
);
370 return put_user(inode
->i_generation
, arg
);
373 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
375 struct btrfs_fs_info
*fs_info
= btrfs_sb(file_inode(file
)->i_sb
);
376 struct btrfs_device
*device
;
377 struct request_queue
*q
;
378 struct fstrim_range range
;
379 u64 minlen
= ULLONG_MAX
;
381 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
384 if (!capable(CAP_SYS_ADMIN
))
388 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
392 q
= bdev_get_queue(device
->bdev
);
393 if (blk_queue_discard(q
)) {
395 minlen
= min((u64
)q
->limits
.discard_granularity
,
403 if (copy_from_user(&range
, arg
, sizeof(range
)))
405 if (range
.start
> total_bytes
||
406 range
.len
< fs_info
->sb
->s_blocksize
)
409 range
.len
= min(range
.len
, total_bytes
- range
.start
);
410 range
.minlen
= max(range
.minlen
, minlen
);
411 ret
= btrfs_trim_fs(fs_info
->tree_root
, &range
);
415 if (copy_to_user(arg
, &range
, sizeof(range
)))
421 int btrfs_is_empty_uuid(u8
*uuid
)
425 for (i
= 0; i
< BTRFS_UUID_SIZE
; i
++) {
432 static noinline
int create_subvol(struct inode
*dir
,
433 struct dentry
*dentry
,
434 char *name
, int namelen
,
436 struct btrfs_qgroup_inherit
*inherit
)
438 struct btrfs_trans_handle
*trans
;
439 struct btrfs_key key
;
440 struct btrfs_root_item root_item
;
441 struct btrfs_inode_item
*inode_item
;
442 struct extent_buffer
*leaf
;
443 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
444 struct btrfs_root
*new_root
;
445 struct btrfs_block_rsv block_rsv
;
446 struct timespec cur_time
= CURRENT_TIME
;
451 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
456 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
461 * Don't create subvolume whose level is not zero. Or qgroup will be
462 * screwed up since it assume subvolme qgroup's level to be 0.
464 if (btrfs_qgroup_level(objectid
))
467 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
469 * The same as the snapshot creation, please see the comment
470 * of create_snapshot().
472 ret
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
473 8, &qgroup_reserved
, false);
477 trans
= btrfs_start_transaction(root
, 0);
479 ret
= PTR_ERR(trans
);
480 btrfs_subvolume_release_metadata(root
, &block_rsv
,
484 trans
->block_rsv
= &block_rsv
;
485 trans
->bytes_reserved
= block_rsv
.size
;
487 ret
= btrfs_qgroup_inherit(trans
, root
->fs_info
, 0, objectid
, inherit
);
491 leaf
= btrfs_alloc_tree_block(trans
, root
, 0, objectid
, NULL
, 0, 0, 0);
497 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
498 btrfs_set_header_bytenr(leaf
, leaf
->start
);
499 btrfs_set_header_generation(leaf
, trans
->transid
);
500 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
501 btrfs_set_header_owner(leaf
, objectid
);
503 write_extent_buffer(leaf
, root
->fs_info
->fsid
, btrfs_header_fsid(),
505 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
506 btrfs_header_chunk_tree_uuid(leaf
),
508 btrfs_mark_buffer_dirty(leaf
);
510 memset(&root_item
, 0, sizeof(root_item
));
512 inode_item
= &root_item
.inode
;
513 btrfs_set_stack_inode_generation(inode_item
, 1);
514 btrfs_set_stack_inode_size(inode_item
, 3);
515 btrfs_set_stack_inode_nlink(inode_item
, 1);
516 btrfs_set_stack_inode_nbytes(inode_item
, root
->nodesize
);
517 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
519 btrfs_set_root_flags(&root_item
, 0);
520 btrfs_set_root_limit(&root_item
, 0);
521 btrfs_set_stack_inode_flags(inode_item
, BTRFS_INODE_ROOT_ITEM_INIT
);
523 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
524 btrfs_set_root_generation(&root_item
, trans
->transid
);
525 btrfs_set_root_level(&root_item
, 0);
526 btrfs_set_root_refs(&root_item
, 1);
527 btrfs_set_root_used(&root_item
, leaf
->len
);
528 btrfs_set_root_last_snapshot(&root_item
, 0);
530 btrfs_set_root_generation_v2(&root_item
,
531 btrfs_root_generation(&root_item
));
532 uuid_le_gen(&new_uuid
);
533 memcpy(root_item
.uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
534 btrfs_set_stack_timespec_sec(&root_item
.otime
, cur_time
.tv_sec
);
535 btrfs_set_stack_timespec_nsec(&root_item
.otime
, cur_time
.tv_nsec
);
536 root_item
.ctime
= root_item
.otime
;
537 btrfs_set_root_ctransid(&root_item
, trans
->transid
);
538 btrfs_set_root_otransid(&root_item
, trans
->transid
);
540 btrfs_tree_unlock(leaf
);
541 free_extent_buffer(leaf
);
544 btrfs_set_root_dirid(&root_item
, new_dirid
);
546 key
.objectid
= objectid
;
548 key
.type
= BTRFS_ROOT_ITEM_KEY
;
549 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
554 key
.offset
= (u64
)-1;
555 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
556 if (IS_ERR(new_root
)) {
557 ret
= PTR_ERR(new_root
);
558 btrfs_abort_transaction(trans
, root
, ret
);
562 btrfs_record_root_in_trans(trans
, new_root
);
564 ret
= btrfs_create_subvol_root(trans
, new_root
, root
, new_dirid
);
566 /* We potentially lose an unused inode item here */
567 btrfs_abort_transaction(trans
, root
, ret
);
572 * insert the directory item
574 ret
= btrfs_set_inode_index(dir
, &index
);
576 btrfs_abort_transaction(trans
, root
, ret
);
580 ret
= btrfs_insert_dir_item(trans
, root
,
581 name
, namelen
, dir
, &key
,
582 BTRFS_FT_DIR
, index
);
584 btrfs_abort_transaction(trans
, root
, ret
);
588 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
589 ret
= btrfs_update_inode(trans
, root
, dir
);
592 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
593 objectid
, root
->root_key
.objectid
,
594 btrfs_ino(dir
), index
, name
, namelen
);
597 ret
= btrfs_uuid_tree_add(trans
, root
->fs_info
->uuid_root
,
598 root_item
.uuid
, BTRFS_UUID_KEY_SUBVOL
,
601 btrfs_abort_transaction(trans
, root
, ret
);
604 trans
->block_rsv
= NULL
;
605 trans
->bytes_reserved
= 0;
606 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
609 *async_transid
= trans
->transid
;
610 err
= btrfs_commit_transaction_async(trans
, root
, 1);
612 err
= btrfs_commit_transaction(trans
, root
);
614 err
= btrfs_commit_transaction(trans
, root
);
620 inode
= btrfs_lookup_dentry(dir
, dentry
);
622 return PTR_ERR(inode
);
623 d_instantiate(dentry
, inode
);
628 static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root
*root
)
634 prepare_to_wait(&root
->subv_writers
->wait
, &wait
,
635 TASK_UNINTERRUPTIBLE
);
637 writers
= percpu_counter_sum(&root
->subv_writers
->counter
);
641 finish_wait(&root
->subv_writers
->wait
, &wait
);
645 static int create_snapshot(struct btrfs_root
*root
, struct inode
*dir
,
646 struct dentry
*dentry
, char *name
, int namelen
,
647 u64
*async_transid
, bool readonly
,
648 struct btrfs_qgroup_inherit
*inherit
)
651 struct btrfs_pending_snapshot
*pending_snapshot
;
652 struct btrfs_trans_handle
*trans
;
655 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
658 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
659 if (!pending_snapshot
)
662 atomic_inc(&root
->will_be_snapshoted
);
663 smp_mb__after_atomic();
664 btrfs_wait_for_no_snapshoting_writes(root
);
666 ret
= btrfs_start_delalloc_inodes(root
, 0);
670 btrfs_wait_ordered_extents(root
, -1);
672 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
673 BTRFS_BLOCK_RSV_TEMP
);
675 * 1 - parent dir inode
678 * 2 - root ref/backref
679 * 1 - root of snapshot
682 ret
= btrfs_subvolume_reserve_metadata(BTRFS_I(dir
)->root
,
683 &pending_snapshot
->block_rsv
, 8,
684 &pending_snapshot
->qgroup_reserved
,
689 pending_snapshot
->dentry
= dentry
;
690 pending_snapshot
->root
= root
;
691 pending_snapshot
->readonly
= readonly
;
692 pending_snapshot
->dir
= dir
;
693 pending_snapshot
->inherit
= inherit
;
695 trans
= btrfs_start_transaction(root
, 0);
697 ret
= PTR_ERR(trans
);
701 spin_lock(&root
->fs_info
->trans_lock
);
702 list_add(&pending_snapshot
->list
,
703 &trans
->transaction
->pending_snapshots
);
704 spin_unlock(&root
->fs_info
->trans_lock
);
706 *async_transid
= trans
->transid
;
707 ret
= btrfs_commit_transaction_async(trans
,
708 root
->fs_info
->extent_root
, 1);
710 ret
= btrfs_commit_transaction(trans
, root
);
712 ret
= btrfs_commit_transaction(trans
,
713 root
->fs_info
->extent_root
);
718 ret
= pending_snapshot
->error
;
722 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
726 inode
= btrfs_lookup_dentry(d_inode(dentry
->d_parent
), dentry
);
728 ret
= PTR_ERR(inode
);
732 d_instantiate(dentry
, inode
);
735 btrfs_subvolume_release_metadata(BTRFS_I(dir
)->root
,
736 &pending_snapshot
->block_rsv
,
737 pending_snapshot
->qgroup_reserved
);
739 if (atomic_dec_and_test(&root
->will_be_snapshoted
))
740 wake_up_atomic_t(&root
->will_be_snapshoted
);
741 kfree(pending_snapshot
);
746 /* copy of may_delete in fs/namei.c()
747 * Check whether we can remove a link victim from directory dir, check
748 * whether the type of victim is right.
749 * 1. We can't do it if dir is read-only (done in permission())
750 * 2. We should have write and exec permissions on dir
751 * 3. We can't remove anything from append-only dir
752 * 4. We can't do anything with immutable dir (done in permission())
753 * 5. If the sticky bit on dir is set we should either
754 * a. be owner of dir, or
755 * b. be owner of victim, or
756 * c. have CAP_FOWNER capability
757 * 6. If the victim is append-only or immutable we can't do antyhing with
758 * links pointing to it.
759 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
760 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
761 * 9. We can't remove a root or mountpoint.
762 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
763 * nfs_async_unlink().
766 static int btrfs_may_delete(struct inode
*dir
, struct dentry
*victim
, int isdir
)
770 if (d_really_is_negative(victim
))
773 BUG_ON(d_inode(victim
->d_parent
) != dir
);
774 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
776 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
781 if (check_sticky(dir
, d_inode(victim
)) || IS_APPEND(d_inode(victim
)) ||
782 IS_IMMUTABLE(d_inode(victim
)) || IS_SWAPFILE(d_inode(victim
)))
785 if (!d_is_dir(victim
))
789 } else if (d_is_dir(victim
))
793 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
798 /* copy of may_create in fs/namei.c() */
799 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
801 if (d_really_is_positive(child
))
805 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
809 * Create a new subvolume below @parent. This is largely modeled after
810 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
811 * inside this filesystem so it's quite a bit simpler.
813 static noinline
int btrfs_mksubvol(struct path
*parent
,
814 char *name
, int namelen
,
815 struct btrfs_root
*snap_src
,
816 u64
*async_transid
, bool readonly
,
817 struct btrfs_qgroup_inherit
*inherit
)
819 struct inode
*dir
= d_inode(parent
->dentry
);
820 struct dentry
*dentry
;
823 error
= mutex_lock_killable_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
827 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
828 error
= PTR_ERR(dentry
);
833 if (d_really_is_positive(dentry
))
836 error
= btrfs_may_create(dir
, dentry
);
841 * even if this name doesn't exist, we may get hash collisions.
842 * check for them now when we can safely fail
844 error
= btrfs_check_dir_item_collision(BTRFS_I(dir
)->root
,
850 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
852 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
856 error
= create_snapshot(snap_src
, dir
, dentry
, name
, namelen
,
857 async_transid
, readonly
, inherit
);
859 error
= create_subvol(dir
, dentry
, name
, namelen
,
860 async_transid
, inherit
);
863 fsnotify_mkdir(dir
, dentry
);
865 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
869 mutex_unlock(&dir
->i_mutex
);
874 * When we're defragging a range, we don't want to kick it off again
875 * if it is really just waiting for delalloc to send it down.
876 * If we find a nice big extent or delalloc range for the bytes in the
877 * file you want to defrag, we return 0 to let you know to skip this
880 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, u32 thresh
)
882 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
883 struct extent_map
*em
= NULL
;
884 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
887 read_lock(&em_tree
->lock
);
888 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
889 read_unlock(&em_tree
->lock
);
892 end
= extent_map_end(em
);
894 if (end
- offset
> thresh
)
897 /* if we already have a nice delalloc here, just stop */
899 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
900 thresh
, EXTENT_DELALLOC
, 1);
907 * helper function to walk through a file and find extents
908 * newer than a specific transid, and smaller than thresh.
910 * This is used by the defragging code to find new and small
913 static int find_new_extents(struct btrfs_root
*root
,
914 struct inode
*inode
, u64 newer_than
,
915 u64
*off
, u32 thresh
)
917 struct btrfs_path
*path
;
918 struct btrfs_key min_key
;
919 struct extent_buffer
*leaf
;
920 struct btrfs_file_extent_item
*extent
;
923 u64 ino
= btrfs_ino(inode
);
925 path
= btrfs_alloc_path();
929 min_key
.objectid
= ino
;
930 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
931 min_key
.offset
= *off
;
934 ret
= btrfs_search_forward(root
, &min_key
, path
, newer_than
);
938 if (min_key
.objectid
!= ino
)
940 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
943 leaf
= path
->nodes
[0];
944 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
945 struct btrfs_file_extent_item
);
947 type
= btrfs_file_extent_type(leaf
, extent
);
948 if (type
== BTRFS_FILE_EXTENT_REG
&&
949 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
950 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
951 *off
= min_key
.offset
;
952 btrfs_free_path(path
);
957 if (path
->slots
[0] < btrfs_header_nritems(leaf
)) {
958 btrfs_item_key_to_cpu(leaf
, &min_key
, path
->slots
[0]);
962 if (min_key
.offset
== (u64
)-1)
966 btrfs_release_path(path
);
969 btrfs_free_path(path
);
973 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
975 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
976 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
977 struct extent_map
*em
;
978 u64 len
= PAGE_CACHE_SIZE
;
981 * hopefully we have this extent in the tree already, try without
982 * the full extent lock
984 read_lock(&em_tree
->lock
);
985 em
= lookup_extent_mapping(em_tree
, start
, len
);
986 read_unlock(&em_tree
->lock
);
989 struct extent_state
*cached
= NULL
;
990 u64 end
= start
+ len
- 1;
992 /* get the big lock and read metadata off disk */
993 lock_extent_bits(io_tree
, start
, end
, 0, &cached
);
994 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
995 unlock_extent_cached(io_tree
, start
, end
, &cached
, GFP_NOFS
);
1004 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
1006 struct extent_map
*next
;
1009 /* this is the last extent */
1010 if (em
->start
+ em
->len
>= i_size_read(inode
))
1013 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
1014 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
1016 else if ((em
->block_start
+ em
->block_len
== next
->block_start
) &&
1017 (em
->block_len
> 128 * 1024 && next
->block_len
> 128 * 1024))
1020 free_extent_map(next
);
1024 static int should_defrag_range(struct inode
*inode
, u64 start
, u32 thresh
,
1025 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
1028 struct extent_map
*em
;
1030 bool next_mergeable
= true;
1031 bool prev_mergeable
= true;
1034 * make sure that once we start defragging an extent, we keep on
1037 if (start
< *defrag_end
)
1042 em
= defrag_lookup_extent(inode
, start
);
1046 /* this will cover holes, and inline extents */
1047 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
1053 prev_mergeable
= false;
1055 next_mergeable
= defrag_check_next_extent(inode
, em
);
1057 * we hit a real extent, if it is big or the next extent is not a
1058 * real extent, don't bother defragging it
1060 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
1061 (em
->len
>= thresh
|| (!next_mergeable
&& !prev_mergeable
)))
1065 * last_len ends up being a counter of how many bytes we've defragged.
1066 * every time we choose not to defrag an extent, we reset *last_len
1067 * so that the next tiny extent will force a defrag.
1069 * The end result of this is that tiny extents before a single big
1070 * extent will force at least part of that big extent to be defragged.
1073 *defrag_end
= extent_map_end(em
);
1076 *skip
= extent_map_end(em
);
1080 free_extent_map(em
);
1085 * it doesn't do much good to defrag one or two pages
1086 * at a time. This pulls in a nice chunk of pages
1087 * to COW and defrag.
1089 * It also makes sure the delalloc code has enough
1090 * dirty data to avoid making new small extents as part
1093 * It's a good idea to start RA on this range
1094 * before calling this.
1096 static int cluster_pages_for_defrag(struct inode
*inode
,
1097 struct page
**pages
,
1098 unsigned long start_index
,
1099 unsigned long num_pages
)
1101 unsigned long file_end
;
1102 u64 isize
= i_size_read(inode
);
1109 struct btrfs_ordered_extent
*ordered
;
1110 struct extent_state
*cached_state
= NULL
;
1111 struct extent_io_tree
*tree
;
1112 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
1114 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1115 if (!isize
|| start_index
> file_end
)
1118 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
1120 ret
= btrfs_delalloc_reserve_space(inode
,
1121 start_index
<< PAGE_CACHE_SHIFT
,
1122 page_cnt
<< PAGE_CACHE_SHIFT
);
1126 tree
= &BTRFS_I(inode
)->io_tree
;
1128 /* step one, lock all the pages */
1129 for (i
= 0; i
< page_cnt
; i
++) {
1132 page
= find_or_create_page(inode
->i_mapping
,
1133 start_index
+ i
, mask
);
1137 page_start
= page_offset(page
);
1138 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1140 lock_extent_bits(tree
, page_start
, page_end
,
1142 ordered
= btrfs_lookup_ordered_extent(inode
,
1144 unlock_extent_cached(tree
, page_start
, page_end
,
1145 &cached_state
, GFP_NOFS
);
1150 btrfs_start_ordered_extent(inode
, ordered
, 1);
1151 btrfs_put_ordered_extent(ordered
);
1154 * we unlocked the page above, so we need check if
1155 * it was released or not.
1157 if (page
->mapping
!= inode
->i_mapping
) {
1159 page_cache_release(page
);
1164 if (!PageUptodate(page
)) {
1165 btrfs_readpage(NULL
, page
);
1167 if (!PageUptodate(page
)) {
1169 page_cache_release(page
);
1175 if (page
->mapping
!= inode
->i_mapping
) {
1177 page_cache_release(page
);
1187 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1191 * so now we have a nice long stream of locked
1192 * and up to date pages, lets wait on them
1194 for (i
= 0; i
< i_done
; i
++)
1195 wait_on_page_writeback(pages
[i
]);
1197 page_start
= page_offset(pages
[0]);
1198 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
1200 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1201 page_start
, page_end
- 1, 0, &cached_state
);
1202 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1203 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1204 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 0, 0,
1205 &cached_state
, GFP_NOFS
);
1207 if (i_done
!= page_cnt
) {
1208 spin_lock(&BTRFS_I(inode
)->lock
);
1209 BTRFS_I(inode
)->outstanding_extents
++;
1210 spin_unlock(&BTRFS_I(inode
)->lock
);
1211 btrfs_delalloc_release_space(inode
,
1212 start_index
<< PAGE_CACHE_SHIFT
,
1213 (page_cnt
- i_done
) << PAGE_CACHE_SHIFT
);
1217 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1218 &cached_state
, GFP_NOFS
);
1220 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1221 page_start
, page_end
- 1, &cached_state
,
1224 for (i
= 0; i
< i_done
; i
++) {
1225 clear_page_dirty_for_io(pages
[i
]);
1226 ClearPageChecked(pages
[i
]);
1227 set_page_extent_mapped(pages
[i
]);
1228 set_page_dirty(pages
[i
]);
1229 unlock_page(pages
[i
]);
1230 page_cache_release(pages
[i
]);
1234 for (i
= 0; i
< i_done
; i
++) {
1235 unlock_page(pages
[i
]);
1236 page_cache_release(pages
[i
]);
1238 btrfs_delalloc_release_space(inode
,
1239 start_index
<< PAGE_CACHE_SHIFT
,
1240 page_cnt
<< PAGE_CACHE_SHIFT
);
1245 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1246 struct btrfs_ioctl_defrag_range_args
*range
,
1247 u64 newer_than
, unsigned long max_to_defrag
)
1249 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1250 struct file_ra_state
*ra
= NULL
;
1251 unsigned long last_index
;
1252 u64 isize
= i_size_read(inode
);
1256 u64 newer_off
= range
->start
;
1258 unsigned long ra_index
= 0;
1260 int defrag_count
= 0;
1261 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1262 u32 extent_thresh
= range
->extent_thresh
;
1263 unsigned long max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1264 unsigned long cluster
= max_cluster
;
1265 u64 new_align
= ~((u64
)128 * 1024 - 1);
1266 struct page
**pages
= NULL
;
1271 if (range
->start
>= isize
)
1274 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1275 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1277 if (range
->compress_type
)
1278 compress_type
= range
->compress_type
;
1281 if (extent_thresh
== 0)
1282 extent_thresh
= 256 * 1024;
1285 * if we were not given a file, allocate a readahead
1289 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1292 file_ra_state_init(ra
, inode
->i_mapping
);
1297 pages
= kmalloc_array(max_cluster
, sizeof(struct page
*),
1304 /* find the last page to defrag */
1305 if (range
->start
+ range
->len
> range
->start
) {
1306 last_index
= min_t(u64
, isize
- 1,
1307 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1309 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1313 ret
= find_new_extents(root
, inode
, newer_than
,
1314 &newer_off
, 64 * 1024);
1316 range
->start
= newer_off
;
1318 * we always align our defrag to help keep
1319 * the extents in the file evenly spaced
1321 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1325 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1328 max_to_defrag
= last_index
- i
+ 1;
1331 * make writeback starts from i, so the defrag range can be
1332 * written sequentially.
1334 if (i
< inode
->i_mapping
->writeback_index
)
1335 inode
->i_mapping
->writeback_index
= i
;
1337 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1338 (i
< DIV_ROUND_UP(i_size_read(inode
), PAGE_CACHE_SIZE
))) {
1340 * make sure we stop running if someone unmounts
1343 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1346 if (btrfs_defrag_cancelled(root
->fs_info
)) {
1347 btrfs_debug(root
->fs_info
, "defrag_file cancelled");
1352 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1353 extent_thresh
, &last_len
, &skip
,
1354 &defrag_end
, range
->flags
&
1355 BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1358 * the should_defrag function tells us how much to skip
1359 * bump our counter by the suggested amount
1361 next
= DIV_ROUND_UP(skip
, PAGE_CACHE_SIZE
);
1362 i
= max(i
+ 1, next
);
1367 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1368 PAGE_CACHE_SHIFT
) - i
;
1369 cluster
= min(cluster
, max_cluster
);
1371 cluster
= max_cluster
;
1374 if (i
+ cluster
> ra_index
) {
1375 ra_index
= max(i
, ra_index
);
1376 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1378 ra_index
+= cluster
;
1381 mutex_lock(&inode
->i_mutex
);
1382 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1383 BTRFS_I(inode
)->force_compress
= compress_type
;
1384 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1386 mutex_unlock(&inode
->i_mutex
);
1390 defrag_count
+= ret
;
1391 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1392 mutex_unlock(&inode
->i_mutex
);
1395 if (newer_off
== (u64
)-1)
1401 newer_off
= max(newer_off
+ 1,
1402 (u64
)i
<< PAGE_CACHE_SHIFT
);
1404 ret
= find_new_extents(root
, inode
,
1405 newer_than
, &newer_off
,
1408 range
->start
= newer_off
;
1409 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1416 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1424 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
)) {
1425 filemap_flush(inode
->i_mapping
);
1426 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1427 &BTRFS_I(inode
)->runtime_flags
))
1428 filemap_flush(inode
->i_mapping
);
1431 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1432 /* the filemap_flush will queue IO into the worker threads, but
1433 * we have to make sure the IO is actually started and that
1434 * ordered extents get created before we return
1436 atomic_inc(&root
->fs_info
->async_submit_draining
);
1437 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1438 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1439 wait_event(root
->fs_info
->async_submit_wait
,
1440 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1441 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1443 atomic_dec(&root
->fs_info
->async_submit_draining
);
1446 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1447 btrfs_set_fs_incompat(root
->fs_info
, COMPRESS_LZO
);
1453 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1454 mutex_lock(&inode
->i_mutex
);
1455 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1456 mutex_unlock(&inode
->i_mutex
);
1464 static noinline
int btrfs_ioctl_resize(struct file
*file
,
1470 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
1471 struct btrfs_ioctl_vol_args
*vol_args
;
1472 struct btrfs_trans_handle
*trans
;
1473 struct btrfs_device
*device
= NULL
;
1476 char *devstr
= NULL
;
1480 if (!capable(CAP_SYS_ADMIN
))
1483 ret
= mnt_want_write_file(file
);
1487 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
1489 mnt_drop_write_file(file
);
1490 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
1493 mutex_lock(&root
->fs_info
->volume_mutex
);
1494 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1495 if (IS_ERR(vol_args
)) {
1496 ret
= PTR_ERR(vol_args
);
1500 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1502 sizestr
= vol_args
->name
;
1503 devstr
= strchr(sizestr
, ':');
1505 sizestr
= devstr
+ 1;
1507 devstr
= vol_args
->name
;
1508 ret
= kstrtoull(devstr
, 10, &devid
);
1515 btrfs_info(root
->fs_info
, "resizing devid %llu", devid
);
1518 device
= btrfs_find_device(root
->fs_info
, devid
, NULL
, NULL
);
1520 btrfs_info(root
->fs_info
, "resizer unable to find device %llu",
1526 if (!device
->writeable
) {
1527 btrfs_info(root
->fs_info
,
1528 "resizer unable to apply on readonly device %llu",
1534 if (!strcmp(sizestr
, "max"))
1535 new_size
= device
->bdev
->bd_inode
->i_size
;
1537 if (sizestr
[0] == '-') {
1540 } else if (sizestr
[0] == '+') {
1544 new_size
= memparse(sizestr
, &retptr
);
1545 if (*retptr
!= '\0' || new_size
== 0) {
1551 if (device
->is_tgtdev_for_dev_replace
) {
1556 old_size
= btrfs_device_get_total_bytes(device
);
1559 if (new_size
> old_size
) {
1563 new_size
= old_size
- new_size
;
1564 } else if (mod
> 0) {
1565 if (new_size
> ULLONG_MAX
- old_size
) {
1569 new_size
= old_size
+ new_size
;
1572 if (new_size
< 256 * 1024 * 1024) {
1576 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1581 new_size
= div_u64(new_size
, root
->sectorsize
);
1582 new_size
*= root
->sectorsize
;
1584 btrfs_info_in_rcu(root
->fs_info
, "new size for %s is %llu",
1585 rcu_str_deref(device
->name
), new_size
);
1587 if (new_size
> old_size
) {
1588 trans
= btrfs_start_transaction(root
, 0);
1589 if (IS_ERR(trans
)) {
1590 ret
= PTR_ERR(trans
);
1593 ret
= btrfs_grow_device(trans
, device
, new_size
);
1594 btrfs_commit_transaction(trans
, root
);
1595 } else if (new_size
< old_size
) {
1596 ret
= btrfs_shrink_device(device
, new_size
);
1597 } /* equal, nothing need to do */
1602 mutex_unlock(&root
->fs_info
->volume_mutex
);
1603 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
1604 mnt_drop_write_file(file
);
1608 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1609 char *name
, unsigned long fd
, int subvol
,
1610 u64
*transid
, bool readonly
,
1611 struct btrfs_qgroup_inherit
*inherit
)
1616 ret
= mnt_want_write_file(file
);
1620 namelen
= strlen(name
);
1621 if (strchr(name
, '/')) {
1623 goto out_drop_write
;
1626 if (name
[0] == '.' &&
1627 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1629 goto out_drop_write
;
1633 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1634 NULL
, transid
, readonly
, inherit
);
1636 struct fd src
= fdget(fd
);
1637 struct inode
*src_inode
;
1640 goto out_drop_write
;
1643 src_inode
= file_inode(src
.file
);
1644 if (src_inode
->i_sb
!= file_inode(file
)->i_sb
) {
1645 btrfs_info(BTRFS_I(src_inode
)->root
->fs_info
,
1646 "Snapshot src from another FS");
1648 } else if (!inode_owner_or_capable(src_inode
)) {
1650 * Subvolume creation is not restricted, but snapshots
1651 * are limited to own subvolumes only
1655 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1656 BTRFS_I(src_inode
)->root
,
1657 transid
, readonly
, inherit
);
1662 mnt_drop_write_file(file
);
1667 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1668 void __user
*arg
, int subvol
)
1670 struct btrfs_ioctl_vol_args
*vol_args
;
1673 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1674 if (IS_ERR(vol_args
))
1675 return PTR_ERR(vol_args
);
1676 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1678 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1679 vol_args
->fd
, subvol
,
1686 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1687 void __user
*arg
, int subvol
)
1689 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1693 bool readonly
= false;
1694 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1696 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1697 if (IS_ERR(vol_args
))
1698 return PTR_ERR(vol_args
);
1699 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1701 if (vol_args
->flags
&
1702 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1703 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1708 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1710 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1712 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1713 if (vol_args
->size
> PAGE_CACHE_SIZE
) {
1717 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1718 if (IS_ERR(inherit
)) {
1719 ret
= PTR_ERR(inherit
);
1724 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1725 vol_args
->fd
, subvol
, ptr
,
1730 if (ptr
&& copy_to_user(arg
+
1731 offsetof(struct btrfs_ioctl_vol_args_v2
,
1743 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1746 struct inode
*inode
= file_inode(file
);
1747 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1751 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1754 down_read(&root
->fs_info
->subvol_sem
);
1755 if (btrfs_root_readonly(root
))
1756 flags
|= BTRFS_SUBVOL_RDONLY
;
1757 up_read(&root
->fs_info
->subvol_sem
);
1759 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1765 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1768 struct inode
*inode
= file_inode(file
);
1769 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1770 struct btrfs_trans_handle
*trans
;
1775 if (!inode_owner_or_capable(inode
))
1778 ret
= mnt_want_write_file(file
);
1782 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1784 goto out_drop_write
;
1787 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1789 goto out_drop_write
;
1792 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1794 goto out_drop_write
;
1797 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1799 goto out_drop_write
;
1802 down_write(&root
->fs_info
->subvol_sem
);
1805 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1808 root_flags
= btrfs_root_flags(&root
->root_item
);
1809 if (flags
& BTRFS_SUBVOL_RDONLY
) {
1810 btrfs_set_root_flags(&root
->root_item
,
1811 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1814 * Block RO -> RW transition if this subvolume is involved in
1817 spin_lock(&root
->root_item_lock
);
1818 if (root
->send_in_progress
== 0) {
1819 btrfs_set_root_flags(&root
->root_item
,
1820 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1821 spin_unlock(&root
->root_item_lock
);
1823 spin_unlock(&root
->root_item_lock
);
1824 btrfs_warn(root
->fs_info
,
1825 "Attempt to set subvolume %llu read-write during send",
1826 root
->root_key
.objectid
);
1832 trans
= btrfs_start_transaction(root
, 1);
1833 if (IS_ERR(trans
)) {
1834 ret
= PTR_ERR(trans
);
1838 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1839 &root
->root_key
, &root
->root_item
);
1841 btrfs_commit_transaction(trans
, root
);
1844 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1846 up_write(&root
->fs_info
->subvol_sem
);
1848 mnt_drop_write_file(file
);
1854 * helper to check if the subvolume references other subvolumes
1856 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1858 struct btrfs_path
*path
;
1859 struct btrfs_dir_item
*di
;
1860 struct btrfs_key key
;
1864 path
= btrfs_alloc_path();
1868 /* Make sure this root isn't set as the default subvol */
1869 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
1870 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, path
,
1871 dir_id
, "default", 7, 0);
1872 if (di
&& !IS_ERR(di
)) {
1873 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1874 if (key
.objectid
== root
->root_key
.objectid
) {
1876 btrfs_err(root
->fs_info
, "deleting default subvolume "
1877 "%llu is not allowed", key
.objectid
);
1880 btrfs_release_path(path
);
1883 key
.objectid
= root
->root_key
.objectid
;
1884 key
.type
= BTRFS_ROOT_REF_KEY
;
1885 key
.offset
= (u64
)-1;
1887 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1894 if (path
->slots
[0] > 0) {
1896 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1897 if (key
.objectid
== root
->root_key
.objectid
&&
1898 key
.type
== BTRFS_ROOT_REF_KEY
)
1902 btrfs_free_path(path
);
1906 static noinline
int key_in_sk(struct btrfs_key
*key
,
1907 struct btrfs_ioctl_search_key
*sk
)
1909 struct btrfs_key test
;
1912 test
.objectid
= sk
->min_objectid
;
1913 test
.type
= sk
->min_type
;
1914 test
.offset
= sk
->min_offset
;
1916 ret
= btrfs_comp_cpu_keys(key
, &test
);
1920 test
.objectid
= sk
->max_objectid
;
1921 test
.type
= sk
->max_type
;
1922 test
.offset
= sk
->max_offset
;
1924 ret
= btrfs_comp_cpu_keys(key
, &test
);
1930 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1931 struct btrfs_path
*path
,
1932 struct btrfs_key
*key
,
1933 struct btrfs_ioctl_search_key
*sk
,
1936 unsigned long *sk_offset
,
1940 struct extent_buffer
*leaf
;
1941 struct btrfs_ioctl_search_header sh
;
1942 struct btrfs_key test
;
1943 unsigned long item_off
;
1944 unsigned long item_len
;
1950 leaf
= path
->nodes
[0];
1951 slot
= path
->slots
[0];
1952 nritems
= btrfs_header_nritems(leaf
);
1954 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1958 found_transid
= btrfs_header_generation(leaf
);
1960 for (i
= slot
; i
< nritems
; i
++) {
1961 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1962 item_len
= btrfs_item_size_nr(leaf
, i
);
1964 btrfs_item_key_to_cpu(leaf
, key
, i
);
1965 if (!key_in_sk(key
, sk
))
1968 if (sizeof(sh
) + item_len
> *buf_size
) {
1975 * return one empty item back for v1, which does not
1979 *buf_size
= sizeof(sh
) + item_len
;
1984 if (sizeof(sh
) + item_len
+ *sk_offset
> *buf_size
) {
1989 sh
.objectid
= key
->objectid
;
1990 sh
.offset
= key
->offset
;
1991 sh
.type
= key
->type
;
1993 sh
.transid
= found_transid
;
1995 /* copy search result header */
1996 if (copy_to_user(ubuf
+ *sk_offset
, &sh
, sizeof(sh
))) {
2001 *sk_offset
+= sizeof(sh
);
2004 char __user
*up
= ubuf
+ *sk_offset
;
2006 if (read_extent_buffer_to_user(leaf
, up
,
2007 item_off
, item_len
)) {
2012 *sk_offset
+= item_len
;
2016 if (ret
) /* -EOVERFLOW from above */
2019 if (*num_found
>= sk
->nr_items
) {
2026 test
.objectid
= sk
->max_objectid
;
2027 test
.type
= sk
->max_type
;
2028 test
.offset
= sk
->max_offset
;
2029 if (btrfs_comp_cpu_keys(key
, &test
) >= 0)
2031 else if (key
->offset
< (u64
)-1)
2033 else if (key
->type
< (u8
)-1) {
2036 } else if (key
->objectid
< (u64
)-1) {
2044 * 0: all items from this leaf copied, continue with next
2045 * 1: * more items can be copied, but unused buffer is too small
2046 * * all items were found
2047 * Either way, it will stops the loop which iterates to the next
2049 * -EOVERFLOW: item was to large for buffer
2050 * -EFAULT: could not copy extent buffer back to userspace
2055 static noinline
int search_ioctl(struct inode
*inode
,
2056 struct btrfs_ioctl_search_key
*sk
,
2060 struct btrfs_root
*root
;
2061 struct btrfs_key key
;
2062 struct btrfs_path
*path
;
2063 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
2066 unsigned long sk_offset
= 0;
2068 if (*buf_size
< sizeof(struct btrfs_ioctl_search_header
)) {
2069 *buf_size
= sizeof(struct btrfs_ioctl_search_header
);
2073 path
= btrfs_alloc_path();
2077 if (sk
->tree_id
== 0) {
2078 /* search the root of the inode that was passed */
2079 root
= BTRFS_I(inode
)->root
;
2081 key
.objectid
= sk
->tree_id
;
2082 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2083 key
.offset
= (u64
)-1;
2084 root
= btrfs_read_fs_root_no_name(info
, &key
);
2086 btrfs_err(info
, "could not find root %llu",
2088 btrfs_free_path(path
);
2093 key
.objectid
= sk
->min_objectid
;
2094 key
.type
= sk
->min_type
;
2095 key
.offset
= sk
->min_offset
;
2098 ret
= btrfs_search_forward(root
, &key
, path
, sk
->min_transid
);
2104 ret
= copy_to_sk(root
, path
, &key
, sk
, buf_size
, ubuf
,
2105 &sk_offset
, &num_found
);
2106 btrfs_release_path(path
);
2114 sk
->nr_items
= num_found
;
2115 btrfs_free_path(path
);
2119 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
2122 struct btrfs_ioctl_search_args __user
*uargs
;
2123 struct btrfs_ioctl_search_key sk
;
2124 struct inode
*inode
;
2128 if (!capable(CAP_SYS_ADMIN
))
2131 uargs
= (struct btrfs_ioctl_search_args __user
*)argp
;
2133 if (copy_from_user(&sk
, &uargs
->key
, sizeof(sk
)))
2136 buf_size
= sizeof(uargs
->buf
);
2138 inode
= file_inode(file
);
2139 ret
= search_ioctl(inode
, &sk
, &buf_size
, uargs
->buf
);
2142 * In the origin implementation an overflow is handled by returning a
2143 * search header with a len of zero, so reset ret.
2145 if (ret
== -EOVERFLOW
)
2148 if (ret
== 0 && copy_to_user(&uargs
->key
, &sk
, sizeof(sk
)))
2153 static noinline
int btrfs_ioctl_tree_search_v2(struct file
*file
,
2156 struct btrfs_ioctl_search_args_v2 __user
*uarg
;
2157 struct btrfs_ioctl_search_args_v2 args
;
2158 struct inode
*inode
;
2161 const size_t buf_limit
= 16 * 1024 * 1024;
2163 if (!capable(CAP_SYS_ADMIN
))
2166 /* copy search header and buffer size */
2167 uarg
= (struct btrfs_ioctl_search_args_v2 __user
*)argp
;
2168 if (copy_from_user(&args
, uarg
, sizeof(args
)))
2171 buf_size
= args
.buf_size
;
2173 if (buf_size
< sizeof(struct btrfs_ioctl_search_header
))
2176 /* limit result size to 16MB */
2177 if (buf_size
> buf_limit
)
2178 buf_size
= buf_limit
;
2180 inode
= file_inode(file
);
2181 ret
= search_ioctl(inode
, &args
.key
, &buf_size
,
2182 (char *)(&uarg
->buf
[0]));
2183 if (ret
== 0 && copy_to_user(&uarg
->key
, &args
.key
, sizeof(args
.key
)))
2185 else if (ret
== -EOVERFLOW
&&
2186 copy_to_user(&uarg
->buf_size
, &buf_size
, sizeof(buf_size
)))
2193 * Search INODE_REFs to identify path name of 'dirid' directory
2194 * in a 'tree_id' tree. and sets path name to 'name'.
2196 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
2197 u64 tree_id
, u64 dirid
, char *name
)
2199 struct btrfs_root
*root
;
2200 struct btrfs_key key
;
2206 struct btrfs_inode_ref
*iref
;
2207 struct extent_buffer
*l
;
2208 struct btrfs_path
*path
;
2210 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
2215 path
= btrfs_alloc_path();
2219 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
2221 key
.objectid
= tree_id
;
2222 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2223 key
.offset
= (u64
)-1;
2224 root
= btrfs_read_fs_root_no_name(info
, &key
);
2226 btrfs_err(info
, "could not find root %llu", tree_id
);
2231 key
.objectid
= dirid
;
2232 key
.type
= BTRFS_INODE_REF_KEY
;
2233 key
.offset
= (u64
)-1;
2236 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2240 ret
= btrfs_previous_item(root
, path
, dirid
,
2241 BTRFS_INODE_REF_KEY
);
2251 slot
= path
->slots
[0];
2252 btrfs_item_key_to_cpu(l
, &key
, slot
);
2254 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
2255 len
= btrfs_inode_ref_name_len(l
, iref
);
2257 total_len
+= len
+ 1;
2259 ret
= -ENAMETOOLONG
;
2264 read_extent_buffer(l
, ptr
, (unsigned long)(iref
+ 1), len
);
2266 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
2269 btrfs_release_path(path
);
2270 key
.objectid
= key
.offset
;
2271 key
.offset
= (u64
)-1;
2272 dirid
= key
.objectid
;
2274 memmove(name
, ptr
, total_len
);
2275 name
[total_len
] = '\0';
2278 btrfs_free_path(path
);
2282 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
2285 struct btrfs_ioctl_ino_lookup_args
*args
;
2286 struct inode
*inode
;
2289 args
= memdup_user(argp
, sizeof(*args
));
2291 return PTR_ERR(args
);
2293 inode
= file_inode(file
);
2296 * Unprivileged query to obtain the containing subvolume root id. The
2297 * path is reset so it's consistent with btrfs_search_path_in_tree.
2299 if (args
->treeid
== 0)
2300 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2302 if (args
->objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
2307 if (!capable(CAP_SYS_ADMIN
)) {
2312 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
2313 args
->treeid
, args
->objectid
,
2317 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2324 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
2327 struct dentry
*parent
= file
->f_path
.dentry
;
2328 struct dentry
*dentry
;
2329 struct inode
*dir
= d_inode(parent
);
2330 struct inode
*inode
;
2331 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2332 struct btrfs_root
*dest
= NULL
;
2333 struct btrfs_ioctl_vol_args
*vol_args
;
2334 struct btrfs_trans_handle
*trans
;
2335 struct btrfs_block_rsv block_rsv
;
2337 u64 qgroup_reserved
;
2342 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2343 if (IS_ERR(vol_args
))
2344 return PTR_ERR(vol_args
);
2346 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2347 namelen
= strlen(vol_args
->name
);
2348 if (strchr(vol_args
->name
, '/') ||
2349 strncmp(vol_args
->name
, "..", namelen
) == 0) {
2354 err
= mnt_want_write_file(file
);
2359 err
= mutex_lock_killable_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
2361 goto out_drop_write
;
2362 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
2363 if (IS_ERR(dentry
)) {
2364 err
= PTR_ERR(dentry
);
2365 goto out_unlock_dir
;
2368 if (d_really_is_negative(dentry
)) {
2373 inode
= d_inode(dentry
);
2374 dest
= BTRFS_I(inode
)->root
;
2375 if (!capable(CAP_SYS_ADMIN
)) {
2377 * Regular user. Only allow this with a special mount
2378 * option, when the user has write+exec access to the
2379 * subvol root, and when rmdir(2) would have been
2382 * Note that this is _not_ check that the subvol is
2383 * empty or doesn't contain data that we wouldn't
2384 * otherwise be able to delete.
2386 * Users who want to delete empty subvols should try
2390 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
2394 * Do not allow deletion if the parent dir is the same
2395 * as the dir to be deleted. That means the ioctl
2396 * must be called on the dentry referencing the root
2397 * of the subvol, not a random directory contained
2404 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
2409 /* check if subvolume may be deleted by a user */
2410 err
= btrfs_may_delete(dir
, dentry
, 1);
2414 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
2419 mutex_lock(&inode
->i_mutex
);
2422 * Don't allow to delete a subvolume with send in progress. This is
2423 * inside the i_mutex so the error handling that has to drop the bit
2424 * again is not run concurrently.
2426 spin_lock(&dest
->root_item_lock
);
2427 root_flags
= btrfs_root_flags(&dest
->root_item
);
2428 if (dest
->send_in_progress
== 0) {
2429 btrfs_set_root_flags(&dest
->root_item
,
2430 root_flags
| BTRFS_ROOT_SUBVOL_DEAD
);
2431 spin_unlock(&dest
->root_item_lock
);
2433 spin_unlock(&dest
->root_item_lock
);
2434 btrfs_warn(root
->fs_info
,
2435 "Attempt to delete subvolume %llu during send",
2436 dest
->root_key
.objectid
);
2438 goto out_unlock_inode
;
2441 down_write(&root
->fs_info
->subvol_sem
);
2443 err
= may_destroy_subvol(dest
);
2447 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
2449 * One for dir inode, two for dir entries, two for root
2452 err
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
2453 5, &qgroup_reserved
, true);
2457 trans
= btrfs_start_transaction(root
, 0);
2458 if (IS_ERR(trans
)) {
2459 err
= PTR_ERR(trans
);
2462 trans
->block_rsv
= &block_rsv
;
2463 trans
->bytes_reserved
= block_rsv
.size
;
2465 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
2466 dest
->root_key
.objectid
,
2467 dentry
->d_name
.name
,
2468 dentry
->d_name
.len
);
2471 btrfs_abort_transaction(trans
, root
, ret
);
2475 btrfs_record_root_in_trans(trans
, dest
);
2477 memset(&dest
->root_item
.drop_progress
, 0,
2478 sizeof(dest
->root_item
.drop_progress
));
2479 dest
->root_item
.drop_level
= 0;
2480 btrfs_set_root_refs(&dest
->root_item
, 0);
2482 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &dest
->state
)) {
2483 ret
= btrfs_insert_orphan_item(trans
,
2484 root
->fs_info
->tree_root
,
2485 dest
->root_key
.objectid
);
2487 btrfs_abort_transaction(trans
, root
, ret
);
2493 ret
= btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
2494 dest
->root_item
.uuid
, BTRFS_UUID_KEY_SUBVOL
,
2495 dest
->root_key
.objectid
);
2496 if (ret
&& ret
!= -ENOENT
) {
2497 btrfs_abort_transaction(trans
, root
, ret
);
2501 if (!btrfs_is_empty_uuid(dest
->root_item
.received_uuid
)) {
2502 ret
= btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
2503 dest
->root_item
.received_uuid
,
2504 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
2505 dest
->root_key
.objectid
);
2506 if (ret
&& ret
!= -ENOENT
) {
2507 btrfs_abort_transaction(trans
, root
, ret
);
2514 trans
->block_rsv
= NULL
;
2515 trans
->bytes_reserved
= 0;
2516 ret
= btrfs_end_transaction(trans
, root
);
2519 inode
->i_flags
|= S_DEAD
;
2521 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
2523 up_write(&root
->fs_info
->subvol_sem
);
2525 spin_lock(&dest
->root_item_lock
);
2526 root_flags
= btrfs_root_flags(&dest
->root_item
);
2527 btrfs_set_root_flags(&dest
->root_item
,
2528 root_flags
& ~BTRFS_ROOT_SUBVOL_DEAD
);
2529 spin_unlock(&dest
->root_item_lock
);
2532 mutex_unlock(&inode
->i_mutex
);
2534 d_invalidate(dentry
);
2535 btrfs_invalidate_inodes(dest
);
2537 ASSERT(dest
->send_in_progress
== 0);
2540 if (dest
->ino_cache_inode
) {
2541 iput(dest
->ino_cache_inode
);
2542 dest
->ino_cache_inode
= NULL
;
2548 mutex_unlock(&dir
->i_mutex
);
2550 mnt_drop_write_file(file
);
2556 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2558 struct inode
*inode
= file_inode(file
);
2559 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2560 struct btrfs_ioctl_defrag_range_args
*range
;
2563 ret
= mnt_want_write_file(file
);
2567 if (btrfs_root_readonly(root
)) {
2572 switch (inode
->i_mode
& S_IFMT
) {
2574 if (!capable(CAP_SYS_ADMIN
)) {
2578 ret
= btrfs_defrag_root(root
);
2581 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
);
2584 if (!(file
->f_mode
& FMODE_WRITE
)) {
2589 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2596 if (copy_from_user(range
, argp
,
2602 /* compression requires us to start the IO */
2603 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2604 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2605 range
->extent_thresh
= (u32
)-1;
2608 /* the rest are all set to zero by kzalloc */
2609 range
->len
= (u64
)-1;
2611 ret
= btrfs_defrag_file(file_inode(file
), file
,
2621 mnt_drop_write_file(file
);
2625 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2627 struct btrfs_ioctl_vol_args
*vol_args
;
2630 if (!capable(CAP_SYS_ADMIN
))
2633 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2635 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2638 mutex_lock(&root
->fs_info
->volume_mutex
);
2639 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2640 if (IS_ERR(vol_args
)) {
2641 ret
= PTR_ERR(vol_args
);
2645 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2646 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2649 btrfs_info(root
->fs_info
, "disk added %s",vol_args
->name
);
2653 mutex_unlock(&root
->fs_info
->volume_mutex
);
2654 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2658 static long btrfs_ioctl_rm_dev(struct file
*file
, void __user
*arg
)
2660 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
2661 struct btrfs_ioctl_vol_args
*vol_args
;
2664 if (!capable(CAP_SYS_ADMIN
))
2667 ret
= mnt_want_write_file(file
);
2671 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2672 if (IS_ERR(vol_args
)) {
2673 ret
= PTR_ERR(vol_args
);
2677 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2679 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2681 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2685 mutex_lock(&root
->fs_info
->volume_mutex
);
2686 ret
= btrfs_rm_device(root
, vol_args
->name
);
2687 mutex_unlock(&root
->fs_info
->volume_mutex
);
2688 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2691 btrfs_info(root
->fs_info
, "disk deleted %s",vol_args
->name
);
2696 mnt_drop_write_file(file
);
2700 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2702 struct btrfs_ioctl_fs_info_args
*fi_args
;
2703 struct btrfs_device
*device
;
2704 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2707 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2711 mutex_lock(&fs_devices
->device_list_mutex
);
2712 fi_args
->num_devices
= fs_devices
->num_devices
;
2713 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2715 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
2716 if (device
->devid
> fi_args
->max_id
)
2717 fi_args
->max_id
= device
->devid
;
2719 mutex_unlock(&fs_devices
->device_list_mutex
);
2721 fi_args
->nodesize
= root
->fs_info
->super_copy
->nodesize
;
2722 fi_args
->sectorsize
= root
->fs_info
->super_copy
->sectorsize
;
2723 fi_args
->clone_alignment
= root
->fs_info
->super_copy
->sectorsize
;
2725 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2732 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2734 struct btrfs_ioctl_dev_info_args
*di_args
;
2735 struct btrfs_device
*dev
;
2736 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2738 char *s_uuid
= NULL
;
2740 di_args
= memdup_user(arg
, sizeof(*di_args
));
2741 if (IS_ERR(di_args
))
2742 return PTR_ERR(di_args
);
2744 if (!btrfs_is_empty_uuid(di_args
->uuid
))
2745 s_uuid
= di_args
->uuid
;
2747 mutex_lock(&fs_devices
->device_list_mutex
);
2748 dev
= btrfs_find_device(root
->fs_info
, di_args
->devid
, s_uuid
, NULL
);
2755 di_args
->devid
= dev
->devid
;
2756 di_args
->bytes_used
= btrfs_device_get_bytes_used(dev
);
2757 di_args
->total_bytes
= btrfs_device_get_total_bytes(dev
);
2758 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2760 struct rcu_string
*name
;
2763 name
= rcu_dereference(dev
->name
);
2764 strncpy(di_args
->path
, name
->str
, sizeof(di_args
->path
));
2766 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
2768 di_args
->path
[0] = '\0';
2772 mutex_unlock(&fs_devices
->device_list_mutex
);
2773 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2780 static struct page
*extent_same_get_page(struct inode
*inode
, pgoff_t index
)
2783 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2785 page
= grab_cache_page(inode
->i_mapping
, index
);
2789 if (!PageUptodate(page
)) {
2790 if (extent_read_full_page_nolock(tree
, page
, btrfs_get_extent
,
2794 if (!PageUptodate(page
)) {
2796 page_cache_release(page
);
2805 static int gather_extent_pages(struct inode
*inode
, struct page
**pages
,
2806 int num_pages
, u64 off
)
2809 pgoff_t index
= off
>> PAGE_CACHE_SHIFT
;
2811 for (i
= 0; i
< num_pages
; i
++) {
2812 pages
[i
] = extent_same_get_page(inode
, index
+ i
);
2819 static inline void lock_extent_range(struct inode
*inode
, u64 off
, u64 len
)
2821 /* do any pending delalloc/csum calc on src, one way or
2822 another, and lock file content */
2824 struct btrfs_ordered_extent
*ordered
;
2825 lock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2826 ordered
= btrfs_lookup_first_ordered_extent(inode
,
2829 ordered
->file_offset
+ ordered
->len
<= off
||
2830 ordered
->file_offset
>= off
+ len
) &&
2831 !test_range_bit(&BTRFS_I(inode
)->io_tree
, off
,
2832 off
+ len
- 1, EXTENT_DELALLOC
, 0, NULL
)) {
2834 btrfs_put_ordered_extent(ordered
);
2837 unlock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2839 btrfs_put_ordered_extent(ordered
);
2840 btrfs_wait_ordered_range(inode
, off
, len
);
2844 static void btrfs_double_inode_unlock(struct inode
*inode1
, struct inode
*inode2
)
2846 mutex_unlock(&inode1
->i_mutex
);
2847 mutex_unlock(&inode2
->i_mutex
);
2850 static void btrfs_double_inode_lock(struct inode
*inode1
, struct inode
*inode2
)
2852 if (inode1
< inode2
)
2853 swap(inode1
, inode2
);
2855 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_PARENT
);
2856 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_CHILD
);
2859 static void btrfs_double_extent_unlock(struct inode
*inode1
, u64 loff1
,
2860 struct inode
*inode2
, u64 loff2
, u64 len
)
2862 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
2863 unlock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
2866 static void btrfs_double_extent_lock(struct inode
*inode1
, u64 loff1
,
2867 struct inode
*inode2
, u64 loff2
, u64 len
)
2869 if (inode1
< inode2
) {
2870 swap(inode1
, inode2
);
2873 lock_extent_range(inode1
, loff1
, len
);
2874 lock_extent_range(inode2
, loff2
, len
);
2879 struct page
**src_pages
;
2880 struct page
**dst_pages
;
2883 static void btrfs_cmp_data_free(struct cmp_pages
*cmp
)
2888 for (i
= 0; i
< cmp
->num_pages
; i
++) {
2889 pg
= cmp
->src_pages
[i
];
2891 page_cache_release(pg
);
2892 pg
= cmp
->dst_pages
[i
];
2894 page_cache_release(pg
);
2896 kfree(cmp
->src_pages
);
2897 kfree(cmp
->dst_pages
);
2900 static int btrfs_cmp_data_prepare(struct inode
*src
, u64 loff
,
2901 struct inode
*dst
, u64 dst_loff
,
2902 u64 len
, struct cmp_pages
*cmp
)
2905 int num_pages
= PAGE_CACHE_ALIGN(len
) >> PAGE_CACHE_SHIFT
;
2906 struct page
**src_pgarr
, **dst_pgarr
;
2909 * We must gather up all the pages before we initiate our
2910 * extent locking. We use an array for the page pointers. Size
2911 * of the array is bounded by len, which is in turn bounded by
2912 * BTRFS_MAX_DEDUPE_LEN.
2914 src_pgarr
= kzalloc(num_pages
* sizeof(struct page
*), GFP_NOFS
);
2915 dst_pgarr
= kzalloc(num_pages
* sizeof(struct page
*), GFP_NOFS
);
2916 if (!src_pgarr
|| !dst_pgarr
) {
2921 cmp
->num_pages
= num_pages
;
2922 cmp
->src_pages
= src_pgarr
;
2923 cmp
->dst_pages
= dst_pgarr
;
2925 ret
= gather_extent_pages(src
, cmp
->src_pages
, cmp
->num_pages
, loff
);
2929 ret
= gather_extent_pages(dst
, cmp
->dst_pages
, cmp
->num_pages
, dst_loff
);
2933 btrfs_cmp_data_free(cmp
);
2937 static int btrfs_cmp_data(struct inode
*src
, u64 loff
, struct inode
*dst
,
2938 u64 dst_loff
, u64 len
, struct cmp_pages
*cmp
)
2942 struct page
*src_page
, *dst_page
;
2943 unsigned int cmp_len
= PAGE_CACHE_SIZE
;
2944 void *addr
, *dst_addr
;
2948 if (len
< PAGE_CACHE_SIZE
)
2951 BUG_ON(i
>= cmp
->num_pages
);
2953 src_page
= cmp
->src_pages
[i
];
2954 dst_page
= cmp
->dst_pages
[i
];
2956 addr
= kmap_atomic(src_page
);
2957 dst_addr
= kmap_atomic(dst_page
);
2959 flush_dcache_page(src_page
);
2960 flush_dcache_page(dst_page
);
2962 if (memcmp(addr
, dst_addr
, cmp_len
))
2963 ret
= BTRFS_SAME_DATA_DIFFERS
;
2965 kunmap_atomic(addr
);
2966 kunmap_atomic(dst_addr
);
2978 static int extent_same_check_offsets(struct inode
*inode
, u64 off
, u64
*plen
,
2982 u64 bs
= BTRFS_I(inode
)->root
->fs_info
->sb
->s_blocksize
;
2984 if (off
+ olen
> inode
->i_size
|| off
+ olen
< off
)
2987 /* if we extend to eof, continue to block boundary */
2988 if (off
+ len
== inode
->i_size
)
2989 *plen
= len
= ALIGN(inode
->i_size
, bs
) - off
;
2991 /* Check that we are block aligned - btrfs_clone() requires this */
2992 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
))
2998 static int btrfs_extent_same(struct inode
*src
, u64 loff
, u64 olen
,
2999 struct inode
*dst
, u64 dst_loff
)
3003 struct cmp_pages cmp
;
3005 u64 same_lock_start
= 0;
3006 u64 same_lock_len
= 0;
3015 mutex_lock(&src
->i_mutex
);
3017 ret
= extent_same_check_offsets(src
, loff
, &len
, olen
);
3022 * Single inode case wants the same checks, except we
3023 * don't want our length pushed out past i_size as
3024 * comparing that data range makes no sense.
3026 * extent_same_check_offsets() will do this for an
3027 * unaligned length at i_size, so catch it here and
3028 * reject the request.
3030 * This effectively means we require aligned extents
3031 * for the single-inode case, whereas the other cases
3032 * allow an unaligned length so long as it ends at
3040 /* Check for overlapping ranges */
3041 if (dst_loff
+ len
> loff
&& dst_loff
< loff
+ len
) {
3046 same_lock_start
= min_t(u64
, loff
, dst_loff
);
3047 same_lock_len
= max_t(u64
, loff
, dst_loff
) + len
- same_lock_start
;
3049 btrfs_double_inode_lock(src
, dst
);
3051 ret
= extent_same_check_offsets(src
, loff
, &len
, olen
);
3055 ret
= extent_same_check_offsets(dst
, dst_loff
, &len
, olen
);
3060 /* don't make the dst file partly checksummed */
3061 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3062 (BTRFS_I(dst
)->flags
& BTRFS_INODE_NODATASUM
)) {
3067 ret
= btrfs_cmp_data_prepare(src
, loff
, dst
, dst_loff
, olen
, &cmp
);
3072 lock_extent_range(src
, same_lock_start
, same_lock_len
);
3074 btrfs_double_extent_lock(src
, loff
, dst
, dst_loff
, len
);
3076 /* pass original length for comparison so we stay within i_size */
3077 ret
= btrfs_cmp_data(src
, loff
, dst
, dst_loff
, olen
, &cmp
);
3079 ret
= btrfs_clone(src
, dst
, loff
, olen
, len
, dst_loff
, 1);
3082 unlock_extent(&BTRFS_I(src
)->io_tree
, same_lock_start
,
3083 same_lock_start
+ same_lock_len
- 1);
3085 btrfs_double_extent_unlock(src
, loff
, dst
, dst_loff
, len
);
3087 btrfs_cmp_data_free(&cmp
);
3090 mutex_unlock(&src
->i_mutex
);
3092 btrfs_double_inode_unlock(src
, dst
);
3097 #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
3099 static long btrfs_ioctl_file_extent_same(struct file
*file
,
3100 struct btrfs_ioctl_same_args __user
*argp
)
3102 struct btrfs_ioctl_same_args
*same
= NULL
;
3103 struct btrfs_ioctl_same_extent_info
*info
;
3104 struct inode
*src
= file_inode(file
);
3110 u64 bs
= BTRFS_I(src
)->root
->fs_info
->sb
->s_blocksize
;
3111 bool is_admin
= capable(CAP_SYS_ADMIN
);
3114 if (!(file
->f_mode
& FMODE_READ
))
3117 ret
= mnt_want_write_file(file
);
3121 if (get_user(count
, &argp
->dest_count
)) {
3126 size
= offsetof(struct btrfs_ioctl_same_args __user
, info
[count
]);
3128 same
= memdup_user(argp
, size
);
3131 ret
= PTR_ERR(same
);
3136 off
= same
->logical_offset
;
3140 * Limit the total length we will dedupe for each operation.
3141 * This is intended to bound the total time spent in this
3142 * ioctl to something sane.
3144 if (len
> BTRFS_MAX_DEDUPE_LEN
)
3145 len
= BTRFS_MAX_DEDUPE_LEN
;
3147 if (WARN_ON_ONCE(bs
< PAGE_CACHE_SIZE
)) {
3149 * Btrfs does not support blocksize < page_size. As a
3150 * result, btrfs_cmp_data() won't correctly handle
3151 * this situation without an update.
3158 if (S_ISDIR(src
->i_mode
))
3162 if (!S_ISREG(src
->i_mode
))
3165 /* pre-format output fields to sane values */
3166 for (i
= 0; i
< count
; i
++) {
3167 same
->info
[i
].bytes_deduped
= 0ULL;
3168 same
->info
[i
].status
= 0;
3171 for (i
= 0, info
= same
->info
; i
< count
; i
++, info
++) {
3173 struct fd dst_file
= fdget(info
->fd
);
3174 if (!dst_file
.file
) {
3175 info
->status
= -EBADF
;
3178 dst
= file_inode(dst_file
.file
);
3180 if (!(is_admin
|| (dst_file
.file
->f_mode
& FMODE_WRITE
))) {
3181 info
->status
= -EINVAL
;
3182 } else if (file
->f_path
.mnt
!= dst_file
.file
->f_path
.mnt
) {
3183 info
->status
= -EXDEV
;
3184 } else if (S_ISDIR(dst
->i_mode
)) {
3185 info
->status
= -EISDIR
;
3186 } else if (!S_ISREG(dst
->i_mode
)) {
3187 info
->status
= -EACCES
;
3189 info
->status
= btrfs_extent_same(src
, off
, len
, dst
,
3190 info
->logical_offset
);
3191 if (info
->status
== 0)
3192 info
->bytes_deduped
+= len
;
3197 ret
= copy_to_user(argp
, same
, size
);
3202 mnt_drop_write_file(file
);
3207 static int clone_finish_inode_update(struct btrfs_trans_handle
*trans
,
3208 struct inode
*inode
,
3214 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3217 inode_inc_iversion(inode
);
3218 if (!no_time_update
)
3219 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
3221 * We round up to the block size at eof when determining which
3222 * extents to clone above, but shouldn't round up the file size.
3224 if (endoff
> destoff
+ olen
)
3225 endoff
= destoff
+ olen
;
3226 if (endoff
> inode
->i_size
)
3227 btrfs_i_size_write(inode
, endoff
);
3229 ret
= btrfs_update_inode(trans
, root
, inode
);
3231 btrfs_abort_transaction(trans
, root
, ret
);
3232 btrfs_end_transaction(trans
, root
);
3235 ret
= btrfs_end_transaction(trans
, root
);
3240 static void clone_update_extent_map(struct inode
*inode
,
3241 const struct btrfs_trans_handle
*trans
,
3242 const struct btrfs_path
*path
,
3243 const u64 hole_offset
,
3246 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
3247 struct extent_map
*em
;
3250 em
= alloc_extent_map();
3252 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3253 &BTRFS_I(inode
)->runtime_flags
);
3258 struct btrfs_file_extent_item
*fi
;
3260 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3261 struct btrfs_file_extent_item
);
3262 btrfs_extent_item_to_extent_map(inode
, path
, fi
, false, em
);
3263 em
->generation
= -1;
3264 if (btrfs_file_extent_type(path
->nodes
[0], fi
) ==
3265 BTRFS_FILE_EXTENT_INLINE
)
3266 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3267 &BTRFS_I(inode
)->runtime_flags
);
3269 em
->start
= hole_offset
;
3271 em
->ram_bytes
= em
->len
;
3272 em
->orig_start
= hole_offset
;
3273 em
->block_start
= EXTENT_MAP_HOLE
;
3275 em
->orig_block_len
= 0;
3276 em
->compress_type
= BTRFS_COMPRESS_NONE
;
3277 em
->generation
= trans
->transid
;
3281 write_lock(&em_tree
->lock
);
3282 ret
= add_extent_mapping(em_tree
, em
, 1);
3283 write_unlock(&em_tree
->lock
);
3284 if (ret
!= -EEXIST
) {
3285 free_extent_map(em
);
3288 btrfs_drop_extent_cache(inode
, em
->start
,
3289 em
->start
+ em
->len
- 1, 0);
3293 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3294 &BTRFS_I(inode
)->runtime_flags
);
3298 * Make sure we do not end up inserting an inline extent into a file that has
3299 * already other (non-inline) extents. If a file has an inline extent it can
3300 * not have any other extents and the (single) inline extent must start at the
3301 * file offset 0. Failing to respect these rules will lead to file corruption,
3302 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3304 * We can have extents that have been already written to disk or we can have
3305 * dirty ranges still in delalloc, in which case the extent maps and items are
3306 * created only when we run delalloc, and the delalloc ranges might fall outside
3307 * the range we are currently locking in the inode's io tree. So we check the
3308 * inode's i_size because of that (i_size updates are done while holding the
3309 * i_mutex, which we are holding here).
3310 * We also check to see if the inode has a size not greater than "datal" but has
3311 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3312 * protected against such concurrent fallocate calls by the i_mutex).
3314 * If the file has no extents but a size greater than datal, do not allow the
3315 * copy because we would need turn the inline extent into a non-inline one (even
3316 * with NO_HOLES enabled). If we find our destination inode only has one inline
3317 * extent, just overwrite it with the source inline extent if its size is less
3318 * than the source extent's size, or we could copy the source inline extent's
3319 * data into the destination inode's inline extent if the later is greater then
3322 static int clone_copy_inline_extent(struct inode
*src
,
3324 struct btrfs_trans_handle
*trans
,
3325 struct btrfs_path
*path
,
3326 struct btrfs_key
*new_key
,
3327 const u64 drop_start
,
3333 struct btrfs_root
*root
= BTRFS_I(dst
)->root
;
3334 const u64 aligned_end
= ALIGN(new_key
->offset
+ datal
,
3337 struct btrfs_key key
;
3339 if (new_key
->offset
> 0)
3342 key
.objectid
= btrfs_ino(dst
);
3343 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3345 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3348 } else if (ret
> 0) {
3349 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
3350 ret
= btrfs_next_leaf(root
, path
);
3354 goto copy_inline_extent
;
3356 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3357 if (key
.objectid
== btrfs_ino(dst
) &&
3358 key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3359 ASSERT(key
.offset
> 0);
3362 } else if (i_size_read(dst
) <= datal
) {
3363 struct btrfs_file_extent_item
*ei
;
3367 * If the file size is <= datal, make sure there are no other
3368 * extents following (can happen do to an fallocate call with
3369 * the flag FALLOC_FL_KEEP_SIZE).
3371 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3372 struct btrfs_file_extent_item
);
3374 * If it's an inline extent, it can not have other extents
3377 if (btrfs_file_extent_type(path
->nodes
[0], ei
) ==
3378 BTRFS_FILE_EXTENT_INLINE
)
3379 goto copy_inline_extent
;
3381 ext_len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
3382 if (ext_len
> aligned_end
)
3385 ret
= btrfs_next_item(root
, path
);
3388 } else if (ret
== 0) {
3389 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3391 if (key
.objectid
== btrfs_ino(dst
) &&
3392 key
.type
== BTRFS_EXTENT_DATA_KEY
)
3399 * We have no extent items, or we have an extent at offset 0 which may
3400 * or may not be inlined. All these cases are dealt the same way.
3402 if (i_size_read(dst
) > datal
) {
3404 * If the destination inode has an inline extent...
3405 * This would require copying the data from the source inline
3406 * extent into the beginning of the destination's inline extent.
3407 * But this is really complex, both extents can be compressed
3408 * or just one of them, which would require decompressing and
3409 * re-compressing data (which could increase the new compressed
3410 * size, not allowing the compressed data to fit anymore in an
3412 * So just don't support this case for now (it should be rare,
3413 * we are not really saving space when cloning inline extents).
3418 btrfs_release_path(path
);
3419 ret
= btrfs_drop_extents(trans
, root
, dst
, drop_start
, aligned_end
, 1);
3422 ret
= btrfs_insert_empty_item(trans
, root
, path
, new_key
, size
);
3427 const u32 start
= btrfs_file_extent_calc_inline_size(0);
3429 memmove(inline_data
+ start
, inline_data
+ start
+ skip
, datal
);
3432 write_extent_buffer(path
->nodes
[0], inline_data
,
3433 btrfs_item_ptr_offset(path
->nodes
[0],
3436 inode_add_bytes(dst
, datal
);
3442 * btrfs_clone() - clone a range from inode file to another
3444 * @src: Inode to clone from
3445 * @inode: Inode to clone to
3446 * @off: Offset within source to start clone from
3447 * @olen: Original length, passed by user, of range to clone
3448 * @olen_aligned: Block-aligned value of olen
3449 * @destoff: Offset within @inode to start clone
3450 * @no_time_update: Whether to update mtime/ctime on the target inode
3452 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
3453 const u64 off
, const u64 olen
, const u64 olen_aligned
,
3454 const u64 destoff
, int no_time_update
)
3456 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3457 struct btrfs_path
*path
= NULL
;
3458 struct extent_buffer
*leaf
;
3459 struct btrfs_trans_handle
*trans
;
3461 struct btrfs_key key
;
3465 const u64 len
= olen_aligned
;
3466 u64 last_dest_end
= destoff
;
3469 buf
= vmalloc(root
->nodesize
);
3473 path
= btrfs_alloc_path();
3479 path
->reada
= READA_FORWARD
;
3481 key
.objectid
= btrfs_ino(src
);
3482 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3486 u64 next_key_min_offset
= key
.offset
+ 1;
3489 * note the key will change type as we walk through the
3492 path
->leave_spinning
= 1;
3493 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
3498 * First search, if no extent item that starts at offset off was
3499 * found but the previous item is an extent item, it's possible
3500 * it might overlap our target range, therefore process it.
3502 if (key
.offset
== off
&& ret
> 0 && path
->slots
[0] > 0) {
3503 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3504 path
->slots
[0] - 1);
3505 if (key
.type
== BTRFS_EXTENT_DATA_KEY
)
3509 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3511 if (path
->slots
[0] >= nritems
) {
3512 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
3517 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3519 leaf
= path
->nodes
[0];
3520 slot
= path
->slots
[0];
3522 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3523 if (key
.type
> BTRFS_EXTENT_DATA_KEY
||
3524 key
.objectid
!= btrfs_ino(src
))
3527 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3528 struct btrfs_file_extent_item
*extent
;
3531 struct btrfs_key new_key
;
3532 u64 disko
= 0, diskl
= 0;
3533 u64 datao
= 0, datal
= 0;
3537 extent
= btrfs_item_ptr(leaf
, slot
,
3538 struct btrfs_file_extent_item
);
3539 comp
= btrfs_file_extent_compression(leaf
, extent
);
3540 type
= btrfs_file_extent_type(leaf
, extent
);
3541 if (type
== BTRFS_FILE_EXTENT_REG
||
3542 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3543 disko
= btrfs_file_extent_disk_bytenr(leaf
,
3545 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
3547 datao
= btrfs_file_extent_offset(leaf
, extent
);
3548 datal
= btrfs_file_extent_num_bytes(leaf
,
3550 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3551 /* take upper bound, may be compressed */
3552 datal
= btrfs_file_extent_ram_bytes(leaf
,
3557 * The first search might have left us at an extent
3558 * item that ends before our target range's start, can
3559 * happen if we have holes and NO_HOLES feature enabled.
3561 if (key
.offset
+ datal
<= off
) {
3564 } else if (key
.offset
>= off
+ len
) {
3567 next_key_min_offset
= key
.offset
+ datal
;
3568 size
= btrfs_item_size_nr(leaf
, slot
);
3569 read_extent_buffer(leaf
, buf
,
3570 btrfs_item_ptr_offset(leaf
, slot
),
3573 btrfs_release_path(path
);
3574 path
->leave_spinning
= 0;
3576 memcpy(&new_key
, &key
, sizeof(new_key
));
3577 new_key
.objectid
= btrfs_ino(inode
);
3578 if (off
<= key
.offset
)
3579 new_key
.offset
= key
.offset
+ destoff
- off
;
3581 new_key
.offset
= destoff
;
3584 * Deal with a hole that doesn't have an extent item
3585 * that represents it (NO_HOLES feature enabled).
3586 * This hole is either in the middle of the cloning
3587 * range or at the beginning (fully overlaps it or
3588 * partially overlaps it).
3590 if (new_key
.offset
!= last_dest_end
)
3591 drop_start
= last_dest_end
;
3593 drop_start
= new_key
.offset
;
3596 * 1 - adjusting old extent (we may have to split it)
3597 * 1 - add new extent
3600 trans
= btrfs_start_transaction(root
, 3);
3601 if (IS_ERR(trans
)) {
3602 ret
= PTR_ERR(trans
);
3606 if (type
== BTRFS_FILE_EXTENT_REG
||
3607 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3609 * a | --- range to clone ---| b
3610 * | ------------- extent ------------- |
3613 /* subtract range b */
3614 if (key
.offset
+ datal
> off
+ len
)
3615 datal
= off
+ len
- key
.offset
;
3617 /* subtract range a */
3618 if (off
> key
.offset
) {
3619 datao
+= off
- key
.offset
;
3620 datal
-= off
- key
.offset
;
3623 ret
= btrfs_drop_extents(trans
, root
, inode
,
3625 new_key
.offset
+ datal
,
3628 if (ret
!= -EOPNOTSUPP
)
3629 btrfs_abort_transaction(trans
,
3631 btrfs_end_transaction(trans
, root
);
3635 ret
= btrfs_insert_empty_item(trans
, root
, path
,
3638 btrfs_abort_transaction(trans
, root
,
3640 btrfs_end_transaction(trans
, root
);
3644 leaf
= path
->nodes
[0];
3645 slot
= path
->slots
[0];
3646 write_extent_buffer(leaf
, buf
,
3647 btrfs_item_ptr_offset(leaf
, slot
),
3650 extent
= btrfs_item_ptr(leaf
, slot
,
3651 struct btrfs_file_extent_item
);
3653 /* disko == 0 means it's a hole */
3657 btrfs_set_file_extent_offset(leaf
, extent
,
3659 btrfs_set_file_extent_num_bytes(leaf
, extent
,
3663 inode_add_bytes(inode
, datal
);
3664 ret
= btrfs_inc_extent_ref(trans
, root
,
3666 root
->root_key
.objectid
,
3668 new_key
.offset
- datao
);
3670 btrfs_abort_transaction(trans
,
3673 btrfs_end_transaction(trans
,
3679 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3683 if (off
> key
.offset
) {
3684 skip
= off
- key
.offset
;
3685 new_key
.offset
+= skip
;
3688 if (key
.offset
+ datal
> off
+ len
)
3689 trim
= key
.offset
+ datal
- (off
+ len
);
3691 if (comp
&& (skip
|| trim
)) {
3693 btrfs_end_transaction(trans
, root
);
3696 size
-= skip
+ trim
;
3697 datal
-= skip
+ trim
;
3699 ret
= clone_copy_inline_extent(src
, inode
,
3706 if (ret
!= -EOPNOTSUPP
)
3707 btrfs_abort_transaction(trans
,
3710 btrfs_end_transaction(trans
, root
);
3713 leaf
= path
->nodes
[0];
3714 slot
= path
->slots
[0];
3717 /* If we have an implicit hole (NO_HOLES feature). */
3718 if (drop_start
< new_key
.offset
)
3719 clone_update_extent_map(inode
, trans
,
3721 new_key
.offset
- drop_start
);
3723 clone_update_extent_map(inode
, trans
, path
, 0, 0);
3725 btrfs_mark_buffer_dirty(leaf
);
3726 btrfs_release_path(path
);
3728 last_dest_end
= ALIGN(new_key
.offset
+ datal
,
3730 ret
= clone_finish_inode_update(trans
, inode
,
3736 if (new_key
.offset
+ datal
>= destoff
+ len
)
3739 btrfs_release_path(path
);
3740 key
.offset
= next_key_min_offset
;
3744 if (last_dest_end
< destoff
+ len
) {
3746 * We have an implicit hole (NO_HOLES feature is enabled) that
3747 * fully or partially overlaps our cloning range at its end.
3749 btrfs_release_path(path
);
3752 * 1 - remove extent(s)
3755 trans
= btrfs_start_transaction(root
, 2);
3756 if (IS_ERR(trans
)) {
3757 ret
= PTR_ERR(trans
);
3760 ret
= btrfs_drop_extents(trans
, root
, inode
,
3761 last_dest_end
, destoff
+ len
, 1);
3763 if (ret
!= -EOPNOTSUPP
)
3764 btrfs_abort_transaction(trans
, root
, ret
);
3765 btrfs_end_transaction(trans
, root
);
3768 clone_update_extent_map(inode
, trans
, NULL
, last_dest_end
,
3769 destoff
+ len
- last_dest_end
);
3770 ret
= clone_finish_inode_update(trans
, inode
, destoff
+ len
,
3771 destoff
, olen
, no_time_update
);
3775 btrfs_free_path(path
);
3780 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
3781 u64 off
, u64 olen
, u64 destoff
)
3783 struct inode
*inode
= file_inode(file
);
3784 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3789 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
3794 * - split compressed inline extents. annoying: we need to
3795 * decompress into destination's address_space (the file offset
3796 * may change, so source mapping won't do), then recompress (or
3797 * otherwise reinsert) a subrange.
3799 * - split destination inode's inline extents. The inline extents can
3800 * be either compressed or non-compressed.
3803 /* the destination must be opened for writing */
3804 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
3807 if (btrfs_root_readonly(root
))
3810 ret
= mnt_want_write_file(file
);
3814 src_file
= fdget(srcfd
);
3815 if (!src_file
.file
) {
3817 goto out_drop_write
;
3821 if (src_file
.file
->f_path
.mnt
!= file
->f_path
.mnt
)
3824 src
= file_inode(src_file
.file
);
3830 /* the src must be open for reading */
3831 if (!(src_file
.file
->f_mode
& FMODE_READ
))
3834 /* don't make the dst file partly checksummed */
3835 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3836 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
3840 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
3844 if (src
->i_sb
!= inode
->i_sb
)
3848 btrfs_double_inode_lock(src
, inode
);
3850 mutex_lock(&src
->i_mutex
);
3853 /* determine range to clone */
3855 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
3858 olen
= len
= src
->i_size
- off
;
3859 /* if we extend to eof, continue to block boundary */
3860 if (off
+ len
== src
->i_size
)
3861 len
= ALIGN(src
->i_size
, bs
) - off
;
3868 /* verify the end result is block aligned */
3869 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
3870 !IS_ALIGNED(destoff
, bs
))
3873 /* verify if ranges are overlapped within the same file */
3875 if (destoff
+ len
> off
&& destoff
< off
+ len
)
3879 if (destoff
> inode
->i_size
) {
3880 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
3886 * Lock the target range too. Right after we replace the file extent
3887 * items in the fs tree (which now point to the cloned data), we might
3888 * have a worker replace them with extent items relative to a write
3889 * operation that was issued before this clone operation (i.e. confront
3890 * with inode.c:btrfs_finish_ordered_io).
3893 u64 lock_start
= min_t(u64
, off
, destoff
);
3894 u64 lock_len
= max_t(u64
, off
, destoff
) + len
- lock_start
;
3896 lock_extent_range(src
, lock_start
, lock_len
);
3898 btrfs_double_extent_lock(src
, off
, inode
, destoff
, len
);
3901 ret
= btrfs_clone(src
, inode
, off
, olen
, len
, destoff
, 0);
3904 u64 lock_start
= min_t(u64
, off
, destoff
);
3905 u64 lock_end
= max_t(u64
, off
, destoff
) + len
- 1;
3907 unlock_extent(&BTRFS_I(src
)->io_tree
, lock_start
, lock_end
);
3909 btrfs_double_extent_unlock(src
, off
, inode
, destoff
, len
);
3912 * Truncate page cache pages so that future reads will see the cloned
3913 * data immediately and not the previous data.
3915 truncate_inode_pages_range(&inode
->i_data
, destoff
,
3916 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
3919 btrfs_double_inode_unlock(src
, inode
);
3921 mutex_unlock(&src
->i_mutex
);
3925 mnt_drop_write_file(file
);
3929 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
3931 struct btrfs_ioctl_clone_range_args args
;
3933 if (copy_from_user(&args
, argp
, sizeof(args
)))
3935 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
3936 args
.src_length
, args
.dest_offset
);
3940 * there are many ways the trans_start and trans_end ioctls can lead
3941 * to deadlocks. They should only be used by applications that
3942 * basically own the machine, and have a very in depth understanding
3943 * of all the possible deadlocks and enospc problems.
3945 static long btrfs_ioctl_trans_start(struct file
*file
)
3947 struct inode
*inode
= file_inode(file
);
3948 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3949 struct btrfs_trans_handle
*trans
;
3953 if (!capable(CAP_SYS_ADMIN
))
3957 if (file
->private_data
)
3961 if (btrfs_root_readonly(root
))
3964 ret
= mnt_want_write_file(file
);
3968 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
3971 trans
= btrfs_start_ioctl_transaction(root
);
3975 file
->private_data
= trans
;
3979 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
3980 mnt_drop_write_file(file
);
3985 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
3987 struct inode
*inode
= file_inode(file
);
3988 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3989 struct btrfs_root
*new_root
;
3990 struct btrfs_dir_item
*di
;
3991 struct btrfs_trans_handle
*trans
;
3992 struct btrfs_path
*path
;
3993 struct btrfs_key location
;
3994 struct btrfs_disk_key disk_key
;
3999 if (!capable(CAP_SYS_ADMIN
))
4002 ret
= mnt_want_write_file(file
);
4006 if (copy_from_user(&objectid
, argp
, sizeof(objectid
))) {
4012 objectid
= BTRFS_FS_TREE_OBJECTID
;
4014 location
.objectid
= objectid
;
4015 location
.type
= BTRFS_ROOT_ITEM_KEY
;
4016 location
.offset
= (u64
)-1;
4018 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
4019 if (IS_ERR(new_root
)) {
4020 ret
= PTR_ERR(new_root
);
4024 path
= btrfs_alloc_path();
4029 path
->leave_spinning
= 1;
4031 trans
= btrfs_start_transaction(root
, 1);
4032 if (IS_ERR(trans
)) {
4033 btrfs_free_path(path
);
4034 ret
= PTR_ERR(trans
);
4038 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
4039 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
4040 dir_id
, "default", 7, 1);
4041 if (IS_ERR_OR_NULL(di
)) {
4042 btrfs_free_path(path
);
4043 btrfs_end_transaction(trans
, root
);
4044 btrfs_err(new_root
->fs_info
, "Umm, you don't have the default dir"
4045 "item, this isn't going to work");
4050 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
4051 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
4052 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4053 btrfs_free_path(path
);
4055 btrfs_set_fs_incompat(root
->fs_info
, DEFAULT_SUBVOL
);
4056 btrfs_end_transaction(trans
, root
);
4058 mnt_drop_write_file(file
);
4062 void btrfs_get_block_group_info(struct list_head
*groups_list
,
4063 struct btrfs_ioctl_space_info
*space
)
4065 struct btrfs_block_group_cache
*block_group
;
4067 space
->total_bytes
= 0;
4068 space
->used_bytes
= 0;
4070 list_for_each_entry(block_group
, groups_list
, list
) {
4071 space
->flags
= block_group
->flags
;
4072 space
->total_bytes
+= block_group
->key
.offset
;
4073 space
->used_bytes
+=
4074 btrfs_block_group_used(&block_group
->item
);
4078 static long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
4080 struct btrfs_ioctl_space_args space_args
;
4081 struct btrfs_ioctl_space_info space
;
4082 struct btrfs_ioctl_space_info
*dest
;
4083 struct btrfs_ioctl_space_info
*dest_orig
;
4084 struct btrfs_ioctl_space_info __user
*user_dest
;
4085 struct btrfs_space_info
*info
;
4086 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
4087 BTRFS_BLOCK_GROUP_SYSTEM
,
4088 BTRFS_BLOCK_GROUP_METADATA
,
4089 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
4096 if (copy_from_user(&space_args
,
4097 (struct btrfs_ioctl_space_args __user
*)arg
,
4098 sizeof(space_args
)))
4101 for (i
= 0; i
< num_types
; i
++) {
4102 struct btrfs_space_info
*tmp
;
4106 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
4108 if (tmp
->flags
== types
[i
]) {
4118 down_read(&info
->groups_sem
);
4119 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4120 if (!list_empty(&info
->block_groups
[c
]))
4123 up_read(&info
->groups_sem
);
4127 * Global block reserve, exported as a space_info
4131 /* space_slots == 0 means they are asking for a count */
4132 if (space_args
.space_slots
== 0) {
4133 space_args
.total_spaces
= slot_count
;
4137 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
4139 alloc_size
= sizeof(*dest
) * slot_count
;
4141 /* we generally have at most 6 or so space infos, one for each raid
4142 * level. So, a whole page should be more than enough for everyone
4144 if (alloc_size
> PAGE_CACHE_SIZE
)
4147 space_args
.total_spaces
= 0;
4148 dest
= kmalloc(alloc_size
, GFP_NOFS
);
4153 /* now we have a buffer to copy into */
4154 for (i
= 0; i
< num_types
; i
++) {
4155 struct btrfs_space_info
*tmp
;
4162 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
4164 if (tmp
->flags
== types
[i
]) {
4173 down_read(&info
->groups_sem
);
4174 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
4175 if (!list_empty(&info
->block_groups
[c
])) {
4176 btrfs_get_block_group_info(
4177 &info
->block_groups
[c
], &space
);
4178 memcpy(dest
, &space
, sizeof(space
));
4180 space_args
.total_spaces
++;
4186 up_read(&info
->groups_sem
);
4190 * Add global block reserve
4193 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->global_block_rsv
;
4195 spin_lock(&block_rsv
->lock
);
4196 space
.total_bytes
= block_rsv
->size
;
4197 space
.used_bytes
= block_rsv
->size
- block_rsv
->reserved
;
4198 spin_unlock(&block_rsv
->lock
);
4199 space
.flags
= BTRFS_SPACE_INFO_GLOBAL_RSV
;
4200 memcpy(dest
, &space
, sizeof(space
));
4201 space_args
.total_spaces
++;
4204 user_dest
= (struct btrfs_ioctl_space_info __user
*)
4205 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
4207 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
4212 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
4219 * there are many ways the trans_start and trans_end ioctls can lead
4220 * to deadlocks. They should only be used by applications that
4221 * basically own the machine, and have a very in depth understanding
4222 * of all the possible deadlocks and enospc problems.
4224 long btrfs_ioctl_trans_end(struct file
*file
)
4226 struct inode
*inode
= file_inode(file
);
4227 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4228 struct btrfs_trans_handle
*trans
;
4230 trans
= file
->private_data
;
4233 file
->private_data
= NULL
;
4235 btrfs_end_transaction(trans
, root
);
4237 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
4239 mnt_drop_write_file(file
);
4243 static noinline
long btrfs_ioctl_start_sync(struct btrfs_root
*root
,
4246 struct btrfs_trans_handle
*trans
;
4250 trans
= btrfs_attach_transaction_barrier(root
);
4251 if (IS_ERR(trans
)) {
4252 if (PTR_ERR(trans
) != -ENOENT
)
4253 return PTR_ERR(trans
);
4255 /* No running transaction, don't bother */
4256 transid
= root
->fs_info
->last_trans_committed
;
4259 transid
= trans
->transid
;
4260 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
4262 btrfs_end_transaction(trans
, root
);
4267 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
4272 static noinline
long btrfs_ioctl_wait_sync(struct btrfs_root
*root
,
4278 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
4281 transid
= 0; /* current trans */
4283 return btrfs_wait_for_commit(root
, transid
);
4286 static long btrfs_ioctl_scrub(struct file
*file
, void __user
*arg
)
4288 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4289 struct btrfs_ioctl_scrub_args
*sa
;
4292 if (!capable(CAP_SYS_ADMIN
))
4295 sa
= memdup_user(arg
, sizeof(*sa
));
4299 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
)) {
4300 ret
= mnt_want_write_file(file
);
4305 ret
= btrfs_scrub_dev(root
->fs_info
, sa
->devid
, sa
->start
, sa
->end
,
4306 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
,
4309 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4312 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
))
4313 mnt_drop_write_file(file
);
4319 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
4321 if (!capable(CAP_SYS_ADMIN
))
4324 return btrfs_scrub_cancel(root
->fs_info
);
4327 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
4330 struct btrfs_ioctl_scrub_args
*sa
;
4333 if (!capable(CAP_SYS_ADMIN
))
4336 sa
= memdup_user(arg
, sizeof(*sa
));
4340 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
4342 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4349 static long btrfs_ioctl_get_dev_stats(struct btrfs_root
*root
,
4352 struct btrfs_ioctl_get_dev_stats
*sa
;
4355 sa
= memdup_user(arg
, sizeof(*sa
));
4359 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
4364 ret
= btrfs_get_dev_stats(root
, sa
);
4366 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
4373 static long btrfs_ioctl_dev_replace(struct btrfs_root
*root
, void __user
*arg
)
4375 struct btrfs_ioctl_dev_replace_args
*p
;
4378 if (!capable(CAP_SYS_ADMIN
))
4381 p
= memdup_user(arg
, sizeof(*p
));
4386 case BTRFS_IOCTL_DEV_REPLACE_CMD_START
:
4387 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
) {
4392 &root
->fs_info
->mutually_exclusive_operation_running
,
4394 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
4396 ret
= btrfs_dev_replace_start(root
, p
);
4398 &root
->fs_info
->mutually_exclusive_operation_running
,
4402 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
:
4403 btrfs_dev_replace_status(root
->fs_info
, p
);
4406 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
:
4407 ret
= btrfs_dev_replace_cancel(root
->fs_info
, p
);
4414 if (copy_to_user(arg
, p
, sizeof(*p
)))
4421 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
4427 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
4428 struct inode_fs_paths
*ipath
= NULL
;
4429 struct btrfs_path
*path
;
4431 if (!capable(CAP_DAC_READ_SEARCH
))
4434 path
= btrfs_alloc_path();
4440 ipa
= memdup_user(arg
, sizeof(*ipa
));
4447 size
= min_t(u32
, ipa
->size
, 4096);
4448 ipath
= init_ipath(size
, root
, path
);
4449 if (IS_ERR(ipath
)) {
4450 ret
= PTR_ERR(ipath
);
4455 ret
= paths_from_inode(ipa
->inum
, ipath
);
4459 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
4460 rel_ptr
= ipath
->fspath
->val
[i
] -
4461 (u64
)(unsigned long)ipath
->fspath
->val
;
4462 ipath
->fspath
->val
[i
] = rel_ptr
;
4465 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
4466 (void *)(unsigned long)ipath
->fspath
, size
);
4473 btrfs_free_path(path
);
4480 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
4482 struct btrfs_data_container
*inodes
= ctx
;
4483 const size_t c
= 3 * sizeof(u64
);
4485 if (inodes
->bytes_left
>= c
) {
4486 inodes
->bytes_left
-= c
;
4487 inodes
->val
[inodes
->elem_cnt
] = inum
;
4488 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
4489 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
4490 inodes
->elem_cnt
+= 3;
4492 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
4493 inodes
->bytes_left
= 0;
4494 inodes
->elem_missed
+= 3;
4500 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
4505 struct btrfs_ioctl_logical_ino_args
*loi
;
4506 struct btrfs_data_container
*inodes
= NULL
;
4507 struct btrfs_path
*path
= NULL
;
4509 if (!capable(CAP_SYS_ADMIN
))
4512 loi
= memdup_user(arg
, sizeof(*loi
));
4519 path
= btrfs_alloc_path();
4525 size
= min_t(u32
, loi
->size
, 64 * 1024);
4526 inodes
= init_data_container(size
);
4527 if (IS_ERR(inodes
)) {
4528 ret
= PTR_ERR(inodes
);
4533 ret
= iterate_inodes_from_logical(loi
->logical
, root
->fs_info
, path
,
4534 build_ino_list
, inodes
);
4540 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
4541 (void *)(unsigned long)inodes
, size
);
4546 btrfs_free_path(path
);
4553 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
4554 struct btrfs_ioctl_balance_args
*bargs
)
4556 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
4558 bargs
->flags
= bctl
->flags
;
4560 if (atomic_read(&fs_info
->balance_running
))
4561 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
4562 if (atomic_read(&fs_info
->balance_pause_req
))
4563 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
4564 if (atomic_read(&fs_info
->balance_cancel_req
))
4565 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
4567 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
4568 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
4569 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
4572 spin_lock(&fs_info
->balance_lock
);
4573 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
4574 spin_unlock(&fs_info
->balance_lock
);
4576 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
4580 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
4582 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4583 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4584 struct btrfs_ioctl_balance_args
*bargs
;
4585 struct btrfs_balance_control
*bctl
;
4586 bool need_unlock
; /* for mut. excl. ops lock */
4589 if (!capable(CAP_SYS_ADMIN
))
4592 ret
= mnt_want_write_file(file
);
4597 if (!atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1)) {
4598 mutex_lock(&fs_info
->volume_mutex
);
4599 mutex_lock(&fs_info
->balance_mutex
);
4605 * mut. excl. ops lock is locked. Three possibilites:
4606 * (1) some other op is running
4607 * (2) balance is running
4608 * (3) balance is paused -- special case (think resume)
4610 mutex_lock(&fs_info
->balance_mutex
);
4611 if (fs_info
->balance_ctl
) {
4612 /* this is either (2) or (3) */
4613 if (!atomic_read(&fs_info
->balance_running
)) {
4614 mutex_unlock(&fs_info
->balance_mutex
);
4615 if (!mutex_trylock(&fs_info
->volume_mutex
))
4617 mutex_lock(&fs_info
->balance_mutex
);
4619 if (fs_info
->balance_ctl
&&
4620 !atomic_read(&fs_info
->balance_running
)) {
4622 need_unlock
= false;
4626 mutex_unlock(&fs_info
->balance_mutex
);
4627 mutex_unlock(&fs_info
->volume_mutex
);
4631 mutex_unlock(&fs_info
->balance_mutex
);
4637 mutex_unlock(&fs_info
->balance_mutex
);
4638 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
4643 BUG_ON(!atomic_read(&fs_info
->mutually_exclusive_operation_running
));
4646 bargs
= memdup_user(arg
, sizeof(*bargs
));
4647 if (IS_ERR(bargs
)) {
4648 ret
= PTR_ERR(bargs
);
4652 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
4653 if (!fs_info
->balance_ctl
) {
4658 bctl
= fs_info
->balance_ctl
;
4659 spin_lock(&fs_info
->balance_lock
);
4660 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
4661 spin_unlock(&fs_info
->balance_lock
);
4669 if (fs_info
->balance_ctl
) {
4674 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
4680 bctl
->fs_info
= fs_info
;
4682 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
4683 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
4684 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
4686 bctl
->flags
= bargs
->flags
;
4688 /* balance everything - no filters */
4689 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
4692 if (bctl
->flags
& ~(BTRFS_BALANCE_ARGS_MASK
| BTRFS_BALANCE_TYPE_MASK
)) {
4699 * Ownership of bctl and mutually_exclusive_operation_running
4700 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4701 * or, if restriper was paused all the way until unmount, in
4702 * free_fs_info. mutually_exclusive_operation_running is
4703 * cleared in __cancel_balance.
4705 need_unlock
= false;
4707 ret
= btrfs_balance(bctl
, bargs
);
4711 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4720 mutex_unlock(&fs_info
->balance_mutex
);
4721 mutex_unlock(&fs_info
->volume_mutex
);
4723 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
4725 mnt_drop_write_file(file
);
4729 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
4731 if (!capable(CAP_SYS_ADMIN
))
4735 case BTRFS_BALANCE_CTL_PAUSE
:
4736 return btrfs_pause_balance(root
->fs_info
);
4737 case BTRFS_BALANCE_CTL_CANCEL
:
4738 return btrfs_cancel_balance(root
->fs_info
);
4744 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
4747 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4748 struct btrfs_ioctl_balance_args
*bargs
;
4751 if (!capable(CAP_SYS_ADMIN
))
4754 mutex_lock(&fs_info
->balance_mutex
);
4755 if (!fs_info
->balance_ctl
) {
4760 bargs
= kzalloc(sizeof(*bargs
), GFP_NOFS
);
4766 update_ioctl_balance_args(fs_info
, 1, bargs
);
4768 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4773 mutex_unlock(&fs_info
->balance_mutex
);
4777 static long btrfs_ioctl_quota_ctl(struct file
*file
, void __user
*arg
)
4779 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4780 struct btrfs_ioctl_quota_ctl_args
*sa
;
4781 struct btrfs_trans_handle
*trans
= NULL
;
4785 if (!capable(CAP_SYS_ADMIN
))
4788 ret
= mnt_want_write_file(file
);
4792 sa
= memdup_user(arg
, sizeof(*sa
));
4798 down_write(&root
->fs_info
->subvol_sem
);
4799 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 2);
4800 if (IS_ERR(trans
)) {
4801 ret
= PTR_ERR(trans
);
4806 case BTRFS_QUOTA_CTL_ENABLE
:
4807 ret
= btrfs_quota_enable(trans
, root
->fs_info
);
4809 case BTRFS_QUOTA_CTL_DISABLE
:
4810 ret
= btrfs_quota_disable(trans
, root
->fs_info
);
4817 err
= btrfs_commit_transaction(trans
, root
->fs_info
->tree_root
);
4822 up_write(&root
->fs_info
->subvol_sem
);
4824 mnt_drop_write_file(file
);
4828 static long btrfs_ioctl_qgroup_assign(struct file
*file
, void __user
*arg
)
4830 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4831 struct btrfs_ioctl_qgroup_assign_args
*sa
;
4832 struct btrfs_trans_handle
*trans
;
4836 if (!capable(CAP_SYS_ADMIN
))
4839 ret
= mnt_want_write_file(file
);
4843 sa
= memdup_user(arg
, sizeof(*sa
));
4849 trans
= btrfs_join_transaction(root
);
4850 if (IS_ERR(trans
)) {
4851 ret
= PTR_ERR(trans
);
4855 /* FIXME: check if the IDs really exist */
4857 ret
= btrfs_add_qgroup_relation(trans
, root
->fs_info
,
4860 ret
= btrfs_del_qgroup_relation(trans
, root
->fs_info
,
4864 /* update qgroup status and info */
4865 err
= btrfs_run_qgroups(trans
, root
->fs_info
);
4867 btrfs_std_error(root
->fs_info
, ret
,
4868 "failed to update qgroup status and info\n");
4869 err
= btrfs_end_transaction(trans
, root
);
4876 mnt_drop_write_file(file
);
4880 static long btrfs_ioctl_qgroup_create(struct file
*file
, void __user
*arg
)
4882 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4883 struct btrfs_ioctl_qgroup_create_args
*sa
;
4884 struct btrfs_trans_handle
*trans
;
4888 if (!capable(CAP_SYS_ADMIN
))
4891 ret
= mnt_want_write_file(file
);
4895 sa
= memdup_user(arg
, sizeof(*sa
));
4901 if (!sa
->qgroupid
) {
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_create_qgroup(trans
, root
->fs_info
, sa
->qgroupid
);
4916 ret
= btrfs_remove_qgroup(trans
, root
->fs_info
, sa
->qgroupid
);
4919 err
= btrfs_end_transaction(trans
, root
);
4926 mnt_drop_write_file(file
);
4930 static long btrfs_ioctl_qgroup_limit(struct file
*file
, void __user
*arg
)
4932 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4933 struct btrfs_ioctl_qgroup_limit_args
*sa
;
4934 struct btrfs_trans_handle
*trans
;
4939 if (!capable(CAP_SYS_ADMIN
))
4942 ret
= mnt_want_write_file(file
);
4946 sa
= memdup_user(arg
, sizeof(*sa
));
4952 trans
= btrfs_join_transaction(root
);
4953 if (IS_ERR(trans
)) {
4954 ret
= PTR_ERR(trans
);
4958 qgroupid
= sa
->qgroupid
;
4960 /* take the current subvol as qgroup */
4961 qgroupid
= root
->root_key
.objectid
;
4964 /* FIXME: check if the IDs really exist */
4965 ret
= btrfs_limit_qgroup(trans
, root
->fs_info
, qgroupid
, &sa
->lim
);
4967 err
= btrfs_end_transaction(trans
, root
);
4974 mnt_drop_write_file(file
);
4978 static long btrfs_ioctl_quota_rescan(struct file
*file
, void __user
*arg
)
4980 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4981 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4984 if (!capable(CAP_SYS_ADMIN
))
4987 ret
= mnt_want_write_file(file
);
4991 qsa
= memdup_user(arg
, sizeof(*qsa
));
5002 ret
= btrfs_qgroup_rescan(root
->fs_info
);
5007 mnt_drop_write_file(file
);
5011 static long btrfs_ioctl_quota_rescan_status(struct file
*file
, void __user
*arg
)
5013 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5014 struct btrfs_ioctl_quota_rescan_args
*qsa
;
5017 if (!capable(CAP_SYS_ADMIN
))
5020 qsa
= kzalloc(sizeof(*qsa
), GFP_NOFS
);
5024 if (root
->fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
5026 qsa
->progress
= root
->fs_info
->qgroup_rescan_progress
.objectid
;
5029 if (copy_to_user(arg
, qsa
, sizeof(*qsa
)))
5036 static long btrfs_ioctl_quota_rescan_wait(struct file
*file
, void __user
*arg
)
5038 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5040 if (!capable(CAP_SYS_ADMIN
))
5043 return btrfs_qgroup_wait_for_completion(root
->fs_info
);
5046 static long _btrfs_ioctl_set_received_subvol(struct file
*file
,
5047 struct btrfs_ioctl_received_subvol_args
*sa
)
5049 struct inode
*inode
= file_inode(file
);
5050 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5051 struct btrfs_root_item
*root_item
= &root
->root_item
;
5052 struct btrfs_trans_handle
*trans
;
5053 struct timespec ct
= CURRENT_TIME
;
5055 int received_uuid_changed
;
5057 if (!inode_owner_or_capable(inode
))
5060 ret
= mnt_want_write_file(file
);
5064 down_write(&root
->fs_info
->subvol_sem
);
5066 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
5071 if (btrfs_root_readonly(root
)) {
5078 * 2 - uuid items (received uuid + subvol uuid)
5080 trans
= btrfs_start_transaction(root
, 3);
5081 if (IS_ERR(trans
)) {
5082 ret
= PTR_ERR(trans
);
5087 sa
->rtransid
= trans
->transid
;
5088 sa
->rtime
.sec
= ct
.tv_sec
;
5089 sa
->rtime
.nsec
= ct
.tv_nsec
;
5091 received_uuid_changed
= memcmp(root_item
->received_uuid
, sa
->uuid
,
5093 if (received_uuid_changed
&&
5094 !btrfs_is_empty_uuid(root_item
->received_uuid
))
5095 btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
5096 root_item
->received_uuid
,
5097 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5098 root
->root_key
.objectid
);
5099 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
5100 btrfs_set_root_stransid(root_item
, sa
->stransid
);
5101 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
5102 btrfs_set_stack_timespec_sec(&root_item
->stime
, sa
->stime
.sec
);
5103 btrfs_set_stack_timespec_nsec(&root_item
->stime
, sa
->stime
.nsec
);
5104 btrfs_set_stack_timespec_sec(&root_item
->rtime
, sa
->rtime
.sec
);
5105 btrfs_set_stack_timespec_nsec(&root_item
->rtime
, sa
->rtime
.nsec
);
5107 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
5108 &root
->root_key
, &root
->root_item
);
5110 btrfs_end_transaction(trans
, root
);
5113 if (received_uuid_changed
&& !btrfs_is_empty_uuid(sa
->uuid
)) {
5114 ret
= btrfs_uuid_tree_add(trans
, root
->fs_info
->uuid_root
,
5116 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
5117 root
->root_key
.objectid
);
5118 if (ret
< 0 && ret
!= -EEXIST
) {
5119 btrfs_abort_transaction(trans
, root
, ret
);
5123 ret
= btrfs_commit_transaction(trans
, root
);
5125 btrfs_abort_transaction(trans
, root
, ret
);
5130 up_write(&root
->fs_info
->subvol_sem
);
5131 mnt_drop_write_file(file
);
5136 static long btrfs_ioctl_set_received_subvol_32(struct file
*file
,
5139 struct btrfs_ioctl_received_subvol_args_32
*args32
= NULL
;
5140 struct btrfs_ioctl_received_subvol_args
*args64
= NULL
;
5143 args32
= memdup_user(arg
, sizeof(*args32
));
5144 if (IS_ERR(args32
)) {
5145 ret
= PTR_ERR(args32
);
5150 args64
= kmalloc(sizeof(*args64
), GFP_NOFS
);
5156 memcpy(args64
->uuid
, args32
->uuid
, BTRFS_UUID_SIZE
);
5157 args64
->stransid
= args32
->stransid
;
5158 args64
->rtransid
= args32
->rtransid
;
5159 args64
->stime
.sec
= args32
->stime
.sec
;
5160 args64
->stime
.nsec
= args32
->stime
.nsec
;
5161 args64
->rtime
.sec
= args32
->rtime
.sec
;
5162 args64
->rtime
.nsec
= args32
->rtime
.nsec
;
5163 args64
->flags
= args32
->flags
;
5165 ret
= _btrfs_ioctl_set_received_subvol(file
, args64
);
5169 memcpy(args32
->uuid
, args64
->uuid
, BTRFS_UUID_SIZE
);
5170 args32
->stransid
= args64
->stransid
;
5171 args32
->rtransid
= args64
->rtransid
;
5172 args32
->stime
.sec
= args64
->stime
.sec
;
5173 args32
->stime
.nsec
= args64
->stime
.nsec
;
5174 args32
->rtime
.sec
= args64
->rtime
.sec
;
5175 args32
->rtime
.nsec
= args64
->rtime
.nsec
;
5176 args32
->flags
= args64
->flags
;
5178 ret
= copy_to_user(arg
, args32
, sizeof(*args32
));
5189 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
5192 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
5195 sa
= memdup_user(arg
, sizeof(*sa
));
5202 ret
= _btrfs_ioctl_set_received_subvol(file
, sa
);
5207 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
5216 static int btrfs_ioctl_get_fslabel(struct file
*file
, void __user
*arg
)
5218 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5221 char label
[BTRFS_LABEL_SIZE
];
5223 spin_lock(&root
->fs_info
->super_lock
);
5224 memcpy(label
, root
->fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
5225 spin_unlock(&root
->fs_info
->super_lock
);
5227 len
= strnlen(label
, BTRFS_LABEL_SIZE
);
5229 if (len
== BTRFS_LABEL_SIZE
) {
5230 btrfs_warn(root
->fs_info
,
5231 "label is too long, return the first %zu bytes", --len
);
5234 ret
= copy_to_user(arg
, label
, len
);
5236 return ret
? -EFAULT
: 0;
5239 static int btrfs_ioctl_set_fslabel(struct file
*file
, void __user
*arg
)
5241 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5242 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
5243 struct btrfs_trans_handle
*trans
;
5244 char label
[BTRFS_LABEL_SIZE
];
5247 if (!capable(CAP_SYS_ADMIN
))
5250 if (copy_from_user(label
, arg
, sizeof(label
)))
5253 if (strnlen(label
, BTRFS_LABEL_SIZE
) == BTRFS_LABEL_SIZE
) {
5254 btrfs_err(root
->fs_info
, "unable to set label with more than %d bytes",
5255 BTRFS_LABEL_SIZE
- 1);
5259 ret
= mnt_want_write_file(file
);
5263 trans
= btrfs_start_transaction(root
, 0);
5264 if (IS_ERR(trans
)) {
5265 ret
= PTR_ERR(trans
);
5269 spin_lock(&root
->fs_info
->super_lock
);
5270 strcpy(super_block
->label
, label
);
5271 spin_unlock(&root
->fs_info
->super_lock
);
5272 ret
= btrfs_commit_transaction(trans
, root
);
5275 mnt_drop_write_file(file
);
5279 #define INIT_FEATURE_FLAGS(suffix) \
5280 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5281 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5282 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5284 static int btrfs_ioctl_get_supported_features(struct file
*file
,
5287 static const struct btrfs_ioctl_feature_flags features
[3] = {
5288 INIT_FEATURE_FLAGS(SUPP
),
5289 INIT_FEATURE_FLAGS(SAFE_SET
),
5290 INIT_FEATURE_FLAGS(SAFE_CLEAR
)
5293 if (copy_to_user(arg
, &features
, sizeof(features
)))
5299 static int btrfs_ioctl_get_features(struct file
*file
, void __user
*arg
)
5301 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5302 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
5303 struct btrfs_ioctl_feature_flags features
;
5305 features
.compat_flags
= btrfs_super_compat_flags(super_block
);
5306 features
.compat_ro_flags
= btrfs_super_compat_ro_flags(super_block
);
5307 features
.incompat_flags
= btrfs_super_incompat_flags(super_block
);
5309 if (copy_to_user(arg
, &features
, sizeof(features
)))
5315 static int check_feature_bits(struct btrfs_root
*root
,
5316 enum btrfs_feature_set set
,
5317 u64 change_mask
, u64 flags
, u64 supported_flags
,
5318 u64 safe_set
, u64 safe_clear
)
5320 const char *type
= btrfs_feature_set_names
[set
];
5322 u64 disallowed
, unsupported
;
5323 u64 set_mask
= flags
& change_mask
;
5324 u64 clear_mask
= ~flags
& change_mask
;
5326 unsupported
= set_mask
& ~supported_flags
;
5328 names
= btrfs_printable_features(set
, unsupported
);
5330 btrfs_warn(root
->fs_info
,
5331 "this kernel does not support the %s feature bit%s",
5332 names
, strchr(names
, ',') ? "s" : "");
5335 btrfs_warn(root
->fs_info
,
5336 "this kernel does not support %s bits 0x%llx",
5341 disallowed
= set_mask
& ~safe_set
;
5343 names
= btrfs_printable_features(set
, disallowed
);
5345 btrfs_warn(root
->fs_info
,
5346 "can't set the %s feature bit%s while mounted",
5347 names
, strchr(names
, ',') ? "s" : "");
5350 btrfs_warn(root
->fs_info
,
5351 "can't set %s bits 0x%llx while mounted",
5356 disallowed
= clear_mask
& ~safe_clear
;
5358 names
= btrfs_printable_features(set
, disallowed
);
5360 btrfs_warn(root
->fs_info
,
5361 "can't clear the %s feature bit%s while mounted",
5362 names
, strchr(names
, ',') ? "s" : "");
5365 btrfs_warn(root
->fs_info
,
5366 "can't clear %s bits 0x%llx while mounted",
5374 #define check_feature(root, change_mask, flags, mask_base) \
5375 check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
5376 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5377 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5378 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5380 static int btrfs_ioctl_set_features(struct file
*file
, void __user
*arg
)
5382 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5383 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
5384 struct btrfs_ioctl_feature_flags flags
[2];
5385 struct btrfs_trans_handle
*trans
;
5389 if (!capable(CAP_SYS_ADMIN
))
5392 if (copy_from_user(flags
, arg
, sizeof(flags
)))
5396 if (!flags
[0].compat_flags
&& !flags
[0].compat_ro_flags
&&
5397 !flags
[0].incompat_flags
)
5400 ret
= check_feature(root
, flags
[0].compat_flags
,
5401 flags
[1].compat_flags
, COMPAT
);
5405 ret
= check_feature(root
, flags
[0].compat_ro_flags
,
5406 flags
[1].compat_ro_flags
, COMPAT_RO
);
5410 ret
= check_feature(root
, flags
[0].incompat_flags
,
5411 flags
[1].incompat_flags
, INCOMPAT
);
5415 trans
= btrfs_start_transaction(root
, 0);
5417 return PTR_ERR(trans
);
5419 spin_lock(&root
->fs_info
->super_lock
);
5420 newflags
= btrfs_super_compat_flags(super_block
);
5421 newflags
|= flags
[0].compat_flags
& flags
[1].compat_flags
;
5422 newflags
&= ~(flags
[0].compat_flags
& ~flags
[1].compat_flags
);
5423 btrfs_set_super_compat_flags(super_block
, newflags
);
5425 newflags
= btrfs_super_compat_ro_flags(super_block
);
5426 newflags
|= flags
[0].compat_ro_flags
& flags
[1].compat_ro_flags
;
5427 newflags
&= ~(flags
[0].compat_ro_flags
& ~flags
[1].compat_ro_flags
);
5428 btrfs_set_super_compat_ro_flags(super_block
, newflags
);
5430 newflags
= btrfs_super_incompat_flags(super_block
);
5431 newflags
|= flags
[0].incompat_flags
& flags
[1].incompat_flags
;
5432 newflags
&= ~(flags
[0].incompat_flags
& ~flags
[1].incompat_flags
);
5433 btrfs_set_super_incompat_flags(super_block
, newflags
);
5434 spin_unlock(&root
->fs_info
->super_lock
);
5436 return btrfs_commit_transaction(trans
, root
);
5439 long btrfs_ioctl(struct file
*file
, unsigned int
5440 cmd
, unsigned long arg
)
5442 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
5443 void __user
*argp
= (void __user
*)arg
;
5446 case FS_IOC_GETFLAGS
:
5447 return btrfs_ioctl_getflags(file
, argp
);
5448 case FS_IOC_SETFLAGS
:
5449 return btrfs_ioctl_setflags(file
, argp
);
5450 case FS_IOC_GETVERSION
:
5451 return btrfs_ioctl_getversion(file
, argp
);
5453 return btrfs_ioctl_fitrim(file
, argp
);
5454 case BTRFS_IOC_SNAP_CREATE
:
5455 return btrfs_ioctl_snap_create(file
, argp
, 0);
5456 case BTRFS_IOC_SNAP_CREATE_V2
:
5457 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
5458 case BTRFS_IOC_SUBVOL_CREATE
:
5459 return btrfs_ioctl_snap_create(file
, argp
, 1);
5460 case BTRFS_IOC_SUBVOL_CREATE_V2
:
5461 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
5462 case BTRFS_IOC_SNAP_DESTROY
:
5463 return btrfs_ioctl_snap_destroy(file
, argp
);
5464 case BTRFS_IOC_SUBVOL_GETFLAGS
:
5465 return btrfs_ioctl_subvol_getflags(file
, argp
);
5466 case BTRFS_IOC_SUBVOL_SETFLAGS
:
5467 return btrfs_ioctl_subvol_setflags(file
, argp
);
5468 case BTRFS_IOC_DEFAULT_SUBVOL
:
5469 return btrfs_ioctl_default_subvol(file
, argp
);
5470 case BTRFS_IOC_DEFRAG
:
5471 return btrfs_ioctl_defrag(file
, NULL
);
5472 case BTRFS_IOC_DEFRAG_RANGE
:
5473 return btrfs_ioctl_defrag(file
, argp
);
5474 case BTRFS_IOC_RESIZE
:
5475 return btrfs_ioctl_resize(file
, argp
);
5476 case BTRFS_IOC_ADD_DEV
:
5477 return btrfs_ioctl_add_dev(root
, argp
);
5478 case BTRFS_IOC_RM_DEV
:
5479 return btrfs_ioctl_rm_dev(file
, argp
);
5480 case BTRFS_IOC_FS_INFO
:
5481 return btrfs_ioctl_fs_info(root
, argp
);
5482 case BTRFS_IOC_DEV_INFO
:
5483 return btrfs_ioctl_dev_info(root
, argp
);
5484 case BTRFS_IOC_BALANCE
:
5485 return btrfs_ioctl_balance(file
, NULL
);
5486 case BTRFS_IOC_CLONE
:
5487 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
5488 case BTRFS_IOC_CLONE_RANGE
:
5489 return btrfs_ioctl_clone_range(file
, argp
);
5490 case BTRFS_IOC_TRANS_START
:
5491 return btrfs_ioctl_trans_start(file
);
5492 case BTRFS_IOC_TRANS_END
:
5493 return btrfs_ioctl_trans_end(file
);
5494 case BTRFS_IOC_TREE_SEARCH
:
5495 return btrfs_ioctl_tree_search(file
, argp
);
5496 case BTRFS_IOC_TREE_SEARCH_V2
:
5497 return btrfs_ioctl_tree_search_v2(file
, argp
);
5498 case BTRFS_IOC_INO_LOOKUP
:
5499 return btrfs_ioctl_ino_lookup(file
, argp
);
5500 case BTRFS_IOC_INO_PATHS
:
5501 return btrfs_ioctl_ino_to_path(root
, argp
);
5502 case BTRFS_IOC_LOGICAL_INO
:
5503 return btrfs_ioctl_logical_to_ino(root
, argp
);
5504 case BTRFS_IOC_SPACE_INFO
:
5505 return btrfs_ioctl_space_info(root
, argp
);
5506 case BTRFS_IOC_SYNC
: {
5509 ret
= btrfs_start_delalloc_roots(root
->fs_info
, 0, -1);
5512 ret
= btrfs_sync_fs(file_inode(file
)->i_sb
, 1);
5514 * The transaction thread may want to do more work,
5515 * namely it pokes the cleaner ktread that will start
5516 * processing uncleaned subvols.
5518 wake_up_process(root
->fs_info
->transaction_kthread
);
5521 case BTRFS_IOC_START_SYNC
:
5522 return btrfs_ioctl_start_sync(root
, argp
);
5523 case BTRFS_IOC_WAIT_SYNC
:
5524 return btrfs_ioctl_wait_sync(root
, argp
);
5525 case BTRFS_IOC_SCRUB
:
5526 return btrfs_ioctl_scrub(file
, argp
);
5527 case BTRFS_IOC_SCRUB_CANCEL
:
5528 return btrfs_ioctl_scrub_cancel(root
, argp
);
5529 case BTRFS_IOC_SCRUB_PROGRESS
:
5530 return btrfs_ioctl_scrub_progress(root
, argp
);
5531 case BTRFS_IOC_BALANCE_V2
:
5532 return btrfs_ioctl_balance(file
, argp
);
5533 case BTRFS_IOC_BALANCE_CTL
:
5534 return btrfs_ioctl_balance_ctl(root
, arg
);
5535 case BTRFS_IOC_BALANCE_PROGRESS
:
5536 return btrfs_ioctl_balance_progress(root
, argp
);
5537 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
5538 return btrfs_ioctl_set_received_subvol(file
, argp
);
5540 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32
:
5541 return btrfs_ioctl_set_received_subvol_32(file
, argp
);
5543 case BTRFS_IOC_SEND
:
5544 return btrfs_ioctl_send(file
, argp
);
5545 case BTRFS_IOC_GET_DEV_STATS
:
5546 return btrfs_ioctl_get_dev_stats(root
, argp
);
5547 case BTRFS_IOC_QUOTA_CTL
:
5548 return btrfs_ioctl_quota_ctl(file
, argp
);
5549 case BTRFS_IOC_QGROUP_ASSIGN
:
5550 return btrfs_ioctl_qgroup_assign(file
, argp
);
5551 case BTRFS_IOC_QGROUP_CREATE
:
5552 return btrfs_ioctl_qgroup_create(file
, argp
);
5553 case BTRFS_IOC_QGROUP_LIMIT
:
5554 return btrfs_ioctl_qgroup_limit(file
, argp
);
5555 case BTRFS_IOC_QUOTA_RESCAN
:
5556 return btrfs_ioctl_quota_rescan(file
, argp
);
5557 case BTRFS_IOC_QUOTA_RESCAN_STATUS
:
5558 return btrfs_ioctl_quota_rescan_status(file
, argp
);
5559 case BTRFS_IOC_QUOTA_RESCAN_WAIT
:
5560 return btrfs_ioctl_quota_rescan_wait(file
, argp
);
5561 case BTRFS_IOC_DEV_REPLACE
:
5562 return btrfs_ioctl_dev_replace(root
, argp
);
5563 case BTRFS_IOC_GET_FSLABEL
:
5564 return btrfs_ioctl_get_fslabel(file
, argp
);
5565 case BTRFS_IOC_SET_FSLABEL
:
5566 return btrfs_ioctl_set_fslabel(file
, argp
);
5567 case BTRFS_IOC_FILE_EXTENT_SAME
:
5568 return btrfs_ioctl_file_extent_same(file
, argp
);
5569 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
5570 return btrfs_ioctl_get_supported_features(file
, argp
);
5571 case BTRFS_IOC_GET_FEATURES
:
5572 return btrfs_ioctl_get_features(file
, argp
);
5573 case BTRFS_IOC_SET_FEATURES
:
5574 return btrfs_ioctl_set_features(file
, argp
);