Btrfs: Refactor btrfs_ioctl_snap_create()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / ioctl.c
blob02554e19d974bba3d8bed522554e07b92d92c4c2
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 "compat.h"
44 #include "ctree.h"
45 #include "disk-io.h"
46 #include "transaction.h"
47 #include "btrfs_inode.h"
48 #include "ioctl.h"
49 #include "print-tree.h"
50 #include "volumes.h"
51 #include "locking.h"
53 /* Mask out flags that are inappropriate for the given type of inode. */
54 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
56 if (S_ISDIR(mode))
57 return flags;
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
60 else
61 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
69 unsigned int iflags = 0;
71 if (flags & BTRFS_INODE_SYNC)
72 iflags |= FS_SYNC_FL;
73 if (flags & BTRFS_INODE_IMMUTABLE)
74 iflags |= FS_IMMUTABLE_FL;
75 if (flags & BTRFS_INODE_APPEND)
76 iflags |= FS_APPEND_FL;
77 if (flags & BTRFS_INODE_NODUMP)
78 iflags |= FS_NODUMP_FL;
79 if (flags & BTRFS_INODE_NOATIME)
80 iflags |= FS_NOATIME_FL;
81 if (flags & BTRFS_INODE_DIRSYNC)
82 iflags |= FS_DIRSYNC_FL;
84 return iflags;
88 * Update inode->i_flags based on the btrfs internal flags.
90 void btrfs_update_iflags(struct inode *inode)
92 struct btrfs_inode *ip = BTRFS_I(inode);
94 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
96 if (ip->flags & BTRFS_INODE_SYNC)
97 inode->i_flags |= S_SYNC;
98 if (ip->flags & BTRFS_INODE_IMMUTABLE)
99 inode->i_flags |= S_IMMUTABLE;
100 if (ip->flags & BTRFS_INODE_APPEND)
101 inode->i_flags |= S_APPEND;
102 if (ip->flags & BTRFS_INODE_NOATIME)
103 inode->i_flags |= S_NOATIME;
104 if (ip->flags & BTRFS_INODE_DIRSYNC)
105 inode->i_flags |= S_DIRSYNC;
109 * Inherit flags from the parent inode.
111 * Unlike extN we don't have any flags we don't want to inherit currently.
113 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
115 unsigned int flags;
117 if (!dir)
118 return;
120 flags = BTRFS_I(dir)->flags;
122 if (S_ISREG(inode->i_mode))
123 flags &= ~BTRFS_INODE_DIRSYNC;
124 else if (!S_ISDIR(inode->i_mode))
125 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
127 BTRFS_I(inode)->flags = flags;
128 btrfs_update_iflags(inode);
131 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
133 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
134 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
136 if (copy_to_user(arg, &flags, sizeof(flags)))
137 return -EFAULT;
138 return 0;
141 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
143 struct inode *inode = file->f_path.dentry->d_inode;
144 struct btrfs_inode *ip = BTRFS_I(inode);
145 struct btrfs_root *root = ip->root;
146 struct btrfs_trans_handle *trans;
147 unsigned int flags, oldflags;
148 int ret;
150 if (copy_from_user(&flags, arg, sizeof(flags)))
151 return -EFAULT;
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL))
156 return -EOPNOTSUPP;
158 if (!is_owner_or_cap(inode))
159 return -EACCES;
161 mutex_lock(&inode->i_mutex);
163 flags = btrfs_mask_flags(inode->i_mode, flags);
164 oldflags = btrfs_flags_to_ioctl(ip->flags);
165 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
166 if (!capable(CAP_LINUX_IMMUTABLE)) {
167 ret = -EPERM;
168 goto out_unlock;
172 ret = mnt_want_write(file->f_path.mnt);
173 if (ret)
174 goto out_unlock;
176 if (flags & FS_SYNC_FL)
177 ip->flags |= BTRFS_INODE_SYNC;
178 else
179 ip->flags &= ~BTRFS_INODE_SYNC;
180 if (flags & FS_IMMUTABLE_FL)
181 ip->flags |= BTRFS_INODE_IMMUTABLE;
182 else
183 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
184 if (flags & FS_APPEND_FL)
185 ip->flags |= BTRFS_INODE_APPEND;
186 else
187 ip->flags &= ~BTRFS_INODE_APPEND;
188 if (flags & FS_NODUMP_FL)
189 ip->flags |= BTRFS_INODE_NODUMP;
190 else
191 ip->flags &= ~BTRFS_INODE_NODUMP;
192 if (flags & FS_NOATIME_FL)
193 ip->flags |= BTRFS_INODE_NOATIME;
194 else
195 ip->flags &= ~BTRFS_INODE_NOATIME;
196 if (flags & FS_DIRSYNC_FL)
197 ip->flags |= BTRFS_INODE_DIRSYNC;
198 else
199 ip->flags &= ~BTRFS_INODE_DIRSYNC;
202 trans = btrfs_join_transaction(root, 1);
203 BUG_ON(!trans);
205 ret = btrfs_update_inode(trans, root, inode);
206 BUG_ON(ret);
208 btrfs_update_iflags(inode);
209 inode->i_ctime = CURRENT_TIME;
210 btrfs_end_transaction(trans, root);
212 mnt_drop_write(file->f_path.mnt);
213 out_unlock:
214 mutex_unlock(&inode->i_mutex);
215 return 0;
218 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
220 struct inode *inode = file->f_path.dentry->d_inode;
222 return put_user(inode->i_generation, arg);
225 static noinline int create_subvol(struct btrfs_root *root,
226 struct dentry *dentry,
227 char *name, int namelen,
228 u64 *async_transid)
230 struct btrfs_trans_handle *trans;
231 struct btrfs_key key;
232 struct btrfs_root_item root_item;
233 struct btrfs_inode_item *inode_item;
234 struct extent_buffer *leaf;
235 struct btrfs_root *new_root;
236 struct dentry *parent = dget_parent(dentry);
237 struct inode *dir;
238 int ret;
239 int err;
240 u64 objectid;
241 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
242 u64 index = 0;
244 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
245 0, &objectid);
246 if (ret) {
247 dput(parent);
248 return ret;
251 dir = parent->d_inode;
254 * 1 - inode item
255 * 2 - refs
256 * 1 - root item
257 * 2 - dir items
259 trans = btrfs_start_transaction(root, 6);
260 if (IS_ERR(trans)) {
261 dput(parent);
262 return PTR_ERR(trans);
265 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
266 0, objectid, NULL, 0, 0, 0);
267 if (IS_ERR(leaf)) {
268 ret = PTR_ERR(leaf);
269 goto fail;
272 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
273 btrfs_set_header_bytenr(leaf, leaf->start);
274 btrfs_set_header_generation(leaf, trans->transid);
275 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
276 btrfs_set_header_owner(leaf, objectid);
278 write_extent_buffer(leaf, root->fs_info->fsid,
279 (unsigned long)btrfs_header_fsid(leaf),
280 BTRFS_FSID_SIZE);
281 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
282 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
283 BTRFS_UUID_SIZE);
284 btrfs_mark_buffer_dirty(leaf);
286 inode_item = &root_item.inode;
287 memset(inode_item, 0, sizeof(*inode_item));
288 inode_item->generation = cpu_to_le64(1);
289 inode_item->size = cpu_to_le64(3);
290 inode_item->nlink = cpu_to_le32(1);
291 inode_item->nbytes = cpu_to_le64(root->leafsize);
292 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
294 btrfs_set_root_bytenr(&root_item, leaf->start);
295 btrfs_set_root_generation(&root_item, trans->transid);
296 btrfs_set_root_level(&root_item, 0);
297 btrfs_set_root_refs(&root_item, 1);
298 btrfs_set_root_used(&root_item, leaf->len);
299 btrfs_set_root_last_snapshot(&root_item, 0);
301 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
302 root_item.drop_level = 0;
304 btrfs_tree_unlock(leaf);
305 free_extent_buffer(leaf);
306 leaf = NULL;
308 btrfs_set_root_dirid(&root_item, new_dirid);
310 key.objectid = objectid;
311 key.offset = 0;
312 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
313 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
314 &root_item);
315 if (ret)
316 goto fail;
318 key.offset = (u64)-1;
319 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
320 BUG_ON(IS_ERR(new_root));
322 btrfs_record_root_in_trans(trans, new_root);
324 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
325 BTRFS_I(dir)->block_group);
327 * insert the directory item
329 ret = btrfs_set_inode_index(dir, &index);
330 BUG_ON(ret);
332 ret = btrfs_insert_dir_item(trans, root,
333 name, namelen, dir->i_ino, &key,
334 BTRFS_FT_DIR, index);
335 if (ret)
336 goto fail;
338 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
339 ret = btrfs_update_inode(trans, root, dir);
340 BUG_ON(ret);
342 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
343 objectid, root->root_key.objectid,
344 dir->i_ino, index, name, namelen);
346 BUG_ON(ret);
348 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
349 fail:
350 dput(parent);
351 if (async_transid) {
352 *async_transid = trans->transid;
353 err = btrfs_commit_transaction_async(trans, root, 1);
354 } else {
355 err = btrfs_commit_transaction(trans, root);
357 if (err && !ret)
358 ret = err;
359 return ret;
362 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
363 char *name, int namelen, u64 *async_transid)
365 struct inode *inode;
366 struct dentry *parent;
367 struct btrfs_pending_snapshot *pending_snapshot;
368 struct btrfs_trans_handle *trans;
369 int ret;
371 if (!root->ref_cows)
372 return -EINVAL;
374 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
375 if (!pending_snapshot)
376 return -ENOMEM;
378 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
379 pending_snapshot->dentry = dentry;
380 pending_snapshot->root = root;
382 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
383 if (IS_ERR(trans)) {
384 ret = PTR_ERR(trans);
385 goto fail;
388 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
389 BUG_ON(ret);
391 list_add(&pending_snapshot->list,
392 &trans->transaction->pending_snapshots);
393 if (async_transid) {
394 *async_transid = trans->transid;
395 ret = btrfs_commit_transaction_async(trans,
396 root->fs_info->extent_root, 1);
397 } else {
398 ret = btrfs_commit_transaction(trans,
399 root->fs_info->extent_root);
401 BUG_ON(ret);
403 ret = pending_snapshot->error;
404 if (ret)
405 goto fail;
407 btrfs_orphan_cleanup(pending_snapshot->snap);
409 parent = dget_parent(dentry);
410 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
411 dput(parent);
412 if (IS_ERR(inode)) {
413 ret = PTR_ERR(inode);
414 goto fail;
416 BUG_ON(!inode);
417 d_instantiate(dentry, inode);
418 ret = 0;
419 fail:
420 kfree(pending_snapshot);
421 return ret;
424 /* copy of check_sticky in fs/namei.c()
425 * It's inline, so penalty for filesystems that don't use sticky bit is
426 * minimal.
428 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
430 uid_t fsuid = current_fsuid();
432 if (!(dir->i_mode & S_ISVTX))
433 return 0;
434 if (inode->i_uid == fsuid)
435 return 0;
436 if (dir->i_uid == fsuid)
437 return 0;
438 return !capable(CAP_FOWNER);
441 /* copy of may_delete in fs/namei.c()
442 * Check whether we can remove a link victim from directory dir, check
443 * whether the type of victim is right.
444 * 1. We can't do it if dir is read-only (done in permission())
445 * 2. We should have write and exec permissions on dir
446 * 3. We can't remove anything from append-only dir
447 * 4. We can't do anything with immutable dir (done in permission())
448 * 5. If the sticky bit on dir is set we should either
449 * a. be owner of dir, or
450 * b. be owner of victim, or
451 * c. have CAP_FOWNER capability
452 * 6. If the victim is append-only or immutable we can't do antyhing with
453 * links pointing to it.
454 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
455 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
456 * 9. We can't remove a root or mountpoint.
457 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
458 * nfs_async_unlink().
461 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
463 int error;
465 if (!victim->d_inode)
466 return -ENOENT;
468 BUG_ON(victim->d_parent->d_inode != dir);
469 audit_inode_child(victim, dir);
471 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
472 if (error)
473 return error;
474 if (IS_APPEND(dir))
475 return -EPERM;
476 if (btrfs_check_sticky(dir, victim->d_inode)||
477 IS_APPEND(victim->d_inode)||
478 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
479 return -EPERM;
480 if (isdir) {
481 if (!S_ISDIR(victim->d_inode->i_mode))
482 return -ENOTDIR;
483 if (IS_ROOT(victim))
484 return -EBUSY;
485 } else if (S_ISDIR(victim->d_inode->i_mode))
486 return -EISDIR;
487 if (IS_DEADDIR(dir))
488 return -ENOENT;
489 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
490 return -EBUSY;
491 return 0;
494 /* copy of may_create in fs/namei.c() */
495 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
497 if (child->d_inode)
498 return -EEXIST;
499 if (IS_DEADDIR(dir))
500 return -ENOENT;
501 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
505 * Create a new subvolume below @parent. This is largely modeled after
506 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
507 * inside this filesystem so it's quite a bit simpler.
509 static noinline int btrfs_mksubvol(struct path *parent,
510 char *name, int namelen,
511 struct btrfs_root *snap_src,
512 u64 *async_transid)
514 struct inode *dir = parent->dentry->d_inode;
515 struct dentry *dentry;
516 int error;
518 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
520 dentry = lookup_one_len(name, parent->dentry, namelen);
521 error = PTR_ERR(dentry);
522 if (IS_ERR(dentry))
523 goto out_unlock;
525 error = -EEXIST;
526 if (dentry->d_inode)
527 goto out_dput;
529 error = mnt_want_write(parent->mnt);
530 if (error)
531 goto out_dput;
533 error = btrfs_may_create(dir, dentry);
534 if (error)
535 goto out_drop_write;
537 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
539 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
540 goto out_up_read;
542 if (snap_src) {
543 error = create_snapshot(snap_src, dentry,
544 name, namelen, async_transid);
545 } else {
546 error = create_subvol(BTRFS_I(dir)->root, dentry,
547 name, namelen, async_transid);
549 if (!error)
550 fsnotify_mkdir(dir, dentry);
551 out_up_read:
552 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
553 out_drop_write:
554 mnt_drop_write(parent->mnt);
555 out_dput:
556 dput(dentry);
557 out_unlock:
558 mutex_unlock(&dir->i_mutex);
559 return error;
562 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
563 int thresh, u64 *last_len, u64 *skip,
564 u64 *defrag_end)
566 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
567 struct extent_map *em = NULL;
568 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
569 int ret = 1;
572 if (thresh == 0)
573 thresh = 256 * 1024;
576 * make sure that once we start defragging and extent, we keep on
577 * defragging it
579 if (start < *defrag_end)
580 return 1;
582 *skip = 0;
585 * hopefully we have this extent in the tree already, try without
586 * the full extent lock
588 read_lock(&em_tree->lock);
589 em = lookup_extent_mapping(em_tree, start, len);
590 read_unlock(&em_tree->lock);
592 if (!em) {
593 /* get the big lock and read metadata off disk */
594 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
595 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
596 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
598 if (IS_ERR(em))
599 return 0;
602 /* this will cover holes, and inline extents */
603 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
604 ret = 0;
607 * we hit a real extent, if it is big don't bother defragging it again
609 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
610 ret = 0;
613 * last_len ends up being a counter of how many bytes we've defragged.
614 * every time we choose not to defrag an extent, we reset *last_len
615 * so that the next tiny extent will force a defrag.
617 * The end result of this is that tiny extents before a single big
618 * extent will force at least part of that big extent to be defragged.
620 if (ret) {
621 *last_len += len;
622 *defrag_end = extent_map_end(em);
623 } else {
624 *last_len = 0;
625 *skip = extent_map_end(em);
626 *defrag_end = 0;
629 free_extent_map(em);
630 return ret;
633 static int btrfs_defrag_file(struct file *file,
634 struct btrfs_ioctl_defrag_range_args *range)
636 struct inode *inode = fdentry(file)->d_inode;
637 struct btrfs_root *root = BTRFS_I(inode)->root;
638 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
639 struct btrfs_ordered_extent *ordered;
640 struct page *page;
641 unsigned long last_index;
642 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
643 unsigned long total_read = 0;
644 u64 page_start;
645 u64 page_end;
646 u64 last_len = 0;
647 u64 skip = 0;
648 u64 defrag_end = 0;
649 unsigned long i;
650 int ret;
652 if (inode->i_size == 0)
653 return 0;
655 if (range->start + range->len > range->start) {
656 last_index = min_t(u64, inode->i_size - 1,
657 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
658 } else {
659 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
662 i = range->start >> PAGE_CACHE_SHIFT;
663 while (i <= last_index) {
664 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
665 PAGE_CACHE_SIZE,
666 range->extent_thresh,
667 &last_len, &skip,
668 &defrag_end)) {
669 unsigned long next;
671 * the should_defrag function tells us how much to skip
672 * bump our counter by the suggested amount
674 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
675 i = max(i + 1, next);
676 continue;
679 if (total_read % ra_pages == 0) {
680 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
681 min(last_index, i + ra_pages - 1));
683 total_read++;
684 mutex_lock(&inode->i_mutex);
685 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
686 BTRFS_I(inode)->force_compress = 1;
688 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
689 if (ret)
690 goto err_unlock;
691 again:
692 if (inode->i_size == 0 ||
693 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
694 ret = 0;
695 goto err_reservations;
698 page = grab_cache_page(inode->i_mapping, i);
699 if (!page) {
700 ret = -ENOMEM;
701 goto err_reservations;
704 if (!PageUptodate(page)) {
705 btrfs_readpage(NULL, page);
706 lock_page(page);
707 if (!PageUptodate(page)) {
708 unlock_page(page);
709 page_cache_release(page);
710 ret = -EIO;
711 goto err_reservations;
715 if (page->mapping != inode->i_mapping) {
716 unlock_page(page);
717 page_cache_release(page);
718 goto again;
721 wait_on_page_writeback(page);
723 if (PageDirty(page)) {
724 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
725 goto loop_unlock;
728 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
729 page_end = page_start + PAGE_CACHE_SIZE - 1;
730 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
732 ordered = btrfs_lookup_ordered_extent(inode, page_start);
733 if (ordered) {
734 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
735 unlock_page(page);
736 page_cache_release(page);
737 btrfs_start_ordered_extent(inode, ordered, 1);
738 btrfs_put_ordered_extent(ordered);
739 goto again;
741 set_page_extent_mapped(page);
744 * this makes sure page_mkwrite is called on the
745 * page if it is dirtied again later
747 clear_page_dirty_for_io(page);
748 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
749 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
750 EXTENT_DO_ACCOUNTING, GFP_NOFS);
752 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
753 ClearPageChecked(page);
754 set_page_dirty(page);
755 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
757 loop_unlock:
758 unlock_page(page);
759 page_cache_release(page);
760 mutex_unlock(&inode->i_mutex);
762 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
763 i++;
766 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
767 filemap_flush(inode->i_mapping);
769 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
770 /* the filemap_flush will queue IO into the worker threads, but
771 * we have to make sure the IO is actually started and that
772 * ordered extents get created before we return
774 atomic_inc(&root->fs_info->async_submit_draining);
775 while (atomic_read(&root->fs_info->nr_async_submits) ||
776 atomic_read(&root->fs_info->async_delalloc_pages)) {
777 wait_event(root->fs_info->async_submit_wait,
778 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
779 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
781 atomic_dec(&root->fs_info->async_submit_draining);
783 mutex_lock(&inode->i_mutex);
784 BTRFS_I(inode)->force_compress = 0;
785 mutex_unlock(&inode->i_mutex);
788 return 0;
790 err_reservations:
791 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
792 err_unlock:
793 mutex_unlock(&inode->i_mutex);
794 return ret;
797 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
798 void __user *arg)
800 u64 new_size;
801 u64 old_size;
802 u64 devid = 1;
803 struct btrfs_ioctl_vol_args *vol_args;
804 struct btrfs_trans_handle *trans;
805 struct btrfs_device *device = NULL;
806 char *sizestr;
807 char *devstr = NULL;
808 int ret = 0;
809 int mod = 0;
811 if (root->fs_info->sb->s_flags & MS_RDONLY)
812 return -EROFS;
814 if (!capable(CAP_SYS_ADMIN))
815 return -EPERM;
817 vol_args = memdup_user(arg, sizeof(*vol_args));
818 if (IS_ERR(vol_args))
819 return PTR_ERR(vol_args);
821 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
823 mutex_lock(&root->fs_info->volume_mutex);
824 sizestr = vol_args->name;
825 devstr = strchr(sizestr, ':');
826 if (devstr) {
827 char *end;
828 sizestr = devstr + 1;
829 *devstr = '\0';
830 devstr = vol_args->name;
831 devid = simple_strtoull(devstr, &end, 10);
832 printk(KERN_INFO "resizing devid %llu\n",
833 (unsigned long long)devid);
835 device = btrfs_find_device(root, devid, NULL, NULL);
836 if (!device) {
837 printk(KERN_INFO "resizer unable to find device %llu\n",
838 (unsigned long long)devid);
839 ret = -EINVAL;
840 goto out_unlock;
842 if (!strcmp(sizestr, "max"))
843 new_size = device->bdev->bd_inode->i_size;
844 else {
845 if (sizestr[0] == '-') {
846 mod = -1;
847 sizestr++;
848 } else if (sizestr[0] == '+') {
849 mod = 1;
850 sizestr++;
852 new_size = memparse(sizestr, NULL);
853 if (new_size == 0) {
854 ret = -EINVAL;
855 goto out_unlock;
859 old_size = device->total_bytes;
861 if (mod < 0) {
862 if (new_size > old_size) {
863 ret = -EINVAL;
864 goto out_unlock;
866 new_size = old_size - new_size;
867 } else if (mod > 0) {
868 new_size = old_size + new_size;
871 if (new_size < 256 * 1024 * 1024) {
872 ret = -EINVAL;
873 goto out_unlock;
875 if (new_size > device->bdev->bd_inode->i_size) {
876 ret = -EFBIG;
877 goto out_unlock;
880 do_div(new_size, root->sectorsize);
881 new_size *= root->sectorsize;
883 printk(KERN_INFO "new size for %s is %llu\n",
884 device->name, (unsigned long long)new_size);
886 if (new_size > old_size) {
887 trans = btrfs_start_transaction(root, 0);
888 ret = btrfs_grow_device(trans, device, new_size);
889 btrfs_commit_transaction(trans, root);
890 } else {
891 ret = btrfs_shrink_device(device, new_size);
894 out_unlock:
895 mutex_unlock(&root->fs_info->volume_mutex);
896 kfree(vol_args);
897 return ret;
900 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
901 char *name,
902 unsigned long fd,
903 int subvol,
904 u64 *transid)
906 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
907 struct file *src_file;
908 int namelen;
909 int ret = 0;
911 if (root->fs_info->sb->s_flags & MS_RDONLY)
912 return -EROFS;
914 namelen = strlen(name);
915 if (strchr(name, '/')) {
916 ret = -EINVAL;
917 goto out;
920 if (subvol) {
921 ret = btrfs_mksubvol(&file->f_path, name, namelen,
922 NULL, transid);
923 } else {
924 struct inode *src_inode;
925 src_file = fget(fd);
926 if (!src_file) {
927 ret = -EINVAL;
928 goto out;
931 src_inode = src_file->f_path.dentry->d_inode;
932 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
933 printk(KERN_INFO "btrfs: Snapshot src from "
934 "another FS\n");
935 ret = -EINVAL;
936 fput(src_file);
937 goto out;
939 ret = btrfs_mksubvol(&file->f_path, name, namelen,
940 BTRFS_I(src_inode)->root,
941 transid);
942 fput(src_file);
944 out:
945 return ret;
948 static noinline int btrfs_ioctl_snap_create(struct file *file,
949 void __user *arg, int subvol)
951 struct btrfs_ioctl_vol_args *vol_args;
952 int ret;
954 vol_args = memdup_user(arg, sizeof(*vol_args));
955 if (IS_ERR(vol_args))
956 return PTR_ERR(vol_args);
957 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
959 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
960 vol_args->fd, subvol, NULL);
962 kfree(vol_args);
963 return ret;
966 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
967 void __user *arg, int subvol)
969 struct btrfs_ioctl_vol_args_v2 *vol_args;
970 int ret;
971 u64 transid = 0;
972 u64 *ptr = NULL;
974 vol_args = memdup_user(arg, sizeof(*vol_args));
975 if (IS_ERR(vol_args))
976 return PTR_ERR(vol_args);
977 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
979 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ASYNC) {
980 ret = -EINVAL;
981 goto out;
984 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
985 ptr = &transid;
987 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
988 vol_args->fd, subvol, ptr);
990 if (ret == 0 && ptr &&
991 copy_to_user(arg +
992 offsetof(struct btrfs_ioctl_vol_args_v2,
993 transid), ptr, sizeof(*ptr)))
994 ret = -EFAULT;
995 out:
996 kfree(vol_args);
997 return ret;
1001 * helper to check if the subvolume references other subvolumes
1003 static noinline int may_destroy_subvol(struct btrfs_root *root)
1005 struct btrfs_path *path;
1006 struct btrfs_key key;
1007 int ret;
1009 path = btrfs_alloc_path();
1010 if (!path)
1011 return -ENOMEM;
1013 key.objectid = root->root_key.objectid;
1014 key.type = BTRFS_ROOT_REF_KEY;
1015 key.offset = (u64)-1;
1017 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1018 &key, path, 0, 0);
1019 if (ret < 0)
1020 goto out;
1021 BUG_ON(ret == 0);
1023 ret = 0;
1024 if (path->slots[0] > 0) {
1025 path->slots[0]--;
1026 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1027 if (key.objectid == root->root_key.objectid &&
1028 key.type == BTRFS_ROOT_REF_KEY)
1029 ret = -ENOTEMPTY;
1031 out:
1032 btrfs_free_path(path);
1033 return ret;
1036 static noinline int key_in_sk(struct btrfs_key *key,
1037 struct btrfs_ioctl_search_key *sk)
1039 struct btrfs_key test;
1040 int ret;
1042 test.objectid = sk->min_objectid;
1043 test.type = sk->min_type;
1044 test.offset = sk->min_offset;
1046 ret = btrfs_comp_cpu_keys(key, &test);
1047 if (ret < 0)
1048 return 0;
1050 test.objectid = sk->max_objectid;
1051 test.type = sk->max_type;
1052 test.offset = sk->max_offset;
1054 ret = btrfs_comp_cpu_keys(key, &test);
1055 if (ret > 0)
1056 return 0;
1057 return 1;
1060 static noinline int copy_to_sk(struct btrfs_root *root,
1061 struct btrfs_path *path,
1062 struct btrfs_key *key,
1063 struct btrfs_ioctl_search_key *sk,
1064 char *buf,
1065 unsigned long *sk_offset,
1066 int *num_found)
1068 u64 found_transid;
1069 struct extent_buffer *leaf;
1070 struct btrfs_ioctl_search_header sh;
1071 unsigned long item_off;
1072 unsigned long item_len;
1073 int nritems;
1074 int i;
1075 int slot;
1076 int found = 0;
1077 int ret = 0;
1079 leaf = path->nodes[0];
1080 slot = path->slots[0];
1081 nritems = btrfs_header_nritems(leaf);
1083 if (btrfs_header_generation(leaf) > sk->max_transid) {
1084 i = nritems;
1085 goto advance_key;
1087 found_transid = btrfs_header_generation(leaf);
1089 for (i = slot; i < nritems; i++) {
1090 item_off = btrfs_item_ptr_offset(leaf, i);
1091 item_len = btrfs_item_size_nr(leaf, i);
1093 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1094 item_len = 0;
1096 if (sizeof(sh) + item_len + *sk_offset >
1097 BTRFS_SEARCH_ARGS_BUFSIZE) {
1098 ret = 1;
1099 goto overflow;
1102 btrfs_item_key_to_cpu(leaf, key, i);
1103 if (!key_in_sk(key, sk))
1104 continue;
1106 sh.objectid = key->objectid;
1107 sh.offset = key->offset;
1108 sh.type = key->type;
1109 sh.len = item_len;
1110 sh.transid = found_transid;
1112 /* copy search result header */
1113 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1114 *sk_offset += sizeof(sh);
1116 if (item_len) {
1117 char *p = buf + *sk_offset;
1118 /* copy the item */
1119 read_extent_buffer(leaf, p,
1120 item_off, item_len);
1121 *sk_offset += item_len;
1123 found++;
1125 if (*num_found >= sk->nr_items)
1126 break;
1128 advance_key:
1129 ret = 0;
1130 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1131 key->offset++;
1132 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1133 key->offset = 0;
1134 key->type++;
1135 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1136 key->offset = 0;
1137 key->type = 0;
1138 key->objectid++;
1139 } else
1140 ret = 1;
1141 overflow:
1142 *num_found += found;
1143 return ret;
1146 static noinline int search_ioctl(struct inode *inode,
1147 struct btrfs_ioctl_search_args *args)
1149 struct btrfs_root *root;
1150 struct btrfs_key key;
1151 struct btrfs_key max_key;
1152 struct btrfs_path *path;
1153 struct btrfs_ioctl_search_key *sk = &args->key;
1154 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1155 int ret;
1156 int num_found = 0;
1157 unsigned long sk_offset = 0;
1159 path = btrfs_alloc_path();
1160 if (!path)
1161 return -ENOMEM;
1163 if (sk->tree_id == 0) {
1164 /* search the root of the inode that was passed */
1165 root = BTRFS_I(inode)->root;
1166 } else {
1167 key.objectid = sk->tree_id;
1168 key.type = BTRFS_ROOT_ITEM_KEY;
1169 key.offset = (u64)-1;
1170 root = btrfs_read_fs_root_no_name(info, &key);
1171 if (IS_ERR(root)) {
1172 printk(KERN_ERR "could not find root %llu\n",
1173 sk->tree_id);
1174 btrfs_free_path(path);
1175 return -ENOENT;
1179 key.objectid = sk->min_objectid;
1180 key.type = sk->min_type;
1181 key.offset = sk->min_offset;
1183 max_key.objectid = sk->max_objectid;
1184 max_key.type = sk->max_type;
1185 max_key.offset = sk->max_offset;
1187 path->keep_locks = 1;
1189 while(1) {
1190 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1191 sk->min_transid);
1192 if (ret != 0) {
1193 if (ret > 0)
1194 ret = 0;
1195 goto err;
1197 ret = copy_to_sk(root, path, &key, sk, args->buf,
1198 &sk_offset, &num_found);
1199 btrfs_release_path(root, path);
1200 if (ret || num_found >= sk->nr_items)
1201 break;
1204 ret = 0;
1205 err:
1206 sk->nr_items = num_found;
1207 btrfs_free_path(path);
1208 return ret;
1211 static noinline int btrfs_ioctl_tree_search(struct file *file,
1212 void __user *argp)
1214 struct btrfs_ioctl_search_args *args;
1215 struct inode *inode;
1216 int ret;
1218 if (!capable(CAP_SYS_ADMIN))
1219 return -EPERM;
1221 args = memdup_user(argp, sizeof(*args));
1222 if (IS_ERR(args))
1223 return PTR_ERR(args);
1225 inode = fdentry(file)->d_inode;
1226 ret = search_ioctl(inode, args);
1227 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1228 ret = -EFAULT;
1229 kfree(args);
1230 return ret;
1234 * Search INODE_REFs to identify path name of 'dirid' directory
1235 * in a 'tree_id' tree. and sets path name to 'name'.
1237 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1238 u64 tree_id, u64 dirid, char *name)
1240 struct btrfs_root *root;
1241 struct btrfs_key key;
1242 char *ptr;
1243 int ret = -1;
1244 int slot;
1245 int len;
1246 int total_len = 0;
1247 struct btrfs_inode_ref *iref;
1248 struct extent_buffer *l;
1249 struct btrfs_path *path;
1251 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1252 name[0]='\0';
1253 return 0;
1256 path = btrfs_alloc_path();
1257 if (!path)
1258 return -ENOMEM;
1260 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1262 key.objectid = tree_id;
1263 key.type = BTRFS_ROOT_ITEM_KEY;
1264 key.offset = (u64)-1;
1265 root = btrfs_read_fs_root_no_name(info, &key);
1266 if (IS_ERR(root)) {
1267 printk(KERN_ERR "could not find root %llu\n", tree_id);
1268 ret = -ENOENT;
1269 goto out;
1272 key.objectid = dirid;
1273 key.type = BTRFS_INODE_REF_KEY;
1274 key.offset = (u64)-1;
1276 while(1) {
1277 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1278 if (ret < 0)
1279 goto out;
1281 l = path->nodes[0];
1282 slot = path->slots[0];
1283 if (ret > 0 && slot > 0)
1284 slot--;
1285 btrfs_item_key_to_cpu(l, &key, slot);
1287 if (ret > 0 && (key.objectid != dirid ||
1288 key.type != BTRFS_INODE_REF_KEY)) {
1289 ret = -ENOENT;
1290 goto out;
1293 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1294 len = btrfs_inode_ref_name_len(l, iref);
1295 ptr -= len + 1;
1296 total_len += len + 1;
1297 if (ptr < name)
1298 goto out;
1300 *(ptr + len) = '/';
1301 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1303 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1304 break;
1306 btrfs_release_path(root, path);
1307 key.objectid = key.offset;
1308 key.offset = (u64)-1;
1309 dirid = key.objectid;
1312 if (ptr < name)
1313 goto out;
1314 memcpy(name, ptr, total_len);
1315 name[total_len]='\0';
1316 ret = 0;
1317 out:
1318 btrfs_free_path(path);
1319 return ret;
1322 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1323 void __user *argp)
1325 struct btrfs_ioctl_ino_lookup_args *args;
1326 struct inode *inode;
1327 int ret;
1329 if (!capable(CAP_SYS_ADMIN))
1330 return -EPERM;
1332 args = memdup_user(argp, sizeof(*args));
1333 if (IS_ERR(args))
1334 return PTR_ERR(args);
1336 inode = fdentry(file)->d_inode;
1338 if (args->treeid == 0)
1339 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1341 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1342 args->treeid, args->objectid,
1343 args->name);
1345 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1346 ret = -EFAULT;
1348 kfree(args);
1349 return ret;
1352 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1353 void __user *arg)
1355 struct dentry *parent = fdentry(file);
1356 struct dentry *dentry;
1357 struct inode *dir = parent->d_inode;
1358 struct inode *inode;
1359 struct btrfs_root *root = BTRFS_I(dir)->root;
1360 struct btrfs_root *dest = NULL;
1361 struct btrfs_ioctl_vol_args *vol_args;
1362 struct btrfs_trans_handle *trans;
1363 int namelen;
1364 int ret;
1365 int err = 0;
1367 vol_args = memdup_user(arg, sizeof(*vol_args));
1368 if (IS_ERR(vol_args))
1369 return PTR_ERR(vol_args);
1371 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1372 namelen = strlen(vol_args->name);
1373 if (strchr(vol_args->name, '/') ||
1374 strncmp(vol_args->name, "..", namelen) == 0) {
1375 err = -EINVAL;
1376 goto out;
1379 err = mnt_want_write(file->f_path.mnt);
1380 if (err)
1381 goto out;
1383 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1384 dentry = lookup_one_len(vol_args->name, parent, namelen);
1385 if (IS_ERR(dentry)) {
1386 err = PTR_ERR(dentry);
1387 goto out_unlock_dir;
1390 if (!dentry->d_inode) {
1391 err = -ENOENT;
1392 goto out_dput;
1395 inode = dentry->d_inode;
1396 dest = BTRFS_I(inode)->root;
1397 if (!capable(CAP_SYS_ADMIN)){
1399 * Regular user. Only allow this with a special mount
1400 * option, when the user has write+exec access to the
1401 * subvol root, and when rmdir(2) would have been
1402 * allowed.
1404 * Note that this is _not_ check that the subvol is
1405 * empty or doesn't contain data that we wouldn't
1406 * otherwise be able to delete.
1408 * Users who want to delete empty subvols should try
1409 * rmdir(2).
1411 err = -EPERM;
1412 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1413 goto out_dput;
1416 * Do not allow deletion if the parent dir is the same
1417 * as the dir to be deleted. That means the ioctl
1418 * must be called on the dentry referencing the root
1419 * of the subvol, not a random directory contained
1420 * within it.
1422 err = -EINVAL;
1423 if (root == dest)
1424 goto out_dput;
1426 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1427 if (err)
1428 goto out_dput;
1430 /* check if subvolume may be deleted by a non-root user */
1431 err = btrfs_may_delete(dir, dentry, 1);
1432 if (err)
1433 goto out_dput;
1436 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1437 err = -EINVAL;
1438 goto out_dput;
1441 mutex_lock(&inode->i_mutex);
1442 err = d_invalidate(dentry);
1443 if (err)
1444 goto out_unlock;
1446 down_write(&root->fs_info->subvol_sem);
1448 err = may_destroy_subvol(dest);
1449 if (err)
1450 goto out_up_write;
1452 trans = btrfs_start_transaction(root, 0);
1453 if (IS_ERR(trans)) {
1454 err = PTR_ERR(trans);
1455 goto out_up_write;
1457 trans->block_rsv = &root->fs_info->global_block_rsv;
1459 ret = btrfs_unlink_subvol(trans, root, dir,
1460 dest->root_key.objectid,
1461 dentry->d_name.name,
1462 dentry->d_name.len);
1463 BUG_ON(ret);
1465 btrfs_record_root_in_trans(trans, dest);
1467 memset(&dest->root_item.drop_progress, 0,
1468 sizeof(dest->root_item.drop_progress));
1469 dest->root_item.drop_level = 0;
1470 btrfs_set_root_refs(&dest->root_item, 0);
1472 if (!xchg(&dest->orphan_item_inserted, 1)) {
1473 ret = btrfs_insert_orphan_item(trans,
1474 root->fs_info->tree_root,
1475 dest->root_key.objectid);
1476 BUG_ON(ret);
1479 ret = btrfs_end_transaction(trans, root);
1480 BUG_ON(ret);
1481 inode->i_flags |= S_DEAD;
1482 out_up_write:
1483 up_write(&root->fs_info->subvol_sem);
1484 out_unlock:
1485 mutex_unlock(&inode->i_mutex);
1486 if (!err) {
1487 shrink_dcache_sb(root->fs_info->sb);
1488 btrfs_invalidate_inodes(dest);
1489 d_delete(dentry);
1491 out_dput:
1492 dput(dentry);
1493 out_unlock_dir:
1494 mutex_unlock(&dir->i_mutex);
1495 mnt_drop_write(file->f_path.mnt);
1496 out:
1497 kfree(vol_args);
1498 return err;
1501 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1503 struct inode *inode = fdentry(file)->d_inode;
1504 struct btrfs_root *root = BTRFS_I(inode)->root;
1505 struct btrfs_ioctl_defrag_range_args *range;
1506 int ret;
1508 ret = mnt_want_write(file->f_path.mnt);
1509 if (ret)
1510 return ret;
1512 switch (inode->i_mode & S_IFMT) {
1513 case S_IFDIR:
1514 if (!capable(CAP_SYS_ADMIN)) {
1515 ret = -EPERM;
1516 goto out;
1518 ret = btrfs_defrag_root(root, 0);
1519 if (ret)
1520 goto out;
1521 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1522 break;
1523 case S_IFREG:
1524 if (!(file->f_mode & FMODE_WRITE)) {
1525 ret = -EINVAL;
1526 goto out;
1529 range = kzalloc(sizeof(*range), GFP_KERNEL);
1530 if (!range) {
1531 ret = -ENOMEM;
1532 goto out;
1535 if (argp) {
1536 if (copy_from_user(range, argp,
1537 sizeof(*range))) {
1538 ret = -EFAULT;
1539 kfree(range);
1540 goto out;
1542 /* compression requires us to start the IO */
1543 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1544 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1545 range->extent_thresh = (u32)-1;
1547 } else {
1548 /* the rest are all set to zero by kzalloc */
1549 range->len = (u64)-1;
1551 ret = btrfs_defrag_file(file, range);
1552 kfree(range);
1553 break;
1554 default:
1555 ret = -EINVAL;
1557 out:
1558 mnt_drop_write(file->f_path.mnt);
1559 return ret;
1562 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1564 struct btrfs_ioctl_vol_args *vol_args;
1565 int ret;
1567 if (!capable(CAP_SYS_ADMIN))
1568 return -EPERM;
1570 vol_args = memdup_user(arg, sizeof(*vol_args));
1571 if (IS_ERR(vol_args))
1572 return PTR_ERR(vol_args);
1574 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1575 ret = btrfs_init_new_device(root, vol_args->name);
1577 kfree(vol_args);
1578 return ret;
1581 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1583 struct btrfs_ioctl_vol_args *vol_args;
1584 int ret;
1586 if (!capable(CAP_SYS_ADMIN))
1587 return -EPERM;
1589 if (root->fs_info->sb->s_flags & MS_RDONLY)
1590 return -EROFS;
1592 vol_args = memdup_user(arg, sizeof(*vol_args));
1593 if (IS_ERR(vol_args))
1594 return PTR_ERR(vol_args);
1596 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1597 ret = btrfs_rm_device(root, vol_args->name);
1599 kfree(vol_args);
1600 return ret;
1603 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1604 u64 off, u64 olen, u64 destoff)
1606 struct inode *inode = fdentry(file)->d_inode;
1607 struct btrfs_root *root = BTRFS_I(inode)->root;
1608 struct file *src_file;
1609 struct inode *src;
1610 struct btrfs_trans_handle *trans;
1611 struct btrfs_path *path;
1612 struct extent_buffer *leaf;
1613 char *buf;
1614 struct btrfs_key key;
1615 u32 nritems;
1616 int slot;
1617 int ret;
1618 u64 len = olen;
1619 u64 bs = root->fs_info->sb->s_blocksize;
1620 u64 hint_byte;
1623 * TODO:
1624 * - split compressed inline extents. annoying: we need to
1625 * decompress into destination's address_space (the file offset
1626 * may change, so source mapping won't do), then recompress (or
1627 * otherwise reinsert) a subrange.
1628 * - allow ranges within the same file to be cloned (provided
1629 * they don't overlap)?
1632 /* the destination must be opened for writing */
1633 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1634 return -EINVAL;
1636 ret = mnt_want_write(file->f_path.mnt);
1637 if (ret)
1638 return ret;
1640 src_file = fget(srcfd);
1641 if (!src_file) {
1642 ret = -EBADF;
1643 goto out_drop_write;
1646 src = src_file->f_dentry->d_inode;
1648 ret = -EINVAL;
1649 if (src == inode)
1650 goto out_fput;
1652 /* the src must be open for reading */
1653 if (!(src_file->f_mode & FMODE_READ))
1654 goto out_fput;
1656 ret = -EISDIR;
1657 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1658 goto out_fput;
1660 ret = -EXDEV;
1661 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1662 goto out_fput;
1664 ret = -ENOMEM;
1665 buf = vmalloc(btrfs_level_size(root, 0));
1666 if (!buf)
1667 goto out_fput;
1669 path = btrfs_alloc_path();
1670 if (!path) {
1671 vfree(buf);
1672 goto out_fput;
1674 path->reada = 2;
1676 if (inode < src) {
1677 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1678 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
1679 } else {
1680 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1681 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1684 /* determine range to clone */
1685 ret = -EINVAL;
1686 if (off + len > src->i_size || off + len < off)
1687 goto out_unlock;
1688 if (len == 0)
1689 olen = len = src->i_size - off;
1690 /* if we extend to eof, continue to block boundary */
1691 if (off + len == src->i_size)
1692 len = ALIGN(src->i_size, bs) - off;
1694 /* verify the end result is block aligned */
1695 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1696 !IS_ALIGNED(destoff, bs))
1697 goto out_unlock;
1699 /* do any pending delalloc/csum calc on src, one way or
1700 another, and lock file content */
1701 while (1) {
1702 struct btrfs_ordered_extent *ordered;
1703 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1704 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1705 if (!ordered &&
1706 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1707 EXTENT_DELALLOC, 0, NULL))
1708 break;
1709 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1710 if (ordered)
1711 btrfs_put_ordered_extent(ordered);
1712 btrfs_wait_ordered_range(src, off, len);
1715 /* clone data */
1716 key.objectid = src->i_ino;
1717 key.type = BTRFS_EXTENT_DATA_KEY;
1718 key.offset = 0;
1720 while (1) {
1722 * note the key will change type as we walk through the
1723 * tree.
1725 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1726 if (ret < 0)
1727 goto out;
1729 nritems = btrfs_header_nritems(path->nodes[0]);
1730 if (path->slots[0] >= nritems) {
1731 ret = btrfs_next_leaf(root, path);
1732 if (ret < 0)
1733 goto out;
1734 if (ret > 0)
1735 break;
1736 nritems = btrfs_header_nritems(path->nodes[0]);
1738 leaf = path->nodes[0];
1739 slot = path->slots[0];
1741 btrfs_item_key_to_cpu(leaf, &key, slot);
1742 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1743 key.objectid != src->i_ino)
1744 break;
1746 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1747 struct btrfs_file_extent_item *extent;
1748 int type;
1749 u32 size;
1750 struct btrfs_key new_key;
1751 u64 disko = 0, diskl = 0;
1752 u64 datao = 0, datal = 0;
1753 u8 comp;
1754 u64 endoff;
1756 size = btrfs_item_size_nr(leaf, slot);
1757 read_extent_buffer(leaf, buf,
1758 btrfs_item_ptr_offset(leaf, slot),
1759 size);
1761 extent = btrfs_item_ptr(leaf, slot,
1762 struct btrfs_file_extent_item);
1763 comp = btrfs_file_extent_compression(leaf, extent);
1764 type = btrfs_file_extent_type(leaf, extent);
1765 if (type == BTRFS_FILE_EXTENT_REG ||
1766 type == BTRFS_FILE_EXTENT_PREALLOC) {
1767 disko = btrfs_file_extent_disk_bytenr(leaf,
1768 extent);
1769 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1770 extent);
1771 datao = btrfs_file_extent_offset(leaf, extent);
1772 datal = btrfs_file_extent_num_bytes(leaf,
1773 extent);
1774 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1775 /* take upper bound, may be compressed */
1776 datal = btrfs_file_extent_ram_bytes(leaf,
1777 extent);
1779 btrfs_release_path(root, path);
1781 if (key.offset + datal <= off ||
1782 key.offset >= off+len)
1783 goto next;
1785 memcpy(&new_key, &key, sizeof(new_key));
1786 new_key.objectid = inode->i_ino;
1787 new_key.offset = key.offset + destoff - off;
1789 trans = btrfs_start_transaction(root, 1);
1790 if (IS_ERR(trans)) {
1791 ret = PTR_ERR(trans);
1792 goto out;
1795 if (type == BTRFS_FILE_EXTENT_REG ||
1796 type == BTRFS_FILE_EXTENT_PREALLOC) {
1797 if (off > key.offset) {
1798 datao += off - key.offset;
1799 datal -= off - key.offset;
1802 if (key.offset + datal > off + len)
1803 datal = off + len - key.offset;
1805 ret = btrfs_drop_extents(trans, inode,
1806 new_key.offset,
1807 new_key.offset + datal,
1808 &hint_byte, 1);
1809 BUG_ON(ret);
1811 ret = btrfs_insert_empty_item(trans, root, path,
1812 &new_key, size);
1813 BUG_ON(ret);
1815 leaf = path->nodes[0];
1816 slot = path->slots[0];
1817 write_extent_buffer(leaf, buf,
1818 btrfs_item_ptr_offset(leaf, slot),
1819 size);
1821 extent = btrfs_item_ptr(leaf, slot,
1822 struct btrfs_file_extent_item);
1824 /* disko == 0 means it's a hole */
1825 if (!disko)
1826 datao = 0;
1828 btrfs_set_file_extent_offset(leaf, extent,
1829 datao);
1830 btrfs_set_file_extent_num_bytes(leaf, extent,
1831 datal);
1832 if (disko) {
1833 inode_add_bytes(inode, datal);
1834 ret = btrfs_inc_extent_ref(trans, root,
1835 disko, diskl, 0,
1836 root->root_key.objectid,
1837 inode->i_ino,
1838 new_key.offset - datao);
1839 BUG_ON(ret);
1841 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1842 u64 skip = 0;
1843 u64 trim = 0;
1844 if (off > key.offset) {
1845 skip = off - key.offset;
1846 new_key.offset += skip;
1849 if (key.offset + datal > off+len)
1850 trim = key.offset + datal - (off+len);
1852 if (comp && (skip || trim)) {
1853 ret = -EINVAL;
1854 btrfs_end_transaction(trans, root);
1855 goto out;
1857 size -= skip + trim;
1858 datal -= skip + trim;
1860 ret = btrfs_drop_extents(trans, inode,
1861 new_key.offset,
1862 new_key.offset + datal,
1863 &hint_byte, 1);
1864 BUG_ON(ret);
1866 ret = btrfs_insert_empty_item(trans, root, path,
1867 &new_key, size);
1868 BUG_ON(ret);
1870 if (skip) {
1871 u32 start =
1872 btrfs_file_extent_calc_inline_size(0);
1873 memmove(buf+start, buf+start+skip,
1874 datal);
1877 leaf = path->nodes[0];
1878 slot = path->slots[0];
1879 write_extent_buffer(leaf, buf,
1880 btrfs_item_ptr_offset(leaf, slot),
1881 size);
1882 inode_add_bytes(inode, datal);
1885 btrfs_mark_buffer_dirty(leaf);
1886 btrfs_release_path(root, path);
1888 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1891 * we round up to the block size at eof when
1892 * determining which extents to clone above,
1893 * but shouldn't round up the file size
1895 endoff = new_key.offset + datal;
1896 if (endoff > destoff+olen)
1897 endoff = destoff+olen;
1898 if (endoff > inode->i_size)
1899 btrfs_i_size_write(inode, endoff);
1901 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1902 ret = btrfs_update_inode(trans, root, inode);
1903 BUG_ON(ret);
1904 btrfs_end_transaction(trans, root);
1906 next:
1907 btrfs_release_path(root, path);
1908 key.offset++;
1910 ret = 0;
1911 out:
1912 btrfs_release_path(root, path);
1913 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1914 out_unlock:
1915 mutex_unlock(&src->i_mutex);
1916 mutex_unlock(&inode->i_mutex);
1917 vfree(buf);
1918 btrfs_free_path(path);
1919 out_fput:
1920 fput(src_file);
1921 out_drop_write:
1922 mnt_drop_write(file->f_path.mnt);
1923 return ret;
1926 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1928 struct btrfs_ioctl_clone_range_args args;
1930 if (copy_from_user(&args, argp, sizeof(args)))
1931 return -EFAULT;
1932 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1933 args.src_length, args.dest_offset);
1937 * there are many ways the trans_start and trans_end ioctls can lead
1938 * to deadlocks. They should only be used by applications that
1939 * basically own the machine, and have a very in depth understanding
1940 * of all the possible deadlocks and enospc problems.
1942 static long btrfs_ioctl_trans_start(struct file *file)
1944 struct inode *inode = fdentry(file)->d_inode;
1945 struct btrfs_root *root = BTRFS_I(inode)->root;
1946 struct btrfs_trans_handle *trans;
1947 int ret;
1949 ret = -EPERM;
1950 if (!capable(CAP_SYS_ADMIN))
1951 goto out;
1953 ret = -EINPROGRESS;
1954 if (file->private_data)
1955 goto out;
1957 ret = mnt_want_write(file->f_path.mnt);
1958 if (ret)
1959 goto out;
1961 mutex_lock(&root->fs_info->trans_mutex);
1962 root->fs_info->open_ioctl_trans++;
1963 mutex_unlock(&root->fs_info->trans_mutex);
1965 ret = -ENOMEM;
1966 trans = btrfs_start_ioctl_transaction(root, 0);
1967 if (!trans)
1968 goto out_drop;
1970 file->private_data = trans;
1971 return 0;
1973 out_drop:
1974 mutex_lock(&root->fs_info->trans_mutex);
1975 root->fs_info->open_ioctl_trans--;
1976 mutex_unlock(&root->fs_info->trans_mutex);
1977 mnt_drop_write(file->f_path.mnt);
1978 out:
1979 return ret;
1982 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1984 struct inode *inode = fdentry(file)->d_inode;
1985 struct btrfs_root *root = BTRFS_I(inode)->root;
1986 struct btrfs_root *new_root;
1987 struct btrfs_dir_item *di;
1988 struct btrfs_trans_handle *trans;
1989 struct btrfs_path *path;
1990 struct btrfs_key location;
1991 struct btrfs_disk_key disk_key;
1992 struct btrfs_super_block *disk_super;
1993 u64 features;
1994 u64 objectid = 0;
1995 u64 dir_id;
1997 if (!capable(CAP_SYS_ADMIN))
1998 return -EPERM;
2000 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2001 return -EFAULT;
2003 if (!objectid)
2004 objectid = root->root_key.objectid;
2006 location.objectid = objectid;
2007 location.type = BTRFS_ROOT_ITEM_KEY;
2008 location.offset = (u64)-1;
2010 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2011 if (IS_ERR(new_root))
2012 return PTR_ERR(new_root);
2014 if (btrfs_root_refs(&new_root->root_item) == 0)
2015 return -ENOENT;
2017 path = btrfs_alloc_path();
2018 if (!path)
2019 return -ENOMEM;
2020 path->leave_spinning = 1;
2022 trans = btrfs_start_transaction(root, 1);
2023 if (!trans) {
2024 btrfs_free_path(path);
2025 return -ENOMEM;
2028 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2029 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2030 dir_id, "default", 7, 1);
2031 if (IS_ERR_OR_NULL(di)) {
2032 btrfs_free_path(path);
2033 btrfs_end_transaction(trans, root);
2034 printk(KERN_ERR "Umm, you don't have the default dir item, "
2035 "this isn't going to work\n");
2036 return -ENOENT;
2039 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2040 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2041 btrfs_mark_buffer_dirty(path->nodes[0]);
2042 btrfs_free_path(path);
2044 disk_super = &root->fs_info->super_copy;
2045 features = btrfs_super_incompat_flags(disk_super);
2046 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2047 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2048 btrfs_set_super_incompat_flags(disk_super, features);
2050 btrfs_end_transaction(trans, root);
2052 return 0;
2055 static void get_block_group_info(struct list_head *groups_list,
2056 struct btrfs_ioctl_space_info *space)
2058 struct btrfs_block_group_cache *block_group;
2060 space->total_bytes = 0;
2061 space->used_bytes = 0;
2062 space->flags = 0;
2063 list_for_each_entry(block_group, groups_list, list) {
2064 space->flags = block_group->flags;
2065 space->total_bytes += block_group->key.offset;
2066 space->used_bytes +=
2067 btrfs_block_group_used(&block_group->item);
2071 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2073 struct btrfs_ioctl_space_args space_args;
2074 struct btrfs_ioctl_space_info space;
2075 struct btrfs_ioctl_space_info *dest;
2076 struct btrfs_ioctl_space_info *dest_orig;
2077 struct btrfs_ioctl_space_info *user_dest;
2078 struct btrfs_space_info *info;
2079 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2080 BTRFS_BLOCK_GROUP_SYSTEM,
2081 BTRFS_BLOCK_GROUP_METADATA,
2082 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2083 int num_types = 4;
2084 int alloc_size;
2085 int ret = 0;
2086 int slot_count = 0;
2087 int i, c;
2089 if (copy_from_user(&space_args,
2090 (struct btrfs_ioctl_space_args __user *)arg,
2091 sizeof(space_args)))
2092 return -EFAULT;
2094 for (i = 0; i < num_types; i++) {
2095 struct btrfs_space_info *tmp;
2097 info = NULL;
2098 rcu_read_lock();
2099 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2100 list) {
2101 if (tmp->flags == types[i]) {
2102 info = tmp;
2103 break;
2106 rcu_read_unlock();
2108 if (!info)
2109 continue;
2111 down_read(&info->groups_sem);
2112 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2113 if (!list_empty(&info->block_groups[c]))
2114 slot_count++;
2116 up_read(&info->groups_sem);
2119 /* space_slots == 0 means they are asking for a count */
2120 if (space_args.space_slots == 0) {
2121 space_args.total_spaces = slot_count;
2122 goto out;
2125 slot_count = min_t(int, space_args.space_slots, slot_count);
2127 alloc_size = sizeof(*dest) * slot_count;
2129 /* we generally have at most 6 or so space infos, one for each raid
2130 * level. So, a whole page should be more than enough for everyone
2132 if (alloc_size > PAGE_CACHE_SIZE)
2133 return -ENOMEM;
2135 space_args.total_spaces = 0;
2136 dest = kmalloc(alloc_size, GFP_NOFS);
2137 if (!dest)
2138 return -ENOMEM;
2139 dest_orig = dest;
2141 /* now we have a buffer to copy into */
2142 for (i = 0; i < num_types; i++) {
2143 struct btrfs_space_info *tmp;
2145 info = NULL;
2146 rcu_read_lock();
2147 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2148 list) {
2149 if (tmp->flags == types[i]) {
2150 info = tmp;
2151 break;
2154 rcu_read_unlock();
2156 if (!info)
2157 continue;
2158 down_read(&info->groups_sem);
2159 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2160 if (!list_empty(&info->block_groups[c])) {
2161 get_block_group_info(&info->block_groups[c],
2162 &space);
2163 memcpy(dest, &space, sizeof(space));
2164 dest++;
2165 space_args.total_spaces++;
2168 up_read(&info->groups_sem);
2171 user_dest = (struct btrfs_ioctl_space_info *)
2172 (arg + sizeof(struct btrfs_ioctl_space_args));
2174 if (copy_to_user(user_dest, dest_orig, alloc_size))
2175 ret = -EFAULT;
2177 kfree(dest_orig);
2178 out:
2179 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2180 ret = -EFAULT;
2182 return ret;
2186 * there are many ways the trans_start and trans_end ioctls can lead
2187 * to deadlocks. They should only be used by applications that
2188 * basically own the machine, and have a very in depth understanding
2189 * of all the possible deadlocks and enospc problems.
2191 long btrfs_ioctl_trans_end(struct file *file)
2193 struct inode *inode = fdentry(file)->d_inode;
2194 struct btrfs_root *root = BTRFS_I(inode)->root;
2195 struct btrfs_trans_handle *trans;
2197 trans = file->private_data;
2198 if (!trans)
2199 return -EINVAL;
2200 file->private_data = NULL;
2202 btrfs_end_transaction(trans, root);
2204 mutex_lock(&root->fs_info->trans_mutex);
2205 root->fs_info->open_ioctl_trans--;
2206 mutex_unlock(&root->fs_info->trans_mutex);
2208 mnt_drop_write(file->f_path.mnt);
2209 return 0;
2212 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2214 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2215 struct btrfs_trans_handle *trans;
2216 u64 transid;
2218 trans = btrfs_start_transaction(root, 0);
2219 transid = trans->transid;
2220 btrfs_commit_transaction_async(trans, root, 0);
2222 if (argp)
2223 if (copy_to_user(argp, &transid, sizeof(transid)))
2224 return -EFAULT;
2225 return 0;
2228 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2230 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2231 u64 transid;
2233 if (argp) {
2234 if (copy_from_user(&transid, argp, sizeof(transid)))
2235 return -EFAULT;
2236 } else {
2237 transid = 0; /* current trans */
2239 return btrfs_wait_for_commit(root, transid);
2242 long btrfs_ioctl(struct file *file, unsigned int
2243 cmd, unsigned long arg)
2245 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2246 void __user *argp = (void __user *)arg;
2248 switch (cmd) {
2249 case FS_IOC_GETFLAGS:
2250 return btrfs_ioctl_getflags(file, argp);
2251 case FS_IOC_SETFLAGS:
2252 return btrfs_ioctl_setflags(file, argp);
2253 case FS_IOC_GETVERSION:
2254 return btrfs_ioctl_getversion(file, argp);
2255 case BTRFS_IOC_SNAP_CREATE:
2256 return btrfs_ioctl_snap_create(file, argp, 0);
2257 case BTRFS_IOC_SNAP_CREATE_V2:
2258 return btrfs_ioctl_snap_create_v2(file, argp, 0);
2259 case BTRFS_IOC_SUBVOL_CREATE:
2260 return btrfs_ioctl_snap_create(file, argp, 1);
2261 case BTRFS_IOC_SNAP_DESTROY:
2262 return btrfs_ioctl_snap_destroy(file, argp);
2263 case BTRFS_IOC_DEFAULT_SUBVOL:
2264 return btrfs_ioctl_default_subvol(file, argp);
2265 case BTRFS_IOC_DEFRAG:
2266 return btrfs_ioctl_defrag(file, NULL);
2267 case BTRFS_IOC_DEFRAG_RANGE:
2268 return btrfs_ioctl_defrag(file, argp);
2269 case BTRFS_IOC_RESIZE:
2270 return btrfs_ioctl_resize(root, argp);
2271 case BTRFS_IOC_ADD_DEV:
2272 return btrfs_ioctl_add_dev(root, argp);
2273 case BTRFS_IOC_RM_DEV:
2274 return btrfs_ioctl_rm_dev(root, argp);
2275 case BTRFS_IOC_BALANCE:
2276 return btrfs_balance(root->fs_info->dev_root);
2277 case BTRFS_IOC_CLONE:
2278 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2279 case BTRFS_IOC_CLONE_RANGE:
2280 return btrfs_ioctl_clone_range(file, argp);
2281 case BTRFS_IOC_TRANS_START:
2282 return btrfs_ioctl_trans_start(file);
2283 case BTRFS_IOC_TRANS_END:
2284 return btrfs_ioctl_trans_end(file);
2285 case BTRFS_IOC_TREE_SEARCH:
2286 return btrfs_ioctl_tree_search(file, argp);
2287 case BTRFS_IOC_INO_LOOKUP:
2288 return btrfs_ioctl_ino_lookup(file, argp);
2289 case BTRFS_IOC_SPACE_INFO:
2290 return btrfs_ioctl_space_info(root, argp);
2291 case BTRFS_IOC_SYNC:
2292 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2293 return 0;
2294 case BTRFS_IOC_START_SYNC:
2295 return btrfs_ioctl_start_sync(file, argp);
2296 case BTRFS_IOC_WAIT_SYNC:
2297 return btrfs_ioctl_wait_sync(file, argp);
2300 return -ENOTTY;