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
| \
148 FS_NOCOW_FL
| FS_COW_FL
))
151 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
154 if ((flags
& FS_NOCOW_FL
) && (flags
& FS_COW_FL
))
160 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
162 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
163 struct btrfs_inode
*ip
= BTRFS_I(inode
);
164 struct btrfs_root
*root
= ip
->root
;
165 struct btrfs_trans_handle
*trans
;
166 unsigned int flags
, oldflags
;
169 if (btrfs_root_readonly(root
))
172 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
175 ret
= check_flags(flags
);
179 if (!is_owner_or_cap(inode
))
182 mutex_lock(&inode
->i_mutex
);
184 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
185 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
186 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
187 if (!capable(CAP_LINUX_IMMUTABLE
)) {
193 ret
= mnt_want_write(file
->f_path
.mnt
);
197 if (flags
& FS_SYNC_FL
)
198 ip
->flags
|= BTRFS_INODE_SYNC
;
200 ip
->flags
&= ~BTRFS_INODE_SYNC
;
201 if (flags
& FS_IMMUTABLE_FL
)
202 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
204 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
205 if (flags
& FS_APPEND_FL
)
206 ip
->flags
|= BTRFS_INODE_APPEND
;
208 ip
->flags
&= ~BTRFS_INODE_APPEND
;
209 if (flags
& FS_NODUMP_FL
)
210 ip
->flags
|= BTRFS_INODE_NODUMP
;
212 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
213 if (flags
& FS_NOATIME_FL
)
214 ip
->flags
|= BTRFS_INODE_NOATIME
;
216 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
217 if (flags
& FS_DIRSYNC_FL
)
218 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
220 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
223 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
224 * flag may be changed automatically if compression code won't make
227 if (flags
& FS_NOCOMP_FL
) {
228 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
229 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
230 } else if (flags
& FS_COMPR_FL
) {
231 ip
->flags
|= BTRFS_INODE_COMPRESS
;
232 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
234 if (flags
& FS_NOCOW_FL
)
235 ip
->flags
|= BTRFS_INODE_NODATACOW
;
236 else if (flags
& FS_COW_FL
)
237 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
239 trans
= btrfs_join_transaction(root
, 1);
240 BUG_ON(IS_ERR(trans
));
242 ret
= btrfs_update_inode(trans
, root
, inode
);
245 btrfs_update_iflags(inode
);
246 inode
->i_ctime
= CURRENT_TIME
;
247 btrfs_end_transaction(trans
, root
);
249 mnt_drop_write(file
->f_path
.mnt
);
251 mutex_unlock(&inode
->i_mutex
);
255 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
257 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
259 return put_user(inode
->i_generation
, arg
);
262 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
264 struct btrfs_root
*root
= fdentry(file
)->d_sb
->s_fs_info
;
265 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
266 struct btrfs_device
*device
;
267 struct request_queue
*q
;
268 struct fstrim_range range
;
269 u64 minlen
= ULLONG_MAX
;
273 if (!capable(CAP_SYS_ADMIN
))
276 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
277 list_for_each_entry(device
, &fs_info
->fs_devices
->devices
, dev_list
) {
280 q
= bdev_get_queue(device
->bdev
);
281 if (blk_queue_discard(q
)) {
283 minlen
= min((u64
)q
->limits
.discard_granularity
,
287 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
291 if (copy_from_user(&range
, arg
, sizeof(range
)))
294 range
.minlen
= max(range
.minlen
, minlen
);
295 ret
= btrfs_trim_fs(root
, &range
);
299 if (copy_to_user(arg
, &range
, sizeof(range
)))
305 static noinline
int create_subvol(struct btrfs_root
*root
,
306 struct dentry
*dentry
,
307 char *name
, int namelen
,
310 struct btrfs_trans_handle
*trans
;
311 struct btrfs_key key
;
312 struct btrfs_root_item root_item
;
313 struct btrfs_inode_item
*inode_item
;
314 struct extent_buffer
*leaf
;
315 struct btrfs_root
*new_root
;
316 struct dentry
*parent
= dget_parent(dentry
);
321 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
324 ret
= btrfs_find_free_objectid(NULL
, root
->fs_info
->tree_root
,
331 dir
= parent
->d_inode
;
339 trans
= btrfs_start_transaction(root
, 6);
342 return PTR_ERR(trans
);
345 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
346 0, objectid
, NULL
, 0, 0, 0);
352 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
353 btrfs_set_header_bytenr(leaf
, leaf
->start
);
354 btrfs_set_header_generation(leaf
, trans
->transid
);
355 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
356 btrfs_set_header_owner(leaf
, objectid
);
358 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
359 (unsigned long)btrfs_header_fsid(leaf
),
361 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
362 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
364 btrfs_mark_buffer_dirty(leaf
);
366 inode_item
= &root_item
.inode
;
367 memset(inode_item
, 0, sizeof(*inode_item
));
368 inode_item
->generation
= cpu_to_le64(1);
369 inode_item
->size
= cpu_to_le64(3);
370 inode_item
->nlink
= cpu_to_le32(1);
371 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
372 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
374 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
375 btrfs_set_root_generation(&root_item
, trans
->transid
);
376 btrfs_set_root_level(&root_item
, 0);
377 btrfs_set_root_refs(&root_item
, 1);
378 btrfs_set_root_used(&root_item
, leaf
->len
);
379 btrfs_set_root_last_snapshot(&root_item
, 0);
381 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
382 root_item
.drop_level
= 0;
384 btrfs_tree_unlock(leaf
);
385 free_extent_buffer(leaf
);
388 btrfs_set_root_dirid(&root_item
, new_dirid
);
390 key
.objectid
= objectid
;
392 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
393 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
398 key
.offset
= (u64
)-1;
399 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
400 BUG_ON(IS_ERR(new_root
));
402 btrfs_record_root_in_trans(trans
, new_root
);
404 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
405 BTRFS_I(dir
)->block_group
);
407 * insert the directory item
409 ret
= btrfs_set_inode_index(dir
, &index
);
412 ret
= btrfs_insert_dir_item(trans
, root
,
413 name
, namelen
, dir
->i_ino
, &key
,
414 BTRFS_FT_DIR
, index
);
418 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
419 ret
= btrfs_update_inode(trans
, root
, dir
);
422 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
423 objectid
, root
->root_key
.objectid
,
424 dir
->i_ino
, index
, name
, namelen
);
428 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
432 *async_transid
= trans
->transid
;
433 err
= btrfs_commit_transaction_async(trans
, root
, 1);
435 err
= btrfs_commit_transaction(trans
, root
);
442 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
443 char *name
, int namelen
, u64
*async_transid
,
447 struct dentry
*parent
;
448 struct btrfs_pending_snapshot
*pending_snapshot
;
449 struct btrfs_trans_handle
*trans
;
455 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
456 if (!pending_snapshot
)
459 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
460 pending_snapshot
->dentry
= dentry
;
461 pending_snapshot
->root
= root
;
462 pending_snapshot
->readonly
= readonly
;
464 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
466 ret
= PTR_ERR(trans
);
470 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
473 list_add(&pending_snapshot
->list
,
474 &trans
->transaction
->pending_snapshots
);
476 *async_transid
= trans
->transid
;
477 ret
= btrfs_commit_transaction_async(trans
,
478 root
->fs_info
->extent_root
, 1);
480 ret
= btrfs_commit_transaction(trans
,
481 root
->fs_info
->extent_root
);
485 ret
= pending_snapshot
->error
;
489 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
493 parent
= dget_parent(dentry
);
494 inode
= btrfs_lookup_dentry(parent
->d_inode
, dentry
);
497 ret
= PTR_ERR(inode
);
501 d_instantiate(dentry
, inode
);
504 kfree(pending_snapshot
);
508 /* copy of check_sticky in fs/namei.c()
509 * It's inline, so penalty for filesystems that don't use sticky bit is
512 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
514 uid_t fsuid
= current_fsuid();
516 if (!(dir
->i_mode
& S_ISVTX
))
518 if (inode
->i_uid
== fsuid
)
520 if (dir
->i_uid
== fsuid
)
522 return !capable(CAP_FOWNER
);
525 /* copy of may_delete in fs/namei.c()
526 * Check whether we can remove a link victim from directory dir, check
527 * whether the type of victim is right.
528 * 1. We can't do it if dir is read-only (done in permission())
529 * 2. We should have write and exec permissions on dir
530 * 3. We can't remove anything from append-only dir
531 * 4. We can't do anything with immutable dir (done in permission())
532 * 5. If the sticky bit on dir is set we should either
533 * a. be owner of dir, or
534 * b. be owner of victim, or
535 * c. have CAP_FOWNER capability
536 * 6. If the victim is append-only or immutable we can't do antyhing with
537 * links pointing to it.
538 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
539 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
540 * 9. We can't remove a root or mountpoint.
541 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
542 * nfs_async_unlink().
545 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
549 if (!victim
->d_inode
)
552 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
553 audit_inode_child(victim
, dir
);
555 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
560 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
561 IS_APPEND(victim
->d_inode
)||
562 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
565 if (!S_ISDIR(victim
->d_inode
->i_mode
))
569 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
573 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
578 /* copy of may_create in fs/namei.c() */
579 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
585 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
589 * Create a new subvolume below @parent. This is largely modeled after
590 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
591 * inside this filesystem so it's quite a bit simpler.
593 static noinline
int btrfs_mksubvol(struct path
*parent
,
594 char *name
, int namelen
,
595 struct btrfs_root
*snap_src
,
596 u64
*async_transid
, bool readonly
)
598 struct inode
*dir
= parent
->dentry
->d_inode
;
599 struct dentry
*dentry
;
602 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
604 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
605 error
= PTR_ERR(dentry
);
613 error
= mnt_want_write(parent
->mnt
);
617 error
= btrfs_may_create(dir
, dentry
);
621 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
623 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
627 error
= create_snapshot(snap_src
, dentry
,
628 name
, namelen
, async_transid
, readonly
);
630 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
631 name
, namelen
, async_transid
);
634 fsnotify_mkdir(dir
, dentry
);
636 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
638 mnt_drop_write(parent
->mnt
);
642 mutex_unlock(&dir
->i_mutex
);
646 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
647 int thresh
, u64
*last_len
, u64
*skip
,
650 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
651 struct extent_map
*em
= NULL
;
652 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
660 * make sure that once we start defragging and extent, we keep on
663 if (start
< *defrag_end
)
669 * hopefully we have this extent in the tree already, try without
670 * the full extent lock
672 read_lock(&em_tree
->lock
);
673 em
= lookup_extent_mapping(em_tree
, start
, len
);
674 read_unlock(&em_tree
->lock
);
677 /* get the big lock and read metadata off disk */
678 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
679 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
680 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
686 /* this will cover holes, and inline extents */
687 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
691 * we hit a real extent, if it is big don't bother defragging it again
693 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
697 * last_len ends up being a counter of how many bytes we've defragged.
698 * every time we choose not to defrag an extent, we reset *last_len
699 * so that the next tiny extent will force a defrag.
701 * The end result of this is that tiny extents before a single big
702 * extent will force at least part of that big extent to be defragged.
706 *defrag_end
= extent_map_end(em
);
709 *skip
= extent_map_end(em
);
717 static int btrfs_defrag_file(struct file
*file
,
718 struct btrfs_ioctl_defrag_range_args
*range
)
720 struct inode
*inode
= fdentry(file
)->d_inode
;
721 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
722 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
723 struct btrfs_ordered_extent
*ordered
;
725 struct btrfs_super_block
*disk_super
;
726 unsigned long last_index
;
727 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
728 unsigned long total_read
= 0;
737 int compress_type
= BTRFS_COMPRESS_ZLIB
;
739 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
740 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
742 if (range
->compress_type
)
743 compress_type
= range
->compress_type
;
746 if (inode
->i_size
== 0)
749 if (range
->start
+ range
->len
> range
->start
) {
750 last_index
= min_t(u64
, inode
->i_size
- 1,
751 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
753 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
756 i
= range
->start
>> PAGE_CACHE_SHIFT
;
757 while (i
<= last_index
) {
758 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
760 range
->extent_thresh
,
765 * the should_defrag function tells us how much to skip
766 * bump our counter by the suggested amount
768 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
769 i
= max(i
+ 1, next
);
773 if (total_read
% ra_pages
== 0) {
774 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
775 min(last_index
, i
+ ra_pages
- 1));
778 mutex_lock(&inode
->i_mutex
);
779 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
780 BTRFS_I(inode
)->force_compress
= compress_type
;
782 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
786 if (inode
->i_size
== 0 ||
787 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
789 goto err_reservations
;
792 page
= grab_cache_page(inode
->i_mapping
, i
);
795 goto err_reservations
;
798 if (!PageUptodate(page
)) {
799 btrfs_readpage(NULL
, page
);
801 if (!PageUptodate(page
)) {
803 page_cache_release(page
);
805 goto err_reservations
;
809 if (page
->mapping
!= inode
->i_mapping
) {
811 page_cache_release(page
);
815 wait_on_page_writeback(page
);
817 if (PageDirty(page
)) {
818 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
822 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
823 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
824 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
826 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
828 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
830 page_cache_release(page
);
831 btrfs_start_ordered_extent(inode
, ordered
, 1);
832 btrfs_put_ordered_extent(ordered
);
835 set_page_extent_mapped(page
);
838 * this makes sure page_mkwrite is called on the
839 * page if it is dirtied again later
841 clear_page_dirty_for_io(page
);
842 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
843 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
844 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
846 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
847 ClearPageChecked(page
);
848 set_page_dirty(page
);
849 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
853 page_cache_release(page
);
854 mutex_unlock(&inode
->i_mutex
);
856 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
860 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
861 filemap_flush(inode
->i_mapping
);
863 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
864 /* the filemap_flush will queue IO into the worker threads, but
865 * we have to make sure the IO is actually started and that
866 * ordered extents get created before we return
868 atomic_inc(&root
->fs_info
->async_submit_draining
);
869 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
870 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
871 wait_event(root
->fs_info
->async_submit_wait
,
872 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
873 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
875 atomic_dec(&root
->fs_info
->async_submit_draining
);
877 mutex_lock(&inode
->i_mutex
);
878 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
879 mutex_unlock(&inode
->i_mutex
);
882 disk_super
= &root
->fs_info
->super_copy
;
883 features
= btrfs_super_incompat_flags(disk_super
);
884 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
885 features
|= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO
;
886 btrfs_set_super_incompat_flags(disk_super
, features
);
892 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
894 mutex_unlock(&inode
->i_mutex
);
898 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
904 struct btrfs_ioctl_vol_args
*vol_args
;
905 struct btrfs_trans_handle
*trans
;
906 struct btrfs_device
*device
= NULL
;
912 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
915 if (!capable(CAP_SYS_ADMIN
))
918 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
919 if (IS_ERR(vol_args
))
920 return PTR_ERR(vol_args
);
922 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
924 mutex_lock(&root
->fs_info
->volume_mutex
);
925 sizestr
= vol_args
->name
;
926 devstr
= strchr(sizestr
, ':');
929 sizestr
= devstr
+ 1;
931 devstr
= vol_args
->name
;
932 devid
= simple_strtoull(devstr
, &end
, 10);
933 printk(KERN_INFO
"resizing devid %llu\n",
934 (unsigned long long)devid
);
936 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
938 printk(KERN_INFO
"resizer unable to find device %llu\n",
939 (unsigned long long)devid
);
943 if (!strcmp(sizestr
, "max"))
944 new_size
= device
->bdev
->bd_inode
->i_size
;
946 if (sizestr
[0] == '-') {
949 } else if (sizestr
[0] == '+') {
953 new_size
= memparse(sizestr
, NULL
);
960 old_size
= device
->total_bytes
;
963 if (new_size
> old_size
) {
967 new_size
= old_size
- new_size
;
968 } else if (mod
> 0) {
969 new_size
= old_size
+ new_size
;
972 if (new_size
< 256 * 1024 * 1024) {
976 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
981 do_div(new_size
, root
->sectorsize
);
982 new_size
*= root
->sectorsize
;
984 printk(KERN_INFO
"new size for %s is %llu\n",
985 device
->name
, (unsigned long long)new_size
);
987 if (new_size
> old_size
) {
988 trans
= btrfs_start_transaction(root
, 0);
990 ret
= PTR_ERR(trans
);
993 ret
= btrfs_grow_device(trans
, device
, new_size
);
994 btrfs_commit_transaction(trans
, root
);
996 ret
= btrfs_shrink_device(device
, new_size
);
1000 mutex_unlock(&root
->fs_info
->volume_mutex
);
1005 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1012 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1013 struct file
*src_file
;
1017 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1020 namelen
= strlen(name
);
1021 if (strchr(name
, '/')) {
1027 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1028 NULL
, transid
, readonly
);
1030 struct inode
*src_inode
;
1031 src_file
= fget(fd
);
1037 src_inode
= src_file
->f_path
.dentry
->d_inode
;
1038 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1039 printk(KERN_INFO
"btrfs: Snapshot src from "
1045 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1046 BTRFS_I(src_inode
)->root
,
1054 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1055 void __user
*arg
, int subvol
)
1057 struct btrfs_ioctl_vol_args
*vol_args
;
1060 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1061 if (IS_ERR(vol_args
))
1062 return PTR_ERR(vol_args
);
1063 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1065 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1066 vol_args
->fd
, subvol
,
1073 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1074 void __user
*arg
, int subvol
)
1076 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1080 bool readonly
= false;
1082 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1083 if (IS_ERR(vol_args
))
1084 return PTR_ERR(vol_args
);
1085 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1087 if (vol_args
->flags
&
1088 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
)) {
1093 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1095 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1098 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1099 vol_args
->fd
, subvol
,
1102 if (ret
== 0 && ptr
&&
1104 offsetof(struct btrfs_ioctl_vol_args_v2
,
1105 transid
), ptr
, sizeof(*ptr
)))
1112 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1115 struct inode
*inode
= fdentry(file
)->d_inode
;
1116 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1120 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
1123 down_read(&root
->fs_info
->subvol_sem
);
1124 if (btrfs_root_readonly(root
))
1125 flags
|= BTRFS_SUBVOL_RDONLY
;
1126 up_read(&root
->fs_info
->subvol_sem
);
1128 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1134 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1137 struct inode
*inode
= fdentry(file
)->d_inode
;
1138 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1139 struct btrfs_trans_handle
*trans
;
1144 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1147 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
1150 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1153 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1156 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1159 if (!is_owner_or_cap(inode
))
1162 down_write(&root
->fs_info
->subvol_sem
);
1165 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1168 root_flags
= btrfs_root_flags(&root
->root_item
);
1169 if (flags
& BTRFS_SUBVOL_RDONLY
)
1170 btrfs_set_root_flags(&root
->root_item
,
1171 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1173 btrfs_set_root_flags(&root
->root_item
,
1174 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1176 trans
= btrfs_start_transaction(root
, 1);
1177 if (IS_ERR(trans
)) {
1178 ret
= PTR_ERR(trans
);
1182 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1183 &root
->root_key
, &root
->root_item
);
1185 btrfs_commit_transaction(trans
, root
);
1188 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1190 up_write(&root
->fs_info
->subvol_sem
);
1195 * helper to check if the subvolume references other subvolumes
1197 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1199 struct btrfs_path
*path
;
1200 struct btrfs_key key
;
1203 path
= btrfs_alloc_path();
1207 key
.objectid
= root
->root_key
.objectid
;
1208 key
.type
= BTRFS_ROOT_REF_KEY
;
1209 key
.offset
= (u64
)-1;
1211 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1218 if (path
->slots
[0] > 0) {
1220 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1221 if (key
.objectid
== root
->root_key
.objectid
&&
1222 key
.type
== BTRFS_ROOT_REF_KEY
)
1226 btrfs_free_path(path
);
1230 static noinline
int key_in_sk(struct btrfs_key
*key
,
1231 struct btrfs_ioctl_search_key
*sk
)
1233 struct btrfs_key test
;
1236 test
.objectid
= sk
->min_objectid
;
1237 test
.type
= sk
->min_type
;
1238 test
.offset
= sk
->min_offset
;
1240 ret
= btrfs_comp_cpu_keys(key
, &test
);
1244 test
.objectid
= sk
->max_objectid
;
1245 test
.type
= sk
->max_type
;
1246 test
.offset
= sk
->max_offset
;
1248 ret
= btrfs_comp_cpu_keys(key
, &test
);
1254 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1255 struct btrfs_path
*path
,
1256 struct btrfs_key
*key
,
1257 struct btrfs_ioctl_search_key
*sk
,
1259 unsigned long *sk_offset
,
1263 struct extent_buffer
*leaf
;
1264 struct btrfs_ioctl_search_header sh
;
1265 unsigned long item_off
;
1266 unsigned long item_len
;
1273 leaf
= path
->nodes
[0];
1274 slot
= path
->slots
[0];
1275 nritems
= btrfs_header_nritems(leaf
);
1277 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1281 found_transid
= btrfs_header_generation(leaf
);
1283 for (i
= slot
; i
< nritems
; i
++) {
1284 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1285 item_len
= btrfs_item_size_nr(leaf
, i
);
1287 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1290 if (sizeof(sh
) + item_len
+ *sk_offset
>
1291 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1296 btrfs_item_key_to_cpu(leaf
, key
, i
);
1297 if (!key_in_sk(key
, sk
))
1300 sh
.objectid
= key
->objectid
;
1301 sh
.offset
= key
->offset
;
1302 sh
.type
= key
->type
;
1304 sh
.transid
= found_transid
;
1306 /* copy search result header */
1307 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1308 *sk_offset
+= sizeof(sh
);
1311 char *p
= buf
+ *sk_offset
;
1313 read_extent_buffer(leaf
, p
,
1314 item_off
, item_len
);
1315 *sk_offset
+= item_len
;
1319 if (*num_found
>= sk
->nr_items
)
1324 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1326 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1329 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1336 *num_found
+= found
;
1340 static noinline
int search_ioctl(struct inode
*inode
,
1341 struct btrfs_ioctl_search_args
*args
)
1343 struct btrfs_root
*root
;
1344 struct btrfs_key key
;
1345 struct btrfs_key max_key
;
1346 struct btrfs_path
*path
;
1347 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1348 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1351 unsigned long sk_offset
= 0;
1353 path
= btrfs_alloc_path();
1357 if (sk
->tree_id
== 0) {
1358 /* search the root of the inode that was passed */
1359 root
= BTRFS_I(inode
)->root
;
1361 key
.objectid
= sk
->tree_id
;
1362 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1363 key
.offset
= (u64
)-1;
1364 root
= btrfs_read_fs_root_no_name(info
, &key
);
1366 printk(KERN_ERR
"could not find root %llu\n",
1368 btrfs_free_path(path
);
1373 key
.objectid
= sk
->min_objectid
;
1374 key
.type
= sk
->min_type
;
1375 key
.offset
= sk
->min_offset
;
1377 max_key
.objectid
= sk
->max_objectid
;
1378 max_key
.type
= sk
->max_type
;
1379 max_key
.offset
= sk
->max_offset
;
1381 path
->keep_locks
= 1;
1384 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1391 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1392 &sk_offset
, &num_found
);
1393 btrfs_release_path(root
, path
);
1394 if (ret
|| num_found
>= sk
->nr_items
)
1400 sk
->nr_items
= num_found
;
1401 btrfs_free_path(path
);
1405 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1408 struct btrfs_ioctl_search_args
*args
;
1409 struct inode
*inode
;
1412 if (!capable(CAP_SYS_ADMIN
))
1415 args
= memdup_user(argp
, sizeof(*args
));
1417 return PTR_ERR(args
);
1419 inode
= fdentry(file
)->d_inode
;
1420 ret
= search_ioctl(inode
, args
);
1421 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1428 * Search INODE_REFs to identify path name of 'dirid' directory
1429 * in a 'tree_id' tree. and sets path name to 'name'.
1431 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1432 u64 tree_id
, u64 dirid
, char *name
)
1434 struct btrfs_root
*root
;
1435 struct btrfs_key key
;
1441 struct btrfs_inode_ref
*iref
;
1442 struct extent_buffer
*l
;
1443 struct btrfs_path
*path
;
1445 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1450 path
= btrfs_alloc_path();
1454 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1456 key
.objectid
= tree_id
;
1457 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1458 key
.offset
= (u64
)-1;
1459 root
= btrfs_read_fs_root_no_name(info
, &key
);
1461 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1466 key
.objectid
= dirid
;
1467 key
.type
= BTRFS_INODE_REF_KEY
;
1468 key
.offset
= (u64
)-1;
1471 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1476 slot
= path
->slots
[0];
1477 if (ret
> 0 && slot
> 0)
1479 btrfs_item_key_to_cpu(l
, &key
, slot
);
1481 if (ret
> 0 && (key
.objectid
!= dirid
||
1482 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1487 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1488 len
= btrfs_inode_ref_name_len(l
, iref
);
1490 total_len
+= len
+ 1;
1495 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1497 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1500 btrfs_release_path(root
, path
);
1501 key
.objectid
= key
.offset
;
1502 key
.offset
= (u64
)-1;
1503 dirid
= key
.objectid
;
1508 memcpy(name
, ptr
, total_len
);
1509 name
[total_len
]='\0';
1512 btrfs_free_path(path
);
1516 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1519 struct btrfs_ioctl_ino_lookup_args
*args
;
1520 struct inode
*inode
;
1523 if (!capable(CAP_SYS_ADMIN
))
1526 args
= memdup_user(argp
, sizeof(*args
));
1528 return PTR_ERR(args
);
1530 inode
= fdentry(file
)->d_inode
;
1532 if (args
->treeid
== 0)
1533 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1535 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1536 args
->treeid
, args
->objectid
,
1539 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1546 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1549 struct dentry
*parent
= fdentry(file
);
1550 struct dentry
*dentry
;
1551 struct inode
*dir
= parent
->d_inode
;
1552 struct inode
*inode
;
1553 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1554 struct btrfs_root
*dest
= NULL
;
1555 struct btrfs_ioctl_vol_args
*vol_args
;
1556 struct btrfs_trans_handle
*trans
;
1561 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1562 if (IS_ERR(vol_args
))
1563 return PTR_ERR(vol_args
);
1565 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1566 namelen
= strlen(vol_args
->name
);
1567 if (strchr(vol_args
->name
, '/') ||
1568 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1573 err
= mnt_want_write(file
->f_path
.mnt
);
1577 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1578 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1579 if (IS_ERR(dentry
)) {
1580 err
= PTR_ERR(dentry
);
1581 goto out_unlock_dir
;
1584 if (!dentry
->d_inode
) {
1589 inode
= dentry
->d_inode
;
1590 dest
= BTRFS_I(inode
)->root
;
1591 if (!capable(CAP_SYS_ADMIN
)){
1593 * Regular user. Only allow this with a special mount
1594 * option, when the user has write+exec access to the
1595 * subvol root, and when rmdir(2) would have been
1598 * Note that this is _not_ check that the subvol is
1599 * empty or doesn't contain data that we wouldn't
1600 * otherwise be able to delete.
1602 * Users who want to delete empty subvols should try
1606 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1610 * Do not allow deletion if the parent dir is the same
1611 * as the dir to be deleted. That means the ioctl
1612 * must be called on the dentry referencing the root
1613 * of the subvol, not a random directory contained
1620 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1624 /* check if subvolume may be deleted by a non-root user */
1625 err
= btrfs_may_delete(dir
, dentry
, 1);
1630 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1635 mutex_lock(&inode
->i_mutex
);
1636 err
= d_invalidate(dentry
);
1640 down_write(&root
->fs_info
->subvol_sem
);
1642 err
= may_destroy_subvol(dest
);
1646 trans
= btrfs_start_transaction(root
, 0);
1647 if (IS_ERR(trans
)) {
1648 err
= PTR_ERR(trans
);
1651 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1653 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1654 dest
->root_key
.objectid
,
1655 dentry
->d_name
.name
,
1656 dentry
->d_name
.len
);
1659 btrfs_record_root_in_trans(trans
, dest
);
1661 memset(&dest
->root_item
.drop_progress
, 0,
1662 sizeof(dest
->root_item
.drop_progress
));
1663 dest
->root_item
.drop_level
= 0;
1664 btrfs_set_root_refs(&dest
->root_item
, 0);
1666 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1667 ret
= btrfs_insert_orphan_item(trans
,
1668 root
->fs_info
->tree_root
,
1669 dest
->root_key
.objectid
);
1673 ret
= btrfs_end_transaction(trans
, root
);
1675 inode
->i_flags
|= S_DEAD
;
1677 up_write(&root
->fs_info
->subvol_sem
);
1679 mutex_unlock(&inode
->i_mutex
);
1681 shrink_dcache_sb(root
->fs_info
->sb
);
1682 btrfs_invalidate_inodes(dest
);
1688 mutex_unlock(&dir
->i_mutex
);
1689 mnt_drop_write(file
->f_path
.mnt
);
1695 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1697 struct inode
*inode
= fdentry(file
)->d_inode
;
1698 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1699 struct btrfs_ioctl_defrag_range_args
*range
;
1702 if (btrfs_root_readonly(root
))
1705 ret
= mnt_want_write(file
->f_path
.mnt
);
1709 switch (inode
->i_mode
& S_IFMT
) {
1711 if (!capable(CAP_SYS_ADMIN
)) {
1715 ret
= btrfs_defrag_root(root
, 0);
1718 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1721 if (!(file
->f_mode
& FMODE_WRITE
)) {
1726 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1733 if (copy_from_user(range
, argp
,
1739 /* compression requires us to start the IO */
1740 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1741 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1742 range
->extent_thresh
= (u32
)-1;
1745 /* the rest are all set to zero by kzalloc */
1746 range
->len
= (u64
)-1;
1748 ret
= btrfs_defrag_file(file
, range
);
1755 mnt_drop_write(file
->f_path
.mnt
);
1759 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1761 struct btrfs_ioctl_vol_args
*vol_args
;
1764 if (!capable(CAP_SYS_ADMIN
))
1767 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1768 if (IS_ERR(vol_args
))
1769 return PTR_ERR(vol_args
);
1771 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1772 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1778 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1780 struct btrfs_ioctl_vol_args
*vol_args
;
1783 if (!capable(CAP_SYS_ADMIN
))
1786 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1789 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1790 if (IS_ERR(vol_args
))
1791 return PTR_ERR(vol_args
);
1793 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1794 ret
= btrfs_rm_device(root
, vol_args
->name
);
1800 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1801 u64 off
, u64 olen
, u64 destoff
)
1803 struct inode
*inode
= fdentry(file
)->d_inode
;
1804 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1805 struct file
*src_file
;
1807 struct btrfs_trans_handle
*trans
;
1808 struct btrfs_path
*path
;
1809 struct extent_buffer
*leaf
;
1811 struct btrfs_key key
;
1816 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1821 * - split compressed inline extents. annoying: we need to
1822 * decompress into destination's address_space (the file offset
1823 * may change, so source mapping won't do), then recompress (or
1824 * otherwise reinsert) a subrange.
1825 * - allow ranges within the same file to be cloned (provided
1826 * they don't overlap)?
1829 /* the destination must be opened for writing */
1830 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
1833 if (btrfs_root_readonly(root
))
1836 ret
= mnt_want_write(file
->f_path
.mnt
);
1840 src_file
= fget(srcfd
);
1843 goto out_drop_write
;
1846 src
= src_file
->f_dentry
->d_inode
;
1852 /* the src must be open for reading */
1853 if (!(src_file
->f_mode
& FMODE_READ
))
1857 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1861 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1865 buf
= vmalloc(btrfs_level_size(root
, 0));
1869 path
= btrfs_alloc_path();
1877 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
1878 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
1880 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
1881 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1884 /* determine range to clone */
1886 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
1889 olen
= len
= src
->i_size
- off
;
1890 /* if we extend to eof, continue to block boundary */
1891 if (off
+ len
== src
->i_size
)
1892 len
= ALIGN(src
->i_size
, bs
) - off
;
1894 /* verify the end result is block aligned */
1895 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
1896 !IS_ALIGNED(destoff
, bs
))
1899 /* do any pending delalloc/csum calc on src, one way or
1900 another, and lock file content */
1902 struct btrfs_ordered_extent
*ordered
;
1903 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1904 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
1906 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
1907 EXTENT_DELALLOC
, 0, NULL
))
1909 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1911 btrfs_put_ordered_extent(ordered
);
1912 btrfs_wait_ordered_range(src
, off
, len
);
1916 key
.objectid
= src
->i_ino
;
1917 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1922 * note the key will change type as we walk through the
1925 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1929 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1930 if (path
->slots
[0] >= nritems
) {
1931 ret
= btrfs_next_leaf(root
, path
);
1936 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1938 leaf
= path
->nodes
[0];
1939 slot
= path
->slots
[0];
1941 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1942 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1943 key
.objectid
!= src
->i_ino
)
1946 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1947 struct btrfs_file_extent_item
*extent
;
1950 struct btrfs_key new_key
;
1951 u64 disko
= 0, diskl
= 0;
1952 u64 datao
= 0, datal
= 0;
1956 size
= btrfs_item_size_nr(leaf
, slot
);
1957 read_extent_buffer(leaf
, buf
,
1958 btrfs_item_ptr_offset(leaf
, slot
),
1961 extent
= btrfs_item_ptr(leaf
, slot
,
1962 struct btrfs_file_extent_item
);
1963 comp
= btrfs_file_extent_compression(leaf
, extent
);
1964 type
= btrfs_file_extent_type(leaf
, extent
);
1965 if (type
== BTRFS_FILE_EXTENT_REG
||
1966 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1967 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1969 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1971 datao
= btrfs_file_extent_offset(leaf
, extent
);
1972 datal
= btrfs_file_extent_num_bytes(leaf
,
1974 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1975 /* take upper bound, may be compressed */
1976 datal
= btrfs_file_extent_ram_bytes(leaf
,
1979 btrfs_release_path(root
, path
);
1981 if (key
.offset
+ datal
<= off
||
1982 key
.offset
>= off
+len
)
1985 memcpy(&new_key
, &key
, sizeof(new_key
));
1986 new_key
.objectid
= inode
->i_ino
;
1987 if (off
<= key
.offset
)
1988 new_key
.offset
= key
.offset
+ destoff
- off
;
1990 new_key
.offset
= destoff
;
1992 trans
= btrfs_start_transaction(root
, 1);
1993 if (IS_ERR(trans
)) {
1994 ret
= PTR_ERR(trans
);
1998 if (type
== BTRFS_FILE_EXTENT_REG
||
1999 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2000 if (off
> key
.offset
) {
2001 datao
+= off
- key
.offset
;
2002 datal
-= off
- key
.offset
;
2005 if (key
.offset
+ datal
> off
+ len
)
2006 datal
= off
+ len
- key
.offset
;
2008 ret
= btrfs_drop_extents(trans
, inode
,
2010 new_key
.offset
+ datal
,
2014 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2018 leaf
= path
->nodes
[0];
2019 slot
= path
->slots
[0];
2020 write_extent_buffer(leaf
, buf
,
2021 btrfs_item_ptr_offset(leaf
, slot
),
2024 extent
= btrfs_item_ptr(leaf
, slot
,
2025 struct btrfs_file_extent_item
);
2027 /* disko == 0 means it's a hole */
2031 btrfs_set_file_extent_offset(leaf
, extent
,
2033 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2036 inode_add_bytes(inode
, datal
);
2037 ret
= btrfs_inc_extent_ref(trans
, root
,
2039 root
->root_key
.objectid
,
2041 new_key
.offset
- datao
);
2044 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2047 if (off
> key
.offset
) {
2048 skip
= off
- key
.offset
;
2049 new_key
.offset
+= skip
;
2052 if (key
.offset
+ datal
> off
+len
)
2053 trim
= key
.offset
+ datal
- (off
+len
);
2055 if (comp
&& (skip
|| trim
)) {
2057 btrfs_end_transaction(trans
, root
);
2060 size
-= skip
+ trim
;
2061 datal
-= skip
+ trim
;
2063 ret
= btrfs_drop_extents(trans
, inode
,
2065 new_key
.offset
+ datal
,
2069 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2075 btrfs_file_extent_calc_inline_size(0);
2076 memmove(buf
+start
, buf
+start
+skip
,
2080 leaf
= path
->nodes
[0];
2081 slot
= path
->slots
[0];
2082 write_extent_buffer(leaf
, buf
,
2083 btrfs_item_ptr_offset(leaf
, slot
),
2085 inode_add_bytes(inode
, datal
);
2088 btrfs_mark_buffer_dirty(leaf
);
2089 btrfs_release_path(root
, path
);
2091 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2094 * we round up to the block size at eof when
2095 * determining which extents to clone above,
2096 * but shouldn't round up the file size
2098 endoff
= new_key
.offset
+ datal
;
2099 if (endoff
> destoff
+olen
)
2100 endoff
= destoff
+olen
;
2101 if (endoff
> inode
->i_size
)
2102 btrfs_i_size_write(inode
, endoff
);
2104 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
2105 ret
= btrfs_update_inode(trans
, root
, inode
);
2107 btrfs_end_transaction(trans
, root
);
2110 btrfs_release_path(root
, path
);
2115 btrfs_release_path(root
, path
);
2116 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2118 mutex_unlock(&src
->i_mutex
);
2119 mutex_unlock(&inode
->i_mutex
);
2121 btrfs_free_path(path
);
2125 mnt_drop_write(file
->f_path
.mnt
);
2129 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2131 struct btrfs_ioctl_clone_range_args args
;
2133 if (copy_from_user(&args
, argp
, sizeof(args
)))
2135 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2136 args
.src_length
, args
.dest_offset
);
2140 * there are many ways the trans_start and trans_end ioctls can lead
2141 * to deadlocks. They should only be used by applications that
2142 * basically own the machine, and have a very in depth understanding
2143 * of all the possible deadlocks and enospc problems.
2145 static long btrfs_ioctl_trans_start(struct file
*file
)
2147 struct inode
*inode
= fdentry(file
)->d_inode
;
2148 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2149 struct btrfs_trans_handle
*trans
;
2153 if (!capable(CAP_SYS_ADMIN
))
2157 if (file
->private_data
)
2161 if (btrfs_root_readonly(root
))
2164 ret
= mnt_want_write(file
->f_path
.mnt
);
2168 mutex_lock(&root
->fs_info
->trans_mutex
);
2169 root
->fs_info
->open_ioctl_trans
++;
2170 mutex_unlock(&root
->fs_info
->trans_mutex
);
2173 trans
= btrfs_start_ioctl_transaction(root
, 0);
2177 file
->private_data
= trans
;
2181 mutex_lock(&root
->fs_info
->trans_mutex
);
2182 root
->fs_info
->open_ioctl_trans
--;
2183 mutex_unlock(&root
->fs_info
->trans_mutex
);
2184 mnt_drop_write(file
->f_path
.mnt
);
2189 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2191 struct inode
*inode
= fdentry(file
)->d_inode
;
2192 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2193 struct btrfs_root
*new_root
;
2194 struct btrfs_dir_item
*di
;
2195 struct btrfs_trans_handle
*trans
;
2196 struct btrfs_path
*path
;
2197 struct btrfs_key location
;
2198 struct btrfs_disk_key disk_key
;
2199 struct btrfs_super_block
*disk_super
;
2204 if (!capable(CAP_SYS_ADMIN
))
2207 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2211 objectid
= root
->root_key
.objectid
;
2213 location
.objectid
= objectid
;
2214 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2215 location
.offset
= (u64
)-1;
2217 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2218 if (IS_ERR(new_root
))
2219 return PTR_ERR(new_root
);
2221 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2224 path
= btrfs_alloc_path();
2227 path
->leave_spinning
= 1;
2229 trans
= btrfs_start_transaction(root
, 1);
2230 if (IS_ERR(trans
)) {
2231 btrfs_free_path(path
);
2232 return PTR_ERR(trans
);
2235 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2236 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2237 dir_id
, "default", 7, 1);
2238 if (IS_ERR_OR_NULL(di
)) {
2239 btrfs_free_path(path
);
2240 btrfs_end_transaction(trans
, root
);
2241 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2242 "this isn't going to work\n");
2246 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2247 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2248 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2249 btrfs_free_path(path
);
2251 disk_super
= &root
->fs_info
->super_copy
;
2252 features
= btrfs_super_incompat_flags(disk_super
);
2253 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2254 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2255 btrfs_set_super_incompat_flags(disk_super
, features
);
2257 btrfs_end_transaction(trans
, root
);
2262 static void get_block_group_info(struct list_head
*groups_list
,
2263 struct btrfs_ioctl_space_info
*space
)
2265 struct btrfs_block_group_cache
*block_group
;
2267 space
->total_bytes
= 0;
2268 space
->used_bytes
= 0;
2270 list_for_each_entry(block_group
, groups_list
, list
) {
2271 space
->flags
= block_group
->flags
;
2272 space
->total_bytes
+= block_group
->key
.offset
;
2273 space
->used_bytes
+=
2274 btrfs_block_group_used(&block_group
->item
);
2278 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2280 struct btrfs_ioctl_space_args space_args
;
2281 struct btrfs_ioctl_space_info space
;
2282 struct btrfs_ioctl_space_info
*dest
;
2283 struct btrfs_ioctl_space_info
*dest_orig
;
2284 struct btrfs_ioctl_space_info
*user_dest
;
2285 struct btrfs_space_info
*info
;
2286 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2287 BTRFS_BLOCK_GROUP_SYSTEM
,
2288 BTRFS_BLOCK_GROUP_METADATA
,
2289 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2296 if (copy_from_user(&space_args
,
2297 (struct btrfs_ioctl_space_args __user
*)arg
,
2298 sizeof(space_args
)))
2301 for (i
= 0; i
< num_types
; i
++) {
2302 struct btrfs_space_info
*tmp
;
2306 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2308 if (tmp
->flags
== types
[i
]) {
2318 down_read(&info
->groups_sem
);
2319 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2320 if (!list_empty(&info
->block_groups
[c
]))
2323 up_read(&info
->groups_sem
);
2326 /* space_slots == 0 means they are asking for a count */
2327 if (space_args
.space_slots
== 0) {
2328 space_args
.total_spaces
= slot_count
;
2332 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2334 alloc_size
= sizeof(*dest
) * slot_count
;
2336 /* we generally have at most 6 or so space infos, one for each raid
2337 * level. So, a whole page should be more than enough for everyone
2339 if (alloc_size
> PAGE_CACHE_SIZE
)
2342 space_args
.total_spaces
= 0;
2343 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2348 /* now we have a buffer to copy into */
2349 for (i
= 0; i
< num_types
; i
++) {
2350 struct btrfs_space_info
*tmp
;
2357 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2359 if (tmp
->flags
== types
[i
]) {
2368 down_read(&info
->groups_sem
);
2369 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2370 if (!list_empty(&info
->block_groups
[c
])) {
2371 get_block_group_info(&info
->block_groups
[c
],
2373 memcpy(dest
, &space
, sizeof(space
));
2375 space_args
.total_spaces
++;
2381 up_read(&info
->groups_sem
);
2384 user_dest
= (struct btrfs_ioctl_space_info
*)
2385 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2387 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2392 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2399 * there are many ways the trans_start and trans_end ioctls can lead
2400 * to deadlocks. They should only be used by applications that
2401 * basically own the machine, and have a very in depth understanding
2402 * of all the possible deadlocks and enospc problems.
2404 long btrfs_ioctl_trans_end(struct file
*file
)
2406 struct inode
*inode
= fdentry(file
)->d_inode
;
2407 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2408 struct btrfs_trans_handle
*trans
;
2410 trans
= file
->private_data
;
2413 file
->private_data
= NULL
;
2415 btrfs_end_transaction(trans
, root
);
2417 mutex_lock(&root
->fs_info
->trans_mutex
);
2418 root
->fs_info
->open_ioctl_trans
--;
2419 mutex_unlock(&root
->fs_info
->trans_mutex
);
2421 mnt_drop_write(file
->f_path
.mnt
);
2425 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2427 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2428 struct btrfs_trans_handle
*trans
;
2432 trans
= btrfs_start_transaction(root
, 0);
2434 return PTR_ERR(trans
);
2435 transid
= trans
->transid
;
2436 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
2441 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2446 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2448 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2452 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2455 transid
= 0; /* current trans */
2457 return btrfs_wait_for_commit(root
, transid
);
2460 long btrfs_ioctl(struct file
*file
, unsigned int
2461 cmd
, unsigned long arg
)
2463 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2464 void __user
*argp
= (void __user
*)arg
;
2467 case FS_IOC_GETFLAGS
:
2468 return btrfs_ioctl_getflags(file
, argp
);
2469 case FS_IOC_SETFLAGS
:
2470 return btrfs_ioctl_setflags(file
, argp
);
2471 case FS_IOC_GETVERSION
:
2472 return btrfs_ioctl_getversion(file
, argp
);
2474 return btrfs_ioctl_fitrim(file
, argp
);
2475 case BTRFS_IOC_SNAP_CREATE
:
2476 return btrfs_ioctl_snap_create(file
, argp
, 0);
2477 case BTRFS_IOC_SNAP_CREATE_V2
:
2478 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
2479 case BTRFS_IOC_SUBVOL_CREATE
:
2480 return btrfs_ioctl_snap_create(file
, argp
, 1);
2481 case BTRFS_IOC_SNAP_DESTROY
:
2482 return btrfs_ioctl_snap_destroy(file
, argp
);
2483 case BTRFS_IOC_SUBVOL_GETFLAGS
:
2484 return btrfs_ioctl_subvol_getflags(file
, argp
);
2485 case BTRFS_IOC_SUBVOL_SETFLAGS
:
2486 return btrfs_ioctl_subvol_setflags(file
, argp
);
2487 case BTRFS_IOC_DEFAULT_SUBVOL
:
2488 return btrfs_ioctl_default_subvol(file
, argp
);
2489 case BTRFS_IOC_DEFRAG
:
2490 return btrfs_ioctl_defrag(file
, NULL
);
2491 case BTRFS_IOC_DEFRAG_RANGE
:
2492 return btrfs_ioctl_defrag(file
, argp
);
2493 case BTRFS_IOC_RESIZE
:
2494 return btrfs_ioctl_resize(root
, argp
);
2495 case BTRFS_IOC_ADD_DEV
:
2496 return btrfs_ioctl_add_dev(root
, argp
);
2497 case BTRFS_IOC_RM_DEV
:
2498 return btrfs_ioctl_rm_dev(root
, argp
);
2499 case BTRFS_IOC_BALANCE
:
2500 return btrfs_balance(root
->fs_info
->dev_root
);
2501 case BTRFS_IOC_CLONE
:
2502 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2503 case BTRFS_IOC_CLONE_RANGE
:
2504 return btrfs_ioctl_clone_range(file
, argp
);
2505 case BTRFS_IOC_TRANS_START
:
2506 return btrfs_ioctl_trans_start(file
);
2507 case BTRFS_IOC_TRANS_END
:
2508 return btrfs_ioctl_trans_end(file
);
2509 case BTRFS_IOC_TREE_SEARCH
:
2510 return btrfs_ioctl_tree_search(file
, argp
);
2511 case BTRFS_IOC_INO_LOOKUP
:
2512 return btrfs_ioctl_ino_lookup(file
, argp
);
2513 case BTRFS_IOC_SPACE_INFO
:
2514 return btrfs_ioctl_space_info(root
, argp
);
2515 case BTRFS_IOC_SYNC
:
2516 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
2518 case BTRFS_IOC_START_SYNC
:
2519 return btrfs_ioctl_start_sync(file
, argp
);
2520 case BTRFS_IOC_WAIT_SYNC
:
2521 return btrfs_ioctl_wait_sync(file
, argp
);