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
;
240 unsigned long nr
= 1;
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
, 0);
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 nr
= trans
->blocks_used
;
346 err
= btrfs_commit_transaction(trans
, root
);
350 btrfs_unreserve_metadata_space(root
, 6);
351 btrfs_btree_balance_dirty(root
, nr
);
355 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
356 char *name
, int namelen
)
358 struct btrfs_pending_snapshot
*pending_snapshot
;
359 struct btrfs_trans_handle
*trans
;
362 unsigned long nr
= 0;
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 err
= btrfs_commit_transaction(trans
, root
);
401 btrfs_btree_balance_dirty(root
, nr
);
405 /* copy of may_create in fs/namei.c() */
406 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
412 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
416 * Create a new subvolume below @parent. This is largely modeled after
417 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
418 * inside this filesystem so it's quite a bit simpler.
420 static noinline
int btrfs_mksubvol(struct path
*parent
,
421 char *name
, int namelen
,
422 struct btrfs_root
*snap_src
)
424 struct inode
*dir
= parent
->dentry
->d_inode
;
425 struct dentry
*dentry
;
428 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
430 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
431 error
= PTR_ERR(dentry
);
439 error
= mnt_want_write(parent
->mnt
);
443 error
= btrfs_may_create(dir
, dentry
);
447 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
449 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
453 error
= create_snapshot(snap_src
, dentry
,
456 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
460 fsnotify_mkdir(dir
, dentry
);
462 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
464 mnt_drop_write(parent
->mnt
);
468 mutex_unlock(&dir
->i_mutex
);
472 static int btrfs_defrag_file(struct file
*file
)
474 struct inode
*inode
= fdentry(file
)->d_inode
;
475 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
476 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
477 struct btrfs_ordered_extent
*ordered
;
479 unsigned long last_index
;
480 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
481 unsigned long total_read
= 0;
487 ret
= btrfs_check_data_free_space(root
, inode
, inode
->i_size
);
491 mutex_lock(&inode
->i_mutex
);
492 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
493 for (i
= 0; i
<= last_index
; i
++) {
494 if (total_read
% ra_pages
== 0) {
495 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
496 min(last_index
, i
+ ra_pages
- 1));
500 page
= grab_cache_page(inode
->i_mapping
, i
);
503 if (!PageUptodate(page
)) {
504 btrfs_readpage(NULL
, page
);
506 if (!PageUptodate(page
)) {
508 page_cache_release(page
);
513 wait_on_page_writeback(page
);
515 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
516 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
517 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
519 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
521 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
523 page_cache_release(page
);
524 btrfs_start_ordered_extent(inode
, ordered
, 1);
525 btrfs_put_ordered_extent(ordered
);
528 set_page_extent_mapped(page
);
531 * this makes sure page_mkwrite is called on the
532 * page if it is dirtied again later
534 clear_page_dirty_for_io(page
);
536 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
537 set_page_dirty(page
);
538 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
540 page_cache_release(page
);
541 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
545 mutex_unlock(&inode
->i_mutex
);
549 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
555 struct btrfs_ioctl_vol_args
*vol_args
;
556 struct btrfs_trans_handle
*trans
;
557 struct btrfs_device
*device
= NULL
;
564 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
567 if (!capable(CAP_SYS_ADMIN
))
570 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
571 if (IS_ERR(vol_args
))
572 return PTR_ERR(vol_args
);
574 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
575 namelen
= strlen(vol_args
->name
);
577 mutex_lock(&root
->fs_info
->volume_mutex
);
578 sizestr
= vol_args
->name
;
579 devstr
= strchr(sizestr
, ':');
582 sizestr
= devstr
+ 1;
584 devstr
= vol_args
->name
;
585 devid
= simple_strtoull(devstr
, &end
, 10);
586 printk(KERN_INFO
"resizing devid %llu\n",
587 (unsigned long long)devid
);
589 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
591 printk(KERN_INFO
"resizer unable to find device %llu\n",
592 (unsigned long long)devid
);
596 if (!strcmp(sizestr
, "max"))
597 new_size
= device
->bdev
->bd_inode
->i_size
;
599 if (sizestr
[0] == '-') {
602 } else if (sizestr
[0] == '+') {
606 new_size
= btrfs_parse_size(sizestr
);
613 old_size
= device
->total_bytes
;
616 if (new_size
> old_size
) {
620 new_size
= old_size
- new_size
;
621 } else if (mod
> 0) {
622 new_size
= old_size
+ new_size
;
625 if (new_size
< 256 * 1024 * 1024) {
629 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
634 do_div(new_size
, root
->sectorsize
);
635 new_size
*= root
->sectorsize
;
637 printk(KERN_INFO
"new size for %s is %llu\n",
638 device
->name
, (unsigned long long)new_size
);
640 if (new_size
> old_size
) {
641 trans
= btrfs_start_transaction(root
, 1);
642 ret
= btrfs_grow_device(trans
, device
, new_size
);
643 btrfs_commit_transaction(trans
, root
);
645 ret
= btrfs_shrink_device(device
, new_size
);
649 mutex_unlock(&root
->fs_info
->volume_mutex
);
654 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
655 void __user
*arg
, int subvol
)
657 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
658 struct btrfs_ioctl_vol_args
*vol_args
;
659 struct file
*src_file
;
663 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
666 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
667 if (IS_ERR(vol_args
))
668 return PTR_ERR(vol_args
);
670 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
671 namelen
= strlen(vol_args
->name
);
672 if (strchr(vol_args
->name
, '/')) {
678 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
681 struct inode
*src_inode
;
682 src_file
= fget(vol_args
->fd
);
688 src_inode
= src_file
->f_path
.dentry
->d_inode
;
689 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
690 printk(KERN_INFO
"btrfs: Snapshot src from "
696 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
, namelen
,
697 BTRFS_I(src_inode
)->root
);
706 * helper to check if the subvolume references other subvolumes
708 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
710 struct btrfs_path
*path
;
711 struct btrfs_key key
;
714 path
= btrfs_alloc_path();
718 key
.objectid
= root
->root_key
.objectid
;
719 key
.type
= BTRFS_ROOT_REF_KEY
;
720 key
.offset
= (u64
)-1;
722 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
729 if (path
->slots
[0] > 0) {
731 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
732 if (key
.objectid
== root
->root_key
.objectid
&&
733 key
.type
== BTRFS_ROOT_REF_KEY
)
737 btrfs_free_path(path
);
741 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
744 struct dentry
*parent
= fdentry(file
);
745 struct dentry
*dentry
;
746 struct inode
*dir
= parent
->d_inode
;
748 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
749 struct btrfs_root
*dest
= NULL
;
750 struct btrfs_ioctl_vol_args
*vol_args
;
751 struct btrfs_trans_handle
*trans
;
756 if (!capable(CAP_SYS_ADMIN
))
759 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
760 if (IS_ERR(vol_args
))
761 return PTR_ERR(vol_args
);
763 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
764 namelen
= strlen(vol_args
->name
);
765 if (strchr(vol_args
->name
, '/') ||
766 strncmp(vol_args
->name
, "..", namelen
) == 0) {
771 err
= mnt_want_write(file
->f_path
.mnt
);
775 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
776 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
777 if (IS_ERR(dentry
)) {
778 err
= PTR_ERR(dentry
);
782 if (!dentry
->d_inode
) {
787 inode
= dentry
->d_inode
;
788 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
793 dest
= BTRFS_I(inode
)->root
;
795 mutex_lock(&inode
->i_mutex
);
796 err
= d_invalidate(dentry
);
800 down_write(&root
->fs_info
->subvol_sem
);
802 err
= may_destroy_subvol(dest
);
806 trans
= btrfs_start_transaction(root
, 1);
807 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
808 dest
->root_key
.objectid
,
813 btrfs_record_root_in_trans(trans
, dest
);
815 memset(&dest
->root_item
.drop_progress
, 0,
816 sizeof(dest
->root_item
.drop_progress
));
817 dest
->root_item
.drop_level
= 0;
818 btrfs_set_root_refs(&dest
->root_item
, 0);
820 ret
= btrfs_insert_orphan_item(trans
,
821 root
->fs_info
->tree_root
,
822 dest
->root_key
.objectid
);
825 ret
= btrfs_commit_transaction(trans
, root
);
827 inode
->i_flags
|= S_DEAD
;
829 up_write(&root
->fs_info
->subvol_sem
);
831 mutex_unlock(&inode
->i_mutex
);
833 shrink_dcache_sb(root
->fs_info
->sb
);
834 btrfs_invalidate_inodes(dest
);
840 mutex_unlock(&dir
->i_mutex
);
841 mnt_drop_write(file
->f_path
.mnt
);
847 static int btrfs_ioctl_defrag(struct file
*file
)
849 struct inode
*inode
= fdentry(file
)->d_inode
;
850 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
853 ret
= mnt_want_write(file
->f_path
.mnt
);
857 switch (inode
->i_mode
& S_IFMT
) {
859 if (!capable(CAP_SYS_ADMIN
)) {
863 btrfs_defrag_root(root
, 0);
864 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
867 if (!(file
->f_mode
& FMODE_WRITE
)) {
871 btrfs_defrag_file(file
);
875 mnt_drop_write(file
->f_path
.mnt
);
879 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
881 struct btrfs_ioctl_vol_args
*vol_args
;
884 if (!capable(CAP_SYS_ADMIN
))
887 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
888 if (IS_ERR(vol_args
))
889 return PTR_ERR(vol_args
);
891 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
892 ret
= btrfs_init_new_device(root
, vol_args
->name
);
898 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
900 struct btrfs_ioctl_vol_args
*vol_args
;
903 if (!capable(CAP_SYS_ADMIN
))
906 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
909 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
910 if (IS_ERR(vol_args
))
911 return PTR_ERR(vol_args
);
913 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
914 ret
= btrfs_rm_device(root
, vol_args
->name
);
920 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
921 u64 off
, u64 olen
, u64 destoff
)
923 struct inode
*inode
= fdentry(file
)->d_inode
;
924 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
925 struct file
*src_file
;
927 struct btrfs_trans_handle
*trans
;
928 struct btrfs_path
*path
;
929 struct extent_buffer
*leaf
;
931 struct btrfs_key key
;
936 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
941 * - split compressed inline extents. annoying: we need to
942 * decompress into destination's address_space (the file offset
943 * may change, so source mapping won't do), then recompress (or
944 * otherwise reinsert) a subrange.
945 * - allow ranges within the same file to be cloned (provided
946 * they don't overlap)?
949 /* the destination must be opened for writing */
950 if (!(file
->f_mode
& FMODE_WRITE
))
953 ret
= mnt_want_write(file
->f_path
.mnt
);
957 src_file
= fget(srcfd
);
962 src
= src_file
->f_dentry
->d_inode
;
969 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
973 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
977 buf
= vmalloc(btrfs_level_size(root
, 0));
981 path
= btrfs_alloc_path();
989 mutex_lock(&inode
->i_mutex
);
990 mutex_lock(&src
->i_mutex
);
992 mutex_lock(&src
->i_mutex
);
993 mutex_lock(&inode
->i_mutex
);
996 /* determine range to clone */
998 if (off
>= src
->i_size
|| off
+ len
> src
->i_size
)
1001 olen
= len
= src
->i_size
- off
;
1002 /* if we extend to eof, continue to block boundary */
1003 if (off
+ len
== src
->i_size
)
1004 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
1007 /* verify the end result is block aligned */
1008 if ((off
& (bs
-1)) ||
1009 ((off
+ len
) & (bs
-1)))
1012 /* do any pending delalloc/csum calc on src, one way or
1013 another, and lock file content */
1015 struct btrfs_ordered_extent
*ordered
;
1016 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1017 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
1018 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
1020 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1022 btrfs_put_ordered_extent(ordered
);
1023 btrfs_wait_ordered_range(src
, off
, off
+len
);
1026 trans
= btrfs_start_transaction(root
, 1);
1029 /* punch hole in destination first */
1030 btrfs_drop_extents(trans
, root
, inode
, off
, off
+ len
,
1031 off
+ len
, 0, &hint_byte
, 1);
1034 key
.objectid
= src
->i_ino
;
1035 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1040 * note the key will change type as we walk through the
1043 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
1047 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1048 if (path
->slots
[0] >= nritems
) {
1049 ret
= btrfs_next_leaf(root
, path
);
1054 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1056 leaf
= path
->nodes
[0];
1057 slot
= path
->slots
[0];
1059 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1060 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1061 key
.objectid
!= src
->i_ino
)
1064 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1065 struct btrfs_file_extent_item
*extent
;
1068 struct btrfs_key new_key
;
1069 u64 disko
= 0, diskl
= 0;
1070 u64 datao
= 0, datal
= 0;
1073 size
= btrfs_item_size_nr(leaf
, slot
);
1074 read_extent_buffer(leaf
, buf
,
1075 btrfs_item_ptr_offset(leaf
, slot
),
1078 extent
= btrfs_item_ptr(leaf
, slot
,
1079 struct btrfs_file_extent_item
);
1080 comp
= btrfs_file_extent_compression(leaf
, extent
);
1081 type
= btrfs_file_extent_type(leaf
, extent
);
1082 if (type
== BTRFS_FILE_EXTENT_REG
||
1083 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1084 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1086 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1088 datao
= btrfs_file_extent_offset(leaf
, extent
);
1089 datal
= btrfs_file_extent_num_bytes(leaf
,
1091 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1092 /* take upper bound, may be compressed */
1093 datal
= btrfs_file_extent_ram_bytes(leaf
,
1096 btrfs_release_path(root
, path
);
1098 if (key
.offset
+ datal
< off
||
1099 key
.offset
>= off
+len
)
1102 memcpy(&new_key
, &key
, sizeof(new_key
));
1103 new_key
.objectid
= inode
->i_ino
;
1104 new_key
.offset
= key
.offset
+ destoff
- off
;
1106 if (type
== BTRFS_FILE_EXTENT_REG
||
1107 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1108 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1113 leaf
= path
->nodes
[0];
1114 slot
= path
->slots
[0];
1115 write_extent_buffer(leaf
, buf
,
1116 btrfs_item_ptr_offset(leaf
, slot
),
1119 extent
= btrfs_item_ptr(leaf
, slot
,
1120 struct btrfs_file_extent_item
);
1122 if (off
> key
.offset
) {
1123 datao
+= off
- key
.offset
;
1124 datal
-= off
- key
.offset
;
1127 if (key
.offset
+ datal
> off
+ len
)
1128 datal
= off
+ len
- key
.offset
;
1130 /* disko == 0 means it's a hole */
1134 btrfs_set_file_extent_offset(leaf
, extent
,
1136 btrfs_set_file_extent_num_bytes(leaf
, extent
,
1139 inode_add_bytes(inode
, datal
);
1140 ret
= btrfs_inc_extent_ref(trans
, root
,
1142 root
->root_key
.objectid
,
1144 new_key
.offset
- datao
);
1147 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1150 if (off
> key
.offset
) {
1151 skip
= off
- key
.offset
;
1152 new_key
.offset
+= skip
;
1155 if (key
.offset
+ datal
> off
+len
)
1156 trim
= key
.offset
+ datal
- (off
+len
);
1158 if (comp
&& (skip
|| trim
)) {
1162 size
-= skip
+ trim
;
1163 datal
-= skip
+ trim
;
1164 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1171 btrfs_file_extent_calc_inline_size(0);
1172 memmove(buf
+start
, buf
+start
+skip
,
1176 leaf
= path
->nodes
[0];
1177 slot
= path
->slots
[0];
1178 write_extent_buffer(leaf
, buf
,
1179 btrfs_item_ptr_offset(leaf
, slot
),
1181 inode_add_bytes(inode
, datal
);
1184 btrfs_mark_buffer_dirty(leaf
);
1188 btrfs_release_path(root
, path
);
1193 btrfs_release_path(root
, path
);
1195 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1196 if (destoff
+ olen
> inode
->i_size
)
1197 btrfs_i_size_write(inode
, destoff
+ olen
);
1198 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1199 ret
= btrfs_update_inode(trans
, root
, inode
);
1201 btrfs_end_transaction(trans
, root
);
1202 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1204 vmtruncate(inode
, 0);
1206 mutex_unlock(&src
->i_mutex
);
1207 mutex_unlock(&inode
->i_mutex
);
1209 btrfs_free_path(path
);
1213 mnt_drop_write(file
->f_path
.mnt
);
1217 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1219 struct btrfs_ioctl_clone_range_args args
;
1221 if (copy_from_user(&args
, argp
, sizeof(args
)))
1223 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1224 args
.src_length
, args
.dest_offset
);
1228 * there are many ways the trans_start and trans_end ioctls can lead
1229 * to deadlocks. They should only be used by applications that
1230 * basically own the machine, and have a very in depth understanding
1231 * of all the possible deadlocks and enospc problems.
1233 static long btrfs_ioctl_trans_start(struct file
*file
)
1235 struct inode
*inode
= fdentry(file
)->d_inode
;
1236 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1237 struct btrfs_trans_handle
*trans
;
1241 if (!capable(CAP_SYS_ADMIN
))
1245 if (file
->private_data
)
1248 ret
= mnt_want_write(file
->f_path
.mnt
);
1252 mutex_lock(&root
->fs_info
->trans_mutex
);
1253 root
->fs_info
->open_ioctl_trans
++;
1254 mutex_unlock(&root
->fs_info
->trans_mutex
);
1257 trans
= btrfs_start_ioctl_transaction(root
, 0);
1261 file
->private_data
= trans
;
1265 mutex_lock(&root
->fs_info
->trans_mutex
);
1266 root
->fs_info
->open_ioctl_trans
--;
1267 mutex_unlock(&root
->fs_info
->trans_mutex
);
1268 mnt_drop_write(file
->f_path
.mnt
);
1274 * there are many ways the trans_start and trans_end ioctls can lead
1275 * to deadlocks. They should only be used by applications that
1276 * basically own the machine, and have a very in depth understanding
1277 * of all the possible deadlocks and enospc problems.
1279 long btrfs_ioctl_trans_end(struct file
*file
)
1281 struct inode
*inode
= fdentry(file
)->d_inode
;
1282 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1283 struct btrfs_trans_handle
*trans
;
1285 trans
= file
->private_data
;
1288 file
->private_data
= NULL
;
1290 btrfs_end_transaction(trans
, root
);
1292 mutex_lock(&root
->fs_info
->trans_mutex
);
1293 root
->fs_info
->open_ioctl_trans
--;
1294 mutex_unlock(&root
->fs_info
->trans_mutex
);
1296 mnt_drop_write(file
->f_path
.mnt
);
1300 long btrfs_ioctl(struct file
*file
, unsigned int
1301 cmd
, unsigned long arg
)
1303 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1304 void __user
*argp
= (void __user
*)arg
;
1307 case FS_IOC_GETFLAGS
:
1308 return btrfs_ioctl_getflags(file
, argp
);
1309 case FS_IOC_SETFLAGS
:
1310 return btrfs_ioctl_setflags(file
, argp
);
1311 case FS_IOC_GETVERSION
:
1312 return btrfs_ioctl_getversion(file
, argp
);
1313 case BTRFS_IOC_SNAP_CREATE
:
1314 return btrfs_ioctl_snap_create(file
, argp
, 0);
1315 case BTRFS_IOC_SUBVOL_CREATE
:
1316 return btrfs_ioctl_snap_create(file
, argp
, 1);
1317 case BTRFS_IOC_SNAP_DESTROY
:
1318 return btrfs_ioctl_snap_destroy(file
, argp
);
1319 case BTRFS_IOC_DEFRAG
:
1320 return btrfs_ioctl_defrag(file
);
1321 case BTRFS_IOC_RESIZE
:
1322 return btrfs_ioctl_resize(root
, argp
);
1323 case BTRFS_IOC_ADD_DEV
:
1324 return btrfs_ioctl_add_dev(root
, argp
);
1325 case BTRFS_IOC_RM_DEV
:
1326 return btrfs_ioctl_rm_dev(root
, argp
);
1327 case BTRFS_IOC_BALANCE
:
1328 return btrfs_balance(root
->fs_info
->dev_root
);
1329 case BTRFS_IOC_CLONE
:
1330 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
1331 case BTRFS_IOC_CLONE_RANGE
:
1332 return btrfs_ioctl_clone_range(file
, argp
);
1333 case BTRFS_IOC_TRANS_START
:
1334 return btrfs_ioctl_trans_start(file
);
1335 case BTRFS_IOC_TRANS_END
:
1336 return btrfs_ioctl_trans_end(file
);
1337 case BTRFS_IOC_SYNC
:
1338 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);