fs: remove FS_COW_FL
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / ioctl.c
blob3240dd90da42899675d4378a6bd22e9589917811
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 "compat.h"
45 #include "ctree.h"
46 #include "disk-io.h"
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "ioctl.h"
50 #include "print-tree.h"
51 #include "volumes.h"
52 #include "locking.h"
54 /* Mask out flags that are inappropriate for the given type of inode. */
55 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
57 if (S_ISDIR(mode))
58 return flags;
59 else if (S_ISREG(mode))
60 return flags & ~FS_DIRSYNC_FL;
61 else
62 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
66 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
68 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
70 unsigned int iflags = 0;
72 if (flags & BTRFS_INODE_SYNC)
73 iflags |= FS_SYNC_FL;
74 if (flags & BTRFS_INODE_IMMUTABLE)
75 iflags |= FS_IMMUTABLE_FL;
76 if (flags & BTRFS_INODE_APPEND)
77 iflags |= FS_APPEND_FL;
78 if (flags & BTRFS_INODE_NODUMP)
79 iflags |= FS_NODUMP_FL;
80 if (flags & BTRFS_INODE_NOATIME)
81 iflags |= FS_NOATIME_FL;
82 if (flags & BTRFS_INODE_DIRSYNC)
83 iflags |= FS_DIRSYNC_FL;
85 return iflags;
89 * Update inode->i_flags based on the btrfs internal flags.
91 void btrfs_update_iflags(struct inode *inode)
93 struct btrfs_inode *ip = BTRFS_I(inode);
95 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
97 if (ip->flags & BTRFS_INODE_SYNC)
98 inode->i_flags |= S_SYNC;
99 if (ip->flags & BTRFS_INODE_IMMUTABLE)
100 inode->i_flags |= S_IMMUTABLE;
101 if (ip->flags & BTRFS_INODE_APPEND)
102 inode->i_flags |= S_APPEND;
103 if (ip->flags & BTRFS_INODE_NOATIME)
104 inode->i_flags |= S_NOATIME;
105 if (ip->flags & BTRFS_INODE_DIRSYNC)
106 inode->i_flags |= S_DIRSYNC;
110 * Inherit flags from the parent inode.
112 * Unlike extN we don't have any flags we don't want to inherit currently.
114 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
116 unsigned int flags;
118 if (!dir)
119 return;
121 flags = BTRFS_I(dir)->flags;
123 if (S_ISREG(inode->i_mode))
124 flags &= ~BTRFS_INODE_DIRSYNC;
125 else if (!S_ISDIR(inode->i_mode))
126 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
128 BTRFS_I(inode)->flags = flags;
129 btrfs_update_iflags(inode);
132 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
134 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
135 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
137 if (copy_to_user(arg, &flags, sizeof(flags)))
138 return -EFAULT;
139 return 0;
142 static int check_flags(unsigned int flags)
144 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
145 FS_NOATIME_FL | FS_NODUMP_FL | \
146 FS_SYNC_FL | FS_DIRSYNC_FL | \
147 FS_NOCOMP_FL | FS_COMPR_FL |
148 FS_NOCOW_FL))
149 return -EOPNOTSUPP;
151 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
152 return -EINVAL;
154 return 0;
157 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
159 struct inode *inode = file->f_path.dentry->d_inode;
160 struct btrfs_inode *ip = BTRFS_I(inode);
161 struct btrfs_root *root = ip->root;
162 struct btrfs_trans_handle *trans;
163 unsigned int flags, oldflags;
164 int ret;
166 if (btrfs_root_readonly(root))
167 return -EROFS;
169 if (copy_from_user(&flags, arg, sizeof(flags)))
170 return -EFAULT;
172 ret = check_flags(flags);
173 if (ret)
174 return ret;
176 if (!is_owner_or_cap(inode))
177 return -EACCES;
179 mutex_lock(&inode->i_mutex);
181 flags = btrfs_mask_flags(inode->i_mode, flags);
182 oldflags = btrfs_flags_to_ioctl(ip->flags);
183 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
184 if (!capable(CAP_LINUX_IMMUTABLE)) {
185 ret = -EPERM;
186 goto out_unlock;
190 ret = mnt_want_write(file->f_path.mnt);
191 if (ret)
192 goto out_unlock;
194 if (flags & FS_SYNC_FL)
195 ip->flags |= BTRFS_INODE_SYNC;
196 else
197 ip->flags &= ~BTRFS_INODE_SYNC;
198 if (flags & FS_IMMUTABLE_FL)
199 ip->flags |= BTRFS_INODE_IMMUTABLE;
200 else
201 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
202 if (flags & FS_APPEND_FL)
203 ip->flags |= BTRFS_INODE_APPEND;
204 else
205 ip->flags &= ~BTRFS_INODE_APPEND;
206 if (flags & FS_NODUMP_FL)
207 ip->flags |= BTRFS_INODE_NODUMP;
208 else
209 ip->flags &= ~BTRFS_INODE_NODUMP;
210 if (flags & FS_NOATIME_FL)
211 ip->flags |= BTRFS_INODE_NOATIME;
212 else
213 ip->flags &= ~BTRFS_INODE_NOATIME;
214 if (flags & FS_DIRSYNC_FL)
215 ip->flags |= BTRFS_INODE_DIRSYNC;
216 else
217 ip->flags &= ~BTRFS_INODE_DIRSYNC;
218 if (flags & FS_NOCOW_FL)
219 ip->flags |= BTRFS_INODE_NODATACOW;
220 else
221 ip->flags &= ~BTRFS_INODE_NODATACOW;
224 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
225 * flag may be changed automatically if compression code won't make
226 * things smaller.
228 if (flags & FS_NOCOMP_FL) {
229 ip->flags &= ~BTRFS_INODE_COMPRESS;
230 ip->flags |= BTRFS_INODE_NOCOMPRESS;
231 } else if (flags & FS_COMPR_FL) {
232 ip->flags |= BTRFS_INODE_COMPRESS;
233 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
236 trans = btrfs_join_transaction(root, 1);
237 BUG_ON(IS_ERR(trans));
239 ret = btrfs_update_inode(trans, root, inode);
240 BUG_ON(ret);
242 btrfs_update_iflags(inode);
243 inode->i_ctime = CURRENT_TIME;
244 btrfs_end_transaction(trans, root);
246 mnt_drop_write(file->f_path.mnt);
248 ret = 0;
249 out_unlock:
250 mutex_unlock(&inode->i_mutex);
251 return ret;
254 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
256 struct inode *inode = file->f_path.dentry->d_inode;
258 return put_user(inode->i_generation, arg);
261 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
263 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
264 struct btrfs_fs_info *fs_info = root->fs_info;
265 struct btrfs_device *device;
266 struct request_queue *q;
267 struct fstrim_range range;
268 u64 minlen = ULLONG_MAX;
269 u64 num_devices = 0;
270 int ret;
272 if (!capable(CAP_SYS_ADMIN))
273 return -EPERM;
275 mutex_lock(&fs_info->fs_devices->device_list_mutex);
276 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
277 if (!device->bdev)
278 continue;
279 q = bdev_get_queue(device->bdev);
280 if (blk_queue_discard(q)) {
281 num_devices++;
282 minlen = min((u64)q->limits.discard_granularity,
283 minlen);
286 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
287 if (!num_devices)
288 return -EOPNOTSUPP;
290 if (copy_from_user(&range, arg, sizeof(range)))
291 return -EFAULT;
293 range.minlen = max(range.minlen, minlen);
294 ret = btrfs_trim_fs(root, &range);
295 if (ret < 0)
296 return ret;
298 if (copy_to_user(arg, &range, sizeof(range)))
299 return -EFAULT;
301 return 0;
304 static noinline int create_subvol(struct btrfs_root *root,
305 struct dentry *dentry,
306 char *name, int namelen,
307 u64 *async_transid)
309 struct btrfs_trans_handle *trans;
310 struct btrfs_key key;
311 struct btrfs_root_item root_item;
312 struct btrfs_inode_item *inode_item;
313 struct extent_buffer *leaf;
314 struct btrfs_root *new_root;
315 struct dentry *parent = dget_parent(dentry);
316 struct inode *dir;
317 int ret;
318 int err;
319 u64 objectid;
320 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
321 u64 index = 0;
323 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
324 0, &objectid);
325 if (ret) {
326 dput(parent);
327 return ret;
330 dir = parent->d_inode;
333 * 1 - inode item
334 * 2 - refs
335 * 1 - root item
336 * 2 - dir items
338 trans = btrfs_start_transaction(root, 6);
339 if (IS_ERR(trans)) {
340 dput(parent);
341 return PTR_ERR(trans);
344 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
345 0, objectid, NULL, 0, 0, 0);
346 if (IS_ERR(leaf)) {
347 ret = PTR_ERR(leaf);
348 goto fail;
351 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
352 btrfs_set_header_bytenr(leaf, leaf->start);
353 btrfs_set_header_generation(leaf, trans->transid);
354 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
355 btrfs_set_header_owner(leaf, objectid);
357 write_extent_buffer(leaf, root->fs_info->fsid,
358 (unsigned long)btrfs_header_fsid(leaf),
359 BTRFS_FSID_SIZE);
360 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
361 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
362 BTRFS_UUID_SIZE);
363 btrfs_mark_buffer_dirty(leaf);
365 inode_item = &root_item.inode;
366 memset(inode_item, 0, sizeof(*inode_item));
367 inode_item->generation = cpu_to_le64(1);
368 inode_item->size = cpu_to_le64(3);
369 inode_item->nlink = cpu_to_le32(1);
370 inode_item->nbytes = cpu_to_le64(root->leafsize);
371 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
373 root_item.flags = 0;
374 root_item.byte_limit = 0;
375 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
377 btrfs_set_root_bytenr(&root_item, leaf->start);
378 btrfs_set_root_generation(&root_item, trans->transid);
379 btrfs_set_root_level(&root_item, 0);
380 btrfs_set_root_refs(&root_item, 1);
381 btrfs_set_root_used(&root_item, leaf->len);
382 btrfs_set_root_last_snapshot(&root_item, 0);
384 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
385 root_item.drop_level = 0;
387 btrfs_tree_unlock(leaf);
388 free_extent_buffer(leaf);
389 leaf = NULL;
391 btrfs_set_root_dirid(&root_item, new_dirid);
393 key.objectid = objectid;
394 key.offset = 0;
395 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
396 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
397 &root_item);
398 if (ret)
399 goto fail;
401 key.offset = (u64)-1;
402 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
403 BUG_ON(IS_ERR(new_root));
405 btrfs_record_root_in_trans(trans, new_root);
407 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
408 BTRFS_I(dir)->block_group);
410 * insert the directory item
412 ret = btrfs_set_inode_index(dir, &index);
413 BUG_ON(ret);
415 ret = btrfs_insert_dir_item(trans, root,
416 name, namelen, dir->i_ino, &key,
417 BTRFS_FT_DIR, index);
418 if (ret)
419 goto fail;
421 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
422 ret = btrfs_update_inode(trans, root, dir);
423 BUG_ON(ret);
425 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
426 objectid, root->root_key.objectid,
427 dir->i_ino, index, name, namelen);
429 BUG_ON(ret);
431 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
432 fail:
433 dput(parent);
434 if (async_transid) {
435 *async_transid = trans->transid;
436 err = btrfs_commit_transaction_async(trans, root, 1);
437 } else {
438 err = btrfs_commit_transaction(trans, root);
440 if (err && !ret)
441 ret = err;
442 return ret;
445 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
446 char *name, int namelen, u64 *async_transid,
447 bool readonly)
449 struct inode *inode;
450 struct dentry *parent;
451 struct btrfs_pending_snapshot *pending_snapshot;
452 struct btrfs_trans_handle *trans;
453 int ret;
455 if (!root->ref_cows)
456 return -EINVAL;
458 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
459 if (!pending_snapshot)
460 return -ENOMEM;
462 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
463 pending_snapshot->dentry = dentry;
464 pending_snapshot->root = root;
465 pending_snapshot->readonly = readonly;
467 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
468 if (IS_ERR(trans)) {
469 ret = PTR_ERR(trans);
470 goto fail;
473 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
474 BUG_ON(ret);
476 list_add(&pending_snapshot->list,
477 &trans->transaction->pending_snapshots);
478 if (async_transid) {
479 *async_transid = trans->transid;
480 ret = btrfs_commit_transaction_async(trans,
481 root->fs_info->extent_root, 1);
482 } else {
483 ret = btrfs_commit_transaction(trans,
484 root->fs_info->extent_root);
486 BUG_ON(ret);
488 ret = pending_snapshot->error;
489 if (ret)
490 goto fail;
492 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
493 if (ret)
494 goto fail;
496 parent = dget_parent(dentry);
497 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
498 dput(parent);
499 if (IS_ERR(inode)) {
500 ret = PTR_ERR(inode);
501 goto fail;
503 BUG_ON(!inode);
504 d_instantiate(dentry, inode);
505 ret = 0;
506 fail:
507 kfree(pending_snapshot);
508 return ret;
511 /* copy of check_sticky in fs/namei.c()
512 * It's inline, so penalty for filesystems that don't use sticky bit is
513 * minimal.
515 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
517 uid_t fsuid = current_fsuid();
519 if (!(dir->i_mode & S_ISVTX))
520 return 0;
521 if (inode->i_uid == fsuid)
522 return 0;
523 if (dir->i_uid == fsuid)
524 return 0;
525 return !capable(CAP_FOWNER);
528 /* copy of may_delete in fs/namei.c()
529 * Check whether we can remove a link victim from directory dir, check
530 * whether the type of victim is right.
531 * 1. We can't do it if dir is read-only (done in permission())
532 * 2. We should have write and exec permissions on dir
533 * 3. We can't remove anything from append-only dir
534 * 4. We can't do anything with immutable dir (done in permission())
535 * 5. If the sticky bit on dir is set we should either
536 * a. be owner of dir, or
537 * b. be owner of victim, or
538 * c. have CAP_FOWNER capability
539 * 6. If the victim is append-only or immutable we can't do antyhing with
540 * links pointing to it.
541 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
542 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
543 * 9. We can't remove a root or mountpoint.
544 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
545 * nfs_async_unlink().
548 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
550 int error;
552 if (!victim->d_inode)
553 return -ENOENT;
555 BUG_ON(victim->d_parent->d_inode != dir);
556 audit_inode_child(victim, dir);
558 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
559 if (error)
560 return error;
561 if (IS_APPEND(dir))
562 return -EPERM;
563 if (btrfs_check_sticky(dir, victim->d_inode)||
564 IS_APPEND(victim->d_inode)||
565 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
566 return -EPERM;
567 if (isdir) {
568 if (!S_ISDIR(victim->d_inode->i_mode))
569 return -ENOTDIR;
570 if (IS_ROOT(victim))
571 return -EBUSY;
572 } else if (S_ISDIR(victim->d_inode->i_mode))
573 return -EISDIR;
574 if (IS_DEADDIR(dir))
575 return -ENOENT;
576 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
577 return -EBUSY;
578 return 0;
581 /* copy of may_create in fs/namei.c() */
582 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
584 if (child->d_inode)
585 return -EEXIST;
586 if (IS_DEADDIR(dir))
587 return -ENOENT;
588 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
592 * Create a new subvolume below @parent. This is largely modeled after
593 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
594 * inside this filesystem so it's quite a bit simpler.
596 static noinline int btrfs_mksubvol(struct path *parent,
597 char *name, int namelen,
598 struct btrfs_root *snap_src,
599 u64 *async_transid, bool readonly)
601 struct inode *dir = parent->dentry->d_inode;
602 struct dentry *dentry;
603 int error;
605 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
607 dentry = lookup_one_len(name, parent->dentry, namelen);
608 error = PTR_ERR(dentry);
609 if (IS_ERR(dentry))
610 goto out_unlock;
612 error = -EEXIST;
613 if (dentry->d_inode)
614 goto out_dput;
616 error = mnt_want_write(parent->mnt);
617 if (error)
618 goto out_dput;
620 error = btrfs_may_create(dir, dentry);
621 if (error)
622 goto out_drop_write;
624 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
626 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
627 goto out_up_read;
629 if (snap_src) {
630 error = create_snapshot(snap_src, dentry,
631 name, namelen, async_transid, readonly);
632 } else {
633 error = create_subvol(BTRFS_I(dir)->root, dentry,
634 name, namelen, async_transid);
636 if (!error)
637 fsnotify_mkdir(dir, dentry);
638 out_up_read:
639 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
640 out_drop_write:
641 mnt_drop_write(parent->mnt);
642 out_dput:
643 dput(dentry);
644 out_unlock:
645 mutex_unlock(&dir->i_mutex);
646 return error;
649 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
650 int thresh, u64 *last_len, u64 *skip,
651 u64 *defrag_end)
653 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
654 struct extent_map *em = NULL;
655 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
656 int ret = 1;
659 if (thresh == 0)
660 thresh = 256 * 1024;
663 * make sure that once we start defragging and extent, we keep on
664 * defragging it
666 if (start < *defrag_end)
667 return 1;
669 *skip = 0;
672 * hopefully we have this extent in the tree already, try without
673 * the full extent lock
675 read_lock(&em_tree->lock);
676 em = lookup_extent_mapping(em_tree, start, len);
677 read_unlock(&em_tree->lock);
679 if (!em) {
680 /* get the big lock and read metadata off disk */
681 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
682 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
683 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
685 if (IS_ERR(em))
686 return 0;
689 /* this will cover holes, and inline extents */
690 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
691 ret = 0;
694 * we hit a real extent, if it is big don't bother defragging it again
696 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
697 ret = 0;
700 * last_len ends up being a counter of how many bytes we've defragged.
701 * every time we choose not to defrag an extent, we reset *last_len
702 * so that the next tiny extent will force a defrag.
704 * The end result of this is that tiny extents before a single big
705 * extent will force at least part of that big extent to be defragged.
707 if (ret) {
708 *last_len += len;
709 *defrag_end = extent_map_end(em);
710 } else {
711 *last_len = 0;
712 *skip = extent_map_end(em);
713 *defrag_end = 0;
716 free_extent_map(em);
717 return ret;
720 static int btrfs_defrag_file(struct file *file,
721 struct btrfs_ioctl_defrag_range_args *range)
723 struct inode *inode = fdentry(file)->d_inode;
724 struct btrfs_root *root = BTRFS_I(inode)->root;
725 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
726 struct btrfs_ordered_extent *ordered;
727 struct page *page;
728 struct btrfs_super_block *disk_super;
729 unsigned long last_index;
730 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
731 unsigned long total_read = 0;
732 u64 features;
733 u64 page_start;
734 u64 page_end;
735 u64 last_len = 0;
736 u64 skip = 0;
737 u64 defrag_end = 0;
738 unsigned long i;
739 int ret;
740 int compress_type = BTRFS_COMPRESS_ZLIB;
742 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
743 if (range->compress_type > BTRFS_COMPRESS_TYPES)
744 return -EINVAL;
745 if (range->compress_type)
746 compress_type = range->compress_type;
749 if (inode->i_size == 0)
750 return 0;
752 if (range->start + range->len > range->start) {
753 last_index = min_t(u64, inode->i_size - 1,
754 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
755 } else {
756 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
759 i = range->start >> PAGE_CACHE_SHIFT;
760 while (i <= last_index) {
761 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
762 PAGE_CACHE_SIZE,
763 range->extent_thresh,
764 &last_len, &skip,
765 &defrag_end)) {
766 unsigned long next;
768 * the should_defrag function tells us how much to skip
769 * bump our counter by the suggested amount
771 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
772 i = max(i + 1, next);
773 continue;
776 if (total_read % ra_pages == 0) {
777 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
778 min(last_index, i + ra_pages - 1));
780 total_read++;
781 mutex_lock(&inode->i_mutex);
782 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
783 BTRFS_I(inode)->force_compress = compress_type;
785 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
786 if (ret)
787 goto err_unlock;
788 again:
789 if (inode->i_size == 0 ||
790 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
791 ret = 0;
792 goto err_reservations;
795 page = grab_cache_page(inode->i_mapping, i);
796 if (!page) {
797 ret = -ENOMEM;
798 goto err_reservations;
801 if (!PageUptodate(page)) {
802 btrfs_readpage(NULL, page);
803 lock_page(page);
804 if (!PageUptodate(page)) {
805 unlock_page(page);
806 page_cache_release(page);
807 ret = -EIO;
808 goto err_reservations;
812 if (page->mapping != inode->i_mapping) {
813 unlock_page(page);
814 page_cache_release(page);
815 goto again;
818 wait_on_page_writeback(page);
820 if (PageDirty(page)) {
821 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
822 goto loop_unlock;
825 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
826 page_end = page_start + PAGE_CACHE_SIZE - 1;
827 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
829 ordered = btrfs_lookup_ordered_extent(inode, page_start);
830 if (ordered) {
831 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
832 unlock_page(page);
833 page_cache_release(page);
834 btrfs_start_ordered_extent(inode, ordered, 1);
835 btrfs_put_ordered_extent(ordered);
836 goto again;
838 set_page_extent_mapped(page);
841 * this makes sure page_mkwrite is called on the
842 * page if it is dirtied again later
844 clear_page_dirty_for_io(page);
845 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
846 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
847 EXTENT_DO_ACCOUNTING, GFP_NOFS);
849 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
850 ClearPageChecked(page);
851 set_page_dirty(page);
852 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
854 loop_unlock:
855 unlock_page(page);
856 page_cache_release(page);
857 mutex_unlock(&inode->i_mutex);
859 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
860 i++;
863 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
864 filemap_flush(inode->i_mapping);
866 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
867 /* the filemap_flush will queue IO into the worker threads, but
868 * we have to make sure the IO is actually started and that
869 * ordered extents get created before we return
871 atomic_inc(&root->fs_info->async_submit_draining);
872 while (atomic_read(&root->fs_info->nr_async_submits) ||
873 atomic_read(&root->fs_info->async_delalloc_pages)) {
874 wait_event(root->fs_info->async_submit_wait,
875 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
876 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
878 atomic_dec(&root->fs_info->async_submit_draining);
880 mutex_lock(&inode->i_mutex);
881 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
882 mutex_unlock(&inode->i_mutex);
885 disk_super = &root->fs_info->super_copy;
886 features = btrfs_super_incompat_flags(disk_super);
887 if (range->compress_type == BTRFS_COMPRESS_LZO) {
888 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
889 btrfs_set_super_incompat_flags(disk_super, features);
892 return 0;
894 err_reservations:
895 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
896 err_unlock:
897 mutex_unlock(&inode->i_mutex);
898 return ret;
901 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
902 void __user *arg)
904 u64 new_size;
905 u64 old_size;
906 u64 devid = 1;
907 struct btrfs_ioctl_vol_args *vol_args;
908 struct btrfs_trans_handle *trans;
909 struct btrfs_device *device = NULL;
910 char *sizestr;
911 char *devstr = NULL;
912 int ret = 0;
913 int mod = 0;
915 if (root->fs_info->sb->s_flags & MS_RDONLY)
916 return -EROFS;
918 if (!capable(CAP_SYS_ADMIN))
919 return -EPERM;
921 vol_args = memdup_user(arg, sizeof(*vol_args));
922 if (IS_ERR(vol_args))
923 return PTR_ERR(vol_args);
925 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
927 mutex_lock(&root->fs_info->volume_mutex);
928 sizestr = vol_args->name;
929 devstr = strchr(sizestr, ':');
930 if (devstr) {
931 char *end;
932 sizestr = devstr + 1;
933 *devstr = '\0';
934 devstr = vol_args->name;
935 devid = simple_strtoull(devstr, &end, 10);
936 printk(KERN_INFO "resizing devid %llu\n",
937 (unsigned long long)devid);
939 device = btrfs_find_device(root, devid, NULL, NULL);
940 if (!device) {
941 printk(KERN_INFO "resizer unable to find device %llu\n",
942 (unsigned long long)devid);
943 ret = -EINVAL;
944 goto out_unlock;
946 if (!strcmp(sizestr, "max"))
947 new_size = device->bdev->bd_inode->i_size;
948 else {
949 if (sizestr[0] == '-') {
950 mod = -1;
951 sizestr++;
952 } else if (sizestr[0] == '+') {
953 mod = 1;
954 sizestr++;
956 new_size = memparse(sizestr, NULL);
957 if (new_size == 0) {
958 ret = -EINVAL;
959 goto out_unlock;
963 old_size = device->total_bytes;
965 if (mod < 0) {
966 if (new_size > old_size) {
967 ret = -EINVAL;
968 goto out_unlock;
970 new_size = old_size - new_size;
971 } else if (mod > 0) {
972 new_size = old_size + new_size;
975 if (new_size < 256 * 1024 * 1024) {
976 ret = -EINVAL;
977 goto out_unlock;
979 if (new_size > device->bdev->bd_inode->i_size) {
980 ret = -EFBIG;
981 goto out_unlock;
984 do_div(new_size, root->sectorsize);
985 new_size *= root->sectorsize;
987 printk(KERN_INFO "new size for %s is %llu\n",
988 device->name, (unsigned long long)new_size);
990 if (new_size > old_size) {
991 trans = btrfs_start_transaction(root, 0);
992 if (IS_ERR(trans)) {
993 ret = PTR_ERR(trans);
994 goto out_unlock;
996 ret = btrfs_grow_device(trans, device, new_size);
997 btrfs_commit_transaction(trans, root);
998 } else {
999 ret = btrfs_shrink_device(device, new_size);
1002 out_unlock:
1003 mutex_unlock(&root->fs_info->volume_mutex);
1004 kfree(vol_args);
1005 return ret;
1008 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1009 char *name,
1010 unsigned long fd,
1011 int subvol,
1012 u64 *transid,
1013 bool readonly)
1015 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1016 struct file *src_file;
1017 int namelen;
1018 int ret = 0;
1020 if (root->fs_info->sb->s_flags & MS_RDONLY)
1021 return -EROFS;
1023 namelen = strlen(name);
1024 if (strchr(name, '/')) {
1025 ret = -EINVAL;
1026 goto out;
1029 if (subvol) {
1030 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1031 NULL, transid, readonly);
1032 } else {
1033 struct inode *src_inode;
1034 src_file = fget(fd);
1035 if (!src_file) {
1036 ret = -EINVAL;
1037 goto out;
1040 src_inode = src_file->f_path.dentry->d_inode;
1041 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1042 printk(KERN_INFO "btrfs: Snapshot src from "
1043 "another FS\n");
1044 ret = -EINVAL;
1045 fput(src_file);
1046 goto out;
1048 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1049 BTRFS_I(src_inode)->root,
1050 transid, readonly);
1051 fput(src_file);
1053 out:
1054 return ret;
1057 static noinline int btrfs_ioctl_snap_create(struct file *file,
1058 void __user *arg, int subvol)
1060 struct btrfs_ioctl_vol_args *vol_args;
1061 int ret;
1063 vol_args = memdup_user(arg, sizeof(*vol_args));
1064 if (IS_ERR(vol_args))
1065 return PTR_ERR(vol_args);
1066 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1068 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1069 vol_args->fd, subvol,
1070 NULL, false);
1072 kfree(vol_args);
1073 return ret;
1076 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1077 void __user *arg, int subvol)
1079 struct btrfs_ioctl_vol_args_v2 *vol_args;
1080 int ret;
1081 u64 transid = 0;
1082 u64 *ptr = NULL;
1083 bool readonly = false;
1085 vol_args = memdup_user(arg, sizeof(*vol_args));
1086 if (IS_ERR(vol_args))
1087 return PTR_ERR(vol_args);
1088 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1090 if (vol_args->flags &
1091 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1092 ret = -EOPNOTSUPP;
1093 goto out;
1096 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1097 ptr = &transid;
1098 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1099 readonly = true;
1101 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1102 vol_args->fd, subvol,
1103 ptr, readonly);
1105 if (ret == 0 && ptr &&
1106 copy_to_user(arg +
1107 offsetof(struct btrfs_ioctl_vol_args_v2,
1108 transid), ptr, sizeof(*ptr)))
1109 ret = -EFAULT;
1110 out:
1111 kfree(vol_args);
1112 return ret;
1115 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1116 void __user *arg)
1118 struct inode *inode = fdentry(file)->d_inode;
1119 struct btrfs_root *root = BTRFS_I(inode)->root;
1120 int ret = 0;
1121 u64 flags = 0;
1123 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1124 return -EINVAL;
1126 down_read(&root->fs_info->subvol_sem);
1127 if (btrfs_root_readonly(root))
1128 flags |= BTRFS_SUBVOL_RDONLY;
1129 up_read(&root->fs_info->subvol_sem);
1131 if (copy_to_user(arg, &flags, sizeof(flags)))
1132 ret = -EFAULT;
1134 return ret;
1137 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1138 void __user *arg)
1140 struct inode *inode = fdentry(file)->d_inode;
1141 struct btrfs_root *root = BTRFS_I(inode)->root;
1142 struct btrfs_trans_handle *trans;
1143 u64 root_flags;
1144 u64 flags;
1145 int ret = 0;
1147 if (root->fs_info->sb->s_flags & MS_RDONLY)
1148 return -EROFS;
1150 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1151 return -EINVAL;
1153 if (copy_from_user(&flags, arg, sizeof(flags)))
1154 return -EFAULT;
1156 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
1157 return -EINVAL;
1159 if (flags & ~BTRFS_SUBVOL_RDONLY)
1160 return -EOPNOTSUPP;
1162 if (!is_owner_or_cap(inode))
1163 return -EACCES;
1165 down_write(&root->fs_info->subvol_sem);
1167 /* nothing to do */
1168 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1169 goto out;
1171 root_flags = btrfs_root_flags(&root->root_item);
1172 if (flags & BTRFS_SUBVOL_RDONLY)
1173 btrfs_set_root_flags(&root->root_item,
1174 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1175 else
1176 btrfs_set_root_flags(&root->root_item,
1177 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1179 trans = btrfs_start_transaction(root, 1);
1180 if (IS_ERR(trans)) {
1181 ret = PTR_ERR(trans);
1182 goto out_reset;
1185 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1186 &root->root_key, &root->root_item);
1188 btrfs_commit_transaction(trans, root);
1189 out_reset:
1190 if (ret)
1191 btrfs_set_root_flags(&root->root_item, root_flags);
1192 out:
1193 up_write(&root->fs_info->subvol_sem);
1194 return ret;
1198 * helper to check if the subvolume references other subvolumes
1200 static noinline int may_destroy_subvol(struct btrfs_root *root)
1202 struct btrfs_path *path;
1203 struct btrfs_key key;
1204 int ret;
1206 path = btrfs_alloc_path();
1207 if (!path)
1208 return -ENOMEM;
1210 key.objectid = root->root_key.objectid;
1211 key.type = BTRFS_ROOT_REF_KEY;
1212 key.offset = (u64)-1;
1214 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1215 &key, path, 0, 0);
1216 if (ret < 0)
1217 goto out;
1218 BUG_ON(ret == 0);
1220 ret = 0;
1221 if (path->slots[0] > 0) {
1222 path->slots[0]--;
1223 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1224 if (key.objectid == root->root_key.objectid &&
1225 key.type == BTRFS_ROOT_REF_KEY)
1226 ret = -ENOTEMPTY;
1228 out:
1229 btrfs_free_path(path);
1230 return ret;
1233 static noinline int key_in_sk(struct btrfs_key *key,
1234 struct btrfs_ioctl_search_key *sk)
1236 struct btrfs_key test;
1237 int ret;
1239 test.objectid = sk->min_objectid;
1240 test.type = sk->min_type;
1241 test.offset = sk->min_offset;
1243 ret = btrfs_comp_cpu_keys(key, &test);
1244 if (ret < 0)
1245 return 0;
1247 test.objectid = sk->max_objectid;
1248 test.type = sk->max_type;
1249 test.offset = sk->max_offset;
1251 ret = btrfs_comp_cpu_keys(key, &test);
1252 if (ret > 0)
1253 return 0;
1254 return 1;
1257 static noinline int copy_to_sk(struct btrfs_root *root,
1258 struct btrfs_path *path,
1259 struct btrfs_key *key,
1260 struct btrfs_ioctl_search_key *sk,
1261 char *buf,
1262 unsigned long *sk_offset,
1263 int *num_found)
1265 u64 found_transid;
1266 struct extent_buffer *leaf;
1267 struct btrfs_ioctl_search_header sh;
1268 unsigned long item_off;
1269 unsigned long item_len;
1270 int nritems;
1271 int i;
1272 int slot;
1273 int found = 0;
1274 int ret = 0;
1276 leaf = path->nodes[0];
1277 slot = path->slots[0];
1278 nritems = btrfs_header_nritems(leaf);
1280 if (btrfs_header_generation(leaf) > sk->max_transid) {
1281 i = nritems;
1282 goto advance_key;
1284 found_transid = btrfs_header_generation(leaf);
1286 for (i = slot; i < nritems; i++) {
1287 item_off = btrfs_item_ptr_offset(leaf, i);
1288 item_len = btrfs_item_size_nr(leaf, i);
1290 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1291 item_len = 0;
1293 if (sizeof(sh) + item_len + *sk_offset >
1294 BTRFS_SEARCH_ARGS_BUFSIZE) {
1295 ret = 1;
1296 goto overflow;
1299 btrfs_item_key_to_cpu(leaf, key, i);
1300 if (!key_in_sk(key, sk))
1301 continue;
1303 sh.objectid = key->objectid;
1304 sh.offset = key->offset;
1305 sh.type = key->type;
1306 sh.len = item_len;
1307 sh.transid = found_transid;
1309 /* copy search result header */
1310 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1311 *sk_offset += sizeof(sh);
1313 if (item_len) {
1314 char *p = buf + *sk_offset;
1315 /* copy the item */
1316 read_extent_buffer(leaf, p,
1317 item_off, item_len);
1318 *sk_offset += item_len;
1320 found++;
1322 if (*num_found >= sk->nr_items)
1323 break;
1325 advance_key:
1326 ret = 0;
1327 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1328 key->offset++;
1329 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1330 key->offset = 0;
1331 key->type++;
1332 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1333 key->offset = 0;
1334 key->type = 0;
1335 key->objectid++;
1336 } else
1337 ret = 1;
1338 overflow:
1339 *num_found += found;
1340 return ret;
1343 static noinline int search_ioctl(struct inode *inode,
1344 struct btrfs_ioctl_search_args *args)
1346 struct btrfs_root *root;
1347 struct btrfs_key key;
1348 struct btrfs_key max_key;
1349 struct btrfs_path *path;
1350 struct btrfs_ioctl_search_key *sk = &args->key;
1351 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1352 int ret;
1353 int num_found = 0;
1354 unsigned long sk_offset = 0;
1356 path = btrfs_alloc_path();
1357 if (!path)
1358 return -ENOMEM;
1360 if (sk->tree_id == 0) {
1361 /* search the root of the inode that was passed */
1362 root = BTRFS_I(inode)->root;
1363 } else {
1364 key.objectid = sk->tree_id;
1365 key.type = BTRFS_ROOT_ITEM_KEY;
1366 key.offset = (u64)-1;
1367 root = btrfs_read_fs_root_no_name(info, &key);
1368 if (IS_ERR(root)) {
1369 printk(KERN_ERR "could not find root %llu\n",
1370 sk->tree_id);
1371 btrfs_free_path(path);
1372 return -ENOENT;
1376 key.objectid = sk->min_objectid;
1377 key.type = sk->min_type;
1378 key.offset = sk->min_offset;
1380 max_key.objectid = sk->max_objectid;
1381 max_key.type = sk->max_type;
1382 max_key.offset = sk->max_offset;
1384 path->keep_locks = 1;
1386 while(1) {
1387 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1388 sk->min_transid);
1389 if (ret != 0) {
1390 if (ret > 0)
1391 ret = 0;
1392 goto err;
1394 ret = copy_to_sk(root, path, &key, sk, args->buf,
1395 &sk_offset, &num_found);
1396 btrfs_release_path(root, path);
1397 if (ret || num_found >= sk->nr_items)
1398 break;
1401 ret = 0;
1402 err:
1403 sk->nr_items = num_found;
1404 btrfs_free_path(path);
1405 return ret;
1408 static noinline int btrfs_ioctl_tree_search(struct file *file,
1409 void __user *argp)
1411 struct btrfs_ioctl_search_args *args;
1412 struct inode *inode;
1413 int ret;
1415 if (!capable(CAP_SYS_ADMIN))
1416 return -EPERM;
1418 args = memdup_user(argp, sizeof(*args));
1419 if (IS_ERR(args))
1420 return PTR_ERR(args);
1422 inode = fdentry(file)->d_inode;
1423 ret = search_ioctl(inode, args);
1424 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1425 ret = -EFAULT;
1426 kfree(args);
1427 return ret;
1431 * Search INODE_REFs to identify path name of 'dirid' directory
1432 * in a 'tree_id' tree. and sets path name to 'name'.
1434 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1435 u64 tree_id, u64 dirid, char *name)
1437 struct btrfs_root *root;
1438 struct btrfs_key key;
1439 char *ptr;
1440 int ret = -1;
1441 int slot;
1442 int len;
1443 int total_len = 0;
1444 struct btrfs_inode_ref *iref;
1445 struct extent_buffer *l;
1446 struct btrfs_path *path;
1448 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1449 name[0]='\0';
1450 return 0;
1453 path = btrfs_alloc_path();
1454 if (!path)
1455 return -ENOMEM;
1457 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1459 key.objectid = tree_id;
1460 key.type = BTRFS_ROOT_ITEM_KEY;
1461 key.offset = (u64)-1;
1462 root = btrfs_read_fs_root_no_name(info, &key);
1463 if (IS_ERR(root)) {
1464 printk(KERN_ERR "could not find root %llu\n", tree_id);
1465 ret = -ENOENT;
1466 goto out;
1469 key.objectid = dirid;
1470 key.type = BTRFS_INODE_REF_KEY;
1471 key.offset = (u64)-1;
1473 while(1) {
1474 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1475 if (ret < 0)
1476 goto out;
1478 l = path->nodes[0];
1479 slot = path->slots[0];
1480 if (ret > 0 && slot > 0)
1481 slot--;
1482 btrfs_item_key_to_cpu(l, &key, slot);
1484 if (ret > 0 && (key.objectid != dirid ||
1485 key.type != BTRFS_INODE_REF_KEY)) {
1486 ret = -ENOENT;
1487 goto out;
1490 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1491 len = btrfs_inode_ref_name_len(l, iref);
1492 ptr -= len + 1;
1493 total_len += len + 1;
1494 if (ptr < name)
1495 goto out;
1497 *(ptr + len) = '/';
1498 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1500 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1501 break;
1503 btrfs_release_path(root, path);
1504 key.objectid = key.offset;
1505 key.offset = (u64)-1;
1506 dirid = key.objectid;
1509 if (ptr < name)
1510 goto out;
1511 memcpy(name, ptr, total_len);
1512 name[total_len]='\0';
1513 ret = 0;
1514 out:
1515 btrfs_free_path(path);
1516 return ret;
1519 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1520 void __user *argp)
1522 struct btrfs_ioctl_ino_lookup_args *args;
1523 struct inode *inode;
1524 int ret;
1526 if (!capable(CAP_SYS_ADMIN))
1527 return -EPERM;
1529 args = memdup_user(argp, sizeof(*args));
1530 if (IS_ERR(args))
1531 return PTR_ERR(args);
1533 inode = fdentry(file)->d_inode;
1535 if (args->treeid == 0)
1536 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1538 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1539 args->treeid, args->objectid,
1540 args->name);
1542 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1543 ret = -EFAULT;
1545 kfree(args);
1546 return ret;
1549 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1550 void __user *arg)
1552 struct dentry *parent = fdentry(file);
1553 struct dentry *dentry;
1554 struct inode *dir = parent->d_inode;
1555 struct inode *inode;
1556 struct btrfs_root *root = BTRFS_I(dir)->root;
1557 struct btrfs_root *dest = NULL;
1558 struct btrfs_ioctl_vol_args *vol_args;
1559 struct btrfs_trans_handle *trans;
1560 int namelen;
1561 int ret;
1562 int err = 0;
1564 vol_args = memdup_user(arg, sizeof(*vol_args));
1565 if (IS_ERR(vol_args))
1566 return PTR_ERR(vol_args);
1568 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1569 namelen = strlen(vol_args->name);
1570 if (strchr(vol_args->name, '/') ||
1571 strncmp(vol_args->name, "..", namelen) == 0) {
1572 err = -EINVAL;
1573 goto out;
1576 err = mnt_want_write(file->f_path.mnt);
1577 if (err)
1578 goto out;
1580 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1581 dentry = lookup_one_len(vol_args->name, parent, namelen);
1582 if (IS_ERR(dentry)) {
1583 err = PTR_ERR(dentry);
1584 goto out_unlock_dir;
1587 if (!dentry->d_inode) {
1588 err = -ENOENT;
1589 goto out_dput;
1592 inode = dentry->d_inode;
1593 dest = BTRFS_I(inode)->root;
1594 if (!capable(CAP_SYS_ADMIN)){
1596 * Regular user. Only allow this with a special mount
1597 * option, when the user has write+exec access to the
1598 * subvol root, and when rmdir(2) would have been
1599 * allowed.
1601 * Note that this is _not_ check that the subvol is
1602 * empty or doesn't contain data that we wouldn't
1603 * otherwise be able to delete.
1605 * Users who want to delete empty subvols should try
1606 * rmdir(2).
1608 err = -EPERM;
1609 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1610 goto out_dput;
1613 * Do not allow deletion if the parent dir is the same
1614 * as the dir to be deleted. That means the ioctl
1615 * must be called on the dentry referencing the root
1616 * of the subvol, not a random directory contained
1617 * within it.
1619 err = -EINVAL;
1620 if (root == dest)
1621 goto out_dput;
1623 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1624 if (err)
1625 goto out_dput;
1627 /* check if subvolume may be deleted by a non-root user */
1628 err = btrfs_may_delete(dir, dentry, 1);
1629 if (err)
1630 goto out_dput;
1633 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1634 err = -EINVAL;
1635 goto out_dput;
1638 mutex_lock(&inode->i_mutex);
1639 err = d_invalidate(dentry);
1640 if (err)
1641 goto out_unlock;
1643 down_write(&root->fs_info->subvol_sem);
1645 err = may_destroy_subvol(dest);
1646 if (err)
1647 goto out_up_write;
1649 trans = btrfs_start_transaction(root, 0);
1650 if (IS_ERR(trans)) {
1651 err = PTR_ERR(trans);
1652 goto out_up_write;
1654 trans->block_rsv = &root->fs_info->global_block_rsv;
1656 ret = btrfs_unlink_subvol(trans, root, dir,
1657 dest->root_key.objectid,
1658 dentry->d_name.name,
1659 dentry->d_name.len);
1660 BUG_ON(ret);
1662 btrfs_record_root_in_trans(trans, dest);
1664 memset(&dest->root_item.drop_progress, 0,
1665 sizeof(dest->root_item.drop_progress));
1666 dest->root_item.drop_level = 0;
1667 btrfs_set_root_refs(&dest->root_item, 0);
1669 if (!xchg(&dest->orphan_item_inserted, 1)) {
1670 ret = btrfs_insert_orphan_item(trans,
1671 root->fs_info->tree_root,
1672 dest->root_key.objectid);
1673 BUG_ON(ret);
1676 ret = btrfs_end_transaction(trans, root);
1677 BUG_ON(ret);
1678 inode->i_flags |= S_DEAD;
1679 out_up_write:
1680 up_write(&root->fs_info->subvol_sem);
1681 out_unlock:
1682 mutex_unlock(&inode->i_mutex);
1683 if (!err) {
1684 shrink_dcache_sb(root->fs_info->sb);
1685 btrfs_invalidate_inodes(dest);
1686 d_delete(dentry);
1688 out_dput:
1689 dput(dentry);
1690 out_unlock_dir:
1691 mutex_unlock(&dir->i_mutex);
1692 mnt_drop_write(file->f_path.mnt);
1693 out:
1694 kfree(vol_args);
1695 return err;
1698 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1700 struct inode *inode = fdentry(file)->d_inode;
1701 struct btrfs_root *root = BTRFS_I(inode)->root;
1702 struct btrfs_ioctl_defrag_range_args *range;
1703 int ret;
1705 if (btrfs_root_readonly(root))
1706 return -EROFS;
1708 ret = mnt_want_write(file->f_path.mnt);
1709 if (ret)
1710 return ret;
1712 switch (inode->i_mode & S_IFMT) {
1713 case S_IFDIR:
1714 if (!capable(CAP_SYS_ADMIN)) {
1715 ret = -EPERM;
1716 goto out;
1718 ret = btrfs_defrag_root(root, 0);
1719 if (ret)
1720 goto out;
1721 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1722 break;
1723 case S_IFREG:
1724 if (!(file->f_mode & FMODE_WRITE)) {
1725 ret = -EINVAL;
1726 goto out;
1729 range = kzalloc(sizeof(*range), GFP_KERNEL);
1730 if (!range) {
1731 ret = -ENOMEM;
1732 goto out;
1735 if (argp) {
1736 if (copy_from_user(range, argp,
1737 sizeof(*range))) {
1738 ret = -EFAULT;
1739 kfree(range);
1740 goto out;
1742 /* compression requires us to start the IO */
1743 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1744 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1745 range->extent_thresh = (u32)-1;
1747 } else {
1748 /* the rest are all set to zero by kzalloc */
1749 range->len = (u64)-1;
1751 ret = btrfs_defrag_file(file, range);
1752 kfree(range);
1753 break;
1754 default:
1755 ret = -EINVAL;
1757 out:
1758 mnt_drop_write(file->f_path.mnt);
1759 return ret;
1762 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1764 struct btrfs_ioctl_vol_args *vol_args;
1765 int ret;
1767 if (!capable(CAP_SYS_ADMIN))
1768 return -EPERM;
1770 vol_args = memdup_user(arg, sizeof(*vol_args));
1771 if (IS_ERR(vol_args))
1772 return PTR_ERR(vol_args);
1774 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1775 ret = btrfs_init_new_device(root, vol_args->name);
1777 kfree(vol_args);
1778 return ret;
1781 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1783 struct btrfs_ioctl_vol_args *vol_args;
1784 int ret;
1786 if (!capable(CAP_SYS_ADMIN))
1787 return -EPERM;
1789 if (root->fs_info->sb->s_flags & MS_RDONLY)
1790 return -EROFS;
1792 vol_args = memdup_user(arg, sizeof(*vol_args));
1793 if (IS_ERR(vol_args))
1794 return PTR_ERR(vol_args);
1796 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1797 ret = btrfs_rm_device(root, vol_args->name);
1799 kfree(vol_args);
1800 return ret;
1803 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1804 u64 off, u64 olen, u64 destoff)
1806 struct inode *inode = fdentry(file)->d_inode;
1807 struct btrfs_root *root = BTRFS_I(inode)->root;
1808 struct file *src_file;
1809 struct inode *src;
1810 struct btrfs_trans_handle *trans;
1811 struct btrfs_path *path;
1812 struct extent_buffer *leaf;
1813 char *buf;
1814 struct btrfs_key key;
1815 u32 nritems;
1816 int slot;
1817 int ret;
1818 u64 len = olen;
1819 u64 bs = root->fs_info->sb->s_blocksize;
1820 u64 hint_byte;
1823 * TODO:
1824 * - split compressed inline extents. annoying: we need to
1825 * decompress into destination's address_space (the file offset
1826 * may change, so source mapping won't do), then recompress (or
1827 * otherwise reinsert) a subrange.
1828 * - allow ranges within the same file to be cloned (provided
1829 * they don't overlap)?
1832 /* the destination must be opened for writing */
1833 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1834 return -EINVAL;
1836 if (btrfs_root_readonly(root))
1837 return -EROFS;
1839 ret = mnt_want_write(file->f_path.mnt);
1840 if (ret)
1841 return ret;
1843 src_file = fget(srcfd);
1844 if (!src_file) {
1845 ret = -EBADF;
1846 goto out_drop_write;
1849 src = src_file->f_dentry->d_inode;
1851 ret = -EINVAL;
1852 if (src == inode)
1853 goto out_fput;
1855 /* the src must be open for reading */
1856 if (!(src_file->f_mode & FMODE_READ))
1857 goto out_fput;
1859 ret = -EISDIR;
1860 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1861 goto out_fput;
1863 ret = -EXDEV;
1864 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1865 goto out_fput;
1867 ret = -ENOMEM;
1868 buf = vmalloc(btrfs_level_size(root, 0));
1869 if (!buf)
1870 goto out_fput;
1872 path = btrfs_alloc_path();
1873 if (!path) {
1874 vfree(buf);
1875 goto out_fput;
1877 path->reada = 2;
1879 if (inode < src) {
1880 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1881 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
1882 } else {
1883 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1884 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1887 /* determine range to clone */
1888 ret = -EINVAL;
1889 if (off + len > src->i_size || off + len < off)
1890 goto out_unlock;
1891 if (len == 0)
1892 olen = len = src->i_size - off;
1893 /* if we extend to eof, continue to block boundary */
1894 if (off + len == src->i_size)
1895 len = ALIGN(src->i_size, bs) - off;
1897 /* verify the end result is block aligned */
1898 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1899 !IS_ALIGNED(destoff, bs))
1900 goto out_unlock;
1902 /* do any pending delalloc/csum calc on src, one way or
1903 another, and lock file content */
1904 while (1) {
1905 struct btrfs_ordered_extent *ordered;
1906 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1907 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1908 if (!ordered &&
1909 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1910 EXTENT_DELALLOC, 0, NULL))
1911 break;
1912 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1913 if (ordered)
1914 btrfs_put_ordered_extent(ordered);
1915 btrfs_wait_ordered_range(src, off, len);
1918 /* clone data */
1919 key.objectid = src->i_ino;
1920 key.type = BTRFS_EXTENT_DATA_KEY;
1921 key.offset = 0;
1923 while (1) {
1925 * note the key will change type as we walk through the
1926 * tree.
1928 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1929 if (ret < 0)
1930 goto out;
1932 nritems = btrfs_header_nritems(path->nodes[0]);
1933 if (path->slots[0] >= nritems) {
1934 ret = btrfs_next_leaf(root, path);
1935 if (ret < 0)
1936 goto out;
1937 if (ret > 0)
1938 break;
1939 nritems = btrfs_header_nritems(path->nodes[0]);
1941 leaf = path->nodes[0];
1942 slot = path->slots[0];
1944 btrfs_item_key_to_cpu(leaf, &key, slot);
1945 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1946 key.objectid != src->i_ino)
1947 break;
1949 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1950 struct btrfs_file_extent_item *extent;
1951 int type;
1952 u32 size;
1953 struct btrfs_key new_key;
1954 u64 disko = 0, diskl = 0;
1955 u64 datao = 0, datal = 0;
1956 u8 comp;
1957 u64 endoff;
1959 size = btrfs_item_size_nr(leaf, slot);
1960 read_extent_buffer(leaf, buf,
1961 btrfs_item_ptr_offset(leaf, slot),
1962 size);
1964 extent = btrfs_item_ptr(leaf, slot,
1965 struct btrfs_file_extent_item);
1966 comp = btrfs_file_extent_compression(leaf, extent);
1967 type = btrfs_file_extent_type(leaf, extent);
1968 if (type == BTRFS_FILE_EXTENT_REG ||
1969 type == BTRFS_FILE_EXTENT_PREALLOC) {
1970 disko = btrfs_file_extent_disk_bytenr(leaf,
1971 extent);
1972 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1973 extent);
1974 datao = btrfs_file_extent_offset(leaf, extent);
1975 datal = btrfs_file_extent_num_bytes(leaf,
1976 extent);
1977 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1978 /* take upper bound, may be compressed */
1979 datal = btrfs_file_extent_ram_bytes(leaf,
1980 extent);
1982 btrfs_release_path(root, path);
1984 if (key.offset + datal <= off ||
1985 key.offset >= off+len)
1986 goto next;
1988 memcpy(&new_key, &key, sizeof(new_key));
1989 new_key.objectid = inode->i_ino;
1990 if (off <= key.offset)
1991 new_key.offset = key.offset + destoff - off;
1992 else
1993 new_key.offset = destoff;
1995 trans = btrfs_start_transaction(root, 1);
1996 if (IS_ERR(trans)) {
1997 ret = PTR_ERR(trans);
1998 goto out;
2001 if (type == BTRFS_FILE_EXTENT_REG ||
2002 type == BTRFS_FILE_EXTENT_PREALLOC) {
2003 if (off > key.offset) {
2004 datao += off - key.offset;
2005 datal -= off - key.offset;
2008 if (key.offset + datal > off + len)
2009 datal = off + len - key.offset;
2011 ret = btrfs_drop_extents(trans, inode,
2012 new_key.offset,
2013 new_key.offset + datal,
2014 &hint_byte, 1);
2015 BUG_ON(ret);
2017 ret = btrfs_insert_empty_item(trans, root, path,
2018 &new_key, size);
2019 BUG_ON(ret);
2021 leaf = path->nodes[0];
2022 slot = path->slots[0];
2023 write_extent_buffer(leaf, buf,
2024 btrfs_item_ptr_offset(leaf, slot),
2025 size);
2027 extent = btrfs_item_ptr(leaf, slot,
2028 struct btrfs_file_extent_item);
2030 /* disko == 0 means it's a hole */
2031 if (!disko)
2032 datao = 0;
2034 btrfs_set_file_extent_offset(leaf, extent,
2035 datao);
2036 btrfs_set_file_extent_num_bytes(leaf, extent,
2037 datal);
2038 if (disko) {
2039 inode_add_bytes(inode, datal);
2040 ret = btrfs_inc_extent_ref(trans, root,
2041 disko, diskl, 0,
2042 root->root_key.objectid,
2043 inode->i_ino,
2044 new_key.offset - datao);
2045 BUG_ON(ret);
2047 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2048 u64 skip = 0;
2049 u64 trim = 0;
2050 if (off > key.offset) {
2051 skip = off - key.offset;
2052 new_key.offset += skip;
2055 if (key.offset + datal > off+len)
2056 trim = key.offset + datal - (off+len);
2058 if (comp && (skip || trim)) {
2059 ret = -EINVAL;
2060 btrfs_end_transaction(trans, root);
2061 goto out;
2063 size -= skip + trim;
2064 datal -= skip + trim;
2066 ret = btrfs_drop_extents(trans, inode,
2067 new_key.offset,
2068 new_key.offset + datal,
2069 &hint_byte, 1);
2070 BUG_ON(ret);
2072 ret = btrfs_insert_empty_item(trans, root, path,
2073 &new_key, size);
2074 BUG_ON(ret);
2076 if (skip) {
2077 u32 start =
2078 btrfs_file_extent_calc_inline_size(0);
2079 memmove(buf+start, buf+start+skip,
2080 datal);
2083 leaf = path->nodes[0];
2084 slot = path->slots[0];
2085 write_extent_buffer(leaf, buf,
2086 btrfs_item_ptr_offset(leaf, slot),
2087 size);
2088 inode_add_bytes(inode, datal);
2091 btrfs_mark_buffer_dirty(leaf);
2092 btrfs_release_path(root, path);
2094 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2097 * we round up to the block size at eof when
2098 * determining which extents to clone above,
2099 * but shouldn't round up the file size
2101 endoff = new_key.offset + datal;
2102 if (endoff > destoff+olen)
2103 endoff = destoff+olen;
2104 if (endoff > inode->i_size)
2105 btrfs_i_size_write(inode, endoff);
2107 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2108 ret = btrfs_update_inode(trans, root, inode);
2109 BUG_ON(ret);
2110 btrfs_end_transaction(trans, root);
2112 next:
2113 btrfs_release_path(root, path);
2114 key.offset++;
2116 ret = 0;
2117 out:
2118 btrfs_release_path(root, path);
2119 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2120 out_unlock:
2121 mutex_unlock(&src->i_mutex);
2122 mutex_unlock(&inode->i_mutex);
2123 vfree(buf);
2124 btrfs_free_path(path);
2125 out_fput:
2126 fput(src_file);
2127 out_drop_write:
2128 mnt_drop_write(file->f_path.mnt);
2129 return ret;
2132 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2134 struct btrfs_ioctl_clone_range_args args;
2136 if (copy_from_user(&args, argp, sizeof(args)))
2137 return -EFAULT;
2138 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2139 args.src_length, args.dest_offset);
2143 * there are many ways the trans_start and trans_end ioctls can lead
2144 * to deadlocks. They should only be used by applications that
2145 * basically own the machine, and have a very in depth understanding
2146 * of all the possible deadlocks and enospc problems.
2148 static long btrfs_ioctl_trans_start(struct file *file)
2150 struct inode *inode = fdentry(file)->d_inode;
2151 struct btrfs_root *root = BTRFS_I(inode)->root;
2152 struct btrfs_trans_handle *trans;
2153 int ret;
2155 ret = -EPERM;
2156 if (!capable(CAP_SYS_ADMIN))
2157 goto out;
2159 ret = -EINPROGRESS;
2160 if (file->private_data)
2161 goto out;
2163 ret = -EROFS;
2164 if (btrfs_root_readonly(root))
2165 goto out;
2167 ret = mnt_want_write(file->f_path.mnt);
2168 if (ret)
2169 goto out;
2171 mutex_lock(&root->fs_info->trans_mutex);
2172 root->fs_info->open_ioctl_trans++;
2173 mutex_unlock(&root->fs_info->trans_mutex);
2175 ret = -ENOMEM;
2176 trans = btrfs_start_ioctl_transaction(root, 0);
2177 if (IS_ERR(trans))
2178 goto out_drop;
2180 file->private_data = trans;
2181 return 0;
2183 out_drop:
2184 mutex_lock(&root->fs_info->trans_mutex);
2185 root->fs_info->open_ioctl_trans--;
2186 mutex_unlock(&root->fs_info->trans_mutex);
2187 mnt_drop_write(file->f_path.mnt);
2188 out:
2189 return ret;
2192 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2194 struct inode *inode = fdentry(file)->d_inode;
2195 struct btrfs_root *root = BTRFS_I(inode)->root;
2196 struct btrfs_root *new_root;
2197 struct btrfs_dir_item *di;
2198 struct btrfs_trans_handle *trans;
2199 struct btrfs_path *path;
2200 struct btrfs_key location;
2201 struct btrfs_disk_key disk_key;
2202 struct btrfs_super_block *disk_super;
2203 u64 features;
2204 u64 objectid = 0;
2205 u64 dir_id;
2207 if (!capable(CAP_SYS_ADMIN))
2208 return -EPERM;
2210 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2211 return -EFAULT;
2213 if (!objectid)
2214 objectid = root->root_key.objectid;
2216 location.objectid = objectid;
2217 location.type = BTRFS_ROOT_ITEM_KEY;
2218 location.offset = (u64)-1;
2220 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2221 if (IS_ERR(new_root))
2222 return PTR_ERR(new_root);
2224 if (btrfs_root_refs(&new_root->root_item) == 0)
2225 return -ENOENT;
2227 path = btrfs_alloc_path();
2228 if (!path)
2229 return -ENOMEM;
2230 path->leave_spinning = 1;
2232 trans = btrfs_start_transaction(root, 1);
2233 if (IS_ERR(trans)) {
2234 btrfs_free_path(path);
2235 return PTR_ERR(trans);
2238 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2239 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2240 dir_id, "default", 7, 1);
2241 if (IS_ERR_OR_NULL(di)) {
2242 btrfs_free_path(path);
2243 btrfs_end_transaction(trans, root);
2244 printk(KERN_ERR "Umm, you don't have the default dir item, "
2245 "this isn't going to work\n");
2246 return -ENOENT;
2249 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2250 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2251 btrfs_mark_buffer_dirty(path->nodes[0]);
2252 btrfs_free_path(path);
2254 disk_super = &root->fs_info->super_copy;
2255 features = btrfs_super_incompat_flags(disk_super);
2256 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2257 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2258 btrfs_set_super_incompat_flags(disk_super, features);
2260 btrfs_end_transaction(trans, root);
2262 return 0;
2265 static void get_block_group_info(struct list_head *groups_list,
2266 struct btrfs_ioctl_space_info *space)
2268 struct btrfs_block_group_cache *block_group;
2270 space->total_bytes = 0;
2271 space->used_bytes = 0;
2272 space->flags = 0;
2273 list_for_each_entry(block_group, groups_list, list) {
2274 space->flags = block_group->flags;
2275 space->total_bytes += block_group->key.offset;
2276 space->used_bytes +=
2277 btrfs_block_group_used(&block_group->item);
2281 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2283 struct btrfs_ioctl_space_args space_args;
2284 struct btrfs_ioctl_space_info space;
2285 struct btrfs_ioctl_space_info *dest;
2286 struct btrfs_ioctl_space_info *dest_orig;
2287 struct btrfs_ioctl_space_info __user *user_dest;
2288 struct btrfs_space_info *info;
2289 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2290 BTRFS_BLOCK_GROUP_SYSTEM,
2291 BTRFS_BLOCK_GROUP_METADATA,
2292 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2293 int num_types = 4;
2294 int alloc_size;
2295 int ret = 0;
2296 u64 slot_count = 0;
2297 int i, c;
2299 if (copy_from_user(&space_args,
2300 (struct btrfs_ioctl_space_args __user *)arg,
2301 sizeof(space_args)))
2302 return -EFAULT;
2304 for (i = 0; i < num_types; i++) {
2305 struct btrfs_space_info *tmp;
2307 info = NULL;
2308 rcu_read_lock();
2309 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2310 list) {
2311 if (tmp->flags == types[i]) {
2312 info = tmp;
2313 break;
2316 rcu_read_unlock();
2318 if (!info)
2319 continue;
2321 down_read(&info->groups_sem);
2322 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2323 if (!list_empty(&info->block_groups[c]))
2324 slot_count++;
2326 up_read(&info->groups_sem);
2329 /* space_slots == 0 means they are asking for a count */
2330 if (space_args.space_slots == 0) {
2331 space_args.total_spaces = slot_count;
2332 goto out;
2335 slot_count = min_t(u64, space_args.space_slots, slot_count);
2337 alloc_size = sizeof(*dest) * slot_count;
2339 /* we generally have at most 6 or so space infos, one for each raid
2340 * level. So, a whole page should be more than enough for everyone
2342 if (alloc_size > PAGE_CACHE_SIZE)
2343 return -ENOMEM;
2345 space_args.total_spaces = 0;
2346 dest = kmalloc(alloc_size, GFP_NOFS);
2347 if (!dest)
2348 return -ENOMEM;
2349 dest_orig = dest;
2351 /* now we have a buffer to copy into */
2352 for (i = 0; i < num_types; i++) {
2353 struct btrfs_space_info *tmp;
2355 if (!slot_count)
2356 break;
2358 info = NULL;
2359 rcu_read_lock();
2360 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2361 list) {
2362 if (tmp->flags == types[i]) {
2363 info = tmp;
2364 break;
2367 rcu_read_unlock();
2369 if (!info)
2370 continue;
2371 down_read(&info->groups_sem);
2372 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2373 if (!list_empty(&info->block_groups[c])) {
2374 get_block_group_info(&info->block_groups[c],
2375 &space);
2376 memcpy(dest, &space, sizeof(space));
2377 dest++;
2378 space_args.total_spaces++;
2379 slot_count--;
2381 if (!slot_count)
2382 break;
2384 up_read(&info->groups_sem);
2387 user_dest = (struct btrfs_ioctl_space_info *)
2388 (arg + sizeof(struct btrfs_ioctl_space_args));
2390 if (copy_to_user(user_dest, dest_orig, alloc_size))
2391 ret = -EFAULT;
2393 kfree(dest_orig);
2394 out:
2395 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2396 ret = -EFAULT;
2398 return ret;
2402 * there are many ways the trans_start and trans_end ioctls can lead
2403 * to deadlocks. They should only be used by applications that
2404 * basically own the machine, and have a very in depth understanding
2405 * of all the possible deadlocks and enospc problems.
2407 long btrfs_ioctl_trans_end(struct file *file)
2409 struct inode *inode = fdentry(file)->d_inode;
2410 struct btrfs_root *root = BTRFS_I(inode)->root;
2411 struct btrfs_trans_handle *trans;
2413 trans = file->private_data;
2414 if (!trans)
2415 return -EINVAL;
2416 file->private_data = NULL;
2418 btrfs_end_transaction(trans, root);
2420 mutex_lock(&root->fs_info->trans_mutex);
2421 root->fs_info->open_ioctl_trans--;
2422 mutex_unlock(&root->fs_info->trans_mutex);
2424 mnt_drop_write(file->f_path.mnt);
2425 return 0;
2428 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2430 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2431 struct btrfs_trans_handle *trans;
2432 u64 transid;
2433 int ret;
2435 trans = btrfs_start_transaction(root, 0);
2436 if (IS_ERR(trans))
2437 return PTR_ERR(trans);
2438 transid = trans->transid;
2439 ret = btrfs_commit_transaction_async(trans, root, 0);
2440 if (ret) {
2441 btrfs_end_transaction(trans, root);
2442 return ret;
2445 if (argp)
2446 if (copy_to_user(argp, &transid, sizeof(transid)))
2447 return -EFAULT;
2448 return 0;
2451 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2453 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2454 u64 transid;
2456 if (argp) {
2457 if (copy_from_user(&transid, argp, sizeof(transid)))
2458 return -EFAULT;
2459 } else {
2460 transid = 0; /* current trans */
2462 return btrfs_wait_for_commit(root, transid);
2465 long btrfs_ioctl(struct file *file, unsigned int
2466 cmd, unsigned long arg)
2468 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2469 void __user *argp = (void __user *)arg;
2471 switch (cmd) {
2472 case FS_IOC_GETFLAGS:
2473 return btrfs_ioctl_getflags(file, argp);
2474 case FS_IOC_SETFLAGS:
2475 return btrfs_ioctl_setflags(file, argp);
2476 case FS_IOC_GETVERSION:
2477 return btrfs_ioctl_getversion(file, argp);
2478 case FITRIM:
2479 return btrfs_ioctl_fitrim(file, argp);
2480 case BTRFS_IOC_SNAP_CREATE:
2481 return btrfs_ioctl_snap_create(file, argp, 0);
2482 case BTRFS_IOC_SNAP_CREATE_V2:
2483 return btrfs_ioctl_snap_create_v2(file, argp, 0);
2484 case BTRFS_IOC_SUBVOL_CREATE:
2485 return btrfs_ioctl_snap_create(file, argp, 1);
2486 case BTRFS_IOC_SNAP_DESTROY:
2487 return btrfs_ioctl_snap_destroy(file, argp);
2488 case BTRFS_IOC_SUBVOL_GETFLAGS:
2489 return btrfs_ioctl_subvol_getflags(file, argp);
2490 case BTRFS_IOC_SUBVOL_SETFLAGS:
2491 return btrfs_ioctl_subvol_setflags(file, argp);
2492 case BTRFS_IOC_DEFAULT_SUBVOL:
2493 return btrfs_ioctl_default_subvol(file, argp);
2494 case BTRFS_IOC_DEFRAG:
2495 return btrfs_ioctl_defrag(file, NULL);
2496 case BTRFS_IOC_DEFRAG_RANGE:
2497 return btrfs_ioctl_defrag(file, argp);
2498 case BTRFS_IOC_RESIZE:
2499 return btrfs_ioctl_resize(root, argp);
2500 case BTRFS_IOC_ADD_DEV:
2501 return btrfs_ioctl_add_dev(root, argp);
2502 case BTRFS_IOC_RM_DEV:
2503 return btrfs_ioctl_rm_dev(root, argp);
2504 case BTRFS_IOC_BALANCE:
2505 return btrfs_balance(root->fs_info->dev_root);
2506 case BTRFS_IOC_CLONE:
2507 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2508 case BTRFS_IOC_CLONE_RANGE:
2509 return btrfs_ioctl_clone_range(file, argp);
2510 case BTRFS_IOC_TRANS_START:
2511 return btrfs_ioctl_trans_start(file);
2512 case BTRFS_IOC_TRANS_END:
2513 return btrfs_ioctl_trans_end(file);
2514 case BTRFS_IOC_TREE_SEARCH:
2515 return btrfs_ioctl_tree_search(file, argp);
2516 case BTRFS_IOC_INO_LOOKUP:
2517 return btrfs_ioctl_ino_lookup(file, argp);
2518 case BTRFS_IOC_SPACE_INFO:
2519 return btrfs_ioctl_space_info(root, argp);
2520 case BTRFS_IOC_SYNC:
2521 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2522 return 0;
2523 case BTRFS_IOC_START_SYNC:
2524 return btrfs_ioctl_start_sync(file, argp);
2525 case BTRFS_IOC_WAIT_SYNC:
2526 return btrfs_ioctl_wait_sync(file, argp);
2529 return -ENOTTY;