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>
46 #include "transaction.h"
47 #include "btrfs_inode.h"
49 #include "print-tree.h"
53 /* Mask out flags that are inappropriate for the given type of inode. */
54 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
58 else if (S_ISREG(mode
))
59 return flags
& ~FS_DIRSYNC_FL
;
61 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
69 unsigned int iflags
= 0;
71 if (flags
& BTRFS_INODE_SYNC
)
73 if (flags
& BTRFS_INODE_IMMUTABLE
)
74 iflags
|= FS_IMMUTABLE_FL
;
75 if (flags
& BTRFS_INODE_APPEND
)
76 iflags
|= FS_APPEND_FL
;
77 if (flags
& BTRFS_INODE_NODUMP
)
78 iflags
|= FS_NODUMP_FL
;
79 if (flags
& BTRFS_INODE_NOATIME
)
80 iflags
|= FS_NOATIME_FL
;
81 if (flags
& BTRFS_INODE_DIRSYNC
)
82 iflags
|= FS_DIRSYNC_FL
;
88 * Update inode->i_flags based on the btrfs internal flags.
90 void btrfs_update_iflags(struct inode
*inode
)
92 struct btrfs_inode
*ip
= BTRFS_I(inode
);
94 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
96 if (ip
->flags
& BTRFS_INODE_SYNC
)
97 inode
->i_flags
|= S_SYNC
;
98 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
99 inode
->i_flags
|= S_IMMUTABLE
;
100 if (ip
->flags
& BTRFS_INODE_APPEND
)
101 inode
->i_flags
|= S_APPEND
;
102 if (ip
->flags
& BTRFS_INODE_NOATIME
)
103 inode
->i_flags
|= S_NOATIME
;
104 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
105 inode
->i_flags
|= S_DIRSYNC
;
109 * Inherit flags from the parent inode.
111 * Unlike extN we don't have any flags we don't want to inherit currently.
113 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
120 flags
= BTRFS_I(dir
)->flags
;
122 if (S_ISREG(inode
->i_mode
))
123 flags
&= ~BTRFS_INODE_DIRSYNC
;
124 else if (!S_ISDIR(inode
->i_mode
))
125 flags
&= (BTRFS_INODE_NODUMP
| BTRFS_INODE_NOATIME
);
127 BTRFS_I(inode
)->flags
= flags
;
128 btrfs_update_iflags(inode
);
131 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
133 struct btrfs_inode
*ip
= BTRFS_I(file
->f_path
.dentry
->d_inode
);
134 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
136 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
141 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
143 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
144 struct btrfs_inode
*ip
= BTRFS_I(inode
);
145 struct btrfs_root
*root
= ip
->root
;
146 struct btrfs_trans_handle
*trans
;
147 unsigned int flags
, oldflags
;
150 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
153 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
154 FS_NOATIME_FL
| FS_NODUMP_FL
| \
155 FS_SYNC_FL
| FS_DIRSYNC_FL
))
158 if (!is_owner_or_cap(inode
))
161 mutex_lock(&inode
->i_mutex
);
163 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
164 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
165 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
166 if (!capable(CAP_LINUX_IMMUTABLE
)) {
172 ret
= mnt_want_write(file
->f_path
.mnt
);
176 if (flags
& FS_SYNC_FL
)
177 ip
->flags
|= BTRFS_INODE_SYNC
;
179 ip
->flags
&= ~BTRFS_INODE_SYNC
;
180 if (flags
& FS_IMMUTABLE_FL
)
181 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
183 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
184 if (flags
& FS_APPEND_FL
)
185 ip
->flags
|= BTRFS_INODE_APPEND
;
187 ip
->flags
&= ~BTRFS_INODE_APPEND
;
188 if (flags
& FS_NODUMP_FL
)
189 ip
->flags
|= BTRFS_INODE_NODUMP
;
191 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
192 if (flags
& FS_NOATIME_FL
)
193 ip
->flags
|= BTRFS_INODE_NOATIME
;
195 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
196 if (flags
& FS_DIRSYNC_FL
)
197 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
199 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
202 trans
= btrfs_join_transaction(root
, 1);
205 ret
= btrfs_update_inode(trans
, root
, inode
);
208 btrfs_update_iflags(inode
);
209 inode
->i_ctime
= CURRENT_TIME
;
210 btrfs_end_transaction(trans
, root
);
212 mnt_drop_write(file
->f_path
.mnt
);
214 mutex_unlock(&inode
->i_mutex
);
218 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
220 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
222 return put_user(inode
->i_generation
, arg
);
225 static noinline
int create_subvol(struct btrfs_root
*root
,
226 struct dentry
*dentry
,
227 char *name
, int namelen
)
229 struct btrfs_trans_handle
*trans
;
230 struct btrfs_key key
;
231 struct btrfs_root_item root_item
;
232 struct btrfs_inode_item
*inode_item
;
233 struct extent_buffer
*leaf
;
234 struct btrfs_root
*new_root
;
235 struct inode
*dir
= dentry
->d_parent
->d_inode
;
239 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
242 ret
= btrfs_find_free_objectid(NULL
, root
->fs_info
->tree_root
,
252 trans
= btrfs_start_transaction(root
, 6);
254 return PTR_ERR(trans
);
256 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
257 0, objectid
, NULL
, 0, 0, 0);
263 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
264 btrfs_set_header_bytenr(leaf
, leaf
->start
);
265 btrfs_set_header_generation(leaf
, trans
->transid
);
266 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
267 btrfs_set_header_owner(leaf
, objectid
);
269 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
270 (unsigned long)btrfs_header_fsid(leaf
),
272 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
273 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
275 btrfs_mark_buffer_dirty(leaf
);
277 inode_item
= &root_item
.inode
;
278 memset(inode_item
, 0, sizeof(*inode_item
));
279 inode_item
->generation
= cpu_to_le64(1);
280 inode_item
->size
= cpu_to_le64(3);
281 inode_item
->nlink
= cpu_to_le32(1);
282 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
283 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
285 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
286 btrfs_set_root_generation(&root_item
, trans
->transid
);
287 btrfs_set_root_level(&root_item
, 0);
288 btrfs_set_root_refs(&root_item
, 1);
289 btrfs_set_root_used(&root_item
, leaf
->len
);
290 btrfs_set_root_last_snapshot(&root_item
, 0);
292 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
293 root_item
.drop_level
= 0;
295 btrfs_tree_unlock(leaf
);
296 free_extent_buffer(leaf
);
299 btrfs_set_root_dirid(&root_item
, new_dirid
);
301 key
.objectid
= objectid
;
303 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
304 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
309 key
.offset
= (u64
)-1;
310 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
311 BUG_ON(IS_ERR(new_root
));
313 btrfs_record_root_in_trans(trans
, new_root
);
315 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
316 BTRFS_I(dir
)->block_group
);
318 * insert the directory item
320 ret
= btrfs_set_inode_index(dir
, &index
);
323 ret
= btrfs_insert_dir_item(trans
, root
,
324 name
, namelen
, dir
->i_ino
, &key
,
325 BTRFS_FT_DIR
, index
);
329 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
330 ret
= btrfs_update_inode(trans
, root
, dir
);
333 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
334 objectid
, root
->root_key
.objectid
,
335 dir
->i_ino
, index
, name
, namelen
);
339 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
341 err
= btrfs_commit_transaction(trans
, root
);
347 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
)
350 struct btrfs_pending_snapshot
*pending_snapshot
;
351 struct btrfs_trans_handle
*trans
;
357 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
358 if (!pending_snapshot
)
361 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
362 pending_snapshot
->dentry
= dentry
;
363 pending_snapshot
->root
= root
;
365 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
367 ret
= PTR_ERR(trans
);
371 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
374 list_add(&pending_snapshot
->list
,
375 &trans
->transaction
->pending_snapshots
);
376 ret
= btrfs_commit_transaction(trans
, root
->fs_info
->extent_root
);
379 ret
= pending_snapshot
->error
;
383 btrfs_orphan_cleanup(pending_snapshot
->snap
);
385 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
387 ret
= PTR_ERR(inode
);
391 d_instantiate(dentry
, inode
);
394 kfree(pending_snapshot
);
398 /* copy of may_create in fs/namei.c() */
399 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
405 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
409 * Create a new subvolume below @parent. This is largely modeled after
410 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
411 * inside this filesystem so it's quite a bit simpler.
413 static noinline
int btrfs_mksubvol(struct path
*parent
,
414 char *name
, int namelen
,
415 struct btrfs_root
*snap_src
)
417 struct inode
*dir
= parent
->dentry
->d_inode
;
418 struct dentry
*dentry
;
421 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
423 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
424 error
= PTR_ERR(dentry
);
432 error
= mnt_want_write(parent
->mnt
);
436 error
= btrfs_may_create(dir
, dentry
);
440 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
442 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
446 error
= create_snapshot(snap_src
, dentry
);
448 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
452 fsnotify_mkdir(dir
, dentry
);
454 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
456 mnt_drop_write(parent
->mnt
);
460 mutex_unlock(&dir
->i_mutex
);
464 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
465 int thresh
, u64
*last_len
, u64
*skip
,
468 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
469 struct extent_map
*em
= NULL
;
470 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
478 * make sure that once we start defragging and extent, we keep on
481 if (start
< *defrag_end
)
487 * hopefully we have this extent in the tree already, try without
488 * the full extent lock
490 read_lock(&em_tree
->lock
);
491 em
= lookup_extent_mapping(em_tree
, start
, len
);
492 read_unlock(&em_tree
->lock
);
495 /* get the big lock and read metadata off disk */
496 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
497 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
498 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
504 /* this will cover holes, and inline extents */
505 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
509 * we hit a real extent, if it is big don't bother defragging it again
511 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
515 * last_len ends up being a counter of how many bytes we've defragged.
516 * every time we choose not to defrag an extent, we reset *last_len
517 * so that the next tiny extent will force a defrag.
519 * The end result of this is that tiny extents before a single big
520 * extent will force at least part of that big extent to be defragged.
524 *defrag_end
= extent_map_end(em
);
527 *skip
= extent_map_end(em
);
535 static int btrfs_defrag_file(struct file
*file
,
536 struct btrfs_ioctl_defrag_range_args
*range
)
538 struct inode
*inode
= fdentry(file
)->d_inode
;
539 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
540 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
541 struct btrfs_ordered_extent
*ordered
;
543 unsigned long last_index
;
544 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
545 unsigned long total_read
= 0;
554 if (inode
->i_size
== 0)
557 if (range
->start
+ range
->len
> range
->start
) {
558 last_index
= min_t(u64
, inode
->i_size
- 1,
559 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
561 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
564 i
= range
->start
>> PAGE_CACHE_SHIFT
;
565 while (i
<= last_index
) {
566 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
568 range
->extent_thresh
,
573 * the should_defrag function tells us how much to skip
574 * bump our counter by the suggested amount
576 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
577 i
= max(i
+ 1, next
);
581 if (total_read
% ra_pages
== 0) {
582 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
583 min(last_index
, i
+ ra_pages
- 1));
586 mutex_lock(&inode
->i_mutex
);
587 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
588 BTRFS_I(inode
)->force_compress
= 1;
590 ret
= btrfs_check_data_free_space(root
, inode
, PAGE_CACHE_SIZE
);
596 ret
= btrfs_reserve_metadata_for_delalloc(root
, inode
, 1);
598 btrfs_free_reserved_data_space(root
, inode
,
604 if (inode
->i_size
== 0 ||
605 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
607 goto err_reservations
;
610 page
= grab_cache_page(inode
->i_mapping
, i
);
612 goto err_reservations
;
614 if (!PageUptodate(page
)) {
615 btrfs_readpage(NULL
, page
);
617 if (!PageUptodate(page
)) {
619 page_cache_release(page
);
620 goto err_reservations
;
624 if (page
->mapping
!= inode
->i_mapping
) {
626 page_cache_release(page
);
630 wait_on_page_writeback(page
);
632 if (PageDirty(page
)) {
633 btrfs_free_reserved_data_space(root
, inode
,
638 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
639 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
640 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
642 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
644 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
646 page_cache_release(page
);
647 btrfs_start_ordered_extent(inode
, ordered
, 1);
648 btrfs_put_ordered_extent(ordered
);
651 set_page_extent_mapped(page
);
654 * this makes sure page_mkwrite is called on the
655 * page if it is dirtied again later
657 clear_page_dirty_for_io(page
);
658 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
659 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
660 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
662 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
663 ClearPageChecked(page
);
664 set_page_dirty(page
);
665 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
669 page_cache_release(page
);
670 mutex_unlock(&inode
->i_mutex
);
672 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
673 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
677 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
678 filemap_flush(inode
->i_mapping
);
680 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
681 /* the filemap_flush will queue IO into the worker threads, but
682 * we have to make sure the IO is actually started and that
683 * ordered extents get created before we return
685 atomic_inc(&root
->fs_info
->async_submit_draining
);
686 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
687 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
688 wait_event(root
->fs_info
->async_submit_wait
,
689 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
690 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
692 atomic_dec(&root
->fs_info
->async_submit_draining
);
694 mutex_lock(&inode
->i_mutex
);
695 BTRFS_I(inode
)->force_compress
= 0;
696 mutex_unlock(&inode
->i_mutex
);
702 mutex_unlock(&inode
->i_mutex
);
703 btrfs_free_reserved_data_space(root
, inode
, PAGE_CACHE_SIZE
);
704 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
708 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
714 struct btrfs_ioctl_vol_args
*vol_args
;
715 struct btrfs_trans_handle
*trans
;
716 struct btrfs_device
*device
= NULL
;
723 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
726 if (!capable(CAP_SYS_ADMIN
))
729 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
730 if (IS_ERR(vol_args
))
731 return PTR_ERR(vol_args
);
733 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
734 namelen
= strlen(vol_args
->name
);
736 mutex_lock(&root
->fs_info
->volume_mutex
);
737 sizestr
= vol_args
->name
;
738 devstr
= strchr(sizestr
, ':');
741 sizestr
= devstr
+ 1;
743 devstr
= vol_args
->name
;
744 devid
= simple_strtoull(devstr
, &end
, 10);
745 printk(KERN_INFO
"resizing devid %llu\n",
746 (unsigned long long)devid
);
748 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
750 printk(KERN_INFO
"resizer unable to find device %llu\n",
751 (unsigned long long)devid
);
755 if (!strcmp(sizestr
, "max"))
756 new_size
= device
->bdev
->bd_inode
->i_size
;
758 if (sizestr
[0] == '-') {
761 } else if (sizestr
[0] == '+') {
765 new_size
= memparse(sizestr
, NULL
);
772 old_size
= device
->total_bytes
;
775 if (new_size
> old_size
) {
779 new_size
= old_size
- new_size
;
780 } else if (mod
> 0) {
781 new_size
= old_size
+ new_size
;
784 if (new_size
< 256 * 1024 * 1024) {
788 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
793 do_div(new_size
, root
->sectorsize
);
794 new_size
*= root
->sectorsize
;
796 printk(KERN_INFO
"new size for %s is %llu\n",
797 device
->name
, (unsigned long long)new_size
);
799 if (new_size
> old_size
) {
800 trans
= btrfs_start_transaction(root
, 0);
801 ret
= btrfs_grow_device(trans
, device
, new_size
);
802 btrfs_commit_transaction(trans
, root
);
804 ret
= btrfs_shrink_device(device
, new_size
);
808 mutex_unlock(&root
->fs_info
->volume_mutex
);
813 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
814 void __user
*arg
, int subvol
)
816 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
817 struct btrfs_ioctl_vol_args
*vol_args
;
818 struct file
*src_file
;
822 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
825 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
826 if (IS_ERR(vol_args
))
827 return PTR_ERR(vol_args
);
829 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
830 namelen
= strlen(vol_args
->name
);
831 if (strchr(vol_args
->name
, '/')) {
837 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
840 struct inode
*src_inode
;
841 src_file
= fget(vol_args
->fd
);
847 src_inode
= src_file
->f_path
.dentry
->d_inode
;
848 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
849 printk(KERN_INFO
"btrfs: Snapshot src from "
855 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
856 BTRFS_I(src_inode
)->root
);
865 * helper to check if the subvolume references other subvolumes
867 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
869 struct btrfs_path
*path
;
870 struct btrfs_key key
;
873 path
= btrfs_alloc_path();
877 key
.objectid
= root
->root_key
.objectid
;
878 key
.type
= BTRFS_ROOT_REF_KEY
;
879 key
.offset
= (u64
)-1;
881 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
888 if (path
->slots
[0] > 0) {
890 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
891 if (key
.objectid
== root
->root_key
.objectid
&&
892 key
.type
== BTRFS_ROOT_REF_KEY
)
896 btrfs_free_path(path
);
900 static noinline
int key_in_sk(struct btrfs_key
*key
,
901 struct btrfs_ioctl_search_key
*sk
)
903 struct btrfs_key test
;
906 test
.objectid
= sk
->min_objectid
;
907 test
.type
= sk
->min_type
;
908 test
.offset
= sk
->min_offset
;
910 ret
= btrfs_comp_cpu_keys(key
, &test
);
914 test
.objectid
= sk
->max_objectid
;
915 test
.type
= sk
->max_type
;
916 test
.offset
= sk
->max_offset
;
918 ret
= btrfs_comp_cpu_keys(key
, &test
);
924 static noinline
int copy_to_sk(struct btrfs_root
*root
,
925 struct btrfs_path
*path
,
926 struct btrfs_key
*key
,
927 struct btrfs_ioctl_search_key
*sk
,
929 unsigned long *sk_offset
,
933 struct extent_buffer
*leaf
;
934 struct btrfs_ioctl_search_header sh
;
935 unsigned long item_off
;
936 unsigned long item_len
;
943 leaf
= path
->nodes
[0];
944 slot
= path
->slots
[0];
945 nritems
= btrfs_header_nritems(leaf
);
947 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
951 found_transid
= btrfs_header_generation(leaf
);
953 for (i
= slot
; i
< nritems
; i
++) {
954 item_off
= btrfs_item_ptr_offset(leaf
, i
);
955 item_len
= btrfs_item_size_nr(leaf
, i
);
957 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
960 if (sizeof(sh
) + item_len
+ *sk_offset
>
961 BTRFS_SEARCH_ARGS_BUFSIZE
) {
966 btrfs_item_key_to_cpu(leaf
, key
, i
);
967 if (!key_in_sk(key
, sk
))
970 sh
.objectid
= key
->objectid
;
971 sh
.offset
= key
->offset
;
974 sh
.transid
= found_transid
;
976 /* copy search result header */
977 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
978 *sk_offset
+= sizeof(sh
);
981 char *p
= buf
+ *sk_offset
;
983 read_extent_buffer(leaf
, p
,
985 *sk_offset
+= item_len
;
989 if (*num_found
>= sk
->nr_items
)
994 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
996 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
999 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1006 *num_found
+= found
;
1010 static noinline
int search_ioctl(struct inode
*inode
,
1011 struct btrfs_ioctl_search_args
*args
)
1013 struct btrfs_root
*root
;
1014 struct btrfs_key key
;
1015 struct btrfs_key max_key
;
1016 struct btrfs_path
*path
;
1017 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1018 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1021 unsigned long sk_offset
= 0;
1023 path
= btrfs_alloc_path();
1027 if (sk
->tree_id
== 0) {
1028 /* search the root of the inode that was passed */
1029 root
= BTRFS_I(inode
)->root
;
1031 key
.objectid
= sk
->tree_id
;
1032 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1033 key
.offset
= (u64
)-1;
1034 root
= btrfs_read_fs_root_no_name(info
, &key
);
1036 printk(KERN_ERR
"could not find root %llu\n",
1038 btrfs_free_path(path
);
1043 key
.objectid
= sk
->min_objectid
;
1044 key
.type
= sk
->min_type
;
1045 key
.offset
= sk
->min_offset
;
1047 max_key
.objectid
= sk
->max_objectid
;
1048 max_key
.type
= sk
->max_type
;
1049 max_key
.offset
= sk
->max_offset
;
1051 path
->keep_locks
= 1;
1054 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1061 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1062 &sk_offset
, &num_found
);
1063 btrfs_release_path(root
, path
);
1064 if (ret
|| num_found
>= sk
->nr_items
)
1070 sk
->nr_items
= num_found
;
1071 btrfs_free_path(path
);
1075 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1078 struct btrfs_ioctl_search_args
*args
;
1079 struct inode
*inode
;
1082 if (!capable(CAP_SYS_ADMIN
))
1085 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
1089 if (copy_from_user(args
, argp
, sizeof(*args
))) {
1093 inode
= fdentry(file
)->d_inode
;
1094 ret
= search_ioctl(inode
, args
);
1095 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1102 * Search INODE_REFs to identify path name of 'dirid' directory
1103 * in a 'tree_id' tree. and sets path name to 'name'.
1105 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1106 u64 tree_id
, u64 dirid
, char *name
)
1108 struct btrfs_root
*root
;
1109 struct btrfs_key key
;
1115 struct btrfs_inode_ref
*iref
;
1116 struct extent_buffer
*l
;
1117 struct btrfs_path
*path
;
1119 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1124 path
= btrfs_alloc_path();
1128 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1130 key
.objectid
= tree_id
;
1131 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1132 key
.offset
= (u64
)-1;
1133 root
= btrfs_read_fs_root_no_name(info
, &key
);
1135 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1140 key
.objectid
= dirid
;
1141 key
.type
= BTRFS_INODE_REF_KEY
;
1142 key
.offset
= (u64
)-1;
1145 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1150 slot
= path
->slots
[0];
1151 if (ret
> 0 && slot
> 0)
1153 btrfs_item_key_to_cpu(l
, &key
, slot
);
1155 if (ret
> 0 && (key
.objectid
!= dirid
||
1156 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1161 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1162 len
= btrfs_inode_ref_name_len(l
, iref
);
1164 total_len
+= len
+ 1;
1169 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1171 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1174 btrfs_release_path(root
, path
);
1175 key
.objectid
= key
.offset
;
1176 key
.offset
= (u64
)-1;
1177 dirid
= key
.objectid
;
1182 memcpy(name
, ptr
, total_len
);
1183 name
[total_len
]='\0';
1186 btrfs_free_path(path
);
1190 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1193 struct btrfs_ioctl_ino_lookup_args
*args
;
1194 struct inode
*inode
;
1197 if (!capable(CAP_SYS_ADMIN
))
1200 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
1204 if (copy_from_user(args
, argp
, sizeof(*args
))) {
1208 inode
= fdentry(file
)->d_inode
;
1210 if (args
->treeid
== 0)
1211 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1213 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1214 args
->treeid
, args
->objectid
,
1217 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1224 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1227 struct dentry
*parent
= fdentry(file
);
1228 struct dentry
*dentry
;
1229 struct inode
*dir
= parent
->d_inode
;
1230 struct inode
*inode
;
1231 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1232 struct btrfs_root
*dest
= NULL
;
1233 struct btrfs_ioctl_vol_args
*vol_args
;
1234 struct btrfs_trans_handle
*trans
;
1239 if (!capable(CAP_SYS_ADMIN
))
1242 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1243 if (IS_ERR(vol_args
))
1244 return PTR_ERR(vol_args
);
1246 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1247 namelen
= strlen(vol_args
->name
);
1248 if (strchr(vol_args
->name
, '/') ||
1249 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1254 err
= mnt_want_write(file
->f_path
.mnt
);
1258 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1259 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1260 if (IS_ERR(dentry
)) {
1261 err
= PTR_ERR(dentry
);
1262 goto out_unlock_dir
;
1265 if (!dentry
->d_inode
) {
1270 inode
= dentry
->d_inode
;
1271 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1276 dest
= BTRFS_I(inode
)->root
;
1278 mutex_lock(&inode
->i_mutex
);
1279 err
= d_invalidate(dentry
);
1283 down_write(&root
->fs_info
->subvol_sem
);
1285 err
= may_destroy_subvol(dest
);
1289 trans
= btrfs_start_transaction(root
, 0);
1290 if (IS_ERR(trans
)) {
1291 err
= PTR_ERR(trans
);
1294 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1296 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1297 dest
->root_key
.objectid
,
1298 dentry
->d_name
.name
,
1299 dentry
->d_name
.len
);
1302 btrfs_record_root_in_trans(trans
, dest
);
1304 memset(&dest
->root_item
.drop_progress
, 0,
1305 sizeof(dest
->root_item
.drop_progress
));
1306 dest
->root_item
.drop_level
= 0;
1307 btrfs_set_root_refs(&dest
->root_item
, 0);
1309 ret
= btrfs_insert_orphan_item(trans
,
1310 root
->fs_info
->tree_root
,
1311 dest
->root_key
.objectid
);
1314 ret
= btrfs_commit_transaction(trans
, root
);
1316 inode
->i_flags
|= S_DEAD
;
1318 up_write(&root
->fs_info
->subvol_sem
);
1320 mutex_unlock(&inode
->i_mutex
);
1322 shrink_dcache_sb(root
->fs_info
->sb
);
1323 btrfs_invalidate_inodes(dest
);
1329 mutex_unlock(&dir
->i_mutex
);
1330 mnt_drop_write(file
->f_path
.mnt
);
1336 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1338 struct inode
*inode
= fdentry(file
)->d_inode
;
1339 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1340 struct btrfs_ioctl_defrag_range_args
*range
;
1343 ret
= mnt_want_write(file
->f_path
.mnt
);
1347 switch (inode
->i_mode
& S_IFMT
) {
1349 if (!capable(CAP_SYS_ADMIN
)) {
1353 btrfs_defrag_root(root
, 0);
1354 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1357 if (!(file
->f_mode
& FMODE_WRITE
)) {
1362 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1369 if (copy_from_user(range
, argp
,
1375 /* compression requires us to start the IO */
1376 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1377 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1378 range
->extent_thresh
= (u32
)-1;
1381 /* the rest are all set to zero by kzalloc */
1382 range
->len
= (u64
)-1;
1384 btrfs_defrag_file(file
, range
);
1389 mnt_drop_write(file
->f_path
.mnt
);
1393 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1395 struct btrfs_ioctl_vol_args
*vol_args
;
1398 if (!capable(CAP_SYS_ADMIN
))
1401 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1402 if (IS_ERR(vol_args
))
1403 return PTR_ERR(vol_args
);
1405 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1406 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1412 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1414 struct btrfs_ioctl_vol_args
*vol_args
;
1417 if (!capable(CAP_SYS_ADMIN
))
1420 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1423 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1424 if (IS_ERR(vol_args
))
1425 return PTR_ERR(vol_args
);
1427 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1428 ret
= btrfs_rm_device(root
, vol_args
->name
);
1434 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1435 u64 off
, u64 olen
, u64 destoff
)
1437 struct inode
*inode
= fdentry(file
)->d_inode
;
1438 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1439 struct file
*src_file
;
1441 struct btrfs_trans_handle
*trans
;
1442 struct btrfs_path
*path
;
1443 struct extent_buffer
*leaf
;
1445 struct btrfs_key key
;
1450 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1455 * - split compressed inline extents. annoying: we need to
1456 * decompress into destination's address_space (the file offset
1457 * may change, so source mapping won't do), then recompress (or
1458 * otherwise reinsert) a subrange.
1459 * - allow ranges within the same file to be cloned (provided
1460 * they don't overlap)?
1463 /* the destination must be opened for writing */
1464 if (!(file
->f_mode
& FMODE_WRITE
))
1467 ret
= mnt_want_write(file
->f_path
.mnt
);
1471 src_file
= fget(srcfd
);
1474 goto out_drop_write
;
1477 src
= src_file
->f_dentry
->d_inode
;
1483 /* the src must be open for reading */
1484 if (!(src_file
->f_mode
& FMODE_READ
))
1488 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1492 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1496 buf
= vmalloc(btrfs_level_size(root
, 0));
1500 path
= btrfs_alloc_path();
1508 mutex_lock(&inode
->i_mutex
);
1509 mutex_lock(&src
->i_mutex
);
1511 mutex_lock(&src
->i_mutex
);
1512 mutex_lock(&inode
->i_mutex
);
1515 /* determine range to clone */
1517 if (off
>= src
->i_size
|| off
+ len
> src
->i_size
)
1520 olen
= len
= src
->i_size
- off
;
1521 /* if we extend to eof, continue to block boundary */
1522 if (off
+ len
== src
->i_size
)
1523 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
1526 /* verify the end result is block aligned */
1527 if ((off
& (bs
-1)) ||
1528 ((off
+ len
) & (bs
-1)))
1531 /* do any pending delalloc/csum calc on src, one way or
1532 another, and lock file content */
1534 struct btrfs_ordered_extent
*ordered
;
1535 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1536 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
1537 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
1539 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1541 btrfs_put_ordered_extent(ordered
);
1542 btrfs_wait_ordered_range(src
, off
, off
+len
);
1546 key
.objectid
= src
->i_ino
;
1547 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1552 * note the key will change type as we walk through the
1555 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1559 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1560 if (path
->slots
[0] >= nritems
) {
1561 ret
= btrfs_next_leaf(root
, path
);
1566 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1568 leaf
= path
->nodes
[0];
1569 slot
= path
->slots
[0];
1571 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1572 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1573 key
.objectid
!= src
->i_ino
)
1576 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1577 struct btrfs_file_extent_item
*extent
;
1580 struct btrfs_key new_key
;
1581 u64 disko
= 0, diskl
= 0;
1582 u64 datao
= 0, datal
= 0;
1585 size
= btrfs_item_size_nr(leaf
, slot
);
1586 read_extent_buffer(leaf
, buf
,
1587 btrfs_item_ptr_offset(leaf
, slot
),
1590 extent
= btrfs_item_ptr(leaf
, slot
,
1591 struct btrfs_file_extent_item
);
1592 comp
= btrfs_file_extent_compression(leaf
, extent
);
1593 type
= btrfs_file_extent_type(leaf
, extent
);
1594 if (type
== BTRFS_FILE_EXTENT_REG
||
1595 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1596 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1598 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1600 datao
= btrfs_file_extent_offset(leaf
, extent
);
1601 datal
= btrfs_file_extent_num_bytes(leaf
,
1603 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1604 /* take upper bound, may be compressed */
1605 datal
= btrfs_file_extent_ram_bytes(leaf
,
1608 btrfs_release_path(root
, path
);
1610 if (key
.offset
+ datal
< off
||
1611 key
.offset
>= off
+len
)
1614 memcpy(&new_key
, &key
, sizeof(new_key
));
1615 new_key
.objectid
= inode
->i_ino
;
1616 new_key
.offset
= key
.offset
+ destoff
- off
;
1618 trans
= btrfs_start_transaction(root
, 1);
1619 if (IS_ERR(trans
)) {
1620 ret
= PTR_ERR(trans
);
1624 if (type
== BTRFS_FILE_EXTENT_REG
||
1625 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1626 if (off
> key
.offset
) {
1627 datao
+= off
- key
.offset
;
1628 datal
-= off
- key
.offset
;
1631 if (key
.offset
+ datal
> off
+ len
)
1632 datal
= off
+ len
- key
.offset
;
1634 ret
= btrfs_drop_extents(trans
, inode
,
1636 new_key
.offset
+ datal
,
1640 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1644 leaf
= path
->nodes
[0];
1645 slot
= path
->slots
[0];
1646 write_extent_buffer(leaf
, buf
,
1647 btrfs_item_ptr_offset(leaf
, slot
),
1650 extent
= btrfs_item_ptr(leaf
, slot
,
1651 struct btrfs_file_extent_item
);
1653 /* disko == 0 means it's a hole */
1657 btrfs_set_file_extent_offset(leaf
, extent
,
1659 btrfs_set_file_extent_num_bytes(leaf
, extent
,
1662 inode_add_bytes(inode
, datal
);
1663 ret
= btrfs_inc_extent_ref(trans
, root
,
1665 root
->root_key
.objectid
,
1667 new_key
.offset
- datao
);
1670 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1673 if (off
> key
.offset
) {
1674 skip
= off
- key
.offset
;
1675 new_key
.offset
+= skip
;
1678 if (key
.offset
+ datal
> off
+len
)
1679 trim
= key
.offset
+ datal
- (off
+len
);
1681 if (comp
&& (skip
|| trim
)) {
1683 btrfs_end_transaction(trans
, root
);
1686 size
-= skip
+ trim
;
1687 datal
-= skip
+ trim
;
1689 ret
= btrfs_drop_extents(trans
, inode
,
1691 new_key
.offset
+ datal
,
1695 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1701 btrfs_file_extent_calc_inline_size(0);
1702 memmove(buf
+start
, buf
+start
+skip
,
1706 leaf
= path
->nodes
[0];
1707 slot
= path
->slots
[0];
1708 write_extent_buffer(leaf
, buf
,
1709 btrfs_item_ptr_offset(leaf
, slot
),
1711 inode_add_bytes(inode
, datal
);
1714 btrfs_mark_buffer_dirty(leaf
);
1715 btrfs_release_path(root
, path
);
1717 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1718 if (new_key
.offset
+ datal
> inode
->i_size
)
1719 btrfs_i_size_write(inode
,
1720 new_key
.offset
+ datal
);
1721 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1722 ret
= btrfs_update_inode(trans
, root
, inode
);
1724 btrfs_end_transaction(trans
, root
);
1727 btrfs_release_path(root
, path
);
1732 btrfs_release_path(root
, path
);
1733 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1735 mutex_unlock(&src
->i_mutex
);
1736 mutex_unlock(&inode
->i_mutex
);
1738 btrfs_free_path(path
);
1742 mnt_drop_write(file
->f_path
.mnt
);
1746 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1748 struct btrfs_ioctl_clone_range_args args
;
1750 if (copy_from_user(&args
, argp
, sizeof(args
)))
1752 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1753 args
.src_length
, args
.dest_offset
);
1757 * there are many ways the trans_start and trans_end ioctls can lead
1758 * to deadlocks. They should only be used by applications that
1759 * basically own the machine, and have a very in depth understanding
1760 * of all the possible deadlocks and enospc problems.
1762 static long btrfs_ioctl_trans_start(struct file
*file
)
1764 struct inode
*inode
= fdentry(file
)->d_inode
;
1765 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1766 struct btrfs_trans_handle
*trans
;
1770 if (!capable(CAP_SYS_ADMIN
))
1774 if (file
->private_data
)
1777 ret
= mnt_want_write(file
->f_path
.mnt
);
1781 mutex_lock(&root
->fs_info
->trans_mutex
);
1782 root
->fs_info
->open_ioctl_trans
++;
1783 mutex_unlock(&root
->fs_info
->trans_mutex
);
1786 trans
= btrfs_start_ioctl_transaction(root
, 0);
1790 file
->private_data
= trans
;
1794 mutex_lock(&root
->fs_info
->trans_mutex
);
1795 root
->fs_info
->open_ioctl_trans
--;
1796 mutex_unlock(&root
->fs_info
->trans_mutex
);
1797 mnt_drop_write(file
->f_path
.mnt
);
1802 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
1804 struct inode
*inode
= fdentry(file
)->d_inode
;
1805 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1806 struct btrfs_root
*new_root
;
1807 struct btrfs_dir_item
*di
;
1808 struct btrfs_trans_handle
*trans
;
1809 struct btrfs_path
*path
;
1810 struct btrfs_key location
;
1811 struct btrfs_disk_key disk_key
;
1812 struct btrfs_super_block
*disk_super
;
1817 if (!capable(CAP_SYS_ADMIN
))
1820 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
1824 objectid
= root
->root_key
.objectid
;
1826 location
.objectid
= objectid
;
1827 location
.type
= BTRFS_ROOT_ITEM_KEY
;
1828 location
.offset
= (u64
)-1;
1830 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
1831 if (IS_ERR(new_root
))
1832 return PTR_ERR(new_root
);
1834 if (btrfs_root_refs(&new_root
->root_item
) == 0)
1837 path
= btrfs_alloc_path();
1840 path
->leave_spinning
= 1;
1842 trans
= btrfs_start_transaction(root
, 1);
1844 btrfs_free_path(path
);
1848 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
1849 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
1850 dir_id
, "default", 7, 1);
1852 btrfs_free_path(path
);
1853 btrfs_end_transaction(trans
, root
);
1854 printk(KERN_ERR
"Umm, you don't have the default dir item, "
1855 "this isn't going to work\n");
1859 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
1860 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
1861 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1862 btrfs_free_path(path
);
1864 disk_super
= &root
->fs_info
->super_copy
;
1865 features
= btrfs_super_incompat_flags(disk_super
);
1866 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
1867 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
1868 btrfs_set_super_incompat_flags(disk_super
, features
);
1870 btrfs_end_transaction(trans
, root
);
1875 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
1877 struct btrfs_ioctl_space_args space_args
;
1878 struct btrfs_ioctl_space_info space
;
1879 struct btrfs_ioctl_space_info
*dest
;
1880 struct btrfs_ioctl_space_info
*dest_orig
;
1881 struct btrfs_ioctl_space_info
*user_dest
;
1882 struct btrfs_space_info
*info
;
1887 if (copy_from_user(&space_args
,
1888 (struct btrfs_ioctl_space_args __user
*)arg
,
1889 sizeof(space_args
)))
1892 /* first we count slots */
1894 list_for_each_entry_rcu(info
, &root
->fs_info
->space_info
, list
)
1898 /* space_slots == 0 means they are asking for a count */
1899 if (space_args
.space_slots
== 0) {
1900 space_args
.total_spaces
= slot_count
;
1903 alloc_size
= sizeof(*dest
) * slot_count
;
1904 /* we generally have at most 6 or so space infos, one for each raid
1905 * level. So, a whole page should be more than enough for everyone
1907 if (alloc_size
> PAGE_CACHE_SIZE
)
1910 space_args
.total_spaces
= 0;
1911 dest
= kmalloc(alloc_size
, GFP_NOFS
);
1916 /* now we have a buffer to copy into */
1918 list_for_each_entry_rcu(info
, &root
->fs_info
->space_info
, list
) {
1919 /* make sure we don't copy more than we allocated
1922 if (slot_count
== 0)
1926 /* make sure userland has enough room in their buffer */
1927 if (space_args
.total_spaces
>= space_args
.space_slots
)
1930 space
.flags
= info
->flags
;
1931 space
.total_bytes
= info
->total_bytes
;
1932 space
.used_bytes
= info
->bytes_used
;
1933 memcpy(dest
, &space
, sizeof(space
));
1935 space_args
.total_spaces
++;
1939 user_dest
= (struct btrfs_ioctl_space_info
*)
1940 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
1942 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
1947 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
1954 * there are many ways the trans_start and trans_end ioctls can lead
1955 * to deadlocks. They should only be used by applications that
1956 * basically own the machine, and have a very in depth understanding
1957 * of all the possible deadlocks and enospc problems.
1959 long btrfs_ioctl_trans_end(struct file
*file
)
1961 struct inode
*inode
= fdentry(file
)->d_inode
;
1962 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1963 struct btrfs_trans_handle
*trans
;
1965 trans
= file
->private_data
;
1968 file
->private_data
= NULL
;
1970 btrfs_end_transaction(trans
, root
);
1972 mutex_lock(&root
->fs_info
->trans_mutex
);
1973 root
->fs_info
->open_ioctl_trans
--;
1974 mutex_unlock(&root
->fs_info
->trans_mutex
);
1976 mnt_drop_write(file
->f_path
.mnt
);
1980 long btrfs_ioctl(struct file
*file
, unsigned int
1981 cmd
, unsigned long arg
)
1983 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1984 void __user
*argp
= (void __user
*)arg
;
1987 case FS_IOC_GETFLAGS
:
1988 return btrfs_ioctl_getflags(file
, argp
);
1989 case FS_IOC_SETFLAGS
:
1990 return btrfs_ioctl_setflags(file
, argp
);
1991 case FS_IOC_GETVERSION
:
1992 return btrfs_ioctl_getversion(file
, argp
);
1993 case BTRFS_IOC_SNAP_CREATE
:
1994 return btrfs_ioctl_snap_create(file
, argp
, 0);
1995 case BTRFS_IOC_SUBVOL_CREATE
:
1996 return btrfs_ioctl_snap_create(file
, argp
, 1);
1997 case BTRFS_IOC_SNAP_DESTROY
:
1998 return btrfs_ioctl_snap_destroy(file
, argp
);
1999 case BTRFS_IOC_DEFAULT_SUBVOL
:
2000 return btrfs_ioctl_default_subvol(file
, argp
);
2001 case BTRFS_IOC_DEFRAG
:
2002 return btrfs_ioctl_defrag(file
, NULL
);
2003 case BTRFS_IOC_DEFRAG_RANGE
:
2004 return btrfs_ioctl_defrag(file
, argp
);
2005 case BTRFS_IOC_RESIZE
:
2006 return btrfs_ioctl_resize(root
, argp
);
2007 case BTRFS_IOC_ADD_DEV
:
2008 return btrfs_ioctl_add_dev(root
, argp
);
2009 case BTRFS_IOC_RM_DEV
:
2010 return btrfs_ioctl_rm_dev(root
, argp
);
2011 case BTRFS_IOC_BALANCE
:
2012 return btrfs_balance(root
->fs_info
->dev_root
);
2013 case BTRFS_IOC_CLONE
:
2014 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2015 case BTRFS_IOC_CLONE_RANGE
:
2016 return btrfs_ioctl_clone_range(file
, argp
);
2017 case BTRFS_IOC_TRANS_START
:
2018 return btrfs_ioctl_trans_start(file
);
2019 case BTRFS_IOC_TRANS_END
:
2020 return btrfs_ioctl_trans_end(file
);
2021 case BTRFS_IOC_TREE_SEARCH
:
2022 return btrfs_ioctl_tree_search(file
, argp
);
2023 case BTRFS_IOC_INO_LOOKUP
:
2024 return btrfs_ioctl_ino_lookup(file
, argp
);
2025 case BTRFS_IOC_SPACE_INFO
:
2026 return btrfs_ioctl_space_info(root
, argp
);
2027 case BTRFS_IOC_SYNC
:
2028 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);