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>
39 #include <linux/vmalloc.h>
42 #include "transaction.h"
43 #include "btrfs_inode.h"
45 #include "print-tree.h"
51 static noinline
int create_subvol(struct btrfs_root
*root
, char *name
,
54 struct btrfs_trans_handle
*trans
;
56 struct btrfs_root_item root_item
;
57 struct btrfs_inode_item
*inode_item
;
58 struct extent_buffer
*leaf
;
59 struct btrfs_root
*new_root
= root
;
64 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
67 ret
= btrfs_check_free_space(root
, 1, 0);
71 trans
= btrfs_start_transaction(root
, 1);
74 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
79 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
80 objectid
, trans
->transid
, 0, 0,
87 btrfs_set_header_nritems(leaf
, 0);
88 btrfs_set_header_level(leaf
, 0);
89 btrfs_set_header_bytenr(leaf
, leaf
->start
);
90 btrfs_set_header_generation(leaf
, trans
->transid
);
91 btrfs_set_header_owner(leaf
, objectid
);
93 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
94 (unsigned long)btrfs_header_fsid(leaf
),
96 btrfs_mark_buffer_dirty(leaf
);
98 inode_item
= &root_item
.inode
;
99 memset(inode_item
, 0, sizeof(*inode_item
));
100 inode_item
->generation
= cpu_to_le64(1);
101 inode_item
->size
= cpu_to_le64(3);
102 inode_item
->nlink
= cpu_to_le32(1);
103 inode_item
->nblocks
= cpu_to_le64(1);
104 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
106 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
107 btrfs_set_root_level(&root_item
, 0);
108 btrfs_set_root_refs(&root_item
, 1);
109 btrfs_set_root_used(&root_item
, 0);
111 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
112 root_item
.drop_level
= 0;
114 btrfs_tree_unlock(leaf
);
115 free_extent_buffer(leaf
);
118 btrfs_set_root_dirid(&root_item
, new_dirid
);
120 key
.objectid
= objectid
;
122 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
123 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
129 * insert the directory item
131 key
.offset
= (u64
)-1;
132 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
133 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
134 name
, namelen
, dir
->i_ino
, &key
,
139 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
140 name
, namelen
, objectid
,
141 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
, 0);
145 ret
= btrfs_commit_transaction(trans
, root
);
149 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
152 trans
= btrfs_start_transaction(new_root
, 1);
155 ret
= btrfs_create_subvol_root(new_root
, trans
, new_dirid
,
156 BTRFS_I(dir
)->block_group
);
160 /* Invalidate existing dcache entry for new subvolume. */
161 btrfs_invalidate_dcache_root(root
, name
, namelen
);
164 nr
= trans
->blocks_used
;
165 err
= btrfs_commit_transaction(trans
, new_root
);
169 btrfs_btree_balance_dirty(root
, nr
);
173 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
175 struct btrfs_pending_snapshot
*pending_snapshot
;
176 struct btrfs_trans_handle
*trans
;
179 unsigned long nr
= 0;
184 ret
= btrfs_check_free_space(root
, 1, 0);
188 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
189 if (!pending_snapshot
) {
193 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
194 if (!pending_snapshot
->name
) {
196 kfree(pending_snapshot
);
199 memcpy(pending_snapshot
->name
, name
, namelen
);
200 pending_snapshot
->name
[namelen
] = '\0';
201 trans
= btrfs_start_transaction(root
, 1);
203 pending_snapshot
->root
= root
;
204 list_add(&pending_snapshot
->list
,
205 &trans
->transaction
->pending_snapshots
);
206 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
207 err
= btrfs_commit_transaction(trans
, root
);
210 btrfs_btree_balance_dirty(root
, nr
);
214 int btrfs_defrag_file(struct file
*file
)
216 struct inode
*inode
= fdentry(file
)->d_inode
;
217 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
218 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
219 struct btrfs_ordered_extent
*ordered
;
221 unsigned long last_index
;
222 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
223 unsigned long total_read
= 0;
229 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
233 mutex_lock(&inode
->i_mutex
);
234 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
235 for (i
= 0; i
<= last_index
; i
++) {
236 if (total_read
% ra_pages
== 0) {
237 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
238 min(last_index
, i
+ ra_pages
- 1));
242 page
= grab_cache_page(inode
->i_mapping
, i
);
245 if (!PageUptodate(page
)) {
246 btrfs_readpage(NULL
, page
);
248 if (!PageUptodate(page
)) {
250 page_cache_release(page
);
255 wait_on_page_writeback(page
);
257 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
258 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
259 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
261 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
263 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
265 page_cache_release(page
);
266 btrfs_start_ordered_extent(inode
, ordered
, 1);
267 btrfs_put_ordered_extent(ordered
);
270 set_page_extent_mapped(page
);
273 * this makes sure page_mkwrite is called on the
274 * page if it is dirtied again later
276 clear_page_dirty_for_io(page
);
278 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
280 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
281 set_page_dirty(page
);
283 page_cache_release(page
);
284 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
288 mutex_unlock(&inode
->i_mutex
);
293 * Called inside transaction, so use GFP_NOFS
296 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
301 struct btrfs_ioctl_vol_args
*vol_args
;
302 struct btrfs_trans_handle
*trans
;
303 struct btrfs_device
*device
= NULL
;
310 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
315 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
320 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
321 namelen
= strlen(vol_args
->name
);
323 mutex_lock(&root
->fs_info
->volume_mutex
);
324 sizestr
= vol_args
->name
;
325 devstr
= strchr(sizestr
, ':');
328 sizestr
= devstr
+ 1;
330 devstr
= vol_args
->name
;
331 devid
= simple_strtoull(devstr
, &end
, 10);
332 printk(KERN_INFO
"resizing devid %llu\n", devid
);
334 device
= btrfs_find_device(root
, devid
, NULL
);
336 printk(KERN_INFO
"resizer unable to find device %llu\n", devid
);
340 if (!strcmp(sizestr
, "max"))
341 new_size
= device
->bdev
->bd_inode
->i_size
;
343 if (sizestr
[0] == '-') {
346 } else if (sizestr
[0] == '+') {
350 new_size
= btrfs_parse_size(sizestr
);
357 old_size
= device
->total_bytes
;
360 if (new_size
> old_size
) {
364 new_size
= old_size
- new_size
;
365 } else if (mod
> 0) {
366 new_size
= old_size
+ new_size
;
369 if (new_size
< 256 * 1024 * 1024) {
373 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
378 do_div(new_size
, root
->sectorsize
);
379 new_size
*= root
->sectorsize
;
381 printk(KERN_INFO
"new size for %s is %llu\n",
382 device
->name
, (unsigned long long)new_size
);
384 if (new_size
> old_size
) {
385 trans
= btrfs_start_transaction(root
, 1);
386 ret
= btrfs_grow_device(trans
, device
, new_size
);
387 btrfs_commit_transaction(trans
, root
);
389 ret
= btrfs_shrink_device(device
, new_size
);
393 mutex_unlock(&root
->fs_info
->volume_mutex
);
399 static noinline
int btrfs_ioctl_snap_create(struct btrfs_root
*root
,
402 struct btrfs_ioctl_vol_args
*vol_args
;
403 struct btrfs_dir_item
*di
;
404 struct btrfs_path
*path
;
409 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
414 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
419 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
420 namelen
= strlen(vol_args
->name
);
421 if (strchr(vol_args
->name
, '/')) {
426 path
= btrfs_alloc_path();
432 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
433 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
435 vol_args
->name
, namelen
, 0);
436 btrfs_free_path(path
);
438 if (di
&& !IS_ERR(di
)) {
448 mutex_lock(&root
->fs_info
->drop_mutex
);
449 if (root
== root
->fs_info
->tree_root
)
450 ret
= create_subvol(root
, vol_args
->name
, namelen
);
452 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
453 mutex_unlock(&root
->fs_info
->drop_mutex
);
459 static int btrfs_ioctl_defrag(struct file
*file
)
461 struct inode
*inode
= fdentry(file
)->d_inode
;
462 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
464 switch (inode
->i_mode
& S_IFMT
) {
466 btrfs_defrag_root(root
, 0);
467 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
470 btrfs_defrag_file(file
);
477 long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
479 struct btrfs_ioctl_vol_args
*vol_args
;
482 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
487 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
491 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
492 ret
= btrfs_init_new_device(root
, vol_args
->name
);
499 long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
501 struct btrfs_ioctl_vol_args
*vol_args
;
504 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
509 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
513 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
514 ret
= btrfs_rm_device(root
, vol_args
->name
);
521 long btrfs_ioctl_clone(struct file
*file
, unsigned long src_fd
)
523 struct inode
*inode
= fdentry(file
)->d_inode
;
524 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
525 struct file
*src_file
;
527 struct btrfs_trans_handle
*trans
;
528 struct btrfs_ordered_extent
*ordered
;
529 struct btrfs_path
*path
;
530 struct extent_buffer
*leaf
;
532 struct btrfs_key key
;
533 struct btrfs_key new_key
;
539 src_file
= fget(src_fd
);
542 src
= src_file
->f_dentry
->d_inode
;
545 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
549 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
553 buf
= vmalloc(btrfs_level_size(root
, 0));
557 path
= btrfs_alloc_path();
565 mutex_lock(&inode
->i_mutex
);
566 mutex_lock(&src
->i_mutex
);
568 mutex_lock(&src
->i_mutex
);
569 mutex_lock(&inode
->i_mutex
);
576 /* do any pending delalloc/csum calc on src, one way or
577 another, and lock file content */
579 lock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
580 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
581 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
583 unlock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
585 btrfs_put_ordered_extent(ordered
);
586 btrfs_wait_ordered_range(src
, 0, (u64
)-1);
589 trans
= btrfs_start_transaction(root
, 1);
592 key
.objectid
= src
->i_ino
;
593 key
.type
= BTRFS_EXTENT_DATA_KEY
;
598 * note the key will change type as we walk through the
601 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
605 nritems
= btrfs_header_nritems(path
->nodes
[0]);
606 if (path
->slots
[0] >= nritems
) {
607 ret
= btrfs_next_leaf(root
, path
);
612 nritems
= btrfs_header_nritems(path
->nodes
[0]);
614 leaf
= path
->nodes
[0];
615 slot
= path
->slots
[0];
617 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
618 if (btrfs_key_type(&key
) > BTRFS_CSUM_ITEM_KEY
||
619 key
.objectid
!= src
->i_ino
)
622 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
623 struct btrfs_file_extent_item
*extent
;
626 extent
= btrfs_item_ptr(leaf
, slot
,
627 struct btrfs_file_extent_item
);
628 found_type
= btrfs_file_extent_type(leaf
, extent
);
629 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
630 u64 ds
= btrfs_file_extent_disk_bytenr(leaf
,
632 u64 dl
= btrfs_file_extent_disk_num_bytes(leaf
,
634 /* ds == 0 means there's a hole */
636 ret
= btrfs_inc_extent_ref(trans
, root
,
638 root
->root_key
.objectid
,
640 inode
->i_ino
, key
.offset
);
647 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
||
648 btrfs_key_type(&key
) == BTRFS_CSUM_ITEM_KEY
) {
649 size
= btrfs_item_size_nr(leaf
, slot
);
650 read_extent_buffer(leaf
, buf
,
651 btrfs_item_ptr_offset(leaf
, slot
),
653 btrfs_release_path(root
, path
);
654 memcpy(&new_key
, &key
, sizeof(new_key
));
655 new_key
.objectid
= inode
->i_ino
;
656 ret
= btrfs_insert_item(trans
, root
, &new_key
,
660 btrfs_release_path(root
, path
);
666 btrfs_release_path(root
, path
);
668 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
669 inode
->i_blocks
= src
->i_blocks
;
670 btrfs_i_size_write(inode
, src
->i_size
);
671 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
672 ret
= btrfs_update_inode(trans
, root
, inode
);
674 btrfs_end_transaction(trans
, root
);
675 unlock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
677 vmtruncate(inode
, 0);
679 mutex_unlock(&src
->i_mutex
);
680 mutex_unlock(&inode
->i_mutex
);
682 btrfs_free_path(path
);
689 * there are many ways the trans_start and trans_end ioctls can lead
690 * to deadlocks. They should only be used by applications that
691 * basically own the machine, and have a very in depth understanding
692 * of all the possible deadlocks and enospc problems.
694 long btrfs_ioctl_trans_start(struct file
*file
)
696 struct inode
*inode
= fdentry(file
)->d_inode
;
697 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
698 struct btrfs_trans_handle
*trans
;
701 if (!capable(CAP_SYS_ADMIN
))
704 if (file
->private_data
) {
709 mutex_lock(&root
->fs_info
->trans_mutex
);
710 root
->fs_info
->open_ioctl_trans
++;
711 mutex_unlock(&root
->fs_info
->trans_mutex
);
713 trans
= btrfs_start_ioctl_transaction(root
, 0);
715 file
->private_data
= trans
;
718 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
724 * there are many ways the trans_start and trans_end ioctls can lead
725 * to deadlocks. They should only be used by applications that
726 * basically own the machine, and have a very in depth understanding
727 * of all the possible deadlocks and enospc problems.
729 long btrfs_ioctl_trans_end(struct file
*file
)
731 struct inode
*inode
= fdentry(file
)->d_inode
;
732 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
733 struct btrfs_trans_handle
*trans
;
736 trans
= file
->private_data
;
741 btrfs_end_transaction(trans
, root
);
742 file
->private_data
= 0;
744 mutex_lock(&root
->fs_info
->trans_mutex
);
745 root
->fs_info
->open_ioctl_trans
--;
746 mutex_unlock(&root
->fs_info
->trans_mutex
);
752 long btrfs_ioctl(struct file
*file
, unsigned int
753 cmd
, unsigned long arg
)
755 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
758 case BTRFS_IOC_SNAP_CREATE
:
759 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
760 case BTRFS_IOC_DEFRAG
:
761 return btrfs_ioctl_defrag(file
);
762 case BTRFS_IOC_RESIZE
:
763 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
764 case BTRFS_IOC_ADD_DEV
:
765 return btrfs_ioctl_add_dev(root
, (void __user
*)arg
);
766 case BTRFS_IOC_RM_DEV
:
767 return btrfs_ioctl_rm_dev(root
, (void __user
*)arg
);
768 case BTRFS_IOC_BALANCE
:
769 return btrfs_balance(root
->fs_info
->dev_root
);
770 case BTRFS_IOC_CLONE
:
771 return btrfs_ioctl_clone(file
, arg
);
772 case BTRFS_IOC_TRANS_START
:
773 return btrfs_ioctl_trans_start(file
);
774 case BTRFS_IOC_TRANS_END
:
775 return btrfs_ioctl_trans_end(file
);
777 btrfs_start_delalloc_inodes(root
);
778 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);