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>
45 #include "transaction.h"
46 #include "btrfs_inode.h"
48 #include "print-tree.h"
52 /* Mask out flags that are inappropriate for the given type of inode. */
53 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
57 else if (S_ISREG(mode
))
58 return flags
& ~FS_DIRSYNC_FL
;
60 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
64 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
66 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
68 unsigned int iflags
= 0;
70 if (flags
& BTRFS_INODE_SYNC
)
72 if (flags
& BTRFS_INODE_IMMUTABLE
)
73 iflags
|= FS_IMMUTABLE_FL
;
74 if (flags
& BTRFS_INODE_APPEND
)
75 iflags
|= FS_APPEND_FL
;
76 if (flags
& BTRFS_INODE_NODUMP
)
77 iflags
|= FS_NODUMP_FL
;
78 if (flags
& BTRFS_INODE_NOATIME
)
79 iflags
|= FS_NOATIME_FL
;
80 if (flags
& BTRFS_INODE_DIRSYNC
)
81 iflags
|= FS_DIRSYNC_FL
;
87 * Update inode->i_flags based on the btrfs internal flags.
89 void btrfs_update_iflags(struct inode
*inode
)
91 struct btrfs_inode
*ip
= BTRFS_I(inode
);
93 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
95 if (ip
->flags
& BTRFS_INODE_SYNC
)
96 inode
->i_flags
|= S_SYNC
;
97 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
98 inode
->i_flags
|= S_IMMUTABLE
;
99 if (ip
->flags
& BTRFS_INODE_APPEND
)
100 inode
->i_flags
|= S_APPEND
;
101 if (ip
->flags
& BTRFS_INODE_NOATIME
)
102 inode
->i_flags
|= S_NOATIME
;
103 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
104 inode
->i_flags
|= S_DIRSYNC
;
108 * Inherit flags from the parent inode.
110 * Unlike extN we don't have any flags we don't want to inherit currently.
112 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
119 flags
= BTRFS_I(dir
)->flags
;
121 if (S_ISREG(inode
->i_mode
))
122 flags
&= ~BTRFS_INODE_DIRSYNC
;
123 else if (!S_ISDIR(inode
->i_mode
))
124 flags
&= (BTRFS_INODE_NODUMP
| BTRFS_INODE_NOATIME
);
126 BTRFS_I(inode
)->flags
= flags
;
127 btrfs_update_iflags(inode
);
130 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
132 struct btrfs_inode
*ip
= BTRFS_I(file
->f_path
.dentry
->d_inode
);
133 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
135 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
140 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
142 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
143 struct btrfs_inode
*ip
= BTRFS_I(inode
);
144 struct btrfs_root
*root
= ip
->root
;
145 struct btrfs_trans_handle
*trans
;
146 unsigned int flags
, oldflags
;
149 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
152 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
153 FS_NOATIME_FL
| FS_NODUMP_FL
| \
154 FS_SYNC_FL
| FS_DIRSYNC_FL
))
157 if (!is_owner_or_cap(inode
))
160 mutex_lock(&inode
->i_mutex
);
162 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
163 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
164 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
165 if (!capable(CAP_LINUX_IMMUTABLE
)) {
171 ret
= mnt_want_write(file
->f_path
.mnt
);
175 if (flags
& FS_SYNC_FL
)
176 ip
->flags
|= BTRFS_INODE_SYNC
;
178 ip
->flags
&= ~BTRFS_INODE_SYNC
;
179 if (flags
& FS_IMMUTABLE_FL
)
180 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
182 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
183 if (flags
& FS_APPEND_FL
)
184 ip
->flags
|= BTRFS_INODE_APPEND
;
186 ip
->flags
&= ~BTRFS_INODE_APPEND
;
187 if (flags
& FS_NODUMP_FL
)
188 ip
->flags
|= BTRFS_INODE_NODUMP
;
190 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
191 if (flags
& FS_NOATIME_FL
)
192 ip
->flags
|= BTRFS_INODE_NOATIME
;
194 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
195 if (flags
& FS_DIRSYNC_FL
)
196 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
198 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
201 trans
= btrfs_join_transaction(root
, 1);
204 ret
= btrfs_update_inode(trans
, root
, inode
);
207 btrfs_update_iflags(inode
);
208 inode
->i_ctime
= CURRENT_TIME
;
209 btrfs_end_transaction(trans
, root
);
211 mnt_drop_write(file
->f_path
.mnt
);
213 mutex_unlock(&inode
->i_mutex
);
217 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
219 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
221 return put_user(inode
->i_generation
, arg
);
224 static noinline
int create_subvol(struct btrfs_root
*root
,
225 struct dentry
*dentry
,
226 char *name
, int namelen
)
228 struct btrfs_trans_handle
*trans
;
229 struct btrfs_key key
;
230 struct btrfs_root_item root_item
;
231 struct btrfs_inode_item
*inode_item
;
232 struct extent_buffer
*leaf
;
233 struct btrfs_root
*new_root
;
234 struct inode
*dir
= dentry
->d_parent
->d_inode
;
238 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
247 ret
= btrfs_reserve_metadata_space(root
, 6);
251 trans
= btrfs_start_transaction(root
, 1);
254 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
259 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
260 0, objectid
, NULL
, 0, 0, 0);
266 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
267 btrfs_set_header_bytenr(leaf
, leaf
->start
);
268 btrfs_set_header_generation(leaf
, trans
->transid
);
269 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
270 btrfs_set_header_owner(leaf
, objectid
);
272 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
273 (unsigned long)btrfs_header_fsid(leaf
),
275 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
276 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
278 btrfs_mark_buffer_dirty(leaf
);
280 inode_item
= &root_item
.inode
;
281 memset(inode_item
, 0, sizeof(*inode_item
));
282 inode_item
->generation
= cpu_to_le64(1);
283 inode_item
->size
= cpu_to_le64(3);
284 inode_item
->nlink
= cpu_to_le32(1);
285 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
286 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
288 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
289 btrfs_set_root_generation(&root_item
, trans
->transid
);
290 btrfs_set_root_level(&root_item
, 0);
291 btrfs_set_root_refs(&root_item
, 1);
292 btrfs_set_root_used(&root_item
, leaf
->len
);
293 btrfs_set_root_last_snapshot(&root_item
, 0);
295 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
296 root_item
.drop_level
= 0;
298 btrfs_tree_unlock(leaf
);
299 free_extent_buffer(leaf
);
302 btrfs_set_root_dirid(&root_item
, new_dirid
);
304 key
.objectid
= objectid
;
306 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
307 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
312 key
.offset
= (u64
)-1;
313 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
314 BUG_ON(IS_ERR(new_root
));
316 btrfs_record_root_in_trans(trans
, new_root
);
318 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
319 BTRFS_I(dir
)->block_group
);
321 * insert the directory item
323 ret
= btrfs_set_inode_index(dir
, &index
);
326 ret
= btrfs_insert_dir_item(trans
, root
,
327 name
, namelen
, dir
->i_ino
, &key
,
328 BTRFS_FT_DIR
, index
);
332 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
333 ret
= btrfs_update_inode(trans
, root
, dir
);
336 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
337 objectid
, root
->root_key
.objectid
,
338 dir
->i_ino
, index
, name
, namelen
);
342 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
344 err
= btrfs_commit_transaction(trans
, root
);
348 btrfs_unreserve_metadata_space(root
, 6);
352 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
353 char *name
, int namelen
)
356 struct btrfs_pending_snapshot
*pending_snapshot
;
357 struct btrfs_trans_handle
*trans
;
369 ret
= btrfs_reserve_metadata_space(root
, 6);
373 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
374 if (!pending_snapshot
) {
376 btrfs_unreserve_metadata_space(root
, 6);
379 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
380 if (!pending_snapshot
->name
) {
382 kfree(pending_snapshot
);
383 btrfs_unreserve_metadata_space(root
, 6);
386 memcpy(pending_snapshot
->name
, name
, namelen
);
387 pending_snapshot
->name
[namelen
] = '\0';
388 pending_snapshot
->dentry
= dentry
;
389 trans
= btrfs_start_transaction(root
, 1);
391 pending_snapshot
->root
= root
;
392 list_add(&pending_snapshot
->list
,
393 &trans
->transaction
->pending_snapshots
);
394 ret
= btrfs_commit_transaction(trans
, root
);
396 btrfs_unreserve_metadata_space(root
, 6);
398 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
400 ret
= PTR_ERR(inode
);
404 d_instantiate(dentry
, inode
);
410 /* copy of may_create in fs/namei.c() */
411 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
417 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
421 * Create a new subvolume below @parent. This is largely modeled after
422 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
423 * inside this filesystem so it's quite a bit simpler.
425 static noinline
int btrfs_mksubvol(struct path
*parent
,
426 char *name
, int namelen
,
427 struct btrfs_root
*snap_src
)
429 struct inode
*dir
= parent
->dentry
->d_inode
;
430 struct dentry
*dentry
;
433 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
435 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
436 error
= PTR_ERR(dentry
);
444 error
= mnt_want_write(parent
->mnt
);
448 error
= btrfs_may_create(dir
, dentry
);
452 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
454 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
458 error
= create_snapshot(snap_src
, dentry
,
461 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
465 fsnotify_mkdir(dir
, dentry
);
467 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
469 mnt_drop_write(parent
->mnt
);
473 mutex_unlock(&dir
->i_mutex
);
477 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
478 int thresh
, u64
*last_len
, u64
*skip
,
481 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
482 struct extent_map
*em
= NULL
;
483 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
491 * make sure that once we start defragging and extent, we keep on
494 if (start
< *defrag_end
)
500 * hopefully we have this extent in the tree already, try without
501 * the full extent lock
503 read_lock(&em_tree
->lock
);
504 em
= lookup_extent_mapping(em_tree
, start
, len
);
505 read_unlock(&em_tree
->lock
);
508 /* get the big lock and read metadata off disk */
509 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
510 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
511 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
517 /* this will cover holes, and inline extents */
518 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
522 * we hit a real extent, if it is big don't bother defragging it again
524 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
528 * last_len ends up being a counter of how many bytes we've defragged.
529 * every time we choose not to defrag an extent, we reset *last_len
530 * so that the next tiny extent will force a defrag.
532 * The end result of this is that tiny extents before a single big
533 * extent will force at least part of that big extent to be defragged.
537 *defrag_end
= extent_map_end(em
);
540 *skip
= extent_map_end(em
);
548 static int btrfs_defrag_file(struct file
*file
,
549 struct btrfs_ioctl_defrag_range_args
*range
)
551 struct inode
*inode
= fdentry(file
)->d_inode
;
552 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
553 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
554 struct btrfs_ordered_extent
*ordered
;
556 unsigned long last_index
;
557 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
558 unsigned long total_read
= 0;
567 if (inode
->i_size
== 0)
570 if (range
->start
+ range
->len
> range
->start
) {
571 last_index
= min_t(u64
, inode
->i_size
- 1,
572 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
574 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
577 i
= range
->start
>> PAGE_CACHE_SHIFT
;
578 while (i
<= last_index
) {
579 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
581 range
->extent_thresh
,
586 * the should_defrag function tells us how much to skip
587 * bump our counter by the suggested amount
589 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
590 i
= max(i
+ 1, next
);
594 if (total_read
% ra_pages
== 0) {
595 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
596 min(last_index
, i
+ ra_pages
- 1));
599 mutex_lock(&inode
->i_mutex
);
600 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
601 BTRFS_I(inode
)->force_compress
= 1;
603 ret
= btrfs_check_data_free_space(root
, inode
, PAGE_CACHE_SIZE
);
609 ret
= btrfs_reserve_metadata_for_delalloc(root
, inode
, 1);
611 btrfs_free_reserved_data_space(root
, inode
,
617 if (inode
->i_size
== 0 ||
618 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
620 goto err_reservations
;
623 page
= grab_cache_page(inode
->i_mapping
, i
);
625 goto err_reservations
;
627 if (!PageUptodate(page
)) {
628 btrfs_readpage(NULL
, page
);
630 if (!PageUptodate(page
)) {
632 page_cache_release(page
);
633 goto err_reservations
;
637 if (page
->mapping
!= inode
->i_mapping
) {
639 page_cache_release(page
);
643 wait_on_page_writeback(page
);
645 if (PageDirty(page
)) {
646 btrfs_free_reserved_data_space(root
, inode
,
651 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
652 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
653 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
655 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
657 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
659 page_cache_release(page
);
660 btrfs_start_ordered_extent(inode
, ordered
, 1);
661 btrfs_put_ordered_extent(ordered
);
664 set_page_extent_mapped(page
);
667 * this makes sure page_mkwrite is called on the
668 * page if it is dirtied again later
670 clear_page_dirty_for_io(page
);
671 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
672 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
673 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
675 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
676 ClearPageChecked(page
);
677 set_page_dirty(page
);
678 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
682 page_cache_release(page
);
683 mutex_unlock(&inode
->i_mutex
);
685 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
686 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
690 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
691 filemap_flush(inode
->i_mapping
);
693 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
694 /* the filemap_flush will queue IO into the worker threads, but
695 * we have to make sure the IO is actually started and that
696 * ordered extents get created before we return
698 atomic_inc(&root
->fs_info
->async_submit_draining
);
699 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
700 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
701 wait_event(root
->fs_info
->async_submit_wait
,
702 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
703 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
705 atomic_dec(&root
->fs_info
->async_submit_draining
);
707 mutex_lock(&inode
->i_mutex
);
708 BTRFS_I(inode
)->force_compress
= 0;
709 mutex_unlock(&inode
->i_mutex
);
715 mutex_unlock(&inode
->i_mutex
);
716 btrfs_free_reserved_data_space(root
, inode
, PAGE_CACHE_SIZE
);
717 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
721 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
727 struct btrfs_ioctl_vol_args
*vol_args
;
728 struct btrfs_trans_handle
*trans
;
729 struct btrfs_device
*device
= NULL
;
736 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
739 if (!capable(CAP_SYS_ADMIN
))
742 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
743 if (IS_ERR(vol_args
))
744 return PTR_ERR(vol_args
);
746 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
747 namelen
= strlen(vol_args
->name
);
749 mutex_lock(&root
->fs_info
->volume_mutex
);
750 sizestr
= vol_args
->name
;
751 devstr
= strchr(sizestr
, ':');
754 sizestr
= devstr
+ 1;
756 devstr
= vol_args
->name
;
757 devid
= simple_strtoull(devstr
, &end
, 10);
758 printk(KERN_INFO
"resizing devid %llu\n",
759 (unsigned long long)devid
);
761 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
763 printk(KERN_INFO
"resizer unable to find device %llu\n",
764 (unsigned long long)devid
);
768 if (!strcmp(sizestr
, "max"))
769 new_size
= device
->bdev
->bd_inode
->i_size
;
771 if (sizestr
[0] == '-') {
774 } else if (sizestr
[0] == '+') {
778 new_size
= memparse(sizestr
, NULL
);
785 old_size
= device
->total_bytes
;
788 if (new_size
> old_size
) {
792 new_size
= old_size
- new_size
;
793 } else if (mod
> 0) {
794 new_size
= old_size
+ new_size
;
797 if (new_size
< 256 * 1024 * 1024) {
801 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
806 do_div(new_size
, root
->sectorsize
);
807 new_size
*= root
->sectorsize
;
809 printk(KERN_INFO
"new size for %s is %llu\n",
810 device
->name
, (unsigned long long)new_size
);
812 if (new_size
> old_size
) {
813 trans
= btrfs_start_transaction(root
, 1);
814 ret
= btrfs_grow_device(trans
, device
, new_size
);
815 btrfs_commit_transaction(trans
, root
);
817 ret
= btrfs_shrink_device(device
, new_size
);
821 mutex_unlock(&root
->fs_info
->volume_mutex
);
826 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
827 void __user
*arg
, int subvol
)
829 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
830 struct btrfs_ioctl_vol_args
*vol_args
;
831 struct file
*src_file
;
835 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
838 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
839 if (IS_ERR(vol_args
))
840 return PTR_ERR(vol_args
);
842 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
843 namelen
= strlen(vol_args
->name
);
844 if (strchr(vol_args
->name
, '/')) {
850 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
853 struct inode
*src_inode
;
854 src_file
= fget(vol_args
->fd
);
860 src_inode
= src_file
->f_path
.dentry
->d_inode
;
861 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
862 printk(KERN_INFO
"btrfs: Snapshot src from "
868 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
869 BTRFS_I(src_inode
)->root
);
878 * helper to check if the subvolume references other subvolumes
880 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
882 struct btrfs_path
*path
;
883 struct btrfs_key key
;
886 path
= btrfs_alloc_path();
890 key
.objectid
= root
->root_key
.objectid
;
891 key
.type
= BTRFS_ROOT_REF_KEY
;
892 key
.offset
= (u64
)-1;
894 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
901 if (path
->slots
[0] > 0) {
903 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
904 if (key
.objectid
== root
->root_key
.objectid
&&
905 key
.type
== BTRFS_ROOT_REF_KEY
)
909 btrfs_free_path(path
);
913 static noinline
int key_in_sk(struct btrfs_key
*key
,
914 struct btrfs_ioctl_search_key
*sk
)
916 struct btrfs_key test
;
919 test
.objectid
= sk
->min_objectid
;
920 test
.type
= sk
->min_type
;
921 test
.offset
= sk
->min_offset
;
923 ret
= btrfs_comp_cpu_keys(key
, &test
);
927 test
.objectid
= sk
->max_objectid
;
928 test
.type
= sk
->max_type
;
929 test
.offset
= sk
->max_offset
;
931 ret
= btrfs_comp_cpu_keys(key
, &test
);
937 static noinline
int copy_to_sk(struct btrfs_root
*root
,
938 struct btrfs_path
*path
,
939 struct btrfs_key
*key
,
940 struct btrfs_ioctl_search_key
*sk
,
942 unsigned long *sk_offset
,
946 struct extent_buffer
*leaf
;
947 struct btrfs_ioctl_search_header sh
;
948 unsigned long item_off
;
949 unsigned long item_len
;
956 leaf
= path
->nodes
[0];
957 slot
= path
->slots
[0];
958 nritems
= btrfs_header_nritems(leaf
);
960 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
964 found_transid
= btrfs_header_generation(leaf
);
966 for (i
= slot
; i
< nritems
; i
++) {
967 item_off
= btrfs_item_ptr_offset(leaf
, i
);
968 item_len
= btrfs_item_size_nr(leaf
, i
);
970 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
973 if (sizeof(sh
) + item_len
+ *sk_offset
>
974 BTRFS_SEARCH_ARGS_BUFSIZE
) {
979 btrfs_item_key_to_cpu(leaf
, key
, i
);
980 if (!key_in_sk(key
, sk
))
983 sh
.objectid
= key
->objectid
;
984 sh
.offset
= key
->offset
;
987 sh
.transid
= found_transid
;
989 /* copy search result header */
990 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
991 *sk_offset
+= sizeof(sh
);
994 char *p
= buf
+ *sk_offset
;
996 read_extent_buffer(leaf
, p
,
998 *sk_offset
+= item_len
;
1002 if (*num_found
>= sk
->nr_items
)
1007 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1009 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1012 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1019 *num_found
+= found
;
1023 static noinline
int search_ioctl(struct inode
*inode
,
1024 struct btrfs_ioctl_search_args
*args
)
1026 struct btrfs_root
*root
;
1027 struct btrfs_key key
;
1028 struct btrfs_key max_key
;
1029 struct btrfs_path
*path
;
1030 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1031 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1034 unsigned long sk_offset
= 0;
1036 path
= btrfs_alloc_path();
1040 if (sk
->tree_id
== 0) {
1041 /* search the root of the inode that was passed */
1042 root
= BTRFS_I(inode
)->root
;
1044 key
.objectid
= sk
->tree_id
;
1045 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1046 key
.offset
= (u64
)-1;
1047 root
= btrfs_read_fs_root_no_name(info
, &key
);
1049 printk(KERN_ERR
"could not find root %llu\n",
1051 btrfs_free_path(path
);
1056 key
.objectid
= sk
->min_objectid
;
1057 key
.type
= sk
->min_type
;
1058 key
.offset
= sk
->min_offset
;
1060 max_key
.objectid
= sk
->max_objectid
;
1061 max_key
.type
= sk
->max_type
;
1062 max_key
.offset
= sk
->max_offset
;
1064 path
->keep_locks
= 1;
1067 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1074 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1075 &sk_offset
, &num_found
);
1076 btrfs_release_path(root
, path
);
1077 if (ret
|| num_found
>= sk
->nr_items
)
1083 sk
->nr_items
= num_found
;
1084 btrfs_free_path(path
);
1088 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1091 struct btrfs_ioctl_search_args
*args
;
1092 struct inode
*inode
;
1095 if (!capable(CAP_SYS_ADMIN
))
1098 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
1102 if (copy_from_user(args
, argp
, sizeof(*args
))) {
1106 inode
= fdentry(file
)->d_inode
;
1107 ret
= search_ioctl(inode
, args
);
1108 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1115 * Search INODE_REFs to identify path name of 'dirid' directory
1116 * in a 'tree_id' tree. and sets path name to 'name'.
1118 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1119 u64 tree_id
, u64 dirid
, char *name
)
1121 struct btrfs_root
*root
;
1122 struct btrfs_key key
;
1128 struct btrfs_inode_ref
*iref
;
1129 struct extent_buffer
*l
;
1130 struct btrfs_path
*path
;
1132 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1137 path
= btrfs_alloc_path();
1141 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1143 key
.objectid
= tree_id
;
1144 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1145 key
.offset
= (u64
)-1;
1146 root
= btrfs_read_fs_root_no_name(info
, &key
);
1148 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1153 key
.objectid
= dirid
;
1154 key
.type
= BTRFS_INODE_REF_KEY
;
1155 key
.offset
= (u64
)-1;
1158 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1163 slot
= path
->slots
[0];
1164 if (ret
> 0 && slot
> 0)
1166 btrfs_item_key_to_cpu(l
, &key
, slot
);
1168 if (ret
> 0 && (key
.objectid
!= dirid
||
1169 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1174 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1175 len
= btrfs_inode_ref_name_len(l
, iref
);
1177 total_len
+= len
+ 1;
1182 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1184 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1187 btrfs_release_path(root
, path
);
1188 key
.objectid
= key
.offset
;
1189 key
.offset
= (u64
)-1;
1190 dirid
= key
.objectid
;
1195 memcpy(name
, ptr
, total_len
);
1196 name
[total_len
]='\0';
1199 btrfs_free_path(path
);
1203 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1206 struct btrfs_ioctl_ino_lookup_args
*args
;
1207 struct inode
*inode
;
1210 if (!capable(CAP_SYS_ADMIN
))
1213 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
1214 if (copy_from_user(args
, argp
, sizeof(*args
))) {
1218 inode
= fdentry(file
)->d_inode
;
1220 if (args
->treeid
== 0)
1221 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1223 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1224 args
->treeid
, args
->objectid
,
1227 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1234 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1237 struct dentry
*parent
= fdentry(file
);
1238 struct dentry
*dentry
;
1239 struct inode
*dir
= parent
->d_inode
;
1240 struct inode
*inode
;
1241 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1242 struct btrfs_root
*dest
= NULL
;
1243 struct btrfs_ioctl_vol_args
*vol_args
;
1244 struct btrfs_trans_handle
*trans
;
1249 if (!capable(CAP_SYS_ADMIN
))
1252 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1253 if (IS_ERR(vol_args
))
1254 return PTR_ERR(vol_args
);
1256 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1257 namelen
= strlen(vol_args
->name
);
1258 if (strchr(vol_args
->name
, '/') ||
1259 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1264 err
= mnt_want_write(file
->f_path
.mnt
);
1268 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1269 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1270 if (IS_ERR(dentry
)) {
1271 err
= PTR_ERR(dentry
);
1272 goto out_unlock_dir
;
1275 if (!dentry
->d_inode
) {
1280 inode
= dentry
->d_inode
;
1281 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1286 dest
= BTRFS_I(inode
)->root
;
1288 mutex_lock(&inode
->i_mutex
);
1289 err
= d_invalidate(dentry
);
1293 down_write(&root
->fs_info
->subvol_sem
);
1295 err
= may_destroy_subvol(dest
);
1299 trans
= btrfs_start_transaction(root
, 1);
1300 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1301 dest
->root_key
.objectid
,
1302 dentry
->d_name
.name
,
1303 dentry
->d_name
.len
);
1306 btrfs_record_root_in_trans(trans
, dest
);
1308 memset(&dest
->root_item
.drop_progress
, 0,
1309 sizeof(dest
->root_item
.drop_progress
));
1310 dest
->root_item
.drop_level
= 0;
1311 btrfs_set_root_refs(&dest
->root_item
, 0);
1313 ret
= btrfs_insert_orphan_item(trans
,
1314 root
->fs_info
->tree_root
,
1315 dest
->root_key
.objectid
);
1318 ret
= btrfs_commit_transaction(trans
, root
);
1320 inode
->i_flags
|= S_DEAD
;
1322 up_write(&root
->fs_info
->subvol_sem
);
1324 mutex_unlock(&inode
->i_mutex
);
1326 shrink_dcache_sb(root
->fs_info
->sb
);
1327 btrfs_invalidate_inodes(dest
);
1333 mutex_unlock(&dir
->i_mutex
);
1334 mnt_drop_write(file
->f_path
.mnt
);
1340 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1342 struct inode
*inode
= fdentry(file
)->d_inode
;
1343 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1344 struct btrfs_ioctl_defrag_range_args
*range
;
1347 ret
= mnt_want_write(file
->f_path
.mnt
);
1351 switch (inode
->i_mode
& S_IFMT
) {
1353 if (!capable(CAP_SYS_ADMIN
)) {
1357 btrfs_defrag_root(root
, 0);
1358 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1361 if (!(file
->f_mode
& FMODE_WRITE
)) {
1366 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1373 if (copy_from_user(range
, argp
,
1378 /* compression requires us to start the IO */
1379 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1380 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1381 range
->extent_thresh
= (u32
)-1;
1384 /* the rest are all set to zero by kzalloc */
1385 range
->len
= (u64
)-1;
1387 btrfs_defrag_file(file
, range
);
1392 mnt_drop_write(file
->f_path
.mnt
);
1396 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1398 struct btrfs_ioctl_vol_args
*vol_args
;
1401 if (!capable(CAP_SYS_ADMIN
))
1404 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1405 if (IS_ERR(vol_args
))
1406 return PTR_ERR(vol_args
);
1408 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1409 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1415 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1417 struct btrfs_ioctl_vol_args
*vol_args
;
1420 if (!capable(CAP_SYS_ADMIN
))
1423 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1426 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1427 if (IS_ERR(vol_args
))
1428 return PTR_ERR(vol_args
);
1430 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1431 ret
= btrfs_rm_device(root
, vol_args
->name
);
1437 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1438 u64 off
, u64 olen
, u64 destoff
)
1440 struct inode
*inode
= fdentry(file
)->d_inode
;
1441 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1442 struct file
*src_file
;
1444 struct btrfs_trans_handle
*trans
;
1445 struct btrfs_path
*path
;
1446 struct extent_buffer
*leaf
;
1448 struct btrfs_key key
;
1453 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1458 * - split compressed inline extents. annoying: we need to
1459 * decompress into destination's address_space (the file offset
1460 * may change, so source mapping won't do), then recompress (or
1461 * otherwise reinsert) a subrange.
1462 * - allow ranges within the same file to be cloned (provided
1463 * they don't overlap)?
1466 /* the destination must be opened for writing */
1467 if (!(file
->f_mode
& FMODE_WRITE
))
1470 ret
= mnt_want_write(file
->f_path
.mnt
);
1474 src_file
= fget(srcfd
);
1477 goto out_drop_write
;
1479 src
= src_file
->f_dentry
->d_inode
;
1486 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1490 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1494 buf
= vmalloc(btrfs_level_size(root
, 0));
1498 path
= btrfs_alloc_path();
1506 mutex_lock(&inode
->i_mutex
);
1507 mutex_lock(&src
->i_mutex
);
1509 mutex_lock(&src
->i_mutex
);
1510 mutex_lock(&inode
->i_mutex
);
1513 /* determine range to clone */
1515 if (off
>= src
->i_size
|| off
+ len
> src
->i_size
)
1518 olen
= len
= src
->i_size
- off
;
1519 /* if we extend to eof, continue to block boundary */
1520 if (off
+ len
== src
->i_size
)
1521 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
1524 /* verify the end result is block aligned */
1525 if ((off
& (bs
-1)) ||
1526 ((off
+ len
) & (bs
-1)))
1529 /* do any pending delalloc/csum calc on src, one way or
1530 another, and lock file content */
1532 struct btrfs_ordered_extent
*ordered
;
1533 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1534 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
1535 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
1537 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1539 btrfs_put_ordered_extent(ordered
);
1540 btrfs_wait_ordered_range(src
, off
, off
+len
);
1543 trans
= btrfs_start_transaction(root
, 1);
1546 /* punch hole in destination first */
1547 btrfs_drop_extents(trans
, inode
, off
, off
+ len
, &hint_byte
, 1);
1550 key
.objectid
= src
->i_ino
;
1551 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1556 * note the key will change type as we walk through the
1559 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
1563 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1564 if (path
->slots
[0] >= nritems
) {
1565 ret
= btrfs_next_leaf(root
, path
);
1570 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1572 leaf
= path
->nodes
[0];
1573 slot
= path
->slots
[0];
1575 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1576 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1577 key
.objectid
!= src
->i_ino
)
1580 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1581 struct btrfs_file_extent_item
*extent
;
1584 struct btrfs_key new_key
;
1585 u64 disko
= 0, diskl
= 0;
1586 u64 datao
= 0, datal
= 0;
1589 size
= btrfs_item_size_nr(leaf
, slot
);
1590 read_extent_buffer(leaf
, buf
,
1591 btrfs_item_ptr_offset(leaf
, slot
),
1594 extent
= btrfs_item_ptr(leaf
, slot
,
1595 struct btrfs_file_extent_item
);
1596 comp
= btrfs_file_extent_compression(leaf
, extent
);
1597 type
= btrfs_file_extent_type(leaf
, extent
);
1598 if (type
== BTRFS_FILE_EXTENT_REG
||
1599 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1600 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1602 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1604 datao
= btrfs_file_extent_offset(leaf
, extent
);
1605 datal
= btrfs_file_extent_num_bytes(leaf
,
1607 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1608 /* take upper bound, may be compressed */
1609 datal
= btrfs_file_extent_ram_bytes(leaf
,
1612 btrfs_release_path(root
, path
);
1614 if (key
.offset
+ datal
< off
||
1615 key
.offset
>= off
+len
)
1618 memcpy(&new_key
, &key
, sizeof(new_key
));
1619 new_key
.objectid
= inode
->i_ino
;
1620 new_key
.offset
= key
.offset
+ destoff
- off
;
1622 if (type
== BTRFS_FILE_EXTENT_REG
||
1623 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1624 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1629 leaf
= path
->nodes
[0];
1630 slot
= path
->slots
[0];
1631 write_extent_buffer(leaf
, buf
,
1632 btrfs_item_ptr_offset(leaf
, slot
),
1635 extent
= btrfs_item_ptr(leaf
, slot
,
1636 struct btrfs_file_extent_item
);
1638 if (off
> key
.offset
) {
1639 datao
+= off
- key
.offset
;
1640 datal
-= off
- key
.offset
;
1643 if (key
.offset
+ datal
> off
+ len
)
1644 datal
= off
+ len
- key
.offset
;
1646 /* disko == 0 means it's a hole */
1650 btrfs_set_file_extent_offset(leaf
, extent
,
1652 btrfs_set_file_extent_num_bytes(leaf
, extent
,
1655 inode_add_bytes(inode
, datal
);
1656 ret
= btrfs_inc_extent_ref(trans
, root
,
1658 root
->root_key
.objectid
,
1660 new_key
.offset
- datao
);
1663 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1666 if (off
> key
.offset
) {
1667 skip
= off
- key
.offset
;
1668 new_key
.offset
+= skip
;
1671 if (key
.offset
+ datal
> off
+len
)
1672 trim
= key
.offset
+ datal
- (off
+len
);
1674 if (comp
&& (skip
|| trim
)) {
1678 size
-= skip
+ trim
;
1679 datal
-= skip
+ trim
;
1680 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1687 btrfs_file_extent_calc_inline_size(0);
1688 memmove(buf
+start
, buf
+start
+skip
,
1692 leaf
= path
->nodes
[0];
1693 slot
= path
->slots
[0];
1694 write_extent_buffer(leaf
, buf
,
1695 btrfs_item_ptr_offset(leaf
, slot
),
1697 inode_add_bytes(inode
, datal
);
1700 btrfs_mark_buffer_dirty(leaf
);
1704 btrfs_release_path(root
, path
);
1709 btrfs_release_path(root
, path
);
1711 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1712 if (destoff
+ olen
> inode
->i_size
)
1713 btrfs_i_size_write(inode
, destoff
+ olen
);
1714 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1715 ret
= btrfs_update_inode(trans
, root
, inode
);
1717 btrfs_end_transaction(trans
, root
);
1718 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1720 vmtruncate(inode
, 0);
1722 mutex_unlock(&src
->i_mutex
);
1723 mutex_unlock(&inode
->i_mutex
);
1725 btrfs_free_path(path
);
1729 mnt_drop_write(file
->f_path
.mnt
);
1733 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1735 struct btrfs_ioctl_clone_range_args args
;
1737 if (copy_from_user(&args
, argp
, sizeof(args
)))
1739 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1740 args
.src_length
, args
.dest_offset
);
1744 * there are many ways the trans_start and trans_end ioctls can lead
1745 * to deadlocks. They should only be used by applications that
1746 * basically own the machine, and have a very in depth understanding
1747 * of all the possible deadlocks and enospc problems.
1749 static long btrfs_ioctl_trans_start(struct file
*file
)
1751 struct inode
*inode
= fdentry(file
)->d_inode
;
1752 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1753 struct btrfs_trans_handle
*trans
;
1757 if (!capable(CAP_SYS_ADMIN
))
1761 if (file
->private_data
)
1764 ret
= mnt_want_write(file
->f_path
.mnt
);
1768 mutex_lock(&root
->fs_info
->trans_mutex
);
1769 root
->fs_info
->open_ioctl_trans
++;
1770 mutex_unlock(&root
->fs_info
->trans_mutex
);
1773 trans
= btrfs_start_ioctl_transaction(root
, 0);
1777 file
->private_data
= trans
;
1781 mutex_lock(&root
->fs_info
->trans_mutex
);
1782 root
->fs_info
->open_ioctl_trans
--;
1783 mutex_unlock(&root
->fs_info
->trans_mutex
);
1784 mnt_drop_write(file
->f_path
.mnt
);
1789 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
1791 struct inode
*inode
= fdentry(file
)->d_inode
;
1792 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1793 struct btrfs_root
*new_root
;
1794 struct btrfs_dir_item
*di
;
1795 struct btrfs_trans_handle
*trans
;
1796 struct btrfs_path
*path
;
1797 struct btrfs_key location
;
1798 struct btrfs_disk_key disk_key
;
1799 struct btrfs_super_block
*disk_super
;
1804 if (!capable(CAP_SYS_ADMIN
))
1807 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
1811 objectid
= root
->root_key
.objectid
;
1813 location
.objectid
= objectid
;
1814 location
.type
= BTRFS_ROOT_ITEM_KEY
;
1815 location
.offset
= (u64
)-1;
1817 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
1818 if (IS_ERR(new_root
))
1819 return PTR_ERR(new_root
);
1821 if (btrfs_root_refs(&new_root
->root_item
) == 0)
1824 path
= btrfs_alloc_path();
1827 path
->leave_spinning
= 1;
1829 trans
= btrfs_start_transaction(root
, 1);
1831 btrfs_free_path(path
);
1835 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
1836 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
1837 dir_id
, "default", 7, 1);
1839 btrfs_free_path(path
);
1840 btrfs_end_transaction(trans
, root
);
1841 printk(KERN_ERR
"Umm, you don't have the default dir item, "
1842 "this isn't going to work\n");
1846 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
1847 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
1848 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1849 btrfs_free_path(path
);
1851 disk_super
= &root
->fs_info
->super_copy
;
1852 features
= btrfs_super_incompat_flags(disk_super
);
1853 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
1854 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
1855 btrfs_set_super_incompat_flags(disk_super
, features
);
1857 btrfs_end_transaction(trans
, root
);
1862 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
1864 struct btrfs_ioctl_space_args space_args
;
1865 struct btrfs_ioctl_space_info space
;
1866 struct btrfs_ioctl_space_info
*dest
;
1867 struct btrfs_ioctl_space_info
*dest_orig
;
1868 struct btrfs_ioctl_space_info
*user_dest
;
1869 struct btrfs_space_info
*info
;
1874 if (copy_from_user(&space_args
,
1875 (struct btrfs_ioctl_space_args __user
*)arg
,
1876 sizeof(space_args
)))
1879 /* first we count slots */
1881 list_for_each_entry_rcu(info
, &root
->fs_info
->space_info
, list
)
1885 /* space_slots == 0 means they are asking for a count */
1886 if (space_args
.space_slots
== 0) {
1887 space_args
.total_spaces
= slot_count
;
1890 alloc_size
= sizeof(*dest
) * slot_count
;
1891 /* we generally have at most 6 or so space infos, one for each raid
1892 * level. So, a whole page should be more than enough for everyone
1894 if (alloc_size
> PAGE_CACHE_SIZE
)
1897 space_args
.total_spaces
= 0;
1898 dest
= kmalloc(alloc_size
, GFP_NOFS
);
1903 /* now we have a buffer to copy into */
1905 list_for_each_entry_rcu(info
, &root
->fs_info
->space_info
, list
) {
1906 /* make sure we don't copy more than we allocated
1909 if (slot_count
== 0)
1913 /* make sure userland has enough room in their buffer */
1914 if (space_args
.total_spaces
>= space_args
.space_slots
)
1917 space
.flags
= info
->flags
;
1918 space
.total_bytes
= info
->total_bytes
;
1919 space
.used_bytes
= info
->bytes_used
;
1920 memcpy(dest
, &space
, sizeof(space
));
1922 space_args
.total_spaces
++;
1926 user_dest
= (struct btrfs_ioctl_space_info
*)
1927 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
1929 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
1934 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
1941 * there are many ways the trans_start and trans_end ioctls can lead
1942 * to deadlocks. They should only be used by applications that
1943 * basically own the machine, and have a very in depth understanding
1944 * of all the possible deadlocks and enospc problems.
1946 long btrfs_ioctl_trans_end(struct file
*file
)
1948 struct inode
*inode
= fdentry(file
)->d_inode
;
1949 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1950 struct btrfs_trans_handle
*trans
;
1952 trans
= file
->private_data
;
1955 file
->private_data
= NULL
;
1957 btrfs_end_transaction(trans
, root
);
1959 mutex_lock(&root
->fs_info
->trans_mutex
);
1960 root
->fs_info
->open_ioctl_trans
--;
1961 mutex_unlock(&root
->fs_info
->trans_mutex
);
1963 mnt_drop_write(file
->f_path
.mnt
);
1967 long btrfs_ioctl(struct file
*file
, unsigned int
1968 cmd
, unsigned long arg
)
1970 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1971 void __user
*argp
= (void __user
*)arg
;
1974 case FS_IOC_GETFLAGS
:
1975 return btrfs_ioctl_getflags(file
, argp
);
1976 case FS_IOC_SETFLAGS
:
1977 return btrfs_ioctl_setflags(file
, argp
);
1978 case FS_IOC_GETVERSION
:
1979 return btrfs_ioctl_getversion(file
, argp
);
1980 case BTRFS_IOC_SNAP_CREATE
:
1981 return btrfs_ioctl_snap_create(file
, argp
, 0);
1982 case BTRFS_IOC_SUBVOL_CREATE
:
1983 return btrfs_ioctl_snap_create(file
, argp
, 1);
1984 case BTRFS_IOC_SNAP_DESTROY
:
1985 return btrfs_ioctl_snap_destroy(file
, argp
);
1986 case BTRFS_IOC_DEFAULT_SUBVOL
:
1987 return btrfs_ioctl_default_subvol(file
, argp
);
1988 case BTRFS_IOC_DEFRAG
:
1989 return btrfs_ioctl_defrag(file
, NULL
);
1990 case BTRFS_IOC_DEFRAG_RANGE
:
1991 return btrfs_ioctl_defrag(file
, argp
);
1992 case BTRFS_IOC_RESIZE
:
1993 return btrfs_ioctl_resize(root
, argp
);
1994 case BTRFS_IOC_ADD_DEV
:
1995 return btrfs_ioctl_add_dev(root
, argp
);
1996 case BTRFS_IOC_RM_DEV
:
1997 return btrfs_ioctl_rm_dev(root
, argp
);
1998 case BTRFS_IOC_BALANCE
:
1999 return btrfs_balance(root
->fs_info
->dev_root
);
2000 case BTRFS_IOC_CLONE
:
2001 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2002 case BTRFS_IOC_CLONE_RANGE
:
2003 return btrfs_ioctl_clone_range(file
, argp
);
2004 case BTRFS_IOC_TRANS_START
:
2005 return btrfs_ioctl_trans_start(file
);
2006 case BTRFS_IOC_TRANS_END
:
2007 return btrfs_ioctl_trans_end(file
);
2008 case BTRFS_IOC_TREE_SEARCH
:
2009 return btrfs_ioctl_tree_search(file
, argp
);
2010 case BTRFS_IOC_INO_LOOKUP
:
2011 return btrfs_ioctl_ino_lookup(file
, argp
);
2012 case BTRFS_IOC_SPACE_INFO
:
2013 return btrfs_ioctl_space_info(root
, argp
);
2014 case BTRFS_IOC_SYNC
:
2015 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);