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/smp_lock.h>
31 #include <linux/backing-dev.h>
32 #include <linux/mount.h>
33 #include <linux/mpage.h>
34 #include <linux/namei.h>
35 #include <linux/swap.h>
36 #include <linux/writeback.h>
37 #include <linux/statfs.h>
38 #include <linux/compat.h>
39 #include <linux/bit_spinlock.h>
40 #include <linux/security.h>
41 #include <linux/version.h>
42 #include <linux/xattr.h>
43 #include <linux/vmalloc.h>
46 #include "transaction.h"
47 #include "btrfs_inode.h"
49 #include "print-tree.h"
55 static noinline int create_subvol(struct btrfs_root *root,
56 struct dentry *dentry,
57 char *name, int namelen)
59 struct btrfs_trans_handle *trans;
61 struct btrfs_root_item root_item;
62 struct btrfs_inode_item *inode_item;
63 struct extent_buffer *leaf;
64 struct btrfs_root *new_root = root;
69 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
72 ret = btrfs_check_free_space(root, 1, 0);
76 trans = btrfs_start_transaction(root, 1);
79 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
84 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
85 objectid, trans->transid, 0, 0, 0);
91 btrfs_set_header_nritems(leaf, 0);
92 btrfs_set_header_level(leaf, 0);
93 btrfs_set_header_bytenr(leaf, leaf->start);
94 btrfs_set_header_generation(leaf, trans->transid);
95 btrfs_set_header_owner(leaf, objectid);
97 write_extent_buffer(leaf, root->fs_info->fsid,
98 (unsigned long)btrfs_header_fsid(leaf),
100 btrfs_mark_buffer_dirty(leaf);
102 inode_item = &root_item.inode;
103 memset(inode_item, 0, sizeof(*inode_item));
104 inode_item->generation = cpu_to_le64(1);
105 inode_item->size = cpu_to_le64(3);
106 inode_item->nlink = cpu_to_le32(1);
107 inode_item->nbytes = cpu_to_le64(root->leafsize);
108 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
110 btrfs_set_root_bytenr(&root_item, leaf->start);
111 btrfs_set_root_generation(&root_item, trans->transid);
112 btrfs_set_root_level(&root_item, 0);
113 btrfs_set_root_refs(&root_item, 1);
114 btrfs_set_root_used(&root_item, 0);
116 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
117 root_item.drop_level = 0;
119 btrfs_tree_unlock(leaf);
120 free_extent_buffer(leaf);
123 btrfs_set_root_dirid(&root_item, new_dirid);
125 key.objectid = objectid;
127 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
128 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
134 * insert the directory item
136 key.offset = (u64)-1;
137 dir = root->fs_info->sb->s_root->d_inode;
138 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
139 name, namelen, dir->i_ino, &key,
144 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
145 name, namelen, objectid,
146 root->fs_info->sb->s_root->d_inode->i_ino, 0);
150 ret = btrfs_commit_transaction(trans, root);
154 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
157 trans = btrfs_start_transaction(new_root, 1);
160 ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
161 BTRFS_I(dir)->block_group);
166 nr = trans->blocks_used;
167 err = btrfs_commit_transaction(trans, new_root);
171 btrfs_btree_balance_dirty(root, nr);
175 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
177 struct btrfs_pending_snapshot *pending_snapshot;
178 struct btrfs_trans_handle *trans;
181 unsigned long nr = 0;
186 ret = btrfs_check_free_space(root, 1, 0);
190 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
191 if (!pending_snapshot) {
195 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
196 if (!pending_snapshot->name) {
198 kfree(pending_snapshot);
201 memcpy(pending_snapshot->name, name, namelen);
202 pending_snapshot->name[namelen] = '\0';
203 trans = btrfs_start_transaction(root, 1);
205 pending_snapshot->root = root;
206 list_add(&pending_snapshot->list,
207 &trans->transaction->pending_snapshots);
208 ret = btrfs_update_inode(trans, root, root->inode);
209 err = btrfs_commit_transaction(trans, root);
212 btrfs_btree_balance_dirty(root, nr);
216 /* copy of may_create in fs/namei.c() */
217 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
223 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
227 * Create a new subvolume below @parent. This is largely modeled after
228 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
229 * inside this filesystem so it's quite a bit simpler.
231 static noinline int btrfs_mksubvol(struct path *parent, char *name,
232 int mode, int namelen)
234 struct dentry *dentry;
237 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
239 dentry = lookup_one_len(name, parent->dentry, namelen);
240 error = PTR_ERR(dentry);
248 if (!IS_POSIXACL(parent->dentry->d_inode))
249 mode &= ~current->fs->umask;
250 error = mnt_want_write(parent->mnt);
254 error = btrfs_may_create(parent->dentry->d_inode, dentry);
259 * Actually perform the low-level subvolume creation after all
262 * Eventually we want to pass in an inode under which we create this
263 * subvolume, but for now all are under the filesystem root.
265 * Also we should pass on the mode eventually to allow creating new
266 * subvolume with specific mode bits.
268 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, dentry,
273 fsnotify_mkdir(parent->dentry->d_inode, dentry);
275 mnt_drop_write(parent->mnt);
279 mutex_unlock(&parent->dentry->d_inode->i_mutex);
284 int btrfs_defrag_file(struct file *file)
286 struct inode *inode = fdentry(file)->d_inode;
287 struct btrfs_root *root = BTRFS_I(inode)->root;
288 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
289 struct btrfs_ordered_extent *ordered;
291 unsigned long last_index;
292 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
293 unsigned long total_read = 0;
299 ret = btrfs_check_free_space(root, inode->i_size, 0);
303 mutex_lock(&inode->i_mutex);
304 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
305 for (i = 0; i <= last_index; i++) {
306 if (total_read % ra_pages == 0) {
307 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
308 min(last_index, i + ra_pages - 1));
312 page = grab_cache_page(inode->i_mapping, i);
315 if (!PageUptodate(page)) {
316 btrfs_readpage(NULL, page);
318 if (!PageUptodate(page)) {
320 page_cache_release(page);
325 wait_on_page_writeback(page);
327 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
328 page_end = page_start + PAGE_CACHE_SIZE - 1;
329 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
331 ordered = btrfs_lookup_ordered_extent(inode, page_start);
333 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
335 page_cache_release(page);
336 btrfs_start_ordered_extent(inode, ordered, 1);
337 btrfs_put_ordered_extent(ordered);
340 set_page_extent_mapped(page);
343 * this makes sure page_mkwrite is called on the
344 * page if it is dirtied again later
346 clear_page_dirty_for_io(page);
348 btrfs_set_extent_delalloc(inode, page_start, page_end);
350 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
351 set_page_dirty(page);
353 page_cache_release(page);
354 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
358 mutex_unlock(&inode->i_mutex);
363 * Called inside transaction, so use GFP_NOFS
366 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
371 struct btrfs_ioctl_vol_args *vol_args;
372 struct btrfs_trans_handle *trans;
373 struct btrfs_device *device = NULL;
380 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
385 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
390 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
391 namelen = strlen(vol_args->name);
393 mutex_lock(&root->fs_info->volume_mutex);
394 sizestr = vol_args->name;
395 devstr = strchr(sizestr, ':');
398 sizestr = devstr + 1;
400 devstr = vol_args->name;
401 devid = simple_strtoull(devstr, &end, 10);
402 printk(KERN_INFO "resizing devid %llu\n", devid);
404 device = btrfs_find_device(root, devid, NULL);
406 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
410 if (!strcmp(sizestr, "max"))
411 new_size = device->bdev->bd_inode->i_size;
413 if (sizestr[0] == '-') {
416 } else if (sizestr[0] == '+') {
420 new_size = btrfs_parse_size(sizestr);
427 old_size = device->total_bytes;
430 if (new_size > old_size) {
434 new_size = old_size - new_size;
435 } else if (mod > 0) {
436 new_size = old_size + new_size;
439 if (new_size < 256 * 1024 * 1024) {
443 if (new_size > device->bdev->bd_inode->i_size) {
448 do_div(new_size, root->sectorsize);
449 new_size *= root->sectorsize;
451 printk(KERN_INFO "new size for %s is %llu\n",
452 device->name, (unsigned long long)new_size);
454 if (new_size > old_size) {
455 trans = btrfs_start_transaction(root, 1);
456 ret = btrfs_grow_device(trans, device, new_size);
457 btrfs_commit_transaction(trans, root);
459 ret = btrfs_shrink_device(device, new_size);
463 mutex_unlock(&root->fs_info->volume_mutex);
469 static noinline int btrfs_ioctl_snap_create(struct file *file,
472 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
473 struct btrfs_ioctl_vol_args *vol_args;
474 struct btrfs_dir_item *di;
475 struct btrfs_path *path;
480 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
485 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
490 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
491 namelen = strlen(vol_args->name);
492 if (strchr(vol_args->name, '/')) {
497 path = btrfs_alloc_path();
503 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
504 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
506 vol_args->name, namelen, 0);
507 btrfs_free_path(path);
509 if (di && !IS_ERR(di)) {
519 if (root == root->fs_info->tree_root) {
520 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
521 file->f_path.dentry->d_inode->i_mode,
524 ret = create_snapshot(root, vol_args->name, namelen);
532 static int btrfs_ioctl_defrag(struct file *file)
534 struct inode *inode = fdentry(file)->d_inode;
535 struct btrfs_root *root = BTRFS_I(inode)->root;
537 switch (inode->i_mode & S_IFMT) {
539 btrfs_defrag_root(root, 0);
540 btrfs_defrag_root(root->fs_info->extent_root, 0);
543 btrfs_defrag_file(file);
550 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
552 struct btrfs_ioctl_vol_args *vol_args;
555 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
560 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
564 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
565 ret = btrfs_init_new_device(root, vol_args->name);
572 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
574 struct btrfs_ioctl_vol_args *vol_args;
577 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
582 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
586 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
587 ret = btrfs_rm_device(root, vol_args->name);
594 long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
596 struct inode *inode = fdentry(file)->d_inode;
597 struct btrfs_root *root = BTRFS_I(inode)->root;
598 struct file *src_file;
600 struct btrfs_trans_handle *trans;
601 struct btrfs_path *path;
602 struct extent_buffer *leaf;
604 struct btrfs_key key;
609 src_file = fget(src_fd);
612 src = src_file->f_dentry->d_inode;
615 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
619 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
623 buf = vmalloc(btrfs_level_size(root, 0));
627 path = btrfs_alloc_path();
635 mutex_lock(&inode->i_mutex);
636 mutex_lock(&src->i_mutex);
638 mutex_lock(&src->i_mutex);
639 mutex_lock(&inode->i_mutex);
646 /* do any pending delalloc/csum calc on src, one way or
647 another, and lock file content */
649 struct btrfs_ordered_extent *ordered;
650 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
651 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
652 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
654 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
656 btrfs_put_ordered_extent(ordered);
657 btrfs_wait_ordered_range(src, 0, (u64)-1);
660 trans = btrfs_start_transaction(root, 1);
663 key.objectid = src->i_ino;
664 key.type = BTRFS_EXTENT_DATA_KEY;
669 * note the key will change type as we walk through the
672 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
676 nritems = btrfs_header_nritems(path->nodes[0]);
677 if (path->slots[0] >= nritems) {
678 ret = btrfs_next_leaf(root, path);
683 nritems = btrfs_header_nritems(path->nodes[0]);
685 leaf = path->nodes[0];
686 slot = path->slots[0];
688 btrfs_item_key_to_cpu(leaf, &key, slot);
689 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
690 key.objectid != src->i_ino)
693 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY ||
694 btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
696 struct btrfs_key new_key;
698 size = btrfs_item_size_nr(leaf, slot);
699 read_extent_buffer(leaf, buf,
700 btrfs_item_ptr_offset(leaf, slot),
702 btrfs_release_path(root, path);
704 memcpy(&new_key, &key, sizeof(new_key));
705 new_key.objectid = inode->i_ino;
706 ret = btrfs_insert_empty_item(trans, root, path,
711 leaf = path->nodes[0];
712 slot = path->slots[0];
713 write_extent_buffer(leaf, buf,
714 btrfs_item_ptr_offset(leaf, slot),
716 btrfs_mark_buffer_dirty(leaf);
719 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
720 struct btrfs_file_extent_item *extent;
723 extent = btrfs_item_ptr(leaf, slot,
724 struct btrfs_file_extent_item);
725 found_type = btrfs_file_extent_type(leaf, extent);
726 if (found_type == BTRFS_FILE_EXTENT_REG) {
727 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
729 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
731 /* ds == 0 means there's a hole */
733 ret = btrfs_inc_extent_ref(trans, root,
735 root->root_key.objectid,
742 btrfs_release_path(root, path);
747 btrfs_release_path(root, path);
749 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
750 inode_set_bytes(inode, inode_get_bytes(src));
751 btrfs_i_size_write(inode, src->i_size);
752 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
753 ret = btrfs_update_inode(trans, root, inode);
755 btrfs_end_transaction(trans, root);
756 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
758 vmtruncate(inode, 0);
760 mutex_unlock(&src->i_mutex);
761 mutex_unlock(&inode->i_mutex);
763 btrfs_free_path(path);
770 * there are many ways the trans_start and trans_end ioctls can lead
771 * to deadlocks. They should only be used by applications that
772 * basically own the machine, and have a very in depth understanding
773 * of all the possible deadlocks and enospc problems.
775 long btrfs_ioctl_trans_start(struct file *file)
777 struct inode *inode = fdentry(file)->d_inode;
778 struct btrfs_root *root = BTRFS_I(inode)->root;
779 struct btrfs_trans_handle *trans;
782 if (!capable(CAP_SYS_ADMIN))
785 if (file->private_data) {
790 mutex_lock(&root->fs_info->trans_mutex);
791 root->fs_info->open_ioctl_trans++;
792 mutex_unlock(&root->fs_info->trans_mutex);
794 trans = btrfs_start_ioctl_transaction(root, 0);
796 file->private_data = trans;
799 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
805 * there are many ways the trans_start and trans_end ioctls can lead
806 * to deadlocks. They should only be used by applications that
807 * basically own the machine, and have a very in depth understanding
808 * of all the possible deadlocks and enospc problems.
810 long btrfs_ioctl_trans_end(struct file *file)
812 struct inode *inode = fdentry(file)->d_inode;
813 struct btrfs_root *root = BTRFS_I(inode)->root;
814 struct btrfs_trans_handle *trans;
817 trans = file->private_data;
822 btrfs_end_transaction(trans, root);
823 file->private_data = NULL;
825 mutex_lock(&root->fs_info->trans_mutex);
826 root->fs_info->open_ioctl_trans--;
827 mutex_unlock(&root->fs_info->trans_mutex);
833 long btrfs_ioctl(struct file *file, unsigned int
834 cmd, unsigned long arg)
836 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
839 case BTRFS_IOC_SNAP_CREATE:
840 return btrfs_ioctl_snap_create(file, (void __user *)arg);
841 case BTRFS_IOC_DEFRAG:
842 return btrfs_ioctl_defrag(file);
843 case BTRFS_IOC_RESIZE:
844 return btrfs_ioctl_resize(root, (void __user *)arg);
845 case BTRFS_IOC_ADD_DEV:
846 return btrfs_ioctl_add_dev(root, (void __user *)arg);
847 case BTRFS_IOC_RM_DEV:
848 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
849 case BTRFS_IOC_BALANCE:
850 return btrfs_balance(root->fs_info->dev_root);
851 case BTRFS_IOC_CLONE:
852 return btrfs_ioctl_clone(file, arg);
853 case BTRFS_IOC_TRANS_START:
854 return btrfs_ioctl_trans_start(file);
855 case BTRFS_IOC_TRANS_END:
856 return btrfs_ioctl_trans_end(file);
858 btrfs_start_delalloc_inodes(root);
859 btrfs_sync_fs(file->f_dentry->d_sb, 1);