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>
43 #include <linux/blkdev.h>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
50 #include "print-tree.h"
53 #include "inode-map.h"
56 /* Mask out flags that are inappropriate for the given type of inode. */
57 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
61 else if (S_ISREG(mode))
62 return flags & ~FS_DIRSYNC_FL;
64 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
68 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
70 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
72 unsigned int iflags = 0;
74 if (flags & BTRFS_INODE_SYNC)
76 if (flags & BTRFS_INODE_IMMUTABLE)
77 iflags |= FS_IMMUTABLE_FL;
78 if (flags & BTRFS_INODE_APPEND)
79 iflags |= FS_APPEND_FL;
80 if (flags & BTRFS_INODE_NODUMP)
81 iflags |= FS_NODUMP_FL;
82 if (flags & BTRFS_INODE_NOATIME)
83 iflags |= FS_NOATIME_FL;
84 if (flags & BTRFS_INODE_DIRSYNC)
85 iflags |= FS_DIRSYNC_FL;
86 if (flags & BTRFS_INODE_NODATACOW)
87 iflags |= FS_NOCOW_FL;
89 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
90 iflags |= FS_COMPR_FL;
91 else if (flags & BTRFS_INODE_NOCOMPRESS)
92 iflags |= FS_NOCOMP_FL;
98 * Update inode->i_flags based on the btrfs internal flags.
100 void btrfs_update_iflags(struct inode *inode)
102 struct btrfs_inode *ip = BTRFS_I(inode);
104 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
106 if (ip->flags & BTRFS_INODE_SYNC)
107 inode->i_flags |= S_SYNC;
108 if (ip->flags & BTRFS_INODE_IMMUTABLE)
109 inode->i_flags |= S_IMMUTABLE;
110 if (ip->flags & BTRFS_INODE_APPEND)
111 inode->i_flags |= S_APPEND;
112 if (ip->flags & BTRFS_INODE_NOATIME)
113 inode->i_flags |= S_NOATIME;
114 if (ip->flags & BTRFS_INODE_DIRSYNC)
115 inode->i_flags |= S_DIRSYNC;
119 * Inherit flags from the parent inode.
121 * Unlike extN we don't have any flags we don't want to inherit currently.
123 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
130 flags = BTRFS_I(dir)->flags;
132 if (S_ISREG(inode->i_mode))
133 flags &= ~BTRFS_INODE_DIRSYNC;
134 else if (!S_ISDIR(inode->i_mode))
135 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
137 BTRFS_I(inode)->flags = flags;
138 btrfs_update_iflags(inode);
141 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
143 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
144 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
146 if (copy_to_user(arg, &flags, sizeof(flags)))
151 static int check_flags(unsigned int flags)
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL | \
156 FS_NOCOMP_FL | FS_COMPR_FL |
160 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
166 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
168 struct inode *inode = file->f_path.dentry->d_inode;
169 struct btrfs_inode *ip = BTRFS_I(inode);
170 struct btrfs_root *root = ip->root;
171 struct btrfs_trans_handle *trans;
172 unsigned int flags, oldflags;
175 if (btrfs_root_readonly(root))
178 if (copy_from_user(&flags, arg, sizeof(flags)))
181 ret = check_flags(flags);
185 if (!inode_owner_or_capable(inode))
188 mutex_lock(&inode->i_mutex);
190 flags = btrfs_mask_flags(inode->i_mode, flags);
191 oldflags = btrfs_flags_to_ioctl(ip->flags);
192 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
193 if (!capable(CAP_LINUX_IMMUTABLE)) {
199 ret = mnt_want_write(file->f_path.mnt);
203 if (flags & FS_SYNC_FL)
204 ip->flags |= BTRFS_INODE_SYNC;
206 ip->flags &= ~BTRFS_INODE_SYNC;
207 if (flags & FS_IMMUTABLE_FL)
208 ip->flags |= BTRFS_INODE_IMMUTABLE;
210 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
211 if (flags & FS_APPEND_FL)
212 ip->flags |= BTRFS_INODE_APPEND;
214 ip->flags &= ~BTRFS_INODE_APPEND;
215 if (flags & FS_NODUMP_FL)
216 ip->flags |= BTRFS_INODE_NODUMP;
218 ip->flags &= ~BTRFS_INODE_NODUMP;
219 if (flags & FS_NOATIME_FL)
220 ip->flags |= BTRFS_INODE_NOATIME;
222 ip->flags &= ~BTRFS_INODE_NOATIME;
223 if (flags & FS_DIRSYNC_FL)
224 ip->flags |= BTRFS_INODE_DIRSYNC;
226 ip->flags &= ~BTRFS_INODE_DIRSYNC;
227 if (flags & FS_NOCOW_FL)
228 ip->flags |= BTRFS_INODE_NODATACOW;
230 ip->flags &= ~BTRFS_INODE_NODATACOW;
233 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
234 * flag may be changed automatically if compression code won't make
237 if (flags & FS_NOCOMP_FL) {
238 ip->flags &= ~BTRFS_INODE_COMPRESS;
239 ip->flags |= BTRFS_INODE_NOCOMPRESS;
240 } else if (flags & FS_COMPR_FL) {
241 ip->flags |= BTRFS_INODE_COMPRESS;
242 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
244 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
247 trans = btrfs_join_transaction(root);
248 BUG_ON(IS_ERR(trans));
250 ret = btrfs_update_inode(trans, root, inode);
253 btrfs_update_iflags(inode);
254 inode->i_ctime = CURRENT_TIME;
255 btrfs_end_transaction(trans, root);
257 mnt_drop_write(file->f_path.mnt);
261 mutex_unlock(&inode->i_mutex);
265 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
267 struct inode *inode = file->f_path.dentry->d_inode;
269 return put_user(inode->i_generation, arg);
272 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
274 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
275 struct btrfs_fs_info *fs_info = root->fs_info;
276 struct btrfs_device *device;
277 struct request_queue *q;
278 struct fstrim_range range;
279 u64 minlen = ULLONG_MAX;
283 if (!capable(CAP_SYS_ADMIN))
287 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
291 q = bdev_get_queue(device->bdev);
292 if (blk_queue_discard(q)) {
294 minlen = min((u64)q->limits.discard_granularity,
302 if (copy_from_user(&range, arg, sizeof(range)))
305 range.minlen = max(range.minlen, minlen);
306 ret = btrfs_trim_fs(root, &range);
310 if (copy_to_user(arg, &range, sizeof(range)))
316 static noinline int create_subvol(struct btrfs_root *root,
317 struct dentry *dentry,
318 char *name, int namelen,
321 struct btrfs_trans_handle *trans;
322 struct btrfs_key key;
323 struct btrfs_root_item root_item;
324 struct btrfs_inode_item *inode_item;
325 struct extent_buffer *leaf;
326 struct btrfs_root *new_root;
327 struct dentry *parent = dentry->d_parent;
332 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
335 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
339 dir = parent->d_inode;
347 trans = btrfs_start_transaction(root, 6);
349 return PTR_ERR(trans);
351 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
352 0, objectid, NULL, 0, 0, 0);
358 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
359 btrfs_set_header_bytenr(leaf, leaf->start);
360 btrfs_set_header_generation(leaf, trans->transid);
361 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
362 btrfs_set_header_owner(leaf, objectid);
364 write_extent_buffer(leaf, root->fs_info->fsid,
365 (unsigned long)btrfs_header_fsid(leaf),
367 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
368 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
370 btrfs_mark_buffer_dirty(leaf);
372 inode_item = &root_item.inode;
373 memset(inode_item, 0, sizeof(*inode_item));
374 inode_item->generation = cpu_to_le64(1);
375 inode_item->size = cpu_to_le64(3);
376 inode_item->nlink = cpu_to_le32(1);
377 inode_item->nbytes = cpu_to_le64(root->leafsize);
378 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
381 root_item.byte_limit = 0;
382 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
384 btrfs_set_root_bytenr(&root_item, leaf->start);
385 btrfs_set_root_generation(&root_item, trans->transid);
386 btrfs_set_root_level(&root_item, 0);
387 btrfs_set_root_refs(&root_item, 1);
388 btrfs_set_root_used(&root_item, leaf->len);
389 btrfs_set_root_last_snapshot(&root_item, 0);
391 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
392 root_item.drop_level = 0;
394 btrfs_tree_unlock(leaf);
395 free_extent_buffer(leaf);
398 btrfs_set_root_dirid(&root_item, new_dirid);
400 key.objectid = objectid;
402 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
403 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
408 key.offset = (u64)-1;
409 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
410 BUG_ON(IS_ERR(new_root));
412 btrfs_record_root_in_trans(trans, new_root);
414 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
416 * insert the directory item
418 ret = btrfs_set_inode_index(dir, &index);
421 ret = btrfs_insert_dir_item(trans, root,
422 name, namelen, dir, &key,
423 BTRFS_FT_DIR, index);
427 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
428 ret = btrfs_update_inode(trans, root, dir);
431 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
432 objectid, root->root_key.objectid,
433 btrfs_ino(dir), index, name, namelen);
437 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
440 *async_transid = trans->transid;
441 err = btrfs_commit_transaction_async(trans, root, 1);
443 err = btrfs_commit_transaction(trans, root);
450 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
451 char *name, int namelen, u64 *async_transid,
455 struct btrfs_pending_snapshot *pending_snapshot;
456 struct btrfs_trans_handle *trans;
462 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
463 if (!pending_snapshot)
466 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
467 pending_snapshot->dentry = dentry;
468 pending_snapshot->root = root;
469 pending_snapshot->readonly = readonly;
471 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
473 ret = PTR_ERR(trans);
477 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
480 spin_lock(&root->fs_info->trans_lock);
481 list_add(&pending_snapshot->list,
482 &trans->transaction->pending_snapshots);
483 spin_unlock(&root->fs_info->trans_lock);
485 *async_transid = trans->transid;
486 ret = btrfs_commit_transaction_async(trans,
487 root->fs_info->extent_root, 1);
489 ret = btrfs_commit_transaction(trans,
490 root->fs_info->extent_root);
494 ret = pending_snapshot->error;
498 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
502 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
504 ret = PTR_ERR(inode);
508 d_instantiate(dentry, inode);
511 kfree(pending_snapshot);
515 /* copy of check_sticky in fs/namei.c()
516 * It's inline, so penalty for filesystems that don't use sticky bit is
519 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
521 uid_t fsuid = current_fsuid();
523 if (!(dir->i_mode & S_ISVTX))
525 if (inode->i_uid == fsuid)
527 if (dir->i_uid == fsuid)
529 return !capable(CAP_FOWNER);
532 /* copy of may_delete in fs/namei.c()
533 * Check whether we can remove a link victim from directory dir, check
534 * whether the type of victim is right.
535 * 1. We can't do it if dir is read-only (done in permission())
536 * 2. We should have write and exec permissions on dir
537 * 3. We can't remove anything from append-only dir
538 * 4. We can't do anything with immutable dir (done in permission())
539 * 5. If the sticky bit on dir is set we should either
540 * a. be owner of dir, or
541 * b. be owner of victim, or
542 * c. have CAP_FOWNER capability
543 * 6. If the victim is append-only or immutable we can't do antyhing with
544 * links pointing to it.
545 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
546 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
547 * 9. We can't remove a root or mountpoint.
548 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
549 * nfs_async_unlink().
552 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
556 if (!victim->d_inode)
559 BUG_ON(victim->d_parent->d_inode != dir);
560 audit_inode_child(victim, dir);
562 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
567 if (btrfs_check_sticky(dir, victim->d_inode)||
568 IS_APPEND(victim->d_inode)||
569 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
572 if (!S_ISDIR(victim->d_inode->i_mode))
576 } else if (S_ISDIR(victim->d_inode->i_mode))
580 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
585 /* copy of may_create in fs/namei.c() */
586 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
592 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
596 * Create a new subvolume below @parent. This is largely modeled after
597 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
598 * inside this filesystem so it's quite a bit simpler.
600 static noinline int btrfs_mksubvol(struct path *parent,
601 char *name, int namelen,
602 struct btrfs_root *snap_src,
603 u64 *async_transid, bool readonly)
605 struct inode *dir = parent->dentry->d_inode;
606 struct dentry *dentry;
609 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
611 dentry = lookup_one_len(name, parent->dentry, namelen);
612 error = PTR_ERR(dentry);
620 error = mnt_want_write(parent->mnt);
624 error = btrfs_may_create(dir, dentry);
628 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
630 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
634 error = create_snapshot(snap_src, dentry,
635 name, namelen, async_transid, readonly);
637 error = create_subvol(BTRFS_I(dir)->root, dentry,
638 name, namelen, async_transid);
641 fsnotify_mkdir(dir, dentry);
643 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
645 mnt_drop_write(parent->mnt);
649 mutex_unlock(&dir->i_mutex);
654 * When we're defragging a range, we don't want to kick it off again
655 * if it is really just waiting for delalloc to send it down.
656 * If we find a nice big extent or delalloc range for the bytes in the
657 * file you want to defrag, we return 0 to let you know to skip this
660 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
662 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
663 struct extent_map *em = NULL;
664 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
667 read_lock(&em_tree->lock);
668 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
669 read_unlock(&em_tree->lock);
672 end = extent_map_end(em);
674 if (end - offset > thresh)
677 /* if we already have a nice delalloc here, just stop */
679 end = count_range_bits(io_tree, &offset, offset + thresh,
680 thresh, EXTENT_DELALLOC, 1);
687 * helper function to walk through a file and find extents
688 * newer than a specific transid, and smaller than thresh.
690 * This is used by the defragging code to find new and small
693 static int find_new_extents(struct btrfs_root *root,
694 struct inode *inode, u64 newer_than,
695 u64 *off, int thresh)
697 struct btrfs_path *path;
698 struct btrfs_key min_key;
699 struct btrfs_key max_key;
700 struct extent_buffer *leaf;
701 struct btrfs_file_extent_item *extent;
704 u64 ino = btrfs_ino(inode);
706 path = btrfs_alloc_path();
710 min_key.objectid = ino;
711 min_key.type = BTRFS_EXTENT_DATA_KEY;
712 min_key.offset = *off;
714 max_key.objectid = ino;
715 max_key.type = (u8)-1;
716 max_key.offset = (u64)-1;
718 path->keep_locks = 1;
721 ret = btrfs_search_forward(root, &min_key, &max_key,
722 path, 0, newer_than);
725 if (min_key.objectid != ino)
727 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
730 leaf = path->nodes[0];
731 extent = btrfs_item_ptr(leaf, path->slots[0],
732 struct btrfs_file_extent_item);
734 type = btrfs_file_extent_type(leaf, extent);
735 if (type == BTRFS_FILE_EXTENT_REG &&
736 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
737 check_defrag_in_cache(inode, min_key.offset, thresh)) {
738 *off = min_key.offset;
739 btrfs_free_path(path);
743 if (min_key.offset == (u64)-1)
747 btrfs_release_path(path);
750 btrfs_free_path(path);
754 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
755 int thresh, u64 *last_len, u64 *skip,
758 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
759 struct extent_map *em = NULL;
760 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
764 * make sure that once we start defragging and extent, we keep on
767 if (start < *defrag_end)
773 * hopefully we have this extent in the tree already, try without
774 * the full extent lock
776 read_lock(&em_tree->lock);
777 em = lookup_extent_mapping(em_tree, start, len);
778 read_unlock(&em_tree->lock);
781 /* get the big lock and read metadata off disk */
782 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
783 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
784 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
790 /* this will cover holes, and inline extents */
791 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
795 * we hit a real extent, if it is big don't bother defragging it again
797 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
801 * last_len ends up being a counter of how many bytes we've defragged.
802 * every time we choose not to defrag an extent, we reset *last_len
803 * so that the next tiny extent will force a defrag.
805 * The end result of this is that tiny extents before a single big
806 * extent will force at least part of that big extent to be defragged.
810 *defrag_end = extent_map_end(em);
813 *skip = extent_map_end(em);
822 * it doesn't do much good to defrag one or two pages
823 * at a time. This pulls in a nice chunk of pages
826 * It also makes sure the delalloc code has enough
827 * dirty data to avoid making new small extents as part
830 * It's a good idea to start RA on this range
831 * before calling this.
833 static int cluster_pages_for_defrag(struct inode *inode,
835 unsigned long start_index,
838 unsigned long file_end;
839 u64 isize = i_size_read(inode);
845 struct btrfs_ordered_extent *ordered;
846 struct extent_state *cached_state = NULL;
850 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
852 ret = btrfs_delalloc_reserve_space(inode,
853 num_pages << PAGE_CACHE_SHIFT);
860 /* step one, lock all the pages */
861 for (i = 0; i < num_pages; i++) {
863 page = find_or_create_page(inode->i_mapping,
864 start_index + i, GFP_NOFS);
868 if (!PageUptodate(page)) {
869 btrfs_readpage(NULL, page);
871 if (!PageUptodate(page)) {
873 page_cache_release(page);
878 isize = i_size_read(inode);
879 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
880 if (!isize || page->index > file_end ||
881 page->mapping != inode->i_mapping) {
882 /* whoops, we blew past eof, skip this page */
884 page_cache_release(page);
893 if (!(inode->i_sb->s_flags & MS_ACTIVE))
897 * so now we have a nice long stream of locked
898 * and up to date pages, lets wait on them
900 for (i = 0; i < i_done; i++)
901 wait_on_page_writeback(pages[i]);
903 page_start = page_offset(pages[0]);
904 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
906 lock_extent_bits(&BTRFS_I(inode)->io_tree,
907 page_start, page_end - 1, 0, &cached_state,
909 ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
911 ordered->file_offset + ordered->len > page_start &&
912 ordered->file_offset < page_end) {
913 btrfs_put_ordered_extent(ordered);
914 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
915 page_start, page_end - 1,
916 &cached_state, GFP_NOFS);
917 for (i = 0; i < i_done; i++) {
918 unlock_page(pages[i]);
919 page_cache_release(pages[i]);
921 btrfs_wait_ordered_range(inode, page_start,
922 page_end - page_start);
926 btrfs_put_ordered_extent(ordered);
928 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
929 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
930 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
933 if (i_done != num_pages) {
934 spin_lock(&BTRFS_I(inode)->lock);
935 BTRFS_I(inode)->outstanding_extents++;
936 spin_unlock(&BTRFS_I(inode)->lock);
937 btrfs_delalloc_release_space(inode,
938 (num_pages - i_done) << PAGE_CACHE_SHIFT);
942 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
945 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
946 page_start, page_end - 1, &cached_state,
949 for (i = 0; i < i_done; i++) {
950 clear_page_dirty_for_io(pages[i]);
951 ClearPageChecked(pages[i]);
952 set_page_extent_mapped(pages[i]);
953 set_page_dirty(pages[i]);
954 unlock_page(pages[i]);
955 page_cache_release(pages[i]);
959 for (i = 0; i < i_done; i++) {
960 unlock_page(pages[i]);
961 page_cache_release(pages[i]);
963 btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
968 int btrfs_defrag_file(struct inode *inode, struct file *file,
969 struct btrfs_ioctl_defrag_range_args *range,
970 u64 newer_than, unsigned long max_to_defrag)
972 struct btrfs_root *root = BTRFS_I(inode)->root;
973 struct btrfs_super_block *disk_super;
974 struct file_ra_state *ra = NULL;
975 unsigned long last_index;
980 u64 newer_off = range->start;
984 int defrag_count = 0;
985 int compress_type = BTRFS_COMPRESS_ZLIB;
986 int extent_thresh = range->extent_thresh;
987 int newer_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
988 u64 new_align = ~((u64)128 * 1024 - 1);
989 struct page **pages = NULL;
991 if (extent_thresh == 0)
992 extent_thresh = 256 * 1024;
994 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
995 if (range->compress_type > BTRFS_COMPRESS_TYPES)
997 if (range->compress_type)
998 compress_type = range->compress_type;
1001 if (inode->i_size == 0)
1005 * if we were not given a file, allocate a readahead
1009 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1012 file_ra_state_init(ra, inode->i_mapping);
1017 pages = kmalloc(sizeof(struct page *) * newer_cluster,
1024 /* find the last page to defrag */
1025 if (range->start + range->len > range->start) {
1026 last_index = min_t(u64, inode->i_size - 1,
1027 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1029 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
1033 ret = find_new_extents(root, inode, newer_than,
1034 &newer_off, 64 * 1024);
1036 range->start = newer_off;
1038 * we always align our defrag to help keep
1039 * the extents in the file evenly spaced
1041 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1042 newer_left = newer_cluster;
1046 i = range->start >> PAGE_CACHE_SHIFT;
1049 max_to_defrag = last_index - 1;
1051 while (i <= last_index && defrag_count < max_to_defrag) {
1053 * make sure we stop running if someone unmounts
1056 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1060 !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1067 * the should_defrag function tells us how much to skip
1068 * bump our counter by the suggested amount
1070 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1071 i = max(i + 1, next);
1074 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1075 BTRFS_I(inode)->force_compress = compress_type;
1077 btrfs_force_ra(inode->i_mapping, ra, file, i, newer_cluster);
1079 ret = cluster_pages_for_defrag(inode, pages, i, newer_cluster);
1083 defrag_count += ret;
1084 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1088 if (newer_off == (u64)-1)
1091 newer_off = max(newer_off + 1,
1092 (u64)i << PAGE_CACHE_SHIFT);
1094 ret = find_new_extents(root, inode,
1095 newer_than, &newer_off,
1098 range->start = newer_off;
1099 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1100 newer_left = newer_cluster;
1109 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1110 filemap_flush(inode->i_mapping);
1112 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1113 /* the filemap_flush will queue IO into the worker threads, but
1114 * we have to make sure the IO is actually started and that
1115 * ordered extents get created before we return
1117 atomic_inc(&root->fs_info->async_submit_draining);
1118 while (atomic_read(&root->fs_info->nr_async_submits) ||
1119 atomic_read(&root->fs_info->async_delalloc_pages)) {
1120 wait_event(root->fs_info->async_submit_wait,
1121 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1122 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1124 atomic_dec(&root->fs_info->async_submit_draining);
1126 mutex_lock(&inode->i_mutex);
1127 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1128 mutex_unlock(&inode->i_mutex);
1131 disk_super = &root->fs_info->super_copy;
1132 features = btrfs_super_incompat_flags(disk_super);
1133 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1134 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1135 btrfs_set_super_incompat_flags(disk_super, features);
1140 return defrag_count;
1149 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1155 struct btrfs_ioctl_vol_args *vol_args;
1156 struct btrfs_trans_handle *trans;
1157 struct btrfs_device *device = NULL;
1159 char *devstr = NULL;
1163 if (root->fs_info->sb->s_flags & MS_RDONLY)
1166 if (!capable(CAP_SYS_ADMIN))
1169 vol_args = memdup_user(arg, sizeof(*vol_args));
1170 if (IS_ERR(vol_args))
1171 return PTR_ERR(vol_args);
1173 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1175 mutex_lock(&root->fs_info->volume_mutex);
1176 sizestr = vol_args->name;
1177 devstr = strchr(sizestr, ':');
1180 sizestr = devstr + 1;
1182 devstr = vol_args->name;
1183 devid = simple_strtoull(devstr, &end, 10);
1184 printk(KERN_INFO "resizing devid %llu\n",
1185 (unsigned long long)devid);
1187 device = btrfs_find_device(root, devid, NULL, NULL);
1189 printk(KERN_INFO "resizer unable to find device %llu\n",
1190 (unsigned long long)devid);
1194 if (!strcmp(sizestr, "max"))
1195 new_size = device->bdev->bd_inode->i_size;
1197 if (sizestr[0] == '-') {
1200 } else if (sizestr[0] == '+') {
1204 new_size = memparse(sizestr, NULL);
1205 if (new_size == 0) {
1211 old_size = device->total_bytes;
1214 if (new_size > old_size) {
1218 new_size = old_size - new_size;
1219 } else if (mod > 0) {
1220 new_size = old_size + new_size;
1223 if (new_size < 256 * 1024 * 1024) {
1227 if (new_size > device->bdev->bd_inode->i_size) {
1232 do_div(new_size, root->sectorsize);
1233 new_size *= root->sectorsize;
1235 printk(KERN_INFO "new size for %s is %llu\n",
1236 device->name, (unsigned long long)new_size);
1238 if (new_size > old_size) {
1239 trans = btrfs_start_transaction(root, 0);
1240 if (IS_ERR(trans)) {
1241 ret = PTR_ERR(trans);
1244 ret = btrfs_grow_device(trans, device, new_size);
1245 btrfs_commit_transaction(trans, root);
1247 ret = btrfs_shrink_device(device, new_size);
1251 mutex_unlock(&root->fs_info->volume_mutex);
1256 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1263 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1264 struct file *src_file;
1268 if (root->fs_info->sb->s_flags & MS_RDONLY)
1271 namelen = strlen(name);
1272 if (strchr(name, '/')) {
1278 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1279 NULL, transid, readonly);
1281 struct inode *src_inode;
1282 src_file = fget(fd);
1288 src_inode = src_file->f_path.dentry->d_inode;
1289 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1290 printk(KERN_INFO "btrfs: Snapshot src from "
1296 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1297 BTRFS_I(src_inode)->root,
1305 static noinline int btrfs_ioctl_snap_create(struct file *file,
1306 void __user *arg, int subvol)
1308 struct btrfs_ioctl_vol_args *vol_args;
1311 vol_args = memdup_user(arg, sizeof(*vol_args));
1312 if (IS_ERR(vol_args))
1313 return PTR_ERR(vol_args);
1314 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1316 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1317 vol_args->fd, subvol,
1324 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1325 void __user *arg, int subvol)
1327 struct btrfs_ioctl_vol_args_v2 *vol_args;
1331 bool readonly = false;
1333 vol_args = memdup_user(arg, sizeof(*vol_args));
1334 if (IS_ERR(vol_args))
1335 return PTR_ERR(vol_args);
1336 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1338 if (vol_args->flags &
1339 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1344 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1346 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1349 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1350 vol_args->fd, subvol,
1353 if (ret == 0 && ptr &&
1355 offsetof(struct btrfs_ioctl_vol_args_v2,
1356 transid), ptr, sizeof(*ptr)))
1363 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1366 struct inode *inode = fdentry(file)->d_inode;
1367 struct btrfs_root *root = BTRFS_I(inode)->root;
1371 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1374 down_read(&root->fs_info->subvol_sem);
1375 if (btrfs_root_readonly(root))
1376 flags |= BTRFS_SUBVOL_RDONLY;
1377 up_read(&root->fs_info->subvol_sem);
1379 if (copy_to_user(arg, &flags, sizeof(flags)))
1385 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1388 struct inode *inode = fdentry(file)->d_inode;
1389 struct btrfs_root *root = BTRFS_I(inode)->root;
1390 struct btrfs_trans_handle *trans;
1395 if (root->fs_info->sb->s_flags & MS_RDONLY)
1398 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1401 if (copy_from_user(&flags, arg, sizeof(flags)))
1404 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
1407 if (flags & ~BTRFS_SUBVOL_RDONLY)
1410 if (!inode_owner_or_capable(inode))
1413 down_write(&root->fs_info->subvol_sem);
1416 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1419 root_flags = btrfs_root_flags(&root->root_item);
1420 if (flags & BTRFS_SUBVOL_RDONLY)
1421 btrfs_set_root_flags(&root->root_item,
1422 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1424 btrfs_set_root_flags(&root->root_item,
1425 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1427 trans = btrfs_start_transaction(root, 1);
1428 if (IS_ERR(trans)) {
1429 ret = PTR_ERR(trans);
1433 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1434 &root->root_key, &root->root_item);
1436 btrfs_commit_transaction(trans, root);
1439 btrfs_set_root_flags(&root->root_item, root_flags);
1441 up_write(&root->fs_info->subvol_sem);
1446 * helper to check if the subvolume references other subvolumes
1448 static noinline int may_destroy_subvol(struct btrfs_root *root)
1450 struct btrfs_path *path;
1451 struct btrfs_key key;
1454 path = btrfs_alloc_path();
1458 key.objectid = root->root_key.objectid;
1459 key.type = BTRFS_ROOT_REF_KEY;
1460 key.offset = (u64)-1;
1462 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1469 if (path->slots[0] > 0) {
1471 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1472 if (key.objectid == root->root_key.objectid &&
1473 key.type == BTRFS_ROOT_REF_KEY)
1477 btrfs_free_path(path);
1481 static noinline int key_in_sk(struct btrfs_key *key,
1482 struct btrfs_ioctl_search_key *sk)
1484 struct btrfs_key test;
1487 test.objectid = sk->min_objectid;
1488 test.type = sk->min_type;
1489 test.offset = sk->min_offset;
1491 ret = btrfs_comp_cpu_keys(key, &test);
1495 test.objectid = sk->max_objectid;
1496 test.type = sk->max_type;
1497 test.offset = sk->max_offset;
1499 ret = btrfs_comp_cpu_keys(key, &test);
1505 static noinline int copy_to_sk(struct btrfs_root *root,
1506 struct btrfs_path *path,
1507 struct btrfs_key *key,
1508 struct btrfs_ioctl_search_key *sk,
1510 unsigned long *sk_offset,
1514 struct extent_buffer *leaf;
1515 struct btrfs_ioctl_search_header sh;
1516 unsigned long item_off;
1517 unsigned long item_len;
1523 leaf = path->nodes[0];
1524 slot = path->slots[0];
1525 nritems = btrfs_header_nritems(leaf);
1527 if (btrfs_header_generation(leaf) > sk->max_transid) {
1531 found_transid = btrfs_header_generation(leaf);
1533 for (i = slot; i < nritems; i++) {
1534 item_off = btrfs_item_ptr_offset(leaf, i);
1535 item_len = btrfs_item_size_nr(leaf, i);
1537 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1540 if (sizeof(sh) + item_len + *sk_offset >
1541 BTRFS_SEARCH_ARGS_BUFSIZE) {
1546 btrfs_item_key_to_cpu(leaf, key, i);
1547 if (!key_in_sk(key, sk))
1550 sh.objectid = key->objectid;
1551 sh.offset = key->offset;
1552 sh.type = key->type;
1554 sh.transid = found_transid;
1556 /* copy search result header */
1557 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1558 *sk_offset += sizeof(sh);
1561 char *p = buf + *sk_offset;
1563 read_extent_buffer(leaf, p,
1564 item_off, item_len);
1565 *sk_offset += item_len;
1569 if (*num_found >= sk->nr_items)
1574 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1576 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1579 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1589 static noinline int search_ioctl(struct inode *inode,
1590 struct btrfs_ioctl_search_args *args)
1592 struct btrfs_root *root;
1593 struct btrfs_key key;
1594 struct btrfs_key max_key;
1595 struct btrfs_path *path;
1596 struct btrfs_ioctl_search_key *sk = &args->key;
1597 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1600 unsigned long sk_offset = 0;
1602 path = btrfs_alloc_path();
1606 if (sk->tree_id == 0) {
1607 /* search the root of the inode that was passed */
1608 root = BTRFS_I(inode)->root;
1610 key.objectid = sk->tree_id;
1611 key.type = BTRFS_ROOT_ITEM_KEY;
1612 key.offset = (u64)-1;
1613 root = btrfs_read_fs_root_no_name(info, &key);
1615 printk(KERN_ERR "could not find root %llu\n",
1617 btrfs_free_path(path);
1622 key.objectid = sk->min_objectid;
1623 key.type = sk->min_type;
1624 key.offset = sk->min_offset;
1626 max_key.objectid = sk->max_objectid;
1627 max_key.type = sk->max_type;
1628 max_key.offset = sk->max_offset;
1630 path->keep_locks = 1;
1633 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1640 ret = copy_to_sk(root, path, &key, sk, args->buf,
1641 &sk_offset, &num_found);
1642 btrfs_release_path(path);
1643 if (ret || num_found >= sk->nr_items)
1649 sk->nr_items = num_found;
1650 btrfs_free_path(path);
1654 static noinline int btrfs_ioctl_tree_search(struct file *file,
1657 struct btrfs_ioctl_search_args *args;
1658 struct inode *inode;
1661 if (!capable(CAP_SYS_ADMIN))
1664 args = memdup_user(argp, sizeof(*args));
1666 return PTR_ERR(args);
1668 inode = fdentry(file)->d_inode;
1669 ret = search_ioctl(inode, args);
1670 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1677 * Search INODE_REFs to identify path name of 'dirid' directory
1678 * in a 'tree_id' tree. and sets path name to 'name'.
1680 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1681 u64 tree_id, u64 dirid, char *name)
1683 struct btrfs_root *root;
1684 struct btrfs_key key;
1690 struct btrfs_inode_ref *iref;
1691 struct extent_buffer *l;
1692 struct btrfs_path *path;
1694 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1699 path = btrfs_alloc_path();
1703 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1705 key.objectid = tree_id;
1706 key.type = BTRFS_ROOT_ITEM_KEY;
1707 key.offset = (u64)-1;
1708 root = btrfs_read_fs_root_no_name(info, &key);
1710 printk(KERN_ERR "could not find root %llu\n", tree_id);
1715 key.objectid = dirid;
1716 key.type = BTRFS_INODE_REF_KEY;
1717 key.offset = (u64)-1;
1720 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1725 slot = path->slots[0];
1726 if (ret > 0 && slot > 0)
1728 btrfs_item_key_to_cpu(l, &key, slot);
1730 if (ret > 0 && (key.objectid != dirid ||
1731 key.type != BTRFS_INODE_REF_KEY)) {
1736 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1737 len = btrfs_inode_ref_name_len(l, iref);
1739 total_len += len + 1;
1744 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1746 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1749 btrfs_release_path(path);
1750 key.objectid = key.offset;
1751 key.offset = (u64)-1;
1752 dirid = key.objectid;
1756 memmove(name, ptr, total_len);
1757 name[total_len]='\0';
1760 btrfs_free_path(path);
1764 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1767 struct btrfs_ioctl_ino_lookup_args *args;
1768 struct inode *inode;
1771 if (!capable(CAP_SYS_ADMIN))
1774 args = memdup_user(argp, sizeof(*args));
1776 return PTR_ERR(args);
1778 inode = fdentry(file)->d_inode;
1780 if (args->treeid == 0)
1781 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1783 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1784 args->treeid, args->objectid,
1787 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1794 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1797 struct dentry *parent = fdentry(file);
1798 struct dentry *dentry;
1799 struct inode *dir = parent->d_inode;
1800 struct inode *inode;
1801 struct btrfs_root *root = BTRFS_I(dir)->root;
1802 struct btrfs_root *dest = NULL;
1803 struct btrfs_ioctl_vol_args *vol_args;
1804 struct btrfs_trans_handle *trans;
1809 vol_args = memdup_user(arg, sizeof(*vol_args));
1810 if (IS_ERR(vol_args))
1811 return PTR_ERR(vol_args);
1813 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1814 namelen = strlen(vol_args->name);
1815 if (strchr(vol_args->name, '/') ||
1816 strncmp(vol_args->name, "..", namelen) == 0) {
1821 err = mnt_want_write(file->f_path.mnt);
1825 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1826 dentry = lookup_one_len(vol_args->name, parent, namelen);
1827 if (IS_ERR(dentry)) {
1828 err = PTR_ERR(dentry);
1829 goto out_unlock_dir;
1832 if (!dentry->d_inode) {
1837 inode = dentry->d_inode;
1838 dest = BTRFS_I(inode)->root;
1839 if (!capable(CAP_SYS_ADMIN)){
1841 * Regular user. Only allow this with a special mount
1842 * option, when the user has write+exec access to the
1843 * subvol root, and when rmdir(2) would have been
1846 * Note that this is _not_ check that the subvol is
1847 * empty or doesn't contain data that we wouldn't
1848 * otherwise be able to delete.
1850 * Users who want to delete empty subvols should try
1854 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1858 * Do not allow deletion if the parent dir is the same
1859 * as the dir to be deleted. That means the ioctl
1860 * must be called on the dentry referencing the root
1861 * of the subvol, not a random directory contained
1868 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1872 /* check if subvolume may be deleted by a non-root user */
1873 err = btrfs_may_delete(dir, dentry, 1);
1878 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1883 mutex_lock(&inode->i_mutex);
1884 err = d_invalidate(dentry);
1888 down_write(&root->fs_info->subvol_sem);
1890 err = may_destroy_subvol(dest);
1894 trans = btrfs_start_transaction(root, 0);
1895 if (IS_ERR(trans)) {
1896 err = PTR_ERR(trans);
1899 trans->block_rsv = &root->fs_info->global_block_rsv;
1901 ret = btrfs_unlink_subvol(trans, root, dir,
1902 dest->root_key.objectid,
1903 dentry->d_name.name,
1904 dentry->d_name.len);
1907 btrfs_record_root_in_trans(trans, dest);
1909 memset(&dest->root_item.drop_progress, 0,
1910 sizeof(dest->root_item.drop_progress));
1911 dest->root_item.drop_level = 0;
1912 btrfs_set_root_refs(&dest->root_item, 0);
1914 if (!xchg(&dest->orphan_item_inserted, 1)) {
1915 ret = btrfs_insert_orphan_item(trans,
1916 root->fs_info->tree_root,
1917 dest->root_key.objectid);
1921 ret = btrfs_end_transaction(trans, root);
1923 inode->i_flags |= S_DEAD;
1925 up_write(&root->fs_info->subvol_sem);
1927 mutex_unlock(&inode->i_mutex);
1929 shrink_dcache_sb(root->fs_info->sb);
1930 btrfs_invalidate_inodes(dest);
1936 mutex_unlock(&dir->i_mutex);
1937 mnt_drop_write(file->f_path.mnt);
1943 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1945 struct inode *inode = fdentry(file)->d_inode;
1946 struct btrfs_root *root = BTRFS_I(inode)->root;
1947 struct btrfs_ioctl_defrag_range_args *range;
1950 if (btrfs_root_readonly(root))
1953 ret = mnt_want_write(file->f_path.mnt);
1957 switch (inode->i_mode & S_IFMT) {
1959 if (!capable(CAP_SYS_ADMIN)) {
1963 ret = btrfs_defrag_root(root, 0);
1966 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1969 if (!(file->f_mode & FMODE_WRITE)) {
1974 range = kzalloc(sizeof(*range), GFP_KERNEL);
1981 if (copy_from_user(range, argp,
1987 /* compression requires us to start the IO */
1988 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1989 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1990 range->extent_thresh = (u32)-1;
1993 /* the rest are all set to zero by kzalloc */
1994 range->len = (u64)-1;
1996 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2006 mnt_drop_write(file->f_path.mnt);
2010 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2012 struct btrfs_ioctl_vol_args *vol_args;
2015 if (!capable(CAP_SYS_ADMIN))
2018 vol_args = memdup_user(arg, sizeof(*vol_args));
2019 if (IS_ERR(vol_args))
2020 return PTR_ERR(vol_args);
2022 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2023 ret = btrfs_init_new_device(root, vol_args->name);
2029 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2031 struct btrfs_ioctl_vol_args *vol_args;
2034 if (!capable(CAP_SYS_ADMIN))
2037 if (root->fs_info->sb->s_flags & MS_RDONLY)
2040 vol_args = memdup_user(arg, sizeof(*vol_args));
2041 if (IS_ERR(vol_args))
2042 return PTR_ERR(vol_args);
2044 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2045 ret = btrfs_rm_device(root, vol_args->name);
2051 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2053 struct btrfs_ioctl_fs_info_args *fi_args;
2054 struct btrfs_device *device;
2055 struct btrfs_device *next;
2056 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2059 if (!capable(CAP_SYS_ADMIN))
2062 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2066 fi_args->num_devices = fs_devices->num_devices;
2067 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2069 mutex_lock(&fs_devices->device_list_mutex);
2070 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2071 if (device->devid > fi_args->max_id)
2072 fi_args->max_id = device->devid;
2074 mutex_unlock(&fs_devices->device_list_mutex);
2076 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2083 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2085 struct btrfs_ioctl_dev_info_args *di_args;
2086 struct btrfs_device *dev;
2087 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2089 char *s_uuid = NULL;
2090 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2092 if (!capable(CAP_SYS_ADMIN))
2095 di_args = memdup_user(arg, sizeof(*di_args));
2096 if (IS_ERR(di_args))
2097 return PTR_ERR(di_args);
2099 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2100 s_uuid = di_args->uuid;
2102 mutex_lock(&fs_devices->device_list_mutex);
2103 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2104 mutex_unlock(&fs_devices->device_list_mutex);
2111 di_args->devid = dev->devid;
2112 di_args->bytes_used = dev->bytes_used;
2113 di_args->total_bytes = dev->total_bytes;
2114 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2115 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2118 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2125 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2126 u64 off, u64 olen, u64 destoff)
2128 struct inode *inode = fdentry(file)->d_inode;
2129 struct btrfs_root *root = BTRFS_I(inode)->root;
2130 struct file *src_file;
2132 struct btrfs_trans_handle *trans;
2133 struct btrfs_path *path;
2134 struct extent_buffer *leaf;
2136 struct btrfs_key key;
2141 u64 bs = root->fs_info->sb->s_blocksize;
2146 * - split compressed inline extents. annoying: we need to
2147 * decompress into destination's address_space (the file offset
2148 * may change, so source mapping won't do), then recompress (or
2149 * otherwise reinsert) a subrange.
2150 * - allow ranges within the same file to be cloned (provided
2151 * they don't overlap)?
2154 /* the destination must be opened for writing */
2155 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2158 if (btrfs_root_readonly(root))
2161 ret = mnt_want_write(file->f_path.mnt);
2165 src_file = fget(srcfd);
2168 goto out_drop_write;
2171 src = src_file->f_dentry->d_inode;
2177 /* the src must be open for reading */
2178 if (!(src_file->f_mode & FMODE_READ))
2181 /* don't make the dst file partly checksummed */
2182 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2183 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2187 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2191 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2195 buf = vmalloc(btrfs_level_size(root, 0));
2199 path = btrfs_alloc_path();
2207 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2208 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2210 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2211 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2214 /* determine range to clone */
2216 if (off + len > src->i_size || off + len < off)
2219 olen = len = src->i_size - off;
2220 /* if we extend to eof, continue to block boundary */
2221 if (off + len == src->i_size)
2222 len = ALIGN(src->i_size, bs) - off;
2224 /* verify the end result is block aligned */
2225 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2226 !IS_ALIGNED(destoff, bs))
2229 if (destoff > inode->i_size) {
2230 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2235 /* truncate page cache pages from target inode range */
2236 truncate_inode_pages_range(&inode->i_data, destoff,
2237 PAGE_CACHE_ALIGN(destoff + len) - 1);
2239 /* do any pending delalloc/csum calc on src, one way or
2240 another, and lock file content */
2242 struct btrfs_ordered_extent *ordered;
2243 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2244 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2246 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2247 EXTENT_DELALLOC, 0, NULL))
2249 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2251 btrfs_put_ordered_extent(ordered);
2252 btrfs_wait_ordered_range(src, off, len);
2256 key.objectid = btrfs_ino(src);
2257 key.type = BTRFS_EXTENT_DATA_KEY;
2262 * note the key will change type as we walk through the
2265 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2269 nritems = btrfs_header_nritems(path->nodes[0]);
2270 if (path->slots[0] >= nritems) {
2271 ret = btrfs_next_leaf(root, path);
2276 nritems = btrfs_header_nritems(path->nodes[0]);
2278 leaf = path->nodes[0];
2279 slot = path->slots[0];
2281 btrfs_item_key_to_cpu(leaf, &key, slot);
2282 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2283 key.objectid != btrfs_ino(src))
2286 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2287 struct btrfs_file_extent_item *extent;
2290 struct btrfs_key new_key;
2291 u64 disko = 0, diskl = 0;
2292 u64 datao = 0, datal = 0;
2296 size = btrfs_item_size_nr(leaf, slot);
2297 read_extent_buffer(leaf, buf,
2298 btrfs_item_ptr_offset(leaf, slot),
2301 extent = btrfs_item_ptr(leaf, slot,
2302 struct btrfs_file_extent_item);
2303 comp = btrfs_file_extent_compression(leaf, extent);
2304 type = btrfs_file_extent_type(leaf, extent);
2305 if (type == BTRFS_FILE_EXTENT_REG ||
2306 type == BTRFS_FILE_EXTENT_PREALLOC) {
2307 disko = btrfs_file_extent_disk_bytenr(leaf,
2309 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2311 datao = btrfs_file_extent_offset(leaf, extent);
2312 datal = btrfs_file_extent_num_bytes(leaf,
2314 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2315 /* take upper bound, may be compressed */
2316 datal = btrfs_file_extent_ram_bytes(leaf,
2319 btrfs_release_path(path);
2321 if (key.offset + datal <= off ||
2322 key.offset >= off+len)
2325 memcpy(&new_key, &key, sizeof(new_key));
2326 new_key.objectid = btrfs_ino(inode);
2327 if (off <= key.offset)
2328 new_key.offset = key.offset + destoff - off;
2330 new_key.offset = destoff;
2333 * 1 - adjusting old extent (we may have to split it)
2334 * 1 - add new extent
2337 trans = btrfs_start_transaction(root, 3);
2338 if (IS_ERR(trans)) {
2339 ret = PTR_ERR(trans);
2343 if (type == BTRFS_FILE_EXTENT_REG ||
2344 type == BTRFS_FILE_EXTENT_PREALLOC) {
2346 * a | --- range to clone ---| b
2347 * | ------------- extent ------------- |
2350 /* substract range b */
2351 if (key.offset + datal > off + len)
2352 datal = off + len - key.offset;
2354 /* substract range a */
2355 if (off > key.offset) {
2356 datao += off - key.offset;
2357 datal -= off - key.offset;
2360 ret = btrfs_drop_extents(trans, inode,
2362 new_key.offset + datal,
2366 ret = btrfs_insert_empty_item(trans, root, path,
2370 leaf = path->nodes[0];
2371 slot = path->slots[0];
2372 write_extent_buffer(leaf, buf,
2373 btrfs_item_ptr_offset(leaf, slot),
2376 extent = btrfs_item_ptr(leaf, slot,
2377 struct btrfs_file_extent_item);
2379 /* disko == 0 means it's a hole */
2383 btrfs_set_file_extent_offset(leaf, extent,
2385 btrfs_set_file_extent_num_bytes(leaf, extent,
2388 inode_add_bytes(inode, datal);
2389 ret = btrfs_inc_extent_ref(trans, root,
2391 root->root_key.objectid,
2393 new_key.offset - datao);
2396 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2399 if (off > key.offset) {
2400 skip = off - key.offset;
2401 new_key.offset += skip;
2404 if (key.offset + datal > off+len)
2405 trim = key.offset + datal - (off+len);
2407 if (comp && (skip || trim)) {
2409 btrfs_end_transaction(trans, root);
2412 size -= skip + trim;
2413 datal -= skip + trim;
2415 ret = btrfs_drop_extents(trans, inode,
2417 new_key.offset + datal,
2421 ret = btrfs_insert_empty_item(trans, root, path,
2427 btrfs_file_extent_calc_inline_size(0);
2428 memmove(buf+start, buf+start+skip,
2432 leaf = path->nodes[0];
2433 slot = path->slots[0];
2434 write_extent_buffer(leaf, buf,
2435 btrfs_item_ptr_offset(leaf, slot),
2437 inode_add_bytes(inode, datal);
2440 btrfs_mark_buffer_dirty(leaf);
2441 btrfs_release_path(path);
2443 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2446 * we round up to the block size at eof when
2447 * determining which extents to clone above,
2448 * but shouldn't round up the file size
2450 endoff = new_key.offset + datal;
2451 if (endoff > destoff+olen)
2452 endoff = destoff+olen;
2453 if (endoff > inode->i_size)
2454 btrfs_i_size_write(inode, endoff);
2456 ret = btrfs_update_inode(trans, root, inode);
2458 btrfs_end_transaction(trans, root);
2461 btrfs_release_path(path);
2466 btrfs_release_path(path);
2467 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2469 mutex_unlock(&src->i_mutex);
2470 mutex_unlock(&inode->i_mutex);
2472 btrfs_free_path(path);
2476 mnt_drop_write(file->f_path.mnt);
2480 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2482 struct btrfs_ioctl_clone_range_args args;
2484 if (copy_from_user(&args, argp, sizeof(args)))
2486 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2487 args.src_length, args.dest_offset);
2491 * there are many ways the trans_start and trans_end ioctls can lead
2492 * to deadlocks. They should only be used by applications that
2493 * basically own the machine, and have a very in depth understanding
2494 * of all the possible deadlocks and enospc problems.
2496 static long btrfs_ioctl_trans_start(struct file *file)
2498 struct inode *inode = fdentry(file)->d_inode;
2499 struct btrfs_root *root = BTRFS_I(inode)->root;
2500 struct btrfs_trans_handle *trans;
2504 if (!capable(CAP_SYS_ADMIN))
2508 if (file->private_data)
2512 if (btrfs_root_readonly(root))
2515 ret = mnt_want_write(file->f_path.mnt);
2519 atomic_inc(&root->fs_info->open_ioctl_trans);
2522 trans = btrfs_start_ioctl_transaction(root);
2526 file->private_data = trans;
2530 atomic_dec(&root->fs_info->open_ioctl_trans);
2531 mnt_drop_write(file->f_path.mnt);
2536 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2538 struct inode *inode = fdentry(file)->d_inode;
2539 struct btrfs_root *root = BTRFS_I(inode)->root;
2540 struct btrfs_root *new_root;
2541 struct btrfs_dir_item *di;
2542 struct btrfs_trans_handle *trans;
2543 struct btrfs_path *path;
2544 struct btrfs_key location;
2545 struct btrfs_disk_key disk_key;
2546 struct btrfs_super_block *disk_super;
2551 if (!capable(CAP_SYS_ADMIN))
2554 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2558 objectid = root->root_key.objectid;
2560 location.objectid = objectid;
2561 location.type = BTRFS_ROOT_ITEM_KEY;
2562 location.offset = (u64)-1;
2564 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2565 if (IS_ERR(new_root))
2566 return PTR_ERR(new_root);
2568 if (btrfs_root_refs(&new_root->root_item) == 0)
2571 path = btrfs_alloc_path();
2574 path->leave_spinning = 1;
2576 trans = btrfs_start_transaction(root, 1);
2577 if (IS_ERR(trans)) {
2578 btrfs_free_path(path);
2579 return PTR_ERR(trans);
2582 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2583 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2584 dir_id, "default", 7, 1);
2585 if (IS_ERR_OR_NULL(di)) {
2586 btrfs_free_path(path);
2587 btrfs_end_transaction(trans, root);
2588 printk(KERN_ERR "Umm, you don't have the default dir item, "
2589 "this isn't going to work\n");
2593 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2594 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2595 btrfs_mark_buffer_dirty(path->nodes[0]);
2596 btrfs_free_path(path);
2598 disk_super = &root->fs_info->super_copy;
2599 features = btrfs_super_incompat_flags(disk_super);
2600 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2601 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2602 btrfs_set_super_incompat_flags(disk_super, features);
2604 btrfs_end_transaction(trans, root);
2609 static void get_block_group_info(struct list_head *groups_list,
2610 struct btrfs_ioctl_space_info *space)
2612 struct btrfs_block_group_cache *block_group;
2614 space->total_bytes = 0;
2615 space->used_bytes = 0;
2617 list_for_each_entry(block_group, groups_list, list) {
2618 space->flags = block_group->flags;
2619 space->total_bytes += block_group->key.offset;
2620 space->used_bytes +=
2621 btrfs_block_group_used(&block_group->item);
2625 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2627 struct btrfs_ioctl_space_args space_args;
2628 struct btrfs_ioctl_space_info space;
2629 struct btrfs_ioctl_space_info *dest;
2630 struct btrfs_ioctl_space_info *dest_orig;
2631 struct btrfs_ioctl_space_info __user *user_dest;
2632 struct btrfs_space_info *info;
2633 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2634 BTRFS_BLOCK_GROUP_SYSTEM,
2635 BTRFS_BLOCK_GROUP_METADATA,
2636 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2643 if (copy_from_user(&space_args,
2644 (struct btrfs_ioctl_space_args __user *)arg,
2645 sizeof(space_args)))
2648 for (i = 0; i < num_types; i++) {
2649 struct btrfs_space_info *tmp;
2653 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2655 if (tmp->flags == types[i]) {
2665 down_read(&info->groups_sem);
2666 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2667 if (!list_empty(&info->block_groups[c]))
2670 up_read(&info->groups_sem);
2673 /* space_slots == 0 means they are asking for a count */
2674 if (space_args.space_slots == 0) {
2675 space_args.total_spaces = slot_count;
2679 slot_count = min_t(u64, space_args.space_slots, slot_count);
2681 alloc_size = sizeof(*dest) * slot_count;
2683 /* we generally have at most 6 or so space infos, one for each raid
2684 * level. So, a whole page should be more than enough for everyone
2686 if (alloc_size > PAGE_CACHE_SIZE)
2689 space_args.total_spaces = 0;
2690 dest = kmalloc(alloc_size, GFP_NOFS);
2695 /* now we have a buffer to copy into */
2696 for (i = 0; i < num_types; i++) {
2697 struct btrfs_space_info *tmp;
2704 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2706 if (tmp->flags == types[i]) {
2715 down_read(&info->groups_sem);
2716 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2717 if (!list_empty(&info->block_groups[c])) {
2718 get_block_group_info(&info->block_groups[c],
2720 memcpy(dest, &space, sizeof(space));
2722 space_args.total_spaces++;
2728 up_read(&info->groups_sem);
2731 user_dest = (struct btrfs_ioctl_space_info *)
2732 (arg + sizeof(struct btrfs_ioctl_space_args));
2734 if (copy_to_user(user_dest, dest_orig, alloc_size))
2739 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2746 * there are many ways the trans_start and trans_end ioctls can lead
2747 * to deadlocks. They should only be used by applications that
2748 * basically own the machine, and have a very in depth understanding
2749 * of all the possible deadlocks and enospc problems.
2751 long btrfs_ioctl_trans_end(struct file *file)
2753 struct inode *inode = fdentry(file)->d_inode;
2754 struct btrfs_root *root = BTRFS_I(inode)->root;
2755 struct btrfs_trans_handle *trans;
2757 trans = file->private_data;
2760 file->private_data = NULL;
2762 btrfs_end_transaction(trans, root);
2764 atomic_dec(&root->fs_info->open_ioctl_trans);
2766 mnt_drop_write(file->f_path.mnt);
2770 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2772 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2773 struct btrfs_trans_handle *trans;
2777 trans = btrfs_start_transaction(root, 0);
2779 return PTR_ERR(trans);
2780 transid = trans->transid;
2781 ret = btrfs_commit_transaction_async(trans, root, 0);
2783 btrfs_end_transaction(trans, root);
2788 if (copy_to_user(argp, &transid, sizeof(transid)))
2793 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2795 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2799 if (copy_from_user(&transid, argp, sizeof(transid)))
2802 transid = 0; /* current trans */
2804 return btrfs_wait_for_commit(root, transid);
2807 static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2810 struct btrfs_ioctl_scrub_args *sa;
2812 if (!capable(CAP_SYS_ADMIN))
2815 sa = memdup_user(arg, sizeof(*sa));
2819 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
2820 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
2822 if (copy_to_user(arg, sa, sizeof(*sa)))
2829 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2831 if (!capable(CAP_SYS_ADMIN))
2834 return btrfs_scrub_cancel(root);
2837 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2840 struct btrfs_ioctl_scrub_args *sa;
2843 if (!capable(CAP_SYS_ADMIN))
2846 sa = memdup_user(arg, sizeof(*sa));
2850 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2852 if (copy_to_user(arg, sa, sizeof(*sa)))
2859 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
2863 unsigned long rel_ptr;
2865 struct btrfs_ioctl_ino_path_args *ipa;
2866 struct inode_fs_paths *ipath = NULL;
2867 struct btrfs_path *path;
2869 if (!capable(CAP_SYS_ADMIN))
2872 path = btrfs_alloc_path();
2878 ipa = memdup_user(arg, sizeof(*ipa));
2885 size = min_t(u32, ipa->size, 4096);
2886 ipath = init_ipath(size, root, path);
2887 if (IS_ERR(ipath)) {
2888 ret = PTR_ERR(ipath);
2893 ret = paths_from_inode(ipa->inum, ipath);
2897 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
2898 rel_ptr = ipath->fspath->str[i] - (char *)ipath->fspath->str;
2899 ipath->fspath->str[i] = (void *)rel_ptr;
2902 ret = copy_to_user(ipa->fspath, ipath->fspath, size);
2909 btrfs_free_path(path);
2916 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
2918 struct btrfs_data_container *inodes = ctx;
2919 const size_t c = 3 * sizeof(u64);
2921 if (inodes->bytes_left >= c) {
2922 inodes->bytes_left -= c;
2923 inodes->val[inodes->elem_cnt] = inum;
2924 inodes->val[inodes->elem_cnt + 1] = offset;
2925 inodes->val[inodes->elem_cnt + 2] = root;
2926 inodes->elem_cnt += 3;
2928 inodes->bytes_missing += c - inodes->bytes_left;
2929 inodes->bytes_left = 0;
2930 inodes->elem_missed += 3;
2936 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
2942 struct btrfs_ioctl_logical_ino_args *loi;
2943 struct btrfs_data_container *inodes = NULL;
2944 struct btrfs_path *path = NULL;
2945 struct btrfs_key key;
2947 if (!capable(CAP_SYS_ADMIN))
2950 loi = memdup_user(arg, sizeof(*loi));
2957 path = btrfs_alloc_path();
2963 size = min_t(u32, loi->size, 4096);
2964 inodes = init_data_container(size);
2965 if (IS_ERR(inodes)) {
2966 ret = PTR_ERR(inodes);
2971 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
2973 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2978 extent_offset = loi->logical - key.objectid;
2979 ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
2980 extent_offset, build_ino_list, inodes);
2985 ret = copy_to_user(loi->inodes, inodes, size);
2990 btrfs_free_path(path);
2997 long btrfs_ioctl(struct file *file, unsigned int
2998 cmd, unsigned long arg)
3000 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3001 void __user *argp = (void __user *)arg;
3004 case FS_IOC_GETFLAGS:
3005 return btrfs_ioctl_getflags(file, argp);
3006 case FS_IOC_SETFLAGS:
3007 return btrfs_ioctl_setflags(file, argp);
3008 case FS_IOC_GETVERSION:
3009 return btrfs_ioctl_getversion(file, argp);
3011 return btrfs_ioctl_fitrim(file, argp);
3012 case BTRFS_IOC_SNAP_CREATE:
3013 return btrfs_ioctl_snap_create(file, argp, 0);
3014 case BTRFS_IOC_SNAP_CREATE_V2:
3015 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3016 case BTRFS_IOC_SUBVOL_CREATE:
3017 return btrfs_ioctl_snap_create(file, argp, 1);
3018 case BTRFS_IOC_SNAP_DESTROY:
3019 return btrfs_ioctl_snap_destroy(file, argp);
3020 case BTRFS_IOC_SUBVOL_GETFLAGS:
3021 return btrfs_ioctl_subvol_getflags(file, argp);
3022 case BTRFS_IOC_SUBVOL_SETFLAGS:
3023 return btrfs_ioctl_subvol_setflags(file, argp);
3024 case BTRFS_IOC_DEFAULT_SUBVOL:
3025 return btrfs_ioctl_default_subvol(file, argp);
3026 case BTRFS_IOC_DEFRAG:
3027 return btrfs_ioctl_defrag(file, NULL);
3028 case BTRFS_IOC_DEFRAG_RANGE:
3029 return btrfs_ioctl_defrag(file, argp);
3030 case BTRFS_IOC_RESIZE:
3031 return btrfs_ioctl_resize(root, argp);
3032 case BTRFS_IOC_ADD_DEV:
3033 return btrfs_ioctl_add_dev(root, argp);
3034 case BTRFS_IOC_RM_DEV:
3035 return btrfs_ioctl_rm_dev(root, argp);
3036 case BTRFS_IOC_FS_INFO:
3037 return btrfs_ioctl_fs_info(root, argp);
3038 case BTRFS_IOC_DEV_INFO:
3039 return btrfs_ioctl_dev_info(root, argp);
3040 case BTRFS_IOC_BALANCE:
3041 return btrfs_balance(root->fs_info->dev_root);
3042 case BTRFS_IOC_CLONE:
3043 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3044 case BTRFS_IOC_CLONE_RANGE:
3045 return btrfs_ioctl_clone_range(file, argp);
3046 case BTRFS_IOC_TRANS_START:
3047 return btrfs_ioctl_trans_start(file);
3048 case BTRFS_IOC_TRANS_END:
3049 return btrfs_ioctl_trans_end(file);
3050 case BTRFS_IOC_TREE_SEARCH:
3051 return btrfs_ioctl_tree_search(file, argp);
3052 case BTRFS_IOC_INO_LOOKUP:
3053 return btrfs_ioctl_ino_lookup(file, argp);
3054 case BTRFS_IOC_INO_PATHS:
3055 return btrfs_ioctl_ino_to_path(root, argp);
3056 case BTRFS_IOC_LOGICAL_INO:
3057 return btrfs_ioctl_logical_to_ino(root, argp);
3058 case BTRFS_IOC_SPACE_INFO:
3059 return btrfs_ioctl_space_info(root, argp);
3060 case BTRFS_IOC_SYNC:
3061 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3063 case BTRFS_IOC_START_SYNC:
3064 return btrfs_ioctl_start_sync(file, argp);
3065 case BTRFS_IOC_WAIT_SYNC:
3066 return btrfs_ioctl_wait_sync(file, argp);
3067 case BTRFS_IOC_SCRUB:
3068 return btrfs_ioctl_scrub(root, argp);
3069 case BTRFS_IOC_SCRUB_CANCEL:
3070 return btrfs_ioctl_scrub_cancel(root, argp);
3071 case BTRFS_IOC_SCRUB_PROGRESS:
3072 return btrfs_ioctl_scrub_progress(root, argp);