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)
120 flags = BTRFS_I(dir)->flags;
122 if (S_ISREG(inode->i_mode))
123 flags &= ~BTRFS_INODE_DIRSYNC;
124 else if (!S_ISDIR(inode->i_mode))
125 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
127 BTRFS_I(inode)->flags = flags;
128 btrfs_update_iflags(inode);
131 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
133 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
134 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
136 if (copy_to_user(arg, &flags, sizeof(flags)))
141 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
143 struct inode *inode = file->f_path.dentry->d_inode;
144 struct btrfs_inode *ip = BTRFS_I(inode);
145 struct btrfs_root *root = ip->root;
146 struct btrfs_trans_handle *trans;
147 unsigned int flags, oldflags;
150 if (copy_from_user(&flags, arg, sizeof(flags)))
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL))
158 if (!is_owner_or_cap(inode))
161 mutex_lock(&inode->i_mutex);
163 flags = btrfs_mask_flags(inode->i_mode, flags);
164 oldflags = btrfs_flags_to_ioctl(ip->flags);
165 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
166 if (!capable(CAP_LINUX_IMMUTABLE)) {
172 ret = mnt_want_write(file->f_path.mnt);
176 if (flags & FS_SYNC_FL)
177 ip->flags |= BTRFS_INODE_SYNC;
179 ip->flags &= ~BTRFS_INODE_SYNC;
180 if (flags & FS_IMMUTABLE_FL)
181 ip->flags |= BTRFS_INODE_IMMUTABLE;
183 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
184 if (flags & FS_APPEND_FL)
185 ip->flags |= BTRFS_INODE_APPEND;
187 ip->flags &= ~BTRFS_INODE_APPEND;
188 if (flags & FS_NODUMP_FL)
189 ip->flags |= BTRFS_INODE_NODUMP;
191 ip->flags &= ~BTRFS_INODE_NODUMP;
192 if (flags & FS_NOATIME_FL)
193 ip->flags |= BTRFS_INODE_NOATIME;
195 ip->flags &= ~BTRFS_INODE_NOATIME;
196 if (flags & FS_DIRSYNC_FL)
197 ip->flags |= BTRFS_INODE_DIRSYNC;
199 ip->flags &= ~BTRFS_INODE_DIRSYNC;
202 trans = btrfs_join_transaction(root, 1);
205 ret = btrfs_update_inode(trans, root, inode);
208 btrfs_update_iflags(inode);
209 inode->i_ctime = CURRENT_TIME;
210 btrfs_end_transaction(trans, root);
212 mnt_drop_write(file->f_path.mnt);
214 mutex_unlock(&inode->i_mutex);
218 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
220 struct inode *inode = file->f_path.dentry->d_inode;
222 return put_user(inode->i_generation, arg);
225 static noinline int create_subvol(struct btrfs_root *root,
226 struct dentry *dentry,
227 char *name, int namelen)
229 struct btrfs_trans_handle *trans;
230 struct btrfs_key key;
231 struct btrfs_root_item root_item;
232 struct btrfs_inode_item *inode_item;
233 struct extent_buffer *leaf;
234 struct btrfs_root *new_root = root;
239 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
241 unsigned long nr = 1;
243 ret = btrfs_check_metadata_free_space(root);
247 trans = btrfs_start_transaction(root, 1);
250 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
255 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
256 0, objectid, NULL, 0, 0, 0);
262 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
263 btrfs_set_header_bytenr(leaf, leaf->start);
264 btrfs_set_header_generation(leaf, trans->transid);
265 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
266 btrfs_set_header_owner(leaf, objectid);
268 write_extent_buffer(leaf, root->fs_info->fsid,
269 (unsigned long)btrfs_header_fsid(leaf),
271 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
272 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
274 btrfs_mark_buffer_dirty(leaf);
276 inode_item = &root_item.inode;
277 memset(inode_item, 0, sizeof(*inode_item));
278 inode_item->generation = cpu_to_le64(1);
279 inode_item->size = cpu_to_le64(3);
280 inode_item->nlink = cpu_to_le32(1);
281 inode_item->nbytes = cpu_to_le64(root->leafsize);
282 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
284 btrfs_set_root_bytenr(&root_item, leaf->start);
285 btrfs_set_root_generation(&root_item, trans->transid);
286 btrfs_set_root_level(&root_item, 0);
287 btrfs_set_root_refs(&root_item, 1);
288 btrfs_set_root_used(&root_item, 0);
289 btrfs_set_root_last_snapshot(&root_item, 0);
291 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
292 root_item.drop_level = 0;
294 btrfs_tree_unlock(leaf);
295 free_extent_buffer(leaf);
298 btrfs_set_root_dirid(&root_item, new_dirid);
300 key.objectid = objectid;
302 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
303 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
309 * insert the directory item
311 key.offset = (u64)-1;
312 dir = dentry->d_parent->d_inode;
313 ret = btrfs_set_inode_index(dir, &index);
316 ret = btrfs_insert_dir_item(trans, root,
317 name, namelen, dir->i_ino, &key,
318 BTRFS_FT_DIR, index);
322 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
323 ret = btrfs_update_inode(trans, root, dir);
326 /* add the backref first */
327 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
328 objectid, BTRFS_ROOT_BACKREF_KEY,
329 root->root_key.objectid,
330 dir->i_ino, index, name, namelen);
334 /* now add the forward ref */
335 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
336 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
338 dir->i_ino, index, name, namelen);
342 ret = btrfs_commit_transaction(trans, root);
346 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
349 trans = btrfs_start_transaction(new_root, 1);
352 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
353 BTRFS_I(dir)->block_group);
358 nr = trans->blocks_used;
359 err = btrfs_commit_transaction(trans, new_root);
363 btrfs_btree_balance_dirty(root, nr);
367 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
368 char *name, int namelen)
370 struct btrfs_pending_snapshot *pending_snapshot;
371 struct btrfs_trans_handle *trans;
374 unsigned long nr = 0;
379 ret = btrfs_check_metadata_free_space(root);
383 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
384 if (!pending_snapshot) {
388 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
389 if (!pending_snapshot->name) {
391 kfree(pending_snapshot);
394 memcpy(pending_snapshot->name, name, namelen);
395 pending_snapshot->name[namelen] = '\0';
396 pending_snapshot->dentry = dentry;
397 trans = btrfs_start_transaction(root, 1);
399 pending_snapshot->root = root;
400 list_add(&pending_snapshot->list,
401 &trans->transaction->pending_snapshots);
402 err = btrfs_commit_transaction(trans, root);
405 btrfs_btree_balance_dirty(root, nr);
409 /* copy of may_create in fs/namei.c() */
410 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
416 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
420 * Create a new subvolume below @parent. This is largely modeled after
421 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
422 * inside this filesystem so it's quite a bit simpler.
424 static noinline int btrfs_mksubvol(struct path *parent, char *name,
425 int mode, int namelen,
426 struct btrfs_root *snap_src)
428 struct dentry *dentry;
431 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
433 dentry = lookup_one_len(name, parent->dentry, namelen);
434 error = PTR_ERR(dentry);
442 if (!IS_POSIXACL(parent->dentry->d_inode))
443 mode &= ~current_umask();
445 error = mnt_want_write(parent->mnt);
449 error = btrfs_may_create(parent->dentry->d_inode, dentry);
454 * Actually perform the low-level subvolume creation after all
457 * Eventually we want to pass in an inode under which we create this
458 * subvolume, but for now all are under the filesystem root.
460 * Also we should pass on the mode eventually to allow creating new
461 * subvolume with specific mode bits.
464 struct dentry *dir = dentry->d_parent;
465 struct dentry *test = dir->d_parent;
466 struct btrfs_path *path = btrfs_alloc_path();
469 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
471 test_oid = snap_src->root_key.objectid;
473 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
474 path, parent_oid, test_oid);
477 btrfs_release_path(snap_src->fs_info->tree_root, path);
479 /* we need to make sure we aren't creating a directory loop
480 * by taking a snapshot of something that has our current
481 * subvol in its directory tree. So, this loops through
482 * the dentries and checks the forward refs for each subvolume
483 * to see if is references the subvolume where we are
484 * placing this new snapshot.
488 dir == snap_src->fs_info->sb->s_root ||
489 test == snap_src->fs_info->sb->s_root ||
490 test->d_inode->i_sb != snap_src->fs_info->sb) {
493 if (S_ISLNK(test->d_inode->i_mode)) {
494 printk(KERN_INFO "Btrfs symlink in snapshot "
497 btrfs_free_path(path);
501 BTRFS_I(test->d_inode)->root->root_key.objectid;
502 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
503 path, test_oid, parent_oid);
505 printk(KERN_INFO "Btrfs snapshot creation "
506 "failed, looping\n");
508 btrfs_free_path(path);
511 btrfs_release_path(snap_src->fs_info->tree_root, path);
512 test = test->d_parent;
515 btrfs_free_path(path);
516 error = create_snapshot(snap_src, dentry, name, namelen);
518 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
519 dentry, name, namelen);
524 fsnotify_mkdir(parent->dentry->d_inode, dentry);
526 mnt_drop_write(parent->mnt);
530 mutex_unlock(&parent->dentry->d_inode->i_mutex);
535 static int btrfs_defrag_file(struct file *file)
537 struct inode *inode = fdentry(file)->d_inode;
538 struct btrfs_root *root = BTRFS_I(inode)->root;
539 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
540 struct btrfs_ordered_extent *ordered;
542 unsigned long last_index;
543 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
544 unsigned long total_read = 0;
550 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
554 mutex_lock(&inode->i_mutex);
555 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
556 for (i = 0; i <= last_index; i++) {
557 if (total_read % ra_pages == 0) {
558 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
559 min(last_index, i + ra_pages - 1));
563 page = grab_cache_page(inode->i_mapping, i);
566 if (!PageUptodate(page)) {
567 btrfs_readpage(NULL, page);
569 if (!PageUptodate(page)) {
571 page_cache_release(page);
576 wait_on_page_writeback(page);
578 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
579 page_end = page_start + PAGE_CACHE_SIZE - 1;
580 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
582 ordered = btrfs_lookup_ordered_extent(inode, page_start);
584 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
586 page_cache_release(page);
587 btrfs_start_ordered_extent(inode, ordered, 1);
588 btrfs_put_ordered_extent(ordered);
591 set_page_extent_mapped(page);
594 * this makes sure page_mkwrite is called on the
595 * page if it is dirtied again later
597 clear_page_dirty_for_io(page);
599 btrfs_set_extent_delalloc(inode, page_start, page_end);
601 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
602 set_page_dirty(page);
604 page_cache_release(page);
605 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
609 mutex_unlock(&inode->i_mutex);
613 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
618 struct btrfs_ioctl_vol_args *vol_args;
619 struct btrfs_trans_handle *trans;
620 struct btrfs_device *device = NULL;
627 if (root->fs_info->sb->s_flags & MS_RDONLY)
630 if (!capable(CAP_SYS_ADMIN))
633 vol_args = memdup_user(arg, sizeof(*vol_args));
634 if (IS_ERR(vol_args))
635 return PTR_ERR(vol_args);
637 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
638 namelen = strlen(vol_args->name);
640 mutex_lock(&root->fs_info->volume_mutex);
641 sizestr = vol_args->name;
642 devstr = strchr(sizestr, ':');
645 sizestr = devstr + 1;
647 devstr = vol_args->name;
648 devid = simple_strtoull(devstr, &end, 10);
649 printk(KERN_INFO "resizing devid %llu\n",
650 (unsigned long long)devid);
652 device = btrfs_find_device(root, devid, NULL, NULL);
654 printk(KERN_INFO "resizer unable to find device %llu\n",
655 (unsigned long long)devid);
659 if (!strcmp(sizestr, "max"))
660 new_size = device->bdev->bd_inode->i_size;
662 if (sizestr[0] == '-') {
665 } else if (sizestr[0] == '+') {
669 new_size = btrfs_parse_size(sizestr);
676 old_size = device->total_bytes;
679 if (new_size > old_size) {
683 new_size = old_size - new_size;
684 } else if (mod > 0) {
685 new_size = old_size + new_size;
688 if (new_size < 256 * 1024 * 1024) {
692 if (new_size > device->bdev->bd_inode->i_size) {
697 do_div(new_size, root->sectorsize);
698 new_size *= root->sectorsize;
700 printk(KERN_INFO "new size for %s is %llu\n",
701 device->name, (unsigned long long)new_size);
703 if (new_size > old_size) {
704 trans = btrfs_start_transaction(root, 1);
705 ret = btrfs_grow_device(trans, device, new_size);
706 btrfs_commit_transaction(trans, root);
708 ret = btrfs_shrink_device(device, new_size);
712 mutex_unlock(&root->fs_info->volume_mutex);
717 static noinline int btrfs_ioctl_snap_create(struct file *file,
718 void __user *arg, int subvol)
720 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
721 struct btrfs_ioctl_vol_args *vol_args;
722 struct btrfs_dir_item *di;
723 struct btrfs_path *path;
724 struct file *src_file;
729 if (root->fs_info->sb->s_flags & MS_RDONLY)
732 vol_args = memdup_user(arg, sizeof(*vol_args));
733 if (IS_ERR(vol_args))
734 return PTR_ERR(vol_args);
736 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
737 namelen = strlen(vol_args->name);
738 if (strchr(vol_args->name, '/')) {
743 path = btrfs_alloc_path();
749 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
750 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
752 vol_args->name, namelen, 0);
753 btrfs_free_path(path);
755 if (di && !IS_ERR(di)) {
766 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
767 file->f_path.dentry->d_inode->i_mode,
770 struct inode *src_inode;
771 src_file = fget(vol_args->fd);
777 src_inode = src_file->f_path.dentry->d_inode;
778 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
779 printk(KERN_INFO "btrfs: Snapshot src from "
785 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
786 file->f_path.dentry->d_inode->i_mode,
787 namelen, BTRFS_I(src_inode)->root);
796 static int btrfs_ioctl_defrag(struct file *file)
798 struct inode *inode = fdentry(file)->d_inode;
799 struct btrfs_root *root = BTRFS_I(inode)->root;
802 ret = mnt_want_write(file->f_path.mnt);
806 switch (inode->i_mode & S_IFMT) {
808 if (!capable(CAP_SYS_ADMIN)) {
812 btrfs_defrag_root(root, 0);
813 btrfs_defrag_root(root->fs_info->extent_root, 0);
816 if (!(file->f_mode & FMODE_WRITE)) {
820 btrfs_defrag_file(file);
824 mnt_drop_write(file->f_path.mnt);
828 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
830 struct btrfs_ioctl_vol_args *vol_args;
833 if (!capable(CAP_SYS_ADMIN))
836 vol_args = memdup_user(arg, sizeof(*vol_args));
837 if (IS_ERR(vol_args))
838 return PTR_ERR(vol_args);
840 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
841 ret = btrfs_init_new_device(root, vol_args->name);
847 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
849 struct btrfs_ioctl_vol_args *vol_args;
852 if (!capable(CAP_SYS_ADMIN))
855 if (root->fs_info->sb->s_flags & MS_RDONLY)
858 vol_args = memdup_user(arg, sizeof(*vol_args));
859 if (IS_ERR(vol_args))
860 return PTR_ERR(vol_args);
862 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
863 ret = btrfs_rm_device(root, vol_args->name);
869 static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
870 u64 off, u64 olen, u64 destoff)
872 struct inode *inode = fdentry(file)->d_inode;
873 struct btrfs_root *root = BTRFS_I(inode)->root;
874 struct file *src_file;
876 struct btrfs_trans_handle *trans;
877 struct btrfs_path *path;
878 struct extent_buffer *leaf;
880 struct btrfs_key key;
885 u64 bs = root->fs_info->sb->s_blocksize;
890 * - split compressed inline extents. annoying: we need to
891 * decompress into destination's address_space (the file offset
892 * may change, so source mapping won't do), then recompress (or
893 * otherwise reinsert) a subrange.
894 * - allow ranges within the same file to be cloned (provided
895 * they don't overlap)?
898 /* the destination must be opened for writing */
899 if (!(file->f_mode & FMODE_WRITE))
902 ret = mnt_want_write(file->f_path.mnt);
906 src_file = fget(srcfd);
911 src = src_file->f_dentry->d_inode;
918 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
922 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
926 buf = vmalloc(btrfs_level_size(root, 0));
930 path = btrfs_alloc_path();
938 mutex_lock(&inode->i_mutex);
939 mutex_lock(&src->i_mutex);
941 mutex_lock(&src->i_mutex);
942 mutex_lock(&inode->i_mutex);
945 /* determine range to clone */
947 if (off >= src->i_size || off + len > src->i_size)
950 olen = len = src->i_size - off;
951 /* if we extend to eof, continue to block boundary */
952 if (off + len == src->i_size)
953 len = ((src->i_size + bs-1) & ~(bs-1))
956 /* verify the end result is block aligned */
957 if ((off & (bs-1)) ||
958 ((off + len) & (bs-1)))
961 /* do any pending delalloc/csum calc on src, one way or
962 another, and lock file content */
964 struct btrfs_ordered_extent *ordered;
965 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
966 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
967 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
969 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
971 btrfs_put_ordered_extent(ordered);
972 btrfs_wait_ordered_range(src, off, off+len);
975 trans = btrfs_start_transaction(root, 1);
978 /* punch hole in destination first */
979 btrfs_drop_extents(trans, root, inode, off, off + len,
980 off + len, 0, &hint_byte);
983 key.objectid = src->i_ino;
984 key.type = BTRFS_EXTENT_DATA_KEY;
989 * note the key will change type as we walk through the
992 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
996 nritems = btrfs_header_nritems(path->nodes[0]);
997 if (path->slots[0] >= nritems) {
998 ret = btrfs_next_leaf(root, path);
1003 nritems = btrfs_header_nritems(path->nodes[0]);
1005 leaf = path->nodes[0];
1006 slot = path->slots[0];
1008 btrfs_item_key_to_cpu(leaf, &key, slot);
1009 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1010 key.objectid != src->i_ino)
1013 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1014 struct btrfs_file_extent_item *extent;
1017 struct btrfs_key new_key;
1018 u64 disko = 0, diskl = 0;
1019 u64 datao = 0, datal = 0;
1022 size = btrfs_item_size_nr(leaf, slot);
1023 read_extent_buffer(leaf, buf,
1024 btrfs_item_ptr_offset(leaf, slot),
1027 extent = btrfs_item_ptr(leaf, slot,
1028 struct btrfs_file_extent_item);
1029 comp = btrfs_file_extent_compression(leaf, extent);
1030 type = btrfs_file_extent_type(leaf, extent);
1031 if (type == BTRFS_FILE_EXTENT_REG) {
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 ret = btrfs_insert_empty_item(trans, root, path,
1060 leaf = path->nodes[0];
1061 slot = path->slots[0];
1062 write_extent_buffer(leaf, buf,
1063 btrfs_item_ptr_offset(leaf, slot),
1066 extent = btrfs_item_ptr(leaf, slot,
1067 struct btrfs_file_extent_item);
1069 if (off > key.offset) {
1070 datao += off - key.offset;
1071 datal -= off - key.offset;
1073 if (key.offset + datao + datal + key.offset >
1075 datal = off + len - key.offset - datao;
1076 /* disko == 0 means it's a hole */
1080 btrfs_set_file_extent_offset(leaf, extent,
1082 btrfs_set_file_extent_num_bytes(leaf, extent,
1085 inode_add_bytes(inode, datal);
1086 ret = btrfs_inc_extent_ref(trans, root,
1088 root->root_key.objectid,
1090 new_key.offset - datao);
1093 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1096 if (off > key.offset) {
1097 skip = off - key.offset;
1098 new_key.offset += skip;
1101 if (key.offset + datal > off+len)
1102 trim = key.offset + datal - (off+len);
1104 if (comp && (skip || trim)) {
1108 size -= skip + trim;
1109 datal -= skip + trim;
1110 ret = btrfs_insert_empty_item(trans, root, path,
1117 btrfs_file_extent_calc_inline_size(0);
1118 memmove(buf+start, buf+start+skip,
1122 leaf = path->nodes[0];
1123 slot = path->slots[0];
1124 write_extent_buffer(leaf, buf,
1125 btrfs_item_ptr_offset(leaf, slot),
1127 inode_add_bytes(inode, datal);
1130 btrfs_mark_buffer_dirty(leaf);
1134 btrfs_release_path(root, path);
1139 btrfs_release_path(root, path);
1141 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1142 if (destoff + olen > inode->i_size)
1143 btrfs_i_size_write(inode, destoff + olen);
1144 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1145 ret = btrfs_update_inode(trans, root, inode);
1147 btrfs_end_transaction(trans, root);
1148 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1150 vmtruncate(inode, 0);
1152 mutex_unlock(&src->i_mutex);
1153 mutex_unlock(&inode->i_mutex);
1155 btrfs_free_path(path);
1159 mnt_drop_write(file->f_path.mnt);
1163 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1165 struct btrfs_ioctl_clone_range_args args;
1167 if (copy_from_user(&args, argp, sizeof(args)))
1169 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1170 args.src_length, args.dest_offset);
1174 * there are many ways the trans_start and trans_end ioctls can lead
1175 * to deadlocks. They should only be used by applications that
1176 * basically own the machine, and have a very in depth understanding
1177 * of all the possible deadlocks and enospc problems.
1179 static long btrfs_ioctl_trans_start(struct file *file)
1181 struct inode *inode = fdentry(file)->d_inode;
1182 struct btrfs_root *root = BTRFS_I(inode)->root;
1183 struct btrfs_trans_handle *trans;
1186 if (!capable(CAP_SYS_ADMIN))
1189 if (file->private_data) {
1194 ret = mnt_want_write(file->f_path.mnt);
1198 mutex_lock(&root->fs_info->trans_mutex);
1199 root->fs_info->open_ioctl_trans++;
1200 mutex_unlock(&root->fs_info->trans_mutex);
1202 trans = btrfs_start_ioctl_transaction(root, 0);
1204 file->private_data = trans;
1207 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1213 * there are many ways the trans_start and trans_end ioctls can lead
1214 * to deadlocks. They should only be used by applications that
1215 * basically own the machine, and have a very in depth understanding
1216 * of all the possible deadlocks and enospc problems.
1218 long btrfs_ioctl_trans_end(struct file *file)
1220 struct inode *inode = fdentry(file)->d_inode;
1221 struct btrfs_root *root = BTRFS_I(inode)->root;
1222 struct btrfs_trans_handle *trans;
1225 trans = file->private_data;
1230 btrfs_end_transaction(trans, root);
1231 file->private_data = NULL;
1233 mutex_lock(&root->fs_info->trans_mutex);
1234 root->fs_info->open_ioctl_trans--;
1235 mutex_unlock(&root->fs_info->trans_mutex);
1237 mnt_drop_write(file->f_path.mnt);
1243 long btrfs_ioctl(struct file *file, unsigned int
1244 cmd, unsigned long arg)
1246 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1247 void __user *argp = (void __user *)arg;
1250 case FS_IOC_GETFLAGS:
1251 return btrfs_ioctl_getflags(file, argp);
1252 case FS_IOC_SETFLAGS:
1253 return btrfs_ioctl_setflags(file, argp);
1254 case FS_IOC_GETVERSION:
1255 return btrfs_ioctl_getversion(file, argp);
1256 case BTRFS_IOC_SNAP_CREATE:
1257 return btrfs_ioctl_snap_create(file, argp, 0);
1258 case BTRFS_IOC_SUBVOL_CREATE:
1259 return btrfs_ioctl_snap_create(file, argp, 1);
1260 case BTRFS_IOC_DEFRAG:
1261 return btrfs_ioctl_defrag(file);
1262 case BTRFS_IOC_RESIZE:
1263 return btrfs_ioctl_resize(root, argp);
1264 case BTRFS_IOC_ADD_DEV:
1265 return btrfs_ioctl_add_dev(root, argp);
1266 case BTRFS_IOC_RM_DEV:
1267 return btrfs_ioctl_rm_dev(root, argp);
1268 case BTRFS_IOC_BALANCE:
1269 return btrfs_balance(root->fs_info->dev_root);
1270 case BTRFS_IOC_CLONE:
1271 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1272 case BTRFS_IOC_CLONE_RANGE:
1273 return btrfs_ioctl_clone_range(file, argp);
1274 case BTRFS_IOC_TRANS_START:
1275 return btrfs_ioctl_trans_start(file);
1276 case BTRFS_IOC_TRANS_END:
1277 return btrfs_ioctl_trans_end(file);
1278 case BTRFS_IOC_SYNC:
1279 btrfs_sync_fs(file->f_dentry->d_sb, 1);