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_level(&root_item, 0);
112 btrfs_set_root_refs(&root_item, 1);
113 btrfs_set_root_used(&root_item, 0);
115 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
116 root_item.drop_level = 0;
118 btrfs_tree_unlock(leaf);
119 free_extent_buffer(leaf);
122 btrfs_set_root_dirid(&root_item, new_dirid);
124 key.objectid = objectid;
126 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
127 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
133 * insert the directory item
135 key.offset = (u64)-1;
136 dir = root->fs_info->sb->s_root->d_inode;
137 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
138 name, namelen, dir->i_ino, &key,
143 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
144 name, namelen, objectid,
145 root->fs_info->sb->s_root->d_inode->i_ino, 0);
149 ret = btrfs_commit_transaction(trans, root);
153 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
156 trans = btrfs_start_transaction(new_root, 1);
159 ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
160 BTRFS_I(dir)->block_group);
165 nr = trans->blocks_used;
166 err = btrfs_commit_transaction(trans, new_root);
170 btrfs_btree_balance_dirty(root, nr);
174 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
176 struct btrfs_pending_snapshot *pending_snapshot;
177 struct btrfs_trans_handle *trans;
180 unsigned long nr = 0;
185 ret = btrfs_check_free_space(root, 1, 0);
189 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
190 if (!pending_snapshot) {
194 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
195 if (!pending_snapshot->name) {
197 kfree(pending_snapshot);
200 memcpy(pending_snapshot->name, name, namelen);
201 pending_snapshot->name[namelen] = '\0';
202 trans = btrfs_start_transaction(root, 1);
204 pending_snapshot->root = root;
205 list_add(&pending_snapshot->list,
206 &trans->transaction->pending_snapshots);
207 ret = btrfs_update_inode(trans, root, root->inode);
208 err = btrfs_commit_transaction(trans, root);
211 btrfs_btree_balance_dirty(root, nr);
215 /* copy of may_create in fs/namei.c() */
216 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
222 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
226 * Create a new subvolume below @parent. This is largely modeled after
227 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
228 * inside this filesystem so it's quite a bit simpler.
230 static noinline int btrfs_mksubvol(struct path *parent, char *name,
231 int mode, int namelen)
233 struct dentry *dentry;
236 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
238 dentry = lookup_one_len(name, parent->dentry, namelen);
239 error = PTR_ERR(dentry);
247 if (!IS_POSIXACL(parent->dentry->d_inode))
248 mode &= ~current->fs->umask;
249 error = mnt_want_write(parent->mnt);
253 error = btrfs_may_create(parent->dentry->d_inode, dentry);
258 * Actually perform the low-level subvolume creation after all
261 * Eventually we want to pass in an inode under which we create this
262 * subvolume, but for now all are under the filesystem root.
264 * Also we should pass on the mode eventually to allow creating new
265 * subvolume with specific mode bits.
267 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, dentry,
272 fsnotify_mkdir(parent->dentry->d_inode, dentry);
274 mnt_drop_write(parent->mnt);
278 mutex_unlock(&parent->dentry->d_inode->i_mutex);
283 int btrfs_defrag_file(struct file *file)
285 struct inode *inode = fdentry(file)->d_inode;
286 struct btrfs_root *root = BTRFS_I(inode)->root;
287 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
288 struct btrfs_ordered_extent *ordered;
290 unsigned long last_index;
291 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
292 unsigned long total_read = 0;
298 ret = btrfs_check_free_space(root, inode->i_size, 0);
302 mutex_lock(&inode->i_mutex);
303 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
304 for (i = 0; i <= last_index; i++) {
305 if (total_read % ra_pages == 0) {
306 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
307 min(last_index, i + ra_pages - 1));
311 page = grab_cache_page(inode->i_mapping, i);
314 if (!PageUptodate(page)) {
315 btrfs_readpage(NULL, page);
317 if (!PageUptodate(page)) {
319 page_cache_release(page);
324 wait_on_page_writeback(page);
326 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
327 page_end = page_start + PAGE_CACHE_SIZE - 1;
328 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
330 ordered = btrfs_lookup_ordered_extent(inode, page_start);
332 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
334 page_cache_release(page);
335 btrfs_start_ordered_extent(inode, ordered, 1);
336 btrfs_put_ordered_extent(ordered);
339 set_page_extent_mapped(page);
342 * this makes sure page_mkwrite is called on the
343 * page if it is dirtied again later
345 clear_page_dirty_for_io(page);
347 btrfs_set_extent_delalloc(inode, page_start, page_end);
349 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
350 set_page_dirty(page);
352 page_cache_release(page);
353 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
357 mutex_unlock(&inode->i_mutex);
362 * Called inside transaction, so use GFP_NOFS
365 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
370 struct btrfs_ioctl_vol_args *vol_args;
371 struct btrfs_trans_handle *trans;
372 struct btrfs_device *device = NULL;
379 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
384 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
389 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
390 namelen = strlen(vol_args->name);
392 mutex_lock(&root->fs_info->volume_mutex);
393 sizestr = vol_args->name;
394 devstr = strchr(sizestr, ':');
397 sizestr = devstr + 1;
399 devstr = vol_args->name;
400 devid = simple_strtoull(devstr, &end, 10);
401 printk(KERN_INFO "resizing devid %llu\n", devid);
403 device = btrfs_find_device(root, devid, NULL);
405 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
409 if (!strcmp(sizestr, "max"))
410 new_size = device->bdev->bd_inode->i_size;
412 if (sizestr[0] == '-') {
415 } else if (sizestr[0] == '+') {
419 new_size = btrfs_parse_size(sizestr);
426 old_size = device->total_bytes;
429 if (new_size > old_size) {
433 new_size = old_size - new_size;
434 } else if (mod > 0) {
435 new_size = old_size + new_size;
438 if (new_size < 256 * 1024 * 1024) {
442 if (new_size > device->bdev->bd_inode->i_size) {
447 do_div(new_size, root->sectorsize);
448 new_size *= root->sectorsize;
450 printk(KERN_INFO "new size for %s is %llu\n",
451 device->name, (unsigned long long)new_size);
453 if (new_size > old_size) {
454 trans = btrfs_start_transaction(root, 1);
455 ret = btrfs_grow_device(trans, device, new_size);
456 btrfs_commit_transaction(trans, root);
458 ret = btrfs_shrink_device(device, new_size);
462 mutex_unlock(&root->fs_info->volume_mutex);
468 static noinline int btrfs_ioctl_snap_create(struct file *file,
471 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
472 struct btrfs_ioctl_vol_args *vol_args;
473 struct btrfs_dir_item *di;
474 struct btrfs_path *path;
479 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
484 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
489 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
490 namelen = strlen(vol_args->name);
491 if (strchr(vol_args->name, '/')) {
496 path = btrfs_alloc_path();
502 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
503 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
505 vol_args->name, namelen, 0);
506 btrfs_free_path(path);
508 if (di && !IS_ERR(di)) {
518 if (root == root->fs_info->tree_root) {
519 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
520 file->f_path.dentry->d_inode->i_mode,
523 ret = create_snapshot(root, vol_args->name, namelen);
531 static int btrfs_ioctl_defrag(struct file *file)
533 struct inode *inode = fdentry(file)->d_inode;
534 struct btrfs_root *root = BTRFS_I(inode)->root;
536 switch (inode->i_mode & S_IFMT) {
538 btrfs_defrag_root(root, 0);
539 btrfs_defrag_root(root->fs_info->extent_root, 0);
542 btrfs_defrag_file(file);
549 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
551 struct btrfs_ioctl_vol_args *vol_args;
554 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
559 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
563 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
564 ret = btrfs_init_new_device(root, vol_args->name);
571 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
573 struct btrfs_ioctl_vol_args *vol_args;
576 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
581 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
585 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
586 ret = btrfs_rm_device(root, vol_args->name);
593 long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
595 struct inode *inode = fdentry(file)->d_inode;
596 struct btrfs_root *root = BTRFS_I(inode)->root;
597 struct file *src_file;
599 struct btrfs_trans_handle *trans;
600 struct btrfs_path *path;
601 struct extent_buffer *leaf;
603 struct btrfs_key key;
608 src_file = fget(src_fd);
611 src = src_file->f_dentry->d_inode;
614 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
618 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
622 buf = vmalloc(btrfs_level_size(root, 0));
626 path = btrfs_alloc_path();
634 mutex_lock(&inode->i_mutex);
635 mutex_lock(&src->i_mutex);
637 mutex_lock(&src->i_mutex);
638 mutex_lock(&inode->i_mutex);
645 /* do any pending delalloc/csum calc on src, one way or
646 another, and lock file content */
648 struct btrfs_ordered_extent *ordered;
649 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
650 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
651 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
653 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
655 btrfs_put_ordered_extent(ordered);
656 btrfs_wait_ordered_range(src, 0, (u64)-1);
659 trans = btrfs_start_transaction(root, 1);
662 key.objectid = src->i_ino;
663 key.type = BTRFS_EXTENT_DATA_KEY;
668 * note the key will change type as we walk through the
671 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
675 nritems = btrfs_header_nritems(path->nodes[0]);
676 if (path->slots[0] >= nritems) {
677 ret = btrfs_next_leaf(root, path);
682 nritems = btrfs_header_nritems(path->nodes[0]);
684 leaf = path->nodes[0];
685 slot = path->slots[0];
687 btrfs_item_key_to_cpu(leaf, &key, slot);
688 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
689 key.objectid != src->i_ino)
692 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY ||
693 btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
695 struct btrfs_key new_key;
697 size = btrfs_item_size_nr(leaf, slot);
698 read_extent_buffer(leaf, buf,
699 btrfs_item_ptr_offset(leaf, slot),
701 btrfs_release_path(root, path);
703 memcpy(&new_key, &key, sizeof(new_key));
704 new_key.objectid = inode->i_ino;
705 ret = btrfs_insert_empty_item(trans, root, path,
710 leaf = path->nodes[0];
711 slot = path->slots[0];
712 write_extent_buffer(leaf, buf,
713 btrfs_item_ptr_offset(leaf, slot),
715 btrfs_mark_buffer_dirty(leaf);
718 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
719 struct btrfs_file_extent_item *extent;
722 extent = btrfs_item_ptr(leaf, slot,
723 struct btrfs_file_extent_item);
724 found_type = btrfs_file_extent_type(leaf, extent);
725 if (found_type == BTRFS_FILE_EXTENT_REG) {
726 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
728 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
730 /* ds == 0 means there's a hole */
732 ret = btrfs_inc_extent_ref(trans, root,
734 root->root_key.objectid,
741 btrfs_release_path(root, path);
746 btrfs_release_path(root, path);
748 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
749 inode_set_bytes(inode, inode_get_bytes(src));
750 btrfs_i_size_write(inode, src->i_size);
751 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
752 ret = btrfs_update_inode(trans, root, inode);
754 btrfs_end_transaction(trans, root);
755 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
757 vmtruncate(inode, 0);
759 mutex_unlock(&src->i_mutex);
760 mutex_unlock(&inode->i_mutex);
762 btrfs_free_path(path);
769 * there are many ways the trans_start and trans_end ioctls can lead
770 * to deadlocks. They should only be used by applications that
771 * basically own the machine, and have a very in depth understanding
772 * of all the possible deadlocks and enospc problems.
774 long btrfs_ioctl_trans_start(struct file *file)
776 struct inode *inode = fdentry(file)->d_inode;
777 struct btrfs_root *root = BTRFS_I(inode)->root;
778 struct btrfs_trans_handle *trans;
781 if (!capable(CAP_SYS_ADMIN))
784 if (file->private_data) {
789 mutex_lock(&root->fs_info->trans_mutex);
790 root->fs_info->open_ioctl_trans++;
791 mutex_unlock(&root->fs_info->trans_mutex);
793 trans = btrfs_start_ioctl_transaction(root, 0);
795 file->private_data = trans;
798 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
804 * there are many ways the trans_start and trans_end ioctls can lead
805 * to deadlocks. They should only be used by applications that
806 * basically own the machine, and have a very in depth understanding
807 * of all the possible deadlocks and enospc problems.
809 long btrfs_ioctl_trans_end(struct file *file)
811 struct inode *inode = fdentry(file)->d_inode;
812 struct btrfs_root *root = BTRFS_I(inode)->root;
813 struct btrfs_trans_handle *trans;
816 trans = file->private_data;
821 btrfs_end_transaction(trans, root);
822 file->private_data = NULL;
824 mutex_lock(&root->fs_info->trans_mutex);
825 root->fs_info->open_ioctl_trans--;
826 mutex_unlock(&root->fs_info->trans_mutex);
832 long btrfs_ioctl(struct file *file, unsigned int
833 cmd, unsigned long arg)
835 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
838 case BTRFS_IOC_SNAP_CREATE:
839 return btrfs_ioctl_snap_create(file, (void __user *)arg);
840 case BTRFS_IOC_DEFRAG:
841 return btrfs_ioctl_defrag(file);
842 case BTRFS_IOC_RESIZE:
843 return btrfs_ioctl_resize(root, (void __user *)arg);
844 case BTRFS_IOC_ADD_DEV:
845 return btrfs_ioctl_add_dev(root, (void __user *)arg);
846 case BTRFS_IOC_RM_DEV:
847 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
848 case BTRFS_IOC_BALANCE:
849 return btrfs_balance(root->fs_info->dev_root);
850 case BTRFS_IOC_CLONE:
851 return btrfs_ioctl_clone(file, arg);
852 case BTRFS_IOC_TRANS_START:
853 return btrfs_ioctl_trans_start(file);
854 case BTRFS_IOC_TRANS_END:
855 return btrfs_ioctl_trans_end(file);
857 btrfs_start_delalloc_inodes(root);
858 btrfs_sync_fs(file->f_dentry->d_sb, 1);