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>
50 #include "transaction.h"
51 #include "btrfs_inode.h"
52 #include "print-tree.h"
55 #include "inode-map.h"
57 #include "rcu-string.h"
59 #include "dev-replace.h"
61 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
62 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
);
64 /* Mask out flags that are inappropriate for the given type of inode. */
65 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
69 else if (S_ISREG(mode
))
70 return flags
& ~FS_DIRSYNC_FL
;
72 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
76 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
78 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
80 unsigned int iflags
= 0;
82 if (flags
& BTRFS_INODE_SYNC
)
84 if (flags
& BTRFS_INODE_IMMUTABLE
)
85 iflags
|= FS_IMMUTABLE_FL
;
86 if (flags
& BTRFS_INODE_APPEND
)
87 iflags
|= FS_APPEND_FL
;
88 if (flags
& BTRFS_INODE_NODUMP
)
89 iflags
|= FS_NODUMP_FL
;
90 if (flags
& BTRFS_INODE_NOATIME
)
91 iflags
|= FS_NOATIME_FL
;
92 if (flags
& BTRFS_INODE_DIRSYNC
)
93 iflags
|= FS_DIRSYNC_FL
;
94 if (flags
& BTRFS_INODE_NODATACOW
)
95 iflags
|= FS_NOCOW_FL
;
97 if ((flags
& BTRFS_INODE_COMPRESS
) && !(flags
& BTRFS_INODE_NOCOMPRESS
))
98 iflags
|= FS_COMPR_FL
;
99 else if (flags
& BTRFS_INODE_NOCOMPRESS
)
100 iflags
|= FS_NOCOMP_FL
;
106 * Update inode->i_flags based on the btrfs internal flags.
108 void btrfs_update_iflags(struct inode
*inode
)
110 struct btrfs_inode
*ip
= BTRFS_I(inode
);
112 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
114 if (ip
->flags
& BTRFS_INODE_SYNC
)
115 inode
->i_flags
|= S_SYNC
;
116 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
117 inode
->i_flags
|= S_IMMUTABLE
;
118 if (ip
->flags
& BTRFS_INODE_APPEND
)
119 inode
->i_flags
|= S_APPEND
;
120 if (ip
->flags
& BTRFS_INODE_NOATIME
)
121 inode
->i_flags
|= S_NOATIME
;
122 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
123 inode
->i_flags
|= S_DIRSYNC
;
127 * Inherit flags from the parent inode.
129 * Currently only the compression flags and the cow flags are inherited.
131 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
138 flags
= BTRFS_I(dir
)->flags
;
140 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
141 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
142 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
143 } else if (flags
& BTRFS_INODE_COMPRESS
) {
144 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
145 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
148 if (flags
& BTRFS_INODE_NODATACOW
) {
149 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
150 if (S_ISREG(inode
->i_mode
))
151 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
154 btrfs_update_iflags(inode
);
157 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
159 struct btrfs_inode
*ip
= BTRFS_I(file_inode(file
));
160 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
162 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
167 static int check_flags(unsigned int flags
)
169 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
170 FS_NOATIME_FL
| FS_NODUMP_FL
| \
171 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
172 FS_NOCOMP_FL
| FS_COMPR_FL
|
176 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
182 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
184 struct inode
*inode
= file_inode(file
);
185 struct btrfs_inode
*ip
= BTRFS_I(inode
);
186 struct btrfs_root
*root
= ip
->root
;
187 struct btrfs_trans_handle
*trans
;
188 unsigned int flags
, oldflags
;
191 unsigned int i_oldflags
;
194 if (btrfs_root_readonly(root
))
197 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
200 ret
= check_flags(flags
);
204 if (!inode_owner_or_capable(inode
))
207 ret
= mnt_want_write_file(file
);
211 mutex_lock(&inode
->i_mutex
);
213 ip_oldflags
= ip
->flags
;
214 i_oldflags
= inode
->i_flags
;
215 mode
= inode
->i_mode
;
217 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
218 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
219 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
220 if (!capable(CAP_LINUX_IMMUTABLE
)) {
226 if (flags
& FS_SYNC_FL
)
227 ip
->flags
|= BTRFS_INODE_SYNC
;
229 ip
->flags
&= ~BTRFS_INODE_SYNC
;
230 if (flags
& FS_IMMUTABLE_FL
)
231 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
233 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
234 if (flags
& FS_APPEND_FL
)
235 ip
->flags
|= BTRFS_INODE_APPEND
;
237 ip
->flags
&= ~BTRFS_INODE_APPEND
;
238 if (flags
& FS_NODUMP_FL
)
239 ip
->flags
|= BTRFS_INODE_NODUMP
;
241 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
242 if (flags
& FS_NOATIME_FL
)
243 ip
->flags
|= BTRFS_INODE_NOATIME
;
245 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
246 if (flags
& FS_DIRSYNC_FL
)
247 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
249 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
250 if (flags
& FS_NOCOW_FL
) {
253 * It's safe to turn csums off here, no extents exist.
254 * Otherwise we want the flag to reflect the real COW
255 * status of the file and will not set it.
257 if (inode
->i_size
== 0)
258 ip
->flags
|= BTRFS_INODE_NODATACOW
259 | BTRFS_INODE_NODATASUM
;
261 ip
->flags
|= BTRFS_INODE_NODATACOW
;
265 * Revert back under same assuptions as above
268 if (inode
->i_size
== 0)
269 ip
->flags
&= ~(BTRFS_INODE_NODATACOW
270 | BTRFS_INODE_NODATASUM
);
272 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
277 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
278 * flag may be changed automatically if compression code won't make
281 if (flags
& FS_NOCOMP_FL
) {
282 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
283 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
284 } else if (flags
& FS_COMPR_FL
) {
285 ip
->flags
|= BTRFS_INODE_COMPRESS
;
286 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
288 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
291 trans
= btrfs_start_transaction(root
, 1);
293 ret
= PTR_ERR(trans
);
297 btrfs_update_iflags(inode
);
298 inode_inc_iversion(inode
);
299 inode
->i_ctime
= CURRENT_TIME
;
300 ret
= btrfs_update_inode(trans
, root
, inode
);
302 btrfs_end_transaction(trans
, root
);
305 ip
->flags
= ip_oldflags
;
306 inode
->i_flags
= i_oldflags
;
310 mutex_unlock(&inode
->i_mutex
);
311 mnt_drop_write_file(file
);
315 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
317 struct inode
*inode
= file_inode(file
);
319 return put_user(inode
->i_generation
, arg
);
322 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
324 struct btrfs_fs_info
*fs_info
= btrfs_sb(fdentry(file
)->d_sb
);
325 struct btrfs_device
*device
;
326 struct request_queue
*q
;
327 struct fstrim_range range
;
328 u64 minlen
= ULLONG_MAX
;
330 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
333 if (!capable(CAP_SYS_ADMIN
))
337 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
341 q
= bdev_get_queue(device
->bdev
);
342 if (blk_queue_discard(q
)) {
344 minlen
= min((u64
)q
->limits
.discard_granularity
,
352 if (copy_from_user(&range
, arg
, sizeof(range
)))
354 if (range
.start
> total_bytes
||
355 range
.len
< fs_info
->sb
->s_blocksize
)
358 range
.len
= min(range
.len
, total_bytes
- range
.start
);
359 range
.minlen
= max(range
.minlen
, minlen
);
360 ret
= btrfs_trim_fs(fs_info
->tree_root
, &range
);
364 if (copy_to_user(arg
, &range
, sizeof(range
)))
370 static noinline
int create_subvol(struct inode
*dir
,
371 struct dentry
*dentry
,
372 char *name
, int namelen
,
374 struct btrfs_qgroup_inherit
*inherit
)
376 struct btrfs_trans_handle
*trans
;
377 struct btrfs_key key
;
378 struct btrfs_root_item root_item
;
379 struct btrfs_inode_item
*inode_item
;
380 struct extent_buffer
*leaf
;
381 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
382 struct btrfs_root
*new_root
;
383 struct btrfs_block_rsv block_rsv
;
384 struct timespec cur_time
= CURRENT_TIME
;
388 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
393 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
397 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
399 * The same as the snapshot creation, please see the comment
400 * of create_snapshot().
402 ret
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
,
403 7, &qgroup_reserved
, false);
407 trans
= btrfs_start_transaction(root
, 0);
409 ret
= PTR_ERR(trans
);
412 trans
->block_rsv
= &block_rsv
;
413 trans
->bytes_reserved
= block_rsv
.size
;
415 ret
= btrfs_qgroup_inherit(trans
, root
->fs_info
, 0, objectid
, inherit
);
419 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
420 0, objectid
, NULL
, 0, 0, 0);
426 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
427 btrfs_set_header_bytenr(leaf
, leaf
->start
);
428 btrfs_set_header_generation(leaf
, trans
->transid
);
429 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
430 btrfs_set_header_owner(leaf
, objectid
);
432 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
433 (unsigned long)btrfs_header_fsid(leaf
),
435 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
436 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
438 btrfs_mark_buffer_dirty(leaf
);
440 memset(&root_item
, 0, sizeof(root_item
));
442 inode_item
= &root_item
.inode
;
443 btrfs_set_stack_inode_generation(inode_item
, 1);
444 btrfs_set_stack_inode_size(inode_item
, 3);
445 btrfs_set_stack_inode_nlink(inode_item
, 1);
446 btrfs_set_stack_inode_nbytes(inode_item
, root
->leafsize
);
447 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
449 btrfs_set_root_flags(&root_item
, 0);
450 btrfs_set_root_limit(&root_item
, 0);
451 btrfs_set_stack_inode_flags(inode_item
, BTRFS_INODE_ROOT_ITEM_INIT
);
453 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
454 btrfs_set_root_generation(&root_item
, trans
->transid
);
455 btrfs_set_root_level(&root_item
, 0);
456 btrfs_set_root_refs(&root_item
, 1);
457 btrfs_set_root_used(&root_item
, leaf
->len
);
458 btrfs_set_root_last_snapshot(&root_item
, 0);
460 btrfs_set_root_generation_v2(&root_item
,
461 btrfs_root_generation(&root_item
));
462 uuid_le_gen(&new_uuid
);
463 memcpy(root_item
.uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
464 btrfs_set_stack_timespec_sec(&root_item
.otime
, cur_time
.tv_sec
);
465 btrfs_set_stack_timespec_nsec(&root_item
.otime
, cur_time
.tv_nsec
);
466 root_item
.ctime
= root_item
.otime
;
467 btrfs_set_root_ctransid(&root_item
, trans
->transid
);
468 btrfs_set_root_otransid(&root_item
, trans
->transid
);
470 btrfs_tree_unlock(leaf
);
471 free_extent_buffer(leaf
);
474 btrfs_set_root_dirid(&root_item
, new_dirid
);
476 key
.objectid
= objectid
;
478 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
479 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
484 key
.offset
= (u64
)-1;
485 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
486 if (IS_ERR(new_root
)) {
487 btrfs_abort_transaction(trans
, root
, PTR_ERR(new_root
));
488 ret
= PTR_ERR(new_root
);
492 btrfs_record_root_in_trans(trans
, new_root
);
494 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
);
496 /* We potentially lose an unused inode item here */
497 btrfs_abort_transaction(trans
, root
, ret
);
502 * insert the directory item
504 ret
= btrfs_set_inode_index(dir
, &index
);
506 btrfs_abort_transaction(trans
, root
, ret
);
510 ret
= btrfs_insert_dir_item(trans
, root
,
511 name
, namelen
, dir
, &key
,
512 BTRFS_FT_DIR
, index
);
514 btrfs_abort_transaction(trans
, root
, ret
);
518 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
519 ret
= btrfs_update_inode(trans
, root
, dir
);
522 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
523 objectid
, root
->root_key
.objectid
,
524 btrfs_ino(dir
), index
, name
, namelen
);
529 trans
->block_rsv
= NULL
;
530 trans
->bytes_reserved
= 0;
532 *async_transid
= trans
->transid
;
533 err
= btrfs_commit_transaction_async(trans
, root
, 1);
535 err
= btrfs_commit_transaction(trans
, root
);
537 err
= btrfs_commit_transaction(trans
, root
);
543 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
545 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
549 static int create_snapshot(struct btrfs_root
*root
, struct inode
*dir
,
550 struct dentry
*dentry
, char *name
, int namelen
,
551 u64
*async_transid
, bool readonly
,
552 struct btrfs_qgroup_inherit
*inherit
)
555 struct btrfs_pending_snapshot
*pending_snapshot
;
556 struct btrfs_trans_handle
*trans
;
562 ret
= btrfs_start_delalloc_inodes(root
, 0);
566 btrfs_wait_ordered_extents(root
, 0);
568 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
569 if (!pending_snapshot
)
572 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
573 BTRFS_BLOCK_RSV_TEMP
);
575 * 1 - parent dir inode
578 * 2 - root ref/backref
579 * 1 - root of snapshot
581 ret
= btrfs_subvolume_reserve_metadata(BTRFS_I(dir
)->root
,
582 &pending_snapshot
->block_rsv
, 7,
583 &pending_snapshot
->qgroup_reserved
,
588 pending_snapshot
->dentry
= dentry
;
589 pending_snapshot
->root
= root
;
590 pending_snapshot
->readonly
= readonly
;
591 pending_snapshot
->dir
= dir
;
592 pending_snapshot
->inherit
= inherit
;
594 trans
= btrfs_start_transaction(root
, 0);
596 ret
= PTR_ERR(trans
);
600 spin_lock(&root
->fs_info
->trans_lock
);
601 list_add(&pending_snapshot
->list
,
602 &trans
->transaction
->pending_snapshots
);
603 spin_unlock(&root
->fs_info
->trans_lock
);
605 *async_transid
= trans
->transid
;
606 ret
= btrfs_commit_transaction_async(trans
,
607 root
->fs_info
->extent_root
, 1);
609 ret
= btrfs_commit_transaction(trans
, root
);
611 ret
= btrfs_commit_transaction(trans
,
612 root
->fs_info
->extent_root
);
617 ret
= pending_snapshot
->error
;
621 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
625 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
627 ret
= PTR_ERR(inode
);
631 d_instantiate(dentry
, inode
);
634 btrfs_subvolume_release_metadata(BTRFS_I(dir
)->root
,
635 &pending_snapshot
->block_rsv
,
636 pending_snapshot
->qgroup_reserved
);
638 kfree(pending_snapshot
);
642 /* copy of check_sticky in fs/namei.c()
643 * It's inline, so penalty for filesystems that don't use sticky bit is
646 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
648 kuid_t fsuid
= current_fsuid();
650 if (!(dir
->i_mode
& S_ISVTX
))
652 if (uid_eq(inode
->i_uid
, fsuid
))
654 if (uid_eq(dir
->i_uid
, fsuid
))
656 return !capable(CAP_FOWNER
);
659 /* copy of may_delete in fs/namei.c()
660 * Check whether we can remove a link victim from directory dir, check
661 * whether the type of victim is right.
662 * 1. We can't do it if dir is read-only (done in permission())
663 * 2. We should have write and exec permissions on dir
664 * 3. We can't remove anything from append-only dir
665 * 4. We can't do anything with immutable dir (done in permission())
666 * 5. If the sticky bit on dir is set we should either
667 * a. be owner of dir, or
668 * b. be owner of victim, or
669 * c. have CAP_FOWNER capability
670 * 6. If the victim is append-only or immutable we can't do antyhing with
671 * links pointing to it.
672 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
673 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
674 * 9. We can't remove a root or mountpoint.
675 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
676 * nfs_async_unlink().
679 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
683 if (!victim
->d_inode
)
686 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
687 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
689 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
694 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
695 IS_APPEND(victim
->d_inode
)||
696 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
699 if (!S_ISDIR(victim
->d_inode
->i_mode
))
703 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
707 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
712 /* copy of may_create in fs/namei.c() */
713 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
719 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
723 * Create a new subvolume below @parent. This is largely modeled after
724 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
725 * inside this filesystem so it's quite a bit simpler.
727 static noinline
int btrfs_mksubvol(struct path
*parent
,
728 char *name
, int namelen
,
729 struct btrfs_root
*snap_src
,
730 u64
*async_transid
, bool readonly
,
731 struct btrfs_qgroup_inherit
*inherit
)
733 struct inode
*dir
= parent
->dentry
->d_inode
;
734 struct dentry
*dentry
;
737 error
= mutex_lock_killable_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
741 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
742 error
= PTR_ERR(dentry
);
750 error
= btrfs_may_create(dir
, dentry
);
755 * even if this name doesn't exist, we may get hash collisions.
756 * check for them now when we can safely fail
758 error
= btrfs_check_dir_item_collision(BTRFS_I(dir
)->root
,
764 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
766 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
770 error
= create_snapshot(snap_src
, dir
, dentry
, name
, namelen
,
771 async_transid
, readonly
, inherit
);
773 error
= create_subvol(dir
, dentry
, name
, namelen
,
774 async_transid
, inherit
);
777 fsnotify_mkdir(dir
, dentry
);
779 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
783 mutex_unlock(&dir
->i_mutex
);
788 * When we're defragging a range, we don't want to kick it off again
789 * if it is really just waiting for delalloc to send it down.
790 * If we find a nice big extent or delalloc range for the bytes in the
791 * file you want to defrag, we return 0 to let you know to skip this
794 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, int thresh
)
796 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
797 struct extent_map
*em
= NULL
;
798 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
801 read_lock(&em_tree
->lock
);
802 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
803 read_unlock(&em_tree
->lock
);
806 end
= extent_map_end(em
);
808 if (end
- offset
> thresh
)
811 /* if we already have a nice delalloc here, just stop */
813 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
814 thresh
, EXTENT_DELALLOC
, 1);
821 * helper function to walk through a file and find extents
822 * newer than a specific transid, and smaller than thresh.
824 * This is used by the defragging code to find new and small
827 static int find_new_extents(struct btrfs_root
*root
,
828 struct inode
*inode
, u64 newer_than
,
829 u64
*off
, int thresh
)
831 struct btrfs_path
*path
;
832 struct btrfs_key min_key
;
833 struct btrfs_key max_key
;
834 struct extent_buffer
*leaf
;
835 struct btrfs_file_extent_item
*extent
;
838 u64 ino
= btrfs_ino(inode
);
840 path
= btrfs_alloc_path();
844 min_key
.objectid
= ino
;
845 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
846 min_key
.offset
= *off
;
848 max_key
.objectid
= ino
;
849 max_key
.type
= (u8
)-1;
850 max_key
.offset
= (u64
)-1;
852 path
->keep_locks
= 1;
855 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
859 if (min_key
.objectid
!= ino
)
861 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
864 leaf
= path
->nodes
[0];
865 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
866 struct btrfs_file_extent_item
);
868 type
= btrfs_file_extent_type(leaf
, extent
);
869 if (type
== BTRFS_FILE_EXTENT_REG
&&
870 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
871 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
872 *off
= min_key
.offset
;
873 btrfs_free_path(path
);
877 if (min_key
.offset
== (u64
)-1)
881 btrfs_release_path(path
);
884 btrfs_free_path(path
);
888 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
890 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
891 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
892 struct extent_map
*em
;
893 u64 len
= PAGE_CACHE_SIZE
;
896 * hopefully we have this extent in the tree already, try without
897 * the full extent lock
899 read_lock(&em_tree
->lock
);
900 em
= lookup_extent_mapping(em_tree
, start
, len
);
901 read_unlock(&em_tree
->lock
);
904 /* get the big lock and read metadata off disk */
905 lock_extent(io_tree
, start
, start
+ len
- 1);
906 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
907 unlock_extent(io_tree
, start
, start
+ len
- 1);
916 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
918 struct extent_map
*next
;
921 /* this is the last extent */
922 if (em
->start
+ em
->len
>= i_size_read(inode
))
925 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
926 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
929 free_extent_map(next
);
933 static int should_defrag_range(struct inode
*inode
, u64 start
, int thresh
,
934 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
937 struct extent_map
*em
;
939 bool next_mergeable
= true;
942 * make sure that once we start defragging an extent, we keep on
945 if (start
< *defrag_end
)
950 em
= defrag_lookup_extent(inode
, start
);
954 /* this will cover holes, and inline extents */
955 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
960 next_mergeable
= defrag_check_next_extent(inode
, em
);
963 * we hit a real extent, if it is big or the next extent is not a
964 * real extent, don't bother defragging it
966 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
967 (em
->len
>= thresh
|| !next_mergeable
))
971 * last_len ends up being a counter of how many bytes we've defragged.
972 * every time we choose not to defrag an extent, we reset *last_len
973 * so that the next tiny extent will force a defrag.
975 * The end result of this is that tiny extents before a single big
976 * extent will force at least part of that big extent to be defragged.
979 *defrag_end
= extent_map_end(em
);
982 *skip
= extent_map_end(em
);
991 * it doesn't do much good to defrag one or two pages
992 * at a time. This pulls in a nice chunk of pages
995 * It also makes sure the delalloc code has enough
996 * dirty data to avoid making new small extents as part
999 * It's a good idea to start RA on this range
1000 * before calling this.
1002 static int cluster_pages_for_defrag(struct inode
*inode
,
1003 struct page
**pages
,
1004 unsigned long start_index
,
1007 unsigned long file_end
;
1008 u64 isize
= i_size_read(inode
);
1015 struct btrfs_ordered_extent
*ordered
;
1016 struct extent_state
*cached_state
= NULL
;
1017 struct extent_io_tree
*tree
;
1018 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
1020 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1021 if (!isize
|| start_index
> file_end
)
1024 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
1026 ret
= btrfs_delalloc_reserve_space(inode
,
1027 page_cnt
<< PAGE_CACHE_SHIFT
);
1031 tree
= &BTRFS_I(inode
)->io_tree
;
1033 /* step one, lock all the pages */
1034 for (i
= 0; i
< page_cnt
; i
++) {
1037 page
= find_or_create_page(inode
->i_mapping
,
1038 start_index
+ i
, mask
);
1042 page_start
= page_offset(page
);
1043 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1045 lock_extent(tree
, page_start
, page_end
);
1046 ordered
= btrfs_lookup_ordered_extent(inode
,
1048 unlock_extent(tree
, page_start
, page_end
);
1053 btrfs_start_ordered_extent(inode
, ordered
, 1);
1054 btrfs_put_ordered_extent(ordered
);
1057 * we unlocked the page above, so we need check if
1058 * it was released or not.
1060 if (page
->mapping
!= inode
->i_mapping
) {
1062 page_cache_release(page
);
1067 if (!PageUptodate(page
)) {
1068 btrfs_readpage(NULL
, page
);
1070 if (!PageUptodate(page
)) {
1072 page_cache_release(page
);
1078 if (page
->mapping
!= inode
->i_mapping
) {
1080 page_cache_release(page
);
1090 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1094 * so now we have a nice long stream of locked
1095 * and up to date pages, lets wait on them
1097 for (i
= 0; i
< i_done
; i
++)
1098 wait_on_page_writeback(pages
[i
]);
1100 page_start
= page_offset(pages
[0]);
1101 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
1103 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1104 page_start
, page_end
- 1, 0, &cached_state
);
1105 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1106 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1107 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 0, 0,
1108 &cached_state
, GFP_NOFS
);
1110 if (i_done
!= page_cnt
) {
1111 spin_lock(&BTRFS_I(inode
)->lock
);
1112 BTRFS_I(inode
)->outstanding_extents
++;
1113 spin_unlock(&BTRFS_I(inode
)->lock
);
1114 btrfs_delalloc_release_space(inode
,
1115 (page_cnt
- i_done
) << PAGE_CACHE_SHIFT
);
1119 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1120 &cached_state
, GFP_NOFS
);
1122 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1123 page_start
, page_end
- 1, &cached_state
,
1126 for (i
= 0; i
< i_done
; i
++) {
1127 clear_page_dirty_for_io(pages
[i
]);
1128 ClearPageChecked(pages
[i
]);
1129 set_page_extent_mapped(pages
[i
]);
1130 set_page_dirty(pages
[i
]);
1131 unlock_page(pages
[i
]);
1132 page_cache_release(pages
[i
]);
1136 for (i
= 0; i
< i_done
; i
++) {
1137 unlock_page(pages
[i
]);
1138 page_cache_release(pages
[i
]);
1140 btrfs_delalloc_release_space(inode
, page_cnt
<< PAGE_CACHE_SHIFT
);
1145 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1146 struct btrfs_ioctl_defrag_range_args
*range
,
1147 u64 newer_than
, unsigned long max_to_defrag
)
1149 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1150 struct file_ra_state
*ra
= NULL
;
1151 unsigned long last_index
;
1152 u64 isize
= i_size_read(inode
);
1156 u64 newer_off
= range
->start
;
1158 unsigned long ra_index
= 0;
1160 int defrag_count
= 0;
1161 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1162 int extent_thresh
= range
->extent_thresh
;
1163 int max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1164 int cluster
= max_cluster
;
1165 u64 new_align
= ~((u64
)128 * 1024 - 1);
1166 struct page
**pages
= NULL
;
1171 if (range
->start
>= isize
)
1174 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1175 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1177 if (range
->compress_type
)
1178 compress_type
= range
->compress_type
;
1181 if (extent_thresh
== 0)
1182 extent_thresh
= 256 * 1024;
1185 * if we were not given a file, allocate a readahead
1189 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1192 file_ra_state_init(ra
, inode
->i_mapping
);
1197 pages
= kmalloc(sizeof(struct page
*) * max_cluster
,
1204 /* find the last page to defrag */
1205 if (range
->start
+ range
->len
> range
->start
) {
1206 last_index
= min_t(u64
, isize
- 1,
1207 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1209 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1213 ret
= find_new_extents(root
, inode
, newer_than
,
1214 &newer_off
, 64 * 1024);
1216 range
->start
= newer_off
;
1218 * we always align our defrag to help keep
1219 * the extents in the file evenly spaced
1221 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1225 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1228 max_to_defrag
= last_index
+ 1;
1231 * make writeback starts from i, so the defrag range can be
1232 * written sequentially.
1234 if (i
< inode
->i_mapping
->writeback_index
)
1235 inode
->i_mapping
->writeback_index
= i
;
1237 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1238 (i
< (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
1239 PAGE_CACHE_SHIFT
)) {
1241 * make sure we stop running if someone unmounts
1244 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1247 if (btrfs_defrag_cancelled(root
->fs_info
)) {
1248 printk(KERN_DEBUG
"btrfs: defrag_file cancelled\n");
1253 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1254 extent_thresh
, &last_len
, &skip
,
1255 &defrag_end
, range
->flags
&
1256 BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1259 * the should_defrag function tells us how much to skip
1260 * bump our counter by the suggested amount
1262 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1263 i
= max(i
+ 1, next
);
1268 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1269 PAGE_CACHE_SHIFT
) - i
;
1270 cluster
= min(cluster
, max_cluster
);
1272 cluster
= max_cluster
;
1275 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1276 BTRFS_I(inode
)->force_compress
= compress_type
;
1278 if (i
+ cluster
> ra_index
) {
1279 ra_index
= max(i
, ra_index
);
1280 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1282 ra_index
+= max_cluster
;
1285 mutex_lock(&inode
->i_mutex
);
1286 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1288 mutex_unlock(&inode
->i_mutex
);
1292 defrag_count
+= ret
;
1293 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1294 mutex_unlock(&inode
->i_mutex
);
1297 if (newer_off
== (u64
)-1)
1303 newer_off
= max(newer_off
+ 1,
1304 (u64
)i
<< PAGE_CACHE_SHIFT
);
1306 ret
= find_new_extents(root
, inode
,
1307 newer_than
, &newer_off
,
1310 range
->start
= newer_off
;
1311 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1318 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1326 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
1327 filemap_flush(inode
->i_mapping
);
1329 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1330 /* the filemap_flush will queue IO into the worker threads, but
1331 * we have to make sure the IO is actually started and that
1332 * ordered extents get created before we return
1334 atomic_inc(&root
->fs_info
->async_submit_draining
);
1335 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1336 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1337 wait_event(root
->fs_info
->async_submit_wait
,
1338 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1339 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1341 atomic_dec(&root
->fs_info
->async_submit_draining
);
1343 mutex_lock(&inode
->i_mutex
);
1344 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1345 mutex_unlock(&inode
->i_mutex
);
1348 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1349 btrfs_set_fs_incompat(root
->fs_info
, COMPRESS_LZO
);
1361 static noinline
int btrfs_ioctl_resize(struct file
*file
,
1367 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
1368 struct btrfs_ioctl_vol_args
*vol_args
;
1369 struct btrfs_trans_handle
*trans
;
1370 struct btrfs_device
*device
= NULL
;
1372 char *devstr
= NULL
;
1376 if (!capable(CAP_SYS_ADMIN
))
1379 ret
= mnt_want_write_file(file
);
1383 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
1385 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1386 mnt_drop_write_file(file
);
1390 mutex_lock(&root
->fs_info
->volume_mutex
);
1391 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1392 if (IS_ERR(vol_args
)) {
1393 ret
= PTR_ERR(vol_args
);
1397 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1399 sizestr
= vol_args
->name
;
1400 devstr
= strchr(sizestr
, ':');
1403 sizestr
= devstr
+ 1;
1405 devstr
= vol_args
->name
;
1406 devid
= simple_strtoull(devstr
, &end
, 10);
1411 printk(KERN_INFO
"btrfs: resizing devid %llu\n",
1412 (unsigned long long)devid
);
1415 device
= btrfs_find_device(root
->fs_info
, devid
, NULL
, NULL
);
1417 printk(KERN_INFO
"btrfs: resizer unable to find device %llu\n",
1418 (unsigned long long)devid
);
1423 if (!device
->writeable
) {
1424 printk(KERN_INFO
"btrfs: resizer unable to apply on "
1425 "readonly device %llu\n",
1426 (unsigned long long)devid
);
1431 if (!strcmp(sizestr
, "max"))
1432 new_size
= device
->bdev
->bd_inode
->i_size
;
1434 if (sizestr
[0] == '-') {
1437 } else if (sizestr
[0] == '+') {
1441 new_size
= memparse(sizestr
, NULL
);
1442 if (new_size
== 0) {
1448 if (device
->is_tgtdev_for_dev_replace
) {
1453 old_size
= device
->total_bytes
;
1456 if (new_size
> old_size
) {
1460 new_size
= old_size
- new_size
;
1461 } else if (mod
> 0) {
1462 new_size
= old_size
+ new_size
;
1465 if (new_size
< 256 * 1024 * 1024) {
1469 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1474 do_div(new_size
, root
->sectorsize
);
1475 new_size
*= root
->sectorsize
;
1477 printk_in_rcu(KERN_INFO
"btrfs: new size for %s is %llu\n",
1478 rcu_str_deref(device
->name
),
1479 (unsigned long long)new_size
);
1481 if (new_size
> old_size
) {
1482 trans
= btrfs_start_transaction(root
, 0);
1483 if (IS_ERR(trans
)) {
1484 ret
= PTR_ERR(trans
);
1487 ret
= btrfs_grow_device(trans
, device
, new_size
);
1488 btrfs_commit_transaction(trans
, root
);
1489 } else if (new_size
< old_size
) {
1490 ret
= btrfs_shrink_device(device
, new_size
);
1491 } /* equal, nothing need to do */
1496 mutex_unlock(&root
->fs_info
->volume_mutex
);
1497 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
1498 mnt_drop_write_file(file
);
1502 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1503 char *name
, unsigned long fd
, int subvol
,
1504 u64
*transid
, bool readonly
,
1505 struct btrfs_qgroup_inherit
*inherit
)
1510 ret
= mnt_want_write_file(file
);
1514 namelen
= strlen(name
);
1515 if (strchr(name
, '/')) {
1517 goto out_drop_write
;
1520 if (name
[0] == '.' &&
1521 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1523 goto out_drop_write
;
1527 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1528 NULL
, transid
, readonly
, inherit
);
1530 struct fd src
= fdget(fd
);
1531 struct inode
*src_inode
;
1534 goto out_drop_write
;
1537 src_inode
= file_inode(src
.file
);
1538 if (src_inode
->i_sb
!= file_inode(file
)->i_sb
) {
1539 printk(KERN_INFO
"btrfs: Snapshot src from "
1543 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1544 BTRFS_I(src_inode
)->root
,
1545 transid
, readonly
, inherit
);
1550 mnt_drop_write_file(file
);
1555 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1556 void __user
*arg
, int subvol
)
1558 struct btrfs_ioctl_vol_args
*vol_args
;
1561 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1562 if (IS_ERR(vol_args
))
1563 return PTR_ERR(vol_args
);
1564 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1566 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1567 vol_args
->fd
, subvol
,
1574 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1575 void __user
*arg
, int subvol
)
1577 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1581 bool readonly
= false;
1582 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1584 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1585 if (IS_ERR(vol_args
))
1586 return PTR_ERR(vol_args
);
1587 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1589 if (vol_args
->flags
&
1590 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1591 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1596 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1598 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1600 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1601 if (vol_args
->size
> PAGE_CACHE_SIZE
) {
1605 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1606 if (IS_ERR(inherit
)) {
1607 ret
= PTR_ERR(inherit
);
1612 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1613 vol_args
->fd
, subvol
, ptr
,
1616 if (ret
== 0 && ptr
&&
1618 offsetof(struct btrfs_ioctl_vol_args_v2
,
1619 transid
), ptr
, sizeof(*ptr
)))
1627 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1630 struct inode
*inode
= file_inode(file
);
1631 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1635 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1638 down_read(&root
->fs_info
->subvol_sem
);
1639 if (btrfs_root_readonly(root
))
1640 flags
|= BTRFS_SUBVOL_RDONLY
;
1641 up_read(&root
->fs_info
->subvol_sem
);
1643 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1649 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1652 struct inode
*inode
= file_inode(file
);
1653 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1654 struct btrfs_trans_handle
*trans
;
1659 ret
= mnt_want_write_file(file
);
1663 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1665 goto out_drop_write
;
1668 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1670 goto out_drop_write
;
1673 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1675 goto out_drop_write
;
1678 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1680 goto out_drop_write
;
1683 if (!inode_owner_or_capable(inode
)) {
1685 goto out_drop_write
;
1688 down_write(&root
->fs_info
->subvol_sem
);
1691 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1694 root_flags
= btrfs_root_flags(&root
->root_item
);
1695 if (flags
& BTRFS_SUBVOL_RDONLY
)
1696 btrfs_set_root_flags(&root
->root_item
,
1697 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1699 btrfs_set_root_flags(&root
->root_item
,
1700 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1702 trans
= btrfs_start_transaction(root
, 1);
1703 if (IS_ERR(trans
)) {
1704 ret
= PTR_ERR(trans
);
1708 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1709 &root
->root_key
, &root
->root_item
);
1711 btrfs_commit_transaction(trans
, root
);
1714 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1716 up_write(&root
->fs_info
->subvol_sem
);
1718 mnt_drop_write_file(file
);
1724 * helper to check if the subvolume references other subvolumes
1726 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1728 struct btrfs_path
*path
;
1729 struct btrfs_dir_item
*di
;
1730 struct btrfs_key key
;
1734 path
= btrfs_alloc_path();
1738 /* Make sure this root isn't set as the default subvol */
1739 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
1740 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
, path
,
1741 dir_id
, "default", 7, 0);
1742 if (di
&& !IS_ERR(di
)) {
1743 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1744 if (key
.objectid
== root
->root_key
.objectid
) {
1748 btrfs_release_path(path
);
1751 key
.objectid
= root
->root_key
.objectid
;
1752 key
.type
= BTRFS_ROOT_REF_KEY
;
1753 key
.offset
= (u64
)-1;
1755 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1762 if (path
->slots
[0] > 0) {
1764 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1765 if (key
.objectid
== root
->root_key
.objectid
&&
1766 key
.type
== BTRFS_ROOT_REF_KEY
)
1770 btrfs_free_path(path
);
1774 static noinline
int key_in_sk(struct btrfs_key
*key
,
1775 struct btrfs_ioctl_search_key
*sk
)
1777 struct btrfs_key test
;
1780 test
.objectid
= sk
->min_objectid
;
1781 test
.type
= sk
->min_type
;
1782 test
.offset
= sk
->min_offset
;
1784 ret
= btrfs_comp_cpu_keys(key
, &test
);
1788 test
.objectid
= sk
->max_objectid
;
1789 test
.type
= sk
->max_type
;
1790 test
.offset
= sk
->max_offset
;
1792 ret
= btrfs_comp_cpu_keys(key
, &test
);
1798 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1799 struct btrfs_path
*path
,
1800 struct btrfs_key
*key
,
1801 struct btrfs_ioctl_search_key
*sk
,
1803 unsigned long *sk_offset
,
1807 struct extent_buffer
*leaf
;
1808 struct btrfs_ioctl_search_header sh
;
1809 unsigned long item_off
;
1810 unsigned long item_len
;
1816 leaf
= path
->nodes
[0];
1817 slot
= path
->slots
[0];
1818 nritems
= btrfs_header_nritems(leaf
);
1820 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1824 found_transid
= btrfs_header_generation(leaf
);
1826 for (i
= slot
; i
< nritems
; i
++) {
1827 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1828 item_len
= btrfs_item_size_nr(leaf
, i
);
1830 btrfs_item_key_to_cpu(leaf
, key
, i
);
1831 if (!key_in_sk(key
, sk
))
1834 if (sizeof(sh
) + item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1837 if (sizeof(sh
) + item_len
+ *sk_offset
>
1838 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1843 sh
.objectid
= key
->objectid
;
1844 sh
.offset
= key
->offset
;
1845 sh
.type
= key
->type
;
1847 sh
.transid
= found_transid
;
1849 /* copy search result header */
1850 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1851 *sk_offset
+= sizeof(sh
);
1854 char *p
= buf
+ *sk_offset
;
1856 read_extent_buffer(leaf
, p
,
1857 item_off
, item_len
);
1858 *sk_offset
+= item_len
;
1862 if (*num_found
>= sk
->nr_items
)
1867 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1869 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1872 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1882 static noinline
int search_ioctl(struct inode
*inode
,
1883 struct btrfs_ioctl_search_args
*args
)
1885 struct btrfs_root
*root
;
1886 struct btrfs_key key
;
1887 struct btrfs_key max_key
;
1888 struct btrfs_path
*path
;
1889 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1890 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1893 unsigned long sk_offset
= 0;
1895 path
= btrfs_alloc_path();
1899 if (sk
->tree_id
== 0) {
1900 /* search the root of the inode that was passed */
1901 root
= BTRFS_I(inode
)->root
;
1903 key
.objectid
= sk
->tree_id
;
1904 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1905 key
.offset
= (u64
)-1;
1906 root
= btrfs_read_fs_root_no_name(info
, &key
);
1908 printk(KERN_ERR
"could not find root %llu\n",
1910 btrfs_free_path(path
);
1915 key
.objectid
= sk
->min_objectid
;
1916 key
.type
= sk
->min_type
;
1917 key
.offset
= sk
->min_offset
;
1919 max_key
.objectid
= sk
->max_objectid
;
1920 max_key
.type
= sk
->max_type
;
1921 max_key
.offset
= sk
->max_offset
;
1923 path
->keep_locks
= 1;
1926 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
,
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
= fdentry(file
);
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
);
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 trans
->block_rsv
= NULL
;
2240 trans
->bytes_reserved
= 0;
2241 ret
= btrfs_end_transaction(trans
, root
);
2244 inode
->i_flags
|= S_DEAD
;
2246 btrfs_subvolume_release_metadata(root
, &block_rsv
, qgroup_reserved
);
2248 up_write(&root
->fs_info
->subvol_sem
);
2250 mutex_unlock(&inode
->i_mutex
);
2252 shrink_dcache_sb(root
->fs_info
->sb
);
2253 btrfs_invalidate_inodes(dest
);
2257 if (dest
->cache_inode
) {
2258 iput(dest
->cache_inode
);
2259 dest
->cache_inode
= NULL
;
2265 mutex_unlock(&dir
->i_mutex
);
2266 mnt_drop_write_file(file
);
2272 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2274 struct inode
*inode
= file_inode(file
);
2275 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2276 struct btrfs_ioctl_defrag_range_args
*range
;
2279 ret
= mnt_want_write_file(file
);
2283 if (btrfs_root_readonly(root
)) {
2288 switch (inode
->i_mode
& S_IFMT
) {
2290 if (!capable(CAP_SYS_ADMIN
)) {
2294 ret
= btrfs_defrag_root(root
);
2297 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
);
2300 if (!(file
->f_mode
& FMODE_WRITE
)) {
2305 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2312 if (copy_from_user(range
, argp
,
2318 /* compression requires us to start the IO */
2319 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2320 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2321 range
->extent_thresh
= (u32
)-1;
2324 /* the rest are all set to zero by kzalloc */
2325 range
->len
= (u64
)-1;
2327 ret
= btrfs_defrag_file(file_inode(file
), file
,
2337 mnt_drop_write_file(file
);
2341 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2343 struct btrfs_ioctl_vol_args
*vol_args
;
2346 if (!capable(CAP_SYS_ADMIN
))
2349 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2351 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2355 mutex_lock(&root
->fs_info
->volume_mutex
);
2356 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2357 if (IS_ERR(vol_args
)) {
2358 ret
= PTR_ERR(vol_args
);
2362 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2363 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2367 mutex_unlock(&root
->fs_info
->volume_mutex
);
2368 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2372 static long btrfs_ioctl_rm_dev(struct file
*file
, void __user
*arg
)
2374 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
2375 struct btrfs_ioctl_vol_args
*vol_args
;
2378 if (!capable(CAP_SYS_ADMIN
))
2381 ret
= mnt_want_write_file(file
);
2385 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2386 if (IS_ERR(vol_args
)) {
2387 ret
= PTR_ERR(vol_args
);
2391 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2393 if (atomic_xchg(&root
->fs_info
->mutually_exclusive_operation_running
,
2395 ret
= BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS
;
2399 mutex_lock(&root
->fs_info
->volume_mutex
);
2400 ret
= btrfs_rm_device(root
, vol_args
->name
);
2401 mutex_unlock(&root
->fs_info
->volume_mutex
);
2402 atomic_set(&root
->fs_info
->mutually_exclusive_operation_running
, 0);
2406 mnt_drop_write_file(file
);
2410 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2412 struct btrfs_ioctl_fs_info_args
*fi_args
;
2413 struct btrfs_device
*device
;
2414 struct btrfs_device
*next
;
2415 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2418 if (!capable(CAP_SYS_ADMIN
))
2421 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2425 fi_args
->num_devices
= fs_devices
->num_devices
;
2426 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2428 mutex_lock(&fs_devices
->device_list_mutex
);
2429 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
2430 if (device
->devid
> fi_args
->max_id
)
2431 fi_args
->max_id
= device
->devid
;
2433 mutex_unlock(&fs_devices
->device_list_mutex
);
2435 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2442 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2444 struct btrfs_ioctl_dev_info_args
*di_args
;
2445 struct btrfs_device
*dev
;
2446 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2448 char *s_uuid
= NULL
;
2449 char empty_uuid
[BTRFS_UUID_SIZE
] = {0};
2451 if (!capable(CAP_SYS_ADMIN
))
2454 di_args
= memdup_user(arg
, sizeof(*di_args
));
2455 if (IS_ERR(di_args
))
2456 return PTR_ERR(di_args
);
2458 if (memcmp(empty_uuid
, di_args
->uuid
, BTRFS_UUID_SIZE
) != 0)
2459 s_uuid
= di_args
->uuid
;
2461 mutex_lock(&fs_devices
->device_list_mutex
);
2462 dev
= btrfs_find_device(root
->fs_info
, di_args
->devid
, s_uuid
, NULL
);
2469 di_args
->devid
= dev
->devid
;
2470 di_args
->bytes_used
= dev
->bytes_used
;
2471 di_args
->total_bytes
= dev
->total_bytes
;
2472 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2474 struct rcu_string
*name
;
2477 name
= rcu_dereference(dev
->name
);
2478 strncpy(di_args
->path
, name
->str
, sizeof(di_args
->path
));
2480 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
2482 di_args
->path
[0] = '\0';
2486 mutex_unlock(&fs_devices
->device_list_mutex
);
2487 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2494 static struct page
*extent_same_get_page(struct inode
*inode
, u64 off
)
2498 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2500 index
= off
>> PAGE_CACHE_SHIFT
;
2502 page
= grab_cache_page(inode
->i_mapping
, index
);
2506 if (!PageUptodate(page
)) {
2507 if (extent_read_full_page_nolock(tree
, page
, btrfs_get_extent
,
2511 if (!PageUptodate(page
)) {
2513 page_cache_release(page
);
2522 static inline void lock_extent_range(struct inode
*inode
, u64 off
, u64 len
)
2524 /* do any pending delalloc/csum calc on src, one way or
2525 another, and lock file content */
2527 struct btrfs_ordered_extent
*ordered
;
2528 lock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2529 ordered
= btrfs_lookup_first_ordered_extent(inode
,
2532 !test_range_bit(&BTRFS_I(inode
)->io_tree
, off
,
2533 off
+ len
- 1, EXTENT_DELALLOC
, 0, NULL
))
2535 unlock_extent(&BTRFS_I(inode
)->io_tree
, off
, off
+ len
- 1);
2537 btrfs_put_ordered_extent(ordered
);
2538 btrfs_wait_ordered_range(inode
, off
, len
);
2542 static void btrfs_double_unlock(struct inode
*inode1
, u64 loff1
,
2543 struct inode
*inode2
, u64 loff2
, u64 len
)
2545 unlock_extent(&BTRFS_I(inode1
)->io_tree
, loff1
, loff1
+ len
- 1);
2546 unlock_extent(&BTRFS_I(inode2
)->io_tree
, loff2
, loff2
+ len
- 1);
2548 mutex_unlock(&inode1
->i_mutex
);
2549 mutex_unlock(&inode2
->i_mutex
);
2552 static void btrfs_double_lock(struct inode
*inode1
, u64 loff1
,
2553 struct inode
*inode2
, u64 loff2
, u64 len
)
2555 if (inode1
< inode2
) {
2556 swap(inode1
, inode2
);
2560 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_PARENT
);
2561 lock_extent_range(inode1
, loff1
, len
);
2562 if (inode1
!= inode2
) {
2563 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_CHILD
);
2564 lock_extent_range(inode2
, loff2
, len
);
2568 static int btrfs_cmp_data(struct inode
*src
, u64 loff
, struct inode
*dst
,
2569 u64 dst_loff
, u64 len
)
2572 struct page
*src_page
, *dst_page
;
2573 unsigned int cmp_len
= PAGE_CACHE_SIZE
;
2574 void *addr
, *dst_addr
;
2577 if (len
< PAGE_CACHE_SIZE
)
2580 src_page
= extent_same_get_page(src
, loff
);
2583 dst_page
= extent_same_get_page(dst
, dst_loff
);
2585 page_cache_release(src_page
);
2588 addr
= kmap_atomic(src_page
);
2589 dst_addr
= kmap_atomic(dst_page
);
2591 flush_dcache_page(src_page
);
2592 flush_dcache_page(dst_page
);
2594 if (memcmp(addr
, dst_addr
, cmp_len
))
2595 ret
= BTRFS_SAME_DATA_DIFFERS
;
2597 kunmap_atomic(addr
);
2598 kunmap_atomic(dst_addr
);
2599 page_cache_release(src_page
);
2600 page_cache_release(dst_page
);
2606 dst_loff
+= cmp_len
;
2613 static int extent_same_check_offsets(struct inode
*inode
, u64 off
, u64 len
)
2615 u64 bs
= BTRFS_I(inode
)->root
->fs_info
->sb
->s_blocksize
;
2617 if (off
+ len
> inode
->i_size
|| off
+ len
< off
)
2619 /* Check that we are block aligned - btrfs_clone() requires this */
2620 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
))
2626 static int btrfs_extent_same(struct inode
*src
, u64 loff
, u64 len
,
2627 struct inode
*dst
, u64 dst_loff
)
2632 * btrfs_clone() can't handle extents in the same file
2633 * yet. Once that works, we can drop this check and replace it
2634 * with a check for the same inode, but overlapping extents.
2639 btrfs_double_lock(src
, loff
, dst
, dst_loff
, len
);
2641 ret
= extent_same_check_offsets(src
, loff
, len
);
2645 ret
= extent_same_check_offsets(dst
, dst_loff
, len
);
2649 /* don't make the dst file partly checksummed */
2650 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
2651 (BTRFS_I(dst
)->flags
& BTRFS_INODE_NODATASUM
)) {
2656 ret
= btrfs_cmp_data(src
, loff
, dst
, dst_loff
, len
);
2658 ret
= btrfs_clone(src
, dst
, loff
, len
, len
, dst_loff
);
2661 btrfs_double_unlock(src
, loff
, dst
, dst_loff
, len
);
2666 #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2668 static long btrfs_ioctl_file_extent_same(struct file
*file
,
2671 struct btrfs_ioctl_same_args
*args
= argp
;
2672 struct btrfs_ioctl_same_args same
;
2673 struct btrfs_ioctl_same_extent_info info
;
2674 struct inode
*src
= file
->f_dentry
->d_inode
;
2675 struct file
*dst_file
= NULL
;
2681 u64 bs
= BTRFS_I(src
)->root
->fs_info
->sb
->s_blocksize
;
2682 bool is_admin
= capable(CAP_SYS_ADMIN
);
2684 if (!(file
->f_mode
& FMODE_READ
))
2687 ret
= mnt_want_write_file(file
);
2691 if (copy_from_user(&same
,
2692 (struct btrfs_ioctl_same_args __user
*)argp
,
2698 off
= same
.logical_offset
;
2702 * Limit the total length we will dedupe for each operation.
2703 * This is intended to bound the total time spent in this
2704 * ioctl to something sane.
2706 if (len
> BTRFS_MAX_DEDUPE_LEN
)
2707 len
= BTRFS_MAX_DEDUPE_LEN
;
2709 if (WARN_ON_ONCE(bs
< PAGE_CACHE_SIZE
)) {
2711 * Btrfs does not support blocksize < page_size. As a
2712 * result, btrfs_cmp_data() won't correctly handle
2713 * this situation without an update.
2720 if (S_ISDIR(src
->i_mode
))
2724 if (!S_ISREG(src
->i_mode
))
2728 for (i
= 0; i
< same
.dest_count
; i
++) {
2729 if (copy_from_user(&info
, &args
->info
[i
], sizeof(info
))) {
2734 info
.bytes_deduped
= 0;
2736 dst_file
= fget(info
.fd
);
2738 info
.status
= -EBADF
;
2742 if (!(is_admin
|| (dst_file
->f_mode
& FMODE_WRITE
))) {
2743 info
.status
= -EINVAL
;
2747 info
.status
= -EXDEV
;
2748 if (file
->f_path
.mnt
!= dst_file
->f_path
.mnt
)
2751 dst
= dst_file
->f_dentry
->d_inode
;
2752 if (src
->i_sb
!= dst
->i_sb
)
2755 if (S_ISDIR(dst
->i_mode
)) {
2756 info
.status
= -EISDIR
;
2760 if (!S_ISREG(dst
->i_mode
)) {
2761 info
.status
= -EACCES
;
2765 info
.status
= btrfs_extent_same(src
, off
, len
, dst
,
2766 info
.logical_offset
);
2767 if (info
.status
== 0)
2768 info
.bytes_deduped
+= len
;
2774 if (__put_user_unaligned(info
.status
, &args
->info
[i
].status
) ||
2775 __put_user_unaligned(info
.bytes_deduped
,
2776 &args
->info
[i
].bytes_deduped
)) {
2783 mnt_drop_write_file(file
);
2788 * btrfs_clone() - clone a range from inode file to another
2790 * @src: Inode to clone from
2791 * @inode: Inode to clone to
2792 * @off: Offset within source to start clone from
2793 * @olen: Original length, passed by user, of range to clone
2794 * @olen_aligned: Block-aligned value of olen, extent_same uses
2795 * identical values here
2796 * @destoff: Offset within @inode to start clone
2798 static int btrfs_clone(struct inode
*src
, struct inode
*inode
,
2799 u64 off
, u64 olen
, u64 olen_aligned
, u64 destoff
)
2801 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2802 struct btrfs_path
*path
= NULL
;
2803 struct extent_buffer
*leaf
;
2804 struct btrfs_trans_handle
*trans
;
2806 struct btrfs_key key
;
2810 u64 len
= olen_aligned
;
2813 buf
= vmalloc(btrfs_level_size(root
, 0));
2817 path
= btrfs_alloc_path();
2825 key
.objectid
= btrfs_ino(src
);
2826 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2831 * note the key will change type as we walk through the
2834 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
2839 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2840 if (path
->slots
[0] >= nritems
) {
2841 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
2846 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2848 leaf
= path
->nodes
[0];
2849 slot
= path
->slots
[0];
2851 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2852 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2853 key
.objectid
!= btrfs_ino(src
))
2856 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2857 struct btrfs_file_extent_item
*extent
;
2860 struct btrfs_key new_key
;
2861 u64 disko
= 0, diskl
= 0;
2862 u64 datao
= 0, datal
= 0;
2866 size
= btrfs_item_size_nr(leaf
, slot
);
2867 read_extent_buffer(leaf
, buf
,
2868 btrfs_item_ptr_offset(leaf
, slot
),
2871 extent
= btrfs_item_ptr(leaf
, slot
,
2872 struct btrfs_file_extent_item
);
2873 comp
= btrfs_file_extent_compression(leaf
, extent
);
2874 type
= btrfs_file_extent_type(leaf
, extent
);
2875 if (type
== BTRFS_FILE_EXTENT_REG
||
2876 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2877 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2879 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2881 datao
= btrfs_file_extent_offset(leaf
, extent
);
2882 datal
= btrfs_file_extent_num_bytes(leaf
,
2884 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2885 /* take upper bound, may be compressed */
2886 datal
= btrfs_file_extent_ram_bytes(leaf
,
2889 btrfs_release_path(path
);
2891 if (key
.offset
+ datal
<= off
||
2892 key
.offset
>= off
+ len
- 1)
2895 memcpy(&new_key
, &key
, sizeof(new_key
));
2896 new_key
.objectid
= btrfs_ino(inode
);
2897 if (off
<= key
.offset
)
2898 new_key
.offset
= key
.offset
+ destoff
- off
;
2900 new_key
.offset
= destoff
;
2903 * 1 - adjusting old extent (we may have to split it)
2904 * 1 - add new extent
2907 trans
= btrfs_start_transaction(root
, 3);
2908 if (IS_ERR(trans
)) {
2909 ret
= PTR_ERR(trans
);
2913 if (type
== BTRFS_FILE_EXTENT_REG
||
2914 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2916 * a | --- range to clone ---| b
2917 * | ------------- extent ------------- |
2920 /* substract range b */
2921 if (key
.offset
+ datal
> off
+ len
)
2922 datal
= off
+ len
- key
.offset
;
2924 /* substract range a */
2925 if (off
> key
.offset
) {
2926 datao
+= off
- key
.offset
;
2927 datal
-= off
- key
.offset
;
2930 ret
= btrfs_drop_extents(trans
, root
, inode
,
2932 new_key
.offset
+ datal
,
2935 btrfs_abort_transaction(trans
, root
,
2937 btrfs_end_transaction(trans
, root
);
2941 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2944 btrfs_abort_transaction(trans
, root
,
2946 btrfs_end_transaction(trans
, root
);
2950 leaf
= path
->nodes
[0];
2951 slot
= path
->slots
[0];
2952 write_extent_buffer(leaf
, buf
,
2953 btrfs_item_ptr_offset(leaf
, slot
),
2956 extent
= btrfs_item_ptr(leaf
, slot
,
2957 struct btrfs_file_extent_item
);
2959 /* disko == 0 means it's a hole */
2963 btrfs_set_file_extent_offset(leaf
, extent
,
2965 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2968 inode_add_bytes(inode
, datal
);
2969 ret
= btrfs_inc_extent_ref(trans
, root
,
2971 root
->root_key
.objectid
,
2973 new_key
.offset
- datao
,
2976 btrfs_abort_transaction(trans
,
2979 btrfs_end_transaction(trans
,
2985 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2988 if (off
> key
.offset
) {
2989 skip
= off
- key
.offset
;
2990 new_key
.offset
+= skip
;
2993 if (key
.offset
+ datal
> off
+ len
)
2994 trim
= key
.offset
+ datal
- (off
+ len
);
2996 if (comp
&& (skip
|| trim
)) {
2998 btrfs_end_transaction(trans
, root
);
3001 size
-= skip
+ trim
;
3002 datal
-= skip
+ trim
;
3004 ret
= btrfs_drop_extents(trans
, root
, inode
,
3006 new_key
.offset
+ datal
,
3009 btrfs_abort_transaction(trans
, root
,
3011 btrfs_end_transaction(trans
, root
);
3015 ret
= btrfs_insert_empty_item(trans
, root
, path
,
3018 btrfs_abort_transaction(trans
, root
,
3020 btrfs_end_transaction(trans
, root
);
3026 btrfs_file_extent_calc_inline_size(0);
3027 memmove(buf
+start
, buf
+start
+skip
,
3031 leaf
= path
->nodes
[0];
3032 slot
= path
->slots
[0];
3033 write_extent_buffer(leaf
, buf
,
3034 btrfs_item_ptr_offset(leaf
, slot
),
3036 inode_add_bytes(inode
, datal
);
3039 btrfs_mark_buffer_dirty(leaf
);
3040 btrfs_release_path(path
);
3042 inode_inc_iversion(inode
);
3043 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
3046 * we round up to the block size at eof when
3047 * determining which extents to clone above,
3048 * but shouldn't round up the file size
3050 endoff
= new_key
.offset
+ datal
;
3051 if (endoff
> destoff
+olen
)
3052 endoff
= destoff
+olen
;
3053 if (endoff
> inode
->i_size
)
3054 btrfs_i_size_write(inode
, endoff
);
3056 ret
= btrfs_update_inode(trans
, root
, inode
);
3058 btrfs_abort_transaction(trans
, root
, ret
);
3059 btrfs_end_transaction(trans
, root
);
3062 ret
= btrfs_end_transaction(trans
, root
);
3065 btrfs_release_path(path
);
3071 btrfs_release_path(path
);
3072 btrfs_free_path(path
);
3077 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
3078 u64 off
, u64 olen
, u64 destoff
)
3080 struct inode
*inode
= fdentry(file
)->d_inode
;
3081 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3086 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
3091 * - split compressed inline extents. annoying: we need to
3092 * decompress into destination's address_space (the file offset
3093 * may change, so source mapping won't do), then recompress (or
3094 * otherwise reinsert) a subrange.
3095 * - allow ranges within the same file to be cloned (provided
3096 * they don't overlap)?
3099 /* the destination must be opened for writing */
3100 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
3103 if (btrfs_root_readonly(root
))
3106 ret
= mnt_want_write_file(file
);
3110 src_file
= fdget(srcfd
);
3111 if (!src_file
.file
) {
3113 goto out_drop_write
;
3117 if (src_file
.file
->f_path
.mnt
!= file
->f_path
.mnt
)
3120 src
= file_inode(src_file
.file
);
3126 /* the src must be open for reading */
3127 if (!(src_file
.file
->f_mode
& FMODE_READ
))
3130 /* don't make the dst file partly checksummed */
3131 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
3132 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
3136 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
3140 if (src
->i_sb
!= inode
->i_sb
)
3145 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
3146 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
3148 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
3149 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
3152 mutex_lock(&src
->i_mutex
);
3155 /* determine range to clone */
3157 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
3160 olen
= len
= src
->i_size
- off
;
3161 /* if we extend to eof, continue to block boundary */
3162 if (off
+ len
== src
->i_size
)
3163 len
= ALIGN(src
->i_size
, bs
) - off
;
3165 /* verify the end result is block aligned */
3166 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
3167 !IS_ALIGNED(destoff
, bs
))
3170 /* verify if ranges are overlapped within the same file */
3172 if (destoff
+ len
> off
&& destoff
< off
+ len
)
3176 if (destoff
> inode
->i_size
) {
3177 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
3182 /* truncate page cache pages from target inode range */
3183 truncate_inode_pages_range(&inode
->i_data
, destoff
,
3184 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
3186 lock_extent_range(src
, off
, len
);
3188 ret
= btrfs_clone(src
, inode
, off
, olen
, len
, destoff
);
3190 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+ len
- 1);
3192 mutex_unlock(&src
->i_mutex
);
3194 mutex_unlock(&inode
->i_mutex
);
3198 mnt_drop_write_file(file
);
3202 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
3204 struct btrfs_ioctl_clone_range_args args
;
3206 if (copy_from_user(&args
, argp
, sizeof(args
)))
3208 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
3209 args
.src_length
, args
.dest_offset
);
3213 * there are many ways the trans_start and trans_end ioctls can lead
3214 * to deadlocks. They should only be used by applications that
3215 * basically own the machine, and have a very in depth understanding
3216 * of all the possible deadlocks and enospc problems.
3218 static long btrfs_ioctl_trans_start(struct file
*file
)
3220 struct inode
*inode
= file_inode(file
);
3221 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3222 struct btrfs_trans_handle
*trans
;
3226 if (!capable(CAP_SYS_ADMIN
))
3230 if (file
->private_data
)
3234 if (btrfs_root_readonly(root
))
3237 ret
= mnt_want_write_file(file
);
3241 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
3244 trans
= btrfs_start_ioctl_transaction(root
);
3248 file
->private_data
= trans
;
3252 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
3253 mnt_drop_write_file(file
);
3258 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
3260 struct inode
*inode
= file_inode(file
);
3261 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3262 struct btrfs_root
*new_root
;
3263 struct btrfs_dir_item
*di
;
3264 struct btrfs_trans_handle
*trans
;
3265 struct btrfs_path
*path
;
3266 struct btrfs_key location
;
3267 struct btrfs_disk_key disk_key
;
3272 if (!capable(CAP_SYS_ADMIN
))
3275 ret
= mnt_want_write_file(file
);
3279 if (copy_from_user(&objectid
, argp
, sizeof(objectid
))) {
3285 objectid
= root
->root_key
.objectid
;
3287 location
.objectid
= objectid
;
3288 location
.type
= BTRFS_ROOT_ITEM_KEY
;
3289 location
.offset
= (u64
)-1;
3291 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
3292 if (IS_ERR(new_root
)) {
3293 ret
= PTR_ERR(new_root
);
3297 path
= btrfs_alloc_path();
3302 path
->leave_spinning
= 1;
3304 trans
= btrfs_start_transaction(root
, 1);
3305 if (IS_ERR(trans
)) {
3306 btrfs_free_path(path
);
3307 ret
= PTR_ERR(trans
);
3311 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
3312 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
3313 dir_id
, "default", 7, 1);
3314 if (IS_ERR_OR_NULL(di
)) {
3315 btrfs_free_path(path
);
3316 btrfs_end_transaction(trans
, root
);
3317 printk(KERN_ERR
"Umm, you don't have the default dir item, "
3318 "this isn't going to work\n");
3323 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
3324 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
3325 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3326 btrfs_free_path(path
);
3328 btrfs_set_fs_incompat(root
->fs_info
, DEFAULT_SUBVOL
);
3329 btrfs_end_transaction(trans
, root
);
3331 mnt_drop_write_file(file
);
3335 void btrfs_get_block_group_info(struct list_head
*groups_list
,
3336 struct btrfs_ioctl_space_info
*space
)
3338 struct btrfs_block_group_cache
*block_group
;
3340 space
->total_bytes
= 0;
3341 space
->used_bytes
= 0;
3343 list_for_each_entry(block_group
, groups_list
, list
) {
3344 space
->flags
= block_group
->flags
;
3345 space
->total_bytes
+= block_group
->key
.offset
;
3346 space
->used_bytes
+=
3347 btrfs_block_group_used(&block_group
->item
);
3351 static long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
3353 struct btrfs_ioctl_space_args space_args
;
3354 struct btrfs_ioctl_space_info space
;
3355 struct btrfs_ioctl_space_info
*dest
;
3356 struct btrfs_ioctl_space_info
*dest_orig
;
3357 struct btrfs_ioctl_space_info __user
*user_dest
;
3358 struct btrfs_space_info
*info
;
3359 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
3360 BTRFS_BLOCK_GROUP_SYSTEM
,
3361 BTRFS_BLOCK_GROUP_METADATA
,
3362 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
3369 if (copy_from_user(&space_args
,
3370 (struct btrfs_ioctl_space_args __user
*)arg
,
3371 sizeof(space_args
)))
3374 for (i
= 0; i
< num_types
; i
++) {
3375 struct btrfs_space_info
*tmp
;
3379 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
3381 if (tmp
->flags
== types
[i
]) {
3391 down_read(&info
->groups_sem
);
3392 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
3393 if (!list_empty(&info
->block_groups
[c
]))
3396 up_read(&info
->groups_sem
);
3399 /* space_slots == 0 means they are asking for a count */
3400 if (space_args
.space_slots
== 0) {
3401 space_args
.total_spaces
= slot_count
;
3405 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
3407 alloc_size
= sizeof(*dest
) * slot_count
;
3409 /* we generally have at most 6 or so space infos, one for each raid
3410 * level. So, a whole page should be more than enough for everyone
3412 if (alloc_size
> PAGE_CACHE_SIZE
)
3415 space_args
.total_spaces
= 0;
3416 dest
= kmalloc(alloc_size
, GFP_NOFS
);
3421 /* now we have a buffer to copy into */
3422 for (i
= 0; i
< num_types
; i
++) {
3423 struct btrfs_space_info
*tmp
;
3430 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
3432 if (tmp
->flags
== types
[i
]) {
3441 down_read(&info
->groups_sem
);
3442 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
3443 if (!list_empty(&info
->block_groups
[c
])) {
3444 btrfs_get_block_group_info(
3445 &info
->block_groups
[c
], &space
);
3446 memcpy(dest
, &space
, sizeof(space
));
3448 space_args
.total_spaces
++;
3454 up_read(&info
->groups_sem
);
3457 user_dest
= (struct btrfs_ioctl_space_info __user
*)
3458 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
3460 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
3465 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
3472 * there are many ways the trans_start and trans_end ioctls can lead
3473 * to deadlocks. They should only be used by applications that
3474 * basically own the machine, and have a very in depth understanding
3475 * of all the possible deadlocks and enospc problems.
3477 long btrfs_ioctl_trans_end(struct file
*file
)
3479 struct inode
*inode
= file_inode(file
);
3480 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3481 struct btrfs_trans_handle
*trans
;
3483 trans
= file
->private_data
;
3486 file
->private_data
= NULL
;
3488 btrfs_end_transaction(trans
, root
);
3490 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
3492 mnt_drop_write_file(file
);
3496 static noinline
long btrfs_ioctl_start_sync(struct btrfs_root
*root
,
3499 struct btrfs_trans_handle
*trans
;
3503 trans
= btrfs_attach_transaction_barrier(root
);
3504 if (IS_ERR(trans
)) {
3505 if (PTR_ERR(trans
) != -ENOENT
)
3506 return PTR_ERR(trans
);
3508 /* No running transaction, don't bother */
3509 transid
= root
->fs_info
->last_trans_committed
;
3512 transid
= trans
->transid
;
3513 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
3515 btrfs_end_transaction(trans
, root
);
3520 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
3525 static noinline
long btrfs_ioctl_wait_sync(struct btrfs_root
*root
,
3531 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
3534 transid
= 0; /* current trans */
3536 return btrfs_wait_for_commit(root
, transid
);
3539 static long btrfs_ioctl_scrub(struct file
*file
, void __user
*arg
)
3541 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
3542 struct btrfs_ioctl_scrub_args
*sa
;
3545 if (!capable(CAP_SYS_ADMIN
))
3548 sa
= memdup_user(arg
, sizeof(*sa
));
3552 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
)) {
3553 ret
= mnt_want_write_file(file
);
3558 ret
= btrfs_scrub_dev(root
->fs_info
, sa
->devid
, sa
->start
, sa
->end
,
3559 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
,
3562 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3565 if (!(sa
->flags
& BTRFS_SCRUB_READONLY
))
3566 mnt_drop_write_file(file
);
3572 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
3574 if (!capable(CAP_SYS_ADMIN
))
3577 return btrfs_scrub_cancel(root
->fs_info
);
3580 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
3583 struct btrfs_ioctl_scrub_args
*sa
;
3586 if (!capable(CAP_SYS_ADMIN
))
3589 sa
= memdup_user(arg
, sizeof(*sa
));
3593 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
3595 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3602 static long btrfs_ioctl_get_dev_stats(struct btrfs_root
*root
,
3605 struct btrfs_ioctl_get_dev_stats
*sa
;
3608 sa
= memdup_user(arg
, sizeof(*sa
));
3612 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
3617 ret
= btrfs_get_dev_stats(root
, sa
);
3619 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3626 static long btrfs_ioctl_dev_replace(struct btrfs_root
*root
, void __user
*arg
)
3628 struct btrfs_ioctl_dev_replace_args
*p
;
3631 if (!capable(CAP_SYS_ADMIN
))
3634 p
= memdup_user(arg
, sizeof(*p
));
3639 case BTRFS_IOCTL_DEV_REPLACE_CMD_START
:
3641 &root
->fs_info
->mutually_exclusive_operation_running
,
3643 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3646 ret
= btrfs_dev_replace_start(root
, p
);
3648 &root
->fs_info
->mutually_exclusive_operation_running
,
3652 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
:
3653 btrfs_dev_replace_status(root
->fs_info
, p
);
3656 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
:
3657 ret
= btrfs_dev_replace_cancel(root
->fs_info
, p
);
3664 if (copy_to_user(arg
, p
, sizeof(*p
)))
3671 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
3677 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
3678 struct inode_fs_paths
*ipath
= NULL
;
3679 struct btrfs_path
*path
;
3681 if (!capable(CAP_DAC_READ_SEARCH
))
3684 path
= btrfs_alloc_path();
3690 ipa
= memdup_user(arg
, sizeof(*ipa
));
3697 size
= min_t(u32
, ipa
->size
, 4096);
3698 ipath
= init_ipath(size
, root
, path
);
3699 if (IS_ERR(ipath
)) {
3700 ret
= PTR_ERR(ipath
);
3705 ret
= paths_from_inode(ipa
->inum
, ipath
);
3709 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
3710 rel_ptr
= ipath
->fspath
->val
[i
] -
3711 (u64
)(unsigned long)ipath
->fspath
->val
;
3712 ipath
->fspath
->val
[i
] = rel_ptr
;
3715 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
3716 (void *)(unsigned long)ipath
->fspath
, size
);
3723 btrfs_free_path(path
);
3730 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
3732 struct btrfs_data_container
*inodes
= ctx
;
3733 const size_t c
= 3 * sizeof(u64
);
3735 if (inodes
->bytes_left
>= c
) {
3736 inodes
->bytes_left
-= c
;
3737 inodes
->val
[inodes
->elem_cnt
] = inum
;
3738 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
3739 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
3740 inodes
->elem_cnt
+= 3;
3742 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
3743 inodes
->bytes_left
= 0;
3744 inodes
->elem_missed
+= 3;
3750 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
3755 struct btrfs_ioctl_logical_ino_args
*loi
;
3756 struct btrfs_data_container
*inodes
= NULL
;
3757 struct btrfs_path
*path
= NULL
;
3759 if (!capable(CAP_SYS_ADMIN
))
3762 loi
= memdup_user(arg
, sizeof(*loi
));
3769 path
= btrfs_alloc_path();
3775 size
= min_t(u32
, loi
->size
, 64 * 1024);
3776 inodes
= init_data_container(size
);
3777 if (IS_ERR(inodes
)) {
3778 ret
= PTR_ERR(inodes
);
3783 ret
= iterate_inodes_from_logical(loi
->logical
, root
->fs_info
, path
,
3784 build_ino_list
, inodes
);
3790 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
3791 (void *)(unsigned long)inodes
, size
);
3796 btrfs_free_path(path
);
3803 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3804 struct btrfs_ioctl_balance_args
*bargs
)
3806 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3808 bargs
->flags
= bctl
->flags
;
3810 if (atomic_read(&fs_info
->balance_running
))
3811 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
3812 if (atomic_read(&fs_info
->balance_pause_req
))
3813 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
3814 if (atomic_read(&fs_info
->balance_cancel_req
))
3815 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
3817 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
3818 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
3819 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
3822 spin_lock(&fs_info
->balance_lock
);
3823 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3824 spin_unlock(&fs_info
->balance_lock
);
3826 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3830 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
3832 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
3833 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3834 struct btrfs_ioctl_balance_args
*bargs
;
3835 struct btrfs_balance_control
*bctl
;
3836 bool need_unlock
; /* for mut. excl. ops lock */
3839 if (!capable(CAP_SYS_ADMIN
))
3842 ret
= mnt_want_write_file(file
);
3847 if (!atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1)) {
3848 mutex_lock(&fs_info
->volume_mutex
);
3849 mutex_lock(&fs_info
->balance_mutex
);
3855 * mut. excl. ops lock is locked. Three possibilites:
3856 * (1) some other op is running
3857 * (2) balance is running
3858 * (3) balance is paused -- special case (think resume)
3860 mutex_lock(&fs_info
->balance_mutex
);
3861 if (fs_info
->balance_ctl
) {
3862 /* this is either (2) or (3) */
3863 if (!atomic_read(&fs_info
->balance_running
)) {
3864 mutex_unlock(&fs_info
->balance_mutex
);
3865 if (!mutex_trylock(&fs_info
->volume_mutex
))
3867 mutex_lock(&fs_info
->balance_mutex
);
3869 if (fs_info
->balance_ctl
&&
3870 !atomic_read(&fs_info
->balance_running
)) {
3872 need_unlock
= false;
3876 mutex_unlock(&fs_info
->balance_mutex
);
3877 mutex_unlock(&fs_info
->volume_mutex
);
3881 mutex_unlock(&fs_info
->balance_mutex
);
3887 mutex_unlock(&fs_info
->balance_mutex
);
3888 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3894 BUG_ON(!atomic_read(&fs_info
->mutually_exclusive_operation_running
));
3897 bargs
= memdup_user(arg
, sizeof(*bargs
));
3898 if (IS_ERR(bargs
)) {
3899 ret
= PTR_ERR(bargs
);
3903 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
3904 if (!fs_info
->balance_ctl
) {
3909 bctl
= fs_info
->balance_ctl
;
3910 spin_lock(&fs_info
->balance_lock
);
3911 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3912 spin_unlock(&fs_info
->balance_lock
);
3920 if (fs_info
->balance_ctl
) {
3925 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3931 bctl
->fs_info
= fs_info
;
3933 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
3934 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
3935 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
3937 bctl
->flags
= bargs
->flags
;
3939 /* balance everything - no filters */
3940 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
3945 * Ownership of bctl and mutually_exclusive_operation_running
3946 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3947 * or, if restriper was paused all the way until unmount, in
3948 * free_fs_info. mutually_exclusive_operation_running is
3949 * cleared in __cancel_balance.
3951 need_unlock
= false;
3953 ret
= btrfs_balance(bctl
, bargs
);
3956 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3963 mutex_unlock(&fs_info
->balance_mutex
);
3964 mutex_unlock(&fs_info
->volume_mutex
);
3966 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3968 mnt_drop_write_file(file
);
3972 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
3974 if (!capable(CAP_SYS_ADMIN
))
3978 case BTRFS_BALANCE_CTL_PAUSE
:
3979 return btrfs_pause_balance(root
->fs_info
);
3980 case BTRFS_BALANCE_CTL_CANCEL
:
3981 return btrfs_cancel_balance(root
->fs_info
);
3987 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
3990 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3991 struct btrfs_ioctl_balance_args
*bargs
;
3994 if (!capable(CAP_SYS_ADMIN
))
3997 mutex_lock(&fs_info
->balance_mutex
);
3998 if (!fs_info
->balance_ctl
) {
4003 bargs
= kzalloc(sizeof(*bargs
), GFP_NOFS
);
4009 update_ioctl_balance_args(fs_info
, 1, bargs
);
4011 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
4016 mutex_unlock(&fs_info
->balance_mutex
);
4020 static long btrfs_ioctl_quota_ctl(struct file
*file
, void __user
*arg
)
4022 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4023 struct btrfs_ioctl_quota_ctl_args
*sa
;
4024 struct btrfs_trans_handle
*trans
= NULL
;
4028 if (!capable(CAP_SYS_ADMIN
))
4031 ret
= mnt_want_write_file(file
);
4035 sa
= memdup_user(arg
, sizeof(*sa
));
4041 down_write(&root
->fs_info
->subvol_sem
);
4042 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 2);
4043 if (IS_ERR(trans
)) {
4044 ret
= PTR_ERR(trans
);
4049 case BTRFS_QUOTA_CTL_ENABLE
:
4050 ret
= btrfs_quota_enable(trans
, root
->fs_info
);
4052 case BTRFS_QUOTA_CTL_DISABLE
:
4053 ret
= btrfs_quota_disable(trans
, root
->fs_info
);
4060 err
= btrfs_commit_transaction(trans
, root
->fs_info
->tree_root
);
4065 up_write(&root
->fs_info
->subvol_sem
);
4067 mnt_drop_write_file(file
);
4071 static long btrfs_ioctl_qgroup_assign(struct file
*file
, void __user
*arg
)
4073 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4074 struct btrfs_ioctl_qgroup_assign_args
*sa
;
4075 struct btrfs_trans_handle
*trans
;
4079 if (!capable(CAP_SYS_ADMIN
))
4082 ret
= mnt_want_write_file(file
);
4086 sa
= memdup_user(arg
, sizeof(*sa
));
4092 trans
= btrfs_join_transaction(root
);
4093 if (IS_ERR(trans
)) {
4094 ret
= PTR_ERR(trans
);
4098 /* FIXME: check if the IDs really exist */
4100 ret
= btrfs_add_qgroup_relation(trans
, root
->fs_info
,
4103 ret
= btrfs_del_qgroup_relation(trans
, root
->fs_info
,
4107 err
= btrfs_end_transaction(trans
, root
);
4114 mnt_drop_write_file(file
);
4118 static long btrfs_ioctl_qgroup_create(struct file
*file
, void __user
*arg
)
4120 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4121 struct btrfs_ioctl_qgroup_create_args
*sa
;
4122 struct btrfs_trans_handle
*trans
;
4126 if (!capable(CAP_SYS_ADMIN
))
4129 ret
= mnt_want_write_file(file
);
4133 sa
= memdup_user(arg
, sizeof(*sa
));
4139 if (!sa
->qgroupid
) {
4144 trans
= btrfs_join_transaction(root
);
4145 if (IS_ERR(trans
)) {
4146 ret
= PTR_ERR(trans
);
4150 /* FIXME: check if the IDs really exist */
4152 ret
= btrfs_create_qgroup(trans
, root
->fs_info
, sa
->qgroupid
,
4155 ret
= btrfs_remove_qgroup(trans
, root
->fs_info
, sa
->qgroupid
);
4158 err
= btrfs_end_transaction(trans
, root
);
4165 mnt_drop_write_file(file
);
4169 static long btrfs_ioctl_qgroup_limit(struct file
*file
, void __user
*arg
)
4171 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4172 struct btrfs_ioctl_qgroup_limit_args
*sa
;
4173 struct btrfs_trans_handle
*trans
;
4178 if (!capable(CAP_SYS_ADMIN
))
4181 ret
= mnt_want_write_file(file
);
4185 sa
= memdup_user(arg
, sizeof(*sa
));
4191 trans
= btrfs_join_transaction(root
);
4192 if (IS_ERR(trans
)) {
4193 ret
= PTR_ERR(trans
);
4197 qgroupid
= sa
->qgroupid
;
4199 /* take the current subvol as qgroup */
4200 qgroupid
= root
->root_key
.objectid
;
4203 /* FIXME: check if the IDs really exist */
4204 ret
= btrfs_limit_qgroup(trans
, root
->fs_info
, qgroupid
, &sa
->lim
);
4206 err
= btrfs_end_transaction(trans
, root
);
4213 mnt_drop_write_file(file
);
4217 static long btrfs_ioctl_quota_rescan(struct file
*file
, void __user
*arg
)
4219 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4220 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4223 if (!capable(CAP_SYS_ADMIN
))
4226 ret
= mnt_want_write_file(file
);
4230 qsa
= memdup_user(arg
, sizeof(*qsa
));
4241 ret
= btrfs_qgroup_rescan(root
->fs_info
);
4246 mnt_drop_write_file(file
);
4250 static long btrfs_ioctl_quota_rescan_status(struct file
*file
, void __user
*arg
)
4252 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4253 struct btrfs_ioctl_quota_rescan_args
*qsa
;
4256 if (!capable(CAP_SYS_ADMIN
))
4259 qsa
= kzalloc(sizeof(*qsa
), GFP_NOFS
);
4263 if (root
->fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
4265 qsa
->progress
= root
->fs_info
->qgroup_rescan_progress
.objectid
;
4268 if (copy_to_user(arg
, qsa
, sizeof(*qsa
)))
4275 static long btrfs_ioctl_quota_rescan_wait(struct file
*file
, void __user
*arg
)
4277 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
4279 if (!capable(CAP_SYS_ADMIN
))
4282 return btrfs_qgroup_wait_for_completion(root
->fs_info
);
4285 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
4288 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
4289 struct inode
*inode
= file_inode(file
);
4290 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4291 struct btrfs_root_item
*root_item
= &root
->root_item
;
4292 struct btrfs_trans_handle
*trans
;
4293 struct timespec ct
= CURRENT_TIME
;
4296 ret
= mnt_want_write_file(file
);
4300 down_write(&root
->fs_info
->subvol_sem
);
4302 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
4307 if (btrfs_root_readonly(root
)) {
4312 if (!inode_owner_or_capable(inode
)) {
4317 sa
= memdup_user(arg
, sizeof(*sa
));
4324 trans
= btrfs_start_transaction(root
, 1);
4325 if (IS_ERR(trans
)) {
4326 ret
= PTR_ERR(trans
);
4331 sa
->rtransid
= trans
->transid
;
4332 sa
->rtime
.sec
= ct
.tv_sec
;
4333 sa
->rtime
.nsec
= ct
.tv_nsec
;
4335 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
4336 btrfs_set_root_stransid(root_item
, sa
->stransid
);
4337 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
4338 btrfs_set_stack_timespec_sec(&root_item
->stime
, sa
->stime
.sec
);
4339 btrfs_set_stack_timespec_nsec(&root_item
->stime
, sa
->stime
.nsec
);
4340 btrfs_set_stack_timespec_sec(&root_item
->rtime
, sa
->rtime
.sec
);
4341 btrfs_set_stack_timespec_nsec(&root_item
->rtime
, sa
->rtime
.nsec
);
4343 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
4344 &root
->root_key
, &root
->root_item
);
4346 btrfs_end_transaction(trans
, root
);
4350 ret
= btrfs_commit_transaction(trans
, root
);
4355 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
4361 up_write(&root
->fs_info
->subvol_sem
);
4362 mnt_drop_write_file(file
);
4366 static int btrfs_ioctl_get_fslabel(struct file
*file
, void __user
*arg
)
4368 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4371 char label
[BTRFS_LABEL_SIZE
];
4373 spin_lock(&root
->fs_info
->super_lock
);
4374 memcpy(label
, root
->fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
4375 spin_unlock(&root
->fs_info
->super_lock
);
4377 len
= strnlen(label
, BTRFS_LABEL_SIZE
);
4379 if (len
== BTRFS_LABEL_SIZE
) {
4380 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4384 ret
= copy_to_user(arg
, label
, len
);
4386 return ret
? -EFAULT
: 0;
4389 static int btrfs_ioctl_set_fslabel(struct file
*file
, void __user
*arg
)
4391 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4392 struct btrfs_super_block
*super_block
= root
->fs_info
->super_copy
;
4393 struct btrfs_trans_handle
*trans
;
4394 char label
[BTRFS_LABEL_SIZE
];
4397 if (!capable(CAP_SYS_ADMIN
))
4400 if (copy_from_user(label
, arg
, sizeof(label
)))
4403 if (strnlen(label
, BTRFS_LABEL_SIZE
) == BTRFS_LABEL_SIZE
) {
4404 pr_err("btrfs: unable to set label with more than %d bytes\n",
4405 BTRFS_LABEL_SIZE
- 1);
4409 ret
= mnt_want_write_file(file
);
4413 trans
= btrfs_start_transaction(root
, 0);
4414 if (IS_ERR(trans
)) {
4415 ret
= PTR_ERR(trans
);
4419 spin_lock(&root
->fs_info
->super_lock
);
4420 strcpy(super_block
->label
, label
);
4421 spin_unlock(&root
->fs_info
->super_lock
);
4422 ret
= btrfs_end_transaction(trans
, root
);
4425 mnt_drop_write_file(file
);
4429 long btrfs_ioctl(struct file
*file
, unsigned int
4430 cmd
, unsigned long arg
)
4432 struct btrfs_root
*root
= BTRFS_I(file_inode(file
))->root
;
4433 void __user
*argp
= (void __user
*)arg
;
4436 case FS_IOC_GETFLAGS
:
4437 return btrfs_ioctl_getflags(file
, argp
);
4438 case FS_IOC_SETFLAGS
:
4439 return btrfs_ioctl_setflags(file
, argp
);
4440 case FS_IOC_GETVERSION
:
4441 return btrfs_ioctl_getversion(file
, argp
);
4443 return btrfs_ioctl_fitrim(file
, argp
);
4444 case BTRFS_IOC_SNAP_CREATE
:
4445 return btrfs_ioctl_snap_create(file
, argp
, 0);
4446 case BTRFS_IOC_SNAP_CREATE_V2
:
4447 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
4448 case BTRFS_IOC_SUBVOL_CREATE
:
4449 return btrfs_ioctl_snap_create(file
, argp
, 1);
4450 case BTRFS_IOC_SUBVOL_CREATE_V2
:
4451 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
4452 case BTRFS_IOC_SNAP_DESTROY
:
4453 return btrfs_ioctl_snap_destroy(file
, argp
);
4454 case BTRFS_IOC_SUBVOL_GETFLAGS
:
4455 return btrfs_ioctl_subvol_getflags(file
, argp
);
4456 case BTRFS_IOC_SUBVOL_SETFLAGS
:
4457 return btrfs_ioctl_subvol_setflags(file
, argp
);
4458 case BTRFS_IOC_DEFAULT_SUBVOL
:
4459 return btrfs_ioctl_default_subvol(file
, argp
);
4460 case BTRFS_IOC_DEFRAG
:
4461 return btrfs_ioctl_defrag(file
, NULL
);
4462 case BTRFS_IOC_DEFRAG_RANGE
:
4463 return btrfs_ioctl_defrag(file
, argp
);
4464 case BTRFS_IOC_RESIZE
:
4465 return btrfs_ioctl_resize(file
, argp
);
4466 case BTRFS_IOC_ADD_DEV
:
4467 return btrfs_ioctl_add_dev(root
, argp
);
4468 case BTRFS_IOC_RM_DEV
:
4469 return btrfs_ioctl_rm_dev(file
, argp
);
4470 case BTRFS_IOC_FS_INFO
:
4471 return btrfs_ioctl_fs_info(root
, argp
);
4472 case BTRFS_IOC_DEV_INFO
:
4473 return btrfs_ioctl_dev_info(root
, argp
);
4474 case BTRFS_IOC_BALANCE
:
4475 return btrfs_ioctl_balance(file
, NULL
);
4476 case BTRFS_IOC_CLONE
:
4477 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
4478 case BTRFS_IOC_CLONE_RANGE
:
4479 return btrfs_ioctl_clone_range(file
, argp
);
4480 case BTRFS_IOC_TRANS_START
:
4481 return btrfs_ioctl_trans_start(file
);
4482 case BTRFS_IOC_TRANS_END
:
4483 return btrfs_ioctl_trans_end(file
);
4484 case BTRFS_IOC_TREE_SEARCH
:
4485 return btrfs_ioctl_tree_search(file
, argp
);
4486 case BTRFS_IOC_INO_LOOKUP
:
4487 return btrfs_ioctl_ino_lookup(file
, argp
);
4488 case BTRFS_IOC_INO_PATHS
:
4489 return btrfs_ioctl_ino_to_path(root
, argp
);
4490 case BTRFS_IOC_LOGICAL_INO
:
4491 return btrfs_ioctl_logical_to_ino(root
, argp
);
4492 case BTRFS_IOC_SPACE_INFO
:
4493 return btrfs_ioctl_space_info(root
, argp
);
4494 case BTRFS_IOC_SYNC
:
4495 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
4497 case BTRFS_IOC_START_SYNC
:
4498 return btrfs_ioctl_start_sync(root
, argp
);
4499 case BTRFS_IOC_WAIT_SYNC
:
4500 return btrfs_ioctl_wait_sync(root
, argp
);
4501 case BTRFS_IOC_SCRUB
:
4502 return btrfs_ioctl_scrub(file
, argp
);
4503 case BTRFS_IOC_SCRUB_CANCEL
:
4504 return btrfs_ioctl_scrub_cancel(root
, argp
);
4505 case BTRFS_IOC_SCRUB_PROGRESS
:
4506 return btrfs_ioctl_scrub_progress(root
, argp
);
4507 case BTRFS_IOC_BALANCE_V2
:
4508 return btrfs_ioctl_balance(file
, argp
);
4509 case BTRFS_IOC_BALANCE_CTL
:
4510 return btrfs_ioctl_balance_ctl(root
, arg
);
4511 case BTRFS_IOC_BALANCE_PROGRESS
:
4512 return btrfs_ioctl_balance_progress(root
, argp
);
4513 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
4514 return btrfs_ioctl_set_received_subvol(file
, argp
);
4515 case BTRFS_IOC_SEND
:
4516 return btrfs_ioctl_send(file
, argp
);
4517 case BTRFS_IOC_GET_DEV_STATS
:
4518 return btrfs_ioctl_get_dev_stats(root
, argp
);
4519 case BTRFS_IOC_QUOTA_CTL
:
4520 return btrfs_ioctl_quota_ctl(file
, argp
);
4521 case BTRFS_IOC_QGROUP_ASSIGN
:
4522 return btrfs_ioctl_qgroup_assign(file
, argp
);
4523 case BTRFS_IOC_QGROUP_CREATE
:
4524 return btrfs_ioctl_qgroup_create(file
, argp
);
4525 case BTRFS_IOC_QGROUP_LIMIT
:
4526 return btrfs_ioctl_qgroup_limit(file
, argp
);
4527 case BTRFS_IOC_QUOTA_RESCAN
:
4528 return btrfs_ioctl_quota_rescan(file
, argp
);
4529 case BTRFS_IOC_QUOTA_RESCAN_STATUS
:
4530 return btrfs_ioctl_quota_rescan_status(file
, argp
);
4531 case BTRFS_IOC_QUOTA_RESCAN_WAIT
:
4532 return btrfs_ioctl_quota_rescan_wait(file
, argp
);
4533 case BTRFS_IOC_DEV_REPLACE
:
4534 return btrfs_ioctl_dev_replace(root
, argp
);
4535 case BTRFS_IOC_GET_FSLABEL
:
4536 return btrfs_ioctl_get_fslabel(file
, argp
);
4537 case BTRFS_IOC_SET_FSLABEL
:
4538 return btrfs_ioctl_set_fslabel(file
, argp
);
4539 case BTRFS_IOC_FILE_EXTENT_SAME
:
4540 return btrfs_ioctl_file_extent_same(file
, argp
);