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);
289 root_item
.byte_limit
= 0;
290 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
292 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
293 btrfs_set_root_generation(&root_item
, trans
->transid
);
294 btrfs_set_root_level(&root_item
, 0);
295 btrfs_set_root_refs(&root_item
, 1);
296 btrfs_set_root_used(&root_item
, leaf
->len
);
297 btrfs_set_root_last_snapshot(&root_item
, 0);
299 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
300 root_item
.drop_level
= 0;
302 btrfs_tree_unlock(leaf
);
303 free_extent_buffer(leaf
);
306 btrfs_set_root_dirid(&root_item
, new_dirid
);
308 key
.objectid
= objectid
;
310 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
311 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
316 key
.offset
= (u64
)-1;
317 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
318 BUG_ON(IS_ERR(new_root
));
320 btrfs_record_root_in_trans(trans
, new_root
);
322 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
323 BTRFS_I(dir
)->block_group
);
325 * insert the directory item
327 ret
= btrfs_set_inode_index(dir
, &index
);
330 ret
= btrfs_insert_dir_item(trans
, root
,
331 name
, namelen
, dir
->i_ino
, &key
,
332 BTRFS_FT_DIR
, index
);
336 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
337 ret
= btrfs_update_inode(trans
, root
, dir
);
340 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
341 objectid
, root
->root_key
.objectid
,
342 dir
->i_ino
, index
, name
, namelen
);
346 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
348 err
= btrfs_commit_transaction(trans
, root
);
352 btrfs_unreserve_metadata_space(root
, 6);
356 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
357 char *name
, int namelen
)
360 struct btrfs_pending_snapshot
*pending_snapshot
;
361 struct btrfs_trans_handle
*trans
;
373 ret
= btrfs_reserve_metadata_space(root
, 6);
377 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
378 if (!pending_snapshot
) {
380 btrfs_unreserve_metadata_space(root
, 6);
383 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
384 if (!pending_snapshot
->name
) {
386 kfree(pending_snapshot
);
387 btrfs_unreserve_metadata_space(root
, 6);
390 memcpy(pending_snapshot
->name
, name
, namelen
);
391 pending_snapshot
->name
[namelen
] = '\0';
392 pending_snapshot
->dentry
= dentry
;
393 trans
= btrfs_start_transaction(root
, 1);
395 pending_snapshot
->root
= root
;
396 list_add(&pending_snapshot
->list
,
397 &trans
->transaction
->pending_snapshots
);
398 ret
= btrfs_commit_transaction(trans
, root
);
400 btrfs_unreserve_metadata_space(root
, 6);
402 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
404 ret
= PTR_ERR(inode
);
408 d_instantiate(dentry
, inode
);
414 /* copy of may_create in fs/namei.c() */
415 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
421 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
425 * Create a new subvolume below @parent. This is largely modeled after
426 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
427 * inside this filesystem so it's quite a bit simpler.
429 static noinline
int btrfs_mksubvol(struct path
*parent
,
430 char *name
, int namelen
,
431 struct btrfs_root
*snap_src
)
433 struct inode
*dir
= parent
->dentry
->d_inode
;
434 struct dentry
*dentry
;
437 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
439 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
440 error
= PTR_ERR(dentry
);
448 error
= mnt_want_write(parent
->mnt
);
452 error
= btrfs_may_create(dir
, dentry
);
456 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
458 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
462 error
= create_snapshot(snap_src
, dentry
,
465 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
469 fsnotify_mkdir(dir
, dentry
);
471 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
473 mnt_drop_write(parent
->mnt
);
477 mutex_unlock(&dir
->i_mutex
);
481 static int btrfs_defrag_file(struct file
*file
)
483 struct inode
*inode
= fdentry(file
)->d_inode
;
484 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
485 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
486 struct btrfs_ordered_extent
*ordered
;
488 unsigned long last_index
;
489 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
490 unsigned long total_read
= 0;
496 ret
= btrfs_check_data_free_space(root
, inode
, inode
->i_size
);
500 mutex_lock(&inode
->i_mutex
);
501 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
502 for (i
= 0; i
<= last_index
; i
++) {
503 if (total_read
% ra_pages
== 0) {
504 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
505 min(last_index
, i
+ ra_pages
- 1));
509 page
= grab_cache_page(inode
->i_mapping
, i
);
512 if (!PageUptodate(page
)) {
513 btrfs_readpage(NULL
, page
);
515 if (!PageUptodate(page
)) {
517 page_cache_release(page
);
522 wait_on_page_writeback(page
);
524 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
525 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
526 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
528 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
530 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
532 page_cache_release(page
);
533 btrfs_start_ordered_extent(inode
, ordered
, 1);
534 btrfs_put_ordered_extent(ordered
);
537 set_page_extent_mapped(page
);
540 * this makes sure page_mkwrite is called on the
541 * page if it is dirtied again later
543 clear_page_dirty_for_io(page
);
545 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
546 set_page_dirty(page
);
547 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
549 page_cache_release(page
);
550 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
554 mutex_unlock(&inode
->i_mutex
);
558 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
564 struct btrfs_ioctl_vol_args
*vol_args
;
565 struct btrfs_trans_handle
*trans
;
566 struct btrfs_device
*device
= NULL
;
573 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
576 if (!capable(CAP_SYS_ADMIN
))
579 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
580 if (IS_ERR(vol_args
))
581 return PTR_ERR(vol_args
);
583 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
584 namelen
= strlen(vol_args
->name
);
586 mutex_lock(&root
->fs_info
->volume_mutex
);
587 sizestr
= vol_args
->name
;
588 devstr
= strchr(sizestr
, ':');
591 sizestr
= devstr
+ 1;
593 devstr
= vol_args
->name
;
594 devid
= simple_strtoull(devstr
, &end
, 10);
595 printk(KERN_INFO
"resizing devid %llu\n",
596 (unsigned long long)devid
);
598 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
600 printk(KERN_INFO
"resizer unable to find device %llu\n",
601 (unsigned long long)devid
);
605 if (!strcmp(sizestr
, "max"))
606 new_size
= device
->bdev
->bd_inode
->i_size
;
608 if (sizestr
[0] == '-') {
611 } else if (sizestr
[0] == '+') {
615 new_size
= btrfs_parse_size(sizestr
);
622 old_size
= device
->total_bytes
;
625 if (new_size
> old_size
) {
629 new_size
= old_size
- new_size
;
630 } else if (mod
> 0) {
631 new_size
= old_size
+ new_size
;
634 if (new_size
< 256 * 1024 * 1024) {
638 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
643 do_div(new_size
, root
->sectorsize
);
644 new_size
*= root
->sectorsize
;
646 printk(KERN_INFO
"new size for %s is %llu\n",
647 device
->name
, (unsigned long long)new_size
);
649 if (new_size
> old_size
) {
650 trans
= btrfs_start_transaction(root
, 1);
651 ret
= btrfs_grow_device(trans
, device
, new_size
);
652 btrfs_commit_transaction(trans
, root
);
654 ret
= btrfs_shrink_device(device
, new_size
);
658 mutex_unlock(&root
->fs_info
->volume_mutex
);
663 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
664 void __user
*arg
, int subvol
)
666 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
667 struct btrfs_ioctl_vol_args
*vol_args
;
668 struct file
*src_file
;
672 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
675 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
676 if (IS_ERR(vol_args
))
677 return PTR_ERR(vol_args
);
679 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
680 namelen
= strlen(vol_args
->name
);
681 if (strchr(vol_args
->name
, '/')) {
687 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
690 struct inode
*src_inode
;
691 src_file
= fget(vol_args
->fd
);
697 src_inode
= src_file
->f_path
.dentry
->d_inode
;
698 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
699 printk(KERN_INFO
"btrfs: Snapshot src from "
705 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
706 BTRFS_I(src_inode
)->root
);
715 * helper to check if the subvolume references other subvolumes
717 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
719 struct btrfs_path
*path
;
720 struct btrfs_key key
;
723 path
= btrfs_alloc_path();
727 key
.objectid
= root
->root_key
.objectid
;
728 key
.type
= BTRFS_ROOT_REF_KEY
;
729 key
.offset
= (u64
)-1;
731 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
738 if (path
->slots
[0] > 0) {
740 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
741 if (key
.objectid
== root
->root_key
.objectid
&&
742 key
.type
== BTRFS_ROOT_REF_KEY
)
746 btrfs_free_path(path
);
750 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
753 struct dentry
*parent
= fdentry(file
);
754 struct dentry
*dentry
;
755 struct inode
*dir
= parent
->d_inode
;
757 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
758 struct btrfs_root
*dest
= NULL
;
759 struct btrfs_ioctl_vol_args
*vol_args
;
760 struct btrfs_trans_handle
*trans
;
765 if (!capable(CAP_SYS_ADMIN
))
768 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
769 if (IS_ERR(vol_args
))
770 return PTR_ERR(vol_args
);
772 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
773 namelen
= strlen(vol_args
->name
);
774 if (strchr(vol_args
->name
, '/') ||
775 strncmp(vol_args
->name
, "..", namelen
) == 0) {
780 err
= mnt_want_write(file
->f_path
.mnt
);
784 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
785 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
786 if (IS_ERR(dentry
)) {
787 err
= PTR_ERR(dentry
);
791 if (!dentry
->d_inode
) {
796 inode
= dentry
->d_inode
;
797 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
802 dest
= BTRFS_I(inode
)->root
;
804 mutex_lock(&inode
->i_mutex
);
805 err
= d_invalidate(dentry
);
809 down_write(&root
->fs_info
->subvol_sem
);
811 err
= may_destroy_subvol(dest
);
815 trans
= btrfs_start_transaction(root
, 1);
816 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
817 dest
->root_key
.objectid
,
822 btrfs_record_root_in_trans(trans
, dest
);
824 memset(&dest
->root_item
.drop_progress
, 0,
825 sizeof(dest
->root_item
.drop_progress
));
826 dest
->root_item
.drop_level
= 0;
827 btrfs_set_root_refs(&dest
->root_item
, 0);
829 ret
= btrfs_insert_orphan_item(trans
,
830 root
->fs_info
->tree_root
,
831 dest
->root_key
.objectid
);
834 ret
= btrfs_commit_transaction(trans
, root
);
836 inode
->i_flags
|= S_DEAD
;
838 up_write(&root
->fs_info
->subvol_sem
);
840 mutex_unlock(&inode
->i_mutex
);
842 shrink_dcache_sb(root
->fs_info
->sb
);
843 btrfs_invalidate_inodes(dest
);
849 mutex_unlock(&dir
->i_mutex
);
850 mnt_drop_write(file
->f_path
.mnt
);
856 static int btrfs_ioctl_defrag(struct file
*file
)
858 struct inode
*inode
= fdentry(file
)->d_inode
;
859 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
862 ret
= mnt_want_write(file
->f_path
.mnt
);
866 switch (inode
->i_mode
& S_IFMT
) {
868 if (!capable(CAP_SYS_ADMIN
)) {
872 btrfs_defrag_root(root
, 0);
873 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
876 if (!(file
->f_mode
& FMODE_WRITE
)) {
880 btrfs_defrag_file(file
);
884 mnt_drop_write(file
->f_path
.mnt
);
888 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
890 struct btrfs_ioctl_vol_args
*vol_args
;
893 if (!capable(CAP_SYS_ADMIN
))
896 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
897 if (IS_ERR(vol_args
))
898 return PTR_ERR(vol_args
);
900 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
901 ret
= btrfs_init_new_device(root
, vol_args
->name
);
907 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
909 struct btrfs_ioctl_vol_args
*vol_args
;
912 if (!capable(CAP_SYS_ADMIN
))
915 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
918 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
919 if (IS_ERR(vol_args
))
920 return PTR_ERR(vol_args
);
922 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
923 ret
= btrfs_rm_device(root
, vol_args
->name
);
929 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
930 u64 off
, u64 olen
, u64 destoff
)
932 struct inode
*inode
= fdentry(file
)->d_inode
;
933 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
934 struct file
*src_file
;
936 struct btrfs_trans_handle
*trans
;
937 struct btrfs_path
*path
;
938 struct extent_buffer
*leaf
;
940 struct btrfs_key key
;
945 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
950 * - split compressed inline extents. annoying: we need to
951 * decompress into destination's address_space (the file offset
952 * may change, so source mapping won't do), then recompress (or
953 * otherwise reinsert) a subrange.
954 * - allow ranges within the same file to be cloned (provided
955 * they don't overlap)?
958 /* the destination must be opened for writing */
959 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
962 ret
= mnt_want_write(file
->f_path
.mnt
);
966 src_file
= fget(srcfd
);
972 src
= src_file
->f_dentry
->d_inode
;
978 /* the src must be open for reading */
979 if (!(src_file
->f_mode
& FMODE_READ
))
983 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
987 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
991 buf
= vmalloc(btrfs_level_size(root
, 0));
995 path
= btrfs_alloc_path();
1003 mutex_lock(&inode
->i_mutex
);
1004 mutex_lock(&src
->i_mutex
);
1006 mutex_lock(&src
->i_mutex
);
1007 mutex_lock(&inode
->i_mutex
);
1010 /* determine range to clone */
1012 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
1015 olen
= len
= src
->i_size
- off
;
1016 /* if we extend to eof, continue to block boundary */
1017 if (off
+ len
== src
->i_size
)
1018 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
1021 /* verify the end result is block aligned */
1022 if ((off
& (bs
-1)) ||
1023 ((off
+ len
) & (bs
-1)))
1026 /* do any pending delalloc/csum calc on src, one way or
1027 another, and lock file content */
1029 struct btrfs_ordered_extent
*ordered
;
1030 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1031 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
1032 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
1034 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1036 btrfs_put_ordered_extent(ordered
);
1037 btrfs_wait_ordered_range(src
, off
, off
+len
);
1040 trans
= btrfs_start_transaction(root
, 1);
1043 /* punch hole in destination first */
1044 btrfs_drop_extents(trans
, inode
, off
, off
+ len
, &hint_byte
, 1);
1047 key
.objectid
= src
->i_ino
;
1048 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1053 * note the key will change type as we walk through the
1056 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
1060 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1061 if (path
->slots
[0] >= nritems
) {
1062 ret
= btrfs_next_leaf(root
, path
);
1067 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1069 leaf
= path
->nodes
[0];
1070 slot
= path
->slots
[0];
1072 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1073 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1074 key
.objectid
!= src
->i_ino
)
1077 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1078 struct btrfs_file_extent_item
*extent
;
1081 struct btrfs_key new_key
;
1082 u64 disko
= 0, diskl
= 0;
1083 u64 datao
= 0, datal
= 0;
1086 size
= btrfs_item_size_nr(leaf
, slot
);
1087 read_extent_buffer(leaf
, buf
,
1088 btrfs_item_ptr_offset(leaf
, slot
),
1091 extent
= btrfs_item_ptr(leaf
, slot
,
1092 struct btrfs_file_extent_item
);
1093 comp
= btrfs_file_extent_compression(leaf
, extent
);
1094 type
= btrfs_file_extent_type(leaf
, extent
);
1095 if (type
== BTRFS_FILE_EXTENT_REG
||
1096 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1097 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1099 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1101 datao
= btrfs_file_extent_offset(leaf
, extent
);
1102 datal
= btrfs_file_extent_num_bytes(leaf
,
1104 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1105 /* take upper bound, may be compressed */
1106 datal
= btrfs_file_extent_ram_bytes(leaf
,
1109 btrfs_release_path(root
, path
);
1111 if (key
.offset
+ datal
< off
||
1112 key
.offset
>= off
+len
)
1115 memcpy(&new_key
, &key
, sizeof(new_key
));
1116 new_key
.objectid
= inode
->i_ino
;
1117 new_key
.offset
= key
.offset
+ destoff
- off
;
1119 if (type
== BTRFS_FILE_EXTENT_REG
||
1120 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1121 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1126 leaf
= path
->nodes
[0];
1127 slot
= path
->slots
[0];
1128 write_extent_buffer(leaf
, buf
,
1129 btrfs_item_ptr_offset(leaf
, slot
),
1132 extent
= btrfs_item_ptr(leaf
, slot
,
1133 struct btrfs_file_extent_item
);
1135 if (off
> key
.offset
) {
1136 datao
+= off
- key
.offset
;
1137 datal
-= off
- key
.offset
;
1140 if (key
.offset
+ datal
> off
+ len
)
1141 datal
= off
+ len
- key
.offset
;
1143 /* disko == 0 means it's a hole */
1147 btrfs_set_file_extent_offset(leaf
, extent
,
1149 btrfs_set_file_extent_num_bytes(leaf
, extent
,
1152 inode_add_bytes(inode
, datal
);
1153 ret
= btrfs_inc_extent_ref(trans
, root
,
1155 root
->root_key
.objectid
,
1157 new_key
.offset
- datao
);
1160 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1163 if (off
> key
.offset
) {
1164 skip
= off
- key
.offset
;
1165 new_key
.offset
+= skip
;
1168 if (key
.offset
+ datal
> off
+len
)
1169 trim
= key
.offset
+ datal
- (off
+len
);
1171 if (comp
&& (skip
|| trim
)) {
1175 size
-= skip
+ trim
;
1176 datal
-= skip
+ trim
;
1177 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1184 btrfs_file_extent_calc_inline_size(0);
1185 memmove(buf
+start
, buf
+start
+skip
,
1189 leaf
= path
->nodes
[0];
1190 slot
= path
->slots
[0];
1191 write_extent_buffer(leaf
, buf
,
1192 btrfs_item_ptr_offset(leaf
, slot
),
1194 inode_add_bytes(inode
, datal
);
1197 btrfs_mark_buffer_dirty(leaf
);
1201 btrfs_release_path(root
, path
);
1206 btrfs_release_path(root
, path
);
1208 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1209 if (destoff
+ olen
> inode
->i_size
)
1210 btrfs_i_size_write(inode
, destoff
+ olen
);
1211 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1212 ret
= btrfs_update_inode(trans
, root
, inode
);
1214 btrfs_end_transaction(trans
, root
);
1215 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1217 vmtruncate(inode
, 0);
1219 mutex_unlock(&src
->i_mutex
);
1220 mutex_unlock(&inode
->i_mutex
);
1222 btrfs_free_path(path
);
1226 mnt_drop_write(file
->f_path
.mnt
);
1230 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1232 struct btrfs_ioctl_clone_range_args args
;
1234 if (copy_from_user(&args
, argp
, sizeof(args
)))
1236 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1237 args
.src_length
, args
.dest_offset
);
1241 * there are many ways the trans_start and trans_end ioctls can lead
1242 * to deadlocks. They should only be used by applications that
1243 * basically own the machine, and have a very in depth understanding
1244 * of all the possible deadlocks and enospc problems.
1246 static long btrfs_ioctl_trans_start(struct file
*file
)
1248 struct inode
*inode
= fdentry(file
)->d_inode
;
1249 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1250 struct btrfs_trans_handle
*trans
;
1254 if (!capable(CAP_SYS_ADMIN
))
1258 if (file
->private_data
)
1261 ret
= mnt_want_write(file
->f_path
.mnt
);
1265 mutex_lock(&root
->fs_info
->trans_mutex
);
1266 root
->fs_info
->open_ioctl_trans
++;
1267 mutex_unlock(&root
->fs_info
->trans_mutex
);
1270 trans
= btrfs_start_ioctl_transaction(root
, 0);
1274 file
->private_data
= trans
;
1278 mutex_lock(&root
->fs_info
->trans_mutex
);
1279 root
->fs_info
->open_ioctl_trans
--;
1280 mutex_unlock(&root
->fs_info
->trans_mutex
);
1281 mnt_drop_write(file
->f_path
.mnt
);
1287 * there are many ways the trans_start and trans_end ioctls can lead
1288 * to deadlocks. They should only be used by applications that
1289 * basically own the machine, and have a very in depth understanding
1290 * of all the possible deadlocks and enospc problems.
1292 long btrfs_ioctl_trans_end(struct file
*file
)
1294 struct inode
*inode
= fdentry(file
)->d_inode
;
1295 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1296 struct btrfs_trans_handle
*trans
;
1298 trans
= file
->private_data
;
1301 file
->private_data
= NULL
;
1303 btrfs_end_transaction(trans
, root
);
1305 mutex_lock(&root
->fs_info
->trans_mutex
);
1306 root
->fs_info
->open_ioctl_trans
--;
1307 mutex_unlock(&root
->fs_info
->trans_mutex
);
1309 mnt_drop_write(file
->f_path
.mnt
);
1313 long btrfs_ioctl(struct file
*file
, unsigned int
1314 cmd
, unsigned long arg
)
1316 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1317 void __user
*argp
= (void __user
*)arg
;
1320 case FS_IOC_GETFLAGS
:
1321 return btrfs_ioctl_getflags(file
, argp
);
1322 case FS_IOC_SETFLAGS
:
1323 return btrfs_ioctl_setflags(file
, argp
);
1324 case FS_IOC_GETVERSION
:
1325 return btrfs_ioctl_getversion(file
, argp
);
1326 case BTRFS_IOC_SNAP_CREATE
:
1327 return btrfs_ioctl_snap_create(file
, argp
, 0);
1328 case BTRFS_IOC_SUBVOL_CREATE
:
1329 return btrfs_ioctl_snap_create(file
, argp
, 1);
1330 case BTRFS_IOC_SNAP_DESTROY
:
1331 return btrfs_ioctl_snap_destroy(file
, argp
);
1332 case BTRFS_IOC_DEFRAG
:
1333 return btrfs_ioctl_defrag(file
);
1334 case BTRFS_IOC_RESIZE
:
1335 return btrfs_ioctl_resize(root
, argp
);
1336 case BTRFS_IOC_ADD_DEV
:
1337 return btrfs_ioctl_add_dev(root
, argp
);
1338 case BTRFS_IOC_RM_DEV
:
1339 return btrfs_ioctl_rm_dev(root
, argp
);
1340 case BTRFS_IOC_BALANCE
:
1341 return btrfs_balance(root
->fs_info
->dev_root
);
1342 case BTRFS_IOC_CLONE
:
1343 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
1344 case BTRFS_IOC_CLONE_RANGE
:
1345 return btrfs_ioctl_clone_range(file
, argp
);
1346 case BTRFS_IOC_TRANS_START
:
1347 return btrfs_ioctl_trans_start(file
);
1348 case BTRFS_IOC_TRANS_END
:
1349 return btrfs_ioctl_trans_end(file
);
1350 case BTRFS_IOC_SYNC
:
1351 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);