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>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
50 #include "print-tree.h"
56 static noinline int create_subvol(struct btrfs_root *root,
57 struct dentry *dentry,
58 char *name, int namelen)
60 struct btrfs_trans_handle *trans;
62 struct btrfs_root_item root_item;
63 struct btrfs_inode_item *inode_item;
64 struct extent_buffer *leaf;
65 struct btrfs_root *new_root = root;
70 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
74 ret = btrfs_check_free_space(root, 1, 0);
78 trans = btrfs_start_transaction(root, 1);
81 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
86 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
87 objectid, trans->transid, 0, 0, 0);
93 btrfs_set_header_nritems(leaf, 0);
94 btrfs_set_header_level(leaf, 0);
95 btrfs_set_header_bytenr(leaf, leaf->start);
96 btrfs_set_header_generation(leaf, trans->transid);
97 btrfs_set_header_owner(leaf, objectid);
99 write_extent_buffer(leaf, root->fs_info->fsid,
100 (unsigned long)btrfs_header_fsid(leaf),
102 btrfs_mark_buffer_dirty(leaf);
104 inode_item = &root_item.inode;
105 memset(inode_item, 0, sizeof(*inode_item));
106 inode_item->generation = cpu_to_le64(1);
107 inode_item->size = cpu_to_le64(3);
108 inode_item->nlink = cpu_to_le32(1);
109 inode_item->nbytes = cpu_to_le64(root->leafsize);
110 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
112 btrfs_set_root_bytenr(&root_item, leaf->start);
113 btrfs_set_root_generation(&root_item, trans->transid);
114 btrfs_set_root_level(&root_item, 0);
115 btrfs_set_root_refs(&root_item, 1);
116 btrfs_set_root_used(&root_item, 0);
117 btrfs_set_root_last_snapshot(&root_item, 0);
119 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
120 root_item.drop_level = 0;
122 btrfs_tree_unlock(leaf);
123 free_extent_buffer(leaf);
126 btrfs_set_root_dirid(&root_item, new_dirid);
128 key.objectid = objectid;
130 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
131 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
137 * insert the directory item
139 key.offset = (u64)-1;
140 dir = dentry->d_parent->d_inode;
141 ret = btrfs_set_inode_index(dir, &index);
144 ret = btrfs_insert_dir_item(trans, root,
145 name, namelen, dir->i_ino, &key,
146 BTRFS_FT_DIR, index);
150 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
151 ret = btrfs_update_inode(trans, root, dir);
154 /* add the backref first */
155 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
156 objectid, BTRFS_ROOT_BACKREF_KEY,
157 root->root_key.objectid,
158 dir->i_ino, index, name, namelen);
162 /* now add the forward ref */
163 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
164 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
166 dir->i_ino, index, name, namelen);
170 ret = btrfs_commit_transaction(trans, root);
174 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
177 trans = btrfs_start_transaction(new_root, 1);
180 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
181 BTRFS_I(dir)->block_group);
186 nr = trans->blocks_used;
187 err = btrfs_commit_transaction(trans, new_root);
191 btrfs_btree_balance_dirty(root, nr);
195 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
196 char *name, int namelen)
198 struct btrfs_pending_snapshot *pending_snapshot;
199 struct btrfs_trans_handle *trans;
202 unsigned long nr = 0;
207 ret = btrfs_check_free_space(root, 1, 0);
211 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
212 if (!pending_snapshot) {
216 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
217 if (!pending_snapshot->name) {
219 kfree(pending_snapshot);
222 memcpy(pending_snapshot->name, name, namelen);
223 pending_snapshot->name[namelen] = '\0';
224 pending_snapshot->dentry = dentry;
225 trans = btrfs_start_transaction(root, 1);
227 pending_snapshot->root = root;
228 list_add(&pending_snapshot->list,
229 &trans->transaction->pending_snapshots);
230 err = btrfs_commit_transaction(trans, root);
233 btrfs_btree_balance_dirty(root, nr);
237 /* copy of may_create in fs/namei.c() */
238 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
244 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
248 * Create a new subvolume below @parent. This is largely modeled after
249 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
250 * inside this filesystem so it's quite a bit simpler.
252 static noinline int btrfs_mksubvol(struct path *parent, char *name,
253 int mode, int namelen,
254 struct btrfs_root *snap_src)
256 struct dentry *dentry;
259 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
261 dentry = lookup_one_len(name, parent->dentry, namelen);
262 error = PTR_ERR(dentry);
270 if (!IS_POSIXACL(parent->dentry->d_inode))
271 mode &= ~current->fs->umask;
273 error = mnt_want_write(parent->mnt);
277 error = btrfs_may_create(parent->dentry->d_inode, dentry);
282 * Actually perform the low-level subvolume creation after all
285 * Eventually we want to pass in an inode under which we create this
286 * subvolume, but for now all are under the filesystem root.
288 * Also we should pass on the mode eventually to allow creating new
289 * subvolume with specific mode bits.
292 struct dentry *dir = dentry->d_parent;
293 struct dentry *test = dir->d_parent;
294 struct btrfs_path *path = btrfs_alloc_path();
297 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
299 test_oid = snap_src->root_key.objectid;
301 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
302 path, parent_oid, test_oid);
305 btrfs_release_path(snap_src->fs_info->tree_root, path);
307 /* we need to make sure we aren't creating a directory loop
308 * by taking a snapshot of something that has our current
309 * subvol in its directory tree. So, this loops through
310 * the dentries and checks the forward refs for each subvolume
311 * to see if is references the subvolume where we are
312 * placing this new snapshot.
316 dir == snap_src->fs_info->sb->s_root ||
317 test == snap_src->fs_info->sb->s_root ||
318 test->d_inode->i_sb != snap_src->fs_info->sb) {
321 if (S_ISLNK(test->d_inode->i_mode)) {
322 printk(KERN_INFO "Btrfs symlink in snapshot "
325 btrfs_free_path(path);
329 BTRFS_I(test->d_inode)->root->root_key.objectid;
330 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
331 path, test_oid, parent_oid);
333 printk(KERN_INFO "Btrfs snapshot creation "
334 "failed, looping\n");
336 btrfs_free_path(path);
339 btrfs_release_path(snap_src->fs_info->tree_root, path);
340 test = test->d_parent;
343 btrfs_free_path(path);
344 error = create_snapshot(snap_src, dentry, name, namelen);
346 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
347 dentry, name, namelen);
352 fsnotify_mkdir(parent->dentry->d_inode, dentry);
354 mnt_drop_write(parent->mnt);
358 mutex_unlock(&parent->dentry->d_inode->i_mutex);
363 static int btrfs_defrag_file(struct file *file)
365 struct inode *inode = fdentry(file)->d_inode;
366 struct btrfs_root *root = BTRFS_I(inode)->root;
367 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
368 struct btrfs_ordered_extent *ordered;
370 unsigned long last_index;
371 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
372 unsigned long total_read = 0;
378 ret = btrfs_check_free_space(root, inode->i_size, 0);
382 mutex_lock(&inode->i_mutex);
383 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
384 for (i = 0; i <= last_index; i++) {
385 if (total_read % ra_pages == 0) {
386 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
387 min(last_index, i + ra_pages - 1));
391 page = grab_cache_page(inode->i_mapping, i);
394 if (!PageUptodate(page)) {
395 btrfs_readpage(NULL, page);
397 if (!PageUptodate(page)) {
399 page_cache_release(page);
404 wait_on_page_writeback(page);
406 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
407 page_end = page_start + PAGE_CACHE_SIZE - 1;
408 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
410 ordered = btrfs_lookup_ordered_extent(inode, page_start);
412 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
414 page_cache_release(page);
415 btrfs_start_ordered_extent(inode, ordered, 1);
416 btrfs_put_ordered_extent(ordered);
419 set_page_extent_mapped(page);
422 * this makes sure page_mkwrite is called on the
423 * page if it is dirtied again later
425 clear_page_dirty_for_io(page);
427 btrfs_set_extent_delalloc(inode, page_start, page_end);
429 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
430 set_page_dirty(page);
432 page_cache_release(page);
433 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
437 mutex_unlock(&inode->i_mutex);
442 * Called inside transaction, so use GFP_NOFS
445 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
450 struct btrfs_ioctl_vol_args *vol_args;
451 struct btrfs_trans_handle *trans;
452 struct btrfs_device *device = NULL;
459 if (root->fs_info->sb->s_flags & MS_RDONLY)
462 if (!capable(CAP_SYS_ADMIN))
465 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
470 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
475 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
476 namelen = strlen(vol_args->name);
478 mutex_lock(&root->fs_info->volume_mutex);
479 sizestr = vol_args->name;
480 devstr = strchr(sizestr, ':');
483 sizestr = devstr + 1;
485 devstr = vol_args->name;
486 devid = simple_strtoull(devstr, &end, 10);
487 printk(KERN_INFO "resizing devid %llu\n", devid);
489 device = btrfs_find_device(root, devid, NULL, NULL);
491 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
495 if (!strcmp(sizestr, "max"))
496 new_size = device->bdev->bd_inode->i_size;
498 if (sizestr[0] == '-') {
501 } else if (sizestr[0] == '+') {
505 new_size = btrfs_parse_size(sizestr);
512 old_size = device->total_bytes;
515 if (new_size > old_size) {
519 new_size = old_size - new_size;
520 } else if (mod > 0) {
521 new_size = old_size + new_size;
524 if (new_size < 256 * 1024 * 1024) {
528 if (new_size > device->bdev->bd_inode->i_size) {
533 do_div(new_size, root->sectorsize);
534 new_size *= root->sectorsize;
536 printk(KERN_INFO "new size for %s is %llu\n",
537 device->name, (unsigned long long)new_size);
539 if (new_size > old_size) {
540 trans = btrfs_start_transaction(root, 1);
541 ret = btrfs_grow_device(trans, device, new_size);
542 btrfs_commit_transaction(trans, root);
544 ret = btrfs_shrink_device(device, new_size);
548 mutex_unlock(&root->fs_info->volume_mutex);
554 static noinline int btrfs_ioctl_snap_create(struct file *file,
555 void __user *arg, int subvol)
557 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
558 struct btrfs_ioctl_vol_args *vol_args;
559 struct btrfs_dir_item *di;
560 struct btrfs_path *path;
561 struct file *src_file;
566 if (root->fs_info->sb->s_flags & MS_RDONLY)
569 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
574 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
579 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
580 namelen = strlen(vol_args->name);
581 if (strchr(vol_args->name, '/')) {
586 path = btrfs_alloc_path();
592 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
593 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
595 vol_args->name, namelen, 0);
596 btrfs_free_path(path);
598 if (di && !IS_ERR(di)) {
609 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
610 file->f_path.dentry->d_inode->i_mode,
613 struct inode *src_inode;
614 src_file = fget(vol_args->fd);
620 src_inode = src_file->f_path.dentry->d_inode;
621 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
622 printk(KERN_INFO "btrfs: Snapshot src from "
628 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
629 file->f_path.dentry->d_inode->i_mode,
630 namelen, BTRFS_I(src_inode)->root);
639 static int btrfs_ioctl_defrag(struct file *file)
641 struct inode *inode = fdentry(file)->d_inode;
642 struct btrfs_root *root = BTRFS_I(inode)->root;
645 ret = mnt_want_write(file->f_path.mnt);
649 switch (inode->i_mode & S_IFMT) {
651 if (!capable(CAP_SYS_ADMIN)) {
655 btrfs_defrag_root(root, 0);
656 btrfs_defrag_root(root->fs_info->extent_root, 0);
659 if (!(file->f_mode & FMODE_WRITE)) {
663 btrfs_defrag_file(file);
667 mnt_drop_write(file->f_path.mnt);
671 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
673 struct btrfs_ioctl_vol_args *vol_args;
676 if (!capable(CAP_SYS_ADMIN))
679 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
684 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
688 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
689 ret = btrfs_init_new_device(root, vol_args->name);
696 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
698 struct btrfs_ioctl_vol_args *vol_args;
701 if (!capable(CAP_SYS_ADMIN))
704 if (root->fs_info->sb->s_flags & MS_RDONLY)
707 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
712 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
716 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
717 ret = btrfs_rm_device(root, vol_args->name);
724 static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
725 u64 off, u64 olen, u64 destoff)
727 struct inode *inode = fdentry(file)->d_inode;
728 struct btrfs_root *root = BTRFS_I(inode)->root;
729 struct file *src_file;
731 struct btrfs_trans_handle *trans;
732 struct btrfs_path *path;
733 struct extent_buffer *leaf;
735 struct btrfs_key key;
740 u64 bs = root->fs_info->sb->s_blocksize;
745 * - split compressed inline extents. annoying: we need to
746 * decompress into destination's address_space (the file offset
747 * may change, so source mapping won't do), then recompress (or
748 * otherwise reinsert) a subrange.
749 * - allow ranges within the same file to be cloned (provided
750 * they don't overlap)?
753 /* the destination must be opened for writing */
754 if (!(file->f_mode & FMODE_WRITE))
757 ret = mnt_want_write(file->f_path.mnt);
761 src_file = fget(srcfd);
766 src = src_file->f_dentry->d_inode;
773 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
777 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
781 buf = vmalloc(btrfs_level_size(root, 0));
785 path = btrfs_alloc_path();
793 mutex_lock(&inode->i_mutex);
794 mutex_lock(&src->i_mutex);
796 mutex_lock(&src->i_mutex);
797 mutex_lock(&inode->i_mutex);
800 /* determine range to clone */
802 if (off >= src->i_size || off + len > src->i_size)
805 olen = len = src->i_size - off;
806 /* if we extend to eof, continue to block boundary */
807 if (off + len == src->i_size)
808 len = ((src->i_size + bs-1) & ~(bs-1))
811 /* verify the end result is block aligned */
812 if ((off & (bs-1)) ||
813 ((off + len) & (bs-1)))
816 /* do any pending delalloc/csum calc on src, one way or
817 another, and lock file content */
819 struct btrfs_ordered_extent *ordered;
820 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
821 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
822 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
824 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
826 btrfs_put_ordered_extent(ordered);
827 btrfs_wait_ordered_range(src, off, off+len);
830 trans = btrfs_start_transaction(root, 1);
833 /* punch hole in destination first */
834 btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
837 key.objectid = src->i_ino;
838 key.type = BTRFS_EXTENT_DATA_KEY;
843 * note the key will change type as we walk through the
846 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
850 nritems = btrfs_header_nritems(path->nodes[0]);
851 if (path->slots[0] >= nritems) {
852 ret = btrfs_next_leaf(root, path);
857 nritems = btrfs_header_nritems(path->nodes[0]);
859 leaf = path->nodes[0];
860 slot = path->slots[0];
862 btrfs_item_key_to_cpu(leaf, &key, slot);
863 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
864 key.objectid != src->i_ino)
867 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
868 struct btrfs_file_extent_item *extent;
871 struct btrfs_key new_key;
872 u64 disko = 0, diskl = 0;
873 u64 datao = 0, datal = 0;
876 size = btrfs_item_size_nr(leaf, slot);
877 read_extent_buffer(leaf, buf,
878 btrfs_item_ptr_offset(leaf, slot),
881 extent = btrfs_item_ptr(leaf, slot,
882 struct btrfs_file_extent_item);
883 comp = btrfs_file_extent_compression(leaf, extent);
884 type = btrfs_file_extent_type(leaf, extent);
885 if (type == BTRFS_FILE_EXTENT_REG) {
886 disko = btrfs_file_extent_disk_bytenr(leaf,
888 diskl = btrfs_file_extent_disk_num_bytes(leaf,
890 datao = btrfs_file_extent_offset(leaf, extent);
891 datal = btrfs_file_extent_num_bytes(leaf,
893 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
894 /* take upper bound, may be compressed */
895 datal = btrfs_file_extent_ram_bytes(leaf,
898 btrfs_release_path(root, path);
900 if (key.offset + datal < off ||
901 key.offset >= off+len)
904 memcpy(&new_key, &key, sizeof(new_key));
905 new_key.objectid = inode->i_ino;
906 new_key.offset = key.offset + destoff - off;
908 if (type == BTRFS_FILE_EXTENT_REG) {
909 ret = btrfs_insert_empty_item(trans, root, path,
914 leaf = path->nodes[0];
915 slot = path->slots[0];
916 write_extent_buffer(leaf, buf,
917 btrfs_item_ptr_offset(leaf, slot),
920 extent = btrfs_item_ptr(leaf, slot,
921 struct btrfs_file_extent_item);
923 if (off > key.offset) {
924 datao += off - key.offset;
925 datal -= off - key.offset;
927 if (key.offset + datao + datal + key.offset >
929 datal = off + len - key.offset - datao;
930 /* disko == 0 means it's a hole */
934 btrfs_set_file_extent_offset(leaf, extent,
936 btrfs_set_file_extent_num_bytes(leaf, extent,
939 inode_add_bytes(inode, datal);
940 ret = btrfs_inc_extent_ref(trans, root,
941 disko, diskl, leaf->start,
942 root->root_key.objectid,
947 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
950 if (off > key.offset) {
951 skip = off - key.offset;
952 new_key.offset += skip;
955 if (key.offset + datal > off+len)
956 trim = key.offset + datal - (off+len);
958 if (comp && (skip || trim)) {
963 datal -= skip + trim;
964 ret = btrfs_insert_empty_item(trans, root, path,
971 btrfs_file_extent_calc_inline_size(0);
972 memmove(buf+start, buf+start+skip,
976 leaf = path->nodes[0];
977 slot = path->slots[0];
978 write_extent_buffer(leaf, buf,
979 btrfs_item_ptr_offset(leaf, slot),
981 inode_add_bytes(inode, datal);
984 btrfs_mark_buffer_dirty(leaf);
988 btrfs_release_path(root, path);
993 btrfs_release_path(root, path);
995 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
996 if (destoff + olen > inode->i_size)
997 btrfs_i_size_write(inode, destoff + olen);
998 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
999 ret = btrfs_update_inode(trans, root, inode);
1001 btrfs_end_transaction(trans, root);
1002 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1004 vmtruncate(inode, 0);
1006 mutex_unlock(&src->i_mutex);
1007 mutex_unlock(&inode->i_mutex);
1009 btrfs_free_path(path);
1013 mnt_drop_write(file->f_path.mnt);
1017 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1019 struct btrfs_ioctl_clone_range_args args;
1021 if (copy_from_user(&args, argp, sizeof(args)))
1023 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1024 args.src_length, args.dest_offset);
1028 * there are many ways the trans_start and trans_end ioctls can lead
1029 * to deadlocks. They should only be used by applications that
1030 * basically own the machine, and have a very in depth understanding
1031 * of all the possible deadlocks and enospc problems.
1033 static long btrfs_ioctl_trans_start(struct file *file)
1035 struct inode *inode = fdentry(file)->d_inode;
1036 struct btrfs_root *root = BTRFS_I(inode)->root;
1037 struct btrfs_trans_handle *trans;
1040 if (!capable(CAP_SYS_ADMIN))
1043 if (file->private_data) {
1048 ret = mnt_want_write(file->f_path.mnt);
1052 mutex_lock(&root->fs_info->trans_mutex);
1053 root->fs_info->open_ioctl_trans++;
1054 mutex_unlock(&root->fs_info->trans_mutex);
1056 trans = btrfs_start_ioctl_transaction(root, 0);
1058 file->private_data = trans;
1061 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1067 * there are many ways the trans_start and trans_end ioctls can lead
1068 * to deadlocks. They should only be used by applications that
1069 * basically own the machine, and have a very in depth understanding
1070 * of all the possible deadlocks and enospc problems.
1072 long btrfs_ioctl_trans_end(struct file *file)
1074 struct inode *inode = fdentry(file)->d_inode;
1075 struct btrfs_root *root = BTRFS_I(inode)->root;
1076 struct btrfs_trans_handle *trans;
1079 trans = file->private_data;
1084 btrfs_end_transaction(trans, root);
1085 file->private_data = NULL;
1087 mutex_lock(&root->fs_info->trans_mutex);
1088 root->fs_info->open_ioctl_trans--;
1089 mutex_unlock(&root->fs_info->trans_mutex);
1091 mnt_drop_write(file->f_path.mnt);
1097 long btrfs_ioctl(struct file *file, unsigned int
1098 cmd, unsigned long arg)
1100 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1101 void __user *argp = (void __user *)arg;
1104 case BTRFS_IOC_SNAP_CREATE:
1105 return btrfs_ioctl_snap_create(file, argp, 0);
1106 case BTRFS_IOC_SUBVOL_CREATE:
1107 return btrfs_ioctl_snap_create(file, argp, 1);
1108 case BTRFS_IOC_DEFRAG:
1109 return btrfs_ioctl_defrag(file);
1110 case BTRFS_IOC_RESIZE:
1111 return btrfs_ioctl_resize(root, argp);
1112 case BTRFS_IOC_ADD_DEV:
1113 return btrfs_ioctl_add_dev(root, argp);
1114 case BTRFS_IOC_RM_DEV:
1115 return btrfs_ioctl_rm_dev(root, argp);
1116 case BTRFS_IOC_BALANCE:
1117 return btrfs_balance(root->fs_info->dev_root);
1118 case BTRFS_IOC_CLONE:
1119 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1120 case BTRFS_IOC_CLONE_RANGE:
1121 return btrfs_ioctl_clone_range(file, argp);
1122 case BTRFS_IOC_TRANS_START:
1123 return btrfs_ioctl_trans_start(file);
1124 case BTRFS_IOC_TRANS_END:
1125 return btrfs_ioctl_trans_end(file);
1126 case BTRFS_IOC_SYNC:
1127 btrfs_sync_fs(file->f_dentry->d_sb, 1);