Linux 3.12.28
[linux/fpc-iii.git] / fs / btrfs / ioctl.c
blobad6a08c5801ecd04716c89f85d9d7d11bd052007
1 /*
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>
23 #include <linux/fs.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>
45 #include <linux/btrfs.h>
46 #include <linux/uaccess.h>
47 #include "compat.h"
48 #include "ctree.h"
49 #include "disk-io.h"
50 #include "transaction.h"
51 #include "btrfs_inode.h"
52 #include "print-tree.h"
53 #include "volumes.h"
54 #include "locking.h"
55 #include "inode-map.h"
56 #include "backref.h"
57 #include "rcu-string.h"
58 #include "send.h"
59 #include "dev-replace.h"
61 static int btrfs_clone(struct inode *src, struct inode *inode,
62 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
64 /* Mask out flags that are inappropriate for the given type of inode. */
65 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
67 if (S_ISDIR(mode))
68 return flags;
69 else if (S_ISREG(mode))
70 return flags & ~FS_DIRSYNC_FL;
71 else
72 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
76 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
78 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
80 unsigned int iflags = 0;
82 if (flags & BTRFS_INODE_SYNC)
83 iflags |= FS_SYNC_FL;
84 if (flags & BTRFS_INODE_IMMUTABLE)
85 iflags |= FS_IMMUTABLE_FL;
86 if (flags & BTRFS_INODE_APPEND)
87 iflags |= FS_APPEND_FL;
88 if (flags & BTRFS_INODE_NODUMP)
89 iflags |= FS_NODUMP_FL;
90 if (flags & BTRFS_INODE_NOATIME)
91 iflags |= FS_NOATIME_FL;
92 if (flags & BTRFS_INODE_DIRSYNC)
93 iflags |= FS_DIRSYNC_FL;
94 if (flags & BTRFS_INODE_NODATACOW)
95 iflags |= FS_NOCOW_FL;
97 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98 iflags |= FS_COMPR_FL;
99 else if (flags & BTRFS_INODE_NOCOMPRESS)
100 iflags |= FS_NOCOMP_FL;
102 return iflags;
106 * Update inode->i_flags based on the btrfs internal flags.
108 void btrfs_update_iflags(struct inode *inode)
110 struct btrfs_inode *ip = BTRFS_I(inode);
112 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
114 if (ip->flags & BTRFS_INODE_SYNC)
115 inode->i_flags |= S_SYNC;
116 if (ip->flags & BTRFS_INODE_IMMUTABLE)
117 inode->i_flags |= S_IMMUTABLE;
118 if (ip->flags & BTRFS_INODE_APPEND)
119 inode->i_flags |= S_APPEND;
120 if (ip->flags & BTRFS_INODE_NOATIME)
121 inode->i_flags |= S_NOATIME;
122 if (ip->flags & BTRFS_INODE_DIRSYNC)
123 inode->i_flags |= S_DIRSYNC;
127 * Inherit flags from the parent inode.
129 * Currently only the compression flags and the cow flags are inherited.
131 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
133 unsigned int flags;
135 if (!dir)
136 return;
138 flags = BTRFS_I(dir)->flags;
140 if (flags & BTRFS_INODE_NOCOMPRESS) {
141 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143 } else if (flags & BTRFS_INODE_COMPRESS) {
144 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
148 if (flags & BTRFS_INODE_NODATACOW) {
149 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
150 if (S_ISREG(inode->i_mode))
151 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
154 btrfs_update_iflags(inode);
157 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
159 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
167 static int check_flags(unsigned int flags)
169 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
170 FS_NOATIME_FL | FS_NODUMP_FL | \
171 FS_SYNC_FL | FS_DIRSYNC_FL | \
172 FS_NOCOMP_FL | FS_COMPR_FL |
173 FS_NOCOW_FL))
174 return -EOPNOTSUPP;
176 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
177 return -EINVAL;
179 return 0;
182 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
184 struct inode *inode = file_inode(file);
185 struct btrfs_inode *ip = BTRFS_I(inode);
186 struct btrfs_root *root = ip->root;
187 struct btrfs_trans_handle *trans;
188 unsigned int flags, oldflags;
189 int ret;
190 u64 ip_oldflags;
191 unsigned int i_oldflags;
192 umode_t mode;
194 if (btrfs_root_readonly(root))
195 return -EROFS;
197 if (copy_from_user(&flags, arg, sizeof(flags)))
198 return -EFAULT;
200 ret = check_flags(flags);
201 if (ret)
202 return ret;
204 if (!inode_owner_or_capable(inode))
205 return -EACCES;
207 ret = mnt_want_write_file(file);
208 if (ret)
209 return ret;
211 mutex_lock(&inode->i_mutex);
213 ip_oldflags = ip->flags;
214 i_oldflags = inode->i_flags;
215 mode = inode->i_mode;
217 flags = btrfs_mask_flags(inode->i_mode, flags);
218 oldflags = btrfs_flags_to_ioctl(ip->flags);
219 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
220 if (!capable(CAP_LINUX_IMMUTABLE)) {
221 ret = -EPERM;
222 goto out_unlock;
226 if (flags & FS_SYNC_FL)
227 ip->flags |= BTRFS_INODE_SYNC;
228 else
229 ip->flags &= ~BTRFS_INODE_SYNC;
230 if (flags & FS_IMMUTABLE_FL)
231 ip->flags |= BTRFS_INODE_IMMUTABLE;
232 else
233 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
234 if (flags & FS_APPEND_FL)
235 ip->flags |= BTRFS_INODE_APPEND;
236 else
237 ip->flags &= ~BTRFS_INODE_APPEND;
238 if (flags & FS_NODUMP_FL)
239 ip->flags |= BTRFS_INODE_NODUMP;
240 else
241 ip->flags &= ~BTRFS_INODE_NODUMP;
242 if (flags & FS_NOATIME_FL)
243 ip->flags |= BTRFS_INODE_NOATIME;
244 else
245 ip->flags &= ~BTRFS_INODE_NOATIME;
246 if (flags & FS_DIRSYNC_FL)
247 ip->flags |= BTRFS_INODE_DIRSYNC;
248 else
249 ip->flags &= ~BTRFS_INODE_DIRSYNC;
250 if (flags & FS_NOCOW_FL) {
251 if (S_ISREG(mode)) {
253 * It's safe to turn csums off here, no extents exist.
254 * Otherwise we want the flag to reflect the real COW
255 * status of the file and will not set it.
257 if (inode->i_size == 0)
258 ip->flags |= BTRFS_INODE_NODATACOW
259 | BTRFS_INODE_NODATASUM;
260 } else {
261 ip->flags |= BTRFS_INODE_NODATACOW;
263 } else {
265 * Revert back under same assuptions as above
267 if (S_ISREG(mode)) {
268 if (inode->i_size == 0)
269 ip->flags &= ~(BTRFS_INODE_NODATACOW
270 | BTRFS_INODE_NODATASUM);
271 } else {
272 ip->flags &= ~BTRFS_INODE_NODATACOW;
277 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
278 * flag may be changed automatically if compression code won't make
279 * things smaller.
281 if (flags & FS_NOCOMP_FL) {
282 ip->flags &= ~BTRFS_INODE_COMPRESS;
283 ip->flags |= BTRFS_INODE_NOCOMPRESS;
284 } else if (flags & FS_COMPR_FL) {
285 ip->flags |= BTRFS_INODE_COMPRESS;
286 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
287 } else {
288 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
291 trans = btrfs_start_transaction(root, 1);
292 if (IS_ERR(trans)) {
293 ret = PTR_ERR(trans);
294 goto out_drop;
297 btrfs_update_iflags(inode);
298 inode_inc_iversion(inode);
299 inode->i_ctime = CURRENT_TIME;
300 ret = btrfs_update_inode(trans, root, inode);
302 btrfs_end_transaction(trans, root);
303 out_drop:
304 if (ret) {
305 ip->flags = ip_oldflags;
306 inode->i_flags = i_oldflags;
309 out_unlock:
310 mutex_unlock(&inode->i_mutex);
311 mnt_drop_write_file(file);
312 return ret;
315 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
317 struct inode *inode = file_inode(file);
319 return put_user(inode->i_generation, arg);
322 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
324 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
325 struct btrfs_device *device;
326 struct request_queue *q;
327 struct fstrim_range range;
328 u64 minlen = ULLONG_MAX;
329 u64 num_devices = 0;
330 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
331 int ret;
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
336 rcu_read_lock();
337 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
338 dev_list) {
339 if (!device->bdev)
340 continue;
341 q = bdev_get_queue(device->bdev);
342 if (blk_queue_discard(q)) {
343 num_devices++;
344 minlen = min((u64)q->limits.discard_granularity,
345 minlen);
348 rcu_read_unlock();
350 if (!num_devices)
351 return -EOPNOTSUPP;
352 if (copy_from_user(&range, arg, sizeof(range)))
353 return -EFAULT;
354 if (range.start > total_bytes ||
355 range.len < fs_info->sb->s_blocksize)
356 return -EINVAL;
358 range.len = min(range.len, total_bytes - range.start);
359 range.minlen = max(range.minlen, minlen);
360 ret = btrfs_trim_fs(fs_info->tree_root, &range);
361 if (ret < 0)
362 return ret;
364 if (copy_to_user(arg, &range, sizeof(range)))
365 return -EFAULT;
367 return 0;
370 int btrfs_is_empty_uuid(u8 *uuid)
372 static char empty_uuid[BTRFS_UUID_SIZE] = {0};
374 return !memcmp(uuid, empty_uuid, BTRFS_UUID_SIZE);
377 static noinline int create_subvol(struct inode *dir,
378 struct dentry *dentry,
379 char *name, int namelen,
380 u64 *async_transid,
381 struct btrfs_qgroup_inherit *inherit)
383 struct btrfs_trans_handle *trans;
384 struct btrfs_key key;
385 struct btrfs_root_item root_item;
386 struct btrfs_inode_item *inode_item;
387 struct extent_buffer *leaf;
388 struct btrfs_root *root = BTRFS_I(dir)->root;
389 struct btrfs_root *new_root;
390 struct btrfs_block_rsv block_rsv;
391 struct timespec cur_time = CURRENT_TIME;
392 int ret;
393 int err;
394 u64 objectid;
395 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
396 u64 index = 0;
397 u64 qgroup_reserved;
398 uuid_le new_uuid;
400 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
401 if (ret)
402 return ret;
404 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
406 * The same as the snapshot creation, please see the comment
407 * of create_snapshot().
409 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
410 8, &qgroup_reserved, false);
411 if (ret)
412 return ret;
414 trans = btrfs_start_transaction(root, 0);
415 if (IS_ERR(trans)) {
416 ret = PTR_ERR(trans);
417 goto out;
419 trans->block_rsv = &block_rsv;
420 trans->bytes_reserved = block_rsv.size;
422 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
423 if (ret)
424 goto fail;
426 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
427 0, objectid, NULL, 0, 0, 0);
428 if (IS_ERR(leaf)) {
429 ret = PTR_ERR(leaf);
430 goto fail;
433 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
434 btrfs_set_header_bytenr(leaf, leaf->start);
435 btrfs_set_header_generation(leaf, trans->transid);
436 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
437 btrfs_set_header_owner(leaf, objectid);
439 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(leaf),
440 BTRFS_FSID_SIZE);
441 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
442 btrfs_header_chunk_tree_uuid(leaf),
443 BTRFS_UUID_SIZE);
444 btrfs_mark_buffer_dirty(leaf);
446 memset(&root_item, 0, sizeof(root_item));
448 inode_item = &root_item.inode;
449 btrfs_set_stack_inode_generation(inode_item, 1);
450 btrfs_set_stack_inode_size(inode_item, 3);
451 btrfs_set_stack_inode_nlink(inode_item, 1);
452 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
453 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
455 btrfs_set_root_flags(&root_item, 0);
456 btrfs_set_root_limit(&root_item, 0);
457 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
459 btrfs_set_root_bytenr(&root_item, leaf->start);
460 btrfs_set_root_generation(&root_item, trans->transid);
461 btrfs_set_root_level(&root_item, 0);
462 btrfs_set_root_refs(&root_item, 1);
463 btrfs_set_root_used(&root_item, leaf->len);
464 btrfs_set_root_last_snapshot(&root_item, 0);
466 btrfs_set_root_generation_v2(&root_item,
467 btrfs_root_generation(&root_item));
468 uuid_le_gen(&new_uuid);
469 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
470 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
471 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
472 root_item.ctime = root_item.otime;
473 btrfs_set_root_ctransid(&root_item, trans->transid);
474 btrfs_set_root_otransid(&root_item, trans->transid);
476 btrfs_tree_unlock(leaf);
477 free_extent_buffer(leaf);
478 leaf = NULL;
480 btrfs_set_root_dirid(&root_item, new_dirid);
482 key.objectid = objectid;
483 key.offset = 0;
484 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
485 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
486 &root_item);
487 if (ret)
488 goto fail;
490 key.offset = (u64)-1;
491 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
492 if (IS_ERR(new_root)) {
493 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
494 ret = PTR_ERR(new_root);
495 goto fail;
498 btrfs_record_root_in_trans(trans, new_root);
500 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
501 if (ret) {
502 /* We potentially lose an unused inode item here */
503 btrfs_abort_transaction(trans, root, ret);
504 goto fail;
508 * insert the directory item
510 ret = btrfs_set_inode_index(dir, &index);
511 if (ret) {
512 btrfs_abort_transaction(trans, root, ret);
513 goto fail;
516 ret = btrfs_insert_dir_item(trans, root,
517 name, namelen, dir, &key,
518 BTRFS_FT_DIR, index);
519 if (ret) {
520 btrfs_abort_transaction(trans, root, ret);
521 goto fail;
524 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
525 ret = btrfs_update_inode(trans, root, dir);
526 BUG_ON(ret);
528 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
529 objectid, root->root_key.objectid,
530 btrfs_ino(dir), index, name, namelen);
531 BUG_ON(ret);
533 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
534 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
535 objectid);
536 if (ret)
537 btrfs_abort_transaction(trans, root, ret);
539 fail:
540 trans->block_rsv = NULL;
541 trans->bytes_reserved = 0;
542 if (async_transid) {
543 *async_transid = trans->transid;
544 err = btrfs_commit_transaction_async(trans, root, 1);
545 if (err)
546 err = btrfs_commit_transaction(trans, root);
547 } else {
548 err = btrfs_commit_transaction(trans, root);
550 if (err && !ret)
551 ret = err;
553 if (!ret)
554 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
555 out:
556 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
557 return ret;
560 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
561 struct dentry *dentry, char *name, int namelen,
562 u64 *async_transid, bool readonly,
563 struct btrfs_qgroup_inherit *inherit)
565 struct inode *inode;
566 struct btrfs_pending_snapshot *pending_snapshot;
567 struct btrfs_trans_handle *trans;
568 int ret;
570 if (!root->ref_cows)
571 return -EINVAL;
573 ret = btrfs_start_delalloc_inodes(root, 0);
574 if (ret)
575 return ret;
577 btrfs_wait_ordered_extents(root);
579 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
580 if (!pending_snapshot)
581 return -ENOMEM;
583 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
584 BTRFS_BLOCK_RSV_TEMP);
586 * 1 - parent dir inode
587 * 2 - dir entries
588 * 1 - root item
589 * 2 - root ref/backref
590 * 1 - root of snapshot
591 * 1 - UUID item
593 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
594 &pending_snapshot->block_rsv, 8,
595 &pending_snapshot->qgroup_reserved,
596 false);
597 if (ret)
598 goto out;
600 pending_snapshot->dentry = dentry;
601 pending_snapshot->root = root;
602 pending_snapshot->readonly = readonly;
603 pending_snapshot->dir = dir;
604 pending_snapshot->inherit = inherit;
606 trans = btrfs_start_transaction(root, 0);
607 if (IS_ERR(trans)) {
608 ret = PTR_ERR(trans);
609 goto fail;
612 spin_lock(&root->fs_info->trans_lock);
613 list_add(&pending_snapshot->list,
614 &trans->transaction->pending_snapshots);
615 spin_unlock(&root->fs_info->trans_lock);
616 if (async_transid) {
617 *async_transid = trans->transid;
618 ret = btrfs_commit_transaction_async(trans,
619 root->fs_info->extent_root, 1);
620 if (ret)
621 ret = btrfs_commit_transaction(trans, root);
622 } else {
623 ret = btrfs_commit_transaction(trans,
624 root->fs_info->extent_root);
626 if (ret)
627 goto fail;
629 ret = pending_snapshot->error;
630 if (ret)
631 goto fail;
633 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
634 if (ret)
635 goto fail;
637 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
638 if (IS_ERR(inode)) {
639 ret = PTR_ERR(inode);
640 goto fail;
642 BUG_ON(!inode);
643 d_instantiate(dentry, inode);
644 ret = 0;
645 fail:
646 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
647 &pending_snapshot->block_rsv,
648 pending_snapshot->qgroup_reserved);
649 out:
650 kfree(pending_snapshot);
651 return ret;
654 /* copy of check_sticky in fs/namei.c()
655 * It's inline, so penalty for filesystems that don't use sticky bit is
656 * minimal.
658 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
660 kuid_t fsuid = current_fsuid();
662 if (!(dir->i_mode & S_ISVTX))
663 return 0;
664 if (uid_eq(inode->i_uid, fsuid))
665 return 0;
666 if (uid_eq(dir->i_uid, fsuid))
667 return 0;
668 return !capable(CAP_FOWNER);
671 /* copy of may_delete in fs/namei.c()
672 * Check whether we can remove a link victim from directory dir, check
673 * whether the type of victim is right.
674 * 1. We can't do it if dir is read-only (done in permission())
675 * 2. We should have write and exec permissions on dir
676 * 3. We can't remove anything from append-only dir
677 * 4. We can't do anything with immutable dir (done in permission())
678 * 5. If the sticky bit on dir is set we should either
679 * a. be owner of dir, or
680 * b. be owner of victim, or
681 * c. have CAP_FOWNER capability
682 * 6. If the victim is append-only or immutable we can't do antyhing with
683 * links pointing to it.
684 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
685 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
686 * 9. We can't remove a root or mountpoint.
687 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
688 * nfs_async_unlink().
691 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
693 int error;
695 if (!victim->d_inode)
696 return -ENOENT;
698 BUG_ON(victim->d_parent->d_inode != dir);
699 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
701 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
702 if (error)
703 return error;
704 if (IS_APPEND(dir))
705 return -EPERM;
706 if (btrfs_check_sticky(dir, victim->d_inode)||
707 IS_APPEND(victim->d_inode)||
708 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
709 return -EPERM;
710 if (isdir) {
711 if (!S_ISDIR(victim->d_inode->i_mode))
712 return -ENOTDIR;
713 if (IS_ROOT(victim))
714 return -EBUSY;
715 } else if (S_ISDIR(victim->d_inode->i_mode))
716 return -EISDIR;
717 if (IS_DEADDIR(dir))
718 return -ENOENT;
719 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
720 return -EBUSY;
721 return 0;
724 /* copy of may_create in fs/namei.c() */
725 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
727 if (child->d_inode)
728 return -EEXIST;
729 if (IS_DEADDIR(dir))
730 return -ENOENT;
731 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
735 * Create a new subvolume below @parent. This is largely modeled after
736 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
737 * inside this filesystem so it's quite a bit simpler.
739 static noinline int btrfs_mksubvol(struct path *parent,
740 char *name, int namelen,
741 struct btrfs_root *snap_src,
742 u64 *async_transid, bool readonly,
743 struct btrfs_qgroup_inherit *inherit)
745 struct inode *dir = parent->dentry->d_inode;
746 struct dentry *dentry;
747 int error;
749 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
750 if (error == -EINTR)
751 return error;
753 dentry = lookup_one_len(name, parent->dentry, namelen);
754 error = PTR_ERR(dentry);
755 if (IS_ERR(dentry))
756 goto out_unlock;
758 error = -EEXIST;
759 if (dentry->d_inode)
760 goto out_dput;
762 error = btrfs_may_create(dir, dentry);
763 if (error)
764 goto out_dput;
767 * even if this name doesn't exist, we may get hash collisions.
768 * check for them now when we can safely fail
770 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
771 dir->i_ino, name,
772 namelen);
773 if (error)
774 goto out_dput;
776 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
778 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
779 goto out_up_read;
781 if (snap_src) {
782 error = create_snapshot(snap_src, dir, dentry, name, namelen,
783 async_transid, readonly, inherit);
784 } else {
785 error = create_subvol(dir, dentry, name, namelen,
786 async_transid, inherit);
788 if (!error)
789 fsnotify_mkdir(dir, dentry);
790 out_up_read:
791 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
792 out_dput:
793 dput(dentry);
794 out_unlock:
795 mutex_unlock(&dir->i_mutex);
796 return error;
800 * When we're defragging a range, we don't want to kick it off again
801 * if it is really just waiting for delalloc to send it down.
802 * If we find a nice big extent or delalloc range for the bytes in the
803 * file you want to defrag, we return 0 to let you know to skip this
804 * part of the file
806 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
808 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
809 struct extent_map *em = NULL;
810 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
811 u64 end;
813 read_lock(&em_tree->lock);
814 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
815 read_unlock(&em_tree->lock);
817 if (em) {
818 end = extent_map_end(em);
819 free_extent_map(em);
820 if (end - offset > thresh)
821 return 0;
823 /* if we already have a nice delalloc here, just stop */
824 thresh /= 2;
825 end = count_range_bits(io_tree, &offset, offset + thresh,
826 thresh, EXTENT_DELALLOC, 1);
827 if (end >= thresh)
828 return 0;
829 return 1;
833 * helper function to walk through a file and find extents
834 * newer than a specific transid, and smaller than thresh.
836 * This is used by the defragging code to find new and small
837 * extents
839 static int find_new_extents(struct btrfs_root *root,
840 struct inode *inode, u64 newer_than,
841 u64 *off, int thresh)
843 struct btrfs_path *path;
844 struct btrfs_key min_key;
845 struct btrfs_key max_key;
846 struct extent_buffer *leaf;
847 struct btrfs_file_extent_item *extent;
848 int type;
849 int ret;
850 u64 ino = btrfs_ino(inode);
852 path = btrfs_alloc_path();
853 if (!path)
854 return -ENOMEM;
856 min_key.objectid = ino;
857 min_key.type = BTRFS_EXTENT_DATA_KEY;
858 min_key.offset = *off;
860 max_key.objectid = ino;
861 max_key.type = (u8)-1;
862 max_key.offset = (u64)-1;
864 path->keep_locks = 1;
866 while(1) {
867 ret = btrfs_search_forward(root, &min_key, &max_key,
868 path, newer_than);
869 if (ret != 0)
870 goto none;
871 if (min_key.objectid != ino)
872 goto none;
873 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
874 goto none;
876 leaf = path->nodes[0];
877 extent = btrfs_item_ptr(leaf, path->slots[0],
878 struct btrfs_file_extent_item);
880 type = btrfs_file_extent_type(leaf, extent);
881 if (type == BTRFS_FILE_EXTENT_REG &&
882 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
883 check_defrag_in_cache(inode, min_key.offset, thresh)) {
884 *off = min_key.offset;
885 btrfs_free_path(path);
886 return 0;
889 if (min_key.offset == (u64)-1)
890 goto none;
892 min_key.offset++;
893 btrfs_release_path(path);
895 none:
896 btrfs_free_path(path);
897 return -ENOENT;
900 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
902 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
903 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
904 struct extent_map *em;
905 u64 len = PAGE_CACHE_SIZE;
908 * hopefully we have this extent in the tree already, try without
909 * the full extent lock
911 read_lock(&em_tree->lock);
912 em = lookup_extent_mapping(em_tree, start, len);
913 read_unlock(&em_tree->lock);
915 if (!em) {
916 /* get the big lock and read metadata off disk */
917 lock_extent(io_tree, start, start + len - 1);
918 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
919 unlock_extent(io_tree, start, start + len - 1);
921 if (IS_ERR(em))
922 return NULL;
925 return em;
928 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
930 struct extent_map *next;
931 bool ret = true;
933 /* this is the last extent */
934 if (em->start + em->len >= i_size_read(inode))
935 return false;
937 next = defrag_lookup_extent(inode, em->start + em->len);
938 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
939 ret = false;
941 free_extent_map(next);
942 return ret;
945 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
946 u64 *last_len, u64 *skip, u64 *defrag_end,
947 int compress)
949 struct extent_map *em;
950 int ret = 1;
951 bool next_mergeable = true;
954 * make sure that once we start defragging an extent, we keep on
955 * defragging it
957 if (start < *defrag_end)
958 return 1;
960 *skip = 0;
962 em = defrag_lookup_extent(inode, start);
963 if (!em)
964 return 0;
966 /* this will cover holes, and inline extents */
967 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
968 ret = 0;
969 goto out;
972 next_mergeable = defrag_check_next_extent(inode, em);
975 * we hit a real extent, if it is big or the next extent is not a
976 * real extent, don't bother defragging it
978 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
979 (em->len >= thresh || !next_mergeable))
980 ret = 0;
981 out:
983 * last_len ends up being a counter of how many bytes we've defragged.
984 * every time we choose not to defrag an extent, we reset *last_len
985 * so that the next tiny extent will force a defrag.
987 * The end result of this is that tiny extents before a single big
988 * extent will force at least part of that big extent to be defragged.
990 if (ret) {
991 *defrag_end = extent_map_end(em);
992 } else {
993 *last_len = 0;
994 *skip = extent_map_end(em);
995 *defrag_end = 0;
998 free_extent_map(em);
999 return ret;
1003 * it doesn't do much good to defrag one or two pages
1004 * at a time. This pulls in a nice chunk of pages
1005 * to COW and defrag.
1007 * It also makes sure the delalloc code has enough
1008 * dirty data to avoid making new small extents as part
1009 * of the defrag
1011 * It's a good idea to start RA on this range
1012 * before calling this.
1014 static int cluster_pages_for_defrag(struct inode *inode,
1015 struct page **pages,
1016 unsigned long start_index,
1017 unsigned long num_pages)
1019 unsigned long file_end;
1020 u64 isize = i_size_read(inode);
1021 u64 page_start;
1022 u64 page_end;
1023 u64 page_cnt;
1024 int ret;
1025 int i;
1026 int i_done;
1027 struct btrfs_ordered_extent *ordered;
1028 struct extent_state *cached_state = NULL;
1029 struct extent_io_tree *tree;
1030 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1032 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
1033 if (!isize || start_index > file_end)
1034 return 0;
1036 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1038 ret = btrfs_delalloc_reserve_space(inode,
1039 page_cnt << PAGE_CACHE_SHIFT);
1040 if (ret)
1041 return ret;
1042 i_done = 0;
1043 tree = &BTRFS_I(inode)->io_tree;
1045 /* step one, lock all the pages */
1046 for (i = 0; i < page_cnt; i++) {
1047 struct page *page;
1048 again:
1049 page = find_or_create_page(inode->i_mapping,
1050 start_index + i, mask);
1051 if (!page)
1052 break;
1054 page_start = page_offset(page);
1055 page_end = page_start + PAGE_CACHE_SIZE - 1;
1056 while (1) {
1057 lock_extent(tree, page_start, page_end);
1058 ordered = btrfs_lookup_ordered_extent(inode,
1059 page_start);
1060 unlock_extent(tree, page_start, page_end);
1061 if (!ordered)
1062 break;
1064 unlock_page(page);
1065 btrfs_start_ordered_extent(inode, ordered, 1);
1066 btrfs_put_ordered_extent(ordered);
1067 lock_page(page);
1069 * we unlocked the page above, so we need check if
1070 * it was released or not.
1072 if (page->mapping != inode->i_mapping) {
1073 unlock_page(page);
1074 page_cache_release(page);
1075 goto again;
1079 if (!PageUptodate(page)) {
1080 btrfs_readpage(NULL, page);
1081 lock_page(page);
1082 if (!PageUptodate(page)) {
1083 unlock_page(page);
1084 page_cache_release(page);
1085 ret = -EIO;
1086 break;
1090 if (page->mapping != inode->i_mapping) {
1091 unlock_page(page);
1092 page_cache_release(page);
1093 goto again;
1096 pages[i] = page;
1097 i_done++;
1099 if (!i_done || ret)
1100 goto out;
1102 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1103 goto out;
1106 * so now we have a nice long stream of locked
1107 * and up to date pages, lets wait on them
1109 for (i = 0; i < i_done; i++)
1110 wait_on_page_writeback(pages[i]);
1112 page_start = page_offset(pages[0]);
1113 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1115 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1116 page_start, page_end - 1, 0, &cached_state);
1117 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1118 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1119 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1120 &cached_state, GFP_NOFS);
1122 if (i_done != page_cnt) {
1123 spin_lock(&BTRFS_I(inode)->lock);
1124 BTRFS_I(inode)->outstanding_extents++;
1125 spin_unlock(&BTRFS_I(inode)->lock);
1126 btrfs_delalloc_release_space(inode,
1127 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1131 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1132 &cached_state, GFP_NOFS);
1134 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1135 page_start, page_end - 1, &cached_state,
1136 GFP_NOFS);
1138 for (i = 0; i < i_done; i++) {
1139 clear_page_dirty_for_io(pages[i]);
1140 ClearPageChecked(pages[i]);
1141 set_page_extent_mapped(pages[i]);
1142 set_page_dirty(pages[i]);
1143 unlock_page(pages[i]);
1144 page_cache_release(pages[i]);
1146 return i_done;
1147 out:
1148 for (i = 0; i < i_done; i++) {
1149 unlock_page(pages[i]);
1150 page_cache_release(pages[i]);
1152 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1153 return ret;
1157 int btrfs_defrag_file(struct inode *inode, struct file *file,
1158 struct btrfs_ioctl_defrag_range_args *range,
1159 u64 newer_than, unsigned long max_to_defrag)
1161 struct btrfs_root *root = BTRFS_I(inode)->root;
1162 struct file_ra_state *ra = NULL;
1163 unsigned long last_index;
1164 u64 isize = i_size_read(inode);
1165 u64 last_len = 0;
1166 u64 skip = 0;
1167 u64 defrag_end = 0;
1168 u64 newer_off = range->start;
1169 unsigned long i;
1170 unsigned long ra_index = 0;
1171 int ret;
1172 int defrag_count = 0;
1173 int compress_type = BTRFS_COMPRESS_ZLIB;
1174 int extent_thresh = range->extent_thresh;
1175 unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1176 unsigned long cluster = max_cluster;
1177 u64 new_align = ~((u64)128 * 1024 - 1);
1178 struct page **pages = NULL;
1180 if (isize == 0)
1181 return 0;
1183 if (range->start >= isize)
1184 return -EINVAL;
1186 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1187 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1188 return -EINVAL;
1189 if (range->compress_type)
1190 compress_type = range->compress_type;
1193 if (extent_thresh == 0)
1194 extent_thresh = 256 * 1024;
1197 * if we were not given a file, allocate a readahead
1198 * context
1200 if (!file) {
1201 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1202 if (!ra)
1203 return -ENOMEM;
1204 file_ra_state_init(ra, inode->i_mapping);
1205 } else {
1206 ra = &file->f_ra;
1209 pages = kmalloc(sizeof(struct page *) * max_cluster,
1210 GFP_NOFS);
1211 if (!pages) {
1212 ret = -ENOMEM;
1213 goto out_ra;
1216 /* find the last page to defrag */
1217 if (range->start + range->len > range->start) {
1218 last_index = min_t(u64, isize - 1,
1219 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1220 } else {
1221 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1224 if (newer_than) {
1225 ret = find_new_extents(root, inode, newer_than,
1226 &newer_off, 64 * 1024);
1227 if (!ret) {
1228 range->start = newer_off;
1230 * we always align our defrag to help keep
1231 * the extents in the file evenly spaced
1233 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1234 } else
1235 goto out_ra;
1236 } else {
1237 i = range->start >> PAGE_CACHE_SHIFT;
1239 if (!max_to_defrag)
1240 max_to_defrag = last_index + 1;
1243 * make writeback starts from i, so the defrag range can be
1244 * written sequentially.
1246 if (i < inode->i_mapping->writeback_index)
1247 inode->i_mapping->writeback_index = i;
1249 while (i <= last_index && defrag_count < max_to_defrag &&
1250 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1251 PAGE_CACHE_SHIFT)) {
1253 * make sure we stop running if someone unmounts
1254 * the FS
1256 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1257 break;
1259 if (btrfs_defrag_cancelled(root->fs_info)) {
1260 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1261 ret = -EAGAIN;
1262 break;
1265 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1266 extent_thresh, &last_len, &skip,
1267 &defrag_end, range->flags &
1268 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1269 unsigned long next;
1271 * the should_defrag function tells us how much to skip
1272 * bump our counter by the suggested amount
1274 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1275 i = max(i + 1, next);
1276 continue;
1279 if (!newer_than) {
1280 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1281 PAGE_CACHE_SHIFT) - i;
1282 cluster = min(cluster, max_cluster);
1283 } else {
1284 cluster = max_cluster;
1287 if (i + cluster > ra_index) {
1288 ra_index = max(i, ra_index);
1289 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1290 cluster);
1291 ra_index += max_cluster;
1294 mutex_lock(&inode->i_mutex);
1295 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1296 BTRFS_I(inode)->force_compress = compress_type;
1297 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1298 if (ret < 0) {
1299 mutex_unlock(&inode->i_mutex);
1300 goto out_ra;
1303 defrag_count += ret;
1304 balance_dirty_pages_ratelimited(inode->i_mapping);
1305 mutex_unlock(&inode->i_mutex);
1307 if (newer_than) {
1308 if (newer_off == (u64)-1)
1309 break;
1311 if (ret > 0)
1312 i += ret;
1314 newer_off = max(newer_off + 1,
1315 (u64)i << PAGE_CACHE_SHIFT);
1317 ret = find_new_extents(root, inode,
1318 newer_than, &newer_off,
1319 64 * 1024);
1320 if (!ret) {
1321 range->start = newer_off;
1322 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1323 } else {
1324 break;
1326 } else {
1327 if (ret > 0) {
1328 i += ret;
1329 last_len += ret << PAGE_CACHE_SHIFT;
1330 } else {
1331 i++;
1332 last_len = 0;
1337 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1338 filemap_flush(inode->i_mapping);
1340 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1341 /* the filemap_flush will queue IO into the worker threads, but
1342 * we have to make sure the IO is actually started and that
1343 * ordered extents get created before we return
1345 atomic_inc(&root->fs_info->async_submit_draining);
1346 while (atomic_read(&root->fs_info->nr_async_submits) ||
1347 atomic_read(&root->fs_info->async_delalloc_pages)) {
1348 wait_event(root->fs_info->async_submit_wait,
1349 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1350 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1352 atomic_dec(&root->fs_info->async_submit_draining);
1355 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1356 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1359 ret = defrag_count;
1361 out_ra:
1362 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1363 mutex_lock(&inode->i_mutex);
1364 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1365 mutex_unlock(&inode->i_mutex);
1367 if (!file)
1368 kfree(ra);
1369 kfree(pages);
1370 return ret;
1373 static noinline int btrfs_ioctl_resize(struct file *file,
1374 void __user *arg)
1376 u64 new_size;
1377 u64 old_size;
1378 u64 devid = 1;
1379 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1380 struct btrfs_ioctl_vol_args *vol_args;
1381 struct btrfs_trans_handle *trans;
1382 struct btrfs_device *device = NULL;
1383 char *sizestr;
1384 char *devstr = NULL;
1385 int ret = 0;
1386 int mod = 0;
1388 if (!capable(CAP_SYS_ADMIN))
1389 return -EPERM;
1391 ret = mnt_want_write_file(file);
1392 if (ret)
1393 return ret;
1395 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1396 1)) {
1397 mnt_drop_write_file(file);
1398 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1401 mutex_lock(&root->fs_info->volume_mutex);
1402 vol_args = memdup_user(arg, sizeof(*vol_args));
1403 if (IS_ERR(vol_args)) {
1404 ret = PTR_ERR(vol_args);
1405 goto out;
1408 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1410 sizestr = vol_args->name;
1411 devstr = strchr(sizestr, ':');
1412 if (devstr) {
1413 char *end;
1414 sizestr = devstr + 1;
1415 *devstr = '\0';
1416 devstr = vol_args->name;
1417 devid = simple_strtoull(devstr, &end, 10);
1418 if (!devid) {
1419 ret = -EINVAL;
1420 goto out_free;
1422 printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
1425 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1426 if (!device) {
1427 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1428 devid);
1429 ret = -ENODEV;
1430 goto out_free;
1433 if (!device->writeable) {
1434 printk(KERN_INFO "btrfs: resizer unable to apply on "
1435 "readonly device %llu\n",
1436 devid);
1437 ret = -EPERM;
1438 goto out_free;
1441 if (!strcmp(sizestr, "max"))
1442 new_size = device->bdev->bd_inode->i_size;
1443 else {
1444 if (sizestr[0] == '-') {
1445 mod = -1;
1446 sizestr++;
1447 } else if (sizestr[0] == '+') {
1448 mod = 1;
1449 sizestr++;
1451 new_size = memparse(sizestr, NULL);
1452 if (new_size == 0) {
1453 ret = -EINVAL;
1454 goto out_free;
1458 if (device->is_tgtdev_for_dev_replace) {
1459 ret = -EPERM;
1460 goto out_free;
1463 old_size = device->total_bytes;
1465 if (mod < 0) {
1466 if (new_size > old_size) {
1467 ret = -EINVAL;
1468 goto out_free;
1470 new_size = old_size - new_size;
1471 } else if (mod > 0) {
1472 new_size = old_size + new_size;
1475 if (new_size < 256 * 1024 * 1024) {
1476 ret = -EINVAL;
1477 goto out_free;
1479 if (new_size > device->bdev->bd_inode->i_size) {
1480 ret = -EFBIG;
1481 goto out_free;
1484 do_div(new_size, root->sectorsize);
1485 new_size *= root->sectorsize;
1487 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1488 rcu_str_deref(device->name), new_size);
1490 if (new_size > old_size) {
1491 trans = btrfs_start_transaction(root, 0);
1492 if (IS_ERR(trans)) {
1493 ret = PTR_ERR(trans);
1494 goto out_free;
1496 ret = btrfs_grow_device(trans, device, new_size);
1497 btrfs_commit_transaction(trans, root);
1498 } else if (new_size < old_size) {
1499 ret = btrfs_shrink_device(device, new_size);
1500 } /* equal, nothing need to do */
1502 out_free:
1503 kfree(vol_args);
1504 out:
1505 mutex_unlock(&root->fs_info->volume_mutex);
1506 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1507 mnt_drop_write_file(file);
1508 return ret;
1511 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1512 char *name, unsigned long fd, int subvol,
1513 u64 *transid, bool readonly,
1514 struct btrfs_qgroup_inherit *inherit)
1516 int namelen;
1517 int ret = 0;
1519 ret = mnt_want_write_file(file);
1520 if (ret)
1521 goto out;
1523 namelen = strlen(name);
1524 if (strchr(name, '/')) {
1525 ret = -EINVAL;
1526 goto out_drop_write;
1529 if (name[0] == '.' &&
1530 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1531 ret = -EEXIST;
1532 goto out_drop_write;
1535 if (subvol) {
1536 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1537 NULL, transid, readonly, inherit);
1538 } else {
1539 struct fd src = fdget(fd);
1540 struct inode *src_inode;
1541 if (!src.file) {
1542 ret = -EINVAL;
1543 goto out_drop_write;
1546 src_inode = file_inode(src.file);
1547 if (src_inode->i_sb != file_inode(file)->i_sb) {
1548 printk(KERN_INFO "btrfs: Snapshot src from "
1549 "another FS\n");
1550 ret = -EINVAL;
1551 } else if (!inode_owner_or_capable(src_inode)) {
1553 * Subvolume creation is not restricted, but snapshots
1554 * are limited to own subvolumes only
1556 ret = -EPERM;
1557 } else {
1558 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1559 BTRFS_I(src_inode)->root,
1560 transid, readonly, inherit);
1562 fdput(src);
1564 out_drop_write:
1565 mnt_drop_write_file(file);
1566 out:
1567 return ret;
1570 static noinline int btrfs_ioctl_snap_create(struct file *file,
1571 void __user *arg, int subvol)
1573 struct btrfs_ioctl_vol_args *vol_args;
1574 int ret;
1576 vol_args = memdup_user(arg, sizeof(*vol_args));
1577 if (IS_ERR(vol_args))
1578 return PTR_ERR(vol_args);
1579 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1581 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1582 vol_args->fd, subvol,
1583 NULL, false, NULL);
1585 kfree(vol_args);
1586 return ret;
1589 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1590 void __user *arg, int subvol)
1592 struct btrfs_ioctl_vol_args_v2 *vol_args;
1593 int ret;
1594 u64 transid = 0;
1595 u64 *ptr = NULL;
1596 bool readonly = false;
1597 struct btrfs_qgroup_inherit *inherit = NULL;
1599 vol_args = memdup_user(arg, sizeof(*vol_args));
1600 if (IS_ERR(vol_args))
1601 return PTR_ERR(vol_args);
1602 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1604 if (vol_args->flags &
1605 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1606 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1607 ret = -EOPNOTSUPP;
1608 goto out;
1611 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1612 ptr = &transid;
1613 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1614 readonly = true;
1615 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1616 if (vol_args->size > PAGE_CACHE_SIZE) {
1617 ret = -EINVAL;
1618 goto out;
1620 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1621 if (IS_ERR(inherit)) {
1622 ret = PTR_ERR(inherit);
1623 goto out;
1627 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1628 vol_args->fd, subvol, ptr,
1629 readonly, inherit);
1631 if (ret == 0 && ptr &&
1632 copy_to_user(arg +
1633 offsetof(struct btrfs_ioctl_vol_args_v2,
1634 transid), ptr, sizeof(*ptr)))
1635 ret = -EFAULT;
1636 out:
1637 kfree(vol_args);
1638 kfree(inherit);
1639 return ret;
1642 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1643 void __user *arg)
1645 struct inode *inode = file_inode(file);
1646 struct btrfs_root *root = BTRFS_I(inode)->root;
1647 int ret = 0;
1648 u64 flags = 0;
1650 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1651 return -EINVAL;
1653 down_read(&root->fs_info->subvol_sem);
1654 if (btrfs_root_readonly(root))
1655 flags |= BTRFS_SUBVOL_RDONLY;
1656 up_read(&root->fs_info->subvol_sem);
1658 if (copy_to_user(arg, &flags, sizeof(flags)))
1659 ret = -EFAULT;
1661 return ret;
1664 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1665 void __user *arg)
1667 struct inode *inode = file_inode(file);
1668 struct btrfs_root *root = BTRFS_I(inode)->root;
1669 struct btrfs_trans_handle *trans;
1670 u64 root_flags;
1671 u64 flags;
1672 int ret = 0;
1674 ret = mnt_want_write_file(file);
1675 if (ret)
1676 goto out;
1678 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1679 ret = -EINVAL;
1680 goto out_drop_write;
1683 if (copy_from_user(&flags, arg, sizeof(flags))) {
1684 ret = -EFAULT;
1685 goto out_drop_write;
1688 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1689 ret = -EINVAL;
1690 goto out_drop_write;
1693 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1694 ret = -EOPNOTSUPP;
1695 goto out_drop_write;
1698 if (!inode_owner_or_capable(inode)) {
1699 ret = -EACCES;
1700 goto out_drop_write;
1703 down_write(&root->fs_info->subvol_sem);
1705 /* nothing to do */
1706 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1707 goto out_drop_sem;
1709 root_flags = btrfs_root_flags(&root->root_item);
1710 if (flags & BTRFS_SUBVOL_RDONLY)
1711 btrfs_set_root_flags(&root->root_item,
1712 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1713 else
1714 btrfs_set_root_flags(&root->root_item,
1715 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1717 trans = btrfs_start_transaction(root, 1);
1718 if (IS_ERR(trans)) {
1719 ret = PTR_ERR(trans);
1720 goto out_reset;
1723 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1724 &root->root_key, &root->root_item);
1726 btrfs_commit_transaction(trans, root);
1727 out_reset:
1728 if (ret)
1729 btrfs_set_root_flags(&root->root_item, root_flags);
1730 out_drop_sem:
1731 up_write(&root->fs_info->subvol_sem);
1732 out_drop_write:
1733 mnt_drop_write_file(file);
1734 out:
1735 return ret;
1739 * helper to check if the subvolume references other subvolumes
1741 static noinline int may_destroy_subvol(struct btrfs_root *root)
1743 struct btrfs_path *path;
1744 struct btrfs_dir_item *di;
1745 struct btrfs_key key;
1746 u64 dir_id;
1747 int ret;
1749 path = btrfs_alloc_path();
1750 if (!path)
1751 return -ENOMEM;
1753 /* Make sure this root isn't set as the default subvol */
1754 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1755 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1756 dir_id, "default", 7, 0);
1757 if (di && !IS_ERR(di)) {
1758 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1759 if (key.objectid == root->root_key.objectid) {
1760 ret = -ENOTEMPTY;
1761 goto out;
1763 btrfs_release_path(path);
1766 key.objectid = root->root_key.objectid;
1767 key.type = BTRFS_ROOT_REF_KEY;
1768 key.offset = (u64)-1;
1770 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1771 &key, path, 0, 0);
1772 if (ret < 0)
1773 goto out;
1774 BUG_ON(ret == 0);
1776 ret = 0;
1777 if (path->slots[0] > 0) {
1778 path->slots[0]--;
1779 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1780 if (key.objectid == root->root_key.objectid &&
1781 key.type == BTRFS_ROOT_REF_KEY)
1782 ret = -ENOTEMPTY;
1784 out:
1785 btrfs_free_path(path);
1786 return ret;
1789 static noinline int key_in_sk(struct btrfs_key *key,
1790 struct btrfs_ioctl_search_key *sk)
1792 struct btrfs_key test;
1793 int ret;
1795 test.objectid = sk->min_objectid;
1796 test.type = sk->min_type;
1797 test.offset = sk->min_offset;
1799 ret = btrfs_comp_cpu_keys(key, &test);
1800 if (ret < 0)
1801 return 0;
1803 test.objectid = sk->max_objectid;
1804 test.type = sk->max_type;
1805 test.offset = sk->max_offset;
1807 ret = btrfs_comp_cpu_keys(key, &test);
1808 if (ret > 0)
1809 return 0;
1810 return 1;
1813 static noinline int copy_to_sk(struct btrfs_root *root,
1814 struct btrfs_path *path,
1815 struct btrfs_key *key,
1816 struct btrfs_ioctl_search_key *sk,
1817 char *buf,
1818 unsigned long *sk_offset,
1819 int *num_found)
1821 u64 found_transid;
1822 struct extent_buffer *leaf;
1823 struct btrfs_ioctl_search_header sh;
1824 unsigned long item_off;
1825 unsigned long item_len;
1826 int nritems;
1827 int i;
1828 int slot;
1829 int ret = 0;
1831 leaf = path->nodes[0];
1832 slot = path->slots[0];
1833 nritems = btrfs_header_nritems(leaf);
1835 if (btrfs_header_generation(leaf) > sk->max_transid) {
1836 i = nritems;
1837 goto advance_key;
1839 found_transid = btrfs_header_generation(leaf);
1841 for (i = slot; i < nritems; i++) {
1842 item_off = btrfs_item_ptr_offset(leaf, i);
1843 item_len = btrfs_item_size_nr(leaf, i);
1845 btrfs_item_key_to_cpu(leaf, key, i);
1846 if (!key_in_sk(key, sk))
1847 continue;
1849 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1850 item_len = 0;
1852 if (sizeof(sh) + item_len + *sk_offset >
1853 BTRFS_SEARCH_ARGS_BUFSIZE) {
1854 ret = 1;
1855 goto overflow;
1858 sh.objectid = key->objectid;
1859 sh.offset = key->offset;
1860 sh.type = key->type;
1861 sh.len = item_len;
1862 sh.transid = found_transid;
1864 /* copy search result header */
1865 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1866 *sk_offset += sizeof(sh);
1868 if (item_len) {
1869 char *p = buf + *sk_offset;
1870 /* copy the item */
1871 read_extent_buffer(leaf, p,
1872 item_off, item_len);
1873 *sk_offset += item_len;
1875 (*num_found)++;
1877 if (*num_found >= sk->nr_items)
1878 break;
1880 advance_key:
1881 ret = 0;
1882 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1883 key->offset++;
1884 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1885 key->offset = 0;
1886 key->type++;
1887 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1888 key->offset = 0;
1889 key->type = 0;
1890 key->objectid++;
1891 } else
1892 ret = 1;
1893 overflow:
1894 return ret;
1897 static noinline int search_ioctl(struct inode *inode,
1898 struct btrfs_ioctl_search_args *args)
1900 struct btrfs_root *root;
1901 struct btrfs_key key;
1902 struct btrfs_key max_key;
1903 struct btrfs_path *path;
1904 struct btrfs_ioctl_search_key *sk = &args->key;
1905 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1906 int ret;
1907 int num_found = 0;
1908 unsigned long sk_offset = 0;
1910 path = btrfs_alloc_path();
1911 if (!path)
1912 return -ENOMEM;
1914 if (sk->tree_id == 0) {
1915 /* search the root of the inode that was passed */
1916 root = BTRFS_I(inode)->root;
1917 } else {
1918 key.objectid = sk->tree_id;
1919 key.type = BTRFS_ROOT_ITEM_KEY;
1920 key.offset = (u64)-1;
1921 root = btrfs_read_fs_root_no_name(info, &key);
1922 if (IS_ERR(root)) {
1923 printk(KERN_ERR "could not find root %llu\n",
1924 sk->tree_id);
1925 btrfs_free_path(path);
1926 return -ENOENT;
1930 key.objectid = sk->min_objectid;
1931 key.type = sk->min_type;
1932 key.offset = sk->min_offset;
1934 max_key.objectid = sk->max_objectid;
1935 max_key.type = sk->max_type;
1936 max_key.offset = sk->max_offset;
1938 path->keep_locks = 1;
1940 while(1) {
1941 ret = btrfs_search_forward(root, &key, &max_key, path,
1942 sk->min_transid);
1943 if (ret != 0) {
1944 if (ret > 0)
1945 ret = 0;
1946 goto err;
1948 ret = copy_to_sk(root, path, &key, sk, args->buf,
1949 &sk_offset, &num_found);
1950 btrfs_release_path(path);
1951 if (ret || num_found >= sk->nr_items)
1952 break;
1955 ret = 0;
1956 err:
1957 sk->nr_items = num_found;
1958 btrfs_free_path(path);
1959 return ret;
1962 static noinline int btrfs_ioctl_tree_search(struct file *file,
1963 void __user *argp)
1965 struct btrfs_ioctl_search_args *args;
1966 struct inode *inode;
1967 int ret;
1969 if (!capable(CAP_SYS_ADMIN))
1970 return -EPERM;
1972 args = memdup_user(argp, sizeof(*args));
1973 if (IS_ERR(args))
1974 return PTR_ERR(args);
1976 inode = file_inode(file);
1977 ret = search_ioctl(inode, args);
1978 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1979 ret = -EFAULT;
1980 kfree(args);
1981 return ret;
1985 * Search INODE_REFs to identify path name of 'dirid' directory
1986 * in a 'tree_id' tree. and sets path name to 'name'.
1988 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1989 u64 tree_id, u64 dirid, char *name)
1991 struct btrfs_root *root;
1992 struct btrfs_key key;
1993 char *ptr;
1994 int ret = -1;
1995 int slot;
1996 int len;
1997 int total_len = 0;
1998 struct btrfs_inode_ref *iref;
1999 struct extent_buffer *l;
2000 struct btrfs_path *path;
2002 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2003 name[0]='\0';
2004 return 0;
2007 path = btrfs_alloc_path();
2008 if (!path)
2009 return -ENOMEM;
2011 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
2013 key.objectid = tree_id;
2014 key.type = BTRFS_ROOT_ITEM_KEY;
2015 key.offset = (u64)-1;
2016 root = btrfs_read_fs_root_no_name(info, &key);
2017 if (IS_ERR(root)) {
2018 printk(KERN_ERR "could not find root %llu\n", tree_id);
2019 ret = -ENOENT;
2020 goto out;
2023 key.objectid = dirid;
2024 key.type = BTRFS_INODE_REF_KEY;
2025 key.offset = (u64)-1;
2027 while(1) {
2028 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2029 if (ret < 0)
2030 goto out;
2031 else if (ret > 0) {
2032 ret = btrfs_previous_item(root, path, dirid,
2033 BTRFS_INODE_REF_KEY);
2034 if (ret < 0)
2035 goto out;
2036 else if (ret > 0) {
2037 ret = -ENOENT;
2038 goto out;
2042 l = path->nodes[0];
2043 slot = path->slots[0];
2044 btrfs_item_key_to_cpu(l, &key, slot);
2046 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2047 len = btrfs_inode_ref_name_len(l, iref);
2048 ptr -= len + 1;
2049 total_len += len + 1;
2050 if (ptr < name) {
2051 ret = -ENAMETOOLONG;
2052 goto out;
2055 *(ptr + len) = '/';
2056 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
2058 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2059 break;
2061 btrfs_release_path(path);
2062 key.objectid = key.offset;
2063 key.offset = (u64)-1;
2064 dirid = key.objectid;
2066 memmove(name, ptr, total_len);
2067 name[total_len]='\0';
2068 ret = 0;
2069 out:
2070 btrfs_free_path(path);
2071 return ret;
2074 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2075 void __user *argp)
2077 struct btrfs_ioctl_ino_lookup_args *args;
2078 struct inode *inode;
2079 int ret;
2081 if (!capable(CAP_SYS_ADMIN))
2082 return -EPERM;
2084 args = memdup_user(argp, sizeof(*args));
2085 if (IS_ERR(args))
2086 return PTR_ERR(args);
2088 inode = file_inode(file);
2090 if (args->treeid == 0)
2091 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2093 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2094 args->treeid, args->objectid,
2095 args->name);
2097 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2098 ret = -EFAULT;
2100 kfree(args);
2101 return ret;
2104 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2105 void __user *arg)
2107 struct dentry *parent = fdentry(file);
2108 struct dentry *dentry;
2109 struct inode *dir = parent->d_inode;
2110 struct inode *inode;
2111 struct btrfs_root *root = BTRFS_I(dir)->root;
2112 struct btrfs_root *dest = NULL;
2113 struct btrfs_ioctl_vol_args *vol_args;
2114 struct btrfs_trans_handle *trans;
2115 struct btrfs_block_rsv block_rsv;
2116 u64 qgroup_reserved;
2117 int namelen;
2118 int ret;
2119 int err = 0;
2121 vol_args = memdup_user(arg, sizeof(*vol_args));
2122 if (IS_ERR(vol_args))
2123 return PTR_ERR(vol_args);
2125 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2126 namelen = strlen(vol_args->name);
2127 if (strchr(vol_args->name, '/') ||
2128 strncmp(vol_args->name, "..", namelen) == 0) {
2129 err = -EINVAL;
2130 goto out;
2133 err = mnt_want_write_file(file);
2134 if (err)
2135 goto out;
2137 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2138 if (err == -EINTR)
2139 goto out_drop_write;
2140 dentry = lookup_one_len(vol_args->name, parent, namelen);
2141 if (IS_ERR(dentry)) {
2142 err = PTR_ERR(dentry);
2143 goto out_unlock_dir;
2146 if (!dentry->d_inode) {
2147 err = -ENOENT;
2148 goto out_dput;
2151 inode = dentry->d_inode;
2152 dest = BTRFS_I(inode)->root;
2153 if (!capable(CAP_SYS_ADMIN)){
2155 * Regular user. Only allow this with a special mount
2156 * option, when the user has write+exec access to the
2157 * subvol root, and when rmdir(2) would have been
2158 * allowed.
2160 * Note that this is _not_ check that the subvol is
2161 * empty or doesn't contain data that we wouldn't
2162 * otherwise be able to delete.
2164 * Users who want to delete empty subvols should try
2165 * rmdir(2).
2167 err = -EPERM;
2168 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2169 goto out_dput;
2172 * Do not allow deletion if the parent dir is the same
2173 * as the dir to be deleted. That means the ioctl
2174 * must be called on the dentry referencing the root
2175 * of the subvol, not a random directory contained
2176 * within it.
2178 err = -EINVAL;
2179 if (root == dest)
2180 goto out_dput;
2182 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2183 if (err)
2184 goto out_dput;
2187 /* check if subvolume may be deleted by a user */
2188 err = btrfs_may_delete(dir, dentry, 1);
2189 if (err)
2190 goto out_dput;
2192 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2193 err = -EINVAL;
2194 goto out_dput;
2197 mutex_lock(&inode->i_mutex);
2198 err = d_invalidate(dentry);
2199 if (err)
2200 goto out_unlock;
2202 down_write(&root->fs_info->subvol_sem);
2204 err = may_destroy_subvol(dest);
2205 if (err)
2206 goto out_up_write;
2208 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2210 * One for dir inode, two for dir entries, two for root
2211 * ref/backref.
2213 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2214 5, &qgroup_reserved, true);
2215 if (err)
2216 goto out_up_write;
2218 trans = btrfs_start_transaction(root, 0);
2219 if (IS_ERR(trans)) {
2220 err = PTR_ERR(trans);
2221 goto out_release;
2223 trans->block_rsv = &block_rsv;
2224 trans->bytes_reserved = block_rsv.size;
2226 ret = btrfs_unlink_subvol(trans, root, dir,
2227 dest->root_key.objectid,
2228 dentry->d_name.name,
2229 dentry->d_name.len);
2230 if (ret) {
2231 err = ret;
2232 btrfs_abort_transaction(trans, root, ret);
2233 goto out_end_trans;
2236 btrfs_record_root_in_trans(trans, dest);
2238 memset(&dest->root_item.drop_progress, 0,
2239 sizeof(dest->root_item.drop_progress));
2240 dest->root_item.drop_level = 0;
2241 btrfs_set_root_refs(&dest->root_item, 0);
2243 if (!xchg(&dest->orphan_item_inserted, 1)) {
2244 ret = btrfs_insert_orphan_item(trans,
2245 root->fs_info->tree_root,
2246 dest->root_key.objectid);
2247 if (ret) {
2248 btrfs_abort_transaction(trans, root, ret);
2249 err = ret;
2250 goto out_end_trans;
2254 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2255 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2256 dest->root_key.objectid);
2257 if (ret && ret != -ENOENT) {
2258 btrfs_abort_transaction(trans, root, ret);
2259 err = ret;
2260 goto out_end_trans;
2262 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2263 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2264 dest->root_item.received_uuid,
2265 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2266 dest->root_key.objectid);
2267 if (ret && ret != -ENOENT) {
2268 btrfs_abort_transaction(trans, root, ret);
2269 err = ret;
2270 goto out_end_trans;
2274 out_end_trans:
2275 trans->block_rsv = NULL;
2276 trans->bytes_reserved = 0;
2277 ret = btrfs_end_transaction(trans, root);
2278 if (ret && !err)
2279 err = ret;
2280 inode->i_flags |= S_DEAD;
2281 out_release:
2282 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
2283 out_up_write:
2284 up_write(&root->fs_info->subvol_sem);
2285 out_unlock:
2286 mutex_unlock(&inode->i_mutex);
2287 if (!err) {
2288 shrink_dcache_sb(root->fs_info->sb);
2289 btrfs_invalidate_inodes(dest);
2290 d_delete(dentry);
2292 /* the last ref */
2293 if (dest->cache_inode) {
2294 iput(dest->cache_inode);
2295 dest->cache_inode = NULL;
2298 out_dput:
2299 dput(dentry);
2300 out_unlock_dir:
2301 mutex_unlock(&dir->i_mutex);
2302 out_drop_write:
2303 mnt_drop_write_file(file);
2304 out:
2305 kfree(vol_args);
2306 return err;
2309 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2311 struct inode *inode = file_inode(file);
2312 struct btrfs_root *root = BTRFS_I(inode)->root;
2313 struct btrfs_ioctl_defrag_range_args *range;
2314 int ret;
2316 ret = mnt_want_write_file(file);
2317 if (ret)
2318 return ret;
2320 if (btrfs_root_readonly(root)) {
2321 ret = -EROFS;
2322 goto out;
2325 switch (inode->i_mode & S_IFMT) {
2326 case S_IFDIR:
2327 if (!capable(CAP_SYS_ADMIN)) {
2328 ret = -EPERM;
2329 goto out;
2331 ret = btrfs_defrag_root(root);
2332 if (ret)
2333 goto out;
2334 ret = btrfs_defrag_root(root->fs_info->extent_root);
2335 break;
2336 case S_IFREG:
2337 if (!(file->f_mode & FMODE_WRITE)) {
2338 ret = -EINVAL;
2339 goto out;
2342 range = kzalloc(sizeof(*range), GFP_KERNEL);
2343 if (!range) {
2344 ret = -ENOMEM;
2345 goto out;
2348 if (argp) {
2349 if (copy_from_user(range, argp,
2350 sizeof(*range))) {
2351 ret = -EFAULT;
2352 kfree(range);
2353 goto out;
2355 /* compression requires us to start the IO */
2356 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2357 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2358 range->extent_thresh = (u32)-1;
2360 } else {
2361 /* the rest are all set to zero by kzalloc */
2362 range->len = (u64)-1;
2364 ret = btrfs_defrag_file(file_inode(file), file,
2365 range, 0, 0);
2366 if (ret > 0)
2367 ret = 0;
2368 kfree(range);
2369 break;
2370 default:
2371 ret = -EINVAL;
2373 out:
2374 mnt_drop_write_file(file);
2375 return ret;
2378 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2380 struct btrfs_ioctl_vol_args *vol_args;
2381 int ret;
2383 if (!capable(CAP_SYS_ADMIN))
2384 return -EPERM;
2386 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2387 1)) {
2388 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2391 mutex_lock(&root->fs_info->volume_mutex);
2392 vol_args = memdup_user(arg, sizeof(*vol_args));
2393 if (IS_ERR(vol_args)) {
2394 ret = PTR_ERR(vol_args);
2395 goto out;
2398 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2399 ret = btrfs_init_new_device(root, vol_args->name);
2401 kfree(vol_args);
2402 out:
2403 mutex_unlock(&root->fs_info->volume_mutex);
2404 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2405 return ret;
2408 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2410 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2411 struct btrfs_ioctl_vol_args *vol_args;
2412 int ret;
2414 if (!capable(CAP_SYS_ADMIN))
2415 return -EPERM;
2417 ret = mnt_want_write_file(file);
2418 if (ret)
2419 return ret;
2421 vol_args = memdup_user(arg, sizeof(*vol_args));
2422 if (IS_ERR(vol_args)) {
2423 ret = PTR_ERR(vol_args);
2424 goto out;
2427 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2429 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2430 1)) {
2431 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2432 goto out;
2435 mutex_lock(&root->fs_info->volume_mutex);
2436 ret = btrfs_rm_device(root, vol_args->name);
2437 mutex_unlock(&root->fs_info->volume_mutex);
2438 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2440 out:
2441 kfree(vol_args);
2442 mnt_drop_write_file(file);
2443 return ret;
2446 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2448 struct btrfs_ioctl_fs_info_args *fi_args;
2449 struct btrfs_device *device;
2450 struct btrfs_device *next;
2451 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2452 int ret = 0;
2454 if (!capable(CAP_SYS_ADMIN))
2455 return -EPERM;
2457 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2458 if (!fi_args)
2459 return -ENOMEM;
2461 mutex_lock(&fs_devices->device_list_mutex);
2462 fi_args->num_devices = fs_devices->num_devices;
2463 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2465 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2466 if (device->devid > fi_args->max_id)
2467 fi_args->max_id = device->devid;
2469 mutex_unlock(&fs_devices->device_list_mutex);
2471 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2472 ret = -EFAULT;
2474 kfree(fi_args);
2475 return ret;
2478 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2480 struct btrfs_ioctl_dev_info_args *di_args;
2481 struct btrfs_device *dev;
2482 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2483 int ret = 0;
2484 char *s_uuid = NULL;
2486 if (!capable(CAP_SYS_ADMIN))
2487 return -EPERM;
2489 di_args = memdup_user(arg, sizeof(*di_args));
2490 if (IS_ERR(di_args))
2491 return PTR_ERR(di_args);
2493 if (!btrfs_is_empty_uuid(di_args->uuid))
2494 s_uuid = di_args->uuid;
2496 mutex_lock(&fs_devices->device_list_mutex);
2497 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2499 if (!dev) {
2500 ret = -ENODEV;
2501 goto out;
2504 di_args->devid = dev->devid;
2505 di_args->bytes_used = dev->bytes_used;
2506 di_args->total_bytes = dev->total_bytes;
2507 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2508 if (dev->name) {
2509 struct rcu_string *name;
2511 rcu_read_lock();
2512 name = rcu_dereference(dev->name);
2513 strncpy(di_args->path, name->str, sizeof(di_args->path));
2514 rcu_read_unlock();
2515 di_args->path[sizeof(di_args->path) - 1] = 0;
2516 } else {
2517 di_args->path[0] = '\0';
2520 out:
2521 mutex_unlock(&fs_devices->device_list_mutex);
2522 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2523 ret = -EFAULT;
2525 kfree(di_args);
2526 return ret;
2529 static struct page *extent_same_get_page(struct inode *inode, u64 off)
2531 struct page *page;
2532 pgoff_t index;
2533 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2535 index = off >> PAGE_CACHE_SHIFT;
2537 page = grab_cache_page(inode->i_mapping, index);
2538 if (!page)
2539 return NULL;
2541 if (!PageUptodate(page)) {
2542 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2544 return NULL;
2545 lock_page(page);
2546 if (!PageUptodate(page)) {
2547 unlock_page(page);
2548 page_cache_release(page);
2549 return NULL;
2552 unlock_page(page);
2554 return page;
2557 static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2559 /* do any pending delalloc/csum calc on src, one way or
2560 another, and lock file content */
2561 while (1) {
2562 struct btrfs_ordered_extent *ordered;
2563 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2564 ordered = btrfs_lookup_first_ordered_extent(inode,
2565 off + len - 1);
2566 if (!ordered &&
2567 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2568 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2569 break;
2570 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2571 if (ordered)
2572 btrfs_put_ordered_extent(ordered);
2573 btrfs_wait_ordered_range(inode, off, len);
2577 static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2578 struct inode *inode2, u64 loff2, u64 len)
2580 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2581 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2583 mutex_unlock(&inode1->i_mutex);
2584 mutex_unlock(&inode2->i_mutex);
2587 static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2588 struct inode *inode2, u64 loff2, u64 len)
2590 if (inode1 < inode2) {
2591 swap(inode1, inode2);
2592 swap(loff1, loff2);
2595 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2596 lock_extent_range(inode1, loff1, len);
2597 if (inode1 != inode2) {
2598 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2599 lock_extent_range(inode2, loff2, len);
2603 static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2604 u64 dst_loff, u64 len)
2606 int ret = 0;
2607 struct page *src_page, *dst_page;
2608 unsigned int cmp_len = PAGE_CACHE_SIZE;
2609 void *addr, *dst_addr;
2611 while (len) {
2612 if (len < PAGE_CACHE_SIZE)
2613 cmp_len = len;
2615 src_page = extent_same_get_page(src, loff);
2616 if (!src_page)
2617 return -EINVAL;
2618 dst_page = extent_same_get_page(dst, dst_loff);
2619 if (!dst_page) {
2620 page_cache_release(src_page);
2621 return -EINVAL;
2623 addr = kmap_atomic(src_page);
2624 dst_addr = kmap_atomic(dst_page);
2626 flush_dcache_page(src_page);
2627 flush_dcache_page(dst_page);
2629 if (memcmp(addr, dst_addr, cmp_len))
2630 ret = BTRFS_SAME_DATA_DIFFERS;
2632 kunmap_atomic(addr);
2633 kunmap_atomic(dst_addr);
2634 page_cache_release(src_page);
2635 page_cache_release(dst_page);
2637 if (ret)
2638 break;
2640 loff += cmp_len;
2641 dst_loff += cmp_len;
2642 len -= cmp_len;
2645 return ret;
2648 static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2650 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2652 if (off + len > inode->i_size || off + len < off)
2653 return -EINVAL;
2654 /* Check that we are block aligned - btrfs_clone() requires this */
2655 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2656 return -EINVAL;
2658 return 0;
2661 static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2662 struct inode *dst, u64 dst_loff)
2664 int ret;
2667 * btrfs_clone() can't handle extents in the same file
2668 * yet. Once that works, we can drop this check and replace it
2669 * with a check for the same inode, but overlapping extents.
2671 if (src == dst)
2672 return -EINVAL;
2674 btrfs_double_lock(src, loff, dst, dst_loff, len);
2676 ret = extent_same_check_offsets(src, loff, len);
2677 if (ret)
2678 goto out_unlock;
2680 ret = extent_same_check_offsets(dst, dst_loff, len);
2681 if (ret)
2682 goto out_unlock;
2684 /* don't make the dst file partly checksummed */
2685 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2686 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2687 ret = -EINVAL;
2688 goto out_unlock;
2691 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2692 if (ret == 0)
2693 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2695 out_unlock:
2696 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2698 return ret;
2701 #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2703 static long btrfs_ioctl_file_extent_same(struct file *file,
2704 void __user *argp)
2706 struct btrfs_ioctl_same_args tmp;
2707 struct btrfs_ioctl_same_args *same;
2708 struct btrfs_ioctl_same_extent_info *info;
2709 struct inode *src = file->f_dentry->d_inode;
2710 struct file *dst_file = NULL;
2711 struct inode *dst;
2712 u64 off;
2713 u64 len;
2714 int i;
2715 int ret;
2716 unsigned long size;
2717 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2718 bool is_admin = capable(CAP_SYS_ADMIN);
2720 if (!(file->f_mode & FMODE_READ))
2721 return -EINVAL;
2723 ret = mnt_want_write_file(file);
2724 if (ret)
2725 return ret;
2727 if (copy_from_user(&tmp,
2728 (struct btrfs_ioctl_same_args __user *)argp,
2729 sizeof(tmp))) {
2730 ret = -EFAULT;
2731 goto out;
2734 size = sizeof(tmp) +
2735 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2737 same = kmalloc(size, GFP_NOFS);
2738 if (!same) {
2739 ret = -EFAULT;
2740 goto out;
2743 if (copy_from_user(same,
2744 (struct btrfs_ioctl_same_args __user *)argp, size)) {
2745 ret = -EFAULT;
2746 goto out;
2749 off = same->logical_offset;
2750 len = same->length;
2753 * Limit the total length we will dedupe for each operation.
2754 * This is intended to bound the total time spent in this
2755 * ioctl to something sane.
2757 if (len > BTRFS_MAX_DEDUPE_LEN)
2758 len = BTRFS_MAX_DEDUPE_LEN;
2760 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2762 * Btrfs does not support blocksize < page_size. As a
2763 * result, btrfs_cmp_data() won't correctly handle
2764 * this situation without an update.
2766 ret = -EINVAL;
2767 goto out;
2770 ret = -EISDIR;
2771 if (S_ISDIR(src->i_mode))
2772 goto out;
2774 ret = -EACCES;
2775 if (!S_ISREG(src->i_mode))
2776 goto out;
2778 /* pre-format output fields to sane values */
2779 for (i = 0; i < same->dest_count; i++) {
2780 same->info[i].bytes_deduped = 0ULL;
2781 same->info[i].status = 0;
2784 ret = 0;
2785 for (i = 0; i < same->dest_count; i++) {
2786 info = &same->info[i];
2788 dst_file = fget(info->fd);
2789 if (!dst_file) {
2790 info->status = -EBADF;
2791 goto next;
2794 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2795 info->status = -EINVAL;
2796 goto next;
2799 info->status = -EXDEV;
2800 if (file->f_path.mnt != dst_file->f_path.mnt)
2801 goto next;
2803 dst = dst_file->f_dentry->d_inode;
2804 if (src->i_sb != dst->i_sb)
2805 goto next;
2807 if (S_ISDIR(dst->i_mode)) {
2808 info->status = -EISDIR;
2809 goto next;
2812 if (!S_ISREG(dst->i_mode)) {
2813 info->status = -EACCES;
2814 goto next;
2817 info->status = btrfs_extent_same(src, off, len, dst,
2818 info->logical_offset);
2819 if (info->status == 0)
2820 info->bytes_deduped += len;
2822 next:
2823 if (dst_file)
2824 fput(dst_file);
2827 ret = copy_to_user(argp, same, size);
2828 if (ret)
2829 ret = -EFAULT;
2831 out:
2832 mnt_drop_write_file(file);
2833 return ret;
2837 * btrfs_clone() - clone a range from inode file to another
2839 * @src: Inode to clone from
2840 * @inode: Inode to clone to
2841 * @off: Offset within source to start clone from
2842 * @olen: Original length, passed by user, of range to clone
2843 * @olen_aligned: Block-aligned value of olen, extent_same uses
2844 * identical values here
2845 * @destoff: Offset within @inode to start clone
2847 static int btrfs_clone(struct inode *src, struct inode *inode,
2848 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2850 struct btrfs_root *root = BTRFS_I(inode)->root;
2851 struct btrfs_path *path = NULL;
2852 struct extent_buffer *leaf;
2853 struct btrfs_trans_handle *trans;
2854 char *buf = NULL;
2855 struct btrfs_key key;
2856 u32 nritems;
2857 int slot;
2858 int ret;
2859 u64 len = olen_aligned;
2861 ret = -ENOMEM;
2862 buf = vmalloc(btrfs_level_size(root, 0));
2863 if (!buf)
2864 return ret;
2866 path = btrfs_alloc_path();
2867 if (!path) {
2868 vfree(buf);
2869 return ret;
2872 path->reada = 2;
2873 /* clone data */
2874 key.objectid = btrfs_ino(src);
2875 key.type = BTRFS_EXTENT_DATA_KEY;
2876 key.offset = 0;
2878 while (1) {
2880 * note the key will change type as we walk through the
2881 * tree.
2883 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2884 0, 0);
2885 if (ret < 0)
2886 goto out;
2888 nritems = btrfs_header_nritems(path->nodes[0]);
2889 if (path->slots[0] >= nritems) {
2890 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2891 if (ret < 0)
2892 goto out;
2893 if (ret > 0)
2894 break;
2895 nritems = btrfs_header_nritems(path->nodes[0]);
2897 leaf = path->nodes[0];
2898 slot = path->slots[0];
2900 btrfs_item_key_to_cpu(leaf, &key, slot);
2901 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2902 key.objectid != btrfs_ino(src))
2903 break;
2905 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2906 struct btrfs_file_extent_item *extent;
2907 int type;
2908 u32 size;
2909 struct btrfs_key new_key;
2910 u64 disko = 0, diskl = 0;
2911 u64 datao = 0, datal = 0;
2912 u8 comp;
2913 u64 endoff;
2915 size = btrfs_item_size_nr(leaf, slot);
2916 read_extent_buffer(leaf, buf,
2917 btrfs_item_ptr_offset(leaf, slot),
2918 size);
2920 extent = btrfs_item_ptr(leaf, slot,
2921 struct btrfs_file_extent_item);
2922 comp = btrfs_file_extent_compression(leaf, extent);
2923 type = btrfs_file_extent_type(leaf, extent);
2924 if (type == BTRFS_FILE_EXTENT_REG ||
2925 type == BTRFS_FILE_EXTENT_PREALLOC) {
2926 disko = btrfs_file_extent_disk_bytenr(leaf,
2927 extent);
2928 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2929 extent);
2930 datao = btrfs_file_extent_offset(leaf, extent);
2931 datal = btrfs_file_extent_num_bytes(leaf,
2932 extent);
2933 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2934 /* take upper bound, may be compressed */
2935 datal = btrfs_file_extent_ram_bytes(leaf,
2936 extent);
2938 btrfs_release_path(path);
2940 if (key.offset + datal <= off ||
2941 key.offset >= off + len - 1)
2942 goto next;
2944 memcpy(&new_key, &key, sizeof(new_key));
2945 new_key.objectid = btrfs_ino(inode);
2946 if (off <= key.offset)
2947 new_key.offset = key.offset + destoff - off;
2948 else
2949 new_key.offset = destoff;
2952 * 1 - adjusting old extent (we may have to split it)
2953 * 1 - add new extent
2954 * 1 - inode update
2956 trans = btrfs_start_transaction(root, 3);
2957 if (IS_ERR(trans)) {
2958 ret = PTR_ERR(trans);
2959 goto out;
2962 if (type == BTRFS_FILE_EXTENT_REG ||
2963 type == BTRFS_FILE_EXTENT_PREALLOC) {
2965 * a | --- range to clone ---| b
2966 * | ------------- extent ------------- |
2969 /* substract range b */
2970 if (key.offset + datal > off + len)
2971 datal = off + len - key.offset;
2973 /* substract range a */
2974 if (off > key.offset) {
2975 datao += off - key.offset;
2976 datal -= off - key.offset;
2979 ret = btrfs_drop_extents(trans, root, inode,
2980 new_key.offset,
2981 new_key.offset + datal,
2983 if (ret) {
2984 btrfs_abort_transaction(trans, root,
2985 ret);
2986 btrfs_end_transaction(trans, root);
2987 goto out;
2990 ret = btrfs_insert_empty_item(trans, root, path,
2991 &new_key, size);
2992 if (ret) {
2993 btrfs_abort_transaction(trans, root,
2994 ret);
2995 btrfs_end_transaction(trans, root);
2996 goto out;
2999 leaf = path->nodes[0];
3000 slot = path->slots[0];
3001 write_extent_buffer(leaf, buf,
3002 btrfs_item_ptr_offset(leaf, slot),
3003 size);
3005 extent = btrfs_item_ptr(leaf, slot,
3006 struct btrfs_file_extent_item);
3008 /* disko == 0 means it's a hole */
3009 if (!disko)
3010 datao = 0;
3012 btrfs_set_file_extent_offset(leaf, extent,
3013 datao);
3014 btrfs_set_file_extent_num_bytes(leaf, extent,
3015 datal);
3016 if (disko) {
3017 inode_add_bytes(inode, datal);
3018 ret = btrfs_inc_extent_ref(trans, root,
3019 disko, diskl, 0,
3020 root->root_key.objectid,
3021 btrfs_ino(inode),
3022 new_key.offset - datao,
3024 if (ret) {
3025 btrfs_abort_transaction(trans,
3026 root,
3027 ret);
3028 btrfs_end_transaction(trans,
3029 root);
3030 goto out;
3034 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3035 u64 skip = 0;
3036 u64 trim = 0;
3037 if (off > key.offset) {
3038 skip = off - key.offset;
3039 new_key.offset += skip;
3042 if (key.offset + datal > off + len)
3043 trim = key.offset + datal - (off + len);
3045 if (comp && (skip || trim)) {
3046 ret = -EINVAL;
3047 btrfs_end_transaction(trans, root);
3048 goto out;
3050 size -= skip + trim;
3051 datal -= skip + trim;
3053 ret = btrfs_drop_extents(trans, root, inode,
3054 new_key.offset,
3055 new_key.offset + datal,
3057 if (ret) {
3058 btrfs_abort_transaction(trans, root,
3059 ret);
3060 btrfs_end_transaction(trans, root);
3061 goto out;
3064 ret = btrfs_insert_empty_item(trans, root, path,
3065 &new_key, size);
3066 if (ret) {
3067 btrfs_abort_transaction(trans, root,
3068 ret);
3069 btrfs_end_transaction(trans, root);
3070 goto out;
3073 if (skip) {
3074 u32 start =
3075 btrfs_file_extent_calc_inline_size(0);
3076 memmove(buf+start, buf+start+skip,
3077 datal);
3080 leaf = path->nodes[0];
3081 slot = path->slots[0];
3082 write_extent_buffer(leaf, buf,
3083 btrfs_item_ptr_offset(leaf, slot),
3084 size);
3085 inode_add_bytes(inode, datal);
3088 btrfs_mark_buffer_dirty(leaf);
3089 btrfs_release_path(path);
3091 inode_inc_iversion(inode);
3092 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3095 * we round up to the block size at eof when
3096 * determining which extents to clone above,
3097 * but shouldn't round up the file size
3099 endoff = new_key.offset + datal;
3100 if (endoff > destoff+olen)
3101 endoff = destoff+olen;
3102 if (endoff > inode->i_size)
3103 btrfs_i_size_write(inode, endoff);
3105 ret = btrfs_update_inode(trans, root, inode);
3106 if (ret) {
3107 btrfs_abort_transaction(trans, root, ret);
3108 btrfs_end_transaction(trans, root);
3109 goto out;
3111 ret = btrfs_end_transaction(trans, root);
3113 next:
3114 btrfs_release_path(path);
3115 key.offset++;
3117 ret = 0;
3119 out:
3120 btrfs_release_path(path);
3121 btrfs_free_path(path);
3122 vfree(buf);
3123 return ret;
3126 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3127 u64 off, u64 olen, u64 destoff)
3129 struct inode *inode = fdentry(file)->d_inode;
3130 struct btrfs_root *root = BTRFS_I(inode)->root;
3131 struct fd src_file;
3132 struct inode *src;
3133 int ret;
3134 u64 len = olen;
3135 u64 bs = root->fs_info->sb->s_blocksize;
3136 int same_inode = 0;
3139 * TODO:
3140 * - split compressed inline extents. annoying: we need to
3141 * decompress into destination's address_space (the file offset
3142 * may change, so source mapping won't do), then recompress (or
3143 * otherwise reinsert) a subrange.
3144 * - allow ranges within the same file to be cloned (provided
3145 * they don't overlap)?
3148 /* the destination must be opened for writing */
3149 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3150 return -EINVAL;
3152 if (btrfs_root_readonly(root))
3153 return -EROFS;
3155 ret = mnt_want_write_file(file);
3156 if (ret)
3157 return ret;
3159 src_file = fdget(srcfd);
3160 if (!src_file.file) {
3161 ret = -EBADF;
3162 goto out_drop_write;
3165 ret = -EXDEV;
3166 if (src_file.file->f_path.mnt != file->f_path.mnt)
3167 goto out_fput;
3169 src = file_inode(src_file.file);
3171 ret = -EINVAL;
3172 if (src == inode)
3173 same_inode = 1;
3175 /* the src must be open for reading */
3176 if (!(src_file.file->f_mode & FMODE_READ))
3177 goto out_fput;
3179 /* don't make the dst file partly checksummed */
3180 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3181 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3182 goto out_fput;
3184 ret = -EISDIR;
3185 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3186 goto out_fput;
3188 ret = -EXDEV;
3189 if (src->i_sb != inode->i_sb)
3190 goto out_fput;
3192 if (!same_inode) {
3193 if (inode < src) {
3194 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3195 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3196 } else {
3197 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3198 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3200 } else {
3201 mutex_lock(&src->i_mutex);
3204 /* determine range to clone */
3205 ret = -EINVAL;
3206 if (off + len > src->i_size || off + len < off)
3207 goto out_unlock;
3208 if (len == 0)
3209 olen = len = src->i_size - off;
3210 /* if we extend to eof, continue to block boundary */
3211 if (off + len == src->i_size)
3212 len = ALIGN(src->i_size, bs) - off;
3214 /* verify the end result is block aligned */
3215 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3216 !IS_ALIGNED(destoff, bs))
3217 goto out_unlock;
3219 /* verify if ranges are overlapped within the same file */
3220 if (same_inode) {
3221 if (destoff + len > off && destoff < off + len)
3222 goto out_unlock;
3225 if (destoff > inode->i_size) {
3226 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3227 if (ret)
3228 goto out_unlock;
3231 /* truncate page cache pages from target inode range */
3232 truncate_inode_pages_range(&inode->i_data, destoff,
3233 PAGE_CACHE_ALIGN(destoff + len) - 1);
3235 lock_extent_range(src, off, len);
3237 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3239 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3240 out_unlock:
3241 mutex_unlock(&src->i_mutex);
3242 if (!same_inode)
3243 mutex_unlock(&inode->i_mutex);
3244 out_fput:
3245 fdput(src_file);
3246 out_drop_write:
3247 mnt_drop_write_file(file);
3248 return ret;
3251 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3253 struct btrfs_ioctl_clone_range_args args;
3255 if (copy_from_user(&args, argp, sizeof(args)))
3256 return -EFAULT;
3257 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3258 args.src_length, args.dest_offset);
3262 * there are many ways the trans_start and trans_end ioctls can lead
3263 * to deadlocks. They should only be used by applications that
3264 * basically own the machine, and have a very in depth understanding
3265 * of all the possible deadlocks and enospc problems.
3267 static long btrfs_ioctl_trans_start(struct file *file)
3269 struct inode *inode = file_inode(file);
3270 struct btrfs_root *root = BTRFS_I(inode)->root;
3271 struct btrfs_trans_handle *trans;
3272 int ret;
3274 ret = -EPERM;
3275 if (!capable(CAP_SYS_ADMIN))
3276 goto out;
3278 ret = -EINPROGRESS;
3279 if (file->private_data)
3280 goto out;
3282 ret = -EROFS;
3283 if (btrfs_root_readonly(root))
3284 goto out;
3286 ret = mnt_want_write_file(file);
3287 if (ret)
3288 goto out;
3290 atomic_inc(&root->fs_info->open_ioctl_trans);
3292 ret = -ENOMEM;
3293 trans = btrfs_start_ioctl_transaction(root);
3294 if (IS_ERR(trans))
3295 goto out_drop;
3297 file->private_data = trans;
3298 return 0;
3300 out_drop:
3301 atomic_dec(&root->fs_info->open_ioctl_trans);
3302 mnt_drop_write_file(file);
3303 out:
3304 return ret;
3307 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3309 struct inode *inode = file_inode(file);
3310 struct btrfs_root *root = BTRFS_I(inode)->root;
3311 struct btrfs_root *new_root;
3312 struct btrfs_dir_item *di;
3313 struct btrfs_trans_handle *trans;
3314 struct btrfs_path *path;
3315 struct btrfs_key location;
3316 struct btrfs_disk_key disk_key;
3317 u64 objectid = 0;
3318 u64 dir_id;
3319 int ret;
3321 if (!capable(CAP_SYS_ADMIN))
3322 return -EPERM;
3324 ret = mnt_want_write_file(file);
3325 if (ret)
3326 return ret;
3328 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3329 ret = -EFAULT;
3330 goto out;
3333 if (!objectid)
3334 objectid = BTRFS_FS_TREE_OBJECTID;
3336 location.objectid = objectid;
3337 location.type = BTRFS_ROOT_ITEM_KEY;
3338 location.offset = (u64)-1;
3340 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
3341 if (IS_ERR(new_root)) {
3342 ret = PTR_ERR(new_root);
3343 goto out;
3346 path = btrfs_alloc_path();
3347 if (!path) {
3348 ret = -ENOMEM;
3349 goto out;
3351 path->leave_spinning = 1;
3353 trans = btrfs_start_transaction(root, 1);
3354 if (IS_ERR(trans)) {
3355 btrfs_free_path(path);
3356 ret = PTR_ERR(trans);
3357 goto out;
3360 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
3361 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3362 dir_id, "default", 7, 1);
3363 if (IS_ERR_OR_NULL(di)) {
3364 btrfs_free_path(path);
3365 btrfs_end_transaction(trans, root);
3366 printk(KERN_ERR "Umm, you don't have the default dir item, "
3367 "this isn't going to work\n");
3368 ret = -ENOENT;
3369 goto out;
3372 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3373 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3374 btrfs_mark_buffer_dirty(path->nodes[0]);
3375 btrfs_free_path(path);
3377 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
3378 btrfs_end_transaction(trans, root);
3379 out:
3380 mnt_drop_write_file(file);
3381 return ret;
3384 void btrfs_get_block_group_info(struct list_head *groups_list,
3385 struct btrfs_ioctl_space_info *space)
3387 struct btrfs_block_group_cache *block_group;
3389 space->total_bytes = 0;
3390 space->used_bytes = 0;
3391 space->flags = 0;
3392 list_for_each_entry(block_group, groups_list, list) {
3393 space->flags = block_group->flags;
3394 space->total_bytes += block_group->key.offset;
3395 space->used_bytes +=
3396 btrfs_block_group_used(&block_group->item);
3400 static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
3402 struct btrfs_ioctl_space_args space_args;
3403 struct btrfs_ioctl_space_info space;
3404 struct btrfs_ioctl_space_info *dest;
3405 struct btrfs_ioctl_space_info *dest_orig;
3406 struct btrfs_ioctl_space_info __user *user_dest;
3407 struct btrfs_space_info *info;
3408 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3409 BTRFS_BLOCK_GROUP_SYSTEM,
3410 BTRFS_BLOCK_GROUP_METADATA,
3411 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3412 int num_types = 4;
3413 int alloc_size;
3414 int ret = 0;
3415 u64 slot_count = 0;
3416 int i, c;
3418 if (copy_from_user(&space_args,
3419 (struct btrfs_ioctl_space_args __user *)arg,
3420 sizeof(space_args)))
3421 return -EFAULT;
3423 for (i = 0; i < num_types; i++) {
3424 struct btrfs_space_info *tmp;
3426 info = NULL;
3427 rcu_read_lock();
3428 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3429 list) {
3430 if (tmp->flags == types[i]) {
3431 info = tmp;
3432 break;
3435 rcu_read_unlock();
3437 if (!info)
3438 continue;
3440 down_read(&info->groups_sem);
3441 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3442 if (!list_empty(&info->block_groups[c]))
3443 slot_count++;
3445 up_read(&info->groups_sem);
3448 /* space_slots == 0 means they are asking for a count */
3449 if (space_args.space_slots == 0) {
3450 space_args.total_spaces = slot_count;
3451 goto out;
3454 slot_count = min_t(u64, space_args.space_slots, slot_count);
3456 alloc_size = sizeof(*dest) * slot_count;
3458 /* we generally have at most 6 or so space infos, one for each raid
3459 * level. So, a whole page should be more than enough for everyone
3461 if (alloc_size > PAGE_CACHE_SIZE)
3462 return -ENOMEM;
3464 space_args.total_spaces = 0;
3465 dest = kmalloc(alloc_size, GFP_NOFS);
3466 if (!dest)
3467 return -ENOMEM;
3468 dest_orig = dest;
3470 /* now we have a buffer to copy into */
3471 for (i = 0; i < num_types; i++) {
3472 struct btrfs_space_info *tmp;
3474 if (!slot_count)
3475 break;
3477 info = NULL;
3478 rcu_read_lock();
3479 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3480 list) {
3481 if (tmp->flags == types[i]) {
3482 info = tmp;
3483 break;
3486 rcu_read_unlock();
3488 if (!info)
3489 continue;
3490 down_read(&info->groups_sem);
3491 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3492 if (!list_empty(&info->block_groups[c])) {
3493 btrfs_get_block_group_info(
3494 &info->block_groups[c], &space);
3495 memcpy(dest, &space, sizeof(space));
3496 dest++;
3497 space_args.total_spaces++;
3498 slot_count--;
3500 if (!slot_count)
3501 break;
3503 up_read(&info->groups_sem);
3506 user_dest = (struct btrfs_ioctl_space_info __user *)
3507 (arg + sizeof(struct btrfs_ioctl_space_args));
3509 if (copy_to_user(user_dest, dest_orig, alloc_size))
3510 ret = -EFAULT;
3512 kfree(dest_orig);
3513 out:
3514 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3515 ret = -EFAULT;
3517 return ret;
3521 * there are many ways the trans_start and trans_end ioctls can lead
3522 * to deadlocks. They should only be used by applications that
3523 * basically own the machine, and have a very in depth understanding
3524 * of all the possible deadlocks and enospc problems.
3526 long btrfs_ioctl_trans_end(struct file *file)
3528 struct inode *inode = file_inode(file);
3529 struct btrfs_root *root = BTRFS_I(inode)->root;
3530 struct btrfs_trans_handle *trans;
3532 trans = file->private_data;
3533 if (!trans)
3534 return -EINVAL;
3535 file->private_data = NULL;
3537 btrfs_end_transaction(trans, root);
3539 atomic_dec(&root->fs_info->open_ioctl_trans);
3541 mnt_drop_write_file(file);
3542 return 0;
3545 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3546 void __user *argp)
3548 struct btrfs_trans_handle *trans;
3549 u64 transid;
3550 int ret;
3552 trans = btrfs_attach_transaction_barrier(root);
3553 if (IS_ERR(trans)) {
3554 if (PTR_ERR(trans) != -ENOENT)
3555 return PTR_ERR(trans);
3557 /* No running transaction, don't bother */
3558 transid = root->fs_info->last_trans_committed;
3559 goto out;
3561 transid = trans->transid;
3562 ret = btrfs_commit_transaction_async(trans, root, 0);
3563 if (ret) {
3564 btrfs_end_transaction(trans, root);
3565 return ret;
3567 out:
3568 if (argp)
3569 if (copy_to_user(argp, &transid, sizeof(transid)))
3570 return -EFAULT;
3571 return 0;
3574 static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3575 void __user *argp)
3577 u64 transid;
3579 if (argp) {
3580 if (copy_from_user(&transid, argp, sizeof(transid)))
3581 return -EFAULT;
3582 } else {
3583 transid = 0; /* current trans */
3585 return btrfs_wait_for_commit(root, transid);
3588 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3590 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3591 struct btrfs_ioctl_scrub_args *sa;
3592 int ret;
3594 if (!capable(CAP_SYS_ADMIN))
3595 return -EPERM;
3597 sa = memdup_user(arg, sizeof(*sa));
3598 if (IS_ERR(sa))
3599 return PTR_ERR(sa);
3601 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3602 ret = mnt_want_write_file(file);
3603 if (ret)
3604 goto out;
3607 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
3608 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3611 if (copy_to_user(arg, sa, sizeof(*sa)))
3612 ret = -EFAULT;
3614 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3615 mnt_drop_write_file(file);
3616 out:
3617 kfree(sa);
3618 return ret;
3621 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3623 if (!capable(CAP_SYS_ADMIN))
3624 return -EPERM;
3626 return btrfs_scrub_cancel(root->fs_info);
3629 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3630 void __user *arg)
3632 struct btrfs_ioctl_scrub_args *sa;
3633 int ret;
3635 if (!capable(CAP_SYS_ADMIN))
3636 return -EPERM;
3638 sa = memdup_user(arg, sizeof(*sa));
3639 if (IS_ERR(sa))
3640 return PTR_ERR(sa);
3642 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3644 if (copy_to_user(arg, sa, sizeof(*sa)))
3645 ret = -EFAULT;
3647 kfree(sa);
3648 return ret;
3651 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3652 void __user *arg)
3654 struct btrfs_ioctl_get_dev_stats *sa;
3655 int ret;
3657 sa = memdup_user(arg, sizeof(*sa));
3658 if (IS_ERR(sa))
3659 return PTR_ERR(sa);
3661 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3662 kfree(sa);
3663 return -EPERM;
3666 ret = btrfs_get_dev_stats(root, sa);
3668 if (copy_to_user(arg, sa, sizeof(*sa)))
3669 ret = -EFAULT;
3671 kfree(sa);
3672 return ret;
3675 static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3677 struct btrfs_ioctl_dev_replace_args *p;
3678 int ret;
3680 if (!capable(CAP_SYS_ADMIN))
3681 return -EPERM;
3683 p = memdup_user(arg, sizeof(*p));
3684 if (IS_ERR(p))
3685 return PTR_ERR(p);
3687 switch (p->cmd) {
3688 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3689 if (root->fs_info->sb->s_flags & MS_RDONLY)
3690 return -EROFS;
3692 if (atomic_xchg(
3693 &root->fs_info->mutually_exclusive_operation_running,
3694 1)) {
3695 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3696 } else {
3697 ret = btrfs_dev_replace_start(root, p);
3698 atomic_set(
3699 &root->fs_info->mutually_exclusive_operation_running,
3702 break;
3703 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3704 btrfs_dev_replace_status(root->fs_info, p);
3705 ret = 0;
3706 break;
3707 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3708 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3709 break;
3710 default:
3711 ret = -EINVAL;
3712 break;
3715 if (copy_to_user(arg, p, sizeof(*p)))
3716 ret = -EFAULT;
3718 kfree(p);
3719 return ret;
3722 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3724 int ret = 0;
3725 int i;
3726 u64 rel_ptr;
3727 int size;
3728 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3729 struct inode_fs_paths *ipath = NULL;
3730 struct btrfs_path *path;
3732 if (!capable(CAP_DAC_READ_SEARCH))
3733 return -EPERM;
3735 path = btrfs_alloc_path();
3736 if (!path) {
3737 ret = -ENOMEM;
3738 goto out;
3741 ipa = memdup_user(arg, sizeof(*ipa));
3742 if (IS_ERR(ipa)) {
3743 ret = PTR_ERR(ipa);
3744 ipa = NULL;
3745 goto out;
3748 size = min_t(u32, ipa->size, 4096);
3749 ipath = init_ipath(size, root, path);
3750 if (IS_ERR(ipath)) {
3751 ret = PTR_ERR(ipath);
3752 ipath = NULL;
3753 goto out;
3756 ret = paths_from_inode(ipa->inum, ipath);
3757 if (ret < 0)
3758 goto out;
3760 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3761 rel_ptr = ipath->fspath->val[i] -
3762 (u64)(unsigned long)ipath->fspath->val;
3763 ipath->fspath->val[i] = rel_ptr;
3766 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3767 (void *)(unsigned long)ipath->fspath, size);
3768 if (ret) {
3769 ret = -EFAULT;
3770 goto out;
3773 out:
3774 btrfs_free_path(path);
3775 free_ipath(ipath);
3776 kfree(ipa);
3778 return ret;
3781 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3783 struct btrfs_data_container *inodes = ctx;
3784 const size_t c = 3 * sizeof(u64);
3786 if (inodes->bytes_left >= c) {
3787 inodes->bytes_left -= c;
3788 inodes->val[inodes->elem_cnt] = inum;
3789 inodes->val[inodes->elem_cnt + 1] = offset;
3790 inodes->val[inodes->elem_cnt + 2] = root;
3791 inodes->elem_cnt += 3;
3792 } else {
3793 inodes->bytes_missing += c - inodes->bytes_left;
3794 inodes->bytes_left = 0;
3795 inodes->elem_missed += 3;
3798 return 0;
3801 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3802 void __user *arg)
3804 int ret = 0;
3805 int size;
3806 struct btrfs_ioctl_logical_ino_args *loi;
3807 struct btrfs_data_container *inodes = NULL;
3808 struct btrfs_path *path = NULL;
3810 if (!capable(CAP_SYS_ADMIN))
3811 return -EPERM;
3813 loi = memdup_user(arg, sizeof(*loi));
3814 if (IS_ERR(loi)) {
3815 ret = PTR_ERR(loi);
3816 loi = NULL;
3817 goto out;
3820 path = btrfs_alloc_path();
3821 if (!path) {
3822 ret = -ENOMEM;
3823 goto out;
3826 size = min_t(u32, loi->size, 64 * 1024);
3827 inodes = init_data_container(size);
3828 if (IS_ERR(inodes)) {
3829 ret = PTR_ERR(inodes);
3830 inodes = NULL;
3831 goto out;
3834 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3835 build_ino_list, inodes);
3836 if (ret == -EINVAL)
3837 ret = -ENOENT;
3838 if (ret < 0)
3839 goto out;
3841 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3842 (void *)(unsigned long)inodes, size);
3843 if (ret)
3844 ret = -EFAULT;
3846 out:
3847 btrfs_free_path(path);
3848 vfree(inodes);
3849 kfree(loi);
3851 return ret;
3854 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3855 struct btrfs_ioctl_balance_args *bargs)
3857 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3859 bargs->flags = bctl->flags;
3861 if (atomic_read(&fs_info->balance_running))
3862 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3863 if (atomic_read(&fs_info->balance_pause_req))
3864 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3865 if (atomic_read(&fs_info->balance_cancel_req))
3866 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3868 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3869 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3870 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3872 if (lock) {
3873 spin_lock(&fs_info->balance_lock);
3874 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3875 spin_unlock(&fs_info->balance_lock);
3876 } else {
3877 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3881 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3883 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3884 struct btrfs_fs_info *fs_info = root->fs_info;
3885 struct btrfs_ioctl_balance_args *bargs;
3886 struct btrfs_balance_control *bctl;
3887 bool need_unlock; /* for mut. excl. ops lock */
3888 int ret;
3890 if (!capable(CAP_SYS_ADMIN))
3891 return -EPERM;
3893 ret = mnt_want_write_file(file);
3894 if (ret)
3895 return ret;
3897 again:
3898 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3899 mutex_lock(&fs_info->volume_mutex);
3900 mutex_lock(&fs_info->balance_mutex);
3901 need_unlock = true;
3902 goto locked;
3906 * mut. excl. ops lock is locked. Three possibilites:
3907 * (1) some other op is running
3908 * (2) balance is running
3909 * (3) balance is paused -- special case (think resume)
3911 mutex_lock(&fs_info->balance_mutex);
3912 if (fs_info->balance_ctl) {
3913 /* this is either (2) or (3) */
3914 if (!atomic_read(&fs_info->balance_running)) {
3915 mutex_unlock(&fs_info->balance_mutex);
3916 if (!mutex_trylock(&fs_info->volume_mutex))
3917 goto again;
3918 mutex_lock(&fs_info->balance_mutex);
3920 if (fs_info->balance_ctl &&
3921 !atomic_read(&fs_info->balance_running)) {
3922 /* this is (3) */
3923 need_unlock = false;
3924 goto locked;
3927 mutex_unlock(&fs_info->balance_mutex);
3928 mutex_unlock(&fs_info->volume_mutex);
3929 goto again;
3930 } else {
3931 /* this is (2) */
3932 mutex_unlock(&fs_info->balance_mutex);
3933 ret = -EINPROGRESS;
3934 goto out;
3936 } else {
3937 /* this is (1) */
3938 mutex_unlock(&fs_info->balance_mutex);
3939 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3940 goto out;
3943 locked:
3944 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3946 if (arg) {
3947 bargs = memdup_user(arg, sizeof(*bargs));
3948 if (IS_ERR(bargs)) {
3949 ret = PTR_ERR(bargs);
3950 goto out_unlock;
3953 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3954 if (!fs_info->balance_ctl) {
3955 ret = -ENOTCONN;
3956 goto out_bargs;
3959 bctl = fs_info->balance_ctl;
3960 spin_lock(&fs_info->balance_lock);
3961 bctl->flags |= BTRFS_BALANCE_RESUME;
3962 spin_unlock(&fs_info->balance_lock);
3964 goto do_balance;
3966 } else {
3967 bargs = NULL;
3970 if (fs_info->balance_ctl) {
3971 ret = -EINPROGRESS;
3972 goto out_bargs;
3975 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3976 if (!bctl) {
3977 ret = -ENOMEM;
3978 goto out_bargs;
3981 bctl->fs_info = fs_info;
3982 if (arg) {
3983 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3984 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3985 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3987 bctl->flags = bargs->flags;
3988 } else {
3989 /* balance everything - no filters */
3990 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3993 do_balance:
3995 * Ownership of bctl and mutually_exclusive_operation_running
3996 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3997 * or, if restriper was paused all the way until unmount, in
3998 * free_fs_info. mutually_exclusive_operation_running is
3999 * cleared in __cancel_balance.
4001 need_unlock = false;
4003 ret = btrfs_balance(bctl, bargs);
4005 if (arg) {
4006 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4007 ret = -EFAULT;
4010 out_bargs:
4011 kfree(bargs);
4012 out_unlock:
4013 mutex_unlock(&fs_info->balance_mutex);
4014 mutex_unlock(&fs_info->volume_mutex);
4015 if (need_unlock)
4016 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4017 out:
4018 mnt_drop_write_file(file);
4019 return ret;
4022 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4024 if (!capable(CAP_SYS_ADMIN))
4025 return -EPERM;
4027 switch (cmd) {
4028 case BTRFS_BALANCE_CTL_PAUSE:
4029 return btrfs_pause_balance(root->fs_info);
4030 case BTRFS_BALANCE_CTL_CANCEL:
4031 return btrfs_cancel_balance(root->fs_info);
4034 return -EINVAL;
4037 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4038 void __user *arg)
4040 struct btrfs_fs_info *fs_info = root->fs_info;
4041 struct btrfs_ioctl_balance_args *bargs;
4042 int ret = 0;
4044 if (!capable(CAP_SYS_ADMIN))
4045 return -EPERM;
4047 mutex_lock(&fs_info->balance_mutex);
4048 if (!fs_info->balance_ctl) {
4049 ret = -ENOTCONN;
4050 goto out;
4053 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4054 if (!bargs) {
4055 ret = -ENOMEM;
4056 goto out;
4059 update_ioctl_balance_args(fs_info, 1, bargs);
4061 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4062 ret = -EFAULT;
4064 kfree(bargs);
4065 out:
4066 mutex_unlock(&fs_info->balance_mutex);
4067 return ret;
4070 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4072 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4073 struct btrfs_ioctl_quota_ctl_args *sa;
4074 struct btrfs_trans_handle *trans = NULL;
4075 int ret;
4076 int err;
4078 if (!capable(CAP_SYS_ADMIN))
4079 return -EPERM;
4081 ret = mnt_want_write_file(file);
4082 if (ret)
4083 return ret;
4085 sa = memdup_user(arg, sizeof(*sa));
4086 if (IS_ERR(sa)) {
4087 ret = PTR_ERR(sa);
4088 goto drop_write;
4091 down_write(&root->fs_info->subvol_sem);
4092 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4093 if (IS_ERR(trans)) {
4094 ret = PTR_ERR(trans);
4095 goto out;
4098 switch (sa->cmd) {
4099 case BTRFS_QUOTA_CTL_ENABLE:
4100 ret = btrfs_quota_enable(trans, root->fs_info);
4101 break;
4102 case BTRFS_QUOTA_CTL_DISABLE:
4103 ret = btrfs_quota_disable(trans, root->fs_info);
4104 break;
4105 default:
4106 ret = -EINVAL;
4107 break;
4110 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4111 if (err && !ret)
4112 ret = err;
4113 out:
4114 kfree(sa);
4115 up_write(&root->fs_info->subvol_sem);
4116 drop_write:
4117 mnt_drop_write_file(file);
4118 return ret;
4121 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4123 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4124 struct btrfs_ioctl_qgroup_assign_args *sa;
4125 struct btrfs_trans_handle *trans;
4126 int ret;
4127 int err;
4129 if (!capable(CAP_SYS_ADMIN))
4130 return -EPERM;
4132 ret = mnt_want_write_file(file);
4133 if (ret)
4134 return ret;
4136 sa = memdup_user(arg, sizeof(*sa));
4137 if (IS_ERR(sa)) {
4138 ret = PTR_ERR(sa);
4139 goto drop_write;
4142 trans = btrfs_join_transaction(root);
4143 if (IS_ERR(trans)) {
4144 ret = PTR_ERR(trans);
4145 goto out;
4148 /* FIXME: check if the IDs really exist */
4149 if (sa->assign) {
4150 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4151 sa->src, sa->dst);
4152 } else {
4153 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4154 sa->src, sa->dst);
4157 err = btrfs_end_transaction(trans, root);
4158 if (err && !ret)
4159 ret = err;
4161 out:
4162 kfree(sa);
4163 drop_write:
4164 mnt_drop_write_file(file);
4165 return ret;
4168 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4170 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4171 struct btrfs_ioctl_qgroup_create_args *sa;
4172 struct btrfs_trans_handle *trans;
4173 int ret;
4174 int err;
4176 if (!capable(CAP_SYS_ADMIN))
4177 return -EPERM;
4179 ret = mnt_want_write_file(file);
4180 if (ret)
4181 return ret;
4183 sa = memdup_user(arg, sizeof(*sa));
4184 if (IS_ERR(sa)) {
4185 ret = PTR_ERR(sa);
4186 goto drop_write;
4189 if (!sa->qgroupid) {
4190 ret = -EINVAL;
4191 goto out;
4194 trans = btrfs_join_transaction(root);
4195 if (IS_ERR(trans)) {
4196 ret = PTR_ERR(trans);
4197 goto out;
4200 /* FIXME: check if the IDs really exist */
4201 if (sa->create) {
4202 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4203 NULL);
4204 } else {
4205 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4208 err = btrfs_end_transaction(trans, root);
4209 if (err && !ret)
4210 ret = err;
4212 out:
4213 kfree(sa);
4214 drop_write:
4215 mnt_drop_write_file(file);
4216 return ret;
4219 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4221 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4222 struct btrfs_ioctl_qgroup_limit_args *sa;
4223 struct btrfs_trans_handle *trans;
4224 int ret;
4225 int err;
4226 u64 qgroupid;
4228 if (!capable(CAP_SYS_ADMIN))
4229 return -EPERM;
4231 ret = mnt_want_write_file(file);
4232 if (ret)
4233 return ret;
4235 sa = memdup_user(arg, sizeof(*sa));
4236 if (IS_ERR(sa)) {
4237 ret = PTR_ERR(sa);
4238 goto drop_write;
4241 trans = btrfs_join_transaction(root);
4242 if (IS_ERR(trans)) {
4243 ret = PTR_ERR(trans);
4244 goto out;
4247 qgroupid = sa->qgroupid;
4248 if (!qgroupid) {
4249 /* take the current subvol as qgroup */
4250 qgroupid = root->root_key.objectid;
4253 /* FIXME: check if the IDs really exist */
4254 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4256 err = btrfs_end_transaction(trans, root);
4257 if (err && !ret)
4258 ret = err;
4260 out:
4261 kfree(sa);
4262 drop_write:
4263 mnt_drop_write_file(file);
4264 return ret;
4267 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4269 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4270 struct btrfs_ioctl_quota_rescan_args *qsa;
4271 int ret;
4273 if (!capable(CAP_SYS_ADMIN))
4274 return -EPERM;
4276 ret = mnt_want_write_file(file);
4277 if (ret)
4278 return ret;
4280 qsa = memdup_user(arg, sizeof(*qsa));
4281 if (IS_ERR(qsa)) {
4282 ret = PTR_ERR(qsa);
4283 goto drop_write;
4286 if (qsa->flags) {
4287 ret = -EINVAL;
4288 goto out;
4291 ret = btrfs_qgroup_rescan(root->fs_info);
4293 out:
4294 kfree(qsa);
4295 drop_write:
4296 mnt_drop_write_file(file);
4297 return ret;
4300 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4302 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4303 struct btrfs_ioctl_quota_rescan_args *qsa;
4304 int ret = 0;
4306 if (!capable(CAP_SYS_ADMIN))
4307 return -EPERM;
4309 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4310 if (!qsa)
4311 return -ENOMEM;
4313 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4314 qsa->flags = 1;
4315 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4318 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4319 ret = -EFAULT;
4321 kfree(qsa);
4322 return ret;
4325 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4327 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4329 if (!capable(CAP_SYS_ADMIN))
4330 return -EPERM;
4332 return btrfs_qgroup_wait_for_completion(root->fs_info);
4335 static long btrfs_ioctl_set_received_subvol(struct file *file,
4336 void __user *arg)
4338 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4339 struct inode *inode = file_inode(file);
4340 struct btrfs_root *root = BTRFS_I(inode)->root;
4341 struct btrfs_root_item *root_item = &root->root_item;
4342 struct btrfs_trans_handle *trans;
4343 struct timespec ct = CURRENT_TIME;
4344 int ret = 0;
4345 int received_uuid_changed;
4347 ret = mnt_want_write_file(file);
4348 if (ret < 0)
4349 return ret;
4351 down_write(&root->fs_info->subvol_sem);
4353 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4354 ret = -EINVAL;
4355 goto out;
4358 if (btrfs_root_readonly(root)) {
4359 ret = -EROFS;
4360 goto out;
4363 if (!inode_owner_or_capable(inode)) {
4364 ret = -EACCES;
4365 goto out;
4368 sa = memdup_user(arg, sizeof(*sa));
4369 if (IS_ERR(sa)) {
4370 ret = PTR_ERR(sa);
4371 sa = NULL;
4372 goto out;
4376 * 1 - root item
4377 * 2 - uuid items (received uuid + subvol uuid)
4379 trans = btrfs_start_transaction(root, 3);
4380 if (IS_ERR(trans)) {
4381 ret = PTR_ERR(trans);
4382 trans = NULL;
4383 goto out;
4386 sa->rtransid = trans->transid;
4387 sa->rtime.sec = ct.tv_sec;
4388 sa->rtime.nsec = ct.tv_nsec;
4390 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4391 BTRFS_UUID_SIZE);
4392 if (received_uuid_changed &&
4393 !btrfs_is_empty_uuid(root_item->received_uuid))
4394 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4395 root_item->received_uuid,
4396 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4397 root->root_key.objectid);
4398 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4399 btrfs_set_root_stransid(root_item, sa->stransid);
4400 btrfs_set_root_rtransid(root_item, sa->rtransid);
4401 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4402 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4403 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4404 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4406 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4407 &root->root_key, &root->root_item);
4408 if (ret < 0) {
4409 btrfs_end_transaction(trans, root);
4410 goto out;
4412 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4413 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4414 sa->uuid,
4415 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4416 root->root_key.objectid);
4417 if (ret < 0 && ret != -EEXIST) {
4418 btrfs_abort_transaction(trans, root, ret);
4419 goto out;
4422 ret = btrfs_commit_transaction(trans, root);
4423 if (ret < 0) {
4424 btrfs_abort_transaction(trans, root, ret);
4425 goto out;
4428 ret = copy_to_user(arg, sa, sizeof(*sa));
4429 if (ret)
4430 ret = -EFAULT;
4432 out:
4433 kfree(sa);
4434 up_write(&root->fs_info->subvol_sem);
4435 mnt_drop_write_file(file);
4436 return ret;
4439 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4441 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4442 size_t len;
4443 int ret;
4444 char label[BTRFS_LABEL_SIZE];
4446 spin_lock(&root->fs_info->super_lock);
4447 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4448 spin_unlock(&root->fs_info->super_lock);
4450 len = strnlen(label, BTRFS_LABEL_SIZE);
4452 if (len == BTRFS_LABEL_SIZE) {
4453 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4454 --len);
4457 ret = copy_to_user(arg, label, len);
4459 return ret ? -EFAULT : 0;
4462 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4464 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4465 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4466 struct btrfs_trans_handle *trans;
4467 char label[BTRFS_LABEL_SIZE];
4468 int ret;
4470 if (!capable(CAP_SYS_ADMIN))
4471 return -EPERM;
4473 if (copy_from_user(label, arg, sizeof(label)))
4474 return -EFAULT;
4476 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4477 pr_err("btrfs: unable to set label with more than %d bytes\n",
4478 BTRFS_LABEL_SIZE - 1);
4479 return -EINVAL;
4482 ret = mnt_want_write_file(file);
4483 if (ret)
4484 return ret;
4486 trans = btrfs_start_transaction(root, 0);
4487 if (IS_ERR(trans)) {
4488 ret = PTR_ERR(trans);
4489 goto out_unlock;
4492 spin_lock(&root->fs_info->super_lock);
4493 strcpy(super_block->label, label);
4494 spin_unlock(&root->fs_info->super_lock);
4495 ret = btrfs_end_transaction(trans, root);
4497 out_unlock:
4498 mnt_drop_write_file(file);
4499 return ret;
4502 long btrfs_ioctl(struct file *file, unsigned int
4503 cmd, unsigned long arg)
4505 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4506 void __user *argp = (void __user *)arg;
4508 switch (cmd) {
4509 case FS_IOC_GETFLAGS:
4510 return btrfs_ioctl_getflags(file, argp);
4511 case FS_IOC_SETFLAGS:
4512 return btrfs_ioctl_setflags(file, argp);
4513 case FS_IOC_GETVERSION:
4514 return btrfs_ioctl_getversion(file, argp);
4515 case FITRIM:
4516 return btrfs_ioctl_fitrim(file, argp);
4517 case BTRFS_IOC_SNAP_CREATE:
4518 return btrfs_ioctl_snap_create(file, argp, 0);
4519 case BTRFS_IOC_SNAP_CREATE_V2:
4520 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4521 case BTRFS_IOC_SUBVOL_CREATE:
4522 return btrfs_ioctl_snap_create(file, argp, 1);
4523 case BTRFS_IOC_SUBVOL_CREATE_V2:
4524 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4525 case BTRFS_IOC_SNAP_DESTROY:
4526 return btrfs_ioctl_snap_destroy(file, argp);
4527 case BTRFS_IOC_SUBVOL_GETFLAGS:
4528 return btrfs_ioctl_subvol_getflags(file, argp);
4529 case BTRFS_IOC_SUBVOL_SETFLAGS:
4530 return btrfs_ioctl_subvol_setflags(file, argp);
4531 case BTRFS_IOC_DEFAULT_SUBVOL:
4532 return btrfs_ioctl_default_subvol(file, argp);
4533 case BTRFS_IOC_DEFRAG:
4534 return btrfs_ioctl_defrag(file, NULL);
4535 case BTRFS_IOC_DEFRAG_RANGE:
4536 return btrfs_ioctl_defrag(file, argp);
4537 case BTRFS_IOC_RESIZE:
4538 return btrfs_ioctl_resize(file, argp);
4539 case BTRFS_IOC_ADD_DEV:
4540 return btrfs_ioctl_add_dev(root, argp);
4541 case BTRFS_IOC_RM_DEV:
4542 return btrfs_ioctl_rm_dev(file, argp);
4543 case BTRFS_IOC_FS_INFO:
4544 return btrfs_ioctl_fs_info(root, argp);
4545 case BTRFS_IOC_DEV_INFO:
4546 return btrfs_ioctl_dev_info(root, argp);
4547 case BTRFS_IOC_BALANCE:
4548 return btrfs_ioctl_balance(file, NULL);
4549 case BTRFS_IOC_CLONE:
4550 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4551 case BTRFS_IOC_CLONE_RANGE:
4552 return btrfs_ioctl_clone_range(file, argp);
4553 case BTRFS_IOC_TRANS_START:
4554 return btrfs_ioctl_trans_start(file);
4555 case BTRFS_IOC_TRANS_END:
4556 return btrfs_ioctl_trans_end(file);
4557 case BTRFS_IOC_TREE_SEARCH:
4558 return btrfs_ioctl_tree_search(file, argp);
4559 case BTRFS_IOC_INO_LOOKUP:
4560 return btrfs_ioctl_ino_lookup(file, argp);
4561 case BTRFS_IOC_INO_PATHS:
4562 return btrfs_ioctl_ino_to_path(root, argp);
4563 case BTRFS_IOC_LOGICAL_INO:
4564 return btrfs_ioctl_logical_to_ino(root, argp);
4565 case BTRFS_IOC_SPACE_INFO:
4566 return btrfs_ioctl_space_info(root, argp);
4567 case BTRFS_IOC_SYNC: {
4568 int ret;
4570 ret = btrfs_start_all_delalloc_inodes(root->fs_info, 0);
4571 if (ret)
4572 return ret;
4573 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4574 return ret;
4576 case BTRFS_IOC_START_SYNC:
4577 return btrfs_ioctl_start_sync(root, argp);
4578 case BTRFS_IOC_WAIT_SYNC:
4579 return btrfs_ioctl_wait_sync(root, argp);
4580 case BTRFS_IOC_SCRUB:
4581 return btrfs_ioctl_scrub(file, argp);
4582 case BTRFS_IOC_SCRUB_CANCEL:
4583 return btrfs_ioctl_scrub_cancel(root, argp);
4584 case BTRFS_IOC_SCRUB_PROGRESS:
4585 return btrfs_ioctl_scrub_progress(root, argp);
4586 case BTRFS_IOC_BALANCE_V2:
4587 return btrfs_ioctl_balance(file, argp);
4588 case BTRFS_IOC_BALANCE_CTL:
4589 return btrfs_ioctl_balance_ctl(root, arg);
4590 case BTRFS_IOC_BALANCE_PROGRESS:
4591 return btrfs_ioctl_balance_progress(root, argp);
4592 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4593 return btrfs_ioctl_set_received_subvol(file, argp);
4594 case BTRFS_IOC_SEND:
4595 return btrfs_ioctl_send(file, argp);
4596 case BTRFS_IOC_GET_DEV_STATS:
4597 return btrfs_ioctl_get_dev_stats(root, argp);
4598 case BTRFS_IOC_QUOTA_CTL:
4599 return btrfs_ioctl_quota_ctl(file, argp);
4600 case BTRFS_IOC_QGROUP_ASSIGN:
4601 return btrfs_ioctl_qgroup_assign(file, argp);
4602 case BTRFS_IOC_QGROUP_CREATE:
4603 return btrfs_ioctl_qgroup_create(file, argp);
4604 case BTRFS_IOC_QGROUP_LIMIT:
4605 return btrfs_ioctl_qgroup_limit(file, argp);
4606 case BTRFS_IOC_QUOTA_RESCAN:
4607 return btrfs_ioctl_quota_rescan(file, argp);
4608 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4609 return btrfs_ioctl_quota_rescan_status(file, argp);
4610 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4611 return btrfs_ioctl_quota_rescan_wait(file, argp);
4612 case BTRFS_IOC_DEV_REPLACE:
4613 return btrfs_ioctl_dev_replace(root, argp);
4614 case BTRFS_IOC_GET_FSLABEL:
4615 return btrfs_ioctl_get_fslabel(file, argp);
4616 case BTRFS_IOC_SET_FSLABEL:
4617 return btrfs_ioctl_set_fslabel(file, argp);
4618 case BTRFS_IOC_FILE_EXTENT_SAME:
4619 return btrfs_ioctl_file_extent_same(file, argp);
4622 return -ENOTTY;