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"
58 #include "dev-replace.h"
60 /* Mask out flags that are inappropriate for the given type of inode. */
61 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
65 else if (S_ISREG(mode))
66 return flags & ~FS_DIRSYNC_FL;
68 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
72 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
74 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
76 unsigned int iflags = 0;
78 if (flags & BTRFS_INODE_SYNC)
80 if (flags & BTRFS_INODE_IMMUTABLE)
81 iflags |= FS_IMMUTABLE_FL;
82 if (flags & BTRFS_INODE_APPEND)
83 iflags |= FS_APPEND_FL;
84 if (flags & BTRFS_INODE_NODUMP)
85 iflags |= FS_NODUMP_FL;
86 if (flags & BTRFS_INODE_NOATIME)
87 iflags |= FS_NOATIME_FL;
88 if (flags & BTRFS_INODE_DIRSYNC)
89 iflags |= FS_DIRSYNC_FL;
90 if (flags & BTRFS_INODE_NODATACOW)
91 iflags |= FS_NOCOW_FL;
93 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
94 iflags |= FS_COMPR_FL;
95 else if (flags & BTRFS_INODE_NOCOMPRESS)
96 iflags |= FS_NOCOMP_FL;
102 * Update inode->i_flags based on the btrfs internal flags.
104 void btrfs_update_iflags(struct inode *inode)
106 struct btrfs_inode *ip = BTRFS_I(inode);
108 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
110 if (ip->flags & BTRFS_INODE_SYNC)
111 inode->i_flags |= S_SYNC;
112 if (ip->flags & BTRFS_INODE_IMMUTABLE)
113 inode->i_flags |= S_IMMUTABLE;
114 if (ip->flags & BTRFS_INODE_APPEND)
115 inode->i_flags |= S_APPEND;
116 if (ip->flags & BTRFS_INODE_NOATIME)
117 inode->i_flags |= S_NOATIME;
118 if (ip->flags & BTRFS_INODE_DIRSYNC)
119 inode->i_flags |= S_DIRSYNC;
123 * Inherit flags from the parent inode.
125 * Currently only the compression flags and the cow flags are inherited.
127 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
134 flags = BTRFS_I(dir)->flags;
136 if (flags & BTRFS_INODE_NOCOMPRESS) {
137 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
138 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
139 } else if (flags & BTRFS_INODE_COMPRESS) {
140 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
141 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
144 if (flags & BTRFS_INODE_NODATACOW) {
145 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
146 if (S_ISREG(inode->i_mode))
147 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
150 btrfs_update_iflags(inode);
153 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
155 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
156 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
158 if (copy_to_user(arg, &flags, sizeof(flags)))
163 static int check_flags(unsigned int flags)
165 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
166 FS_NOATIME_FL | FS_NODUMP_FL | \
167 FS_SYNC_FL | FS_DIRSYNC_FL | \
168 FS_NOCOMP_FL | FS_COMPR_FL |
172 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
178 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
180 struct inode *inode = file_inode(file);
181 struct btrfs_inode *ip = BTRFS_I(inode);
182 struct btrfs_root *root = ip->root;
183 struct btrfs_trans_handle *trans;
184 unsigned int flags, oldflags;
187 unsigned int i_oldflags;
190 if (btrfs_root_readonly(root))
193 if (copy_from_user(&flags, arg, sizeof(flags)))
196 ret = check_flags(flags);
200 if (!inode_owner_or_capable(inode))
203 ret = mnt_want_write_file(file);
207 mutex_lock(&inode->i_mutex);
209 ip_oldflags = ip->flags;
210 i_oldflags = inode->i_flags;
211 mode = inode->i_mode;
213 flags = btrfs_mask_flags(inode->i_mode, flags);
214 oldflags = btrfs_flags_to_ioctl(ip->flags);
215 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
216 if (!capable(CAP_LINUX_IMMUTABLE)) {
222 if (flags & FS_SYNC_FL)
223 ip->flags |= BTRFS_INODE_SYNC;
225 ip->flags &= ~BTRFS_INODE_SYNC;
226 if (flags & FS_IMMUTABLE_FL)
227 ip->flags |= BTRFS_INODE_IMMUTABLE;
229 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
230 if (flags & FS_APPEND_FL)
231 ip->flags |= BTRFS_INODE_APPEND;
233 ip->flags &= ~BTRFS_INODE_APPEND;
234 if (flags & FS_NODUMP_FL)
235 ip->flags |= BTRFS_INODE_NODUMP;
237 ip->flags &= ~BTRFS_INODE_NODUMP;
238 if (flags & FS_NOATIME_FL)
239 ip->flags |= BTRFS_INODE_NOATIME;
241 ip->flags &= ~BTRFS_INODE_NOATIME;
242 if (flags & FS_DIRSYNC_FL)
243 ip->flags |= BTRFS_INODE_DIRSYNC;
245 ip->flags &= ~BTRFS_INODE_DIRSYNC;
246 if (flags & FS_NOCOW_FL) {
249 * It's safe to turn csums off here, no extents exist.
250 * Otherwise we want the flag to reflect the real COW
251 * status of the file and will not set it.
253 if (inode->i_size == 0)
254 ip->flags |= BTRFS_INODE_NODATACOW
255 | BTRFS_INODE_NODATASUM;
257 ip->flags |= BTRFS_INODE_NODATACOW;
261 * Revert back under same assuptions as above
264 if (inode->i_size == 0)
265 ip->flags &= ~(BTRFS_INODE_NODATACOW
266 | BTRFS_INODE_NODATASUM);
268 ip->flags &= ~BTRFS_INODE_NODATACOW;
273 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
274 * flag may be changed automatically if compression code won't make
277 if (flags & FS_NOCOMP_FL) {
278 ip->flags &= ~BTRFS_INODE_COMPRESS;
279 ip->flags |= BTRFS_INODE_NOCOMPRESS;
280 } else if (flags & FS_COMPR_FL) {
281 ip->flags |= BTRFS_INODE_COMPRESS;
282 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
284 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
287 trans = btrfs_start_transaction(root, 1);
289 ret = PTR_ERR(trans);
293 btrfs_update_iflags(inode);
294 inode_inc_iversion(inode);
295 inode->i_ctime = CURRENT_TIME;
296 ret = btrfs_update_inode(trans, root, inode);
298 btrfs_end_transaction(trans, root);
301 ip->flags = ip_oldflags;
302 inode->i_flags = i_oldflags;
306 mutex_unlock(&inode->i_mutex);
307 mnt_drop_write_file(file);
311 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
313 struct inode *inode = file_inode(file);
315 return put_user(inode->i_generation, arg);
318 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
320 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
321 struct btrfs_device *device;
322 struct request_queue *q;
323 struct fstrim_range range;
324 u64 minlen = ULLONG_MAX;
326 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
329 if (!capable(CAP_SYS_ADMIN))
333 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
337 q = bdev_get_queue(device->bdev);
338 if (blk_queue_discard(q)) {
340 minlen = min((u64)q->limits.discard_granularity,
348 if (copy_from_user(&range, arg, sizeof(range)))
350 if (range.start > total_bytes ||
351 range.len < fs_info->sb->s_blocksize)
354 range.len = min(range.len, total_bytes - range.start);
355 range.minlen = max(range.minlen, minlen);
356 ret = btrfs_trim_fs(fs_info->tree_root, &range);
360 if (copy_to_user(arg, &range, sizeof(range)))
366 static noinline int create_subvol(struct btrfs_root *root,
367 struct dentry *dentry,
368 char *name, int namelen,
370 struct btrfs_qgroup_inherit **inherit)
372 struct btrfs_trans_handle *trans;
373 struct btrfs_key key;
374 struct btrfs_root_item root_item;
375 struct btrfs_inode_item *inode_item;
376 struct extent_buffer *leaf;
377 struct btrfs_root *new_root;
378 struct dentry *parent = dentry->d_parent;
380 struct timespec cur_time = CURRENT_TIME;
384 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
388 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
392 dir = parent->d_inode;
400 trans = btrfs_start_transaction(root, 6);
402 return PTR_ERR(trans);
404 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
405 inherit ? *inherit : NULL);
409 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
410 0, objectid, NULL, 0, 0, 0);
416 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
417 btrfs_set_header_bytenr(leaf, leaf->start);
418 btrfs_set_header_generation(leaf, trans->transid);
419 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
420 btrfs_set_header_owner(leaf, objectid);
422 write_extent_buffer(leaf, root->fs_info->fsid,
423 (unsigned long)btrfs_header_fsid(leaf),
425 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
426 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
428 btrfs_mark_buffer_dirty(leaf);
430 memset(&root_item, 0, sizeof(root_item));
432 inode_item = &root_item.inode;
433 inode_item->generation = cpu_to_le64(1);
434 inode_item->size = cpu_to_le64(3);
435 inode_item->nlink = cpu_to_le32(1);
436 inode_item->nbytes = cpu_to_le64(root->leafsize);
437 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
440 root_item.byte_limit = 0;
441 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
443 btrfs_set_root_bytenr(&root_item, leaf->start);
444 btrfs_set_root_generation(&root_item, trans->transid);
445 btrfs_set_root_level(&root_item, 0);
446 btrfs_set_root_refs(&root_item, 1);
447 btrfs_set_root_used(&root_item, leaf->len);
448 btrfs_set_root_last_snapshot(&root_item, 0);
450 btrfs_set_root_generation_v2(&root_item,
451 btrfs_root_generation(&root_item));
452 uuid_le_gen(&new_uuid);
453 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
454 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
455 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
456 root_item.ctime = root_item.otime;
457 btrfs_set_root_ctransid(&root_item, trans->transid);
458 btrfs_set_root_otransid(&root_item, trans->transid);
460 btrfs_tree_unlock(leaf);
461 free_extent_buffer(leaf);
464 btrfs_set_root_dirid(&root_item, new_dirid);
466 key.objectid = objectid;
468 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
469 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
474 key.offset = (u64)-1;
475 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
476 if (IS_ERR(new_root)) {
477 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
478 ret = PTR_ERR(new_root);
482 btrfs_record_root_in_trans(trans, new_root);
484 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
486 /* We potentially lose an unused inode item here */
487 btrfs_abort_transaction(trans, root, ret);
492 * insert the directory item
494 ret = btrfs_set_inode_index(dir, &index);
496 btrfs_abort_transaction(trans, root, ret);
500 ret = btrfs_insert_dir_item(trans, root,
501 name, namelen, dir, &key,
502 BTRFS_FT_DIR, index);
504 btrfs_abort_transaction(trans, root, ret);
508 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
509 ret = btrfs_update_inode(trans, root, dir);
512 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
513 objectid, root->root_key.objectid,
514 btrfs_ino(dir), index, name, namelen);
520 *async_transid = trans->transid;
521 err = btrfs_commit_transaction_async(trans, root, 1);
523 err = btrfs_commit_transaction(trans, root);
529 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
534 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
535 char *name, int namelen, u64 *async_transid,
536 bool readonly, struct btrfs_qgroup_inherit **inherit)
539 struct btrfs_pending_snapshot *pending_snapshot;
540 struct btrfs_trans_handle *trans;
546 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
547 if (!pending_snapshot)
550 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
551 BTRFS_BLOCK_RSV_TEMP);
552 pending_snapshot->dentry = dentry;
553 pending_snapshot->root = root;
554 pending_snapshot->readonly = readonly;
556 pending_snapshot->inherit = *inherit;
557 *inherit = NULL; /* take responsibility to free it */
560 trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
562 ret = PTR_ERR(trans);
566 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
569 spin_lock(&root->fs_info->trans_lock);
570 list_add(&pending_snapshot->list,
571 &trans->transaction->pending_snapshots);
572 spin_unlock(&root->fs_info->trans_lock);
574 *async_transid = trans->transid;
575 ret = btrfs_commit_transaction_async(trans,
576 root->fs_info->extent_root, 1);
578 ret = btrfs_commit_transaction(trans,
579 root->fs_info->extent_root);
582 /* cleanup_transaction has freed this for us */
584 pending_snapshot = NULL;
588 ret = pending_snapshot->error;
592 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
596 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
598 ret = PTR_ERR(inode);
602 d_instantiate(dentry, inode);
605 kfree(pending_snapshot);
609 /* copy of check_sticky in fs/namei.c()
610 * It's inline, so penalty for filesystems that don't use sticky bit is
613 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
615 kuid_t fsuid = current_fsuid();
617 if (!(dir->i_mode & S_ISVTX))
619 if (uid_eq(inode->i_uid, fsuid))
621 if (uid_eq(dir->i_uid, fsuid))
623 return !capable(CAP_FOWNER);
626 /* copy of may_delete in fs/namei.c()
627 * Check whether we can remove a link victim from directory dir, check
628 * whether the type of victim is right.
629 * 1. We can't do it if dir is read-only (done in permission())
630 * 2. We should have write and exec permissions on dir
631 * 3. We can't remove anything from append-only dir
632 * 4. We can't do anything with immutable dir (done in permission())
633 * 5. If the sticky bit on dir is set we should either
634 * a. be owner of dir, or
635 * b. be owner of victim, or
636 * c. have CAP_FOWNER capability
637 * 6. If the victim is append-only or immutable we can't do antyhing with
638 * links pointing to it.
639 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
640 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
641 * 9. We can't remove a root or mountpoint.
642 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
643 * nfs_async_unlink().
646 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
650 if (!victim->d_inode)
653 BUG_ON(victim->d_parent->d_inode != dir);
654 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
656 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
661 if (btrfs_check_sticky(dir, victim->d_inode)||
662 IS_APPEND(victim->d_inode)||
663 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
666 if (!S_ISDIR(victim->d_inode->i_mode))
670 } else if (S_ISDIR(victim->d_inode->i_mode))
674 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
679 /* copy of may_create in fs/namei.c() */
680 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
686 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
690 * Create a new subvolume below @parent. This is largely modeled after
691 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
692 * inside this filesystem so it's quite a bit simpler.
694 static noinline int btrfs_mksubvol(struct path *parent,
695 char *name, int namelen,
696 struct btrfs_root *snap_src,
697 u64 *async_transid, bool readonly,
698 struct btrfs_qgroup_inherit **inherit)
700 struct inode *dir = parent->dentry->d_inode;
701 struct dentry *dentry;
704 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
706 dentry = lookup_one_len(name, parent->dentry, namelen);
707 error = PTR_ERR(dentry);
715 error = btrfs_may_create(dir, dentry);
720 * even if this name doesn't exist, we may get hash collisions.
721 * check for them now when we can safely fail
723 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
729 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
731 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
735 error = create_snapshot(snap_src, dentry, name, namelen,
736 async_transid, readonly, inherit);
738 error = create_subvol(BTRFS_I(dir)->root, dentry,
739 name, namelen, async_transid, inherit);
742 fsnotify_mkdir(dir, dentry);
744 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
748 mutex_unlock(&dir->i_mutex);
753 * When we're defragging a range, we don't want to kick it off again
754 * if it is really just waiting for delalloc to send it down.
755 * If we find a nice big extent or delalloc range for the bytes in the
756 * file you want to defrag, we return 0 to let you know to skip this
759 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
761 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
762 struct extent_map *em = NULL;
763 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
766 read_lock(&em_tree->lock);
767 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
768 read_unlock(&em_tree->lock);
771 end = extent_map_end(em);
773 if (end - offset > thresh)
776 /* if we already have a nice delalloc here, just stop */
778 end = count_range_bits(io_tree, &offset, offset + thresh,
779 thresh, EXTENT_DELALLOC, 1);
786 * helper function to walk through a file and find extents
787 * newer than a specific transid, and smaller than thresh.
789 * This is used by the defragging code to find new and small
792 static int find_new_extents(struct btrfs_root *root,
793 struct inode *inode, u64 newer_than,
794 u64 *off, int thresh)
796 struct btrfs_path *path;
797 struct btrfs_key min_key;
798 struct btrfs_key max_key;
799 struct extent_buffer *leaf;
800 struct btrfs_file_extent_item *extent;
803 u64 ino = btrfs_ino(inode);
805 path = btrfs_alloc_path();
809 min_key.objectid = ino;
810 min_key.type = BTRFS_EXTENT_DATA_KEY;
811 min_key.offset = *off;
813 max_key.objectid = ino;
814 max_key.type = (u8)-1;
815 max_key.offset = (u64)-1;
817 path->keep_locks = 1;
820 ret = btrfs_search_forward(root, &min_key, &max_key,
821 path, 0, newer_than);
824 if (min_key.objectid != ino)
826 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
829 leaf = path->nodes[0];
830 extent = btrfs_item_ptr(leaf, path->slots[0],
831 struct btrfs_file_extent_item);
833 type = btrfs_file_extent_type(leaf, extent);
834 if (type == BTRFS_FILE_EXTENT_REG &&
835 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
836 check_defrag_in_cache(inode, min_key.offset, thresh)) {
837 *off = min_key.offset;
838 btrfs_free_path(path);
842 if (min_key.offset == (u64)-1)
846 btrfs_release_path(path);
849 btrfs_free_path(path);
853 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
855 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
856 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
857 struct extent_map *em;
858 u64 len = PAGE_CACHE_SIZE;
861 * hopefully we have this extent in the tree already, try without
862 * the full extent lock
864 read_lock(&em_tree->lock);
865 em = lookup_extent_mapping(em_tree, start, len);
866 read_unlock(&em_tree->lock);
869 /* get the big lock and read metadata off disk */
870 lock_extent(io_tree, start, start + len - 1);
871 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
872 unlock_extent(io_tree, start, start + len - 1);
881 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
883 struct extent_map *next;
886 /* this is the last extent */
887 if (em->start + em->len >= i_size_read(inode))
890 next = defrag_lookup_extent(inode, em->start + em->len);
891 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
894 free_extent_map(next);
898 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
899 u64 *last_len, u64 *skip, u64 *defrag_end,
902 struct extent_map *em;
904 bool next_mergeable = true;
907 * make sure that once we start defragging an extent, we keep on
910 if (start < *defrag_end)
915 em = defrag_lookup_extent(inode, start);
919 /* this will cover holes, and inline extents */
920 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
925 next_mergeable = defrag_check_next_extent(inode, em);
928 * we hit a real extent, if it is big or the next extent is not a
929 * real extent, don't bother defragging it
931 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
932 (em->len >= thresh || !next_mergeable))
936 * last_len ends up being a counter of how many bytes we've defragged.
937 * every time we choose not to defrag an extent, we reset *last_len
938 * so that the next tiny extent will force a defrag.
940 * The end result of this is that tiny extents before a single big
941 * extent will force at least part of that big extent to be defragged.
944 *defrag_end = extent_map_end(em);
947 *skip = extent_map_end(em);
956 * it doesn't do much good to defrag one or two pages
957 * at a time. This pulls in a nice chunk of pages
960 * It also makes sure the delalloc code has enough
961 * dirty data to avoid making new small extents as part
964 * It's a good idea to start RA on this range
965 * before calling this.
967 static int cluster_pages_for_defrag(struct inode *inode,
969 unsigned long start_index,
972 unsigned long file_end;
973 u64 isize = i_size_read(inode);
980 struct btrfs_ordered_extent *ordered;
981 struct extent_state *cached_state = NULL;
982 struct extent_io_tree *tree;
983 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
985 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
986 if (!isize || start_index > file_end)
989 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
991 ret = btrfs_delalloc_reserve_space(inode,
992 page_cnt << PAGE_CACHE_SHIFT);
996 tree = &BTRFS_I(inode)->io_tree;
998 /* step one, lock all the pages */
999 for (i = 0; i < page_cnt; i++) {
1002 page = find_or_create_page(inode->i_mapping,
1003 start_index + i, mask);
1007 page_start = page_offset(page);
1008 page_end = page_start + PAGE_CACHE_SIZE - 1;
1010 lock_extent(tree, page_start, page_end);
1011 ordered = btrfs_lookup_ordered_extent(inode,
1013 unlock_extent(tree, page_start, page_end);
1018 btrfs_start_ordered_extent(inode, ordered, 1);
1019 btrfs_put_ordered_extent(ordered);
1022 * we unlocked the page above, so we need check if
1023 * it was released or not.
1025 if (page->mapping != inode->i_mapping) {
1027 page_cache_release(page);
1032 if (!PageUptodate(page)) {
1033 btrfs_readpage(NULL, page);
1035 if (!PageUptodate(page)) {
1037 page_cache_release(page);
1043 if (page->mapping != inode->i_mapping) {
1045 page_cache_release(page);
1055 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1059 * so now we have a nice long stream of locked
1060 * and up to date pages, lets wait on them
1062 for (i = 0; i < i_done; i++)
1063 wait_on_page_writeback(pages[i]);
1065 page_start = page_offset(pages[0]);
1066 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1068 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1069 page_start, page_end - 1, 0, &cached_state);
1070 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1071 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1072 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1073 &cached_state, GFP_NOFS);
1075 if (i_done != page_cnt) {
1076 spin_lock(&BTRFS_I(inode)->lock);
1077 BTRFS_I(inode)->outstanding_extents++;
1078 spin_unlock(&BTRFS_I(inode)->lock);
1079 btrfs_delalloc_release_space(inode,
1080 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1084 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1085 &cached_state, GFP_NOFS);
1087 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1088 page_start, page_end - 1, &cached_state,
1091 for (i = 0; i < i_done; i++) {
1092 clear_page_dirty_for_io(pages[i]);
1093 ClearPageChecked(pages[i]);
1094 set_page_extent_mapped(pages[i]);
1095 set_page_dirty(pages[i]);
1096 unlock_page(pages[i]);
1097 page_cache_release(pages[i]);
1101 for (i = 0; i < i_done; i++) {
1102 unlock_page(pages[i]);
1103 page_cache_release(pages[i]);
1105 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1110 int btrfs_defrag_file(struct inode *inode, struct file *file,
1111 struct btrfs_ioctl_defrag_range_args *range,
1112 u64 newer_than, unsigned long max_to_defrag)
1114 struct btrfs_root *root = BTRFS_I(inode)->root;
1115 struct file_ra_state *ra = NULL;
1116 unsigned long last_index;
1117 u64 isize = i_size_read(inode);
1121 u64 newer_off = range->start;
1123 unsigned long ra_index = 0;
1125 int defrag_count = 0;
1126 int compress_type = BTRFS_COMPRESS_ZLIB;
1127 int extent_thresh = range->extent_thresh;
1128 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1129 int cluster = max_cluster;
1130 u64 new_align = ~((u64)128 * 1024 - 1);
1131 struct page **pages = NULL;
1133 if (extent_thresh == 0)
1134 extent_thresh = 256 * 1024;
1136 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1137 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1139 if (range->compress_type)
1140 compress_type = range->compress_type;
1147 * if we were not given a file, allocate a readahead
1151 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1154 file_ra_state_init(ra, inode->i_mapping);
1159 pages = kmalloc(sizeof(struct page *) * max_cluster,
1166 /* find the last page to defrag */
1167 if (range->start + range->len > range->start) {
1168 last_index = min_t(u64, isize - 1,
1169 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1171 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1175 ret = find_new_extents(root, inode, newer_than,
1176 &newer_off, 64 * 1024);
1178 range->start = newer_off;
1180 * we always align our defrag to help keep
1181 * the extents in the file evenly spaced
1183 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1187 i = range->start >> PAGE_CACHE_SHIFT;
1190 max_to_defrag = last_index + 1;
1193 * make writeback starts from i, so the defrag range can be
1194 * written sequentially.
1196 if (i < inode->i_mapping->writeback_index)
1197 inode->i_mapping->writeback_index = i;
1199 while (i <= last_index && defrag_count < max_to_defrag &&
1200 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1201 PAGE_CACHE_SHIFT)) {
1203 * make sure we stop running if someone unmounts
1206 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1209 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1210 extent_thresh, &last_len, &skip,
1211 &defrag_end, range->flags &
1212 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1215 * the should_defrag function tells us how much to skip
1216 * bump our counter by the suggested amount
1218 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1219 i = max(i + 1, next);
1224 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1225 PAGE_CACHE_SHIFT) - i;
1226 cluster = min(cluster, max_cluster);
1228 cluster = max_cluster;
1231 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1232 BTRFS_I(inode)->force_compress = compress_type;
1234 if (i + cluster > ra_index) {
1235 ra_index = max(i, ra_index);
1236 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1238 ra_index += max_cluster;
1241 mutex_lock(&inode->i_mutex);
1242 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1244 mutex_unlock(&inode->i_mutex);
1248 defrag_count += ret;
1249 balance_dirty_pages_ratelimited(inode->i_mapping);
1250 mutex_unlock(&inode->i_mutex);
1253 if (newer_off == (u64)-1)
1259 newer_off = max(newer_off + 1,
1260 (u64)i << PAGE_CACHE_SHIFT);
1262 ret = find_new_extents(root, inode,
1263 newer_than, &newer_off,
1266 range->start = newer_off;
1267 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1274 last_len += ret << PAGE_CACHE_SHIFT;
1282 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1283 filemap_flush(inode->i_mapping);
1285 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1286 /* the filemap_flush will queue IO into the worker threads, but
1287 * we have to make sure the IO is actually started and that
1288 * ordered extents get created before we return
1290 atomic_inc(&root->fs_info->async_submit_draining);
1291 while (atomic_read(&root->fs_info->nr_async_submits) ||
1292 atomic_read(&root->fs_info->async_delalloc_pages)) {
1293 wait_event(root->fs_info->async_submit_wait,
1294 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1295 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1297 atomic_dec(&root->fs_info->async_submit_draining);
1299 mutex_lock(&inode->i_mutex);
1300 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1301 mutex_unlock(&inode->i_mutex);
1304 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1305 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1317 static noinline int btrfs_ioctl_resize(struct file *file,
1323 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1324 struct btrfs_ioctl_vol_args *vol_args;
1325 struct btrfs_trans_handle *trans;
1326 struct btrfs_device *device = NULL;
1328 char *devstr = NULL;
1332 if (root->fs_info->sb->s_flags & MS_RDONLY)
1335 if (!capable(CAP_SYS_ADMIN))
1338 ret = mnt_want_write_file(file);
1342 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1344 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1345 mnt_drop_write_file(file);
1349 mutex_lock(&root->fs_info->volume_mutex);
1350 vol_args = memdup_user(arg, sizeof(*vol_args));
1351 if (IS_ERR(vol_args)) {
1352 ret = PTR_ERR(vol_args);
1356 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1358 sizestr = vol_args->name;
1359 devstr = strchr(sizestr, ':');
1362 sizestr = devstr + 1;
1364 devstr = vol_args->name;
1365 devid = simple_strtoull(devstr, &end, 10);
1366 printk(KERN_INFO "btrfs: resizing devid %llu\n",
1367 (unsigned long long)devid);
1370 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1372 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1373 (unsigned long long)devid);
1378 if (!device->writeable) {
1379 printk(KERN_INFO "btrfs: resizer unable to apply on "
1380 "readonly device %llu\n",
1381 (unsigned long long)devid);
1386 if (!strcmp(sizestr, "max"))
1387 new_size = device->bdev->bd_inode->i_size;
1389 if (sizestr[0] == '-') {
1392 } else if (sizestr[0] == '+') {
1396 new_size = memparse(sizestr, NULL);
1397 if (new_size == 0) {
1403 if (device->is_tgtdev_for_dev_replace) {
1408 old_size = device->total_bytes;
1411 if (new_size > old_size) {
1415 new_size = old_size - new_size;
1416 } else if (mod > 0) {
1417 new_size = old_size + new_size;
1420 if (new_size < 256 * 1024 * 1024) {
1424 if (new_size > device->bdev->bd_inode->i_size) {
1429 do_div(new_size, root->sectorsize);
1430 new_size *= root->sectorsize;
1432 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1433 rcu_str_deref(device->name),
1434 (unsigned long long)new_size);
1436 if (new_size > old_size) {
1437 trans = btrfs_start_transaction(root, 0);
1438 if (IS_ERR(trans)) {
1439 ret = PTR_ERR(trans);
1442 ret = btrfs_grow_device(trans, device, new_size);
1443 btrfs_commit_transaction(trans, root);
1444 } else if (new_size < old_size) {
1445 ret = btrfs_shrink_device(device, new_size);
1446 } /* equal, nothing need to do */
1451 mutex_unlock(&root->fs_info->volume_mutex);
1452 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1453 mnt_drop_write_file(file);
1457 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1458 char *name, unsigned long fd, int subvol,
1459 u64 *transid, bool readonly,
1460 struct btrfs_qgroup_inherit **inherit)
1465 ret = mnt_want_write_file(file);
1469 namelen = strlen(name);
1470 if (strchr(name, '/')) {
1472 goto out_drop_write;
1475 if (name[0] == '.' &&
1476 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1478 goto out_drop_write;
1482 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1483 NULL, transid, readonly, inherit);
1485 struct fd src = fdget(fd);
1486 struct inode *src_inode;
1489 goto out_drop_write;
1492 src_inode = file_inode(src.file);
1493 if (src_inode->i_sb != file_inode(file)->i_sb) {
1494 printk(KERN_INFO "btrfs: Snapshot src from "
1498 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1499 BTRFS_I(src_inode)->root,
1500 transid, readonly, inherit);
1505 mnt_drop_write_file(file);
1510 static noinline int btrfs_ioctl_snap_create(struct file *file,
1511 void __user *arg, int subvol)
1513 struct btrfs_ioctl_vol_args *vol_args;
1516 vol_args = memdup_user(arg, sizeof(*vol_args));
1517 if (IS_ERR(vol_args))
1518 return PTR_ERR(vol_args);
1519 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1521 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1522 vol_args->fd, subvol,
1529 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1530 void __user *arg, int subvol)
1532 struct btrfs_ioctl_vol_args_v2 *vol_args;
1536 bool readonly = false;
1537 struct btrfs_qgroup_inherit *inherit = NULL;
1539 vol_args = memdup_user(arg, sizeof(*vol_args));
1540 if (IS_ERR(vol_args))
1541 return PTR_ERR(vol_args);
1542 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1544 if (vol_args->flags &
1545 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1546 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1551 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1553 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1555 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1556 if (vol_args->size > PAGE_CACHE_SIZE) {
1560 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1561 if (IS_ERR(inherit)) {
1562 ret = PTR_ERR(inherit);
1567 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1568 vol_args->fd, subvol, ptr,
1569 readonly, &inherit);
1571 if (ret == 0 && ptr &&
1573 offsetof(struct btrfs_ioctl_vol_args_v2,
1574 transid), ptr, sizeof(*ptr)))
1582 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1585 struct inode *inode = file_inode(file);
1586 struct btrfs_root *root = BTRFS_I(inode)->root;
1590 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1593 down_read(&root->fs_info->subvol_sem);
1594 if (btrfs_root_readonly(root))
1595 flags |= BTRFS_SUBVOL_RDONLY;
1596 up_read(&root->fs_info->subvol_sem);
1598 if (copy_to_user(arg, &flags, sizeof(flags)))
1604 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1607 struct inode *inode = file_inode(file);
1608 struct btrfs_root *root = BTRFS_I(inode)->root;
1609 struct btrfs_trans_handle *trans;
1614 ret = mnt_want_write_file(file);
1618 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1620 goto out_drop_write;
1623 if (copy_from_user(&flags, arg, sizeof(flags))) {
1625 goto out_drop_write;
1628 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1630 goto out_drop_write;
1633 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1635 goto out_drop_write;
1638 if (!inode_owner_or_capable(inode)) {
1640 goto out_drop_write;
1643 down_write(&root->fs_info->subvol_sem);
1646 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1649 root_flags = btrfs_root_flags(&root->root_item);
1650 if (flags & BTRFS_SUBVOL_RDONLY)
1651 btrfs_set_root_flags(&root->root_item,
1652 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1654 btrfs_set_root_flags(&root->root_item,
1655 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1657 trans = btrfs_start_transaction(root, 1);
1658 if (IS_ERR(trans)) {
1659 ret = PTR_ERR(trans);
1663 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1664 &root->root_key, &root->root_item);
1666 btrfs_commit_transaction(trans, root);
1669 btrfs_set_root_flags(&root->root_item, root_flags);
1671 up_write(&root->fs_info->subvol_sem);
1673 mnt_drop_write_file(file);
1679 * helper to check if the subvolume references other subvolumes
1681 static noinline int may_destroy_subvol(struct btrfs_root *root)
1683 struct btrfs_path *path;
1684 struct btrfs_key key;
1687 path = btrfs_alloc_path();
1691 key.objectid = root->root_key.objectid;
1692 key.type = BTRFS_ROOT_REF_KEY;
1693 key.offset = (u64)-1;
1695 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1702 if (path->slots[0] > 0) {
1704 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1705 if (key.objectid == root->root_key.objectid &&
1706 key.type == BTRFS_ROOT_REF_KEY)
1710 btrfs_free_path(path);
1714 static noinline int key_in_sk(struct btrfs_key *key,
1715 struct btrfs_ioctl_search_key *sk)
1717 struct btrfs_key test;
1720 test.objectid = sk->min_objectid;
1721 test.type = sk->min_type;
1722 test.offset = sk->min_offset;
1724 ret = btrfs_comp_cpu_keys(key, &test);
1728 test.objectid = sk->max_objectid;
1729 test.type = sk->max_type;
1730 test.offset = sk->max_offset;
1732 ret = btrfs_comp_cpu_keys(key, &test);
1738 static noinline int copy_to_sk(struct btrfs_root *root,
1739 struct btrfs_path *path,
1740 struct btrfs_key *key,
1741 struct btrfs_ioctl_search_key *sk,
1743 unsigned long *sk_offset,
1747 struct extent_buffer *leaf;
1748 struct btrfs_ioctl_search_header sh;
1749 unsigned long item_off;
1750 unsigned long item_len;
1756 leaf = path->nodes[0];
1757 slot = path->slots[0];
1758 nritems = btrfs_header_nritems(leaf);
1760 if (btrfs_header_generation(leaf) > sk->max_transid) {
1764 found_transid = btrfs_header_generation(leaf);
1766 for (i = slot; i < nritems; i++) {
1767 item_off = btrfs_item_ptr_offset(leaf, i);
1768 item_len = btrfs_item_size_nr(leaf, i);
1770 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1773 if (sizeof(sh) + item_len + *sk_offset >
1774 BTRFS_SEARCH_ARGS_BUFSIZE) {
1779 btrfs_item_key_to_cpu(leaf, key, i);
1780 if (!key_in_sk(key, sk))
1783 sh.objectid = key->objectid;
1784 sh.offset = key->offset;
1785 sh.type = key->type;
1787 sh.transid = found_transid;
1789 /* copy search result header */
1790 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1791 *sk_offset += sizeof(sh);
1794 char *p = buf + *sk_offset;
1796 read_extent_buffer(leaf, p,
1797 item_off, item_len);
1798 *sk_offset += item_len;
1802 if (*num_found >= sk->nr_items)
1807 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1809 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1812 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1822 static noinline int search_ioctl(struct inode *inode,
1823 struct btrfs_ioctl_search_args *args)
1825 struct btrfs_root *root;
1826 struct btrfs_key key;
1827 struct btrfs_key max_key;
1828 struct btrfs_path *path;
1829 struct btrfs_ioctl_search_key *sk = &args->key;
1830 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1833 unsigned long sk_offset = 0;
1835 path = btrfs_alloc_path();
1839 if (sk->tree_id == 0) {
1840 /* search the root of the inode that was passed */
1841 root = BTRFS_I(inode)->root;
1843 key.objectid = sk->tree_id;
1844 key.type = BTRFS_ROOT_ITEM_KEY;
1845 key.offset = (u64)-1;
1846 root = btrfs_read_fs_root_no_name(info, &key);
1848 printk(KERN_ERR "could not find root %llu\n",
1850 btrfs_free_path(path);
1855 key.objectid = sk->min_objectid;
1856 key.type = sk->min_type;
1857 key.offset = sk->min_offset;
1859 max_key.objectid = sk->max_objectid;
1860 max_key.type = sk->max_type;
1861 max_key.offset = sk->max_offset;
1863 path->keep_locks = 1;
1866 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1873 ret = copy_to_sk(root, path, &key, sk, args->buf,
1874 &sk_offset, &num_found);
1875 btrfs_release_path(path);
1876 if (ret || num_found >= sk->nr_items)
1882 sk->nr_items = num_found;
1883 btrfs_free_path(path);
1887 static noinline int btrfs_ioctl_tree_search(struct file *file,
1890 struct btrfs_ioctl_search_args *args;
1891 struct inode *inode;
1894 if (!capable(CAP_SYS_ADMIN))
1897 args = memdup_user(argp, sizeof(*args));
1899 return PTR_ERR(args);
1901 inode = file_inode(file);
1902 ret = search_ioctl(inode, args);
1903 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1910 * Search INODE_REFs to identify path name of 'dirid' directory
1911 * in a 'tree_id' tree. and sets path name to 'name'.
1913 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1914 u64 tree_id, u64 dirid, char *name)
1916 struct btrfs_root *root;
1917 struct btrfs_key key;
1923 struct btrfs_inode_ref *iref;
1924 struct extent_buffer *l;
1925 struct btrfs_path *path;
1927 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1932 path = btrfs_alloc_path();
1936 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1938 key.objectid = tree_id;
1939 key.type = BTRFS_ROOT_ITEM_KEY;
1940 key.offset = (u64)-1;
1941 root = btrfs_read_fs_root_no_name(info, &key);
1943 printk(KERN_ERR "could not find root %llu\n", tree_id);
1948 key.objectid = dirid;
1949 key.type = BTRFS_INODE_REF_KEY;
1950 key.offset = (u64)-1;
1953 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1958 slot = path->slots[0];
1959 if (ret > 0 && slot > 0)
1961 btrfs_item_key_to_cpu(l, &key, slot);
1963 if (ret > 0 && (key.objectid != dirid ||
1964 key.type != BTRFS_INODE_REF_KEY)) {
1969 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1970 len = btrfs_inode_ref_name_len(l, iref);
1972 total_len += len + 1;
1977 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1979 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1982 btrfs_release_path(path);
1983 key.objectid = key.offset;
1984 key.offset = (u64)-1;
1985 dirid = key.objectid;
1989 memmove(name, ptr, total_len);
1990 name[total_len]='\0';
1993 btrfs_free_path(path);
1997 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2000 struct btrfs_ioctl_ino_lookup_args *args;
2001 struct inode *inode;
2004 if (!capable(CAP_SYS_ADMIN))
2007 args = memdup_user(argp, sizeof(*args));
2009 return PTR_ERR(args);
2011 inode = file_inode(file);
2013 if (args->treeid == 0)
2014 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2016 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2017 args->treeid, args->objectid,
2020 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2027 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2030 struct dentry *parent = fdentry(file);
2031 struct dentry *dentry;
2032 struct inode *dir = parent->d_inode;
2033 struct inode *inode;
2034 struct btrfs_root *root = BTRFS_I(dir)->root;
2035 struct btrfs_root *dest = NULL;
2036 struct btrfs_ioctl_vol_args *vol_args;
2037 struct btrfs_trans_handle *trans;
2042 vol_args = memdup_user(arg, sizeof(*vol_args));
2043 if (IS_ERR(vol_args))
2044 return PTR_ERR(vol_args);
2046 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2047 namelen = strlen(vol_args->name);
2048 if (strchr(vol_args->name, '/') ||
2049 strncmp(vol_args->name, "..", namelen) == 0) {
2054 err = mnt_want_write_file(file);
2058 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2059 dentry = lookup_one_len(vol_args->name, parent, namelen);
2060 if (IS_ERR(dentry)) {
2061 err = PTR_ERR(dentry);
2062 goto out_unlock_dir;
2065 if (!dentry->d_inode) {
2070 inode = dentry->d_inode;
2071 dest = BTRFS_I(inode)->root;
2072 if (!capable(CAP_SYS_ADMIN)){
2074 * Regular user. Only allow this with a special mount
2075 * option, when the user has write+exec access to the
2076 * subvol root, and when rmdir(2) would have been
2079 * Note that this is _not_ check that the subvol is
2080 * empty or doesn't contain data that we wouldn't
2081 * otherwise be able to delete.
2083 * Users who want to delete empty subvols should try
2087 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2091 * Do not allow deletion if the parent dir is the same
2092 * as the dir to be deleted. That means the ioctl
2093 * must be called on the dentry referencing the root
2094 * of the subvol, not a random directory contained
2101 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2106 /* check if subvolume may be deleted by a user */
2107 err = btrfs_may_delete(dir, dentry, 1);
2111 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2116 mutex_lock(&inode->i_mutex);
2117 err = d_invalidate(dentry);
2121 down_write(&root->fs_info->subvol_sem);
2123 err = may_destroy_subvol(dest);
2127 trans = btrfs_start_transaction(root, 0);
2128 if (IS_ERR(trans)) {
2129 err = PTR_ERR(trans);
2132 trans->block_rsv = &root->fs_info->global_block_rsv;
2134 ret = btrfs_unlink_subvol(trans, root, dir,
2135 dest->root_key.objectid,
2136 dentry->d_name.name,
2137 dentry->d_name.len);
2140 btrfs_abort_transaction(trans, root, ret);
2144 btrfs_record_root_in_trans(trans, dest);
2146 memset(&dest->root_item.drop_progress, 0,
2147 sizeof(dest->root_item.drop_progress));
2148 dest->root_item.drop_level = 0;
2149 btrfs_set_root_refs(&dest->root_item, 0);
2151 if (!xchg(&dest->orphan_item_inserted, 1)) {
2152 ret = btrfs_insert_orphan_item(trans,
2153 root->fs_info->tree_root,
2154 dest->root_key.objectid);
2156 btrfs_abort_transaction(trans, root, ret);
2162 ret = btrfs_end_transaction(trans, root);
2165 inode->i_flags |= S_DEAD;
2167 up_write(&root->fs_info->subvol_sem);
2169 mutex_unlock(&inode->i_mutex);
2171 shrink_dcache_sb(root->fs_info->sb);
2172 btrfs_invalidate_inodes(dest);
2178 mutex_unlock(&dir->i_mutex);
2179 mnt_drop_write_file(file);
2185 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2187 struct inode *inode = file_inode(file);
2188 struct btrfs_root *root = BTRFS_I(inode)->root;
2189 struct btrfs_ioctl_defrag_range_args *range;
2192 ret = mnt_want_write_file(file);
2196 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2198 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2199 mnt_drop_write_file(file);
2203 if (btrfs_root_readonly(root)) {
2208 switch (inode->i_mode & S_IFMT) {
2210 if (!capable(CAP_SYS_ADMIN)) {
2214 ret = btrfs_defrag_root(root, 0);
2217 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2220 if (!(file->f_mode & FMODE_WRITE)) {
2225 range = kzalloc(sizeof(*range), GFP_KERNEL);
2232 if (copy_from_user(range, argp,
2238 /* compression requires us to start the IO */
2239 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2240 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2241 range->extent_thresh = (u32)-1;
2244 /* the rest are all set to zero by kzalloc */
2245 range->len = (u64)-1;
2247 ret = btrfs_defrag_file(file_inode(file), file,
2257 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2258 mnt_drop_write_file(file);
2262 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2264 struct btrfs_ioctl_vol_args *vol_args;
2267 if (!capable(CAP_SYS_ADMIN))
2270 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2272 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2276 mutex_lock(&root->fs_info->volume_mutex);
2277 vol_args = memdup_user(arg, sizeof(*vol_args));
2278 if (IS_ERR(vol_args)) {
2279 ret = PTR_ERR(vol_args);
2283 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2284 ret = btrfs_init_new_device(root, vol_args->name);
2288 mutex_unlock(&root->fs_info->volume_mutex);
2289 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2293 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2295 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2296 struct btrfs_ioctl_vol_args *vol_args;
2299 if (!capable(CAP_SYS_ADMIN))
2302 ret = mnt_want_write_file(file);
2306 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2308 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2309 mnt_drop_write_file(file);
2313 mutex_lock(&root->fs_info->volume_mutex);
2314 vol_args = memdup_user(arg, sizeof(*vol_args));
2315 if (IS_ERR(vol_args)) {
2316 ret = PTR_ERR(vol_args);
2320 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2321 ret = btrfs_rm_device(root, vol_args->name);
2325 mutex_unlock(&root->fs_info->volume_mutex);
2326 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2327 mnt_drop_write_file(file);
2331 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2333 struct btrfs_ioctl_fs_info_args *fi_args;
2334 struct btrfs_device *device;
2335 struct btrfs_device *next;
2336 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2339 if (!capable(CAP_SYS_ADMIN))
2342 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2346 fi_args->num_devices = fs_devices->num_devices;
2347 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2349 mutex_lock(&fs_devices->device_list_mutex);
2350 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2351 if (device->devid > fi_args->max_id)
2352 fi_args->max_id = device->devid;
2354 mutex_unlock(&fs_devices->device_list_mutex);
2356 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2363 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2365 struct btrfs_ioctl_dev_info_args *di_args;
2366 struct btrfs_device *dev;
2367 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2369 char *s_uuid = NULL;
2370 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2372 if (!capable(CAP_SYS_ADMIN))
2375 di_args = memdup_user(arg, sizeof(*di_args));
2376 if (IS_ERR(di_args))
2377 return PTR_ERR(di_args);
2379 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2380 s_uuid = di_args->uuid;
2382 mutex_lock(&fs_devices->device_list_mutex);
2383 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2384 mutex_unlock(&fs_devices->device_list_mutex);
2391 di_args->devid = dev->devid;
2392 di_args->bytes_used = dev->bytes_used;
2393 di_args->total_bytes = dev->total_bytes;
2394 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2396 struct rcu_string *name;
2399 name = rcu_dereference(dev->name);
2400 strncpy(di_args->path, name->str, sizeof(di_args->path));
2402 di_args->path[sizeof(di_args->path) - 1] = 0;
2404 di_args->path[0] = '\0';
2408 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2415 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2416 u64 off, u64 olen, u64 destoff)
2418 struct inode *inode = file_inode(file);
2419 struct btrfs_root *root = BTRFS_I(inode)->root;
2422 struct btrfs_trans_handle *trans;
2423 struct btrfs_path *path;
2424 struct extent_buffer *leaf;
2426 struct btrfs_key key;
2431 u64 bs = root->fs_info->sb->s_blocksize;
2435 * - split compressed inline extents. annoying: we need to
2436 * decompress into destination's address_space (the file offset
2437 * may change, so source mapping won't do), then recompress (or
2438 * otherwise reinsert) a subrange.
2439 * - allow ranges within the same file to be cloned (provided
2440 * they don't overlap)?
2443 /* the destination must be opened for writing */
2444 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2447 if (btrfs_root_readonly(root))
2450 ret = mnt_want_write_file(file);
2454 src_file = fdget(srcfd);
2455 if (!src_file.file) {
2457 goto out_drop_write;
2461 if (src_file.file->f_path.mnt != file->f_path.mnt)
2464 src = file_inode(src_file.file);
2470 /* the src must be open for reading */
2471 if (!(src_file.file->f_mode & FMODE_READ))
2474 /* don't make the dst file partly checksummed */
2475 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2476 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2480 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2484 if (src->i_sb != inode->i_sb)
2488 buf = vmalloc(btrfs_level_size(root, 0));
2492 path = btrfs_alloc_path();
2500 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2501 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2503 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2504 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2507 /* determine range to clone */
2509 if (off + len > src->i_size || off + len < off)
2512 olen = len = src->i_size - off;
2513 /* if we extend to eof, continue to block boundary */
2514 if (off + len == src->i_size)
2515 len = ALIGN(src->i_size, bs) - off;
2517 /* verify the end result is block aligned */
2518 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2519 !IS_ALIGNED(destoff, bs))
2522 if (destoff > inode->i_size) {
2523 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2528 /* truncate page cache pages from target inode range */
2529 truncate_inode_pages_range(&inode->i_data, destoff,
2530 PAGE_CACHE_ALIGN(destoff + len) - 1);
2532 /* do any pending delalloc/csum calc on src, one way or
2533 another, and lock file content */
2535 struct btrfs_ordered_extent *ordered;
2536 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2537 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
2539 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2540 EXTENT_DELALLOC, 0, NULL))
2542 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2544 btrfs_put_ordered_extent(ordered);
2545 btrfs_wait_ordered_range(src, off, len);
2549 key.objectid = btrfs_ino(src);
2550 key.type = BTRFS_EXTENT_DATA_KEY;
2555 * note the key will change type as we walk through the
2558 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2563 nritems = btrfs_header_nritems(path->nodes[0]);
2564 if (path->slots[0] >= nritems) {
2565 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2570 nritems = btrfs_header_nritems(path->nodes[0]);
2572 leaf = path->nodes[0];
2573 slot = path->slots[0];
2575 btrfs_item_key_to_cpu(leaf, &key, slot);
2576 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2577 key.objectid != btrfs_ino(src))
2580 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2581 struct btrfs_file_extent_item *extent;
2584 struct btrfs_key new_key;
2585 u64 disko = 0, diskl = 0;
2586 u64 datao = 0, datal = 0;
2590 size = btrfs_item_size_nr(leaf, slot);
2591 read_extent_buffer(leaf, buf,
2592 btrfs_item_ptr_offset(leaf, slot),
2595 extent = btrfs_item_ptr(leaf, slot,
2596 struct btrfs_file_extent_item);
2597 comp = btrfs_file_extent_compression(leaf, extent);
2598 type = btrfs_file_extent_type(leaf, extent);
2599 if (type == BTRFS_FILE_EXTENT_REG ||
2600 type == BTRFS_FILE_EXTENT_PREALLOC) {
2601 disko = btrfs_file_extent_disk_bytenr(leaf,
2603 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2605 datao = btrfs_file_extent_offset(leaf, extent);
2606 datal = btrfs_file_extent_num_bytes(leaf,
2608 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2609 /* take upper bound, may be compressed */
2610 datal = btrfs_file_extent_ram_bytes(leaf,
2613 btrfs_release_path(path);
2615 if (key.offset + datal <= off ||
2616 key.offset >= off + len - 1)
2619 memcpy(&new_key, &key, sizeof(new_key));
2620 new_key.objectid = btrfs_ino(inode);
2621 if (off <= key.offset)
2622 new_key.offset = key.offset + destoff - off;
2624 new_key.offset = destoff;
2627 * 1 - adjusting old extent (we may have to split it)
2628 * 1 - add new extent
2631 trans = btrfs_start_transaction(root, 3);
2632 if (IS_ERR(trans)) {
2633 ret = PTR_ERR(trans);
2637 if (type == BTRFS_FILE_EXTENT_REG ||
2638 type == BTRFS_FILE_EXTENT_PREALLOC) {
2640 * a | --- range to clone ---| b
2641 * | ------------- extent ------------- |
2644 /* substract range b */
2645 if (key.offset + datal > off + len)
2646 datal = off + len - key.offset;
2648 /* substract range a */
2649 if (off > key.offset) {
2650 datao += off - key.offset;
2651 datal -= off - key.offset;
2654 ret = btrfs_drop_extents(trans, root, inode,
2656 new_key.offset + datal,
2659 btrfs_abort_transaction(trans, root,
2661 btrfs_end_transaction(trans, root);
2665 ret = btrfs_insert_empty_item(trans, root, path,
2668 btrfs_abort_transaction(trans, root,
2670 btrfs_end_transaction(trans, root);
2674 leaf = path->nodes[0];
2675 slot = path->slots[0];
2676 write_extent_buffer(leaf, buf,
2677 btrfs_item_ptr_offset(leaf, slot),
2680 extent = btrfs_item_ptr(leaf, slot,
2681 struct btrfs_file_extent_item);
2683 /* disko == 0 means it's a hole */
2687 btrfs_set_file_extent_offset(leaf, extent,
2689 btrfs_set_file_extent_num_bytes(leaf, extent,
2692 inode_add_bytes(inode, datal);
2693 ret = btrfs_inc_extent_ref(trans, root,
2695 root->root_key.objectid,
2697 new_key.offset - datao,
2700 btrfs_abort_transaction(trans,
2703 btrfs_end_transaction(trans,
2709 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2712 if (off > key.offset) {
2713 skip = off - key.offset;
2714 new_key.offset += skip;
2717 if (key.offset + datal > off + len)
2718 trim = key.offset + datal - (off + len);
2720 if (comp && (skip || trim)) {
2722 btrfs_end_transaction(trans, root);
2725 size -= skip + trim;
2726 datal -= skip + trim;
2728 ret = btrfs_drop_extents(trans, root, inode,
2730 new_key.offset + datal,
2733 btrfs_abort_transaction(trans, root,
2735 btrfs_end_transaction(trans, root);
2739 ret = btrfs_insert_empty_item(trans, root, path,
2742 btrfs_abort_transaction(trans, root,
2744 btrfs_end_transaction(trans, root);
2750 btrfs_file_extent_calc_inline_size(0);
2751 memmove(buf+start, buf+start+skip,
2755 leaf = path->nodes[0];
2756 slot = path->slots[0];
2757 write_extent_buffer(leaf, buf,
2758 btrfs_item_ptr_offset(leaf, slot),
2760 inode_add_bytes(inode, datal);
2763 btrfs_mark_buffer_dirty(leaf);
2764 btrfs_release_path(path);
2766 inode_inc_iversion(inode);
2767 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2770 * we round up to the block size at eof when
2771 * determining which extents to clone above,
2772 * but shouldn't round up the file size
2774 endoff = new_key.offset + datal;
2775 if (endoff > destoff+olen)
2776 endoff = destoff+olen;
2777 if (endoff > inode->i_size)
2778 btrfs_i_size_write(inode, endoff);
2780 ret = btrfs_update_inode(trans, root, inode);
2782 btrfs_abort_transaction(trans, root, ret);
2783 btrfs_end_transaction(trans, root);
2786 ret = btrfs_end_transaction(trans, root);
2789 btrfs_release_path(path);
2794 btrfs_release_path(path);
2795 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2797 mutex_unlock(&src->i_mutex);
2798 mutex_unlock(&inode->i_mutex);
2800 btrfs_free_path(path);
2804 mnt_drop_write_file(file);
2808 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2810 struct btrfs_ioctl_clone_range_args args;
2812 if (copy_from_user(&args, argp, sizeof(args)))
2814 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2815 args.src_length, args.dest_offset);
2819 * there are many ways the trans_start and trans_end ioctls can lead
2820 * to deadlocks. They should only be used by applications that
2821 * basically own the machine, and have a very in depth understanding
2822 * of all the possible deadlocks and enospc problems.
2824 static long btrfs_ioctl_trans_start(struct file *file)
2826 struct inode *inode = file_inode(file);
2827 struct btrfs_root *root = BTRFS_I(inode)->root;
2828 struct btrfs_trans_handle *trans;
2832 if (!capable(CAP_SYS_ADMIN))
2836 if (file->private_data)
2840 if (btrfs_root_readonly(root))
2843 ret = mnt_want_write_file(file);
2847 atomic_inc(&root->fs_info->open_ioctl_trans);
2850 trans = btrfs_start_ioctl_transaction(root);
2854 file->private_data = trans;
2858 atomic_dec(&root->fs_info->open_ioctl_trans);
2859 mnt_drop_write_file(file);
2864 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2866 struct inode *inode = file_inode(file);
2867 struct btrfs_root *root = BTRFS_I(inode)->root;
2868 struct btrfs_root *new_root;
2869 struct btrfs_dir_item *di;
2870 struct btrfs_trans_handle *trans;
2871 struct btrfs_path *path;
2872 struct btrfs_key location;
2873 struct btrfs_disk_key disk_key;
2878 if (!capable(CAP_SYS_ADMIN))
2881 ret = mnt_want_write_file(file);
2885 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2891 objectid = root->root_key.objectid;
2893 location.objectid = objectid;
2894 location.type = BTRFS_ROOT_ITEM_KEY;
2895 location.offset = (u64)-1;
2897 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2898 if (IS_ERR(new_root)) {
2899 ret = PTR_ERR(new_root);
2903 if (btrfs_root_refs(&new_root->root_item) == 0) {
2908 path = btrfs_alloc_path();
2913 path->leave_spinning = 1;
2915 trans = btrfs_start_transaction(root, 1);
2916 if (IS_ERR(trans)) {
2917 btrfs_free_path(path);
2918 ret = PTR_ERR(trans);
2922 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
2923 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2924 dir_id, "default", 7, 1);
2925 if (IS_ERR_OR_NULL(di)) {
2926 btrfs_free_path(path);
2927 btrfs_end_transaction(trans, root);
2928 printk(KERN_ERR "Umm, you don't have the default dir item, "
2929 "this isn't going to work\n");
2934 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2935 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2936 btrfs_mark_buffer_dirty(path->nodes[0]);
2937 btrfs_free_path(path);
2939 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
2940 btrfs_end_transaction(trans, root);
2942 mnt_drop_write_file(file);
2946 void btrfs_get_block_group_info(struct list_head *groups_list,
2947 struct btrfs_ioctl_space_info *space)
2949 struct btrfs_block_group_cache *block_group;
2951 space->total_bytes = 0;
2952 space->used_bytes = 0;
2954 list_for_each_entry(block_group, groups_list, list) {
2955 space->flags = block_group->flags;
2956 space->total_bytes += block_group->key.offset;
2957 space->used_bytes +=
2958 btrfs_block_group_used(&block_group->item);
2962 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2964 struct btrfs_ioctl_space_args space_args;
2965 struct btrfs_ioctl_space_info space;
2966 struct btrfs_ioctl_space_info *dest;
2967 struct btrfs_ioctl_space_info *dest_orig;
2968 struct btrfs_ioctl_space_info __user *user_dest;
2969 struct btrfs_space_info *info;
2970 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2971 BTRFS_BLOCK_GROUP_SYSTEM,
2972 BTRFS_BLOCK_GROUP_METADATA,
2973 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2980 if (copy_from_user(&space_args,
2981 (struct btrfs_ioctl_space_args __user *)arg,
2982 sizeof(space_args)))
2985 for (i = 0; i < num_types; i++) {
2986 struct btrfs_space_info *tmp;
2990 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2992 if (tmp->flags == types[i]) {
3002 down_read(&info->groups_sem);
3003 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3004 if (!list_empty(&info->block_groups[c]))
3007 up_read(&info->groups_sem);
3010 /* space_slots == 0 means they are asking for a count */
3011 if (space_args.space_slots == 0) {
3012 space_args.total_spaces = slot_count;
3016 slot_count = min_t(u64, space_args.space_slots, slot_count);
3018 alloc_size = sizeof(*dest) * slot_count;
3020 /* we generally have at most 6 or so space infos, one for each raid
3021 * level. So, a whole page should be more than enough for everyone
3023 if (alloc_size > PAGE_CACHE_SIZE)
3026 space_args.total_spaces = 0;
3027 dest = kmalloc(alloc_size, GFP_NOFS);
3032 /* now we have a buffer to copy into */
3033 for (i = 0; i < num_types; i++) {
3034 struct btrfs_space_info *tmp;
3041 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3043 if (tmp->flags == types[i]) {
3052 down_read(&info->groups_sem);
3053 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3054 if (!list_empty(&info->block_groups[c])) {
3055 btrfs_get_block_group_info(
3056 &info->block_groups[c], &space);
3057 memcpy(dest, &space, sizeof(space));
3059 space_args.total_spaces++;
3065 up_read(&info->groups_sem);
3068 user_dest = (struct btrfs_ioctl_space_info __user *)
3069 (arg + sizeof(struct btrfs_ioctl_space_args));
3071 if (copy_to_user(user_dest, dest_orig, alloc_size))
3076 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3083 * there are many ways the trans_start and trans_end ioctls can lead
3084 * to deadlocks. They should only be used by applications that
3085 * basically own the machine, and have a very in depth understanding
3086 * of all the possible deadlocks and enospc problems.
3088 long btrfs_ioctl_trans_end(struct file *file)
3090 struct inode *inode = file_inode(file);
3091 struct btrfs_root *root = BTRFS_I(inode)->root;
3092 struct btrfs_trans_handle *trans;
3094 trans = file->private_data;
3097 file->private_data = NULL;
3099 btrfs_end_transaction(trans, root);
3101 atomic_dec(&root->fs_info->open_ioctl_trans);
3103 mnt_drop_write_file(file);
3107 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3110 struct btrfs_trans_handle *trans;
3114 trans = btrfs_attach_transaction(root);
3115 if (IS_ERR(trans)) {
3116 if (PTR_ERR(trans) != -ENOENT)
3117 return PTR_ERR(trans);
3119 /* No running transaction, don't bother */
3120 transid = root->fs_info->last_trans_committed;
3123 transid = trans->transid;
3124 ret = btrfs_commit_transaction_async(trans, root, 0);
3126 btrfs_end_transaction(trans, root);
3131 if (copy_to_user(argp, &transid, sizeof(transid)))
3136 static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3142 if (copy_from_user(&transid, argp, sizeof(transid)))
3145 transid = 0; /* current trans */
3147 return btrfs_wait_for_commit(root, transid);
3150 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3152 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3153 struct btrfs_ioctl_scrub_args *sa;
3156 if (!capable(CAP_SYS_ADMIN))
3159 sa = memdup_user(arg, sizeof(*sa));
3163 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3164 ret = mnt_want_write_file(file);
3169 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
3170 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3173 if (copy_to_user(arg, sa, sizeof(*sa)))
3176 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3177 mnt_drop_write_file(file);
3183 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3185 if (!capable(CAP_SYS_ADMIN))
3188 return btrfs_scrub_cancel(root->fs_info);
3191 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3194 struct btrfs_ioctl_scrub_args *sa;
3197 if (!capable(CAP_SYS_ADMIN))
3200 sa = memdup_user(arg, sizeof(*sa));
3204 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3206 if (copy_to_user(arg, sa, sizeof(*sa)))
3213 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3216 struct btrfs_ioctl_get_dev_stats *sa;
3219 sa = memdup_user(arg, sizeof(*sa));
3223 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3228 ret = btrfs_get_dev_stats(root, sa);
3230 if (copy_to_user(arg, sa, sizeof(*sa)))
3237 static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3239 struct btrfs_ioctl_dev_replace_args *p;
3242 if (!capable(CAP_SYS_ADMIN))
3245 p = memdup_user(arg, sizeof(*p));
3250 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3252 &root->fs_info->mutually_exclusive_operation_running,
3254 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3257 ret = btrfs_dev_replace_start(root, p);
3259 &root->fs_info->mutually_exclusive_operation_running,
3263 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3264 btrfs_dev_replace_status(root->fs_info, p);
3267 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3268 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3275 if (copy_to_user(arg, p, sizeof(*p)))
3282 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3288 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3289 struct inode_fs_paths *ipath = NULL;
3290 struct btrfs_path *path;
3292 if (!capable(CAP_SYS_ADMIN))
3295 path = btrfs_alloc_path();
3301 ipa = memdup_user(arg, sizeof(*ipa));
3308 size = min_t(u32, ipa->size, 4096);
3309 ipath = init_ipath(size, root, path);
3310 if (IS_ERR(ipath)) {
3311 ret = PTR_ERR(ipath);
3316 ret = paths_from_inode(ipa->inum, ipath);
3320 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3321 rel_ptr = ipath->fspath->val[i] -
3322 (u64)(unsigned long)ipath->fspath->val;
3323 ipath->fspath->val[i] = rel_ptr;
3326 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3327 (void *)(unsigned long)ipath->fspath, size);
3334 btrfs_free_path(path);
3341 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3343 struct btrfs_data_container *inodes = ctx;
3344 const size_t c = 3 * sizeof(u64);
3346 if (inodes->bytes_left >= c) {
3347 inodes->bytes_left -= c;
3348 inodes->val[inodes->elem_cnt] = inum;
3349 inodes->val[inodes->elem_cnt + 1] = offset;
3350 inodes->val[inodes->elem_cnt + 2] = root;
3351 inodes->elem_cnt += 3;
3353 inodes->bytes_missing += c - inodes->bytes_left;
3354 inodes->bytes_left = 0;
3355 inodes->elem_missed += 3;
3361 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3366 struct btrfs_ioctl_logical_ino_args *loi;
3367 struct btrfs_data_container *inodes = NULL;
3368 struct btrfs_path *path = NULL;
3370 if (!capable(CAP_SYS_ADMIN))
3373 loi = memdup_user(arg, sizeof(*loi));
3380 path = btrfs_alloc_path();
3386 size = min_t(u32, loi->size, 64 * 1024);
3387 inodes = init_data_container(size);
3388 if (IS_ERR(inodes)) {
3389 ret = PTR_ERR(inodes);
3394 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3395 build_ino_list, inodes);
3401 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3402 (void *)(unsigned long)inodes, size);
3407 btrfs_free_path(path);
3414 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3415 struct btrfs_ioctl_balance_args *bargs)
3417 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3419 bargs->flags = bctl->flags;
3421 if (atomic_read(&fs_info->balance_running))
3422 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3423 if (atomic_read(&fs_info->balance_pause_req))
3424 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3425 if (atomic_read(&fs_info->balance_cancel_req))
3426 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3428 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3429 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3430 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3433 spin_lock(&fs_info->balance_lock);
3434 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3435 spin_unlock(&fs_info->balance_lock);
3437 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3441 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3443 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3444 struct btrfs_fs_info *fs_info = root->fs_info;
3445 struct btrfs_ioctl_balance_args *bargs;
3446 struct btrfs_balance_control *bctl;
3447 bool need_unlock; /* for mut. excl. ops lock */
3450 if (!capable(CAP_SYS_ADMIN))
3453 ret = mnt_want_write_file(file);
3458 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3459 mutex_lock(&fs_info->volume_mutex);
3460 mutex_lock(&fs_info->balance_mutex);
3466 * mut. excl. ops lock is locked. Three possibilites:
3467 * (1) some other op is running
3468 * (2) balance is running
3469 * (3) balance is paused -- special case (think resume)
3471 mutex_lock(&fs_info->balance_mutex);
3472 if (fs_info->balance_ctl) {
3473 /* this is either (2) or (3) */
3474 if (!atomic_read(&fs_info->balance_running)) {
3475 mutex_unlock(&fs_info->balance_mutex);
3476 if (!mutex_trylock(&fs_info->volume_mutex))
3478 mutex_lock(&fs_info->balance_mutex);
3480 if (fs_info->balance_ctl &&
3481 !atomic_read(&fs_info->balance_running)) {
3483 need_unlock = false;
3487 mutex_unlock(&fs_info->balance_mutex);
3488 mutex_unlock(&fs_info->volume_mutex);
3492 mutex_unlock(&fs_info->balance_mutex);
3498 mutex_unlock(&fs_info->balance_mutex);
3499 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3505 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3508 bargs = memdup_user(arg, sizeof(*bargs));
3509 if (IS_ERR(bargs)) {
3510 ret = PTR_ERR(bargs);
3514 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3515 if (!fs_info->balance_ctl) {
3520 bctl = fs_info->balance_ctl;
3521 spin_lock(&fs_info->balance_lock);
3522 bctl->flags |= BTRFS_BALANCE_RESUME;
3523 spin_unlock(&fs_info->balance_lock);
3531 if (fs_info->balance_ctl) {
3536 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3542 bctl->fs_info = fs_info;
3544 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3545 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3546 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3548 bctl->flags = bargs->flags;
3550 /* balance everything - no filters */
3551 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3556 * Ownership of bctl and mutually_exclusive_operation_running
3557 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3558 * or, if restriper was paused all the way until unmount, in
3559 * free_fs_info. mutually_exclusive_operation_running is
3560 * cleared in __cancel_balance.
3562 need_unlock = false;
3564 ret = btrfs_balance(bctl, bargs);
3567 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3574 mutex_unlock(&fs_info->balance_mutex);
3575 mutex_unlock(&fs_info->volume_mutex);
3577 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3579 mnt_drop_write_file(file);
3583 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3585 if (!capable(CAP_SYS_ADMIN))
3589 case BTRFS_BALANCE_CTL_PAUSE:
3590 return btrfs_pause_balance(root->fs_info);
3591 case BTRFS_BALANCE_CTL_CANCEL:
3592 return btrfs_cancel_balance(root->fs_info);
3598 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3601 struct btrfs_fs_info *fs_info = root->fs_info;
3602 struct btrfs_ioctl_balance_args *bargs;
3605 if (!capable(CAP_SYS_ADMIN))
3608 mutex_lock(&fs_info->balance_mutex);
3609 if (!fs_info->balance_ctl) {
3614 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3620 update_ioctl_balance_args(fs_info, 1, bargs);
3622 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3627 mutex_unlock(&fs_info->balance_mutex);
3631 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
3633 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3634 struct btrfs_ioctl_quota_ctl_args *sa;
3635 struct btrfs_trans_handle *trans = NULL;
3639 if (!capable(CAP_SYS_ADMIN))
3642 ret = mnt_want_write_file(file);
3646 sa = memdup_user(arg, sizeof(*sa));
3652 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3653 trans = btrfs_start_transaction(root, 2);
3654 if (IS_ERR(trans)) {
3655 ret = PTR_ERR(trans);
3661 case BTRFS_QUOTA_CTL_ENABLE:
3662 ret = btrfs_quota_enable(trans, root->fs_info);
3664 case BTRFS_QUOTA_CTL_DISABLE:
3665 ret = btrfs_quota_disable(trans, root->fs_info);
3667 case BTRFS_QUOTA_CTL_RESCAN:
3668 ret = btrfs_quota_rescan(root->fs_info);
3675 if (copy_to_user(arg, sa, sizeof(*sa)))
3679 err = btrfs_commit_transaction(trans, root);
3686 mnt_drop_write_file(file);
3690 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
3692 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3693 struct btrfs_ioctl_qgroup_assign_args *sa;
3694 struct btrfs_trans_handle *trans;
3698 if (!capable(CAP_SYS_ADMIN))
3701 ret = mnt_want_write_file(file);
3705 sa = memdup_user(arg, sizeof(*sa));
3711 trans = btrfs_join_transaction(root);
3712 if (IS_ERR(trans)) {
3713 ret = PTR_ERR(trans);
3717 /* FIXME: check if the IDs really exist */
3719 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3722 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3726 err = btrfs_end_transaction(trans, root);
3733 mnt_drop_write_file(file);
3737 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
3739 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3740 struct btrfs_ioctl_qgroup_create_args *sa;
3741 struct btrfs_trans_handle *trans;
3745 if (!capable(CAP_SYS_ADMIN))
3748 ret = mnt_want_write_file(file);
3752 sa = memdup_user(arg, sizeof(*sa));
3758 if (!sa->qgroupid) {
3763 trans = btrfs_join_transaction(root);
3764 if (IS_ERR(trans)) {
3765 ret = PTR_ERR(trans);
3769 /* FIXME: check if the IDs really exist */
3771 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3774 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3777 err = btrfs_end_transaction(trans, root);
3784 mnt_drop_write_file(file);
3788 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
3790 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3791 struct btrfs_ioctl_qgroup_limit_args *sa;
3792 struct btrfs_trans_handle *trans;
3797 if (!capable(CAP_SYS_ADMIN))
3800 ret = mnt_want_write_file(file);
3804 sa = memdup_user(arg, sizeof(*sa));
3810 trans = btrfs_join_transaction(root);
3811 if (IS_ERR(trans)) {
3812 ret = PTR_ERR(trans);
3816 qgroupid = sa->qgroupid;
3818 /* take the current subvol as qgroup */
3819 qgroupid = root->root_key.objectid;
3822 /* FIXME: check if the IDs really exist */
3823 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3825 err = btrfs_end_transaction(trans, root);
3832 mnt_drop_write_file(file);
3836 static long btrfs_ioctl_set_received_subvol(struct file *file,
3839 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3840 struct inode *inode = file_inode(file);
3841 struct btrfs_root *root = BTRFS_I(inode)->root;
3842 struct btrfs_root_item *root_item = &root->root_item;
3843 struct btrfs_trans_handle *trans;
3844 struct timespec ct = CURRENT_TIME;
3847 ret = mnt_want_write_file(file);
3851 down_write(&root->fs_info->subvol_sem);
3853 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3858 if (btrfs_root_readonly(root)) {
3863 if (!inode_owner_or_capable(inode)) {
3868 sa = memdup_user(arg, sizeof(*sa));
3875 trans = btrfs_start_transaction(root, 1);
3876 if (IS_ERR(trans)) {
3877 ret = PTR_ERR(trans);
3882 sa->rtransid = trans->transid;
3883 sa->rtime.sec = ct.tv_sec;
3884 sa->rtime.nsec = ct.tv_nsec;
3886 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3887 btrfs_set_root_stransid(root_item, sa->stransid);
3888 btrfs_set_root_rtransid(root_item, sa->rtransid);
3889 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3890 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3891 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3892 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3894 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3895 &root->root_key, &root->root_item);
3897 btrfs_end_transaction(trans, root);
3901 ret = btrfs_commit_transaction(trans, root);
3906 ret = copy_to_user(arg, sa, sizeof(*sa));
3912 up_write(&root->fs_info->subvol_sem);
3913 mnt_drop_write_file(file);
3917 long btrfs_ioctl(struct file *file, unsigned int
3918 cmd, unsigned long arg)
3920 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3921 void __user *argp = (void __user *)arg;
3924 case FS_IOC_GETFLAGS:
3925 return btrfs_ioctl_getflags(file, argp);
3926 case FS_IOC_SETFLAGS:
3927 return btrfs_ioctl_setflags(file, argp);
3928 case FS_IOC_GETVERSION:
3929 return btrfs_ioctl_getversion(file, argp);
3931 return btrfs_ioctl_fitrim(file, argp);
3932 case BTRFS_IOC_SNAP_CREATE:
3933 return btrfs_ioctl_snap_create(file, argp, 0);
3934 case BTRFS_IOC_SNAP_CREATE_V2:
3935 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3936 case BTRFS_IOC_SUBVOL_CREATE:
3937 return btrfs_ioctl_snap_create(file, argp, 1);
3938 case BTRFS_IOC_SUBVOL_CREATE_V2:
3939 return btrfs_ioctl_snap_create_v2(file, argp, 1);
3940 case BTRFS_IOC_SNAP_DESTROY:
3941 return btrfs_ioctl_snap_destroy(file, argp);
3942 case BTRFS_IOC_SUBVOL_GETFLAGS:
3943 return btrfs_ioctl_subvol_getflags(file, argp);
3944 case BTRFS_IOC_SUBVOL_SETFLAGS:
3945 return btrfs_ioctl_subvol_setflags(file, argp);
3946 case BTRFS_IOC_DEFAULT_SUBVOL:
3947 return btrfs_ioctl_default_subvol(file, argp);
3948 case BTRFS_IOC_DEFRAG:
3949 return btrfs_ioctl_defrag(file, NULL);
3950 case BTRFS_IOC_DEFRAG_RANGE:
3951 return btrfs_ioctl_defrag(file, argp);
3952 case BTRFS_IOC_RESIZE:
3953 return btrfs_ioctl_resize(file, argp);
3954 case BTRFS_IOC_ADD_DEV:
3955 return btrfs_ioctl_add_dev(root, argp);
3956 case BTRFS_IOC_RM_DEV:
3957 return btrfs_ioctl_rm_dev(file, argp);
3958 case BTRFS_IOC_FS_INFO:
3959 return btrfs_ioctl_fs_info(root, argp);
3960 case BTRFS_IOC_DEV_INFO:
3961 return btrfs_ioctl_dev_info(root, argp);
3962 case BTRFS_IOC_BALANCE:
3963 return btrfs_ioctl_balance(file, NULL);
3964 case BTRFS_IOC_CLONE:
3965 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3966 case BTRFS_IOC_CLONE_RANGE:
3967 return btrfs_ioctl_clone_range(file, argp);
3968 case BTRFS_IOC_TRANS_START:
3969 return btrfs_ioctl_trans_start(file);
3970 case BTRFS_IOC_TRANS_END:
3971 return btrfs_ioctl_trans_end(file);
3972 case BTRFS_IOC_TREE_SEARCH:
3973 return btrfs_ioctl_tree_search(file, argp);
3974 case BTRFS_IOC_INO_LOOKUP:
3975 return btrfs_ioctl_ino_lookup(file, argp);
3976 case BTRFS_IOC_INO_PATHS:
3977 return btrfs_ioctl_ino_to_path(root, argp);
3978 case BTRFS_IOC_LOGICAL_INO:
3979 return btrfs_ioctl_logical_to_ino(root, argp);
3980 case BTRFS_IOC_SPACE_INFO:
3981 return btrfs_ioctl_space_info(root, argp);
3982 case BTRFS_IOC_SYNC:
3983 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3985 case BTRFS_IOC_START_SYNC:
3986 return btrfs_ioctl_start_sync(root, argp);
3987 case BTRFS_IOC_WAIT_SYNC:
3988 return btrfs_ioctl_wait_sync(root, argp);
3989 case BTRFS_IOC_SCRUB:
3990 return btrfs_ioctl_scrub(file, argp);
3991 case BTRFS_IOC_SCRUB_CANCEL:
3992 return btrfs_ioctl_scrub_cancel(root, argp);
3993 case BTRFS_IOC_SCRUB_PROGRESS:
3994 return btrfs_ioctl_scrub_progress(root, argp);
3995 case BTRFS_IOC_BALANCE_V2:
3996 return btrfs_ioctl_balance(file, argp);
3997 case BTRFS_IOC_BALANCE_CTL:
3998 return btrfs_ioctl_balance_ctl(root, arg);
3999 case BTRFS_IOC_BALANCE_PROGRESS:
4000 return btrfs_ioctl_balance_progress(root, argp);
4001 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4002 return btrfs_ioctl_set_received_subvol(file, argp);
4003 case BTRFS_IOC_SEND:
4004 return btrfs_ioctl_send(file, argp);
4005 case BTRFS_IOC_GET_DEV_STATS:
4006 return btrfs_ioctl_get_dev_stats(root, argp);
4007 case BTRFS_IOC_QUOTA_CTL:
4008 return btrfs_ioctl_quota_ctl(file, argp);
4009 case BTRFS_IOC_QGROUP_ASSIGN:
4010 return btrfs_ioctl_qgroup_assign(file, argp);
4011 case BTRFS_IOC_QGROUP_CREATE:
4012 return btrfs_ioctl_qgroup_create(file, argp);
4013 case BTRFS_IOC_QGROUP_LIMIT:
4014 return btrfs_ioctl_qgroup_limit(file, argp);
4015 case BTRFS_IOC_DEV_REPLACE:
4016 return btrfs_ioctl_dev_replace(root, argp);