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
);
253 mutex_unlock(&inode
->i_mutex
);
257 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
259 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
261 return put_user(inode
->i_generation
, arg
);
264 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
266 struct btrfs_root
*root
= fdentry(file
)->d_sb
->s_fs_info
;
267 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
268 struct btrfs_device
*device
;
269 struct request_queue
*q
;
270 struct fstrim_range range
;
271 u64 minlen
= ULLONG_MAX
;
275 if (!capable(CAP_SYS_ADMIN
))
278 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
279 list_for_each_entry(device
, &fs_info
->fs_devices
->devices
, dev_list
) {
282 q
= bdev_get_queue(device
->bdev
);
283 if (blk_queue_discard(q
)) {
285 minlen
= min((u64
)q
->limits
.discard_granularity
,
289 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
293 if (copy_from_user(&range
, arg
, sizeof(range
)))
296 range
.minlen
= max(range
.minlen
, minlen
);
297 ret
= btrfs_trim_fs(root
, &range
);
301 if (copy_to_user(arg
, &range
, sizeof(range
)))
307 static noinline
int create_subvol(struct btrfs_root
*root
,
308 struct dentry
*dentry
,
309 char *name
, int namelen
,
312 struct btrfs_trans_handle
*trans
;
313 struct btrfs_key key
;
314 struct btrfs_root_item root_item
;
315 struct btrfs_inode_item
*inode_item
;
316 struct extent_buffer
*leaf
;
317 struct btrfs_root
*new_root
;
318 struct dentry
*parent
= dget_parent(dentry
);
323 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
326 ret
= btrfs_find_free_objectid(NULL
, root
->fs_info
->tree_root
,
333 dir
= parent
->d_inode
;
341 trans
= btrfs_start_transaction(root
, 6);
344 return PTR_ERR(trans
);
347 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
348 0, objectid
, NULL
, 0, 0, 0);
354 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
355 btrfs_set_header_bytenr(leaf
, leaf
->start
);
356 btrfs_set_header_generation(leaf
, trans
->transid
);
357 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
358 btrfs_set_header_owner(leaf
, objectid
);
360 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
361 (unsigned long)btrfs_header_fsid(leaf
),
363 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
364 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
366 btrfs_mark_buffer_dirty(leaf
);
368 inode_item
= &root_item
.inode
;
369 memset(inode_item
, 0, sizeof(*inode_item
));
370 inode_item
->generation
= cpu_to_le64(1);
371 inode_item
->size
= cpu_to_le64(3);
372 inode_item
->nlink
= cpu_to_le32(1);
373 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
374 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
377 root_item
.byte_limit
= 0;
378 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
380 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
381 btrfs_set_root_generation(&root_item
, trans
->transid
);
382 btrfs_set_root_level(&root_item
, 0);
383 btrfs_set_root_refs(&root_item
, 1);
384 btrfs_set_root_used(&root_item
, leaf
->len
);
385 btrfs_set_root_last_snapshot(&root_item
, 0);
387 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
388 root_item
.drop_level
= 0;
390 btrfs_tree_unlock(leaf
);
391 free_extent_buffer(leaf
);
394 btrfs_set_root_dirid(&root_item
, new_dirid
);
396 key
.objectid
= objectid
;
398 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
399 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
404 key
.offset
= (u64
)-1;
405 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
406 BUG_ON(IS_ERR(new_root
));
408 btrfs_record_root_in_trans(trans
, new_root
);
410 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
411 BTRFS_I(dir
)->block_group
);
413 * insert the directory item
415 ret
= btrfs_set_inode_index(dir
, &index
);
418 ret
= btrfs_insert_dir_item(trans
, root
,
419 name
, namelen
, dir
->i_ino
, &key
,
420 BTRFS_FT_DIR
, index
);
424 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
425 ret
= btrfs_update_inode(trans
, root
, dir
);
428 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
429 objectid
, root
->root_key
.objectid
,
430 dir
->i_ino
, index
, name
, namelen
);
434 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
438 *async_transid
= trans
->transid
;
439 err
= btrfs_commit_transaction_async(trans
, root
, 1);
441 err
= btrfs_commit_transaction(trans
, root
);
448 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
449 char *name
, int namelen
, u64
*async_transid
,
453 struct dentry
*parent
;
454 struct btrfs_pending_snapshot
*pending_snapshot
;
455 struct btrfs_trans_handle
*trans
;
461 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
462 if (!pending_snapshot
)
465 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
466 pending_snapshot
->dentry
= dentry
;
467 pending_snapshot
->root
= root
;
468 pending_snapshot
->readonly
= readonly
;
470 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
472 ret
= PTR_ERR(trans
);
476 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
479 list_add(&pending_snapshot
->list
,
480 &trans
->transaction
->pending_snapshots
);
482 *async_transid
= trans
->transid
;
483 ret
= btrfs_commit_transaction_async(trans
,
484 root
->fs_info
->extent_root
, 1);
486 ret
= btrfs_commit_transaction(trans
,
487 root
->fs_info
->extent_root
);
491 ret
= pending_snapshot
->error
;
495 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
499 parent
= dget_parent(dentry
);
500 inode
= btrfs_lookup_dentry(parent
->d_inode
, dentry
);
503 ret
= PTR_ERR(inode
);
507 d_instantiate(dentry
, inode
);
510 kfree(pending_snapshot
);
514 /* copy of check_sticky in fs/namei.c()
515 * It's inline, so penalty for filesystems that don't use sticky bit is
518 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
520 uid_t fsuid
= current_fsuid();
522 if (!(dir
->i_mode
& S_ISVTX
))
524 if (inode
->i_uid
== fsuid
)
526 if (dir
->i_uid
== fsuid
)
528 return !capable(CAP_FOWNER
);
531 /* copy of may_delete in fs/namei.c()
532 * Check whether we can remove a link victim from directory dir, check
533 * whether the type of victim is right.
534 * 1. We can't do it if dir is read-only (done in permission())
535 * 2. We should have write and exec permissions on dir
536 * 3. We can't remove anything from append-only dir
537 * 4. We can't do anything with immutable dir (done in permission())
538 * 5. If the sticky bit on dir is set we should either
539 * a. be owner of dir, or
540 * b. be owner of victim, or
541 * c. have CAP_FOWNER capability
542 * 6. If the victim is append-only or immutable we can't do antyhing with
543 * links pointing to it.
544 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
545 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
546 * 9. We can't remove a root or mountpoint.
547 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
548 * nfs_async_unlink().
551 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
555 if (!victim
->d_inode
)
558 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
559 audit_inode_child(victim
, dir
);
561 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
566 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
567 IS_APPEND(victim
->d_inode
)||
568 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
571 if (!S_ISDIR(victim
->d_inode
->i_mode
))
575 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
579 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
584 /* copy of may_create in fs/namei.c() */
585 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
591 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
595 * Create a new subvolume below @parent. This is largely modeled after
596 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
597 * inside this filesystem so it's quite a bit simpler.
599 static noinline
int btrfs_mksubvol(struct path
*parent
,
600 char *name
, int namelen
,
601 struct btrfs_root
*snap_src
,
602 u64
*async_transid
, bool readonly
)
604 struct inode
*dir
= parent
->dentry
->d_inode
;
605 struct dentry
*dentry
;
608 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
610 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
611 error
= PTR_ERR(dentry
);
619 error
= mnt_want_write(parent
->mnt
);
623 error
= btrfs_may_create(dir
, dentry
);
627 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
629 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
633 error
= create_snapshot(snap_src
, dentry
,
634 name
, namelen
, async_transid
, readonly
);
636 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
637 name
, namelen
, async_transid
);
640 fsnotify_mkdir(dir
, dentry
);
642 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
644 mnt_drop_write(parent
->mnt
);
648 mutex_unlock(&dir
->i_mutex
);
652 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
653 int thresh
, u64
*last_len
, u64
*skip
,
656 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
657 struct extent_map
*em
= NULL
;
658 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
666 * make sure that once we start defragging and extent, we keep on
669 if (start
< *defrag_end
)
675 * hopefully we have this extent in the tree already, try without
676 * the full extent lock
678 read_lock(&em_tree
->lock
);
679 em
= lookup_extent_mapping(em_tree
, start
, len
);
680 read_unlock(&em_tree
->lock
);
683 /* get the big lock and read metadata off disk */
684 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
685 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
686 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
692 /* this will cover holes, and inline extents */
693 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
697 * we hit a real extent, if it is big don't bother defragging it again
699 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
703 * last_len ends up being a counter of how many bytes we've defragged.
704 * every time we choose not to defrag an extent, we reset *last_len
705 * so that the next tiny extent will force a defrag.
707 * The end result of this is that tiny extents before a single big
708 * extent will force at least part of that big extent to be defragged.
712 *defrag_end
= extent_map_end(em
);
715 *skip
= extent_map_end(em
);
723 static int btrfs_defrag_file(struct file
*file
,
724 struct btrfs_ioctl_defrag_range_args
*range
)
726 struct inode
*inode
= fdentry(file
)->d_inode
;
727 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
728 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
729 struct btrfs_ordered_extent
*ordered
;
731 struct btrfs_super_block
*disk_super
;
732 unsigned long last_index
;
733 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
734 unsigned long total_read
= 0;
743 int compress_type
= BTRFS_COMPRESS_ZLIB
;
745 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
746 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
748 if (range
->compress_type
)
749 compress_type
= range
->compress_type
;
752 if (inode
->i_size
== 0)
755 if (range
->start
+ range
->len
> range
->start
) {
756 last_index
= min_t(u64
, inode
->i_size
- 1,
757 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
759 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
762 i
= range
->start
>> PAGE_CACHE_SHIFT
;
763 while (i
<= last_index
) {
764 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
766 range
->extent_thresh
,
771 * the should_defrag function tells us how much to skip
772 * bump our counter by the suggested amount
774 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
775 i
= max(i
+ 1, next
);
779 if (total_read
% ra_pages
== 0) {
780 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
781 min(last_index
, i
+ ra_pages
- 1));
784 mutex_lock(&inode
->i_mutex
);
785 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
786 BTRFS_I(inode
)->force_compress
= compress_type
;
788 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
792 if (inode
->i_size
== 0 ||
793 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
795 goto err_reservations
;
798 page
= grab_cache_page(inode
->i_mapping
, i
);
801 goto err_reservations
;
804 if (!PageUptodate(page
)) {
805 btrfs_readpage(NULL
, page
);
807 if (!PageUptodate(page
)) {
809 page_cache_release(page
);
811 goto err_reservations
;
815 if (page
->mapping
!= inode
->i_mapping
) {
817 page_cache_release(page
);
821 wait_on_page_writeback(page
);
823 if (PageDirty(page
)) {
824 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
828 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
829 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
830 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
832 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
834 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
836 page_cache_release(page
);
837 btrfs_start_ordered_extent(inode
, ordered
, 1);
838 btrfs_put_ordered_extent(ordered
);
841 set_page_extent_mapped(page
);
844 * this makes sure page_mkwrite is called on the
845 * page if it is dirtied again later
847 clear_page_dirty_for_io(page
);
848 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
849 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
850 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
852 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
853 ClearPageChecked(page
);
854 set_page_dirty(page
);
855 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
859 page_cache_release(page
);
860 mutex_unlock(&inode
->i_mutex
);
862 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
866 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
867 filemap_flush(inode
->i_mapping
);
869 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
870 /* the filemap_flush will queue IO into the worker threads, but
871 * we have to make sure the IO is actually started and that
872 * ordered extents get created before we return
874 atomic_inc(&root
->fs_info
->async_submit_draining
);
875 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
876 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
877 wait_event(root
->fs_info
->async_submit_wait
,
878 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
879 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
881 atomic_dec(&root
->fs_info
->async_submit_draining
);
883 mutex_lock(&inode
->i_mutex
);
884 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
885 mutex_unlock(&inode
->i_mutex
);
888 disk_super
= &root
->fs_info
->super_copy
;
889 features
= btrfs_super_incompat_flags(disk_super
);
890 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
891 features
|= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO
;
892 btrfs_set_super_incompat_flags(disk_super
, features
);
898 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
900 mutex_unlock(&inode
->i_mutex
);
904 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
910 struct btrfs_ioctl_vol_args
*vol_args
;
911 struct btrfs_trans_handle
*trans
;
912 struct btrfs_device
*device
= NULL
;
918 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
921 if (!capable(CAP_SYS_ADMIN
))
924 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
925 if (IS_ERR(vol_args
))
926 return PTR_ERR(vol_args
);
928 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
930 mutex_lock(&root
->fs_info
->volume_mutex
);
931 sizestr
= vol_args
->name
;
932 devstr
= strchr(sizestr
, ':');
935 sizestr
= devstr
+ 1;
937 devstr
= vol_args
->name
;
938 devid
= simple_strtoull(devstr
, &end
, 10);
939 printk(KERN_INFO
"resizing devid %llu\n",
940 (unsigned long long)devid
);
942 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
944 printk(KERN_INFO
"resizer unable to find device %llu\n",
945 (unsigned long long)devid
);
949 if (!strcmp(sizestr
, "max"))
950 new_size
= device
->bdev
->bd_inode
->i_size
;
952 if (sizestr
[0] == '-') {
955 } else if (sizestr
[0] == '+') {
959 new_size
= memparse(sizestr
, NULL
);
966 old_size
= device
->total_bytes
;
969 if (new_size
> old_size
) {
973 new_size
= old_size
- new_size
;
974 } else if (mod
> 0) {
975 new_size
= old_size
+ new_size
;
978 if (new_size
< 256 * 1024 * 1024) {
982 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
987 do_div(new_size
, root
->sectorsize
);
988 new_size
*= root
->sectorsize
;
990 printk(KERN_INFO
"new size for %s is %llu\n",
991 device
->name
, (unsigned long long)new_size
);
993 if (new_size
> old_size
) {
994 trans
= btrfs_start_transaction(root
, 0);
996 ret
= PTR_ERR(trans
);
999 ret
= btrfs_grow_device(trans
, device
, new_size
);
1000 btrfs_commit_transaction(trans
, root
);
1002 ret
= btrfs_shrink_device(device
, new_size
);
1006 mutex_unlock(&root
->fs_info
->volume_mutex
);
1011 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1018 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1019 struct file
*src_file
;
1023 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1026 namelen
= strlen(name
);
1027 if (strchr(name
, '/')) {
1033 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1034 NULL
, transid
, readonly
);
1036 struct inode
*src_inode
;
1037 src_file
= fget(fd
);
1043 src_inode
= src_file
->f_path
.dentry
->d_inode
;
1044 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1045 printk(KERN_INFO
"btrfs: Snapshot src from "
1051 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1052 BTRFS_I(src_inode
)->root
,
1060 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1061 void __user
*arg
, int subvol
)
1063 struct btrfs_ioctl_vol_args
*vol_args
;
1066 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1067 if (IS_ERR(vol_args
))
1068 return PTR_ERR(vol_args
);
1069 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1071 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1072 vol_args
->fd
, subvol
,
1079 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1080 void __user
*arg
, int subvol
)
1082 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1086 bool readonly
= false;
1088 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1089 if (IS_ERR(vol_args
))
1090 return PTR_ERR(vol_args
);
1091 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1093 if (vol_args
->flags
&
1094 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
)) {
1099 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1101 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1104 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1105 vol_args
->fd
, subvol
,
1108 if (ret
== 0 && ptr
&&
1110 offsetof(struct btrfs_ioctl_vol_args_v2
,
1111 transid
), ptr
, sizeof(*ptr
)))
1118 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1121 struct inode
*inode
= fdentry(file
)->d_inode
;
1122 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1126 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
1129 down_read(&root
->fs_info
->subvol_sem
);
1130 if (btrfs_root_readonly(root
))
1131 flags
|= BTRFS_SUBVOL_RDONLY
;
1132 up_read(&root
->fs_info
->subvol_sem
);
1134 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1140 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1143 struct inode
*inode
= fdentry(file
)->d_inode
;
1144 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1145 struct btrfs_trans_handle
*trans
;
1150 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1153 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
1156 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1159 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1162 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1165 if (!is_owner_or_cap(inode
))
1168 down_write(&root
->fs_info
->subvol_sem
);
1171 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1174 root_flags
= btrfs_root_flags(&root
->root_item
);
1175 if (flags
& BTRFS_SUBVOL_RDONLY
)
1176 btrfs_set_root_flags(&root
->root_item
,
1177 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1179 btrfs_set_root_flags(&root
->root_item
,
1180 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1182 trans
= btrfs_start_transaction(root
, 1);
1183 if (IS_ERR(trans
)) {
1184 ret
= PTR_ERR(trans
);
1188 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1189 &root
->root_key
, &root
->root_item
);
1191 btrfs_commit_transaction(trans
, root
);
1194 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1196 up_write(&root
->fs_info
->subvol_sem
);
1201 * helper to check if the subvolume references other subvolumes
1203 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1205 struct btrfs_path
*path
;
1206 struct btrfs_key key
;
1209 path
= btrfs_alloc_path();
1213 key
.objectid
= root
->root_key
.objectid
;
1214 key
.type
= BTRFS_ROOT_REF_KEY
;
1215 key
.offset
= (u64
)-1;
1217 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1224 if (path
->slots
[0] > 0) {
1226 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1227 if (key
.objectid
== root
->root_key
.objectid
&&
1228 key
.type
== BTRFS_ROOT_REF_KEY
)
1232 btrfs_free_path(path
);
1236 static noinline
int key_in_sk(struct btrfs_key
*key
,
1237 struct btrfs_ioctl_search_key
*sk
)
1239 struct btrfs_key test
;
1242 test
.objectid
= sk
->min_objectid
;
1243 test
.type
= sk
->min_type
;
1244 test
.offset
= sk
->min_offset
;
1246 ret
= btrfs_comp_cpu_keys(key
, &test
);
1250 test
.objectid
= sk
->max_objectid
;
1251 test
.type
= sk
->max_type
;
1252 test
.offset
= sk
->max_offset
;
1254 ret
= btrfs_comp_cpu_keys(key
, &test
);
1260 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1261 struct btrfs_path
*path
,
1262 struct btrfs_key
*key
,
1263 struct btrfs_ioctl_search_key
*sk
,
1265 unsigned long *sk_offset
,
1269 struct extent_buffer
*leaf
;
1270 struct btrfs_ioctl_search_header sh
;
1271 unsigned long item_off
;
1272 unsigned long item_len
;
1279 leaf
= path
->nodes
[0];
1280 slot
= path
->slots
[0];
1281 nritems
= btrfs_header_nritems(leaf
);
1283 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1287 found_transid
= btrfs_header_generation(leaf
);
1289 for (i
= slot
; i
< nritems
; i
++) {
1290 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1291 item_len
= btrfs_item_size_nr(leaf
, i
);
1293 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1296 if (sizeof(sh
) + item_len
+ *sk_offset
>
1297 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1302 btrfs_item_key_to_cpu(leaf
, key
, i
);
1303 if (!key_in_sk(key
, sk
))
1306 sh
.objectid
= key
->objectid
;
1307 sh
.offset
= key
->offset
;
1308 sh
.type
= key
->type
;
1310 sh
.transid
= found_transid
;
1312 /* copy search result header */
1313 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1314 *sk_offset
+= sizeof(sh
);
1317 char *p
= buf
+ *sk_offset
;
1319 read_extent_buffer(leaf
, p
,
1320 item_off
, item_len
);
1321 *sk_offset
+= item_len
;
1325 if (*num_found
>= sk
->nr_items
)
1330 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1332 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1335 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1342 *num_found
+= found
;
1346 static noinline
int search_ioctl(struct inode
*inode
,
1347 struct btrfs_ioctl_search_args
*args
)
1349 struct btrfs_root
*root
;
1350 struct btrfs_key key
;
1351 struct btrfs_key max_key
;
1352 struct btrfs_path
*path
;
1353 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1354 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1357 unsigned long sk_offset
= 0;
1359 path
= btrfs_alloc_path();
1363 if (sk
->tree_id
== 0) {
1364 /* search the root of the inode that was passed */
1365 root
= BTRFS_I(inode
)->root
;
1367 key
.objectid
= sk
->tree_id
;
1368 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1369 key
.offset
= (u64
)-1;
1370 root
= btrfs_read_fs_root_no_name(info
, &key
);
1372 printk(KERN_ERR
"could not find root %llu\n",
1374 btrfs_free_path(path
);
1379 key
.objectid
= sk
->min_objectid
;
1380 key
.type
= sk
->min_type
;
1381 key
.offset
= sk
->min_offset
;
1383 max_key
.objectid
= sk
->max_objectid
;
1384 max_key
.type
= sk
->max_type
;
1385 max_key
.offset
= sk
->max_offset
;
1387 path
->keep_locks
= 1;
1390 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1397 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1398 &sk_offset
, &num_found
);
1399 btrfs_release_path(root
, path
);
1400 if (ret
|| num_found
>= sk
->nr_items
)
1406 sk
->nr_items
= num_found
;
1407 btrfs_free_path(path
);
1411 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1414 struct btrfs_ioctl_search_args
*args
;
1415 struct inode
*inode
;
1418 if (!capable(CAP_SYS_ADMIN
))
1421 args
= memdup_user(argp
, sizeof(*args
));
1423 return PTR_ERR(args
);
1425 inode
= fdentry(file
)->d_inode
;
1426 ret
= search_ioctl(inode
, args
);
1427 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1434 * Search INODE_REFs to identify path name of 'dirid' directory
1435 * in a 'tree_id' tree. and sets path name to 'name'.
1437 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1438 u64 tree_id
, u64 dirid
, char *name
)
1440 struct btrfs_root
*root
;
1441 struct btrfs_key key
;
1447 struct btrfs_inode_ref
*iref
;
1448 struct extent_buffer
*l
;
1449 struct btrfs_path
*path
;
1451 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1456 path
= btrfs_alloc_path();
1460 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1462 key
.objectid
= tree_id
;
1463 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1464 key
.offset
= (u64
)-1;
1465 root
= btrfs_read_fs_root_no_name(info
, &key
);
1467 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1472 key
.objectid
= dirid
;
1473 key
.type
= BTRFS_INODE_REF_KEY
;
1474 key
.offset
= (u64
)-1;
1477 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1482 slot
= path
->slots
[0];
1483 if (ret
> 0 && slot
> 0)
1485 btrfs_item_key_to_cpu(l
, &key
, slot
);
1487 if (ret
> 0 && (key
.objectid
!= dirid
||
1488 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1493 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1494 len
= btrfs_inode_ref_name_len(l
, iref
);
1496 total_len
+= len
+ 1;
1501 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1503 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1506 btrfs_release_path(root
, path
);
1507 key
.objectid
= key
.offset
;
1508 key
.offset
= (u64
)-1;
1509 dirid
= key
.objectid
;
1514 memcpy(name
, ptr
, total_len
);
1515 name
[total_len
]='\0';
1518 btrfs_free_path(path
);
1522 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1525 struct btrfs_ioctl_ino_lookup_args
*args
;
1526 struct inode
*inode
;
1529 if (!capable(CAP_SYS_ADMIN
))
1532 args
= memdup_user(argp
, sizeof(*args
));
1534 return PTR_ERR(args
);
1536 inode
= fdentry(file
)->d_inode
;
1538 if (args
->treeid
== 0)
1539 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1541 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1542 args
->treeid
, args
->objectid
,
1545 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1552 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1555 struct dentry
*parent
= fdentry(file
);
1556 struct dentry
*dentry
;
1557 struct inode
*dir
= parent
->d_inode
;
1558 struct inode
*inode
;
1559 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1560 struct btrfs_root
*dest
= NULL
;
1561 struct btrfs_ioctl_vol_args
*vol_args
;
1562 struct btrfs_trans_handle
*trans
;
1567 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1568 if (IS_ERR(vol_args
))
1569 return PTR_ERR(vol_args
);
1571 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1572 namelen
= strlen(vol_args
->name
);
1573 if (strchr(vol_args
->name
, '/') ||
1574 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1579 err
= mnt_want_write(file
->f_path
.mnt
);
1583 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1584 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1585 if (IS_ERR(dentry
)) {
1586 err
= PTR_ERR(dentry
);
1587 goto out_unlock_dir
;
1590 if (!dentry
->d_inode
) {
1595 inode
= dentry
->d_inode
;
1596 dest
= BTRFS_I(inode
)->root
;
1597 if (!capable(CAP_SYS_ADMIN
)){
1599 * Regular user. Only allow this with a special mount
1600 * option, when the user has write+exec access to the
1601 * subvol root, and when rmdir(2) would have been
1604 * Note that this is _not_ check that the subvol is
1605 * empty or doesn't contain data that we wouldn't
1606 * otherwise be able to delete.
1608 * Users who want to delete empty subvols should try
1612 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1616 * Do not allow deletion if the parent dir is the same
1617 * as the dir to be deleted. That means the ioctl
1618 * must be called on the dentry referencing the root
1619 * of the subvol, not a random directory contained
1626 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1630 /* check if subvolume may be deleted by a non-root user */
1631 err
= btrfs_may_delete(dir
, dentry
, 1);
1636 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1641 mutex_lock(&inode
->i_mutex
);
1642 err
= d_invalidate(dentry
);
1646 down_write(&root
->fs_info
->subvol_sem
);
1648 err
= may_destroy_subvol(dest
);
1652 trans
= btrfs_start_transaction(root
, 0);
1653 if (IS_ERR(trans
)) {
1654 err
= PTR_ERR(trans
);
1657 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1659 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1660 dest
->root_key
.objectid
,
1661 dentry
->d_name
.name
,
1662 dentry
->d_name
.len
);
1665 btrfs_record_root_in_trans(trans
, dest
);
1667 memset(&dest
->root_item
.drop_progress
, 0,
1668 sizeof(dest
->root_item
.drop_progress
));
1669 dest
->root_item
.drop_level
= 0;
1670 btrfs_set_root_refs(&dest
->root_item
, 0);
1672 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1673 ret
= btrfs_insert_orphan_item(trans
,
1674 root
->fs_info
->tree_root
,
1675 dest
->root_key
.objectid
);
1679 ret
= btrfs_end_transaction(trans
, root
);
1681 inode
->i_flags
|= S_DEAD
;
1683 up_write(&root
->fs_info
->subvol_sem
);
1685 mutex_unlock(&inode
->i_mutex
);
1687 shrink_dcache_sb(root
->fs_info
->sb
);
1688 btrfs_invalidate_inodes(dest
);
1694 mutex_unlock(&dir
->i_mutex
);
1695 mnt_drop_write(file
->f_path
.mnt
);
1701 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1703 struct inode
*inode
= fdentry(file
)->d_inode
;
1704 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1705 struct btrfs_ioctl_defrag_range_args
*range
;
1708 if (btrfs_root_readonly(root
))
1711 ret
= mnt_want_write(file
->f_path
.mnt
);
1715 switch (inode
->i_mode
& S_IFMT
) {
1717 if (!capable(CAP_SYS_ADMIN
)) {
1721 ret
= btrfs_defrag_root(root
, 0);
1724 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1727 if (!(file
->f_mode
& FMODE_WRITE
)) {
1732 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1739 if (copy_from_user(range
, argp
,
1745 /* compression requires us to start the IO */
1746 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1747 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1748 range
->extent_thresh
= (u32
)-1;
1751 /* the rest are all set to zero by kzalloc */
1752 range
->len
= (u64
)-1;
1754 ret
= btrfs_defrag_file(file
, range
);
1761 mnt_drop_write(file
->f_path
.mnt
);
1765 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1767 struct btrfs_ioctl_vol_args
*vol_args
;
1770 if (!capable(CAP_SYS_ADMIN
))
1773 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1774 if (IS_ERR(vol_args
))
1775 return PTR_ERR(vol_args
);
1777 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1778 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1784 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1786 struct btrfs_ioctl_vol_args
*vol_args
;
1789 if (!capable(CAP_SYS_ADMIN
))
1792 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1795 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1796 if (IS_ERR(vol_args
))
1797 return PTR_ERR(vol_args
);
1799 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1800 ret
= btrfs_rm_device(root
, vol_args
->name
);
1806 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1807 u64 off
, u64 olen
, u64 destoff
)
1809 struct inode
*inode
= fdentry(file
)->d_inode
;
1810 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1811 struct file
*src_file
;
1813 struct btrfs_trans_handle
*trans
;
1814 struct btrfs_path
*path
;
1815 struct extent_buffer
*leaf
;
1817 struct btrfs_key key
;
1822 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1827 * - split compressed inline extents. annoying: we need to
1828 * decompress into destination's address_space (the file offset
1829 * may change, so source mapping won't do), then recompress (or
1830 * otherwise reinsert) a subrange.
1831 * - allow ranges within the same file to be cloned (provided
1832 * they don't overlap)?
1835 /* the destination must be opened for writing */
1836 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
1839 if (btrfs_root_readonly(root
))
1842 ret
= mnt_want_write(file
->f_path
.mnt
);
1846 src_file
= fget(srcfd
);
1849 goto out_drop_write
;
1852 src
= src_file
->f_dentry
->d_inode
;
1858 /* the src must be open for reading */
1859 if (!(src_file
->f_mode
& FMODE_READ
))
1863 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1867 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1871 buf
= vmalloc(btrfs_level_size(root
, 0));
1875 path
= btrfs_alloc_path();
1883 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
1884 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
1886 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
1887 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1890 /* determine range to clone */
1892 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
1895 olen
= len
= src
->i_size
- off
;
1896 /* if we extend to eof, continue to block boundary */
1897 if (off
+ len
== src
->i_size
)
1898 len
= ALIGN(src
->i_size
, bs
) - off
;
1900 /* verify the end result is block aligned */
1901 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
1902 !IS_ALIGNED(destoff
, bs
))
1905 /* do any pending delalloc/csum calc on src, one way or
1906 another, and lock file content */
1908 struct btrfs_ordered_extent
*ordered
;
1909 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1910 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
1912 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
1913 EXTENT_DELALLOC
, 0, NULL
))
1915 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1917 btrfs_put_ordered_extent(ordered
);
1918 btrfs_wait_ordered_range(src
, off
, len
);
1922 key
.objectid
= src
->i_ino
;
1923 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1928 * note the key will change type as we walk through the
1931 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1935 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1936 if (path
->slots
[0] >= nritems
) {
1937 ret
= btrfs_next_leaf(root
, path
);
1942 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1944 leaf
= path
->nodes
[0];
1945 slot
= path
->slots
[0];
1947 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1948 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1949 key
.objectid
!= src
->i_ino
)
1952 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1953 struct btrfs_file_extent_item
*extent
;
1956 struct btrfs_key new_key
;
1957 u64 disko
= 0, diskl
= 0;
1958 u64 datao
= 0, datal
= 0;
1962 size
= btrfs_item_size_nr(leaf
, slot
);
1963 read_extent_buffer(leaf
, buf
,
1964 btrfs_item_ptr_offset(leaf
, slot
),
1967 extent
= btrfs_item_ptr(leaf
, slot
,
1968 struct btrfs_file_extent_item
);
1969 comp
= btrfs_file_extent_compression(leaf
, extent
);
1970 type
= btrfs_file_extent_type(leaf
, extent
);
1971 if (type
== BTRFS_FILE_EXTENT_REG
||
1972 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1973 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1975 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1977 datao
= btrfs_file_extent_offset(leaf
, extent
);
1978 datal
= btrfs_file_extent_num_bytes(leaf
,
1980 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1981 /* take upper bound, may be compressed */
1982 datal
= btrfs_file_extent_ram_bytes(leaf
,
1985 btrfs_release_path(root
, path
);
1987 if (key
.offset
+ datal
<= off
||
1988 key
.offset
>= off
+len
)
1991 memcpy(&new_key
, &key
, sizeof(new_key
));
1992 new_key
.objectid
= inode
->i_ino
;
1993 if (off
<= key
.offset
)
1994 new_key
.offset
= key
.offset
+ destoff
- off
;
1996 new_key
.offset
= destoff
;
1998 trans
= btrfs_start_transaction(root
, 1);
1999 if (IS_ERR(trans
)) {
2000 ret
= PTR_ERR(trans
);
2004 if (type
== BTRFS_FILE_EXTENT_REG
||
2005 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2006 if (off
> key
.offset
) {
2007 datao
+= off
- key
.offset
;
2008 datal
-= off
- key
.offset
;
2011 if (key
.offset
+ datal
> off
+ len
)
2012 datal
= off
+ len
- key
.offset
;
2014 ret
= btrfs_drop_extents(trans
, inode
,
2016 new_key
.offset
+ datal
,
2020 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2024 leaf
= path
->nodes
[0];
2025 slot
= path
->slots
[0];
2026 write_extent_buffer(leaf
, buf
,
2027 btrfs_item_ptr_offset(leaf
, slot
),
2030 extent
= btrfs_item_ptr(leaf
, slot
,
2031 struct btrfs_file_extent_item
);
2033 /* disko == 0 means it's a hole */
2037 btrfs_set_file_extent_offset(leaf
, extent
,
2039 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2042 inode_add_bytes(inode
, datal
);
2043 ret
= btrfs_inc_extent_ref(trans
, root
,
2045 root
->root_key
.objectid
,
2047 new_key
.offset
- datao
);
2050 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2053 if (off
> key
.offset
) {
2054 skip
= off
- key
.offset
;
2055 new_key
.offset
+= skip
;
2058 if (key
.offset
+ datal
> off
+len
)
2059 trim
= key
.offset
+ datal
- (off
+len
);
2061 if (comp
&& (skip
|| trim
)) {
2063 btrfs_end_transaction(trans
, root
);
2066 size
-= skip
+ trim
;
2067 datal
-= skip
+ trim
;
2069 ret
= btrfs_drop_extents(trans
, inode
,
2071 new_key
.offset
+ datal
,
2075 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2081 btrfs_file_extent_calc_inline_size(0);
2082 memmove(buf
+start
, buf
+start
+skip
,
2086 leaf
= path
->nodes
[0];
2087 slot
= path
->slots
[0];
2088 write_extent_buffer(leaf
, buf
,
2089 btrfs_item_ptr_offset(leaf
, slot
),
2091 inode_add_bytes(inode
, datal
);
2094 btrfs_mark_buffer_dirty(leaf
);
2095 btrfs_release_path(root
, path
);
2097 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2100 * we round up to the block size at eof when
2101 * determining which extents to clone above,
2102 * but shouldn't round up the file size
2104 endoff
= new_key
.offset
+ datal
;
2105 if (endoff
> destoff
+olen
)
2106 endoff
= destoff
+olen
;
2107 if (endoff
> inode
->i_size
)
2108 btrfs_i_size_write(inode
, endoff
);
2110 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
2111 ret
= btrfs_update_inode(trans
, root
, inode
);
2113 btrfs_end_transaction(trans
, root
);
2116 btrfs_release_path(root
, path
);
2121 btrfs_release_path(root
, path
);
2122 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2124 mutex_unlock(&src
->i_mutex
);
2125 mutex_unlock(&inode
->i_mutex
);
2127 btrfs_free_path(path
);
2131 mnt_drop_write(file
->f_path
.mnt
);
2135 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2137 struct btrfs_ioctl_clone_range_args args
;
2139 if (copy_from_user(&args
, argp
, sizeof(args
)))
2141 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2142 args
.src_length
, args
.dest_offset
);
2146 * there are many ways the trans_start and trans_end ioctls can lead
2147 * to deadlocks. They should only be used by applications that
2148 * basically own the machine, and have a very in depth understanding
2149 * of all the possible deadlocks and enospc problems.
2151 static long btrfs_ioctl_trans_start(struct file
*file
)
2153 struct inode
*inode
= fdentry(file
)->d_inode
;
2154 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2155 struct btrfs_trans_handle
*trans
;
2159 if (!capable(CAP_SYS_ADMIN
))
2163 if (file
->private_data
)
2167 if (btrfs_root_readonly(root
))
2170 ret
= mnt_want_write(file
->f_path
.mnt
);
2174 mutex_lock(&root
->fs_info
->trans_mutex
);
2175 root
->fs_info
->open_ioctl_trans
++;
2176 mutex_unlock(&root
->fs_info
->trans_mutex
);
2179 trans
= btrfs_start_ioctl_transaction(root
, 0);
2183 file
->private_data
= trans
;
2187 mutex_lock(&root
->fs_info
->trans_mutex
);
2188 root
->fs_info
->open_ioctl_trans
--;
2189 mutex_unlock(&root
->fs_info
->trans_mutex
);
2190 mnt_drop_write(file
->f_path
.mnt
);
2195 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2197 struct inode
*inode
= fdentry(file
)->d_inode
;
2198 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2199 struct btrfs_root
*new_root
;
2200 struct btrfs_dir_item
*di
;
2201 struct btrfs_trans_handle
*trans
;
2202 struct btrfs_path
*path
;
2203 struct btrfs_key location
;
2204 struct btrfs_disk_key disk_key
;
2205 struct btrfs_super_block
*disk_super
;
2210 if (!capable(CAP_SYS_ADMIN
))
2213 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2217 objectid
= root
->root_key
.objectid
;
2219 location
.objectid
= objectid
;
2220 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2221 location
.offset
= (u64
)-1;
2223 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2224 if (IS_ERR(new_root
))
2225 return PTR_ERR(new_root
);
2227 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2230 path
= btrfs_alloc_path();
2233 path
->leave_spinning
= 1;
2235 trans
= btrfs_start_transaction(root
, 1);
2236 if (IS_ERR(trans
)) {
2237 btrfs_free_path(path
);
2238 return PTR_ERR(trans
);
2241 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2242 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2243 dir_id
, "default", 7, 1);
2244 if (IS_ERR_OR_NULL(di
)) {
2245 btrfs_free_path(path
);
2246 btrfs_end_transaction(trans
, root
);
2247 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2248 "this isn't going to work\n");
2252 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2253 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2254 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2255 btrfs_free_path(path
);
2257 disk_super
= &root
->fs_info
->super_copy
;
2258 features
= btrfs_super_incompat_flags(disk_super
);
2259 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2260 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2261 btrfs_set_super_incompat_flags(disk_super
, features
);
2263 btrfs_end_transaction(trans
, root
);
2268 static void get_block_group_info(struct list_head
*groups_list
,
2269 struct btrfs_ioctl_space_info
*space
)
2271 struct btrfs_block_group_cache
*block_group
;
2273 space
->total_bytes
= 0;
2274 space
->used_bytes
= 0;
2276 list_for_each_entry(block_group
, groups_list
, list
) {
2277 space
->flags
= block_group
->flags
;
2278 space
->total_bytes
+= block_group
->key
.offset
;
2279 space
->used_bytes
+=
2280 btrfs_block_group_used(&block_group
->item
);
2284 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2286 struct btrfs_ioctl_space_args space_args
;
2287 struct btrfs_ioctl_space_info space
;
2288 struct btrfs_ioctl_space_info
*dest
;
2289 struct btrfs_ioctl_space_info
*dest_orig
;
2290 struct btrfs_ioctl_space_info
*user_dest
;
2291 struct btrfs_space_info
*info
;
2292 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2293 BTRFS_BLOCK_GROUP_SYSTEM
,
2294 BTRFS_BLOCK_GROUP_METADATA
,
2295 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2302 if (copy_from_user(&space_args
,
2303 (struct btrfs_ioctl_space_args __user
*)arg
,
2304 sizeof(space_args
)))
2307 for (i
= 0; i
< num_types
; i
++) {
2308 struct btrfs_space_info
*tmp
;
2312 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2314 if (tmp
->flags
== types
[i
]) {
2324 down_read(&info
->groups_sem
);
2325 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2326 if (!list_empty(&info
->block_groups
[c
]))
2329 up_read(&info
->groups_sem
);
2332 /* space_slots == 0 means they are asking for a count */
2333 if (space_args
.space_slots
== 0) {
2334 space_args
.total_spaces
= slot_count
;
2338 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2340 alloc_size
= sizeof(*dest
) * slot_count
;
2342 /* we generally have at most 6 or so space infos, one for each raid
2343 * level. So, a whole page should be more than enough for everyone
2345 if (alloc_size
> PAGE_CACHE_SIZE
)
2348 space_args
.total_spaces
= 0;
2349 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2354 /* now we have a buffer to copy into */
2355 for (i
= 0; i
< num_types
; i
++) {
2356 struct btrfs_space_info
*tmp
;
2363 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2365 if (tmp
->flags
== types
[i
]) {
2374 down_read(&info
->groups_sem
);
2375 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2376 if (!list_empty(&info
->block_groups
[c
])) {
2377 get_block_group_info(&info
->block_groups
[c
],
2379 memcpy(dest
, &space
, sizeof(space
));
2381 space_args
.total_spaces
++;
2387 up_read(&info
->groups_sem
);
2390 user_dest
= (struct btrfs_ioctl_space_info
*)
2391 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2393 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2398 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2405 * there are many ways the trans_start and trans_end ioctls can lead
2406 * to deadlocks. They should only be used by applications that
2407 * basically own the machine, and have a very in depth understanding
2408 * of all the possible deadlocks and enospc problems.
2410 long btrfs_ioctl_trans_end(struct file
*file
)
2412 struct inode
*inode
= fdentry(file
)->d_inode
;
2413 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2414 struct btrfs_trans_handle
*trans
;
2416 trans
= file
->private_data
;
2419 file
->private_data
= NULL
;
2421 btrfs_end_transaction(trans
, root
);
2423 mutex_lock(&root
->fs_info
->trans_mutex
);
2424 root
->fs_info
->open_ioctl_trans
--;
2425 mutex_unlock(&root
->fs_info
->trans_mutex
);
2427 mnt_drop_write(file
->f_path
.mnt
);
2431 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2433 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2434 struct btrfs_trans_handle
*trans
;
2438 trans
= btrfs_start_transaction(root
, 0);
2440 return PTR_ERR(trans
);
2441 transid
= trans
->transid
;
2442 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
2444 btrfs_end_transaction(trans
, root
);
2449 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2454 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2456 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2460 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2463 transid
= 0; /* current trans */
2465 return btrfs_wait_for_commit(root
, transid
);
2468 long btrfs_ioctl(struct file
*file
, unsigned int
2469 cmd
, unsigned long arg
)
2471 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2472 void __user
*argp
= (void __user
*)arg
;
2475 case FS_IOC_GETFLAGS
:
2476 return btrfs_ioctl_getflags(file
, argp
);
2477 case FS_IOC_SETFLAGS
:
2478 return btrfs_ioctl_setflags(file
, argp
);
2479 case FS_IOC_GETVERSION
:
2480 return btrfs_ioctl_getversion(file
, argp
);
2482 return btrfs_ioctl_fitrim(file
, argp
);
2483 case BTRFS_IOC_SNAP_CREATE
:
2484 return btrfs_ioctl_snap_create(file
, argp
, 0);
2485 case BTRFS_IOC_SNAP_CREATE_V2
:
2486 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
2487 case BTRFS_IOC_SUBVOL_CREATE
:
2488 return btrfs_ioctl_snap_create(file
, argp
, 1);
2489 case BTRFS_IOC_SNAP_DESTROY
:
2490 return btrfs_ioctl_snap_destroy(file
, argp
);
2491 case BTRFS_IOC_SUBVOL_GETFLAGS
:
2492 return btrfs_ioctl_subvol_getflags(file
, argp
);
2493 case BTRFS_IOC_SUBVOL_SETFLAGS
:
2494 return btrfs_ioctl_subvol_setflags(file
, argp
);
2495 case BTRFS_IOC_DEFAULT_SUBVOL
:
2496 return btrfs_ioctl_default_subvol(file
, argp
);
2497 case BTRFS_IOC_DEFRAG
:
2498 return btrfs_ioctl_defrag(file
, NULL
);
2499 case BTRFS_IOC_DEFRAG_RANGE
:
2500 return btrfs_ioctl_defrag(file
, argp
);
2501 case BTRFS_IOC_RESIZE
:
2502 return btrfs_ioctl_resize(root
, argp
);
2503 case BTRFS_IOC_ADD_DEV
:
2504 return btrfs_ioctl_add_dev(root
, argp
);
2505 case BTRFS_IOC_RM_DEV
:
2506 return btrfs_ioctl_rm_dev(root
, argp
);
2507 case BTRFS_IOC_BALANCE
:
2508 return btrfs_balance(root
->fs_info
->dev_root
);
2509 case BTRFS_IOC_CLONE
:
2510 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2511 case BTRFS_IOC_CLONE_RANGE
:
2512 return btrfs_ioctl_clone_range(file
, argp
);
2513 case BTRFS_IOC_TRANS_START
:
2514 return btrfs_ioctl_trans_start(file
);
2515 case BTRFS_IOC_TRANS_END
:
2516 return btrfs_ioctl_trans_end(file
);
2517 case BTRFS_IOC_TREE_SEARCH
:
2518 return btrfs_ioctl_tree_search(file
, argp
);
2519 case BTRFS_IOC_INO_LOOKUP
:
2520 return btrfs_ioctl_ino_lookup(file
, argp
);
2521 case BTRFS_IOC_SPACE_INFO
:
2522 return btrfs_ioctl_space_info(root
, argp
);
2523 case BTRFS_IOC_SYNC
:
2524 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
2526 case BTRFS_IOC_START_SYNC
:
2527 return btrfs_ioctl_start_sync(file
, argp
);
2528 case BTRFS_IOC_WAIT_SYNC
:
2529 return btrfs_ioctl_wait_sync(file
, argp
);