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>
42 #include <linux/slab.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 (btrfs_root_readonly(root))
153 if (copy_from_user(&flags, arg, sizeof(flags)))
156 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
157 FS_NOATIME_FL | FS_NODUMP_FL | \
158 FS_SYNC_FL | FS_DIRSYNC_FL))
161 if (!is_owner_or_cap(inode))
164 mutex_lock(&inode->i_mutex);
166 flags = btrfs_mask_flags(inode->i_mode, flags);
167 oldflags = btrfs_flags_to_ioctl(ip->flags);
168 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
169 if (!capable(CAP_LINUX_IMMUTABLE)) {
175 ret = mnt_want_write(file->f_path.mnt);
179 if (flags & FS_SYNC_FL)
180 ip->flags |= BTRFS_INODE_SYNC;
182 ip->flags &= ~BTRFS_INODE_SYNC;
183 if (flags & FS_IMMUTABLE_FL)
184 ip->flags |= BTRFS_INODE_IMMUTABLE;
186 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
187 if (flags & FS_APPEND_FL)
188 ip->flags |= BTRFS_INODE_APPEND;
190 ip->flags &= ~BTRFS_INODE_APPEND;
191 if (flags & FS_NODUMP_FL)
192 ip->flags |= BTRFS_INODE_NODUMP;
194 ip->flags &= ~BTRFS_INODE_NODUMP;
195 if (flags & FS_NOATIME_FL)
196 ip->flags |= BTRFS_INODE_NOATIME;
198 ip->flags &= ~BTRFS_INODE_NOATIME;
199 if (flags & FS_DIRSYNC_FL)
200 ip->flags |= BTRFS_INODE_DIRSYNC;
202 ip->flags &= ~BTRFS_INODE_DIRSYNC;
205 trans = btrfs_join_transaction(root, 1);
206 BUG_ON(IS_ERR(trans));
208 ret = btrfs_update_inode(trans, root, inode);
211 btrfs_update_iflags(inode);
212 inode->i_ctime = CURRENT_TIME;
213 btrfs_end_transaction(trans, root);
215 mnt_drop_write(file->f_path.mnt);
217 mutex_unlock(&inode->i_mutex);
221 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
223 struct inode *inode = file->f_path.dentry->d_inode;
225 return put_user(inode->i_generation, arg);
228 static noinline int create_subvol(struct btrfs_root *root,
229 struct dentry *dentry,
230 char *name, int namelen,
233 struct btrfs_trans_handle *trans;
234 struct btrfs_key key;
235 struct btrfs_root_item root_item;
236 struct btrfs_inode_item *inode_item;
237 struct extent_buffer *leaf;
238 struct btrfs_root *new_root;
239 struct dentry *parent = dget_parent(dentry);
244 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
247 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
254 dir = parent->d_inode;
262 trans = btrfs_start_transaction(root, 6);
265 return PTR_ERR(trans);
268 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
269 0, objectid, NULL, 0, 0, 0);
275 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
276 btrfs_set_header_bytenr(leaf, leaf->start);
277 btrfs_set_header_generation(leaf, trans->transid);
278 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
279 btrfs_set_header_owner(leaf, objectid);
281 write_extent_buffer(leaf, root->fs_info->fsid,
282 (unsigned long)btrfs_header_fsid(leaf),
284 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
285 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
287 btrfs_mark_buffer_dirty(leaf);
289 inode_item = &root_item.inode;
290 memset(inode_item, 0, sizeof(*inode_item));
291 inode_item->generation = cpu_to_le64(1);
292 inode_item->size = cpu_to_le64(3);
293 inode_item->nlink = cpu_to_le32(1);
294 inode_item->nbytes = cpu_to_le64(root->leafsize);
295 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
297 btrfs_set_root_bytenr(&root_item, leaf->start);
298 btrfs_set_root_generation(&root_item, trans->transid);
299 btrfs_set_root_level(&root_item, 0);
300 btrfs_set_root_refs(&root_item, 1);
301 btrfs_set_root_used(&root_item, leaf->len);
302 btrfs_set_root_last_snapshot(&root_item, 0);
304 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
305 root_item.drop_level = 0;
307 btrfs_tree_unlock(leaf);
308 free_extent_buffer(leaf);
311 btrfs_set_root_dirid(&root_item, new_dirid);
313 key.objectid = objectid;
315 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
316 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
321 key.offset = (u64)-1;
322 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
323 BUG_ON(IS_ERR(new_root));
325 btrfs_record_root_in_trans(trans, new_root);
327 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
328 BTRFS_I(dir)->block_group);
330 * insert the directory item
332 ret = btrfs_set_inode_index(dir, &index);
335 ret = btrfs_insert_dir_item(trans, root,
336 name, namelen, dir->i_ino, &key,
337 BTRFS_FT_DIR, index);
341 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
342 ret = btrfs_update_inode(trans, root, dir);
345 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
346 objectid, root->root_key.objectid,
347 dir->i_ino, index, name, namelen);
351 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
355 *async_transid = trans->transid;
356 err = btrfs_commit_transaction_async(trans, root, 1);
358 err = btrfs_commit_transaction(trans, root);
365 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
366 char *name, int namelen, u64 *async_transid,
370 struct dentry *parent;
371 struct btrfs_pending_snapshot *pending_snapshot;
372 struct btrfs_trans_handle *trans;
378 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
379 if (!pending_snapshot)
382 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
383 pending_snapshot->dentry = dentry;
384 pending_snapshot->root = root;
385 pending_snapshot->readonly = readonly;
387 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
389 ret = PTR_ERR(trans);
393 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
396 list_add(&pending_snapshot->list,
397 &trans->transaction->pending_snapshots);
399 *async_transid = trans->transid;
400 ret = btrfs_commit_transaction_async(trans,
401 root->fs_info->extent_root, 1);
403 ret = btrfs_commit_transaction(trans,
404 root->fs_info->extent_root);
408 ret = pending_snapshot->error;
412 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
416 parent = dget_parent(dentry);
417 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
420 ret = PTR_ERR(inode);
424 d_instantiate(dentry, inode);
427 kfree(pending_snapshot);
431 /* copy of check_sticky in fs/namei.c()
432 * It's inline, so penalty for filesystems that don't use sticky bit is
435 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
437 uid_t fsuid = current_fsuid();
439 if (!(dir->i_mode & S_ISVTX))
441 if (inode->i_uid == fsuid)
443 if (dir->i_uid == fsuid)
445 return !capable(CAP_FOWNER);
448 /* copy of may_delete in fs/namei.c()
449 * Check whether we can remove a link victim from directory dir, check
450 * whether the type of victim is right.
451 * 1. We can't do it if dir is read-only (done in permission())
452 * 2. We should have write and exec permissions on dir
453 * 3. We can't remove anything from append-only dir
454 * 4. We can't do anything with immutable dir (done in permission())
455 * 5. If the sticky bit on dir is set we should either
456 * a. be owner of dir, or
457 * b. be owner of victim, or
458 * c. have CAP_FOWNER capability
459 * 6. If the victim is append-only or immutable we can't do antyhing with
460 * links pointing to it.
461 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
462 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
463 * 9. We can't remove a root or mountpoint.
464 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
465 * nfs_async_unlink().
468 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
472 if (!victim->d_inode)
475 BUG_ON(victim->d_parent->d_inode != dir);
476 audit_inode_child(victim, dir);
478 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
483 if (btrfs_check_sticky(dir, victim->d_inode)||
484 IS_APPEND(victim->d_inode)||
485 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
488 if (!S_ISDIR(victim->d_inode->i_mode))
492 } else if (S_ISDIR(victim->d_inode->i_mode))
496 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
501 /* copy of may_create in fs/namei.c() */
502 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
508 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
512 * Create a new subvolume below @parent. This is largely modeled after
513 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
514 * inside this filesystem so it's quite a bit simpler.
516 static noinline int btrfs_mksubvol(struct path *parent,
517 char *name, int namelen,
518 struct btrfs_root *snap_src,
519 u64 *async_transid, bool readonly)
521 struct inode *dir = parent->dentry->d_inode;
522 struct dentry *dentry;
525 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
527 dentry = lookup_one_len(name, parent->dentry, namelen);
528 error = PTR_ERR(dentry);
536 error = mnt_want_write(parent->mnt);
540 error = btrfs_may_create(dir, dentry);
544 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
546 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
550 error = create_snapshot(snap_src, dentry,
551 name, namelen, async_transid, readonly);
553 error = create_subvol(BTRFS_I(dir)->root, dentry,
554 name, namelen, async_transid);
557 fsnotify_mkdir(dir, dentry);
559 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
561 mnt_drop_write(parent->mnt);
565 mutex_unlock(&dir->i_mutex);
569 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
570 int thresh, u64 *last_len, u64 *skip,
573 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
574 struct extent_map *em = NULL;
575 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
583 * make sure that once we start defragging and extent, we keep on
586 if (start < *defrag_end)
592 * hopefully we have this extent in the tree already, try without
593 * the full extent lock
595 read_lock(&em_tree->lock);
596 em = lookup_extent_mapping(em_tree, start, len);
597 read_unlock(&em_tree->lock);
600 /* get the big lock and read metadata off disk */
601 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
602 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
603 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
609 /* this will cover holes, and inline extents */
610 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
614 * we hit a real extent, if it is big don't bother defragging it again
616 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
620 * last_len ends up being a counter of how many bytes we've defragged.
621 * every time we choose not to defrag an extent, we reset *last_len
622 * so that the next tiny extent will force a defrag.
624 * The end result of this is that tiny extents before a single big
625 * extent will force at least part of that big extent to be defragged.
629 *defrag_end = extent_map_end(em);
632 *skip = extent_map_end(em);
640 static int btrfs_defrag_file(struct file *file,
641 struct btrfs_ioctl_defrag_range_args *range)
643 struct inode *inode = fdentry(file)->d_inode;
644 struct btrfs_root *root = BTRFS_I(inode)->root;
645 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
646 struct btrfs_ordered_extent *ordered;
648 struct btrfs_super_block *disk_super;
649 unsigned long last_index;
650 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
651 unsigned long total_read = 0;
660 int compress_type = BTRFS_COMPRESS_ZLIB;
662 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
663 if (range->compress_type > BTRFS_COMPRESS_TYPES)
665 if (range->compress_type)
666 compress_type = range->compress_type;
669 if (inode->i_size == 0)
672 if (range->start + range->len > range->start) {
673 last_index = min_t(u64, inode->i_size - 1,
674 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
676 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
679 i = range->start >> PAGE_CACHE_SHIFT;
680 while (i <= last_index) {
681 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
683 range->extent_thresh,
688 * the should_defrag function tells us how much to skip
689 * bump our counter by the suggested amount
691 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
692 i = max(i + 1, next);
696 if (total_read % ra_pages == 0) {
697 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
698 min(last_index, i + ra_pages - 1));
701 mutex_lock(&inode->i_mutex);
702 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
703 BTRFS_I(inode)->force_compress = compress_type;
705 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
709 if (inode->i_size == 0 ||
710 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
712 goto err_reservations;
715 page = grab_cache_page(inode->i_mapping, i);
718 goto err_reservations;
721 if (!PageUptodate(page)) {
722 btrfs_readpage(NULL, page);
724 if (!PageUptodate(page)) {
726 page_cache_release(page);
728 goto err_reservations;
732 if (page->mapping != inode->i_mapping) {
734 page_cache_release(page);
738 wait_on_page_writeback(page);
740 if (PageDirty(page)) {
741 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
745 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
746 page_end = page_start + PAGE_CACHE_SIZE - 1;
747 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
749 ordered = btrfs_lookup_ordered_extent(inode, page_start);
751 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
753 page_cache_release(page);
754 btrfs_start_ordered_extent(inode, ordered, 1);
755 btrfs_put_ordered_extent(ordered);
758 set_page_extent_mapped(page);
761 * this makes sure page_mkwrite is called on the
762 * page if it is dirtied again later
764 clear_page_dirty_for_io(page);
765 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
766 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
767 EXTENT_DO_ACCOUNTING, GFP_NOFS);
769 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
770 ClearPageChecked(page);
771 set_page_dirty(page);
772 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
776 page_cache_release(page);
777 mutex_unlock(&inode->i_mutex);
779 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
783 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
784 filemap_flush(inode->i_mapping);
786 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
787 /* the filemap_flush will queue IO into the worker threads, but
788 * we have to make sure the IO is actually started and that
789 * ordered extents get created before we return
791 atomic_inc(&root->fs_info->async_submit_draining);
792 while (atomic_read(&root->fs_info->nr_async_submits) ||
793 atomic_read(&root->fs_info->async_delalloc_pages)) {
794 wait_event(root->fs_info->async_submit_wait,
795 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
796 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
798 atomic_dec(&root->fs_info->async_submit_draining);
800 mutex_lock(&inode->i_mutex);
801 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
802 mutex_unlock(&inode->i_mutex);
805 disk_super = &root->fs_info->super_copy;
806 features = btrfs_super_incompat_flags(disk_super);
807 if (range->compress_type == BTRFS_COMPRESS_LZO) {
808 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
809 btrfs_set_super_incompat_flags(disk_super, features);
815 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
817 mutex_unlock(&inode->i_mutex);
821 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
827 struct btrfs_ioctl_vol_args *vol_args;
828 struct btrfs_trans_handle *trans;
829 struct btrfs_device *device = NULL;
835 if (root->fs_info->sb->s_flags & MS_RDONLY)
838 if (!capable(CAP_SYS_ADMIN))
841 vol_args = memdup_user(arg, sizeof(*vol_args));
842 if (IS_ERR(vol_args))
843 return PTR_ERR(vol_args);
845 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
847 mutex_lock(&root->fs_info->volume_mutex);
848 sizestr = vol_args->name;
849 devstr = strchr(sizestr, ':');
852 sizestr = devstr + 1;
854 devstr = vol_args->name;
855 devid = simple_strtoull(devstr, &end, 10);
856 printk(KERN_INFO "resizing devid %llu\n",
857 (unsigned long long)devid);
859 device = btrfs_find_device(root, devid, NULL, NULL);
861 printk(KERN_INFO "resizer unable to find device %llu\n",
862 (unsigned long long)devid);
866 if (!strcmp(sizestr, "max"))
867 new_size = device->bdev->bd_inode->i_size;
869 if (sizestr[0] == '-') {
872 } else if (sizestr[0] == '+') {
876 new_size = memparse(sizestr, NULL);
883 old_size = device->total_bytes;
886 if (new_size > old_size) {
890 new_size = old_size - new_size;
891 } else if (mod > 0) {
892 new_size = old_size + new_size;
895 if (new_size < 256 * 1024 * 1024) {
899 if (new_size > device->bdev->bd_inode->i_size) {
904 do_div(new_size, root->sectorsize);
905 new_size *= root->sectorsize;
907 printk(KERN_INFO "new size for %s is %llu\n",
908 device->name, (unsigned long long)new_size);
910 if (new_size > old_size) {
911 trans = btrfs_start_transaction(root, 0);
913 ret = PTR_ERR(trans);
916 ret = btrfs_grow_device(trans, device, new_size);
917 btrfs_commit_transaction(trans, root);
919 ret = btrfs_shrink_device(device, new_size);
923 mutex_unlock(&root->fs_info->volume_mutex);
928 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
935 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
936 struct file *src_file;
940 if (root->fs_info->sb->s_flags & MS_RDONLY)
943 namelen = strlen(name);
944 if (strchr(name, '/')) {
950 ret = btrfs_mksubvol(&file->f_path, name, namelen,
951 NULL, transid, readonly);
953 struct inode *src_inode;
960 src_inode = src_file->f_path.dentry->d_inode;
961 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
962 printk(KERN_INFO "btrfs: Snapshot src from "
968 ret = btrfs_mksubvol(&file->f_path, name, namelen,
969 BTRFS_I(src_inode)->root,
977 static noinline int btrfs_ioctl_snap_create(struct file *file,
978 void __user *arg, int subvol)
980 struct btrfs_ioctl_vol_args *vol_args;
983 vol_args = memdup_user(arg, sizeof(*vol_args));
984 if (IS_ERR(vol_args))
985 return PTR_ERR(vol_args);
986 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
988 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
989 vol_args->fd, subvol,
996 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
997 void __user *arg, int subvol)
999 struct btrfs_ioctl_vol_args_v2 *vol_args;
1003 bool readonly = false;
1005 vol_args = memdup_user(arg, sizeof(*vol_args));
1006 if (IS_ERR(vol_args))
1007 return PTR_ERR(vol_args);
1008 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1010 if (vol_args->flags &
1011 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1016 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1018 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1021 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1022 vol_args->fd, subvol,
1025 if (ret == 0 && ptr &&
1027 offsetof(struct btrfs_ioctl_vol_args_v2,
1028 transid), ptr, sizeof(*ptr)))
1035 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1038 struct inode *inode = fdentry(file)->d_inode;
1039 struct btrfs_root *root = BTRFS_I(inode)->root;
1043 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1046 down_read(&root->fs_info->subvol_sem);
1047 if (btrfs_root_readonly(root))
1048 flags |= BTRFS_SUBVOL_RDONLY;
1049 up_read(&root->fs_info->subvol_sem);
1051 if (copy_to_user(arg, &flags, sizeof(flags)))
1057 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1060 struct inode *inode = fdentry(file)->d_inode;
1061 struct btrfs_root *root = BTRFS_I(inode)->root;
1062 struct btrfs_trans_handle *trans;
1067 if (root->fs_info->sb->s_flags & MS_RDONLY)
1070 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1073 if (copy_from_user(&flags, arg, sizeof(flags)))
1076 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
1079 if (flags & ~BTRFS_SUBVOL_RDONLY)
1082 if (!is_owner_or_cap(inode))
1085 down_write(&root->fs_info->subvol_sem);
1088 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1091 root_flags = btrfs_root_flags(&root->root_item);
1092 if (flags & BTRFS_SUBVOL_RDONLY)
1093 btrfs_set_root_flags(&root->root_item,
1094 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1096 btrfs_set_root_flags(&root->root_item,
1097 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1099 trans = btrfs_start_transaction(root, 1);
1100 if (IS_ERR(trans)) {
1101 ret = PTR_ERR(trans);
1105 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1106 &root->root_key, &root->root_item);
1108 btrfs_commit_transaction(trans, root);
1111 btrfs_set_root_flags(&root->root_item, root_flags);
1113 up_write(&root->fs_info->subvol_sem);
1118 * helper to check if the subvolume references other subvolumes
1120 static noinline int may_destroy_subvol(struct btrfs_root *root)
1122 struct btrfs_path *path;
1123 struct btrfs_key key;
1126 path = btrfs_alloc_path();
1130 key.objectid = root->root_key.objectid;
1131 key.type = BTRFS_ROOT_REF_KEY;
1132 key.offset = (u64)-1;
1134 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1141 if (path->slots[0] > 0) {
1143 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1144 if (key.objectid == root->root_key.objectid &&
1145 key.type == BTRFS_ROOT_REF_KEY)
1149 btrfs_free_path(path);
1153 static noinline int key_in_sk(struct btrfs_key *key,
1154 struct btrfs_ioctl_search_key *sk)
1156 struct btrfs_key test;
1159 test.objectid = sk->min_objectid;
1160 test.type = sk->min_type;
1161 test.offset = sk->min_offset;
1163 ret = btrfs_comp_cpu_keys(key, &test);
1167 test.objectid = sk->max_objectid;
1168 test.type = sk->max_type;
1169 test.offset = sk->max_offset;
1171 ret = btrfs_comp_cpu_keys(key, &test);
1177 static noinline int copy_to_sk(struct btrfs_root *root,
1178 struct btrfs_path *path,
1179 struct btrfs_key *key,
1180 struct btrfs_ioctl_search_key *sk,
1182 unsigned long *sk_offset,
1186 struct extent_buffer *leaf;
1187 struct btrfs_ioctl_search_header sh;
1188 unsigned long item_off;
1189 unsigned long item_len;
1196 leaf = path->nodes[0];
1197 slot = path->slots[0];
1198 nritems = btrfs_header_nritems(leaf);
1200 if (btrfs_header_generation(leaf) > sk->max_transid) {
1204 found_transid = btrfs_header_generation(leaf);
1206 for (i = slot; i < nritems; i++) {
1207 item_off = btrfs_item_ptr_offset(leaf, i);
1208 item_len = btrfs_item_size_nr(leaf, i);
1210 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1213 if (sizeof(sh) + item_len + *sk_offset >
1214 BTRFS_SEARCH_ARGS_BUFSIZE) {
1219 btrfs_item_key_to_cpu(leaf, key, i);
1220 if (!key_in_sk(key, sk))
1223 sh.objectid = key->objectid;
1224 sh.offset = key->offset;
1225 sh.type = key->type;
1227 sh.transid = found_transid;
1229 /* copy search result header */
1230 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1231 *sk_offset += sizeof(sh);
1234 char *p = buf + *sk_offset;
1236 read_extent_buffer(leaf, p,
1237 item_off, item_len);
1238 *sk_offset += item_len;
1242 if (*num_found >= sk->nr_items)
1247 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1249 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1252 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1259 *num_found += found;
1263 static noinline int search_ioctl(struct inode *inode,
1264 struct btrfs_ioctl_search_args *args)
1266 struct btrfs_root *root;
1267 struct btrfs_key key;
1268 struct btrfs_key max_key;
1269 struct btrfs_path *path;
1270 struct btrfs_ioctl_search_key *sk = &args->key;
1271 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1274 unsigned long sk_offset = 0;
1276 path = btrfs_alloc_path();
1280 if (sk->tree_id == 0) {
1281 /* search the root of the inode that was passed */
1282 root = BTRFS_I(inode)->root;
1284 key.objectid = sk->tree_id;
1285 key.type = BTRFS_ROOT_ITEM_KEY;
1286 key.offset = (u64)-1;
1287 root = btrfs_read_fs_root_no_name(info, &key);
1289 printk(KERN_ERR "could not find root %llu\n",
1291 btrfs_free_path(path);
1296 key.objectid = sk->min_objectid;
1297 key.type = sk->min_type;
1298 key.offset = sk->min_offset;
1300 max_key.objectid = sk->max_objectid;
1301 max_key.type = sk->max_type;
1302 max_key.offset = sk->max_offset;
1304 path->keep_locks = 1;
1307 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1314 ret = copy_to_sk(root, path, &key, sk, args->buf,
1315 &sk_offset, &num_found);
1316 btrfs_release_path(root, path);
1317 if (ret || num_found >= sk->nr_items)
1323 sk->nr_items = num_found;
1324 btrfs_free_path(path);
1328 static noinline int btrfs_ioctl_tree_search(struct file *file,
1331 struct btrfs_ioctl_search_args *args;
1332 struct inode *inode;
1335 if (!capable(CAP_SYS_ADMIN))
1338 args = memdup_user(argp, sizeof(*args));
1340 return PTR_ERR(args);
1342 inode = fdentry(file)->d_inode;
1343 ret = search_ioctl(inode, args);
1344 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1351 * Search INODE_REFs to identify path name of 'dirid' directory
1352 * in a 'tree_id' tree. and sets path name to 'name'.
1354 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1355 u64 tree_id, u64 dirid, char *name)
1357 struct btrfs_root *root;
1358 struct btrfs_key key;
1364 struct btrfs_inode_ref *iref;
1365 struct extent_buffer *l;
1366 struct btrfs_path *path;
1368 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1373 path = btrfs_alloc_path();
1377 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1379 key.objectid = tree_id;
1380 key.type = BTRFS_ROOT_ITEM_KEY;
1381 key.offset = (u64)-1;
1382 root = btrfs_read_fs_root_no_name(info, &key);
1384 printk(KERN_ERR "could not find root %llu\n", tree_id);
1389 key.objectid = dirid;
1390 key.type = BTRFS_INODE_REF_KEY;
1391 key.offset = (u64)-1;
1394 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1399 slot = path->slots[0];
1400 if (ret > 0 && slot > 0)
1402 btrfs_item_key_to_cpu(l, &key, slot);
1404 if (ret > 0 && (key.objectid != dirid ||
1405 key.type != BTRFS_INODE_REF_KEY)) {
1410 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1411 len = btrfs_inode_ref_name_len(l, iref);
1413 total_len += len + 1;
1418 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1420 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1423 btrfs_release_path(root, path);
1424 key.objectid = key.offset;
1425 key.offset = (u64)-1;
1426 dirid = key.objectid;
1431 memcpy(name, ptr, total_len);
1432 name[total_len]='\0';
1435 btrfs_free_path(path);
1439 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1442 struct btrfs_ioctl_ino_lookup_args *args;
1443 struct inode *inode;
1446 if (!capable(CAP_SYS_ADMIN))
1449 args = memdup_user(argp, sizeof(*args));
1451 return PTR_ERR(args);
1453 inode = fdentry(file)->d_inode;
1455 if (args->treeid == 0)
1456 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1458 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1459 args->treeid, args->objectid,
1462 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1469 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1472 struct dentry *parent = fdentry(file);
1473 struct dentry *dentry;
1474 struct inode *dir = parent->d_inode;
1475 struct inode *inode;
1476 struct btrfs_root *root = BTRFS_I(dir)->root;
1477 struct btrfs_root *dest = NULL;
1478 struct btrfs_ioctl_vol_args *vol_args;
1479 struct btrfs_trans_handle *trans;
1484 vol_args = memdup_user(arg, sizeof(*vol_args));
1485 if (IS_ERR(vol_args))
1486 return PTR_ERR(vol_args);
1488 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1489 namelen = strlen(vol_args->name);
1490 if (strchr(vol_args->name, '/') ||
1491 strncmp(vol_args->name, "..", namelen) == 0) {
1496 err = mnt_want_write(file->f_path.mnt);
1500 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1501 dentry = lookup_one_len(vol_args->name, parent, namelen);
1502 if (IS_ERR(dentry)) {
1503 err = PTR_ERR(dentry);
1504 goto out_unlock_dir;
1507 if (!dentry->d_inode) {
1512 inode = dentry->d_inode;
1513 dest = BTRFS_I(inode)->root;
1514 if (!capable(CAP_SYS_ADMIN)){
1516 * Regular user. Only allow this with a special mount
1517 * option, when the user has write+exec access to the
1518 * subvol root, and when rmdir(2) would have been
1521 * Note that this is _not_ check that the subvol is
1522 * empty or doesn't contain data that we wouldn't
1523 * otherwise be able to delete.
1525 * Users who want to delete empty subvols should try
1529 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1533 * Do not allow deletion if the parent dir is the same
1534 * as the dir to be deleted. That means the ioctl
1535 * must be called on the dentry referencing the root
1536 * of the subvol, not a random directory contained
1543 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1547 /* check if subvolume may be deleted by a non-root user */
1548 err = btrfs_may_delete(dir, dentry, 1);
1553 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1558 mutex_lock(&inode->i_mutex);
1559 err = d_invalidate(dentry);
1563 down_write(&root->fs_info->subvol_sem);
1565 err = may_destroy_subvol(dest);
1569 trans = btrfs_start_transaction(root, 0);
1570 if (IS_ERR(trans)) {
1571 err = PTR_ERR(trans);
1574 trans->block_rsv = &root->fs_info->global_block_rsv;
1576 ret = btrfs_unlink_subvol(trans, root, dir,
1577 dest->root_key.objectid,
1578 dentry->d_name.name,
1579 dentry->d_name.len);
1582 btrfs_record_root_in_trans(trans, dest);
1584 memset(&dest->root_item.drop_progress, 0,
1585 sizeof(dest->root_item.drop_progress));
1586 dest->root_item.drop_level = 0;
1587 btrfs_set_root_refs(&dest->root_item, 0);
1589 if (!xchg(&dest->orphan_item_inserted, 1)) {
1590 ret = btrfs_insert_orphan_item(trans,
1591 root->fs_info->tree_root,
1592 dest->root_key.objectid);
1596 ret = btrfs_end_transaction(trans, root);
1598 inode->i_flags |= S_DEAD;
1600 up_write(&root->fs_info->subvol_sem);
1602 mutex_unlock(&inode->i_mutex);
1604 shrink_dcache_sb(root->fs_info->sb);
1605 btrfs_invalidate_inodes(dest);
1611 mutex_unlock(&dir->i_mutex);
1612 mnt_drop_write(file->f_path.mnt);
1618 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1620 struct inode *inode = fdentry(file)->d_inode;
1621 struct btrfs_root *root = BTRFS_I(inode)->root;
1622 struct btrfs_ioctl_defrag_range_args *range;
1625 if (btrfs_root_readonly(root))
1628 ret = mnt_want_write(file->f_path.mnt);
1632 switch (inode->i_mode & S_IFMT) {
1634 if (!capable(CAP_SYS_ADMIN)) {
1638 ret = btrfs_defrag_root(root, 0);
1641 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1644 if (!(file->f_mode & FMODE_WRITE)) {
1649 range = kzalloc(sizeof(*range), GFP_KERNEL);
1656 if (copy_from_user(range, argp,
1662 /* compression requires us to start the IO */
1663 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1664 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1665 range->extent_thresh = (u32)-1;
1668 /* the rest are all set to zero by kzalloc */
1669 range->len = (u64)-1;
1671 ret = btrfs_defrag_file(file, range);
1678 mnt_drop_write(file->f_path.mnt);
1682 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1684 struct btrfs_ioctl_vol_args *vol_args;
1687 if (!capable(CAP_SYS_ADMIN))
1690 vol_args = memdup_user(arg, sizeof(*vol_args));
1691 if (IS_ERR(vol_args))
1692 return PTR_ERR(vol_args);
1694 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1695 ret = btrfs_init_new_device(root, vol_args->name);
1701 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1703 struct btrfs_ioctl_vol_args *vol_args;
1706 if (!capable(CAP_SYS_ADMIN))
1709 if (root->fs_info->sb->s_flags & MS_RDONLY)
1712 vol_args = memdup_user(arg, sizeof(*vol_args));
1713 if (IS_ERR(vol_args))
1714 return PTR_ERR(vol_args);
1716 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1717 ret = btrfs_rm_device(root, vol_args->name);
1723 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1724 u64 off, u64 olen, u64 destoff)
1726 struct inode *inode = fdentry(file)->d_inode;
1727 struct btrfs_root *root = BTRFS_I(inode)->root;
1728 struct file *src_file;
1730 struct btrfs_trans_handle *trans;
1731 struct btrfs_path *path;
1732 struct extent_buffer *leaf;
1734 struct btrfs_key key;
1739 u64 bs = root->fs_info->sb->s_blocksize;
1744 * - split compressed inline extents. annoying: we need to
1745 * decompress into destination's address_space (the file offset
1746 * may change, so source mapping won't do), then recompress (or
1747 * otherwise reinsert) a subrange.
1748 * - allow ranges within the same file to be cloned (provided
1749 * they don't overlap)?
1752 /* the destination must be opened for writing */
1753 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1756 if (btrfs_root_readonly(root))
1759 ret = mnt_want_write(file->f_path.mnt);
1763 src_file = fget(srcfd);
1766 goto out_drop_write;
1769 src = src_file->f_dentry->d_inode;
1775 /* the src must be open for reading */
1776 if (!(src_file->f_mode & FMODE_READ))
1780 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1784 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1788 buf = vmalloc(btrfs_level_size(root, 0));
1792 path = btrfs_alloc_path();
1800 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1801 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
1803 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1804 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1807 /* determine range to clone */
1809 if (off + len > src->i_size || off + len < off)
1812 olen = len = src->i_size - off;
1813 /* if we extend to eof, continue to block boundary */
1814 if (off + len == src->i_size)
1815 len = ALIGN(src->i_size, bs) - off;
1817 /* verify the end result is block aligned */
1818 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1819 !IS_ALIGNED(destoff, bs))
1822 /* do any pending delalloc/csum calc on src, one way or
1823 another, and lock file content */
1825 struct btrfs_ordered_extent *ordered;
1826 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1827 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1829 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1830 EXTENT_DELALLOC, 0, NULL))
1832 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1834 btrfs_put_ordered_extent(ordered);
1835 btrfs_wait_ordered_range(src, off, len);
1839 key.objectid = src->i_ino;
1840 key.type = BTRFS_EXTENT_DATA_KEY;
1845 * note the key will change type as we walk through the
1848 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1852 nritems = btrfs_header_nritems(path->nodes[0]);
1853 if (path->slots[0] >= nritems) {
1854 ret = btrfs_next_leaf(root, path);
1859 nritems = btrfs_header_nritems(path->nodes[0]);
1861 leaf = path->nodes[0];
1862 slot = path->slots[0];
1864 btrfs_item_key_to_cpu(leaf, &key, slot);
1865 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1866 key.objectid != src->i_ino)
1869 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1870 struct btrfs_file_extent_item *extent;
1873 struct btrfs_key new_key;
1874 u64 disko = 0, diskl = 0;
1875 u64 datao = 0, datal = 0;
1879 size = btrfs_item_size_nr(leaf, slot);
1880 read_extent_buffer(leaf, buf,
1881 btrfs_item_ptr_offset(leaf, slot),
1884 extent = btrfs_item_ptr(leaf, slot,
1885 struct btrfs_file_extent_item);
1886 comp = btrfs_file_extent_compression(leaf, extent);
1887 type = btrfs_file_extent_type(leaf, extent);
1888 if (type == BTRFS_FILE_EXTENT_REG ||
1889 type == BTRFS_FILE_EXTENT_PREALLOC) {
1890 disko = btrfs_file_extent_disk_bytenr(leaf,
1892 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1894 datao = btrfs_file_extent_offset(leaf, extent);
1895 datal = btrfs_file_extent_num_bytes(leaf,
1897 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1898 /* take upper bound, may be compressed */
1899 datal = btrfs_file_extent_ram_bytes(leaf,
1902 btrfs_release_path(root, path);
1904 if (key.offset + datal <= off ||
1905 key.offset >= off+len)
1908 memcpy(&new_key, &key, sizeof(new_key));
1909 new_key.objectid = inode->i_ino;
1910 if (off <= key.offset)
1911 new_key.offset = key.offset + destoff - off;
1913 new_key.offset = destoff;
1915 trans = btrfs_start_transaction(root, 1);
1916 if (IS_ERR(trans)) {
1917 ret = PTR_ERR(trans);
1921 if (type == BTRFS_FILE_EXTENT_REG ||
1922 type == BTRFS_FILE_EXTENT_PREALLOC) {
1923 if (off > key.offset) {
1924 datao += off - key.offset;
1925 datal -= off - key.offset;
1928 if (key.offset + datal > off + len)
1929 datal = off + len - key.offset;
1931 ret = btrfs_drop_extents(trans, inode,
1933 new_key.offset + datal,
1937 ret = btrfs_insert_empty_item(trans, root, path,
1941 leaf = path->nodes[0];
1942 slot = path->slots[0];
1943 write_extent_buffer(leaf, buf,
1944 btrfs_item_ptr_offset(leaf, slot),
1947 extent = btrfs_item_ptr(leaf, slot,
1948 struct btrfs_file_extent_item);
1950 /* disko == 0 means it's a hole */
1954 btrfs_set_file_extent_offset(leaf, extent,
1956 btrfs_set_file_extent_num_bytes(leaf, extent,
1959 inode_add_bytes(inode, datal);
1960 ret = btrfs_inc_extent_ref(trans, root,
1962 root->root_key.objectid,
1964 new_key.offset - datao);
1967 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1970 if (off > key.offset) {
1971 skip = off - key.offset;
1972 new_key.offset += skip;
1975 if (key.offset + datal > off+len)
1976 trim = key.offset + datal - (off+len);
1978 if (comp && (skip || trim)) {
1980 btrfs_end_transaction(trans, root);
1983 size -= skip + trim;
1984 datal -= skip + trim;
1986 ret = btrfs_drop_extents(trans, inode,
1988 new_key.offset + datal,
1992 ret = btrfs_insert_empty_item(trans, root, path,
1998 btrfs_file_extent_calc_inline_size(0);
1999 memmove(buf+start, buf+start+skip,
2003 leaf = path->nodes[0];
2004 slot = path->slots[0];
2005 write_extent_buffer(leaf, buf,
2006 btrfs_item_ptr_offset(leaf, slot),
2008 inode_add_bytes(inode, datal);
2011 btrfs_mark_buffer_dirty(leaf);
2012 btrfs_release_path(root, path);
2014 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2017 * we round up to the block size at eof when
2018 * determining which extents to clone above,
2019 * but shouldn't round up the file size
2021 endoff = new_key.offset + datal;
2022 if (endoff > destoff+olen)
2023 endoff = destoff+olen;
2024 if (endoff > inode->i_size)
2025 btrfs_i_size_write(inode, endoff);
2027 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2028 ret = btrfs_update_inode(trans, root, inode);
2030 btrfs_end_transaction(trans, root);
2033 btrfs_release_path(root, path);
2038 btrfs_release_path(root, path);
2039 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2041 mutex_unlock(&src->i_mutex);
2042 mutex_unlock(&inode->i_mutex);
2044 btrfs_free_path(path);
2048 mnt_drop_write(file->f_path.mnt);
2052 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2054 struct btrfs_ioctl_clone_range_args args;
2056 if (copy_from_user(&args, argp, sizeof(args)))
2058 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2059 args.src_length, args.dest_offset);
2063 * there are many ways the trans_start and trans_end ioctls can lead
2064 * to deadlocks. They should only be used by applications that
2065 * basically own the machine, and have a very in depth understanding
2066 * of all the possible deadlocks and enospc problems.
2068 static long btrfs_ioctl_trans_start(struct file *file)
2070 struct inode *inode = fdentry(file)->d_inode;
2071 struct btrfs_root *root = BTRFS_I(inode)->root;
2072 struct btrfs_trans_handle *trans;
2076 if (!capable(CAP_SYS_ADMIN))
2080 if (file->private_data)
2084 if (btrfs_root_readonly(root))
2087 ret = mnt_want_write(file->f_path.mnt);
2091 mutex_lock(&root->fs_info->trans_mutex);
2092 root->fs_info->open_ioctl_trans++;
2093 mutex_unlock(&root->fs_info->trans_mutex);
2096 trans = btrfs_start_ioctl_transaction(root, 0);
2100 file->private_data = trans;
2104 mutex_lock(&root->fs_info->trans_mutex);
2105 root->fs_info->open_ioctl_trans--;
2106 mutex_unlock(&root->fs_info->trans_mutex);
2107 mnt_drop_write(file->f_path.mnt);
2112 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2114 struct inode *inode = fdentry(file)->d_inode;
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
2116 struct btrfs_root *new_root;
2117 struct btrfs_dir_item *di;
2118 struct btrfs_trans_handle *trans;
2119 struct btrfs_path *path;
2120 struct btrfs_key location;
2121 struct btrfs_disk_key disk_key;
2122 struct btrfs_super_block *disk_super;
2127 if (!capable(CAP_SYS_ADMIN))
2130 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2134 objectid = root->root_key.objectid;
2136 location.objectid = objectid;
2137 location.type = BTRFS_ROOT_ITEM_KEY;
2138 location.offset = (u64)-1;
2140 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2141 if (IS_ERR(new_root))
2142 return PTR_ERR(new_root);
2144 if (btrfs_root_refs(&new_root->root_item) == 0)
2147 path = btrfs_alloc_path();
2150 path->leave_spinning = 1;
2152 trans = btrfs_start_transaction(root, 1);
2153 if (IS_ERR(trans)) {
2154 btrfs_free_path(path);
2155 return PTR_ERR(trans);
2158 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2159 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2160 dir_id, "default", 7, 1);
2161 if (IS_ERR_OR_NULL(di)) {
2162 btrfs_free_path(path);
2163 btrfs_end_transaction(trans, root);
2164 printk(KERN_ERR "Umm, you don't have the default dir item, "
2165 "this isn't going to work\n");
2169 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2170 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2171 btrfs_mark_buffer_dirty(path->nodes[0]);
2172 btrfs_free_path(path);
2174 disk_super = &root->fs_info->super_copy;
2175 features = btrfs_super_incompat_flags(disk_super);
2176 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2177 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2178 btrfs_set_super_incompat_flags(disk_super, features);
2180 btrfs_end_transaction(trans, root);
2185 static void get_block_group_info(struct list_head *groups_list,
2186 struct btrfs_ioctl_space_info *space)
2188 struct btrfs_block_group_cache *block_group;
2190 space->total_bytes = 0;
2191 space->used_bytes = 0;
2193 list_for_each_entry(block_group, groups_list, list) {
2194 space->flags = block_group->flags;
2195 space->total_bytes += block_group->key.offset;
2196 space->used_bytes +=
2197 btrfs_block_group_used(&block_group->item);
2201 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2203 struct btrfs_ioctl_space_args space_args;
2204 struct btrfs_ioctl_space_info space;
2205 struct btrfs_ioctl_space_info *dest;
2206 struct btrfs_ioctl_space_info *dest_orig;
2207 struct btrfs_ioctl_space_info *user_dest;
2208 struct btrfs_space_info *info;
2209 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2210 BTRFS_BLOCK_GROUP_SYSTEM,
2211 BTRFS_BLOCK_GROUP_METADATA,
2212 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2219 if (copy_from_user(&space_args,
2220 (struct btrfs_ioctl_space_args __user *)arg,
2221 sizeof(space_args)))
2224 for (i = 0; i < num_types; i++) {
2225 struct btrfs_space_info *tmp;
2229 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2231 if (tmp->flags == types[i]) {
2241 down_read(&info->groups_sem);
2242 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2243 if (!list_empty(&info->block_groups[c]))
2246 up_read(&info->groups_sem);
2249 /* space_slots == 0 means they are asking for a count */
2250 if (space_args.space_slots == 0) {
2251 space_args.total_spaces = slot_count;
2255 slot_count = min_t(u64, space_args.space_slots, slot_count);
2257 alloc_size = sizeof(*dest) * slot_count;
2259 /* we generally have at most 6 or so space infos, one for each raid
2260 * level. So, a whole page should be more than enough for everyone
2262 if (alloc_size > PAGE_CACHE_SIZE)
2265 space_args.total_spaces = 0;
2266 dest = kmalloc(alloc_size, GFP_NOFS);
2271 /* now we have a buffer to copy into */
2272 for (i = 0; i < num_types; i++) {
2273 struct btrfs_space_info *tmp;
2280 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2282 if (tmp->flags == types[i]) {
2291 down_read(&info->groups_sem);
2292 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2293 if (!list_empty(&info->block_groups[c])) {
2294 get_block_group_info(&info->block_groups[c],
2296 memcpy(dest, &space, sizeof(space));
2298 space_args.total_spaces++;
2304 up_read(&info->groups_sem);
2307 user_dest = (struct btrfs_ioctl_space_info *)
2308 (arg + sizeof(struct btrfs_ioctl_space_args));
2310 if (copy_to_user(user_dest, dest_orig, alloc_size))
2315 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2322 * there are many ways the trans_start and trans_end ioctls can lead
2323 * to deadlocks. They should only be used by applications that
2324 * basically own the machine, and have a very in depth understanding
2325 * of all the possible deadlocks and enospc problems.
2327 long btrfs_ioctl_trans_end(struct file *file)
2329 struct inode *inode = fdentry(file)->d_inode;
2330 struct btrfs_root *root = BTRFS_I(inode)->root;
2331 struct btrfs_trans_handle *trans;
2333 trans = file->private_data;
2336 file->private_data = NULL;
2338 btrfs_end_transaction(trans, root);
2340 mutex_lock(&root->fs_info->trans_mutex);
2341 root->fs_info->open_ioctl_trans--;
2342 mutex_unlock(&root->fs_info->trans_mutex);
2344 mnt_drop_write(file->f_path.mnt);
2348 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2350 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2351 struct btrfs_trans_handle *trans;
2355 trans = btrfs_start_transaction(root, 0);
2357 return PTR_ERR(trans);
2358 transid = trans->transid;
2359 ret = btrfs_commit_transaction_async(trans, root, 0);
2364 if (copy_to_user(argp, &transid, sizeof(transid)))
2369 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2371 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2375 if (copy_from_user(&transid, argp, sizeof(transid)))
2378 transid = 0; /* current trans */
2380 return btrfs_wait_for_commit(root, transid);
2383 long btrfs_ioctl(struct file *file, unsigned int
2384 cmd, unsigned long arg)
2386 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2387 void __user *argp = (void __user *)arg;
2390 case FS_IOC_GETFLAGS:
2391 return btrfs_ioctl_getflags(file, argp);
2392 case FS_IOC_SETFLAGS:
2393 return btrfs_ioctl_setflags(file, argp);
2394 case FS_IOC_GETVERSION:
2395 return btrfs_ioctl_getversion(file, argp);
2396 case BTRFS_IOC_SNAP_CREATE:
2397 return btrfs_ioctl_snap_create(file, argp, 0);
2398 case BTRFS_IOC_SNAP_CREATE_V2:
2399 return btrfs_ioctl_snap_create_v2(file, argp, 0);
2400 case BTRFS_IOC_SUBVOL_CREATE:
2401 return btrfs_ioctl_snap_create(file, argp, 1);
2402 case BTRFS_IOC_SNAP_DESTROY:
2403 return btrfs_ioctl_snap_destroy(file, argp);
2404 case BTRFS_IOC_SUBVOL_GETFLAGS:
2405 return btrfs_ioctl_subvol_getflags(file, argp);
2406 case BTRFS_IOC_SUBVOL_SETFLAGS:
2407 return btrfs_ioctl_subvol_setflags(file, argp);
2408 case BTRFS_IOC_DEFAULT_SUBVOL:
2409 return btrfs_ioctl_default_subvol(file, argp);
2410 case BTRFS_IOC_DEFRAG:
2411 return btrfs_ioctl_defrag(file, NULL);
2412 case BTRFS_IOC_DEFRAG_RANGE:
2413 return btrfs_ioctl_defrag(file, argp);
2414 case BTRFS_IOC_RESIZE:
2415 return btrfs_ioctl_resize(root, argp);
2416 case BTRFS_IOC_ADD_DEV:
2417 return btrfs_ioctl_add_dev(root, argp);
2418 case BTRFS_IOC_RM_DEV:
2419 return btrfs_ioctl_rm_dev(root, argp);
2420 case BTRFS_IOC_BALANCE:
2421 return btrfs_balance(root->fs_info->dev_root);
2422 case BTRFS_IOC_CLONE:
2423 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2424 case BTRFS_IOC_CLONE_RANGE:
2425 return btrfs_ioctl_clone_range(file, argp);
2426 case BTRFS_IOC_TRANS_START:
2427 return btrfs_ioctl_trans_start(file);
2428 case BTRFS_IOC_TRANS_END:
2429 return btrfs_ioctl_trans_end(file);
2430 case BTRFS_IOC_TREE_SEARCH:
2431 return btrfs_ioctl_tree_search(file, argp);
2432 case BTRFS_IOC_INO_LOOKUP:
2433 return btrfs_ioctl_ino_lookup(file, argp);
2434 case BTRFS_IOC_SPACE_INFO:
2435 return btrfs_ioctl_space_info(root, argp);
2436 case BTRFS_IOC_SYNC:
2437 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2439 case BTRFS_IOC_START_SYNC:
2440 return btrfs_ioctl_start_sync(file, argp);
2441 case BTRFS_IOC_WAIT_SYNC:
2442 return btrfs_ioctl_wait_sync(file, argp);