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 /* add the backref first */
150 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
151 objectid, BTRFS_ROOT_BACKREF_KEY,
152 root->root_key.objectid,
153 dir->i_ino, index, name, namelen);
157 /* now add the forward ref */
158 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
159 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
161 dir->i_ino, index, name, namelen);
165 ret = btrfs_commit_transaction(trans, root);
169 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
172 trans = btrfs_start_transaction(new_root, 1);
175 ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
176 BTRFS_I(dir)->block_group);
181 nr = trans->blocks_used;
182 err = btrfs_commit_transaction(trans, new_root);
186 btrfs_btree_balance_dirty(root, nr);
190 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
191 char *name, int namelen)
193 struct btrfs_pending_snapshot *pending_snapshot;
194 struct btrfs_trans_handle *trans;
197 unsigned long nr = 0;
202 ret = btrfs_check_free_space(root, 1, 0);
206 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
207 if (!pending_snapshot) {
211 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
212 if (!pending_snapshot->name) {
214 kfree(pending_snapshot);
217 memcpy(pending_snapshot->name, name, namelen);
218 pending_snapshot->name[namelen] = '\0';
219 pending_snapshot->dentry = dentry;
220 trans = btrfs_start_transaction(root, 1);
222 pending_snapshot->root = root;
223 list_add(&pending_snapshot->list,
224 &trans->transaction->pending_snapshots);
225 err = btrfs_commit_transaction(trans, root);
228 btrfs_btree_balance_dirty(root, nr);
232 /* copy of may_create in fs/namei.c() */
233 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
239 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
243 * Create a new subvolume below @parent. This is largely modeled after
244 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
245 * inside this filesystem so it's quite a bit simpler.
247 static noinline int btrfs_mksubvol(struct path *parent, char *name,
248 int mode, int namelen,
249 struct btrfs_root *snap_src)
251 struct dentry *dentry;
254 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
256 dentry = lookup_one_len(name, parent->dentry, namelen);
257 error = PTR_ERR(dentry);
265 if (!IS_POSIXACL(parent->dentry->d_inode))
266 mode &= ~current->fs->umask;
268 error = mnt_want_write(parent->mnt);
272 error = btrfs_may_create(parent->dentry->d_inode, dentry);
277 * Actually perform the low-level subvolume creation after all
280 * Eventually we want to pass in an inode under which we create this
281 * subvolume, but for now all are under the filesystem root.
283 * Also we should pass on the mode eventually to allow creating new
284 * subvolume with specific mode bits.
287 struct dentry *dir = dentry->d_parent;
288 struct dentry *test = dir->d_parent;
289 struct btrfs_path *path = btrfs_alloc_path();
292 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
294 test_oid = snap_src->root_key.objectid;
296 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
297 path, parent_oid, test_oid);
300 btrfs_release_path(snap_src->fs_info->tree_root, path);
302 /* we need to make sure we aren't creating a directory loop
303 * by taking a snapshot of something that has our current
304 * subvol in its directory tree. So, this loops through
305 * the dentries and checks the forward refs for each subvolume
306 * to see if is references the subvolume where we are
307 * placing this new snapshot.
311 dir == snap_src->fs_info->sb->s_root ||
312 test == snap_src->fs_info->sb->s_root ||
313 test->d_inode->i_sb != snap_src->fs_info->sb) {
316 if (S_ISLNK(test->d_inode->i_mode)) {
317 printk("Symlink in snapshot path, failed\n");
319 btrfs_free_path(path);
323 BTRFS_I(test->d_inode)->root->root_key.objectid;
324 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
325 path, test_oid, parent_oid);
327 printk("Snapshot creation failed, looping\n");
329 btrfs_free_path(path);
332 btrfs_release_path(snap_src->fs_info->tree_root, path);
333 test = test->d_parent;
336 btrfs_free_path(path);
337 error = create_snapshot(snap_src, dentry, name, namelen);
339 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
340 dentry, name, namelen);
345 fsnotify_mkdir(parent->dentry->d_inode, dentry);
347 mnt_drop_write(parent->mnt);
351 mutex_unlock(&parent->dentry->d_inode->i_mutex);
356 int btrfs_defrag_file(struct file *file)
358 struct inode *inode = fdentry(file)->d_inode;
359 struct btrfs_root *root = BTRFS_I(inode)->root;
360 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
361 struct btrfs_ordered_extent *ordered;
363 unsigned long last_index;
364 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
365 unsigned long total_read = 0;
371 ret = btrfs_check_free_space(root, inode->i_size, 0);
375 mutex_lock(&inode->i_mutex);
376 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
377 for (i = 0; i <= last_index; i++) {
378 if (total_read % ra_pages == 0) {
379 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
380 min(last_index, i + ra_pages - 1));
384 page = grab_cache_page(inode->i_mapping, i);
387 if (!PageUptodate(page)) {
388 btrfs_readpage(NULL, page);
390 if (!PageUptodate(page)) {
392 page_cache_release(page);
397 wait_on_page_writeback(page);
399 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
400 page_end = page_start + PAGE_CACHE_SIZE - 1;
401 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
403 ordered = btrfs_lookup_ordered_extent(inode, page_start);
405 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
407 page_cache_release(page);
408 btrfs_start_ordered_extent(inode, ordered, 1);
409 btrfs_put_ordered_extent(ordered);
412 set_page_extent_mapped(page);
415 * this makes sure page_mkwrite is called on the
416 * page if it is dirtied again later
418 clear_page_dirty_for_io(page);
420 btrfs_set_extent_delalloc(inode, page_start, page_end);
422 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
423 set_page_dirty(page);
425 page_cache_release(page);
426 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
430 mutex_unlock(&inode->i_mutex);
435 * Called inside transaction, so use GFP_NOFS
438 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
443 struct btrfs_ioctl_vol_args *vol_args;
444 struct btrfs_trans_handle *trans;
445 struct btrfs_device *device = NULL;
452 if (root->fs_info->sb->s_flags & MS_RDONLY)
455 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
460 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
465 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
466 namelen = strlen(vol_args->name);
468 mutex_lock(&root->fs_info->volume_mutex);
469 sizestr = vol_args->name;
470 devstr = strchr(sizestr, ':');
473 sizestr = devstr + 1;
475 devstr = vol_args->name;
476 devid = simple_strtoull(devstr, &end, 10);
477 printk(KERN_INFO "resizing devid %llu\n", devid);
479 device = btrfs_find_device(root, devid, NULL, NULL);
481 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
485 if (!strcmp(sizestr, "max"))
486 new_size = device->bdev->bd_inode->i_size;
488 if (sizestr[0] == '-') {
491 } else if (sizestr[0] == '+') {
495 new_size = btrfs_parse_size(sizestr);
502 old_size = device->total_bytes;
505 if (new_size > old_size) {
509 new_size = old_size - new_size;
510 } else if (mod > 0) {
511 new_size = old_size + new_size;
514 if (new_size < 256 * 1024 * 1024) {
518 if (new_size > device->bdev->bd_inode->i_size) {
523 do_div(new_size, root->sectorsize);
524 new_size *= root->sectorsize;
526 printk(KERN_INFO "new size for %s is %llu\n",
527 device->name, (unsigned long long)new_size);
529 if (new_size > old_size) {
530 trans = btrfs_start_transaction(root, 1);
531 ret = btrfs_grow_device(trans, device, new_size);
532 btrfs_commit_transaction(trans, root);
534 ret = btrfs_shrink_device(device, new_size);
538 mutex_unlock(&root->fs_info->volume_mutex);
544 static noinline int btrfs_ioctl_snap_create(struct file *file,
545 void __user *arg, int subvol)
547 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
548 struct btrfs_ioctl_vol_args *vol_args;
549 struct btrfs_dir_item *di;
550 struct btrfs_path *path;
551 struct file *src_file;
556 if (root->fs_info->sb->s_flags & MS_RDONLY)
559 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
564 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
569 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
570 namelen = strlen(vol_args->name);
571 if (strchr(vol_args->name, '/')) {
576 path = btrfs_alloc_path();
582 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
583 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
585 vol_args->name, namelen, 0);
586 btrfs_free_path(path);
588 if (di && !IS_ERR(di)) {
599 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
600 file->f_path.dentry->d_inode->i_mode,
603 struct inode *src_inode;
604 src_file = fget(vol_args->fd);
610 src_inode = src_file->f_path.dentry->d_inode;
611 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
612 printk("btrfs: Snapshot src from another FS\n");
617 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
618 file->f_path.dentry->d_inode->i_mode,
619 namelen, BTRFS_I(src_inode)->root);
628 static int btrfs_ioctl_defrag(struct file *file)
630 struct inode *inode = fdentry(file)->d_inode;
631 struct btrfs_root *root = BTRFS_I(inode)->root;
634 ret = mnt_want_write(file->f_path.mnt);
638 switch (inode->i_mode & S_IFMT) {
640 btrfs_defrag_root(root, 0);
641 btrfs_defrag_root(root->fs_info->extent_root, 0);
644 btrfs_defrag_file(file);
651 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
653 struct btrfs_ioctl_vol_args *vol_args;
656 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
661 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
665 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
666 ret = btrfs_init_new_device(root, vol_args->name);
673 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
675 struct btrfs_ioctl_vol_args *vol_args;
678 if (root->fs_info->sb->s_flags & MS_RDONLY)
681 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
686 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
690 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
691 ret = btrfs_rm_device(root, vol_args->name);
698 long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, u64 off,
699 u64 olen, u64 destoff)
701 struct inode *inode = fdentry(file)->d_inode;
702 struct btrfs_root *root = BTRFS_I(inode)->root;
703 struct file *src_file;
705 struct btrfs_trans_handle *trans;
706 struct btrfs_path *path;
707 struct extent_buffer *leaf;
709 struct btrfs_key key;
714 u64 bs = root->fs_info->sb->s_blocksize;
719 * - split compressed inline extents. annoying: we need to
720 * decompress into destination's address_space (the file offset
721 * may change, so source mapping won't do), then recompress (or
722 * otherwise reinsert) a subrange.
723 * - allow ranges within the same file to be cloned (provided
724 * they don't overlap)?
727 ret = mnt_want_write(file->f_path.mnt);
731 src_file = fget(srcfd);
734 src = src_file->f_dentry->d_inode;
741 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
745 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
749 buf = vmalloc(btrfs_level_size(root, 0));
753 path = btrfs_alloc_path();
761 mutex_lock(&inode->i_mutex);
762 mutex_lock(&src->i_mutex);
764 mutex_lock(&src->i_mutex);
765 mutex_lock(&inode->i_mutex);
768 /* determine range to clone */
770 if (off >= src->i_size || off + len > src->i_size)
773 olen = len = src->i_size - off;
774 /* if we extend to eof, continue to block boundary */
775 if (off + len == src->i_size)
776 len = ((src->i_size + bs-1) & ~(bs-1))
779 /* verify the end result is block aligned */
780 if ((off & (bs-1)) ||
781 ((off + len) & (bs-1)))
784 printk("final src extent is %llu~%llu\n", off, len);
785 printk("final dst extent is %llu~%llu\n", destoff, len);
787 /* do any pending delalloc/csum calc on src, one way or
788 another, and lock file content */
790 struct btrfs_ordered_extent *ordered;
791 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
792 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
793 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
795 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
797 btrfs_put_ordered_extent(ordered);
798 btrfs_wait_ordered_range(src, off, off+len);
801 trans = btrfs_start_transaction(root, 1);
804 /* punch hole in destination first */
805 btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
808 key.objectid = src->i_ino;
809 key.type = BTRFS_EXTENT_DATA_KEY;
814 * note the key will change type as we walk through the
817 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
821 nritems = btrfs_header_nritems(path->nodes[0]);
822 if (path->slots[0] >= nritems) {
823 ret = btrfs_next_leaf(root, path);
828 nritems = btrfs_header_nritems(path->nodes[0]);
830 leaf = path->nodes[0];
831 slot = path->slots[0];
833 btrfs_item_key_to_cpu(leaf, &key, slot);
834 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
835 key.objectid != src->i_ino)
838 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
839 struct btrfs_file_extent_item *extent;
842 struct btrfs_key new_key;
843 u64 disko = 0, diskl = 0;
844 u64 datao = 0, datal = 0;
847 size = btrfs_item_size_nr(leaf, slot);
848 read_extent_buffer(leaf, buf,
849 btrfs_item_ptr_offset(leaf, slot),
852 extent = btrfs_item_ptr(leaf, slot,
853 struct btrfs_file_extent_item);
854 comp = btrfs_file_extent_compression(leaf, extent);
855 type = btrfs_file_extent_type(leaf, extent);
856 if (type == BTRFS_FILE_EXTENT_REG) {
857 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
858 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
859 datao = btrfs_file_extent_offset(leaf, extent);
860 datal = btrfs_file_extent_num_bytes(leaf, extent);
861 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
862 /* take upper bound, may be compressed */
863 datal = btrfs_file_extent_ram_bytes(leaf,
866 btrfs_release_path(root, path);
868 if (key.offset + datal < off ||
869 key.offset >= off+len)
872 memcpy(&new_key, &key, sizeof(new_key));
873 new_key.objectid = inode->i_ino;
874 new_key.offset = key.offset + destoff - off;
876 if (type == BTRFS_FILE_EXTENT_REG) {
877 ret = btrfs_insert_empty_item(trans, root, path,
882 leaf = path->nodes[0];
883 slot = path->slots[0];
884 write_extent_buffer(leaf, buf,
885 btrfs_item_ptr_offset(leaf, slot),
888 extent = btrfs_item_ptr(leaf, slot,
889 struct btrfs_file_extent_item);
890 printk(" orig disk %llu~%llu data %llu~%llu\n",
891 disko, diskl, datao, datal);
893 if (off > key.offset) {
894 datao += off - key.offset;
895 datal -= off - key.offset;
897 if (key.offset + datao + datal + key.offset >
899 datal = off + len - key.offset - datao;
900 /* disko == 0 means it's a hole */
903 printk(" final disk %llu~%llu data %llu~%llu\n",
904 disko, diskl, datao, datal);
906 btrfs_set_file_extent_offset(leaf, extent,
908 btrfs_set_file_extent_num_bytes(leaf, extent,
911 inode_add_bytes(inode, datal);
912 ret = btrfs_inc_extent_ref(trans, root,
913 disko, diskl, leaf->start,
914 root->root_key.objectid,
919 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
922 if (off > key.offset) {
923 skip = off - key.offset;
924 new_key.offset += skip;
926 if (key.offset + datal > off+len)
927 trim = key.offset + datal - (off+len);
928 printk("len %lld skip %lld trim %lld\n",
930 if (comp && (skip || trim)) {
931 printk("btrfs clone_range can't split compressed inline extents yet\n");
936 datal -= skip + trim;
937 ret = btrfs_insert_empty_item(trans, root, path,
943 u32 start = btrfs_file_extent_calc_inline_size(0);
944 memmove(buf+start, buf+start+skip,
948 leaf = path->nodes[0];
949 slot = path->slots[0];
950 write_extent_buffer(leaf, buf,
951 btrfs_item_ptr_offset(leaf, slot),
953 inode_add_bytes(inode, datal);
956 btrfs_mark_buffer_dirty(leaf);
959 if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
961 struct btrfs_key new_key;
965 size = btrfs_item_size_nr(leaf, slot);
966 coverslen = (size / BTRFS_CRC32_SIZE) <<
967 root->fs_info->sb->s_blocksize_bits;
968 printk("csums for %llu~%llu\n",
969 key.offset, coverslen);
970 if (key.offset + coverslen < off ||
971 key.offset >= off+len)
974 read_extent_buffer(leaf, buf,
975 btrfs_item_ptr_offset(leaf, slot),
977 btrfs_release_path(root, path);
980 if (off > key.offset)
981 coff = ((off - key.offset) >>
982 root->fs_info->sb->s_blocksize_bits) *
985 if (key.offset + coverslen > off+len)
986 clen -= ((key.offset+coverslen-off-len) >>
987 root->fs_info->sb->s_blocksize_bits) *
989 printk(" will dup %d~%d of %d\n",
992 memcpy(&new_key, &key, sizeof(new_key));
993 new_key.objectid = inode->i_ino;
994 new_key.offset = key.offset + destoff - off;
996 ret = btrfs_insert_empty_item(trans, root, path,
1001 leaf = path->nodes[0];
1002 slot = path->slots[0];
1003 write_extent_buffer(leaf, buf + coff,
1004 btrfs_item_ptr_offset(leaf, slot),
1006 btrfs_mark_buffer_dirty(leaf);
1010 btrfs_release_path(root, path);
1015 btrfs_release_path(root, path);
1017 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1018 if (destoff + olen > inode->i_size)
1019 btrfs_i_size_write(inode, destoff + olen);
1020 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1021 ret = btrfs_update_inode(trans, root, inode);
1023 btrfs_end_transaction(trans, root);
1024 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1026 vmtruncate(inode, 0);
1028 mutex_unlock(&src->i_mutex);
1029 mutex_unlock(&inode->i_mutex);
1031 btrfs_free_path(path);
1037 long btrfs_ioctl_clone_range(struct file *file, unsigned long argptr)
1039 struct btrfs_ioctl_clone_range_args args;
1041 if (copy_from_user(&args, (void *)argptr, sizeof(args)))
1043 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1044 args.src_length, args.dest_offset);
1048 * there are many ways the trans_start and trans_end ioctls can lead
1049 * to deadlocks. They should only be used by applications that
1050 * basically own the machine, and have a very in depth understanding
1051 * of all the possible deadlocks and enospc problems.
1053 long btrfs_ioctl_trans_start(struct file *file)
1055 struct inode *inode = fdentry(file)->d_inode;
1056 struct btrfs_root *root = BTRFS_I(inode)->root;
1057 struct btrfs_trans_handle *trans;
1060 if (!capable(CAP_SYS_ADMIN))
1063 if (file->private_data) {
1068 ret = mnt_want_write(file->f_path.mnt);
1072 mutex_lock(&root->fs_info->trans_mutex);
1073 root->fs_info->open_ioctl_trans++;
1074 mutex_unlock(&root->fs_info->trans_mutex);
1076 trans = btrfs_start_ioctl_transaction(root, 0);
1078 file->private_data = trans;
1081 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1087 * there are many ways the trans_start and trans_end ioctls can lead
1088 * to deadlocks. They should only be used by applications that
1089 * basically own the machine, and have a very in depth understanding
1090 * of all the possible deadlocks and enospc problems.
1092 long btrfs_ioctl_trans_end(struct file *file)
1094 struct inode *inode = fdentry(file)->d_inode;
1095 struct btrfs_root *root = BTRFS_I(inode)->root;
1096 struct btrfs_trans_handle *trans;
1099 trans = file->private_data;
1104 btrfs_end_transaction(trans, root);
1105 file->private_data = NULL;
1107 mutex_lock(&root->fs_info->trans_mutex);
1108 root->fs_info->open_ioctl_trans--;
1109 mutex_unlock(&root->fs_info->trans_mutex);
1115 long btrfs_ioctl(struct file *file, unsigned int
1116 cmd, unsigned long arg)
1118 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1121 case BTRFS_IOC_SNAP_CREATE:
1122 return btrfs_ioctl_snap_create(file, (void __user *)arg, 0);
1123 case BTRFS_IOC_SUBVOL_CREATE:
1124 return btrfs_ioctl_snap_create(file, (void __user *)arg, 1);
1125 case BTRFS_IOC_DEFRAG:
1126 return btrfs_ioctl_defrag(file);
1127 case BTRFS_IOC_RESIZE:
1128 return btrfs_ioctl_resize(root, (void __user *)arg);
1129 case BTRFS_IOC_ADD_DEV:
1130 return btrfs_ioctl_add_dev(root, (void __user *)arg);
1131 case BTRFS_IOC_RM_DEV:
1132 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
1133 case BTRFS_IOC_BALANCE:
1134 return btrfs_balance(root->fs_info->dev_root);
1135 case BTRFS_IOC_CLONE:
1136 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1137 case BTRFS_IOC_CLONE_RANGE:
1138 return btrfs_ioctl_clone_range(file, arg);
1139 case BTRFS_IOC_TRANS_START:
1140 return btrfs_ioctl_trans_start(file);
1141 case BTRFS_IOC_TRANS_END:
1142 return btrfs_ioctl_trans_end(file);
1143 case BTRFS_IOC_SYNC:
1144 btrfs_start_delalloc_inodes(root);
1145 btrfs_sync_fs(file->f_dentry->d_sb, 1);