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"
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)
59 else if (S_ISREG(mode))
60 return flags & ~FS_DIRSYNC_FL;
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)
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;
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)
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)))
142 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
144 struct inode *inode = file->f_path.dentry->d_inode;
145 struct btrfs_inode *ip = BTRFS_I(inode);
146 struct btrfs_root *root = ip->root;
147 struct btrfs_trans_handle *trans;
148 unsigned int flags, oldflags;
151 if (copy_from_user(&flags, arg, sizeof(flags)))
154 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
155 FS_NOATIME_FL | FS_NODUMP_FL | \
156 FS_SYNC_FL | FS_DIRSYNC_FL))
159 if (!is_owner_or_cap(inode))
162 mutex_lock(&inode->i_mutex);
164 flags = btrfs_mask_flags(inode->i_mode, flags);
165 oldflags = btrfs_flags_to_ioctl(ip->flags);
166 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
167 if (!capable(CAP_LINUX_IMMUTABLE)) {
173 ret = mnt_want_write(file->f_path.mnt);
177 if (flags & FS_SYNC_FL)
178 ip->flags |= BTRFS_INODE_SYNC;
180 ip->flags &= ~BTRFS_INODE_SYNC;
181 if (flags & FS_IMMUTABLE_FL)
182 ip->flags |= BTRFS_INODE_IMMUTABLE;
184 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
185 if (flags & FS_APPEND_FL)
186 ip->flags |= BTRFS_INODE_APPEND;
188 ip->flags &= ~BTRFS_INODE_APPEND;
189 if (flags & FS_NODUMP_FL)
190 ip->flags |= BTRFS_INODE_NODUMP;
192 ip->flags &= ~BTRFS_INODE_NODUMP;
193 if (flags & FS_NOATIME_FL)
194 ip->flags |= BTRFS_INODE_NOATIME;
196 ip->flags &= ~BTRFS_INODE_NOATIME;
197 if (flags & FS_DIRSYNC_FL)
198 ip->flags |= BTRFS_INODE_DIRSYNC;
200 ip->flags &= ~BTRFS_INODE_DIRSYNC;
203 trans = btrfs_join_transaction(root, 1);
206 ret = btrfs_update_inode(trans, root, inode);
209 btrfs_update_iflags(inode);
210 inode->i_ctime = CURRENT_TIME;
211 btrfs_end_transaction(trans, root);
213 mnt_drop_write(file->f_path.mnt);
215 mutex_unlock(&inode->i_mutex);
219 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
221 struct inode *inode = file->f_path.dentry->d_inode;
223 return put_user(inode->i_generation, arg);
226 static noinline int create_subvol(struct btrfs_root *root,
227 struct dentry *dentry,
228 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;
249 ret = btrfs_reserve_metadata_space(root, 6);
253 trans = btrfs_start_transaction(root, 1);
256 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
261 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
262 0, objectid, NULL, 0, 0, 0);
268 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
269 btrfs_set_header_bytenr(leaf, leaf->start);
270 btrfs_set_header_generation(leaf, trans->transid);
271 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
272 btrfs_set_header_owner(leaf, objectid);
274 write_extent_buffer(leaf, root->fs_info->fsid,
275 (unsigned long)btrfs_header_fsid(leaf),
277 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
278 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
280 btrfs_mark_buffer_dirty(leaf);
282 inode_item = &root_item.inode;
283 memset(inode_item, 0, sizeof(*inode_item));
284 inode_item->generation = cpu_to_le64(1);
285 inode_item->size = cpu_to_le64(3);
286 inode_item->nlink = cpu_to_le32(1);
287 inode_item->nbytes = cpu_to_le64(root->leafsize);
288 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
290 btrfs_set_root_bytenr(&root_item, leaf->start);
291 btrfs_set_root_generation(&root_item, trans->transid);
292 btrfs_set_root_level(&root_item, 0);
293 btrfs_set_root_refs(&root_item, 1);
294 btrfs_set_root_used(&root_item, leaf->len);
295 btrfs_set_root_last_snapshot(&root_item, 0);
297 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
298 root_item.drop_level = 0;
300 btrfs_tree_unlock(leaf);
301 free_extent_buffer(leaf);
304 btrfs_set_root_dirid(&root_item, new_dirid);
306 key.objectid = objectid;
308 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
309 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
314 key.offset = (u64)-1;
315 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
316 BUG_ON(IS_ERR(new_root));
318 btrfs_record_root_in_trans(trans, new_root);
320 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
321 BTRFS_I(dir)->block_group);
323 * insert the directory item
325 ret = btrfs_set_inode_index(dir, &index);
328 ret = btrfs_insert_dir_item(trans, root,
329 name, namelen, dir->i_ino, &key,
330 BTRFS_FT_DIR, index);
334 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
335 ret = btrfs_update_inode(trans, root, dir);
338 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
339 objectid, root->root_key.objectid,
340 dir->i_ino, index, name, namelen);
344 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
346 err = btrfs_commit_transaction(trans, root);
350 btrfs_unreserve_metadata_space(root, 6);
354 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
355 char *name, int namelen)
358 struct btrfs_pending_snapshot *pending_snapshot;
359 struct btrfs_trans_handle *trans;
371 ret = btrfs_reserve_metadata_space(root, 6);
375 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
376 if (!pending_snapshot) {
378 btrfs_unreserve_metadata_space(root, 6);
381 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
382 if (!pending_snapshot->name) {
384 kfree(pending_snapshot);
385 btrfs_unreserve_metadata_space(root, 6);
388 memcpy(pending_snapshot->name, name, namelen);
389 pending_snapshot->name[namelen] = '\0';
390 pending_snapshot->dentry = dentry;
391 trans = btrfs_start_transaction(root, 1);
393 pending_snapshot->root = root;
394 list_add(&pending_snapshot->list,
395 &trans->transaction->pending_snapshots);
396 ret = btrfs_commit_transaction(trans, root);
398 btrfs_unreserve_metadata_space(root, 6);
400 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
402 ret = PTR_ERR(inode);
406 d_instantiate(dentry, inode);
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)
431 struct inode *dir = parent->dentry->d_inode;
432 struct dentry *dentry;
435 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
437 dentry = lookup_one_len(name, parent->dentry, namelen);
438 error = PTR_ERR(dentry);
446 error = mnt_want_write(parent->mnt);
450 error = btrfs_may_create(dir, dentry);
454 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
456 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
460 error = create_snapshot(snap_src, dentry,
463 error = create_subvol(BTRFS_I(dir)->root, dentry,
467 fsnotify_mkdir(dir, dentry);
469 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
471 mnt_drop_write(parent->mnt);
475 mutex_unlock(&dir->i_mutex);
479 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
480 int thresh, u64 *last_len, u64 *skip,
483 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
484 struct extent_map *em = NULL;
485 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
493 * make sure that once we start defragging and extent, we keep on
496 if (start < *defrag_end)
502 * hopefully we have this extent in the tree already, try without
503 * the full extent lock
505 read_lock(&em_tree->lock);
506 em = lookup_extent_mapping(em_tree, start, len);
507 read_unlock(&em_tree->lock);
510 /* get the big lock and read metadata off disk */
511 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
512 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
513 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
519 /* this will cover holes, and inline extents */
520 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
524 * we hit a real extent, if it is big don't bother defragging it again
526 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
530 * last_len ends up being a counter of how many bytes we've defragged.
531 * every time we choose not to defrag an extent, we reset *last_len
532 * so that the next tiny extent will force a defrag.
534 * The end result of this is that tiny extents before a single big
535 * extent will force at least part of that big extent to be defragged.
539 *defrag_end = extent_map_end(em);
542 *skip = extent_map_end(em);
550 static int btrfs_defrag_file(struct file *file,
551 struct btrfs_ioctl_defrag_range_args *range)
553 struct inode *inode = fdentry(file)->d_inode;
554 struct btrfs_root *root = BTRFS_I(inode)->root;
555 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
556 struct btrfs_ordered_extent *ordered;
558 unsigned long last_index;
559 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
560 unsigned long total_read = 0;
569 if (inode->i_size == 0)
572 if (range->start + range->len > range->start) {
573 last_index = min_t(u64, inode->i_size - 1,
574 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
576 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
579 i = range->start >> PAGE_CACHE_SHIFT;
580 while (i <= last_index) {
581 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
583 range->extent_thresh,
588 * the should_defrag function tells us how much to skip
589 * bump our counter by the suggested amount
591 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
592 i = max(i + 1, next);
596 if (total_read % ra_pages == 0) {
597 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
598 min(last_index, i + ra_pages - 1));
601 mutex_lock(&inode->i_mutex);
602 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
603 BTRFS_I(inode)->force_compress = 1;
605 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
611 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
613 btrfs_free_reserved_data_space(root, inode,
619 if (inode->i_size == 0 ||
620 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
622 goto err_reservations;
625 page = grab_cache_page(inode->i_mapping, i);
627 goto err_reservations;
629 if (!PageUptodate(page)) {
630 btrfs_readpage(NULL, page);
632 if (!PageUptodate(page)) {
634 page_cache_release(page);
635 goto err_reservations;
639 if (page->mapping != inode->i_mapping) {
641 page_cache_release(page);
645 wait_on_page_writeback(page);
647 if (PageDirty(page)) {
648 btrfs_free_reserved_data_space(root, inode,
653 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
654 page_end = page_start + PAGE_CACHE_SIZE - 1;
655 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
657 ordered = btrfs_lookup_ordered_extent(inode, page_start);
659 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
661 page_cache_release(page);
662 btrfs_start_ordered_extent(inode, ordered, 1);
663 btrfs_put_ordered_extent(ordered);
666 set_page_extent_mapped(page);
669 * this makes sure page_mkwrite is called on the
670 * page if it is dirtied again later
672 clear_page_dirty_for_io(page);
673 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
674 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
675 EXTENT_DO_ACCOUNTING, GFP_NOFS);
677 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
678 ClearPageChecked(page);
679 set_page_dirty(page);
680 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
684 page_cache_release(page);
685 mutex_unlock(&inode->i_mutex);
687 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
688 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
692 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
693 filemap_flush(inode->i_mapping);
695 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
696 /* the filemap_flush will queue IO into the worker threads, but
697 * we have to make sure the IO is actually started and that
698 * ordered extents get created before we return
700 atomic_inc(&root->fs_info->async_submit_draining);
701 while (atomic_read(&root->fs_info->nr_async_submits) ||
702 atomic_read(&root->fs_info->async_delalloc_pages)) {
703 wait_event(root->fs_info->async_submit_wait,
704 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
705 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
707 atomic_dec(&root->fs_info->async_submit_draining);
709 mutex_lock(&inode->i_mutex);
710 BTRFS_I(inode)->force_compress = 0;
711 mutex_unlock(&inode->i_mutex);
717 mutex_unlock(&inode->i_mutex);
718 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
719 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
723 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
729 struct btrfs_ioctl_vol_args *vol_args;
730 struct btrfs_trans_handle *trans;
731 struct btrfs_device *device = NULL;
738 if (root->fs_info->sb->s_flags & MS_RDONLY)
741 if (!capable(CAP_SYS_ADMIN))
744 vol_args = memdup_user(arg, sizeof(*vol_args));
745 if (IS_ERR(vol_args))
746 return PTR_ERR(vol_args);
748 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
749 namelen = strlen(vol_args->name);
751 mutex_lock(&root->fs_info->volume_mutex);
752 sizestr = vol_args->name;
753 devstr = strchr(sizestr, ':');
756 sizestr = devstr + 1;
758 devstr = vol_args->name;
759 devid = simple_strtoull(devstr, &end, 10);
760 printk(KERN_INFO "resizing devid %llu\n",
761 (unsigned long long)devid);
763 device = btrfs_find_device(root, devid, NULL, NULL);
765 printk(KERN_INFO "resizer unable to find device %llu\n",
766 (unsigned long long)devid);
770 if (!strcmp(sizestr, "max"))
771 new_size = device->bdev->bd_inode->i_size;
773 if (sizestr[0] == '-') {
776 } else if (sizestr[0] == '+') {
780 new_size = memparse(sizestr, NULL);
787 old_size = device->total_bytes;
790 if (new_size > old_size) {
794 new_size = old_size - new_size;
795 } else if (mod > 0) {
796 new_size = old_size + new_size;
799 if (new_size < 256 * 1024 * 1024) {
803 if (new_size > device->bdev->bd_inode->i_size) {
808 do_div(new_size, root->sectorsize);
809 new_size *= root->sectorsize;
811 printk(KERN_INFO "new size for %s is %llu\n",
812 device->name, (unsigned long long)new_size);
814 if (new_size > old_size) {
815 trans = btrfs_start_transaction(root, 1);
816 ret = btrfs_grow_device(trans, device, new_size);
817 btrfs_commit_transaction(trans, root);
819 ret = btrfs_shrink_device(device, new_size);
823 mutex_unlock(&root->fs_info->volume_mutex);
828 static noinline int btrfs_ioctl_snap_create(struct file *file,
829 void __user *arg, int subvol)
831 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
832 struct btrfs_ioctl_vol_args *vol_args;
833 struct file *src_file;
837 if (root->fs_info->sb->s_flags & MS_RDONLY)
840 vol_args = memdup_user(arg, sizeof(*vol_args));
841 if (IS_ERR(vol_args))
842 return PTR_ERR(vol_args);
844 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
845 namelen = strlen(vol_args->name);
846 if (strchr(vol_args->name, '/')) {
852 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
855 struct inode *src_inode;
856 src_file = fget(vol_args->fd);
862 src_inode = src_file->f_path.dentry->d_inode;
863 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
864 printk(KERN_INFO "btrfs: Snapshot src from "
870 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
871 BTRFS_I(src_inode)->root);
880 * helper to check if the subvolume references other subvolumes
882 static noinline int may_destroy_subvol(struct btrfs_root *root)
884 struct btrfs_path *path;
885 struct btrfs_key key;
888 path = btrfs_alloc_path();
892 key.objectid = root->root_key.objectid;
893 key.type = BTRFS_ROOT_REF_KEY;
894 key.offset = (u64)-1;
896 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
903 if (path->slots[0] > 0) {
905 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
906 if (key.objectid == root->root_key.objectid &&
907 key.type == BTRFS_ROOT_REF_KEY)
911 btrfs_free_path(path);
915 static noinline int key_in_sk(struct btrfs_key *key,
916 struct btrfs_ioctl_search_key *sk)
918 struct btrfs_key test;
921 test.objectid = sk->min_objectid;
922 test.type = sk->min_type;
923 test.offset = sk->min_offset;
925 ret = btrfs_comp_cpu_keys(key, &test);
929 test.objectid = sk->max_objectid;
930 test.type = sk->max_type;
931 test.offset = sk->max_offset;
933 ret = btrfs_comp_cpu_keys(key, &test);
939 static noinline int copy_to_sk(struct btrfs_root *root,
940 struct btrfs_path *path,
941 struct btrfs_key *key,
942 struct btrfs_ioctl_search_key *sk,
944 unsigned long *sk_offset,
948 struct extent_buffer *leaf;
949 struct btrfs_ioctl_search_header sh;
950 unsigned long item_off;
951 unsigned long item_len;
958 leaf = path->nodes[0];
959 slot = path->slots[0];
960 nritems = btrfs_header_nritems(leaf);
962 if (btrfs_header_generation(leaf) > sk->max_transid) {
966 found_transid = btrfs_header_generation(leaf);
968 for (i = slot; i < nritems; i++) {
969 item_off = btrfs_item_ptr_offset(leaf, i);
970 item_len = btrfs_item_size_nr(leaf, i);
972 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
975 if (sizeof(sh) + item_len + *sk_offset >
976 BTRFS_SEARCH_ARGS_BUFSIZE) {
981 btrfs_item_key_to_cpu(leaf, key, i);
982 if (!key_in_sk(key, sk))
985 sh.objectid = key->objectid;
986 sh.offset = key->offset;
989 sh.transid = found_transid;
991 /* copy search result header */
992 memcpy(buf + *sk_offset, &sh, sizeof(sh));
993 *sk_offset += sizeof(sh);
996 char *p = buf + *sk_offset;
998 read_extent_buffer(leaf, p,
1000 *sk_offset += item_len;
1004 if (*num_found >= sk->nr_items)
1009 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1011 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1014 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1021 *num_found += found;
1025 static noinline int search_ioctl(struct inode *inode,
1026 struct btrfs_ioctl_search_args *args)
1028 struct btrfs_root *root;
1029 struct btrfs_key key;
1030 struct btrfs_key max_key;
1031 struct btrfs_path *path;
1032 struct btrfs_ioctl_search_key *sk = &args->key;
1033 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1036 unsigned long sk_offset = 0;
1038 path = btrfs_alloc_path();
1042 if (sk->tree_id == 0) {
1043 /* search the root of the inode that was passed */
1044 root = BTRFS_I(inode)->root;
1046 key.objectid = sk->tree_id;
1047 key.type = BTRFS_ROOT_ITEM_KEY;
1048 key.offset = (u64)-1;
1049 root = btrfs_read_fs_root_no_name(info, &key);
1051 printk(KERN_ERR "could not find root %llu\n",
1053 btrfs_free_path(path);
1058 key.objectid = sk->min_objectid;
1059 key.type = sk->min_type;
1060 key.offset = sk->min_offset;
1062 max_key.objectid = sk->max_objectid;
1063 max_key.type = sk->max_type;
1064 max_key.offset = sk->max_offset;
1066 path->keep_locks = 1;
1069 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1076 ret = copy_to_sk(root, path, &key, sk, args->buf,
1077 &sk_offset, &num_found);
1078 btrfs_release_path(root, path);
1079 if (ret || num_found >= sk->nr_items)
1085 sk->nr_items = num_found;
1086 btrfs_free_path(path);
1090 static noinline int btrfs_ioctl_tree_search(struct file *file,
1093 struct btrfs_ioctl_search_args *args;
1094 struct inode *inode;
1097 if (!capable(CAP_SYS_ADMIN))
1100 args = kmalloc(sizeof(*args), GFP_KERNEL);
1104 if (copy_from_user(args, argp, sizeof(*args))) {
1108 inode = fdentry(file)->d_inode;
1109 ret = search_ioctl(inode, args);
1110 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1117 * Search INODE_REFs to identify path name of 'dirid' directory
1118 * in a 'tree_id' tree. and sets path name to 'name'.
1120 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1121 u64 tree_id, u64 dirid, char *name)
1123 struct btrfs_root *root;
1124 struct btrfs_key key;
1130 struct btrfs_inode_ref *iref;
1131 struct extent_buffer *l;
1132 struct btrfs_path *path;
1134 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1139 path = btrfs_alloc_path();
1143 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1145 key.objectid = tree_id;
1146 key.type = BTRFS_ROOT_ITEM_KEY;
1147 key.offset = (u64)-1;
1148 root = btrfs_read_fs_root_no_name(info, &key);
1150 printk(KERN_ERR "could not find root %llu\n", tree_id);
1155 key.objectid = dirid;
1156 key.type = BTRFS_INODE_REF_KEY;
1157 key.offset = (u64)-1;
1160 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1165 slot = path->slots[0];
1166 if (ret > 0 && slot > 0)
1168 btrfs_item_key_to_cpu(l, &key, slot);
1170 if (ret > 0 && (key.objectid != dirid ||
1171 key.type != BTRFS_INODE_REF_KEY)) {
1176 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1177 len = btrfs_inode_ref_name_len(l, iref);
1179 total_len += len + 1;
1184 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1186 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1189 btrfs_release_path(root, path);
1190 key.objectid = key.offset;
1191 key.offset = (u64)-1;
1192 dirid = key.objectid;
1197 memcpy(name, ptr, total_len);
1198 name[total_len]='\0';
1201 btrfs_free_path(path);
1205 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1208 struct btrfs_ioctl_ino_lookup_args *args;
1209 struct inode *inode;
1212 if (!capable(CAP_SYS_ADMIN))
1215 args = kmalloc(sizeof(*args), GFP_KERNEL);
1216 if (copy_from_user(args, argp, sizeof(*args))) {
1220 inode = fdentry(file)->d_inode;
1222 if (args->treeid == 0)
1223 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1225 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1226 args->treeid, args->objectid,
1229 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1236 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1239 struct dentry *parent = fdentry(file);
1240 struct dentry *dentry;
1241 struct inode *dir = parent->d_inode;
1242 struct inode *inode;
1243 struct btrfs_root *root = BTRFS_I(dir)->root;
1244 struct btrfs_root *dest = NULL;
1245 struct btrfs_ioctl_vol_args *vol_args;
1246 struct btrfs_trans_handle *trans;
1251 if (!capable(CAP_SYS_ADMIN))
1254 vol_args = memdup_user(arg, sizeof(*vol_args));
1255 if (IS_ERR(vol_args))
1256 return PTR_ERR(vol_args);
1258 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1259 namelen = strlen(vol_args->name);
1260 if (strchr(vol_args->name, '/') ||
1261 strncmp(vol_args->name, "..", namelen) == 0) {
1266 err = mnt_want_write(file->f_path.mnt);
1270 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1271 dentry = lookup_one_len(vol_args->name, parent, namelen);
1272 if (IS_ERR(dentry)) {
1273 err = PTR_ERR(dentry);
1274 goto out_unlock_dir;
1277 if (!dentry->d_inode) {
1282 inode = dentry->d_inode;
1283 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1288 dest = BTRFS_I(inode)->root;
1290 mutex_lock(&inode->i_mutex);
1291 err = d_invalidate(dentry);
1295 down_write(&root->fs_info->subvol_sem);
1297 err = may_destroy_subvol(dest);
1301 trans = btrfs_start_transaction(root, 1);
1302 ret = btrfs_unlink_subvol(trans, root, dir,
1303 dest->root_key.objectid,
1304 dentry->d_name.name,
1305 dentry->d_name.len);
1308 btrfs_record_root_in_trans(trans, dest);
1310 memset(&dest->root_item.drop_progress, 0,
1311 sizeof(dest->root_item.drop_progress));
1312 dest->root_item.drop_level = 0;
1313 btrfs_set_root_refs(&dest->root_item, 0);
1315 ret = btrfs_insert_orphan_item(trans,
1316 root->fs_info->tree_root,
1317 dest->root_key.objectid);
1320 ret = btrfs_commit_transaction(trans, root);
1322 inode->i_flags |= S_DEAD;
1324 up_write(&root->fs_info->subvol_sem);
1326 mutex_unlock(&inode->i_mutex);
1328 shrink_dcache_sb(root->fs_info->sb);
1329 btrfs_invalidate_inodes(dest);
1335 mutex_unlock(&dir->i_mutex);
1336 mnt_drop_write(file->f_path.mnt);
1342 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1344 struct inode *inode = fdentry(file)->d_inode;
1345 struct btrfs_root *root = BTRFS_I(inode)->root;
1346 struct btrfs_ioctl_defrag_range_args *range;
1349 ret = mnt_want_write(file->f_path.mnt);
1353 switch (inode->i_mode & S_IFMT) {
1355 if (!capable(CAP_SYS_ADMIN)) {
1359 btrfs_defrag_root(root, 0);
1360 btrfs_defrag_root(root->fs_info->extent_root, 0);
1363 if (!(file->f_mode & FMODE_WRITE)) {
1368 range = kzalloc(sizeof(*range), GFP_KERNEL);
1375 if (copy_from_user(range, argp,
1380 /* compression requires us to start the IO */
1381 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1382 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1383 range->extent_thresh = (u32)-1;
1386 /* the rest are all set to zero by kzalloc */
1387 range->len = (u64)-1;
1389 btrfs_defrag_file(file, range);
1394 mnt_drop_write(file->f_path.mnt);
1398 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1400 struct btrfs_ioctl_vol_args *vol_args;
1403 if (!capable(CAP_SYS_ADMIN))
1406 vol_args = memdup_user(arg, sizeof(*vol_args));
1407 if (IS_ERR(vol_args))
1408 return PTR_ERR(vol_args);
1410 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1411 ret = btrfs_init_new_device(root, vol_args->name);
1417 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1419 struct btrfs_ioctl_vol_args *vol_args;
1422 if (!capable(CAP_SYS_ADMIN))
1425 if (root->fs_info->sb->s_flags & MS_RDONLY)
1428 vol_args = memdup_user(arg, sizeof(*vol_args));
1429 if (IS_ERR(vol_args))
1430 return PTR_ERR(vol_args);
1432 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1433 ret = btrfs_rm_device(root, vol_args->name);
1439 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1440 u64 off, u64 olen, u64 destoff)
1442 struct inode *inode = fdentry(file)->d_inode;
1443 struct btrfs_root *root = BTRFS_I(inode)->root;
1444 struct file *src_file;
1446 struct btrfs_trans_handle *trans;
1447 struct btrfs_path *path;
1448 struct extent_buffer *leaf;
1450 struct btrfs_key key;
1455 u64 bs = root->fs_info->sb->s_blocksize;
1460 * - split compressed inline extents. annoying: we need to
1461 * decompress into destination's address_space (the file offset
1462 * may change, so source mapping won't do), then recompress (or
1463 * otherwise reinsert) a subrange.
1464 * - allow ranges within the same file to be cloned (provided
1465 * they don't overlap)?
1468 /* the destination must be opened for writing */
1469 if (!(file->f_mode & FMODE_WRITE))
1472 ret = mnt_want_write(file->f_path.mnt);
1476 src_file = fget(srcfd);
1479 goto out_drop_write;
1481 src = src_file->f_dentry->d_inode;
1488 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1492 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1496 buf = vmalloc(btrfs_level_size(root, 0));
1500 path = btrfs_alloc_path();
1508 mutex_lock(&inode->i_mutex);
1509 mutex_lock(&src->i_mutex);
1511 mutex_lock(&src->i_mutex);
1512 mutex_lock(&inode->i_mutex);
1515 /* determine range to clone */
1517 if (off >= src->i_size || off + len > src->i_size)
1520 olen = len = src->i_size - off;
1521 /* if we extend to eof, continue to block boundary */
1522 if (off + len == src->i_size)
1523 len = ((src->i_size + bs-1) & ~(bs-1))
1526 /* verify the end result is block aligned */
1527 if ((off & (bs-1)) ||
1528 ((off + len) & (bs-1)))
1531 /* do any pending delalloc/csum calc on src, one way or
1532 another, and lock file content */
1534 struct btrfs_ordered_extent *ordered;
1535 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1536 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
1537 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
1539 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1541 btrfs_put_ordered_extent(ordered);
1542 btrfs_wait_ordered_range(src, off, off+len);
1545 trans = btrfs_start_transaction(root, 1);
1548 /* punch hole in destination first */
1549 btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
1552 key.objectid = src->i_ino;
1553 key.type = BTRFS_EXTENT_DATA_KEY;
1558 * note the key will change type as we walk through the
1561 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1565 nritems = btrfs_header_nritems(path->nodes[0]);
1566 if (path->slots[0] >= nritems) {
1567 ret = btrfs_next_leaf(root, path);
1572 nritems = btrfs_header_nritems(path->nodes[0]);
1574 leaf = path->nodes[0];
1575 slot = path->slots[0];
1577 btrfs_item_key_to_cpu(leaf, &key, slot);
1578 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1579 key.objectid != src->i_ino)
1582 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1583 struct btrfs_file_extent_item *extent;
1586 struct btrfs_key new_key;
1587 u64 disko = 0, diskl = 0;
1588 u64 datao = 0, datal = 0;
1591 size = btrfs_item_size_nr(leaf, slot);
1592 read_extent_buffer(leaf, buf,
1593 btrfs_item_ptr_offset(leaf, slot),
1596 extent = btrfs_item_ptr(leaf, slot,
1597 struct btrfs_file_extent_item);
1598 comp = btrfs_file_extent_compression(leaf, extent);
1599 type = btrfs_file_extent_type(leaf, extent);
1600 if (type == BTRFS_FILE_EXTENT_REG ||
1601 type == BTRFS_FILE_EXTENT_PREALLOC) {
1602 disko = btrfs_file_extent_disk_bytenr(leaf,
1604 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1606 datao = btrfs_file_extent_offset(leaf, extent);
1607 datal = btrfs_file_extent_num_bytes(leaf,
1609 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1610 /* take upper bound, may be compressed */
1611 datal = btrfs_file_extent_ram_bytes(leaf,
1614 btrfs_release_path(root, path);
1616 if (key.offset + datal < off ||
1617 key.offset >= off+len)
1620 memcpy(&new_key, &key, sizeof(new_key));
1621 new_key.objectid = inode->i_ino;
1622 new_key.offset = key.offset + destoff - off;
1624 if (type == BTRFS_FILE_EXTENT_REG ||
1625 type == BTRFS_FILE_EXTENT_PREALLOC) {
1626 ret = btrfs_insert_empty_item(trans, root, path,
1631 leaf = path->nodes[0];
1632 slot = path->slots[0];
1633 write_extent_buffer(leaf, buf,
1634 btrfs_item_ptr_offset(leaf, slot),
1637 extent = btrfs_item_ptr(leaf, slot,
1638 struct btrfs_file_extent_item);
1640 if (off > key.offset) {
1641 datao += off - key.offset;
1642 datal -= off - key.offset;
1645 if (key.offset + datal > off + len)
1646 datal = off + len - key.offset;
1648 /* disko == 0 means it's a hole */
1652 btrfs_set_file_extent_offset(leaf, extent,
1654 btrfs_set_file_extent_num_bytes(leaf, extent,
1657 inode_add_bytes(inode, datal);
1658 ret = btrfs_inc_extent_ref(trans, root,
1660 root->root_key.objectid,
1662 new_key.offset - datao);
1665 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1668 if (off > key.offset) {
1669 skip = off - key.offset;
1670 new_key.offset += skip;
1673 if (key.offset + datal > off+len)
1674 trim = key.offset + datal - (off+len);
1676 if (comp && (skip || trim)) {
1680 size -= skip + trim;
1681 datal -= skip + trim;
1682 ret = btrfs_insert_empty_item(trans, root, path,
1689 btrfs_file_extent_calc_inline_size(0);
1690 memmove(buf+start, buf+start+skip,
1694 leaf = path->nodes[0];
1695 slot = path->slots[0];
1696 write_extent_buffer(leaf, buf,
1697 btrfs_item_ptr_offset(leaf, slot),
1699 inode_add_bytes(inode, datal);
1702 btrfs_mark_buffer_dirty(leaf);
1706 btrfs_release_path(root, path);
1711 btrfs_release_path(root, path);
1713 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1714 if (destoff + olen > inode->i_size)
1715 btrfs_i_size_write(inode, destoff + olen);
1716 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1717 ret = btrfs_update_inode(trans, root, inode);
1719 btrfs_end_transaction(trans, root);
1720 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1722 vmtruncate(inode, 0);
1724 mutex_unlock(&src->i_mutex);
1725 mutex_unlock(&inode->i_mutex);
1727 btrfs_free_path(path);
1731 mnt_drop_write(file->f_path.mnt);
1735 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1737 struct btrfs_ioctl_clone_range_args args;
1739 if (copy_from_user(&args, argp, sizeof(args)))
1741 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1742 args.src_length, args.dest_offset);
1746 * there are many ways the trans_start and trans_end ioctls can lead
1747 * to deadlocks. They should only be used by applications that
1748 * basically own the machine, and have a very in depth understanding
1749 * of all the possible deadlocks and enospc problems.
1751 static long btrfs_ioctl_trans_start(struct file *file)
1753 struct inode *inode = fdentry(file)->d_inode;
1754 struct btrfs_root *root = BTRFS_I(inode)->root;
1755 struct btrfs_trans_handle *trans;
1759 if (!capable(CAP_SYS_ADMIN))
1763 if (file->private_data)
1766 ret = mnt_want_write(file->f_path.mnt);
1770 mutex_lock(&root->fs_info->trans_mutex);
1771 root->fs_info->open_ioctl_trans++;
1772 mutex_unlock(&root->fs_info->trans_mutex);
1775 trans = btrfs_start_ioctl_transaction(root, 0);
1779 file->private_data = trans;
1783 mutex_lock(&root->fs_info->trans_mutex);
1784 root->fs_info->open_ioctl_trans--;
1785 mutex_unlock(&root->fs_info->trans_mutex);
1786 mnt_drop_write(file->f_path.mnt);
1791 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1793 struct inode *inode = fdentry(file)->d_inode;
1794 struct btrfs_root *root = BTRFS_I(inode)->root;
1795 struct btrfs_root *new_root;
1796 struct btrfs_dir_item *di;
1797 struct btrfs_trans_handle *trans;
1798 struct btrfs_path *path;
1799 struct btrfs_key location;
1800 struct btrfs_disk_key disk_key;
1801 struct btrfs_super_block *disk_super;
1806 if (!capable(CAP_SYS_ADMIN))
1809 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1813 objectid = root->root_key.objectid;
1815 location.objectid = objectid;
1816 location.type = BTRFS_ROOT_ITEM_KEY;
1817 location.offset = (u64)-1;
1819 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1820 if (IS_ERR(new_root))
1821 return PTR_ERR(new_root);
1823 if (btrfs_root_refs(&new_root->root_item) == 0)
1826 path = btrfs_alloc_path();
1829 path->leave_spinning = 1;
1831 trans = btrfs_start_transaction(root, 1);
1833 btrfs_free_path(path);
1837 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
1838 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
1839 dir_id, "default", 7, 1);
1841 btrfs_free_path(path);
1842 btrfs_end_transaction(trans, root);
1843 printk(KERN_ERR "Umm, you don't have the default dir item, "
1844 "this isn't going to work\n");
1848 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
1849 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
1850 btrfs_mark_buffer_dirty(path->nodes[0]);
1851 btrfs_free_path(path);
1853 disk_super = &root->fs_info->super_copy;
1854 features = btrfs_super_incompat_flags(disk_super);
1855 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
1856 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
1857 btrfs_set_super_incompat_flags(disk_super, features);
1859 btrfs_end_transaction(trans, root);
1864 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1866 struct btrfs_ioctl_space_args space_args;
1867 struct btrfs_ioctl_space_info space;
1868 struct btrfs_ioctl_space_info *dest;
1869 struct btrfs_ioctl_space_info *dest_orig;
1870 struct btrfs_ioctl_space_info *user_dest;
1871 struct btrfs_space_info *info;
1876 if (copy_from_user(&space_args,
1877 (struct btrfs_ioctl_space_args __user *)arg,
1878 sizeof(space_args)))
1881 /* first we count slots */
1883 list_for_each_entry_rcu(info, &root->fs_info->space_info, list)
1887 /* space_slots == 0 means they are asking for a count */
1888 if (space_args.space_slots == 0) {
1889 space_args.total_spaces = slot_count;
1892 alloc_size = sizeof(*dest) * slot_count;
1893 /* we generally have at most 6 or so space infos, one for each raid
1894 * level. So, a whole page should be more than enough for everyone
1896 if (alloc_size > PAGE_CACHE_SIZE)
1899 space_args.total_spaces = 0;
1900 dest = kmalloc(alloc_size, GFP_NOFS);
1905 /* now we have a buffer to copy into */
1907 list_for_each_entry_rcu(info, &root->fs_info->space_info, list) {
1908 /* make sure we don't copy more than we allocated
1911 if (slot_count == 0)
1915 /* make sure userland has enough room in their buffer */
1916 if (space_args.total_spaces >= space_args.space_slots)
1919 space.flags = info->flags;
1920 space.total_bytes = info->total_bytes;
1921 space.used_bytes = info->bytes_used;
1922 memcpy(dest, &space, sizeof(space));
1924 space_args.total_spaces++;
1928 user_dest = (struct btrfs_ioctl_space_info *)
1929 (arg + sizeof(struct btrfs_ioctl_space_args));
1931 if (copy_to_user(user_dest, dest_orig, alloc_size))
1936 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1943 * there are many ways the trans_start and trans_end ioctls can lead
1944 * to deadlocks. They should only be used by applications that
1945 * basically own the machine, and have a very in depth understanding
1946 * of all the possible deadlocks and enospc problems.
1948 long btrfs_ioctl_trans_end(struct file *file)
1950 struct inode *inode = fdentry(file)->d_inode;
1951 struct btrfs_root *root = BTRFS_I(inode)->root;
1952 struct btrfs_trans_handle *trans;
1954 trans = file->private_data;
1957 file->private_data = NULL;
1959 btrfs_end_transaction(trans, root);
1961 mutex_lock(&root->fs_info->trans_mutex);
1962 root->fs_info->open_ioctl_trans--;
1963 mutex_unlock(&root->fs_info->trans_mutex);
1965 mnt_drop_write(file->f_path.mnt);
1969 long btrfs_ioctl(struct file *file, unsigned int
1970 cmd, unsigned long arg)
1972 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1973 void __user *argp = (void __user *)arg;
1976 case FS_IOC_GETFLAGS:
1977 return btrfs_ioctl_getflags(file, argp);
1978 case FS_IOC_SETFLAGS:
1979 return btrfs_ioctl_setflags(file, argp);
1980 case FS_IOC_GETVERSION:
1981 return btrfs_ioctl_getversion(file, argp);
1982 case BTRFS_IOC_SNAP_CREATE:
1983 return btrfs_ioctl_snap_create(file, argp, 0);
1984 case BTRFS_IOC_SUBVOL_CREATE:
1985 return btrfs_ioctl_snap_create(file, argp, 1);
1986 case BTRFS_IOC_SNAP_DESTROY:
1987 return btrfs_ioctl_snap_destroy(file, argp);
1988 case BTRFS_IOC_DEFAULT_SUBVOL:
1989 return btrfs_ioctl_default_subvol(file, argp);
1990 case BTRFS_IOC_DEFRAG:
1991 return btrfs_ioctl_defrag(file, NULL);
1992 case BTRFS_IOC_DEFRAG_RANGE:
1993 return btrfs_ioctl_defrag(file, argp);
1994 case BTRFS_IOC_RESIZE:
1995 return btrfs_ioctl_resize(root, argp);
1996 case BTRFS_IOC_ADD_DEV:
1997 return btrfs_ioctl_add_dev(root, argp);
1998 case BTRFS_IOC_RM_DEV:
1999 return btrfs_ioctl_rm_dev(root, argp);
2000 case BTRFS_IOC_BALANCE:
2001 return btrfs_balance(root->fs_info->dev_root);
2002 case BTRFS_IOC_CLONE:
2003 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2004 case BTRFS_IOC_CLONE_RANGE:
2005 return btrfs_ioctl_clone_range(file, argp);
2006 case BTRFS_IOC_TRANS_START:
2007 return btrfs_ioctl_trans_start(file);
2008 case BTRFS_IOC_TRANS_END:
2009 return btrfs_ioctl_trans_end(file);
2010 case BTRFS_IOC_TREE_SEARCH:
2011 return btrfs_ioctl_tree_search(file, argp);
2012 case BTRFS_IOC_INO_LOOKUP:
2013 return btrfs_ioctl_ino_lookup(file, argp);
2014 case BTRFS_IOC_SPACE_INFO:
2015 return btrfs_ioctl_space_info(root, argp);
2016 case BTRFS_IOC_SYNC:
2017 btrfs_sync_fs(file->f_dentry->d_sb, 1);