Btrfs: Integrate metadata reservation with start_transaction
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / ioctl.c
blob3066da468c6dc9e06a74f75b109c7e15daed65a3
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/fsnotify.h>
25 #include <linux/pagemap.h>
26 #include <linux/highmem.h>
27 #include <linux/time.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mount.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/swap.h>
35 #include <linux/writeback.h>
36 #include <linux/statfs.h>
37 #include <linux/compat.h>
38 #include <linux/bit_spinlock.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/vmalloc.h>
42 #include <linux/slab.h>
43 #include "compat.h"
44 #include "ctree.h"
45 #include "disk-io.h"
46 #include "transaction.h"
47 #include "btrfs_inode.h"
48 #include "ioctl.h"
49 #include "print-tree.h"
50 #include "volumes.h"
51 #include "locking.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)
56 if (S_ISDIR(mode))
57 return flags;
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
60 else
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)
72 iflags |= FS_SYNC_FL;
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;
84 return iflags;
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)
115 unsigned int flags;
117 if (!dir)
118 return;
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)))
137 return -EFAULT;
138 return 0;
141 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
143 struct inode *inode = file->f_path.dentry->d_inode;
144 struct btrfs_inode *ip = BTRFS_I(inode);
145 struct btrfs_root *root = ip->root;
146 struct btrfs_trans_handle *trans;
147 unsigned int flags, oldflags;
148 int ret;
150 if (copy_from_user(&flags, arg, sizeof(flags)))
151 return -EFAULT;
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL))
156 return -EOPNOTSUPP;
158 if (!is_owner_or_cap(inode))
159 return -EACCES;
161 mutex_lock(&inode->i_mutex);
163 flags = btrfs_mask_flags(inode->i_mode, flags);
164 oldflags = btrfs_flags_to_ioctl(ip->flags);
165 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
166 if (!capable(CAP_LINUX_IMMUTABLE)) {
167 ret = -EPERM;
168 goto out_unlock;
172 ret = mnt_want_write(file->f_path.mnt);
173 if (ret)
174 goto out_unlock;
176 if (flags & FS_SYNC_FL)
177 ip->flags |= BTRFS_INODE_SYNC;
178 else
179 ip->flags &= ~BTRFS_INODE_SYNC;
180 if (flags & FS_IMMUTABLE_FL)
181 ip->flags |= BTRFS_INODE_IMMUTABLE;
182 else
183 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
184 if (flags & FS_APPEND_FL)
185 ip->flags |= BTRFS_INODE_APPEND;
186 else
187 ip->flags &= ~BTRFS_INODE_APPEND;
188 if (flags & FS_NODUMP_FL)
189 ip->flags |= BTRFS_INODE_NODUMP;
190 else
191 ip->flags &= ~BTRFS_INODE_NODUMP;
192 if (flags & FS_NOATIME_FL)
193 ip->flags |= BTRFS_INODE_NOATIME;
194 else
195 ip->flags &= ~BTRFS_INODE_NOATIME;
196 if (flags & FS_DIRSYNC_FL)
197 ip->flags |= BTRFS_INODE_DIRSYNC;
198 else
199 ip->flags &= ~BTRFS_INODE_DIRSYNC;
202 trans = btrfs_join_transaction(root, 1);
203 BUG_ON(!trans);
205 ret = btrfs_update_inode(trans, root, inode);
206 BUG_ON(ret);
208 btrfs_update_iflags(inode);
209 inode->i_ctime = CURRENT_TIME;
210 btrfs_end_transaction(trans, root);
212 mnt_drop_write(file->f_path.mnt);
213 out_unlock:
214 mutex_unlock(&inode->i_mutex);
215 return 0;
218 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
220 struct inode *inode = file->f_path.dentry->d_inode;
222 return put_user(inode->i_generation, arg);
225 static noinline int create_subvol(struct btrfs_root *root,
226 struct dentry *dentry,
227 char *name, int namelen)
229 struct btrfs_trans_handle *trans;
230 struct btrfs_key key;
231 struct btrfs_root_item root_item;
232 struct btrfs_inode_item *inode_item;
233 struct extent_buffer *leaf;
234 struct btrfs_root *new_root;
235 struct inode *dir = dentry->d_parent->d_inode;
236 int ret;
237 int err;
238 u64 objectid;
239 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
240 u64 index = 0;
242 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
243 0, &objectid);
244 if (ret)
245 return ret;
247 * 1 - inode item
248 * 2 - refs
249 * 1 - root item
250 * 2 - dir items
252 trans = btrfs_start_transaction(root, 6);
253 if (IS_ERR(trans))
254 return PTR_ERR(trans);
256 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
257 0, objectid, NULL, 0, 0, 0);
258 if (IS_ERR(leaf)) {
259 ret = PTR_ERR(leaf);
260 goto fail;
263 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
264 btrfs_set_header_bytenr(leaf, leaf->start);
265 btrfs_set_header_generation(leaf, trans->transid);
266 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
267 btrfs_set_header_owner(leaf, objectid);
269 write_extent_buffer(leaf, root->fs_info->fsid,
270 (unsigned long)btrfs_header_fsid(leaf),
271 BTRFS_FSID_SIZE);
272 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
273 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
274 BTRFS_UUID_SIZE);
275 btrfs_mark_buffer_dirty(leaf);
277 inode_item = &root_item.inode;
278 memset(inode_item, 0, sizeof(*inode_item));
279 inode_item->generation = cpu_to_le64(1);
280 inode_item->size = cpu_to_le64(3);
281 inode_item->nlink = cpu_to_le32(1);
282 inode_item->nbytes = cpu_to_le64(root->leafsize);
283 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
285 btrfs_set_root_bytenr(&root_item, leaf->start);
286 btrfs_set_root_generation(&root_item, trans->transid);
287 btrfs_set_root_level(&root_item, 0);
288 btrfs_set_root_refs(&root_item, 1);
289 btrfs_set_root_used(&root_item, leaf->len);
290 btrfs_set_root_last_snapshot(&root_item, 0);
292 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
293 root_item.drop_level = 0;
295 btrfs_tree_unlock(leaf);
296 free_extent_buffer(leaf);
297 leaf = NULL;
299 btrfs_set_root_dirid(&root_item, new_dirid);
301 key.objectid = objectid;
302 key.offset = 0;
303 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
304 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
305 &root_item);
306 if (ret)
307 goto fail;
309 key.offset = (u64)-1;
310 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
311 BUG_ON(IS_ERR(new_root));
313 btrfs_record_root_in_trans(trans, new_root);
315 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
316 BTRFS_I(dir)->block_group);
318 * insert the directory item
320 ret = btrfs_set_inode_index(dir, &index);
321 BUG_ON(ret);
323 ret = btrfs_insert_dir_item(trans, root,
324 name, namelen, dir->i_ino, &key,
325 BTRFS_FT_DIR, index);
326 if (ret)
327 goto fail;
329 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
330 ret = btrfs_update_inode(trans, root, dir);
331 BUG_ON(ret);
333 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
334 objectid, root->root_key.objectid,
335 dir->i_ino, index, name, namelen);
337 BUG_ON(ret);
339 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
340 fail:
341 err = btrfs_commit_transaction(trans, root);
342 if (err && !ret)
343 ret = err;
344 return ret;
347 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry)
349 struct inode *inode;
350 struct btrfs_pending_snapshot *pending_snapshot;
351 struct btrfs_trans_handle *trans;
352 int ret;
354 if (!root->ref_cows)
355 return -EINVAL;
357 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
358 if (!pending_snapshot)
359 return -ENOMEM;
361 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
362 pending_snapshot->dentry = dentry;
363 pending_snapshot->root = root;
365 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
366 if (IS_ERR(trans)) {
367 ret = PTR_ERR(trans);
368 goto fail;
371 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
372 BUG_ON(ret);
374 list_add(&pending_snapshot->list,
375 &trans->transaction->pending_snapshots);
376 ret = btrfs_commit_transaction(trans, root->fs_info->extent_root);
377 BUG_ON(ret);
379 ret = pending_snapshot->error;
380 if (ret)
381 goto fail;
383 btrfs_orphan_cleanup(pending_snapshot->snap);
385 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
386 if (IS_ERR(inode)) {
387 ret = PTR_ERR(inode);
388 goto fail;
390 BUG_ON(!inode);
391 d_instantiate(dentry, inode);
392 ret = 0;
393 fail:
394 kfree(pending_snapshot);
395 return ret;
398 /* copy of may_create in fs/namei.c() */
399 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
401 if (child->d_inode)
402 return -EEXIST;
403 if (IS_DEADDIR(dir))
404 return -ENOENT;
405 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
409 * Create a new subvolume below @parent. This is largely modeled after
410 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
411 * inside this filesystem so it's quite a bit simpler.
413 static noinline int btrfs_mksubvol(struct path *parent,
414 char *name, int namelen,
415 struct btrfs_root *snap_src)
417 struct inode *dir = parent->dentry->d_inode;
418 struct dentry *dentry;
419 int error;
421 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
423 dentry = lookup_one_len(name, parent->dentry, namelen);
424 error = PTR_ERR(dentry);
425 if (IS_ERR(dentry))
426 goto out_unlock;
428 error = -EEXIST;
429 if (dentry->d_inode)
430 goto out_dput;
432 error = mnt_want_write(parent->mnt);
433 if (error)
434 goto out_dput;
436 error = btrfs_may_create(dir, dentry);
437 if (error)
438 goto out_drop_write;
440 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
442 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
443 goto out_up_read;
445 if (snap_src) {
446 error = create_snapshot(snap_src, dentry);
447 } else {
448 error = create_subvol(BTRFS_I(dir)->root, dentry,
449 name, namelen);
451 if (!error)
452 fsnotify_mkdir(dir, dentry);
453 out_up_read:
454 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
455 out_drop_write:
456 mnt_drop_write(parent->mnt);
457 out_dput:
458 dput(dentry);
459 out_unlock:
460 mutex_unlock(&dir->i_mutex);
461 return error;
464 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
465 int thresh, u64 *last_len, u64 *skip,
466 u64 *defrag_end)
468 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
469 struct extent_map *em = NULL;
470 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
471 int ret = 1;
474 if (thresh == 0)
475 thresh = 256 * 1024;
478 * make sure that once we start defragging and extent, we keep on
479 * defragging it
481 if (start < *defrag_end)
482 return 1;
484 *skip = 0;
487 * hopefully we have this extent in the tree already, try without
488 * the full extent lock
490 read_lock(&em_tree->lock);
491 em = lookup_extent_mapping(em_tree, start, len);
492 read_unlock(&em_tree->lock);
494 if (!em) {
495 /* get the big lock and read metadata off disk */
496 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
497 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
498 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
500 if (IS_ERR(em))
501 return 0;
504 /* this will cover holes, and inline extents */
505 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
506 ret = 0;
509 * we hit a real extent, if it is big don't bother defragging it again
511 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
512 ret = 0;
515 * last_len ends up being a counter of how many bytes we've defragged.
516 * every time we choose not to defrag an extent, we reset *last_len
517 * so that the next tiny extent will force a defrag.
519 * The end result of this is that tiny extents before a single big
520 * extent will force at least part of that big extent to be defragged.
522 if (ret) {
523 *last_len += len;
524 *defrag_end = extent_map_end(em);
525 } else {
526 *last_len = 0;
527 *skip = extent_map_end(em);
528 *defrag_end = 0;
531 free_extent_map(em);
532 return ret;
535 static int btrfs_defrag_file(struct file *file,
536 struct btrfs_ioctl_defrag_range_args *range)
538 struct inode *inode = fdentry(file)->d_inode;
539 struct btrfs_root *root = BTRFS_I(inode)->root;
540 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
541 struct btrfs_ordered_extent *ordered;
542 struct page *page;
543 unsigned long last_index;
544 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
545 unsigned long total_read = 0;
546 u64 page_start;
547 u64 page_end;
548 u64 last_len = 0;
549 u64 skip = 0;
550 u64 defrag_end = 0;
551 unsigned long i;
552 int ret;
554 if (inode->i_size == 0)
555 return 0;
557 if (range->start + range->len > range->start) {
558 last_index = min_t(u64, inode->i_size - 1,
559 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
560 } else {
561 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
564 i = range->start >> PAGE_CACHE_SHIFT;
565 while (i <= last_index) {
566 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
567 PAGE_CACHE_SIZE,
568 range->extent_thresh,
569 &last_len, &skip,
570 &defrag_end)) {
571 unsigned long next;
573 * the should_defrag function tells us how much to skip
574 * bump our counter by the suggested amount
576 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
577 i = max(i + 1, next);
578 continue;
581 if (total_read % ra_pages == 0) {
582 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
583 min(last_index, i + ra_pages - 1));
585 total_read++;
586 mutex_lock(&inode->i_mutex);
587 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
588 BTRFS_I(inode)->force_compress = 1;
590 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
591 if (ret) {
592 ret = -ENOSPC;
593 break;
596 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
597 if (ret) {
598 btrfs_free_reserved_data_space(root, inode,
599 PAGE_CACHE_SIZE);
600 ret = -ENOSPC;
601 break;
603 again:
604 if (inode->i_size == 0 ||
605 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
606 ret = 0;
607 goto err_reservations;
610 page = grab_cache_page(inode->i_mapping, i);
611 if (!page)
612 goto err_reservations;
614 if (!PageUptodate(page)) {
615 btrfs_readpage(NULL, page);
616 lock_page(page);
617 if (!PageUptodate(page)) {
618 unlock_page(page);
619 page_cache_release(page);
620 goto err_reservations;
624 if (page->mapping != inode->i_mapping) {
625 unlock_page(page);
626 page_cache_release(page);
627 goto again;
630 wait_on_page_writeback(page);
632 if (PageDirty(page)) {
633 btrfs_free_reserved_data_space(root, inode,
634 PAGE_CACHE_SIZE);
635 goto loop_unlock;
638 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
639 page_end = page_start + PAGE_CACHE_SIZE - 1;
640 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
642 ordered = btrfs_lookup_ordered_extent(inode, page_start);
643 if (ordered) {
644 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
645 unlock_page(page);
646 page_cache_release(page);
647 btrfs_start_ordered_extent(inode, ordered, 1);
648 btrfs_put_ordered_extent(ordered);
649 goto again;
651 set_page_extent_mapped(page);
654 * this makes sure page_mkwrite is called on the
655 * page if it is dirtied again later
657 clear_page_dirty_for_io(page);
658 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
659 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
660 EXTENT_DO_ACCOUNTING, GFP_NOFS);
662 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
663 ClearPageChecked(page);
664 set_page_dirty(page);
665 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
667 loop_unlock:
668 unlock_page(page);
669 page_cache_release(page);
670 mutex_unlock(&inode->i_mutex);
672 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
673 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
674 i++;
677 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
678 filemap_flush(inode->i_mapping);
680 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
681 /* the filemap_flush will queue IO into the worker threads, but
682 * we have to make sure the IO is actually started and that
683 * ordered extents get created before we return
685 atomic_inc(&root->fs_info->async_submit_draining);
686 while (atomic_read(&root->fs_info->nr_async_submits) ||
687 atomic_read(&root->fs_info->async_delalloc_pages)) {
688 wait_event(root->fs_info->async_submit_wait,
689 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
690 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
692 atomic_dec(&root->fs_info->async_submit_draining);
694 mutex_lock(&inode->i_mutex);
695 BTRFS_I(inode)->force_compress = 0;
696 mutex_unlock(&inode->i_mutex);
699 return 0;
701 err_reservations:
702 mutex_unlock(&inode->i_mutex);
703 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
704 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
705 return ret;
708 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
709 void __user *arg)
711 u64 new_size;
712 u64 old_size;
713 u64 devid = 1;
714 struct btrfs_ioctl_vol_args *vol_args;
715 struct btrfs_trans_handle *trans;
716 struct btrfs_device *device = NULL;
717 char *sizestr;
718 char *devstr = NULL;
719 int ret = 0;
720 int namelen;
721 int mod = 0;
723 if (root->fs_info->sb->s_flags & MS_RDONLY)
724 return -EROFS;
726 if (!capable(CAP_SYS_ADMIN))
727 return -EPERM;
729 vol_args = memdup_user(arg, sizeof(*vol_args));
730 if (IS_ERR(vol_args))
731 return PTR_ERR(vol_args);
733 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
734 namelen = strlen(vol_args->name);
736 mutex_lock(&root->fs_info->volume_mutex);
737 sizestr = vol_args->name;
738 devstr = strchr(sizestr, ':');
739 if (devstr) {
740 char *end;
741 sizestr = devstr + 1;
742 *devstr = '\0';
743 devstr = vol_args->name;
744 devid = simple_strtoull(devstr, &end, 10);
745 printk(KERN_INFO "resizing devid %llu\n",
746 (unsigned long long)devid);
748 device = btrfs_find_device(root, devid, NULL, NULL);
749 if (!device) {
750 printk(KERN_INFO "resizer unable to find device %llu\n",
751 (unsigned long long)devid);
752 ret = -EINVAL;
753 goto out_unlock;
755 if (!strcmp(sizestr, "max"))
756 new_size = device->bdev->bd_inode->i_size;
757 else {
758 if (sizestr[0] == '-') {
759 mod = -1;
760 sizestr++;
761 } else if (sizestr[0] == '+') {
762 mod = 1;
763 sizestr++;
765 new_size = memparse(sizestr, NULL);
766 if (new_size == 0) {
767 ret = -EINVAL;
768 goto out_unlock;
772 old_size = device->total_bytes;
774 if (mod < 0) {
775 if (new_size > old_size) {
776 ret = -EINVAL;
777 goto out_unlock;
779 new_size = old_size - new_size;
780 } else if (mod > 0) {
781 new_size = old_size + new_size;
784 if (new_size < 256 * 1024 * 1024) {
785 ret = -EINVAL;
786 goto out_unlock;
788 if (new_size > device->bdev->bd_inode->i_size) {
789 ret = -EFBIG;
790 goto out_unlock;
793 do_div(new_size, root->sectorsize);
794 new_size *= root->sectorsize;
796 printk(KERN_INFO "new size for %s is %llu\n",
797 device->name, (unsigned long long)new_size);
799 if (new_size > old_size) {
800 trans = btrfs_start_transaction(root, 0);
801 ret = btrfs_grow_device(trans, device, new_size);
802 btrfs_commit_transaction(trans, root);
803 } else {
804 ret = btrfs_shrink_device(device, new_size);
807 out_unlock:
808 mutex_unlock(&root->fs_info->volume_mutex);
809 kfree(vol_args);
810 return ret;
813 static noinline int btrfs_ioctl_snap_create(struct file *file,
814 void __user *arg, int subvol)
816 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
817 struct btrfs_ioctl_vol_args *vol_args;
818 struct file *src_file;
819 int namelen;
820 int ret = 0;
822 if (root->fs_info->sb->s_flags & MS_RDONLY)
823 return -EROFS;
825 vol_args = memdup_user(arg, sizeof(*vol_args));
826 if (IS_ERR(vol_args))
827 return PTR_ERR(vol_args);
829 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
830 namelen = strlen(vol_args->name);
831 if (strchr(vol_args->name, '/')) {
832 ret = -EINVAL;
833 goto out;
836 if (subvol) {
837 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
838 NULL);
839 } else {
840 struct inode *src_inode;
841 src_file = fget(vol_args->fd);
842 if (!src_file) {
843 ret = -EINVAL;
844 goto out;
847 src_inode = src_file->f_path.dentry->d_inode;
848 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
849 printk(KERN_INFO "btrfs: Snapshot src from "
850 "another FS\n");
851 ret = -EINVAL;
852 fput(src_file);
853 goto out;
855 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
856 BTRFS_I(src_inode)->root);
857 fput(src_file);
859 out:
860 kfree(vol_args);
861 return ret;
865 * helper to check if the subvolume references other subvolumes
867 static noinline int may_destroy_subvol(struct btrfs_root *root)
869 struct btrfs_path *path;
870 struct btrfs_key key;
871 int ret;
873 path = btrfs_alloc_path();
874 if (!path)
875 return -ENOMEM;
877 key.objectid = root->root_key.objectid;
878 key.type = BTRFS_ROOT_REF_KEY;
879 key.offset = (u64)-1;
881 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
882 &key, path, 0, 0);
883 if (ret < 0)
884 goto out;
885 BUG_ON(ret == 0);
887 ret = 0;
888 if (path->slots[0] > 0) {
889 path->slots[0]--;
890 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
891 if (key.objectid == root->root_key.objectid &&
892 key.type == BTRFS_ROOT_REF_KEY)
893 ret = -ENOTEMPTY;
895 out:
896 btrfs_free_path(path);
897 return ret;
900 static noinline int key_in_sk(struct btrfs_key *key,
901 struct btrfs_ioctl_search_key *sk)
903 struct btrfs_key test;
904 int ret;
906 test.objectid = sk->min_objectid;
907 test.type = sk->min_type;
908 test.offset = sk->min_offset;
910 ret = btrfs_comp_cpu_keys(key, &test);
911 if (ret < 0)
912 return 0;
914 test.objectid = sk->max_objectid;
915 test.type = sk->max_type;
916 test.offset = sk->max_offset;
918 ret = btrfs_comp_cpu_keys(key, &test);
919 if (ret > 0)
920 return 0;
921 return 1;
924 static noinline int copy_to_sk(struct btrfs_root *root,
925 struct btrfs_path *path,
926 struct btrfs_key *key,
927 struct btrfs_ioctl_search_key *sk,
928 char *buf,
929 unsigned long *sk_offset,
930 int *num_found)
932 u64 found_transid;
933 struct extent_buffer *leaf;
934 struct btrfs_ioctl_search_header sh;
935 unsigned long item_off;
936 unsigned long item_len;
937 int nritems;
938 int i;
939 int slot;
940 int found = 0;
941 int ret = 0;
943 leaf = path->nodes[0];
944 slot = path->slots[0];
945 nritems = btrfs_header_nritems(leaf);
947 if (btrfs_header_generation(leaf) > sk->max_transid) {
948 i = nritems;
949 goto advance_key;
951 found_transid = btrfs_header_generation(leaf);
953 for (i = slot; i < nritems; i++) {
954 item_off = btrfs_item_ptr_offset(leaf, i);
955 item_len = btrfs_item_size_nr(leaf, i);
957 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
958 item_len = 0;
960 if (sizeof(sh) + item_len + *sk_offset >
961 BTRFS_SEARCH_ARGS_BUFSIZE) {
962 ret = 1;
963 goto overflow;
966 btrfs_item_key_to_cpu(leaf, key, i);
967 if (!key_in_sk(key, sk))
968 continue;
970 sh.objectid = key->objectid;
971 sh.offset = key->offset;
972 sh.type = key->type;
973 sh.len = item_len;
974 sh.transid = found_transid;
976 /* copy search result header */
977 memcpy(buf + *sk_offset, &sh, sizeof(sh));
978 *sk_offset += sizeof(sh);
980 if (item_len) {
981 char *p = buf + *sk_offset;
982 /* copy the item */
983 read_extent_buffer(leaf, p,
984 item_off, item_len);
985 *sk_offset += item_len;
987 found++;
989 if (*num_found >= sk->nr_items)
990 break;
992 advance_key:
993 ret = 0;
994 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
995 key->offset++;
996 else if (key->type < (u8)-1 && key->type < sk->max_type) {
997 key->offset = 0;
998 key->type++;
999 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1000 key->offset = 0;
1001 key->type = 0;
1002 key->objectid++;
1003 } else
1004 ret = 1;
1005 overflow:
1006 *num_found += found;
1007 return ret;
1010 static noinline int search_ioctl(struct inode *inode,
1011 struct btrfs_ioctl_search_args *args)
1013 struct btrfs_root *root;
1014 struct btrfs_key key;
1015 struct btrfs_key max_key;
1016 struct btrfs_path *path;
1017 struct btrfs_ioctl_search_key *sk = &args->key;
1018 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1019 int ret;
1020 int num_found = 0;
1021 unsigned long sk_offset = 0;
1023 path = btrfs_alloc_path();
1024 if (!path)
1025 return -ENOMEM;
1027 if (sk->tree_id == 0) {
1028 /* search the root of the inode that was passed */
1029 root = BTRFS_I(inode)->root;
1030 } else {
1031 key.objectid = sk->tree_id;
1032 key.type = BTRFS_ROOT_ITEM_KEY;
1033 key.offset = (u64)-1;
1034 root = btrfs_read_fs_root_no_name(info, &key);
1035 if (IS_ERR(root)) {
1036 printk(KERN_ERR "could not find root %llu\n",
1037 sk->tree_id);
1038 btrfs_free_path(path);
1039 return -ENOENT;
1043 key.objectid = sk->min_objectid;
1044 key.type = sk->min_type;
1045 key.offset = sk->min_offset;
1047 max_key.objectid = sk->max_objectid;
1048 max_key.type = sk->max_type;
1049 max_key.offset = sk->max_offset;
1051 path->keep_locks = 1;
1053 while(1) {
1054 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1055 sk->min_transid);
1056 if (ret != 0) {
1057 if (ret > 0)
1058 ret = 0;
1059 goto err;
1061 ret = copy_to_sk(root, path, &key, sk, args->buf,
1062 &sk_offset, &num_found);
1063 btrfs_release_path(root, path);
1064 if (ret || num_found >= sk->nr_items)
1065 break;
1068 ret = 0;
1069 err:
1070 sk->nr_items = num_found;
1071 btrfs_free_path(path);
1072 return ret;
1075 static noinline int btrfs_ioctl_tree_search(struct file *file,
1076 void __user *argp)
1078 struct btrfs_ioctl_search_args *args;
1079 struct inode *inode;
1080 int ret;
1082 if (!capable(CAP_SYS_ADMIN))
1083 return -EPERM;
1085 args = kmalloc(sizeof(*args), GFP_KERNEL);
1086 if (!args)
1087 return -ENOMEM;
1089 if (copy_from_user(args, argp, sizeof(*args))) {
1090 kfree(args);
1091 return -EFAULT;
1093 inode = fdentry(file)->d_inode;
1094 ret = search_ioctl(inode, args);
1095 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1096 ret = -EFAULT;
1097 kfree(args);
1098 return ret;
1102 * Search INODE_REFs to identify path name of 'dirid' directory
1103 * in a 'tree_id' tree. and sets path name to 'name'.
1105 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1106 u64 tree_id, u64 dirid, char *name)
1108 struct btrfs_root *root;
1109 struct btrfs_key key;
1110 char *ptr;
1111 int ret = -1;
1112 int slot;
1113 int len;
1114 int total_len = 0;
1115 struct btrfs_inode_ref *iref;
1116 struct extent_buffer *l;
1117 struct btrfs_path *path;
1119 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1120 name[0]='\0';
1121 return 0;
1124 path = btrfs_alloc_path();
1125 if (!path)
1126 return -ENOMEM;
1128 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1130 key.objectid = tree_id;
1131 key.type = BTRFS_ROOT_ITEM_KEY;
1132 key.offset = (u64)-1;
1133 root = btrfs_read_fs_root_no_name(info, &key);
1134 if (IS_ERR(root)) {
1135 printk(KERN_ERR "could not find root %llu\n", tree_id);
1136 ret = -ENOENT;
1137 goto out;
1140 key.objectid = dirid;
1141 key.type = BTRFS_INODE_REF_KEY;
1142 key.offset = (u64)-1;
1144 while(1) {
1145 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1146 if (ret < 0)
1147 goto out;
1149 l = path->nodes[0];
1150 slot = path->slots[0];
1151 if (ret > 0 && slot > 0)
1152 slot--;
1153 btrfs_item_key_to_cpu(l, &key, slot);
1155 if (ret > 0 && (key.objectid != dirid ||
1156 key.type != BTRFS_INODE_REF_KEY)) {
1157 ret = -ENOENT;
1158 goto out;
1161 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1162 len = btrfs_inode_ref_name_len(l, iref);
1163 ptr -= len + 1;
1164 total_len += len + 1;
1165 if (ptr < name)
1166 goto out;
1168 *(ptr + len) = '/';
1169 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1171 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1172 break;
1174 btrfs_release_path(root, path);
1175 key.objectid = key.offset;
1176 key.offset = (u64)-1;
1177 dirid = key.objectid;
1180 if (ptr < name)
1181 goto out;
1182 memcpy(name, ptr, total_len);
1183 name[total_len]='\0';
1184 ret = 0;
1185 out:
1186 btrfs_free_path(path);
1187 return ret;
1190 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1191 void __user *argp)
1193 struct btrfs_ioctl_ino_lookup_args *args;
1194 struct inode *inode;
1195 int ret;
1197 if (!capable(CAP_SYS_ADMIN))
1198 return -EPERM;
1200 args = kmalloc(sizeof(*args), GFP_KERNEL);
1201 if (!args)
1202 return -ENOMEM;
1204 if (copy_from_user(args, argp, sizeof(*args))) {
1205 kfree(args);
1206 return -EFAULT;
1208 inode = fdentry(file)->d_inode;
1210 if (args->treeid == 0)
1211 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1213 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1214 args->treeid, args->objectid,
1215 args->name);
1217 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1218 ret = -EFAULT;
1220 kfree(args);
1221 return ret;
1224 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1225 void __user *arg)
1227 struct dentry *parent = fdentry(file);
1228 struct dentry *dentry;
1229 struct inode *dir = parent->d_inode;
1230 struct inode *inode;
1231 struct btrfs_root *root = BTRFS_I(dir)->root;
1232 struct btrfs_root *dest = NULL;
1233 struct btrfs_ioctl_vol_args *vol_args;
1234 struct btrfs_trans_handle *trans;
1235 int namelen;
1236 int ret;
1237 int err = 0;
1239 if (!capable(CAP_SYS_ADMIN))
1240 return -EPERM;
1242 vol_args = memdup_user(arg, sizeof(*vol_args));
1243 if (IS_ERR(vol_args))
1244 return PTR_ERR(vol_args);
1246 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1247 namelen = strlen(vol_args->name);
1248 if (strchr(vol_args->name, '/') ||
1249 strncmp(vol_args->name, "..", namelen) == 0) {
1250 err = -EINVAL;
1251 goto out;
1254 err = mnt_want_write(file->f_path.mnt);
1255 if (err)
1256 goto out;
1258 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1259 dentry = lookup_one_len(vol_args->name, parent, namelen);
1260 if (IS_ERR(dentry)) {
1261 err = PTR_ERR(dentry);
1262 goto out_unlock_dir;
1265 if (!dentry->d_inode) {
1266 err = -ENOENT;
1267 goto out_dput;
1270 inode = dentry->d_inode;
1271 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1272 err = -EINVAL;
1273 goto out_dput;
1276 dest = BTRFS_I(inode)->root;
1278 mutex_lock(&inode->i_mutex);
1279 err = d_invalidate(dentry);
1280 if (err)
1281 goto out_unlock;
1283 down_write(&root->fs_info->subvol_sem);
1285 err = may_destroy_subvol(dest);
1286 if (err)
1287 goto out_up_write;
1289 trans = btrfs_start_transaction(root, 0);
1290 if (IS_ERR(trans)) {
1291 err = PTR_ERR(trans);
1292 goto out;
1294 trans->block_rsv = &root->fs_info->global_block_rsv;
1296 ret = btrfs_unlink_subvol(trans, root, dir,
1297 dest->root_key.objectid,
1298 dentry->d_name.name,
1299 dentry->d_name.len);
1300 BUG_ON(ret);
1302 btrfs_record_root_in_trans(trans, dest);
1304 memset(&dest->root_item.drop_progress, 0,
1305 sizeof(dest->root_item.drop_progress));
1306 dest->root_item.drop_level = 0;
1307 btrfs_set_root_refs(&dest->root_item, 0);
1309 ret = btrfs_insert_orphan_item(trans,
1310 root->fs_info->tree_root,
1311 dest->root_key.objectid);
1312 BUG_ON(ret);
1314 ret = btrfs_commit_transaction(trans, root);
1315 BUG_ON(ret);
1316 inode->i_flags |= S_DEAD;
1317 out_up_write:
1318 up_write(&root->fs_info->subvol_sem);
1319 out_unlock:
1320 mutex_unlock(&inode->i_mutex);
1321 if (!err) {
1322 shrink_dcache_sb(root->fs_info->sb);
1323 btrfs_invalidate_inodes(dest);
1324 d_delete(dentry);
1326 out_dput:
1327 dput(dentry);
1328 out_unlock_dir:
1329 mutex_unlock(&dir->i_mutex);
1330 mnt_drop_write(file->f_path.mnt);
1331 out:
1332 kfree(vol_args);
1333 return err;
1336 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1338 struct inode *inode = fdentry(file)->d_inode;
1339 struct btrfs_root *root = BTRFS_I(inode)->root;
1340 struct btrfs_ioctl_defrag_range_args *range;
1341 int ret;
1343 ret = mnt_want_write(file->f_path.mnt);
1344 if (ret)
1345 return ret;
1347 switch (inode->i_mode & S_IFMT) {
1348 case S_IFDIR:
1349 if (!capable(CAP_SYS_ADMIN)) {
1350 ret = -EPERM;
1351 goto out;
1353 btrfs_defrag_root(root, 0);
1354 btrfs_defrag_root(root->fs_info->extent_root, 0);
1355 break;
1356 case S_IFREG:
1357 if (!(file->f_mode & FMODE_WRITE)) {
1358 ret = -EINVAL;
1359 goto out;
1362 range = kzalloc(sizeof(*range), GFP_KERNEL);
1363 if (!range) {
1364 ret = -ENOMEM;
1365 goto out;
1368 if (argp) {
1369 if (copy_from_user(range, argp,
1370 sizeof(*range))) {
1371 ret = -EFAULT;
1372 kfree(range);
1373 goto out;
1375 /* compression requires us to start the IO */
1376 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1377 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1378 range->extent_thresh = (u32)-1;
1380 } else {
1381 /* the rest are all set to zero by kzalloc */
1382 range->len = (u64)-1;
1384 btrfs_defrag_file(file, range);
1385 kfree(range);
1386 break;
1388 out:
1389 mnt_drop_write(file->f_path.mnt);
1390 return ret;
1393 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1395 struct btrfs_ioctl_vol_args *vol_args;
1396 int ret;
1398 if (!capable(CAP_SYS_ADMIN))
1399 return -EPERM;
1401 vol_args = memdup_user(arg, sizeof(*vol_args));
1402 if (IS_ERR(vol_args))
1403 return PTR_ERR(vol_args);
1405 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1406 ret = btrfs_init_new_device(root, vol_args->name);
1408 kfree(vol_args);
1409 return ret;
1412 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1414 struct btrfs_ioctl_vol_args *vol_args;
1415 int ret;
1417 if (!capable(CAP_SYS_ADMIN))
1418 return -EPERM;
1420 if (root->fs_info->sb->s_flags & MS_RDONLY)
1421 return -EROFS;
1423 vol_args = memdup_user(arg, sizeof(*vol_args));
1424 if (IS_ERR(vol_args))
1425 return PTR_ERR(vol_args);
1427 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1428 ret = btrfs_rm_device(root, vol_args->name);
1430 kfree(vol_args);
1431 return ret;
1434 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1435 u64 off, u64 olen, u64 destoff)
1437 struct inode *inode = fdentry(file)->d_inode;
1438 struct btrfs_root *root = BTRFS_I(inode)->root;
1439 struct file *src_file;
1440 struct inode *src;
1441 struct btrfs_trans_handle *trans;
1442 struct btrfs_path *path;
1443 struct extent_buffer *leaf;
1444 char *buf;
1445 struct btrfs_key key;
1446 u32 nritems;
1447 int slot;
1448 int ret;
1449 u64 len = olen;
1450 u64 bs = root->fs_info->sb->s_blocksize;
1451 u64 hint_byte;
1454 * TODO:
1455 * - split compressed inline extents. annoying: we need to
1456 * decompress into destination's address_space (the file offset
1457 * may change, so source mapping won't do), then recompress (or
1458 * otherwise reinsert) a subrange.
1459 * - allow ranges within the same file to be cloned (provided
1460 * they don't overlap)?
1463 /* the destination must be opened for writing */
1464 if (!(file->f_mode & FMODE_WRITE))
1465 return -EINVAL;
1467 ret = mnt_want_write(file->f_path.mnt);
1468 if (ret)
1469 return ret;
1471 src_file = fget(srcfd);
1472 if (!src_file) {
1473 ret = -EBADF;
1474 goto out_drop_write;
1477 src = src_file->f_dentry->d_inode;
1479 ret = -EINVAL;
1480 if (src == inode)
1481 goto out_fput;
1483 /* the src must be open for reading */
1484 if (!(src_file->f_mode & FMODE_READ))
1485 goto out_fput;
1487 ret = -EISDIR;
1488 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1489 goto out_fput;
1491 ret = -EXDEV;
1492 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1493 goto out_fput;
1495 ret = -ENOMEM;
1496 buf = vmalloc(btrfs_level_size(root, 0));
1497 if (!buf)
1498 goto out_fput;
1500 path = btrfs_alloc_path();
1501 if (!path) {
1502 vfree(buf);
1503 goto out_fput;
1505 path->reada = 2;
1507 if (inode < src) {
1508 mutex_lock(&inode->i_mutex);
1509 mutex_lock(&src->i_mutex);
1510 } else {
1511 mutex_lock(&src->i_mutex);
1512 mutex_lock(&inode->i_mutex);
1515 /* determine range to clone */
1516 ret = -EINVAL;
1517 if (off >= src->i_size || off + len > src->i_size)
1518 goto out_unlock;
1519 if (len == 0)
1520 olen = len = src->i_size - off;
1521 /* if we extend to eof, continue to block boundary */
1522 if (off + len == src->i_size)
1523 len = ((src->i_size + bs-1) & ~(bs-1))
1524 - off;
1526 /* verify the end result is block aligned */
1527 if ((off & (bs-1)) ||
1528 ((off + len) & (bs-1)))
1529 goto out_unlock;
1531 /* do any pending delalloc/csum calc on src, one way or
1532 another, and lock file content */
1533 while (1) {
1534 struct btrfs_ordered_extent *ordered;
1535 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1536 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
1537 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
1538 break;
1539 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1540 if (ordered)
1541 btrfs_put_ordered_extent(ordered);
1542 btrfs_wait_ordered_range(src, off, off+len);
1545 /* clone data */
1546 key.objectid = src->i_ino;
1547 key.type = BTRFS_EXTENT_DATA_KEY;
1548 key.offset = 0;
1550 while (1) {
1552 * note the key will change type as we walk through the
1553 * tree.
1555 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1556 if (ret < 0)
1557 goto out;
1559 nritems = btrfs_header_nritems(path->nodes[0]);
1560 if (path->slots[0] >= nritems) {
1561 ret = btrfs_next_leaf(root, path);
1562 if (ret < 0)
1563 goto out;
1564 if (ret > 0)
1565 break;
1566 nritems = btrfs_header_nritems(path->nodes[0]);
1568 leaf = path->nodes[0];
1569 slot = path->slots[0];
1571 btrfs_item_key_to_cpu(leaf, &key, slot);
1572 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1573 key.objectid != src->i_ino)
1574 break;
1576 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1577 struct btrfs_file_extent_item *extent;
1578 int type;
1579 u32 size;
1580 struct btrfs_key new_key;
1581 u64 disko = 0, diskl = 0;
1582 u64 datao = 0, datal = 0;
1583 u8 comp;
1585 size = btrfs_item_size_nr(leaf, slot);
1586 read_extent_buffer(leaf, buf,
1587 btrfs_item_ptr_offset(leaf, slot),
1588 size);
1590 extent = btrfs_item_ptr(leaf, slot,
1591 struct btrfs_file_extent_item);
1592 comp = btrfs_file_extent_compression(leaf, extent);
1593 type = btrfs_file_extent_type(leaf, extent);
1594 if (type == BTRFS_FILE_EXTENT_REG ||
1595 type == BTRFS_FILE_EXTENT_PREALLOC) {
1596 disko = btrfs_file_extent_disk_bytenr(leaf,
1597 extent);
1598 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1599 extent);
1600 datao = btrfs_file_extent_offset(leaf, extent);
1601 datal = btrfs_file_extent_num_bytes(leaf,
1602 extent);
1603 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1604 /* take upper bound, may be compressed */
1605 datal = btrfs_file_extent_ram_bytes(leaf,
1606 extent);
1608 btrfs_release_path(root, path);
1610 if (key.offset + datal < off ||
1611 key.offset >= off+len)
1612 goto next;
1614 memcpy(&new_key, &key, sizeof(new_key));
1615 new_key.objectid = inode->i_ino;
1616 new_key.offset = key.offset + destoff - off;
1618 trans = btrfs_start_transaction(root, 1);
1619 if (IS_ERR(trans)) {
1620 ret = PTR_ERR(trans);
1621 goto out;
1624 if (type == BTRFS_FILE_EXTENT_REG ||
1625 type == BTRFS_FILE_EXTENT_PREALLOC) {
1626 if (off > key.offset) {
1627 datao += off - key.offset;
1628 datal -= off - key.offset;
1631 if (key.offset + datal > off + len)
1632 datal = off + len - key.offset;
1634 ret = btrfs_drop_extents(trans, inode,
1635 new_key.offset,
1636 new_key.offset + datal,
1637 &hint_byte, 1);
1638 BUG_ON(ret);
1640 ret = btrfs_insert_empty_item(trans, root, path,
1641 &new_key, size);
1642 BUG_ON(ret);
1644 leaf = path->nodes[0];
1645 slot = path->slots[0];
1646 write_extent_buffer(leaf, buf,
1647 btrfs_item_ptr_offset(leaf, slot),
1648 size);
1650 extent = btrfs_item_ptr(leaf, slot,
1651 struct btrfs_file_extent_item);
1653 /* disko == 0 means it's a hole */
1654 if (!disko)
1655 datao = 0;
1657 btrfs_set_file_extent_offset(leaf, extent,
1658 datao);
1659 btrfs_set_file_extent_num_bytes(leaf, extent,
1660 datal);
1661 if (disko) {
1662 inode_add_bytes(inode, datal);
1663 ret = btrfs_inc_extent_ref(trans, root,
1664 disko, diskl, 0,
1665 root->root_key.objectid,
1666 inode->i_ino,
1667 new_key.offset - datao);
1668 BUG_ON(ret);
1670 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1671 u64 skip = 0;
1672 u64 trim = 0;
1673 if (off > key.offset) {
1674 skip = off - key.offset;
1675 new_key.offset += skip;
1678 if (key.offset + datal > off+len)
1679 trim = key.offset + datal - (off+len);
1681 if (comp && (skip || trim)) {
1682 ret = -EINVAL;
1683 btrfs_end_transaction(trans, root);
1684 goto out;
1686 size -= skip + trim;
1687 datal -= skip + trim;
1689 ret = btrfs_drop_extents(trans, inode,
1690 new_key.offset,
1691 new_key.offset + datal,
1692 &hint_byte, 1);
1693 BUG_ON(ret);
1695 ret = btrfs_insert_empty_item(trans, root, path,
1696 &new_key, size);
1697 BUG_ON(ret);
1699 if (skip) {
1700 u32 start =
1701 btrfs_file_extent_calc_inline_size(0);
1702 memmove(buf+start, buf+start+skip,
1703 datal);
1706 leaf = path->nodes[0];
1707 slot = path->slots[0];
1708 write_extent_buffer(leaf, buf,
1709 btrfs_item_ptr_offset(leaf, slot),
1710 size);
1711 inode_add_bytes(inode, datal);
1714 btrfs_mark_buffer_dirty(leaf);
1715 btrfs_release_path(root, path);
1717 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1718 if (new_key.offset + datal > inode->i_size)
1719 btrfs_i_size_write(inode,
1720 new_key.offset + datal);
1721 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1722 ret = btrfs_update_inode(trans, root, inode);
1723 BUG_ON(ret);
1724 btrfs_end_transaction(trans, root);
1726 next:
1727 btrfs_release_path(root, path);
1728 key.offset++;
1730 ret = 0;
1731 out:
1732 btrfs_release_path(root, path);
1733 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1734 out_unlock:
1735 mutex_unlock(&src->i_mutex);
1736 mutex_unlock(&inode->i_mutex);
1737 vfree(buf);
1738 btrfs_free_path(path);
1739 out_fput:
1740 fput(src_file);
1741 out_drop_write:
1742 mnt_drop_write(file->f_path.mnt);
1743 return ret;
1746 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1748 struct btrfs_ioctl_clone_range_args args;
1750 if (copy_from_user(&args, argp, sizeof(args)))
1751 return -EFAULT;
1752 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1753 args.src_length, args.dest_offset);
1757 * there are many ways the trans_start and trans_end ioctls can lead
1758 * to deadlocks. They should only be used by applications that
1759 * basically own the machine, and have a very in depth understanding
1760 * of all the possible deadlocks and enospc problems.
1762 static long btrfs_ioctl_trans_start(struct file *file)
1764 struct inode *inode = fdentry(file)->d_inode;
1765 struct btrfs_root *root = BTRFS_I(inode)->root;
1766 struct btrfs_trans_handle *trans;
1767 int ret;
1769 ret = -EPERM;
1770 if (!capable(CAP_SYS_ADMIN))
1771 goto out;
1773 ret = -EINPROGRESS;
1774 if (file->private_data)
1775 goto out;
1777 ret = mnt_want_write(file->f_path.mnt);
1778 if (ret)
1779 goto out;
1781 mutex_lock(&root->fs_info->trans_mutex);
1782 root->fs_info->open_ioctl_trans++;
1783 mutex_unlock(&root->fs_info->trans_mutex);
1785 ret = -ENOMEM;
1786 trans = btrfs_start_ioctl_transaction(root, 0);
1787 if (!trans)
1788 goto out_drop;
1790 file->private_data = trans;
1791 return 0;
1793 out_drop:
1794 mutex_lock(&root->fs_info->trans_mutex);
1795 root->fs_info->open_ioctl_trans--;
1796 mutex_unlock(&root->fs_info->trans_mutex);
1797 mnt_drop_write(file->f_path.mnt);
1798 out:
1799 return ret;
1802 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1804 struct inode *inode = fdentry(file)->d_inode;
1805 struct btrfs_root *root = BTRFS_I(inode)->root;
1806 struct btrfs_root *new_root;
1807 struct btrfs_dir_item *di;
1808 struct btrfs_trans_handle *trans;
1809 struct btrfs_path *path;
1810 struct btrfs_key location;
1811 struct btrfs_disk_key disk_key;
1812 struct btrfs_super_block *disk_super;
1813 u64 features;
1814 u64 objectid = 0;
1815 u64 dir_id;
1817 if (!capable(CAP_SYS_ADMIN))
1818 return -EPERM;
1820 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1821 return -EFAULT;
1823 if (!objectid)
1824 objectid = root->root_key.objectid;
1826 location.objectid = objectid;
1827 location.type = BTRFS_ROOT_ITEM_KEY;
1828 location.offset = (u64)-1;
1830 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1831 if (IS_ERR(new_root))
1832 return PTR_ERR(new_root);
1834 if (btrfs_root_refs(&new_root->root_item) == 0)
1835 return -ENOENT;
1837 path = btrfs_alloc_path();
1838 if (!path)
1839 return -ENOMEM;
1840 path->leave_spinning = 1;
1842 trans = btrfs_start_transaction(root, 1);
1843 if (!trans) {
1844 btrfs_free_path(path);
1845 return -ENOMEM;
1848 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
1849 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
1850 dir_id, "default", 7, 1);
1851 if (!di) {
1852 btrfs_free_path(path);
1853 btrfs_end_transaction(trans, root);
1854 printk(KERN_ERR "Umm, you don't have the default dir item, "
1855 "this isn't going to work\n");
1856 return -ENOENT;
1859 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
1860 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
1861 btrfs_mark_buffer_dirty(path->nodes[0]);
1862 btrfs_free_path(path);
1864 disk_super = &root->fs_info->super_copy;
1865 features = btrfs_super_incompat_flags(disk_super);
1866 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
1867 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
1868 btrfs_set_super_incompat_flags(disk_super, features);
1870 btrfs_end_transaction(trans, root);
1872 return 0;
1875 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1877 struct btrfs_ioctl_space_args space_args;
1878 struct btrfs_ioctl_space_info space;
1879 struct btrfs_ioctl_space_info *dest;
1880 struct btrfs_ioctl_space_info *dest_orig;
1881 struct btrfs_ioctl_space_info *user_dest;
1882 struct btrfs_space_info *info;
1883 int alloc_size;
1884 int ret = 0;
1885 int slot_count = 0;
1887 if (copy_from_user(&space_args,
1888 (struct btrfs_ioctl_space_args __user *)arg,
1889 sizeof(space_args)))
1890 return -EFAULT;
1892 /* first we count slots */
1893 rcu_read_lock();
1894 list_for_each_entry_rcu(info, &root->fs_info->space_info, list)
1895 slot_count++;
1896 rcu_read_unlock();
1898 /* space_slots == 0 means they are asking for a count */
1899 if (space_args.space_slots == 0) {
1900 space_args.total_spaces = slot_count;
1901 goto out;
1903 alloc_size = sizeof(*dest) * slot_count;
1904 /* we generally have at most 6 or so space infos, one for each raid
1905 * level. So, a whole page should be more than enough for everyone
1907 if (alloc_size > PAGE_CACHE_SIZE)
1908 return -ENOMEM;
1910 space_args.total_spaces = 0;
1911 dest = kmalloc(alloc_size, GFP_NOFS);
1912 if (!dest)
1913 return -ENOMEM;
1914 dest_orig = dest;
1916 /* now we have a buffer to copy into */
1917 rcu_read_lock();
1918 list_for_each_entry_rcu(info, &root->fs_info->space_info, list) {
1919 /* make sure we don't copy more than we allocated
1920 * in our buffer
1922 if (slot_count == 0)
1923 break;
1924 slot_count--;
1926 /* make sure userland has enough room in their buffer */
1927 if (space_args.total_spaces >= space_args.space_slots)
1928 break;
1930 space.flags = info->flags;
1931 space.total_bytes = info->total_bytes;
1932 space.used_bytes = info->bytes_used;
1933 memcpy(dest, &space, sizeof(space));
1934 dest++;
1935 space_args.total_spaces++;
1937 rcu_read_unlock();
1939 user_dest = (struct btrfs_ioctl_space_info *)
1940 (arg + sizeof(struct btrfs_ioctl_space_args));
1942 if (copy_to_user(user_dest, dest_orig, alloc_size))
1943 ret = -EFAULT;
1945 kfree(dest_orig);
1946 out:
1947 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1948 ret = -EFAULT;
1950 return ret;
1954 * there are many ways the trans_start and trans_end ioctls can lead
1955 * to deadlocks. They should only be used by applications that
1956 * basically own the machine, and have a very in depth understanding
1957 * of all the possible deadlocks and enospc problems.
1959 long btrfs_ioctl_trans_end(struct file *file)
1961 struct inode *inode = fdentry(file)->d_inode;
1962 struct btrfs_root *root = BTRFS_I(inode)->root;
1963 struct btrfs_trans_handle *trans;
1965 trans = file->private_data;
1966 if (!trans)
1967 return -EINVAL;
1968 file->private_data = NULL;
1970 btrfs_end_transaction(trans, root);
1972 mutex_lock(&root->fs_info->trans_mutex);
1973 root->fs_info->open_ioctl_trans--;
1974 mutex_unlock(&root->fs_info->trans_mutex);
1976 mnt_drop_write(file->f_path.mnt);
1977 return 0;
1980 long btrfs_ioctl(struct file *file, unsigned int
1981 cmd, unsigned long arg)
1983 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1984 void __user *argp = (void __user *)arg;
1986 switch (cmd) {
1987 case FS_IOC_GETFLAGS:
1988 return btrfs_ioctl_getflags(file, argp);
1989 case FS_IOC_SETFLAGS:
1990 return btrfs_ioctl_setflags(file, argp);
1991 case FS_IOC_GETVERSION:
1992 return btrfs_ioctl_getversion(file, argp);
1993 case BTRFS_IOC_SNAP_CREATE:
1994 return btrfs_ioctl_snap_create(file, argp, 0);
1995 case BTRFS_IOC_SUBVOL_CREATE:
1996 return btrfs_ioctl_snap_create(file, argp, 1);
1997 case BTRFS_IOC_SNAP_DESTROY:
1998 return btrfs_ioctl_snap_destroy(file, argp);
1999 case BTRFS_IOC_DEFAULT_SUBVOL:
2000 return btrfs_ioctl_default_subvol(file, argp);
2001 case BTRFS_IOC_DEFRAG:
2002 return btrfs_ioctl_defrag(file, NULL);
2003 case BTRFS_IOC_DEFRAG_RANGE:
2004 return btrfs_ioctl_defrag(file, argp);
2005 case BTRFS_IOC_RESIZE:
2006 return btrfs_ioctl_resize(root, argp);
2007 case BTRFS_IOC_ADD_DEV:
2008 return btrfs_ioctl_add_dev(root, argp);
2009 case BTRFS_IOC_RM_DEV:
2010 return btrfs_ioctl_rm_dev(root, argp);
2011 case BTRFS_IOC_BALANCE:
2012 return btrfs_balance(root->fs_info->dev_root);
2013 case BTRFS_IOC_CLONE:
2014 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2015 case BTRFS_IOC_CLONE_RANGE:
2016 return btrfs_ioctl_clone_range(file, argp);
2017 case BTRFS_IOC_TRANS_START:
2018 return btrfs_ioctl_trans_start(file);
2019 case BTRFS_IOC_TRANS_END:
2020 return btrfs_ioctl_trans_end(file);
2021 case BTRFS_IOC_TREE_SEARCH:
2022 return btrfs_ioctl_tree_search(file, argp);
2023 case BTRFS_IOC_INO_LOOKUP:
2024 return btrfs_ioctl_ino_lookup(file, argp);
2025 case BTRFS_IOC_SPACE_INFO:
2026 return btrfs_ioctl_space_info(root, argp);
2027 case BTRFS_IOC_SYNC:
2028 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2029 return 0;
2032 return -ENOTTY;