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
, 0,
80 objectid
, trans
->transid
, 0, 0, 0);
86 btrfs_set_header_nritems(leaf
, 0);
87 btrfs_set_header_level(leaf
, 0);
88 btrfs_set_header_bytenr(leaf
, leaf
->start
);
89 btrfs_set_header_generation(leaf
, trans
->transid
);
90 btrfs_set_header_owner(leaf
, objectid
);
92 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
93 (unsigned long)btrfs_header_fsid(leaf
),
95 btrfs_mark_buffer_dirty(leaf
);
97 inode_item
= &root_item
.inode
;
98 memset(inode_item
, 0, sizeof(*inode_item
));
99 inode_item
->generation
= cpu_to_le64(1);
100 inode_item
->size
= cpu_to_le64(3);
101 inode_item
->nlink
= cpu_to_le32(1);
102 inode_item
->nblocks
= cpu_to_le64(1);
103 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
105 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
106 btrfs_set_root_level(&root_item
, 0);
107 btrfs_set_root_refs(&root_item
, 1);
108 btrfs_set_root_used(&root_item
, 0);
110 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
111 root_item
.drop_level
= 0;
113 btrfs_tree_unlock(leaf
);
114 free_extent_buffer(leaf
);
117 btrfs_set_root_dirid(&root_item
, new_dirid
);
119 key
.objectid
= objectid
;
121 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
122 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
128 * insert the directory item
130 key
.offset
= (u64
)-1;
131 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
132 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
133 name
, namelen
, dir
->i_ino
, &key
,
138 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
139 name
, namelen
, objectid
,
140 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
, 0);
144 ret
= btrfs_commit_transaction(trans
, root
);
148 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
151 trans
= btrfs_start_transaction(new_root
, 1);
154 ret
= btrfs_create_subvol_root(new_root
, trans
, new_dirid
,
155 BTRFS_I(dir
)->block_group
);
159 /* Invalidate existing dcache entry for new subvolume. */
160 btrfs_invalidate_dcache_root(root
, name
, namelen
);
163 nr
= trans
->blocks_used
;
164 err
= btrfs_commit_transaction(trans
, new_root
);
168 btrfs_btree_balance_dirty(root
, nr
);
172 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
174 struct btrfs_pending_snapshot
*pending_snapshot
;
175 struct btrfs_trans_handle
*trans
;
178 unsigned long nr
= 0;
183 ret
= btrfs_check_free_space(root
, 1, 0);
187 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
188 if (!pending_snapshot
) {
192 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
193 if (!pending_snapshot
->name
) {
195 kfree(pending_snapshot
);
198 memcpy(pending_snapshot
->name
, name
, namelen
);
199 pending_snapshot
->name
[namelen
] = '\0';
200 trans
= btrfs_start_transaction(root
, 1);
202 pending_snapshot
->root
= root
;
203 list_add(&pending_snapshot
->list
,
204 &trans
->transaction
->pending_snapshots
);
205 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
206 err
= btrfs_commit_transaction(trans
, root
);
209 btrfs_btree_balance_dirty(root
, nr
);
213 int btrfs_defrag_file(struct file
*file
)
215 struct inode
*inode
= fdentry(file
)->d_inode
;
216 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
217 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
218 struct btrfs_ordered_extent
*ordered
;
220 unsigned long last_index
;
221 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
222 unsigned long total_read
= 0;
228 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
232 mutex_lock(&inode
->i_mutex
);
233 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
234 for (i
= 0; i
<= last_index
; i
++) {
235 if (total_read
% ra_pages
== 0) {
236 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
237 min(last_index
, i
+ ra_pages
- 1));
241 page
= grab_cache_page(inode
->i_mapping
, i
);
244 if (!PageUptodate(page
)) {
245 btrfs_readpage(NULL
, page
);
247 if (!PageUptodate(page
)) {
249 page_cache_release(page
);
254 wait_on_page_writeback(page
);
256 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
257 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
258 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
260 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
262 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
264 page_cache_release(page
);
265 btrfs_start_ordered_extent(inode
, ordered
, 1);
266 btrfs_put_ordered_extent(ordered
);
269 set_page_extent_mapped(page
);
272 * this makes sure page_mkwrite is called on the
273 * page if it is dirtied again later
275 clear_page_dirty_for_io(page
);
277 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
279 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
280 set_page_dirty(page
);
282 page_cache_release(page
);
283 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
287 mutex_unlock(&inode
->i_mutex
);
292 * Called inside transaction, so use GFP_NOFS
295 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
300 struct btrfs_ioctl_vol_args
*vol_args
;
301 struct btrfs_trans_handle
*trans
;
302 struct btrfs_device
*device
= NULL
;
309 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
314 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
319 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
320 namelen
= strlen(vol_args
->name
);
322 mutex_lock(&root
->fs_info
->volume_mutex
);
323 sizestr
= vol_args
->name
;
324 devstr
= strchr(sizestr
, ':');
327 sizestr
= devstr
+ 1;
329 devstr
= vol_args
->name
;
330 devid
= simple_strtoull(devstr
, &end
, 10);
331 printk(KERN_INFO
"resizing devid %llu\n", devid
);
333 device
= btrfs_find_device(root
, devid
, NULL
);
335 printk(KERN_INFO
"resizer unable to find device %llu\n", devid
);
339 if (!strcmp(sizestr
, "max"))
340 new_size
= device
->bdev
->bd_inode
->i_size
;
342 if (sizestr
[0] == '-') {
345 } else if (sizestr
[0] == '+') {
349 new_size
= btrfs_parse_size(sizestr
);
356 old_size
= device
->total_bytes
;
359 if (new_size
> old_size
) {
363 new_size
= old_size
- new_size
;
364 } else if (mod
> 0) {
365 new_size
= old_size
+ new_size
;
368 if (new_size
< 256 * 1024 * 1024) {
372 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
377 do_div(new_size
, root
->sectorsize
);
378 new_size
*= root
->sectorsize
;
380 printk(KERN_INFO
"new size for %s is %llu\n",
381 device
->name
, (unsigned long long)new_size
);
383 if (new_size
> old_size
) {
384 trans
= btrfs_start_transaction(root
, 1);
385 ret
= btrfs_grow_device(trans
, device
, new_size
);
386 btrfs_commit_transaction(trans
, root
);
388 ret
= btrfs_shrink_device(device
, new_size
);
392 mutex_unlock(&root
->fs_info
->volume_mutex
);
398 static noinline
int btrfs_ioctl_snap_create(struct btrfs_root
*root
,
401 struct btrfs_ioctl_vol_args
*vol_args
;
402 struct btrfs_dir_item
*di
;
403 struct btrfs_path
*path
;
408 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
413 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
418 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
419 namelen
= strlen(vol_args
->name
);
420 if (strchr(vol_args
->name
, '/')) {
425 path
= btrfs_alloc_path();
431 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
432 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
434 vol_args
->name
, namelen
, 0);
435 btrfs_free_path(path
);
437 if (di
&& !IS_ERR(di
)) {
447 mutex_lock(&root
->fs_info
->drop_mutex
);
448 if (root
== root
->fs_info
->tree_root
)
449 ret
= create_subvol(root
, vol_args
->name
, namelen
);
451 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
452 mutex_unlock(&root
->fs_info
->drop_mutex
);
458 static int btrfs_ioctl_defrag(struct file
*file
)
460 struct inode
*inode
= fdentry(file
)->d_inode
;
461 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
463 switch (inode
->i_mode
& S_IFMT
) {
465 btrfs_defrag_root(root
, 0);
466 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
469 btrfs_defrag_file(file
);
476 long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
478 struct btrfs_ioctl_vol_args
*vol_args
;
481 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
486 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
490 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
491 ret
= btrfs_init_new_device(root
, vol_args
->name
);
498 long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
500 struct btrfs_ioctl_vol_args
*vol_args
;
503 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
508 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
512 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
513 ret
= btrfs_rm_device(root
, vol_args
->name
);
520 long btrfs_ioctl_clone(struct file
*file
, unsigned long src_fd
)
522 struct inode
*inode
= fdentry(file
)->d_inode
;
523 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
524 struct file
*src_file
;
526 struct btrfs_trans_handle
*trans
;
527 struct btrfs_path
*path
;
528 struct extent_buffer
*leaf
;
530 struct btrfs_key key
;
535 src_file
= fget(src_fd
);
538 src
= src_file
->f_dentry
->d_inode
;
541 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
545 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
549 buf
= vmalloc(btrfs_level_size(root
, 0));
553 path
= btrfs_alloc_path();
561 mutex_lock(&inode
->i_mutex
);
562 mutex_lock(&src
->i_mutex
);
564 mutex_lock(&src
->i_mutex
);
565 mutex_lock(&inode
->i_mutex
);
572 /* do any pending delalloc/csum calc on src, one way or
573 another, and lock file content */
575 struct btrfs_ordered_extent
*ordered
;
576 lock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
577 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
578 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
580 unlock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
582 btrfs_put_ordered_extent(ordered
);
583 btrfs_wait_ordered_range(src
, 0, (u64
)-1);
586 trans
= btrfs_start_transaction(root
, 1);
589 key
.objectid
= src
->i_ino
;
590 key
.type
= BTRFS_EXTENT_DATA_KEY
;
595 * note the key will change type as we walk through the
598 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
602 nritems
= btrfs_header_nritems(path
->nodes
[0]);
603 if (path
->slots
[0] >= nritems
) {
604 ret
= btrfs_next_leaf(root
, path
);
609 nritems
= btrfs_header_nritems(path
->nodes
[0]);
611 leaf
= path
->nodes
[0];
612 slot
= path
->slots
[0];
614 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
615 if (btrfs_key_type(&key
) > BTRFS_CSUM_ITEM_KEY
||
616 key
.objectid
!= src
->i_ino
)
619 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
||
620 btrfs_key_type(&key
) == BTRFS_CSUM_ITEM_KEY
) {
622 struct btrfs_key new_key
;
624 size
= btrfs_item_size_nr(leaf
, slot
);
625 read_extent_buffer(leaf
, buf
,
626 btrfs_item_ptr_offset(leaf
, slot
),
628 btrfs_release_path(root
, path
);
630 memcpy(&new_key
, &key
, sizeof(new_key
));
631 new_key
.objectid
= inode
->i_ino
;
632 ret
= btrfs_insert_empty_item(trans
, root
, path
,
637 leaf
= path
->nodes
[0];
638 slot
= path
->slots
[0];
639 write_extent_buffer(leaf
, buf
,
640 btrfs_item_ptr_offset(leaf
, slot
),
642 btrfs_mark_buffer_dirty(leaf
);
645 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
646 struct btrfs_file_extent_item
*extent
;
649 extent
= btrfs_item_ptr(leaf
, slot
,
650 struct btrfs_file_extent_item
);
651 found_type
= btrfs_file_extent_type(leaf
, extent
);
652 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
653 u64 ds
= btrfs_file_extent_disk_bytenr(leaf
,
655 u64 dl
= btrfs_file_extent_disk_num_bytes(leaf
,
657 /* ds == 0 means there's a hole */
659 ret
= btrfs_inc_extent_ref(trans
, root
,
661 root
->root_key
.objectid
,
663 inode
->i_ino
, key
.offset
);
668 btrfs_release_path(root
, path
);
673 btrfs_release_path(root
, path
);
675 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
676 inode
->i_blocks
= src
->i_blocks
;
677 btrfs_i_size_write(inode
, src
->i_size
);
678 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
679 ret
= btrfs_update_inode(trans
, root
, inode
);
681 btrfs_end_transaction(trans
, root
);
682 unlock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
684 vmtruncate(inode
, 0);
686 mutex_unlock(&src
->i_mutex
);
687 mutex_unlock(&inode
->i_mutex
);
689 btrfs_free_path(path
);
696 * there are many ways the trans_start and trans_end ioctls can lead
697 * to deadlocks. They should only be used by applications that
698 * basically own the machine, and have a very in depth understanding
699 * of all the possible deadlocks and enospc problems.
701 long btrfs_ioctl_trans_start(struct file
*file
)
703 struct inode
*inode
= fdentry(file
)->d_inode
;
704 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
705 struct btrfs_trans_handle
*trans
;
708 if (!capable(CAP_SYS_ADMIN
))
711 if (file
->private_data
) {
716 mutex_lock(&root
->fs_info
->trans_mutex
);
717 root
->fs_info
->open_ioctl_trans
++;
718 mutex_unlock(&root
->fs_info
->trans_mutex
);
720 trans
= btrfs_start_ioctl_transaction(root
, 0);
722 file
->private_data
= trans
;
725 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
731 * there are many ways the trans_start and trans_end ioctls can lead
732 * to deadlocks. They should only be used by applications that
733 * basically own the machine, and have a very in depth understanding
734 * of all the possible deadlocks and enospc problems.
736 long btrfs_ioctl_trans_end(struct file
*file
)
738 struct inode
*inode
= fdentry(file
)->d_inode
;
739 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
740 struct btrfs_trans_handle
*trans
;
743 trans
= file
->private_data
;
748 btrfs_end_transaction(trans
, root
);
749 file
->private_data
= NULL
;
751 mutex_lock(&root
->fs_info
->trans_mutex
);
752 root
->fs_info
->open_ioctl_trans
--;
753 mutex_unlock(&root
->fs_info
->trans_mutex
);
759 long btrfs_ioctl(struct file
*file
, unsigned int
760 cmd
, unsigned long arg
)
762 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
765 case BTRFS_IOC_SNAP_CREATE
:
766 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
767 case BTRFS_IOC_DEFRAG
:
768 return btrfs_ioctl_defrag(file
);
769 case BTRFS_IOC_RESIZE
:
770 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
771 case BTRFS_IOC_ADD_DEV
:
772 return btrfs_ioctl_add_dev(root
, (void __user
*)arg
);
773 case BTRFS_IOC_RM_DEV
:
774 return btrfs_ioctl_rm_dev(root
, (void __user
*)arg
);
775 case BTRFS_IOC_BALANCE
:
776 return btrfs_balance(root
->fs_info
->dev_root
);
777 case BTRFS_IOC_CLONE
:
778 return btrfs_ioctl_clone(file
, arg
);
779 case BTRFS_IOC_TRANS_START
:
780 return btrfs_ioctl_trans_start(file
);
781 case BTRFS_IOC_TRANS_END
:
782 return btrfs_ioctl_trans_end(file
);
784 btrfs_start_delalloc_inodes(root
);
785 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);