2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/fsnotify.h>
25 #include <linux/pagemap.h>
26 #include <linux/highmem.h>
27 #include <linux/time.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mount.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/swap.h>
35 #include <linux/writeback.h>
36 #include <linux/statfs.h>
37 #include <linux/compat.h>
38 #include <linux/bit_spinlock.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/vmalloc.h>
42 #include <linux/slab.h>
46 #include "transaction.h"
47 #include "btrfs_inode.h"
49 #include "print-tree.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)
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
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)
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;
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)
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)))
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;
150 if (copy_from_user(&flags, arg, sizeof(flags)))
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL))
158 if (!is_owner_or_cap(inode))
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)) {
172 ret = mnt_want_write(file->f_path.mnt);
176 if (flags & FS_SYNC_FL)
177 ip->flags |= BTRFS_INODE_SYNC;
179 ip->flags &= ~BTRFS_INODE_SYNC;
180 if (flags & FS_IMMUTABLE_FL)
181 ip->flags |= BTRFS_INODE_IMMUTABLE;
183 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
184 if (flags & FS_APPEND_FL)
185 ip->flags |= BTRFS_INODE_APPEND;
187 ip->flags &= ~BTRFS_INODE_APPEND;
188 if (flags & FS_NODUMP_FL)
189 ip->flags |= BTRFS_INODE_NODUMP;
191 ip->flags &= ~BTRFS_INODE_NODUMP;
192 if (flags & FS_NOATIME_FL)
193 ip->flags |= BTRFS_INODE_NOATIME;
195 ip->flags &= ~BTRFS_INODE_NOATIME;
196 if (flags & FS_DIRSYNC_FL)
197 ip->flags |= BTRFS_INODE_DIRSYNC;
199 ip->flags &= ~BTRFS_INODE_DIRSYNC;
202 trans = btrfs_join_transaction(root, 1);
205 ret = btrfs_update_inode(trans, root, inode);
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);
214 mutex_unlock(&inode->i_mutex);
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,
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 inode *dir = dentry->d_parent->d_inode;
240 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
243 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
253 trans = btrfs_start_transaction(root, 6);
255 return PTR_ERR(trans);
257 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
258 0, objectid, NULL, 0, 0, 0);
264 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
265 btrfs_set_header_bytenr(leaf, leaf->start);
266 btrfs_set_header_generation(leaf, trans->transid);
267 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
268 btrfs_set_header_owner(leaf, objectid);
270 write_extent_buffer(leaf, root->fs_info->fsid,
271 (unsigned long)btrfs_header_fsid(leaf),
273 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
274 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
276 btrfs_mark_buffer_dirty(leaf);
278 inode_item = &root_item.inode;
279 memset(inode_item, 0, sizeof(*inode_item));
280 inode_item->generation = cpu_to_le64(1);
281 inode_item->size = cpu_to_le64(3);
282 inode_item->nlink = cpu_to_le32(1);
283 inode_item->nbytes = cpu_to_le64(root->leafsize);
284 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
286 btrfs_set_root_bytenr(&root_item, leaf->start);
287 btrfs_set_root_generation(&root_item, trans->transid);
288 btrfs_set_root_level(&root_item, 0);
289 btrfs_set_root_refs(&root_item, 1);
290 btrfs_set_root_used(&root_item, leaf->len);
291 btrfs_set_root_last_snapshot(&root_item, 0);
293 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
294 root_item.drop_level = 0;
296 btrfs_tree_unlock(leaf);
297 free_extent_buffer(leaf);
300 btrfs_set_root_dirid(&root_item, new_dirid);
302 key.objectid = objectid;
304 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
305 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
310 key.offset = (u64)-1;
311 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
312 BUG_ON(IS_ERR(new_root));
314 btrfs_record_root_in_trans(trans, new_root);
316 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
317 BTRFS_I(dir)->block_group);
319 * insert the directory item
321 ret = btrfs_set_inode_index(dir, &index);
324 ret = btrfs_insert_dir_item(trans, root,
325 name, namelen, dir->i_ino, &key,
326 BTRFS_FT_DIR, index);
330 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
331 ret = btrfs_update_inode(trans, root, dir);
334 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
335 objectid, root->root_key.objectid,
336 dir->i_ino, index, name, namelen);
340 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
343 *async_transid = trans->transid;
344 err = btrfs_commit_transaction_async(trans, root, 1);
346 err = btrfs_commit_transaction(trans, root);
353 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
354 char *name, int namelen, u64 *async_transid)
357 struct btrfs_pending_snapshot *pending_snapshot;
358 struct btrfs_trans_handle *trans;
364 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
365 if (!pending_snapshot)
368 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
369 pending_snapshot->dentry = dentry;
370 pending_snapshot->root = root;
372 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
374 ret = PTR_ERR(trans);
378 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
381 list_add(&pending_snapshot->list,
382 &trans->transaction->pending_snapshots);
384 *async_transid = trans->transid;
385 ret = btrfs_commit_transaction_async(trans,
386 root->fs_info->extent_root, 1);
388 ret = btrfs_commit_transaction(trans,
389 root->fs_info->extent_root);
393 ret = pending_snapshot->error;
397 btrfs_orphan_cleanup(pending_snapshot->snap);
399 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
401 ret = PTR_ERR(inode);
405 d_instantiate(dentry, inode);
408 kfree(pending_snapshot);
412 /* copy of may_create in fs/namei.c() */
413 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
419 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
423 * Create a new subvolume below @parent. This is largely modeled after
424 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
425 * inside this filesystem so it's quite a bit simpler.
427 static noinline int btrfs_mksubvol(struct path *parent,
428 char *name, int namelen,
429 struct btrfs_root *snap_src,
432 struct inode *dir = parent->dentry->d_inode;
433 struct dentry *dentry;
436 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
438 dentry = lookup_one_len(name, parent->dentry, namelen);
439 error = PTR_ERR(dentry);
447 error = mnt_want_write(parent->mnt);
451 error = btrfs_may_create(dir, dentry);
455 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
457 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
461 error = create_snapshot(snap_src, dentry,
462 name, namelen, async_transid);
464 error = create_subvol(BTRFS_I(dir)->root, dentry,
465 name, namelen, async_transid);
468 fsnotify_mkdir(dir, dentry);
470 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
472 mnt_drop_write(parent->mnt);
476 mutex_unlock(&dir->i_mutex);
480 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
481 int thresh, u64 *last_len, u64 *skip,
484 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
485 struct extent_map *em = NULL;
486 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
494 * make sure that once we start defragging and extent, we keep on
497 if (start < *defrag_end)
503 * hopefully we have this extent in the tree already, try without
504 * the full extent lock
506 read_lock(&em_tree->lock);
507 em = lookup_extent_mapping(em_tree, start, len);
508 read_unlock(&em_tree->lock);
511 /* get the big lock and read metadata off disk */
512 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
513 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
514 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
520 /* this will cover holes, and inline extents */
521 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
525 * we hit a real extent, if it is big don't bother defragging it again
527 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
531 * last_len ends up being a counter of how many bytes we've defragged.
532 * every time we choose not to defrag an extent, we reset *last_len
533 * so that the next tiny extent will force a defrag.
535 * The end result of this is that tiny extents before a single big
536 * extent will force at least part of that big extent to be defragged.
540 *defrag_end = extent_map_end(em);
543 *skip = extent_map_end(em);
551 static int btrfs_defrag_file(struct file *file,
552 struct btrfs_ioctl_defrag_range_args *range)
554 struct inode *inode = fdentry(file)->d_inode;
555 struct btrfs_root *root = BTRFS_I(inode)->root;
556 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
557 struct btrfs_ordered_extent *ordered;
559 unsigned long last_index;
560 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
561 unsigned long total_read = 0;
570 if (inode->i_size == 0)
573 if (range->start + range->len > range->start) {
574 last_index = min_t(u64, inode->i_size - 1,
575 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
577 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
580 i = range->start >> PAGE_CACHE_SHIFT;
581 while (i <= last_index) {
582 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
584 range->extent_thresh,
589 * the should_defrag function tells us how much to skip
590 * bump our counter by the suggested amount
592 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
593 i = max(i + 1, next);
597 if (total_read % ra_pages == 0) {
598 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
599 min(last_index, i + ra_pages - 1));
602 mutex_lock(&inode->i_mutex);
603 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
604 BTRFS_I(inode)->force_compress = 1;
606 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
610 if (inode->i_size == 0 ||
611 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
613 goto err_reservations;
616 page = grab_cache_page(inode->i_mapping, i);
619 goto err_reservations;
622 if (!PageUptodate(page)) {
623 btrfs_readpage(NULL, page);
625 if (!PageUptodate(page)) {
627 page_cache_release(page);
629 goto err_reservations;
633 if (page->mapping != inode->i_mapping) {
635 page_cache_release(page);
639 wait_on_page_writeback(page);
641 if (PageDirty(page)) {
642 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
646 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
647 page_end = page_start + PAGE_CACHE_SIZE - 1;
648 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
650 ordered = btrfs_lookup_ordered_extent(inode, page_start);
652 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
654 page_cache_release(page);
655 btrfs_start_ordered_extent(inode, ordered, 1);
656 btrfs_put_ordered_extent(ordered);
659 set_page_extent_mapped(page);
662 * this makes sure page_mkwrite is called on the
663 * page if it is dirtied again later
665 clear_page_dirty_for_io(page);
666 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
667 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
668 EXTENT_DO_ACCOUNTING, GFP_NOFS);
670 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
671 ClearPageChecked(page);
672 set_page_dirty(page);
673 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
677 page_cache_release(page);
678 mutex_unlock(&inode->i_mutex);
680 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
684 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
685 filemap_flush(inode->i_mapping);
687 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
688 /* the filemap_flush will queue IO into the worker threads, but
689 * we have to make sure the IO is actually started and that
690 * ordered extents get created before we return
692 atomic_inc(&root->fs_info->async_submit_draining);
693 while (atomic_read(&root->fs_info->nr_async_submits) ||
694 atomic_read(&root->fs_info->async_delalloc_pages)) {
695 wait_event(root->fs_info->async_submit_wait,
696 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
697 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
699 atomic_dec(&root->fs_info->async_submit_draining);
701 mutex_lock(&inode->i_mutex);
702 BTRFS_I(inode)->force_compress = 0;
703 mutex_unlock(&inode->i_mutex);
709 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
711 mutex_unlock(&inode->i_mutex);
715 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
721 struct btrfs_ioctl_vol_args *vol_args;
722 struct btrfs_trans_handle *trans;
723 struct btrfs_device *device = NULL;
729 if (root->fs_info->sb->s_flags & MS_RDONLY)
732 if (!capable(CAP_SYS_ADMIN))
735 vol_args = memdup_user(arg, sizeof(*vol_args));
736 if (IS_ERR(vol_args))
737 return PTR_ERR(vol_args);
739 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
741 mutex_lock(&root->fs_info->volume_mutex);
742 sizestr = vol_args->name;
743 devstr = strchr(sizestr, ':');
746 sizestr = devstr + 1;
748 devstr = vol_args->name;
749 devid = simple_strtoull(devstr, &end, 10);
750 printk(KERN_INFO "resizing devid %llu\n",
751 (unsigned long long)devid);
753 device = btrfs_find_device(root, devid, NULL, NULL);
755 printk(KERN_INFO "resizer unable to find device %llu\n",
756 (unsigned long long)devid);
760 if (!strcmp(sizestr, "max"))
761 new_size = device->bdev->bd_inode->i_size;
763 if (sizestr[0] == '-') {
766 } else if (sizestr[0] == '+') {
770 new_size = memparse(sizestr, NULL);
777 old_size = device->total_bytes;
780 if (new_size > old_size) {
784 new_size = old_size - new_size;
785 } else if (mod > 0) {
786 new_size = old_size + new_size;
789 if (new_size < 256 * 1024 * 1024) {
793 if (new_size > device->bdev->bd_inode->i_size) {
798 do_div(new_size, root->sectorsize);
799 new_size *= root->sectorsize;
801 printk(KERN_INFO "new size for %s is %llu\n",
802 device->name, (unsigned long long)new_size);
804 if (new_size > old_size) {
805 trans = btrfs_start_transaction(root, 0);
806 ret = btrfs_grow_device(trans, device, new_size);
807 btrfs_commit_transaction(trans, root);
809 ret = btrfs_shrink_device(device, new_size);
813 mutex_unlock(&root->fs_info->volume_mutex);
818 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
824 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
825 struct file *src_file;
829 if (root->fs_info->sb->s_flags & MS_RDONLY)
832 namelen = strlen(name);
833 if (strchr(name, '/')) {
839 ret = btrfs_mksubvol(&file->f_path, name, namelen,
842 struct inode *src_inode;
849 src_inode = src_file->f_path.dentry->d_inode;
850 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
851 printk(KERN_INFO "btrfs: Snapshot src from "
857 ret = btrfs_mksubvol(&file->f_path, name, namelen,
858 BTRFS_I(src_inode)->root,
866 static noinline int btrfs_ioctl_snap_create(struct file *file,
867 void __user *arg, int subvol,
870 struct btrfs_ioctl_vol_args *vol_args = NULL;
871 struct btrfs_ioctl_async_vol_args *async_vol_args = NULL;
878 async_vol_args = memdup_user(arg, sizeof(*async_vol_args));
879 if (IS_ERR(async_vol_args))
880 return PTR_ERR(async_vol_args);
882 name = async_vol_args->name;
883 fd = async_vol_args->fd;
884 async_vol_args->name[BTRFS_SNAPSHOT_NAME_MAX] = '\0';
886 vol_args = memdup_user(arg, sizeof(*vol_args));
887 if (IS_ERR(vol_args))
888 return PTR_ERR(vol_args);
889 name = vol_args->name;
891 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
894 ret = btrfs_ioctl_snap_create_transid(file, name, fd,
898 if (copy_to_user(arg +
899 offsetof(struct btrfs_ioctl_async_vol_args,
900 transid), &transid, sizeof(transid)))
905 kfree(async_vol_args);
911 * helper to check if the subvolume references other subvolumes
913 static noinline int may_destroy_subvol(struct btrfs_root *root)
915 struct btrfs_path *path;
916 struct btrfs_key key;
919 path = btrfs_alloc_path();
923 key.objectid = root->root_key.objectid;
924 key.type = BTRFS_ROOT_REF_KEY;
925 key.offset = (u64)-1;
927 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
934 if (path->slots[0] > 0) {
936 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
937 if (key.objectid == root->root_key.objectid &&
938 key.type == BTRFS_ROOT_REF_KEY)
942 btrfs_free_path(path);
946 static noinline int key_in_sk(struct btrfs_key *key,
947 struct btrfs_ioctl_search_key *sk)
949 struct btrfs_key test;
952 test.objectid = sk->min_objectid;
953 test.type = sk->min_type;
954 test.offset = sk->min_offset;
956 ret = btrfs_comp_cpu_keys(key, &test);
960 test.objectid = sk->max_objectid;
961 test.type = sk->max_type;
962 test.offset = sk->max_offset;
964 ret = btrfs_comp_cpu_keys(key, &test);
970 static noinline int copy_to_sk(struct btrfs_root *root,
971 struct btrfs_path *path,
972 struct btrfs_key *key,
973 struct btrfs_ioctl_search_key *sk,
975 unsigned long *sk_offset,
979 struct extent_buffer *leaf;
980 struct btrfs_ioctl_search_header sh;
981 unsigned long item_off;
982 unsigned long item_len;
989 leaf = path->nodes[0];
990 slot = path->slots[0];
991 nritems = btrfs_header_nritems(leaf);
993 if (btrfs_header_generation(leaf) > sk->max_transid) {
997 found_transid = btrfs_header_generation(leaf);
999 for (i = slot; i < nritems; i++) {
1000 item_off = btrfs_item_ptr_offset(leaf, i);
1001 item_len = btrfs_item_size_nr(leaf, i);
1003 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1006 if (sizeof(sh) + item_len + *sk_offset >
1007 BTRFS_SEARCH_ARGS_BUFSIZE) {
1012 btrfs_item_key_to_cpu(leaf, key, i);
1013 if (!key_in_sk(key, sk))
1016 sh.objectid = key->objectid;
1017 sh.offset = key->offset;
1018 sh.type = key->type;
1020 sh.transid = found_transid;
1022 /* copy search result header */
1023 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1024 *sk_offset += sizeof(sh);
1027 char *p = buf + *sk_offset;
1029 read_extent_buffer(leaf, p,
1030 item_off, item_len);
1031 *sk_offset += item_len;
1035 if (*num_found >= sk->nr_items)
1040 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1042 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1045 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1052 *num_found += found;
1056 static noinline int search_ioctl(struct inode *inode,
1057 struct btrfs_ioctl_search_args *args)
1059 struct btrfs_root *root;
1060 struct btrfs_key key;
1061 struct btrfs_key max_key;
1062 struct btrfs_path *path;
1063 struct btrfs_ioctl_search_key *sk = &args->key;
1064 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1067 unsigned long sk_offset = 0;
1069 path = btrfs_alloc_path();
1073 if (sk->tree_id == 0) {
1074 /* search the root of the inode that was passed */
1075 root = BTRFS_I(inode)->root;
1077 key.objectid = sk->tree_id;
1078 key.type = BTRFS_ROOT_ITEM_KEY;
1079 key.offset = (u64)-1;
1080 root = btrfs_read_fs_root_no_name(info, &key);
1082 printk(KERN_ERR "could not find root %llu\n",
1084 btrfs_free_path(path);
1089 key.objectid = sk->min_objectid;
1090 key.type = sk->min_type;
1091 key.offset = sk->min_offset;
1093 max_key.objectid = sk->max_objectid;
1094 max_key.type = sk->max_type;
1095 max_key.offset = sk->max_offset;
1097 path->keep_locks = 1;
1100 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1107 ret = copy_to_sk(root, path, &key, sk, args->buf,
1108 &sk_offset, &num_found);
1109 btrfs_release_path(root, path);
1110 if (ret || num_found >= sk->nr_items)
1116 sk->nr_items = num_found;
1117 btrfs_free_path(path);
1121 static noinline int btrfs_ioctl_tree_search(struct file *file,
1124 struct btrfs_ioctl_search_args *args;
1125 struct inode *inode;
1128 if (!capable(CAP_SYS_ADMIN))
1131 args = memdup_user(argp, sizeof(*args));
1133 return PTR_ERR(args);
1135 inode = fdentry(file)->d_inode;
1136 ret = search_ioctl(inode, args);
1137 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1144 * Search INODE_REFs to identify path name of 'dirid' directory
1145 * in a 'tree_id' tree. and sets path name to 'name'.
1147 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1148 u64 tree_id, u64 dirid, char *name)
1150 struct btrfs_root *root;
1151 struct btrfs_key key;
1157 struct btrfs_inode_ref *iref;
1158 struct extent_buffer *l;
1159 struct btrfs_path *path;
1161 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1166 path = btrfs_alloc_path();
1170 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1172 key.objectid = tree_id;
1173 key.type = BTRFS_ROOT_ITEM_KEY;
1174 key.offset = (u64)-1;
1175 root = btrfs_read_fs_root_no_name(info, &key);
1177 printk(KERN_ERR "could not find root %llu\n", tree_id);
1182 key.objectid = dirid;
1183 key.type = BTRFS_INODE_REF_KEY;
1184 key.offset = (u64)-1;
1187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1192 slot = path->slots[0];
1193 if (ret > 0 && slot > 0)
1195 btrfs_item_key_to_cpu(l, &key, slot);
1197 if (ret > 0 && (key.objectid != dirid ||
1198 key.type != BTRFS_INODE_REF_KEY)) {
1203 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1204 len = btrfs_inode_ref_name_len(l, iref);
1206 total_len += len + 1;
1211 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1213 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1216 btrfs_release_path(root, path);
1217 key.objectid = key.offset;
1218 key.offset = (u64)-1;
1219 dirid = key.objectid;
1224 memcpy(name, ptr, total_len);
1225 name[total_len]='\0';
1228 btrfs_free_path(path);
1232 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1235 struct btrfs_ioctl_ino_lookup_args *args;
1236 struct inode *inode;
1239 if (!capable(CAP_SYS_ADMIN))
1242 args = memdup_user(argp, sizeof(*args));
1244 return PTR_ERR(args);
1246 inode = fdentry(file)->d_inode;
1248 if (args->treeid == 0)
1249 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1251 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1252 args->treeid, args->objectid,
1255 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1262 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1265 struct dentry *parent = fdentry(file);
1266 struct dentry *dentry;
1267 struct inode *dir = parent->d_inode;
1268 struct inode *inode;
1269 struct btrfs_root *root = BTRFS_I(dir)->root;
1270 struct btrfs_root *dest = NULL;
1271 struct btrfs_ioctl_vol_args *vol_args;
1272 struct btrfs_trans_handle *trans;
1277 if (!capable(CAP_SYS_ADMIN))
1280 vol_args = memdup_user(arg, sizeof(*vol_args));
1281 if (IS_ERR(vol_args))
1282 return PTR_ERR(vol_args);
1284 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1285 namelen = strlen(vol_args->name);
1286 if (strchr(vol_args->name, '/') ||
1287 strncmp(vol_args->name, "..", namelen) == 0) {
1292 err = mnt_want_write(file->f_path.mnt);
1296 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1297 dentry = lookup_one_len(vol_args->name, parent, namelen);
1298 if (IS_ERR(dentry)) {
1299 err = PTR_ERR(dentry);
1300 goto out_unlock_dir;
1303 if (!dentry->d_inode) {
1308 inode = dentry->d_inode;
1309 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1314 dest = BTRFS_I(inode)->root;
1316 mutex_lock(&inode->i_mutex);
1317 err = d_invalidate(dentry);
1321 down_write(&root->fs_info->subvol_sem);
1323 err = may_destroy_subvol(dest);
1327 trans = btrfs_start_transaction(root, 0);
1328 if (IS_ERR(trans)) {
1329 err = PTR_ERR(trans);
1332 trans->block_rsv = &root->fs_info->global_block_rsv;
1334 ret = btrfs_unlink_subvol(trans, root, dir,
1335 dest->root_key.objectid,
1336 dentry->d_name.name,
1337 dentry->d_name.len);
1340 btrfs_record_root_in_trans(trans, dest);
1342 memset(&dest->root_item.drop_progress, 0,
1343 sizeof(dest->root_item.drop_progress));
1344 dest->root_item.drop_level = 0;
1345 btrfs_set_root_refs(&dest->root_item, 0);
1347 if (!xchg(&dest->orphan_item_inserted, 1)) {
1348 ret = btrfs_insert_orphan_item(trans,
1349 root->fs_info->tree_root,
1350 dest->root_key.objectid);
1354 ret = btrfs_end_transaction(trans, root);
1356 inode->i_flags |= S_DEAD;
1358 up_write(&root->fs_info->subvol_sem);
1360 mutex_unlock(&inode->i_mutex);
1362 shrink_dcache_sb(root->fs_info->sb);
1363 btrfs_invalidate_inodes(dest);
1369 mutex_unlock(&dir->i_mutex);
1370 mnt_drop_write(file->f_path.mnt);
1376 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1378 struct inode *inode = fdentry(file)->d_inode;
1379 struct btrfs_root *root = BTRFS_I(inode)->root;
1380 struct btrfs_ioctl_defrag_range_args *range;
1383 ret = mnt_want_write(file->f_path.mnt);
1387 switch (inode->i_mode & S_IFMT) {
1389 if (!capable(CAP_SYS_ADMIN)) {
1393 ret = btrfs_defrag_root(root, 0);
1396 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1399 if (!(file->f_mode & FMODE_WRITE)) {
1404 range = kzalloc(sizeof(*range), GFP_KERNEL);
1411 if (copy_from_user(range, argp,
1417 /* compression requires us to start the IO */
1418 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1419 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1420 range->extent_thresh = (u32)-1;
1423 /* the rest are all set to zero by kzalloc */
1424 range->len = (u64)-1;
1426 ret = btrfs_defrag_file(file, range);
1433 mnt_drop_write(file->f_path.mnt);
1437 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1439 struct btrfs_ioctl_vol_args *vol_args;
1442 if (!capable(CAP_SYS_ADMIN))
1445 vol_args = memdup_user(arg, sizeof(*vol_args));
1446 if (IS_ERR(vol_args))
1447 return PTR_ERR(vol_args);
1449 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1450 ret = btrfs_init_new_device(root, vol_args->name);
1456 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1458 struct btrfs_ioctl_vol_args *vol_args;
1461 if (!capable(CAP_SYS_ADMIN))
1464 if (root->fs_info->sb->s_flags & MS_RDONLY)
1467 vol_args = memdup_user(arg, sizeof(*vol_args));
1468 if (IS_ERR(vol_args))
1469 return PTR_ERR(vol_args);
1471 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1472 ret = btrfs_rm_device(root, vol_args->name);
1478 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1479 u64 off, u64 olen, u64 destoff)
1481 struct inode *inode = fdentry(file)->d_inode;
1482 struct btrfs_root *root = BTRFS_I(inode)->root;
1483 struct file *src_file;
1485 struct btrfs_trans_handle *trans;
1486 struct btrfs_path *path;
1487 struct extent_buffer *leaf;
1489 struct btrfs_key key;
1494 u64 bs = root->fs_info->sb->s_blocksize;
1499 * - split compressed inline extents. annoying: we need to
1500 * decompress into destination's address_space (the file offset
1501 * may change, so source mapping won't do), then recompress (or
1502 * otherwise reinsert) a subrange.
1503 * - allow ranges within the same file to be cloned (provided
1504 * they don't overlap)?
1507 /* the destination must be opened for writing */
1508 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1511 ret = mnt_want_write(file->f_path.mnt);
1515 src_file = fget(srcfd);
1518 goto out_drop_write;
1521 src = src_file->f_dentry->d_inode;
1527 /* the src must be open for reading */
1528 if (!(src_file->f_mode & FMODE_READ))
1532 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1536 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1540 buf = vmalloc(btrfs_level_size(root, 0));
1544 path = btrfs_alloc_path();
1552 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1553 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
1555 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1556 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1559 /* determine range to clone */
1561 if (off + len > src->i_size || off + len < off)
1564 olen = len = src->i_size - off;
1565 /* if we extend to eof, continue to block boundary */
1566 if (off + len == src->i_size)
1567 len = ((src->i_size + bs-1) & ~(bs-1))
1570 /* verify the end result is block aligned */
1571 if ((off & (bs-1)) ||
1572 ((off + len) & (bs-1)))
1575 /* do any pending delalloc/csum calc on src, one way or
1576 another, and lock file content */
1578 struct btrfs_ordered_extent *ordered;
1579 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1580 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1582 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1583 EXTENT_DELALLOC, 0, NULL))
1585 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1587 btrfs_put_ordered_extent(ordered);
1588 btrfs_wait_ordered_range(src, off, len);
1592 key.objectid = src->i_ino;
1593 key.type = BTRFS_EXTENT_DATA_KEY;
1598 * note the key will change type as we walk through the
1601 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1605 nritems = btrfs_header_nritems(path->nodes[0]);
1606 if (path->slots[0] >= nritems) {
1607 ret = btrfs_next_leaf(root, path);
1612 nritems = btrfs_header_nritems(path->nodes[0]);
1614 leaf = path->nodes[0];
1615 slot = path->slots[0];
1617 btrfs_item_key_to_cpu(leaf, &key, slot);
1618 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1619 key.objectid != src->i_ino)
1622 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1623 struct btrfs_file_extent_item *extent;
1626 struct btrfs_key new_key;
1627 u64 disko = 0, diskl = 0;
1628 u64 datao = 0, datal = 0;
1632 size = btrfs_item_size_nr(leaf, slot);
1633 read_extent_buffer(leaf, buf,
1634 btrfs_item_ptr_offset(leaf, slot),
1637 extent = btrfs_item_ptr(leaf, slot,
1638 struct btrfs_file_extent_item);
1639 comp = btrfs_file_extent_compression(leaf, extent);
1640 type = btrfs_file_extent_type(leaf, extent);
1641 if (type == BTRFS_FILE_EXTENT_REG ||
1642 type == BTRFS_FILE_EXTENT_PREALLOC) {
1643 disko = btrfs_file_extent_disk_bytenr(leaf,
1645 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1647 datao = btrfs_file_extent_offset(leaf, extent);
1648 datal = btrfs_file_extent_num_bytes(leaf,
1650 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1651 /* take upper bound, may be compressed */
1652 datal = btrfs_file_extent_ram_bytes(leaf,
1655 btrfs_release_path(root, path);
1657 if (key.offset + datal <= off ||
1658 key.offset >= off+len)
1661 memcpy(&new_key, &key, sizeof(new_key));
1662 new_key.objectid = inode->i_ino;
1663 new_key.offset = key.offset + destoff - off;
1665 trans = btrfs_start_transaction(root, 1);
1666 if (IS_ERR(trans)) {
1667 ret = PTR_ERR(trans);
1671 if (type == BTRFS_FILE_EXTENT_REG ||
1672 type == BTRFS_FILE_EXTENT_PREALLOC) {
1673 if (off > key.offset) {
1674 datao += off - key.offset;
1675 datal -= off - key.offset;
1678 if (key.offset + datal > off + len)
1679 datal = off + len - key.offset;
1681 ret = btrfs_drop_extents(trans, inode,
1683 new_key.offset + datal,
1687 ret = btrfs_insert_empty_item(trans, root, path,
1691 leaf = path->nodes[0];
1692 slot = path->slots[0];
1693 write_extent_buffer(leaf, buf,
1694 btrfs_item_ptr_offset(leaf, slot),
1697 extent = btrfs_item_ptr(leaf, slot,
1698 struct btrfs_file_extent_item);
1700 /* disko == 0 means it's a hole */
1704 btrfs_set_file_extent_offset(leaf, extent,
1706 btrfs_set_file_extent_num_bytes(leaf, extent,
1709 inode_add_bytes(inode, datal);
1710 ret = btrfs_inc_extent_ref(trans, root,
1712 root->root_key.objectid,
1714 new_key.offset - datao);
1717 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1720 if (off > key.offset) {
1721 skip = off - key.offset;
1722 new_key.offset += skip;
1725 if (key.offset + datal > off+len)
1726 trim = key.offset + datal - (off+len);
1728 if (comp && (skip || trim)) {
1730 btrfs_end_transaction(trans, root);
1733 size -= skip + trim;
1734 datal -= skip + trim;
1736 ret = btrfs_drop_extents(trans, inode,
1738 new_key.offset + datal,
1742 ret = btrfs_insert_empty_item(trans, root, path,
1748 btrfs_file_extent_calc_inline_size(0);
1749 memmove(buf+start, buf+start+skip,
1753 leaf = path->nodes[0];
1754 slot = path->slots[0];
1755 write_extent_buffer(leaf, buf,
1756 btrfs_item_ptr_offset(leaf, slot),
1758 inode_add_bytes(inode, datal);
1761 btrfs_mark_buffer_dirty(leaf);
1762 btrfs_release_path(root, path);
1764 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1767 * we round up to the block size at eof when
1768 * determining which extents to clone above,
1769 * but shouldn't round up the file size
1771 endoff = new_key.offset + datal;
1772 if (endoff > off+olen)
1774 if (endoff > inode->i_size)
1775 btrfs_i_size_write(inode, endoff);
1777 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1778 ret = btrfs_update_inode(trans, root, inode);
1780 btrfs_end_transaction(trans, root);
1783 btrfs_release_path(root, path);
1788 btrfs_release_path(root, path);
1789 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1791 mutex_unlock(&src->i_mutex);
1792 mutex_unlock(&inode->i_mutex);
1794 btrfs_free_path(path);
1798 mnt_drop_write(file->f_path.mnt);
1802 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1804 struct btrfs_ioctl_clone_range_args args;
1806 if (copy_from_user(&args, argp, sizeof(args)))
1808 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1809 args.src_length, args.dest_offset);
1813 * there are many ways the trans_start and trans_end ioctls can lead
1814 * to deadlocks. They should only be used by applications that
1815 * basically own the machine, and have a very in depth understanding
1816 * of all the possible deadlocks and enospc problems.
1818 static long btrfs_ioctl_trans_start(struct file *file)
1820 struct inode *inode = fdentry(file)->d_inode;
1821 struct btrfs_root *root = BTRFS_I(inode)->root;
1822 struct btrfs_trans_handle *trans;
1826 if (!capable(CAP_SYS_ADMIN))
1830 if (file->private_data)
1833 ret = mnt_want_write(file->f_path.mnt);
1837 mutex_lock(&root->fs_info->trans_mutex);
1838 root->fs_info->open_ioctl_trans++;
1839 mutex_unlock(&root->fs_info->trans_mutex);
1842 trans = btrfs_start_ioctl_transaction(root, 0);
1846 file->private_data = trans;
1850 mutex_lock(&root->fs_info->trans_mutex);
1851 root->fs_info->open_ioctl_trans--;
1852 mutex_unlock(&root->fs_info->trans_mutex);
1853 mnt_drop_write(file->f_path.mnt);
1858 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1860 struct inode *inode = fdentry(file)->d_inode;
1861 struct btrfs_root *root = BTRFS_I(inode)->root;
1862 struct btrfs_root *new_root;
1863 struct btrfs_dir_item *di;
1864 struct btrfs_trans_handle *trans;
1865 struct btrfs_path *path;
1866 struct btrfs_key location;
1867 struct btrfs_disk_key disk_key;
1868 struct btrfs_super_block *disk_super;
1873 if (!capable(CAP_SYS_ADMIN))
1876 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1880 objectid = root->root_key.objectid;
1882 location.objectid = objectid;
1883 location.type = BTRFS_ROOT_ITEM_KEY;
1884 location.offset = (u64)-1;
1886 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1887 if (IS_ERR(new_root))
1888 return PTR_ERR(new_root);
1890 if (btrfs_root_refs(&new_root->root_item) == 0)
1893 path = btrfs_alloc_path();
1896 path->leave_spinning = 1;
1898 trans = btrfs_start_transaction(root, 1);
1900 btrfs_free_path(path);
1904 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
1905 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
1906 dir_id, "default", 7, 1);
1907 if (IS_ERR_OR_NULL(di)) {
1908 btrfs_free_path(path);
1909 btrfs_end_transaction(trans, root);
1910 printk(KERN_ERR "Umm, you don't have the default dir item, "
1911 "this isn't going to work\n");
1915 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
1916 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
1917 btrfs_mark_buffer_dirty(path->nodes[0]);
1918 btrfs_free_path(path);
1920 disk_super = &root->fs_info->super_copy;
1921 features = btrfs_super_incompat_flags(disk_super);
1922 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
1923 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
1924 btrfs_set_super_incompat_flags(disk_super, features);
1926 btrfs_end_transaction(trans, root);
1931 static void get_block_group_info(struct list_head *groups_list,
1932 struct btrfs_ioctl_space_info *space)
1934 struct btrfs_block_group_cache *block_group;
1936 space->total_bytes = 0;
1937 space->used_bytes = 0;
1939 list_for_each_entry(block_group, groups_list, list) {
1940 space->flags = block_group->flags;
1941 space->total_bytes += block_group->key.offset;
1942 space->used_bytes +=
1943 btrfs_block_group_used(&block_group->item);
1947 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1949 struct btrfs_ioctl_space_args space_args;
1950 struct btrfs_ioctl_space_info space;
1951 struct btrfs_ioctl_space_info *dest;
1952 struct btrfs_ioctl_space_info *dest_orig;
1953 struct btrfs_ioctl_space_info *user_dest;
1954 struct btrfs_space_info *info;
1955 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
1956 BTRFS_BLOCK_GROUP_SYSTEM,
1957 BTRFS_BLOCK_GROUP_METADATA,
1958 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
1965 if (copy_from_user(&space_args,
1966 (struct btrfs_ioctl_space_args __user *)arg,
1967 sizeof(space_args)))
1970 for (i = 0; i < num_types; i++) {
1971 struct btrfs_space_info *tmp;
1975 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
1977 if (tmp->flags == types[i]) {
1987 down_read(&info->groups_sem);
1988 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
1989 if (!list_empty(&info->block_groups[c]))
1992 up_read(&info->groups_sem);
1995 /* space_slots == 0 means they are asking for a count */
1996 if (space_args.space_slots == 0) {
1997 space_args.total_spaces = slot_count;
2001 slot_count = min_t(int, space_args.space_slots, slot_count);
2003 alloc_size = sizeof(*dest) * slot_count;
2005 /* we generally have at most 6 or so space infos, one for each raid
2006 * level. So, a whole page should be more than enough for everyone
2008 if (alloc_size > PAGE_CACHE_SIZE)
2011 space_args.total_spaces = 0;
2012 dest = kmalloc(alloc_size, GFP_NOFS);
2017 /* now we have a buffer to copy into */
2018 for (i = 0; i < num_types; i++) {
2019 struct btrfs_space_info *tmp;
2023 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2025 if (tmp->flags == types[i]) {
2034 down_read(&info->groups_sem);
2035 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2036 if (!list_empty(&info->block_groups[c])) {
2037 get_block_group_info(&info->block_groups[c],
2039 memcpy(dest, &space, sizeof(space));
2041 space_args.total_spaces++;
2044 up_read(&info->groups_sem);
2047 user_dest = (struct btrfs_ioctl_space_info *)
2048 (arg + sizeof(struct btrfs_ioctl_space_args));
2050 if (copy_to_user(user_dest, dest_orig, alloc_size))
2055 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2062 * there are many ways the trans_start and trans_end ioctls can lead
2063 * to deadlocks. They should only be used by applications that
2064 * basically own the machine, and have a very in depth understanding
2065 * of all the possible deadlocks and enospc problems.
2067 long btrfs_ioctl_trans_end(struct file *file)
2069 struct inode *inode = fdentry(file)->d_inode;
2070 struct btrfs_root *root = BTRFS_I(inode)->root;
2071 struct btrfs_trans_handle *trans;
2073 trans = file->private_data;
2076 file->private_data = NULL;
2078 btrfs_end_transaction(trans, root);
2080 mutex_lock(&root->fs_info->trans_mutex);
2081 root->fs_info->open_ioctl_trans--;
2082 mutex_unlock(&root->fs_info->trans_mutex);
2084 mnt_drop_write(file->f_path.mnt);
2088 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2090 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2091 struct btrfs_trans_handle *trans;
2094 trans = btrfs_start_transaction(root, 0);
2095 transid = trans->transid;
2096 btrfs_commit_transaction_async(trans, root, 0);
2099 if (copy_to_user(argp, &transid, sizeof(transid)))
2104 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2106 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2110 if (copy_from_user(&transid, argp, sizeof(transid)))
2113 transid = 0; /* current trans */
2115 return btrfs_wait_for_commit(root, transid);
2118 long btrfs_ioctl(struct file *file, unsigned int
2119 cmd, unsigned long arg)
2121 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2122 void __user *argp = (void __user *)arg;
2125 case FS_IOC_GETFLAGS:
2126 return btrfs_ioctl_getflags(file, argp);
2127 case FS_IOC_SETFLAGS:
2128 return btrfs_ioctl_setflags(file, argp);
2129 case FS_IOC_GETVERSION:
2130 return btrfs_ioctl_getversion(file, argp);
2131 case BTRFS_IOC_SNAP_CREATE:
2132 return btrfs_ioctl_snap_create(file, argp, 0, 0);
2133 case BTRFS_IOC_SNAP_CREATE_ASYNC:
2134 return btrfs_ioctl_snap_create(file, argp, 0, 1);
2135 case BTRFS_IOC_SUBVOL_CREATE:
2136 return btrfs_ioctl_snap_create(file, argp, 1, 0);
2137 case BTRFS_IOC_SNAP_DESTROY:
2138 return btrfs_ioctl_snap_destroy(file, argp);
2139 case BTRFS_IOC_DEFAULT_SUBVOL:
2140 return btrfs_ioctl_default_subvol(file, argp);
2141 case BTRFS_IOC_DEFRAG:
2142 return btrfs_ioctl_defrag(file, NULL);
2143 case BTRFS_IOC_DEFRAG_RANGE:
2144 return btrfs_ioctl_defrag(file, argp);
2145 case BTRFS_IOC_RESIZE:
2146 return btrfs_ioctl_resize(root, argp);
2147 case BTRFS_IOC_ADD_DEV:
2148 return btrfs_ioctl_add_dev(root, argp);
2149 case BTRFS_IOC_RM_DEV:
2150 return btrfs_ioctl_rm_dev(root, argp);
2151 case BTRFS_IOC_BALANCE:
2152 return btrfs_balance(root->fs_info->dev_root);
2153 case BTRFS_IOC_CLONE:
2154 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2155 case BTRFS_IOC_CLONE_RANGE:
2156 return btrfs_ioctl_clone_range(file, argp);
2157 case BTRFS_IOC_TRANS_START:
2158 return btrfs_ioctl_trans_start(file);
2159 case BTRFS_IOC_TRANS_END:
2160 return btrfs_ioctl_trans_end(file);
2161 case BTRFS_IOC_TREE_SEARCH:
2162 return btrfs_ioctl_tree_search(file, argp);
2163 case BTRFS_IOC_INO_LOOKUP:
2164 return btrfs_ioctl_ino_lookup(file, argp);
2165 case BTRFS_IOC_SPACE_INFO:
2166 return btrfs_ioctl_space_info(root, argp);
2167 case BTRFS_IOC_SYNC:
2168 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2170 case BTRFS_IOC_START_SYNC:
2171 return btrfs_ioctl_start_sync(file, argp);
2172 case BTRFS_IOC_WAIT_SYNC:
2173 return btrfs_ioctl_wait_sync(file, argp);