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>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
50 #include "print-tree.h"
53 #include "inode-map.h"
56 /* Mask out flags that are inappropriate for the given type of inode. */
57 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
61 else if (S_ISREG(mode
))
62 return flags
& ~FS_DIRSYNC_FL
;
64 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
68 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
70 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
72 unsigned int iflags
= 0;
74 if (flags
& BTRFS_INODE_SYNC
)
76 if (flags
& BTRFS_INODE_IMMUTABLE
)
77 iflags
|= FS_IMMUTABLE_FL
;
78 if (flags
& BTRFS_INODE_APPEND
)
79 iflags
|= FS_APPEND_FL
;
80 if (flags
& BTRFS_INODE_NODUMP
)
81 iflags
|= FS_NODUMP_FL
;
82 if (flags
& BTRFS_INODE_NOATIME
)
83 iflags
|= FS_NOATIME_FL
;
84 if (flags
& BTRFS_INODE_DIRSYNC
)
85 iflags
|= FS_DIRSYNC_FL
;
86 if (flags
& BTRFS_INODE_NODATACOW
)
87 iflags
|= FS_NOCOW_FL
;
89 if ((flags
& BTRFS_INODE_COMPRESS
) && !(flags
& BTRFS_INODE_NOCOMPRESS
))
90 iflags
|= FS_COMPR_FL
;
91 else if (flags
& BTRFS_INODE_NOCOMPRESS
)
92 iflags
|= FS_NOCOMP_FL
;
98 * Update inode->i_flags based on the btrfs internal flags.
100 void btrfs_update_iflags(struct inode
*inode
)
102 struct btrfs_inode
*ip
= BTRFS_I(inode
);
104 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
106 if (ip
->flags
& BTRFS_INODE_SYNC
)
107 inode
->i_flags
|= S_SYNC
;
108 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
109 inode
->i_flags
|= S_IMMUTABLE
;
110 if (ip
->flags
& BTRFS_INODE_APPEND
)
111 inode
->i_flags
|= S_APPEND
;
112 if (ip
->flags
& BTRFS_INODE_NOATIME
)
113 inode
->i_flags
|= S_NOATIME
;
114 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
115 inode
->i_flags
|= S_DIRSYNC
;
119 * Inherit flags from the parent inode.
121 * Currently only the compression flags and the cow flags are inherited.
123 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
130 flags
= BTRFS_I(dir
)->flags
;
132 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
133 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
134 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
135 } else if (flags
& BTRFS_INODE_COMPRESS
) {
136 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
137 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
140 if (flags
& BTRFS_INODE_NODATACOW
)
141 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
143 btrfs_update_iflags(inode
);
146 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
148 struct btrfs_inode
*ip
= BTRFS_I(file
->f_path
.dentry
->d_inode
);
149 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
151 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
156 static int check_flags(unsigned int flags
)
158 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
159 FS_NOATIME_FL
| FS_NODUMP_FL
| \
160 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
161 FS_NOCOMP_FL
| FS_COMPR_FL
|
165 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
171 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
173 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
174 struct btrfs_inode
*ip
= BTRFS_I(inode
);
175 struct btrfs_root
*root
= ip
->root
;
176 struct btrfs_trans_handle
*trans
;
177 unsigned int flags
, oldflags
;
180 unsigned int i_oldflags
;
182 if (btrfs_root_readonly(root
))
185 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
188 ret
= check_flags(flags
);
192 if (!inode_owner_or_capable(inode
))
195 mutex_lock(&inode
->i_mutex
);
197 ip_oldflags
= ip
->flags
;
198 i_oldflags
= inode
->i_flags
;
200 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
201 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
202 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
203 if (!capable(CAP_LINUX_IMMUTABLE
)) {
209 ret
= mnt_want_write_file(file
);
213 if (flags
& FS_SYNC_FL
)
214 ip
->flags
|= BTRFS_INODE_SYNC
;
216 ip
->flags
&= ~BTRFS_INODE_SYNC
;
217 if (flags
& FS_IMMUTABLE_FL
)
218 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
220 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
221 if (flags
& FS_APPEND_FL
)
222 ip
->flags
|= BTRFS_INODE_APPEND
;
224 ip
->flags
&= ~BTRFS_INODE_APPEND
;
225 if (flags
& FS_NODUMP_FL
)
226 ip
->flags
|= BTRFS_INODE_NODUMP
;
228 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
229 if (flags
& FS_NOATIME_FL
)
230 ip
->flags
|= BTRFS_INODE_NOATIME
;
232 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
233 if (flags
& FS_DIRSYNC_FL
)
234 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
236 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
237 if (flags
& FS_NOCOW_FL
)
238 ip
->flags
|= BTRFS_INODE_NODATACOW
;
240 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
243 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
244 * flag may be changed automatically if compression code won't make
247 if (flags
& FS_NOCOMP_FL
) {
248 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
249 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
250 } else if (flags
& FS_COMPR_FL
) {
251 ip
->flags
|= BTRFS_INODE_COMPRESS
;
252 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
254 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
257 trans
= btrfs_start_transaction(root
, 1);
259 ret
= PTR_ERR(trans
);
263 btrfs_update_iflags(inode
);
264 inode_inc_iversion(inode
);
265 inode
->i_ctime
= CURRENT_TIME
;
266 ret
= btrfs_update_inode(trans
, root
, inode
);
268 btrfs_end_transaction(trans
, root
);
271 ip
->flags
= ip_oldflags
;
272 inode
->i_flags
= i_oldflags
;
275 mnt_drop_write_file(file
);
277 mutex_unlock(&inode
->i_mutex
);
281 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
283 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
285 return put_user(inode
->i_generation
, arg
);
288 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
290 struct btrfs_fs_info
*fs_info
= btrfs_sb(fdentry(file
)->d_sb
);
291 struct btrfs_device
*device
;
292 struct request_queue
*q
;
293 struct fstrim_range range
;
294 u64 minlen
= ULLONG_MAX
;
296 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
299 if (!capable(CAP_SYS_ADMIN
))
303 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
307 q
= bdev_get_queue(device
->bdev
);
308 if (blk_queue_discard(q
)) {
310 minlen
= min((u64
)q
->limits
.discard_granularity
,
318 if (copy_from_user(&range
, arg
, sizeof(range
)))
320 if (range
.start
> total_bytes
)
323 range
.len
= min(range
.len
, total_bytes
- range
.start
);
324 range
.minlen
= max(range
.minlen
, minlen
);
325 ret
= btrfs_trim_fs(fs_info
->tree_root
, &range
);
329 if (copy_to_user(arg
, &range
, sizeof(range
)))
335 static noinline
int create_subvol(struct btrfs_root
*root
,
336 struct dentry
*dentry
,
337 char *name
, int namelen
,
340 struct btrfs_trans_handle
*trans
;
341 struct btrfs_key key
;
342 struct btrfs_root_item root_item
;
343 struct btrfs_inode_item
*inode_item
;
344 struct extent_buffer
*leaf
;
345 struct btrfs_root
*new_root
;
346 struct dentry
*parent
= dentry
->d_parent
;
351 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
354 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
358 dir
= parent
->d_inode
;
366 trans
= btrfs_start_transaction(root
, 6);
368 return PTR_ERR(trans
);
370 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
371 0, objectid
, NULL
, 0, 0, 0);
377 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
378 btrfs_set_header_bytenr(leaf
, leaf
->start
);
379 btrfs_set_header_generation(leaf
, trans
->transid
);
380 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
381 btrfs_set_header_owner(leaf
, objectid
);
383 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
384 (unsigned long)btrfs_header_fsid(leaf
),
386 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
387 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
389 btrfs_mark_buffer_dirty(leaf
);
391 inode_item
= &root_item
.inode
;
392 memset(inode_item
, 0, sizeof(*inode_item
));
393 inode_item
->generation
= cpu_to_le64(1);
394 inode_item
->size
= cpu_to_le64(3);
395 inode_item
->nlink
= cpu_to_le32(1);
396 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
397 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
400 root_item
.byte_limit
= 0;
401 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
403 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
404 btrfs_set_root_generation(&root_item
, trans
->transid
);
405 btrfs_set_root_level(&root_item
, 0);
406 btrfs_set_root_refs(&root_item
, 1);
407 btrfs_set_root_used(&root_item
, leaf
->len
);
408 btrfs_set_root_last_snapshot(&root_item
, 0);
410 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
411 root_item
.drop_level
= 0;
413 btrfs_tree_unlock(leaf
);
414 free_extent_buffer(leaf
);
417 btrfs_set_root_dirid(&root_item
, new_dirid
);
419 key
.objectid
= objectid
;
421 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
422 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
427 key
.offset
= (u64
)-1;
428 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
429 if (IS_ERR(new_root
)) {
430 btrfs_abort_transaction(trans
, root
, PTR_ERR(new_root
));
431 ret
= PTR_ERR(new_root
);
435 btrfs_record_root_in_trans(trans
, new_root
);
437 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
);
439 /* We potentially lose an unused inode item here */
440 btrfs_abort_transaction(trans
, root
, ret
);
445 * insert the directory item
447 ret
= btrfs_set_inode_index(dir
, &index
);
449 btrfs_abort_transaction(trans
, root
, ret
);
453 ret
= btrfs_insert_dir_item(trans
, root
,
454 name
, namelen
, dir
, &key
,
455 BTRFS_FT_DIR
, index
);
457 btrfs_abort_transaction(trans
, root
, ret
);
461 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
462 ret
= btrfs_update_inode(trans
, root
, dir
);
465 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
466 objectid
, root
->root_key
.objectid
,
467 btrfs_ino(dir
), index
, name
, namelen
);
471 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
474 *async_transid
= trans
->transid
;
475 err
= btrfs_commit_transaction_async(trans
, root
, 1);
477 err
= btrfs_commit_transaction(trans
, root
);
484 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
485 char *name
, int namelen
, u64
*async_transid
,
489 struct btrfs_pending_snapshot
*pending_snapshot
;
490 struct btrfs_trans_handle
*trans
;
496 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
497 if (!pending_snapshot
)
500 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
501 pending_snapshot
->dentry
= dentry
;
502 pending_snapshot
->root
= root
;
503 pending_snapshot
->readonly
= readonly
;
505 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
507 ret
= PTR_ERR(trans
);
511 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
514 spin_lock(&root
->fs_info
->trans_lock
);
515 list_add(&pending_snapshot
->list
,
516 &trans
->transaction
->pending_snapshots
);
517 spin_unlock(&root
->fs_info
->trans_lock
);
519 *async_transid
= trans
->transid
;
520 ret
= btrfs_commit_transaction_async(trans
,
521 root
->fs_info
->extent_root
, 1);
523 ret
= btrfs_commit_transaction(trans
,
524 root
->fs_info
->extent_root
);
528 ret
= pending_snapshot
->error
;
532 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
536 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
538 ret
= PTR_ERR(inode
);
542 d_instantiate(dentry
, inode
);
545 kfree(pending_snapshot
);
549 /* copy of check_sticky in fs/namei.c()
550 * It's inline, so penalty for filesystems that don't use sticky bit is
553 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
555 uid_t fsuid
= current_fsuid();
557 if (!(dir
->i_mode
& S_ISVTX
))
559 if (inode
->i_uid
== fsuid
)
561 if (dir
->i_uid
== fsuid
)
563 return !capable(CAP_FOWNER
);
566 /* copy of may_delete in fs/namei.c()
567 * Check whether we can remove a link victim from directory dir, check
568 * whether the type of victim is right.
569 * 1. We can't do it if dir is read-only (done in permission())
570 * 2. We should have write and exec permissions on dir
571 * 3. We can't remove anything from append-only dir
572 * 4. We can't do anything with immutable dir (done in permission())
573 * 5. If the sticky bit on dir is set we should either
574 * a. be owner of dir, or
575 * b. be owner of victim, or
576 * c. have CAP_FOWNER capability
577 * 6. If the victim is append-only or immutable we can't do antyhing with
578 * links pointing to it.
579 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
580 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
581 * 9. We can't remove a root or mountpoint.
582 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
583 * nfs_async_unlink().
586 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
590 if (!victim
->d_inode
)
593 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
594 audit_inode_child(victim
, dir
);
596 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
601 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
602 IS_APPEND(victim
->d_inode
)||
603 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
606 if (!S_ISDIR(victim
->d_inode
->i_mode
))
610 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
614 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
619 /* copy of may_create in fs/namei.c() */
620 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
626 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
630 * Create a new subvolume below @parent. This is largely modeled after
631 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
632 * inside this filesystem so it's quite a bit simpler.
634 static noinline
int btrfs_mksubvol(struct path
*parent
,
635 char *name
, int namelen
,
636 struct btrfs_root
*snap_src
,
637 u64
*async_transid
, bool readonly
)
639 struct inode
*dir
= parent
->dentry
->d_inode
;
640 struct dentry
*dentry
;
643 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
645 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
646 error
= PTR_ERR(dentry
);
654 error
= mnt_want_write(parent
->mnt
);
658 error
= btrfs_may_create(dir
, dentry
);
662 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
664 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
668 error
= create_snapshot(snap_src
, dentry
,
669 name
, namelen
, async_transid
, readonly
);
671 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
672 name
, namelen
, async_transid
);
675 fsnotify_mkdir(dir
, dentry
);
677 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
679 mnt_drop_write(parent
->mnt
);
683 mutex_unlock(&dir
->i_mutex
);
688 * When we're defragging a range, we don't want to kick it off again
689 * if it is really just waiting for delalloc to send it down.
690 * If we find a nice big extent or delalloc range for the bytes in the
691 * file you want to defrag, we return 0 to let you know to skip this
694 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, int thresh
)
696 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
697 struct extent_map
*em
= NULL
;
698 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
701 read_lock(&em_tree
->lock
);
702 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
703 read_unlock(&em_tree
->lock
);
706 end
= extent_map_end(em
);
708 if (end
- offset
> thresh
)
711 /* if we already have a nice delalloc here, just stop */
713 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
714 thresh
, EXTENT_DELALLOC
, 1);
721 * helper function to walk through a file and find extents
722 * newer than a specific transid, and smaller than thresh.
724 * This is used by the defragging code to find new and small
727 static int find_new_extents(struct btrfs_root
*root
,
728 struct inode
*inode
, u64 newer_than
,
729 u64
*off
, int thresh
)
731 struct btrfs_path
*path
;
732 struct btrfs_key min_key
;
733 struct btrfs_key max_key
;
734 struct extent_buffer
*leaf
;
735 struct btrfs_file_extent_item
*extent
;
738 u64 ino
= btrfs_ino(inode
);
740 path
= btrfs_alloc_path();
744 min_key
.objectid
= ino
;
745 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
746 min_key
.offset
= *off
;
748 max_key
.objectid
= ino
;
749 max_key
.type
= (u8
)-1;
750 max_key
.offset
= (u64
)-1;
752 path
->keep_locks
= 1;
755 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
756 path
, 0, newer_than
);
759 if (min_key
.objectid
!= ino
)
761 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
764 leaf
= path
->nodes
[0];
765 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
766 struct btrfs_file_extent_item
);
768 type
= btrfs_file_extent_type(leaf
, extent
);
769 if (type
== BTRFS_FILE_EXTENT_REG
&&
770 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
771 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
772 *off
= min_key
.offset
;
773 btrfs_free_path(path
);
777 if (min_key
.offset
== (u64
)-1)
781 btrfs_release_path(path
);
784 btrfs_free_path(path
);
789 * Validaty check of prev em and next em:
791 * 2) prev/next em is an hole/inline extent
793 static int check_adjacent_extents(struct inode
*inode
, struct extent_map
*em
)
795 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
796 struct extent_map
*prev
= NULL
, *next
= NULL
;
799 read_lock(&em_tree
->lock
);
800 prev
= lookup_extent_mapping(em_tree
, em
->start
- 1, (u64
)-1);
801 next
= lookup_extent_mapping(em_tree
, em
->start
+ em
->len
, (u64
)-1);
802 read_unlock(&em_tree
->lock
);
804 if ((!prev
|| prev
->block_start
>= EXTENT_MAP_LAST_BYTE
) &&
805 (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
))
807 free_extent_map(prev
);
808 free_extent_map(next
);
813 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
814 int thresh
, u64
*last_len
, u64
*skip
,
817 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
818 struct extent_map
*em
= NULL
;
819 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
823 * make sure that once we start defragging an extent, we keep on
826 if (start
< *defrag_end
)
832 * hopefully we have this extent in the tree already, try without
833 * the full extent lock
835 read_lock(&em_tree
->lock
);
836 em
= lookup_extent_mapping(em_tree
, start
, len
);
837 read_unlock(&em_tree
->lock
);
840 /* get the big lock and read metadata off disk */
841 lock_extent(io_tree
, start
, start
+ len
- 1);
842 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
843 unlock_extent(io_tree
, start
, start
+ len
- 1);
849 /* this will cover holes, and inline extents */
850 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
855 /* If we have nothing to merge with us, just skip. */
856 if (check_adjacent_extents(inode
, em
)) {
862 * we hit a real extent, if it is big don't bother defragging it again
864 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
869 * last_len ends up being a counter of how many bytes we've defragged.
870 * every time we choose not to defrag an extent, we reset *last_len
871 * so that the next tiny extent will force a defrag.
873 * The end result of this is that tiny extents before a single big
874 * extent will force at least part of that big extent to be defragged.
877 *defrag_end
= extent_map_end(em
);
880 *skip
= extent_map_end(em
);
889 * it doesn't do much good to defrag one or two pages
890 * at a time. This pulls in a nice chunk of pages
893 * It also makes sure the delalloc code has enough
894 * dirty data to avoid making new small extents as part
897 * It's a good idea to start RA on this range
898 * before calling this.
900 static int cluster_pages_for_defrag(struct inode
*inode
,
902 unsigned long start_index
,
905 unsigned long file_end
;
906 u64 isize
= i_size_read(inode
);
913 struct btrfs_ordered_extent
*ordered
;
914 struct extent_state
*cached_state
= NULL
;
915 struct extent_io_tree
*tree
;
916 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
918 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
919 if (!isize
|| start_index
> file_end
)
922 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
924 ret
= btrfs_delalloc_reserve_space(inode
,
925 page_cnt
<< PAGE_CACHE_SHIFT
);
929 tree
= &BTRFS_I(inode
)->io_tree
;
931 /* step one, lock all the pages */
932 for (i
= 0; i
< page_cnt
; i
++) {
935 page
= find_or_create_page(inode
->i_mapping
,
936 start_index
+ i
, mask
);
940 page_start
= page_offset(page
);
941 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
943 lock_extent(tree
, page_start
, page_end
);
944 ordered
= btrfs_lookup_ordered_extent(inode
,
946 unlock_extent(tree
, page_start
, page_end
);
951 btrfs_start_ordered_extent(inode
, ordered
, 1);
952 btrfs_put_ordered_extent(ordered
);
955 * we unlocked the page above, so we need check if
956 * it was released or not.
958 if (page
->mapping
!= inode
->i_mapping
) {
960 page_cache_release(page
);
965 if (!PageUptodate(page
)) {
966 btrfs_readpage(NULL
, page
);
968 if (!PageUptodate(page
)) {
970 page_cache_release(page
);
976 if (page
->mapping
!= inode
->i_mapping
) {
978 page_cache_release(page
);
988 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
992 * so now we have a nice long stream of locked
993 * and up to date pages, lets wait on them
995 for (i
= 0; i
< i_done
; i
++)
996 wait_on_page_writeback(pages
[i
]);
998 page_start
= page_offset(pages
[0]);
999 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
1001 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1002 page_start
, page_end
- 1, 0, &cached_state
);
1003 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1004 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1005 EXTENT_DO_ACCOUNTING
, 0, 0, &cached_state
,
1008 if (i_done
!= page_cnt
) {
1009 spin_lock(&BTRFS_I(inode
)->lock
);
1010 BTRFS_I(inode
)->outstanding_extents
++;
1011 spin_unlock(&BTRFS_I(inode
)->lock
);
1012 btrfs_delalloc_release_space(inode
,
1013 (page_cnt
- i_done
) << PAGE_CACHE_SHIFT
);
1017 btrfs_set_extent_delalloc(inode
, page_start
, page_end
- 1,
1020 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1021 page_start
, page_end
- 1, &cached_state
,
1024 for (i
= 0; i
< i_done
; i
++) {
1025 clear_page_dirty_for_io(pages
[i
]);
1026 ClearPageChecked(pages
[i
]);
1027 set_page_extent_mapped(pages
[i
]);
1028 set_page_dirty(pages
[i
]);
1029 unlock_page(pages
[i
]);
1030 page_cache_release(pages
[i
]);
1034 for (i
= 0; i
< i_done
; i
++) {
1035 unlock_page(pages
[i
]);
1036 page_cache_release(pages
[i
]);
1038 btrfs_delalloc_release_space(inode
, page_cnt
<< PAGE_CACHE_SHIFT
);
1043 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1044 struct btrfs_ioctl_defrag_range_args
*range
,
1045 u64 newer_than
, unsigned long max_to_defrag
)
1047 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1048 struct btrfs_super_block
*disk_super
;
1049 struct file_ra_state
*ra
= NULL
;
1050 unsigned long last_index
;
1051 u64 isize
= i_size_read(inode
);
1056 u64 newer_off
= range
->start
;
1058 unsigned long ra_index
= 0;
1060 int defrag_count
= 0;
1061 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1062 int extent_thresh
= range
->extent_thresh
;
1063 int max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1064 int cluster
= max_cluster
;
1065 u64 new_align
= ~((u64
)128 * 1024 - 1);
1066 struct page
**pages
= NULL
;
1068 if (extent_thresh
== 0)
1069 extent_thresh
= 256 * 1024;
1071 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1072 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1074 if (range
->compress_type
)
1075 compress_type
= range
->compress_type
;
1082 * if we were not given a file, allocate a readahead
1086 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1089 file_ra_state_init(ra
, inode
->i_mapping
);
1094 pages
= kmalloc(sizeof(struct page
*) * max_cluster
,
1101 /* find the last page to defrag */
1102 if (range
->start
+ range
->len
> range
->start
) {
1103 last_index
= min_t(u64
, isize
- 1,
1104 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1106 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1110 ret
= find_new_extents(root
, inode
, newer_than
,
1111 &newer_off
, 64 * 1024);
1113 range
->start
= newer_off
;
1115 * we always align our defrag to help keep
1116 * the extents in the file evenly spaced
1118 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1122 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1125 max_to_defrag
= last_index
+ 1;
1128 * make writeback starts from i, so the defrag range can be
1129 * written sequentially.
1131 if (i
< inode
->i_mapping
->writeback_index
)
1132 inode
->i_mapping
->writeback_index
= i
;
1134 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1135 (i
< (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
1136 PAGE_CACHE_SHIFT
)) {
1138 * make sure we stop running if someone unmounts
1141 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1144 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1145 PAGE_CACHE_SIZE
, extent_thresh
,
1146 &last_len
, &skip
, &defrag_end
)) {
1149 * the should_defrag function tells us how much to skip
1150 * bump our counter by the suggested amount
1152 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1153 i
= max(i
+ 1, next
);
1158 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1159 PAGE_CACHE_SHIFT
) - i
;
1160 cluster
= min(cluster
, max_cluster
);
1162 cluster
= max_cluster
;
1165 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1166 BTRFS_I(inode
)->force_compress
= compress_type
;
1168 if (i
+ cluster
> ra_index
) {
1169 ra_index
= max(i
, ra_index
);
1170 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1172 ra_index
+= max_cluster
;
1175 mutex_lock(&inode
->i_mutex
);
1176 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1178 mutex_unlock(&inode
->i_mutex
);
1182 defrag_count
+= ret
;
1183 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, ret
);
1184 mutex_unlock(&inode
->i_mutex
);
1187 if (newer_off
== (u64
)-1)
1193 newer_off
= max(newer_off
+ 1,
1194 (u64
)i
<< PAGE_CACHE_SHIFT
);
1196 ret
= find_new_extents(root
, inode
,
1197 newer_than
, &newer_off
,
1200 range
->start
= newer_off
;
1201 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1208 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1216 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
1217 filemap_flush(inode
->i_mapping
);
1219 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1220 /* the filemap_flush will queue IO into the worker threads, but
1221 * we have to make sure the IO is actually started and that
1222 * ordered extents get created before we return
1224 atomic_inc(&root
->fs_info
->async_submit_draining
);
1225 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1226 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1227 wait_event(root
->fs_info
->async_submit_wait
,
1228 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1229 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1231 atomic_dec(&root
->fs_info
->async_submit_draining
);
1233 mutex_lock(&inode
->i_mutex
);
1234 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1235 mutex_unlock(&inode
->i_mutex
);
1238 disk_super
= root
->fs_info
->super_copy
;
1239 features
= btrfs_super_incompat_flags(disk_super
);
1240 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1241 features
|= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO
;
1242 btrfs_set_super_incompat_flags(disk_super
, features
);
1254 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
1260 struct btrfs_ioctl_vol_args
*vol_args
;
1261 struct btrfs_trans_handle
*trans
;
1262 struct btrfs_device
*device
= NULL
;
1264 char *devstr
= NULL
;
1268 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1271 if (!capable(CAP_SYS_ADMIN
))
1274 mutex_lock(&root
->fs_info
->volume_mutex
);
1275 if (root
->fs_info
->balance_ctl
) {
1276 printk(KERN_INFO
"btrfs: balance in progress\n");
1281 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1282 if (IS_ERR(vol_args
)) {
1283 ret
= PTR_ERR(vol_args
);
1287 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1289 sizestr
= vol_args
->name
;
1290 devstr
= strchr(sizestr
, ':');
1293 sizestr
= devstr
+ 1;
1295 devstr
= vol_args
->name
;
1296 devid
= simple_strtoull(devstr
, &end
, 10);
1297 printk(KERN_INFO
"btrfs: resizing devid %llu\n",
1298 (unsigned long long)devid
);
1300 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1302 printk(KERN_INFO
"btrfs: resizer unable to find device %llu\n",
1303 (unsigned long long)devid
);
1307 if (!strcmp(sizestr
, "max"))
1308 new_size
= device
->bdev
->bd_inode
->i_size
;
1310 if (sizestr
[0] == '-') {
1313 } else if (sizestr
[0] == '+') {
1317 new_size
= memparse(sizestr
, NULL
);
1318 if (new_size
== 0) {
1324 old_size
= device
->total_bytes
;
1327 if (new_size
> old_size
) {
1331 new_size
= old_size
- new_size
;
1332 } else if (mod
> 0) {
1333 new_size
= old_size
+ new_size
;
1336 if (new_size
< 256 * 1024 * 1024) {
1340 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1345 do_div(new_size
, root
->sectorsize
);
1346 new_size
*= root
->sectorsize
;
1348 printk(KERN_INFO
"btrfs: new size for %s is %llu\n",
1349 device
->name
, (unsigned long long)new_size
);
1351 if (new_size
> old_size
) {
1352 trans
= btrfs_start_transaction(root
, 0);
1353 if (IS_ERR(trans
)) {
1354 ret
= PTR_ERR(trans
);
1357 ret
= btrfs_grow_device(trans
, device
, new_size
);
1358 btrfs_commit_transaction(trans
, root
);
1359 } else if (new_size
< old_size
) {
1360 ret
= btrfs_shrink_device(device
, new_size
);
1366 mutex_unlock(&root
->fs_info
->volume_mutex
);
1370 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1377 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1378 struct file
*src_file
;
1382 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1385 namelen
= strlen(name
);
1386 if (strchr(name
, '/')) {
1391 if (name
[0] == '.' &&
1392 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1398 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1399 NULL
, transid
, readonly
);
1401 struct inode
*src_inode
;
1402 src_file
= fget(fd
);
1408 src_inode
= src_file
->f_path
.dentry
->d_inode
;
1409 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1410 printk(KERN_INFO
"btrfs: Snapshot src from "
1416 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1417 BTRFS_I(src_inode
)->root
,
1425 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1426 void __user
*arg
, int subvol
)
1428 struct btrfs_ioctl_vol_args
*vol_args
;
1431 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1432 if (IS_ERR(vol_args
))
1433 return PTR_ERR(vol_args
);
1434 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1436 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1437 vol_args
->fd
, subvol
,
1444 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1445 void __user
*arg
, int subvol
)
1447 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1451 bool readonly
= false;
1453 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1454 if (IS_ERR(vol_args
))
1455 return PTR_ERR(vol_args
);
1456 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1458 if (vol_args
->flags
&
1459 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
)) {
1464 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1466 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1469 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1470 vol_args
->fd
, subvol
,
1473 if (ret
== 0 && ptr
&&
1475 offsetof(struct btrfs_ioctl_vol_args_v2
,
1476 transid
), ptr
, sizeof(*ptr
)))
1483 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1486 struct inode
*inode
= fdentry(file
)->d_inode
;
1487 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1491 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1494 down_read(&root
->fs_info
->subvol_sem
);
1495 if (btrfs_root_readonly(root
))
1496 flags
|= BTRFS_SUBVOL_RDONLY
;
1497 up_read(&root
->fs_info
->subvol_sem
);
1499 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1505 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1508 struct inode
*inode
= fdentry(file
)->d_inode
;
1509 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1510 struct btrfs_trans_handle
*trans
;
1515 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1518 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1521 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1524 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1527 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1530 if (!inode_owner_or_capable(inode
))
1533 down_write(&root
->fs_info
->subvol_sem
);
1536 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1539 root_flags
= btrfs_root_flags(&root
->root_item
);
1540 if (flags
& BTRFS_SUBVOL_RDONLY
)
1541 btrfs_set_root_flags(&root
->root_item
,
1542 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1544 btrfs_set_root_flags(&root
->root_item
,
1545 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1547 trans
= btrfs_start_transaction(root
, 1);
1548 if (IS_ERR(trans
)) {
1549 ret
= PTR_ERR(trans
);
1553 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1554 &root
->root_key
, &root
->root_item
);
1556 btrfs_commit_transaction(trans
, root
);
1559 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1561 up_write(&root
->fs_info
->subvol_sem
);
1566 * helper to check if the subvolume references other subvolumes
1568 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1570 struct btrfs_path
*path
;
1571 struct btrfs_key key
;
1574 path
= btrfs_alloc_path();
1578 key
.objectid
= root
->root_key
.objectid
;
1579 key
.type
= BTRFS_ROOT_REF_KEY
;
1580 key
.offset
= (u64
)-1;
1582 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1589 if (path
->slots
[0] > 0) {
1591 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1592 if (key
.objectid
== root
->root_key
.objectid
&&
1593 key
.type
== BTRFS_ROOT_REF_KEY
)
1597 btrfs_free_path(path
);
1601 static noinline
int key_in_sk(struct btrfs_key
*key
,
1602 struct btrfs_ioctl_search_key
*sk
)
1604 struct btrfs_key test
;
1607 test
.objectid
= sk
->min_objectid
;
1608 test
.type
= sk
->min_type
;
1609 test
.offset
= sk
->min_offset
;
1611 ret
= btrfs_comp_cpu_keys(key
, &test
);
1615 test
.objectid
= sk
->max_objectid
;
1616 test
.type
= sk
->max_type
;
1617 test
.offset
= sk
->max_offset
;
1619 ret
= btrfs_comp_cpu_keys(key
, &test
);
1625 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1626 struct btrfs_path
*path
,
1627 struct btrfs_key
*key
,
1628 struct btrfs_ioctl_search_key
*sk
,
1630 unsigned long *sk_offset
,
1634 struct extent_buffer
*leaf
;
1635 struct btrfs_ioctl_search_header sh
;
1636 unsigned long item_off
;
1637 unsigned long item_len
;
1643 leaf
= path
->nodes
[0];
1644 slot
= path
->slots
[0];
1645 nritems
= btrfs_header_nritems(leaf
);
1647 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1651 found_transid
= btrfs_header_generation(leaf
);
1653 for (i
= slot
; i
< nritems
; i
++) {
1654 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1655 item_len
= btrfs_item_size_nr(leaf
, i
);
1657 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1660 if (sizeof(sh
) + item_len
+ *sk_offset
>
1661 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1666 btrfs_item_key_to_cpu(leaf
, key
, i
);
1667 if (!key_in_sk(key
, sk
))
1670 sh
.objectid
= key
->objectid
;
1671 sh
.offset
= key
->offset
;
1672 sh
.type
= key
->type
;
1674 sh
.transid
= found_transid
;
1676 /* copy search result header */
1677 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1678 *sk_offset
+= sizeof(sh
);
1681 char *p
= buf
+ *sk_offset
;
1683 read_extent_buffer(leaf
, p
,
1684 item_off
, item_len
);
1685 *sk_offset
+= item_len
;
1689 if (*num_found
>= sk
->nr_items
)
1694 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1696 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1699 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1709 static noinline
int search_ioctl(struct inode
*inode
,
1710 struct btrfs_ioctl_search_args
*args
)
1712 struct btrfs_root
*root
;
1713 struct btrfs_key key
;
1714 struct btrfs_key max_key
;
1715 struct btrfs_path
*path
;
1716 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1717 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1720 unsigned long sk_offset
= 0;
1722 path
= btrfs_alloc_path();
1726 if (sk
->tree_id
== 0) {
1727 /* search the root of the inode that was passed */
1728 root
= BTRFS_I(inode
)->root
;
1730 key
.objectid
= sk
->tree_id
;
1731 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1732 key
.offset
= (u64
)-1;
1733 root
= btrfs_read_fs_root_no_name(info
, &key
);
1735 printk(KERN_ERR
"could not find root %llu\n",
1737 btrfs_free_path(path
);
1742 key
.objectid
= sk
->min_objectid
;
1743 key
.type
= sk
->min_type
;
1744 key
.offset
= sk
->min_offset
;
1746 max_key
.objectid
= sk
->max_objectid
;
1747 max_key
.type
= sk
->max_type
;
1748 max_key
.offset
= sk
->max_offset
;
1750 path
->keep_locks
= 1;
1753 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1760 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1761 &sk_offset
, &num_found
);
1762 btrfs_release_path(path
);
1763 if (ret
|| num_found
>= sk
->nr_items
)
1769 sk
->nr_items
= num_found
;
1770 btrfs_free_path(path
);
1774 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1777 struct btrfs_ioctl_search_args
*args
;
1778 struct inode
*inode
;
1781 if (!capable(CAP_SYS_ADMIN
))
1784 args
= memdup_user(argp
, sizeof(*args
));
1786 return PTR_ERR(args
);
1788 inode
= fdentry(file
)->d_inode
;
1789 ret
= search_ioctl(inode
, args
);
1790 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1797 * Search INODE_REFs to identify path name of 'dirid' directory
1798 * in a 'tree_id' tree. and sets path name to 'name'.
1800 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1801 u64 tree_id
, u64 dirid
, char *name
)
1803 struct btrfs_root
*root
;
1804 struct btrfs_key key
;
1810 struct btrfs_inode_ref
*iref
;
1811 struct extent_buffer
*l
;
1812 struct btrfs_path
*path
;
1814 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1819 path
= btrfs_alloc_path();
1823 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1825 key
.objectid
= tree_id
;
1826 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1827 key
.offset
= (u64
)-1;
1828 root
= btrfs_read_fs_root_no_name(info
, &key
);
1830 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1835 key
.objectid
= dirid
;
1836 key
.type
= BTRFS_INODE_REF_KEY
;
1837 key
.offset
= (u64
)-1;
1840 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1845 slot
= path
->slots
[0];
1846 if (ret
> 0 && slot
> 0)
1848 btrfs_item_key_to_cpu(l
, &key
, slot
);
1850 if (ret
> 0 && (key
.objectid
!= dirid
||
1851 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1856 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1857 len
= btrfs_inode_ref_name_len(l
, iref
);
1859 total_len
+= len
+ 1;
1864 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1866 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1869 btrfs_release_path(path
);
1870 key
.objectid
= key
.offset
;
1871 key
.offset
= (u64
)-1;
1872 dirid
= key
.objectid
;
1876 memmove(name
, ptr
, total_len
);
1877 name
[total_len
]='\0';
1880 btrfs_free_path(path
);
1884 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1887 struct btrfs_ioctl_ino_lookup_args
*args
;
1888 struct inode
*inode
;
1891 if (!capable(CAP_SYS_ADMIN
))
1894 args
= memdup_user(argp
, sizeof(*args
));
1896 return PTR_ERR(args
);
1898 inode
= fdentry(file
)->d_inode
;
1900 if (args
->treeid
== 0)
1901 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1903 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1904 args
->treeid
, args
->objectid
,
1907 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1914 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1917 struct dentry
*parent
= fdentry(file
);
1918 struct dentry
*dentry
;
1919 struct inode
*dir
= parent
->d_inode
;
1920 struct inode
*inode
;
1921 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1922 struct btrfs_root
*dest
= NULL
;
1923 struct btrfs_ioctl_vol_args
*vol_args
;
1924 struct btrfs_trans_handle
*trans
;
1929 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1930 if (IS_ERR(vol_args
))
1931 return PTR_ERR(vol_args
);
1933 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1934 namelen
= strlen(vol_args
->name
);
1935 if (strchr(vol_args
->name
, '/') ||
1936 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1941 err
= mnt_want_write_file(file
);
1945 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1946 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1947 if (IS_ERR(dentry
)) {
1948 err
= PTR_ERR(dentry
);
1949 goto out_unlock_dir
;
1952 if (!dentry
->d_inode
) {
1957 inode
= dentry
->d_inode
;
1958 dest
= BTRFS_I(inode
)->root
;
1959 if (!capable(CAP_SYS_ADMIN
)){
1961 * Regular user. Only allow this with a special mount
1962 * option, when the user has write+exec access to the
1963 * subvol root, and when rmdir(2) would have been
1966 * Note that this is _not_ check that the subvol is
1967 * empty or doesn't contain data that we wouldn't
1968 * otherwise be able to delete.
1970 * Users who want to delete empty subvols should try
1974 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1978 * Do not allow deletion if the parent dir is the same
1979 * as the dir to be deleted. That means the ioctl
1980 * must be called on the dentry referencing the root
1981 * of the subvol, not a random directory contained
1988 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1992 /* check if subvolume may be deleted by a non-root user */
1993 err
= btrfs_may_delete(dir
, dentry
, 1);
1998 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
2003 mutex_lock(&inode
->i_mutex
);
2004 err
= d_invalidate(dentry
);
2008 down_write(&root
->fs_info
->subvol_sem
);
2010 err
= may_destroy_subvol(dest
);
2014 trans
= btrfs_start_transaction(root
, 0);
2015 if (IS_ERR(trans
)) {
2016 err
= PTR_ERR(trans
);
2019 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2021 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
2022 dest
->root_key
.objectid
,
2023 dentry
->d_name
.name
,
2024 dentry
->d_name
.len
);
2027 btrfs_abort_transaction(trans
, root
, ret
);
2031 btrfs_record_root_in_trans(trans
, dest
);
2033 memset(&dest
->root_item
.drop_progress
, 0,
2034 sizeof(dest
->root_item
.drop_progress
));
2035 dest
->root_item
.drop_level
= 0;
2036 btrfs_set_root_refs(&dest
->root_item
, 0);
2038 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
2039 ret
= btrfs_insert_orphan_item(trans
,
2040 root
->fs_info
->tree_root
,
2041 dest
->root_key
.objectid
);
2043 btrfs_abort_transaction(trans
, root
, ret
);
2049 ret
= btrfs_end_transaction(trans
, root
);
2052 inode
->i_flags
|= S_DEAD
;
2054 up_write(&root
->fs_info
->subvol_sem
);
2056 mutex_unlock(&inode
->i_mutex
);
2058 shrink_dcache_sb(root
->fs_info
->sb
);
2059 btrfs_invalidate_inodes(dest
);
2065 mutex_unlock(&dir
->i_mutex
);
2066 mnt_drop_write_file(file
);
2072 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2074 struct inode
*inode
= fdentry(file
)->d_inode
;
2075 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2076 struct btrfs_ioctl_defrag_range_args
*range
;
2079 if (btrfs_root_readonly(root
))
2082 ret
= mnt_want_write_file(file
);
2086 switch (inode
->i_mode
& S_IFMT
) {
2088 if (!capable(CAP_SYS_ADMIN
)) {
2092 ret
= btrfs_defrag_root(root
, 0);
2095 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2098 if (!(file
->f_mode
& FMODE_WRITE
)) {
2103 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2110 if (copy_from_user(range
, argp
,
2116 /* compression requires us to start the IO */
2117 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2118 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2119 range
->extent_thresh
= (u32
)-1;
2122 /* the rest are all set to zero by kzalloc */
2123 range
->len
= (u64
)-1;
2125 ret
= btrfs_defrag_file(fdentry(file
)->d_inode
, file
,
2135 mnt_drop_write_file(file
);
2139 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2141 struct btrfs_ioctl_vol_args
*vol_args
;
2144 if (!capable(CAP_SYS_ADMIN
))
2147 mutex_lock(&root
->fs_info
->volume_mutex
);
2148 if (root
->fs_info
->balance_ctl
) {
2149 printk(KERN_INFO
"btrfs: balance in progress\n");
2154 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2155 if (IS_ERR(vol_args
)) {
2156 ret
= PTR_ERR(vol_args
);
2160 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2161 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2165 mutex_unlock(&root
->fs_info
->volume_mutex
);
2169 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
2171 struct btrfs_ioctl_vol_args
*vol_args
;
2174 if (!capable(CAP_SYS_ADMIN
))
2177 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2180 mutex_lock(&root
->fs_info
->volume_mutex
);
2181 if (root
->fs_info
->balance_ctl
) {
2182 printk(KERN_INFO
"btrfs: balance in progress\n");
2187 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2188 if (IS_ERR(vol_args
)) {
2189 ret
= PTR_ERR(vol_args
);
2193 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2194 ret
= btrfs_rm_device(root
, vol_args
->name
);
2198 mutex_unlock(&root
->fs_info
->volume_mutex
);
2202 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2204 struct btrfs_ioctl_fs_info_args
*fi_args
;
2205 struct btrfs_device
*device
;
2206 struct btrfs_device
*next
;
2207 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2210 if (!capable(CAP_SYS_ADMIN
))
2213 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2217 fi_args
->num_devices
= fs_devices
->num_devices
;
2218 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2220 mutex_lock(&fs_devices
->device_list_mutex
);
2221 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
2222 if (device
->devid
> fi_args
->max_id
)
2223 fi_args
->max_id
= device
->devid
;
2225 mutex_unlock(&fs_devices
->device_list_mutex
);
2227 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2234 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2236 struct btrfs_ioctl_dev_info_args
*di_args
;
2237 struct btrfs_device
*dev
;
2238 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2240 char *s_uuid
= NULL
;
2241 char empty_uuid
[BTRFS_UUID_SIZE
] = {0};
2243 if (!capable(CAP_SYS_ADMIN
))
2246 di_args
= memdup_user(arg
, sizeof(*di_args
));
2247 if (IS_ERR(di_args
))
2248 return PTR_ERR(di_args
);
2250 if (memcmp(empty_uuid
, di_args
->uuid
, BTRFS_UUID_SIZE
) != 0)
2251 s_uuid
= di_args
->uuid
;
2253 mutex_lock(&fs_devices
->device_list_mutex
);
2254 dev
= btrfs_find_device(root
, di_args
->devid
, s_uuid
, NULL
);
2255 mutex_unlock(&fs_devices
->device_list_mutex
);
2262 di_args
->devid
= dev
->devid
;
2263 di_args
->bytes_used
= dev
->bytes_used
;
2264 di_args
->total_bytes
= dev
->total_bytes
;
2265 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2267 strncpy(di_args
->path
, dev
->name
, sizeof(di_args
->path
));
2268 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
2270 di_args
->path
[0] = '\0';
2274 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2281 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
2282 u64 off
, u64 olen
, u64 destoff
)
2284 struct inode
*inode
= fdentry(file
)->d_inode
;
2285 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2286 struct file
*src_file
;
2288 struct btrfs_trans_handle
*trans
;
2289 struct btrfs_path
*path
;
2290 struct extent_buffer
*leaf
;
2292 struct btrfs_key key
;
2297 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
2302 * - split compressed inline extents. annoying: we need to
2303 * decompress into destination's address_space (the file offset
2304 * may change, so source mapping won't do), then recompress (or
2305 * otherwise reinsert) a subrange.
2306 * - allow ranges within the same file to be cloned (provided
2307 * they don't overlap)?
2310 /* the destination must be opened for writing */
2311 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
2314 if (btrfs_root_readonly(root
))
2317 ret
= mnt_want_write_file(file
);
2321 src_file
= fget(srcfd
);
2324 goto out_drop_write
;
2327 src
= src_file
->f_dentry
->d_inode
;
2333 /* the src must be open for reading */
2334 if (!(src_file
->f_mode
& FMODE_READ
))
2337 /* don't make the dst file partly checksummed */
2338 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
2339 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
2343 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
2347 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
2351 buf
= vmalloc(btrfs_level_size(root
, 0));
2355 path
= btrfs_alloc_path();
2363 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
2364 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
2366 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
2367 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
2370 /* determine range to clone */
2372 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
2375 olen
= len
= src
->i_size
- off
;
2376 /* if we extend to eof, continue to block boundary */
2377 if (off
+ len
== src
->i_size
)
2378 len
= ALIGN(src
->i_size
, bs
) - off
;
2380 /* verify the end result is block aligned */
2381 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
2382 !IS_ALIGNED(destoff
, bs
))
2385 if (destoff
> inode
->i_size
) {
2386 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
2391 /* truncate page cache pages from target inode range */
2392 truncate_inode_pages_range(&inode
->i_data
, destoff
,
2393 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
2395 /* do any pending delalloc/csum calc on src, one way or
2396 another, and lock file content */
2398 struct btrfs_ordered_extent
*ordered
;
2399 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
);
2400 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
2402 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
2403 EXTENT_DELALLOC
, 0, NULL
))
2405 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
);
2407 btrfs_put_ordered_extent(ordered
);
2408 btrfs_wait_ordered_range(src
, off
, len
);
2412 key
.objectid
= btrfs_ino(src
);
2413 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2418 * note the key will change type as we walk through the
2421 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2425 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2426 if (path
->slots
[0] >= nritems
) {
2427 ret
= btrfs_next_leaf(root
, path
);
2432 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2434 leaf
= path
->nodes
[0];
2435 slot
= path
->slots
[0];
2437 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2438 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2439 key
.objectid
!= btrfs_ino(src
))
2442 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2443 struct btrfs_file_extent_item
*extent
;
2446 struct btrfs_key new_key
;
2447 u64 disko
= 0, diskl
= 0;
2448 u64 datao
= 0, datal
= 0;
2452 size
= btrfs_item_size_nr(leaf
, slot
);
2453 read_extent_buffer(leaf
, buf
,
2454 btrfs_item_ptr_offset(leaf
, slot
),
2457 extent
= btrfs_item_ptr(leaf
, slot
,
2458 struct btrfs_file_extent_item
);
2459 comp
= btrfs_file_extent_compression(leaf
, extent
);
2460 type
= btrfs_file_extent_type(leaf
, extent
);
2461 if (type
== BTRFS_FILE_EXTENT_REG
||
2462 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2463 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2465 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2467 datao
= btrfs_file_extent_offset(leaf
, extent
);
2468 datal
= btrfs_file_extent_num_bytes(leaf
,
2470 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2471 /* take upper bound, may be compressed */
2472 datal
= btrfs_file_extent_ram_bytes(leaf
,
2475 btrfs_release_path(path
);
2477 if (key
.offset
+ datal
<= off
||
2478 key
.offset
>= off
+len
)
2481 memcpy(&new_key
, &key
, sizeof(new_key
));
2482 new_key
.objectid
= btrfs_ino(inode
);
2483 if (off
<= key
.offset
)
2484 new_key
.offset
= key
.offset
+ destoff
- off
;
2486 new_key
.offset
= destoff
;
2489 * 1 - adjusting old extent (we may have to split it)
2490 * 1 - add new extent
2493 trans
= btrfs_start_transaction(root
, 3);
2494 if (IS_ERR(trans
)) {
2495 ret
= PTR_ERR(trans
);
2499 if (type
== BTRFS_FILE_EXTENT_REG
||
2500 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2502 * a | --- range to clone ---| b
2503 * | ------------- extent ------------- |
2506 /* substract range b */
2507 if (key
.offset
+ datal
> off
+ len
)
2508 datal
= off
+ len
- key
.offset
;
2510 /* substract range a */
2511 if (off
> key
.offset
) {
2512 datao
+= off
- key
.offset
;
2513 datal
-= off
- key
.offset
;
2516 ret
= btrfs_drop_extents(trans
, inode
,
2518 new_key
.offset
+ datal
,
2521 btrfs_abort_transaction(trans
, root
,
2523 btrfs_end_transaction(trans
, root
);
2527 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2530 btrfs_abort_transaction(trans
, root
,
2532 btrfs_end_transaction(trans
, root
);
2536 leaf
= path
->nodes
[0];
2537 slot
= path
->slots
[0];
2538 write_extent_buffer(leaf
, buf
,
2539 btrfs_item_ptr_offset(leaf
, slot
),
2542 extent
= btrfs_item_ptr(leaf
, slot
,
2543 struct btrfs_file_extent_item
);
2545 /* disko == 0 means it's a hole */
2549 btrfs_set_file_extent_offset(leaf
, extent
,
2551 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2554 inode_add_bytes(inode
, datal
);
2555 ret
= btrfs_inc_extent_ref(trans
, root
,
2557 root
->root_key
.objectid
,
2559 new_key
.offset
- datao
,
2562 btrfs_abort_transaction(trans
,
2565 btrfs_end_transaction(trans
,
2571 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2574 if (off
> key
.offset
) {
2575 skip
= off
- key
.offset
;
2576 new_key
.offset
+= skip
;
2579 if (key
.offset
+ datal
> off
+len
)
2580 trim
= key
.offset
+ datal
- (off
+len
);
2582 if (comp
&& (skip
|| trim
)) {
2584 btrfs_end_transaction(trans
, root
);
2587 size
-= skip
+ trim
;
2588 datal
-= skip
+ trim
;
2590 ret
= btrfs_drop_extents(trans
, inode
,
2592 new_key
.offset
+ datal
,
2595 btrfs_abort_transaction(trans
, root
,
2597 btrfs_end_transaction(trans
, root
);
2601 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2604 btrfs_abort_transaction(trans
, root
,
2606 btrfs_end_transaction(trans
, root
);
2612 btrfs_file_extent_calc_inline_size(0);
2613 memmove(buf
+start
, buf
+start
+skip
,
2617 leaf
= path
->nodes
[0];
2618 slot
= path
->slots
[0];
2619 write_extent_buffer(leaf
, buf
,
2620 btrfs_item_ptr_offset(leaf
, slot
),
2622 inode_add_bytes(inode
, datal
);
2625 btrfs_mark_buffer_dirty(leaf
);
2626 btrfs_release_path(path
);
2628 inode_inc_iversion(inode
);
2629 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2632 * we round up to the block size at eof when
2633 * determining which extents to clone above,
2634 * but shouldn't round up the file size
2636 endoff
= new_key
.offset
+ datal
;
2637 if (endoff
> destoff
+olen
)
2638 endoff
= destoff
+olen
;
2639 if (endoff
> inode
->i_size
)
2640 btrfs_i_size_write(inode
, endoff
);
2642 ret
= btrfs_update_inode(trans
, root
, inode
);
2644 btrfs_abort_transaction(trans
, root
, ret
);
2645 btrfs_end_transaction(trans
, root
);
2648 ret
= btrfs_end_transaction(trans
, root
);
2651 btrfs_release_path(path
);
2656 btrfs_release_path(path
);
2657 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
);
2659 mutex_unlock(&src
->i_mutex
);
2660 mutex_unlock(&inode
->i_mutex
);
2662 btrfs_free_path(path
);
2666 mnt_drop_write_file(file
);
2670 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2672 struct btrfs_ioctl_clone_range_args args
;
2674 if (copy_from_user(&args
, argp
, sizeof(args
)))
2676 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2677 args
.src_length
, args
.dest_offset
);
2681 * there are many ways the trans_start and trans_end ioctls can lead
2682 * to deadlocks. They should only be used by applications that
2683 * basically own the machine, and have a very in depth understanding
2684 * of all the possible deadlocks and enospc problems.
2686 static long btrfs_ioctl_trans_start(struct file
*file
)
2688 struct inode
*inode
= fdentry(file
)->d_inode
;
2689 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2690 struct btrfs_trans_handle
*trans
;
2694 if (!capable(CAP_SYS_ADMIN
))
2698 if (file
->private_data
)
2702 if (btrfs_root_readonly(root
))
2705 ret
= mnt_want_write_file(file
);
2709 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
2712 trans
= btrfs_start_ioctl_transaction(root
);
2716 file
->private_data
= trans
;
2720 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2721 mnt_drop_write_file(file
);
2726 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2728 struct inode
*inode
= fdentry(file
)->d_inode
;
2729 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2730 struct btrfs_root
*new_root
;
2731 struct btrfs_dir_item
*di
;
2732 struct btrfs_trans_handle
*trans
;
2733 struct btrfs_path
*path
;
2734 struct btrfs_key location
;
2735 struct btrfs_disk_key disk_key
;
2736 struct btrfs_super_block
*disk_super
;
2741 if (!capable(CAP_SYS_ADMIN
))
2744 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2748 objectid
= root
->root_key
.objectid
;
2750 location
.objectid
= objectid
;
2751 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2752 location
.offset
= (u64
)-1;
2754 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2755 if (IS_ERR(new_root
))
2756 return PTR_ERR(new_root
);
2758 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2761 path
= btrfs_alloc_path();
2764 path
->leave_spinning
= 1;
2766 trans
= btrfs_start_transaction(root
, 1);
2767 if (IS_ERR(trans
)) {
2768 btrfs_free_path(path
);
2769 return PTR_ERR(trans
);
2772 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
2773 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2774 dir_id
, "default", 7, 1);
2775 if (IS_ERR_OR_NULL(di
)) {
2776 btrfs_free_path(path
);
2777 btrfs_end_transaction(trans
, root
);
2778 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2779 "this isn't going to work\n");
2783 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2784 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2785 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2786 btrfs_free_path(path
);
2788 disk_super
= root
->fs_info
->super_copy
;
2789 features
= btrfs_super_incompat_flags(disk_super
);
2790 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2791 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2792 btrfs_set_super_incompat_flags(disk_super
, features
);
2794 btrfs_end_transaction(trans
, root
);
2799 static void get_block_group_info(struct list_head
*groups_list
,
2800 struct btrfs_ioctl_space_info
*space
)
2802 struct btrfs_block_group_cache
*block_group
;
2804 space
->total_bytes
= 0;
2805 space
->used_bytes
= 0;
2807 list_for_each_entry(block_group
, groups_list
, list
) {
2808 space
->flags
= block_group
->flags
;
2809 space
->total_bytes
+= block_group
->key
.offset
;
2810 space
->used_bytes
+=
2811 btrfs_block_group_used(&block_group
->item
);
2815 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2817 struct btrfs_ioctl_space_args space_args
;
2818 struct btrfs_ioctl_space_info space
;
2819 struct btrfs_ioctl_space_info
*dest
;
2820 struct btrfs_ioctl_space_info
*dest_orig
;
2821 struct btrfs_ioctl_space_info __user
*user_dest
;
2822 struct btrfs_space_info
*info
;
2823 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2824 BTRFS_BLOCK_GROUP_SYSTEM
,
2825 BTRFS_BLOCK_GROUP_METADATA
,
2826 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2833 if (copy_from_user(&space_args
,
2834 (struct btrfs_ioctl_space_args __user
*)arg
,
2835 sizeof(space_args
)))
2838 for (i
= 0; i
< num_types
; i
++) {
2839 struct btrfs_space_info
*tmp
;
2843 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2845 if (tmp
->flags
== types
[i
]) {
2855 down_read(&info
->groups_sem
);
2856 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2857 if (!list_empty(&info
->block_groups
[c
]))
2860 up_read(&info
->groups_sem
);
2863 /* space_slots == 0 means they are asking for a count */
2864 if (space_args
.space_slots
== 0) {
2865 space_args
.total_spaces
= slot_count
;
2869 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2871 alloc_size
= sizeof(*dest
) * slot_count
;
2873 /* we generally have at most 6 or so space infos, one for each raid
2874 * level. So, a whole page should be more than enough for everyone
2876 if (alloc_size
> PAGE_CACHE_SIZE
)
2879 space_args
.total_spaces
= 0;
2880 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2885 /* now we have a buffer to copy into */
2886 for (i
= 0; i
< num_types
; i
++) {
2887 struct btrfs_space_info
*tmp
;
2894 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2896 if (tmp
->flags
== types
[i
]) {
2905 down_read(&info
->groups_sem
);
2906 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2907 if (!list_empty(&info
->block_groups
[c
])) {
2908 get_block_group_info(&info
->block_groups
[c
],
2910 memcpy(dest
, &space
, sizeof(space
));
2912 space_args
.total_spaces
++;
2918 up_read(&info
->groups_sem
);
2921 user_dest
= (struct btrfs_ioctl_space_info __user
*)
2922 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2924 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2929 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2936 * there are many ways the trans_start and trans_end ioctls can lead
2937 * to deadlocks. They should only be used by applications that
2938 * basically own the machine, and have a very in depth understanding
2939 * of all the possible deadlocks and enospc problems.
2941 long btrfs_ioctl_trans_end(struct file
*file
)
2943 struct inode
*inode
= fdentry(file
)->d_inode
;
2944 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2945 struct btrfs_trans_handle
*trans
;
2947 trans
= file
->private_data
;
2950 file
->private_data
= NULL
;
2952 btrfs_end_transaction(trans
, root
);
2954 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2956 mnt_drop_write_file(file
);
2960 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2962 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2963 struct btrfs_trans_handle
*trans
;
2967 trans
= btrfs_start_transaction(root
, 0);
2969 return PTR_ERR(trans
);
2970 transid
= trans
->transid
;
2971 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
2973 btrfs_end_transaction(trans
, root
);
2978 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2983 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2985 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2989 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2992 transid
= 0; /* current trans */
2994 return btrfs_wait_for_commit(root
, transid
);
2997 static long btrfs_ioctl_scrub(struct btrfs_root
*root
, void __user
*arg
)
3000 struct btrfs_ioctl_scrub_args
*sa
;
3002 if (!capable(CAP_SYS_ADMIN
))
3005 sa
= memdup_user(arg
, sizeof(*sa
));
3009 ret
= btrfs_scrub_dev(root
, sa
->devid
, sa
->start
, sa
->end
,
3010 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
);
3012 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3019 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
3021 if (!capable(CAP_SYS_ADMIN
))
3024 return btrfs_scrub_cancel(root
);
3027 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
3030 struct btrfs_ioctl_scrub_args
*sa
;
3033 if (!capable(CAP_SYS_ADMIN
))
3036 sa
= memdup_user(arg
, sizeof(*sa
));
3040 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
3042 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3049 static long btrfs_ioctl_get_dev_stats(struct btrfs_root
*root
,
3050 void __user
*arg
, int reset_after_read
)
3052 struct btrfs_ioctl_get_dev_stats
*sa
;
3055 if (reset_after_read
&& !capable(CAP_SYS_ADMIN
))
3058 sa
= memdup_user(arg
, sizeof(*sa
));
3062 ret
= btrfs_get_dev_stats(root
, sa
, reset_after_read
);
3064 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3071 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
3077 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
3078 struct inode_fs_paths
*ipath
= NULL
;
3079 struct btrfs_path
*path
;
3081 if (!capable(CAP_SYS_ADMIN
))
3084 path
= btrfs_alloc_path();
3090 ipa
= memdup_user(arg
, sizeof(*ipa
));
3097 size
= min_t(u32
, ipa
->size
, 4096);
3098 ipath
= init_ipath(size
, root
, path
);
3099 if (IS_ERR(ipath
)) {
3100 ret
= PTR_ERR(ipath
);
3105 ret
= paths_from_inode(ipa
->inum
, ipath
);
3109 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
3110 rel_ptr
= ipath
->fspath
->val
[i
] -
3111 (u64
)(unsigned long)ipath
->fspath
->val
;
3112 ipath
->fspath
->val
[i
] = rel_ptr
;
3115 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
3116 (void *)(unsigned long)ipath
->fspath
, size
);
3123 btrfs_free_path(path
);
3130 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
3132 struct btrfs_data_container
*inodes
= ctx
;
3133 const size_t c
= 3 * sizeof(u64
);
3135 if (inodes
->bytes_left
>= c
) {
3136 inodes
->bytes_left
-= c
;
3137 inodes
->val
[inodes
->elem_cnt
] = inum
;
3138 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
3139 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
3140 inodes
->elem_cnt
+= 3;
3142 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
3143 inodes
->bytes_left
= 0;
3144 inodes
->elem_missed
+= 3;
3150 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
3155 u64 extent_item_pos
;
3156 struct btrfs_ioctl_logical_ino_args
*loi
;
3157 struct btrfs_data_container
*inodes
= NULL
;
3158 struct btrfs_path
*path
= NULL
;
3159 struct btrfs_key key
;
3161 if (!capable(CAP_SYS_ADMIN
))
3164 loi
= memdup_user(arg
, sizeof(*loi
));
3171 path
= btrfs_alloc_path();
3177 size
= min_t(u32
, loi
->size
, 4096);
3178 inodes
= init_data_container(size
);
3179 if (IS_ERR(inodes
)) {
3180 ret
= PTR_ERR(inodes
);
3185 ret
= extent_from_logical(root
->fs_info
, loi
->logical
, path
, &key
);
3186 btrfs_release_path(path
);
3188 if (ret
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
3193 extent_item_pos
= loi
->logical
- key
.objectid
;
3194 ret
= iterate_extent_inodes(root
->fs_info
, key
.objectid
,
3195 extent_item_pos
, 0, build_ino_list
,
3201 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
3202 (void *)(unsigned long)inodes
, size
);
3207 btrfs_free_path(path
);
3214 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3215 struct btrfs_ioctl_balance_args
*bargs
)
3217 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3219 bargs
->flags
= bctl
->flags
;
3221 if (atomic_read(&fs_info
->balance_running
))
3222 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
3223 if (atomic_read(&fs_info
->balance_pause_req
))
3224 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
3225 if (atomic_read(&fs_info
->balance_cancel_req
))
3226 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
3228 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
3229 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
3230 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
3233 spin_lock(&fs_info
->balance_lock
);
3234 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3235 spin_unlock(&fs_info
->balance_lock
);
3237 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3241 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
3243 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
3244 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3245 struct btrfs_ioctl_balance_args
*bargs
;
3246 struct btrfs_balance_control
*bctl
;
3249 if (!capable(CAP_SYS_ADMIN
))
3252 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
3255 ret
= mnt_want_write(file
->f_path
.mnt
);
3259 mutex_lock(&fs_info
->volume_mutex
);
3260 mutex_lock(&fs_info
->balance_mutex
);
3263 bargs
= memdup_user(arg
, sizeof(*bargs
));
3264 if (IS_ERR(bargs
)) {
3265 ret
= PTR_ERR(bargs
);
3269 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
3270 if (!fs_info
->balance_ctl
) {
3275 bctl
= fs_info
->balance_ctl
;
3276 spin_lock(&fs_info
->balance_lock
);
3277 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3278 spin_unlock(&fs_info
->balance_lock
);
3286 if (fs_info
->balance_ctl
) {
3291 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3297 bctl
->fs_info
= fs_info
;
3299 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
3300 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
3301 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
3303 bctl
->flags
= bargs
->flags
;
3305 /* balance everything - no filters */
3306 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
3310 ret
= btrfs_balance(bctl
, bargs
);
3312 * bctl is freed in __cancel_balance or in free_fs_info if
3313 * restriper was paused all the way until unmount
3316 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3323 mutex_unlock(&fs_info
->balance_mutex
);
3324 mutex_unlock(&fs_info
->volume_mutex
);
3325 mnt_drop_write(file
->f_path
.mnt
);
3329 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
3331 if (!capable(CAP_SYS_ADMIN
))
3335 case BTRFS_BALANCE_CTL_PAUSE
:
3336 return btrfs_pause_balance(root
->fs_info
);
3337 case BTRFS_BALANCE_CTL_CANCEL
:
3338 return btrfs_cancel_balance(root
->fs_info
);
3344 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
3347 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3348 struct btrfs_ioctl_balance_args
*bargs
;
3351 if (!capable(CAP_SYS_ADMIN
))
3354 mutex_lock(&fs_info
->balance_mutex
);
3355 if (!fs_info
->balance_ctl
) {
3360 bargs
= kzalloc(sizeof(*bargs
), GFP_NOFS
);
3366 update_ioctl_balance_args(fs_info
, 1, bargs
);
3368 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3373 mutex_unlock(&fs_info
->balance_mutex
);
3377 long btrfs_ioctl(struct file
*file
, unsigned int
3378 cmd
, unsigned long arg
)
3380 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
3381 void __user
*argp
= (void __user
*)arg
;
3384 case FS_IOC_GETFLAGS
:
3385 return btrfs_ioctl_getflags(file
, argp
);
3386 case FS_IOC_SETFLAGS
:
3387 return btrfs_ioctl_setflags(file
, argp
);
3388 case FS_IOC_GETVERSION
:
3389 return btrfs_ioctl_getversion(file
, argp
);
3391 return btrfs_ioctl_fitrim(file
, argp
);
3392 case BTRFS_IOC_SNAP_CREATE
:
3393 return btrfs_ioctl_snap_create(file
, argp
, 0);
3394 case BTRFS_IOC_SNAP_CREATE_V2
:
3395 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
3396 case BTRFS_IOC_SUBVOL_CREATE
:
3397 return btrfs_ioctl_snap_create(file
, argp
, 1);
3398 case BTRFS_IOC_SNAP_DESTROY
:
3399 return btrfs_ioctl_snap_destroy(file
, argp
);
3400 case BTRFS_IOC_SUBVOL_GETFLAGS
:
3401 return btrfs_ioctl_subvol_getflags(file
, argp
);
3402 case BTRFS_IOC_SUBVOL_SETFLAGS
:
3403 return btrfs_ioctl_subvol_setflags(file
, argp
);
3404 case BTRFS_IOC_DEFAULT_SUBVOL
:
3405 return btrfs_ioctl_default_subvol(file
, argp
);
3406 case BTRFS_IOC_DEFRAG
:
3407 return btrfs_ioctl_defrag(file
, NULL
);
3408 case BTRFS_IOC_DEFRAG_RANGE
:
3409 return btrfs_ioctl_defrag(file
, argp
);
3410 case BTRFS_IOC_RESIZE
:
3411 return btrfs_ioctl_resize(root
, argp
);
3412 case BTRFS_IOC_ADD_DEV
:
3413 return btrfs_ioctl_add_dev(root
, argp
);
3414 case BTRFS_IOC_RM_DEV
:
3415 return btrfs_ioctl_rm_dev(root
, argp
);
3416 case BTRFS_IOC_FS_INFO
:
3417 return btrfs_ioctl_fs_info(root
, argp
);
3418 case BTRFS_IOC_DEV_INFO
:
3419 return btrfs_ioctl_dev_info(root
, argp
);
3420 case BTRFS_IOC_BALANCE
:
3421 return btrfs_ioctl_balance(file
, NULL
);
3422 case BTRFS_IOC_CLONE
:
3423 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
3424 case BTRFS_IOC_CLONE_RANGE
:
3425 return btrfs_ioctl_clone_range(file
, argp
);
3426 case BTRFS_IOC_TRANS_START
:
3427 return btrfs_ioctl_trans_start(file
);
3428 case BTRFS_IOC_TRANS_END
:
3429 return btrfs_ioctl_trans_end(file
);
3430 case BTRFS_IOC_TREE_SEARCH
:
3431 return btrfs_ioctl_tree_search(file
, argp
);
3432 case BTRFS_IOC_INO_LOOKUP
:
3433 return btrfs_ioctl_ino_lookup(file
, argp
);
3434 case BTRFS_IOC_INO_PATHS
:
3435 return btrfs_ioctl_ino_to_path(root
, argp
);
3436 case BTRFS_IOC_LOGICAL_INO
:
3437 return btrfs_ioctl_logical_to_ino(root
, argp
);
3438 case BTRFS_IOC_SPACE_INFO
:
3439 return btrfs_ioctl_space_info(root
, argp
);
3440 case BTRFS_IOC_SYNC
:
3441 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
3443 case BTRFS_IOC_START_SYNC
:
3444 return btrfs_ioctl_start_sync(file
, argp
);
3445 case BTRFS_IOC_WAIT_SYNC
:
3446 return btrfs_ioctl_wait_sync(file
, argp
);
3447 case BTRFS_IOC_SCRUB
:
3448 return btrfs_ioctl_scrub(root
, argp
);
3449 case BTRFS_IOC_SCRUB_CANCEL
:
3450 return btrfs_ioctl_scrub_cancel(root
, argp
);
3451 case BTRFS_IOC_SCRUB_PROGRESS
:
3452 return btrfs_ioctl_scrub_progress(root
, argp
);
3453 case BTRFS_IOC_BALANCE_V2
:
3454 return btrfs_ioctl_balance(file
, argp
);
3455 case BTRFS_IOC_BALANCE_CTL
:
3456 return btrfs_ioctl_balance_ctl(root
, arg
);
3457 case BTRFS_IOC_BALANCE_PROGRESS
:
3458 return btrfs_ioctl_balance_progress(root
, argp
);
3459 case BTRFS_IOC_GET_DEV_STATS
:
3460 return btrfs_ioctl_get_dev_stats(root
, argp
, 0);
3461 case BTRFS_IOC_GET_AND_RESET_DEV_STATS
:
3462 return btrfs_ioctl_get_dev_stats(root
, argp
, 1);