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"
54 /* Mask out flags that are inappropriate for the given type of inode. */
55 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
59 else if (S_ISREG(mode
))
60 return flags
& ~FS_DIRSYNC_FL
;
62 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
66 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
68 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
70 unsigned int iflags
= 0;
72 if (flags
& BTRFS_INODE_SYNC
)
74 if (flags
& BTRFS_INODE_IMMUTABLE
)
75 iflags
|= FS_IMMUTABLE_FL
;
76 if (flags
& BTRFS_INODE_APPEND
)
77 iflags
|= FS_APPEND_FL
;
78 if (flags
& BTRFS_INODE_NODUMP
)
79 iflags
|= FS_NODUMP_FL
;
80 if (flags
& BTRFS_INODE_NOATIME
)
81 iflags
|= FS_NOATIME_FL
;
82 if (flags
& BTRFS_INODE_DIRSYNC
)
83 iflags
|= FS_DIRSYNC_FL
;
89 * Update inode->i_flags based on the btrfs internal flags.
91 void btrfs_update_iflags(struct inode
*inode
)
93 struct btrfs_inode
*ip
= BTRFS_I(inode
);
95 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
97 if (ip
->flags
& BTRFS_INODE_SYNC
)
98 inode
->i_flags
|= S_SYNC
;
99 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
100 inode
->i_flags
|= S_IMMUTABLE
;
101 if (ip
->flags
& BTRFS_INODE_APPEND
)
102 inode
->i_flags
|= S_APPEND
;
103 if (ip
->flags
& BTRFS_INODE_NOATIME
)
104 inode
->i_flags
|= S_NOATIME
;
105 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
106 inode
->i_flags
|= S_DIRSYNC
;
110 * Inherit flags from the parent inode.
112 * Unlike extN we don't have any flags we don't want to inherit currently.
114 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
121 flags
= BTRFS_I(dir
)->flags
;
123 if (S_ISREG(inode
->i_mode
))
124 flags
&= ~BTRFS_INODE_DIRSYNC
;
125 else if (!S_ISDIR(inode
->i_mode
))
126 flags
&= (BTRFS_INODE_NODUMP
| BTRFS_INODE_NOATIME
);
128 BTRFS_I(inode
)->flags
= flags
;
129 btrfs_update_iflags(inode
);
132 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
134 struct btrfs_inode
*ip
= BTRFS_I(file
->f_path
.dentry
->d_inode
);
135 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
137 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
142 static int check_flags(unsigned int flags
)
144 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
145 FS_NOATIME_FL
| FS_NODUMP_FL
| \
146 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
147 FS_NOCOMP_FL
| FS_COMPR_FL
|
151 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
157 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
159 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
160 struct btrfs_inode
*ip
= BTRFS_I(inode
);
161 struct btrfs_root
*root
= ip
->root
;
162 struct btrfs_trans_handle
*trans
;
163 unsigned int flags
, oldflags
;
166 if (btrfs_root_readonly(root
))
169 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
172 ret
= check_flags(flags
);
176 if (!is_owner_or_cap(inode
))
179 mutex_lock(&inode
->i_mutex
);
181 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
182 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
183 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
184 if (!capable(CAP_LINUX_IMMUTABLE
)) {
190 ret
= mnt_want_write(file
->f_path
.mnt
);
194 if (flags
& FS_SYNC_FL
)
195 ip
->flags
|= BTRFS_INODE_SYNC
;
197 ip
->flags
&= ~BTRFS_INODE_SYNC
;
198 if (flags
& FS_IMMUTABLE_FL
)
199 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
201 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
202 if (flags
& FS_APPEND_FL
)
203 ip
->flags
|= BTRFS_INODE_APPEND
;
205 ip
->flags
&= ~BTRFS_INODE_APPEND
;
206 if (flags
& FS_NODUMP_FL
)
207 ip
->flags
|= BTRFS_INODE_NODUMP
;
209 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
210 if (flags
& FS_NOATIME_FL
)
211 ip
->flags
|= BTRFS_INODE_NOATIME
;
213 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
214 if (flags
& FS_DIRSYNC_FL
)
215 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
217 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
218 if (flags
& FS_NOCOW_FL
)
219 ip
->flags
|= BTRFS_INODE_NODATACOW
;
221 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
224 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
225 * flag may be changed automatically if compression code won't make
228 if (flags
& FS_NOCOMP_FL
) {
229 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
230 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
231 } else if (flags
& FS_COMPR_FL
) {
232 ip
->flags
|= BTRFS_INODE_COMPRESS
;
233 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
236 trans
= btrfs_join_transaction(root
, 1);
237 BUG_ON(IS_ERR(trans
));
239 ret
= btrfs_update_inode(trans
, root
, inode
);
242 btrfs_update_iflags(inode
);
243 inode
->i_ctime
= CURRENT_TIME
;
244 btrfs_end_transaction(trans
, root
);
246 mnt_drop_write(file
->f_path
.mnt
);
250 mutex_unlock(&inode
->i_mutex
);
254 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
256 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
258 return put_user(inode
->i_generation
, arg
);
261 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
263 struct btrfs_root
*root
= fdentry(file
)->d_sb
->s_fs_info
;
264 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
265 struct btrfs_device
*device
;
266 struct request_queue
*q
;
267 struct fstrim_range range
;
268 u64 minlen
= ULLONG_MAX
;
272 if (!capable(CAP_SYS_ADMIN
))
275 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
276 list_for_each_entry(device
, &fs_info
->fs_devices
->devices
, dev_list
) {
279 q
= bdev_get_queue(device
->bdev
);
280 if (blk_queue_discard(q
)) {
282 minlen
= min((u64
)q
->limits
.discard_granularity
,
286 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
290 if (copy_from_user(&range
, arg
, sizeof(range
)))
293 range
.minlen
= max(range
.minlen
, minlen
);
294 ret
= btrfs_trim_fs(root
, &range
);
298 if (copy_to_user(arg
, &range
, sizeof(range
)))
304 static noinline
int create_subvol(struct btrfs_root
*root
,
305 struct dentry
*dentry
,
306 char *name
, int namelen
,
309 struct btrfs_trans_handle
*trans
;
310 struct btrfs_key key
;
311 struct btrfs_root_item root_item
;
312 struct btrfs_inode_item
*inode_item
;
313 struct extent_buffer
*leaf
;
314 struct btrfs_root
*new_root
;
315 struct dentry
*parent
= dget_parent(dentry
);
320 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
323 ret
= btrfs_find_free_objectid(NULL
, root
->fs_info
->tree_root
,
330 dir
= parent
->d_inode
;
338 trans
= btrfs_start_transaction(root
, 6);
341 return PTR_ERR(trans
);
344 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
345 0, objectid
, NULL
, 0, 0, 0);
351 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
352 btrfs_set_header_bytenr(leaf
, leaf
->start
);
353 btrfs_set_header_generation(leaf
, trans
->transid
);
354 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
355 btrfs_set_header_owner(leaf
, objectid
);
357 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
358 (unsigned long)btrfs_header_fsid(leaf
),
360 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
361 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
363 btrfs_mark_buffer_dirty(leaf
);
365 inode_item
= &root_item
.inode
;
366 memset(inode_item
, 0, sizeof(*inode_item
));
367 inode_item
->generation
= cpu_to_le64(1);
368 inode_item
->size
= cpu_to_le64(3);
369 inode_item
->nlink
= cpu_to_le32(1);
370 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
371 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
374 root_item
.byte_limit
= 0;
375 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
377 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
378 btrfs_set_root_generation(&root_item
, trans
->transid
);
379 btrfs_set_root_level(&root_item
, 0);
380 btrfs_set_root_refs(&root_item
, 1);
381 btrfs_set_root_used(&root_item
, leaf
->len
);
382 btrfs_set_root_last_snapshot(&root_item
, 0);
384 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
385 root_item
.drop_level
= 0;
387 btrfs_tree_unlock(leaf
);
388 free_extent_buffer(leaf
);
391 btrfs_set_root_dirid(&root_item
, new_dirid
);
393 key
.objectid
= objectid
;
395 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
396 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
401 key
.offset
= (u64
)-1;
402 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
403 BUG_ON(IS_ERR(new_root
));
405 btrfs_record_root_in_trans(trans
, new_root
);
407 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
408 BTRFS_I(dir
)->block_group
);
410 * insert the directory item
412 ret
= btrfs_set_inode_index(dir
, &index
);
415 ret
= btrfs_insert_dir_item(trans
, root
,
416 name
, namelen
, dir
->i_ino
, &key
,
417 BTRFS_FT_DIR
, index
);
421 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
422 ret
= btrfs_update_inode(trans
, root
, dir
);
425 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
426 objectid
, root
->root_key
.objectid
,
427 dir
->i_ino
, index
, name
, namelen
);
431 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
435 *async_transid
= trans
->transid
;
436 err
= btrfs_commit_transaction_async(trans
, root
, 1);
438 err
= btrfs_commit_transaction(trans
, root
);
445 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
446 char *name
, int namelen
, u64
*async_transid
,
450 struct dentry
*parent
;
451 struct btrfs_pending_snapshot
*pending_snapshot
;
452 struct btrfs_trans_handle
*trans
;
458 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
459 if (!pending_snapshot
)
462 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
463 pending_snapshot
->dentry
= dentry
;
464 pending_snapshot
->root
= root
;
465 pending_snapshot
->readonly
= readonly
;
467 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
469 ret
= PTR_ERR(trans
);
473 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
476 list_add(&pending_snapshot
->list
,
477 &trans
->transaction
->pending_snapshots
);
479 *async_transid
= trans
->transid
;
480 ret
= btrfs_commit_transaction_async(trans
,
481 root
->fs_info
->extent_root
, 1);
483 ret
= btrfs_commit_transaction(trans
,
484 root
->fs_info
->extent_root
);
488 ret
= pending_snapshot
->error
;
492 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
496 parent
= dget_parent(dentry
);
497 inode
= btrfs_lookup_dentry(parent
->d_inode
, dentry
);
500 ret
= PTR_ERR(inode
);
504 d_instantiate(dentry
, inode
);
507 kfree(pending_snapshot
);
511 /* copy of check_sticky in fs/namei.c()
512 * It's inline, so penalty for filesystems that don't use sticky bit is
515 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
517 uid_t fsuid
= current_fsuid();
519 if (!(dir
->i_mode
& S_ISVTX
))
521 if (inode
->i_uid
== fsuid
)
523 if (dir
->i_uid
== fsuid
)
525 return !capable(CAP_FOWNER
);
528 /* copy of may_delete in fs/namei.c()
529 * Check whether we can remove a link victim from directory dir, check
530 * whether the type of victim is right.
531 * 1. We can't do it if dir is read-only (done in permission())
532 * 2. We should have write and exec permissions on dir
533 * 3. We can't remove anything from append-only dir
534 * 4. We can't do anything with immutable dir (done in permission())
535 * 5. If the sticky bit on dir is set we should either
536 * a. be owner of dir, or
537 * b. be owner of victim, or
538 * c. have CAP_FOWNER capability
539 * 6. If the victim is append-only or immutable we can't do antyhing with
540 * links pointing to it.
541 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
542 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
543 * 9. We can't remove a root or mountpoint.
544 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
545 * nfs_async_unlink().
548 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
552 if (!victim
->d_inode
)
555 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
556 audit_inode_child(victim
, dir
);
558 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
563 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
564 IS_APPEND(victim
->d_inode
)||
565 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
568 if (!S_ISDIR(victim
->d_inode
->i_mode
))
572 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
576 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
581 /* copy of may_create in fs/namei.c() */
582 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
588 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
592 * Create a new subvolume below @parent. This is largely modeled after
593 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
594 * inside this filesystem so it's quite a bit simpler.
596 static noinline
int btrfs_mksubvol(struct path
*parent
,
597 char *name
, int namelen
,
598 struct btrfs_root
*snap_src
,
599 u64
*async_transid
, bool readonly
)
601 struct inode
*dir
= parent
->dentry
->d_inode
;
602 struct dentry
*dentry
;
605 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
607 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
608 error
= PTR_ERR(dentry
);
616 error
= mnt_want_write(parent
->mnt
);
620 error
= btrfs_may_create(dir
, dentry
);
624 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
626 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
630 error
= create_snapshot(snap_src
, dentry
,
631 name
, namelen
, async_transid
, readonly
);
633 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
634 name
, namelen
, async_transid
);
637 fsnotify_mkdir(dir
, dentry
);
639 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
641 mnt_drop_write(parent
->mnt
);
645 mutex_unlock(&dir
->i_mutex
);
649 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
650 int thresh
, u64
*last_len
, u64
*skip
,
653 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
654 struct extent_map
*em
= NULL
;
655 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
663 * make sure that once we start defragging and extent, we keep on
666 if (start
< *defrag_end
)
672 * hopefully we have this extent in the tree already, try without
673 * the full extent lock
675 read_lock(&em_tree
->lock
);
676 em
= lookup_extent_mapping(em_tree
, start
, len
);
677 read_unlock(&em_tree
->lock
);
680 /* get the big lock and read metadata off disk */
681 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
682 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
683 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
689 /* this will cover holes, and inline extents */
690 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
694 * we hit a real extent, if it is big don't bother defragging it again
696 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
700 * last_len ends up being a counter of how many bytes we've defragged.
701 * every time we choose not to defrag an extent, we reset *last_len
702 * so that the next tiny extent will force a defrag.
704 * The end result of this is that tiny extents before a single big
705 * extent will force at least part of that big extent to be defragged.
709 *defrag_end
= extent_map_end(em
);
712 *skip
= extent_map_end(em
);
720 static int btrfs_defrag_file(struct file
*file
,
721 struct btrfs_ioctl_defrag_range_args
*range
)
723 struct inode
*inode
= fdentry(file
)->d_inode
;
724 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
725 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
726 struct btrfs_ordered_extent
*ordered
;
728 struct btrfs_super_block
*disk_super
;
729 unsigned long last_index
;
730 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
731 unsigned long total_read
= 0;
740 int compress_type
= BTRFS_COMPRESS_ZLIB
;
742 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
743 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
745 if (range
->compress_type
)
746 compress_type
= range
->compress_type
;
749 if (inode
->i_size
== 0)
752 if (range
->start
+ range
->len
> range
->start
) {
753 last_index
= min_t(u64
, inode
->i_size
- 1,
754 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
756 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
759 i
= range
->start
>> PAGE_CACHE_SHIFT
;
760 while (i
<= last_index
) {
761 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
763 range
->extent_thresh
,
768 * the should_defrag function tells us how much to skip
769 * bump our counter by the suggested amount
771 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
772 i
= max(i
+ 1, next
);
776 if (total_read
% ra_pages
== 0) {
777 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
778 min(last_index
, i
+ ra_pages
- 1));
781 mutex_lock(&inode
->i_mutex
);
782 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
783 BTRFS_I(inode
)->force_compress
= compress_type
;
785 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
789 if (inode
->i_size
== 0 ||
790 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
792 goto err_reservations
;
795 page
= grab_cache_page(inode
->i_mapping
, i
);
798 goto err_reservations
;
801 if (!PageUptodate(page
)) {
802 btrfs_readpage(NULL
, page
);
804 if (!PageUptodate(page
)) {
806 page_cache_release(page
);
808 goto err_reservations
;
812 if (page
->mapping
!= inode
->i_mapping
) {
814 page_cache_release(page
);
818 wait_on_page_writeback(page
);
820 if (PageDirty(page
)) {
821 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
825 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
826 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
827 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
829 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
831 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
833 page_cache_release(page
);
834 btrfs_start_ordered_extent(inode
, ordered
, 1);
835 btrfs_put_ordered_extent(ordered
);
838 set_page_extent_mapped(page
);
841 * this makes sure page_mkwrite is called on the
842 * page if it is dirtied again later
844 clear_page_dirty_for_io(page
);
845 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
846 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
847 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
849 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
850 ClearPageChecked(page
);
851 set_page_dirty(page
);
852 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
856 page_cache_release(page
);
857 mutex_unlock(&inode
->i_mutex
);
859 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
863 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
864 filemap_flush(inode
->i_mapping
);
866 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
867 /* the filemap_flush will queue IO into the worker threads, but
868 * we have to make sure the IO is actually started and that
869 * ordered extents get created before we return
871 atomic_inc(&root
->fs_info
->async_submit_draining
);
872 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
873 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
874 wait_event(root
->fs_info
->async_submit_wait
,
875 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
876 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
878 atomic_dec(&root
->fs_info
->async_submit_draining
);
880 mutex_lock(&inode
->i_mutex
);
881 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
882 mutex_unlock(&inode
->i_mutex
);
885 disk_super
= &root
->fs_info
->super_copy
;
886 features
= btrfs_super_incompat_flags(disk_super
);
887 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
888 features
|= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO
;
889 btrfs_set_super_incompat_flags(disk_super
, features
);
895 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
897 mutex_unlock(&inode
->i_mutex
);
901 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
907 struct btrfs_ioctl_vol_args
*vol_args
;
908 struct btrfs_trans_handle
*trans
;
909 struct btrfs_device
*device
= NULL
;
915 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
918 if (!capable(CAP_SYS_ADMIN
))
921 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
922 if (IS_ERR(vol_args
))
923 return PTR_ERR(vol_args
);
925 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
927 mutex_lock(&root
->fs_info
->volume_mutex
);
928 sizestr
= vol_args
->name
;
929 devstr
= strchr(sizestr
, ':');
932 sizestr
= devstr
+ 1;
934 devstr
= vol_args
->name
;
935 devid
= simple_strtoull(devstr
, &end
, 10);
936 printk(KERN_INFO
"resizing devid %llu\n",
937 (unsigned long long)devid
);
939 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
941 printk(KERN_INFO
"resizer unable to find device %llu\n",
942 (unsigned long long)devid
);
946 if (!strcmp(sizestr
, "max"))
947 new_size
= device
->bdev
->bd_inode
->i_size
;
949 if (sizestr
[0] == '-') {
952 } else if (sizestr
[0] == '+') {
956 new_size
= memparse(sizestr
, NULL
);
963 old_size
= device
->total_bytes
;
966 if (new_size
> old_size
) {
970 new_size
= old_size
- new_size
;
971 } else if (mod
> 0) {
972 new_size
= old_size
+ new_size
;
975 if (new_size
< 256 * 1024 * 1024) {
979 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
984 do_div(new_size
, root
->sectorsize
);
985 new_size
*= root
->sectorsize
;
987 printk(KERN_INFO
"new size for %s is %llu\n",
988 device
->name
, (unsigned long long)new_size
);
990 if (new_size
> old_size
) {
991 trans
= btrfs_start_transaction(root
, 0);
993 ret
= PTR_ERR(trans
);
996 ret
= btrfs_grow_device(trans
, device
, new_size
);
997 btrfs_commit_transaction(trans
, root
);
999 ret
= btrfs_shrink_device(device
, new_size
);
1003 mutex_unlock(&root
->fs_info
->volume_mutex
);
1008 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1015 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1016 struct file
*src_file
;
1020 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1023 namelen
= strlen(name
);
1024 if (strchr(name
, '/')) {
1030 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1031 NULL
, transid
, readonly
);
1033 struct inode
*src_inode
;
1034 src_file
= fget(fd
);
1040 src_inode
= src_file
->f_path
.dentry
->d_inode
;
1041 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1042 printk(KERN_INFO
"btrfs: Snapshot src from "
1048 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1049 BTRFS_I(src_inode
)->root
,
1057 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1058 void __user
*arg
, int subvol
)
1060 struct btrfs_ioctl_vol_args
*vol_args
;
1063 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1064 if (IS_ERR(vol_args
))
1065 return PTR_ERR(vol_args
);
1066 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1068 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1069 vol_args
->fd
, subvol
,
1076 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1077 void __user
*arg
, int subvol
)
1079 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1083 bool readonly
= false;
1085 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1086 if (IS_ERR(vol_args
))
1087 return PTR_ERR(vol_args
);
1088 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1090 if (vol_args
->flags
&
1091 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
)) {
1096 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1098 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1101 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1102 vol_args
->fd
, subvol
,
1105 if (ret
== 0 && ptr
&&
1107 offsetof(struct btrfs_ioctl_vol_args_v2
,
1108 transid
), ptr
, sizeof(*ptr
)))
1115 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1118 struct inode
*inode
= fdentry(file
)->d_inode
;
1119 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1123 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
1126 down_read(&root
->fs_info
->subvol_sem
);
1127 if (btrfs_root_readonly(root
))
1128 flags
|= BTRFS_SUBVOL_RDONLY
;
1129 up_read(&root
->fs_info
->subvol_sem
);
1131 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1137 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1140 struct inode
*inode
= fdentry(file
)->d_inode
;
1141 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1142 struct btrfs_trans_handle
*trans
;
1147 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1150 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
1153 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1156 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1159 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1162 if (!is_owner_or_cap(inode
))
1165 down_write(&root
->fs_info
->subvol_sem
);
1168 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1171 root_flags
= btrfs_root_flags(&root
->root_item
);
1172 if (flags
& BTRFS_SUBVOL_RDONLY
)
1173 btrfs_set_root_flags(&root
->root_item
,
1174 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1176 btrfs_set_root_flags(&root
->root_item
,
1177 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1179 trans
= btrfs_start_transaction(root
, 1);
1180 if (IS_ERR(trans
)) {
1181 ret
= PTR_ERR(trans
);
1185 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1186 &root
->root_key
, &root
->root_item
);
1188 btrfs_commit_transaction(trans
, root
);
1191 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1193 up_write(&root
->fs_info
->subvol_sem
);
1198 * helper to check if the subvolume references other subvolumes
1200 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1202 struct btrfs_path
*path
;
1203 struct btrfs_key key
;
1206 path
= btrfs_alloc_path();
1210 key
.objectid
= root
->root_key
.objectid
;
1211 key
.type
= BTRFS_ROOT_REF_KEY
;
1212 key
.offset
= (u64
)-1;
1214 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1221 if (path
->slots
[0] > 0) {
1223 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1224 if (key
.objectid
== root
->root_key
.objectid
&&
1225 key
.type
== BTRFS_ROOT_REF_KEY
)
1229 btrfs_free_path(path
);
1233 static noinline
int key_in_sk(struct btrfs_key
*key
,
1234 struct btrfs_ioctl_search_key
*sk
)
1236 struct btrfs_key test
;
1239 test
.objectid
= sk
->min_objectid
;
1240 test
.type
= sk
->min_type
;
1241 test
.offset
= sk
->min_offset
;
1243 ret
= btrfs_comp_cpu_keys(key
, &test
);
1247 test
.objectid
= sk
->max_objectid
;
1248 test
.type
= sk
->max_type
;
1249 test
.offset
= sk
->max_offset
;
1251 ret
= btrfs_comp_cpu_keys(key
, &test
);
1257 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1258 struct btrfs_path
*path
,
1259 struct btrfs_key
*key
,
1260 struct btrfs_ioctl_search_key
*sk
,
1262 unsigned long *sk_offset
,
1266 struct extent_buffer
*leaf
;
1267 struct btrfs_ioctl_search_header sh
;
1268 unsigned long item_off
;
1269 unsigned long item_len
;
1276 leaf
= path
->nodes
[0];
1277 slot
= path
->slots
[0];
1278 nritems
= btrfs_header_nritems(leaf
);
1280 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1284 found_transid
= btrfs_header_generation(leaf
);
1286 for (i
= slot
; i
< nritems
; i
++) {
1287 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1288 item_len
= btrfs_item_size_nr(leaf
, i
);
1290 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1293 if (sizeof(sh
) + item_len
+ *sk_offset
>
1294 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1299 btrfs_item_key_to_cpu(leaf
, key
, i
);
1300 if (!key_in_sk(key
, sk
))
1303 sh
.objectid
= key
->objectid
;
1304 sh
.offset
= key
->offset
;
1305 sh
.type
= key
->type
;
1307 sh
.transid
= found_transid
;
1309 /* copy search result header */
1310 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1311 *sk_offset
+= sizeof(sh
);
1314 char *p
= buf
+ *sk_offset
;
1316 read_extent_buffer(leaf
, p
,
1317 item_off
, item_len
);
1318 *sk_offset
+= item_len
;
1322 if (*num_found
>= sk
->nr_items
)
1327 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1329 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1332 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1339 *num_found
+= found
;
1343 static noinline
int search_ioctl(struct inode
*inode
,
1344 struct btrfs_ioctl_search_args
*args
)
1346 struct btrfs_root
*root
;
1347 struct btrfs_key key
;
1348 struct btrfs_key max_key
;
1349 struct btrfs_path
*path
;
1350 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1351 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1354 unsigned long sk_offset
= 0;
1356 path
= btrfs_alloc_path();
1360 if (sk
->tree_id
== 0) {
1361 /* search the root of the inode that was passed */
1362 root
= BTRFS_I(inode
)->root
;
1364 key
.objectid
= sk
->tree_id
;
1365 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1366 key
.offset
= (u64
)-1;
1367 root
= btrfs_read_fs_root_no_name(info
, &key
);
1369 printk(KERN_ERR
"could not find root %llu\n",
1371 btrfs_free_path(path
);
1376 key
.objectid
= sk
->min_objectid
;
1377 key
.type
= sk
->min_type
;
1378 key
.offset
= sk
->min_offset
;
1380 max_key
.objectid
= sk
->max_objectid
;
1381 max_key
.type
= sk
->max_type
;
1382 max_key
.offset
= sk
->max_offset
;
1384 path
->keep_locks
= 1;
1387 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1394 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1395 &sk_offset
, &num_found
);
1396 btrfs_release_path(root
, path
);
1397 if (ret
|| num_found
>= sk
->nr_items
)
1403 sk
->nr_items
= num_found
;
1404 btrfs_free_path(path
);
1408 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1411 struct btrfs_ioctl_search_args
*args
;
1412 struct inode
*inode
;
1415 if (!capable(CAP_SYS_ADMIN
))
1418 args
= memdup_user(argp
, sizeof(*args
));
1420 return PTR_ERR(args
);
1422 inode
= fdentry(file
)->d_inode
;
1423 ret
= search_ioctl(inode
, args
);
1424 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1431 * Search INODE_REFs to identify path name of 'dirid' directory
1432 * in a 'tree_id' tree. and sets path name to 'name'.
1434 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1435 u64 tree_id
, u64 dirid
, char *name
)
1437 struct btrfs_root
*root
;
1438 struct btrfs_key key
;
1444 struct btrfs_inode_ref
*iref
;
1445 struct extent_buffer
*l
;
1446 struct btrfs_path
*path
;
1448 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1453 path
= btrfs_alloc_path();
1457 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1459 key
.objectid
= tree_id
;
1460 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1461 key
.offset
= (u64
)-1;
1462 root
= btrfs_read_fs_root_no_name(info
, &key
);
1464 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1469 key
.objectid
= dirid
;
1470 key
.type
= BTRFS_INODE_REF_KEY
;
1471 key
.offset
= (u64
)-1;
1474 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1479 slot
= path
->slots
[0];
1480 if (ret
> 0 && slot
> 0)
1482 btrfs_item_key_to_cpu(l
, &key
, slot
);
1484 if (ret
> 0 && (key
.objectid
!= dirid
||
1485 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1490 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1491 len
= btrfs_inode_ref_name_len(l
, iref
);
1493 total_len
+= len
+ 1;
1498 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1500 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1503 btrfs_release_path(root
, path
);
1504 key
.objectid
= key
.offset
;
1505 key
.offset
= (u64
)-1;
1506 dirid
= key
.objectid
;
1511 memcpy(name
, ptr
, total_len
);
1512 name
[total_len
]='\0';
1515 btrfs_free_path(path
);
1519 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1522 struct btrfs_ioctl_ino_lookup_args
*args
;
1523 struct inode
*inode
;
1526 if (!capable(CAP_SYS_ADMIN
))
1529 args
= memdup_user(argp
, sizeof(*args
));
1531 return PTR_ERR(args
);
1533 inode
= fdentry(file
)->d_inode
;
1535 if (args
->treeid
== 0)
1536 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1538 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1539 args
->treeid
, args
->objectid
,
1542 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1549 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1552 struct dentry
*parent
= fdentry(file
);
1553 struct dentry
*dentry
;
1554 struct inode
*dir
= parent
->d_inode
;
1555 struct inode
*inode
;
1556 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1557 struct btrfs_root
*dest
= NULL
;
1558 struct btrfs_ioctl_vol_args
*vol_args
;
1559 struct btrfs_trans_handle
*trans
;
1564 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1565 if (IS_ERR(vol_args
))
1566 return PTR_ERR(vol_args
);
1568 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1569 namelen
= strlen(vol_args
->name
);
1570 if (strchr(vol_args
->name
, '/') ||
1571 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1576 err
= mnt_want_write(file
->f_path
.mnt
);
1580 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1581 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1582 if (IS_ERR(dentry
)) {
1583 err
= PTR_ERR(dentry
);
1584 goto out_unlock_dir
;
1587 if (!dentry
->d_inode
) {
1592 inode
= dentry
->d_inode
;
1593 dest
= BTRFS_I(inode
)->root
;
1594 if (!capable(CAP_SYS_ADMIN
)){
1596 * Regular user. Only allow this with a special mount
1597 * option, when the user has write+exec access to the
1598 * subvol root, and when rmdir(2) would have been
1601 * Note that this is _not_ check that the subvol is
1602 * empty or doesn't contain data that we wouldn't
1603 * otherwise be able to delete.
1605 * Users who want to delete empty subvols should try
1609 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1613 * Do not allow deletion if the parent dir is the same
1614 * as the dir to be deleted. That means the ioctl
1615 * must be called on the dentry referencing the root
1616 * of the subvol, not a random directory contained
1623 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1627 /* check if subvolume may be deleted by a non-root user */
1628 err
= btrfs_may_delete(dir
, dentry
, 1);
1633 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1638 mutex_lock(&inode
->i_mutex
);
1639 err
= d_invalidate(dentry
);
1643 down_write(&root
->fs_info
->subvol_sem
);
1645 err
= may_destroy_subvol(dest
);
1649 trans
= btrfs_start_transaction(root
, 0);
1650 if (IS_ERR(trans
)) {
1651 err
= PTR_ERR(trans
);
1654 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1656 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1657 dest
->root_key
.objectid
,
1658 dentry
->d_name
.name
,
1659 dentry
->d_name
.len
);
1662 btrfs_record_root_in_trans(trans
, dest
);
1664 memset(&dest
->root_item
.drop_progress
, 0,
1665 sizeof(dest
->root_item
.drop_progress
));
1666 dest
->root_item
.drop_level
= 0;
1667 btrfs_set_root_refs(&dest
->root_item
, 0);
1669 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1670 ret
= btrfs_insert_orphan_item(trans
,
1671 root
->fs_info
->tree_root
,
1672 dest
->root_key
.objectid
);
1676 ret
= btrfs_end_transaction(trans
, root
);
1678 inode
->i_flags
|= S_DEAD
;
1680 up_write(&root
->fs_info
->subvol_sem
);
1682 mutex_unlock(&inode
->i_mutex
);
1684 shrink_dcache_sb(root
->fs_info
->sb
);
1685 btrfs_invalidate_inodes(dest
);
1691 mutex_unlock(&dir
->i_mutex
);
1692 mnt_drop_write(file
->f_path
.mnt
);
1698 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1700 struct inode
*inode
= fdentry(file
)->d_inode
;
1701 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1702 struct btrfs_ioctl_defrag_range_args
*range
;
1705 if (btrfs_root_readonly(root
))
1708 ret
= mnt_want_write(file
->f_path
.mnt
);
1712 switch (inode
->i_mode
& S_IFMT
) {
1714 if (!capable(CAP_SYS_ADMIN
)) {
1718 ret
= btrfs_defrag_root(root
, 0);
1721 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1724 if (!(file
->f_mode
& FMODE_WRITE
)) {
1729 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1736 if (copy_from_user(range
, argp
,
1742 /* compression requires us to start the IO */
1743 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1744 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1745 range
->extent_thresh
= (u32
)-1;
1748 /* the rest are all set to zero by kzalloc */
1749 range
->len
= (u64
)-1;
1751 ret
= btrfs_defrag_file(file
, range
);
1758 mnt_drop_write(file
->f_path
.mnt
);
1762 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1764 struct btrfs_ioctl_vol_args
*vol_args
;
1767 if (!capable(CAP_SYS_ADMIN
))
1770 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1771 if (IS_ERR(vol_args
))
1772 return PTR_ERR(vol_args
);
1774 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1775 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1781 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1783 struct btrfs_ioctl_vol_args
*vol_args
;
1786 if (!capable(CAP_SYS_ADMIN
))
1789 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1792 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1793 if (IS_ERR(vol_args
))
1794 return PTR_ERR(vol_args
);
1796 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1797 ret
= btrfs_rm_device(root
, vol_args
->name
);
1803 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1804 u64 off
, u64 olen
, u64 destoff
)
1806 struct inode
*inode
= fdentry(file
)->d_inode
;
1807 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1808 struct file
*src_file
;
1810 struct btrfs_trans_handle
*trans
;
1811 struct btrfs_path
*path
;
1812 struct extent_buffer
*leaf
;
1814 struct btrfs_key key
;
1819 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1824 * - split compressed inline extents. annoying: we need to
1825 * decompress into destination's address_space (the file offset
1826 * may change, so source mapping won't do), then recompress (or
1827 * otherwise reinsert) a subrange.
1828 * - allow ranges within the same file to be cloned (provided
1829 * they don't overlap)?
1832 /* the destination must be opened for writing */
1833 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
1836 if (btrfs_root_readonly(root
))
1839 ret
= mnt_want_write(file
->f_path
.mnt
);
1843 src_file
= fget(srcfd
);
1846 goto out_drop_write
;
1849 src
= src_file
->f_dentry
->d_inode
;
1855 /* the src must be open for reading */
1856 if (!(src_file
->f_mode
& FMODE_READ
))
1860 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1864 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1868 buf
= vmalloc(btrfs_level_size(root
, 0));
1872 path
= btrfs_alloc_path();
1880 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
1881 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
1883 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
1884 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1887 /* determine range to clone */
1889 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
1892 olen
= len
= src
->i_size
- off
;
1893 /* if we extend to eof, continue to block boundary */
1894 if (off
+ len
== src
->i_size
)
1895 len
= ALIGN(src
->i_size
, bs
) - off
;
1897 /* verify the end result is block aligned */
1898 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
1899 !IS_ALIGNED(destoff
, bs
))
1902 /* do any pending delalloc/csum calc on src, one way or
1903 another, and lock file content */
1905 struct btrfs_ordered_extent
*ordered
;
1906 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1907 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
1909 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
1910 EXTENT_DELALLOC
, 0, NULL
))
1912 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1914 btrfs_put_ordered_extent(ordered
);
1915 btrfs_wait_ordered_range(src
, off
, len
);
1919 key
.objectid
= src
->i_ino
;
1920 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1925 * note the key will change type as we walk through the
1928 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1932 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1933 if (path
->slots
[0] >= nritems
) {
1934 ret
= btrfs_next_leaf(root
, path
);
1939 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1941 leaf
= path
->nodes
[0];
1942 slot
= path
->slots
[0];
1944 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1945 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1946 key
.objectid
!= src
->i_ino
)
1949 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1950 struct btrfs_file_extent_item
*extent
;
1953 struct btrfs_key new_key
;
1954 u64 disko
= 0, diskl
= 0;
1955 u64 datao
= 0, datal
= 0;
1959 size
= btrfs_item_size_nr(leaf
, slot
);
1960 read_extent_buffer(leaf
, buf
,
1961 btrfs_item_ptr_offset(leaf
, slot
),
1964 extent
= btrfs_item_ptr(leaf
, slot
,
1965 struct btrfs_file_extent_item
);
1966 comp
= btrfs_file_extent_compression(leaf
, extent
);
1967 type
= btrfs_file_extent_type(leaf
, extent
);
1968 if (type
== BTRFS_FILE_EXTENT_REG
||
1969 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1970 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1972 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1974 datao
= btrfs_file_extent_offset(leaf
, extent
);
1975 datal
= btrfs_file_extent_num_bytes(leaf
,
1977 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1978 /* take upper bound, may be compressed */
1979 datal
= btrfs_file_extent_ram_bytes(leaf
,
1982 btrfs_release_path(root
, path
);
1984 if (key
.offset
+ datal
<= off
||
1985 key
.offset
>= off
+len
)
1988 memcpy(&new_key
, &key
, sizeof(new_key
));
1989 new_key
.objectid
= inode
->i_ino
;
1990 if (off
<= key
.offset
)
1991 new_key
.offset
= key
.offset
+ destoff
- off
;
1993 new_key
.offset
= destoff
;
1995 trans
= btrfs_start_transaction(root
, 1);
1996 if (IS_ERR(trans
)) {
1997 ret
= PTR_ERR(trans
);
2001 if (type
== BTRFS_FILE_EXTENT_REG
||
2002 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2003 if (off
> key
.offset
) {
2004 datao
+= off
- key
.offset
;
2005 datal
-= off
- key
.offset
;
2008 if (key
.offset
+ datal
> off
+ len
)
2009 datal
= off
+ len
- key
.offset
;
2011 ret
= btrfs_drop_extents(trans
, inode
,
2013 new_key
.offset
+ datal
,
2017 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2021 leaf
= path
->nodes
[0];
2022 slot
= path
->slots
[0];
2023 write_extent_buffer(leaf
, buf
,
2024 btrfs_item_ptr_offset(leaf
, slot
),
2027 extent
= btrfs_item_ptr(leaf
, slot
,
2028 struct btrfs_file_extent_item
);
2030 /* disko == 0 means it's a hole */
2034 btrfs_set_file_extent_offset(leaf
, extent
,
2036 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2039 inode_add_bytes(inode
, datal
);
2040 ret
= btrfs_inc_extent_ref(trans
, root
,
2042 root
->root_key
.objectid
,
2044 new_key
.offset
- datao
);
2047 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2050 if (off
> key
.offset
) {
2051 skip
= off
- key
.offset
;
2052 new_key
.offset
+= skip
;
2055 if (key
.offset
+ datal
> off
+len
)
2056 trim
= key
.offset
+ datal
- (off
+len
);
2058 if (comp
&& (skip
|| trim
)) {
2060 btrfs_end_transaction(trans
, root
);
2063 size
-= skip
+ trim
;
2064 datal
-= skip
+ trim
;
2066 ret
= btrfs_drop_extents(trans
, inode
,
2068 new_key
.offset
+ datal
,
2072 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2078 btrfs_file_extent_calc_inline_size(0);
2079 memmove(buf
+start
, buf
+start
+skip
,
2083 leaf
= path
->nodes
[0];
2084 slot
= path
->slots
[0];
2085 write_extent_buffer(leaf
, buf
,
2086 btrfs_item_ptr_offset(leaf
, slot
),
2088 inode_add_bytes(inode
, datal
);
2091 btrfs_mark_buffer_dirty(leaf
);
2092 btrfs_release_path(root
, path
);
2094 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2097 * we round up to the block size at eof when
2098 * determining which extents to clone above,
2099 * but shouldn't round up the file size
2101 endoff
= new_key
.offset
+ datal
;
2102 if (endoff
> destoff
+olen
)
2103 endoff
= destoff
+olen
;
2104 if (endoff
> inode
->i_size
)
2105 btrfs_i_size_write(inode
, endoff
);
2107 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
2108 ret
= btrfs_update_inode(trans
, root
, inode
);
2110 btrfs_end_transaction(trans
, root
);
2113 btrfs_release_path(root
, path
);
2118 btrfs_release_path(root
, path
);
2119 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2121 mutex_unlock(&src
->i_mutex
);
2122 mutex_unlock(&inode
->i_mutex
);
2124 btrfs_free_path(path
);
2128 mnt_drop_write(file
->f_path
.mnt
);
2132 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2134 struct btrfs_ioctl_clone_range_args args
;
2136 if (copy_from_user(&args
, argp
, sizeof(args
)))
2138 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2139 args
.src_length
, args
.dest_offset
);
2143 * there are many ways the trans_start and trans_end ioctls can lead
2144 * to deadlocks. They should only be used by applications that
2145 * basically own the machine, and have a very in depth understanding
2146 * of all the possible deadlocks and enospc problems.
2148 static long btrfs_ioctl_trans_start(struct file
*file
)
2150 struct inode
*inode
= fdentry(file
)->d_inode
;
2151 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2152 struct btrfs_trans_handle
*trans
;
2156 if (!capable(CAP_SYS_ADMIN
))
2160 if (file
->private_data
)
2164 if (btrfs_root_readonly(root
))
2167 ret
= mnt_want_write(file
->f_path
.mnt
);
2171 mutex_lock(&root
->fs_info
->trans_mutex
);
2172 root
->fs_info
->open_ioctl_trans
++;
2173 mutex_unlock(&root
->fs_info
->trans_mutex
);
2176 trans
= btrfs_start_ioctl_transaction(root
, 0);
2180 file
->private_data
= trans
;
2184 mutex_lock(&root
->fs_info
->trans_mutex
);
2185 root
->fs_info
->open_ioctl_trans
--;
2186 mutex_unlock(&root
->fs_info
->trans_mutex
);
2187 mnt_drop_write(file
->f_path
.mnt
);
2192 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2194 struct inode
*inode
= fdentry(file
)->d_inode
;
2195 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2196 struct btrfs_root
*new_root
;
2197 struct btrfs_dir_item
*di
;
2198 struct btrfs_trans_handle
*trans
;
2199 struct btrfs_path
*path
;
2200 struct btrfs_key location
;
2201 struct btrfs_disk_key disk_key
;
2202 struct btrfs_super_block
*disk_super
;
2207 if (!capable(CAP_SYS_ADMIN
))
2210 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2214 objectid
= root
->root_key
.objectid
;
2216 location
.objectid
= objectid
;
2217 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2218 location
.offset
= (u64
)-1;
2220 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2221 if (IS_ERR(new_root
))
2222 return PTR_ERR(new_root
);
2224 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2227 path
= btrfs_alloc_path();
2230 path
->leave_spinning
= 1;
2232 trans
= btrfs_start_transaction(root
, 1);
2233 if (IS_ERR(trans
)) {
2234 btrfs_free_path(path
);
2235 return PTR_ERR(trans
);
2238 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2239 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2240 dir_id
, "default", 7, 1);
2241 if (IS_ERR_OR_NULL(di
)) {
2242 btrfs_free_path(path
);
2243 btrfs_end_transaction(trans
, root
);
2244 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2245 "this isn't going to work\n");
2249 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2250 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2251 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2252 btrfs_free_path(path
);
2254 disk_super
= &root
->fs_info
->super_copy
;
2255 features
= btrfs_super_incompat_flags(disk_super
);
2256 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2257 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2258 btrfs_set_super_incompat_flags(disk_super
, features
);
2260 btrfs_end_transaction(trans
, root
);
2265 static void get_block_group_info(struct list_head
*groups_list
,
2266 struct btrfs_ioctl_space_info
*space
)
2268 struct btrfs_block_group_cache
*block_group
;
2270 space
->total_bytes
= 0;
2271 space
->used_bytes
= 0;
2273 list_for_each_entry(block_group
, groups_list
, list
) {
2274 space
->flags
= block_group
->flags
;
2275 space
->total_bytes
+= block_group
->key
.offset
;
2276 space
->used_bytes
+=
2277 btrfs_block_group_used(&block_group
->item
);
2281 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2283 struct btrfs_ioctl_space_args space_args
;
2284 struct btrfs_ioctl_space_info space
;
2285 struct btrfs_ioctl_space_info
*dest
;
2286 struct btrfs_ioctl_space_info
*dest_orig
;
2287 struct btrfs_ioctl_space_info __user
*user_dest
;
2288 struct btrfs_space_info
*info
;
2289 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2290 BTRFS_BLOCK_GROUP_SYSTEM
,
2291 BTRFS_BLOCK_GROUP_METADATA
,
2292 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2299 if (copy_from_user(&space_args
,
2300 (struct btrfs_ioctl_space_args __user
*)arg
,
2301 sizeof(space_args
)))
2304 for (i
= 0; i
< num_types
; i
++) {
2305 struct btrfs_space_info
*tmp
;
2309 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2311 if (tmp
->flags
== types
[i
]) {
2321 down_read(&info
->groups_sem
);
2322 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2323 if (!list_empty(&info
->block_groups
[c
]))
2326 up_read(&info
->groups_sem
);
2329 /* space_slots == 0 means they are asking for a count */
2330 if (space_args
.space_slots
== 0) {
2331 space_args
.total_spaces
= slot_count
;
2335 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2337 alloc_size
= sizeof(*dest
) * slot_count
;
2339 /* we generally have at most 6 or so space infos, one for each raid
2340 * level. So, a whole page should be more than enough for everyone
2342 if (alloc_size
> PAGE_CACHE_SIZE
)
2345 space_args
.total_spaces
= 0;
2346 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2351 /* now we have a buffer to copy into */
2352 for (i
= 0; i
< num_types
; i
++) {
2353 struct btrfs_space_info
*tmp
;
2360 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2362 if (tmp
->flags
== types
[i
]) {
2371 down_read(&info
->groups_sem
);
2372 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2373 if (!list_empty(&info
->block_groups
[c
])) {
2374 get_block_group_info(&info
->block_groups
[c
],
2376 memcpy(dest
, &space
, sizeof(space
));
2378 space_args
.total_spaces
++;
2384 up_read(&info
->groups_sem
);
2387 user_dest
= (struct btrfs_ioctl_space_info
*)
2388 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2390 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2395 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2402 * there are many ways the trans_start and trans_end ioctls can lead
2403 * to deadlocks. They should only be used by applications that
2404 * basically own the machine, and have a very in depth understanding
2405 * of all the possible deadlocks and enospc problems.
2407 long btrfs_ioctl_trans_end(struct file
*file
)
2409 struct inode
*inode
= fdentry(file
)->d_inode
;
2410 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2411 struct btrfs_trans_handle
*trans
;
2413 trans
= file
->private_data
;
2416 file
->private_data
= NULL
;
2418 btrfs_end_transaction(trans
, root
);
2420 mutex_lock(&root
->fs_info
->trans_mutex
);
2421 root
->fs_info
->open_ioctl_trans
--;
2422 mutex_unlock(&root
->fs_info
->trans_mutex
);
2424 mnt_drop_write(file
->f_path
.mnt
);
2428 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2430 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2431 struct btrfs_trans_handle
*trans
;
2435 trans
= btrfs_start_transaction(root
, 0);
2437 return PTR_ERR(trans
);
2438 transid
= trans
->transid
;
2439 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
2441 btrfs_end_transaction(trans
, root
);
2446 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2451 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2453 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2457 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2460 transid
= 0; /* current trans */
2462 return btrfs_wait_for_commit(root
, transid
);
2465 long btrfs_ioctl(struct file
*file
, unsigned int
2466 cmd
, unsigned long arg
)
2468 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2469 void __user
*argp
= (void __user
*)arg
;
2472 case FS_IOC_GETFLAGS
:
2473 return btrfs_ioctl_getflags(file
, argp
);
2474 case FS_IOC_SETFLAGS
:
2475 return btrfs_ioctl_setflags(file
, argp
);
2476 case FS_IOC_GETVERSION
:
2477 return btrfs_ioctl_getversion(file
, argp
);
2479 return btrfs_ioctl_fitrim(file
, argp
);
2480 case BTRFS_IOC_SNAP_CREATE
:
2481 return btrfs_ioctl_snap_create(file
, argp
, 0);
2482 case BTRFS_IOC_SNAP_CREATE_V2
:
2483 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
2484 case BTRFS_IOC_SUBVOL_CREATE
:
2485 return btrfs_ioctl_snap_create(file
, argp
, 1);
2486 case BTRFS_IOC_SNAP_DESTROY
:
2487 return btrfs_ioctl_snap_destroy(file
, argp
);
2488 case BTRFS_IOC_SUBVOL_GETFLAGS
:
2489 return btrfs_ioctl_subvol_getflags(file
, argp
);
2490 case BTRFS_IOC_SUBVOL_SETFLAGS
:
2491 return btrfs_ioctl_subvol_setflags(file
, argp
);
2492 case BTRFS_IOC_DEFAULT_SUBVOL
:
2493 return btrfs_ioctl_default_subvol(file
, argp
);
2494 case BTRFS_IOC_DEFRAG
:
2495 return btrfs_ioctl_defrag(file
, NULL
);
2496 case BTRFS_IOC_DEFRAG_RANGE
:
2497 return btrfs_ioctl_defrag(file
, argp
);
2498 case BTRFS_IOC_RESIZE
:
2499 return btrfs_ioctl_resize(root
, argp
);
2500 case BTRFS_IOC_ADD_DEV
:
2501 return btrfs_ioctl_add_dev(root
, argp
);
2502 case BTRFS_IOC_RM_DEV
:
2503 return btrfs_ioctl_rm_dev(root
, argp
);
2504 case BTRFS_IOC_BALANCE
:
2505 return btrfs_balance(root
->fs_info
->dev_root
);
2506 case BTRFS_IOC_CLONE
:
2507 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2508 case BTRFS_IOC_CLONE_RANGE
:
2509 return btrfs_ioctl_clone_range(file
, argp
);
2510 case BTRFS_IOC_TRANS_START
:
2511 return btrfs_ioctl_trans_start(file
);
2512 case BTRFS_IOC_TRANS_END
:
2513 return btrfs_ioctl_trans_end(file
);
2514 case BTRFS_IOC_TREE_SEARCH
:
2515 return btrfs_ioctl_tree_search(file
, argp
);
2516 case BTRFS_IOC_INO_LOOKUP
:
2517 return btrfs_ioctl_ino_lookup(file
, argp
);
2518 case BTRFS_IOC_SPACE_INFO
:
2519 return btrfs_ioctl_space_info(root
, argp
);
2520 case BTRFS_IOC_SYNC
:
2521 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
2523 case BTRFS_IOC_START_SYNC
:
2524 return btrfs_ioctl_start_sync(file
, argp
);
2525 case BTRFS_IOC_WAIT_SYNC
:
2526 return btrfs_ioctl_wait_sync(file
, argp
);