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"
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
;
248 ret
= btrfs_reserve_metadata_space(root
, 6);
252 trans
= btrfs_start_transaction(root
, 1);
255 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
260 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
261 0, objectid
, NULL
, 0, 0, 0);
267 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
268 btrfs_set_header_bytenr(leaf
, leaf
->start
);
269 btrfs_set_header_generation(leaf
, trans
->transid
);
270 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
271 btrfs_set_header_owner(leaf
, objectid
);
273 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
274 (unsigned long)btrfs_header_fsid(leaf
),
276 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
277 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
279 btrfs_mark_buffer_dirty(leaf
);
281 inode_item
= &root_item
.inode
;
282 memset(inode_item
, 0, sizeof(*inode_item
));
283 inode_item
->generation
= cpu_to_le64(1);
284 inode_item
->size
= cpu_to_le64(3);
285 inode_item
->nlink
= cpu_to_le32(1);
286 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
287 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
289 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
290 btrfs_set_root_generation(&root_item
, trans
->transid
);
291 btrfs_set_root_level(&root_item
, 0);
292 btrfs_set_root_refs(&root_item
, 1);
293 btrfs_set_root_used(&root_item
, leaf
->len
);
294 btrfs_set_root_last_snapshot(&root_item
, 0);
296 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
297 root_item
.drop_level
= 0;
299 btrfs_tree_unlock(leaf
);
300 free_extent_buffer(leaf
);
303 btrfs_set_root_dirid(&root_item
, new_dirid
);
305 key
.objectid
= objectid
;
307 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
308 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
313 key
.offset
= (u64
)-1;
314 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
315 BUG_ON(IS_ERR(new_root
));
317 btrfs_record_root_in_trans(trans
, new_root
);
319 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
320 BTRFS_I(dir
)->block_group
);
322 * insert the directory item
324 ret
= btrfs_set_inode_index(dir
, &index
);
327 ret
= btrfs_insert_dir_item(trans
, root
,
328 name
, namelen
, dir
->i_ino
, &key
,
329 BTRFS_FT_DIR
, index
);
333 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
334 ret
= btrfs_update_inode(trans
, root
, dir
);
337 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
338 objectid
, root
->root_key
.objectid
,
339 dir
->i_ino
, index
, name
, namelen
);
343 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
345 err
= btrfs_commit_transaction(trans
, root
);
349 btrfs_unreserve_metadata_space(root
, 6);
353 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
354 char *name
, int namelen
)
357 struct btrfs_pending_snapshot
*pending_snapshot
;
358 struct btrfs_trans_handle
*trans
;
370 ret
= btrfs_reserve_metadata_space(root
, 6);
374 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
375 if (!pending_snapshot
) {
377 btrfs_unreserve_metadata_space(root
, 6);
380 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
381 if (!pending_snapshot
->name
) {
383 kfree(pending_snapshot
);
384 btrfs_unreserve_metadata_space(root
, 6);
387 memcpy(pending_snapshot
->name
, name
, namelen
);
388 pending_snapshot
->name
[namelen
] = '\0';
389 pending_snapshot
->dentry
= dentry
;
390 trans
= btrfs_start_transaction(root
, 1);
392 pending_snapshot
->root
= root
;
393 list_add(&pending_snapshot
->list
,
394 &trans
->transaction
->pending_snapshots
);
395 ret
= btrfs_commit_transaction(trans
, root
);
397 btrfs_unreserve_metadata_space(root
, 6);
399 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
401 ret
= PTR_ERR(inode
);
405 d_instantiate(dentry
, inode
);
411 /* copy of may_create in fs/namei.c() */
412 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
418 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
422 * Create a new subvolume below @parent. This is largely modeled after
423 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
424 * inside this filesystem so it's quite a bit simpler.
426 static noinline
int btrfs_mksubvol(struct path
*parent
,
427 char *name
, int namelen
,
428 struct btrfs_root
*snap_src
)
430 struct inode
*dir
= parent
->dentry
->d_inode
;
431 struct dentry
*dentry
;
434 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
436 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
437 error
= PTR_ERR(dentry
);
445 error
= mnt_want_write(parent
->mnt
);
449 error
= btrfs_may_create(dir
, dentry
);
453 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
455 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
459 error
= create_snapshot(snap_src
, dentry
,
462 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
466 fsnotify_mkdir(dir
, dentry
);
468 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
470 mnt_drop_write(parent
->mnt
);
474 mutex_unlock(&dir
->i_mutex
);
478 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
479 int thresh
, u64
*last_len
, u64
*skip
,
482 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
483 struct extent_map
*em
= NULL
;
484 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
492 * make sure that once we start defragging and extent, we keep on
495 if (start
< *defrag_end
)
501 * hopefully we have this extent in the tree already, try without
502 * the full extent lock
504 read_lock(&em_tree
->lock
);
505 em
= lookup_extent_mapping(em_tree
, start
, len
);
506 read_unlock(&em_tree
->lock
);
509 /* get the big lock and read metadata off disk */
510 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
511 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
512 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
518 /* this will cover holes, and inline extents */
519 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
523 * we hit a real extent, if it is big don't bother defragging it again
525 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
529 * last_len ends up being a counter of how many bytes we've defragged.
530 * every time we choose not to defrag an extent, we reset *last_len
531 * so that the next tiny extent will force a defrag.
533 * The end result of this is that tiny extents before a single big
534 * extent will force at least part of that big extent to be defragged.
538 *defrag_end
= extent_map_end(em
);
541 *skip
= extent_map_end(em
);
549 static int btrfs_defrag_file(struct file
*file
,
550 struct btrfs_ioctl_defrag_range_args
*range
)
552 struct inode
*inode
= fdentry(file
)->d_inode
;
553 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
554 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
555 struct btrfs_ordered_extent
*ordered
;
557 unsigned long last_index
;
558 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
559 unsigned long total_read
= 0;
568 if (inode
->i_size
== 0)
571 if (range
->start
+ range
->len
> range
->start
) {
572 last_index
= min_t(u64
, inode
->i_size
- 1,
573 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
575 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
578 i
= range
->start
>> PAGE_CACHE_SHIFT
;
579 while (i
<= last_index
) {
580 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
582 range
->extent_thresh
,
587 * the should_defrag function tells us how much to skip
588 * bump our counter by the suggested amount
590 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
591 i
= max(i
+ 1, next
);
595 if (total_read
% ra_pages
== 0) {
596 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
597 min(last_index
, i
+ ra_pages
- 1));
600 mutex_lock(&inode
->i_mutex
);
601 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
602 BTRFS_I(inode
)->force_compress
= 1;
604 ret
= btrfs_check_data_free_space(root
, inode
, PAGE_CACHE_SIZE
);
610 ret
= btrfs_reserve_metadata_for_delalloc(root
, inode
, 1);
612 btrfs_free_reserved_data_space(root
, inode
,
618 if (inode
->i_size
== 0 ||
619 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
621 goto err_reservations
;
624 page
= grab_cache_page(inode
->i_mapping
, i
);
626 goto err_reservations
;
628 if (!PageUptodate(page
)) {
629 btrfs_readpage(NULL
, page
);
631 if (!PageUptodate(page
)) {
633 page_cache_release(page
);
634 goto err_reservations
;
638 if (page
->mapping
!= inode
->i_mapping
) {
640 page_cache_release(page
);
644 wait_on_page_writeback(page
);
646 if (PageDirty(page
)) {
647 btrfs_free_reserved_data_space(root
, inode
,
652 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
653 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
654 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
656 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
658 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
660 page_cache_release(page
);
661 btrfs_start_ordered_extent(inode
, ordered
, 1);
662 btrfs_put_ordered_extent(ordered
);
665 set_page_extent_mapped(page
);
668 * this makes sure page_mkwrite is called on the
669 * page if it is dirtied again later
671 clear_page_dirty_for_io(page
);
672 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
673 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
674 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
676 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
677 ClearPageChecked(page
);
678 set_page_dirty(page
);
679 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
683 page_cache_release(page
);
684 mutex_unlock(&inode
->i_mutex
);
686 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
687 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
691 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
692 filemap_flush(inode
->i_mapping
);
694 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
695 /* the filemap_flush will queue IO into the worker threads, but
696 * we have to make sure the IO is actually started and that
697 * ordered extents get created before we return
699 atomic_inc(&root
->fs_info
->async_submit_draining
);
700 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
701 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
702 wait_event(root
->fs_info
->async_submit_wait
,
703 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
704 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
706 atomic_dec(&root
->fs_info
->async_submit_draining
);
708 mutex_lock(&inode
->i_mutex
);
709 BTRFS_I(inode
)->force_compress
= 0;
710 mutex_unlock(&inode
->i_mutex
);
716 mutex_unlock(&inode
->i_mutex
);
717 btrfs_free_reserved_data_space(root
, inode
, PAGE_CACHE_SIZE
);
718 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
722 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
728 struct btrfs_ioctl_vol_args
*vol_args
;
729 struct btrfs_trans_handle
*trans
;
730 struct btrfs_device
*device
= NULL
;
737 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
740 if (!capable(CAP_SYS_ADMIN
))
743 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
744 if (IS_ERR(vol_args
))
745 return PTR_ERR(vol_args
);
747 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
748 namelen
= strlen(vol_args
->name
);
750 mutex_lock(&root
->fs_info
->volume_mutex
);
751 sizestr
= vol_args
->name
;
752 devstr
= strchr(sizestr
, ':');
755 sizestr
= devstr
+ 1;
757 devstr
= vol_args
->name
;
758 devid
= simple_strtoull(devstr
, &end
, 10);
759 printk(KERN_INFO
"resizing devid %llu\n",
760 (unsigned long long)devid
);
762 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
764 printk(KERN_INFO
"resizer unable to find device %llu\n",
765 (unsigned long long)devid
);
769 if (!strcmp(sizestr
, "max"))
770 new_size
= device
->bdev
->bd_inode
->i_size
;
772 if (sizestr
[0] == '-') {
775 } else if (sizestr
[0] == '+') {
779 new_size
= memparse(sizestr
, NULL
);
786 old_size
= device
->total_bytes
;
789 if (new_size
> old_size
) {
793 new_size
= old_size
- new_size
;
794 } else if (mod
> 0) {
795 new_size
= old_size
+ new_size
;
798 if (new_size
< 256 * 1024 * 1024) {
802 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
807 do_div(new_size
, root
->sectorsize
);
808 new_size
*= root
->sectorsize
;
810 printk(KERN_INFO
"new size for %s is %llu\n",
811 device
->name
, (unsigned long long)new_size
);
813 if (new_size
> old_size
) {
814 trans
= btrfs_start_transaction(root
, 1);
815 ret
= btrfs_grow_device(trans
, device
, new_size
);
816 btrfs_commit_transaction(trans
, root
);
818 ret
= btrfs_shrink_device(device
, new_size
);
822 mutex_unlock(&root
->fs_info
->volume_mutex
);
827 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
828 void __user
*arg
, int subvol
)
830 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
831 struct btrfs_ioctl_vol_args
*vol_args
;
832 struct file
*src_file
;
836 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
839 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
840 if (IS_ERR(vol_args
))
841 return PTR_ERR(vol_args
);
843 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
844 namelen
= strlen(vol_args
->name
);
845 if (strchr(vol_args
->name
, '/')) {
851 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
854 struct inode
*src_inode
;
855 src_file
= fget(vol_args
->fd
);
861 src_inode
= src_file
->f_path
.dentry
->d_inode
;
862 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
863 printk(KERN_INFO
"btrfs: Snapshot src from "
869 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
870 BTRFS_I(src_inode
)->root
);
879 * helper to check if the subvolume references other subvolumes
881 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
883 struct btrfs_path
*path
;
884 struct btrfs_key key
;
887 path
= btrfs_alloc_path();
891 key
.objectid
= root
->root_key
.objectid
;
892 key
.type
= BTRFS_ROOT_REF_KEY
;
893 key
.offset
= (u64
)-1;
895 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
902 if (path
->slots
[0] > 0) {
904 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
905 if (key
.objectid
== root
->root_key
.objectid
&&
906 key
.type
== BTRFS_ROOT_REF_KEY
)
910 btrfs_free_path(path
);
914 static noinline
int key_in_sk(struct btrfs_key
*key
,
915 struct btrfs_ioctl_search_key
*sk
)
917 struct btrfs_key test
;
920 test
.objectid
= sk
->min_objectid
;
921 test
.type
= sk
->min_type
;
922 test
.offset
= sk
->min_offset
;
924 ret
= btrfs_comp_cpu_keys(key
, &test
);
928 test
.objectid
= sk
->max_objectid
;
929 test
.type
= sk
->max_type
;
930 test
.offset
= sk
->max_offset
;
932 ret
= btrfs_comp_cpu_keys(key
, &test
);
938 static noinline
int copy_to_sk(struct btrfs_root
*root
,
939 struct btrfs_path
*path
,
940 struct btrfs_key
*key
,
941 struct btrfs_ioctl_search_key
*sk
,
943 unsigned long *sk_offset
,
947 struct extent_buffer
*leaf
;
948 struct btrfs_ioctl_search_header sh
;
949 unsigned long item_off
;
950 unsigned long item_len
;
957 leaf
= path
->nodes
[0];
958 slot
= path
->slots
[0];
959 nritems
= btrfs_header_nritems(leaf
);
961 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
965 found_transid
= btrfs_header_generation(leaf
);
967 for (i
= slot
; i
< nritems
; i
++) {
968 item_off
= btrfs_item_ptr_offset(leaf
, i
);
969 item_len
= btrfs_item_size_nr(leaf
, i
);
971 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
974 if (sizeof(sh
) + item_len
+ *sk_offset
>
975 BTRFS_SEARCH_ARGS_BUFSIZE
) {
980 btrfs_item_key_to_cpu(leaf
, key
, i
);
981 if (!key_in_sk(key
, sk
))
984 sh
.objectid
= key
->objectid
;
985 sh
.offset
= key
->offset
;
988 sh
.transid
= found_transid
;
990 /* copy search result header */
991 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
992 *sk_offset
+= sizeof(sh
);
995 char *p
= buf
+ *sk_offset
;
997 read_extent_buffer(leaf
, p
,
999 *sk_offset
+= item_len
;
1003 if (*num_found
>= sk
->nr_items
)
1008 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1010 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1013 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1020 *num_found
+= found
;
1024 static noinline
int search_ioctl(struct inode
*inode
,
1025 struct btrfs_ioctl_search_args
*args
)
1027 struct btrfs_root
*root
;
1028 struct btrfs_key key
;
1029 struct btrfs_key max_key
;
1030 struct btrfs_path
*path
;
1031 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1032 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1035 unsigned long sk_offset
= 0;
1037 path
= btrfs_alloc_path();
1041 if (sk
->tree_id
== 0) {
1042 /* search the root of the inode that was passed */
1043 root
= BTRFS_I(inode
)->root
;
1045 key
.objectid
= sk
->tree_id
;
1046 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1047 key
.offset
= (u64
)-1;
1048 root
= btrfs_read_fs_root_no_name(info
, &key
);
1050 printk(KERN_ERR
"could not find root %llu\n",
1052 btrfs_free_path(path
);
1057 key
.objectid
= sk
->min_objectid
;
1058 key
.type
= sk
->min_type
;
1059 key
.offset
= sk
->min_offset
;
1061 max_key
.objectid
= sk
->max_objectid
;
1062 max_key
.type
= sk
->max_type
;
1063 max_key
.offset
= sk
->max_offset
;
1065 path
->keep_locks
= 1;
1068 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1075 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1076 &sk_offset
, &num_found
);
1077 btrfs_release_path(root
, path
);
1078 if (ret
|| num_found
>= sk
->nr_items
)
1084 sk
->nr_items
= num_found
;
1085 btrfs_free_path(path
);
1089 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1092 struct btrfs_ioctl_search_args
*args
;
1093 struct inode
*inode
;
1096 if (!capable(CAP_SYS_ADMIN
))
1099 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
1103 if (copy_from_user(args
, argp
, sizeof(*args
))) {
1107 inode
= fdentry(file
)->d_inode
;
1108 ret
= search_ioctl(inode
, args
);
1109 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1116 * Search INODE_REFs to identify path name of 'dirid' directory
1117 * in a 'tree_id' tree. and sets path name to 'name'.
1119 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1120 u64 tree_id
, u64 dirid
, char *name
)
1122 struct btrfs_root
*root
;
1123 struct btrfs_key key
;
1129 struct btrfs_inode_ref
*iref
;
1130 struct extent_buffer
*l
;
1131 struct btrfs_path
*path
;
1133 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1138 path
= btrfs_alloc_path();
1142 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1144 key
.objectid
= tree_id
;
1145 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1146 key
.offset
= (u64
)-1;
1147 root
= btrfs_read_fs_root_no_name(info
, &key
);
1149 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1154 key
.objectid
= dirid
;
1155 key
.type
= BTRFS_INODE_REF_KEY
;
1156 key
.offset
= (u64
)-1;
1159 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1164 slot
= path
->slots
[0];
1165 if (ret
> 0 && slot
> 0)
1167 btrfs_item_key_to_cpu(l
, &key
, slot
);
1169 if (ret
> 0 && (key
.objectid
!= dirid
||
1170 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1175 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1176 len
= btrfs_inode_ref_name_len(l
, iref
);
1178 total_len
+= len
+ 1;
1183 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1185 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1188 btrfs_release_path(root
, path
);
1189 key
.objectid
= key
.offset
;
1190 key
.offset
= (u64
)-1;
1191 dirid
= key
.objectid
;
1196 memcpy(name
, ptr
, total_len
);
1197 name
[total_len
]='\0';
1200 btrfs_free_path(path
);
1204 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1207 struct btrfs_ioctl_ino_lookup_args
*args
;
1208 struct inode
*inode
;
1211 if (!capable(CAP_SYS_ADMIN
))
1214 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
1215 if (copy_from_user(args
, argp
, sizeof(*args
))) {
1219 inode
= fdentry(file
)->d_inode
;
1221 if (args
->treeid
== 0)
1222 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1224 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1225 args
->treeid
, args
->objectid
,
1228 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1235 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1238 struct dentry
*parent
= fdentry(file
);
1239 struct dentry
*dentry
;
1240 struct inode
*dir
= parent
->d_inode
;
1241 struct inode
*inode
;
1242 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1243 struct btrfs_root
*dest
= NULL
;
1244 struct btrfs_ioctl_vol_args
*vol_args
;
1245 struct btrfs_trans_handle
*trans
;
1250 if (!capable(CAP_SYS_ADMIN
))
1253 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1254 if (IS_ERR(vol_args
))
1255 return PTR_ERR(vol_args
);
1257 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1258 namelen
= strlen(vol_args
->name
);
1259 if (strchr(vol_args
->name
, '/') ||
1260 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1265 err
= mnt_want_write(file
->f_path
.mnt
);
1269 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1270 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1271 if (IS_ERR(dentry
)) {
1272 err
= PTR_ERR(dentry
);
1273 goto out_unlock_dir
;
1276 if (!dentry
->d_inode
) {
1281 inode
= dentry
->d_inode
;
1282 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1287 dest
= BTRFS_I(inode
)->root
;
1289 mutex_lock(&inode
->i_mutex
);
1290 err
= d_invalidate(dentry
);
1294 down_write(&root
->fs_info
->subvol_sem
);
1296 err
= may_destroy_subvol(dest
);
1300 trans
= btrfs_start_transaction(root
, 1);
1301 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1302 dest
->root_key
.objectid
,
1303 dentry
->d_name
.name
,
1304 dentry
->d_name
.len
);
1307 btrfs_record_root_in_trans(trans
, dest
);
1309 memset(&dest
->root_item
.drop_progress
, 0,
1310 sizeof(dest
->root_item
.drop_progress
));
1311 dest
->root_item
.drop_level
= 0;
1312 btrfs_set_root_refs(&dest
->root_item
, 0);
1314 ret
= btrfs_insert_orphan_item(trans
,
1315 root
->fs_info
->tree_root
,
1316 dest
->root_key
.objectid
);
1319 ret
= btrfs_commit_transaction(trans
, root
);
1321 inode
->i_flags
|= S_DEAD
;
1323 up_write(&root
->fs_info
->subvol_sem
);
1325 mutex_unlock(&inode
->i_mutex
);
1327 shrink_dcache_sb(root
->fs_info
->sb
);
1328 btrfs_invalidate_inodes(dest
);
1334 mutex_unlock(&dir
->i_mutex
);
1335 mnt_drop_write(file
->f_path
.mnt
);
1341 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1343 struct inode
*inode
= fdentry(file
)->d_inode
;
1344 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1345 struct btrfs_ioctl_defrag_range_args
*range
;
1348 ret
= mnt_want_write(file
->f_path
.mnt
);
1352 switch (inode
->i_mode
& S_IFMT
) {
1354 if (!capable(CAP_SYS_ADMIN
)) {
1358 btrfs_defrag_root(root
, 0);
1359 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1362 if (!(file
->f_mode
& FMODE_WRITE
)) {
1367 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1374 if (copy_from_user(range
, argp
,
1379 /* compression requires us to start the IO */
1380 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1381 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1382 range
->extent_thresh
= (u32
)-1;
1385 /* the rest are all set to zero by kzalloc */
1386 range
->len
= (u64
)-1;
1388 btrfs_defrag_file(file
, range
);
1393 mnt_drop_write(file
->f_path
.mnt
);
1397 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1399 struct btrfs_ioctl_vol_args
*vol_args
;
1402 if (!capable(CAP_SYS_ADMIN
))
1405 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1406 if (IS_ERR(vol_args
))
1407 return PTR_ERR(vol_args
);
1409 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1410 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1416 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1418 struct btrfs_ioctl_vol_args
*vol_args
;
1421 if (!capable(CAP_SYS_ADMIN
))
1424 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1427 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1428 if (IS_ERR(vol_args
))
1429 return PTR_ERR(vol_args
);
1431 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1432 ret
= btrfs_rm_device(root
, vol_args
->name
);
1438 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1439 u64 off
, u64 olen
, u64 destoff
)
1441 struct inode
*inode
= fdentry(file
)->d_inode
;
1442 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1443 struct file
*src_file
;
1445 struct btrfs_trans_handle
*trans
;
1446 struct btrfs_path
*path
;
1447 struct extent_buffer
*leaf
;
1449 struct btrfs_key key
;
1454 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1459 * - split compressed inline extents. annoying: we need to
1460 * decompress into destination's address_space (the file offset
1461 * may change, so source mapping won't do), then recompress (or
1462 * otherwise reinsert) a subrange.
1463 * - allow ranges within the same file to be cloned (provided
1464 * they don't overlap)?
1467 /* the destination must be opened for writing */
1468 if (!(file
->f_mode
& FMODE_WRITE
))
1471 ret
= mnt_want_write(file
->f_path
.mnt
);
1475 src_file
= fget(srcfd
);
1478 goto out_drop_write
;
1480 src
= src_file
->f_dentry
->d_inode
;
1487 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1491 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1495 buf
= vmalloc(btrfs_level_size(root
, 0));
1499 path
= btrfs_alloc_path();
1507 mutex_lock(&inode
->i_mutex
);
1508 mutex_lock(&src
->i_mutex
);
1510 mutex_lock(&src
->i_mutex
);
1511 mutex_lock(&inode
->i_mutex
);
1514 /* determine range to clone */
1516 if (off
>= src
->i_size
|| off
+ len
> src
->i_size
)
1519 olen
= len
= src
->i_size
- off
;
1520 /* if we extend to eof, continue to block boundary */
1521 if (off
+ len
== src
->i_size
)
1522 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
1525 /* verify the end result is block aligned */
1526 if ((off
& (bs
-1)) ||
1527 ((off
+ len
) & (bs
-1)))
1530 /* do any pending delalloc/csum calc on src, one way or
1531 another, and lock file content */
1533 struct btrfs_ordered_extent
*ordered
;
1534 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1535 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
1536 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
1538 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1540 btrfs_put_ordered_extent(ordered
);
1541 btrfs_wait_ordered_range(src
, off
, off
+len
);
1544 trans
= btrfs_start_transaction(root
, 1);
1547 /* punch hole in destination first */
1548 btrfs_drop_extents(trans
, inode
, off
, off
+ len
, &hint_byte
, 1);
1551 key
.objectid
= src
->i_ino
;
1552 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1557 * note the key will change type as we walk through the
1560 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
1564 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1565 if (path
->slots
[0] >= nritems
) {
1566 ret
= btrfs_next_leaf(root
, path
);
1571 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1573 leaf
= path
->nodes
[0];
1574 slot
= path
->slots
[0];
1576 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1577 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1578 key
.objectid
!= src
->i_ino
)
1581 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1582 struct btrfs_file_extent_item
*extent
;
1585 struct btrfs_key new_key
;
1586 u64 disko
= 0, diskl
= 0;
1587 u64 datao
= 0, datal
= 0;
1590 size
= btrfs_item_size_nr(leaf
, slot
);
1591 read_extent_buffer(leaf
, buf
,
1592 btrfs_item_ptr_offset(leaf
, slot
),
1595 extent
= btrfs_item_ptr(leaf
, slot
,
1596 struct btrfs_file_extent_item
);
1597 comp
= btrfs_file_extent_compression(leaf
, extent
);
1598 type
= btrfs_file_extent_type(leaf
, extent
);
1599 if (type
== BTRFS_FILE_EXTENT_REG
||
1600 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1601 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1603 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1605 datao
= btrfs_file_extent_offset(leaf
, extent
);
1606 datal
= btrfs_file_extent_num_bytes(leaf
,
1608 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1609 /* take upper bound, may be compressed */
1610 datal
= btrfs_file_extent_ram_bytes(leaf
,
1613 btrfs_release_path(root
, path
);
1615 if (key
.offset
+ datal
< off
||
1616 key
.offset
>= off
+len
)
1619 memcpy(&new_key
, &key
, sizeof(new_key
));
1620 new_key
.objectid
= inode
->i_ino
;
1621 new_key
.offset
= key
.offset
+ destoff
- off
;
1623 if (type
== BTRFS_FILE_EXTENT_REG
||
1624 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1625 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1630 leaf
= path
->nodes
[0];
1631 slot
= path
->slots
[0];
1632 write_extent_buffer(leaf
, buf
,
1633 btrfs_item_ptr_offset(leaf
, slot
),
1636 extent
= btrfs_item_ptr(leaf
, slot
,
1637 struct btrfs_file_extent_item
);
1639 if (off
> key
.offset
) {
1640 datao
+= off
- key
.offset
;
1641 datal
-= off
- key
.offset
;
1644 if (key
.offset
+ datal
> off
+ len
)
1645 datal
= off
+ len
- key
.offset
;
1647 /* disko == 0 means it's a hole */
1651 btrfs_set_file_extent_offset(leaf
, extent
,
1653 btrfs_set_file_extent_num_bytes(leaf
, extent
,
1656 inode_add_bytes(inode
, datal
);
1657 ret
= btrfs_inc_extent_ref(trans
, root
,
1659 root
->root_key
.objectid
,
1661 new_key
.offset
- datao
);
1664 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1667 if (off
> key
.offset
) {
1668 skip
= off
- key
.offset
;
1669 new_key
.offset
+= skip
;
1672 if (key
.offset
+ datal
> off
+len
)
1673 trim
= key
.offset
+ datal
- (off
+len
);
1675 if (comp
&& (skip
|| trim
)) {
1679 size
-= skip
+ trim
;
1680 datal
-= skip
+ trim
;
1681 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1688 btrfs_file_extent_calc_inline_size(0);
1689 memmove(buf
+start
, buf
+start
+skip
,
1693 leaf
= path
->nodes
[0];
1694 slot
= path
->slots
[0];
1695 write_extent_buffer(leaf
, buf
,
1696 btrfs_item_ptr_offset(leaf
, slot
),
1698 inode_add_bytes(inode
, datal
);
1701 btrfs_mark_buffer_dirty(leaf
);
1705 btrfs_release_path(root
, path
);
1710 btrfs_release_path(root
, path
);
1712 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1713 if (destoff
+ olen
> inode
->i_size
)
1714 btrfs_i_size_write(inode
, destoff
+ olen
);
1715 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1716 ret
= btrfs_update_inode(trans
, root
, inode
);
1718 btrfs_end_transaction(trans
, root
);
1719 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1721 vmtruncate(inode
, 0);
1723 mutex_unlock(&src
->i_mutex
);
1724 mutex_unlock(&inode
->i_mutex
);
1726 btrfs_free_path(path
);
1730 mnt_drop_write(file
->f_path
.mnt
);
1734 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1736 struct btrfs_ioctl_clone_range_args args
;
1738 if (copy_from_user(&args
, argp
, sizeof(args
)))
1740 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1741 args
.src_length
, args
.dest_offset
);
1745 * there are many ways the trans_start and trans_end ioctls can lead
1746 * to deadlocks. They should only be used by applications that
1747 * basically own the machine, and have a very in depth understanding
1748 * of all the possible deadlocks and enospc problems.
1750 static long btrfs_ioctl_trans_start(struct file
*file
)
1752 struct inode
*inode
= fdentry(file
)->d_inode
;
1753 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1754 struct btrfs_trans_handle
*trans
;
1758 if (!capable(CAP_SYS_ADMIN
))
1762 if (file
->private_data
)
1765 ret
= mnt_want_write(file
->f_path
.mnt
);
1769 mutex_lock(&root
->fs_info
->trans_mutex
);
1770 root
->fs_info
->open_ioctl_trans
++;
1771 mutex_unlock(&root
->fs_info
->trans_mutex
);
1774 trans
= btrfs_start_ioctl_transaction(root
, 0);
1778 file
->private_data
= trans
;
1782 mutex_lock(&root
->fs_info
->trans_mutex
);
1783 root
->fs_info
->open_ioctl_trans
--;
1784 mutex_unlock(&root
->fs_info
->trans_mutex
);
1785 mnt_drop_write(file
->f_path
.mnt
);
1790 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
1792 struct inode
*inode
= fdentry(file
)->d_inode
;
1793 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1794 struct btrfs_root
*new_root
;
1795 struct btrfs_dir_item
*di
;
1796 struct btrfs_trans_handle
*trans
;
1797 struct btrfs_path
*path
;
1798 struct btrfs_key location
;
1799 struct btrfs_disk_key disk_key
;
1800 struct btrfs_super_block
*disk_super
;
1805 if (!capable(CAP_SYS_ADMIN
))
1808 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
1812 objectid
= root
->root_key
.objectid
;
1814 location
.objectid
= objectid
;
1815 location
.type
= BTRFS_ROOT_ITEM_KEY
;
1816 location
.offset
= (u64
)-1;
1818 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
1819 if (IS_ERR(new_root
))
1820 return PTR_ERR(new_root
);
1822 if (btrfs_root_refs(&new_root
->root_item
) == 0)
1825 path
= btrfs_alloc_path();
1828 path
->leave_spinning
= 1;
1830 trans
= btrfs_start_transaction(root
, 1);
1832 btrfs_free_path(path
);
1836 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
1837 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
1838 dir_id
, "default", 7, 1);
1840 btrfs_free_path(path
);
1841 btrfs_end_transaction(trans
, root
);
1842 printk(KERN_ERR
"Umm, you don't have the default dir item, "
1843 "this isn't going to work\n");
1847 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
1848 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
1849 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1850 btrfs_free_path(path
);
1852 disk_super
= &root
->fs_info
->super_copy
;
1853 features
= btrfs_super_incompat_flags(disk_super
);
1854 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
1855 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
1856 btrfs_set_super_incompat_flags(disk_super
, features
);
1858 btrfs_end_transaction(trans
, root
);
1863 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
1865 struct btrfs_ioctl_space_args space_args
;
1866 struct btrfs_ioctl_space_info space
;
1867 struct btrfs_ioctl_space_info
*dest
;
1868 struct btrfs_ioctl_space_info
*dest_orig
;
1869 struct btrfs_ioctl_space_info
*user_dest
;
1870 struct btrfs_space_info
*info
;
1875 if (copy_from_user(&space_args
,
1876 (struct btrfs_ioctl_space_args __user
*)arg
,
1877 sizeof(space_args
)))
1880 /* first we count slots */
1882 list_for_each_entry_rcu(info
, &root
->fs_info
->space_info
, list
)
1886 /* space_slots == 0 means they are asking for a count */
1887 if (space_args
.space_slots
== 0) {
1888 space_args
.total_spaces
= slot_count
;
1891 alloc_size
= sizeof(*dest
) * slot_count
;
1892 /* we generally have at most 6 or so space infos, one for each raid
1893 * level. So, a whole page should be more than enough for everyone
1895 if (alloc_size
> PAGE_CACHE_SIZE
)
1898 space_args
.total_spaces
= 0;
1899 dest
= kmalloc(alloc_size
, GFP_NOFS
);
1904 /* now we have a buffer to copy into */
1906 list_for_each_entry_rcu(info
, &root
->fs_info
->space_info
, list
) {
1907 /* make sure we don't copy more than we allocated
1910 if (slot_count
== 0)
1914 /* make sure userland has enough room in their buffer */
1915 if (space_args
.total_spaces
>= space_args
.space_slots
)
1918 space
.flags
= info
->flags
;
1919 space
.total_bytes
= info
->total_bytes
;
1920 space
.used_bytes
= info
->bytes_used
;
1921 memcpy(dest
, &space
, sizeof(space
));
1923 space_args
.total_spaces
++;
1927 user_dest
= (struct btrfs_ioctl_space_info
*)
1928 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
1930 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
1935 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
1942 * there are many ways the trans_start and trans_end ioctls can lead
1943 * to deadlocks. They should only be used by applications that
1944 * basically own the machine, and have a very in depth understanding
1945 * of all the possible deadlocks and enospc problems.
1947 long btrfs_ioctl_trans_end(struct file
*file
)
1949 struct inode
*inode
= fdentry(file
)->d_inode
;
1950 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1951 struct btrfs_trans_handle
*trans
;
1953 trans
= file
->private_data
;
1956 file
->private_data
= NULL
;
1958 btrfs_end_transaction(trans
, root
);
1960 mutex_lock(&root
->fs_info
->trans_mutex
);
1961 root
->fs_info
->open_ioctl_trans
--;
1962 mutex_unlock(&root
->fs_info
->trans_mutex
);
1964 mnt_drop_write(file
->f_path
.mnt
);
1968 long btrfs_ioctl(struct file
*file
, unsigned int
1969 cmd
, unsigned long arg
)
1971 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1972 void __user
*argp
= (void __user
*)arg
;
1975 case FS_IOC_GETFLAGS
:
1976 return btrfs_ioctl_getflags(file
, argp
);
1977 case FS_IOC_SETFLAGS
:
1978 return btrfs_ioctl_setflags(file
, argp
);
1979 case FS_IOC_GETVERSION
:
1980 return btrfs_ioctl_getversion(file
, argp
);
1981 case BTRFS_IOC_SNAP_CREATE
:
1982 return btrfs_ioctl_snap_create(file
, argp
, 0);
1983 case BTRFS_IOC_SUBVOL_CREATE
:
1984 return btrfs_ioctl_snap_create(file
, argp
, 1);
1985 case BTRFS_IOC_SNAP_DESTROY
:
1986 return btrfs_ioctl_snap_destroy(file
, argp
);
1987 case BTRFS_IOC_DEFAULT_SUBVOL
:
1988 return btrfs_ioctl_default_subvol(file
, argp
);
1989 case BTRFS_IOC_DEFRAG
:
1990 return btrfs_ioctl_defrag(file
, NULL
);
1991 case BTRFS_IOC_DEFRAG_RANGE
:
1992 return btrfs_ioctl_defrag(file
, argp
);
1993 case BTRFS_IOC_RESIZE
:
1994 return btrfs_ioctl_resize(root
, argp
);
1995 case BTRFS_IOC_ADD_DEV
:
1996 return btrfs_ioctl_add_dev(root
, argp
);
1997 case BTRFS_IOC_RM_DEV
:
1998 return btrfs_ioctl_rm_dev(root
, argp
);
1999 case BTRFS_IOC_BALANCE
:
2000 return btrfs_balance(root
->fs_info
->dev_root
);
2001 case BTRFS_IOC_CLONE
:
2002 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2003 case BTRFS_IOC_CLONE_RANGE
:
2004 return btrfs_ioctl_clone_range(file
, argp
);
2005 case BTRFS_IOC_TRANS_START
:
2006 return btrfs_ioctl_trans_start(file
);
2007 case BTRFS_IOC_TRANS_END
:
2008 return btrfs_ioctl_trans_end(file
);
2009 case BTRFS_IOC_TREE_SEARCH
:
2010 return btrfs_ioctl_tree_search(file
, argp
);
2011 case BTRFS_IOC_INO_LOOKUP
:
2012 return btrfs_ioctl_ino_lookup(file
, argp
);
2013 case BTRFS_IOC_SPACE_INFO
:
2014 return btrfs_ioctl_space_info(root
, argp
);
2015 case BTRFS_IOC_SYNC
:
2016 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);