2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/fsnotify.h>
25 #include <linux/pagemap.h>
26 #include <linux/highmem.h>
27 #include <linux/time.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mount.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/swap.h>
35 #include <linux/writeback.h>
36 #include <linux/statfs.h>
37 #include <linux/compat.h>
38 #include <linux/bit_spinlock.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/vmalloc.h>
42 #include <linux/slab.h>
46 #include "transaction.h"
47 #include "btrfs_inode.h"
49 #include "print-tree.h"
53 /* Mask out flags that are inappropriate for the given type of inode. */
54 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
61 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
69 unsigned int iflags = 0;
71 if (flags & BTRFS_INODE_SYNC)
73 if (flags & BTRFS_INODE_IMMUTABLE)
74 iflags |= FS_IMMUTABLE_FL;
75 if (flags & BTRFS_INODE_APPEND)
76 iflags |= FS_APPEND_FL;
77 if (flags & BTRFS_INODE_NODUMP)
78 iflags |= FS_NODUMP_FL;
79 if (flags & BTRFS_INODE_NOATIME)
80 iflags |= FS_NOATIME_FL;
81 if (flags & BTRFS_INODE_DIRSYNC)
82 iflags |= FS_DIRSYNC_FL;
88 * Update inode->i_flags based on the btrfs internal flags.
90 void btrfs_update_iflags(struct inode *inode)
92 struct btrfs_inode *ip = BTRFS_I(inode);
94 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
96 if (ip->flags & BTRFS_INODE_SYNC)
97 inode->i_flags |= S_SYNC;
98 if (ip->flags & BTRFS_INODE_IMMUTABLE)
99 inode->i_flags |= S_IMMUTABLE;
100 if (ip->flags & BTRFS_INODE_APPEND)
101 inode->i_flags |= S_APPEND;
102 if (ip->flags & BTRFS_INODE_NOATIME)
103 inode->i_flags |= S_NOATIME;
104 if (ip->flags & BTRFS_INODE_DIRSYNC)
105 inode->i_flags |= S_DIRSYNC;
109 * Inherit flags from the parent inode.
111 * Unlike extN we don't have any flags we don't want to inherit currently.
113 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
120 flags = BTRFS_I(dir)->flags;
122 if (S_ISREG(inode->i_mode))
123 flags &= ~BTRFS_INODE_DIRSYNC;
124 else if (!S_ISDIR(inode->i_mode))
125 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
127 BTRFS_I(inode)->flags = flags;
128 btrfs_update_iflags(inode);
131 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
133 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
134 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
136 if (copy_to_user(arg, &flags, sizeof(flags)))
141 static int check_flags(unsigned int flags)
143 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
144 FS_NOATIME_FL | FS_NODUMP_FL | \
145 FS_SYNC_FL | FS_DIRSYNC_FL | \
146 FS_NOCOMP_FL | FS_COMPR_FL | \
147 FS_NOCOW_FL | FS_COW_FL))
150 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
153 if ((flags & FS_NOCOW_FL) && (flags & FS_COW_FL))
159 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
161 struct inode *inode = file->f_path.dentry->d_inode;
162 struct btrfs_inode *ip = BTRFS_I(inode);
163 struct btrfs_root *root = ip->root;
164 struct btrfs_trans_handle *trans;
165 unsigned int flags, oldflags;
168 if (btrfs_root_readonly(root))
171 if (copy_from_user(&flags, arg, sizeof(flags)))
174 ret = check_flags(flags);
178 if (!is_owner_or_cap(inode))
181 mutex_lock(&inode->i_mutex);
183 flags = btrfs_mask_flags(inode->i_mode, flags);
184 oldflags = btrfs_flags_to_ioctl(ip->flags);
185 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
186 if (!capable(CAP_LINUX_IMMUTABLE)) {
192 ret = mnt_want_write(file->f_path.mnt);
196 if (flags & FS_SYNC_FL)
197 ip->flags |= BTRFS_INODE_SYNC;
199 ip->flags &= ~BTRFS_INODE_SYNC;
200 if (flags & FS_IMMUTABLE_FL)
201 ip->flags |= BTRFS_INODE_IMMUTABLE;
203 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
204 if (flags & FS_APPEND_FL)
205 ip->flags |= BTRFS_INODE_APPEND;
207 ip->flags &= ~BTRFS_INODE_APPEND;
208 if (flags & FS_NODUMP_FL)
209 ip->flags |= BTRFS_INODE_NODUMP;
211 ip->flags &= ~BTRFS_INODE_NODUMP;
212 if (flags & FS_NOATIME_FL)
213 ip->flags |= BTRFS_INODE_NOATIME;
215 ip->flags &= ~BTRFS_INODE_NOATIME;
216 if (flags & FS_DIRSYNC_FL)
217 ip->flags |= BTRFS_INODE_DIRSYNC;
219 ip->flags &= ~BTRFS_INODE_DIRSYNC;
222 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
223 * flag may be changed automatically if compression code won't make
226 if (flags & FS_NOCOMP_FL) {
227 ip->flags &= ~BTRFS_INODE_COMPRESS;
228 ip->flags |= BTRFS_INODE_NOCOMPRESS;
229 } else if (flags & FS_COMPR_FL) {
230 ip->flags |= BTRFS_INODE_COMPRESS;
231 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
233 if (flags & FS_NOCOW_FL)
234 ip->flags |= BTRFS_INODE_NODATACOW;
235 else if (flags & FS_COW_FL)
236 ip->flags &= ~BTRFS_INODE_NODATACOW;
238 trans = btrfs_join_transaction(root, 1);
239 BUG_ON(IS_ERR(trans));
241 ret = btrfs_update_inode(trans, root, inode);
244 btrfs_update_iflags(inode);
245 inode->i_ctime = CURRENT_TIME;
246 btrfs_end_transaction(trans, root);
248 mnt_drop_write(file->f_path.mnt);
250 mutex_unlock(&inode->i_mutex);
254 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
256 struct inode *inode = file->f_path.dentry->d_inode;
258 return put_user(inode->i_generation, arg);
261 static noinline int create_subvol(struct btrfs_root *root,
262 struct dentry *dentry,
263 char *name, int namelen,
266 struct btrfs_trans_handle *trans;
267 struct btrfs_key key;
268 struct btrfs_root_item root_item;
269 struct btrfs_inode_item *inode_item;
270 struct extent_buffer *leaf;
271 struct btrfs_root *new_root;
272 struct dentry *parent = dget_parent(dentry);
277 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
280 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
287 dir = parent->d_inode;
295 trans = btrfs_start_transaction(root, 6);
298 return PTR_ERR(trans);
301 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
302 0, objectid, NULL, 0, 0, 0);
308 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
309 btrfs_set_header_bytenr(leaf, leaf->start);
310 btrfs_set_header_generation(leaf, trans->transid);
311 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
312 btrfs_set_header_owner(leaf, objectid);
314 write_extent_buffer(leaf, root->fs_info->fsid,
315 (unsigned long)btrfs_header_fsid(leaf),
317 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
318 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
320 btrfs_mark_buffer_dirty(leaf);
322 inode_item = &root_item.inode;
323 memset(inode_item, 0, sizeof(*inode_item));
324 inode_item->generation = cpu_to_le64(1);
325 inode_item->size = cpu_to_le64(3);
326 inode_item->nlink = cpu_to_le32(1);
327 inode_item->nbytes = cpu_to_le64(root->leafsize);
328 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
330 btrfs_set_root_bytenr(&root_item, leaf->start);
331 btrfs_set_root_generation(&root_item, trans->transid);
332 btrfs_set_root_level(&root_item, 0);
333 btrfs_set_root_refs(&root_item, 1);
334 btrfs_set_root_used(&root_item, leaf->len);
335 btrfs_set_root_last_snapshot(&root_item, 0);
337 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
338 root_item.drop_level = 0;
340 btrfs_tree_unlock(leaf);
341 free_extent_buffer(leaf);
344 btrfs_set_root_dirid(&root_item, new_dirid);
346 key.objectid = objectid;
348 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
349 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
354 key.offset = (u64)-1;
355 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
356 BUG_ON(IS_ERR(new_root));
358 btrfs_record_root_in_trans(trans, new_root);
360 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
361 BTRFS_I(dir)->block_group);
363 * insert the directory item
365 ret = btrfs_set_inode_index(dir, &index);
368 ret = btrfs_insert_dir_item(trans, root,
369 name, namelen, dir->i_ino, &key,
370 BTRFS_FT_DIR, index);
374 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
375 ret = btrfs_update_inode(trans, root, dir);
378 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
379 objectid, root->root_key.objectid,
380 dir->i_ino, index, name, namelen);
384 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
388 *async_transid = trans->transid;
389 err = btrfs_commit_transaction_async(trans, root, 1);
391 err = btrfs_commit_transaction(trans, root);
398 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
399 char *name, int namelen, u64 *async_transid,
403 struct dentry *parent;
404 struct btrfs_pending_snapshot *pending_snapshot;
405 struct btrfs_trans_handle *trans;
411 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
412 if (!pending_snapshot)
415 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
416 pending_snapshot->dentry = dentry;
417 pending_snapshot->root = root;
418 pending_snapshot->readonly = readonly;
420 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
422 ret = PTR_ERR(trans);
426 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
429 list_add(&pending_snapshot->list,
430 &trans->transaction->pending_snapshots);
432 *async_transid = trans->transid;
433 ret = btrfs_commit_transaction_async(trans,
434 root->fs_info->extent_root, 1);
436 ret = btrfs_commit_transaction(trans,
437 root->fs_info->extent_root);
441 ret = pending_snapshot->error;
445 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
449 parent = dget_parent(dentry);
450 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
453 ret = PTR_ERR(inode);
457 d_instantiate(dentry, inode);
460 kfree(pending_snapshot);
464 /* copy of check_sticky in fs/namei.c()
465 * It's inline, so penalty for filesystems that don't use sticky bit is
468 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
470 uid_t fsuid = current_fsuid();
472 if (!(dir->i_mode & S_ISVTX))
474 if (inode->i_uid == fsuid)
476 if (dir->i_uid == fsuid)
478 return !capable(CAP_FOWNER);
481 /* copy of may_delete in fs/namei.c()
482 * Check whether we can remove a link victim from directory dir, check
483 * whether the type of victim is right.
484 * 1. We can't do it if dir is read-only (done in permission())
485 * 2. We should have write and exec permissions on dir
486 * 3. We can't remove anything from append-only dir
487 * 4. We can't do anything with immutable dir (done in permission())
488 * 5. If the sticky bit on dir is set we should either
489 * a. be owner of dir, or
490 * b. be owner of victim, or
491 * c. have CAP_FOWNER capability
492 * 6. If the victim is append-only or immutable we can't do antyhing with
493 * links pointing to it.
494 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
495 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
496 * 9. We can't remove a root or mountpoint.
497 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
498 * nfs_async_unlink().
501 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
505 if (!victim->d_inode)
508 BUG_ON(victim->d_parent->d_inode != dir);
509 audit_inode_child(victim, dir);
511 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
516 if (btrfs_check_sticky(dir, victim->d_inode)||
517 IS_APPEND(victim->d_inode)||
518 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
521 if (!S_ISDIR(victim->d_inode->i_mode))
525 } else if (S_ISDIR(victim->d_inode->i_mode))
529 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
534 /* copy of may_create in fs/namei.c() */
535 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
541 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
545 * Create a new subvolume below @parent. This is largely modeled after
546 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
547 * inside this filesystem so it's quite a bit simpler.
549 static noinline int btrfs_mksubvol(struct path *parent,
550 char *name, int namelen,
551 struct btrfs_root *snap_src,
552 u64 *async_transid, bool readonly)
554 struct inode *dir = parent->dentry->d_inode;
555 struct dentry *dentry;
558 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
560 dentry = lookup_one_len(name, parent->dentry, namelen);
561 error = PTR_ERR(dentry);
569 error = mnt_want_write(parent->mnt);
573 error = btrfs_may_create(dir, dentry);
577 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
579 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
583 error = create_snapshot(snap_src, dentry,
584 name, namelen, async_transid, readonly);
586 error = create_subvol(BTRFS_I(dir)->root, dentry,
587 name, namelen, async_transid);
590 fsnotify_mkdir(dir, dentry);
592 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
594 mnt_drop_write(parent->mnt);
598 mutex_unlock(&dir->i_mutex);
602 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
603 int thresh, u64 *last_len, u64 *skip,
606 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
607 struct extent_map *em = NULL;
608 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
616 * make sure that once we start defragging and extent, we keep on
619 if (start < *defrag_end)
625 * hopefully we have this extent in the tree already, try without
626 * the full extent lock
628 read_lock(&em_tree->lock);
629 em = lookup_extent_mapping(em_tree, start, len);
630 read_unlock(&em_tree->lock);
633 /* get the big lock and read metadata off disk */
634 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
635 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
636 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
642 /* this will cover holes, and inline extents */
643 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
647 * we hit a real extent, if it is big don't bother defragging it again
649 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
653 * last_len ends up being a counter of how many bytes we've defragged.
654 * every time we choose not to defrag an extent, we reset *last_len
655 * so that the next tiny extent will force a defrag.
657 * The end result of this is that tiny extents before a single big
658 * extent will force at least part of that big extent to be defragged.
662 *defrag_end = extent_map_end(em);
665 *skip = extent_map_end(em);
673 static int btrfs_defrag_file(struct file *file,
674 struct btrfs_ioctl_defrag_range_args *range)
676 struct inode *inode = fdentry(file)->d_inode;
677 struct btrfs_root *root = BTRFS_I(inode)->root;
678 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
679 struct btrfs_ordered_extent *ordered;
681 struct btrfs_super_block *disk_super;
682 unsigned long last_index;
683 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
684 unsigned long total_read = 0;
693 int compress_type = BTRFS_COMPRESS_ZLIB;
695 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
696 if (range->compress_type > BTRFS_COMPRESS_TYPES)
698 if (range->compress_type)
699 compress_type = range->compress_type;
702 if (inode->i_size == 0)
705 if (range->start + range->len > range->start) {
706 last_index = min_t(u64, inode->i_size - 1,
707 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
709 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
712 i = range->start >> PAGE_CACHE_SHIFT;
713 while (i <= last_index) {
714 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
716 range->extent_thresh,
721 * the should_defrag function tells us how much to skip
722 * bump our counter by the suggested amount
724 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
725 i = max(i + 1, next);
729 if (total_read % ra_pages == 0) {
730 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
731 min(last_index, i + ra_pages - 1));
734 mutex_lock(&inode->i_mutex);
735 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
736 BTRFS_I(inode)->force_compress = compress_type;
738 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
742 if (inode->i_size == 0 ||
743 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
745 goto err_reservations;
748 page = grab_cache_page(inode->i_mapping, i);
751 goto err_reservations;
754 if (!PageUptodate(page)) {
755 btrfs_readpage(NULL, page);
757 if (!PageUptodate(page)) {
759 page_cache_release(page);
761 goto err_reservations;
765 if (page->mapping != inode->i_mapping) {
767 page_cache_release(page);
771 wait_on_page_writeback(page);
773 if (PageDirty(page)) {
774 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
778 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
779 page_end = page_start + PAGE_CACHE_SIZE - 1;
780 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
782 ordered = btrfs_lookup_ordered_extent(inode, page_start);
784 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
786 page_cache_release(page);
787 btrfs_start_ordered_extent(inode, ordered, 1);
788 btrfs_put_ordered_extent(ordered);
791 set_page_extent_mapped(page);
794 * this makes sure page_mkwrite is called on the
795 * page if it is dirtied again later
797 clear_page_dirty_for_io(page);
798 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
799 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
800 EXTENT_DO_ACCOUNTING, GFP_NOFS);
802 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
803 ClearPageChecked(page);
804 set_page_dirty(page);
805 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
809 page_cache_release(page);
810 mutex_unlock(&inode->i_mutex);
812 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
816 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
817 filemap_flush(inode->i_mapping);
819 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
820 /* the filemap_flush will queue IO into the worker threads, but
821 * we have to make sure the IO is actually started and that
822 * ordered extents get created before we return
824 atomic_inc(&root->fs_info->async_submit_draining);
825 while (atomic_read(&root->fs_info->nr_async_submits) ||
826 atomic_read(&root->fs_info->async_delalloc_pages)) {
827 wait_event(root->fs_info->async_submit_wait,
828 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
829 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
831 atomic_dec(&root->fs_info->async_submit_draining);
833 mutex_lock(&inode->i_mutex);
834 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
835 mutex_unlock(&inode->i_mutex);
838 disk_super = &root->fs_info->super_copy;
839 features = btrfs_super_incompat_flags(disk_super);
840 if (range->compress_type == BTRFS_COMPRESS_LZO) {
841 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
842 btrfs_set_super_incompat_flags(disk_super, features);
848 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
850 mutex_unlock(&inode->i_mutex);
854 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
860 struct btrfs_ioctl_vol_args *vol_args;
861 struct btrfs_trans_handle *trans;
862 struct btrfs_device *device = NULL;
868 if (root->fs_info->sb->s_flags & MS_RDONLY)
871 if (!capable(CAP_SYS_ADMIN))
874 vol_args = memdup_user(arg, sizeof(*vol_args));
875 if (IS_ERR(vol_args))
876 return PTR_ERR(vol_args);
878 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
880 mutex_lock(&root->fs_info->volume_mutex);
881 sizestr = vol_args->name;
882 devstr = strchr(sizestr, ':');
885 sizestr = devstr + 1;
887 devstr = vol_args->name;
888 devid = simple_strtoull(devstr, &end, 10);
889 printk(KERN_INFO "resizing devid %llu\n",
890 (unsigned long long)devid);
892 device = btrfs_find_device(root, devid, NULL, NULL);
894 printk(KERN_INFO "resizer unable to find device %llu\n",
895 (unsigned long long)devid);
899 if (!strcmp(sizestr, "max"))
900 new_size = device->bdev->bd_inode->i_size;
902 if (sizestr[0] == '-') {
905 } else if (sizestr[0] == '+') {
909 new_size = memparse(sizestr, NULL);
916 old_size = device->total_bytes;
919 if (new_size > old_size) {
923 new_size = old_size - new_size;
924 } else if (mod > 0) {
925 new_size = old_size + new_size;
928 if (new_size < 256 * 1024 * 1024) {
932 if (new_size > device->bdev->bd_inode->i_size) {
937 do_div(new_size, root->sectorsize);
938 new_size *= root->sectorsize;
940 printk(KERN_INFO "new size for %s is %llu\n",
941 device->name, (unsigned long long)new_size);
943 if (new_size > old_size) {
944 trans = btrfs_start_transaction(root, 0);
946 ret = PTR_ERR(trans);
949 ret = btrfs_grow_device(trans, device, new_size);
950 btrfs_commit_transaction(trans, root);
952 ret = btrfs_shrink_device(device, new_size);
956 mutex_unlock(&root->fs_info->volume_mutex);
961 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
968 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
969 struct file *src_file;
973 if (root->fs_info->sb->s_flags & MS_RDONLY)
976 namelen = strlen(name);
977 if (strchr(name, '/')) {
983 ret = btrfs_mksubvol(&file->f_path, name, namelen,
984 NULL, transid, readonly);
986 struct inode *src_inode;
993 src_inode = src_file->f_path.dentry->d_inode;
994 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
995 printk(KERN_INFO "btrfs: Snapshot src from "
1001 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1002 BTRFS_I(src_inode)->root,
1010 static noinline int btrfs_ioctl_snap_create(struct file *file,
1011 void __user *arg, int subvol)
1013 struct btrfs_ioctl_vol_args *vol_args;
1016 vol_args = memdup_user(arg, sizeof(*vol_args));
1017 if (IS_ERR(vol_args))
1018 return PTR_ERR(vol_args);
1019 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1021 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1022 vol_args->fd, subvol,
1029 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1030 void __user *arg, int subvol)
1032 struct btrfs_ioctl_vol_args_v2 *vol_args;
1036 bool readonly = false;
1038 vol_args = memdup_user(arg, sizeof(*vol_args));
1039 if (IS_ERR(vol_args))
1040 return PTR_ERR(vol_args);
1041 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1043 if (vol_args->flags &
1044 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1049 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1051 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1054 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1055 vol_args->fd, subvol,
1058 if (ret == 0 && ptr &&
1060 offsetof(struct btrfs_ioctl_vol_args_v2,
1061 transid), ptr, sizeof(*ptr)))
1068 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1071 struct inode *inode = fdentry(file)->d_inode;
1072 struct btrfs_root *root = BTRFS_I(inode)->root;
1076 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1079 down_read(&root->fs_info->subvol_sem);
1080 if (btrfs_root_readonly(root))
1081 flags |= BTRFS_SUBVOL_RDONLY;
1082 up_read(&root->fs_info->subvol_sem);
1084 if (copy_to_user(arg, &flags, sizeof(flags)))
1090 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1093 struct inode *inode = fdentry(file)->d_inode;
1094 struct btrfs_root *root = BTRFS_I(inode)->root;
1095 struct btrfs_trans_handle *trans;
1100 if (root->fs_info->sb->s_flags & MS_RDONLY)
1103 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1106 if (copy_from_user(&flags, arg, sizeof(flags)))
1109 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
1112 if (flags & ~BTRFS_SUBVOL_RDONLY)
1115 if (!is_owner_or_cap(inode))
1118 down_write(&root->fs_info->subvol_sem);
1121 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1124 root_flags = btrfs_root_flags(&root->root_item);
1125 if (flags & BTRFS_SUBVOL_RDONLY)
1126 btrfs_set_root_flags(&root->root_item,
1127 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1129 btrfs_set_root_flags(&root->root_item,
1130 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1132 trans = btrfs_start_transaction(root, 1);
1133 if (IS_ERR(trans)) {
1134 ret = PTR_ERR(trans);
1138 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1139 &root->root_key, &root->root_item);
1141 btrfs_commit_transaction(trans, root);
1144 btrfs_set_root_flags(&root->root_item, root_flags);
1146 up_write(&root->fs_info->subvol_sem);
1151 * helper to check if the subvolume references other subvolumes
1153 static noinline int may_destroy_subvol(struct btrfs_root *root)
1155 struct btrfs_path *path;
1156 struct btrfs_key key;
1159 path = btrfs_alloc_path();
1163 key.objectid = root->root_key.objectid;
1164 key.type = BTRFS_ROOT_REF_KEY;
1165 key.offset = (u64)-1;
1167 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1174 if (path->slots[0] > 0) {
1176 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1177 if (key.objectid == root->root_key.objectid &&
1178 key.type == BTRFS_ROOT_REF_KEY)
1182 btrfs_free_path(path);
1186 static noinline int key_in_sk(struct btrfs_key *key,
1187 struct btrfs_ioctl_search_key *sk)
1189 struct btrfs_key test;
1192 test.objectid = sk->min_objectid;
1193 test.type = sk->min_type;
1194 test.offset = sk->min_offset;
1196 ret = btrfs_comp_cpu_keys(key, &test);
1200 test.objectid = sk->max_objectid;
1201 test.type = sk->max_type;
1202 test.offset = sk->max_offset;
1204 ret = btrfs_comp_cpu_keys(key, &test);
1210 static noinline int copy_to_sk(struct btrfs_root *root,
1211 struct btrfs_path *path,
1212 struct btrfs_key *key,
1213 struct btrfs_ioctl_search_key *sk,
1215 unsigned long *sk_offset,
1219 struct extent_buffer *leaf;
1220 struct btrfs_ioctl_search_header sh;
1221 unsigned long item_off;
1222 unsigned long item_len;
1229 leaf = path->nodes[0];
1230 slot = path->slots[0];
1231 nritems = btrfs_header_nritems(leaf);
1233 if (btrfs_header_generation(leaf) > sk->max_transid) {
1237 found_transid = btrfs_header_generation(leaf);
1239 for (i = slot; i < nritems; i++) {
1240 item_off = btrfs_item_ptr_offset(leaf, i);
1241 item_len = btrfs_item_size_nr(leaf, i);
1243 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1246 if (sizeof(sh) + item_len + *sk_offset >
1247 BTRFS_SEARCH_ARGS_BUFSIZE) {
1252 btrfs_item_key_to_cpu(leaf, key, i);
1253 if (!key_in_sk(key, sk))
1256 sh.objectid = key->objectid;
1257 sh.offset = key->offset;
1258 sh.type = key->type;
1260 sh.transid = found_transid;
1262 /* copy search result header */
1263 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1264 *sk_offset += sizeof(sh);
1267 char *p = buf + *sk_offset;
1269 read_extent_buffer(leaf, p,
1270 item_off, item_len);
1271 *sk_offset += item_len;
1275 if (*num_found >= sk->nr_items)
1280 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1282 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1285 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1292 *num_found += found;
1296 static noinline int search_ioctl(struct inode *inode,
1297 struct btrfs_ioctl_search_args *args)
1299 struct btrfs_root *root;
1300 struct btrfs_key key;
1301 struct btrfs_key max_key;
1302 struct btrfs_path *path;
1303 struct btrfs_ioctl_search_key *sk = &args->key;
1304 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1307 unsigned long sk_offset = 0;
1309 path = btrfs_alloc_path();
1313 if (sk->tree_id == 0) {
1314 /* search the root of the inode that was passed */
1315 root = BTRFS_I(inode)->root;
1317 key.objectid = sk->tree_id;
1318 key.type = BTRFS_ROOT_ITEM_KEY;
1319 key.offset = (u64)-1;
1320 root = btrfs_read_fs_root_no_name(info, &key);
1322 printk(KERN_ERR "could not find root %llu\n",
1324 btrfs_free_path(path);
1329 key.objectid = sk->min_objectid;
1330 key.type = sk->min_type;
1331 key.offset = sk->min_offset;
1333 max_key.objectid = sk->max_objectid;
1334 max_key.type = sk->max_type;
1335 max_key.offset = sk->max_offset;
1337 path->keep_locks = 1;
1340 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1347 ret = copy_to_sk(root, path, &key, sk, args->buf,
1348 &sk_offset, &num_found);
1349 btrfs_release_path(root, path);
1350 if (ret || num_found >= sk->nr_items)
1356 sk->nr_items = num_found;
1357 btrfs_free_path(path);
1361 static noinline int btrfs_ioctl_tree_search(struct file *file,
1364 struct btrfs_ioctl_search_args *args;
1365 struct inode *inode;
1368 if (!capable(CAP_SYS_ADMIN))
1371 args = memdup_user(argp, sizeof(*args));
1373 return PTR_ERR(args);
1375 inode = fdentry(file)->d_inode;
1376 ret = search_ioctl(inode, args);
1377 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1384 * Search INODE_REFs to identify path name of 'dirid' directory
1385 * in a 'tree_id' tree. and sets path name to 'name'.
1387 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1388 u64 tree_id, u64 dirid, char *name)
1390 struct btrfs_root *root;
1391 struct btrfs_key key;
1397 struct btrfs_inode_ref *iref;
1398 struct extent_buffer *l;
1399 struct btrfs_path *path;
1401 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1406 path = btrfs_alloc_path();
1410 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1412 key.objectid = tree_id;
1413 key.type = BTRFS_ROOT_ITEM_KEY;
1414 key.offset = (u64)-1;
1415 root = btrfs_read_fs_root_no_name(info, &key);
1417 printk(KERN_ERR "could not find root %llu\n", tree_id);
1422 key.objectid = dirid;
1423 key.type = BTRFS_INODE_REF_KEY;
1424 key.offset = (u64)-1;
1427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1432 slot = path->slots[0];
1433 if (ret > 0 && slot > 0)
1435 btrfs_item_key_to_cpu(l, &key, slot);
1437 if (ret > 0 && (key.objectid != dirid ||
1438 key.type != BTRFS_INODE_REF_KEY)) {
1443 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1444 len = btrfs_inode_ref_name_len(l, iref);
1446 total_len += len + 1;
1451 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1453 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1456 btrfs_release_path(root, path);
1457 key.objectid = key.offset;
1458 key.offset = (u64)-1;
1459 dirid = key.objectid;
1464 memcpy(name, ptr, total_len);
1465 name[total_len]='\0';
1468 btrfs_free_path(path);
1472 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1475 struct btrfs_ioctl_ino_lookup_args *args;
1476 struct inode *inode;
1479 if (!capable(CAP_SYS_ADMIN))
1482 args = memdup_user(argp, sizeof(*args));
1484 return PTR_ERR(args);
1486 inode = fdentry(file)->d_inode;
1488 if (args->treeid == 0)
1489 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1491 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1492 args->treeid, args->objectid,
1495 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1502 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1505 struct dentry *parent = fdentry(file);
1506 struct dentry *dentry;
1507 struct inode *dir = parent->d_inode;
1508 struct inode *inode;
1509 struct btrfs_root *root = BTRFS_I(dir)->root;
1510 struct btrfs_root *dest = NULL;
1511 struct btrfs_ioctl_vol_args *vol_args;
1512 struct btrfs_trans_handle *trans;
1517 vol_args = memdup_user(arg, sizeof(*vol_args));
1518 if (IS_ERR(vol_args))
1519 return PTR_ERR(vol_args);
1521 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1522 namelen = strlen(vol_args->name);
1523 if (strchr(vol_args->name, '/') ||
1524 strncmp(vol_args->name, "..", namelen) == 0) {
1529 err = mnt_want_write(file->f_path.mnt);
1533 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1534 dentry = lookup_one_len(vol_args->name, parent, namelen);
1535 if (IS_ERR(dentry)) {
1536 err = PTR_ERR(dentry);
1537 goto out_unlock_dir;
1540 if (!dentry->d_inode) {
1545 inode = dentry->d_inode;
1546 dest = BTRFS_I(inode)->root;
1547 if (!capable(CAP_SYS_ADMIN)){
1549 * Regular user. Only allow this with a special mount
1550 * option, when the user has write+exec access to the
1551 * subvol root, and when rmdir(2) would have been
1554 * Note that this is _not_ check that the subvol is
1555 * empty or doesn't contain data that we wouldn't
1556 * otherwise be able to delete.
1558 * Users who want to delete empty subvols should try
1562 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1566 * Do not allow deletion if the parent dir is the same
1567 * as the dir to be deleted. That means the ioctl
1568 * must be called on the dentry referencing the root
1569 * of the subvol, not a random directory contained
1576 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1580 /* check if subvolume may be deleted by a non-root user */
1581 err = btrfs_may_delete(dir, dentry, 1);
1586 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1591 mutex_lock(&inode->i_mutex);
1592 err = d_invalidate(dentry);
1596 down_write(&root->fs_info->subvol_sem);
1598 err = may_destroy_subvol(dest);
1602 trans = btrfs_start_transaction(root, 0);
1603 if (IS_ERR(trans)) {
1604 err = PTR_ERR(trans);
1607 trans->block_rsv = &root->fs_info->global_block_rsv;
1609 ret = btrfs_unlink_subvol(trans, root, dir,
1610 dest->root_key.objectid,
1611 dentry->d_name.name,
1612 dentry->d_name.len);
1615 btrfs_record_root_in_trans(trans, dest);
1617 memset(&dest->root_item.drop_progress, 0,
1618 sizeof(dest->root_item.drop_progress));
1619 dest->root_item.drop_level = 0;
1620 btrfs_set_root_refs(&dest->root_item, 0);
1622 if (!xchg(&dest->orphan_item_inserted, 1)) {
1623 ret = btrfs_insert_orphan_item(trans,
1624 root->fs_info->tree_root,
1625 dest->root_key.objectid);
1629 ret = btrfs_end_transaction(trans, root);
1631 inode->i_flags |= S_DEAD;
1633 up_write(&root->fs_info->subvol_sem);
1635 mutex_unlock(&inode->i_mutex);
1637 shrink_dcache_sb(root->fs_info->sb);
1638 btrfs_invalidate_inodes(dest);
1644 mutex_unlock(&dir->i_mutex);
1645 mnt_drop_write(file->f_path.mnt);
1651 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1653 struct inode *inode = fdentry(file)->d_inode;
1654 struct btrfs_root *root = BTRFS_I(inode)->root;
1655 struct btrfs_ioctl_defrag_range_args *range;
1658 if (btrfs_root_readonly(root))
1661 ret = mnt_want_write(file->f_path.mnt);
1665 switch (inode->i_mode & S_IFMT) {
1667 if (!capable(CAP_SYS_ADMIN)) {
1671 ret = btrfs_defrag_root(root, 0);
1674 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1677 if (!(file->f_mode & FMODE_WRITE)) {
1682 range = kzalloc(sizeof(*range), GFP_KERNEL);
1689 if (copy_from_user(range, argp,
1695 /* compression requires us to start the IO */
1696 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1697 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1698 range->extent_thresh = (u32)-1;
1701 /* the rest are all set to zero by kzalloc */
1702 range->len = (u64)-1;
1704 ret = btrfs_defrag_file(file, range);
1711 mnt_drop_write(file->f_path.mnt);
1715 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1717 struct btrfs_ioctl_vol_args *vol_args;
1720 if (!capable(CAP_SYS_ADMIN))
1723 vol_args = memdup_user(arg, sizeof(*vol_args));
1724 if (IS_ERR(vol_args))
1725 return PTR_ERR(vol_args);
1727 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1728 ret = btrfs_init_new_device(root, vol_args->name);
1734 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1736 struct btrfs_ioctl_vol_args *vol_args;
1739 if (!capable(CAP_SYS_ADMIN))
1742 if (root->fs_info->sb->s_flags & MS_RDONLY)
1745 vol_args = memdup_user(arg, sizeof(*vol_args));
1746 if (IS_ERR(vol_args))
1747 return PTR_ERR(vol_args);
1749 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1750 ret = btrfs_rm_device(root, vol_args->name);
1756 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1757 u64 off, u64 olen, u64 destoff)
1759 struct inode *inode = fdentry(file)->d_inode;
1760 struct btrfs_root *root = BTRFS_I(inode)->root;
1761 struct file *src_file;
1763 struct btrfs_trans_handle *trans;
1764 struct btrfs_path *path;
1765 struct extent_buffer *leaf;
1767 struct btrfs_key key;
1772 u64 bs = root->fs_info->sb->s_blocksize;
1777 * - split compressed inline extents. annoying: we need to
1778 * decompress into destination's address_space (the file offset
1779 * may change, so source mapping won't do), then recompress (or
1780 * otherwise reinsert) a subrange.
1781 * - allow ranges within the same file to be cloned (provided
1782 * they don't overlap)?
1785 /* the destination must be opened for writing */
1786 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1789 if (btrfs_root_readonly(root))
1792 ret = mnt_want_write(file->f_path.mnt);
1796 src_file = fget(srcfd);
1799 goto out_drop_write;
1802 src = src_file->f_dentry->d_inode;
1808 /* the src must be open for reading */
1809 if (!(src_file->f_mode & FMODE_READ))
1813 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1817 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1821 buf = vmalloc(btrfs_level_size(root, 0));
1825 path = btrfs_alloc_path();
1833 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1834 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
1836 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1837 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1840 /* determine range to clone */
1842 if (off + len > src->i_size || off + len < off)
1845 olen = len = src->i_size - off;
1846 /* if we extend to eof, continue to block boundary */
1847 if (off + len == src->i_size)
1848 len = ALIGN(src->i_size, bs) - off;
1850 /* verify the end result is block aligned */
1851 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1852 !IS_ALIGNED(destoff, bs))
1855 /* do any pending delalloc/csum calc on src, one way or
1856 another, and lock file content */
1858 struct btrfs_ordered_extent *ordered;
1859 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1860 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1862 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1863 EXTENT_DELALLOC, 0, NULL))
1865 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1867 btrfs_put_ordered_extent(ordered);
1868 btrfs_wait_ordered_range(src, off, len);
1872 key.objectid = src->i_ino;
1873 key.type = BTRFS_EXTENT_DATA_KEY;
1878 * note the key will change type as we walk through the
1881 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1885 nritems = btrfs_header_nritems(path->nodes[0]);
1886 if (path->slots[0] >= nritems) {
1887 ret = btrfs_next_leaf(root, path);
1892 nritems = btrfs_header_nritems(path->nodes[0]);
1894 leaf = path->nodes[0];
1895 slot = path->slots[0];
1897 btrfs_item_key_to_cpu(leaf, &key, slot);
1898 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1899 key.objectid != src->i_ino)
1902 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1903 struct btrfs_file_extent_item *extent;
1906 struct btrfs_key new_key;
1907 u64 disko = 0, diskl = 0;
1908 u64 datao = 0, datal = 0;
1912 size = btrfs_item_size_nr(leaf, slot);
1913 read_extent_buffer(leaf, buf,
1914 btrfs_item_ptr_offset(leaf, slot),
1917 extent = btrfs_item_ptr(leaf, slot,
1918 struct btrfs_file_extent_item);
1919 comp = btrfs_file_extent_compression(leaf, extent);
1920 type = btrfs_file_extent_type(leaf, extent);
1921 if (type == BTRFS_FILE_EXTENT_REG ||
1922 type == BTRFS_FILE_EXTENT_PREALLOC) {
1923 disko = btrfs_file_extent_disk_bytenr(leaf,
1925 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1927 datao = btrfs_file_extent_offset(leaf, extent);
1928 datal = btrfs_file_extent_num_bytes(leaf,
1930 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1931 /* take upper bound, may be compressed */
1932 datal = btrfs_file_extent_ram_bytes(leaf,
1935 btrfs_release_path(root, path);
1937 if (key.offset + datal <= off ||
1938 key.offset >= off+len)
1941 memcpy(&new_key, &key, sizeof(new_key));
1942 new_key.objectid = inode->i_ino;
1943 if (off <= key.offset)
1944 new_key.offset = key.offset + destoff - off;
1946 new_key.offset = destoff;
1948 trans = btrfs_start_transaction(root, 1);
1949 if (IS_ERR(trans)) {
1950 ret = PTR_ERR(trans);
1954 if (type == BTRFS_FILE_EXTENT_REG ||
1955 type == BTRFS_FILE_EXTENT_PREALLOC) {
1956 if (off > key.offset) {
1957 datao += off - key.offset;
1958 datal -= off - key.offset;
1961 if (key.offset + datal > off + len)
1962 datal = off + len - key.offset;
1964 ret = btrfs_drop_extents(trans, inode,
1966 new_key.offset + datal,
1970 ret = btrfs_insert_empty_item(trans, root, path,
1974 leaf = path->nodes[0];
1975 slot = path->slots[0];
1976 write_extent_buffer(leaf, buf,
1977 btrfs_item_ptr_offset(leaf, slot),
1980 extent = btrfs_item_ptr(leaf, slot,
1981 struct btrfs_file_extent_item);
1983 /* disko == 0 means it's a hole */
1987 btrfs_set_file_extent_offset(leaf, extent,
1989 btrfs_set_file_extent_num_bytes(leaf, extent,
1992 inode_add_bytes(inode, datal);
1993 ret = btrfs_inc_extent_ref(trans, root,
1995 root->root_key.objectid,
1997 new_key.offset - datao);
2000 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2003 if (off > key.offset) {
2004 skip = off - key.offset;
2005 new_key.offset += skip;
2008 if (key.offset + datal > off+len)
2009 trim = key.offset + datal - (off+len);
2011 if (comp && (skip || trim)) {
2013 btrfs_end_transaction(trans, root);
2016 size -= skip + trim;
2017 datal -= skip + trim;
2019 ret = btrfs_drop_extents(trans, inode,
2021 new_key.offset + datal,
2025 ret = btrfs_insert_empty_item(trans, root, path,
2031 btrfs_file_extent_calc_inline_size(0);
2032 memmove(buf+start, buf+start+skip,
2036 leaf = path->nodes[0];
2037 slot = path->slots[0];
2038 write_extent_buffer(leaf, buf,
2039 btrfs_item_ptr_offset(leaf, slot),
2041 inode_add_bytes(inode, datal);
2044 btrfs_mark_buffer_dirty(leaf);
2045 btrfs_release_path(root, path);
2047 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2050 * we round up to the block size at eof when
2051 * determining which extents to clone above,
2052 * but shouldn't round up the file size
2054 endoff = new_key.offset + datal;
2055 if (endoff > destoff+olen)
2056 endoff = destoff+olen;
2057 if (endoff > inode->i_size)
2058 btrfs_i_size_write(inode, endoff);
2060 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2061 ret = btrfs_update_inode(trans, root, inode);
2063 btrfs_end_transaction(trans, root);
2066 btrfs_release_path(root, path);
2071 btrfs_release_path(root, path);
2072 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2074 mutex_unlock(&src->i_mutex);
2075 mutex_unlock(&inode->i_mutex);
2077 btrfs_free_path(path);
2081 mnt_drop_write(file->f_path.mnt);
2085 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2087 struct btrfs_ioctl_clone_range_args args;
2089 if (copy_from_user(&args, argp, sizeof(args)))
2091 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2092 args.src_length, args.dest_offset);
2096 * there are many ways the trans_start and trans_end ioctls can lead
2097 * to deadlocks. They should only be used by applications that
2098 * basically own the machine, and have a very in depth understanding
2099 * of all the possible deadlocks and enospc problems.
2101 static long btrfs_ioctl_trans_start(struct file *file)
2103 struct inode *inode = fdentry(file)->d_inode;
2104 struct btrfs_root *root = BTRFS_I(inode)->root;
2105 struct btrfs_trans_handle *trans;
2109 if (!capable(CAP_SYS_ADMIN))
2113 if (file->private_data)
2117 if (btrfs_root_readonly(root))
2120 ret = mnt_want_write(file->f_path.mnt);
2124 mutex_lock(&root->fs_info->trans_mutex);
2125 root->fs_info->open_ioctl_trans++;
2126 mutex_unlock(&root->fs_info->trans_mutex);
2129 trans = btrfs_start_ioctl_transaction(root, 0);
2133 file->private_data = trans;
2137 mutex_lock(&root->fs_info->trans_mutex);
2138 root->fs_info->open_ioctl_trans--;
2139 mutex_unlock(&root->fs_info->trans_mutex);
2140 mnt_drop_write(file->f_path.mnt);
2145 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2147 struct inode *inode = fdentry(file)->d_inode;
2148 struct btrfs_root *root = BTRFS_I(inode)->root;
2149 struct btrfs_root *new_root;
2150 struct btrfs_dir_item *di;
2151 struct btrfs_trans_handle *trans;
2152 struct btrfs_path *path;
2153 struct btrfs_key location;
2154 struct btrfs_disk_key disk_key;
2155 struct btrfs_super_block *disk_super;
2160 if (!capable(CAP_SYS_ADMIN))
2163 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2167 objectid = root->root_key.objectid;
2169 location.objectid = objectid;
2170 location.type = BTRFS_ROOT_ITEM_KEY;
2171 location.offset = (u64)-1;
2173 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2174 if (IS_ERR(new_root))
2175 return PTR_ERR(new_root);
2177 if (btrfs_root_refs(&new_root->root_item) == 0)
2180 path = btrfs_alloc_path();
2183 path->leave_spinning = 1;
2185 trans = btrfs_start_transaction(root, 1);
2186 if (IS_ERR(trans)) {
2187 btrfs_free_path(path);
2188 return PTR_ERR(trans);
2191 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2192 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2193 dir_id, "default", 7, 1);
2194 if (IS_ERR_OR_NULL(di)) {
2195 btrfs_free_path(path);
2196 btrfs_end_transaction(trans, root);
2197 printk(KERN_ERR "Umm, you don't have the default dir item, "
2198 "this isn't going to work\n");
2202 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2203 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2204 btrfs_mark_buffer_dirty(path->nodes[0]);
2205 btrfs_free_path(path);
2207 disk_super = &root->fs_info->super_copy;
2208 features = btrfs_super_incompat_flags(disk_super);
2209 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2210 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2211 btrfs_set_super_incompat_flags(disk_super, features);
2213 btrfs_end_transaction(trans, root);
2218 static void get_block_group_info(struct list_head *groups_list,
2219 struct btrfs_ioctl_space_info *space)
2221 struct btrfs_block_group_cache *block_group;
2223 space->total_bytes = 0;
2224 space->used_bytes = 0;
2226 list_for_each_entry(block_group, groups_list, list) {
2227 space->flags = block_group->flags;
2228 space->total_bytes += block_group->key.offset;
2229 space->used_bytes +=
2230 btrfs_block_group_used(&block_group->item);
2234 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2236 struct btrfs_ioctl_space_args space_args;
2237 struct btrfs_ioctl_space_info space;
2238 struct btrfs_ioctl_space_info *dest;
2239 struct btrfs_ioctl_space_info *dest_orig;
2240 struct btrfs_ioctl_space_info *user_dest;
2241 struct btrfs_space_info *info;
2242 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2243 BTRFS_BLOCK_GROUP_SYSTEM,
2244 BTRFS_BLOCK_GROUP_METADATA,
2245 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2252 if (copy_from_user(&space_args,
2253 (struct btrfs_ioctl_space_args __user *)arg,
2254 sizeof(space_args)))
2257 for (i = 0; i < num_types; i++) {
2258 struct btrfs_space_info *tmp;
2262 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2264 if (tmp->flags == types[i]) {
2274 down_read(&info->groups_sem);
2275 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2276 if (!list_empty(&info->block_groups[c]))
2279 up_read(&info->groups_sem);
2282 /* space_slots == 0 means they are asking for a count */
2283 if (space_args.space_slots == 0) {
2284 space_args.total_spaces = slot_count;
2288 slot_count = min_t(u64, space_args.space_slots, slot_count);
2290 alloc_size = sizeof(*dest) * slot_count;
2292 /* we generally have at most 6 or so space infos, one for each raid
2293 * level. So, a whole page should be more than enough for everyone
2295 if (alloc_size > PAGE_CACHE_SIZE)
2298 space_args.total_spaces = 0;
2299 dest = kmalloc(alloc_size, GFP_NOFS);
2304 /* now we have a buffer to copy into */
2305 for (i = 0; i < num_types; i++) {
2306 struct btrfs_space_info *tmp;
2313 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2315 if (tmp->flags == types[i]) {
2324 down_read(&info->groups_sem);
2325 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2326 if (!list_empty(&info->block_groups[c])) {
2327 get_block_group_info(&info->block_groups[c],
2329 memcpy(dest, &space, sizeof(space));
2331 space_args.total_spaces++;
2337 up_read(&info->groups_sem);
2340 user_dest = (struct btrfs_ioctl_space_info *)
2341 (arg + sizeof(struct btrfs_ioctl_space_args));
2343 if (copy_to_user(user_dest, dest_orig, alloc_size))
2348 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2355 * there are many ways the trans_start and trans_end ioctls can lead
2356 * to deadlocks. They should only be used by applications that
2357 * basically own the machine, and have a very in depth understanding
2358 * of all the possible deadlocks and enospc problems.
2360 long btrfs_ioctl_trans_end(struct file *file)
2362 struct inode *inode = fdentry(file)->d_inode;
2363 struct btrfs_root *root = BTRFS_I(inode)->root;
2364 struct btrfs_trans_handle *trans;
2366 trans = file->private_data;
2369 file->private_data = NULL;
2371 btrfs_end_transaction(trans, root);
2373 mutex_lock(&root->fs_info->trans_mutex);
2374 root->fs_info->open_ioctl_trans--;
2375 mutex_unlock(&root->fs_info->trans_mutex);
2377 mnt_drop_write(file->f_path.mnt);
2381 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2383 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2384 struct btrfs_trans_handle *trans;
2388 trans = btrfs_start_transaction(root, 0);
2390 return PTR_ERR(trans);
2391 transid = trans->transid;
2392 ret = btrfs_commit_transaction_async(trans, root, 0);
2397 if (copy_to_user(argp, &transid, sizeof(transid)))
2402 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2404 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2408 if (copy_from_user(&transid, argp, sizeof(transid)))
2411 transid = 0; /* current trans */
2413 return btrfs_wait_for_commit(root, transid);
2416 long btrfs_ioctl(struct file *file, unsigned int
2417 cmd, unsigned long arg)
2419 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2420 void __user *argp = (void __user *)arg;
2423 case FS_IOC_GETFLAGS:
2424 return btrfs_ioctl_getflags(file, argp);
2425 case FS_IOC_SETFLAGS:
2426 return btrfs_ioctl_setflags(file, argp);
2427 case FS_IOC_GETVERSION:
2428 return btrfs_ioctl_getversion(file, argp);
2429 case BTRFS_IOC_SNAP_CREATE:
2430 return btrfs_ioctl_snap_create(file, argp, 0);
2431 case BTRFS_IOC_SNAP_CREATE_V2:
2432 return btrfs_ioctl_snap_create_v2(file, argp, 0);
2433 case BTRFS_IOC_SUBVOL_CREATE:
2434 return btrfs_ioctl_snap_create(file, argp, 1);
2435 case BTRFS_IOC_SNAP_DESTROY:
2436 return btrfs_ioctl_snap_destroy(file, argp);
2437 case BTRFS_IOC_SUBVOL_GETFLAGS:
2438 return btrfs_ioctl_subvol_getflags(file, argp);
2439 case BTRFS_IOC_SUBVOL_SETFLAGS:
2440 return btrfs_ioctl_subvol_setflags(file, argp);
2441 case BTRFS_IOC_DEFAULT_SUBVOL:
2442 return btrfs_ioctl_default_subvol(file, argp);
2443 case BTRFS_IOC_DEFRAG:
2444 return btrfs_ioctl_defrag(file, NULL);
2445 case BTRFS_IOC_DEFRAG_RANGE:
2446 return btrfs_ioctl_defrag(file, argp);
2447 case BTRFS_IOC_RESIZE:
2448 return btrfs_ioctl_resize(root, argp);
2449 case BTRFS_IOC_ADD_DEV:
2450 return btrfs_ioctl_add_dev(root, argp);
2451 case BTRFS_IOC_RM_DEV:
2452 return btrfs_ioctl_rm_dev(root, argp);
2453 case BTRFS_IOC_BALANCE:
2454 return btrfs_balance(root->fs_info->dev_root);
2455 case BTRFS_IOC_CLONE:
2456 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2457 case BTRFS_IOC_CLONE_RANGE:
2458 return btrfs_ioctl_clone_range(file, argp);
2459 case BTRFS_IOC_TRANS_START:
2460 return btrfs_ioctl_trans_start(file);
2461 case BTRFS_IOC_TRANS_END:
2462 return btrfs_ioctl_trans_end(file);
2463 case BTRFS_IOC_TREE_SEARCH:
2464 return btrfs_ioctl_tree_search(file, argp);
2465 case BTRFS_IOC_INO_LOOKUP:
2466 return btrfs_ioctl_ino_lookup(file, argp);
2467 case BTRFS_IOC_SPACE_INFO:
2468 return btrfs_ioctl_space_info(root, argp);
2469 case BTRFS_IOC_SYNC:
2470 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2472 case BTRFS_IOC_START_SYNC:
2473 return btrfs_ioctl_start_sync(file, argp);
2474 case BTRFS_IOC_WAIT_SYNC:
2475 return btrfs_ioctl_wait_sync(file, argp);