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"
62 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
63 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
);
65 /* Mask out flags that are inappropriate for the given type of inode. */
66 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
70 else if (S_ISREG(mode
))
71 return flags
& ~FS_DIRSYNC_FL
;
73 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
77 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
79 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
81 unsigned int iflags
= 0;
83 if (flags
& BTRFS_INODE_SYNC
)
85 if (flags
& BTRFS_INODE_IMMUTABLE
)
86 iflags
|= FS_IMMUTABLE_FL
;
87 if (flags
& BTRFS_INODE_APPEND
)
88 iflags
|= FS_APPEND_FL
;
89 if (flags
& BTRFS_INODE_NODUMP
)
90 iflags
|= FS_NODUMP_FL
;
91 if (flags
& BTRFS_INODE_NOATIME
)
92 iflags
|= FS_NOATIME_FL
;
93 if (flags
& BTRFS_INODE_DIRSYNC
)
94 iflags
|= FS_DIRSYNC_FL
;
95 if (flags
& BTRFS_INODE_NODATACOW
)
96 iflags
|= FS_NOCOW_FL
;
98 if ((flags
& BTRFS_INODE_COMPRESS
) && !(flags
& BTRFS_INODE_NOCOMPRESS
))
99 iflags
|= FS_COMPR_FL
;
100 else if (flags
& BTRFS_INODE_NOCOMPRESS
)
101 iflags
|= FS_NOCOMP_FL
;
107 * Update inode->i_flags based on the btrfs internal flags.
109 void btrfs_update_iflags(struct inode
*inode
)
111 struct btrfs_inode
*ip
= BTRFS_I(inode
);
113 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
115 if (ip
->flags
& BTRFS_INODE_SYNC
)
116 inode
->i_flags
|= S_SYNC
;
117 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
118 inode
->i_flags
|= S_IMMUTABLE
;
119 if (ip
->flags
& BTRFS_INODE_APPEND
)
120 inode
->i_flags
|= S_APPEND
;
121 if (ip
->flags
& BTRFS_INODE_NOATIME
)
122 inode
->i_flags
|= S_NOATIME
;
123 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
124 inode
->i_flags
|= S_DIRSYNC
;
128 * Inherit flags from the parent inode.
130 * Currently only the compression flags and the cow flags are inherited.
132 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
139 flags
= BTRFS_I(dir
)->flags
;
141 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
142 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
143 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
144 } else if (flags
& BTRFS_INODE_COMPRESS
) {
145 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
146 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
149 if (flags
& BTRFS_INODE_NODATACOW
) {
150 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
151 if (S_ISREG(inode
->i_mode
))
152 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
155 btrfs_update_iflags(inode
);
158 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
160 struct btrfs_inode
*ip
= BTRFS_I(file_inode(file
));
161 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
163 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
168 static int check_flags(unsigned int flags
)
170 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
171 FS_NOATIME_FL
| FS_NODUMP_FL
| \
172 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
173 FS_NOCOMP_FL
| FS_COMPR_FL
|
177 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
183 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
185 struct inode
*inode
= file_inode(file
);
186 struct btrfs_inode
*ip
= BTRFS_I(inode
);
187 struct btrfs_root
*root
= ip
->root
;
188 struct btrfs_trans_handle
*trans
;
189 unsigned int flags
, oldflags
;
192 unsigned int i_oldflags
;
195 if (btrfs_root_readonly(root
))
198 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
201 ret
= check_flags(flags
);
205 if (!inode_owner_or_capable(inode
))
208 ret
= mnt_want_write_file(file
);
212 mutex_lock(&inode
->i_mutex
);
214 ip_oldflags
= ip
->flags
;
215 i_oldflags
= inode
->i_flags
;
216 mode
= inode
->i_mode
;
218 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
219 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
220 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
221 if (!capable(CAP_LINUX_IMMUTABLE
)) {
227 if (flags
& FS_SYNC_FL
)
228 ip
->flags
|= BTRFS_INODE_SYNC
;
230 ip
->flags
&= ~BTRFS_INODE_SYNC
;
231 if (flags
& FS_IMMUTABLE_FL
)
232 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
234 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
235 if (flags
& FS_APPEND_FL
)
236 ip
->flags
|= BTRFS_INODE_APPEND
;
238 ip
->flags
&= ~BTRFS_INODE_APPEND
;
239 if (flags
& FS_NODUMP_FL
)
240 ip
->flags
|= BTRFS_INODE_NODUMP
;
242 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
243 if (flags
& FS_NOATIME_FL
)
244 ip
->flags
|= BTRFS_INODE_NOATIME
;
246 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
247 if (flags
& FS_DIRSYNC_FL
)
248 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
250 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
251 if (flags
& FS_NOCOW_FL
) {
254 * It's safe to turn csums off here, no extents exist.
255 * Otherwise we want the flag to reflect the real COW
256 * status of the file and will not set it.
258 if (inode
->i_size
== 0)
259 ip
->flags
|= BTRFS_INODE_NODATACOW
260 | BTRFS_INODE_NODATASUM
;
262 ip
->flags
|= BTRFS_INODE_NODATACOW
;
266 * Revert back under same assuptions as above
269 if (inode
->i_size
== 0)
270 ip
->flags
&= ~(BTRFS_INODE_NODATACOW
271 | BTRFS_INODE_NODATASUM
);
273 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
278 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
279 * flag may be changed automatically if compression code won't make
282 if (flags
& FS_NOCOMP_FL
) {
283 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
284 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
286 ret
= btrfs_set_prop(inode
, "btrfs.compression", NULL
, 0, 0);
287 if (ret
&& ret
!= -ENODATA
)
289 } else if (flags
& FS_COMPR_FL
) {
292 ip
->flags
|= BTRFS_INODE_COMPRESS
;
293 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
295 if (root
->fs_info
->compress_type
== BTRFS_COMPRESS_LZO
)
299 ret
= btrfs_set_prop(inode
, "btrfs.compression",
300 comp
, strlen(comp
), 0);
305 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
308 trans
= btrfs_start_transaction(root
, 1);
310 ret
= PTR_ERR(trans
);
314 btrfs_update_iflags(inode
);
315 inode_inc_iversion(inode
);
316 inode
->i_ctime
= CURRENT_TIME
;
317 ret
= btrfs_update_inode(trans
, root
, inode
);
319 btrfs_end_transaction(trans
, root
);
322 ip
->flags
= ip_oldflags
;
323 inode
->i_flags
= i_oldflags
;
327 mutex_unlock(&inode
->i_mutex
);
328 mnt_drop_write_file(file
);
332 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
334 struct inode
*inode
= file_inode(file
);
336 return put_user(inode
->i_generation
, arg
);
339 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
341 struct btrfs_fs_info
*fs_info
= btrfs_sb(file_inode(file
)->i_sb
);
342 struct btrfs_device
*device
;
343 struct request_queue
*q
;
344 struct fstrim_range range
;
345 u64 minlen
= ULLONG_MAX
;
347 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
350 if (!capable(CAP_SYS_ADMIN
))
354 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
358 q
= bdev_get_queue(device
->bdev
);
359 if (blk_queue_discard(q
)) {
361 minlen
= min((u64
)q
->limits
.discard_granularity
,
369 if (copy_from_user(&range
, arg
, sizeof(range
)))
371 if (range
.start
> total_bytes
||
372 range
.len
< fs_info
->sb
->s_blocksize
)
375 range
.len
= min(range
.len
, total_bytes
- range
.start
);
376 range
.minlen
= max(range
.minlen
, minlen
);
377 ret
= btrfs_trim_fs(fs_info
->tree_root
, &range
);
381 if (copy_to_user(arg
, &range
, sizeof(range
)))
387 int btrfs_is_empty_uuid(u8
*uuid
)
391 for (i
= 0; i
< BTRFS_UUID_SIZE
; i
++) {
398 static noinline
int create_subvol(struct inode
*dir
,
399 struct dentry
*dentry
,
400 char *name
, int namelen
,
402 struct btrfs_qgroup_inherit
*inherit
)
404 struct btrfs_trans_handle
*trans
;
405 struct btrfs_key key
;
406 struct btrfs_root_item root_item
;
407 struct btrfs_inode_item
*inode_item
;
408 struct extent_buffer
*leaf
;
409 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
410 struct btrfs_root
*new_root
;
411 struct btrfs_block_rsv block_rsv
;
412 struct timespec cur_time
= CURRENT_TIME
;
417 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
422 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
426 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
428 * The same as the snapshot creation, please see the comment
429 * of create_snapshot().
431 ret
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
432 8, &qgroup_reserved
, false);
436 trans
= btrfs_start_transaction(root
, 0);
438 ret
= PTR_ERR(trans
);
441 trans
->block_rsv
= &block_rsv
;
442 trans
->bytes_reserved
= block_rsv
.size
;
444 ret
= btrfs_qgroup_inherit(trans
, root
->fs_info
, 0, objectid
, inherit
);
448 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
449 0, objectid
, NULL
, 0, 0, 0);
455 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
456 btrfs_set_header_bytenr(leaf
, leaf
->start
);
457 btrfs_set_header_generation(leaf
, trans
->transid
);
458 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
459 btrfs_set_header_owner(leaf
, objectid
);
461 write_extent_buffer(leaf
, root
->fs_info
->fsid
, btrfs_header_fsid(),
463 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
464 btrfs_header_chunk_tree_uuid(leaf
),
466 btrfs_mark_buffer_dirty(leaf
);
468 memset(&root_item
, 0, sizeof(root_item
));
470 inode_item
= &root_item
.inode
;
471 btrfs_set_stack_inode_generation(inode_item
, 1);
472 btrfs_set_stack_inode_size(inode_item
, 3);
473 btrfs_set_stack_inode_nlink(inode_item
, 1);
474 btrfs_set_stack_inode_nbytes(inode_item
, root
->leafsize
);
475 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
477 btrfs_set_root_flags(&root_item
, 0);
478 btrfs_set_root_limit(&root_item
, 0);
479 btrfs_set_stack_inode_flags(inode_item
, BTRFS_INODE_ROOT_ITEM_INIT
);
481 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
482 btrfs_set_root_generation(&root_item
, trans
->transid
);
483 btrfs_set_root_level(&root_item
, 0);
484 btrfs_set_root_refs(&root_item
, 1);
485 btrfs_set_root_used(&root_item
, leaf
->len
);
486 btrfs_set_root_last_snapshot(&root_item
, 0);
488 btrfs_set_root_generation_v2(&root_item
,
489 btrfs_root_generation(&root_item
));
490 uuid_le_gen(&new_uuid
);
491 memcpy(root_item
.uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
492 btrfs_set_stack_timespec_sec(&root_item
.otime
, cur_time
.tv_sec
);
493 btrfs_set_stack_timespec_nsec(&root_item
.otime
, cur_time
.tv_nsec
);
494 root_item
.ctime
= root_item
.otime
;
495 btrfs_set_root_ctransid(&root_item
, trans
->transid
);
496 btrfs_set_root_otransid(&root_item
, trans
->transid
);
498 btrfs_tree_unlock(leaf
);
499 free_extent_buffer(leaf
);
502 btrfs_set_root_dirid(&root_item
, new_dirid
);
504 key
.objectid
= objectid
;
506 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
507 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
512 key
.offset
= (u64
)-1;
513 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
514 if (IS_ERR(new_root
)) {
515 btrfs_abort_transaction(trans
, root
, PTR_ERR(new_root
));
516 ret
= PTR_ERR(new_root
);
520 btrfs_record_root_in_trans(trans
, new_root
);
522 ret
= btrfs_create_subvol_root(trans
, new_root
, root
, new_dirid
);
524 /* We potentially lose an unused inode item here */
525 btrfs_abort_transaction(trans
, root
, ret
);
530 * insert the directory item
532 ret
= btrfs_set_inode_index(dir
, &index
);
534 btrfs_abort_transaction(trans
, root
, ret
);
538 ret
= btrfs_insert_dir_item(trans
, root
,
539 name
, namelen
, dir
, &key
,
540 BTRFS_FT_DIR
, index
);
542 btrfs_abort_transaction(trans
, root
, ret
);
546 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
547 ret
= btrfs_update_inode(trans
, root
, dir
);
550 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
551 objectid
, root
->root_key
.objectid
,
552 btrfs_ino(dir
), index
, name
, namelen
);
555 ret
= btrfs_uuid_tree_add(trans
, root
->fs_info
->uuid_root
,
556 root_item
.uuid
, BTRFS_UUID_KEY_SUBVOL
,
559 btrfs_abort_transaction(trans
, root
, ret
);
562 trans
->block_rsv
= NULL
;
563 trans
->bytes_reserved
= 0;
565 *async_transid
= trans
->transid
;
566 err
= btrfs_commit_transaction_async(trans
, root
, 1);
568 err
= btrfs_commit_transaction(trans
, root
);
570 err
= btrfs_commit_transaction(trans
, root
);
576 inode
= btrfs_lookup_dentry(dir
, dentry
);
578 ret
= PTR_ERR(inode
);
581 d_instantiate(dentry
, inode
);
584 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
588 static int create_snapshot(struct btrfs_root
*root
, struct inode
*dir
,
589 struct dentry
*dentry
, char *name
, int namelen
,
590 u64
*async_transid
, bool readonly
,
591 struct btrfs_qgroup_inherit
*inherit
)
594 struct btrfs_pending_snapshot
*pending_snapshot
;
595 struct btrfs_trans_handle
*trans
;
601 ret
= btrfs_start_delalloc_inodes(root
, 0);
605 btrfs_wait_ordered_extents(root
, -1);
607 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
608 if (!pending_snapshot
)
611 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
612 BTRFS_BLOCK_RSV_TEMP
);
614 * 1 - parent dir inode
617 * 2 - root ref/backref
618 * 1 - root of snapshot
621 ret
= btrfs_subvolume_reserve_metadata(BTRFS_I(dir
)->root
,
622 &pending_snapshot
->block_rsv
, 8,
623 &pending_snapshot
->qgroup_reserved
,
628 pending_snapshot
->dentry
= dentry
;
629 pending_snapshot
->root
= root
;
630 pending_snapshot
->readonly
= readonly
;
631 pending_snapshot
->dir
= dir
;
632 pending_snapshot
->inherit
= inherit
;
634 trans
= btrfs_start_transaction(root
, 0);
636 ret
= PTR_ERR(trans
);
640 spin_lock(&root
->fs_info
->trans_lock
);
641 list_add(&pending_snapshot
->list
,
642 &trans
->transaction
->pending_snapshots
);
643 spin_unlock(&root
->fs_info
->trans_lock
);
645 *async_transid
= trans
->transid
;
646 ret
= btrfs_commit_transaction_async(trans
,
647 root
->fs_info
->extent_root
, 1);
649 ret
= btrfs_commit_transaction(trans
, root
);
651 ret
= btrfs_commit_transaction(trans
,
652 root
->fs_info
->extent_root
);
657 ret
= pending_snapshot
->error
;
661 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
665 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
667 ret
= PTR_ERR(inode
);
671 d_instantiate(dentry
, inode
);
674 btrfs_subvolume_release_metadata(BTRFS_I(dir
)->root
,
675 &pending_snapshot
->block_rsv
,
676 pending_snapshot
->qgroup_reserved
);
678 kfree(pending_snapshot
);
682 /* copy of check_sticky in fs/namei.c()
683 * It's inline, so penalty for filesystems that don't use sticky bit is
686 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
688 kuid_t fsuid
= current_fsuid();
690 if (!(dir
->i_mode
& S_ISVTX
))
692 if (uid_eq(inode
->i_uid
, fsuid
))
694 if (uid_eq(dir
->i_uid
, fsuid
))
696 return !capable(CAP_FOWNER
);
699 /* copy of may_delete in fs/namei.c()
700 * Check whether we can remove a link victim from directory dir, check
701 * whether the type of victim is right.
702 * 1. We can't do it if dir is read-only (done in permission())
703 * 2. We should have write and exec permissions on dir
704 * 3. We can't remove anything from append-only dir
705 * 4. We can't do anything with immutable dir (done in permission())
706 * 5. If the sticky bit on dir is set we should either
707 * a. be owner of dir, or
708 * b. be owner of victim, or
709 * c. have CAP_FOWNER capability
710 * 6. If the victim is append-only or immutable we can't do antyhing with
711 * links pointing to it.
712 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
713 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
714 * 9. We can't remove a root or mountpoint.
715 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
716 * nfs_async_unlink().
719 static int btrfs_may_delete(struct inode
*dir
, struct dentry
*victim
, int isdir
)
723 if (!victim
->d_inode
)
726 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
727 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
729 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
734 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
735 IS_APPEND(victim
->d_inode
)||
736 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
739 if (!S_ISDIR(victim
->d_inode
->i_mode
))
743 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
747 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
752 /* copy of may_create in fs/namei.c() */
753 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
759 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
763 * Create a new subvolume below @parent. This is largely modeled after
764 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
765 * inside this filesystem so it's quite a bit simpler.
767 static noinline
int btrfs_mksubvol(struct path
*parent
,
768 char *name
, int namelen
,
769 struct btrfs_root
*snap_src
,
770 u64
*async_transid
, bool readonly
,
771 struct btrfs_qgroup_inherit
*inherit
)
773 struct inode
*dir
= parent
->dentry
->d_inode
;
774 struct dentry
*dentry
;
777 error
= mutex_lock_killable_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
781 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
782 error
= PTR_ERR(dentry
);
790 error
= btrfs_may_create(dir
, dentry
);
795 * even if this name doesn't exist, we may get hash collisions.
796 * check for them now when we can safely fail
798 error
= btrfs_check_dir_item_collision(BTRFS_I(dir
)->root
,
804 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
806 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
810 error
= create_snapshot(snap_src
, dir
, dentry
, name
, namelen
,
811 async_transid
, readonly
, inherit
);
813 error
= create_subvol(dir
, dentry
, name
, namelen
,
814 async_transid
, inherit
);
817 fsnotify_mkdir(dir
, dentry
);
819 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
823 mutex_unlock(&dir
->i_mutex
);
828 * When we're defragging a range, we don't want to kick it off again
829 * if it is really just waiting for delalloc to send it down.
830 * If we find a nice big extent or delalloc range for the bytes in the
831 * file you want to defrag, we return 0 to let you know to skip this
834 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, int thresh
)
836 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
837 struct extent_map
*em
= NULL
;
838 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
841 read_lock(&em_tree
->lock
);
842 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
843 read_unlock(&em_tree
->lock
);
846 end
= extent_map_end(em
);
848 if (end
- offset
> thresh
)
851 /* if we already have a nice delalloc here, just stop */
853 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
854 thresh
, EXTENT_DELALLOC
, 1);
861 * helper function to walk through a file and find extents
862 * newer than a specific transid, and smaller than thresh.
864 * This is used by the defragging code to find new and small
867 static int find_new_extents(struct btrfs_root
*root
,
868 struct inode
*inode
, u64 newer_than
,
869 u64
*off
, int thresh
)
871 struct btrfs_path
*path
;
872 struct btrfs_key min_key
;
873 struct extent_buffer
*leaf
;
874 struct btrfs_file_extent_item
*extent
;
877 u64 ino
= btrfs_ino(inode
);
879 path
= btrfs_alloc_path();
883 min_key
.objectid
= ino
;
884 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
885 min_key
.offset
= *off
;
887 path
->keep_locks
= 1;
890 ret
= btrfs_search_forward(root
, &min_key
, path
, newer_than
);
893 if (min_key
.objectid
!= ino
)
895 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
898 leaf
= path
->nodes
[0];
899 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
900 struct btrfs_file_extent_item
);
902 type
= btrfs_file_extent_type(leaf
, extent
);
903 if (type
== BTRFS_FILE_EXTENT_REG
&&
904 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
905 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
906 *off
= min_key
.offset
;
907 btrfs_free_path(path
);
911 if (min_key
.offset
== (u64
)-1)
915 btrfs_release_path(path
);
918 btrfs_free_path(path
);
922 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
924 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
925 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
926 struct extent_map
*em
;
927 u64 len
= PAGE_CACHE_SIZE
;
930 * hopefully we have this extent in the tree already, try without
931 * the full extent lock
933 read_lock(&em_tree
->lock
);
934 em
= lookup_extent_mapping(em_tree
, start
, len
);
935 read_unlock(&em_tree
->lock
);
938 /* get the big lock and read metadata off disk */
939 lock_extent(io_tree
, start
, start
+ len
- 1);
940 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
941 unlock_extent(io_tree
, start
, start
+ len
- 1);
950 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
952 struct extent_map
*next
;
955 /* this is the last extent */
956 if (em
->start
+ em
->len
>= i_size_read(inode
))
959 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
960 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
963 free_extent_map(next
);
967 static int should_defrag_range(struct inode
*inode
, u64 start
, int thresh
,
968 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
971 struct extent_map
*em
;
973 bool next_mergeable
= true;
976 * make sure that once we start defragging an extent, we keep on
979 if (start
< *defrag_end
)
984 em
= defrag_lookup_extent(inode
, start
);
988 /* this will cover holes, and inline extents */
989 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
994 next_mergeable
= defrag_check_next_extent(inode
, em
);
997 * we hit a real extent, if it is big or the next extent is not a
998 * real extent, don't bother defragging it
1000 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
1001 (em
->len
>= thresh
|| !next_mergeable
))
1005 * last_len ends up being a counter of how many bytes we've defragged.
1006 * every time we choose not to defrag an extent, we reset *last_len
1007 * so that the next tiny extent will force a defrag.
1009 * The end result of this is that tiny extents before a single big
1010 * extent will force at least part of that big extent to be defragged.
1013 *defrag_end
= extent_map_end(em
);
1016 *skip
= extent_map_end(em
);
1020 free_extent_map(em
);
1025 * it doesn't do much good to defrag one or two pages
1026 * at a time. This pulls in a nice chunk of pages
1027 * to COW and defrag.
1029 * It also makes sure the delalloc code has enough
1030 * dirty data to avoid making new small extents as part
1033 * It's a good idea to start RA on this range
1034 * before calling this.
1036 static int cluster_pages_for_defrag(struct inode
*inode
,
1037 struct page
**pages
,
1038 unsigned long start_index
,
1041 unsigned long file_end
;
1042 u64 isize
= i_size_read(inode
);
1049 struct btrfs_ordered_extent
*ordered
;
1050 struct extent_state
*cached_state
= NULL
;
1051 struct extent_io_tree
*tree
;
1052 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
1054 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1055 if (!isize
|| start_index
> file_end
)
1058 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
1060 ret
= btrfs_delalloc_reserve_space(inode
,
1061 page_cnt
<< PAGE_CACHE_SHIFT
);
1065 tree
= &BTRFS_I(inode
)->io_tree
;
1067 /* step one, lock all the pages */
1068 for (i
= 0; i
< page_cnt
; i
++) {
1071 page
= find_or_create_page(inode
->i_mapping
,
1072 start_index
+ i
, mask
);
1076 page_start
= page_offset(page
);
1077 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1079 lock_extent(tree
, page_start
, page_end
);
1080 ordered
= btrfs_lookup_ordered_extent(inode
,
1082 unlock_extent(tree
, page_start
, page_end
);
1087 btrfs_start_ordered_extent(inode
, ordered
, 1);
1088 btrfs_put_ordered_extent(ordered
);
1091 * we unlocked the page above, so we need check if
1092 * it was released or not.
1094 if (page
->mapping
!= inode
->i_mapping
) {
1096 page_cache_release(page
);
1101 if (!PageUptodate(page
)) {
1102 btrfs_readpage(NULL
, page
);
1104 if (!PageUptodate(page
)) {
1106 page_cache_release(page
);
1112 if (page
->mapping
!= inode
->i_mapping
) {
1114 page_cache_release(page
);
1124 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1128 * so now we have a nice long stream of locked
1129 * and up to date pages, lets wait on them
1131 for (i
= 0; i
< i_done
; i
++)
1132 wait_on_page_writeback(pages
[i
]);
1134 page_start
= page_offset(pages
[0]);
1135 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
1137 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1138 page_start
, page_end
- 1, 0, &cached_state
);
1139 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1140 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1141 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 0, 0,
1142 &cached_state
, GFP_NOFS
);
1144 if (i_done
!= page_cnt
) {
1145 spin_lock(&BTRFS_I(inode
)->lock
);
1146 BTRFS_I(inode
)->outstanding_extents
++;
1147 spin_unlock(&BTRFS_I(inode
)->lock
);
1148 btrfs_delalloc_release_space(inode
,
1149 (page_cnt
- i_done
) << PAGE_CACHE_SHIFT
);
1153 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1154 &cached_state
, GFP_NOFS
);
1156 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1157 page_start
, page_end
- 1, &cached_state
,
1160 for (i
= 0; i
< i_done
; i
++) {
1161 clear_page_dirty_for_io(pages
[i
]);
1162 ClearPageChecked(pages
[i
]);
1163 set_page_extent_mapped(pages
[i
]);
1164 set_page_dirty(pages
[i
]);
1165 unlock_page(pages
[i
]);
1166 page_cache_release(pages
[i
]);
1170 for (i
= 0; i
< i_done
; i
++) {
1171 unlock_page(pages
[i
]);
1172 page_cache_release(pages
[i
]);
1174 btrfs_delalloc_release_space(inode
, page_cnt
<< PAGE_CACHE_SHIFT
);
1179 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1180 struct btrfs_ioctl_defrag_range_args
*range
,
1181 u64 newer_than
, unsigned long max_to_defrag
)
1183 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1184 struct file_ra_state
*ra
= NULL
;
1185 unsigned long last_index
;
1186 u64 isize
= i_size_read(inode
);
1190 u64 newer_off
= range
->start
;
1192 unsigned long ra_index
= 0;
1194 int defrag_count
= 0;
1195 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1196 int extent_thresh
= range
->extent_thresh
;
1197 int max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1198 int cluster
= max_cluster
;
1199 u64 new_align
= ~((u64
)128 * 1024 - 1);
1200 struct page
**pages
= NULL
;
1205 if (range
->start
>= isize
)
1208 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1209 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1211 if (range
->compress_type
)
1212 compress_type
= range
->compress_type
;
1215 if (extent_thresh
== 0)
1216 extent_thresh
= 256 * 1024;
1219 * if we were not given a file, allocate a readahead
1223 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1226 file_ra_state_init(ra
, inode
->i_mapping
);
1231 pages
= kmalloc_array(max_cluster
, sizeof(struct page
*),
1238 /* find the last page to defrag */
1239 if (range
->start
+ range
->len
> range
->start
) {
1240 last_index
= min_t(u64
, isize
- 1,
1241 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1243 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1247 ret
= find_new_extents(root
, inode
, newer_than
,
1248 &newer_off
, 64 * 1024);
1250 range
->start
= newer_off
;
1252 * we always align our defrag to help keep
1253 * the extents in the file evenly spaced
1255 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1259 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1262 max_to_defrag
= last_index
+ 1;
1265 * make writeback starts from i, so the defrag range can be
1266 * written sequentially.
1268 if (i
< inode
->i_mapping
->writeback_index
)
1269 inode
->i_mapping
->writeback_index
= i
;
1271 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1272 (i
< (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
1273 PAGE_CACHE_SHIFT
)) {
1275 * make sure we stop running if someone unmounts
1278 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1281 if (btrfs_defrag_cancelled(root
->fs_info
)) {
1282 printk(KERN_DEBUG
"BTRFS: defrag_file cancelled\n");
1287 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1288 extent_thresh
, &last_len
, &skip
,
1289 &defrag_end
, range
->flags
&
1290 BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1293 * the should_defrag function tells us how much to skip
1294 * bump our counter by the suggested amount
1296 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1297 i
= max(i
+ 1, next
);
1302 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1303 PAGE_CACHE_SHIFT
) - i
;
1304 cluster
= min(cluster
, max_cluster
);
1306 cluster
= max_cluster
;
1309 if (i
+ cluster
> ra_index
) {
1310 ra_index
= max(i
, ra_index
);
1311 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1313 ra_index
+= max_cluster
;
1316 mutex_lock(&inode
->i_mutex
);
1317 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1318 BTRFS_I(inode
)->force_compress
= compress_type
;
1319 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1321 mutex_unlock(&inode
->i_mutex
);
1325 defrag_count
+= ret
;
1326 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1327 mutex_unlock(&inode
->i_mutex
);
1330 if (newer_off
== (u64
)-1)
1336 newer_off
= max(newer_off
+ 1,
1337 (u64
)i
<< PAGE_CACHE_SHIFT
);
1339 ret
= find_new_extents(root
, inode
,
1340 newer_than
, &newer_off
,
1343 range
->start
= newer_off
;
1344 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1351 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1359 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
1360 filemap_flush(inode
->i_mapping
);
1362 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1363 /* the filemap_flush will queue IO into the worker threads, but
1364 * we have to make sure the IO is actually started and that
1365 * ordered extents get created before we return
1367 atomic_inc(&root
->fs_info
->async_submit_draining
);
1368 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1369 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1370 wait_event(root
->fs_info
->async_submit_wait
,
1371 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1372 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1374 atomic_dec(&root
->fs_info
->async_submit_draining
);
1377 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1378 btrfs_set_fs_incompat(root
->fs_info
, COMPRESS_LZO
);
1384 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1385 mutex_lock(&inode
->i_mutex
);
1386 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1387 mutex_unlock(&inode
->i_mutex
);
1395 static noinline
int btrfs_ioctl_resize(struct file
*file
,
1401 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
1402 struct btrfs_ioctl_vol_args
*vol_args
;
1403 struct btrfs_trans_handle
*trans
;
1404 struct btrfs_device
*device
= NULL
;
1406 char *devstr
= NULL
;
1410 if (!capable(CAP_SYS_ADMIN
))
1413 ret
= mnt_want_write_file(file
);
1417 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
1419 mnt_drop_write_file(file
);
1420 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
1423 mutex_lock(&root
->fs_info
->volume_mutex
);
1424 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1425 if (IS_ERR(vol_args
)) {
1426 ret
= PTR_ERR(vol_args
);
1430 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1432 sizestr
= vol_args
->name
;
1433 devstr
= strchr(sizestr
, ':');
1436 sizestr
= devstr
+ 1;
1438 devstr
= vol_args
->name
;
1439 devid
= simple_strtoull(devstr
, &end
, 10);
1444 btrfs_info(root
->fs_info
, "resizing devid %llu", devid
);
1447 device
= btrfs_find_device(root
->fs_info
, devid
, NULL
, NULL
);
1449 btrfs_info(root
->fs_info
, "resizer unable to find device %llu",
1455 if (!device
->writeable
) {
1456 btrfs_info(root
->fs_info
,
1457 "resizer unable to apply on readonly device %llu",
1463 if (!strcmp(sizestr
, "max"))
1464 new_size
= device
->bdev
->bd_inode
->i_size
;
1466 if (sizestr
[0] == '-') {
1469 } else if (sizestr
[0] == '+') {
1473 new_size
= memparse(sizestr
, NULL
);
1474 if (new_size
== 0) {
1480 if (device
->is_tgtdev_for_dev_replace
) {
1485 old_size
= device
->total_bytes
;
1488 if (new_size
> old_size
) {
1492 new_size
= old_size
- new_size
;
1493 } else if (mod
> 0) {
1494 if (new_size
> ULLONG_MAX
- old_size
) {
1498 new_size
= old_size
+ new_size
;
1501 if (new_size
< 256 * 1024 * 1024) {
1505 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1510 do_div(new_size
, root
->sectorsize
);
1511 new_size
*= root
->sectorsize
;
1513 printk_in_rcu(KERN_INFO
"BTRFS: new size for %s is %llu\n",
1514 rcu_str_deref(device
->name
), new_size
);
1516 if (new_size
> old_size
) {
1517 trans
= btrfs_start_transaction(root
, 0);
1518 if (IS_ERR(trans
)) {
1519 ret
= PTR_ERR(trans
);
1522 ret
= btrfs_grow_device(trans
, device
, new_size
);
1523 btrfs_commit_transaction(trans
, root
);
1524 } else if (new_size
< old_size
) {
1525 ret
= btrfs_shrink_device(device
, new_size
);
1526 } /* equal, nothing need to do */
1531 mutex_unlock(&root
->fs_info
->volume_mutex
);
1532 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
1533 mnt_drop_write_file(file
);
1537 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1538 char *name
, unsigned long fd
, int subvol
,
1539 u64
*transid
, bool readonly
,
1540 struct btrfs_qgroup_inherit
*inherit
)
1545 ret
= mnt_want_write_file(file
);
1549 namelen
= strlen(name
);
1550 if (strchr(name
, '/')) {
1552 goto out_drop_write
;
1555 if (name
[0] == '.' &&
1556 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1558 goto out_drop_write
;
1562 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1563 NULL
, transid
, readonly
, inherit
);
1565 struct fd src
= fdget(fd
);
1566 struct inode
*src_inode
;
1569 goto out_drop_write
;
1572 src_inode
= file_inode(src
.file
);
1573 if (src_inode
->i_sb
!= file_inode(file
)->i_sb
) {
1574 btrfs_info(BTRFS_I(src_inode
)->root
->fs_info
,
1575 "Snapshot src from another FS");
1578 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1579 BTRFS_I(src_inode
)->root
,
1580 transid
, readonly
, inherit
);
1585 mnt_drop_write_file(file
);
1590 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1591 void __user
*arg
, int subvol
)
1593 struct btrfs_ioctl_vol_args
*vol_args
;
1596 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1597 if (IS_ERR(vol_args
))
1598 return PTR_ERR(vol_args
);
1599 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1601 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1602 vol_args
->fd
, subvol
,
1609 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1610 void __user
*arg
, int subvol
)
1612 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1616 bool readonly
= false;
1617 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1619 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1620 if (IS_ERR(vol_args
))
1621 return PTR_ERR(vol_args
);
1622 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1624 if (vol_args
->flags
&
1625 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1626 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1631 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1633 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1635 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1636 if (vol_args
->size
> PAGE_CACHE_SIZE
) {
1640 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1641 if (IS_ERR(inherit
)) {
1642 ret
= PTR_ERR(inherit
);
1647 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1648 vol_args
->fd
, subvol
, ptr
,
1651 if (ret
== 0 && ptr
&&
1653 offsetof(struct btrfs_ioctl_vol_args_v2
,
1654 transid
), ptr
, sizeof(*ptr
)))
1662 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1665 struct inode
*inode
= file_inode(file
);
1666 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1670 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1673 down_read(&root
->fs_info
->subvol_sem
);
1674 if (btrfs_root_readonly(root
))
1675 flags
|= BTRFS_SUBVOL_RDONLY
;
1676 up_read(&root
->fs_info
->subvol_sem
);
1678 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1684 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1687 struct inode
*inode
= file_inode(file
);
1688 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1689 struct btrfs_trans_handle
*trans
;
1694 ret
= mnt_want_write_file(file
);
1698 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1700 goto out_drop_write
;
1703 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1705 goto out_drop_write
;
1708 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1710 goto out_drop_write
;
1713 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1715 goto out_drop_write
;
1718 if (!inode_owner_or_capable(inode
)) {
1720 goto out_drop_write
;
1723 down_write(&root
->fs_info
->subvol_sem
);
1726 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1729 root_flags
= btrfs_root_flags(&root
->root_item
);
1730 if (flags
& BTRFS_SUBVOL_RDONLY
) {
1731 btrfs_set_root_flags(&root
->root_item
,
1732 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1735 * Block RO -> RW transition if this subvolume is involved in
1738 spin_lock(&root
->root_item_lock
);
1739 if (root
->send_in_progress
== 0) {
1740 btrfs_set_root_flags(&root
->root_item
,
1741 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1742 spin_unlock(&root
->root_item_lock
);
1744 spin_unlock(&root
->root_item_lock
);
1745 btrfs_warn(root
->fs_info
,
1746 "Attempt to set subvolume %llu read-write during send",
1747 root
->root_key
.objectid
);
1753 trans
= btrfs_start_transaction(root
, 1);
1754 if (IS_ERR(trans
)) {
1755 ret
= PTR_ERR(trans
);
1759 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1760 &root
->root_key
, &root
->root_item
);
1762 btrfs_commit_transaction(trans
, root
);
1765 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1767 up_write(&root
->fs_info
->subvol_sem
);
1769 mnt_drop_write_file(file
);
1775 * helper to check if the subvolume references other subvolumes
1777 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1779 struct btrfs_path
*path
;
1780 struct btrfs_dir_item
*di
;
1781 struct btrfs_key key
;
1785 path
= btrfs_alloc_path();
1789 /* Make sure this root isn't set as the default subvol */
1790 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
1791 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, path
,
1792 dir_id
, "default", 7, 0);
1793 if (di
&& !IS_ERR(di
)) {
1794 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1795 if (key
.objectid
== root
->root_key
.objectid
) {
1799 btrfs_release_path(path
);
1802 key
.objectid
= root
->root_key
.objectid
;
1803 key
.type
= BTRFS_ROOT_REF_KEY
;
1804 key
.offset
= (u64
)-1;
1806 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1813 if (path
->slots
[0] > 0) {
1815 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1816 if (key
.objectid
== root
->root_key
.objectid
&&
1817 key
.type
== BTRFS_ROOT_REF_KEY
)
1821 btrfs_free_path(path
);
1825 static noinline
int key_in_sk(struct btrfs_key
*key
,
1826 struct btrfs_ioctl_search_key
*sk
)
1828 struct btrfs_key test
;
1831 test
.objectid
= sk
->min_objectid
;
1832 test
.type
= sk
->min_type
;
1833 test
.offset
= sk
->min_offset
;
1835 ret
= btrfs_comp_cpu_keys(key
, &test
);
1839 test
.objectid
= sk
->max_objectid
;
1840 test
.type
= sk
->max_type
;
1841 test
.offset
= sk
->max_offset
;
1843 ret
= btrfs_comp_cpu_keys(key
, &test
);
1849 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1850 struct btrfs_path
*path
,
1851 struct btrfs_key
*key
,
1852 struct btrfs_ioctl_search_key
*sk
,
1854 unsigned long *sk_offset
,
1858 struct extent_buffer
*leaf
;
1859 struct btrfs_ioctl_search_header sh
;
1860 unsigned long item_off
;
1861 unsigned long item_len
;
1867 leaf
= path
->nodes
[0];
1868 slot
= path
->slots
[0];
1869 nritems
= btrfs_header_nritems(leaf
);
1871 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1875 found_transid
= btrfs_header_generation(leaf
);
1877 for (i
= slot
; i
< nritems
; i
++) {
1878 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1879 item_len
= btrfs_item_size_nr(leaf
, i
);
1881 btrfs_item_key_to_cpu(leaf
, key
, i
);
1882 if (!key_in_sk(key
, sk
))
1885 if (sizeof(sh
) + item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1888 if (sizeof(sh
) + item_len
+ *sk_offset
>
1889 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1894 sh
.objectid
= key
->objectid
;
1895 sh
.offset
= key
->offset
;
1896 sh
.type
= key
->type
;
1898 sh
.transid
= found_transid
;
1900 /* copy search result header */
1901 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1902 *sk_offset
+= sizeof(sh
);
1905 char *p
= buf
+ *sk_offset
;
1907 read_extent_buffer(leaf
, p
,
1908 item_off
, item_len
);
1909 *sk_offset
+= item_len
;
1913 if (*num_found
>= sk
->nr_items
)
1918 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1920 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1923 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1933 static noinline
int search_ioctl(struct inode
*inode
,
1934 struct btrfs_ioctl_search_args
*args
)
1936 struct btrfs_root
*root
;
1937 struct btrfs_key key
;
1938 struct btrfs_path
*path
;
1939 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1940 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1943 unsigned long sk_offset
= 0;
1945 path
= btrfs_alloc_path();
1949 if (sk
->tree_id
== 0) {
1950 /* search the root of the inode that was passed */
1951 root
= BTRFS_I(inode
)->root
;
1953 key
.objectid
= sk
->tree_id
;
1954 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1955 key
.offset
= (u64
)-1;
1956 root
= btrfs_read_fs_root_no_name(info
, &key
);
1958 printk(KERN_ERR
"BTRFS: could not find root %llu\n",
1960 btrfs_free_path(path
);
1965 key
.objectid
= sk
->min_objectid
;
1966 key
.type
= sk
->min_type
;
1967 key
.offset
= sk
->min_offset
;
1969 path
->keep_locks
= 1;
1972 ret
= btrfs_search_forward(root
, &key
, path
, sk
->min_transid
);
1978 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1979 &sk_offset
, &num_found
);
1980 btrfs_release_path(path
);
1981 if (ret
|| num_found
>= sk
->nr_items
)
1987 sk
->nr_items
= num_found
;
1988 btrfs_free_path(path
);
1992 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1995 struct btrfs_ioctl_search_args
*args
;
1996 struct inode
*inode
;
1999 if (!capable(CAP_SYS_ADMIN
))
2002 args
= memdup_user(argp
, sizeof(*args
));
2004 return PTR_ERR(args
);
2006 inode
= file_inode(file
);
2007 ret
= search_ioctl(inode
, args
);
2008 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2015 * Search INODE_REFs to identify path name of 'dirid' directory
2016 * in a 'tree_id' tree. and sets path name to 'name'.
2018 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
2019 u64 tree_id
, u64 dirid
, char *name
)
2021 struct btrfs_root
*root
;
2022 struct btrfs_key key
;
2028 struct btrfs_inode_ref
*iref
;
2029 struct extent_buffer
*l
;
2030 struct btrfs_path
*path
;
2032 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
2037 path
= btrfs_alloc_path();
2041 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
2043 key
.objectid
= tree_id
;
2044 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2045 key
.offset
= (u64
)-1;
2046 root
= btrfs_read_fs_root_no_name(info
, &key
);
2048 printk(KERN_ERR
"BTRFS: could not find root %llu\n", tree_id
);
2053 key
.objectid
= dirid
;
2054 key
.type
= BTRFS_INODE_REF_KEY
;
2055 key
.offset
= (u64
)-1;
2058 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2062 ret
= btrfs_previous_item(root
, path
, dirid
,
2063 BTRFS_INODE_REF_KEY
);
2073 slot
= path
->slots
[0];
2074 btrfs_item_key_to_cpu(l
, &key
, slot
);
2076 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
2077 len
= btrfs_inode_ref_name_len(l
, iref
);
2079 total_len
+= len
+ 1;
2081 ret
= -ENAMETOOLONG
;
2086 read_extent_buffer(l
, ptr
, (unsigned long)(iref
+ 1), len
);
2088 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
2091 btrfs_release_path(path
);
2092 key
.objectid
= key
.offset
;
2093 key
.offset
= (u64
)-1;
2094 dirid
= key
.objectid
;
2096 memmove(name
, ptr
, total_len
);
2097 name
[total_len
] = '\0';
2100 btrfs_free_path(path
);
2104 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
2107 struct btrfs_ioctl_ino_lookup_args
*args
;
2108 struct inode
*inode
;
2111 if (!capable(CAP_SYS_ADMIN
))
2114 args
= memdup_user(argp
, sizeof(*args
));
2116 return PTR_ERR(args
);
2118 inode
= file_inode(file
);
2120 if (args
->treeid
== 0)
2121 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2123 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
2124 args
->treeid
, args
->objectid
,
2127 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2134 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
2137 struct dentry
*parent
= file
->f_path
.dentry
;
2138 struct dentry
*dentry
;
2139 struct inode
*dir
= parent
->d_inode
;
2140 struct inode
*inode
;
2141 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2142 struct btrfs_root
*dest
= NULL
;
2143 struct btrfs_ioctl_vol_args
*vol_args
;
2144 struct btrfs_trans_handle
*trans
;
2145 struct btrfs_block_rsv block_rsv
;
2146 u64 qgroup_reserved
;
2151 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2152 if (IS_ERR(vol_args
))
2153 return PTR_ERR(vol_args
);
2155 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2156 namelen
= strlen(vol_args
->name
);
2157 if (strchr(vol_args
->name
, '/') ||
2158 strncmp(vol_args
->name
, "..", namelen
) == 0) {
2163 err
= mnt_want_write_file(file
);
2167 err
= mutex_lock_killable_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
2169 goto out_drop_write
;
2170 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
2171 if (IS_ERR(dentry
)) {
2172 err
= PTR_ERR(dentry
);
2173 goto out_unlock_dir
;
2176 if (!dentry
->d_inode
) {
2181 inode
= dentry
->d_inode
;
2182 dest
= BTRFS_I(inode
)->root
;
2183 if (!capable(CAP_SYS_ADMIN
)) {
2185 * Regular user. Only allow this with a special mount
2186 * option, when the user has write+exec access to the
2187 * subvol root, and when rmdir(2) would have been
2190 * Note that this is _not_ check that the subvol is
2191 * empty or doesn't contain data that we wouldn't
2192 * otherwise be able to delete.
2194 * Users who want to delete empty subvols should try
2198 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
2202 * Do not allow deletion if the parent dir is the same
2203 * as the dir to be deleted. That means the ioctl
2204 * must be called on the dentry referencing the root
2205 * of the subvol, not a random directory contained
2212 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
2217 /* check if subvolume may be deleted by a user */
2218 err
= btrfs_may_delete(dir
, dentry
, 1);
2222 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
2227 mutex_lock(&inode
->i_mutex
);
2228 err
= d_invalidate(dentry
);
2232 down_write(&root
->fs_info
->subvol_sem
);
2234 err
= may_destroy_subvol(dest
);
2238 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
2240 * One for dir inode, two for dir entries, two for root
2243 err
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
2244 5, &qgroup_reserved
, true);
2248 trans
= btrfs_start_transaction(root
, 0);
2249 if (IS_ERR(trans
)) {
2250 err
= PTR_ERR(trans
);
2253 trans
->block_rsv
= &block_rsv
;
2254 trans
->bytes_reserved
= block_rsv
.size
;
2256 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
2257 dest
->root_key
.objectid
,
2258 dentry
->d_name
.name
,
2259 dentry
->d_name
.len
);
2262 btrfs_abort_transaction(trans
, root
, ret
);
2266 btrfs_record_root_in_trans(trans
, dest
);
2268 memset(&dest
->root_item
.drop_progress
, 0,
2269 sizeof(dest
->root_item
.drop_progress
));
2270 dest
->root_item
.drop_level
= 0;
2271 btrfs_set_root_refs(&dest
->root_item
, 0);
2273 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
2274 ret
= btrfs_insert_orphan_item(trans
,
2275 root
->fs_info
->tree_root
,
2276 dest
->root_key
.objectid
);
2278 btrfs_abort_transaction(trans
, root
, ret
);
2284 ret
= btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
2285 dest
->root_item
.uuid
, BTRFS_UUID_KEY_SUBVOL
,
2286 dest
->root_key
.objectid
);
2287 if (ret
&& ret
!= -ENOENT
) {
2288 btrfs_abort_transaction(trans
, root
, ret
);
2292 if (!btrfs_is_empty_uuid(dest
->root_item
.received_uuid
)) {
2293 ret
= btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
2294 dest
->root_item
.received_uuid
,
2295 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
2296 dest
->root_key
.objectid
);
2297 if (ret
&& ret
!= -ENOENT
) {
2298 btrfs_abort_transaction(trans
, root
, ret
);
2305 trans
->block_rsv
= NULL
;
2306 trans
->bytes_reserved
= 0;
2307 ret
= btrfs_end_transaction(trans
, root
);
2310 inode
->i_flags
|= S_DEAD
;
2312 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
2314 up_write(&root
->fs_info
->subvol_sem
);
2316 mutex_unlock(&inode
->i_mutex
);
2318 shrink_dcache_sb(root
->fs_info
->sb
);
2319 btrfs_invalidate_inodes(dest
);
2323 if (dest
->cache_inode
) {
2324 iput(dest
->cache_inode
);
2325 dest
->cache_inode
= NULL
;
2331 mutex_unlock(&dir
->i_mutex
);
2333 mnt_drop_write_file(file
);
2339 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2341 struct inode
*inode
= file_inode(file
);
2342 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2343 struct btrfs_ioctl_defrag_range_args
*range
;
2346 ret
= mnt_want_write_file(file
);
2350 if (btrfs_root_readonly(root
)) {
2355 switch (inode
->i_mode
& S_IFMT
) {
2357 if (!capable(CAP_SYS_ADMIN
)) {
2361 ret
= btrfs_defrag_root(root
);
2364 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
);
2367 if (!(file
->f_mode
& FMODE_WRITE
)) {
2372 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2379 if (copy_from_user(range
, argp
,
2385 /* compression requires us to start the IO */
2386 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2387 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2388 range
->extent_thresh
= (u32
)-1;
2391 /* the rest are all set to zero by kzalloc */
2392 range
->len
= (u64
)-1;
2394 ret
= btrfs_defrag_file(file_inode(file
), file
,
2404 mnt_drop_write_file(file
);
2408 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2410 struct btrfs_ioctl_vol_args
*vol_args
;
2413 if (!capable(CAP_SYS_ADMIN
))
2416 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2418 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2421 mutex_lock(&root
->fs_info
->volume_mutex
);
2422 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2423 if (IS_ERR(vol_args
)) {
2424 ret
= PTR_ERR(vol_args
);
2428 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2429 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2433 mutex_unlock(&root
->fs_info
->volume_mutex
);
2434 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2438 static long btrfs_ioctl_rm_dev(struct file
*file
, void __user
*arg
)
2440 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
2441 struct btrfs_ioctl_vol_args
*vol_args
;
2444 if (!capable(CAP_SYS_ADMIN
))
2447 ret
= mnt_want_write_file(file
);
2451 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2452 if (IS_ERR(vol_args
)) {
2453 ret
= PTR_ERR(vol_args
);
2457 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2459 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2461 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2465 mutex_lock(&root
->fs_info
->volume_mutex
);
2466 ret
= btrfs_rm_device(root
, vol_args
->name
);
2467 mutex_unlock(&root
->fs_info
->volume_mutex
);
2468 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2472 mnt_drop_write_file(file
);
2476 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2478 struct btrfs_ioctl_fs_info_args
*fi_args
;
2479 struct btrfs_device
*device
;
2480 struct btrfs_device
*next
;
2481 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2484 if (!capable(CAP_SYS_ADMIN
))
2487 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2491 mutex_lock(&fs_devices
->device_list_mutex
);
2492 fi_args
->num_devices
= fs_devices
->num_devices
;
2493 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2495 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
2496 if (device
->devid
> fi_args
->max_id
)
2497 fi_args
->max_id
= device
->devid
;
2499 mutex_unlock(&fs_devices
->device_list_mutex
);
2501 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2508 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2510 struct btrfs_ioctl_dev_info_args
*di_args
;
2511 struct btrfs_device
*dev
;
2512 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2514 char *s_uuid
= NULL
;
2516 if (!capable(CAP_SYS_ADMIN
))
2519 di_args
= memdup_user(arg
, sizeof(*di_args
));
2520 if (IS_ERR(di_args
))
2521 return PTR_ERR(di_args
);
2523 if (!btrfs_is_empty_uuid(di_args
->uuid
))
2524 s_uuid
= di_args
->uuid
;
2526 mutex_lock(&fs_devices
->device_list_mutex
);
2527 dev
= btrfs_find_device(root
->fs_info
, di_args
->devid
, s_uuid
, NULL
);
2534 di_args
->devid
= dev
->devid
;
2535 di_args
->bytes_used
= dev
->bytes_used
;
2536 di_args
->total_bytes
= dev
->total_bytes
;
2537 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2539 struct rcu_string
*name
;
2542 name
= rcu_dereference(dev
->name
);
2543 strncpy(di_args
->path
, name
->str
, sizeof(di_args
->path
));
2545 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
2547 di_args
->path
[0] = '\0';
2551 mutex_unlock(&fs_devices
->device_list_mutex
);
2552 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2559 static struct page
*extent_same_get_page(struct inode
*inode
, u64 off
)
2563 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2565 index
= off
>> PAGE_CACHE_SHIFT
;
2567 page
= grab_cache_page(inode
->i_mapping
, index
);
2571 if (!PageUptodate(page
)) {
2572 if (extent_read_full_page_nolock(tree
, page
, btrfs_get_extent
,
2576 if (!PageUptodate(page
)) {
2578 page_cache_release(page
);
2587 static inline void lock_extent_range(struct inode
*inode
, u64 off
, u64 len
)
2589 /* do any pending delalloc/csum calc on src, one way or
2590 another, and lock file content */
2592 struct btrfs_ordered_extent
*ordered
;
2593 lock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2594 ordered
= btrfs_lookup_first_ordered_extent(inode
,
2597 !test_range_bit(&BTRFS_I(inode
)->io_tree
, off
,
2598 off
+ len
- 1, EXTENT_DELALLOC
, 0, NULL
))
2600 unlock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2602 btrfs_put_ordered_extent(ordered
);
2603 btrfs_wait_ordered_range(inode
, off
, len
);
2607 static void btrfs_double_unlock(struct inode
*inode1
, u64 loff1
,
2608 struct inode
*inode2
, u64 loff2
, u64 len
)
2610 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
2611 unlock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
2613 mutex_unlock(&inode1
->i_mutex
);
2614 mutex_unlock(&inode2
->i_mutex
);
2617 static void btrfs_double_lock(struct inode
*inode1
, u64 loff1
,
2618 struct inode
*inode2
, u64 loff2
, u64 len
)
2620 if (inode1
< inode2
) {
2621 swap(inode1
, inode2
);
2625 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_PARENT
);
2626 lock_extent_range(inode1
, loff1
, len
);
2627 if (inode1
!= inode2
) {
2628 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_CHILD
);
2629 lock_extent_range(inode2
, loff2
, len
);
2633 static int btrfs_cmp_data(struct inode
*src
, u64 loff
, struct inode
*dst
,
2634 u64 dst_loff
, u64 len
)
2637 struct page
*src_page
, *dst_page
;
2638 unsigned int cmp_len
= PAGE_CACHE_SIZE
;
2639 void *addr
, *dst_addr
;
2642 if (len
< PAGE_CACHE_SIZE
)
2645 src_page
= extent_same_get_page(src
, loff
);
2648 dst_page
= extent_same_get_page(dst
, dst_loff
);
2650 page_cache_release(src_page
);
2653 addr
= kmap_atomic(src_page
);
2654 dst_addr
= kmap_atomic(dst_page
);
2656 flush_dcache_page(src_page
);
2657 flush_dcache_page(dst_page
);
2659 if (memcmp(addr
, dst_addr
, cmp_len
))
2660 ret
= BTRFS_SAME_DATA_DIFFERS
;
2662 kunmap_atomic(addr
);
2663 kunmap_atomic(dst_addr
);
2664 page_cache_release(src_page
);
2665 page_cache_release(dst_page
);
2671 dst_loff
+= cmp_len
;
2678 static int extent_same_check_offsets(struct inode
*inode
, u64 off
, u64 len
)
2680 u64 bs
= BTRFS_I(inode
)->root
->fs_info
->sb
->s_blocksize
;
2682 if (off
+ len
> inode
->i_size
|| off
+ len
< off
)
2684 /* Check that we are block aligned - btrfs_clone() requires this */
2685 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
))
2691 static int btrfs_extent_same(struct inode
*src
, u64 loff
, u64 len
,
2692 struct inode
*dst
, u64 dst_loff
)
2697 * btrfs_clone() can't handle extents in the same file
2698 * yet. Once that works, we can drop this check and replace it
2699 * with a check for the same inode, but overlapping extents.
2704 btrfs_double_lock(src
, loff
, dst
, dst_loff
, len
);
2706 ret
= extent_same_check_offsets(src
, loff
, len
);
2710 ret
= extent_same_check_offsets(dst
, dst_loff
, len
);
2714 /* don't make the dst file partly checksummed */
2715 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
2716 (BTRFS_I(dst
)->flags
& BTRFS_INODE_NODATASUM
)) {
2721 ret
= btrfs_cmp_data(src
, loff
, dst
, dst_loff
, len
);
2723 ret
= btrfs_clone(src
, dst
, loff
, len
, len
, dst_loff
);
2726 btrfs_double_unlock(src
, loff
, dst
, dst_loff
, len
);
2731 #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2733 static long btrfs_ioctl_file_extent_same(struct file
*file
,
2736 struct btrfs_ioctl_same_args tmp
;
2737 struct btrfs_ioctl_same_args
*same
;
2738 struct btrfs_ioctl_same_extent_info
*info
;
2739 struct inode
*src
= file
->f_dentry
->d_inode
;
2740 struct file
*dst_file
= NULL
;
2747 u64 bs
= BTRFS_I(src
)->root
->fs_info
->sb
->s_blocksize
;
2748 bool is_admin
= capable(CAP_SYS_ADMIN
);
2750 if (!(file
->f_mode
& FMODE_READ
))
2753 ret
= mnt_want_write_file(file
);
2757 if (copy_from_user(&tmp
,
2758 (struct btrfs_ioctl_same_args __user
*)argp
,
2764 size
= sizeof(tmp
) +
2765 tmp
.dest_count
* sizeof(struct btrfs_ioctl_same_extent_info
);
2767 same
= memdup_user((struct btrfs_ioctl_same_args __user
*)argp
, size
);
2770 ret
= PTR_ERR(same
);
2774 off
= same
->logical_offset
;
2778 * Limit the total length we will dedupe for each operation.
2779 * This is intended to bound the total time spent in this
2780 * ioctl to something sane.
2782 if (len
> BTRFS_MAX_DEDUPE_LEN
)
2783 len
= BTRFS_MAX_DEDUPE_LEN
;
2785 if (WARN_ON_ONCE(bs
< PAGE_CACHE_SIZE
)) {
2787 * Btrfs does not support blocksize < page_size. As a
2788 * result, btrfs_cmp_data() won't correctly handle
2789 * this situation without an update.
2796 if (S_ISDIR(src
->i_mode
))
2800 if (!S_ISREG(src
->i_mode
))
2803 /* pre-format output fields to sane values */
2804 for (i
= 0; i
< same
->dest_count
; i
++) {
2805 same
->info
[i
].bytes_deduped
= 0ULL;
2806 same
->info
[i
].status
= 0;
2810 for (i
= 0; i
< same
->dest_count
; i
++) {
2811 info
= &same
->info
[i
];
2813 dst_file
= fget(info
->fd
);
2815 info
->status
= -EBADF
;
2819 if (!(is_admin
|| (dst_file
->f_mode
& FMODE_WRITE
))) {
2820 info
->status
= -EINVAL
;
2824 info
->status
= -EXDEV
;
2825 if (file
->f_path
.mnt
!= dst_file
->f_path
.mnt
)
2828 dst
= dst_file
->f_dentry
->d_inode
;
2829 if (src
->i_sb
!= dst
->i_sb
)
2832 if (S_ISDIR(dst
->i_mode
)) {
2833 info
->status
= -EISDIR
;
2837 if (!S_ISREG(dst
->i_mode
)) {
2838 info
->status
= -EACCES
;
2842 info
->status
= btrfs_extent_same(src
, off
, len
, dst
,
2843 info
->logical_offset
);
2844 if (info
->status
== 0)
2845 info
->bytes_deduped
+= len
;
2852 ret
= copy_to_user(argp
, same
, size
);
2857 mnt_drop_write_file(file
);
2862 * btrfs_clone() - clone a range from inode file to another
2864 * @src: Inode to clone from
2865 * @inode: Inode to clone to
2866 * @off: Offset within source to start clone from
2867 * @olen: Original length, passed by user, of range to clone
2868 * @olen_aligned: Block-aligned value of olen, extent_same uses
2869 * identical values here
2870 * @destoff: Offset within @inode to start clone
2872 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
2873 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
)
2875 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2876 struct btrfs_path
*path
= NULL
;
2877 struct extent_buffer
*leaf
;
2878 struct btrfs_trans_handle
*trans
;
2880 struct btrfs_key key
;
2884 u64 len
= olen_aligned
;
2887 buf
= vmalloc(btrfs_level_size(root
, 0));
2891 path
= btrfs_alloc_path();
2899 key
.objectid
= btrfs_ino(src
);
2900 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2905 * note the key will change type as we walk through the
2908 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
2913 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2914 if (path
->slots
[0] >= nritems
) {
2915 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
2920 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2922 leaf
= path
->nodes
[0];
2923 slot
= path
->slots
[0];
2925 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2926 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2927 key
.objectid
!= btrfs_ino(src
))
2930 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2931 struct btrfs_file_extent_item
*extent
;
2934 struct btrfs_key new_key
;
2935 u64 disko
= 0, diskl
= 0;
2936 u64 datao
= 0, datal
= 0;
2940 size
= btrfs_item_size_nr(leaf
, slot
);
2941 read_extent_buffer(leaf
, buf
,
2942 btrfs_item_ptr_offset(leaf
, slot
),
2945 extent
= btrfs_item_ptr(leaf
, slot
,
2946 struct btrfs_file_extent_item
);
2947 comp
= btrfs_file_extent_compression(leaf
, extent
);
2948 type
= btrfs_file_extent_type(leaf
, extent
);
2949 if (type
== BTRFS_FILE_EXTENT_REG
||
2950 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2951 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2953 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2955 datao
= btrfs_file_extent_offset(leaf
, extent
);
2956 datal
= btrfs_file_extent_num_bytes(leaf
,
2958 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2959 /* take upper bound, may be compressed */
2960 datal
= btrfs_file_extent_ram_bytes(leaf
,
2963 btrfs_release_path(path
);
2965 if (key
.offset
+ datal
<= off
||
2966 key
.offset
>= off
+ len
- 1)
2969 memcpy(&new_key
, &key
, sizeof(new_key
));
2970 new_key
.objectid
= btrfs_ino(inode
);
2971 if (off
<= key
.offset
)
2972 new_key
.offset
= key
.offset
+ destoff
- off
;
2974 new_key
.offset
= destoff
;
2977 * 1 - adjusting old extent (we may have to split it)
2978 * 1 - add new extent
2981 trans
= btrfs_start_transaction(root
, 3);
2982 if (IS_ERR(trans
)) {
2983 ret
= PTR_ERR(trans
);
2987 if (type
== BTRFS_FILE_EXTENT_REG
||
2988 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2990 * a | --- range to clone ---| b
2991 * | ------------- extent ------------- |
2994 /* substract range b */
2995 if (key
.offset
+ datal
> off
+ len
)
2996 datal
= off
+ len
- key
.offset
;
2998 /* substract range a */
2999 if (off
> key
.offset
) {
3000 datao
+= off
- key
.offset
;
3001 datal
-= off
- key
.offset
;
3004 ret
= btrfs_drop_extents(trans
, root
, inode
,
3006 new_key
.offset
+ datal
,
3009 btrfs_abort_transaction(trans
, root
,
3011 btrfs_end_transaction(trans
, root
);
3015 ret
= btrfs_insert_empty_item(trans
, root
, path
,
3018 btrfs_abort_transaction(trans
, root
,
3020 btrfs_end_transaction(trans
, root
);
3024 leaf
= path
->nodes
[0];
3025 slot
= path
->slots
[0];
3026 write_extent_buffer(leaf
, buf
,
3027 btrfs_item_ptr_offset(leaf
, slot
),
3030 extent
= btrfs_item_ptr(leaf
, slot
,
3031 struct btrfs_file_extent_item
);
3033 /* disko == 0 means it's a hole */
3037 btrfs_set_file_extent_offset(leaf
, extent
,
3039 btrfs_set_file_extent_num_bytes(leaf
, extent
,
3042 inode_add_bytes(inode
, datal
);
3043 ret
= btrfs_inc_extent_ref(trans
, root
,
3045 root
->root_key
.objectid
,
3047 new_key
.offset
- datao
,
3050 btrfs_abort_transaction(trans
,
3053 btrfs_end_transaction(trans
,
3059 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3062 if (off
> key
.offset
) {
3063 skip
= off
- key
.offset
;
3064 new_key
.offset
+= skip
;
3067 if (key
.offset
+ datal
> off
+ len
)
3068 trim
= key
.offset
+ datal
- (off
+ len
);
3070 if (comp
&& (skip
|| trim
)) {
3072 btrfs_end_transaction(trans
, root
);
3075 size
-= skip
+ trim
;
3076 datal
-= skip
+ trim
;
3078 ret
= btrfs_drop_extents(trans
, root
, inode
,
3080 new_key
.offset
+ datal
,
3083 btrfs_abort_transaction(trans
, root
,
3085 btrfs_end_transaction(trans
, root
);
3089 ret
= btrfs_insert_empty_item(trans
, root
, path
,
3092 btrfs_abort_transaction(trans
, root
,
3094 btrfs_end_transaction(trans
, root
);
3100 btrfs_file_extent_calc_inline_size(0);
3101 memmove(buf
+start
, buf
+start
+skip
,
3105 leaf
= path
->nodes
[0];
3106 slot
= path
->slots
[0];
3107 write_extent_buffer(leaf
, buf
,
3108 btrfs_item_ptr_offset(leaf
, slot
),
3110 inode_add_bytes(inode
, datal
);
3113 btrfs_mark_buffer_dirty(leaf
);
3114 btrfs_release_path(path
);
3116 inode_inc_iversion(inode
);
3117 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
3120 * we round up to the block size at eof when
3121 * determining which extents to clone above,
3122 * but shouldn't round up the file size
3124 endoff
= new_key
.offset
+ datal
;
3125 if (endoff
> destoff
+olen
)
3126 endoff
= destoff
+olen
;
3127 if (endoff
> inode
->i_size
)
3128 btrfs_i_size_write(inode
, endoff
);
3130 ret
= btrfs_update_inode(trans
, root
, inode
);
3132 btrfs_abort_transaction(trans
, root
, ret
);
3133 btrfs_end_transaction(trans
, root
);
3136 ret
= btrfs_end_transaction(trans
, root
);
3139 btrfs_release_path(path
);
3145 btrfs_release_path(path
);
3146 btrfs_free_path(path
);
3151 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
3152 u64 off
, u64 olen
, u64 destoff
)
3154 struct inode
*inode
= file_inode(file
);
3155 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3160 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
3165 * - split compressed inline extents. annoying: we need to
3166 * decompress into destination's address_space (the file offset
3167 * may change, so source mapping won't do), then recompress (or
3168 * otherwise reinsert) a subrange.
3169 * - allow ranges within the same file to be cloned (provided
3170 * they don't overlap)?
3173 /* the destination must be opened for writing */
3174 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
3177 if (btrfs_root_readonly(root
))
3180 ret
= mnt_want_write_file(file
);
3184 src_file
= fdget(srcfd
);
3185 if (!src_file
.file
) {
3187 goto out_drop_write
;
3191 if (src_file
.file
->f_path
.mnt
!= file
->f_path
.mnt
)
3194 src
= file_inode(src_file
.file
);
3200 /* the src must be open for reading */
3201 if (!(src_file
.file
->f_mode
& FMODE_READ
))
3204 /* don't make the dst file partly checksummed */
3205 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3206 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
3210 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
3214 if (src
->i_sb
!= inode
->i_sb
)
3219 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
3220 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
3222 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
3223 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
3226 mutex_lock(&src
->i_mutex
);
3229 /* determine range to clone */
3231 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
3234 olen
= len
= src
->i_size
- off
;
3235 /* if we extend to eof, continue to block boundary */
3236 if (off
+ len
== src
->i_size
)
3237 len
= ALIGN(src
->i_size
, bs
) - off
;
3239 /* verify the end result is block aligned */
3240 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
3241 !IS_ALIGNED(destoff
, bs
))
3244 /* verify if ranges are overlapped within the same file */
3246 if (destoff
+ len
> off
&& destoff
< off
+ len
)
3250 if (destoff
> inode
->i_size
) {
3251 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
3256 /* truncate page cache pages from target inode range */
3257 truncate_inode_pages_range(&inode
->i_data
, destoff
,
3258 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
3260 lock_extent_range(src
, off
, len
);
3262 ret
= btrfs_clone(src
, inode
, off
, olen
, len
, destoff
);
3264 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+ len
- 1);
3266 mutex_unlock(&src
->i_mutex
);
3268 mutex_unlock(&inode
->i_mutex
);
3272 mnt_drop_write_file(file
);
3276 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
3278 struct btrfs_ioctl_clone_range_args args
;
3280 if (copy_from_user(&args
, argp
, sizeof(args
)))
3282 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
3283 args
.src_length
, args
.dest_offset
);
3287 * there are many ways the trans_start and trans_end ioctls can lead
3288 * to deadlocks. They should only be used by applications that
3289 * basically own the machine, and have a very in depth understanding
3290 * of all the possible deadlocks and enospc problems.
3292 static long btrfs_ioctl_trans_start(struct file
*file
)
3294 struct inode
*inode
= file_inode(file
);
3295 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3296 struct btrfs_trans_handle
*trans
;
3300 if (!capable(CAP_SYS_ADMIN
))
3304 if (file
->private_data
)
3308 if (btrfs_root_readonly(root
))
3311 ret
= mnt_want_write_file(file
);
3315 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
3318 trans
= btrfs_start_ioctl_transaction(root
);
3322 file
->private_data
= trans
;
3326 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
3327 mnt_drop_write_file(file
);
3332 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
3334 struct inode
*inode
= file_inode(file
);
3335 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3336 struct btrfs_root
*new_root
;
3337 struct btrfs_dir_item
*di
;
3338 struct btrfs_trans_handle
*trans
;
3339 struct btrfs_path
*path
;
3340 struct btrfs_key location
;
3341 struct btrfs_disk_key disk_key
;
3346 if (!capable(CAP_SYS_ADMIN
))
3349 ret
= mnt_want_write_file(file
);
3353 if (copy_from_user(&objectid
, argp
, sizeof(objectid
))) {
3359 objectid
= BTRFS_FS_TREE_OBJECTID
;
3361 location
.objectid
= objectid
;
3362 location
.type
= BTRFS_ROOT_ITEM_KEY
;
3363 location
.offset
= (u64
)-1;
3365 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
3366 if (IS_ERR(new_root
)) {
3367 ret
= PTR_ERR(new_root
);
3371 path
= btrfs_alloc_path();
3376 path
->leave_spinning
= 1;
3378 trans
= btrfs_start_transaction(root
, 1);
3379 if (IS_ERR(trans
)) {
3380 btrfs_free_path(path
);
3381 ret
= PTR_ERR(trans
);
3385 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
3386 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
3387 dir_id
, "default", 7, 1);
3388 if (IS_ERR_OR_NULL(di
)) {
3389 btrfs_free_path(path
);
3390 btrfs_end_transaction(trans
, root
);
3391 btrfs_err(new_root
->fs_info
, "Umm, you don't have the default dir"
3392 "item, this isn't going to work");
3397 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
3398 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
3399 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3400 btrfs_free_path(path
);
3402 btrfs_set_fs_incompat(root
->fs_info
, DEFAULT_SUBVOL
);
3403 btrfs_end_transaction(trans
, root
);
3405 mnt_drop_write_file(file
);
3409 void btrfs_get_block_group_info(struct list_head
*groups_list
,
3410 struct btrfs_ioctl_space_info
*space
)
3412 struct btrfs_block_group_cache
*block_group
;
3414 space
->total_bytes
= 0;
3415 space
->used_bytes
= 0;
3417 list_for_each_entry(block_group
, groups_list
, list
) {
3418 space
->flags
= block_group
->flags
;
3419 space
->total_bytes
+= block_group
->key
.offset
;
3420 space
->used_bytes
+=
3421 btrfs_block_group_used(&block_group
->item
);
3425 static long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
3427 struct btrfs_ioctl_space_args space_args
;
3428 struct btrfs_ioctl_space_info space
;
3429 struct btrfs_ioctl_space_info
*dest
;
3430 struct btrfs_ioctl_space_info
*dest_orig
;
3431 struct btrfs_ioctl_space_info __user
*user_dest
;
3432 struct btrfs_space_info
*info
;
3433 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
3434 BTRFS_BLOCK_GROUP_SYSTEM
,
3435 BTRFS_BLOCK_GROUP_METADATA
,
3436 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
3443 if (copy_from_user(&space_args
,
3444 (struct btrfs_ioctl_space_args __user
*)arg
,
3445 sizeof(space_args
)))
3448 for (i
= 0; i
< num_types
; i
++) {
3449 struct btrfs_space_info
*tmp
;
3453 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
3455 if (tmp
->flags
== types
[i
]) {
3465 down_read(&info
->groups_sem
);
3466 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
3467 if (!list_empty(&info
->block_groups
[c
]))
3470 up_read(&info
->groups_sem
);
3473 /* space_slots == 0 means they are asking for a count */
3474 if (space_args
.space_slots
== 0) {
3475 space_args
.total_spaces
= slot_count
;
3479 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
3481 alloc_size
= sizeof(*dest
) * slot_count
;
3483 /* we generally have at most 6 or so space infos, one for each raid
3484 * level. So, a whole page should be more than enough for everyone
3486 if (alloc_size
> PAGE_CACHE_SIZE
)
3489 space_args
.total_spaces
= 0;
3490 dest
= kmalloc(alloc_size
, GFP_NOFS
);
3495 /* now we have a buffer to copy into */
3496 for (i
= 0; i
< num_types
; i
++) {
3497 struct btrfs_space_info
*tmp
;
3504 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
3506 if (tmp
->flags
== types
[i
]) {
3515 down_read(&info
->groups_sem
);
3516 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
3517 if (!list_empty(&info
->block_groups
[c
])) {
3518 btrfs_get_block_group_info(
3519 &info
->block_groups
[c
], &space
);
3520 memcpy(dest
, &space
, sizeof(space
));
3522 space_args
.total_spaces
++;
3528 up_read(&info
->groups_sem
);
3531 user_dest
= (struct btrfs_ioctl_space_info __user
*)
3532 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
3534 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
3539 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
3545 static long btrfs_ioctl_global_rsv(struct btrfs_root
*root
, void __user
*arg
)
3547 struct btrfs_block_rsv
*block_rsv
= &root
->fs_info
->global_block_rsv
;
3550 spin_lock(&block_rsv
->lock
);
3551 reserved
= block_rsv
->reserved
;
3552 spin_unlock(&block_rsv
->lock
);
3554 if (arg
&& copy_to_user(arg
, &reserved
, sizeof(reserved
)))
3560 * there are many ways the trans_start and trans_end ioctls can lead
3561 * to deadlocks. They should only be used by applications that
3562 * basically own the machine, and have a very in depth understanding
3563 * of all the possible deadlocks and enospc problems.
3565 long btrfs_ioctl_trans_end(struct file
*file
)
3567 struct inode
*inode
= file_inode(file
);
3568 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3569 struct btrfs_trans_handle
*trans
;
3571 trans
= file
->private_data
;
3574 file
->private_data
= NULL
;
3576 btrfs_end_transaction(trans
, root
);
3578 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
3580 mnt_drop_write_file(file
);
3584 static noinline
long btrfs_ioctl_start_sync(struct btrfs_root
*root
,
3587 struct btrfs_trans_handle
*trans
;
3591 trans
= btrfs_attach_transaction_barrier(root
);
3592 if (IS_ERR(trans
)) {
3593 if (PTR_ERR(trans
) != -ENOENT
)
3594 return PTR_ERR(trans
);
3596 /* No running transaction, don't bother */
3597 transid
= root
->fs_info
->last_trans_committed
;
3600 transid
= trans
->transid
;
3601 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
3603 btrfs_end_transaction(trans
, root
);
3608 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
3613 static noinline
long btrfs_ioctl_wait_sync(struct btrfs_root
*root
,
3619 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
3622 transid
= 0; /* current trans */
3624 return btrfs_wait_for_commit(root
, transid
);
3627 static long btrfs_ioctl_scrub(struct file
*file
, void __user
*arg
)
3629 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
3630 struct btrfs_ioctl_scrub_args
*sa
;
3633 if (!capable(CAP_SYS_ADMIN
))
3636 sa
= memdup_user(arg
, sizeof(*sa
));
3640 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
)) {
3641 ret
= mnt_want_write_file(file
);
3646 ret
= btrfs_scrub_dev(root
->fs_info
, sa
->devid
, sa
->start
, sa
->end
,
3647 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
,
3650 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3653 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
))
3654 mnt_drop_write_file(file
);
3660 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
3662 if (!capable(CAP_SYS_ADMIN
))
3665 return btrfs_scrub_cancel(root
->fs_info
);
3668 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
3671 struct btrfs_ioctl_scrub_args
*sa
;
3674 if (!capable(CAP_SYS_ADMIN
))
3677 sa
= memdup_user(arg
, sizeof(*sa
));
3681 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
3683 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3690 static long btrfs_ioctl_get_dev_stats(struct btrfs_root
*root
,
3693 struct btrfs_ioctl_get_dev_stats
*sa
;
3696 sa
= memdup_user(arg
, sizeof(*sa
));
3700 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
3705 ret
= btrfs_get_dev_stats(root
, sa
);
3707 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3714 static long btrfs_ioctl_dev_replace(struct btrfs_root
*root
, void __user
*arg
)
3716 struct btrfs_ioctl_dev_replace_args
*p
;
3719 if (!capable(CAP_SYS_ADMIN
))
3722 p
= memdup_user(arg
, sizeof(*p
));
3727 case BTRFS_IOCTL_DEV_REPLACE_CMD_START
:
3728 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
) {
3733 &root
->fs_info
->mutually_exclusive_operation_running
,
3735 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3737 ret
= btrfs_dev_replace_start(root
, p
);
3739 &root
->fs_info
->mutually_exclusive_operation_running
,
3743 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
:
3744 btrfs_dev_replace_status(root
->fs_info
, p
);
3747 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
:
3748 ret
= btrfs_dev_replace_cancel(root
->fs_info
, p
);
3755 if (copy_to_user(arg
, p
, sizeof(*p
)))
3762 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
3768 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
3769 struct inode_fs_paths
*ipath
= NULL
;
3770 struct btrfs_path
*path
;
3772 if (!capable(CAP_DAC_READ_SEARCH
))
3775 path
= btrfs_alloc_path();
3781 ipa
= memdup_user(arg
, sizeof(*ipa
));
3788 size
= min_t(u32
, ipa
->size
, 4096);
3789 ipath
= init_ipath(size
, root
, path
);
3790 if (IS_ERR(ipath
)) {
3791 ret
= PTR_ERR(ipath
);
3796 ret
= paths_from_inode(ipa
->inum
, ipath
);
3800 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
3801 rel_ptr
= ipath
->fspath
->val
[i
] -
3802 (u64
)(unsigned long)ipath
->fspath
->val
;
3803 ipath
->fspath
->val
[i
] = rel_ptr
;
3806 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
3807 (void *)(unsigned long)ipath
->fspath
, size
);
3814 btrfs_free_path(path
);
3821 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
3823 struct btrfs_data_container
*inodes
= ctx
;
3824 const size_t c
= 3 * sizeof(u64
);
3826 if (inodes
->bytes_left
>= c
) {
3827 inodes
->bytes_left
-= c
;
3828 inodes
->val
[inodes
->elem_cnt
] = inum
;
3829 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
3830 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
3831 inodes
->elem_cnt
+= 3;
3833 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
3834 inodes
->bytes_left
= 0;
3835 inodes
->elem_missed
+= 3;
3841 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
3846 struct btrfs_ioctl_logical_ino_args
*loi
;
3847 struct btrfs_data_container
*inodes
= NULL
;
3848 struct btrfs_path
*path
= NULL
;
3850 if (!capable(CAP_SYS_ADMIN
))
3853 loi
= memdup_user(arg
, sizeof(*loi
));
3860 path
= btrfs_alloc_path();
3866 size
= min_t(u32
, loi
->size
, 64 * 1024);
3867 inodes
= init_data_container(size
);
3868 if (IS_ERR(inodes
)) {
3869 ret
= PTR_ERR(inodes
);
3874 ret
= iterate_inodes_from_logical(loi
->logical
, root
->fs_info
, path
,
3875 build_ino_list
, inodes
);
3881 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
3882 (void *)(unsigned long)inodes
, size
);
3887 btrfs_free_path(path
);
3894 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3895 struct btrfs_ioctl_balance_args
*bargs
)
3897 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3899 bargs
->flags
= bctl
->flags
;
3901 if (atomic_read(&fs_info
->balance_running
))
3902 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
3903 if (atomic_read(&fs_info
->balance_pause_req
))
3904 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
3905 if (atomic_read(&fs_info
->balance_cancel_req
))
3906 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
3908 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
3909 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
3910 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
3913 spin_lock(&fs_info
->balance_lock
);
3914 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3915 spin_unlock(&fs_info
->balance_lock
);
3917 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3921 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
3923 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
3924 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3925 struct btrfs_ioctl_balance_args
*bargs
;
3926 struct btrfs_balance_control
*bctl
;
3927 bool need_unlock
; /* for mut. excl. ops lock */
3930 if (!capable(CAP_SYS_ADMIN
))
3933 ret
= mnt_want_write_file(file
);
3938 if (!atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1)) {
3939 mutex_lock(&fs_info
->volume_mutex
);
3940 mutex_lock(&fs_info
->balance_mutex
);
3946 * mut. excl. ops lock is locked. Three possibilites:
3947 * (1) some other op is running
3948 * (2) balance is running
3949 * (3) balance is paused -- special case (think resume)
3951 mutex_lock(&fs_info
->balance_mutex
);
3952 if (fs_info
->balance_ctl
) {
3953 /* this is either (2) or (3) */
3954 if (!atomic_read(&fs_info
->balance_running
)) {
3955 mutex_unlock(&fs_info
->balance_mutex
);
3956 if (!mutex_trylock(&fs_info
->volume_mutex
))
3958 mutex_lock(&fs_info
->balance_mutex
);
3960 if (fs_info
->balance_ctl
&&
3961 !atomic_read(&fs_info
->balance_running
)) {
3963 need_unlock
= false;
3967 mutex_unlock(&fs_info
->balance_mutex
);
3968 mutex_unlock(&fs_info
->volume_mutex
);
3972 mutex_unlock(&fs_info
->balance_mutex
);
3978 mutex_unlock(&fs_info
->balance_mutex
);
3979 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3984 BUG_ON(!atomic_read(&fs_info
->mutually_exclusive_operation_running
));
3987 bargs
= memdup_user(arg
, sizeof(*bargs
));
3988 if (IS_ERR(bargs
)) {
3989 ret
= PTR_ERR(bargs
);
3993 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
3994 if (!fs_info
->balance_ctl
) {
3999 bctl
= fs_info
->balance_ctl
;
4000 spin_lock(&fs_info
->balance_lock
);
4001 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
4002 spin_unlock(&fs_info
->balance_lock
);
4010 if (fs_info
->balance_ctl
) {
4015 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
4021 bctl
->fs_info
= fs_info
;
4023 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
4024 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
4025 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
4027 bctl
->flags
= bargs
->flags
;
4029 /* balance everything - no filters */
4030 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
4035 * Ownership of bctl and mutually_exclusive_operation_running
4036 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4037 * or, if restriper was paused all the way until unmount, in
4038 * free_fs_info. mutually_exclusive_operation_running is
4039 * cleared in __cancel_balance.
4041 need_unlock
= false;
4043 ret
= btrfs_balance(bctl
, bargs
);
4046 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4053 mutex_unlock(&fs_info
->balance_mutex
);
4054 mutex_unlock(&fs_info
->volume_mutex
);
4056 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
4058 mnt_drop_write_file(file
);
4062 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
4064 if (!capable(CAP_SYS_ADMIN
))
4068 case BTRFS_BALANCE_CTL_PAUSE
:
4069 return btrfs_pause_balance(root
->fs_info
);
4070 case BTRFS_BALANCE_CTL_CANCEL
:
4071 return btrfs_cancel_balance(root
->fs_info
);
4077 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
4080 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4081 struct btrfs_ioctl_balance_args
*bargs
;
4084 if (!capable(CAP_SYS_ADMIN
))
4087 mutex_lock(&fs_info
->balance_mutex
);
4088 if (!fs_info
->balance_ctl
) {
4093 bargs
= kzalloc(sizeof(*bargs
), GFP_NOFS
);
4099 update_ioctl_balance_args(fs_info
, 1, bargs
);
4101 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4106 mutex_unlock(&fs_info
->balance_mutex
);
4110 static long btrfs_ioctl_quota_ctl(struct file
*file
, void __user
*arg
)
4112 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4113 struct btrfs_ioctl_quota_ctl_args
*sa
;
4114 struct btrfs_trans_handle
*trans
= NULL
;
4118 if (!capable(CAP_SYS_ADMIN
))
4121 ret
= mnt_want_write_file(file
);
4125 sa
= memdup_user(arg
, sizeof(*sa
));
4131 down_write(&root
->fs_info
->subvol_sem
);
4132 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 2);
4133 if (IS_ERR(trans
)) {
4134 ret
= PTR_ERR(trans
);
4139 case BTRFS_QUOTA_CTL_ENABLE
:
4140 ret
= btrfs_quota_enable(trans
, root
->fs_info
);
4142 case BTRFS_QUOTA_CTL_DISABLE
:
4143 ret
= btrfs_quota_disable(trans
, root
->fs_info
);
4150 err
= btrfs_commit_transaction(trans
, root
->fs_info
->tree_root
);
4155 up_write(&root
->fs_info
->subvol_sem
);
4157 mnt_drop_write_file(file
);
4161 static long btrfs_ioctl_qgroup_assign(struct file
*file
, void __user
*arg
)
4163 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4164 struct btrfs_ioctl_qgroup_assign_args
*sa
;
4165 struct btrfs_trans_handle
*trans
;
4169 if (!capable(CAP_SYS_ADMIN
))
4172 ret
= mnt_want_write_file(file
);
4176 sa
= memdup_user(arg
, sizeof(*sa
));
4182 trans
= btrfs_join_transaction(root
);
4183 if (IS_ERR(trans
)) {
4184 ret
= PTR_ERR(trans
);
4188 /* FIXME: check if the IDs really exist */
4190 ret
= btrfs_add_qgroup_relation(trans
, root
->fs_info
,
4193 ret
= btrfs_del_qgroup_relation(trans
, root
->fs_info
,
4197 err
= btrfs_end_transaction(trans
, root
);
4204 mnt_drop_write_file(file
);
4208 static long btrfs_ioctl_qgroup_create(struct file
*file
, void __user
*arg
)
4210 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4211 struct btrfs_ioctl_qgroup_create_args
*sa
;
4212 struct btrfs_trans_handle
*trans
;
4216 if (!capable(CAP_SYS_ADMIN
))
4219 ret
= mnt_want_write_file(file
);
4223 sa
= memdup_user(arg
, sizeof(*sa
));
4229 if (!sa
->qgroupid
) {
4234 trans
= btrfs_join_transaction(root
);
4235 if (IS_ERR(trans
)) {
4236 ret
= PTR_ERR(trans
);
4240 /* FIXME: check if the IDs really exist */
4242 ret
= btrfs_create_qgroup(trans
, root
->fs_info
, sa
->qgroupid
,
4245 ret
= btrfs_remove_qgroup(trans
, root
->fs_info
, sa
->qgroupid
);
4248 err
= btrfs_end_transaction(trans
, root
);
4255 mnt_drop_write_file(file
);
4259 static long btrfs_ioctl_qgroup_limit(struct file
*file
, void __user
*arg
)
4261 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4262 struct btrfs_ioctl_qgroup_limit_args
*sa
;
4263 struct btrfs_trans_handle
*trans
;
4268 if (!capable(CAP_SYS_ADMIN
))
4271 ret
= mnt_want_write_file(file
);
4275 sa
= memdup_user(arg
, sizeof(*sa
));
4281 trans
= btrfs_join_transaction(root
);
4282 if (IS_ERR(trans
)) {
4283 ret
= PTR_ERR(trans
);
4287 qgroupid
= sa
->qgroupid
;
4289 /* take the current subvol as qgroup */
4290 qgroupid
= root
->root_key
.objectid
;
4293 /* FIXME: check if the IDs really exist */
4294 ret
= btrfs_limit_qgroup(trans
, root
->fs_info
, qgroupid
, &sa
->lim
);
4296 err
= btrfs_end_transaction(trans
, root
);
4303 mnt_drop_write_file(file
);
4307 static long btrfs_ioctl_quota_rescan(struct file
*file
, void __user
*arg
)
4309 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4310 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4313 if (!capable(CAP_SYS_ADMIN
))
4316 ret
= mnt_want_write_file(file
);
4320 qsa
= memdup_user(arg
, sizeof(*qsa
));
4331 ret
= btrfs_qgroup_rescan(root
->fs_info
);
4336 mnt_drop_write_file(file
);
4340 static long btrfs_ioctl_quota_rescan_status(struct file
*file
, void __user
*arg
)
4342 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4343 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4346 if (!capable(CAP_SYS_ADMIN
))
4349 qsa
= kzalloc(sizeof(*qsa
), GFP_NOFS
);
4353 if (root
->fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
4355 qsa
->progress
= root
->fs_info
->qgroup_rescan_progress
.objectid
;
4358 if (copy_to_user(arg
, qsa
, sizeof(*qsa
)))
4365 static long btrfs_ioctl_quota_rescan_wait(struct file
*file
, void __user
*arg
)
4367 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4369 if (!capable(CAP_SYS_ADMIN
))
4372 return btrfs_qgroup_wait_for_completion(root
->fs_info
);
4375 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
4378 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
4379 struct inode
*inode
= file_inode(file
);
4380 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4381 struct btrfs_root_item
*root_item
= &root
->root_item
;
4382 struct btrfs_trans_handle
*trans
;
4383 struct timespec ct
= CURRENT_TIME
;
4385 int received_uuid_changed
;
4387 ret
= mnt_want_write_file(file
);
4391 down_write(&root
->fs_info
->subvol_sem
);
4393 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
4398 if (btrfs_root_readonly(root
)) {
4403 if (!inode_owner_or_capable(inode
)) {
4408 sa
= memdup_user(arg
, sizeof(*sa
));
4417 * 2 - uuid items (received uuid + subvol uuid)
4419 trans
= btrfs_start_transaction(root
, 3);
4420 if (IS_ERR(trans
)) {
4421 ret
= PTR_ERR(trans
);
4426 sa
->rtransid
= trans
->transid
;
4427 sa
->rtime
.sec
= ct
.tv_sec
;
4428 sa
->rtime
.nsec
= ct
.tv_nsec
;
4430 received_uuid_changed
= memcmp(root_item
->received_uuid
, sa
->uuid
,
4432 if (received_uuid_changed
&&
4433 !btrfs_is_empty_uuid(root_item
->received_uuid
))
4434 btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
4435 root_item
->received_uuid
,
4436 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4437 root
->root_key
.objectid
);
4438 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
4439 btrfs_set_root_stransid(root_item
, sa
->stransid
);
4440 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
4441 btrfs_set_stack_timespec_sec(&root_item
->stime
, sa
->stime
.sec
);
4442 btrfs_set_stack_timespec_nsec(&root_item
->stime
, sa
->stime
.nsec
);
4443 btrfs_set_stack_timespec_sec(&root_item
->rtime
, sa
->rtime
.sec
);
4444 btrfs_set_stack_timespec_nsec(&root_item
->rtime
, sa
->rtime
.nsec
);
4446 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
4447 &root
->root_key
, &root
->root_item
);
4449 btrfs_end_transaction(trans
, root
);
4452 if (received_uuid_changed
&& !btrfs_is_empty_uuid(sa
->uuid
)) {
4453 ret
= btrfs_uuid_tree_add(trans
, root
->fs_info
->uuid_root
,
4455 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4456 root
->root_key
.objectid
);
4457 if (ret
< 0 && ret
!= -EEXIST
) {
4458 btrfs_abort_transaction(trans
, root
, ret
);
4462 ret
= btrfs_commit_transaction(trans
, root
);
4464 btrfs_abort_transaction(trans
, root
, ret
);
4468 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
4474 up_write(&root
->fs_info
->subvol_sem
);
4475 mnt_drop_write_file(file
);
4479 static int btrfs_ioctl_get_fslabel(struct file
*file
, void __user
*arg
)
4481 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4484 char label
[BTRFS_LABEL_SIZE
];
4486 spin_lock(&root
->fs_info
->super_lock
);
4487 memcpy(label
, root
->fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
4488 spin_unlock(&root
->fs_info
->super_lock
);
4490 len
= strnlen(label
, BTRFS_LABEL_SIZE
);
4492 if (len
== BTRFS_LABEL_SIZE
) {
4493 btrfs_warn(root
->fs_info
,
4494 "label is too long, return the first %zu bytes", --len
);
4497 ret
= copy_to_user(arg
, label
, len
);
4499 return ret
? -EFAULT
: 0;
4502 static int btrfs_ioctl_set_fslabel(struct file
*file
, void __user
*arg
)
4504 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4505 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
4506 struct btrfs_trans_handle
*trans
;
4507 char label
[BTRFS_LABEL_SIZE
];
4510 if (!capable(CAP_SYS_ADMIN
))
4513 if (copy_from_user(label
, arg
, sizeof(label
)))
4516 if (strnlen(label
, BTRFS_LABEL_SIZE
) == BTRFS_LABEL_SIZE
) {
4517 btrfs_err(root
->fs_info
, "unable to set label with more than %d bytes",
4518 BTRFS_LABEL_SIZE
- 1);
4522 ret
= mnt_want_write_file(file
);
4526 trans
= btrfs_start_transaction(root
, 0);
4527 if (IS_ERR(trans
)) {
4528 ret
= PTR_ERR(trans
);
4532 spin_lock(&root
->fs_info
->super_lock
);
4533 strcpy(super_block
->label
, label
);
4534 spin_unlock(&root
->fs_info
->super_lock
);
4535 ret
= btrfs_end_transaction(trans
, root
);
4538 mnt_drop_write_file(file
);
4542 #define INIT_FEATURE_FLAGS(suffix) \
4543 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4544 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4545 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4547 static int btrfs_ioctl_get_supported_features(struct file
*file
,
4550 static struct btrfs_ioctl_feature_flags features
[3] = {
4551 INIT_FEATURE_FLAGS(SUPP
),
4552 INIT_FEATURE_FLAGS(SAFE_SET
),
4553 INIT_FEATURE_FLAGS(SAFE_CLEAR
)
4556 if (copy_to_user(arg
, &features
, sizeof(features
)))
4562 static int btrfs_ioctl_get_features(struct file
*file
, void __user
*arg
)
4564 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4565 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
4566 struct btrfs_ioctl_feature_flags features
;
4568 features
.compat_flags
= btrfs_super_compat_flags(super_block
);
4569 features
.compat_ro_flags
= btrfs_super_compat_ro_flags(super_block
);
4570 features
.incompat_flags
= btrfs_super_incompat_flags(super_block
);
4572 if (copy_to_user(arg
, &features
, sizeof(features
)))
4578 static int check_feature_bits(struct btrfs_root
*root
,
4579 enum btrfs_feature_set set
,
4580 u64 change_mask
, u64 flags
, u64 supported_flags
,
4581 u64 safe_set
, u64 safe_clear
)
4583 const char *type
= btrfs_feature_set_names
[set
];
4585 u64 disallowed
, unsupported
;
4586 u64 set_mask
= flags
& change_mask
;
4587 u64 clear_mask
= ~flags
& change_mask
;
4589 unsupported
= set_mask
& ~supported_flags
;
4591 names
= btrfs_printable_features(set
, unsupported
);
4593 btrfs_warn(root
->fs_info
,
4594 "this kernel does not support the %s feature bit%s",
4595 names
, strchr(names
, ',') ? "s" : "");
4598 btrfs_warn(root
->fs_info
,
4599 "this kernel does not support %s bits 0x%llx",
4604 disallowed
= set_mask
& ~safe_set
;
4606 names
= btrfs_printable_features(set
, disallowed
);
4608 btrfs_warn(root
->fs_info
,
4609 "can't set the %s feature bit%s while mounted",
4610 names
, strchr(names
, ',') ? "s" : "");
4613 btrfs_warn(root
->fs_info
,
4614 "can't set %s bits 0x%llx while mounted",
4619 disallowed
= clear_mask
& ~safe_clear
;
4621 names
= btrfs_printable_features(set
, disallowed
);
4623 btrfs_warn(root
->fs_info
,
4624 "can't clear the %s feature bit%s while mounted",
4625 names
, strchr(names
, ',') ? "s" : "");
4628 btrfs_warn(root
->fs_info
,
4629 "can't clear %s bits 0x%llx while mounted",
4637 #define check_feature(root, change_mask, flags, mask_base) \
4638 check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
4639 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4640 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4641 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4643 static int btrfs_ioctl_set_features(struct file
*file
, void __user
*arg
)
4645 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4646 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
4647 struct btrfs_ioctl_feature_flags flags
[2];
4648 struct btrfs_trans_handle
*trans
;
4652 if (!capable(CAP_SYS_ADMIN
))
4655 if (copy_from_user(flags
, arg
, sizeof(flags
)))
4659 if (!flags
[0].compat_flags
&& !flags
[0].compat_ro_flags
&&
4660 !flags
[0].incompat_flags
)
4663 ret
= check_feature(root
, flags
[0].compat_flags
,
4664 flags
[1].compat_flags
, COMPAT
);
4668 ret
= check_feature(root
, flags
[0].compat_ro_flags
,
4669 flags
[1].compat_ro_flags
, COMPAT_RO
);
4673 ret
= check_feature(root
, flags
[0].incompat_flags
,
4674 flags
[1].incompat_flags
, INCOMPAT
);
4678 trans
= btrfs_start_transaction(root
, 1);
4680 return PTR_ERR(trans
);
4682 spin_lock(&root
->fs_info
->super_lock
);
4683 newflags
= btrfs_super_compat_flags(super_block
);
4684 newflags
|= flags
[0].compat_flags
& flags
[1].compat_flags
;
4685 newflags
&= ~(flags
[0].compat_flags
& ~flags
[1].compat_flags
);
4686 btrfs_set_super_compat_flags(super_block
, newflags
);
4688 newflags
= btrfs_super_compat_ro_flags(super_block
);
4689 newflags
|= flags
[0].compat_ro_flags
& flags
[1].compat_ro_flags
;
4690 newflags
&= ~(flags
[0].compat_ro_flags
& ~flags
[1].compat_ro_flags
);
4691 btrfs_set_super_compat_ro_flags(super_block
, newflags
);
4693 newflags
= btrfs_super_incompat_flags(super_block
);
4694 newflags
|= flags
[0].incompat_flags
& flags
[1].incompat_flags
;
4695 newflags
&= ~(flags
[0].incompat_flags
& ~flags
[1].incompat_flags
);
4696 btrfs_set_super_incompat_flags(super_block
, newflags
);
4697 spin_unlock(&root
->fs_info
->super_lock
);
4699 return btrfs_end_transaction(trans
, root
);
4702 long btrfs_ioctl(struct file
*file
, unsigned int
4703 cmd
, unsigned long arg
)
4705 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4706 void __user
*argp
= (void __user
*)arg
;
4709 case FS_IOC_GETFLAGS
:
4710 return btrfs_ioctl_getflags(file
, argp
);
4711 case FS_IOC_SETFLAGS
:
4712 return btrfs_ioctl_setflags(file
, argp
);
4713 case FS_IOC_GETVERSION
:
4714 return btrfs_ioctl_getversion(file
, argp
);
4716 return btrfs_ioctl_fitrim(file
, argp
);
4717 case BTRFS_IOC_SNAP_CREATE
:
4718 return btrfs_ioctl_snap_create(file
, argp
, 0);
4719 case BTRFS_IOC_SNAP_CREATE_V2
:
4720 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
4721 case BTRFS_IOC_SUBVOL_CREATE
:
4722 return btrfs_ioctl_snap_create(file
, argp
, 1);
4723 case BTRFS_IOC_SUBVOL_CREATE_V2
:
4724 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
4725 case BTRFS_IOC_SNAP_DESTROY
:
4726 return btrfs_ioctl_snap_destroy(file
, argp
);
4727 case BTRFS_IOC_SUBVOL_GETFLAGS
:
4728 return btrfs_ioctl_subvol_getflags(file
, argp
);
4729 case BTRFS_IOC_SUBVOL_SETFLAGS
:
4730 return btrfs_ioctl_subvol_setflags(file
, argp
);
4731 case BTRFS_IOC_DEFAULT_SUBVOL
:
4732 return btrfs_ioctl_default_subvol(file
, argp
);
4733 case BTRFS_IOC_DEFRAG
:
4734 return btrfs_ioctl_defrag(file
, NULL
);
4735 case BTRFS_IOC_DEFRAG_RANGE
:
4736 return btrfs_ioctl_defrag(file
, argp
);
4737 case BTRFS_IOC_RESIZE
:
4738 return btrfs_ioctl_resize(file
, argp
);
4739 case BTRFS_IOC_ADD_DEV
:
4740 return btrfs_ioctl_add_dev(root
, argp
);
4741 case BTRFS_IOC_RM_DEV
:
4742 return btrfs_ioctl_rm_dev(file
, argp
);
4743 case BTRFS_IOC_FS_INFO
:
4744 return btrfs_ioctl_fs_info(root
, argp
);
4745 case BTRFS_IOC_DEV_INFO
:
4746 return btrfs_ioctl_dev_info(root
, argp
);
4747 case BTRFS_IOC_BALANCE
:
4748 return btrfs_ioctl_balance(file
, NULL
);
4749 case BTRFS_IOC_CLONE
:
4750 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
4751 case BTRFS_IOC_CLONE_RANGE
:
4752 return btrfs_ioctl_clone_range(file
, argp
);
4753 case BTRFS_IOC_TRANS_START
:
4754 return btrfs_ioctl_trans_start(file
);
4755 case BTRFS_IOC_TRANS_END
:
4756 return btrfs_ioctl_trans_end(file
);
4757 case BTRFS_IOC_TREE_SEARCH
:
4758 return btrfs_ioctl_tree_search(file
, argp
);
4759 case BTRFS_IOC_INO_LOOKUP
:
4760 return btrfs_ioctl_ino_lookup(file
, argp
);
4761 case BTRFS_IOC_INO_PATHS
:
4762 return btrfs_ioctl_ino_to_path(root
, argp
);
4763 case BTRFS_IOC_LOGICAL_INO
:
4764 return btrfs_ioctl_logical_to_ino(root
, argp
);
4765 case BTRFS_IOC_SPACE_INFO
:
4766 return btrfs_ioctl_space_info(root
, argp
);
4767 case BTRFS_IOC_GLOBAL_RSV
:
4768 return btrfs_ioctl_global_rsv(root
, argp
);
4769 case BTRFS_IOC_SYNC
: {
4772 ret
= btrfs_start_delalloc_roots(root
->fs_info
, 0);
4775 ret
= btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
4778 case BTRFS_IOC_START_SYNC
:
4779 return btrfs_ioctl_start_sync(root
, argp
);
4780 case BTRFS_IOC_WAIT_SYNC
:
4781 return btrfs_ioctl_wait_sync(root
, argp
);
4782 case BTRFS_IOC_SCRUB
:
4783 return btrfs_ioctl_scrub(file
, argp
);
4784 case BTRFS_IOC_SCRUB_CANCEL
:
4785 return btrfs_ioctl_scrub_cancel(root
, argp
);
4786 case BTRFS_IOC_SCRUB_PROGRESS
:
4787 return btrfs_ioctl_scrub_progress(root
, argp
);
4788 case BTRFS_IOC_BALANCE_V2
:
4789 return btrfs_ioctl_balance(file
, argp
);
4790 case BTRFS_IOC_BALANCE_CTL
:
4791 return btrfs_ioctl_balance_ctl(root
, arg
);
4792 case BTRFS_IOC_BALANCE_PROGRESS
:
4793 return btrfs_ioctl_balance_progress(root
, argp
);
4794 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
4795 return btrfs_ioctl_set_received_subvol(file
, argp
);
4796 case BTRFS_IOC_SEND
:
4797 return btrfs_ioctl_send(file
, argp
);
4798 case BTRFS_IOC_GET_DEV_STATS
:
4799 return btrfs_ioctl_get_dev_stats(root
, argp
);
4800 case BTRFS_IOC_QUOTA_CTL
:
4801 return btrfs_ioctl_quota_ctl(file
, argp
);
4802 case BTRFS_IOC_QGROUP_ASSIGN
:
4803 return btrfs_ioctl_qgroup_assign(file
, argp
);
4804 case BTRFS_IOC_QGROUP_CREATE
:
4805 return btrfs_ioctl_qgroup_create(file
, argp
);
4806 case BTRFS_IOC_QGROUP_LIMIT
:
4807 return btrfs_ioctl_qgroup_limit(file
, argp
);
4808 case BTRFS_IOC_QUOTA_RESCAN
:
4809 return btrfs_ioctl_quota_rescan(file
, argp
);
4810 case BTRFS_IOC_QUOTA_RESCAN_STATUS
:
4811 return btrfs_ioctl_quota_rescan_status(file
, argp
);
4812 case BTRFS_IOC_QUOTA_RESCAN_WAIT
:
4813 return btrfs_ioctl_quota_rescan_wait(file
, argp
);
4814 case BTRFS_IOC_DEV_REPLACE
:
4815 return btrfs_ioctl_dev_replace(root
, argp
);
4816 case BTRFS_IOC_GET_FSLABEL
:
4817 return btrfs_ioctl_get_fslabel(file
, argp
);
4818 case BTRFS_IOC_SET_FSLABEL
:
4819 return btrfs_ioctl_set_fslabel(file
, argp
);
4820 case BTRFS_IOC_FILE_EXTENT_SAME
:
4821 return btrfs_ioctl_file_extent_same(file
, argp
);
4822 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
4823 return btrfs_ioctl_get_supported_features(file
, argp
);
4824 case BTRFS_IOC_GET_FEATURES
:
4825 return btrfs_ioctl_get_features(file
, argp
);
4826 case BTRFS_IOC_SET_FEATURES
:
4827 return btrfs_ioctl_set_features(file
, argp
);