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/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
41 #include "transaction.h"
42 #include "btrfs_inode.h"
44 #include "print-tree.h"
50 static noinline
int create_subvol(struct btrfs_root
*root
, char *name
,
53 struct btrfs_trans_handle
*trans
;
55 struct btrfs_root_item root_item
;
56 struct btrfs_inode_item
*inode_item
;
57 struct extent_buffer
*leaf
;
58 struct btrfs_root
*new_root
= root
;
63 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
66 ret
= btrfs_check_free_space(root
, 1, 0);
70 trans
= btrfs_start_transaction(root
, 1);
73 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
78 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
79 objectid
, trans
->transid
, 0, 0,
84 btrfs_set_header_nritems(leaf
, 0);
85 btrfs_set_header_level(leaf
, 0);
86 btrfs_set_header_bytenr(leaf
, leaf
->start
);
87 btrfs_set_header_generation(leaf
, trans
->transid
);
88 btrfs_set_header_owner(leaf
, objectid
);
90 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
91 (unsigned long)btrfs_header_fsid(leaf
),
93 btrfs_mark_buffer_dirty(leaf
);
95 inode_item
= &root_item
.inode
;
96 memset(inode_item
, 0, sizeof(*inode_item
));
97 inode_item
->generation
= cpu_to_le64(1);
98 inode_item
->size
= cpu_to_le64(3);
99 inode_item
->nlink
= cpu_to_le32(1);
100 inode_item
->nblocks
= cpu_to_le64(1);
101 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
103 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
104 btrfs_set_root_level(&root_item
, 0);
105 btrfs_set_root_refs(&root_item
, 1);
106 btrfs_set_root_used(&root_item
, 0);
108 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
109 root_item
.drop_level
= 0;
111 btrfs_tree_unlock(leaf
);
112 free_extent_buffer(leaf
);
115 btrfs_set_root_dirid(&root_item
, new_dirid
);
117 key
.objectid
= objectid
;
119 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
120 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
126 * insert the directory item
128 key
.offset
= (u64
)-1;
129 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
130 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
131 name
, namelen
, dir
->i_ino
, &key
,
136 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
137 name
, namelen
, objectid
,
138 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
142 ret
= btrfs_commit_transaction(trans
, root
);
146 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
149 trans
= btrfs_start_transaction(new_root
, 1);
152 ret
= btrfs_create_subvol_root(new_root
, trans
, new_dirid
,
153 BTRFS_I(dir
)->block_group
);
157 /* Invalidate existing dcache entry for new subvolume. */
158 btrfs_invalidate_dcache_root(root
, name
, namelen
);
161 nr
= trans
->blocks_used
;
162 err
= btrfs_commit_transaction(trans
, new_root
);
166 btrfs_btree_balance_dirty(root
, nr
);
170 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
172 struct btrfs_pending_snapshot
*pending_snapshot
;
173 struct btrfs_trans_handle
*trans
;
176 unsigned long nr
= 0;
181 ret
= btrfs_check_free_space(root
, 1, 0);
185 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
186 if (!pending_snapshot
) {
190 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
191 if (!pending_snapshot
->name
) {
193 kfree(pending_snapshot
);
196 memcpy(pending_snapshot
->name
, name
, namelen
);
197 pending_snapshot
->name
[namelen
] = '\0';
198 trans
= btrfs_start_transaction(root
, 1);
200 pending_snapshot
->root
= root
;
201 list_add(&pending_snapshot
->list
,
202 &trans
->transaction
->pending_snapshots
);
203 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
204 err
= btrfs_commit_transaction(trans
, root
);
207 btrfs_btree_balance_dirty(root
, nr
);
211 int btrfs_defrag_file(struct file
*file
)
213 struct inode
*inode
= fdentry(file
)->d_inode
;
214 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
215 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
217 unsigned long last_index
;
218 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
219 unsigned long total_read
= 0;
225 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
229 mutex_lock(&inode
->i_mutex
);
230 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
231 for (i
= 0; i
<= last_index
; i
++) {
232 if (total_read
% ra_pages
== 0) {
233 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
234 min(last_index
, i
+ ra_pages
- 1));
237 page
= grab_cache_page(inode
->i_mapping
, i
);
240 if (!PageUptodate(page
)) {
241 btrfs_readpage(NULL
, page
);
243 if (!PageUptodate(page
)) {
245 page_cache_release(page
);
250 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
251 ClearPageDirty(page
);
253 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
255 wait_on_page_writeback(page
);
256 set_page_extent_mapped(page
);
258 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
259 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
261 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
262 set_extent_delalloc(io_tree
, page_start
,
265 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
266 set_page_dirty(page
);
268 page_cache_release(page
);
269 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
273 mutex_unlock(&inode
->i_mutex
);
278 * Called inside transaction, so use GFP_NOFS
281 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
286 struct btrfs_ioctl_vol_args
*vol_args
;
287 struct btrfs_trans_handle
*trans
;
288 struct btrfs_device
*device
= NULL
;
295 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
300 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
304 namelen
= strlen(vol_args
->name
);
305 if (namelen
> BTRFS_VOL_NAME_MAX
) {
310 mutex_lock(&root
->fs_info
->volume_mutex
);
311 sizestr
= vol_args
->name
;
312 devstr
= strchr(sizestr
, ':');
315 sizestr
= devstr
+ 1;
317 devstr
= vol_args
->name
;
318 devid
= simple_strtoull(devstr
, &end
, 10);
319 printk(KERN_INFO
"resizing devid %llu\n", devid
);
321 device
= btrfs_find_device(root
, devid
, NULL
);
323 printk(KERN_INFO
"resizer unable to find device %llu\n", devid
);
327 if (!strcmp(sizestr
, "max"))
328 new_size
= device
->bdev
->bd_inode
->i_size
;
330 if (sizestr
[0] == '-') {
333 } else if (sizestr
[0] == '+') {
337 new_size
= btrfs_parse_size(sizestr
);
344 old_size
= device
->total_bytes
;
347 if (new_size
> old_size
) {
351 new_size
= old_size
- new_size
;
352 } else if (mod
> 0) {
353 new_size
= old_size
+ new_size
;
356 if (new_size
< 256 * 1024 * 1024) {
360 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
365 do_div(new_size
, root
->sectorsize
);
366 new_size
*= root
->sectorsize
;
368 printk(KERN_INFO
"new size for %s is %llu\n",
369 device
->name
, (unsigned long long)new_size
);
371 if (new_size
> old_size
) {
372 trans
= btrfs_start_transaction(root
, 1);
373 ret
= btrfs_grow_device(trans
, device
, new_size
);
374 btrfs_commit_transaction(trans
, root
);
376 ret
= btrfs_shrink_device(device
, new_size
);
380 mutex_unlock(&root
->fs_info
->volume_mutex
);
386 static noinline
int btrfs_ioctl_snap_create(struct btrfs_root
*root
,
389 struct btrfs_ioctl_vol_args
*vol_args
;
390 struct btrfs_dir_item
*di
;
391 struct btrfs_path
*path
;
396 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
401 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
406 namelen
= strlen(vol_args
->name
);
407 if (namelen
> BTRFS_VOL_NAME_MAX
) {
411 if (strchr(vol_args
->name
, '/')) {
416 path
= btrfs_alloc_path();
422 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
423 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
425 vol_args
->name
, namelen
, 0);
426 btrfs_free_path(path
);
428 if (di
&& !IS_ERR(di
)) {
438 mutex_lock(&root
->fs_info
->drop_mutex
);
439 if (root
== root
->fs_info
->tree_root
)
440 ret
= create_subvol(root
, vol_args
->name
, namelen
);
442 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
443 mutex_unlock(&root
->fs_info
->drop_mutex
);
449 static int btrfs_ioctl_defrag(struct file
*file
)
451 struct inode
*inode
= fdentry(file
)->d_inode
;
452 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
454 switch (inode
->i_mode
& S_IFMT
) {
456 btrfs_defrag_root(root
, 0);
457 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
460 btrfs_defrag_file(file
);
467 long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
469 struct btrfs_ioctl_vol_args
*vol_args
;
472 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
477 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
481 ret
= btrfs_init_new_device(root
, vol_args
->name
);
488 long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
490 struct btrfs_ioctl_vol_args
*vol_args
;
493 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
498 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
502 ret
= btrfs_rm_device(root
, vol_args
->name
);
509 int dup_item_to_inode(struct btrfs_trans_handle
*trans
,
510 struct btrfs_root
*root
,
511 struct btrfs_path
*path
,
512 struct extent_buffer
*leaf
,
514 struct btrfs_key
*key
,
518 int len
= btrfs_item_size_nr(leaf
, slot
);
519 struct btrfs_key ckey
= *key
;
522 dup
= kmalloc(len
, GFP_NOFS
);
526 read_extent_buffer(leaf
, dup
, btrfs_item_ptr_offset(leaf
, slot
), len
);
527 btrfs_release_path(root
, path
);
529 ckey
.objectid
= destino
;
530 ret
= btrfs_insert_item(trans
, root
, &ckey
, dup
, len
);
535 long btrfs_ioctl_clone(struct file
*file
, unsigned long src_fd
)
537 struct inode
*inode
= fdentry(file
)->d_inode
;
538 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
539 struct file
*src_file
;
541 struct btrfs_trans_handle
*trans
;
544 struct btrfs_path
*path
;
545 struct btrfs_key key
;
546 struct extent_buffer
*leaf
;
550 src_file
= fget(src_fd
);
553 src
= src_file
->f_dentry
->d_inode
;
556 if (src
->i_sb
!= inode
->i_sb
)
560 mutex_lock(&inode
->i_mutex
);
561 mutex_lock(&src
->i_mutex
);
563 mutex_lock(&src
->i_mutex
);
564 mutex_lock(&inode
->i_mutex
);
571 /* do any pending delalloc/csum calc on src, one way or
572 another, and lock file content */
574 filemap_write_and_wait(src
->i_mapping
);
575 lock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
576 if (BTRFS_I(src
)->delalloc_bytes
== 0)
578 unlock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
581 trans
= btrfs_start_transaction(root
, 0);
582 path
= btrfs_alloc_path();
588 key
.type
= BTRFS_EXTENT_DATA_KEY
;
589 key
.objectid
= src
->i_ino
;
595 * note the key will change type as we walk through the
598 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
602 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
603 ret
= btrfs_next_leaf(root
, path
);
609 leaf
= path
->nodes
[0];
610 slot
= path
->slots
[0];
611 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
612 nritems
= btrfs_header_nritems(leaf
);
614 if (btrfs_key_type(&key
) > BTRFS_CSUM_ITEM_KEY
||
615 key
.objectid
!= src
->i_ino
)
618 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
619 struct btrfs_file_extent_item
*extent
;
622 extent
= btrfs_item_ptr(leaf
, slot
,
623 struct btrfs_file_extent_item
);
624 found_type
= btrfs_file_extent_type(leaf
, extent
);
625 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
626 u64 len
= btrfs_file_extent_num_bytes(leaf
,
628 u64 ds
= btrfs_file_extent_disk_bytenr(leaf
,
630 u64 dl
= btrfs_file_extent_disk_num_bytes(leaf
,
632 u64 off
= btrfs_file_extent_offset(leaf
,
634 btrfs_insert_file_extent(trans
, root
,
637 /* ds == 0 means there's a hole */
639 btrfs_inc_extent_ref(trans
, root
,
641 root
->root_key
.objectid
,
645 pos
= key
.offset
+ len
;
646 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
647 ret
= dup_item_to_inode(trans
, root
, path
,
652 pos
= key
.offset
+ btrfs_item_size_nr(leaf
,
655 } else if (btrfs_key_type(&key
) == BTRFS_CSUM_ITEM_KEY
) {
656 ret
= dup_item_to_inode(trans
, root
, path
, leaf
,
657 slot
, &key
, inode
->i_ino
);
663 btrfs_release_path(root
, path
);
668 btrfs_free_path(path
);
670 inode
->i_blocks
= src
->i_blocks
;
671 i_size_write(inode
, src
->i_size
);
672 btrfs_update_inode(trans
, root
, inode
);
674 unlock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
676 btrfs_end_transaction(trans
, root
);
679 mutex_unlock(&src
->i_mutex
);
680 mutex_unlock(&inode
->i_mutex
);
687 * there are many ways the trans_start and trans_end ioctls can lead
688 * to deadlocks. They should only be used by applications that
689 * basically own the machine, and have a very in depth understanding
690 * of all the possible deadlocks and enospc problems.
692 long btrfs_ioctl_trans_start(struct file
*file
)
694 struct inode
*inode
= fdentry(file
)->d_inode
;
695 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
696 struct btrfs_trans_handle
*trans
;
699 if (!capable(CAP_SYS_ADMIN
))
702 if (file
->private_data
) {
706 trans
= btrfs_start_transaction(root
, 0);
708 file
->private_data
= trans
;
711 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
717 * there are many ways the trans_start and trans_end ioctls can lead
718 * to deadlocks. They should only be used by applications that
719 * basically own the machine, and have a very in depth understanding
720 * of all the possible deadlocks and enospc problems.
722 long btrfs_ioctl_trans_end(struct file
*file
)
724 struct inode
*inode
= fdentry(file
)->d_inode
;
725 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
726 struct btrfs_trans_handle
*trans
;
729 trans
= file
->private_data
;
734 btrfs_end_transaction(trans
, root
);
735 file
->private_data
= 0;
740 long btrfs_ioctl(struct file
*file
, unsigned int
741 cmd
, unsigned long arg
)
743 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
746 case BTRFS_IOC_SNAP_CREATE
:
747 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
748 case BTRFS_IOC_DEFRAG
:
749 return btrfs_ioctl_defrag(file
);
750 case BTRFS_IOC_RESIZE
:
751 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
752 case BTRFS_IOC_ADD_DEV
:
753 return btrfs_ioctl_add_dev(root
, (void __user
*)arg
);
754 case BTRFS_IOC_RM_DEV
:
755 return btrfs_ioctl_rm_dev(root
, (void __user
*)arg
);
756 case BTRFS_IOC_BALANCE
:
757 return btrfs_balance(root
->fs_info
->dev_root
);
758 case BTRFS_IOC_CLONE
:
759 return btrfs_ioctl_clone(file
, arg
);
760 case BTRFS_IOC_TRANS_START
:
761 return btrfs_ioctl_trans_start(file
);
762 case BTRFS_IOC_TRANS_END
:
763 return btrfs_ioctl_trans_end(file
);
765 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);