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/xattr.h>
42 #include <linux/vmalloc.h>
46 #include "transaction.h"
47 #include "btrfs_inode.h"
49 #include "print-tree.h"
53 /* Mask out flags that are inappropriate for the given type of inode. */
54 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
61 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
69 unsigned int iflags = 0;
71 if (flags & BTRFS_INODE_SYNC)
73 if (flags & BTRFS_INODE_IMMUTABLE)
74 iflags |= FS_IMMUTABLE_FL;
75 if (flags & BTRFS_INODE_APPEND)
76 iflags |= FS_APPEND_FL;
77 if (flags & BTRFS_INODE_NODUMP)
78 iflags |= FS_NODUMP_FL;
79 if (flags & BTRFS_INODE_NOATIME)
80 iflags |= FS_NOATIME_FL;
81 if (flags & BTRFS_INODE_DIRSYNC)
82 iflags |= FS_DIRSYNC_FL;
88 * Update inode->i_flags based on the btrfs internal flags.
90 void btrfs_update_iflags(struct inode *inode)
92 struct btrfs_inode *ip = BTRFS_I(inode);
94 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
96 if (ip->flags & BTRFS_INODE_SYNC)
97 inode->i_flags |= S_SYNC;
98 if (ip->flags & BTRFS_INODE_IMMUTABLE)
99 inode->i_flags |= S_IMMUTABLE;
100 if (ip->flags & BTRFS_INODE_APPEND)
101 inode->i_flags |= S_APPEND;
102 if (ip->flags & BTRFS_INODE_NOATIME)
103 inode->i_flags |= S_NOATIME;
104 if (ip->flags & BTRFS_INODE_DIRSYNC)
105 inode->i_flags |= S_DIRSYNC;
109 * Inherit flags from the parent inode.
111 * Unlike extN we don't have any flags we don't want to inherit currently.
113 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
115 unsigned int flags = BTRFS_I(dir)->flags;
117 if (S_ISREG(inode->i_mode))
118 flags &= ~BTRFS_INODE_DIRSYNC;
119 else if (!S_ISDIR(inode->i_mode))
120 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
122 BTRFS_I(inode)->flags = flags;
123 btrfs_update_iflags(inode);
126 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
128 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
129 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
131 if (copy_to_user(arg, &flags, sizeof(flags)))
136 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
138 struct inode *inode = file->f_path.dentry->d_inode;
139 struct btrfs_inode *ip = BTRFS_I(inode);
140 struct btrfs_root *root = ip->root;
141 struct btrfs_trans_handle *trans;
142 unsigned int flags, oldflags;
145 if (copy_from_user(&flags, arg, sizeof(flags)))
148 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
149 FS_NOATIME_FL | FS_NODUMP_FL | \
150 FS_SYNC_FL | FS_DIRSYNC_FL))
153 if (!is_owner_or_cap(inode))
156 mutex_lock(&inode->i_mutex);
158 flags = btrfs_mask_flags(inode->i_mode, flags);
159 oldflags = btrfs_flags_to_ioctl(ip->flags);
160 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
161 if (!capable(CAP_LINUX_IMMUTABLE)) {
167 ret = mnt_want_write(file->f_path.mnt);
171 if (flags & FS_SYNC_FL)
172 ip->flags |= BTRFS_INODE_SYNC;
174 ip->flags &= ~BTRFS_INODE_SYNC;
175 if (flags & FS_IMMUTABLE_FL)
176 ip->flags |= BTRFS_INODE_IMMUTABLE;
178 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
179 if (flags & FS_APPEND_FL)
180 ip->flags |= BTRFS_INODE_APPEND;
182 ip->flags &= ~BTRFS_INODE_APPEND;
183 if (flags & FS_NODUMP_FL)
184 ip->flags |= BTRFS_INODE_NODUMP;
186 ip->flags &= ~BTRFS_INODE_NODUMP;
187 if (flags & FS_NOATIME_FL)
188 ip->flags |= BTRFS_INODE_NOATIME;
190 ip->flags &= ~BTRFS_INODE_NOATIME;
191 if (flags & FS_DIRSYNC_FL)
192 ip->flags |= BTRFS_INODE_DIRSYNC;
194 ip->flags &= ~BTRFS_INODE_DIRSYNC;
197 trans = btrfs_join_transaction(root, 1);
200 ret = btrfs_update_inode(trans, root, inode);
203 btrfs_update_iflags(inode);
204 inode->i_ctime = CURRENT_TIME;
205 btrfs_end_transaction(trans, root);
207 mnt_drop_write(file->f_path.mnt);
209 mutex_unlock(&inode->i_mutex);
213 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
215 struct inode *inode = file->f_path.dentry->d_inode;
217 return put_user(inode->i_generation, arg);
220 static noinline int create_subvol(struct btrfs_root *root,
221 struct dentry *dentry,
222 char *name, int namelen)
224 struct btrfs_trans_handle *trans;
225 struct btrfs_key key;
226 struct btrfs_root_item root_item;
227 struct btrfs_inode_item *inode_item;
228 struct extent_buffer *leaf;
229 struct btrfs_root *new_root = root;
234 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
236 unsigned long nr = 1;
238 ret = btrfs_check_metadata_free_space(root);
242 trans = btrfs_start_transaction(root, 1);
245 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
250 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
251 0, objectid, NULL, 0, 0, 0);
257 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
258 btrfs_set_header_bytenr(leaf, leaf->start);
259 btrfs_set_header_generation(leaf, trans->transid);
260 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
261 btrfs_set_header_owner(leaf, objectid);
263 write_extent_buffer(leaf, root->fs_info->fsid,
264 (unsigned long)btrfs_header_fsid(leaf),
266 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
267 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
269 btrfs_mark_buffer_dirty(leaf);
271 inode_item = &root_item.inode;
272 memset(inode_item, 0, sizeof(*inode_item));
273 inode_item->generation = cpu_to_le64(1);
274 inode_item->size = cpu_to_le64(3);
275 inode_item->nlink = cpu_to_le32(1);
276 inode_item->nbytes = cpu_to_le64(root->leafsize);
277 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
279 btrfs_set_root_bytenr(&root_item, leaf->start);
280 btrfs_set_root_generation(&root_item, trans->transid);
281 btrfs_set_root_level(&root_item, 0);
282 btrfs_set_root_refs(&root_item, 1);
283 btrfs_set_root_used(&root_item, 0);
284 btrfs_set_root_last_snapshot(&root_item, 0);
286 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
287 root_item.drop_level = 0;
289 btrfs_tree_unlock(leaf);
290 free_extent_buffer(leaf);
293 btrfs_set_root_dirid(&root_item, new_dirid);
295 key.objectid = objectid;
297 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
298 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
304 * insert the directory item
306 key.offset = (u64)-1;
307 dir = dentry->d_parent->d_inode;
308 ret = btrfs_set_inode_index(dir, &index);
311 ret = btrfs_insert_dir_item(trans, root,
312 name, namelen, dir->i_ino, &key,
313 BTRFS_FT_DIR, index);
317 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
318 ret = btrfs_update_inode(trans, root, dir);
321 /* add the backref first */
322 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
323 objectid, BTRFS_ROOT_BACKREF_KEY,
324 root->root_key.objectid,
325 dir->i_ino, index, name, namelen);
329 /* now add the forward ref */
330 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
331 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
333 dir->i_ino, index, name, namelen);
337 ret = btrfs_commit_transaction(trans, root);
341 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
344 trans = btrfs_start_transaction(new_root, 1);
347 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
348 BTRFS_I(dir)->block_group);
353 nr = trans->blocks_used;
354 err = btrfs_commit_transaction(trans, new_root);
358 btrfs_btree_balance_dirty(root, nr);
362 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
363 char *name, int namelen)
365 struct btrfs_pending_snapshot *pending_snapshot;
366 struct btrfs_trans_handle *trans;
369 unsigned long nr = 0;
374 ret = btrfs_check_metadata_free_space(root);
378 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
379 if (!pending_snapshot) {
383 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
384 if (!pending_snapshot->name) {
386 kfree(pending_snapshot);
389 memcpy(pending_snapshot->name, name, namelen);
390 pending_snapshot->name[namelen] = '\0';
391 pending_snapshot->dentry = dentry;
392 trans = btrfs_start_transaction(root, 1);
394 pending_snapshot->root = root;
395 list_add(&pending_snapshot->list,
396 &trans->transaction->pending_snapshots);
397 err = btrfs_commit_transaction(trans, root);
400 btrfs_btree_balance_dirty(root, nr);
404 /* copy of may_create in fs/namei.c() */
405 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
411 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
415 * Create a new subvolume below @parent. This is largely modeled after
416 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
417 * inside this filesystem so it's quite a bit simpler.
419 static noinline int btrfs_mksubvol(struct path *parent, char *name,
420 int mode, int namelen,
421 struct btrfs_root *snap_src)
423 struct dentry *dentry;
426 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
428 dentry = lookup_one_len(name, parent->dentry, namelen);
429 error = PTR_ERR(dentry);
437 if (!IS_POSIXACL(parent->dentry->d_inode))
438 mode &= ~current_umask();
440 error = mnt_want_write(parent->mnt);
444 error = btrfs_may_create(parent->dentry->d_inode, dentry);
449 * Actually perform the low-level subvolume creation after all
452 * Eventually we want to pass in an inode under which we create this
453 * subvolume, but for now all are under the filesystem root.
455 * Also we should pass on the mode eventually to allow creating new
456 * subvolume with specific mode bits.
459 struct dentry *dir = dentry->d_parent;
460 struct dentry *test = dir->d_parent;
461 struct btrfs_path *path = btrfs_alloc_path();
464 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
466 test_oid = snap_src->root_key.objectid;
468 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
469 path, parent_oid, test_oid);
472 btrfs_release_path(snap_src->fs_info->tree_root, path);
474 /* we need to make sure we aren't creating a directory loop
475 * by taking a snapshot of something that has our current
476 * subvol in its directory tree. So, this loops through
477 * the dentries and checks the forward refs for each subvolume
478 * to see if is references the subvolume where we are
479 * placing this new snapshot.
483 dir == snap_src->fs_info->sb->s_root ||
484 test == snap_src->fs_info->sb->s_root ||
485 test->d_inode->i_sb != snap_src->fs_info->sb) {
488 if (S_ISLNK(test->d_inode->i_mode)) {
489 printk(KERN_INFO "Btrfs symlink in snapshot "
492 btrfs_free_path(path);
496 BTRFS_I(test->d_inode)->root->root_key.objectid;
497 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
498 path, test_oid, parent_oid);
500 printk(KERN_INFO "Btrfs snapshot creation "
501 "failed, looping\n");
503 btrfs_free_path(path);
506 btrfs_release_path(snap_src->fs_info->tree_root, path);
507 test = test->d_parent;
510 btrfs_free_path(path);
511 error = create_snapshot(snap_src, dentry, name, namelen);
513 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
514 dentry, name, namelen);
519 fsnotify_mkdir(parent->dentry->d_inode, dentry);
521 mnt_drop_write(parent->mnt);
525 mutex_unlock(&parent->dentry->d_inode->i_mutex);
530 static int btrfs_defrag_file(struct file *file)
532 struct inode *inode = fdentry(file)->d_inode;
533 struct btrfs_root *root = BTRFS_I(inode)->root;
534 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
535 struct btrfs_ordered_extent *ordered;
537 unsigned long last_index;
538 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
539 unsigned long total_read = 0;
545 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
549 mutex_lock(&inode->i_mutex);
550 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
551 for (i = 0; i <= last_index; i++) {
552 if (total_read % ra_pages == 0) {
553 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
554 min(last_index, i + ra_pages - 1));
558 page = grab_cache_page(inode->i_mapping, i);
561 if (!PageUptodate(page)) {
562 btrfs_readpage(NULL, page);
564 if (!PageUptodate(page)) {
566 page_cache_release(page);
571 wait_on_page_writeback(page);
573 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
574 page_end = page_start + PAGE_CACHE_SIZE - 1;
575 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
577 ordered = btrfs_lookup_ordered_extent(inode, page_start);
579 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
581 page_cache_release(page);
582 btrfs_start_ordered_extent(inode, ordered, 1);
583 btrfs_put_ordered_extent(ordered);
586 set_page_extent_mapped(page);
589 * this makes sure page_mkwrite is called on the
590 * page if it is dirtied again later
592 clear_page_dirty_for_io(page);
594 btrfs_set_extent_delalloc(inode, page_start, page_end);
596 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
597 set_page_dirty(page);
599 page_cache_release(page);
600 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
604 mutex_unlock(&inode->i_mutex);
608 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
613 struct btrfs_ioctl_vol_args *vol_args;
614 struct btrfs_trans_handle *trans;
615 struct btrfs_device *device = NULL;
622 if (root->fs_info->sb->s_flags & MS_RDONLY)
625 if (!capable(CAP_SYS_ADMIN))
628 vol_args = memdup_user(arg, sizeof(*vol_args));
629 if (IS_ERR(vol_args))
630 return PTR_ERR(vol_args);
632 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
633 namelen = strlen(vol_args->name);
635 mutex_lock(&root->fs_info->volume_mutex);
636 sizestr = vol_args->name;
637 devstr = strchr(sizestr, ':');
640 sizestr = devstr + 1;
642 devstr = vol_args->name;
643 devid = simple_strtoull(devstr, &end, 10);
644 printk(KERN_INFO "resizing devid %llu\n",
645 (unsigned long long)devid);
647 device = btrfs_find_device(root, devid, NULL, NULL);
649 printk(KERN_INFO "resizer unable to find device %llu\n",
650 (unsigned long long)devid);
654 if (!strcmp(sizestr, "max"))
655 new_size = device->bdev->bd_inode->i_size;
657 if (sizestr[0] == '-') {
660 } else if (sizestr[0] == '+') {
664 new_size = btrfs_parse_size(sizestr);
671 old_size = device->total_bytes;
674 if (new_size > old_size) {
678 new_size = old_size - new_size;
679 } else if (mod > 0) {
680 new_size = old_size + new_size;
683 if (new_size < 256 * 1024 * 1024) {
687 if (new_size > device->bdev->bd_inode->i_size) {
692 do_div(new_size, root->sectorsize);
693 new_size *= root->sectorsize;
695 printk(KERN_INFO "new size for %s is %llu\n",
696 device->name, (unsigned long long)new_size);
698 if (new_size > old_size) {
699 trans = btrfs_start_transaction(root, 1);
700 ret = btrfs_grow_device(trans, device, new_size);
701 btrfs_commit_transaction(trans, root);
703 ret = btrfs_shrink_device(device, new_size);
707 mutex_unlock(&root->fs_info->volume_mutex);
712 static noinline int btrfs_ioctl_snap_create(struct file *file,
713 void __user *arg, int subvol)
715 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
716 struct btrfs_ioctl_vol_args *vol_args;
717 struct btrfs_dir_item *di;
718 struct btrfs_path *path;
719 struct file *src_file;
724 if (root->fs_info->sb->s_flags & MS_RDONLY)
727 vol_args = memdup_user(arg, sizeof(*vol_args));
728 if (IS_ERR(vol_args))
729 return PTR_ERR(vol_args);
731 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
732 namelen = strlen(vol_args->name);
733 if (strchr(vol_args->name, '/')) {
738 path = btrfs_alloc_path();
744 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
745 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
747 vol_args->name, namelen, 0);
748 btrfs_free_path(path);
750 if (di && !IS_ERR(di)) {
761 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
762 file->f_path.dentry->d_inode->i_mode,
765 struct inode *src_inode;
766 src_file = fget(vol_args->fd);
772 src_inode = src_file->f_path.dentry->d_inode;
773 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
774 printk(KERN_INFO "btrfs: Snapshot src from "
780 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
781 file->f_path.dentry->d_inode->i_mode,
782 namelen, BTRFS_I(src_inode)->root);
791 static int btrfs_ioctl_defrag(struct file *file)
793 struct inode *inode = fdentry(file)->d_inode;
794 struct btrfs_root *root = BTRFS_I(inode)->root;
797 ret = mnt_want_write(file->f_path.mnt);
801 switch (inode->i_mode & S_IFMT) {
803 if (!capable(CAP_SYS_ADMIN)) {
807 btrfs_defrag_root(root, 0);
808 btrfs_defrag_root(root->fs_info->extent_root, 0);
811 if (!(file->f_mode & FMODE_WRITE)) {
815 btrfs_defrag_file(file);
819 mnt_drop_write(file->f_path.mnt);
823 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
825 struct btrfs_ioctl_vol_args *vol_args;
828 if (!capable(CAP_SYS_ADMIN))
831 vol_args = memdup_user(arg, sizeof(*vol_args));
832 if (IS_ERR(vol_args))
833 return PTR_ERR(vol_args);
835 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
836 ret = btrfs_init_new_device(root, vol_args->name);
842 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
844 struct btrfs_ioctl_vol_args *vol_args;
847 if (!capable(CAP_SYS_ADMIN))
850 if (root->fs_info->sb->s_flags & MS_RDONLY)
853 vol_args = memdup_user(arg, sizeof(*vol_args));
854 if (IS_ERR(vol_args))
855 return PTR_ERR(vol_args);
857 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
858 ret = btrfs_rm_device(root, vol_args->name);
864 static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
865 u64 off, u64 olen, u64 destoff)
867 struct inode *inode = fdentry(file)->d_inode;
868 struct btrfs_root *root = BTRFS_I(inode)->root;
869 struct file *src_file;
871 struct btrfs_trans_handle *trans;
872 struct btrfs_path *path;
873 struct extent_buffer *leaf;
875 struct btrfs_key key;
880 u64 bs = root->fs_info->sb->s_blocksize;
885 * - split compressed inline extents. annoying: we need to
886 * decompress into destination's address_space (the file offset
887 * may change, so source mapping won't do), then recompress (or
888 * otherwise reinsert) a subrange.
889 * - allow ranges within the same file to be cloned (provided
890 * they don't overlap)?
893 /* the destination must be opened for writing */
894 if (!(file->f_mode & FMODE_WRITE))
897 ret = mnt_want_write(file->f_path.mnt);
901 src_file = fget(srcfd);
906 src = src_file->f_dentry->d_inode;
913 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
917 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
921 buf = vmalloc(btrfs_level_size(root, 0));
925 path = btrfs_alloc_path();
933 mutex_lock(&inode->i_mutex);
934 mutex_lock(&src->i_mutex);
936 mutex_lock(&src->i_mutex);
937 mutex_lock(&inode->i_mutex);
940 /* determine range to clone */
942 if (off >= src->i_size || off + len > src->i_size)
945 olen = len = src->i_size - off;
946 /* if we extend to eof, continue to block boundary */
947 if (off + len == src->i_size)
948 len = ((src->i_size + bs-1) & ~(bs-1))
951 /* verify the end result is block aligned */
952 if ((off & (bs-1)) ||
953 ((off + len) & (bs-1)))
956 /* do any pending delalloc/csum calc on src, one way or
957 another, and lock file content */
959 struct btrfs_ordered_extent *ordered;
960 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
961 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
962 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
964 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
966 btrfs_put_ordered_extent(ordered);
967 btrfs_wait_ordered_range(src, off, off+len);
970 trans = btrfs_start_transaction(root, 1);
973 /* punch hole in destination first */
974 btrfs_drop_extents(trans, root, inode, off, off + len,
975 off + len, 0, &hint_byte);
978 key.objectid = src->i_ino;
979 key.type = BTRFS_EXTENT_DATA_KEY;
984 * note the key will change type as we walk through the
987 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
991 nritems = btrfs_header_nritems(path->nodes[0]);
992 if (path->slots[0] >= nritems) {
993 ret = btrfs_next_leaf(root, path);
998 nritems = btrfs_header_nritems(path->nodes[0]);
1000 leaf = path->nodes[0];
1001 slot = path->slots[0];
1003 btrfs_item_key_to_cpu(leaf, &key, slot);
1004 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1005 key.objectid != src->i_ino)
1008 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1009 struct btrfs_file_extent_item *extent;
1012 struct btrfs_key new_key;
1013 u64 disko = 0, diskl = 0;
1014 u64 datao = 0, datal = 0;
1017 size = btrfs_item_size_nr(leaf, slot);
1018 read_extent_buffer(leaf, buf,
1019 btrfs_item_ptr_offset(leaf, slot),
1022 extent = btrfs_item_ptr(leaf, slot,
1023 struct btrfs_file_extent_item);
1024 comp = btrfs_file_extent_compression(leaf, extent);
1025 type = btrfs_file_extent_type(leaf, extent);
1026 if (type == BTRFS_FILE_EXTENT_REG) {
1027 disko = btrfs_file_extent_disk_bytenr(leaf,
1029 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1031 datao = btrfs_file_extent_offset(leaf, extent);
1032 datal = btrfs_file_extent_num_bytes(leaf,
1034 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1035 /* take upper bound, may be compressed */
1036 datal = btrfs_file_extent_ram_bytes(leaf,
1039 btrfs_release_path(root, path);
1041 if (key.offset + datal < off ||
1042 key.offset >= off+len)
1045 memcpy(&new_key, &key, sizeof(new_key));
1046 new_key.objectid = inode->i_ino;
1047 new_key.offset = key.offset + destoff - off;
1049 if (type == BTRFS_FILE_EXTENT_REG) {
1050 ret = btrfs_insert_empty_item(trans, root, path,
1055 leaf = path->nodes[0];
1056 slot = path->slots[0];
1057 write_extent_buffer(leaf, buf,
1058 btrfs_item_ptr_offset(leaf, slot),
1061 extent = btrfs_item_ptr(leaf, slot,
1062 struct btrfs_file_extent_item);
1064 if (off > key.offset) {
1065 datao += off - key.offset;
1066 datal -= off - key.offset;
1068 if (key.offset + datao + datal + key.offset >
1070 datal = off + len - key.offset - datao;
1071 /* disko == 0 means it's a hole */
1075 btrfs_set_file_extent_offset(leaf, extent,
1077 btrfs_set_file_extent_num_bytes(leaf, extent,
1080 inode_add_bytes(inode, datal);
1081 ret = btrfs_inc_extent_ref(trans, root,
1083 root->root_key.objectid,
1085 new_key.offset - datao);
1088 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1091 if (off > key.offset) {
1092 skip = off - key.offset;
1093 new_key.offset += skip;
1096 if (key.offset + datal > off+len)
1097 trim = key.offset + datal - (off+len);
1099 if (comp && (skip || trim)) {
1103 size -= skip + trim;
1104 datal -= skip + trim;
1105 ret = btrfs_insert_empty_item(trans, root, path,
1112 btrfs_file_extent_calc_inline_size(0);
1113 memmove(buf+start, buf+start+skip,
1117 leaf = path->nodes[0];
1118 slot = path->slots[0];
1119 write_extent_buffer(leaf, buf,
1120 btrfs_item_ptr_offset(leaf, slot),
1122 inode_add_bytes(inode, datal);
1125 btrfs_mark_buffer_dirty(leaf);
1129 btrfs_release_path(root, path);
1134 btrfs_release_path(root, path);
1136 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1137 if (destoff + olen > inode->i_size)
1138 btrfs_i_size_write(inode, destoff + olen);
1139 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1140 ret = btrfs_update_inode(trans, root, inode);
1142 btrfs_end_transaction(trans, root);
1143 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1145 vmtruncate(inode, 0);
1147 mutex_unlock(&src->i_mutex);
1148 mutex_unlock(&inode->i_mutex);
1150 btrfs_free_path(path);
1154 mnt_drop_write(file->f_path.mnt);
1158 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1160 struct btrfs_ioctl_clone_range_args args;
1162 if (copy_from_user(&args, argp, sizeof(args)))
1164 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1165 args.src_length, args.dest_offset);
1169 * there are many ways the trans_start and trans_end ioctls can lead
1170 * to deadlocks. They should only be used by applications that
1171 * basically own the machine, and have a very in depth understanding
1172 * of all the possible deadlocks and enospc problems.
1174 static long btrfs_ioctl_trans_start(struct file *file)
1176 struct inode *inode = fdentry(file)->d_inode;
1177 struct btrfs_root *root = BTRFS_I(inode)->root;
1178 struct btrfs_trans_handle *trans;
1181 if (!capable(CAP_SYS_ADMIN))
1184 if (file->private_data) {
1189 ret = mnt_want_write(file->f_path.mnt);
1193 mutex_lock(&root->fs_info->trans_mutex);
1194 root->fs_info->open_ioctl_trans++;
1195 mutex_unlock(&root->fs_info->trans_mutex);
1197 trans = btrfs_start_ioctl_transaction(root, 0);
1199 file->private_data = trans;
1202 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1208 * there are many ways the trans_start and trans_end ioctls can lead
1209 * to deadlocks. They should only be used by applications that
1210 * basically own the machine, and have a very in depth understanding
1211 * of all the possible deadlocks and enospc problems.
1213 long btrfs_ioctl_trans_end(struct file *file)
1215 struct inode *inode = fdentry(file)->d_inode;
1216 struct btrfs_root *root = BTRFS_I(inode)->root;
1217 struct btrfs_trans_handle *trans;
1220 trans = file->private_data;
1225 btrfs_end_transaction(trans, root);
1226 file->private_data = NULL;
1228 mutex_lock(&root->fs_info->trans_mutex);
1229 root->fs_info->open_ioctl_trans--;
1230 mutex_unlock(&root->fs_info->trans_mutex);
1232 mnt_drop_write(file->f_path.mnt);
1238 long btrfs_ioctl(struct file *file, unsigned int
1239 cmd, unsigned long arg)
1241 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1242 void __user *argp = (void __user *)arg;
1245 case FS_IOC_GETFLAGS:
1246 return btrfs_ioctl_getflags(file, argp);
1247 case FS_IOC_SETFLAGS:
1248 return btrfs_ioctl_setflags(file, argp);
1249 case FS_IOC_GETVERSION:
1250 return btrfs_ioctl_getversion(file, argp);
1251 case BTRFS_IOC_SNAP_CREATE:
1252 return btrfs_ioctl_snap_create(file, argp, 0);
1253 case BTRFS_IOC_SUBVOL_CREATE:
1254 return btrfs_ioctl_snap_create(file, argp, 1);
1255 case BTRFS_IOC_DEFRAG:
1256 return btrfs_ioctl_defrag(file);
1257 case BTRFS_IOC_RESIZE:
1258 return btrfs_ioctl_resize(root, argp);
1259 case BTRFS_IOC_ADD_DEV:
1260 return btrfs_ioctl_add_dev(root, argp);
1261 case BTRFS_IOC_RM_DEV:
1262 return btrfs_ioctl_rm_dev(root, argp);
1263 case BTRFS_IOC_BALANCE:
1264 return btrfs_balance(root->fs_info->dev_root);
1265 case BTRFS_IOC_CLONE:
1266 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1267 case BTRFS_IOC_CLONE_RANGE:
1268 return btrfs_ioctl_clone_range(file, argp);
1269 case BTRFS_IOC_TRANS_START:
1270 return btrfs_ioctl_trans_start(file);
1271 case BTRFS_IOC_TRANS_END:
1272 return btrfs_ioctl_trans_end(file);
1273 case BTRFS_IOC_SYNC:
1274 btrfs_sync_fs(file->f_dentry->d_sb, 1);