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>
43 #include <linux/blkdev.h>
44 #include <linux/uuid.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
51 #include "print-tree.h"
54 #include "inode-map.h"
56 #include "rcu-string.h"
59 /* Mask out flags that are inappropriate for the given type of inode. */
60 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
64 else if (S_ISREG(mode))
65 return flags & ~FS_DIRSYNC_FL;
67 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
71 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
73 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
75 unsigned int iflags = 0;
77 if (flags & BTRFS_INODE_SYNC)
79 if (flags & BTRFS_INODE_IMMUTABLE)
80 iflags |= FS_IMMUTABLE_FL;
81 if (flags & BTRFS_INODE_APPEND)
82 iflags |= FS_APPEND_FL;
83 if (flags & BTRFS_INODE_NODUMP)
84 iflags |= FS_NODUMP_FL;
85 if (flags & BTRFS_INODE_NOATIME)
86 iflags |= FS_NOATIME_FL;
87 if (flags & BTRFS_INODE_DIRSYNC)
88 iflags |= FS_DIRSYNC_FL;
89 if (flags & BTRFS_INODE_NODATACOW)
90 iflags |= FS_NOCOW_FL;
92 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
93 iflags |= FS_COMPR_FL;
94 else if (flags & BTRFS_INODE_NOCOMPRESS)
95 iflags |= FS_NOCOMP_FL;
101 * Update inode->i_flags based on the btrfs internal flags.
103 void btrfs_update_iflags(struct inode *inode)
105 struct btrfs_inode *ip = BTRFS_I(inode);
107 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
109 if (ip->flags & BTRFS_INODE_SYNC)
110 inode->i_flags |= S_SYNC;
111 if (ip->flags & BTRFS_INODE_IMMUTABLE)
112 inode->i_flags |= S_IMMUTABLE;
113 if (ip->flags & BTRFS_INODE_APPEND)
114 inode->i_flags |= S_APPEND;
115 if (ip->flags & BTRFS_INODE_NOATIME)
116 inode->i_flags |= S_NOATIME;
117 if (ip->flags & BTRFS_INODE_DIRSYNC)
118 inode->i_flags |= S_DIRSYNC;
122 * Inherit flags from the parent inode.
124 * Currently only the compression flags and the cow flags are inherited.
126 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
133 flags = BTRFS_I(dir)->flags;
135 if (flags & BTRFS_INODE_NOCOMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
138 } else if (flags & BTRFS_INODE_COMPRESS) {
139 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
140 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
143 if (flags & BTRFS_INODE_NODATACOW)
144 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
146 btrfs_update_iflags(inode);
149 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
151 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
152 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
154 if (copy_to_user(arg, &flags, sizeof(flags)))
159 static int check_flags(unsigned int flags)
161 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
162 FS_NOATIME_FL | FS_NODUMP_FL | \
163 FS_SYNC_FL | FS_DIRSYNC_FL | \
164 FS_NOCOMP_FL | FS_COMPR_FL |
168 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
174 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
176 struct inode *inode = file->f_path.dentry->d_inode;
177 struct btrfs_inode *ip = BTRFS_I(inode);
178 struct btrfs_root *root = ip->root;
179 struct btrfs_trans_handle *trans;
180 unsigned int flags, oldflags;
183 unsigned int i_oldflags;
185 if (btrfs_root_readonly(root))
188 if (copy_from_user(&flags, arg, sizeof(flags)))
191 ret = check_flags(flags);
195 if (!inode_owner_or_capable(inode))
198 ret = mnt_want_write_file(file);
202 mutex_lock(&inode->i_mutex);
204 ip_oldflags = ip->flags;
205 i_oldflags = inode->i_flags;
207 flags = btrfs_mask_flags(inode->i_mode, flags);
208 oldflags = btrfs_flags_to_ioctl(ip->flags);
209 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
210 if (!capable(CAP_LINUX_IMMUTABLE)) {
216 if (flags & FS_SYNC_FL)
217 ip->flags |= BTRFS_INODE_SYNC;
219 ip->flags &= ~BTRFS_INODE_SYNC;
220 if (flags & FS_IMMUTABLE_FL)
221 ip->flags |= BTRFS_INODE_IMMUTABLE;
223 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
224 if (flags & FS_APPEND_FL)
225 ip->flags |= BTRFS_INODE_APPEND;
227 ip->flags &= ~BTRFS_INODE_APPEND;
228 if (flags & FS_NODUMP_FL)
229 ip->flags |= BTRFS_INODE_NODUMP;
231 ip->flags &= ~BTRFS_INODE_NODUMP;
232 if (flags & FS_NOATIME_FL)
233 ip->flags |= BTRFS_INODE_NOATIME;
235 ip->flags &= ~BTRFS_INODE_NOATIME;
236 if (flags & FS_DIRSYNC_FL)
237 ip->flags |= BTRFS_INODE_DIRSYNC;
239 ip->flags &= ~BTRFS_INODE_DIRSYNC;
240 if (flags & FS_NOCOW_FL)
241 ip->flags |= BTRFS_INODE_NODATACOW;
243 ip->flags &= ~BTRFS_INODE_NODATACOW;
246 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
247 * flag may be changed automatically if compression code won't make
250 if (flags & FS_NOCOMP_FL) {
251 ip->flags &= ~BTRFS_INODE_COMPRESS;
252 ip->flags |= BTRFS_INODE_NOCOMPRESS;
253 } else if (flags & FS_COMPR_FL) {
254 ip->flags |= BTRFS_INODE_COMPRESS;
255 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
257 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
260 trans = btrfs_start_transaction(root, 1);
262 ret = PTR_ERR(trans);
266 btrfs_update_iflags(inode);
267 inode_inc_iversion(inode);
268 inode->i_ctime = CURRENT_TIME;
269 ret = btrfs_update_inode(trans, root, inode);
271 btrfs_end_transaction(trans, root);
274 ip->flags = ip_oldflags;
275 inode->i_flags = i_oldflags;
279 mutex_unlock(&inode->i_mutex);
280 mnt_drop_write_file(file);
284 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
286 struct inode *inode = file->f_path.dentry->d_inode;
288 return put_user(inode->i_generation, arg);
291 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
293 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
294 struct btrfs_device *device;
295 struct request_queue *q;
296 struct fstrim_range range;
297 u64 minlen = ULLONG_MAX;
299 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
302 if (!capable(CAP_SYS_ADMIN))
306 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
310 q = bdev_get_queue(device->bdev);
311 if (blk_queue_discard(q)) {
313 minlen = min((u64)q->limits.discard_granularity,
321 if (copy_from_user(&range, arg, sizeof(range)))
323 if (range.start > total_bytes)
326 range.len = min(range.len, total_bytes - range.start);
327 range.minlen = max(range.minlen, minlen);
328 ret = btrfs_trim_fs(fs_info->tree_root, &range);
332 if (copy_to_user(arg, &range, sizeof(range)))
338 static noinline int create_subvol(struct btrfs_root *root,
339 struct dentry *dentry,
340 char *name, int namelen,
342 struct btrfs_qgroup_inherit **inherit)
344 struct btrfs_trans_handle *trans;
345 struct btrfs_key key;
346 struct btrfs_root_item root_item;
347 struct btrfs_inode_item *inode_item;
348 struct extent_buffer *leaf;
349 struct btrfs_root *new_root;
350 struct dentry *parent = dentry->d_parent;
352 struct timespec cur_time = CURRENT_TIME;
356 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
360 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
364 dir = parent->d_inode;
372 trans = btrfs_start_transaction(root, 6);
374 return PTR_ERR(trans);
376 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
377 inherit ? *inherit : NULL);
381 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
382 0, objectid, NULL, 0, 0, 0);
388 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
389 btrfs_set_header_bytenr(leaf, leaf->start);
390 btrfs_set_header_generation(leaf, trans->transid);
391 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
392 btrfs_set_header_owner(leaf, objectid);
394 write_extent_buffer(leaf, root->fs_info->fsid,
395 (unsigned long)btrfs_header_fsid(leaf),
397 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
398 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
400 btrfs_mark_buffer_dirty(leaf);
402 memset(&root_item, 0, sizeof(root_item));
404 inode_item = &root_item.inode;
405 inode_item->generation = cpu_to_le64(1);
406 inode_item->size = cpu_to_le64(3);
407 inode_item->nlink = cpu_to_le32(1);
408 inode_item->nbytes = cpu_to_le64(root->leafsize);
409 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
412 root_item.byte_limit = 0;
413 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
415 btrfs_set_root_bytenr(&root_item, leaf->start);
416 btrfs_set_root_generation(&root_item, trans->transid);
417 btrfs_set_root_level(&root_item, 0);
418 btrfs_set_root_refs(&root_item, 1);
419 btrfs_set_root_used(&root_item, leaf->len);
420 btrfs_set_root_last_snapshot(&root_item, 0);
422 btrfs_set_root_generation_v2(&root_item,
423 btrfs_root_generation(&root_item));
424 uuid_le_gen(&new_uuid);
425 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
426 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
427 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
428 root_item.ctime = root_item.otime;
429 btrfs_set_root_ctransid(&root_item, trans->transid);
430 btrfs_set_root_otransid(&root_item, trans->transid);
432 btrfs_tree_unlock(leaf);
433 free_extent_buffer(leaf);
436 btrfs_set_root_dirid(&root_item, new_dirid);
438 key.objectid = objectid;
440 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
441 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
446 key.offset = (u64)-1;
447 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
448 if (IS_ERR(new_root)) {
449 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
450 ret = PTR_ERR(new_root);
454 btrfs_record_root_in_trans(trans, new_root);
456 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
458 /* We potentially lose an unused inode item here */
459 btrfs_abort_transaction(trans, root, ret);
464 * insert the directory item
466 ret = btrfs_set_inode_index(dir, &index);
468 btrfs_abort_transaction(trans, root, ret);
472 ret = btrfs_insert_dir_item(trans, root,
473 name, namelen, dir, &key,
474 BTRFS_FT_DIR, index);
476 btrfs_abort_transaction(trans, root, ret);
480 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
481 ret = btrfs_update_inode(trans, root, dir);
484 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
485 objectid, root->root_key.objectid,
486 btrfs_ino(dir), index, name, namelen);
490 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
493 *async_transid = trans->transid;
494 err = btrfs_commit_transaction_async(trans, root, 1);
496 err = btrfs_commit_transaction(trans, root);
503 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
504 char *name, int namelen, u64 *async_transid,
505 bool readonly, struct btrfs_qgroup_inherit **inherit)
508 struct btrfs_pending_snapshot *pending_snapshot;
509 struct btrfs_trans_handle *trans;
515 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
516 if (!pending_snapshot)
519 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
520 BTRFS_BLOCK_RSV_TEMP);
521 pending_snapshot->dentry = dentry;
522 pending_snapshot->root = root;
523 pending_snapshot->readonly = readonly;
525 pending_snapshot->inherit = *inherit;
526 *inherit = NULL; /* take responsibility to free it */
529 trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
531 ret = PTR_ERR(trans);
535 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
538 spin_lock(&root->fs_info->trans_lock);
539 list_add(&pending_snapshot->list,
540 &trans->transaction->pending_snapshots);
541 spin_unlock(&root->fs_info->trans_lock);
543 *async_transid = trans->transid;
544 ret = btrfs_commit_transaction_async(trans,
545 root->fs_info->extent_root, 1);
547 ret = btrfs_commit_transaction(trans,
548 root->fs_info->extent_root);
552 ret = pending_snapshot->error;
556 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
560 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
562 ret = PTR_ERR(inode);
566 d_instantiate(dentry, inode);
569 kfree(pending_snapshot);
573 /* copy of check_sticky in fs/namei.c()
574 * It's inline, so penalty for filesystems that don't use sticky bit is
577 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
579 uid_t fsuid = current_fsuid();
581 if (!(dir->i_mode & S_ISVTX))
583 if (inode->i_uid == fsuid)
585 if (dir->i_uid == fsuid)
587 return !capable(CAP_FOWNER);
590 /* copy of may_delete in fs/namei.c()
591 * Check whether we can remove a link victim from directory dir, check
592 * whether the type of victim is right.
593 * 1. We can't do it if dir is read-only (done in permission())
594 * 2. We should have write and exec permissions on dir
595 * 3. We can't remove anything from append-only dir
596 * 4. We can't do anything with immutable dir (done in permission())
597 * 5. If the sticky bit on dir is set we should either
598 * a. be owner of dir, or
599 * b. be owner of victim, or
600 * c. have CAP_FOWNER capability
601 * 6. If the victim is append-only or immutable we can't do antyhing with
602 * links pointing to it.
603 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
604 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
605 * 9. We can't remove a root or mountpoint.
606 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
607 * nfs_async_unlink().
610 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
614 if (!victim->d_inode)
617 BUG_ON(victim->d_parent->d_inode != dir);
618 audit_inode_child(victim, dir);
620 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
625 if (btrfs_check_sticky(dir, victim->d_inode)||
626 IS_APPEND(victim->d_inode)||
627 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
630 if (!S_ISDIR(victim->d_inode->i_mode))
634 } else if (S_ISDIR(victim->d_inode->i_mode))
638 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
643 /* copy of may_create in fs/namei.c() */
644 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
650 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
654 * Create a new subvolume below @parent. This is largely modeled after
655 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
656 * inside this filesystem so it's quite a bit simpler.
658 static noinline int btrfs_mksubvol(struct path *parent,
659 char *name, int namelen,
660 struct btrfs_root *snap_src,
661 u64 *async_transid, bool readonly,
662 struct btrfs_qgroup_inherit **inherit)
664 struct inode *dir = parent->dentry->d_inode;
665 struct dentry *dentry;
668 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
670 dentry = lookup_one_len(name, parent->dentry, namelen);
671 error = PTR_ERR(dentry);
679 error = btrfs_may_create(dir, dentry);
683 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
685 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
689 error = create_snapshot(snap_src, dentry, name, namelen,
690 async_transid, readonly, inherit);
692 error = create_subvol(BTRFS_I(dir)->root, dentry,
693 name, namelen, async_transid, inherit);
696 fsnotify_mkdir(dir, dentry);
698 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
702 mutex_unlock(&dir->i_mutex);
707 * When we're defragging a range, we don't want to kick it off again
708 * if it is really just waiting for delalloc to send it down.
709 * If we find a nice big extent or delalloc range for the bytes in the
710 * file you want to defrag, we return 0 to let you know to skip this
713 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
715 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
716 struct extent_map *em = NULL;
717 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
720 read_lock(&em_tree->lock);
721 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
722 read_unlock(&em_tree->lock);
725 end = extent_map_end(em);
727 if (end - offset > thresh)
730 /* if we already have a nice delalloc here, just stop */
732 end = count_range_bits(io_tree, &offset, offset + thresh,
733 thresh, EXTENT_DELALLOC, 1);
740 * helper function to walk through a file and find extents
741 * newer than a specific transid, and smaller than thresh.
743 * This is used by the defragging code to find new and small
746 static int find_new_extents(struct btrfs_root *root,
747 struct inode *inode, u64 newer_than,
748 u64 *off, int thresh)
750 struct btrfs_path *path;
751 struct btrfs_key min_key;
752 struct btrfs_key max_key;
753 struct extent_buffer *leaf;
754 struct btrfs_file_extent_item *extent;
757 u64 ino = btrfs_ino(inode);
759 path = btrfs_alloc_path();
763 min_key.objectid = ino;
764 min_key.type = BTRFS_EXTENT_DATA_KEY;
765 min_key.offset = *off;
767 max_key.objectid = ino;
768 max_key.type = (u8)-1;
769 max_key.offset = (u64)-1;
771 path->keep_locks = 1;
774 ret = btrfs_search_forward(root, &min_key, &max_key,
775 path, 0, newer_than);
778 if (min_key.objectid != ino)
780 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
783 leaf = path->nodes[0];
784 extent = btrfs_item_ptr(leaf, path->slots[0],
785 struct btrfs_file_extent_item);
787 type = btrfs_file_extent_type(leaf, extent);
788 if (type == BTRFS_FILE_EXTENT_REG &&
789 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
790 check_defrag_in_cache(inode, min_key.offset, thresh)) {
791 *off = min_key.offset;
792 btrfs_free_path(path);
796 if (min_key.offset == (u64)-1)
800 btrfs_release_path(path);
803 btrfs_free_path(path);
807 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
809 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
810 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
811 struct extent_map *em;
812 u64 len = PAGE_CACHE_SIZE;
815 * hopefully we have this extent in the tree already, try without
816 * the full extent lock
818 read_lock(&em_tree->lock);
819 em = lookup_extent_mapping(em_tree, start, len);
820 read_unlock(&em_tree->lock);
823 /* get the big lock and read metadata off disk */
824 lock_extent(io_tree, start, start + len - 1);
825 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
826 unlock_extent(io_tree, start, start + len - 1);
835 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
837 struct extent_map *next;
840 /* this is the last extent */
841 if (em->start + em->len >= i_size_read(inode))
844 next = defrag_lookup_extent(inode, em->start + em->len);
845 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
848 free_extent_map(next);
852 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
853 u64 *last_len, u64 *skip, u64 *defrag_end,
856 struct extent_map *em;
858 bool next_mergeable = true;
861 * make sure that once we start defragging an extent, we keep on
864 if (start < *defrag_end)
869 em = defrag_lookup_extent(inode, start);
873 /* this will cover holes, and inline extents */
874 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
879 next_mergeable = defrag_check_next_extent(inode, em);
882 * we hit a real extent, if it is big or the next extent is not a
883 * real extent, don't bother defragging it
885 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
886 (em->len >= thresh || !next_mergeable))
890 * last_len ends up being a counter of how many bytes we've defragged.
891 * every time we choose not to defrag an extent, we reset *last_len
892 * so that the next tiny extent will force a defrag.
894 * The end result of this is that tiny extents before a single big
895 * extent will force at least part of that big extent to be defragged.
898 *defrag_end = extent_map_end(em);
901 *skip = extent_map_end(em);
910 * it doesn't do much good to defrag one or two pages
911 * at a time. This pulls in a nice chunk of pages
914 * It also makes sure the delalloc code has enough
915 * dirty data to avoid making new small extents as part
918 * It's a good idea to start RA on this range
919 * before calling this.
921 static int cluster_pages_for_defrag(struct inode *inode,
923 unsigned long start_index,
926 unsigned long file_end;
927 u64 isize = i_size_read(inode);
934 struct btrfs_ordered_extent *ordered;
935 struct extent_state *cached_state = NULL;
936 struct extent_io_tree *tree;
937 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
939 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
940 if (!isize || start_index > file_end)
943 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
945 ret = btrfs_delalloc_reserve_space(inode,
946 page_cnt << PAGE_CACHE_SHIFT);
950 tree = &BTRFS_I(inode)->io_tree;
952 /* step one, lock all the pages */
953 for (i = 0; i < page_cnt; i++) {
956 page = find_or_create_page(inode->i_mapping,
957 start_index + i, mask);
961 page_start = page_offset(page);
962 page_end = page_start + PAGE_CACHE_SIZE - 1;
964 lock_extent(tree, page_start, page_end);
965 ordered = btrfs_lookup_ordered_extent(inode,
967 unlock_extent(tree, page_start, page_end);
972 btrfs_start_ordered_extent(inode, ordered, 1);
973 btrfs_put_ordered_extent(ordered);
976 * we unlocked the page above, so we need check if
977 * it was released or not.
979 if (page->mapping != inode->i_mapping) {
981 page_cache_release(page);
986 if (!PageUptodate(page)) {
987 btrfs_readpage(NULL, page);
989 if (!PageUptodate(page)) {
991 page_cache_release(page);
997 if (page->mapping != inode->i_mapping) {
999 page_cache_release(page);
1009 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1013 * so now we have a nice long stream of locked
1014 * and up to date pages, lets wait on them
1016 for (i = 0; i < i_done; i++)
1017 wait_on_page_writeback(pages[i]);
1019 page_start = page_offset(pages[0]);
1020 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1022 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1023 page_start, page_end - 1, 0, &cached_state);
1024 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1025 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1026 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1027 &cached_state, GFP_NOFS);
1029 if (i_done != page_cnt) {
1030 spin_lock(&BTRFS_I(inode)->lock);
1031 BTRFS_I(inode)->outstanding_extents++;
1032 spin_unlock(&BTRFS_I(inode)->lock);
1033 btrfs_delalloc_release_space(inode,
1034 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1038 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1039 &cached_state, GFP_NOFS);
1041 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1042 page_start, page_end - 1, &cached_state,
1045 for (i = 0; i < i_done; i++) {
1046 clear_page_dirty_for_io(pages[i]);
1047 ClearPageChecked(pages[i]);
1048 set_page_extent_mapped(pages[i]);
1049 set_page_dirty(pages[i]);
1050 unlock_page(pages[i]);
1051 page_cache_release(pages[i]);
1055 for (i = 0; i < i_done; i++) {
1056 unlock_page(pages[i]);
1057 page_cache_release(pages[i]);
1059 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1064 int btrfs_defrag_file(struct inode *inode, struct file *file,
1065 struct btrfs_ioctl_defrag_range_args *range,
1066 u64 newer_than, unsigned long max_to_defrag)
1068 struct btrfs_root *root = BTRFS_I(inode)->root;
1069 struct file_ra_state *ra = NULL;
1070 unsigned long last_index;
1071 u64 isize = i_size_read(inode);
1075 u64 newer_off = range->start;
1077 unsigned long ra_index = 0;
1079 int defrag_count = 0;
1080 int compress_type = BTRFS_COMPRESS_ZLIB;
1081 int extent_thresh = range->extent_thresh;
1082 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1083 int cluster = max_cluster;
1084 u64 new_align = ~((u64)128 * 1024 - 1);
1085 struct page **pages = NULL;
1087 if (extent_thresh == 0)
1088 extent_thresh = 256 * 1024;
1090 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1091 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1093 if (range->compress_type)
1094 compress_type = range->compress_type;
1101 * if we were not given a file, allocate a readahead
1105 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1108 file_ra_state_init(ra, inode->i_mapping);
1113 pages = kmalloc(sizeof(struct page *) * max_cluster,
1120 /* find the last page to defrag */
1121 if (range->start + range->len > range->start) {
1122 last_index = min_t(u64, isize - 1,
1123 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1125 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1129 ret = find_new_extents(root, inode, newer_than,
1130 &newer_off, 64 * 1024);
1132 range->start = newer_off;
1134 * we always align our defrag to help keep
1135 * the extents in the file evenly spaced
1137 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1141 i = range->start >> PAGE_CACHE_SHIFT;
1144 max_to_defrag = last_index + 1;
1147 * make writeback starts from i, so the defrag range can be
1148 * written sequentially.
1150 if (i < inode->i_mapping->writeback_index)
1151 inode->i_mapping->writeback_index = i;
1153 while (i <= last_index && defrag_count < max_to_defrag &&
1154 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1155 PAGE_CACHE_SHIFT)) {
1157 * make sure we stop running if someone unmounts
1160 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1163 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1164 extent_thresh, &last_len, &skip,
1165 &defrag_end, range->flags &
1166 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1169 * the should_defrag function tells us how much to skip
1170 * bump our counter by the suggested amount
1172 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1173 i = max(i + 1, next);
1178 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1179 PAGE_CACHE_SHIFT) - i;
1180 cluster = min(cluster, max_cluster);
1182 cluster = max_cluster;
1185 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1186 BTRFS_I(inode)->force_compress = compress_type;
1188 if (i + cluster > ra_index) {
1189 ra_index = max(i, ra_index);
1190 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1192 ra_index += max_cluster;
1195 mutex_lock(&inode->i_mutex);
1196 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1198 mutex_unlock(&inode->i_mutex);
1202 defrag_count += ret;
1203 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1204 mutex_unlock(&inode->i_mutex);
1207 if (newer_off == (u64)-1)
1213 newer_off = max(newer_off + 1,
1214 (u64)i << PAGE_CACHE_SHIFT);
1216 ret = find_new_extents(root, inode,
1217 newer_than, &newer_off,
1220 range->start = newer_off;
1221 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1228 last_len += ret << PAGE_CACHE_SHIFT;
1236 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1237 filemap_flush(inode->i_mapping);
1239 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1240 /* the filemap_flush will queue IO into the worker threads, but
1241 * we have to make sure the IO is actually started and that
1242 * ordered extents get created before we return
1244 atomic_inc(&root->fs_info->async_submit_draining);
1245 while (atomic_read(&root->fs_info->nr_async_submits) ||
1246 atomic_read(&root->fs_info->async_delalloc_pages)) {
1247 wait_event(root->fs_info->async_submit_wait,
1248 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1249 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1251 atomic_dec(&root->fs_info->async_submit_draining);
1253 mutex_lock(&inode->i_mutex);
1254 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1255 mutex_unlock(&inode->i_mutex);
1258 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1259 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1271 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1277 struct btrfs_ioctl_vol_args *vol_args;
1278 struct btrfs_trans_handle *trans;
1279 struct btrfs_device *device = NULL;
1281 char *devstr = NULL;
1285 if (root->fs_info->sb->s_flags & MS_RDONLY)
1288 if (!capable(CAP_SYS_ADMIN))
1291 mutex_lock(&root->fs_info->volume_mutex);
1292 if (root->fs_info->balance_ctl) {
1293 printk(KERN_INFO "btrfs: balance in progress\n");
1298 vol_args = memdup_user(arg, sizeof(*vol_args));
1299 if (IS_ERR(vol_args)) {
1300 ret = PTR_ERR(vol_args);
1304 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1306 sizestr = vol_args->name;
1307 devstr = strchr(sizestr, ':');
1310 sizestr = devstr + 1;
1312 devstr = vol_args->name;
1313 devid = simple_strtoull(devstr, &end, 10);
1314 printk(KERN_INFO "btrfs: resizing devid %llu\n",
1315 (unsigned long long)devid);
1317 device = btrfs_find_device(root, devid, NULL, NULL);
1319 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1320 (unsigned long long)devid);
1324 if (device->fs_devices && device->fs_devices->seeding) {
1325 printk(KERN_INFO "btrfs: resizer unable to apply on "
1326 "seeding device %llu\n",
1327 (unsigned long long)devid);
1332 if (!strcmp(sizestr, "max"))
1333 new_size = device->bdev->bd_inode->i_size;
1335 if (sizestr[0] == '-') {
1338 } else if (sizestr[0] == '+') {
1342 new_size = memparse(sizestr, NULL);
1343 if (new_size == 0) {
1349 old_size = device->total_bytes;
1352 if (new_size > old_size) {
1356 new_size = old_size - new_size;
1357 } else if (mod > 0) {
1358 new_size = old_size + new_size;
1361 if (new_size < 256 * 1024 * 1024) {
1365 if (new_size > device->bdev->bd_inode->i_size) {
1370 do_div(new_size, root->sectorsize);
1371 new_size *= root->sectorsize;
1373 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1374 rcu_str_deref(device->name),
1375 (unsigned long long)new_size);
1377 if (new_size > old_size) {
1378 trans = btrfs_start_transaction(root, 0);
1379 if (IS_ERR(trans)) {
1380 ret = PTR_ERR(trans);
1383 ret = btrfs_grow_device(trans, device, new_size);
1384 btrfs_commit_transaction(trans, root);
1385 } else if (new_size < old_size) {
1386 ret = btrfs_shrink_device(device, new_size);
1392 mutex_unlock(&root->fs_info->volume_mutex);
1396 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1397 char *name, unsigned long fd, int subvol,
1398 u64 *transid, bool readonly,
1399 struct btrfs_qgroup_inherit **inherit)
1401 struct file *src_file;
1405 ret = mnt_want_write_file(file);
1409 namelen = strlen(name);
1410 if (strchr(name, '/')) {
1412 goto out_drop_write;
1415 if (name[0] == '.' &&
1416 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1418 goto out_drop_write;
1422 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1423 NULL, transid, readonly, inherit);
1425 struct inode *src_inode;
1426 src_file = fget(fd);
1429 goto out_drop_write;
1432 src_inode = src_file->f_path.dentry->d_inode;
1433 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1434 printk(KERN_INFO "btrfs: Snapshot src from "
1438 goto out_drop_write;
1440 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1441 BTRFS_I(src_inode)->root,
1442 transid, readonly, inherit);
1446 mnt_drop_write_file(file);
1451 static noinline int btrfs_ioctl_snap_create(struct file *file,
1452 void __user *arg, int subvol)
1454 struct btrfs_ioctl_vol_args *vol_args;
1457 vol_args = memdup_user(arg, sizeof(*vol_args));
1458 if (IS_ERR(vol_args))
1459 return PTR_ERR(vol_args);
1460 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1462 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1463 vol_args->fd, subvol,
1470 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1471 void __user *arg, int subvol)
1473 struct btrfs_ioctl_vol_args_v2 *vol_args;
1477 bool readonly = false;
1478 struct btrfs_qgroup_inherit *inherit = NULL;
1480 vol_args = memdup_user(arg, sizeof(*vol_args));
1481 if (IS_ERR(vol_args))
1482 return PTR_ERR(vol_args);
1483 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1485 if (vol_args->flags &
1486 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1487 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1492 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1494 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1496 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1497 if (vol_args->size > PAGE_CACHE_SIZE) {
1501 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1502 if (IS_ERR(inherit)) {
1503 ret = PTR_ERR(inherit);
1508 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1509 vol_args->fd, subvol, ptr,
1510 readonly, &inherit);
1512 if (ret == 0 && ptr &&
1514 offsetof(struct btrfs_ioctl_vol_args_v2,
1515 transid), ptr, sizeof(*ptr)))
1523 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1526 struct inode *inode = fdentry(file)->d_inode;
1527 struct btrfs_root *root = BTRFS_I(inode)->root;
1531 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1534 down_read(&root->fs_info->subvol_sem);
1535 if (btrfs_root_readonly(root))
1536 flags |= BTRFS_SUBVOL_RDONLY;
1537 up_read(&root->fs_info->subvol_sem);
1539 if (copy_to_user(arg, &flags, sizeof(flags)))
1545 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1548 struct inode *inode = fdentry(file)->d_inode;
1549 struct btrfs_root *root = BTRFS_I(inode)->root;
1550 struct btrfs_trans_handle *trans;
1555 ret = mnt_want_write_file(file);
1559 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1561 goto out_drop_write;
1564 if (copy_from_user(&flags, arg, sizeof(flags))) {
1566 goto out_drop_write;
1569 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1571 goto out_drop_write;
1574 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1576 goto out_drop_write;
1579 if (!inode_owner_or_capable(inode)) {
1581 goto out_drop_write;
1584 down_write(&root->fs_info->subvol_sem);
1587 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1590 root_flags = btrfs_root_flags(&root->root_item);
1591 if (flags & BTRFS_SUBVOL_RDONLY)
1592 btrfs_set_root_flags(&root->root_item,
1593 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1595 btrfs_set_root_flags(&root->root_item,
1596 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1598 trans = btrfs_start_transaction(root, 1);
1599 if (IS_ERR(trans)) {
1600 ret = PTR_ERR(trans);
1604 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1605 &root->root_key, &root->root_item);
1607 btrfs_commit_transaction(trans, root);
1610 btrfs_set_root_flags(&root->root_item, root_flags);
1612 up_write(&root->fs_info->subvol_sem);
1614 mnt_drop_write_file(file);
1620 * helper to check if the subvolume references other subvolumes
1622 static noinline int may_destroy_subvol(struct btrfs_root *root)
1624 struct btrfs_path *path;
1625 struct btrfs_key key;
1628 path = btrfs_alloc_path();
1632 key.objectid = root->root_key.objectid;
1633 key.type = BTRFS_ROOT_REF_KEY;
1634 key.offset = (u64)-1;
1636 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1643 if (path->slots[0] > 0) {
1645 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1646 if (key.objectid == root->root_key.objectid &&
1647 key.type == BTRFS_ROOT_REF_KEY)
1651 btrfs_free_path(path);
1655 static noinline int key_in_sk(struct btrfs_key *key,
1656 struct btrfs_ioctl_search_key *sk)
1658 struct btrfs_key test;
1661 test.objectid = sk->min_objectid;
1662 test.type = sk->min_type;
1663 test.offset = sk->min_offset;
1665 ret = btrfs_comp_cpu_keys(key, &test);
1669 test.objectid = sk->max_objectid;
1670 test.type = sk->max_type;
1671 test.offset = sk->max_offset;
1673 ret = btrfs_comp_cpu_keys(key, &test);
1679 static noinline int copy_to_sk(struct btrfs_root *root,
1680 struct btrfs_path *path,
1681 struct btrfs_key *key,
1682 struct btrfs_ioctl_search_key *sk,
1684 unsigned long *sk_offset,
1688 struct extent_buffer *leaf;
1689 struct btrfs_ioctl_search_header sh;
1690 unsigned long item_off;
1691 unsigned long item_len;
1697 leaf = path->nodes[0];
1698 slot = path->slots[0];
1699 nritems = btrfs_header_nritems(leaf);
1701 if (btrfs_header_generation(leaf) > sk->max_transid) {
1705 found_transid = btrfs_header_generation(leaf);
1707 for (i = slot; i < nritems; i++) {
1708 item_off = btrfs_item_ptr_offset(leaf, i);
1709 item_len = btrfs_item_size_nr(leaf, i);
1711 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1714 if (sizeof(sh) + item_len + *sk_offset >
1715 BTRFS_SEARCH_ARGS_BUFSIZE) {
1720 btrfs_item_key_to_cpu(leaf, key, i);
1721 if (!key_in_sk(key, sk))
1724 sh.objectid = key->objectid;
1725 sh.offset = key->offset;
1726 sh.type = key->type;
1728 sh.transid = found_transid;
1730 /* copy search result header */
1731 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1732 *sk_offset += sizeof(sh);
1735 char *p = buf + *sk_offset;
1737 read_extent_buffer(leaf, p,
1738 item_off, item_len);
1739 *sk_offset += item_len;
1743 if (*num_found >= sk->nr_items)
1748 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1750 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1753 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1763 static noinline int search_ioctl(struct inode *inode,
1764 struct btrfs_ioctl_search_args *args)
1766 struct btrfs_root *root;
1767 struct btrfs_key key;
1768 struct btrfs_key max_key;
1769 struct btrfs_path *path;
1770 struct btrfs_ioctl_search_key *sk = &args->key;
1771 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1774 unsigned long sk_offset = 0;
1776 path = btrfs_alloc_path();
1780 if (sk->tree_id == 0) {
1781 /* search the root of the inode that was passed */
1782 root = BTRFS_I(inode)->root;
1784 key.objectid = sk->tree_id;
1785 key.type = BTRFS_ROOT_ITEM_KEY;
1786 key.offset = (u64)-1;
1787 root = btrfs_read_fs_root_no_name(info, &key);
1789 printk(KERN_ERR "could not find root %llu\n",
1791 btrfs_free_path(path);
1796 key.objectid = sk->min_objectid;
1797 key.type = sk->min_type;
1798 key.offset = sk->min_offset;
1800 max_key.objectid = sk->max_objectid;
1801 max_key.type = sk->max_type;
1802 max_key.offset = sk->max_offset;
1804 path->keep_locks = 1;
1807 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1814 ret = copy_to_sk(root, path, &key, sk, args->buf,
1815 &sk_offset, &num_found);
1816 btrfs_release_path(path);
1817 if (ret || num_found >= sk->nr_items)
1823 sk->nr_items = num_found;
1824 btrfs_free_path(path);
1828 static noinline int btrfs_ioctl_tree_search(struct file *file,
1831 struct btrfs_ioctl_search_args *args;
1832 struct inode *inode;
1835 if (!capable(CAP_SYS_ADMIN))
1838 args = memdup_user(argp, sizeof(*args));
1840 return PTR_ERR(args);
1842 inode = fdentry(file)->d_inode;
1843 ret = search_ioctl(inode, args);
1844 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1851 * Search INODE_REFs to identify path name of 'dirid' directory
1852 * in a 'tree_id' tree. and sets path name to 'name'.
1854 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1855 u64 tree_id, u64 dirid, char *name)
1857 struct btrfs_root *root;
1858 struct btrfs_key key;
1864 struct btrfs_inode_ref *iref;
1865 struct extent_buffer *l;
1866 struct btrfs_path *path;
1868 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1873 path = btrfs_alloc_path();
1877 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1879 key.objectid = tree_id;
1880 key.type = BTRFS_ROOT_ITEM_KEY;
1881 key.offset = (u64)-1;
1882 root = btrfs_read_fs_root_no_name(info, &key);
1884 printk(KERN_ERR "could not find root %llu\n", tree_id);
1889 key.objectid = dirid;
1890 key.type = BTRFS_INODE_REF_KEY;
1891 key.offset = (u64)-1;
1894 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1899 slot = path->slots[0];
1900 if (ret > 0 && slot > 0)
1902 btrfs_item_key_to_cpu(l, &key, slot);
1904 if (ret > 0 && (key.objectid != dirid ||
1905 key.type != BTRFS_INODE_REF_KEY)) {
1910 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1911 len = btrfs_inode_ref_name_len(l, iref);
1913 total_len += len + 1;
1918 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1920 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1923 btrfs_release_path(path);
1924 key.objectid = key.offset;
1925 key.offset = (u64)-1;
1926 dirid = key.objectid;
1930 memmove(name, ptr, total_len);
1931 name[total_len]='\0';
1934 btrfs_free_path(path);
1938 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1941 struct btrfs_ioctl_ino_lookup_args *args;
1942 struct inode *inode;
1945 if (!capable(CAP_SYS_ADMIN))
1948 args = memdup_user(argp, sizeof(*args));
1950 return PTR_ERR(args);
1952 inode = fdentry(file)->d_inode;
1954 if (args->treeid == 0)
1955 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1957 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1958 args->treeid, args->objectid,
1961 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1968 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1971 struct dentry *parent = fdentry(file);
1972 struct dentry *dentry;
1973 struct inode *dir = parent->d_inode;
1974 struct inode *inode;
1975 struct btrfs_root *root = BTRFS_I(dir)->root;
1976 struct btrfs_root *dest = NULL;
1977 struct btrfs_ioctl_vol_args *vol_args;
1978 struct btrfs_trans_handle *trans;
1983 vol_args = memdup_user(arg, sizeof(*vol_args));
1984 if (IS_ERR(vol_args))
1985 return PTR_ERR(vol_args);
1987 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1988 namelen = strlen(vol_args->name);
1989 if (strchr(vol_args->name, '/') ||
1990 strncmp(vol_args->name, "..", namelen) == 0) {
1995 err = mnt_want_write_file(file);
1999 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2000 dentry = lookup_one_len(vol_args->name, parent, namelen);
2001 if (IS_ERR(dentry)) {
2002 err = PTR_ERR(dentry);
2003 goto out_unlock_dir;
2006 if (!dentry->d_inode) {
2011 inode = dentry->d_inode;
2012 dest = BTRFS_I(inode)->root;
2013 if (!capable(CAP_SYS_ADMIN)){
2015 * Regular user. Only allow this with a special mount
2016 * option, when the user has write+exec access to the
2017 * subvol root, and when rmdir(2) would have been
2020 * Note that this is _not_ check that the subvol is
2021 * empty or doesn't contain data that we wouldn't
2022 * otherwise be able to delete.
2024 * Users who want to delete empty subvols should try
2028 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2032 * Do not allow deletion if the parent dir is the same
2033 * as the dir to be deleted. That means the ioctl
2034 * must be called on the dentry referencing the root
2035 * of the subvol, not a random directory contained
2042 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2046 /* check if subvolume may be deleted by a non-root user */
2047 err = btrfs_may_delete(dir, dentry, 1);
2052 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2057 mutex_lock(&inode->i_mutex);
2058 err = d_invalidate(dentry);
2062 down_write(&root->fs_info->subvol_sem);
2064 err = may_destroy_subvol(dest);
2068 trans = btrfs_start_transaction(root, 0);
2069 if (IS_ERR(trans)) {
2070 err = PTR_ERR(trans);
2073 trans->block_rsv = &root->fs_info->global_block_rsv;
2075 ret = btrfs_unlink_subvol(trans, root, dir,
2076 dest->root_key.objectid,
2077 dentry->d_name.name,
2078 dentry->d_name.len);
2081 btrfs_abort_transaction(trans, root, ret);
2085 btrfs_record_root_in_trans(trans, dest);
2087 memset(&dest->root_item.drop_progress, 0,
2088 sizeof(dest->root_item.drop_progress));
2089 dest->root_item.drop_level = 0;
2090 btrfs_set_root_refs(&dest->root_item, 0);
2092 if (!xchg(&dest->orphan_item_inserted, 1)) {
2093 ret = btrfs_insert_orphan_item(trans,
2094 root->fs_info->tree_root,
2095 dest->root_key.objectid);
2097 btrfs_abort_transaction(trans, root, ret);
2103 ret = btrfs_end_transaction(trans, root);
2106 inode->i_flags |= S_DEAD;
2108 up_write(&root->fs_info->subvol_sem);
2110 mutex_unlock(&inode->i_mutex);
2112 shrink_dcache_sb(root->fs_info->sb);
2113 btrfs_invalidate_inodes(dest);
2119 mutex_unlock(&dir->i_mutex);
2120 mnt_drop_write_file(file);
2126 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2128 struct inode *inode = fdentry(file)->d_inode;
2129 struct btrfs_root *root = BTRFS_I(inode)->root;
2130 struct btrfs_ioctl_defrag_range_args *range;
2133 if (btrfs_root_readonly(root))
2136 ret = mnt_want_write_file(file);
2140 switch (inode->i_mode & S_IFMT) {
2142 if (!capable(CAP_SYS_ADMIN)) {
2146 ret = btrfs_defrag_root(root, 0);
2149 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2152 if (!(file->f_mode & FMODE_WRITE)) {
2157 range = kzalloc(sizeof(*range), GFP_KERNEL);
2164 if (copy_from_user(range, argp,
2170 /* compression requires us to start the IO */
2171 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2172 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2173 range->extent_thresh = (u32)-1;
2176 /* the rest are all set to zero by kzalloc */
2177 range->len = (u64)-1;
2179 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2189 mnt_drop_write_file(file);
2193 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2195 struct btrfs_ioctl_vol_args *vol_args;
2198 if (!capable(CAP_SYS_ADMIN))
2201 mutex_lock(&root->fs_info->volume_mutex);
2202 if (root->fs_info->balance_ctl) {
2203 printk(KERN_INFO "btrfs: balance in progress\n");
2208 vol_args = memdup_user(arg, sizeof(*vol_args));
2209 if (IS_ERR(vol_args)) {
2210 ret = PTR_ERR(vol_args);
2214 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2215 ret = btrfs_init_new_device(root, vol_args->name);
2219 mutex_unlock(&root->fs_info->volume_mutex);
2223 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2225 struct btrfs_ioctl_vol_args *vol_args;
2228 if (!capable(CAP_SYS_ADMIN))
2231 if (root->fs_info->sb->s_flags & MS_RDONLY)
2234 mutex_lock(&root->fs_info->volume_mutex);
2235 if (root->fs_info->balance_ctl) {
2236 printk(KERN_INFO "btrfs: balance in progress\n");
2241 vol_args = memdup_user(arg, sizeof(*vol_args));
2242 if (IS_ERR(vol_args)) {
2243 ret = PTR_ERR(vol_args);
2247 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2248 ret = btrfs_rm_device(root, vol_args->name);
2252 mutex_unlock(&root->fs_info->volume_mutex);
2256 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2258 struct btrfs_ioctl_fs_info_args *fi_args;
2259 struct btrfs_device *device;
2260 struct btrfs_device *next;
2261 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2264 if (!capable(CAP_SYS_ADMIN))
2267 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2271 fi_args->num_devices = fs_devices->num_devices;
2272 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2274 mutex_lock(&fs_devices->device_list_mutex);
2275 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2276 if (device->devid > fi_args->max_id)
2277 fi_args->max_id = device->devid;
2279 mutex_unlock(&fs_devices->device_list_mutex);
2281 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2288 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2290 struct btrfs_ioctl_dev_info_args *di_args;
2291 struct btrfs_device *dev;
2292 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2294 char *s_uuid = NULL;
2295 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2297 if (!capable(CAP_SYS_ADMIN))
2300 di_args = memdup_user(arg, sizeof(*di_args));
2301 if (IS_ERR(di_args))
2302 return PTR_ERR(di_args);
2304 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2305 s_uuid = di_args->uuid;
2307 mutex_lock(&fs_devices->device_list_mutex);
2308 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2309 mutex_unlock(&fs_devices->device_list_mutex);
2316 di_args->devid = dev->devid;
2317 di_args->bytes_used = dev->bytes_used;
2318 di_args->total_bytes = dev->total_bytes;
2319 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2321 struct rcu_string *name;
2324 name = rcu_dereference(dev->name);
2325 strncpy(di_args->path, name->str, sizeof(di_args->path));
2327 di_args->path[sizeof(di_args->path) - 1] = 0;
2329 di_args->path[0] = '\0';
2333 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2340 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2341 u64 off, u64 olen, u64 destoff)
2343 struct inode *inode = fdentry(file)->d_inode;
2344 struct btrfs_root *root = BTRFS_I(inode)->root;
2345 struct file *src_file;
2347 struct btrfs_trans_handle *trans;
2348 struct btrfs_path *path;
2349 struct extent_buffer *leaf;
2351 struct btrfs_key key;
2356 u64 bs = root->fs_info->sb->s_blocksize;
2360 * - split compressed inline extents. annoying: we need to
2361 * decompress into destination's address_space (the file offset
2362 * may change, so source mapping won't do), then recompress (or
2363 * otherwise reinsert) a subrange.
2364 * - allow ranges within the same file to be cloned (provided
2365 * they don't overlap)?
2368 /* the destination must be opened for writing */
2369 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2372 if (btrfs_root_readonly(root))
2375 ret = mnt_want_write_file(file);
2379 src_file = fget(srcfd);
2382 goto out_drop_write;
2386 if (src_file->f_path.mnt != file->f_path.mnt)
2389 src = src_file->f_dentry->d_inode;
2395 /* the src must be open for reading */
2396 if (!(src_file->f_mode & FMODE_READ))
2399 /* don't make the dst file partly checksummed */
2400 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2401 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2405 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2409 if (src->i_sb != inode->i_sb)
2413 buf = vmalloc(btrfs_level_size(root, 0));
2417 path = btrfs_alloc_path();
2425 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2426 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2428 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2429 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2432 /* determine range to clone */
2434 if (off + len > src->i_size || off + len < off)
2437 olen = len = src->i_size - off;
2438 /* if we extend to eof, continue to block boundary */
2439 if (off + len == src->i_size)
2440 len = ALIGN(src->i_size, bs) - off;
2442 /* verify the end result is block aligned */
2443 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2444 !IS_ALIGNED(destoff, bs))
2447 if (destoff > inode->i_size) {
2448 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2453 /* truncate page cache pages from target inode range */
2454 truncate_inode_pages_range(&inode->i_data, destoff,
2455 PAGE_CACHE_ALIGN(destoff + len) - 1);
2457 /* do any pending delalloc/csum calc on src, one way or
2458 another, and lock file content */
2460 struct btrfs_ordered_extent *ordered;
2461 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2462 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2464 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2465 EXTENT_DELALLOC, 0, NULL))
2467 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2469 btrfs_put_ordered_extent(ordered);
2470 btrfs_wait_ordered_range(src, off, len);
2474 key.objectid = btrfs_ino(src);
2475 key.type = BTRFS_EXTENT_DATA_KEY;
2480 * note the key will change type as we walk through the
2483 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2488 nritems = btrfs_header_nritems(path->nodes[0]);
2489 if (path->slots[0] >= nritems) {
2490 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2495 nritems = btrfs_header_nritems(path->nodes[0]);
2497 leaf = path->nodes[0];
2498 slot = path->slots[0];
2500 btrfs_item_key_to_cpu(leaf, &key, slot);
2501 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2502 key.objectid != btrfs_ino(src))
2505 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2506 struct btrfs_file_extent_item *extent;
2509 struct btrfs_key new_key;
2510 u64 disko = 0, diskl = 0;
2511 u64 datao = 0, datal = 0;
2515 size = btrfs_item_size_nr(leaf, slot);
2516 read_extent_buffer(leaf, buf,
2517 btrfs_item_ptr_offset(leaf, slot),
2520 extent = btrfs_item_ptr(leaf, slot,
2521 struct btrfs_file_extent_item);
2522 comp = btrfs_file_extent_compression(leaf, extent);
2523 type = btrfs_file_extent_type(leaf, extent);
2524 if (type == BTRFS_FILE_EXTENT_REG ||
2525 type == BTRFS_FILE_EXTENT_PREALLOC) {
2526 disko = btrfs_file_extent_disk_bytenr(leaf,
2528 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2530 datao = btrfs_file_extent_offset(leaf, extent);
2531 datal = btrfs_file_extent_num_bytes(leaf,
2533 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2534 /* take upper bound, may be compressed */
2535 datal = btrfs_file_extent_ram_bytes(leaf,
2538 btrfs_release_path(path);
2540 if (key.offset + datal <= off ||
2541 key.offset >= off+len)
2544 memcpy(&new_key, &key, sizeof(new_key));
2545 new_key.objectid = btrfs_ino(inode);
2546 if (off <= key.offset)
2547 new_key.offset = key.offset + destoff - off;
2549 new_key.offset = destoff;
2552 * 1 - adjusting old extent (we may have to split it)
2553 * 1 - add new extent
2556 trans = btrfs_start_transaction(root, 3);
2557 if (IS_ERR(trans)) {
2558 ret = PTR_ERR(trans);
2562 if (type == BTRFS_FILE_EXTENT_REG ||
2563 type == BTRFS_FILE_EXTENT_PREALLOC) {
2565 * a | --- range to clone ---| b
2566 * | ------------- extent ------------- |
2569 /* substract range b */
2570 if (key.offset + datal > off + len)
2571 datal = off + len - key.offset;
2573 /* substract range a */
2574 if (off > key.offset) {
2575 datao += off - key.offset;
2576 datal -= off - key.offset;
2579 ret = btrfs_drop_extents(trans, root, inode,
2581 new_key.offset + datal,
2584 btrfs_abort_transaction(trans, root,
2586 btrfs_end_transaction(trans, root);
2590 ret = btrfs_insert_empty_item(trans, root, path,
2593 btrfs_abort_transaction(trans, root,
2595 btrfs_end_transaction(trans, root);
2599 leaf = path->nodes[0];
2600 slot = path->slots[0];
2601 write_extent_buffer(leaf, buf,
2602 btrfs_item_ptr_offset(leaf, slot),
2605 extent = btrfs_item_ptr(leaf, slot,
2606 struct btrfs_file_extent_item);
2608 /* disko == 0 means it's a hole */
2612 btrfs_set_file_extent_offset(leaf, extent,
2614 btrfs_set_file_extent_num_bytes(leaf, extent,
2617 inode_add_bytes(inode, datal);
2618 ret = btrfs_inc_extent_ref(trans, root,
2620 root->root_key.objectid,
2622 new_key.offset - datao,
2625 btrfs_abort_transaction(trans,
2628 btrfs_end_transaction(trans,
2634 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2637 if (off > key.offset) {
2638 skip = off - key.offset;
2639 new_key.offset += skip;
2642 if (key.offset + datal > off+len)
2643 trim = key.offset + datal - (off+len);
2645 if (comp && (skip || trim)) {
2647 btrfs_end_transaction(trans, root);
2650 size -= skip + trim;
2651 datal -= skip + trim;
2653 ret = btrfs_drop_extents(trans, root, inode,
2655 new_key.offset + datal,
2658 btrfs_abort_transaction(trans, root,
2660 btrfs_end_transaction(trans, root);
2664 ret = btrfs_insert_empty_item(trans, root, path,
2667 btrfs_abort_transaction(trans, root,
2669 btrfs_end_transaction(trans, root);
2675 btrfs_file_extent_calc_inline_size(0);
2676 memmove(buf+start, buf+start+skip,
2680 leaf = path->nodes[0];
2681 slot = path->slots[0];
2682 write_extent_buffer(leaf, buf,
2683 btrfs_item_ptr_offset(leaf, slot),
2685 inode_add_bytes(inode, datal);
2688 btrfs_mark_buffer_dirty(leaf);
2689 btrfs_release_path(path);
2691 inode_inc_iversion(inode);
2692 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2695 * we round up to the block size at eof when
2696 * determining which extents to clone above,
2697 * but shouldn't round up the file size
2699 endoff = new_key.offset + datal;
2700 if (endoff > destoff+olen)
2701 endoff = destoff+olen;
2702 if (endoff > inode->i_size)
2703 btrfs_i_size_write(inode, endoff);
2705 ret = btrfs_update_inode(trans, root, inode);
2707 btrfs_abort_transaction(trans, root, ret);
2708 btrfs_end_transaction(trans, root);
2711 ret = btrfs_end_transaction(trans, root);
2714 btrfs_release_path(path);
2719 btrfs_release_path(path);
2720 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2722 mutex_unlock(&src->i_mutex);
2723 mutex_unlock(&inode->i_mutex);
2725 btrfs_free_path(path);
2729 mnt_drop_write_file(file);
2733 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2735 struct btrfs_ioctl_clone_range_args args;
2737 if (copy_from_user(&args, argp, sizeof(args)))
2739 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2740 args.src_length, args.dest_offset);
2744 * there are many ways the trans_start and trans_end ioctls can lead
2745 * to deadlocks. They should only be used by applications that
2746 * basically own the machine, and have a very in depth understanding
2747 * of all the possible deadlocks and enospc problems.
2749 static long btrfs_ioctl_trans_start(struct file *file)
2751 struct inode *inode = fdentry(file)->d_inode;
2752 struct btrfs_root *root = BTRFS_I(inode)->root;
2753 struct btrfs_trans_handle *trans;
2757 if (!capable(CAP_SYS_ADMIN))
2761 if (file->private_data)
2765 if (btrfs_root_readonly(root))
2768 ret = mnt_want_write_file(file);
2772 atomic_inc(&root->fs_info->open_ioctl_trans);
2775 trans = btrfs_start_ioctl_transaction(root);
2779 file->private_data = trans;
2783 atomic_dec(&root->fs_info->open_ioctl_trans);
2784 mnt_drop_write_file(file);
2789 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2791 struct inode *inode = fdentry(file)->d_inode;
2792 struct btrfs_root *root = BTRFS_I(inode)->root;
2793 struct btrfs_root *new_root;
2794 struct btrfs_dir_item *di;
2795 struct btrfs_trans_handle *trans;
2796 struct btrfs_path *path;
2797 struct btrfs_key location;
2798 struct btrfs_disk_key disk_key;
2802 if (!capable(CAP_SYS_ADMIN))
2805 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2809 objectid = root->root_key.objectid;
2811 location.objectid = objectid;
2812 location.type = BTRFS_ROOT_ITEM_KEY;
2813 location.offset = (u64)-1;
2815 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2816 if (IS_ERR(new_root))
2817 return PTR_ERR(new_root);
2819 if (btrfs_root_refs(&new_root->root_item) == 0)
2822 path = btrfs_alloc_path();
2825 path->leave_spinning = 1;
2827 trans = btrfs_start_transaction(root, 1);
2828 if (IS_ERR(trans)) {
2829 btrfs_free_path(path);
2830 return PTR_ERR(trans);
2833 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
2834 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2835 dir_id, "default", 7, 1);
2836 if (IS_ERR_OR_NULL(di)) {
2837 btrfs_free_path(path);
2838 btrfs_end_transaction(trans, root);
2839 printk(KERN_ERR "Umm, you don't have the default dir item, "
2840 "this isn't going to work\n");
2844 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2845 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2846 btrfs_mark_buffer_dirty(path->nodes[0]);
2847 btrfs_free_path(path);
2849 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
2850 btrfs_end_transaction(trans, root);
2855 static void get_block_group_info(struct list_head *groups_list,
2856 struct btrfs_ioctl_space_info *space)
2858 struct btrfs_block_group_cache *block_group;
2860 space->total_bytes = 0;
2861 space->used_bytes = 0;
2863 list_for_each_entry(block_group, groups_list, list) {
2864 space->flags = block_group->flags;
2865 space->total_bytes += block_group->key.offset;
2866 space->used_bytes +=
2867 btrfs_block_group_used(&block_group->item);
2871 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2873 struct btrfs_ioctl_space_args space_args;
2874 struct btrfs_ioctl_space_info space;
2875 struct btrfs_ioctl_space_info *dest;
2876 struct btrfs_ioctl_space_info *dest_orig;
2877 struct btrfs_ioctl_space_info __user *user_dest;
2878 struct btrfs_space_info *info;
2879 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2880 BTRFS_BLOCK_GROUP_SYSTEM,
2881 BTRFS_BLOCK_GROUP_METADATA,
2882 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2889 if (copy_from_user(&space_args,
2890 (struct btrfs_ioctl_space_args __user *)arg,
2891 sizeof(space_args)))
2894 for (i = 0; i < num_types; i++) {
2895 struct btrfs_space_info *tmp;
2899 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2901 if (tmp->flags == types[i]) {
2911 down_read(&info->groups_sem);
2912 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2913 if (!list_empty(&info->block_groups[c]))
2916 up_read(&info->groups_sem);
2919 /* space_slots == 0 means they are asking for a count */
2920 if (space_args.space_slots == 0) {
2921 space_args.total_spaces = slot_count;
2925 slot_count = min_t(u64, space_args.space_slots, slot_count);
2927 alloc_size = sizeof(*dest) * slot_count;
2929 /* we generally have at most 6 or so space infos, one for each raid
2930 * level. So, a whole page should be more than enough for everyone
2932 if (alloc_size > PAGE_CACHE_SIZE)
2935 space_args.total_spaces = 0;
2936 dest = kmalloc(alloc_size, GFP_NOFS);
2941 /* now we have a buffer to copy into */
2942 for (i = 0; i < num_types; i++) {
2943 struct btrfs_space_info *tmp;
2950 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2952 if (tmp->flags == types[i]) {
2961 down_read(&info->groups_sem);
2962 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2963 if (!list_empty(&info->block_groups[c])) {
2964 get_block_group_info(&info->block_groups[c],
2966 memcpy(dest, &space, sizeof(space));
2968 space_args.total_spaces++;
2974 up_read(&info->groups_sem);
2977 user_dest = (struct btrfs_ioctl_space_info __user *)
2978 (arg + sizeof(struct btrfs_ioctl_space_args));
2980 if (copy_to_user(user_dest, dest_orig, alloc_size))
2985 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2992 * there are many ways the trans_start and trans_end ioctls can lead
2993 * to deadlocks. They should only be used by applications that
2994 * basically own the machine, and have a very in depth understanding
2995 * of all the possible deadlocks and enospc problems.
2997 long btrfs_ioctl_trans_end(struct file *file)
2999 struct inode *inode = fdentry(file)->d_inode;
3000 struct btrfs_root *root = BTRFS_I(inode)->root;
3001 struct btrfs_trans_handle *trans;
3003 trans = file->private_data;
3006 file->private_data = NULL;
3008 btrfs_end_transaction(trans, root);
3010 atomic_dec(&root->fs_info->open_ioctl_trans);
3012 mnt_drop_write_file(file);
3016 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
3018 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3019 struct btrfs_trans_handle *trans;
3023 trans = btrfs_start_transaction(root, 0);
3025 return PTR_ERR(trans);
3026 transid = trans->transid;
3027 ret = btrfs_commit_transaction_async(trans, root, 0);
3029 btrfs_end_transaction(trans, root);
3034 if (copy_to_user(argp, &transid, sizeof(transid)))
3039 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
3041 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3045 if (copy_from_user(&transid, argp, sizeof(transid)))
3048 transid = 0; /* current trans */
3050 return btrfs_wait_for_commit(root, transid);
3053 static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3056 struct btrfs_ioctl_scrub_args *sa;
3058 if (!capable(CAP_SYS_ADMIN))
3061 sa = memdup_user(arg, sizeof(*sa));
3065 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
3066 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
3068 if (copy_to_user(arg, sa, sizeof(*sa)))
3075 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3077 if (!capable(CAP_SYS_ADMIN))
3080 return btrfs_scrub_cancel(root);
3083 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3086 struct btrfs_ioctl_scrub_args *sa;
3089 if (!capable(CAP_SYS_ADMIN))
3092 sa = memdup_user(arg, sizeof(*sa));
3096 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3098 if (copy_to_user(arg, sa, sizeof(*sa)))
3105 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3108 struct btrfs_ioctl_get_dev_stats *sa;
3111 sa = memdup_user(arg, sizeof(*sa));
3115 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3120 ret = btrfs_get_dev_stats(root, sa);
3122 if (copy_to_user(arg, sa, sizeof(*sa)))
3129 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3135 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3136 struct inode_fs_paths *ipath = NULL;
3137 struct btrfs_path *path;
3139 if (!capable(CAP_SYS_ADMIN))
3142 path = btrfs_alloc_path();
3148 ipa = memdup_user(arg, sizeof(*ipa));
3155 size = min_t(u32, ipa->size, 4096);
3156 ipath = init_ipath(size, root, path);
3157 if (IS_ERR(ipath)) {
3158 ret = PTR_ERR(ipath);
3163 ret = paths_from_inode(ipa->inum, ipath);
3167 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3168 rel_ptr = ipath->fspath->val[i] -
3169 (u64)(unsigned long)ipath->fspath->val;
3170 ipath->fspath->val[i] = rel_ptr;
3173 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3174 (void *)(unsigned long)ipath->fspath, size);
3181 btrfs_free_path(path);
3188 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3190 struct btrfs_data_container *inodes = ctx;
3191 const size_t c = 3 * sizeof(u64);
3193 if (inodes->bytes_left >= c) {
3194 inodes->bytes_left -= c;
3195 inodes->val[inodes->elem_cnt] = inum;
3196 inodes->val[inodes->elem_cnt + 1] = offset;
3197 inodes->val[inodes->elem_cnt + 2] = root;
3198 inodes->elem_cnt += 3;
3200 inodes->bytes_missing += c - inodes->bytes_left;
3201 inodes->bytes_left = 0;
3202 inodes->elem_missed += 3;
3208 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3213 struct btrfs_ioctl_logical_ino_args *loi;
3214 struct btrfs_data_container *inodes = NULL;
3215 struct btrfs_path *path = NULL;
3217 if (!capable(CAP_SYS_ADMIN))
3220 loi = memdup_user(arg, sizeof(*loi));
3227 path = btrfs_alloc_path();
3233 size = min_t(u32, loi->size, 64 * 1024);
3234 inodes = init_data_container(size);
3235 if (IS_ERR(inodes)) {
3236 ret = PTR_ERR(inodes);
3241 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3242 build_ino_list, inodes);
3248 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3249 (void *)(unsigned long)inodes, size);
3254 btrfs_free_path(path);
3261 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3262 struct btrfs_ioctl_balance_args *bargs)
3264 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3266 bargs->flags = bctl->flags;
3268 if (atomic_read(&fs_info->balance_running))
3269 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3270 if (atomic_read(&fs_info->balance_pause_req))
3271 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3272 if (atomic_read(&fs_info->balance_cancel_req))
3273 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3275 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3276 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3277 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3280 spin_lock(&fs_info->balance_lock);
3281 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3282 spin_unlock(&fs_info->balance_lock);
3284 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3288 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3290 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3291 struct btrfs_fs_info *fs_info = root->fs_info;
3292 struct btrfs_ioctl_balance_args *bargs;
3293 struct btrfs_balance_control *bctl;
3296 if (!capable(CAP_SYS_ADMIN))
3299 ret = mnt_want_write_file(file);
3303 mutex_lock(&fs_info->volume_mutex);
3304 mutex_lock(&fs_info->balance_mutex);
3307 bargs = memdup_user(arg, sizeof(*bargs));
3308 if (IS_ERR(bargs)) {
3309 ret = PTR_ERR(bargs);
3313 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3314 if (!fs_info->balance_ctl) {
3319 bctl = fs_info->balance_ctl;
3320 spin_lock(&fs_info->balance_lock);
3321 bctl->flags |= BTRFS_BALANCE_RESUME;
3322 spin_unlock(&fs_info->balance_lock);
3330 if (fs_info->balance_ctl) {
3335 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3341 bctl->fs_info = fs_info;
3343 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3344 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3345 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3347 bctl->flags = bargs->flags;
3349 /* balance everything - no filters */
3350 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3354 ret = btrfs_balance(bctl, bargs);
3356 * bctl is freed in __cancel_balance or in free_fs_info if
3357 * restriper was paused all the way until unmount
3360 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3367 mutex_unlock(&fs_info->balance_mutex);
3368 mutex_unlock(&fs_info->volume_mutex);
3369 mnt_drop_write_file(file);
3373 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3375 if (!capable(CAP_SYS_ADMIN))
3379 case BTRFS_BALANCE_CTL_PAUSE:
3380 return btrfs_pause_balance(root->fs_info);
3381 case BTRFS_BALANCE_CTL_CANCEL:
3382 return btrfs_cancel_balance(root->fs_info);
3388 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3391 struct btrfs_fs_info *fs_info = root->fs_info;
3392 struct btrfs_ioctl_balance_args *bargs;
3395 if (!capable(CAP_SYS_ADMIN))
3398 mutex_lock(&fs_info->balance_mutex);
3399 if (!fs_info->balance_ctl) {
3404 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3410 update_ioctl_balance_args(fs_info, 1, bargs);
3412 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3417 mutex_unlock(&fs_info->balance_mutex);
3421 static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3423 struct btrfs_ioctl_quota_ctl_args *sa;
3424 struct btrfs_trans_handle *trans = NULL;
3428 if (!capable(CAP_SYS_ADMIN))
3431 if (root->fs_info->sb->s_flags & MS_RDONLY)
3434 sa = memdup_user(arg, sizeof(*sa));
3438 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3439 trans = btrfs_start_transaction(root, 2);
3440 if (IS_ERR(trans)) {
3441 ret = PTR_ERR(trans);
3447 case BTRFS_QUOTA_CTL_ENABLE:
3448 ret = btrfs_quota_enable(trans, root->fs_info);
3450 case BTRFS_QUOTA_CTL_DISABLE:
3451 ret = btrfs_quota_disable(trans, root->fs_info);
3453 case BTRFS_QUOTA_CTL_RESCAN:
3454 ret = btrfs_quota_rescan(root->fs_info);
3461 if (copy_to_user(arg, sa, sizeof(*sa)))
3465 err = btrfs_commit_transaction(trans, root);
3475 static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3477 struct btrfs_ioctl_qgroup_assign_args *sa;
3478 struct btrfs_trans_handle *trans;
3482 if (!capable(CAP_SYS_ADMIN))
3485 if (root->fs_info->sb->s_flags & MS_RDONLY)
3488 sa = memdup_user(arg, sizeof(*sa));
3492 trans = btrfs_join_transaction(root);
3493 if (IS_ERR(trans)) {
3494 ret = PTR_ERR(trans);
3498 /* FIXME: check if the IDs really exist */
3500 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3503 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3507 err = btrfs_end_transaction(trans, root);
3516 static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3518 struct btrfs_ioctl_qgroup_create_args *sa;
3519 struct btrfs_trans_handle *trans;
3523 if (!capable(CAP_SYS_ADMIN))
3526 if (root->fs_info->sb->s_flags & MS_RDONLY)
3529 sa = memdup_user(arg, sizeof(*sa));
3533 trans = btrfs_join_transaction(root);
3534 if (IS_ERR(trans)) {
3535 ret = PTR_ERR(trans);
3539 /* FIXME: check if the IDs really exist */
3541 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3544 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3547 err = btrfs_end_transaction(trans, root);
3556 static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3558 struct btrfs_ioctl_qgroup_limit_args *sa;
3559 struct btrfs_trans_handle *trans;
3564 if (!capable(CAP_SYS_ADMIN))
3567 if (root->fs_info->sb->s_flags & MS_RDONLY)
3570 sa = memdup_user(arg, sizeof(*sa));
3574 trans = btrfs_join_transaction(root);
3575 if (IS_ERR(trans)) {
3576 ret = PTR_ERR(trans);
3580 qgroupid = sa->qgroupid;
3582 /* take the current subvol as qgroup */
3583 qgroupid = root->root_key.objectid;
3586 /* FIXME: check if the IDs really exist */
3587 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3589 err = btrfs_end_transaction(trans, root);
3598 static long btrfs_ioctl_set_received_subvol(struct file *file,
3601 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3602 struct inode *inode = fdentry(file)->d_inode;
3603 struct btrfs_root *root = BTRFS_I(inode)->root;
3604 struct btrfs_root_item *root_item = &root->root_item;
3605 struct btrfs_trans_handle *trans;
3606 struct timespec ct = CURRENT_TIME;
3609 ret = mnt_want_write_file(file);
3613 down_write(&root->fs_info->subvol_sem);
3615 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3620 if (btrfs_root_readonly(root)) {
3625 if (!inode_owner_or_capable(inode)) {
3630 sa = memdup_user(arg, sizeof(*sa));
3637 trans = btrfs_start_transaction(root, 1);
3638 if (IS_ERR(trans)) {
3639 ret = PTR_ERR(trans);
3644 sa->rtransid = trans->transid;
3645 sa->rtime.sec = ct.tv_sec;
3646 sa->rtime.nsec = ct.tv_nsec;
3648 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3649 btrfs_set_root_stransid(root_item, sa->stransid);
3650 btrfs_set_root_rtransid(root_item, sa->rtransid);
3651 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3652 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3653 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3654 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3656 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3657 &root->root_key, &root->root_item);
3659 btrfs_end_transaction(trans, root);
3663 ret = btrfs_commit_transaction(trans, root);
3668 ret = copy_to_user(arg, sa, sizeof(*sa));
3674 up_write(&root->fs_info->subvol_sem);
3675 mnt_drop_write_file(file);
3679 long btrfs_ioctl(struct file *file, unsigned int
3680 cmd, unsigned long arg)
3682 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3683 void __user *argp = (void __user *)arg;
3686 case FS_IOC_GETFLAGS:
3687 return btrfs_ioctl_getflags(file, argp);
3688 case FS_IOC_SETFLAGS:
3689 return btrfs_ioctl_setflags(file, argp);
3690 case FS_IOC_GETVERSION:
3691 return btrfs_ioctl_getversion(file, argp);
3693 return btrfs_ioctl_fitrim(file, argp);
3694 case BTRFS_IOC_SNAP_CREATE:
3695 return btrfs_ioctl_snap_create(file, argp, 0);
3696 case BTRFS_IOC_SNAP_CREATE_V2:
3697 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3698 case BTRFS_IOC_SUBVOL_CREATE:
3699 return btrfs_ioctl_snap_create(file, argp, 1);
3700 case BTRFS_IOC_SUBVOL_CREATE_V2:
3701 return btrfs_ioctl_snap_create_v2(file, argp, 1);
3702 case BTRFS_IOC_SNAP_DESTROY:
3703 return btrfs_ioctl_snap_destroy(file, argp);
3704 case BTRFS_IOC_SUBVOL_GETFLAGS:
3705 return btrfs_ioctl_subvol_getflags(file, argp);
3706 case BTRFS_IOC_SUBVOL_SETFLAGS:
3707 return btrfs_ioctl_subvol_setflags(file, argp);
3708 case BTRFS_IOC_DEFAULT_SUBVOL:
3709 return btrfs_ioctl_default_subvol(file, argp);
3710 case BTRFS_IOC_DEFRAG:
3711 return btrfs_ioctl_defrag(file, NULL);
3712 case BTRFS_IOC_DEFRAG_RANGE:
3713 return btrfs_ioctl_defrag(file, argp);
3714 case BTRFS_IOC_RESIZE:
3715 return btrfs_ioctl_resize(root, argp);
3716 case BTRFS_IOC_ADD_DEV:
3717 return btrfs_ioctl_add_dev(root, argp);
3718 case BTRFS_IOC_RM_DEV:
3719 return btrfs_ioctl_rm_dev(root, argp);
3720 case BTRFS_IOC_FS_INFO:
3721 return btrfs_ioctl_fs_info(root, argp);
3722 case BTRFS_IOC_DEV_INFO:
3723 return btrfs_ioctl_dev_info(root, argp);
3724 case BTRFS_IOC_BALANCE:
3725 return btrfs_ioctl_balance(file, NULL);
3726 case BTRFS_IOC_CLONE:
3727 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3728 case BTRFS_IOC_CLONE_RANGE:
3729 return btrfs_ioctl_clone_range(file, argp);
3730 case BTRFS_IOC_TRANS_START:
3731 return btrfs_ioctl_trans_start(file);
3732 case BTRFS_IOC_TRANS_END:
3733 return btrfs_ioctl_trans_end(file);
3734 case BTRFS_IOC_TREE_SEARCH:
3735 return btrfs_ioctl_tree_search(file, argp);
3736 case BTRFS_IOC_INO_LOOKUP:
3737 return btrfs_ioctl_ino_lookup(file, argp);
3738 case BTRFS_IOC_INO_PATHS:
3739 return btrfs_ioctl_ino_to_path(root, argp);
3740 case BTRFS_IOC_LOGICAL_INO:
3741 return btrfs_ioctl_logical_to_ino(root, argp);
3742 case BTRFS_IOC_SPACE_INFO:
3743 return btrfs_ioctl_space_info(root, argp);
3744 case BTRFS_IOC_SYNC:
3745 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3747 case BTRFS_IOC_START_SYNC:
3748 return btrfs_ioctl_start_sync(file, argp);
3749 case BTRFS_IOC_WAIT_SYNC:
3750 return btrfs_ioctl_wait_sync(file, argp);
3751 case BTRFS_IOC_SCRUB:
3752 return btrfs_ioctl_scrub(root, argp);
3753 case BTRFS_IOC_SCRUB_CANCEL:
3754 return btrfs_ioctl_scrub_cancel(root, argp);
3755 case BTRFS_IOC_SCRUB_PROGRESS:
3756 return btrfs_ioctl_scrub_progress(root, argp);
3757 case BTRFS_IOC_BALANCE_V2:
3758 return btrfs_ioctl_balance(file, argp);
3759 case BTRFS_IOC_BALANCE_CTL:
3760 return btrfs_ioctl_balance_ctl(root, arg);
3761 case BTRFS_IOC_BALANCE_PROGRESS:
3762 return btrfs_ioctl_balance_progress(root, argp);
3763 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3764 return btrfs_ioctl_set_received_subvol(file, argp);
3765 case BTRFS_IOC_SEND:
3766 return btrfs_ioctl_send(file, argp);
3767 case BTRFS_IOC_GET_DEV_STATS:
3768 return btrfs_ioctl_get_dev_stats(root, argp);
3769 case BTRFS_IOC_QUOTA_CTL:
3770 return btrfs_ioctl_quota_ctl(root, argp);
3771 case BTRFS_IOC_QGROUP_ASSIGN:
3772 return btrfs_ioctl_qgroup_assign(root, argp);
3773 case BTRFS_IOC_QGROUP_CREATE:
3774 return btrfs_ioctl_qgroup_create(root, argp);
3775 case BTRFS_IOC_QGROUP_LIMIT:
3776 return btrfs_ioctl_qgroup_limit(root, argp);