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_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
594 if (inode
->i_size
== 0 ||
595 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
597 goto err_reservations
;
600 page
= grab_cache_page(inode
->i_mapping
, i
);
603 goto err_reservations
;
606 if (!PageUptodate(page
)) {
607 btrfs_readpage(NULL
, page
);
609 if (!PageUptodate(page
)) {
611 page_cache_release(page
);
613 goto err_reservations
;
617 if (page
->mapping
!= inode
->i_mapping
) {
619 page_cache_release(page
);
623 wait_on_page_writeback(page
);
625 if (PageDirty(page
)) {
626 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
630 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
631 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
632 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
634 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
636 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
638 page_cache_release(page
);
639 btrfs_start_ordered_extent(inode
, ordered
, 1);
640 btrfs_put_ordered_extent(ordered
);
643 set_page_extent_mapped(page
);
646 * this makes sure page_mkwrite is called on the
647 * page if it is dirtied again later
649 clear_page_dirty_for_io(page
);
650 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
651 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
652 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
654 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
655 ClearPageChecked(page
);
656 set_page_dirty(page
);
657 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
661 page_cache_release(page
);
662 mutex_unlock(&inode
->i_mutex
);
664 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
668 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
669 filemap_flush(inode
->i_mapping
);
671 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
672 /* the filemap_flush will queue IO into the worker threads, but
673 * we have to make sure the IO is actually started and that
674 * ordered extents get created before we return
676 atomic_inc(&root
->fs_info
->async_submit_draining
);
677 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
678 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
679 wait_event(root
->fs_info
->async_submit_wait
,
680 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
681 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
683 atomic_dec(&root
->fs_info
->async_submit_draining
);
685 mutex_lock(&inode
->i_mutex
);
686 BTRFS_I(inode
)->force_compress
= 0;
687 mutex_unlock(&inode
->i_mutex
);
693 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
695 mutex_unlock(&inode
->i_mutex
);
699 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
705 struct btrfs_ioctl_vol_args
*vol_args
;
706 struct btrfs_trans_handle
*trans
;
707 struct btrfs_device
*device
= NULL
;
714 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
717 if (!capable(CAP_SYS_ADMIN
))
720 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
721 if (IS_ERR(vol_args
))
722 return PTR_ERR(vol_args
);
724 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
725 namelen
= strlen(vol_args
->name
);
727 mutex_lock(&root
->fs_info
->volume_mutex
);
728 sizestr
= vol_args
->name
;
729 devstr
= strchr(sizestr
, ':');
732 sizestr
= devstr
+ 1;
734 devstr
= vol_args
->name
;
735 devid
= simple_strtoull(devstr
, &end
, 10);
736 printk(KERN_INFO
"resizing devid %llu\n",
737 (unsigned long long)devid
);
739 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
741 printk(KERN_INFO
"resizer unable to find device %llu\n",
742 (unsigned long long)devid
);
746 if (!strcmp(sizestr
, "max"))
747 new_size
= device
->bdev
->bd_inode
->i_size
;
749 if (sizestr
[0] == '-') {
752 } else if (sizestr
[0] == '+') {
756 new_size
= memparse(sizestr
, NULL
);
763 old_size
= device
->total_bytes
;
766 if (new_size
> old_size
) {
770 new_size
= old_size
- new_size
;
771 } else if (mod
> 0) {
772 new_size
= old_size
+ new_size
;
775 if (new_size
< 256 * 1024 * 1024) {
779 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
784 do_div(new_size
, root
->sectorsize
);
785 new_size
*= root
->sectorsize
;
787 printk(KERN_INFO
"new size for %s is %llu\n",
788 device
->name
, (unsigned long long)new_size
);
790 if (new_size
> old_size
) {
791 trans
= btrfs_start_transaction(root
, 0);
792 ret
= btrfs_grow_device(trans
, device
, new_size
);
793 btrfs_commit_transaction(trans
, root
);
795 ret
= btrfs_shrink_device(device
, new_size
);
799 mutex_unlock(&root
->fs_info
->volume_mutex
);
804 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
805 void __user
*arg
, int subvol
)
807 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
808 struct btrfs_ioctl_vol_args
*vol_args
;
809 struct file
*src_file
;
813 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
816 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
817 if (IS_ERR(vol_args
))
818 return PTR_ERR(vol_args
);
820 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
821 namelen
= strlen(vol_args
->name
);
822 if (strchr(vol_args
->name
, '/')) {
828 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
831 struct inode
*src_inode
;
832 src_file
= fget(vol_args
->fd
);
838 src_inode
= src_file
->f_path
.dentry
->d_inode
;
839 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
840 printk(KERN_INFO
"btrfs: Snapshot src from "
846 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
847 BTRFS_I(src_inode
)->root
);
856 * helper to check if the subvolume references other subvolumes
858 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
860 struct btrfs_path
*path
;
861 struct btrfs_key key
;
864 path
= btrfs_alloc_path();
868 key
.objectid
= root
->root_key
.objectid
;
869 key
.type
= BTRFS_ROOT_REF_KEY
;
870 key
.offset
= (u64
)-1;
872 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
879 if (path
->slots
[0] > 0) {
881 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
882 if (key
.objectid
== root
->root_key
.objectid
&&
883 key
.type
== BTRFS_ROOT_REF_KEY
)
887 btrfs_free_path(path
);
891 static noinline
int key_in_sk(struct btrfs_key
*key
,
892 struct btrfs_ioctl_search_key
*sk
)
894 struct btrfs_key test
;
897 test
.objectid
= sk
->min_objectid
;
898 test
.type
= sk
->min_type
;
899 test
.offset
= sk
->min_offset
;
901 ret
= btrfs_comp_cpu_keys(key
, &test
);
905 test
.objectid
= sk
->max_objectid
;
906 test
.type
= sk
->max_type
;
907 test
.offset
= sk
->max_offset
;
909 ret
= btrfs_comp_cpu_keys(key
, &test
);
915 static noinline
int copy_to_sk(struct btrfs_root
*root
,
916 struct btrfs_path
*path
,
917 struct btrfs_key
*key
,
918 struct btrfs_ioctl_search_key
*sk
,
920 unsigned long *sk_offset
,
924 struct extent_buffer
*leaf
;
925 struct btrfs_ioctl_search_header sh
;
926 unsigned long item_off
;
927 unsigned long item_len
;
934 leaf
= path
->nodes
[0];
935 slot
= path
->slots
[0];
936 nritems
= btrfs_header_nritems(leaf
);
938 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
942 found_transid
= btrfs_header_generation(leaf
);
944 for (i
= slot
; i
< nritems
; i
++) {
945 item_off
= btrfs_item_ptr_offset(leaf
, i
);
946 item_len
= btrfs_item_size_nr(leaf
, i
);
948 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
951 if (sizeof(sh
) + item_len
+ *sk_offset
>
952 BTRFS_SEARCH_ARGS_BUFSIZE
) {
957 btrfs_item_key_to_cpu(leaf
, key
, i
);
958 if (!key_in_sk(key
, sk
))
961 sh
.objectid
= key
->objectid
;
962 sh
.offset
= key
->offset
;
965 sh
.transid
= found_transid
;
967 /* copy search result header */
968 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
969 *sk_offset
+= sizeof(sh
);
972 char *p
= buf
+ *sk_offset
;
974 read_extent_buffer(leaf
, p
,
976 *sk_offset
+= item_len
;
980 if (*num_found
>= sk
->nr_items
)
985 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
987 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
990 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1001 static noinline
int search_ioctl(struct inode
*inode
,
1002 struct btrfs_ioctl_search_args
*args
)
1004 struct btrfs_root
*root
;
1005 struct btrfs_key key
;
1006 struct btrfs_key max_key
;
1007 struct btrfs_path
*path
;
1008 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1009 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1012 unsigned long sk_offset
= 0;
1014 path
= btrfs_alloc_path();
1018 if (sk
->tree_id
== 0) {
1019 /* search the root of the inode that was passed */
1020 root
= BTRFS_I(inode
)->root
;
1022 key
.objectid
= sk
->tree_id
;
1023 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1024 key
.offset
= (u64
)-1;
1025 root
= btrfs_read_fs_root_no_name(info
, &key
);
1027 printk(KERN_ERR
"could not find root %llu\n",
1029 btrfs_free_path(path
);
1034 key
.objectid
= sk
->min_objectid
;
1035 key
.type
= sk
->min_type
;
1036 key
.offset
= sk
->min_offset
;
1038 max_key
.objectid
= sk
->max_objectid
;
1039 max_key
.type
= sk
->max_type
;
1040 max_key
.offset
= sk
->max_offset
;
1042 path
->keep_locks
= 1;
1045 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1052 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1053 &sk_offset
, &num_found
);
1054 btrfs_release_path(root
, path
);
1055 if (ret
|| num_found
>= sk
->nr_items
)
1061 sk
->nr_items
= num_found
;
1062 btrfs_free_path(path
);
1066 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1069 struct btrfs_ioctl_search_args
*args
;
1070 struct inode
*inode
;
1073 if (!capable(CAP_SYS_ADMIN
))
1076 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
1080 if (copy_from_user(args
, argp
, sizeof(*args
))) {
1084 inode
= fdentry(file
)->d_inode
;
1085 ret
= search_ioctl(inode
, args
);
1086 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1093 * Search INODE_REFs to identify path name of 'dirid' directory
1094 * in a 'tree_id' tree. and sets path name to 'name'.
1096 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1097 u64 tree_id
, u64 dirid
, char *name
)
1099 struct btrfs_root
*root
;
1100 struct btrfs_key key
;
1106 struct btrfs_inode_ref
*iref
;
1107 struct extent_buffer
*l
;
1108 struct btrfs_path
*path
;
1110 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1115 path
= btrfs_alloc_path();
1119 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1121 key
.objectid
= tree_id
;
1122 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1123 key
.offset
= (u64
)-1;
1124 root
= btrfs_read_fs_root_no_name(info
, &key
);
1126 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1131 key
.objectid
= dirid
;
1132 key
.type
= BTRFS_INODE_REF_KEY
;
1133 key
.offset
= (u64
)-1;
1136 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1141 slot
= path
->slots
[0];
1142 if (ret
> 0 && slot
> 0)
1144 btrfs_item_key_to_cpu(l
, &key
, slot
);
1146 if (ret
> 0 && (key
.objectid
!= dirid
||
1147 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1152 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1153 len
= btrfs_inode_ref_name_len(l
, iref
);
1155 total_len
+= len
+ 1;
1160 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1162 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1165 btrfs_release_path(root
, path
);
1166 key
.objectid
= key
.offset
;
1167 key
.offset
= (u64
)-1;
1168 dirid
= key
.objectid
;
1173 memcpy(name
, ptr
, total_len
);
1174 name
[total_len
]='\0';
1177 btrfs_free_path(path
);
1181 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1184 struct btrfs_ioctl_ino_lookup_args
*args
;
1185 struct inode
*inode
;
1188 if (!capable(CAP_SYS_ADMIN
))
1191 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
1195 if (copy_from_user(args
, argp
, sizeof(*args
))) {
1199 inode
= fdentry(file
)->d_inode
;
1201 if (args
->treeid
== 0)
1202 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1204 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1205 args
->treeid
, args
->objectid
,
1208 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1215 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1218 struct dentry
*parent
= fdentry(file
);
1219 struct dentry
*dentry
;
1220 struct inode
*dir
= parent
->d_inode
;
1221 struct inode
*inode
;
1222 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1223 struct btrfs_root
*dest
= NULL
;
1224 struct btrfs_ioctl_vol_args
*vol_args
;
1225 struct btrfs_trans_handle
*trans
;
1230 if (!capable(CAP_SYS_ADMIN
))
1233 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1234 if (IS_ERR(vol_args
))
1235 return PTR_ERR(vol_args
);
1237 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1238 namelen
= strlen(vol_args
->name
);
1239 if (strchr(vol_args
->name
, '/') ||
1240 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1245 err
= mnt_want_write(file
->f_path
.mnt
);
1249 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1250 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1251 if (IS_ERR(dentry
)) {
1252 err
= PTR_ERR(dentry
);
1253 goto out_unlock_dir
;
1256 if (!dentry
->d_inode
) {
1261 inode
= dentry
->d_inode
;
1262 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1267 dest
= BTRFS_I(inode
)->root
;
1269 mutex_lock(&inode
->i_mutex
);
1270 err
= d_invalidate(dentry
);
1274 down_write(&root
->fs_info
->subvol_sem
);
1276 err
= may_destroy_subvol(dest
);
1280 trans
= btrfs_start_transaction(root
, 0);
1281 if (IS_ERR(trans
)) {
1282 err
= PTR_ERR(trans
);
1285 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1287 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1288 dest
->root_key
.objectid
,
1289 dentry
->d_name
.name
,
1290 dentry
->d_name
.len
);
1293 btrfs_record_root_in_trans(trans
, dest
);
1295 memset(&dest
->root_item
.drop_progress
, 0,
1296 sizeof(dest
->root_item
.drop_progress
));
1297 dest
->root_item
.drop_level
= 0;
1298 btrfs_set_root_refs(&dest
->root_item
, 0);
1300 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1301 ret
= btrfs_insert_orphan_item(trans
,
1302 root
->fs_info
->tree_root
,
1303 dest
->root_key
.objectid
);
1307 ret
= btrfs_commit_transaction(trans
, root
);
1309 inode
->i_flags
|= S_DEAD
;
1311 up_write(&root
->fs_info
->subvol_sem
);
1313 mutex_unlock(&inode
->i_mutex
);
1315 shrink_dcache_sb(root
->fs_info
->sb
);
1316 btrfs_invalidate_inodes(dest
);
1322 mutex_unlock(&dir
->i_mutex
);
1323 mnt_drop_write(file
->f_path
.mnt
);
1329 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1331 struct inode
*inode
= fdentry(file
)->d_inode
;
1332 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1333 struct btrfs_ioctl_defrag_range_args
*range
;
1336 ret
= mnt_want_write(file
->f_path
.mnt
);
1340 switch (inode
->i_mode
& S_IFMT
) {
1342 if (!capable(CAP_SYS_ADMIN
)) {
1346 ret
= btrfs_defrag_root(root
, 0);
1349 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1352 if (!(file
->f_mode
& FMODE_WRITE
)) {
1357 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1364 if (copy_from_user(range
, argp
,
1370 /* compression requires us to start the IO */
1371 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1372 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1373 range
->extent_thresh
= (u32
)-1;
1376 /* the rest are all set to zero by kzalloc */
1377 range
->len
= (u64
)-1;
1379 ret
= btrfs_defrag_file(file
, range
);
1386 mnt_drop_write(file
->f_path
.mnt
);
1390 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1392 struct btrfs_ioctl_vol_args
*vol_args
;
1395 if (!capable(CAP_SYS_ADMIN
))
1398 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1399 if (IS_ERR(vol_args
))
1400 return PTR_ERR(vol_args
);
1402 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1403 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1409 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1411 struct btrfs_ioctl_vol_args
*vol_args
;
1414 if (!capable(CAP_SYS_ADMIN
))
1417 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1420 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1421 if (IS_ERR(vol_args
))
1422 return PTR_ERR(vol_args
);
1424 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1425 ret
= btrfs_rm_device(root
, vol_args
->name
);
1431 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1432 u64 off
, u64 olen
, u64 destoff
)
1434 struct inode
*inode
= fdentry(file
)->d_inode
;
1435 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1436 struct file
*src_file
;
1438 struct btrfs_trans_handle
*trans
;
1439 struct btrfs_path
*path
;
1440 struct extent_buffer
*leaf
;
1442 struct btrfs_key key
;
1447 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1452 * - split compressed inline extents. annoying: we need to
1453 * decompress into destination's address_space (the file offset
1454 * may change, so source mapping won't do), then recompress (or
1455 * otherwise reinsert) a subrange.
1456 * - allow ranges within the same file to be cloned (provided
1457 * they don't overlap)?
1460 /* the destination must be opened for writing */
1461 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
1464 ret
= mnt_want_write(file
->f_path
.mnt
);
1468 src_file
= fget(srcfd
);
1471 goto out_drop_write
;
1474 src
= src_file
->f_dentry
->d_inode
;
1480 /* the src must be open for reading */
1481 if (!(src_file
->f_mode
& FMODE_READ
))
1485 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1489 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1493 buf
= vmalloc(btrfs_level_size(root
, 0));
1497 path
= btrfs_alloc_path();
1505 mutex_lock(&inode
->i_mutex
);
1506 mutex_lock(&src
->i_mutex
);
1508 mutex_lock(&src
->i_mutex
);
1509 mutex_lock(&inode
->i_mutex
);
1512 /* determine range to clone */
1514 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
1517 olen
= len
= src
->i_size
- off
;
1518 /* if we extend to eof, continue to block boundary */
1519 if (off
+ len
== src
->i_size
)
1520 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
1523 /* verify the end result is block aligned */
1524 if ((off
& (bs
-1)) ||
1525 ((off
+ len
) & (bs
-1)))
1528 /* do any pending delalloc/csum calc on src, one way or
1529 another, and lock file content */
1531 struct btrfs_ordered_extent
*ordered
;
1532 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1533 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
1534 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
1536 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1538 btrfs_put_ordered_extent(ordered
);
1539 btrfs_wait_ordered_range(src
, off
, off
+len
);
1543 key
.objectid
= src
->i_ino
;
1544 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1549 * note the key will change type as we walk through the
1552 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1556 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1557 if (path
->slots
[0] >= nritems
) {
1558 ret
= btrfs_next_leaf(root
, path
);
1563 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1565 leaf
= path
->nodes
[0];
1566 slot
= path
->slots
[0];
1568 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1569 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1570 key
.objectid
!= src
->i_ino
)
1573 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1574 struct btrfs_file_extent_item
*extent
;
1577 struct btrfs_key new_key
;
1578 u64 disko
= 0, diskl
= 0;
1579 u64 datao
= 0, datal
= 0;
1583 size
= btrfs_item_size_nr(leaf
, slot
);
1584 read_extent_buffer(leaf
, buf
,
1585 btrfs_item_ptr_offset(leaf
, slot
),
1588 extent
= btrfs_item_ptr(leaf
, slot
,
1589 struct btrfs_file_extent_item
);
1590 comp
= btrfs_file_extent_compression(leaf
, extent
);
1591 type
= btrfs_file_extent_type(leaf
, extent
);
1592 if (type
== BTRFS_FILE_EXTENT_REG
||
1593 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1594 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1596 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1598 datao
= btrfs_file_extent_offset(leaf
, extent
);
1599 datal
= btrfs_file_extent_num_bytes(leaf
,
1601 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1602 /* take upper bound, may be compressed */
1603 datal
= btrfs_file_extent_ram_bytes(leaf
,
1606 btrfs_release_path(root
, path
);
1608 if (key
.offset
+ datal
< off
||
1609 key
.offset
>= off
+len
)
1612 memcpy(&new_key
, &key
, sizeof(new_key
));
1613 new_key
.objectid
= inode
->i_ino
;
1614 new_key
.offset
= key
.offset
+ destoff
- off
;
1616 trans
= btrfs_start_transaction(root
, 1);
1617 if (IS_ERR(trans
)) {
1618 ret
= PTR_ERR(trans
);
1622 if (type
== BTRFS_FILE_EXTENT_REG
||
1623 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1624 if (off
> key
.offset
) {
1625 datao
+= off
- key
.offset
;
1626 datal
-= off
- key
.offset
;
1629 if (key
.offset
+ datal
> off
+ len
)
1630 datal
= off
+ len
- key
.offset
;
1632 ret
= btrfs_drop_extents(trans
, inode
,
1634 new_key
.offset
+ datal
,
1638 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1642 leaf
= path
->nodes
[0];
1643 slot
= path
->slots
[0];
1644 write_extent_buffer(leaf
, buf
,
1645 btrfs_item_ptr_offset(leaf
, slot
),
1648 extent
= btrfs_item_ptr(leaf
, slot
,
1649 struct btrfs_file_extent_item
);
1651 /* disko == 0 means it's a hole */
1655 btrfs_set_file_extent_offset(leaf
, extent
,
1657 btrfs_set_file_extent_num_bytes(leaf
, extent
,
1660 inode_add_bytes(inode
, datal
);
1661 ret
= btrfs_inc_extent_ref(trans
, root
,
1663 root
->root_key
.objectid
,
1665 new_key
.offset
- datao
);
1668 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1671 if (off
> key
.offset
) {
1672 skip
= off
- key
.offset
;
1673 new_key
.offset
+= skip
;
1676 if (key
.offset
+ datal
> off
+len
)
1677 trim
= key
.offset
+ datal
- (off
+len
);
1679 if (comp
&& (skip
|| trim
)) {
1681 btrfs_end_transaction(trans
, root
);
1684 size
-= skip
+ trim
;
1685 datal
-= skip
+ trim
;
1687 ret
= btrfs_drop_extents(trans
, inode
,
1689 new_key
.offset
+ datal
,
1693 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1699 btrfs_file_extent_calc_inline_size(0);
1700 memmove(buf
+start
, buf
+start
+skip
,
1704 leaf
= path
->nodes
[0];
1705 slot
= path
->slots
[0];
1706 write_extent_buffer(leaf
, buf
,
1707 btrfs_item_ptr_offset(leaf
, slot
),
1709 inode_add_bytes(inode
, datal
);
1712 btrfs_mark_buffer_dirty(leaf
);
1713 btrfs_release_path(root
, path
);
1715 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1718 * we round up to the block size at eof when
1719 * determining which extents to clone above,
1720 * but shouldn't round up the file size
1722 endoff
= new_key
.offset
+ datal
;
1723 if (endoff
> off
+olen
)
1725 if (endoff
> inode
->i_size
)
1726 btrfs_i_size_write(inode
, endoff
);
1728 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1729 ret
= btrfs_update_inode(trans
, root
, inode
);
1731 btrfs_end_transaction(trans
, root
);
1734 btrfs_release_path(root
, path
);
1739 btrfs_release_path(root
, path
);
1740 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1742 mutex_unlock(&src
->i_mutex
);
1743 mutex_unlock(&inode
->i_mutex
);
1745 btrfs_free_path(path
);
1749 mnt_drop_write(file
->f_path
.mnt
);
1753 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1755 struct btrfs_ioctl_clone_range_args args
;
1757 if (copy_from_user(&args
, argp
, sizeof(args
)))
1759 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1760 args
.src_length
, args
.dest_offset
);
1764 * there are many ways the trans_start and trans_end ioctls can lead
1765 * to deadlocks. They should only be used by applications that
1766 * basically own the machine, and have a very in depth understanding
1767 * of all the possible deadlocks and enospc problems.
1769 static long btrfs_ioctl_trans_start(struct file
*file
)
1771 struct inode
*inode
= fdentry(file
)->d_inode
;
1772 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1773 struct btrfs_trans_handle
*trans
;
1777 if (!capable(CAP_SYS_ADMIN
))
1781 if (file
->private_data
)
1784 ret
= mnt_want_write(file
->f_path
.mnt
);
1788 mutex_lock(&root
->fs_info
->trans_mutex
);
1789 root
->fs_info
->open_ioctl_trans
++;
1790 mutex_unlock(&root
->fs_info
->trans_mutex
);
1793 trans
= btrfs_start_ioctl_transaction(root
, 0);
1797 file
->private_data
= trans
;
1801 mutex_lock(&root
->fs_info
->trans_mutex
);
1802 root
->fs_info
->open_ioctl_trans
--;
1803 mutex_unlock(&root
->fs_info
->trans_mutex
);
1804 mnt_drop_write(file
->f_path
.mnt
);
1809 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
1811 struct inode
*inode
= fdentry(file
)->d_inode
;
1812 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1813 struct btrfs_root
*new_root
;
1814 struct btrfs_dir_item
*di
;
1815 struct btrfs_trans_handle
*trans
;
1816 struct btrfs_path
*path
;
1817 struct btrfs_key location
;
1818 struct btrfs_disk_key disk_key
;
1819 struct btrfs_super_block
*disk_super
;
1824 if (!capable(CAP_SYS_ADMIN
))
1827 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
1831 objectid
= root
->root_key
.objectid
;
1833 location
.objectid
= objectid
;
1834 location
.type
= BTRFS_ROOT_ITEM_KEY
;
1835 location
.offset
= (u64
)-1;
1837 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
1838 if (IS_ERR(new_root
))
1839 return PTR_ERR(new_root
);
1841 if (btrfs_root_refs(&new_root
->root_item
) == 0)
1844 path
= btrfs_alloc_path();
1847 path
->leave_spinning
= 1;
1849 trans
= btrfs_start_transaction(root
, 1);
1851 btrfs_free_path(path
);
1855 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
1856 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
1857 dir_id
, "default", 7, 1);
1858 if (IS_ERR_OR_NULL(di
)) {
1859 btrfs_free_path(path
);
1860 btrfs_end_transaction(trans
, root
);
1861 printk(KERN_ERR
"Umm, you don't have the default dir item, "
1862 "this isn't going to work\n");
1866 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
1867 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
1868 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1869 btrfs_free_path(path
);
1871 disk_super
= &root
->fs_info
->super_copy
;
1872 features
= btrfs_super_incompat_flags(disk_super
);
1873 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
1874 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
1875 btrfs_set_super_incompat_flags(disk_super
, features
);
1877 btrfs_end_transaction(trans
, root
);
1882 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
1884 struct btrfs_ioctl_space_args space_args
;
1885 struct btrfs_ioctl_space_info space
;
1886 struct btrfs_ioctl_space_info
*dest
;
1887 struct btrfs_ioctl_space_info
*dest_orig
;
1888 struct btrfs_ioctl_space_info
*user_dest
;
1889 struct btrfs_space_info
*info
;
1894 if (copy_from_user(&space_args
,
1895 (struct btrfs_ioctl_space_args __user
*)arg
,
1896 sizeof(space_args
)))
1899 /* first we count slots */
1901 list_for_each_entry_rcu(info
, &root
->fs_info
->space_info
, list
)
1905 /* space_slots == 0 means they are asking for a count */
1906 if (space_args
.space_slots
== 0) {
1907 space_args
.total_spaces
= slot_count
;
1910 alloc_size
= sizeof(*dest
) * slot_count
;
1911 /* we generally have at most 6 or so space infos, one for each raid
1912 * level. So, a whole page should be more than enough for everyone
1914 if (alloc_size
> PAGE_CACHE_SIZE
)
1917 space_args
.total_spaces
= 0;
1918 dest
= kmalloc(alloc_size
, GFP_NOFS
);
1923 /* now we have a buffer to copy into */
1925 list_for_each_entry_rcu(info
, &root
->fs_info
->space_info
, list
) {
1926 /* make sure we don't copy more than we allocated
1929 if (slot_count
== 0)
1933 /* make sure userland has enough room in their buffer */
1934 if (space_args
.total_spaces
>= space_args
.space_slots
)
1937 space
.flags
= info
->flags
;
1938 space
.total_bytes
= info
->total_bytes
;
1939 space
.used_bytes
= info
->bytes_used
;
1940 memcpy(dest
, &space
, sizeof(space
));
1942 space_args
.total_spaces
++;
1946 user_dest
= (struct btrfs_ioctl_space_info
*)
1947 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
1949 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
1954 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
1961 * there are many ways the trans_start and trans_end ioctls can lead
1962 * to deadlocks. They should only be used by applications that
1963 * basically own the machine, and have a very in depth understanding
1964 * of all the possible deadlocks and enospc problems.
1966 long btrfs_ioctl_trans_end(struct file
*file
)
1968 struct inode
*inode
= fdentry(file
)->d_inode
;
1969 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1970 struct btrfs_trans_handle
*trans
;
1972 trans
= file
->private_data
;
1975 file
->private_data
= NULL
;
1977 btrfs_end_transaction(trans
, root
);
1979 mutex_lock(&root
->fs_info
->trans_mutex
);
1980 root
->fs_info
->open_ioctl_trans
--;
1981 mutex_unlock(&root
->fs_info
->trans_mutex
);
1983 mnt_drop_write(file
->f_path
.mnt
);
1987 long btrfs_ioctl(struct file
*file
, unsigned int
1988 cmd
, unsigned long arg
)
1990 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1991 void __user
*argp
= (void __user
*)arg
;
1994 case FS_IOC_GETFLAGS
:
1995 return btrfs_ioctl_getflags(file
, argp
);
1996 case FS_IOC_SETFLAGS
:
1997 return btrfs_ioctl_setflags(file
, argp
);
1998 case FS_IOC_GETVERSION
:
1999 return btrfs_ioctl_getversion(file
, argp
);
2000 case BTRFS_IOC_SNAP_CREATE
:
2001 return btrfs_ioctl_snap_create(file
, argp
, 0);
2002 case BTRFS_IOC_SUBVOL_CREATE
:
2003 return btrfs_ioctl_snap_create(file
, argp
, 1);
2004 case BTRFS_IOC_SNAP_DESTROY
:
2005 return btrfs_ioctl_snap_destroy(file
, argp
);
2006 case BTRFS_IOC_DEFAULT_SUBVOL
:
2007 return btrfs_ioctl_default_subvol(file
, argp
);
2008 case BTRFS_IOC_DEFRAG
:
2009 return btrfs_ioctl_defrag(file
, NULL
);
2010 case BTRFS_IOC_DEFRAG_RANGE
:
2011 return btrfs_ioctl_defrag(file
, argp
);
2012 case BTRFS_IOC_RESIZE
:
2013 return btrfs_ioctl_resize(root
, argp
);
2014 case BTRFS_IOC_ADD_DEV
:
2015 return btrfs_ioctl_add_dev(root
, argp
);
2016 case BTRFS_IOC_RM_DEV
:
2017 return btrfs_ioctl_rm_dev(root
, argp
);
2018 case BTRFS_IOC_BALANCE
:
2019 return btrfs_balance(root
->fs_info
->dev_root
);
2020 case BTRFS_IOC_CLONE
:
2021 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2022 case BTRFS_IOC_CLONE_RANGE
:
2023 return btrfs_ioctl_clone_range(file
, argp
);
2024 case BTRFS_IOC_TRANS_START
:
2025 return btrfs_ioctl_trans_start(file
);
2026 case BTRFS_IOC_TRANS_END
:
2027 return btrfs_ioctl_trans_end(file
);
2028 case BTRFS_IOC_TREE_SEARCH
:
2029 return btrfs_ioctl_tree_search(file
, argp
);
2030 case BTRFS_IOC_INO_LOOKUP
:
2031 return btrfs_ioctl_ino_lookup(file
, argp
);
2032 case BTRFS_IOC_SPACE_INFO
:
2033 return btrfs_ioctl_space_info(root
, argp
);
2034 case BTRFS_IOC_SYNC
:
2035 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);