added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / fs / btrfs / ioctl.c
blobbca729fc80c83e07a3847b2b7bb6e73123d6c30f
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/smp_lock.h>
31 #include <linux/backing-dev.h>
32 #include <linux/mount.h>
33 #include <linux/mpage.h>
34 #include <linux/namei.h>
35 #include <linux/swap.h>
36 #include <linux/writeback.h>
37 #include <linux/statfs.h>
38 #include <linux/compat.h>
39 #include <linux/bit_spinlock.h>
40 #include <linux/security.h>
41 #include <linux/xattr.h>
42 #include <linux/vmalloc.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"
55 static noinline int create_subvol(struct btrfs_root *root,
56 struct dentry *dentry,
57 char *name, int namelen)
59 struct btrfs_trans_handle *trans;
60 struct btrfs_key key;
61 struct btrfs_root_item root_item;
62 struct btrfs_inode_item *inode_item;
63 struct extent_buffer *leaf;
64 struct btrfs_root *new_root = root;
65 struct inode *dir;
66 int ret;
67 int err;
68 u64 objectid;
69 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
70 u64 index = 0;
71 unsigned long nr = 1;
73 ret = btrfs_check_metadata_free_space(root);
74 if (ret)
75 goto fail_commit;
77 trans = btrfs_start_transaction(root, 1);
78 BUG_ON(!trans);
80 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
81 0, &objectid);
82 if (ret)
83 goto fail;
85 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
86 objectid, trans->transid, 0, 0, 0);
87 if (IS_ERR(leaf)) {
88 ret = PTR_ERR(leaf);
89 goto fail;
92 btrfs_set_header_nritems(leaf, 0);
93 btrfs_set_header_level(leaf, 0);
94 btrfs_set_header_bytenr(leaf, leaf->start);
95 btrfs_set_header_generation(leaf, trans->transid);
96 btrfs_set_header_owner(leaf, objectid);
98 write_extent_buffer(leaf, root->fs_info->fsid,
99 (unsigned long)btrfs_header_fsid(leaf),
100 BTRFS_FSID_SIZE);
101 btrfs_mark_buffer_dirty(leaf);
103 inode_item = &root_item.inode;
104 memset(inode_item, 0, sizeof(*inode_item));
105 inode_item->generation = cpu_to_le64(1);
106 inode_item->size = cpu_to_le64(3);
107 inode_item->nlink = cpu_to_le32(1);
108 inode_item->nbytes = cpu_to_le64(root->leafsize);
109 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
111 btrfs_set_root_bytenr(&root_item, leaf->start);
112 btrfs_set_root_generation(&root_item, trans->transid);
113 btrfs_set_root_level(&root_item, 0);
114 btrfs_set_root_refs(&root_item, 1);
115 btrfs_set_root_used(&root_item, 0);
116 btrfs_set_root_last_snapshot(&root_item, 0);
118 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
119 root_item.drop_level = 0;
121 btrfs_tree_unlock(leaf);
122 free_extent_buffer(leaf);
123 leaf = NULL;
125 btrfs_set_root_dirid(&root_item, new_dirid);
127 key.objectid = objectid;
128 key.offset = 1;
129 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
130 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
131 &root_item);
132 if (ret)
133 goto fail;
136 * insert the directory item
138 key.offset = (u64)-1;
139 dir = dentry->d_parent->d_inode;
140 ret = btrfs_set_inode_index(dir, &index);
141 BUG_ON(ret);
143 ret = btrfs_insert_dir_item(trans, root,
144 name, namelen, dir->i_ino, &key,
145 BTRFS_FT_DIR, index);
146 if (ret)
147 goto fail;
149 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
150 ret = btrfs_update_inode(trans, root, dir);
151 BUG_ON(ret);
153 /* add the backref first */
154 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
155 objectid, BTRFS_ROOT_BACKREF_KEY,
156 root->root_key.objectid,
157 dir->i_ino, index, name, namelen);
159 BUG_ON(ret);
161 /* now add the forward ref */
162 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
163 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
164 objectid,
165 dir->i_ino, index, name, namelen);
167 BUG_ON(ret);
169 ret = btrfs_commit_transaction(trans, root);
170 if (ret)
171 goto fail_commit;
173 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
174 BUG_ON(!new_root);
176 trans = btrfs_start_transaction(new_root, 1);
177 BUG_ON(!trans);
179 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
180 BTRFS_I(dir)->block_group);
181 if (ret)
182 goto fail;
184 fail:
185 nr = trans->blocks_used;
186 err = btrfs_commit_transaction(trans, new_root);
187 if (err && !ret)
188 ret = err;
189 fail_commit:
190 btrfs_btree_balance_dirty(root, nr);
191 return ret;
194 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
195 char *name, int namelen)
197 struct btrfs_pending_snapshot *pending_snapshot;
198 struct btrfs_trans_handle *trans;
199 int ret = 0;
200 int err;
201 unsigned long nr = 0;
203 if (!root->ref_cows)
204 return -EINVAL;
206 ret = btrfs_check_metadata_free_space(root);
207 if (ret)
208 goto fail_unlock;
210 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
211 if (!pending_snapshot) {
212 ret = -ENOMEM;
213 goto fail_unlock;
215 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
216 if (!pending_snapshot->name) {
217 ret = -ENOMEM;
218 kfree(pending_snapshot);
219 goto fail_unlock;
221 memcpy(pending_snapshot->name, name, namelen);
222 pending_snapshot->name[namelen] = '\0';
223 pending_snapshot->dentry = dentry;
224 trans = btrfs_start_transaction(root, 1);
225 BUG_ON(!trans);
226 pending_snapshot->root = root;
227 list_add(&pending_snapshot->list,
228 &trans->transaction->pending_snapshots);
229 err = btrfs_commit_transaction(trans, root);
231 fail_unlock:
232 btrfs_btree_balance_dirty(root, nr);
233 return ret;
236 /* copy of may_create in fs/namei.c() */
237 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
239 if (child->d_inode)
240 return -EEXIST;
241 if (IS_DEADDIR(dir))
242 return -ENOENT;
243 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
247 * Create a new subvolume below @parent. This is largely modeled after
248 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
249 * inside this filesystem so it's quite a bit simpler.
251 static noinline int btrfs_mksubvol(struct path *parent, char *name,
252 int mode, int namelen,
253 struct btrfs_root *snap_src)
255 struct dentry *dentry;
256 int error;
258 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
260 dentry = lookup_one_len(name, parent->dentry, namelen);
261 error = PTR_ERR(dentry);
262 if (IS_ERR(dentry))
263 goto out_unlock;
265 error = -EEXIST;
266 if (dentry->d_inode)
267 goto out_dput;
269 if (!IS_POSIXACL(parent->dentry->d_inode))
270 mode &= ~current->fs->umask;
272 error = mnt_want_write(parent->mnt);
273 if (error)
274 goto out_dput;
276 error = btrfs_may_create(parent->dentry->d_inode, dentry);
277 if (error)
278 goto out_drop_write;
281 * Actually perform the low-level subvolume creation after all
282 * this VFS fuzz.
284 * Eventually we want to pass in an inode under which we create this
285 * subvolume, but for now all are under the filesystem root.
287 * Also we should pass on the mode eventually to allow creating new
288 * subvolume with specific mode bits.
290 if (snap_src) {
291 struct dentry *dir = dentry->d_parent;
292 struct dentry *test = dir->d_parent;
293 struct btrfs_path *path = btrfs_alloc_path();
294 int ret;
295 u64 test_oid;
296 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
298 test_oid = snap_src->root_key.objectid;
300 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
301 path, parent_oid, test_oid);
302 if (ret == 0)
303 goto create;
304 btrfs_release_path(snap_src->fs_info->tree_root, path);
306 /* we need to make sure we aren't creating a directory loop
307 * by taking a snapshot of something that has our current
308 * subvol in its directory tree. So, this loops through
309 * the dentries and checks the forward refs for each subvolume
310 * to see if is references the subvolume where we are
311 * placing this new snapshot.
313 while (1) {
314 if (!test ||
315 dir == snap_src->fs_info->sb->s_root ||
316 test == snap_src->fs_info->sb->s_root ||
317 test->d_inode->i_sb != snap_src->fs_info->sb) {
318 break;
320 if (S_ISLNK(test->d_inode->i_mode)) {
321 printk(KERN_INFO "Btrfs symlink in snapshot "
322 "path, failed\n");
323 error = -EMLINK;
324 btrfs_free_path(path);
325 goto out_drop_write;
327 test_oid =
328 BTRFS_I(test->d_inode)->root->root_key.objectid;
329 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
330 path, test_oid, parent_oid);
331 if (ret == 0) {
332 printk(KERN_INFO "Btrfs snapshot creation "
333 "failed, looping\n");
334 error = -EMLINK;
335 btrfs_free_path(path);
336 goto out_drop_write;
338 btrfs_release_path(snap_src->fs_info->tree_root, path);
339 test = test->d_parent;
341 create:
342 btrfs_free_path(path);
343 error = create_snapshot(snap_src, dentry, name, namelen);
344 } else {
345 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
346 dentry, name, namelen);
348 if (error)
349 goto out_drop_write;
351 fsnotify_mkdir(parent->dentry->d_inode, dentry);
352 out_drop_write:
353 mnt_drop_write(parent->mnt);
354 out_dput:
355 dput(dentry);
356 out_unlock:
357 mutex_unlock(&parent->dentry->d_inode->i_mutex);
358 return error;
362 static int btrfs_defrag_file(struct file *file)
364 struct inode *inode = fdentry(file)->d_inode;
365 struct btrfs_root *root = BTRFS_I(inode)->root;
366 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
367 struct btrfs_ordered_extent *ordered;
368 struct page *page;
369 unsigned long last_index;
370 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
371 unsigned long total_read = 0;
372 u64 page_start;
373 u64 page_end;
374 unsigned long i;
375 int ret;
377 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
378 if (ret)
379 return -ENOSPC;
381 mutex_lock(&inode->i_mutex);
382 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
383 for (i = 0; i <= last_index; i++) {
384 if (total_read % ra_pages == 0) {
385 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
386 min(last_index, i + ra_pages - 1));
388 total_read++;
389 again:
390 page = grab_cache_page(inode->i_mapping, i);
391 if (!page)
392 goto out_unlock;
393 if (!PageUptodate(page)) {
394 btrfs_readpage(NULL, page);
395 lock_page(page);
396 if (!PageUptodate(page)) {
397 unlock_page(page);
398 page_cache_release(page);
399 goto out_unlock;
403 wait_on_page_writeback(page);
405 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
406 page_end = page_start + PAGE_CACHE_SIZE - 1;
407 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
409 ordered = btrfs_lookup_ordered_extent(inode, page_start);
410 if (ordered) {
411 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
412 unlock_page(page);
413 page_cache_release(page);
414 btrfs_start_ordered_extent(inode, ordered, 1);
415 btrfs_put_ordered_extent(ordered);
416 goto again;
418 set_page_extent_mapped(page);
421 * this makes sure page_mkwrite is called on the
422 * page if it is dirtied again later
424 clear_page_dirty_for_io(page);
426 btrfs_set_extent_delalloc(inode, page_start, page_end);
428 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
429 set_page_dirty(page);
430 unlock_page(page);
431 page_cache_release(page);
432 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
435 out_unlock:
436 mutex_unlock(&inode->i_mutex);
437 return 0;
441 * Called inside transaction, so use GFP_NOFS
444 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
446 u64 new_size;
447 u64 old_size;
448 u64 devid = 1;
449 struct btrfs_ioctl_vol_args *vol_args;
450 struct btrfs_trans_handle *trans;
451 struct btrfs_device *device = NULL;
452 char *sizestr;
453 char *devstr = NULL;
454 int ret = 0;
455 int namelen;
456 int mod = 0;
458 if (root->fs_info->sb->s_flags & MS_RDONLY)
459 return -EROFS;
461 if (!capable(CAP_SYS_ADMIN))
462 return -EPERM;
464 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
466 if (!vol_args)
467 return -ENOMEM;
469 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
470 ret = -EFAULT;
471 goto out;
474 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
475 namelen = strlen(vol_args->name);
477 mutex_lock(&root->fs_info->volume_mutex);
478 sizestr = vol_args->name;
479 devstr = strchr(sizestr, ':');
480 if (devstr) {
481 char *end;
482 sizestr = devstr + 1;
483 *devstr = '\0';
484 devstr = vol_args->name;
485 devid = simple_strtoull(devstr, &end, 10);
486 printk(KERN_INFO "resizing devid %llu\n", devid);
488 device = btrfs_find_device(root, devid, NULL, NULL);
489 if (!device) {
490 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
491 ret = -EINVAL;
492 goto out_unlock;
494 if (!strcmp(sizestr, "max"))
495 new_size = device->bdev->bd_inode->i_size;
496 else {
497 if (sizestr[0] == '-') {
498 mod = -1;
499 sizestr++;
500 } else if (sizestr[0] == '+') {
501 mod = 1;
502 sizestr++;
504 new_size = btrfs_parse_size(sizestr);
505 if (new_size == 0) {
506 ret = -EINVAL;
507 goto out_unlock;
511 old_size = device->total_bytes;
513 if (mod < 0) {
514 if (new_size > old_size) {
515 ret = -EINVAL;
516 goto out_unlock;
518 new_size = old_size - new_size;
519 } else if (mod > 0) {
520 new_size = old_size + new_size;
523 if (new_size < 256 * 1024 * 1024) {
524 ret = -EINVAL;
525 goto out_unlock;
527 if (new_size > device->bdev->bd_inode->i_size) {
528 ret = -EFBIG;
529 goto out_unlock;
532 do_div(new_size, root->sectorsize);
533 new_size *= root->sectorsize;
535 printk(KERN_INFO "new size for %s is %llu\n",
536 device->name, (unsigned long long)new_size);
538 if (new_size > old_size) {
539 trans = btrfs_start_transaction(root, 1);
540 ret = btrfs_grow_device(trans, device, new_size);
541 btrfs_commit_transaction(trans, root);
542 } else {
543 ret = btrfs_shrink_device(device, new_size);
546 out_unlock:
547 mutex_unlock(&root->fs_info->volume_mutex);
548 out:
549 kfree(vol_args);
550 return ret;
553 static noinline int btrfs_ioctl_snap_create(struct file *file,
554 void __user *arg, int subvol)
556 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
557 struct btrfs_ioctl_vol_args *vol_args;
558 struct btrfs_dir_item *di;
559 struct btrfs_path *path;
560 struct file *src_file;
561 u64 root_dirid;
562 int namelen;
563 int ret = 0;
565 if (root->fs_info->sb->s_flags & MS_RDONLY)
566 return -EROFS;
568 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
570 if (!vol_args)
571 return -ENOMEM;
573 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
574 ret = -EFAULT;
575 goto out;
578 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
579 namelen = strlen(vol_args->name);
580 if (strchr(vol_args->name, '/')) {
581 ret = -EINVAL;
582 goto out;
585 path = btrfs_alloc_path();
586 if (!path) {
587 ret = -ENOMEM;
588 goto out;
591 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
592 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
593 path, root_dirid,
594 vol_args->name, namelen, 0);
595 btrfs_free_path(path);
597 if (di && !IS_ERR(di)) {
598 ret = -EEXIST;
599 goto out;
602 if (IS_ERR(di)) {
603 ret = PTR_ERR(di);
604 goto out;
607 if (subvol) {
608 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
609 file->f_path.dentry->d_inode->i_mode,
610 namelen, NULL);
611 } else {
612 struct inode *src_inode;
613 src_file = fget(vol_args->fd);
614 if (!src_file) {
615 ret = -EINVAL;
616 goto out;
619 src_inode = src_file->f_path.dentry->d_inode;
620 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
621 printk(KERN_INFO "btrfs: Snapshot src from "
622 "another FS\n");
623 ret = -EINVAL;
624 fput(src_file);
625 goto out;
627 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
628 file->f_path.dentry->d_inode->i_mode,
629 namelen, BTRFS_I(src_inode)->root);
630 fput(src_file);
633 out:
634 kfree(vol_args);
635 return ret;
638 static int btrfs_ioctl_defrag(struct file *file)
640 struct inode *inode = fdentry(file)->d_inode;
641 struct btrfs_root *root = BTRFS_I(inode)->root;
642 int ret;
644 ret = mnt_want_write(file->f_path.mnt);
645 if (ret)
646 return ret;
648 switch (inode->i_mode & S_IFMT) {
649 case S_IFDIR:
650 if (!capable(CAP_SYS_ADMIN)) {
651 ret = -EPERM;
652 goto out;
654 btrfs_defrag_root(root, 0);
655 btrfs_defrag_root(root->fs_info->extent_root, 0);
656 break;
657 case S_IFREG:
658 if (!(file->f_mode & FMODE_WRITE)) {
659 ret = -EINVAL;
660 goto out;
662 btrfs_defrag_file(file);
663 break;
665 out:
666 mnt_drop_write(file->f_path.mnt);
667 return ret;
670 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
672 struct btrfs_ioctl_vol_args *vol_args;
673 int ret;
675 if (!capable(CAP_SYS_ADMIN))
676 return -EPERM;
678 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
680 if (!vol_args)
681 return -ENOMEM;
683 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
684 ret = -EFAULT;
685 goto out;
687 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
688 ret = btrfs_init_new_device(root, vol_args->name);
690 out:
691 kfree(vol_args);
692 return ret;
695 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
697 struct btrfs_ioctl_vol_args *vol_args;
698 int ret;
700 if (!capable(CAP_SYS_ADMIN))
701 return -EPERM;
703 if (root->fs_info->sb->s_flags & MS_RDONLY)
704 return -EROFS;
706 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
708 if (!vol_args)
709 return -ENOMEM;
711 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
712 ret = -EFAULT;
713 goto out;
715 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
716 ret = btrfs_rm_device(root, vol_args->name);
718 out:
719 kfree(vol_args);
720 return ret;
723 static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
724 u64 off, u64 olen, u64 destoff)
726 struct inode *inode = fdentry(file)->d_inode;
727 struct btrfs_root *root = BTRFS_I(inode)->root;
728 struct file *src_file;
729 struct inode *src;
730 struct btrfs_trans_handle *trans;
731 struct btrfs_path *path;
732 struct extent_buffer *leaf;
733 char *buf;
734 struct btrfs_key key;
735 u32 nritems;
736 int slot;
737 int ret;
738 u64 len = olen;
739 u64 bs = root->fs_info->sb->s_blocksize;
740 u64 hint_byte;
743 * TODO:
744 * - split compressed inline extents. annoying: we need to
745 * decompress into destination's address_space (the file offset
746 * may change, so source mapping won't do), then recompress (or
747 * otherwise reinsert) a subrange.
748 * - allow ranges within the same file to be cloned (provided
749 * they don't overlap)?
752 /* the destination must be opened for writing */
753 if (!(file->f_mode & FMODE_WRITE))
754 return -EINVAL;
756 ret = mnt_want_write(file->f_path.mnt);
757 if (ret)
758 return ret;
760 src_file = fget(srcfd);
761 if (!src_file) {
762 ret = -EBADF;
763 goto out_drop_write;
765 src = src_file->f_dentry->d_inode;
767 ret = -EINVAL;
768 if (src == inode)
769 goto out_fput;
771 ret = -EISDIR;
772 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
773 goto out_fput;
775 ret = -EXDEV;
776 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
777 goto out_fput;
779 ret = -ENOMEM;
780 buf = vmalloc(btrfs_level_size(root, 0));
781 if (!buf)
782 goto out_fput;
784 path = btrfs_alloc_path();
785 if (!path) {
786 vfree(buf);
787 goto out_fput;
789 path->reada = 2;
791 if (inode < src) {
792 mutex_lock(&inode->i_mutex);
793 mutex_lock(&src->i_mutex);
794 } else {
795 mutex_lock(&src->i_mutex);
796 mutex_lock(&inode->i_mutex);
799 /* determine range to clone */
800 ret = -EINVAL;
801 if (off >= src->i_size || off + len > src->i_size)
802 goto out_unlock;
803 if (len == 0)
804 olen = len = src->i_size - off;
805 /* if we extend to eof, continue to block boundary */
806 if (off + len == src->i_size)
807 len = ((src->i_size + bs-1) & ~(bs-1))
808 - off;
810 /* verify the end result is block aligned */
811 if ((off & (bs-1)) ||
812 ((off + len) & (bs-1)))
813 goto out_unlock;
815 /* do any pending delalloc/csum calc on src, one way or
816 another, and lock file content */
817 while (1) {
818 struct btrfs_ordered_extent *ordered;
819 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
820 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
821 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
822 break;
823 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
824 if (ordered)
825 btrfs_put_ordered_extent(ordered);
826 btrfs_wait_ordered_range(src, off, off+len);
829 trans = btrfs_start_transaction(root, 1);
830 BUG_ON(!trans);
832 /* punch hole in destination first */
833 btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
835 /* clone data */
836 key.objectid = src->i_ino;
837 key.type = BTRFS_EXTENT_DATA_KEY;
838 key.offset = 0;
840 while (1) {
842 * note the key will change type as we walk through the
843 * tree.
845 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
846 if (ret < 0)
847 goto out;
849 nritems = btrfs_header_nritems(path->nodes[0]);
850 if (path->slots[0] >= nritems) {
851 ret = btrfs_next_leaf(root, path);
852 if (ret < 0)
853 goto out;
854 if (ret > 0)
855 break;
856 nritems = btrfs_header_nritems(path->nodes[0]);
858 leaf = path->nodes[0];
859 slot = path->slots[0];
861 btrfs_item_key_to_cpu(leaf, &key, slot);
862 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
863 key.objectid != src->i_ino)
864 break;
866 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
867 struct btrfs_file_extent_item *extent;
868 int type;
869 u32 size;
870 struct btrfs_key new_key;
871 u64 disko = 0, diskl = 0;
872 u64 datao = 0, datal = 0;
873 u8 comp;
875 size = btrfs_item_size_nr(leaf, slot);
876 read_extent_buffer(leaf, buf,
877 btrfs_item_ptr_offset(leaf, slot),
878 size);
880 extent = btrfs_item_ptr(leaf, slot,
881 struct btrfs_file_extent_item);
882 comp = btrfs_file_extent_compression(leaf, extent);
883 type = btrfs_file_extent_type(leaf, extent);
884 if (type == BTRFS_FILE_EXTENT_REG) {
885 disko = btrfs_file_extent_disk_bytenr(leaf,
886 extent);
887 diskl = btrfs_file_extent_disk_num_bytes(leaf,
888 extent);
889 datao = btrfs_file_extent_offset(leaf, extent);
890 datal = btrfs_file_extent_num_bytes(leaf,
891 extent);
892 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
893 /* take upper bound, may be compressed */
894 datal = btrfs_file_extent_ram_bytes(leaf,
895 extent);
897 btrfs_release_path(root, path);
899 if (key.offset + datal < off ||
900 key.offset >= off+len)
901 goto next;
903 memcpy(&new_key, &key, sizeof(new_key));
904 new_key.objectid = inode->i_ino;
905 new_key.offset = key.offset + destoff - off;
907 if (type == BTRFS_FILE_EXTENT_REG) {
908 ret = btrfs_insert_empty_item(trans, root, path,
909 &new_key, size);
910 if (ret)
911 goto out;
913 leaf = path->nodes[0];
914 slot = path->slots[0];
915 write_extent_buffer(leaf, buf,
916 btrfs_item_ptr_offset(leaf, slot),
917 size);
919 extent = btrfs_item_ptr(leaf, slot,
920 struct btrfs_file_extent_item);
922 if (off > key.offset) {
923 datao += off - key.offset;
924 datal -= off - key.offset;
926 if (key.offset + datao + datal + key.offset >
927 off + len)
928 datal = off + len - key.offset - datao;
929 /* disko == 0 means it's a hole */
930 if (!disko)
931 datao = 0;
933 btrfs_set_file_extent_offset(leaf, extent,
934 datao);
935 btrfs_set_file_extent_num_bytes(leaf, extent,
936 datal);
937 if (disko) {
938 inode_add_bytes(inode, datal);
939 ret = btrfs_inc_extent_ref(trans, root,
940 disko, diskl, leaf->start,
941 root->root_key.objectid,
942 trans->transid,
943 inode->i_ino);
944 BUG_ON(ret);
946 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
947 u64 skip = 0;
948 u64 trim = 0;
949 if (off > key.offset) {
950 skip = off - key.offset;
951 new_key.offset += skip;
954 if (key.offset + datal > off+len)
955 trim = key.offset + datal - (off+len);
957 if (comp && (skip || trim)) {
958 ret = -EINVAL;
959 goto out;
961 size -= skip + trim;
962 datal -= skip + trim;
963 ret = btrfs_insert_empty_item(trans, root, path,
964 &new_key, size);
965 if (ret)
966 goto out;
968 if (skip) {
969 u32 start =
970 btrfs_file_extent_calc_inline_size(0);
971 memmove(buf+start, buf+start+skip,
972 datal);
975 leaf = path->nodes[0];
976 slot = path->slots[0];
977 write_extent_buffer(leaf, buf,
978 btrfs_item_ptr_offset(leaf, slot),
979 size);
980 inode_add_bytes(inode, datal);
983 btrfs_mark_buffer_dirty(leaf);
986 next:
987 btrfs_release_path(root, path);
988 key.offset++;
990 ret = 0;
991 out:
992 btrfs_release_path(root, path);
993 if (ret == 0) {
994 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
995 if (destoff + olen > inode->i_size)
996 btrfs_i_size_write(inode, destoff + olen);
997 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
998 ret = btrfs_update_inode(trans, root, inode);
1000 btrfs_end_transaction(trans, root);
1001 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1002 if (ret)
1003 vmtruncate(inode, 0);
1004 out_unlock:
1005 mutex_unlock(&src->i_mutex);
1006 mutex_unlock(&inode->i_mutex);
1007 vfree(buf);
1008 btrfs_free_path(path);
1009 out_fput:
1010 fput(src_file);
1011 out_drop_write:
1012 mnt_drop_write(file->f_path.mnt);
1013 return ret;
1016 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1018 struct btrfs_ioctl_clone_range_args args;
1020 if (copy_from_user(&args, argp, sizeof(args)))
1021 return -EFAULT;
1022 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1023 args.src_length, args.dest_offset);
1027 * there are many ways the trans_start and trans_end ioctls can lead
1028 * to deadlocks. They should only be used by applications that
1029 * basically own the machine, and have a very in depth understanding
1030 * of all the possible deadlocks and enospc problems.
1032 static long btrfs_ioctl_trans_start(struct file *file)
1034 struct inode *inode = fdentry(file)->d_inode;
1035 struct btrfs_root *root = BTRFS_I(inode)->root;
1036 struct btrfs_trans_handle *trans;
1037 int ret = 0;
1039 if (!capable(CAP_SYS_ADMIN))
1040 return -EPERM;
1042 if (file->private_data) {
1043 ret = -EINPROGRESS;
1044 goto out;
1047 ret = mnt_want_write(file->f_path.mnt);
1048 if (ret)
1049 goto out;
1051 mutex_lock(&root->fs_info->trans_mutex);
1052 root->fs_info->open_ioctl_trans++;
1053 mutex_unlock(&root->fs_info->trans_mutex);
1055 trans = btrfs_start_ioctl_transaction(root, 0);
1056 if (trans)
1057 file->private_data = trans;
1058 else
1059 ret = -ENOMEM;
1060 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1061 out:
1062 return ret;
1066 * there are many ways the trans_start and trans_end ioctls can lead
1067 * to deadlocks. They should only be used by applications that
1068 * basically own the machine, and have a very in depth understanding
1069 * of all the possible deadlocks and enospc problems.
1071 long btrfs_ioctl_trans_end(struct file *file)
1073 struct inode *inode = fdentry(file)->d_inode;
1074 struct btrfs_root *root = BTRFS_I(inode)->root;
1075 struct btrfs_trans_handle *trans;
1076 int ret = 0;
1078 trans = file->private_data;
1079 if (!trans) {
1080 ret = -EINVAL;
1081 goto out;
1083 btrfs_end_transaction(trans, root);
1084 file->private_data = NULL;
1086 mutex_lock(&root->fs_info->trans_mutex);
1087 root->fs_info->open_ioctl_trans--;
1088 mutex_unlock(&root->fs_info->trans_mutex);
1090 mnt_drop_write(file->f_path.mnt);
1092 out:
1093 return ret;
1096 long btrfs_ioctl(struct file *file, unsigned int
1097 cmd, unsigned long arg)
1099 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1100 void __user *argp = (void __user *)arg;
1102 switch (cmd) {
1103 case BTRFS_IOC_SNAP_CREATE:
1104 return btrfs_ioctl_snap_create(file, argp, 0);
1105 case BTRFS_IOC_SUBVOL_CREATE:
1106 return btrfs_ioctl_snap_create(file, argp, 1);
1107 case BTRFS_IOC_DEFRAG:
1108 return btrfs_ioctl_defrag(file);
1109 case BTRFS_IOC_RESIZE:
1110 return btrfs_ioctl_resize(root, argp);
1111 case BTRFS_IOC_ADD_DEV:
1112 return btrfs_ioctl_add_dev(root, argp);
1113 case BTRFS_IOC_RM_DEV:
1114 return btrfs_ioctl_rm_dev(root, argp);
1115 case BTRFS_IOC_BALANCE:
1116 return btrfs_balance(root->fs_info->dev_root);
1117 case BTRFS_IOC_CLONE:
1118 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1119 case BTRFS_IOC_CLONE_RANGE:
1120 return btrfs_ioctl_clone_range(file, argp);
1121 case BTRFS_IOC_TRANS_START:
1122 return btrfs_ioctl_trans_start(file);
1123 case BTRFS_IOC_TRANS_END:
1124 return btrfs_ioctl_trans_end(file);
1125 case BTRFS_IOC_SYNC:
1126 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1127 return 0;
1130 return -ENOTTY;