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"
60 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
61 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
);
63 /* Mask out flags that are inappropriate for the given type of inode. */
64 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
68 else if (S_ISREG(mode
))
69 return flags
& ~FS_DIRSYNC_FL
;
71 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
75 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
77 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
79 unsigned int iflags
= 0;
81 if (flags
& BTRFS_INODE_SYNC
)
83 if (flags
& BTRFS_INODE_IMMUTABLE
)
84 iflags
|= FS_IMMUTABLE_FL
;
85 if (flags
& BTRFS_INODE_APPEND
)
86 iflags
|= FS_APPEND_FL
;
87 if (flags
& BTRFS_INODE_NODUMP
)
88 iflags
|= FS_NODUMP_FL
;
89 if (flags
& BTRFS_INODE_NOATIME
)
90 iflags
|= FS_NOATIME_FL
;
91 if (flags
& BTRFS_INODE_DIRSYNC
)
92 iflags
|= FS_DIRSYNC_FL
;
93 if (flags
& BTRFS_INODE_NODATACOW
)
94 iflags
|= FS_NOCOW_FL
;
96 if ((flags
& BTRFS_INODE_COMPRESS
) && !(flags
& BTRFS_INODE_NOCOMPRESS
))
97 iflags
|= FS_COMPR_FL
;
98 else if (flags
& BTRFS_INODE_NOCOMPRESS
)
99 iflags
|= FS_NOCOMP_FL
;
105 * Update inode->i_flags based on the btrfs internal flags.
107 void btrfs_update_iflags(struct inode
*inode
)
109 struct btrfs_inode
*ip
= BTRFS_I(inode
);
111 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
113 if (ip
->flags
& BTRFS_INODE_SYNC
)
114 inode
->i_flags
|= S_SYNC
;
115 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
116 inode
->i_flags
|= S_IMMUTABLE
;
117 if (ip
->flags
& BTRFS_INODE_APPEND
)
118 inode
->i_flags
|= S_APPEND
;
119 if (ip
->flags
& BTRFS_INODE_NOATIME
)
120 inode
->i_flags
|= S_NOATIME
;
121 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
122 inode
->i_flags
|= S_DIRSYNC
;
126 * Inherit flags from the parent inode.
128 * Currently only the compression flags and the cow flags are inherited.
130 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
137 flags
= BTRFS_I(dir
)->flags
;
139 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
140 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
141 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
142 } else if (flags
& BTRFS_INODE_COMPRESS
) {
143 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
144 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
147 if (flags
& BTRFS_INODE_NODATACOW
) {
148 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
149 if (S_ISREG(inode
->i_mode
))
150 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
153 btrfs_update_iflags(inode
);
156 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
158 struct btrfs_inode
*ip
= BTRFS_I(file_inode(file
));
159 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
161 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
166 static int check_flags(unsigned int flags
)
168 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
169 FS_NOATIME_FL
| FS_NODUMP_FL
| \
170 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
171 FS_NOCOMP_FL
| FS_COMPR_FL
|
175 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
181 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
183 struct inode
*inode
= file_inode(file
);
184 struct btrfs_inode
*ip
= BTRFS_I(inode
);
185 struct btrfs_root
*root
= ip
->root
;
186 struct btrfs_trans_handle
*trans
;
187 unsigned int flags
, oldflags
;
190 unsigned int i_oldflags
;
193 if (btrfs_root_readonly(root
))
196 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
199 ret
= check_flags(flags
);
203 if (!inode_owner_or_capable(inode
))
206 ret
= mnt_want_write_file(file
);
210 mutex_lock(&inode
->i_mutex
);
212 ip_oldflags
= ip
->flags
;
213 i_oldflags
= inode
->i_flags
;
214 mode
= inode
->i_mode
;
216 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
217 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
218 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
219 if (!capable(CAP_LINUX_IMMUTABLE
)) {
225 if (flags
& FS_SYNC_FL
)
226 ip
->flags
|= BTRFS_INODE_SYNC
;
228 ip
->flags
&= ~BTRFS_INODE_SYNC
;
229 if (flags
& FS_IMMUTABLE_FL
)
230 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
232 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
233 if (flags
& FS_APPEND_FL
)
234 ip
->flags
|= BTRFS_INODE_APPEND
;
236 ip
->flags
&= ~BTRFS_INODE_APPEND
;
237 if (flags
& FS_NODUMP_FL
)
238 ip
->flags
|= BTRFS_INODE_NODUMP
;
240 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
241 if (flags
& FS_NOATIME_FL
)
242 ip
->flags
|= BTRFS_INODE_NOATIME
;
244 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
245 if (flags
& FS_DIRSYNC_FL
)
246 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
248 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
249 if (flags
& FS_NOCOW_FL
) {
252 * It's safe to turn csums off here, no extents exist.
253 * Otherwise we want the flag to reflect the real COW
254 * status of the file and will not set it.
256 if (inode
->i_size
== 0)
257 ip
->flags
|= BTRFS_INODE_NODATACOW
258 | BTRFS_INODE_NODATASUM
;
260 ip
->flags
|= BTRFS_INODE_NODATACOW
;
264 * Revert back under same assuptions as above
267 if (inode
->i_size
== 0)
268 ip
->flags
&= ~(BTRFS_INODE_NODATACOW
269 | BTRFS_INODE_NODATASUM
);
271 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
276 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
277 * flag may be changed automatically if compression code won't make
280 if (flags
& FS_NOCOMP_FL
) {
281 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
282 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
283 } else if (flags
& FS_COMPR_FL
) {
284 ip
->flags
|= BTRFS_INODE_COMPRESS
;
285 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
287 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
290 trans
= btrfs_start_transaction(root
, 1);
292 ret
= PTR_ERR(trans
);
296 btrfs_update_iflags(inode
);
297 inode_inc_iversion(inode
);
298 inode
->i_ctime
= CURRENT_TIME
;
299 ret
= btrfs_update_inode(trans
, root
, inode
);
301 btrfs_end_transaction(trans
, root
);
304 ip
->flags
= ip_oldflags
;
305 inode
->i_flags
= i_oldflags
;
309 mutex_unlock(&inode
->i_mutex
);
310 mnt_drop_write_file(file
);
314 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
316 struct inode
*inode
= file_inode(file
);
318 return put_user(inode
->i_generation
, arg
);
321 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
323 struct btrfs_fs_info
*fs_info
= btrfs_sb(file_inode(file
)->i_sb
);
324 struct btrfs_device
*device
;
325 struct request_queue
*q
;
326 struct fstrim_range range
;
327 u64 minlen
= ULLONG_MAX
;
329 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
332 if (!capable(CAP_SYS_ADMIN
))
336 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
340 q
= bdev_get_queue(device
->bdev
);
341 if (blk_queue_discard(q
)) {
343 minlen
= min((u64
)q
->limits
.discard_granularity
,
351 if (copy_from_user(&range
, arg
, sizeof(range
)))
353 if (range
.start
> total_bytes
||
354 range
.len
< fs_info
->sb
->s_blocksize
)
357 range
.len
= min(range
.len
, total_bytes
- range
.start
);
358 range
.minlen
= max(range
.minlen
, minlen
);
359 ret
= btrfs_trim_fs(fs_info
->tree_root
, &range
);
363 if (copy_to_user(arg
, &range
, sizeof(range
)))
369 int btrfs_is_empty_uuid(u8
*uuid
)
373 for (i
= 0; i
< BTRFS_UUID_SIZE
; i
++) {
380 static noinline
int create_subvol(struct inode
*dir
,
381 struct dentry
*dentry
,
382 char *name
, int namelen
,
384 struct btrfs_qgroup_inherit
*inherit
)
386 struct btrfs_trans_handle
*trans
;
387 struct btrfs_key key
;
388 struct btrfs_root_item root_item
;
389 struct btrfs_inode_item
*inode_item
;
390 struct extent_buffer
*leaf
;
391 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
392 struct btrfs_root
*new_root
;
393 struct btrfs_block_rsv block_rsv
;
394 struct timespec cur_time
= CURRENT_TIME
;
398 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
403 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
407 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
409 * The same as the snapshot creation, please see the comment
410 * of create_snapshot().
412 ret
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
413 8, &qgroup_reserved
, false);
417 trans
= btrfs_start_transaction(root
, 0);
419 ret
= PTR_ERR(trans
);
422 trans
->block_rsv
= &block_rsv
;
423 trans
->bytes_reserved
= block_rsv
.size
;
425 ret
= btrfs_qgroup_inherit(trans
, root
->fs_info
, 0, objectid
, inherit
);
429 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
430 0, objectid
, NULL
, 0, 0, 0);
436 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
437 btrfs_set_header_bytenr(leaf
, leaf
->start
);
438 btrfs_set_header_generation(leaf
, trans
->transid
);
439 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
440 btrfs_set_header_owner(leaf
, objectid
);
442 write_extent_buffer(leaf
, root
->fs_info
->fsid
, btrfs_header_fsid(),
444 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
445 btrfs_header_chunk_tree_uuid(leaf
),
447 btrfs_mark_buffer_dirty(leaf
);
449 memset(&root_item
, 0, sizeof(root_item
));
451 inode_item
= &root_item
.inode
;
452 btrfs_set_stack_inode_generation(inode_item
, 1);
453 btrfs_set_stack_inode_size(inode_item
, 3);
454 btrfs_set_stack_inode_nlink(inode_item
, 1);
455 btrfs_set_stack_inode_nbytes(inode_item
, root
->leafsize
);
456 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
458 btrfs_set_root_flags(&root_item
, 0);
459 btrfs_set_root_limit(&root_item
, 0);
460 btrfs_set_stack_inode_flags(inode_item
, BTRFS_INODE_ROOT_ITEM_INIT
);
462 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
463 btrfs_set_root_generation(&root_item
, trans
->transid
);
464 btrfs_set_root_level(&root_item
, 0);
465 btrfs_set_root_refs(&root_item
, 1);
466 btrfs_set_root_used(&root_item
, leaf
->len
);
467 btrfs_set_root_last_snapshot(&root_item
, 0);
469 btrfs_set_root_generation_v2(&root_item
,
470 btrfs_root_generation(&root_item
));
471 uuid_le_gen(&new_uuid
);
472 memcpy(root_item
.uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
473 btrfs_set_stack_timespec_sec(&root_item
.otime
, cur_time
.tv_sec
);
474 btrfs_set_stack_timespec_nsec(&root_item
.otime
, cur_time
.tv_nsec
);
475 root_item
.ctime
= root_item
.otime
;
476 btrfs_set_root_ctransid(&root_item
, trans
->transid
);
477 btrfs_set_root_otransid(&root_item
, trans
->transid
);
479 btrfs_tree_unlock(leaf
);
480 free_extent_buffer(leaf
);
483 btrfs_set_root_dirid(&root_item
, new_dirid
);
485 key
.objectid
= objectid
;
487 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
488 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
493 key
.offset
= (u64
)-1;
494 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
495 if (IS_ERR(new_root
)) {
496 btrfs_abort_transaction(trans
, root
, PTR_ERR(new_root
));
497 ret
= PTR_ERR(new_root
);
501 btrfs_record_root_in_trans(trans
, new_root
);
503 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
);
505 /* We potentially lose an unused inode item here */
506 btrfs_abort_transaction(trans
, root
, ret
);
511 * insert the directory item
513 ret
= btrfs_set_inode_index(dir
, &index
);
515 btrfs_abort_transaction(trans
, root
, ret
);
519 ret
= btrfs_insert_dir_item(trans
, root
,
520 name
, namelen
, dir
, &key
,
521 BTRFS_FT_DIR
, index
);
523 btrfs_abort_transaction(trans
, root
, ret
);
527 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
528 ret
= btrfs_update_inode(trans
, root
, dir
);
531 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
532 objectid
, root
->root_key
.objectid
,
533 btrfs_ino(dir
), index
, name
, namelen
);
536 ret
= btrfs_uuid_tree_add(trans
, root
->fs_info
->uuid_root
,
537 root_item
.uuid
, BTRFS_UUID_KEY_SUBVOL
,
540 btrfs_abort_transaction(trans
, root
, ret
);
543 trans
->block_rsv
= NULL
;
544 trans
->bytes_reserved
= 0;
546 *async_transid
= trans
->transid
;
547 err
= btrfs_commit_transaction_async(trans
, root
, 1);
549 err
= btrfs_commit_transaction(trans
, root
);
551 err
= btrfs_commit_transaction(trans
, root
);
557 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
559 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
563 static int create_snapshot(struct btrfs_root
*root
, struct inode
*dir
,
564 struct dentry
*dentry
, char *name
, int namelen
,
565 u64
*async_transid
, bool readonly
,
566 struct btrfs_qgroup_inherit
*inherit
)
569 struct btrfs_pending_snapshot
*pending_snapshot
;
570 struct btrfs_trans_handle
*trans
;
576 ret
= btrfs_start_delalloc_inodes(root
, 0);
580 btrfs_wait_ordered_extents(root
, -1);
582 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
583 if (!pending_snapshot
)
586 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
587 BTRFS_BLOCK_RSV_TEMP
);
589 * 1 - parent dir inode
592 * 2 - root ref/backref
593 * 1 - root of snapshot
596 ret
= btrfs_subvolume_reserve_metadata(BTRFS_I(dir
)->root
,
597 &pending_snapshot
->block_rsv
, 8,
598 &pending_snapshot
->qgroup_reserved
,
603 pending_snapshot
->dentry
= dentry
;
604 pending_snapshot
->root
= root
;
605 pending_snapshot
->readonly
= readonly
;
606 pending_snapshot
->dir
= dir
;
607 pending_snapshot
->inherit
= inherit
;
609 trans
= btrfs_start_transaction(root
, 0);
611 ret
= PTR_ERR(trans
);
615 spin_lock(&root
->fs_info
->trans_lock
);
616 list_add(&pending_snapshot
->list
,
617 &trans
->transaction
->pending_snapshots
);
618 spin_unlock(&root
->fs_info
->trans_lock
);
620 *async_transid
= trans
->transid
;
621 ret
= btrfs_commit_transaction_async(trans
,
622 root
->fs_info
->extent_root
, 1);
624 ret
= btrfs_commit_transaction(trans
, root
);
626 ret
= btrfs_commit_transaction(trans
,
627 root
->fs_info
->extent_root
);
632 ret
= pending_snapshot
->error
;
636 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
640 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
642 ret
= PTR_ERR(inode
);
646 d_instantiate(dentry
, inode
);
649 btrfs_subvolume_release_metadata(BTRFS_I(dir
)->root
,
650 &pending_snapshot
->block_rsv
,
651 pending_snapshot
->qgroup_reserved
);
653 kfree(pending_snapshot
);
657 /* copy of check_sticky in fs/namei.c()
658 * It's inline, so penalty for filesystems that don't use sticky bit is
661 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
663 kuid_t fsuid
= current_fsuid();
665 if (!(dir
->i_mode
& S_ISVTX
))
667 if (uid_eq(inode
->i_uid
, fsuid
))
669 if (uid_eq(dir
->i_uid
, fsuid
))
671 return !capable(CAP_FOWNER
);
674 /* copy of may_delete in fs/namei.c()
675 * Check whether we can remove a link victim from directory dir, check
676 * whether the type of victim is right.
677 * 1. We can't do it if dir is read-only (done in permission())
678 * 2. We should have write and exec permissions on dir
679 * 3. We can't remove anything from append-only dir
680 * 4. We can't do anything with immutable dir (done in permission())
681 * 5. If the sticky bit on dir is set we should either
682 * a. be owner of dir, or
683 * b. be owner of victim, or
684 * c. have CAP_FOWNER capability
685 * 6. If the victim is append-only or immutable we can't do antyhing with
686 * links pointing to it.
687 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
688 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
689 * 9. We can't remove a root or mountpoint.
690 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
691 * nfs_async_unlink().
694 static int btrfs_may_delete(struct inode
*dir
, struct dentry
*victim
, int isdir
)
698 if (!victim
->d_inode
)
701 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
702 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
704 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
709 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
710 IS_APPEND(victim
->d_inode
)||
711 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
714 if (!S_ISDIR(victim
->d_inode
->i_mode
))
718 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
722 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
727 /* copy of may_create in fs/namei.c() */
728 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
734 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
738 * Create a new subvolume below @parent. This is largely modeled after
739 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
740 * inside this filesystem so it's quite a bit simpler.
742 static noinline
int btrfs_mksubvol(struct path
*parent
,
743 char *name
, int namelen
,
744 struct btrfs_root
*snap_src
,
745 u64
*async_transid
, bool readonly
,
746 struct btrfs_qgroup_inherit
*inherit
)
748 struct inode
*dir
= parent
->dentry
->d_inode
;
749 struct dentry
*dentry
;
752 error
= mutex_lock_killable_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
756 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
757 error
= PTR_ERR(dentry
);
765 error
= btrfs_may_create(dir
, dentry
);
770 * even if this name doesn't exist, we may get hash collisions.
771 * check for them now when we can safely fail
773 error
= btrfs_check_dir_item_collision(BTRFS_I(dir
)->root
,
779 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
781 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
785 error
= create_snapshot(snap_src
, dir
, dentry
, name
, namelen
,
786 async_transid
, readonly
, inherit
);
788 error
= create_subvol(dir
, dentry
, name
, namelen
,
789 async_transid
, inherit
);
792 fsnotify_mkdir(dir
, dentry
);
794 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
798 mutex_unlock(&dir
->i_mutex
);
803 * When we're defragging a range, we don't want to kick it off again
804 * if it is really just waiting for delalloc to send it down.
805 * If we find a nice big extent or delalloc range for the bytes in the
806 * file you want to defrag, we return 0 to let you know to skip this
809 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, int thresh
)
811 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
812 struct extent_map
*em
= NULL
;
813 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
816 read_lock(&em_tree
->lock
);
817 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
818 read_unlock(&em_tree
->lock
);
821 end
= extent_map_end(em
);
823 if (end
- offset
> thresh
)
826 /* if we already have a nice delalloc here, just stop */
828 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
829 thresh
, EXTENT_DELALLOC
, 1);
836 * helper function to walk through a file and find extents
837 * newer than a specific transid, and smaller than thresh.
839 * This is used by the defragging code to find new and small
842 static int find_new_extents(struct btrfs_root
*root
,
843 struct inode
*inode
, u64 newer_than
,
844 u64
*off
, int thresh
)
846 struct btrfs_path
*path
;
847 struct btrfs_key min_key
;
848 struct extent_buffer
*leaf
;
849 struct btrfs_file_extent_item
*extent
;
852 u64 ino
= btrfs_ino(inode
);
854 path
= btrfs_alloc_path();
858 min_key
.objectid
= ino
;
859 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
860 min_key
.offset
= *off
;
862 path
->keep_locks
= 1;
865 ret
= btrfs_search_forward(root
, &min_key
, path
, newer_than
);
868 if (min_key
.objectid
!= ino
)
870 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
873 leaf
= path
->nodes
[0];
874 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
875 struct btrfs_file_extent_item
);
877 type
= btrfs_file_extent_type(leaf
, extent
);
878 if (type
== BTRFS_FILE_EXTENT_REG
&&
879 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
880 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
881 *off
= min_key
.offset
;
882 btrfs_free_path(path
);
886 if (min_key
.offset
== (u64
)-1)
890 btrfs_release_path(path
);
893 btrfs_free_path(path
);
897 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
899 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
900 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
901 struct extent_map
*em
;
902 u64 len
= PAGE_CACHE_SIZE
;
905 * hopefully we have this extent in the tree already, try without
906 * the full extent lock
908 read_lock(&em_tree
->lock
);
909 em
= lookup_extent_mapping(em_tree
, start
, len
);
910 read_unlock(&em_tree
->lock
);
913 /* get the big lock and read metadata off disk */
914 lock_extent(io_tree
, start
, start
+ len
- 1);
915 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
916 unlock_extent(io_tree
, start
, start
+ len
- 1);
925 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
927 struct extent_map
*next
;
930 /* this is the last extent */
931 if (em
->start
+ em
->len
>= i_size_read(inode
))
934 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
935 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
938 free_extent_map(next
);
942 static int should_defrag_range(struct inode
*inode
, u64 start
, int thresh
,
943 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
946 struct extent_map
*em
;
948 bool next_mergeable
= true;
951 * make sure that once we start defragging an extent, we keep on
954 if (start
< *defrag_end
)
959 em
= defrag_lookup_extent(inode
, start
);
963 /* this will cover holes, and inline extents */
964 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
969 next_mergeable
= defrag_check_next_extent(inode
, em
);
972 * we hit a real extent, if it is big or the next extent is not a
973 * real extent, don't bother defragging it
975 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
976 (em
->len
>= thresh
|| !next_mergeable
))
980 * last_len ends up being a counter of how many bytes we've defragged.
981 * every time we choose not to defrag an extent, we reset *last_len
982 * so that the next tiny extent will force a defrag.
984 * The end result of this is that tiny extents before a single big
985 * extent will force at least part of that big extent to be defragged.
988 *defrag_end
= extent_map_end(em
);
991 *skip
= extent_map_end(em
);
1000 * it doesn't do much good to defrag one or two pages
1001 * at a time. This pulls in a nice chunk of pages
1002 * to COW and defrag.
1004 * It also makes sure the delalloc code has enough
1005 * dirty data to avoid making new small extents as part
1008 * It's a good idea to start RA on this range
1009 * before calling this.
1011 static int cluster_pages_for_defrag(struct inode
*inode
,
1012 struct page
**pages
,
1013 unsigned long start_index
,
1016 unsigned long file_end
;
1017 u64 isize
= i_size_read(inode
);
1024 struct btrfs_ordered_extent
*ordered
;
1025 struct extent_state
*cached_state
= NULL
;
1026 struct extent_io_tree
*tree
;
1027 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
1029 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1030 if (!isize
|| start_index
> file_end
)
1033 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
1035 ret
= btrfs_delalloc_reserve_space(inode
,
1036 page_cnt
<< PAGE_CACHE_SHIFT
);
1040 tree
= &BTRFS_I(inode
)->io_tree
;
1042 /* step one, lock all the pages */
1043 for (i
= 0; i
< page_cnt
; i
++) {
1046 page
= find_or_create_page(inode
->i_mapping
,
1047 start_index
+ i
, mask
);
1051 page_start
= page_offset(page
);
1052 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1054 lock_extent(tree
, page_start
, page_end
);
1055 ordered
= btrfs_lookup_ordered_extent(inode
,
1057 unlock_extent(tree
, page_start
, page_end
);
1062 btrfs_start_ordered_extent(inode
, ordered
, 1);
1063 btrfs_put_ordered_extent(ordered
);
1066 * we unlocked the page above, so we need check if
1067 * it was released or not.
1069 if (page
->mapping
!= inode
->i_mapping
) {
1071 page_cache_release(page
);
1076 if (!PageUptodate(page
)) {
1077 btrfs_readpage(NULL
, page
);
1079 if (!PageUptodate(page
)) {
1081 page_cache_release(page
);
1087 if (page
->mapping
!= inode
->i_mapping
) {
1089 page_cache_release(page
);
1099 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1103 * so now we have a nice long stream of locked
1104 * and up to date pages, lets wait on them
1106 for (i
= 0; i
< i_done
; i
++)
1107 wait_on_page_writeback(pages
[i
]);
1109 page_start
= page_offset(pages
[0]);
1110 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
1112 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1113 page_start
, page_end
- 1, 0, &cached_state
);
1114 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1115 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1116 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 0, 0,
1117 &cached_state
, GFP_NOFS
);
1119 if (i_done
!= page_cnt
) {
1120 spin_lock(&BTRFS_I(inode
)->lock
);
1121 BTRFS_I(inode
)->outstanding_extents
++;
1122 spin_unlock(&BTRFS_I(inode
)->lock
);
1123 btrfs_delalloc_release_space(inode
,
1124 (page_cnt
- i_done
) << PAGE_CACHE_SHIFT
);
1128 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1129 &cached_state
, GFP_NOFS
);
1131 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1132 page_start
, page_end
- 1, &cached_state
,
1135 for (i
= 0; i
< i_done
; i
++) {
1136 clear_page_dirty_for_io(pages
[i
]);
1137 ClearPageChecked(pages
[i
]);
1138 set_page_extent_mapped(pages
[i
]);
1139 set_page_dirty(pages
[i
]);
1140 unlock_page(pages
[i
]);
1141 page_cache_release(pages
[i
]);
1145 for (i
= 0; i
< i_done
; i
++) {
1146 unlock_page(pages
[i
]);
1147 page_cache_release(pages
[i
]);
1149 btrfs_delalloc_release_space(inode
, page_cnt
<< PAGE_CACHE_SHIFT
);
1154 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1155 struct btrfs_ioctl_defrag_range_args
*range
,
1156 u64 newer_than
, unsigned long max_to_defrag
)
1158 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1159 struct file_ra_state
*ra
= NULL
;
1160 unsigned long last_index
;
1161 u64 isize
= i_size_read(inode
);
1165 u64 newer_off
= range
->start
;
1167 unsigned long ra_index
= 0;
1169 int defrag_count
= 0;
1170 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1171 int extent_thresh
= range
->extent_thresh
;
1172 int max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1173 int cluster
= max_cluster
;
1174 u64 new_align
= ~((u64
)128 * 1024 - 1);
1175 struct page
**pages
= NULL
;
1180 if (range
->start
>= isize
)
1183 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1184 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1186 if (range
->compress_type
)
1187 compress_type
= range
->compress_type
;
1190 if (extent_thresh
== 0)
1191 extent_thresh
= 256 * 1024;
1194 * if we were not given a file, allocate a readahead
1198 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1201 file_ra_state_init(ra
, inode
->i_mapping
);
1206 pages
= kmalloc_array(max_cluster
, sizeof(struct page
*),
1213 /* find the last page to defrag */
1214 if (range
->start
+ range
->len
> range
->start
) {
1215 last_index
= min_t(u64
, isize
- 1,
1216 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1218 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1222 ret
= find_new_extents(root
, inode
, newer_than
,
1223 &newer_off
, 64 * 1024);
1225 range
->start
= newer_off
;
1227 * we always align our defrag to help keep
1228 * the extents in the file evenly spaced
1230 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1234 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1237 max_to_defrag
= last_index
+ 1;
1240 * make writeback starts from i, so the defrag range can be
1241 * written sequentially.
1243 if (i
< inode
->i_mapping
->writeback_index
)
1244 inode
->i_mapping
->writeback_index
= i
;
1246 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1247 (i
< (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
1248 PAGE_CACHE_SHIFT
)) {
1250 * make sure we stop running if someone unmounts
1253 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1256 if (btrfs_defrag_cancelled(root
->fs_info
)) {
1257 printk(KERN_DEBUG
"btrfs: defrag_file cancelled\n");
1262 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1263 extent_thresh
, &last_len
, &skip
,
1264 &defrag_end
, range
->flags
&
1265 BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1268 * the should_defrag function tells us how much to skip
1269 * bump our counter by the suggested amount
1271 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1272 i
= max(i
+ 1, next
);
1277 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1278 PAGE_CACHE_SHIFT
) - i
;
1279 cluster
= min(cluster
, max_cluster
);
1281 cluster
= max_cluster
;
1284 if (i
+ cluster
> ra_index
) {
1285 ra_index
= max(i
, ra_index
);
1286 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1288 ra_index
+= max_cluster
;
1291 mutex_lock(&inode
->i_mutex
);
1292 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1293 BTRFS_I(inode
)->force_compress
= compress_type
;
1294 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1296 mutex_unlock(&inode
->i_mutex
);
1300 defrag_count
+= ret
;
1301 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1302 mutex_unlock(&inode
->i_mutex
);
1305 if (newer_off
== (u64
)-1)
1311 newer_off
= max(newer_off
+ 1,
1312 (u64
)i
<< PAGE_CACHE_SHIFT
);
1314 ret
= find_new_extents(root
, inode
,
1315 newer_than
, &newer_off
,
1318 range
->start
= newer_off
;
1319 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1326 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1334 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
1335 filemap_flush(inode
->i_mapping
);
1337 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1338 /* the filemap_flush will queue IO into the worker threads, but
1339 * we have to make sure the IO is actually started and that
1340 * ordered extents get created before we return
1342 atomic_inc(&root
->fs_info
->async_submit_draining
);
1343 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1344 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1345 wait_event(root
->fs_info
->async_submit_wait
,
1346 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1347 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1349 atomic_dec(&root
->fs_info
->async_submit_draining
);
1352 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1353 btrfs_set_fs_incompat(root
->fs_info
, COMPRESS_LZO
);
1359 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1360 mutex_lock(&inode
->i_mutex
);
1361 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1362 mutex_unlock(&inode
->i_mutex
);
1370 static noinline
int btrfs_ioctl_resize(struct file
*file
,
1376 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
1377 struct btrfs_ioctl_vol_args
*vol_args
;
1378 struct btrfs_trans_handle
*trans
;
1379 struct btrfs_device
*device
= NULL
;
1381 char *devstr
= NULL
;
1385 if (!capable(CAP_SYS_ADMIN
))
1388 ret
= mnt_want_write_file(file
);
1392 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
1394 mnt_drop_write_file(file
);
1395 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
1398 mutex_lock(&root
->fs_info
->volume_mutex
);
1399 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1400 if (IS_ERR(vol_args
)) {
1401 ret
= PTR_ERR(vol_args
);
1405 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1407 sizestr
= vol_args
->name
;
1408 devstr
= strchr(sizestr
, ':');
1411 sizestr
= devstr
+ 1;
1413 devstr
= vol_args
->name
;
1414 devid
= simple_strtoull(devstr
, &end
, 10);
1419 printk(KERN_INFO
"btrfs: resizing devid %llu\n", devid
);
1422 device
= btrfs_find_device(root
->fs_info
, devid
, NULL
, NULL
);
1424 printk(KERN_INFO
"btrfs: resizer unable to find device %llu\n",
1430 if (!device
->writeable
) {
1431 printk(KERN_INFO
"btrfs: resizer unable to apply on "
1432 "readonly device %llu\n",
1438 if (!strcmp(sizestr
, "max"))
1439 new_size
= device
->bdev
->bd_inode
->i_size
;
1441 if (sizestr
[0] == '-') {
1444 } else if (sizestr
[0] == '+') {
1448 new_size
= memparse(sizestr
, NULL
);
1449 if (new_size
== 0) {
1455 if (device
->is_tgtdev_for_dev_replace
) {
1460 old_size
= device
->total_bytes
;
1463 if (new_size
> old_size
) {
1467 new_size
= old_size
- new_size
;
1468 } else if (mod
> 0) {
1469 new_size
= old_size
+ new_size
;
1472 if (new_size
< 256 * 1024 * 1024) {
1476 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1481 do_div(new_size
, root
->sectorsize
);
1482 new_size
*= root
->sectorsize
;
1484 printk_in_rcu(KERN_INFO
"btrfs: new size for %s is %llu\n",
1485 rcu_str_deref(device
->name
), new_size
);
1487 if (new_size
> old_size
) {
1488 trans
= btrfs_start_transaction(root
, 0);
1489 if (IS_ERR(trans
)) {
1490 ret
= PTR_ERR(trans
);
1493 ret
= btrfs_grow_device(trans
, device
, new_size
);
1494 btrfs_commit_transaction(trans
, root
);
1495 } else if (new_size
< old_size
) {
1496 ret
= btrfs_shrink_device(device
, new_size
);
1497 } /* equal, nothing need to do */
1502 mutex_unlock(&root
->fs_info
->volume_mutex
);
1503 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
1504 mnt_drop_write_file(file
);
1508 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1509 char *name
, unsigned long fd
, int subvol
,
1510 u64
*transid
, bool readonly
,
1511 struct btrfs_qgroup_inherit
*inherit
)
1516 ret
= mnt_want_write_file(file
);
1520 namelen
= strlen(name
);
1521 if (strchr(name
, '/')) {
1523 goto out_drop_write
;
1526 if (name
[0] == '.' &&
1527 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1529 goto out_drop_write
;
1533 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1534 NULL
, transid
, readonly
, inherit
);
1536 struct fd src
= fdget(fd
);
1537 struct inode
*src_inode
;
1540 goto out_drop_write
;
1543 src_inode
= file_inode(src
.file
);
1544 if (src_inode
->i_sb
!= file_inode(file
)->i_sb
) {
1545 printk(KERN_INFO
"btrfs: Snapshot src from "
1549 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1550 BTRFS_I(src_inode
)->root
,
1551 transid
, readonly
, inherit
);
1556 mnt_drop_write_file(file
);
1561 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1562 void __user
*arg
, int subvol
)
1564 struct btrfs_ioctl_vol_args
*vol_args
;
1567 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1568 if (IS_ERR(vol_args
))
1569 return PTR_ERR(vol_args
);
1570 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1572 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1573 vol_args
->fd
, subvol
,
1580 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1581 void __user
*arg
, int subvol
)
1583 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1587 bool readonly
= false;
1588 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1590 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1591 if (IS_ERR(vol_args
))
1592 return PTR_ERR(vol_args
);
1593 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1595 if (vol_args
->flags
&
1596 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1597 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1602 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1604 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1606 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1607 if (vol_args
->size
> PAGE_CACHE_SIZE
) {
1611 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1612 if (IS_ERR(inherit
)) {
1613 ret
= PTR_ERR(inherit
);
1618 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1619 vol_args
->fd
, subvol
, ptr
,
1622 if (ret
== 0 && ptr
&&
1624 offsetof(struct btrfs_ioctl_vol_args_v2
,
1625 transid
), ptr
, sizeof(*ptr
)))
1633 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1636 struct inode
*inode
= file_inode(file
);
1637 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1641 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1644 down_read(&root
->fs_info
->subvol_sem
);
1645 if (btrfs_root_readonly(root
))
1646 flags
|= BTRFS_SUBVOL_RDONLY
;
1647 up_read(&root
->fs_info
->subvol_sem
);
1649 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1655 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1658 struct inode
*inode
= file_inode(file
);
1659 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1660 struct btrfs_trans_handle
*trans
;
1665 ret
= mnt_want_write_file(file
);
1669 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1671 goto out_drop_write
;
1674 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1676 goto out_drop_write
;
1679 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1681 goto out_drop_write
;
1684 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1686 goto out_drop_write
;
1689 if (!inode_owner_or_capable(inode
)) {
1691 goto out_drop_write
;
1694 down_write(&root
->fs_info
->subvol_sem
);
1697 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1700 root_flags
= btrfs_root_flags(&root
->root_item
);
1701 if (flags
& BTRFS_SUBVOL_RDONLY
)
1702 btrfs_set_root_flags(&root
->root_item
,
1703 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1705 btrfs_set_root_flags(&root
->root_item
,
1706 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1708 trans
= btrfs_start_transaction(root
, 1);
1709 if (IS_ERR(trans
)) {
1710 ret
= PTR_ERR(trans
);
1714 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1715 &root
->root_key
, &root
->root_item
);
1717 btrfs_commit_transaction(trans
, root
);
1720 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1722 up_write(&root
->fs_info
->subvol_sem
);
1724 mnt_drop_write_file(file
);
1730 * helper to check if the subvolume references other subvolumes
1732 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1734 struct btrfs_path
*path
;
1735 struct btrfs_dir_item
*di
;
1736 struct btrfs_key key
;
1740 path
= btrfs_alloc_path();
1744 /* Make sure this root isn't set as the default subvol */
1745 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
1746 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, path
,
1747 dir_id
, "default", 7, 0);
1748 if (di
&& !IS_ERR(di
)) {
1749 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1750 if (key
.objectid
== root
->root_key
.objectid
) {
1754 btrfs_release_path(path
);
1757 key
.objectid
= root
->root_key
.objectid
;
1758 key
.type
= BTRFS_ROOT_REF_KEY
;
1759 key
.offset
= (u64
)-1;
1761 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1768 if (path
->slots
[0] > 0) {
1770 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1771 if (key
.objectid
== root
->root_key
.objectid
&&
1772 key
.type
== BTRFS_ROOT_REF_KEY
)
1776 btrfs_free_path(path
);
1780 static noinline
int key_in_sk(struct btrfs_key
*key
,
1781 struct btrfs_ioctl_search_key
*sk
)
1783 struct btrfs_key test
;
1786 test
.objectid
= sk
->min_objectid
;
1787 test
.type
= sk
->min_type
;
1788 test
.offset
= sk
->min_offset
;
1790 ret
= btrfs_comp_cpu_keys(key
, &test
);
1794 test
.objectid
= sk
->max_objectid
;
1795 test
.type
= sk
->max_type
;
1796 test
.offset
= sk
->max_offset
;
1798 ret
= btrfs_comp_cpu_keys(key
, &test
);
1804 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1805 struct btrfs_path
*path
,
1806 struct btrfs_key
*key
,
1807 struct btrfs_ioctl_search_key
*sk
,
1809 unsigned long *sk_offset
,
1813 struct extent_buffer
*leaf
;
1814 struct btrfs_ioctl_search_header sh
;
1815 unsigned long item_off
;
1816 unsigned long item_len
;
1822 leaf
= path
->nodes
[0];
1823 slot
= path
->slots
[0];
1824 nritems
= btrfs_header_nritems(leaf
);
1826 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1830 found_transid
= btrfs_header_generation(leaf
);
1832 for (i
= slot
; i
< nritems
; i
++) {
1833 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1834 item_len
= btrfs_item_size_nr(leaf
, i
);
1836 btrfs_item_key_to_cpu(leaf
, key
, i
);
1837 if (!key_in_sk(key
, sk
))
1840 if (sizeof(sh
) + item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1843 if (sizeof(sh
) + item_len
+ *sk_offset
>
1844 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1849 sh
.objectid
= key
->objectid
;
1850 sh
.offset
= key
->offset
;
1851 sh
.type
= key
->type
;
1853 sh
.transid
= found_transid
;
1855 /* copy search result header */
1856 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1857 *sk_offset
+= sizeof(sh
);
1860 char *p
= buf
+ *sk_offset
;
1862 read_extent_buffer(leaf
, p
,
1863 item_off
, item_len
);
1864 *sk_offset
+= item_len
;
1868 if (*num_found
>= sk
->nr_items
)
1873 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1875 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1878 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1888 static noinline
int search_ioctl(struct inode
*inode
,
1889 struct btrfs_ioctl_search_args
*args
)
1891 struct btrfs_root
*root
;
1892 struct btrfs_key key
;
1893 struct btrfs_path
*path
;
1894 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1895 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1898 unsigned long sk_offset
= 0;
1900 path
= btrfs_alloc_path();
1904 if (sk
->tree_id
== 0) {
1905 /* search the root of the inode that was passed */
1906 root
= BTRFS_I(inode
)->root
;
1908 key
.objectid
= sk
->tree_id
;
1909 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1910 key
.offset
= (u64
)-1;
1911 root
= btrfs_read_fs_root_no_name(info
, &key
);
1913 printk(KERN_ERR
"could not find root %llu\n",
1915 btrfs_free_path(path
);
1920 key
.objectid
= sk
->min_objectid
;
1921 key
.type
= sk
->min_type
;
1922 key
.offset
= sk
->min_offset
;
1924 path
->keep_locks
= 1;
1927 ret
= btrfs_search_forward(root
, &key
, path
, sk
->min_transid
);
1933 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1934 &sk_offset
, &num_found
);
1935 btrfs_release_path(path
);
1936 if (ret
|| num_found
>= sk
->nr_items
)
1942 sk
->nr_items
= num_found
;
1943 btrfs_free_path(path
);
1947 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1950 struct btrfs_ioctl_search_args
*args
;
1951 struct inode
*inode
;
1954 if (!capable(CAP_SYS_ADMIN
))
1957 args
= memdup_user(argp
, sizeof(*args
));
1959 return PTR_ERR(args
);
1961 inode
= file_inode(file
);
1962 ret
= search_ioctl(inode
, args
);
1963 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1970 * Search INODE_REFs to identify path name of 'dirid' directory
1971 * in a 'tree_id' tree. and sets path name to 'name'.
1973 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1974 u64 tree_id
, u64 dirid
, char *name
)
1976 struct btrfs_root
*root
;
1977 struct btrfs_key key
;
1983 struct btrfs_inode_ref
*iref
;
1984 struct extent_buffer
*l
;
1985 struct btrfs_path
*path
;
1987 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1992 path
= btrfs_alloc_path();
1996 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1998 key
.objectid
= tree_id
;
1999 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2000 key
.offset
= (u64
)-1;
2001 root
= btrfs_read_fs_root_no_name(info
, &key
);
2003 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
2008 key
.objectid
= dirid
;
2009 key
.type
= BTRFS_INODE_REF_KEY
;
2010 key
.offset
= (u64
)-1;
2013 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2017 ret
= btrfs_previous_item(root
, path
, dirid
,
2018 BTRFS_INODE_REF_KEY
);
2028 slot
= path
->slots
[0];
2029 btrfs_item_key_to_cpu(l
, &key
, slot
);
2031 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
2032 len
= btrfs_inode_ref_name_len(l
, iref
);
2034 total_len
+= len
+ 1;
2036 ret
= -ENAMETOOLONG
;
2041 read_extent_buffer(l
, ptr
, (unsigned long)(iref
+ 1), len
);
2043 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
2046 btrfs_release_path(path
);
2047 key
.objectid
= key
.offset
;
2048 key
.offset
= (u64
)-1;
2049 dirid
= key
.objectid
;
2051 memmove(name
, ptr
, total_len
);
2052 name
[total_len
] = '\0';
2055 btrfs_free_path(path
);
2059 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
2062 struct btrfs_ioctl_ino_lookup_args
*args
;
2063 struct inode
*inode
;
2066 if (!capable(CAP_SYS_ADMIN
))
2069 args
= memdup_user(argp
, sizeof(*args
));
2071 return PTR_ERR(args
);
2073 inode
= file_inode(file
);
2075 if (args
->treeid
== 0)
2076 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
2078 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
2079 args
->treeid
, args
->objectid
,
2082 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
2089 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
2092 struct dentry
*parent
= file
->f_path
.dentry
;
2093 struct dentry
*dentry
;
2094 struct inode
*dir
= parent
->d_inode
;
2095 struct inode
*inode
;
2096 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2097 struct btrfs_root
*dest
= NULL
;
2098 struct btrfs_ioctl_vol_args
*vol_args
;
2099 struct btrfs_trans_handle
*trans
;
2100 struct btrfs_block_rsv block_rsv
;
2101 u64 qgroup_reserved
;
2106 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2107 if (IS_ERR(vol_args
))
2108 return PTR_ERR(vol_args
);
2110 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2111 namelen
= strlen(vol_args
->name
);
2112 if (strchr(vol_args
->name
, '/') ||
2113 strncmp(vol_args
->name
, "..", namelen
) == 0) {
2118 err
= mnt_want_write_file(file
);
2122 err
= mutex_lock_killable_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
2124 goto out_drop_write
;
2125 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
2126 if (IS_ERR(dentry
)) {
2127 err
= PTR_ERR(dentry
);
2128 goto out_unlock_dir
;
2131 if (!dentry
->d_inode
) {
2136 inode
= dentry
->d_inode
;
2137 dest
= BTRFS_I(inode
)->root
;
2138 if (!capable(CAP_SYS_ADMIN
)) {
2140 * Regular user. Only allow this with a special mount
2141 * option, when the user has write+exec access to the
2142 * subvol root, and when rmdir(2) would have been
2145 * Note that this is _not_ check that the subvol is
2146 * empty or doesn't contain data that we wouldn't
2147 * otherwise be able to delete.
2149 * Users who want to delete empty subvols should try
2153 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
2157 * Do not allow deletion if the parent dir is the same
2158 * as the dir to be deleted. That means the ioctl
2159 * must be called on the dentry referencing the root
2160 * of the subvol, not a random directory contained
2167 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
2172 /* check if subvolume may be deleted by a user */
2173 err
= btrfs_may_delete(dir
, dentry
, 1);
2177 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
2182 mutex_lock(&inode
->i_mutex
);
2183 err
= d_invalidate(dentry
);
2187 down_write(&root
->fs_info
->subvol_sem
);
2189 err
= may_destroy_subvol(dest
);
2193 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
2195 * One for dir inode, two for dir entries, two for root
2198 err
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
2199 5, &qgroup_reserved
, true);
2203 trans
= btrfs_start_transaction(root
, 0);
2204 if (IS_ERR(trans
)) {
2205 err
= PTR_ERR(trans
);
2208 trans
->block_rsv
= &block_rsv
;
2209 trans
->bytes_reserved
= block_rsv
.size
;
2211 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
2212 dest
->root_key
.objectid
,
2213 dentry
->d_name
.name
,
2214 dentry
->d_name
.len
);
2217 btrfs_abort_transaction(trans
, root
, ret
);
2221 btrfs_record_root_in_trans(trans
, dest
);
2223 memset(&dest
->root_item
.drop_progress
, 0,
2224 sizeof(dest
->root_item
.drop_progress
));
2225 dest
->root_item
.drop_level
= 0;
2226 btrfs_set_root_refs(&dest
->root_item
, 0);
2228 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
2229 ret
= btrfs_insert_orphan_item(trans
,
2230 root
->fs_info
->tree_root
,
2231 dest
->root_key
.objectid
);
2233 btrfs_abort_transaction(trans
, root
, ret
);
2239 ret
= btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
2240 dest
->root_item
.uuid
, BTRFS_UUID_KEY_SUBVOL
,
2241 dest
->root_key
.objectid
);
2242 if (ret
&& ret
!= -ENOENT
) {
2243 btrfs_abort_transaction(trans
, root
, ret
);
2247 if (!btrfs_is_empty_uuid(dest
->root_item
.received_uuid
)) {
2248 ret
= btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
2249 dest
->root_item
.received_uuid
,
2250 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
2251 dest
->root_key
.objectid
);
2252 if (ret
&& ret
!= -ENOENT
) {
2253 btrfs_abort_transaction(trans
, root
, ret
);
2260 trans
->block_rsv
= NULL
;
2261 trans
->bytes_reserved
= 0;
2262 ret
= btrfs_end_transaction(trans
, root
);
2265 inode
->i_flags
|= S_DEAD
;
2267 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
2269 up_write(&root
->fs_info
->subvol_sem
);
2271 mutex_unlock(&inode
->i_mutex
);
2273 shrink_dcache_sb(root
->fs_info
->sb
);
2274 btrfs_invalidate_inodes(dest
);
2278 if (dest
->cache_inode
) {
2279 iput(dest
->cache_inode
);
2280 dest
->cache_inode
= NULL
;
2286 mutex_unlock(&dir
->i_mutex
);
2288 mnt_drop_write_file(file
);
2294 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2296 struct inode
*inode
= file_inode(file
);
2297 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2298 struct btrfs_ioctl_defrag_range_args
*range
;
2301 ret
= mnt_want_write_file(file
);
2305 if (btrfs_root_readonly(root
)) {
2310 switch (inode
->i_mode
& S_IFMT
) {
2312 if (!capable(CAP_SYS_ADMIN
)) {
2316 ret
= btrfs_defrag_root(root
);
2319 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
);
2322 if (!(file
->f_mode
& FMODE_WRITE
)) {
2327 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2334 if (copy_from_user(range
, argp
,
2340 /* compression requires us to start the IO */
2341 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2342 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2343 range
->extent_thresh
= (u32
)-1;
2346 /* the rest are all set to zero by kzalloc */
2347 range
->len
= (u64
)-1;
2349 ret
= btrfs_defrag_file(file_inode(file
), file
,
2359 mnt_drop_write_file(file
);
2363 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2365 struct btrfs_ioctl_vol_args
*vol_args
;
2368 if (!capable(CAP_SYS_ADMIN
))
2371 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2373 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2376 mutex_lock(&root
->fs_info
->volume_mutex
);
2377 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2378 if (IS_ERR(vol_args
)) {
2379 ret
= PTR_ERR(vol_args
);
2383 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2384 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2388 mutex_unlock(&root
->fs_info
->volume_mutex
);
2389 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2393 static long btrfs_ioctl_rm_dev(struct file
*file
, void __user
*arg
)
2395 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
2396 struct btrfs_ioctl_vol_args
*vol_args
;
2399 if (!capable(CAP_SYS_ADMIN
))
2402 ret
= mnt_want_write_file(file
);
2406 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2407 if (IS_ERR(vol_args
)) {
2408 ret
= PTR_ERR(vol_args
);
2412 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2414 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2416 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2420 mutex_lock(&root
->fs_info
->volume_mutex
);
2421 ret
= btrfs_rm_device(root
, vol_args
->name
);
2422 mutex_unlock(&root
->fs_info
->volume_mutex
);
2423 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2427 mnt_drop_write_file(file
);
2431 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2433 struct btrfs_ioctl_fs_info_args
*fi_args
;
2434 struct btrfs_device
*device
;
2435 struct btrfs_device
*next
;
2436 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2439 if (!capable(CAP_SYS_ADMIN
))
2442 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2446 mutex_lock(&fs_devices
->device_list_mutex
);
2447 fi_args
->num_devices
= fs_devices
->num_devices
;
2448 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2450 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
2451 if (device
->devid
> fi_args
->max_id
)
2452 fi_args
->max_id
= device
->devid
;
2454 mutex_unlock(&fs_devices
->device_list_mutex
);
2456 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2463 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2465 struct btrfs_ioctl_dev_info_args
*di_args
;
2466 struct btrfs_device
*dev
;
2467 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2469 char *s_uuid
= NULL
;
2471 if (!capable(CAP_SYS_ADMIN
))
2474 di_args
= memdup_user(arg
, sizeof(*di_args
));
2475 if (IS_ERR(di_args
))
2476 return PTR_ERR(di_args
);
2478 if (!btrfs_is_empty_uuid(di_args
->uuid
))
2479 s_uuid
= di_args
->uuid
;
2481 mutex_lock(&fs_devices
->device_list_mutex
);
2482 dev
= btrfs_find_device(root
->fs_info
, di_args
->devid
, s_uuid
, NULL
);
2489 di_args
->devid
= dev
->devid
;
2490 di_args
->bytes_used
= dev
->bytes_used
;
2491 di_args
->total_bytes
= dev
->total_bytes
;
2492 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2494 struct rcu_string
*name
;
2497 name
= rcu_dereference(dev
->name
);
2498 strncpy(di_args
->path
, name
->str
, sizeof(di_args
->path
));
2500 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
2502 di_args
->path
[0] = '\0';
2506 mutex_unlock(&fs_devices
->device_list_mutex
);
2507 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2514 static struct page
*extent_same_get_page(struct inode
*inode
, u64 off
)
2518 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2520 index
= off
>> PAGE_CACHE_SHIFT
;
2522 page
= grab_cache_page(inode
->i_mapping
, index
);
2526 if (!PageUptodate(page
)) {
2527 if (extent_read_full_page_nolock(tree
, page
, btrfs_get_extent
,
2531 if (!PageUptodate(page
)) {
2533 page_cache_release(page
);
2542 static inline void lock_extent_range(struct inode
*inode
, u64 off
, u64 len
)
2544 /* do any pending delalloc/csum calc on src, one way or
2545 another, and lock file content */
2547 struct btrfs_ordered_extent
*ordered
;
2548 lock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2549 ordered
= btrfs_lookup_first_ordered_extent(inode
,
2552 !test_range_bit(&BTRFS_I(inode
)->io_tree
, off
,
2553 off
+ len
- 1, EXTENT_DELALLOC
, 0, NULL
))
2555 unlock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2557 btrfs_put_ordered_extent(ordered
);
2558 btrfs_wait_ordered_range(inode
, off
, len
);
2562 static void btrfs_double_unlock(struct inode
*inode1
, u64 loff1
,
2563 struct inode
*inode2
, u64 loff2
, u64 len
)
2565 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
2566 unlock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
2568 mutex_unlock(&inode1
->i_mutex
);
2569 mutex_unlock(&inode2
->i_mutex
);
2572 static void btrfs_double_lock(struct inode
*inode1
, u64 loff1
,
2573 struct inode
*inode2
, u64 loff2
, u64 len
)
2575 if (inode1
< inode2
) {
2576 swap(inode1
, inode2
);
2580 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_PARENT
);
2581 lock_extent_range(inode1
, loff1
, len
);
2582 if (inode1
!= inode2
) {
2583 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_CHILD
);
2584 lock_extent_range(inode2
, loff2
, len
);
2588 static int btrfs_cmp_data(struct inode
*src
, u64 loff
, struct inode
*dst
,
2589 u64 dst_loff
, u64 len
)
2592 struct page
*src_page
, *dst_page
;
2593 unsigned int cmp_len
= PAGE_CACHE_SIZE
;
2594 void *addr
, *dst_addr
;
2597 if (len
< PAGE_CACHE_SIZE
)
2600 src_page
= extent_same_get_page(src
, loff
);
2603 dst_page
= extent_same_get_page(dst
, dst_loff
);
2605 page_cache_release(src_page
);
2608 addr
= kmap_atomic(src_page
);
2609 dst_addr
= kmap_atomic(dst_page
);
2611 flush_dcache_page(src_page
);
2612 flush_dcache_page(dst_page
);
2614 if (memcmp(addr
, dst_addr
, cmp_len
))
2615 ret
= BTRFS_SAME_DATA_DIFFERS
;
2617 kunmap_atomic(addr
);
2618 kunmap_atomic(dst_addr
);
2619 page_cache_release(src_page
);
2620 page_cache_release(dst_page
);
2626 dst_loff
+= cmp_len
;
2633 static int extent_same_check_offsets(struct inode
*inode
, u64 off
, u64 len
)
2635 u64 bs
= BTRFS_I(inode
)->root
->fs_info
->sb
->s_blocksize
;
2637 if (off
+ len
> inode
->i_size
|| off
+ len
< off
)
2639 /* Check that we are block aligned - btrfs_clone() requires this */
2640 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
))
2646 static int btrfs_extent_same(struct inode
*src
, u64 loff
, u64 len
,
2647 struct inode
*dst
, u64 dst_loff
)
2652 * btrfs_clone() can't handle extents in the same file
2653 * yet. Once that works, we can drop this check and replace it
2654 * with a check for the same inode, but overlapping extents.
2659 btrfs_double_lock(src
, loff
, dst
, dst_loff
, len
);
2661 ret
= extent_same_check_offsets(src
, loff
, len
);
2665 ret
= extent_same_check_offsets(dst
, dst_loff
, len
);
2669 /* don't make the dst file partly checksummed */
2670 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
2671 (BTRFS_I(dst
)->flags
& BTRFS_INODE_NODATASUM
)) {
2676 ret
= btrfs_cmp_data(src
, loff
, dst
, dst_loff
, len
);
2678 ret
= btrfs_clone(src
, dst
, loff
, len
, len
, dst_loff
);
2681 btrfs_double_unlock(src
, loff
, dst
, dst_loff
, len
);
2686 #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2688 static long btrfs_ioctl_file_extent_same(struct file
*file
,
2689 struct btrfs_ioctl_same_args __user
*argp
)
2691 struct btrfs_ioctl_same_args
*same
;
2692 struct btrfs_ioctl_same_extent_info
*info
;
2693 struct inode
*src
= file_inode(file
);
2699 u64 bs
= BTRFS_I(src
)->root
->fs_info
->sb
->s_blocksize
;
2700 bool is_admin
= capable(CAP_SYS_ADMIN
);
2703 if (!(file
->f_mode
& FMODE_READ
))
2706 ret
= mnt_want_write_file(file
);
2710 if (get_user(count
, &argp
->dest_count
)) {
2715 size
= offsetof(struct btrfs_ioctl_same_args __user
, info
[count
]);
2717 same
= memdup_user(argp
, size
);
2720 ret
= PTR_ERR(same
);
2724 off
= same
->logical_offset
;
2728 * Limit the total length we will dedupe for each operation.
2729 * This is intended to bound the total time spent in this
2730 * ioctl to something sane.
2732 if (len
> BTRFS_MAX_DEDUPE_LEN
)
2733 len
= BTRFS_MAX_DEDUPE_LEN
;
2735 if (WARN_ON_ONCE(bs
< PAGE_CACHE_SIZE
)) {
2737 * Btrfs does not support blocksize < page_size. As a
2738 * result, btrfs_cmp_data() won't correctly handle
2739 * this situation without an update.
2746 if (S_ISDIR(src
->i_mode
))
2750 if (!S_ISREG(src
->i_mode
))
2753 /* pre-format output fields to sane values */
2754 for (i
= 0; i
< count
; i
++) {
2755 same
->info
[i
].bytes_deduped
= 0ULL;
2756 same
->info
[i
].status
= 0;
2759 for (i
= 0, info
= same
->info
; i
< count
; i
++, info
++) {
2761 struct fd dst_file
= fdget(info
->fd
);
2762 if (!dst_file
.file
) {
2763 info
->status
= -EBADF
;
2766 dst
= file_inode(dst_file
.file
);
2768 if (!(is_admin
|| (dst_file
.file
->f_mode
& FMODE_WRITE
))) {
2769 info
->status
= -EINVAL
;
2770 } else if (file
->f_path
.mnt
!= dst_file
.file
->f_path
.mnt
) {
2771 info
->status
= -EXDEV
;
2772 } else if (S_ISDIR(dst
->i_mode
)) {
2773 info
->status
= -EISDIR
;
2774 } else if (!S_ISREG(dst
->i_mode
)) {
2775 info
->status
= -EACCES
;
2777 info
->status
= btrfs_extent_same(src
, off
, len
, dst
,
2778 info
->logical_offset
);
2779 if (info
->status
== 0)
2780 info
->bytes_deduped
+= len
;
2785 ret
= copy_to_user(argp
, same
, size
);
2790 mnt_drop_write_file(file
);
2795 * btrfs_clone() - clone a range from inode file to another
2797 * @src: Inode to clone from
2798 * @inode: Inode to clone to
2799 * @off: Offset within source to start clone from
2800 * @olen: Original length, passed by user, of range to clone
2801 * @olen_aligned: Block-aligned value of olen, extent_same uses
2802 * identical values here
2803 * @destoff: Offset within @inode to start clone
2805 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
2806 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
)
2808 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2809 struct btrfs_path
*path
= NULL
;
2810 struct extent_buffer
*leaf
;
2811 struct btrfs_trans_handle
*trans
;
2813 struct btrfs_key key
;
2817 u64 len
= olen_aligned
;
2820 buf
= vmalloc(btrfs_level_size(root
, 0));
2824 path
= btrfs_alloc_path();
2832 key
.objectid
= btrfs_ino(src
);
2833 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2838 * note the key will change type as we walk through the
2841 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
2846 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2847 if (path
->slots
[0] >= nritems
) {
2848 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
2853 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2855 leaf
= path
->nodes
[0];
2856 slot
= path
->slots
[0];
2858 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2859 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2860 key
.objectid
!= btrfs_ino(src
))
2863 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2864 struct btrfs_file_extent_item
*extent
;
2867 struct btrfs_key new_key
;
2868 u64 disko
= 0, diskl
= 0;
2869 u64 datao
= 0, datal
= 0;
2873 size
= btrfs_item_size_nr(leaf
, slot
);
2874 read_extent_buffer(leaf
, buf
,
2875 btrfs_item_ptr_offset(leaf
, slot
),
2878 extent
= btrfs_item_ptr(leaf
, slot
,
2879 struct btrfs_file_extent_item
);
2880 comp
= btrfs_file_extent_compression(leaf
, extent
);
2881 type
= btrfs_file_extent_type(leaf
, extent
);
2882 if (type
== BTRFS_FILE_EXTENT_REG
||
2883 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2884 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2886 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2888 datao
= btrfs_file_extent_offset(leaf
, extent
);
2889 datal
= btrfs_file_extent_num_bytes(leaf
,
2891 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2892 /* take upper bound, may be compressed */
2893 datal
= btrfs_file_extent_ram_bytes(leaf
,
2896 btrfs_release_path(path
);
2898 if (key
.offset
+ datal
<= off
||
2899 key
.offset
>= off
+ len
- 1)
2902 memcpy(&new_key
, &key
, sizeof(new_key
));
2903 new_key
.objectid
= btrfs_ino(inode
);
2904 if (off
<= key
.offset
)
2905 new_key
.offset
= key
.offset
+ destoff
- off
;
2907 new_key
.offset
= destoff
;
2910 * 1 - adjusting old extent (we may have to split it)
2911 * 1 - add new extent
2914 trans
= btrfs_start_transaction(root
, 3);
2915 if (IS_ERR(trans
)) {
2916 ret
= PTR_ERR(trans
);
2920 if (type
== BTRFS_FILE_EXTENT_REG
||
2921 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2923 * a | --- range to clone ---| b
2924 * | ------------- extent ------------- |
2927 /* substract range b */
2928 if (key
.offset
+ datal
> off
+ len
)
2929 datal
= off
+ len
- key
.offset
;
2931 /* substract range a */
2932 if (off
> key
.offset
) {
2933 datao
+= off
- key
.offset
;
2934 datal
-= off
- key
.offset
;
2937 ret
= btrfs_drop_extents(trans
, root
, inode
,
2939 new_key
.offset
+ datal
,
2942 btrfs_abort_transaction(trans
, root
,
2944 btrfs_end_transaction(trans
, root
);
2948 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2951 btrfs_abort_transaction(trans
, root
,
2953 btrfs_end_transaction(trans
, root
);
2957 leaf
= path
->nodes
[0];
2958 slot
= path
->slots
[0];
2959 write_extent_buffer(leaf
, buf
,
2960 btrfs_item_ptr_offset(leaf
, slot
),
2963 extent
= btrfs_item_ptr(leaf
, slot
,
2964 struct btrfs_file_extent_item
);
2966 /* disko == 0 means it's a hole */
2970 btrfs_set_file_extent_offset(leaf
, extent
,
2972 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2975 inode_add_bytes(inode
, datal
);
2976 ret
= btrfs_inc_extent_ref(trans
, root
,
2978 root
->root_key
.objectid
,
2980 new_key
.offset
- datao
,
2983 btrfs_abort_transaction(trans
,
2986 btrfs_end_transaction(trans
,
2992 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2995 if (off
> key
.offset
) {
2996 skip
= off
- key
.offset
;
2997 new_key
.offset
+= skip
;
3000 if (key
.offset
+ datal
> off
+ len
)
3001 trim
= key
.offset
+ datal
- (off
+ len
);
3003 if (comp
&& (skip
|| trim
)) {
3005 btrfs_end_transaction(trans
, root
);
3008 size
-= skip
+ trim
;
3009 datal
-= skip
+ trim
;
3011 ret
= btrfs_drop_extents(trans
, root
, inode
,
3013 new_key
.offset
+ datal
,
3016 btrfs_abort_transaction(trans
, root
,
3018 btrfs_end_transaction(trans
, root
);
3022 ret
= btrfs_insert_empty_item(trans
, root
, path
,
3025 btrfs_abort_transaction(trans
, root
,
3027 btrfs_end_transaction(trans
, root
);
3033 btrfs_file_extent_calc_inline_size(0);
3034 memmove(buf
+start
, buf
+start
+skip
,
3038 leaf
= path
->nodes
[0];
3039 slot
= path
->slots
[0];
3040 write_extent_buffer(leaf
, buf
,
3041 btrfs_item_ptr_offset(leaf
, slot
),
3043 inode_add_bytes(inode
, datal
);
3046 btrfs_mark_buffer_dirty(leaf
);
3047 btrfs_release_path(path
);
3049 inode_inc_iversion(inode
);
3050 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
3053 * we round up to the block size at eof when
3054 * determining which extents to clone above,
3055 * but shouldn't round up the file size
3057 endoff
= new_key
.offset
+ datal
;
3058 if (endoff
> destoff
+olen
)
3059 endoff
= destoff
+olen
;
3060 if (endoff
> inode
->i_size
)
3061 btrfs_i_size_write(inode
, endoff
);
3063 ret
= btrfs_update_inode(trans
, root
, inode
);
3065 btrfs_abort_transaction(trans
, root
, ret
);
3066 btrfs_end_transaction(trans
, root
);
3069 ret
= btrfs_end_transaction(trans
, root
);
3072 btrfs_release_path(path
);
3078 btrfs_release_path(path
);
3079 btrfs_free_path(path
);
3084 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
3085 u64 off
, u64 olen
, u64 destoff
)
3087 struct inode
*inode
= file_inode(file
);
3088 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3093 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
3098 * - split compressed inline extents. annoying: we need to
3099 * decompress into destination's address_space (the file offset
3100 * may change, so source mapping won't do), then recompress (or
3101 * otherwise reinsert) a subrange.
3102 * - allow ranges within the same file to be cloned (provided
3103 * they don't overlap)?
3106 /* the destination must be opened for writing */
3107 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
3110 if (btrfs_root_readonly(root
))
3113 ret
= mnt_want_write_file(file
);
3117 src_file
= fdget(srcfd
);
3118 if (!src_file
.file
) {
3120 goto out_drop_write
;
3124 if (src_file
.file
->f_path
.mnt
!= file
->f_path
.mnt
)
3127 src
= file_inode(src_file
.file
);
3133 /* the src must be open for reading */
3134 if (!(src_file
.file
->f_mode
& FMODE_READ
))
3137 /* don't make the dst file partly checksummed */
3138 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3139 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
3143 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
3147 if (src
->i_sb
!= inode
->i_sb
)
3152 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
3153 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
3155 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
3156 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
3159 mutex_lock(&src
->i_mutex
);
3162 /* determine range to clone */
3164 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
3167 olen
= len
= src
->i_size
- off
;
3168 /* if we extend to eof, continue to block boundary */
3169 if (off
+ len
== src
->i_size
)
3170 len
= ALIGN(src
->i_size
, bs
) - off
;
3172 /* verify the end result is block aligned */
3173 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
3174 !IS_ALIGNED(destoff
, bs
))
3177 /* verify if ranges are overlapped within the same file */
3179 if (destoff
+ len
> off
&& destoff
< off
+ len
)
3183 if (destoff
> inode
->i_size
) {
3184 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
3189 /* truncate page cache pages from target inode range */
3190 truncate_inode_pages_range(&inode
->i_data
, destoff
,
3191 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
3193 lock_extent_range(src
, off
, len
);
3195 ret
= btrfs_clone(src
, inode
, off
, olen
, len
, destoff
);
3197 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+ len
- 1);
3199 mutex_unlock(&src
->i_mutex
);
3201 mutex_unlock(&inode
->i_mutex
);
3205 mnt_drop_write_file(file
);
3209 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
3211 struct btrfs_ioctl_clone_range_args args
;
3213 if (copy_from_user(&args
, argp
, sizeof(args
)))
3215 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
3216 args
.src_length
, args
.dest_offset
);
3220 * there are many ways the trans_start and trans_end ioctls can lead
3221 * to deadlocks. They should only be used by applications that
3222 * basically own the machine, and have a very in depth understanding
3223 * of all the possible deadlocks and enospc problems.
3225 static long btrfs_ioctl_trans_start(struct file
*file
)
3227 struct inode
*inode
= file_inode(file
);
3228 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3229 struct btrfs_trans_handle
*trans
;
3233 if (!capable(CAP_SYS_ADMIN
))
3237 if (file
->private_data
)
3241 if (btrfs_root_readonly(root
))
3244 ret
= mnt_want_write_file(file
);
3248 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
3251 trans
= btrfs_start_ioctl_transaction(root
);
3255 file
->private_data
= trans
;
3259 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
3260 mnt_drop_write_file(file
);
3265 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
3267 struct inode
*inode
= file_inode(file
);
3268 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3269 struct btrfs_root
*new_root
;
3270 struct btrfs_dir_item
*di
;
3271 struct btrfs_trans_handle
*trans
;
3272 struct btrfs_path
*path
;
3273 struct btrfs_key location
;
3274 struct btrfs_disk_key disk_key
;
3279 if (!capable(CAP_SYS_ADMIN
))
3282 ret
= mnt_want_write_file(file
);
3286 if (copy_from_user(&objectid
, argp
, sizeof(objectid
))) {
3292 objectid
= BTRFS_FS_TREE_OBJECTID
;
3294 location
.objectid
= objectid
;
3295 location
.type
= BTRFS_ROOT_ITEM_KEY
;
3296 location
.offset
= (u64
)-1;
3298 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
3299 if (IS_ERR(new_root
)) {
3300 ret
= PTR_ERR(new_root
);
3304 path
= btrfs_alloc_path();
3309 path
->leave_spinning
= 1;
3311 trans
= btrfs_start_transaction(root
, 1);
3312 if (IS_ERR(trans
)) {
3313 btrfs_free_path(path
);
3314 ret
= PTR_ERR(trans
);
3318 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
3319 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
3320 dir_id
, "default", 7, 1);
3321 if (IS_ERR_OR_NULL(di
)) {
3322 btrfs_free_path(path
);
3323 btrfs_end_transaction(trans
, root
);
3324 printk(KERN_ERR
"Umm, you don't have the default dir item, "
3325 "this isn't going to work\n");
3330 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
3331 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
3332 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3333 btrfs_free_path(path
);
3335 btrfs_set_fs_incompat(root
->fs_info
, DEFAULT_SUBVOL
);
3336 btrfs_end_transaction(trans
, root
);
3338 mnt_drop_write_file(file
);
3342 void btrfs_get_block_group_info(struct list_head
*groups_list
,
3343 struct btrfs_ioctl_space_info
*space
)
3345 struct btrfs_block_group_cache
*block_group
;
3347 space
->total_bytes
= 0;
3348 space
->used_bytes
= 0;
3350 list_for_each_entry(block_group
, groups_list
, list
) {
3351 space
->flags
= block_group
->flags
;
3352 space
->total_bytes
+= block_group
->key
.offset
;
3353 space
->used_bytes
+=
3354 btrfs_block_group_used(&block_group
->item
);
3358 static long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
3360 struct btrfs_ioctl_space_args space_args
;
3361 struct btrfs_ioctl_space_info space
;
3362 struct btrfs_ioctl_space_info
*dest
;
3363 struct btrfs_ioctl_space_info
*dest_orig
;
3364 struct btrfs_ioctl_space_info __user
*user_dest
;
3365 struct btrfs_space_info
*info
;
3366 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
3367 BTRFS_BLOCK_GROUP_SYSTEM
,
3368 BTRFS_BLOCK_GROUP_METADATA
,
3369 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
3376 if (copy_from_user(&space_args
,
3377 (struct btrfs_ioctl_space_args __user
*)arg
,
3378 sizeof(space_args
)))
3381 for (i
= 0; i
< num_types
; i
++) {
3382 struct btrfs_space_info
*tmp
;
3386 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
3388 if (tmp
->flags
== types
[i
]) {
3398 down_read(&info
->groups_sem
);
3399 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
3400 if (!list_empty(&info
->block_groups
[c
]))
3403 up_read(&info
->groups_sem
);
3406 /* space_slots == 0 means they are asking for a count */
3407 if (space_args
.space_slots
== 0) {
3408 space_args
.total_spaces
= slot_count
;
3412 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
3414 alloc_size
= sizeof(*dest
) * slot_count
;
3416 /* we generally have at most 6 or so space infos, one for each raid
3417 * level. So, a whole page should be more than enough for everyone
3419 if (alloc_size
> PAGE_CACHE_SIZE
)
3422 space_args
.total_spaces
= 0;
3423 dest
= kmalloc(alloc_size
, GFP_NOFS
);
3428 /* now we have a buffer to copy into */
3429 for (i
= 0; i
< num_types
; i
++) {
3430 struct btrfs_space_info
*tmp
;
3437 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
3439 if (tmp
->flags
== types
[i
]) {
3448 down_read(&info
->groups_sem
);
3449 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
3450 if (!list_empty(&info
->block_groups
[c
])) {
3451 btrfs_get_block_group_info(
3452 &info
->block_groups
[c
], &space
);
3453 memcpy(dest
, &space
, sizeof(space
));
3455 space_args
.total_spaces
++;
3461 up_read(&info
->groups_sem
);
3464 user_dest
= (struct btrfs_ioctl_space_info __user
*)
3465 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
3467 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
3472 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
3479 * there are many ways the trans_start and trans_end ioctls can lead
3480 * to deadlocks. They should only be used by applications that
3481 * basically own the machine, and have a very in depth understanding
3482 * of all the possible deadlocks and enospc problems.
3484 long btrfs_ioctl_trans_end(struct file
*file
)
3486 struct inode
*inode
= file_inode(file
);
3487 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3488 struct btrfs_trans_handle
*trans
;
3490 trans
= file
->private_data
;
3493 file
->private_data
= NULL
;
3495 btrfs_end_transaction(trans
, root
);
3497 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
3499 mnt_drop_write_file(file
);
3503 static noinline
long btrfs_ioctl_start_sync(struct btrfs_root
*root
,
3506 struct btrfs_trans_handle
*trans
;
3510 trans
= btrfs_attach_transaction_barrier(root
);
3511 if (IS_ERR(trans
)) {
3512 if (PTR_ERR(trans
) != -ENOENT
)
3513 return PTR_ERR(trans
);
3515 /* No running transaction, don't bother */
3516 transid
= root
->fs_info
->last_trans_committed
;
3519 transid
= trans
->transid
;
3520 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
3522 btrfs_end_transaction(trans
, root
);
3527 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
3532 static noinline
long btrfs_ioctl_wait_sync(struct btrfs_root
*root
,
3538 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
3541 transid
= 0; /* current trans */
3543 return btrfs_wait_for_commit(root
, transid
);
3546 static long btrfs_ioctl_scrub(struct file
*file
, void __user
*arg
)
3548 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
3549 struct btrfs_ioctl_scrub_args
*sa
;
3552 if (!capable(CAP_SYS_ADMIN
))
3555 sa
= memdup_user(arg
, sizeof(*sa
));
3559 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
)) {
3560 ret
= mnt_want_write_file(file
);
3565 ret
= btrfs_scrub_dev(root
->fs_info
, sa
->devid
, sa
->start
, sa
->end
,
3566 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
,
3569 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3572 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
))
3573 mnt_drop_write_file(file
);
3579 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
3581 if (!capable(CAP_SYS_ADMIN
))
3584 return btrfs_scrub_cancel(root
->fs_info
);
3587 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
3590 struct btrfs_ioctl_scrub_args
*sa
;
3593 if (!capable(CAP_SYS_ADMIN
))
3596 sa
= memdup_user(arg
, sizeof(*sa
));
3600 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
3602 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3609 static long btrfs_ioctl_get_dev_stats(struct btrfs_root
*root
,
3612 struct btrfs_ioctl_get_dev_stats
*sa
;
3615 sa
= memdup_user(arg
, sizeof(*sa
));
3619 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
3624 ret
= btrfs_get_dev_stats(root
, sa
);
3626 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3633 static long btrfs_ioctl_dev_replace(struct btrfs_root
*root
, void __user
*arg
)
3635 struct btrfs_ioctl_dev_replace_args
*p
;
3638 if (!capable(CAP_SYS_ADMIN
))
3641 p
= memdup_user(arg
, sizeof(*p
));
3646 case BTRFS_IOCTL_DEV_REPLACE_CMD_START
:
3647 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
) {
3652 &root
->fs_info
->mutually_exclusive_operation_running
,
3654 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3656 ret
= btrfs_dev_replace_start(root
, p
);
3658 &root
->fs_info
->mutually_exclusive_operation_running
,
3662 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
:
3663 btrfs_dev_replace_status(root
->fs_info
, p
);
3666 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
:
3667 ret
= btrfs_dev_replace_cancel(root
->fs_info
, p
);
3674 if (copy_to_user(arg
, p
, sizeof(*p
)))
3681 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
3687 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
3688 struct inode_fs_paths
*ipath
= NULL
;
3689 struct btrfs_path
*path
;
3691 if (!capable(CAP_DAC_READ_SEARCH
))
3694 path
= btrfs_alloc_path();
3700 ipa
= memdup_user(arg
, sizeof(*ipa
));
3707 size
= min_t(u32
, ipa
->size
, 4096);
3708 ipath
= init_ipath(size
, root
, path
);
3709 if (IS_ERR(ipath
)) {
3710 ret
= PTR_ERR(ipath
);
3715 ret
= paths_from_inode(ipa
->inum
, ipath
);
3719 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
3720 rel_ptr
= ipath
->fspath
->val
[i
] -
3721 (u64
)(unsigned long)ipath
->fspath
->val
;
3722 ipath
->fspath
->val
[i
] = rel_ptr
;
3725 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
3726 (void *)(unsigned long)ipath
->fspath
, size
);
3733 btrfs_free_path(path
);
3740 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
3742 struct btrfs_data_container
*inodes
= ctx
;
3743 const size_t c
= 3 * sizeof(u64
);
3745 if (inodes
->bytes_left
>= c
) {
3746 inodes
->bytes_left
-= c
;
3747 inodes
->val
[inodes
->elem_cnt
] = inum
;
3748 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
3749 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
3750 inodes
->elem_cnt
+= 3;
3752 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
3753 inodes
->bytes_left
= 0;
3754 inodes
->elem_missed
+= 3;
3760 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
3765 struct btrfs_ioctl_logical_ino_args
*loi
;
3766 struct btrfs_data_container
*inodes
= NULL
;
3767 struct btrfs_path
*path
= NULL
;
3769 if (!capable(CAP_SYS_ADMIN
))
3772 loi
= memdup_user(arg
, sizeof(*loi
));
3779 path
= btrfs_alloc_path();
3785 size
= min_t(u32
, loi
->size
, 64 * 1024);
3786 inodes
= init_data_container(size
);
3787 if (IS_ERR(inodes
)) {
3788 ret
= PTR_ERR(inodes
);
3793 ret
= iterate_inodes_from_logical(loi
->logical
, root
->fs_info
, path
,
3794 build_ino_list
, inodes
);
3800 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
3801 (void *)(unsigned long)inodes
, size
);
3806 btrfs_free_path(path
);
3813 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3814 struct btrfs_ioctl_balance_args
*bargs
)
3816 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3818 bargs
->flags
= bctl
->flags
;
3820 if (atomic_read(&fs_info
->balance_running
))
3821 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
3822 if (atomic_read(&fs_info
->balance_pause_req
))
3823 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
3824 if (atomic_read(&fs_info
->balance_cancel_req
))
3825 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
3827 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
3828 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
3829 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
3832 spin_lock(&fs_info
->balance_lock
);
3833 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3834 spin_unlock(&fs_info
->balance_lock
);
3836 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3840 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
3842 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
3843 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3844 struct btrfs_ioctl_balance_args
*bargs
;
3845 struct btrfs_balance_control
*bctl
;
3846 bool need_unlock
; /* for mut. excl. ops lock */
3849 if (!capable(CAP_SYS_ADMIN
))
3852 ret
= mnt_want_write_file(file
);
3857 if (!atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1)) {
3858 mutex_lock(&fs_info
->volume_mutex
);
3859 mutex_lock(&fs_info
->balance_mutex
);
3865 * mut. excl. ops lock is locked. Three possibilites:
3866 * (1) some other op is running
3867 * (2) balance is running
3868 * (3) balance is paused -- special case (think resume)
3870 mutex_lock(&fs_info
->balance_mutex
);
3871 if (fs_info
->balance_ctl
) {
3872 /* this is either (2) or (3) */
3873 if (!atomic_read(&fs_info
->balance_running
)) {
3874 mutex_unlock(&fs_info
->balance_mutex
);
3875 if (!mutex_trylock(&fs_info
->volume_mutex
))
3877 mutex_lock(&fs_info
->balance_mutex
);
3879 if (fs_info
->balance_ctl
&&
3880 !atomic_read(&fs_info
->balance_running
)) {
3882 need_unlock
= false;
3886 mutex_unlock(&fs_info
->balance_mutex
);
3887 mutex_unlock(&fs_info
->volume_mutex
);
3891 mutex_unlock(&fs_info
->balance_mutex
);
3897 mutex_unlock(&fs_info
->balance_mutex
);
3898 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
3903 BUG_ON(!atomic_read(&fs_info
->mutually_exclusive_operation_running
));
3906 bargs
= memdup_user(arg
, sizeof(*bargs
));
3907 if (IS_ERR(bargs
)) {
3908 ret
= PTR_ERR(bargs
);
3912 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
3913 if (!fs_info
->balance_ctl
) {
3918 bctl
= fs_info
->balance_ctl
;
3919 spin_lock(&fs_info
->balance_lock
);
3920 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3921 spin_unlock(&fs_info
->balance_lock
);
3929 if (fs_info
->balance_ctl
) {
3934 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3940 bctl
->fs_info
= fs_info
;
3942 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
3943 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
3944 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
3946 bctl
->flags
= bargs
->flags
;
3948 /* balance everything - no filters */
3949 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
3954 * Ownership of bctl and mutually_exclusive_operation_running
3955 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3956 * or, if restriper was paused all the way until unmount, in
3957 * free_fs_info. mutually_exclusive_operation_running is
3958 * cleared in __cancel_balance.
3960 need_unlock
= false;
3962 ret
= btrfs_balance(bctl
, bargs
);
3965 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3972 mutex_unlock(&fs_info
->balance_mutex
);
3973 mutex_unlock(&fs_info
->volume_mutex
);
3975 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3977 mnt_drop_write_file(file
);
3981 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
3983 if (!capable(CAP_SYS_ADMIN
))
3987 case BTRFS_BALANCE_CTL_PAUSE
:
3988 return btrfs_pause_balance(root
->fs_info
);
3989 case BTRFS_BALANCE_CTL_CANCEL
:
3990 return btrfs_cancel_balance(root
->fs_info
);
3996 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
3999 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4000 struct btrfs_ioctl_balance_args
*bargs
;
4003 if (!capable(CAP_SYS_ADMIN
))
4006 mutex_lock(&fs_info
->balance_mutex
);
4007 if (!fs_info
->balance_ctl
) {
4012 bargs
= kzalloc(sizeof(*bargs
), GFP_NOFS
);
4018 update_ioctl_balance_args(fs_info
, 1, bargs
);
4020 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4025 mutex_unlock(&fs_info
->balance_mutex
);
4029 static long btrfs_ioctl_quota_ctl(struct file
*file
, void __user
*arg
)
4031 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4032 struct btrfs_ioctl_quota_ctl_args
*sa
;
4033 struct btrfs_trans_handle
*trans
= NULL
;
4037 if (!capable(CAP_SYS_ADMIN
))
4040 ret
= mnt_want_write_file(file
);
4044 sa
= memdup_user(arg
, sizeof(*sa
));
4050 down_write(&root
->fs_info
->subvol_sem
);
4051 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 2);
4052 if (IS_ERR(trans
)) {
4053 ret
= PTR_ERR(trans
);
4058 case BTRFS_QUOTA_CTL_ENABLE
:
4059 ret
= btrfs_quota_enable(trans
, root
->fs_info
);
4061 case BTRFS_QUOTA_CTL_DISABLE
:
4062 ret
= btrfs_quota_disable(trans
, root
->fs_info
);
4069 err
= btrfs_commit_transaction(trans
, root
->fs_info
->tree_root
);
4074 up_write(&root
->fs_info
->subvol_sem
);
4076 mnt_drop_write_file(file
);
4080 static long btrfs_ioctl_qgroup_assign(struct file
*file
, void __user
*arg
)
4082 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4083 struct btrfs_ioctl_qgroup_assign_args
*sa
;
4084 struct btrfs_trans_handle
*trans
;
4088 if (!capable(CAP_SYS_ADMIN
))
4091 ret
= mnt_want_write_file(file
);
4095 sa
= memdup_user(arg
, sizeof(*sa
));
4101 trans
= btrfs_join_transaction(root
);
4102 if (IS_ERR(trans
)) {
4103 ret
= PTR_ERR(trans
);
4107 /* FIXME: check if the IDs really exist */
4109 ret
= btrfs_add_qgroup_relation(trans
, root
->fs_info
,
4112 ret
= btrfs_del_qgroup_relation(trans
, root
->fs_info
,
4116 err
= btrfs_end_transaction(trans
, root
);
4123 mnt_drop_write_file(file
);
4127 static long btrfs_ioctl_qgroup_create(struct file
*file
, void __user
*arg
)
4129 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4130 struct btrfs_ioctl_qgroup_create_args
*sa
;
4131 struct btrfs_trans_handle
*trans
;
4135 if (!capable(CAP_SYS_ADMIN
))
4138 ret
= mnt_want_write_file(file
);
4142 sa
= memdup_user(arg
, sizeof(*sa
));
4148 if (!sa
->qgroupid
) {
4153 trans
= btrfs_join_transaction(root
);
4154 if (IS_ERR(trans
)) {
4155 ret
= PTR_ERR(trans
);
4159 /* FIXME: check if the IDs really exist */
4161 ret
= btrfs_create_qgroup(trans
, root
->fs_info
, sa
->qgroupid
,
4164 ret
= btrfs_remove_qgroup(trans
, root
->fs_info
, sa
->qgroupid
);
4167 err
= btrfs_end_transaction(trans
, root
);
4174 mnt_drop_write_file(file
);
4178 static long btrfs_ioctl_qgroup_limit(struct file
*file
, void __user
*arg
)
4180 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4181 struct btrfs_ioctl_qgroup_limit_args
*sa
;
4182 struct btrfs_trans_handle
*trans
;
4187 if (!capable(CAP_SYS_ADMIN
))
4190 ret
= mnt_want_write_file(file
);
4194 sa
= memdup_user(arg
, sizeof(*sa
));
4200 trans
= btrfs_join_transaction(root
);
4201 if (IS_ERR(trans
)) {
4202 ret
= PTR_ERR(trans
);
4206 qgroupid
= sa
->qgroupid
;
4208 /* take the current subvol as qgroup */
4209 qgroupid
= root
->root_key
.objectid
;
4212 /* FIXME: check if the IDs really exist */
4213 ret
= btrfs_limit_qgroup(trans
, root
->fs_info
, qgroupid
, &sa
->lim
);
4215 err
= btrfs_end_transaction(trans
, root
);
4222 mnt_drop_write_file(file
);
4226 static long btrfs_ioctl_quota_rescan(struct file
*file
, void __user
*arg
)
4228 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4229 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4232 if (!capable(CAP_SYS_ADMIN
))
4235 ret
= mnt_want_write_file(file
);
4239 qsa
= memdup_user(arg
, sizeof(*qsa
));
4250 ret
= btrfs_qgroup_rescan(root
->fs_info
);
4255 mnt_drop_write_file(file
);
4259 static long btrfs_ioctl_quota_rescan_status(struct file
*file
, void __user
*arg
)
4261 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4262 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4265 if (!capable(CAP_SYS_ADMIN
))
4268 qsa
= kzalloc(sizeof(*qsa
), GFP_NOFS
);
4272 if (root
->fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
4274 qsa
->progress
= root
->fs_info
->qgroup_rescan_progress
.objectid
;
4277 if (copy_to_user(arg
, qsa
, sizeof(*qsa
)))
4284 static long btrfs_ioctl_quota_rescan_wait(struct file
*file
, void __user
*arg
)
4286 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4288 if (!capable(CAP_SYS_ADMIN
))
4291 return btrfs_qgroup_wait_for_completion(root
->fs_info
);
4294 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
4297 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
4298 struct inode
*inode
= file_inode(file
);
4299 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4300 struct btrfs_root_item
*root_item
= &root
->root_item
;
4301 struct btrfs_trans_handle
*trans
;
4302 struct timespec ct
= CURRENT_TIME
;
4304 int received_uuid_changed
;
4306 ret
= mnt_want_write_file(file
);
4310 down_write(&root
->fs_info
->subvol_sem
);
4312 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
4317 if (btrfs_root_readonly(root
)) {
4322 if (!inode_owner_or_capable(inode
)) {
4327 sa
= memdup_user(arg
, sizeof(*sa
));
4336 * 2 - uuid items (received uuid + subvol uuid)
4338 trans
= btrfs_start_transaction(root
, 3);
4339 if (IS_ERR(trans
)) {
4340 ret
= PTR_ERR(trans
);
4345 sa
->rtransid
= trans
->transid
;
4346 sa
->rtime
.sec
= ct
.tv_sec
;
4347 sa
->rtime
.nsec
= ct
.tv_nsec
;
4349 received_uuid_changed
= memcmp(root_item
->received_uuid
, sa
->uuid
,
4351 if (received_uuid_changed
&&
4352 !btrfs_is_empty_uuid(root_item
->received_uuid
))
4353 btrfs_uuid_tree_rem(trans
, root
->fs_info
->uuid_root
,
4354 root_item
->received_uuid
,
4355 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4356 root
->root_key
.objectid
);
4357 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
4358 btrfs_set_root_stransid(root_item
, sa
->stransid
);
4359 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
4360 btrfs_set_stack_timespec_sec(&root_item
->stime
, sa
->stime
.sec
);
4361 btrfs_set_stack_timespec_nsec(&root_item
->stime
, sa
->stime
.nsec
);
4362 btrfs_set_stack_timespec_sec(&root_item
->rtime
, sa
->rtime
.sec
);
4363 btrfs_set_stack_timespec_nsec(&root_item
->rtime
, sa
->rtime
.nsec
);
4365 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
4366 &root
->root_key
, &root
->root_item
);
4368 btrfs_end_transaction(trans
, root
);
4371 if (received_uuid_changed
&& !btrfs_is_empty_uuid(sa
->uuid
)) {
4372 ret
= btrfs_uuid_tree_add(trans
, root
->fs_info
->uuid_root
,
4374 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4375 root
->root_key
.objectid
);
4376 if (ret
< 0 && ret
!= -EEXIST
) {
4377 btrfs_abort_transaction(trans
, root
, ret
);
4381 ret
= btrfs_commit_transaction(trans
, root
);
4383 btrfs_abort_transaction(trans
, root
, ret
);
4387 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
4393 up_write(&root
->fs_info
->subvol_sem
);
4394 mnt_drop_write_file(file
);
4398 static int btrfs_ioctl_get_fslabel(struct file
*file
, void __user
*arg
)
4400 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4403 char label
[BTRFS_LABEL_SIZE
];
4405 spin_lock(&root
->fs_info
->super_lock
);
4406 memcpy(label
, root
->fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
4407 spin_unlock(&root
->fs_info
->super_lock
);
4409 len
= strnlen(label
, BTRFS_LABEL_SIZE
);
4411 if (len
== BTRFS_LABEL_SIZE
) {
4412 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4416 ret
= copy_to_user(arg
, label
, len
);
4418 return ret
? -EFAULT
: 0;
4421 static int btrfs_ioctl_set_fslabel(struct file
*file
, void __user
*arg
)
4423 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4424 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
4425 struct btrfs_trans_handle
*trans
;
4426 char label
[BTRFS_LABEL_SIZE
];
4429 if (!capable(CAP_SYS_ADMIN
))
4432 if (copy_from_user(label
, arg
, sizeof(label
)))
4435 if (strnlen(label
, BTRFS_LABEL_SIZE
) == BTRFS_LABEL_SIZE
) {
4436 pr_err("btrfs: unable to set label with more than %d bytes\n",
4437 BTRFS_LABEL_SIZE
- 1);
4441 ret
= mnt_want_write_file(file
);
4445 trans
= btrfs_start_transaction(root
, 0);
4446 if (IS_ERR(trans
)) {
4447 ret
= PTR_ERR(trans
);
4451 spin_lock(&root
->fs_info
->super_lock
);
4452 strcpy(super_block
->label
, label
);
4453 spin_unlock(&root
->fs_info
->super_lock
);
4454 ret
= btrfs_end_transaction(trans
, root
);
4457 mnt_drop_write_file(file
);
4461 long btrfs_ioctl(struct file
*file
, unsigned int
4462 cmd
, unsigned long arg
)
4464 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4465 void __user
*argp
= (void __user
*)arg
;
4468 case FS_IOC_GETFLAGS
:
4469 return btrfs_ioctl_getflags(file
, argp
);
4470 case FS_IOC_SETFLAGS
:
4471 return btrfs_ioctl_setflags(file
, argp
);
4472 case FS_IOC_GETVERSION
:
4473 return btrfs_ioctl_getversion(file
, argp
);
4475 return btrfs_ioctl_fitrim(file
, argp
);
4476 case BTRFS_IOC_SNAP_CREATE
:
4477 return btrfs_ioctl_snap_create(file
, argp
, 0);
4478 case BTRFS_IOC_SNAP_CREATE_V2
:
4479 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
4480 case BTRFS_IOC_SUBVOL_CREATE
:
4481 return btrfs_ioctl_snap_create(file
, argp
, 1);
4482 case BTRFS_IOC_SUBVOL_CREATE_V2
:
4483 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
4484 case BTRFS_IOC_SNAP_DESTROY
:
4485 return btrfs_ioctl_snap_destroy(file
, argp
);
4486 case BTRFS_IOC_SUBVOL_GETFLAGS
:
4487 return btrfs_ioctl_subvol_getflags(file
, argp
);
4488 case BTRFS_IOC_SUBVOL_SETFLAGS
:
4489 return btrfs_ioctl_subvol_setflags(file
, argp
);
4490 case BTRFS_IOC_DEFAULT_SUBVOL
:
4491 return btrfs_ioctl_default_subvol(file
, argp
);
4492 case BTRFS_IOC_DEFRAG
:
4493 return btrfs_ioctl_defrag(file
, NULL
);
4494 case BTRFS_IOC_DEFRAG_RANGE
:
4495 return btrfs_ioctl_defrag(file
, argp
);
4496 case BTRFS_IOC_RESIZE
:
4497 return btrfs_ioctl_resize(file
, argp
);
4498 case BTRFS_IOC_ADD_DEV
:
4499 return btrfs_ioctl_add_dev(root
, argp
);
4500 case BTRFS_IOC_RM_DEV
:
4501 return btrfs_ioctl_rm_dev(file
, argp
);
4502 case BTRFS_IOC_FS_INFO
:
4503 return btrfs_ioctl_fs_info(root
, argp
);
4504 case BTRFS_IOC_DEV_INFO
:
4505 return btrfs_ioctl_dev_info(root
, argp
);
4506 case BTRFS_IOC_BALANCE
:
4507 return btrfs_ioctl_balance(file
, NULL
);
4508 case BTRFS_IOC_CLONE
:
4509 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
4510 case BTRFS_IOC_CLONE_RANGE
:
4511 return btrfs_ioctl_clone_range(file
, argp
);
4512 case BTRFS_IOC_TRANS_START
:
4513 return btrfs_ioctl_trans_start(file
);
4514 case BTRFS_IOC_TRANS_END
:
4515 return btrfs_ioctl_trans_end(file
);
4516 case BTRFS_IOC_TREE_SEARCH
:
4517 return btrfs_ioctl_tree_search(file
, argp
);
4518 case BTRFS_IOC_INO_LOOKUP
:
4519 return btrfs_ioctl_ino_lookup(file
, argp
);
4520 case BTRFS_IOC_INO_PATHS
:
4521 return btrfs_ioctl_ino_to_path(root
, argp
);
4522 case BTRFS_IOC_LOGICAL_INO
:
4523 return btrfs_ioctl_logical_to_ino(root
, argp
);
4524 case BTRFS_IOC_SPACE_INFO
:
4525 return btrfs_ioctl_space_info(root
, argp
);
4526 case BTRFS_IOC_SYNC
: {
4529 ret
= btrfs_start_delalloc_roots(root
->fs_info
, 0);
4532 ret
= btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
4535 case BTRFS_IOC_START_SYNC
:
4536 return btrfs_ioctl_start_sync(root
, argp
);
4537 case BTRFS_IOC_WAIT_SYNC
:
4538 return btrfs_ioctl_wait_sync(root
, argp
);
4539 case BTRFS_IOC_SCRUB
:
4540 return btrfs_ioctl_scrub(file
, argp
);
4541 case BTRFS_IOC_SCRUB_CANCEL
:
4542 return btrfs_ioctl_scrub_cancel(root
, argp
);
4543 case BTRFS_IOC_SCRUB_PROGRESS
:
4544 return btrfs_ioctl_scrub_progress(root
, argp
);
4545 case BTRFS_IOC_BALANCE_V2
:
4546 return btrfs_ioctl_balance(file
, argp
);
4547 case BTRFS_IOC_BALANCE_CTL
:
4548 return btrfs_ioctl_balance_ctl(root
, arg
);
4549 case BTRFS_IOC_BALANCE_PROGRESS
:
4550 return btrfs_ioctl_balance_progress(root
, argp
);
4551 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
4552 return btrfs_ioctl_set_received_subvol(file
, argp
);
4553 case BTRFS_IOC_SEND
:
4554 return btrfs_ioctl_send(file
, argp
);
4555 case BTRFS_IOC_GET_DEV_STATS
:
4556 return btrfs_ioctl_get_dev_stats(root
, argp
);
4557 case BTRFS_IOC_QUOTA_CTL
:
4558 return btrfs_ioctl_quota_ctl(file
, argp
);
4559 case BTRFS_IOC_QGROUP_ASSIGN
:
4560 return btrfs_ioctl_qgroup_assign(file
, argp
);
4561 case BTRFS_IOC_QGROUP_CREATE
:
4562 return btrfs_ioctl_qgroup_create(file
, argp
);
4563 case BTRFS_IOC_QGROUP_LIMIT
:
4564 return btrfs_ioctl_qgroup_limit(file
, argp
);
4565 case BTRFS_IOC_QUOTA_RESCAN
:
4566 return btrfs_ioctl_quota_rescan(file
, argp
);
4567 case BTRFS_IOC_QUOTA_RESCAN_STATUS
:
4568 return btrfs_ioctl_quota_rescan_status(file
, argp
);
4569 case BTRFS_IOC_QUOTA_RESCAN_WAIT
:
4570 return btrfs_ioctl_quota_rescan_wait(file
, argp
);
4571 case BTRFS_IOC_DEV_REPLACE
:
4572 return btrfs_ioctl_dev_replace(root
, argp
);
4573 case BTRFS_IOC_GET_FSLABEL
:
4574 return btrfs_ioctl_get_fslabel(file
, argp
);
4575 case BTRFS_IOC_SET_FSLABEL
:
4576 return btrfs_ioctl_set_fslabel(file
, argp
);
4577 case BTRFS_IOC_FILE_EXTENT_SAME
:
4578 return btrfs_ioctl_file_extent_same(file
, argp
);