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/backing-dev.h>
31 #include <linux/mount.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/swap.h>
35 #include <linux/writeback.h>
36 #include <linux/statfs.h>
37 #include <linux/compat.h>
38 #include <linux/bit_spinlock.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/vmalloc.h>
45 #include "transaction.h"
46 #include "btrfs_inode.h"
48 #include "print-tree.h"
52 /* Mask out flags that are inappropriate for the given type of inode. */
53 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
57 else if (S_ISREG(mode))
58 return flags & ~FS_DIRSYNC_FL;
60 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
64 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
66 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
68 unsigned int iflags = 0;
70 if (flags & BTRFS_INODE_SYNC)
72 if (flags & BTRFS_INODE_IMMUTABLE)
73 iflags |= FS_IMMUTABLE_FL;
74 if (flags & BTRFS_INODE_APPEND)
75 iflags |= FS_APPEND_FL;
76 if (flags & BTRFS_INODE_NODUMP)
77 iflags |= FS_NODUMP_FL;
78 if (flags & BTRFS_INODE_NOATIME)
79 iflags |= FS_NOATIME_FL;
80 if (flags & BTRFS_INODE_DIRSYNC)
81 iflags |= FS_DIRSYNC_FL;
87 * Update inode->i_flags based on the btrfs internal flags.
89 void btrfs_update_iflags(struct inode *inode)
91 struct btrfs_inode *ip = BTRFS_I(inode);
93 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
95 if (ip->flags & BTRFS_INODE_SYNC)
96 inode->i_flags |= S_SYNC;
97 if (ip->flags & BTRFS_INODE_IMMUTABLE)
98 inode->i_flags |= S_IMMUTABLE;
99 if (ip->flags & BTRFS_INODE_APPEND)
100 inode->i_flags |= S_APPEND;
101 if (ip->flags & BTRFS_INODE_NOATIME)
102 inode->i_flags |= S_NOATIME;
103 if (ip->flags & BTRFS_INODE_DIRSYNC)
104 inode->i_flags |= S_DIRSYNC;
108 * Inherit flags from the parent inode.
110 * Unlike extN we don't have any flags we don't want to inherit currently.
112 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
119 flags = BTRFS_I(dir)->flags;
121 if (S_ISREG(inode->i_mode))
122 flags &= ~BTRFS_INODE_DIRSYNC;
123 else if (!S_ISDIR(inode->i_mode))
124 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
126 BTRFS_I(inode)->flags = flags;
127 btrfs_update_iflags(inode);
130 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
132 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
133 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
135 if (copy_to_user(arg, &flags, sizeof(flags)))
140 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
142 struct inode *inode = file->f_path.dentry->d_inode;
143 struct btrfs_inode *ip = BTRFS_I(inode);
144 struct btrfs_root *root = ip->root;
145 struct btrfs_trans_handle *trans;
146 unsigned int flags, oldflags;
149 if (copy_from_user(&flags, arg, sizeof(flags)))
152 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
153 FS_NOATIME_FL | FS_NODUMP_FL | \
154 FS_SYNC_FL | FS_DIRSYNC_FL))
157 if (!is_owner_or_cap(inode))
160 mutex_lock(&inode->i_mutex);
162 flags = btrfs_mask_flags(inode->i_mode, flags);
163 oldflags = btrfs_flags_to_ioctl(ip->flags);
164 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
165 if (!capable(CAP_LINUX_IMMUTABLE)) {
171 ret = mnt_want_write(file->f_path.mnt);
175 if (flags & FS_SYNC_FL)
176 ip->flags |= BTRFS_INODE_SYNC;
178 ip->flags &= ~BTRFS_INODE_SYNC;
179 if (flags & FS_IMMUTABLE_FL)
180 ip->flags |= BTRFS_INODE_IMMUTABLE;
182 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
183 if (flags & FS_APPEND_FL)
184 ip->flags |= BTRFS_INODE_APPEND;
186 ip->flags &= ~BTRFS_INODE_APPEND;
187 if (flags & FS_NODUMP_FL)
188 ip->flags |= BTRFS_INODE_NODUMP;
190 ip->flags &= ~BTRFS_INODE_NODUMP;
191 if (flags & FS_NOATIME_FL)
192 ip->flags |= BTRFS_INODE_NOATIME;
194 ip->flags &= ~BTRFS_INODE_NOATIME;
195 if (flags & FS_DIRSYNC_FL)
196 ip->flags |= BTRFS_INODE_DIRSYNC;
198 ip->flags &= ~BTRFS_INODE_DIRSYNC;
201 trans = btrfs_join_transaction(root, 1);
204 ret = btrfs_update_inode(trans, root, inode);
207 btrfs_update_iflags(inode);
208 inode->i_ctime = CURRENT_TIME;
209 btrfs_end_transaction(trans, root);
211 mnt_drop_write(file->f_path.mnt);
213 mutex_unlock(&inode->i_mutex);
217 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
219 struct inode *inode = file->f_path.dentry->d_inode;
221 return put_user(inode->i_generation, arg);
224 static noinline int create_subvol(struct btrfs_root *root,
225 struct dentry *dentry,
226 char *name, int namelen)
228 struct btrfs_trans_handle *trans;
229 struct btrfs_key key;
230 struct btrfs_root_item root_item;
231 struct btrfs_inode_item *inode_item;
232 struct extent_buffer *leaf;
233 struct btrfs_root *new_root = root;
238 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
240 unsigned long nr = 1;
242 ret = btrfs_check_metadata_free_space(root);
246 trans = btrfs_start_transaction(root, 1);
249 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
254 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
255 0, objectid, NULL, 0, 0, 0);
261 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
262 btrfs_set_header_bytenr(leaf, leaf->start);
263 btrfs_set_header_generation(leaf, trans->transid);
264 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
265 btrfs_set_header_owner(leaf, objectid);
267 write_extent_buffer(leaf, root->fs_info->fsid,
268 (unsigned long)btrfs_header_fsid(leaf),
270 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
271 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
273 btrfs_mark_buffer_dirty(leaf);
275 inode_item = &root_item.inode;
276 memset(inode_item, 0, sizeof(*inode_item));
277 inode_item->generation = cpu_to_le64(1);
278 inode_item->size = cpu_to_le64(3);
279 inode_item->nlink = cpu_to_le32(1);
280 inode_item->nbytes = cpu_to_le64(root->leafsize);
281 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
283 btrfs_set_root_bytenr(&root_item, leaf->start);
284 btrfs_set_root_generation(&root_item, trans->transid);
285 btrfs_set_root_level(&root_item, 0);
286 btrfs_set_root_refs(&root_item, 1);
287 btrfs_set_root_used(&root_item, 0);
288 btrfs_set_root_last_snapshot(&root_item, 0);
290 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
291 root_item.drop_level = 0;
293 btrfs_tree_unlock(leaf);
294 free_extent_buffer(leaf);
297 btrfs_set_root_dirid(&root_item, new_dirid);
299 key.objectid = objectid;
301 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
302 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
308 * insert the directory item
310 key.offset = (u64)-1;
311 dir = dentry->d_parent->d_inode;
312 ret = btrfs_set_inode_index(dir, &index);
315 ret = btrfs_insert_dir_item(trans, root,
316 name, namelen, dir->i_ino, &key,
317 BTRFS_FT_DIR, index);
321 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
322 ret = btrfs_update_inode(trans, root, dir);
325 /* add the backref first */
326 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
327 objectid, BTRFS_ROOT_BACKREF_KEY,
328 root->root_key.objectid,
329 dir->i_ino, index, name, namelen);
333 /* now add the forward ref */
334 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
335 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
337 dir->i_ino, index, name, namelen);
341 ret = btrfs_commit_transaction(trans, root);
345 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
348 trans = btrfs_start_transaction(new_root, 1);
351 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
352 BTRFS_I(dir)->block_group);
357 nr = trans->blocks_used;
358 err = btrfs_commit_transaction(trans, new_root);
362 btrfs_btree_balance_dirty(root, nr);
366 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
367 char *name, int namelen)
369 struct btrfs_pending_snapshot *pending_snapshot;
370 struct btrfs_trans_handle *trans;
373 unsigned long nr = 0;
378 ret = btrfs_check_metadata_free_space(root);
382 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
383 if (!pending_snapshot) {
387 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
388 if (!pending_snapshot->name) {
390 kfree(pending_snapshot);
393 memcpy(pending_snapshot->name, name, namelen);
394 pending_snapshot->name[namelen] = '\0';
395 pending_snapshot->dentry = dentry;
396 trans = btrfs_start_transaction(root, 1);
398 pending_snapshot->root = root;
399 list_add(&pending_snapshot->list,
400 &trans->transaction->pending_snapshots);
401 err = btrfs_commit_transaction(trans, root);
404 btrfs_btree_balance_dirty(root, nr);
408 /* copy of may_create in fs/namei.c() */
409 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
415 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
419 * Create a new subvolume below @parent. This is largely modeled after
420 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
421 * inside this filesystem so it's quite a bit simpler.
423 static noinline int btrfs_mksubvol(struct path *parent, char *name,
424 int mode, int namelen,
425 struct btrfs_root *snap_src)
427 struct dentry *dentry;
430 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
432 dentry = lookup_one_len(name, parent->dentry, namelen);
433 error = PTR_ERR(dentry);
441 if (!IS_POSIXACL(parent->dentry->d_inode))
442 mode &= ~current_umask();
444 error = mnt_want_write(parent->mnt);
448 error = btrfs_may_create(parent->dentry->d_inode, dentry);
453 * Actually perform the low-level subvolume creation after all
456 * Eventually we want to pass in an inode under which we create this
457 * subvolume, but for now all are under the filesystem root.
459 * Also we should pass on the mode eventually to allow creating new
460 * subvolume with specific mode bits.
463 struct dentry *dir = dentry->d_parent;
464 struct dentry *test = dir->d_parent;
465 struct btrfs_path *path = btrfs_alloc_path();
468 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
470 test_oid = snap_src->root_key.objectid;
472 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
473 path, parent_oid, test_oid);
476 btrfs_release_path(snap_src->fs_info->tree_root, path);
478 /* we need to make sure we aren't creating a directory loop
479 * by taking a snapshot of something that has our current
480 * subvol in its directory tree. So, this loops through
481 * the dentries and checks the forward refs for each subvolume
482 * to see if is references the subvolume where we are
483 * placing this new snapshot.
487 dir == snap_src->fs_info->sb->s_root ||
488 test == snap_src->fs_info->sb->s_root ||
489 test->d_inode->i_sb != snap_src->fs_info->sb) {
492 if (S_ISLNK(test->d_inode->i_mode)) {
493 printk(KERN_INFO "Btrfs symlink in snapshot "
496 btrfs_free_path(path);
500 BTRFS_I(test->d_inode)->root->root_key.objectid;
501 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
502 path, test_oid, parent_oid);
504 printk(KERN_INFO "Btrfs snapshot creation "
505 "failed, looping\n");
507 btrfs_free_path(path);
510 btrfs_release_path(snap_src->fs_info->tree_root, path);
511 test = test->d_parent;
514 btrfs_free_path(path);
515 error = create_snapshot(snap_src, dentry, name, namelen);
517 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
518 dentry, name, namelen);
523 fsnotify_mkdir(parent->dentry->d_inode, dentry);
525 mnt_drop_write(parent->mnt);
529 mutex_unlock(&parent->dentry->d_inode->i_mutex);
534 static int btrfs_defrag_file(struct file *file)
536 struct inode *inode = fdentry(file)->d_inode;
537 struct btrfs_root *root = BTRFS_I(inode)->root;
538 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
539 struct btrfs_ordered_extent *ordered;
541 unsigned long last_index;
542 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
543 unsigned long total_read = 0;
549 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
553 mutex_lock(&inode->i_mutex);
554 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
555 for (i = 0; i <= last_index; i++) {
556 if (total_read % ra_pages == 0) {
557 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
558 min(last_index, i + ra_pages - 1));
562 page = grab_cache_page(inode->i_mapping, i);
565 if (!PageUptodate(page)) {
566 btrfs_readpage(NULL, page);
568 if (!PageUptodate(page)) {
570 page_cache_release(page);
575 wait_on_page_writeback(page);
577 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
578 page_end = page_start + PAGE_CACHE_SIZE - 1;
579 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
581 ordered = btrfs_lookup_ordered_extent(inode, page_start);
583 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
585 page_cache_release(page);
586 btrfs_start_ordered_extent(inode, ordered, 1);
587 btrfs_put_ordered_extent(ordered);
590 set_page_extent_mapped(page);
593 * this makes sure page_mkwrite is called on the
594 * page if it is dirtied again later
596 clear_page_dirty_for_io(page);
598 btrfs_set_extent_delalloc(inode, page_start, page_end);
600 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
601 set_page_dirty(page);
603 page_cache_release(page);
604 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
608 mutex_unlock(&inode->i_mutex);
612 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
617 struct btrfs_ioctl_vol_args *vol_args;
618 struct btrfs_trans_handle *trans;
619 struct btrfs_device *device = NULL;
626 if (root->fs_info->sb->s_flags & MS_RDONLY)
629 if (!capable(CAP_SYS_ADMIN))
632 vol_args = memdup_user(arg, sizeof(*vol_args));
633 if (IS_ERR(vol_args))
634 return PTR_ERR(vol_args);
636 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
637 namelen = strlen(vol_args->name);
639 mutex_lock(&root->fs_info->volume_mutex);
640 sizestr = vol_args->name;
641 devstr = strchr(sizestr, ':');
644 sizestr = devstr + 1;
646 devstr = vol_args->name;
647 devid = simple_strtoull(devstr, &end, 10);
648 printk(KERN_INFO "resizing devid %llu\n",
649 (unsigned long long)devid);
651 device = btrfs_find_device(root, devid, NULL, NULL);
653 printk(KERN_INFO "resizer unable to find device %llu\n",
654 (unsigned long long)devid);
658 if (!strcmp(sizestr, "max"))
659 new_size = device->bdev->bd_inode->i_size;
661 if (sizestr[0] == '-') {
664 } else if (sizestr[0] == '+') {
668 new_size = btrfs_parse_size(sizestr);
675 old_size = device->total_bytes;
678 if (new_size > old_size) {
682 new_size = old_size - new_size;
683 } else if (mod > 0) {
684 new_size = old_size + new_size;
687 if (new_size < 256 * 1024 * 1024) {
691 if (new_size > device->bdev->bd_inode->i_size) {
696 do_div(new_size, root->sectorsize);
697 new_size *= root->sectorsize;
699 printk(KERN_INFO "new size for %s is %llu\n",
700 device->name, (unsigned long long)new_size);
702 if (new_size > old_size) {
703 trans = btrfs_start_transaction(root, 1);
704 ret = btrfs_grow_device(trans, device, new_size);
705 btrfs_commit_transaction(trans, root);
707 ret = btrfs_shrink_device(device, new_size);
711 mutex_unlock(&root->fs_info->volume_mutex);
716 static noinline int btrfs_ioctl_snap_create(struct file *file,
717 void __user *arg, int subvol)
719 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
720 struct btrfs_ioctl_vol_args *vol_args;
721 struct btrfs_dir_item *di;
722 struct btrfs_path *path;
723 struct file *src_file;
728 if (root->fs_info->sb->s_flags & MS_RDONLY)
731 vol_args = memdup_user(arg, sizeof(*vol_args));
732 if (IS_ERR(vol_args))
733 return PTR_ERR(vol_args);
735 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
736 namelen = strlen(vol_args->name);
737 if (strchr(vol_args->name, '/')) {
742 path = btrfs_alloc_path();
748 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
749 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
751 vol_args->name, namelen, 0);
752 btrfs_free_path(path);
754 if (di && !IS_ERR(di)) {
765 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
766 file->f_path.dentry->d_inode->i_mode,
769 struct inode *src_inode;
770 src_file = fget(vol_args->fd);
776 src_inode = src_file->f_path.dentry->d_inode;
777 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
778 printk(KERN_INFO "btrfs: Snapshot src from "
784 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
785 file->f_path.dentry->d_inode->i_mode,
786 namelen, BTRFS_I(src_inode)->root);
795 static int btrfs_ioctl_defrag(struct file *file)
797 struct inode *inode = fdentry(file)->d_inode;
798 struct btrfs_root *root = BTRFS_I(inode)->root;
801 ret = mnt_want_write(file->f_path.mnt);
805 switch (inode->i_mode & S_IFMT) {
807 if (!capable(CAP_SYS_ADMIN)) {
811 btrfs_defrag_root(root, 0);
812 btrfs_defrag_root(root->fs_info->extent_root, 0);
815 if (!(file->f_mode & FMODE_WRITE)) {
819 btrfs_defrag_file(file);
823 mnt_drop_write(file->f_path.mnt);
827 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
829 struct btrfs_ioctl_vol_args *vol_args;
832 if (!capable(CAP_SYS_ADMIN))
835 vol_args = memdup_user(arg, sizeof(*vol_args));
836 if (IS_ERR(vol_args))
837 return PTR_ERR(vol_args);
839 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
840 ret = btrfs_init_new_device(root, vol_args->name);
846 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
848 struct btrfs_ioctl_vol_args *vol_args;
851 if (!capable(CAP_SYS_ADMIN))
854 if (root->fs_info->sb->s_flags & MS_RDONLY)
857 vol_args = memdup_user(arg, sizeof(*vol_args));
858 if (IS_ERR(vol_args))
859 return PTR_ERR(vol_args);
861 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
862 ret = btrfs_rm_device(root, vol_args->name);
868 static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
869 u64 off, u64 olen, u64 destoff)
871 struct inode *inode = fdentry(file)->d_inode;
872 struct btrfs_root *root = BTRFS_I(inode)->root;
873 struct file *src_file;
875 struct btrfs_trans_handle *trans;
876 struct btrfs_path *path;
877 struct extent_buffer *leaf;
879 struct btrfs_key key;
884 u64 bs = root->fs_info->sb->s_blocksize;
889 * - split compressed inline extents. annoying: we need to
890 * decompress into destination's address_space (the file offset
891 * may change, so source mapping won't do), then recompress (or
892 * otherwise reinsert) a subrange.
893 * - allow ranges within the same file to be cloned (provided
894 * they don't overlap)?
897 /* the destination must be opened for writing */
898 if (!(file->f_mode & FMODE_WRITE))
901 ret = mnt_want_write(file->f_path.mnt);
905 src_file = fget(srcfd);
910 src = src_file->f_dentry->d_inode;
917 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
921 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
925 buf = vmalloc(btrfs_level_size(root, 0));
929 path = btrfs_alloc_path();
937 mutex_lock(&inode->i_mutex);
938 mutex_lock(&src->i_mutex);
940 mutex_lock(&src->i_mutex);
941 mutex_lock(&inode->i_mutex);
944 /* determine range to clone */
946 if (off >= src->i_size || off + len > src->i_size)
949 olen = len = src->i_size - off;
950 /* if we extend to eof, continue to block boundary */
951 if (off + len == src->i_size)
952 len = ((src->i_size + bs-1) & ~(bs-1))
955 /* verify the end result is block aligned */
956 if ((off & (bs-1)) ||
957 ((off + len) & (bs-1)))
960 /* do any pending delalloc/csum calc on src, one way or
961 another, and lock file content */
963 struct btrfs_ordered_extent *ordered;
964 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
965 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
966 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
968 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
970 btrfs_put_ordered_extent(ordered);
971 btrfs_wait_ordered_range(src, off, off+len);
974 trans = btrfs_start_transaction(root, 1);
977 /* punch hole in destination first */
978 btrfs_drop_extents(trans, root, inode, off, off + len,
979 off + len, 0, &hint_byte);
982 key.objectid = src->i_ino;
983 key.type = BTRFS_EXTENT_DATA_KEY;
988 * note the key will change type as we walk through the
991 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
995 nritems = btrfs_header_nritems(path->nodes[0]);
996 if (path->slots[0] >= nritems) {
997 ret = btrfs_next_leaf(root, path);
1002 nritems = btrfs_header_nritems(path->nodes[0]);
1004 leaf = path->nodes[0];
1005 slot = path->slots[0];
1007 btrfs_item_key_to_cpu(leaf, &key, slot);
1008 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1009 key.objectid != src->i_ino)
1012 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1013 struct btrfs_file_extent_item *extent;
1016 struct btrfs_key new_key;
1017 u64 disko = 0, diskl = 0;
1018 u64 datao = 0, datal = 0;
1021 size = btrfs_item_size_nr(leaf, slot);
1022 read_extent_buffer(leaf, buf,
1023 btrfs_item_ptr_offset(leaf, slot),
1026 extent = btrfs_item_ptr(leaf, slot,
1027 struct btrfs_file_extent_item);
1028 comp = btrfs_file_extent_compression(leaf, extent);
1029 type = btrfs_file_extent_type(leaf, extent);
1030 if (type == BTRFS_FILE_EXTENT_REG ||
1031 type == BTRFS_FILE_EXTENT_PREALLOC) {
1032 disko = btrfs_file_extent_disk_bytenr(leaf,
1034 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1036 datao = btrfs_file_extent_offset(leaf, extent);
1037 datal = btrfs_file_extent_num_bytes(leaf,
1039 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1040 /* take upper bound, may be compressed */
1041 datal = btrfs_file_extent_ram_bytes(leaf,
1044 btrfs_release_path(root, path);
1046 if (key.offset + datal < off ||
1047 key.offset >= off+len)
1050 memcpy(&new_key, &key, sizeof(new_key));
1051 new_key.objectid = inode->i_ino;
1052 new_key.offset = key.offset + destoff - off;
1054 if (type == BTRFS_FILE_EXTENT_REG ||
1055 type == BTRFS_FILE_EXTENT_PREALLOC) {
1056 ret = btrfs_insert_empty_item(trans, root, path,
1061 leaf = path->nodes[0];
1062 slot = path->slots[0];
1063 write_extent_buffer(leaf, buf,
1064 btrfs_item_ptr_offset(leaf, slot),
1067 extent = btrfs_item_ptr(leaf, slot,
1068 struct btrfs_file_extent_item);
1070 if (off > key.offset) {
1071 datao += off - key.offset;
1072 datal -= off - key.offset;
1074 if (key.offset + datao + datal + key.offset >
1076 datal = off + len - key.offset - datao;
1077 /* disko == 0 means it's a hole */
1081 btrfs_set_file_extent_offset(leaf, extent,
1083 btrfs_set_file_extent_num_bytes(leaf, extent,
1086 inode_add_bytes(inode, datal);
1087 ret = btrfs_inc_extent_ref(trans, root,
1089 root->root_key.objectid,
1091 new_key.offset - datao);
1094 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1097 if (off > key.offset) {
1098 skip = off - key.offset;
1099 new_key.offset += skip;
1102 if (key.offset + datal > off+len)
1103 trim = key.offset + datal - (off+len);
1105 if (comp && (skip || trim)) {
1109 size -= skip + trim;
1110 datal -= skip + trim;
1111 ret = btrfs_insert_empty_item(trans, root, path,
1118 btrfs_file_extent_calc_inline_size(0);
1119 memmove(buf+start, buf+start+skip,
1123 leaf = path->nodes[0];
1124 slot = path->slots[0];
1125 write_extent_buffer(leaf, buf,
1126 btrfs_item_ptr_offset(leaf, slot),
1128 inode_add_bytes(inode, datal);
1131 btrfs_mark_buffer_dirty(leaf);
1135 btrfs_release_path(root, path);
1140 btrfs_release_path(root, path);
1142 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1143 if (destoff + olen > inode->i_size)
1144 btrfs_i_size_write(inode, destoff + olen);
1145 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1146 ret = btrfs_update_inode(trans, root, inode);
1148 btrfs_end_transaction(trans, root);
1149 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1151 vmtruncate(inode, 0);
1153 mutex_unlock(&src->i_mutex);
1154 mutex_unlock(&inode->i_mutex);
1156 btrfs_free_path(path);
1160 mnt_drop_write(file->f_path.mnt);
1164 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1166 struct btrfs_ioctl_clone_range_args args;
1168 if (copy_from_user(&args, argp, sizeof(args)))
1170 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1171 args.src_length, args.dest_offset);
1175 * there are many ways the trans_start and trans_end ioctls can lead
1176 * to deadlocks. They should only be used by applications that
1177 * basically own the machine, and have a very in depth understanding
1178 * of all the possible deadlocks and enospc problems.
1180 static long btrfs_ioctl_trans_start(struct file *file)
1182 struct inode *inode = fdentry(file)->d_inode;
1183 struct btrfs_root *root = BTRFS_I(inode)->root;
1184 struct btrfs_trans_handle *trans;
1187 if (!capable(CAP_SYS_ADMIN))
1190 if (file->private_data) {
1195 ret = mnt_want_write(file->f_path.mnt);
1199 mutex_lock(&root->fs_info->trans_mutex);
1200 root->fs_info->open_ioctl_trans++;
1201 mutex_unlock(&root->fs_info->trans_mutex);
1203 trans = btrfs_start_ioctl_transaction(root, 0);
1205 file->private_data = trans;
1208 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1214 * there are many ways the trans_start and trans_end ioctls can lead
1215 * to deadlocks. They should only be used by applications that
1216 * basically own the machine, and have a very in depth understanding
1217 * of all the possible deadlocks and enospc problems.
1219 long btrfs_ioctl_trans_end(struct file *file)
1221 struct inode *inode = fdentry(file)->d_inode;
1222 struct btrfs_root *root = BTRFS_I(inode)->root;
1223 struct btrfs_trans_handle *trans;
1226 trans = file->private_data;
1231 btrfs_end_transaction(trans, root);
1232 file->private_data = NULL;
1234 mutex_lock(&root->fs_info->trans_mutex);
1235 root->fs_info->open_ioctl_trans--;
1236 mutex_unlock(&root->fs_info->trans_mutex);
1238 mnt_drop_write(file->f_path.mnt);
1244 long btrfs_ioctl(struct file *file, unsigned int
1245 cmd, unsigned long arg)
1247 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1248 void __user *argp = (void __user *)arg;
1251 case FS_IOC_GETFLAGS:
1252 return btrfs_ioctl_getflags(file, argp);
1253 case FS_IOC_SETFLAGS:
1254 return btrfs_ioctl_setflags(file, argp);
1255 case FS_IOC_GETVERSION:
1256 return btrfs_ioctl_getversion(file, argp);
1257 case BTRFS_IOC_SNAP_CREATE:
1258 return btrfs_ioctl_snap_create(file, argp, 0);
1259 case BTRFS_IOC_SUBVOL_CREATE:
1260 return btrfs_ioctl_snap_create(file, argp, 1);
1261 case BTRFS_IOC_DEFRAG:
1262 return btrfs_ioctl_defrag(file);
1263 case BTRFS_IOC_RESIZE:
1264 return btrfs_ioctl_resize(root, argp);
1265 case BTRFS_IOC_ADD_DEV:
1266 return btrfs_ioctl_add_dev(root, argp);
1267 case BTRFS_IOC_RM_DEV:
1268 return btrfs_ioctl_rm_dev(root, argp);
1269 case BTRFS_IOC_BALANCE:
1270 return btrfs_balance(root->fs_info->dev_root);
1271 case BTRFS_IOC_CLONE:
1272 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1273 case BTRFS_IOC_CLONE_RANGE:
1274 return btrfs_ioctl_clone_range(file, argp);
1275 case BTRFS_IOC_TRANS_START:
1276 return btrfs_ioctl_trans_start(file);
1277 case BTRFS_IOC_TRANS_END:
1278 return btrfs_ioctl_trans_end(file);
1279 case BTRFS_IOC_SYNC:
1280 btrfs_sync_fs(file->f_dentry->d_sb, 1);