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;
73 ret = btrfs_check_free_space(root, 1, 0);
77 trans = btrfs_start_transaction(root, 1);
80 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
85 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
86 objectid, trans->transid, 0, 0, 0);
92 btrfs_set_header_nritems(leaf, 0);
93 btrfs_set_header_level(leaf, 0);
94 btrfs_set_header_bytenr(leaf, leaf->start);
95 btrfs_set_header_generation(leaf, trans->transid);
96 btrfs_set_header_owner(leaf, objectid);
98 write_extent_buffer(leaf, root->fs_info->fsid,
99 (unsigned long)btrfs_header_fsid(leaf),
101 btrfs_mark_buffer_dirty(leaf);
103 inode_item = &root_item.inode;
104 memset(inode_item, 0, sizeof(*inode_item));
105 inode_item->generation = cpu_to_le64(1);
106 inode_item->size = cpu_to_le64(3);
107 inode_item->nlink = cpu_to_le32(1);
108 inode_item->nbytes = cpu_to_le64(root->leafsize);
109 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
111 btrfs_set_root_bytenr(&root_item, leaf->start);
112 btrfs_set_root_generation(&root_item, trans->transid);
113 btrfs_set_root_level(&root_item, 0);
114 btrfs_set_root_refs(&root_item, 1);
115 btrfs_set_root_used(&root_item, 0);
116 btrfs_set_root_last_snapshot(&root_item, 0);
118 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
119 root_item.drop_level = 0;
121 btrfs_tree_unlock(leaf);
122 free_extent_buffer(leaf);
125 btrfs_set_root_dirid(&root_item, new_dirid);
127 key.objectid = objectid;
129 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
130 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
136 * insert the directory item
138 key.offset = (u64)-1;
139 dir = dentry->d_parent->d_inode;
140 ret = btrfs_set_inode_index(dir, &index);
143 ret = btrfs_insert_dir_item(trans, root,
144 name, namelen, dir->i_ino, &key,
145 BTRFS_FT_DIR, index);
149 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
150 name, namelen, objectid,
151 root->fs_info->sb->s_root->d_inode->i_ino, 0);
155 ret = btrfs_commit_transaction(trans, root);
159 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
162 trans = btrfs_start_transaction(new_root, 1);
165 ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
166 BTRFS_I(dir)->block_group);
171 nr = trans->blocks_used;
172 err = btrfs_commit_transaction(trans, new_root);
176 btrfs_btree_balance_dirty(root, nr);
180 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
181 char *name, int namelen)
183 struct btrfs_pending_snapshot *pending_snapshot;
184 struct btrfs_trans_handle *trans;
187 unsigned long nr = 0;
192 ret = btrfs_check_free_space(root, 1, 0);
196 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
197 if (!pending_snapshot) {
201 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
202 if (!pending_snapshot->name) {
204 kfree(pending_snapshot);
207 memcpy(pending_snapshot->name, name, namelen);
208 pending_snapshot->name[namelen] = '\0';
209 pending_snapshot->dentry = dentry;
210 trans = btrfs_start_transaction(root, 1);
212 pending_snapshot->root = root;
213 list_add(&pending_snapshot->list,
214 &trans->transaction->pending_snapshots);
215 err = btrfs_commit_transaction(trans, root);
218 btrfs_btree_balance_dirty(root, nr);
222 /* copy of may_create in fs/namei.c() */
223 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
229 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
233 * Create a new subvolume below @parent. This is largely modeled after
234 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
235 * inside this filesystem so it's quite a bit simpler.
237 static noinline int btrfs_mksubvol(struct path *parent, char *name,
238 int mode, int namelen,
239 struct btrfs_root *snap_src)
241 struct dentry *dentry;
244 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
246 dentry = lookup_one_len(name, parent->dentry, namelen);
247 error = PTR_ERR(dentry);
255 if (!IS_POSIXACL(parent->dentry->d_inode))
256 mode &= ~current->fs->umask;
258 error = mnt_want_write(parent->mnt);
262 error = btrfs_may_create(parent->dentry->d_inode, dentry);
267 * Actually perform the low-level subvolume creation after all
270 * Eventually we want to pass in an inode under which we create this
271 * subvolume, but for now all are under the filesystem root.
273 * Also we should pass on the mode eventually to allow creating new
274 * subvolume with specific mode bits.
277 error = create_snapshot(snap_src, dentry, name, namelen);
279 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
280 dentry, name, namelen);
285 fsnotify_mkdir(parent->dentry->d_inode, dentry);
287 mnt_drop_write(parent->mnt);
291 mutex_unlock(&parent->dentry->d_inode->i_mutex);
296 int btrfs_defrag_file(struct file *file)
298 struct inode *inode = fdentry(file)->d_inode;
299 struct btrfs_root *root = BTRFS_I(inode)->root;
300 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
301 struct btrfs_ordered_extent *ordered;
303 unsigned long last_index;
304 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
305 unsigned long total_read = 0;
311 ret = btrfs_check_free_space(root, inode->i_size, 0);
315 mutex_lock(&inode->i_mutex);
316 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
317 for (i = 0; i <= last_index; i++) {
318 if (total_read % ra_pages == 0) {
319 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
320 min(last_index, i + ra_pages - 1));
324 page = grab_cache_page(inode->i_mapping, i);
327 if (!PageUptodate(page)) {
328 btrfs_readpage(NULL, page);
330 if (!PageUptodate(page)) {
332 page_cache_release(page);
337 wait_on_page_writeback(page);
339 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
340 page_end = page_start + PAGE_CACHE_SIZE - 1;
341 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
343 ordered = btrfs_lookup_ordered_extent(inode, page_start);
345 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
347 page_cache_release(page);
348 btrfs_start_ordered_extent(inode, ordered, 1);
349 btrfs_put_ordered_extent(ordered);
352 set_page_extent_mapped(page);
355 * this makes sure page_mkwrite is called on the
356 * page if it is dirtied again later
358 clear_page_dirty_for_io(page);
360 btrfs_set_extent_delalloc(inode, page_start, page_end);
362 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
363 set_page_dirty(page);
365 page_cache_release(page);
366 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
370 mutex_unlock(&inode->i_mutex);
375 * Called inside transaction, so use GFP_NOFS
378 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
383 struct btrfs_ioctl_vol_args *vol_args;
384 struct btrfs_trans_handle *trans;
385 struct btrfs_device *device = NULL;
392 if (root->fs_info->sb->s_flags & MS_RDONLY)
395 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
400 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
405 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
406 namelen = strlen(vol_args->name);
408 mutex_lock(&root->fs_info->volume_mutex);
409 sizestr = vol_args->name;
410 devstr = strchr(sizestr, ':');
413 sizestr = devstr + 1;
415 devstr = vol_args->name;
416 devid = simple_strtoull(devstr, &end, 10);
417 printk(KERN_INFO "resizing devid %llu\n", devid);
419 device = btrfs_find_device(root, devid, NULL, NULL);
421 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
425 if (!strcmp(sizestr, "max"))
426 new_size = device->bdev->bd_inode->i_size;
428 if (sizestr[0] == '-') {
431 } else if (sizestr[0] == '+') {
435 new_size = btrfs_parse_size(sizestr);
442 old_size = device->total_bytes;
445 if (new_size > old_size) {
449 new_size = old_size - new_size;
450 } else if (mod > 0) {
451 new_size = old_size + new_size;
454 if (new_size < 256 * 1024 * 1024) {
458 if (new_size > device->bdev->bd_inode->i_size) {
463 do_div(new_size, root->sectorsize);
464 new_size *= root->sectorsize;
466 printk(KERN_INFO "new size for %s is %llu\n",
467 device->name, (unsigned long long)new_size);
469 if (new_size > old_size) {
470 trans = btrfs_start_transaction(root, 1);
471 ret = btrfs_grow_device(trans, device, new_size);
472 btrfs_commit_transaction(trans, root);
474 ret = btrfs_shrink_device(device, new_size);
478 mutex_unlock(&root->fs_info->volume_mutex);
484 static noinline int btrfs_ioctl_snap_create(struct file *file,
485 void __user *arg, int subvol)
487 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
488 struct btrfs_ioctl_vol_args *vol_args;
489 struct btrfs_dir_item *di;
490 struct btrfs_path *path;
491 struct file *src_file;
496 if (root->fs_info->sb->s_flags & MS_RDONLY)
499 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
504 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
509 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
510 namelen = strlen(vol_args->name);
511 if (strchr(vol_args->name, '/')) {
516 path = btrfs_alloc_path();
522 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
523 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
525 vol_args->name, namelen, 0);
526 btrfs_free_path(path);
528 if (di && !IS_ERR(di)) {
539 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
540 file->f_path.dentry->d_inode->i_mode,
543 struct inode *src_inode;
544 src_file = fget(vol_args->fd);
550 src_inode = src_file->f_path.dentry->d_inode;
551 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
552 printk("btrfs: Snapshot src from another FS\n");
557 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
558 file->f_path.dentry->d_inode->i_mode,
559 namelen, BTRFS_I(src_inode)->root);
568 static int btrfs_ioctl_defrag(struct file *file)
570 struct inode *inode = fdentry(file)->d_inode;
571 struct btrfs_root *root = BTRFS_I(inode)->root;
574 ret = mnt_want_write(file->f_path.mnt);
578 switch (inode->i_mode & S_IFMT) {
580 btrfs_defrag_root(root, 0);
581 btrfs_defrag_root(root->fs_info->extent_root, 0);
584 btrfs_defrag_file(file);
591 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
593 struct btrfs_ioctl_vol_args *vol_args;
596 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
601 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
605 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
606 ret = btrfs_init_new_device(root, vol_args->name);
613 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
615 struct btrfs_ioctl_vol_args *vol_args;
618 if (root->fs_info->sb->s_flags & MS_RDONLY)
621 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
626 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
630 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
631 ret = btrfs_rm_device(root, vol_args->name);
638 long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, u64 off,
639 u64 olen, u64 destoff)
641 struct inode *inode = fdentry(file)->d_inode;
642 struct btrfs_root *root = BTRFS_I(inode)->root;
643 struct file *src_file;
645 struct btrfs_trans_handle *trans;
646 struct btrfs_path *path;
647 struct extent_buffer *leaf;
649 struct btrfs_key key;
654 u64 bs = root->fs_info->sb->s_blocksize;
659 * - split compressed inline extents. annoying: we need to
660 * decompress into destination's address_space (the file offset
661 * may change, so source mapping won't do), then recompress (or
662 * otherwise reinsert) a subrange.
663 * - allow ranges within the same file to be cloned (provided
664 * they don't overlap)?
667 ret = mnt_want_write(file->f_path.mnt);
671 src_file = fget(srcfd);
674 src = src_file->f_dentry->d_inode;
681 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
685 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
689 buf = vmalloc(btrfs_level_size(root, 0));
693 path = btrfs_alloc_path();
701 mutex_lock(&inode->i_mutex);
702 mutex_lock(&src->i_mutex);
704 mutex_lock(&src->i_mutex);
705 mutex_lock(&inode->i_mutex);
708 /* determine range to clone */
710 if (off >= src->i_size || off + len > src->i_size)
713 olen = len = src->i_size - off;
714 /* if we extend to eof, continue to block boundary */
715 if (off + len == src->i_size)
716 len = ((src->i_size + bs-1) & ~(bs-1))
719 /* verify the end result is block aligned */
720 if ((off & (bs-1)) ||
721 ((off + len) & (bs-1)))
724 printk("final src extent is %llu~%llu\n", off, len);
725 printk("final dst extent is %llu~%llu\n", destoff, len);
727 /* do any pending delalloc/csum calc on src, one way or
728 another, and lock file content */
730 struct btrfs_ordered_extent *ordered;
731 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
732 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
733 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
735 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
737 btrfs_put_ordered_extent(ordered);
738 btrfs_wait_ordered_range(src, off, off+len);
741 trans = btrfs_start_transaction(root, 1);
744 /* punch hole in destination first */
745 btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
748 key.objectid = src->i_ino;
749 key.type = BTRFS_EXTENT_DATA_KEY;
754 * note the key will change type as we walk through the
757 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
761 nritems = btrfs_header_nritems(path->nodes[0]);
762 if (path->slots[0] >= nritems) {
763 ret = btrfs_next_leaf(root, path);
768 nritems = btrfs_header_nritems(path->nodes[0]);
770 leaf = path->nodes[0];
771 slot = path->slots[0];
773 btrfs_item_key_to_cpu(leaf, &key, slot);
774 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
775 key.objectid != src->i_ino)
778 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
779 struct btrfs_file_extent_item *extent;
782 struct btrfs_key new_key;
783 u64 disko = 0, diskl = 0;
784 u64 datao = 0, datal = 0;
787 size = btrfs_item_size_nr(leaf, slot);
788 read_extent_buffer(leaf, buf,
789 btrfs_item_ptr_offset(leaf, slot),
792 extent = btrfs_item_ptr(leaf, slot,
793 struct btrfs_file_extent_item);
794 comp = btrfs_file_extent_compression(leaf, extent);
795 type = btrfs_file_extent_type(leaf, extent);
796 if (type == BTRFS_FILE_EXTENT_REG) {
797 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
798 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
799 datao = btrfs_file_extent_offset(leaf, extent);
800 datal = btrfs_file_extent_num_bytes(leaf, extent);
801 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
802 /* take upper bound, may be compressed */
803 datal = btrfs_file_extent_ram_bytes(leaf,
806 btrfs_release_path(root, path);
808 if (key.offset + datal < off ||
809 key.offset >= off+len)
812 memcpy(&new_key, &key, sizeof(new_key));
813 new_key.objectid = inode->i_ino;
814 new_key.offset = key.offset + destoff - off;
816 if (type == BTRFS_FILE_EXTENT_REG) {
817 ret = btrfs_insert_empty_item(trans, root, path,
822 leaf = path->nodes[0];
823 slot = path->slots[0];
824 write_extent_buffer(leaf, buf,
825 btrfs_item_ptr_offset(leaf, slot),
828 extent = btrfs_item_ptr(leaf, slot,
829 struct btrfs_file_extent_item);
830 printk(" orig disk %llu~%llu data %llu~%llu\n",
831 disko, diskl, datao, datal);
833 if (off > key.offset) {
834 datao += off - key.offset;
835 datal -= off - key.offset;
837 if (key.offset + datao + datal + key.offset >
839 datal = off + len - key.offset - datao;
840 /* disko == 0 means it's a hole */
843 printk(" final disk %llu~%llu data %llu~%llu\n",
844 disko, diskl, datao, datal);
846 btrfs_set_file_extent_offset(leaf, extent,
848 btrfs_set_file_extent_num_bytes(leaf, extent,
851 inode_add_bytes(inode, datal);
852 ret = btrfs_inc_extent_ref(trans, root,
853 disko, diskl, leaf->start,
854 root->root_key.objectid,
859 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
862 if (off > key.offset) {
863 skip = off - key.offset;
864 new_key.offset += skip;
866 if (key.offset + datal > off+len)
867 trim = key.offset + datal - (off+len);
868 printk("len %lld skip %lld trim %lld\n",
870 if (comp && (skip || trim)) {
871 printk("btrfs clone_range can't split compressed inline extents yet\n");
876 datal -= skip + trim;
877 ret = btrfs_insert_empty_item(trans, root, path,
883 u32 start = btrfs_file_extent_calc_inline_size(0);
884 memmove(buf+start, buf+start+skip,
888 leaf = path->nodes[0];
889 slot = path->slots[0];
890 write_extent_buffer(leaf, buf,
891 btrfs_item_ptr_offset(leaf, slot),
893 inode_add_bytes(inode, datal);
896 btrfs_mark_buffer_dirty(leaf);
899 if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
901 struct btrfs_key new_key;
905 size = btrfs_item_size_nr(leaf, slot);
906 coverslen = (size / BTRFS_CRC32_SIZE) <<
907 root->fs_info->sb->s_blocksize_bits;
908 printk("csums for %llu~%llu\n",
909 key.offset, coverslen);
910 if (key.offset + coverslen < off ||
911 key.offset >= off+len)
914 read_extent_buffer(leaf, buf,
915 btrfs_item_ptr_offset(leaf, slot),
917 btrfs_release_path(root, path);
920 if (off > key.offset)
921 coff = ((off - key.offset) >>
922 root->fs_info->sb->s_blocksize_bits) *
925 if (key.offset + coverslen > off+len)
926 clen -= ((key.offset+coverslen-off-len) >>
927 root->fs_info->sb->s_blocksize_bits) *
929 printk(" will dup %d~%d of %d\n",
932 memcpy(&new_key, &key, sizeof(new_key));
933 new_key.objectid = inode->i_ino;
934 new_key.offset = key.offset + destoff - off;
936 ret = btrfs_insert_empty_item(trans, root, path,
941 leaf = path->nodes[0];
942 slot = path->slots[0];
943 write_extent_buffer(leaf, buf + coff,
944 btrfs_item_ptr_offset(leaf, slot),
946 btrfs_mark_buffer_dirty(leaf);
950 btrfs_release_path(root, path);
955 btrfs_release_path(root, path);
957 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
958 if (destoff + olen > inode->i_size)
959 btrfs_i_size_write(inode, destoff + olen);
960 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
961 ret = btrfs_update_inode(trans, root, inode);
963 btrfs_end_transaction(trans, root);
964 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
966 vmtruncate(inode, 0);
968 mutex_unlock(&src->i_mutex);
969 mutex_unlock(&inode->i_mutex);
971 btrfs_free_path(path);
977 long btrfs_ioctl_clone_range(struct file *file, unsigned long argptr)
979 struct btrfs_ioctl_clone_range_args args;
981 if (copy_from_user(&args, (void *)argptr, sizeof(args)))
983 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
984 args.src_length, args.dest_offset);
988 * there are many ways the trans_start and trans_end ioctls can lead
989 * to deadlocks. They should only be used by applications that
990 * basically own the machine, and have a very in depth understanding
991 * of all the possible deadlocks and enospc problems.
993 long btrfs_ioctl_trans_start(struct file *file)
995 struct inode *inode = fdentry(file)->d_inode;
996 struct btrfs_root *root = BTRFS_I(inode)->root;
997 struct btrfs_trans_handle *trans;
1000 if (!capable(CAP_SYS_ADMIN))
1003 if (file->private_data) {
1008 ret = mnt_want_write(file->f_path.mnt);
1012 mutex_lock(&root->fs_info->trans_mutex);
1013 root->fs_info->open_ioctl_trans++;
1014 mutex_unlock(&root->fs_info->trans_mutex);
1016 trans = btrfs_start_ioctl_transaction(root, 0);
1018 file->private_data = trans;
1021 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1027 * there are many ways the trans_start and trans_end ioctls can lead
1028 * to deadlocks. They should only be used by applications that
1029 * basically own the machine, and have a very in depth understanding
1030 * of all the possible deadlocks and enospc problems.
1032 long btrfs_ioctl_trans_end(struct file *file)
1034 struct inode *inode = fdentry(file)->d_inode;
1035 struct btrfs_root *root = BTRFS_I(inode)->root;
1036 struct btrfs_trans_handle *trans;
1039 trans = file->private_data;
1044 btrfs_end_transaction(trans, root);
1045 file->private_data = NULL;
1047 mutex_lock(&root->fs_info->trans_mutex);
1048 root->fs_info->open_ioctl_trans--;
1049 mutex_unlock(&root->fs_info->trans_mutex);
1055 long btrfs_ioctl(struct file *file, unsigned int
1056 cmd, unsigned long arg)
1058 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1061 case BTRFS_IOC_SNAP_CREATE:
1062 return btrfs_ioctl_snap_create(file, (void __user *)arg, 0);
1063 case BTRFS_IOC_SUBVOL_CREATE:
1064 return btrfs_ioctl_snap_create(file, (void __user *)arg, 1);
1065 case BTRFS_IOC_DEFRAG:
1066 return btrfs_ioctl_defrag(file);
1067 case BTRFS_IOC_RESIZE:
1068 return btrfs_ioctl_resize(root, (void __user *)arg);
1069 case BTRFS_IOC_ADD_DEV:
1070 return btrfs_ioctl_add_dev(root, (void __user *)arg);
1071 case BTRFS_IOC_RM_DEV:
1072 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
1073 case BTRFS_IOC_BALANCE:
1074 return btrfs_balance(root->fs_info->dev_root);
1075 case BTRFS_IOC_CLONE:
1076 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1077 case BTRFS_IOC_CLONE_RANGE:
1078 return btrfs_ioctl_clone_range(file, arg);
1079 case BTRFS_IOC_TRANS_START:
1080 return btrfs_ioctl_trans_start(file);
1081 case BTRFS_IOC_TRANS_END:
1082 return btrfs_ioctl_trans_end(file);
1083 case BTRFS_IOC_SYNC:
1084 btrfs_start_delalloc_inodes(root);
1085 btrfs_sync_fs(file->f_dentry->d_sb, 1);