Btrfs: Give each subvol and snapshot their own anonymous devid
[linux-block.git] / fs / btrfs / ioctl.c
CommitLineData
f46b5a66
CH
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
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>
cb8e7090 24#include <linux/fsnotify.h>
f46b5a66
CH
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>
cb8e7090 32#include <linux/mount.h>
f46b5a66 33#include <linux/mpage.h>
cb8e7090 34#include <linux/namei.h>
f46b5a66
CH
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>
cb8e7090 40#include <linux/security.h>
f46b5a66
CH
41#include <linux/version.h>
42#include <linux/xattr.h>
7ea394f1 43#include <linux/vmalloc.h>
f46b5a66
CH
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"
925baedd 51#include "locking.h"
f46b5a66
CH
52
53
54
cb8e7090
CH
55static noinline int create_subvol(struct btrfs_root *root,
56 struct dentry *dentry,
57 char *name, int namelen)
f46b5a66
CH
58{
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;
3de4586c 70 u64 index = 0;
f46b5a66
CH
71 unsigned long nr = 1;
72
f46b5a66
CH
73 ret = btrfs_check_free_space(root, 1, 0);
74 if (ret)
75 goto fail_commit;
76
77 trans = btrfs_start_transaction(root, 1);
78 BUG_ON(!trans);
79
80 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
81 0, &objectid);
82 if (ret)
83 goto fail;
84
31840ae1
ZY
85 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
86 objectid, trans->transid, 0, 0, 0);
8e8a1e31
JB
87 if (IS_ERR(leaf)) {
88 ret = PTR_ERR(leaf);
89 goto fail;
90 }
f46b5a66
CH
91
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);
97
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);
102
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);
a76a3cd4 108 inode_item->nbytes = cpu_to_le64(root->leafsize);
f46b5a66
CH
109 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
110
111 btrfs_set_root_bytenr(&root_item, leaf->start);
84234f3a 112 btrfs_set_root_generation(&root_item, trans->transid);
f46b5a66
CH
113 btrfs_set_root_level(&root_item, 0);
114 btrfs_set_root_refs(&root_item, 1);
115 btrfs_set_root_used(&root_item, 0);
80ff3856 116 btrfs_set_root_last_snapshot(&root_item, 0);
f46b5a66
CH
117
118 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
119 root_item.drop_level = 0;
120
925baedd 121 btrfs_tree_unlock(leaf);
f46b5a66
CH
122 free_extent_buffer(leaf);
123 leaf = NULL;
124
125 btrfs_set_root_dirid(&root_item, new_dirid);
126
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;
134
135 /*
136 * insert the directory item
137 */
138 key.offset = (u64)-1;
3de4586c
CM
139 dir = dentry->d_parent->d_inode;
140 ret = btrfs_set_inode_index(dir, &index);
141 BUG_ON(ret);
142
143 ret = btrfs_insert_dir_item(trans, root,
f46b5a66 144 name, namelen, dir->i_ino, &key,
3de4586c 145 BTRFS_FT_DIR, index);
f46b5a66
CH
146 if (ret)
147 goto fail;
3de4586c 148#if 0
f46b5a66
CH
149 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
150 name, namelen, objectid,
aec7477b 151 root->fs_info->sb->s_root->d_inode->i_ino, 0);
f46b5a66
CH
152 if (ret)
153 goto fail;
3de4586c 154#endif
f46b5a66
CH
155 ret = btrfs_commit_transaction(trans, root);
156 if (ret)
157 goto fail_commit;
158
3de4586c 159 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
f46b5a66
CH
160 BUG_ON(!new_root);
161
162 trans = btrfs_start_transaction(new_root, 1);
163 BUG_ON(!trans);
164
cb8e7090 165 ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
f46b5a66
CH
166 BTRFS_I(dir)->block_group);
167 if (ret)
168 goto fail;
169
f46b5a66
CH
170fail:
171 nr = trans->blocks_used;
172 err = btrfs_commit_transaction(trans, new_root);
173 if (err && !ret)
174 ret = err;
175fail_commit:
f46b5a66 176 btrfs_btree_balance_dirty(root, nr);
f46b5a66
CH
177 return ret;
178}
179
3de4586c
CM
180static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
181 char *name, int namelen)
f46b5a66
CH
182{
183 struct btrfs_pending_snapshot *pending_snapshot;
184 struct btrfs_trans_handle *trans;
3de4586c 185 int ret = 0;
f46b5a66
CH
186 int err;
187 unsigned long nr = 0;
188
189 if (!root->ref_cows)
190 return -EINVAL;
191
f46b5a66
CH
192 ret = btrfs_check_free_space(root, 1, 0);
193 if (ret)
194 goto fail_unlock;
195
3de4586c 196 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
f46b5a66
CH
197 if (!pending_snapshot) {
198 ret = -ENOMEM;
199 goto fail_unlock;
200 }
201 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
202 if (!pending_snapshot->name) {
203 ret = -ENOMEM;
204 kfree(pending_snapshot);
205 goto fail_unlock;
206 }
207 memcpy(pending_snapshot->name, name, namelen);
208 pending_snapshot->name[namelen] = '\0';
3de4586c 209 pending_snapshot->dentry = dentry;
f46b5a66
CH
210 trans = btrfs_start_transaction(root, 1);
211 BUG_ON(!trans);
212 pending_snapshot->root = root;
213 list_add(&pending_snapshot->list,
214 &trans->transaction->pending_snapshots);
f46b5a66
CH
215 err = btrfs_commit_transaction(trans, root);
216
217fail_unlock:
f46b5a66 218 btrfs_btree_balance_dirty(root, nr);
f46b5a66
CH
219 return ret;
220}
221
cb8e7090
CH
222/* copy of may_create in fs/namei.c() */
223static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
224{
225 if (child->d_inode)
226 return -EEXIST;
227 if (IS_DEADDIR(dir))
228 return -ENOENT;
229 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
230}
231
232/*
233 * Create a new subvolume below @parent. This is largely modeled after
234 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
235 * inside this filesystem so it's quite a bit simpler.
236 */
237static noinline int btrfs_mksubvol(struct path *parent, char *name,
3de4586c
CM
238 int mode, int namelen,
239 struct btrfs_root *snap_src)
cb8e7090
CH
240{
241 struct dentry *dentry;
242 int error;
243
244 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
245
246 dentry = lookup_one_len(name, parent->dentry, namelen);
247 error = PTR_ERR(dentry);
248 if (IS_ERR(dentry))
249 goto out_unlock;
250
251 error = -EEXIST;
252 if (dentry->d_inode)
253 goto out_dput;
254
255 if (!IS_POSIXACL(parent->dentry->d_inode))
256 mode &= ~current->fs->umask;
3de4586c 257
cb8e7090
CH
258 error = mnt_want_write(parent->mnt);
259 if (error)
260 goto out_dput;
261
262 error = btrfs_may_create(parent->dentry->d_inode, dentry);
263 if (error)
264 goto out_drop_write;
265
cb8e7090
CH
266 /*
267 * Actually perform the low-level subvolume creation after all
268 * this VFS fuzz.
269 *
270 * Eventually we want to pass in an inode under which we create this
271 * subvolume, but for now all are under the filesystem root.
272 *
273 * Also we should pass on the mode eventually to allow creating new
274 * subvolume with specific mode bits.
275 */
3de4586c
CM
276 if (snap_src) {
277 error = create_snapshot(snap_src, dentry, name, namelen);
278 } else {
279 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
280 dentry, name, namelen);
281 }
cb8e7090
CH
282 if (error)
283 goto out_drop_write;
284
285 fsnotify_mkdir(parent->dentry->d_inode, dentry);
286out_drop_write:
287 mnt_drop_write(parent->mnt);
288out_dput:
289 dput(dentry);
290out_unlock:
291 mutex_unlock(&parent->dentry->d_inode->i_mutex);
292 return error;
293}
294
295
f46b5a66
CH
296int btrfs_defrag_file(struct file *file)
297{
298 struct inode *inode = fdentry(file)->d_inode;
299 struct btrfs_root *root = BTRFS_I(inode)->root;
300 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3eaa2885 301 struct btrfs_ordered_extent *ordered;
f46b5a66
CH
302 struct page *page;
303 unsigned long last_index;
304 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
305 unsigned long total_read = 0;
306 u64 page_start;
307 u64 page_end;
308 unsigned long i;
309 int ret;
310
f46b5a66 311 ret = btrfs_check_free_space(root, inode->i_size, 0);
f46b5a66
CH
312 if (ret)
313 return -ENOSPC;
314
315 mutex_lock(&inode->i_mutex);
316 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
317 for (i = 0; i <= last_index; i++) {
318 if (total_read % ra_pages == 0) {
319 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
320 min(last_index, i + ra_pages - 1));
321 }
322 total_read++;
3eaa2885 323again:
f46b5a66
CH
324 page = grab_cache_page(inode->i_mapping, i);
325 if (!page)
326 goto out_unlock;
327 if (!PageUptodate(page)) {
328 btrfs_readpage(NULL, page);
329 lock_page(page);
330 if (!PageUptodate(page)) {
331 unlock_page(page);
332 page_cache_release(page);
333 goto out_unlock;
334 }
335 }
336
f46b5a66 337 wait_on_page_writeback(page);
f46b5a66
CH
338
339 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
340 page_end = page_start + PAGE_CACHE_SIZE - 1;
f46b5a66 341 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3eaa2885
CM
342
343 ordered = btrfs_lookup_ordered_extent(inode, page_start);
344 if (ordered) {
345 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
346 unlock_page(page);
347 page_cache_release(page);
348 btrfs_start_ordered_extent(inode, ordered, 1);
349 btrfs_put_ordered_extent(ordered);
350 goto again;
351 }
352 set_page_extent_mapped(page);
353
f87f057b
CM
354 /*
355 * this makes sure page_mkwrite is called on the
356 * page if it is dirtied again later
357 */
358 clear_page_dirty_for_io(page);
359
ea8c2819 360 btrfs_set_extent_delalloc(inode, page_start, page_end);
f46b5a66
CH
361
362 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
363 set_page_dirty(page);
364 unlock_page(page);
365 page_cache_release(page);
366 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
367 }
368
369out_unlock:
370 mutex_unlock(&inode->i_mutex);
371 return 0;
372}
373
374/*
375 * Called inside transaction, so use GFP_NOFS
376 */
377
378static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
379{
380 u64 new_size;
381 u64 old_size;
382 u64 devid = 1;
383 struct btrfs_ioctl_vol_args *vol_args;
384 struct btrfs_trans_handle *trans;
385 struct btrfs_device *device = NULL;
386 char *sizestr;
387 char *devstr = NULL;
388 int ret = 0;
389 int namelen;
390 int mod = 0;
391
c146afad
YZ
392 if (root->fs_info->sb->s_flags & MS_RDONLY)
393 return -EROFS;
394
f46b5a66
CH
395 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
396
397 if (!vol_args)
398 return -ENOMEM;
399
400 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
401 ret = -EFAULT;
402 goto out;
403 }
5516e595
MF
404
405 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 406 namelen = strlen(vol_args->name);
f46b5a66 407
7d9eb12c 408 mutex_lock(&root->fs_info->volume_mutex);
f46b5a66
CH
409 sizestr = vol_args->name;
410 devstr = strchr(sizestr, ':');
411 if (devstr) {
412 char *end;
413 sizestr = devstr + 1;
414 *devstr = '\0';
415 devstr = vol_args->name;
416 devid = simple_strtoull(devstr, &end, 10);
417 printk(KERN_INFO "resizing devid %llu\n", devid);
418 }
2b82032c 419 device = btrfs_find_device(root, devid, NULL, NULL);
f46b5a66
CH
420 if (!device) {
421 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
422 ret = -EINVAL;
423 goto out_unlock;
424 }
425 if (!strcmp(sizestr, "max"))
426 new_size = device->bdev->bd_inode->i_size;
427 else {
428 if (sizestr[0] == '-') {
429 mod = -1;
430 sizestr++;
431 } else if (sizestr[0] == '+') {
432 mod = 1;
433 sizestr++;
434 }
435 new_size = btrfs_parse_size(sizestr);
436 if (new_size == 0) {
437 ret = -EINVAL;
438 goto out_unlock;
439 }
440 }
441
442 old_size = device->total_bytes;
443
444 if (mod < 0) {
445 if (new_size > old_size) {
446 ret = -EINVAL;
447 goto out_unlock;
448 }
449 new_size = old_size - new_size;
450 } else if (mod > 0) {
451 new_size = old_size + new_size;
452 }
453
454 if (new_size < 256 * 1024 * 1024) {
455 ret = -EINVAL;
456 goto out_unlock;
457 }
458 if (new_size > device->bdev->bd_inode->i_size) {
459 ret = -EFBIG;
460 goto out_unlock;
461 }
462
463 do_div(new_size, root->sectorsize);
464 new_size *= root->sectorsize;
465
466 printk(KERN_INFO "new size for %s is %llu\n",
467 device->name, (unsigned long long)new_size);
468
469 if (new_size > old_size) {
470 trans = btrfs_start_transaction(root, 1);
471 ret = btrfs_grow_device(trans, device, new_size);
472 btrfs_commit_transaction(trans, root);
473 } else {
474 ret = btrfs_shrink_device(device, new_size);
475 }
476
477out_unlock:
7d9eb12c 478 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
479out:
480 kfree(vol_args);
481 return ret;
482}
483
cb8e7090 484static noinline int btrfs_ioctl_snap_create(struct file *file,
3de4586c 485 void __user *arg, int subvol)
f46b5a66 486{
cb8e7090 487 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
f46b5a66
CH
488 struct btrfs_ioctl_vol_args *vol_args;
489 struct btrfs_dir_item *di;
490 struct btrfs_path *path;
3de4586c 491 struct file *src_file;
f46b5a66
CH
492 u64 root_dirid;
493 int namelen;
3de4586c 494 int ret = 0;
f46b5a66 495
c146afad
YZ
496 if (root->fs_info->sb->s_flags & MS_RDONLY)
497 return -EROFS;
498
f46b5a66
CH
499 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
500
501 if (!vol_args)
502 return -ENOMEM;
503
504 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
505 ret = -EFAULT;
506 goto out;
507 }
508
5516e595 509 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 510 namelen = strlen(vol_args->name);
f46b5a66
CH
511 if (strchr(vol_args->name, '/')) {
512 ret = -EINVAL;
513 goto out;
514 }
515
516 path = btrfs_alloc_path();
517 if (!path) {
518 ret = -ENOMEM;
519 goto out;
520 }
521
522 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
f46b5a66
CH
523 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
524 path, root_dirid,
525 vol_args->name, namelen, 0);
f46b5a66
CH
526 btrfs_free_path(path);
527
528 if (di && !IS_ERR(di)) {
529 ret = -EEXIST;
530 goto out;
531 }
532
533 if (IS_ERR(di)) {
534 ret = PTR_ERR(di);
535 goto out;
536 }
537
3de4586c 538 if (subvol) {
cb8e7090
CH
539 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
540 file->f_path.dentry->d_inode->i_mode,
3de4586c 541 namelen, NULL);
cb8e7090 542 } else {
3de4586c
CM
543 struct inode *src_inode;
544 src_file = fget(vol_args->fd);
545 if (!src_file) {
546 ret = -EINVAL;
547 goto out;
548 }
549
550 src_inode = src_file->f_path.dentry->d_inode;
551 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
552 printk("btrfs: Snapshot src from another FS\n");
553 ret = -EINVAL;
554 fput(src_file);
555 goto out;
556 }
557 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
558 file->f_path.dentry->d_inode->i_mode,
559 namelen, BTRFS_I(src_inode)->root);
560 fput(src_file);
cb8e7090
CH
561 }
562
f46b5a66
CH
563out:
564 kfree(vol_args);
565 return ret;
566}
567
568static int btrfs_ioctl_defrag(struct file *file)
569{
570 struct inode *inode = fdentry(file)->d_inode;
571 struct btrfs_root *root = BTRFS_I(inode)->root;
c146afad
YZ
572 int ret;
573
574 ret = mnt_want_write(file->f_path.mnt);
575 if (ret)
576 return ret;
f46b5a66
CH
577
578 switch (inode->i_mode & S_IFMT) {
579 case S_IFDIR:
f46b5a66
CH
580 btrfs_defrag_root(root, 0);
581 btrfs_defrag_root(root->fs_info->extent_root, 0);
f46b5a66
CH
582 break;
583 case S_IFREG:
584 btrfs_defrag_file(file);
585 break;
586 }
587
588 return 0;
589}
590
591long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
592{
593 struct btrfs_ioctl_vol_args *vol_args;
594 int ret;
595
596 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
597
598 if (!vol_args)
599 return -ENOMEM;
600
601 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
602 ret = -EFAULT;
603 goto out;
604 }
5516e595 605 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
606 ret = btrfs_init_new_device(root, vol_args->name);
607
608out:
609 kfree(vol_args);
610 return ret;
611}
612
613long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
614{
615 struct btrfs_ioctl_vol_args *vol_args;
616 int ret;
617
c146afad
YZ
618 if (root->fs_info->sb->s_flags & MS_RDONLY)
619 return -EROFS;
620
f46b5a66
CH
621 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
622
623 if (!vol_args)
624 return -ENOMEM;
625
626 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
627 ret = -EFAULT;
628 goto out;
629 }
5516e595 630 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
631 ret = btrfs_rm_device(root, vol_args->name);
632
633out:
634 kfree(vol_args);
635 return ret;
636}
637
c5c9cd4d
SW
638long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, u64 off,
639 u64 olen, u64 destoff)
f46b5a66
CH
640{
641 struct inode *inode = fdentry(file)->d_inode;
642 struct btrfs_root *root = BTRFS_I(inode)->root;
643 struct file *src_file;
644 struct inode *src;
645 struct btrfs_trans_handle *trans;
f46b5a66 646 struct btrfs_path *path;
f46b5a66 647 struct extent_buffer *leaf;
ae01a0ab
YZ
648 char *buf;
649 struct btrfs_key key;
f46b5a66
CH
650 u32 nritems;
651 int slot;
ae01a0ab 652 int ret;
c5c9cd4d
SW
653 u64 len = olen;
654 u64 bs = root->fs_info->sb->s_blocksize;
655 u64 hint_byte;
f46b5a66 656
c5c9cd4d
SW
657 /*
658 * TODO:
659 * - split compressed inline extents. annoying: we need to
660 * decompress into destination's address_space (the file offset
661 * may change, so source mapping won't do), then recompress (or
662 * otherwise reinsert) a subrange.
663 * - allow ranges within the same file to be cloned (provided
664 * they don't overlap)?
665 */
666
c146afad
YZ
667 ret = mnt_want_write(file->f_path.mnt);
668 if (ret)
669 return ret;
670
c5c9cd4d 671 src_file = fget(srcfd);
f46b5a66
CH
672 if (!src_file)
673 return -EBADF;
674 src = src_file->f_dentry->d_inode;
675
c5c9cd4d
SW
676 ret = -EINVAL;
677 if (src == inode)
678 goto out_fput;
679
ae01a0ab
YZ
680 ret = -EISDIR;
681 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
682 goto out_fput;
683
f46b5a66 684 ret = -EXDEV;
ae01a0ab
YZ
685 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
686 goto out_fput;
687
688 ret = -ENOMEM;
689 buf = vmalloc(btrfs_level_size(root, 0));
690 if (!buf)
691 goto out_fput;
692
693 path = btrfs_alloc_path();
694 if (!path) {
695 vfree(buf);
f46b5a66 696 goto out_fput;
ae01a0ab
YZ
697 }
698 path->reada = 2;
f46b5a66
CH
699
700 if (inode < src) {
701 mutex_lock(&inode->i_mutex);
702 mutex_lock(&src->i_mutex);
703 } else {
704 mutex_lock(&src->i_mutex);
705 mutex_lock(&inode->i_mutex);
706 }
707
c5c9cd4d
SW
708 /* determine range to clone */
709 ret = -EINVAL;
710 if (off >= src->i_size || off + len > src->i_size)
f46b5a66 711 goto out_unlock;
c5c9cd4d
SW
712 if (len == 0)
713 olen = len = src->i_size - off;
714 /* if we extend to eof, continue to block boundary */
715 if (off + len == src->i_size)
716 len = ((src->i_size + bs-1) & ~(bs-1))
717 - off;
718
719 /* verify the end result is block aligned */
720 if ((off & (bs-1)) ||
721 ((off + len) & (bs-1)))
722 goto out_unlock;
723
724 printk("final src extent is %llu~%llu\n", off, len);
725 printk("final dst extent is %llu~%llu\n", destoff, len);
f46b5a66
CH
726
727 /* do any pending delalloc/csum calc on src, one way or
728 another, and lock file content */
729 while (1) {
31840ae1 730 struct btrfs_ordered_extent *ordered;
c5c9cd4d
SW
731 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
732 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
ae01a0ab 733 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
f46b5a66 734 break;
c5c9cd4d 735 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
ae01a0ab
YZ
736 if (ordered)
737 btrfs_put_ordered_extent(ordered);
c5c9cd4d 738 btrfs_wait_ordered_range(src, off, off+len);
f46b5a66
CH
739 }
740
ae01a0ab
YZ
741 trans = btrfs_start_transaction(root, 1);
742 BUG_ON(!trans);
743
c5c9cd4d
SW
744 /* punch hole in destination first */
745 btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
746
747 /* clone data */
f46b5a66 748 key.objectid = src->i_ino;
ae01a0ab
YZ
749 key.type = BTRFS_EXTENT_DATA_KEY;
750 key.offset = 0;
f46b5a66
CH
751
752 while (1) {
753 /*
754 * note the key will change type as we walk through the
755 * tree.
756 */
757 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
758 if (ret < 0)
759 goto out;
760
ae01a0ab
YZ
761 nritems = btrfs_header_nritems(path->nodes[0]);
762 if (path->slots[0] >= nritems) {
f46b5a66
CH
763 ret = btrfs_next_leaf(root, path);
764 if (ret < 0)
765 goto out;
766 if (ret > 0)
767 break;
ae01a0ab 768 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
769 }
770 leaf = path->nodes[0];
771 slot = path->slots[0];
f46b5a66 772
ae01a0ab 773 btrfs_item_key_to_cpu(leaf, &key, slot);
f46b5a66
CH
774 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
775 key.objectid != src->i_ino)
776 break;
777
c5c9cd4d
SW
778 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
779 struct btrfs_file_extent_item *extent;
780 int type;
31840ae1
ZY
781 u32 size;
782 struct btrfs_key new_key;
c5c9cd4d
SW
783 u64 disko = 0, diskl = 0;
784 u64 datao = 0, datal = 0;
785 u8 comp;
31840ae1
ZY
786
787 size = btrfs_item_size_nr(leaf, slot);
788 read_extent_buffer(leaf, buf,
789 btrfs_item_ptr_offset(leaf, slot),
790 size);
c5c9cd4d
SW
791
792 extent = btrfs_item_ptr(leaf, slot,
793 struct btrfs_file_extent_item);
794 comp = btrfs_file_extent_compression(leaf, extent);
795 type = btrfs_file_extent_type(leaf, extent);
796 if (type == BTRFS_FILE_EXTENT_REG) {
797 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
798 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
799 datao = btrfs_file_extent_offset(leaf, extent);
800 datal = btrfs_file_extent_num_bytes(leaf, extent);
801 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
802 /* take upper bound, may be compressed */
803 datal = btrfs_file_extent_ram_bytes(leaf,
804 extent);
805 }
31840ae1
ZY
806 btrfs_release_path(root, path);
807
c5c9cd4d
SW
808 if (key.offset + datal < off ||
809 key.offset >= off+len)
810 goto next;
811
31840ae1
ZY
812 memcpy(&new_key, &key, sizeof(new_key));
813 new_key.objectid = inode->i_ino;
c5c9cd4d 814 new_key.offset = key.offset + destoff - off;
31840ae1 815
c5c9cd4d
SW
816 if (type == BTRFS_FILE_EXTENT_REG) {
817 ret = btrfs_insert_empty_item(trans, root, path,
818 &new_key, size);
819 if (ret)
820 goto out;
821
822 leaf = path->nodes[0];
823 slot = path->slots[0];
824 write_extent_buffer(leaf, buf,
31840ae1
ZY
825 btrfs_item_ptr_offset(leaf, slot),
826 size);
ae01a0ab 827
c5c9cd4d 828 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 829 struct btrfs_file_extent_item);
c5c9cd4d
SW
830 printk(" orig disk %llu~%llu data %llu~%llu\n",
831 disko, diskl, datao, datal);
832
833 if (off > key.offset) {
834 datao += off - key.offset;
835 datal -= off - key.offset;
836 }
837 if (key.offset + datao + datal + key.offset >
838 off + len)
839 datal = off + len - key.offset - datao;
840 /* disko == 0 means it's a hole */
841 if (!disko)
842 datao = 0;
843 printk(" final disk %llu~%llu data %llu~%llu\n",
844 disko, diskl, datao, datal);
845
846 btrfs_set_file_extent_offset(leaf, extent,
847 datao);
848 btrfs_set_file_extent_num_bytes(leaf, extent,
849 datal);
850 if (disko) {
851 inode_add_bytes(inode, datal);
ae01a0ab 852 ret = btrfs_inc_extent_ref(trans, root,
c5c9cd4d
SW
853 disko, diskl, leaf->start,
854 root->root_key.objectid,
855 trans->transid,
856 inode->i_ino);
31840ae1 857 BUG_ON(ret);
f46b5a66 858 }
c5c9cd4d
SW
859 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
860 u64 skip = 0;
861 u64 trim = 0;
862 if (off > key.offset) {
863 skip = off - key.offset;
864 new_key.offset += skip;
865 }
866 if (key.offset + datal > off+len)
867 trim = key.offset + datal - (off+len);
868 printk("len %lld skip %lld trim %lld\n",
869 datal, skip, trim);
870 if (comp && (skip || trim)) {
871 printk("btrfs clone_range can't split compressed inline extents yet\n");
872 ret = -EINVAL;
873 goto out;
874 }
875 size -= skip + trim;
876 datal -= skip + trim;
877 ret = btrfs_insert_empty_item(trans, root, path,
878 &new_key, size);
879 if (ret)
880 goto out;
881
882 if (skip) {
883 u32 start = btrfs_file_extent_calc_inline_size(0);
884 memmove(buf+start, buf+start+skip,
885 datal);
886 }
887
888 leaf = path->nodes[0];
889 slot = path->slots[0];
890 write_extent_buffer(leaf, buf,
891 btrfs_item_ptr_offset(leaf, slot),
892 size);
893 inode_add_bytes(inode, datal);
f46b5a66 894 }
c5c9cd4d
SW
895
896 btrfs_mark_buffer_dirty(leaf);
ae01a0ab 897 }
c5c9cd4d
SW
898
899 if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
900 u32 size;
901 struct btrfs_key new_key;
902 u64 coverslen;
903 int coff, clen;
904
905 size = btrfs_item_size_nr(leaf, slot);
906 coverslen = (size / BTRFS_CRC32_SIZE) <<
907 root->fs_info->sb->s_blocksize_bits;
908 printk("csums for %llu~%llu\n",
909 key.offset, coverslen);
910 if (key.offset + coverslen < off ||
911 key.offset >= off+len)
912 goto next;
913
914 read_extent_buffer(leaf, buf,
915 btrfs_item_ptr_offset(leaf, slot),
916 size);
917 btrfs_release_path(root, path);
918
919 coff = 0;
920 if (off > key.offset)
921 coff = ((off - key.offset) >>
922 root->fs_info->sb->s_blocksize_bits) *
923 BTRFS_CRC32_SIZE;
924 clen = size - coff;
925 if (key.offset + coverslen > off+len)
926 clen -= ((key.offset+coverslen-off-len) >>
927 root->fs_info->sb->s_blocksize_bits) *
928 BTRFS_CRC32_SIZE;
929 printk(" will dup %d~%d of %d\n",
930 coff, clen, size);
931
932 memcpy(&new_key, &key, sizeof(new_key));
933 new_key.objectid = inode->i_ino;
934 new_key.offset = key.offset + destoff - off;
935
936 ret = btrfs_insert_empty_item(trans, root, path,
937 &new_key, clen);
938 if (ret)
939 goto out;
940
941 leaf = path->nodes[0];
942 slot = path->slots[0];
943 write_extent_buffer(leaf, buf + coff,
944 btrfs_item_ptr_offset(leaf, slot),
945 clen);
946 btrfs_mark_buffer_dirty(leaf);
947 }
948
949 next:
31840ae1 950 btrfs_release_path(root, path);
f46b5a66 951 key.offset++;
f46b5a66 952 }
f46b5a66
CH
953 ret = 0;
954out:
ae01a0ab
YZ
955 btrfs_release_path(root, path);
956 if (ret == 0) {
957 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
c5c9cd4d
SW
958 if (destoff + olen > inode->i_size)
959 btrfs_i_size_write(inode, destoff + olen);
ae01a0ab
YZ
960 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
961 ret = btrfs_update_inode(trans, root, inode);
962 }
f46b5a66 963 btrfs_end_transaction(trans, root);
c5c9cd4d 964 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
ae01a0ab
YZ
965 if (ret)
966 vmtruncate(inode, 0);
f46b5a66
CH
967out_unlock:
968 mutex_unlock(&src->i_mutex);
969 mutex_unlock(&inode->i_mutex);
ae01a0ab
YZ
970 vfree(buf);
971 btrfs_free_path(path);
f46b5a66
CH
972out_fput:
973 fput(src_file);
974 return ret;
975}
976
c5c9cd4d
SW
977long btrfs_ioctl_clone_range(struct file *file, unsigned long argptr)
978{
979 struct btrfs_ioctl_clone_range_args args;
980
981 if (copy_from_user(&args, (void *)argptr, sizeof(args)))
982 return -EFAULT;
983 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
984 args.src_length, args.dest_offset);
985}
986
f46b5a66
CH
987/*
988 * there are many ways the trans_start and trans_end ioctls can lead
989 * to deadlocks. They should only be used by applications that
990 * basically own the machine, and have a very in depth understanding
991 * of all the possible deadlocks and enospc problems.
992 */
993long btrfs_ioctl_trans_start(struct file *file)
994{
995 struct inode *inode = fdentry(file)->d_inode;
996 struct btrfs_root *root = BTRFS_I(inode)->root;
997 struct btrfs_trans_handle *trans;
998 int ret = 0;
999
df5b5520
CH
1000 if (!capable(CAP_SYS_ADMIN))
1001 return -EPERM;
1002
f46b5a66
CH
1003 if (file->private_data) {
1004 ret = -EINPROGRESS;
1005 goto out;
1006 }
9ca9ee09 1007
c146afad
YZ
1008 ret = mnt_want_write(file->f_path.mnt);
1009 if (ret)
1010 goto out;
1011
9ca9ee09
SW
1012 mutex_lock(&root->fs_info->trans_mutex);
1013 root->fs_info->open_ioctl_trans++;
1014 mutex_unlock(&root->fs_info->trans_mutex);
1015
1016 trans = btrfs_start_ioctl_transaction(root, 0);
f46b5a66
CH
1017 if (trans)
1018 file->private_data = trans;
1019 else
1020 ret = -ENOMEM;
1021 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1022out:
f46b5a66
CH
1023 return ret;
1024}
1025
1026/*
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.
1031 */
1032long btrfs_ioctl_trans_end(struct file *file)
1033{
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;
1038
f46b5a66
CH
1039 trans = file->private_data;
1040 if (!trans) {
1041 ret = -EINVAL;
1042 goto out;
1043 }
1044 btrfs_end_transaction(trans, root);
b214107e 1045 file->private_data = NULL;
9ca9ee09
SW
1046
1047 mutex_lock(&root->fs_info->trans_mutex);
1048 root->fs_info->open_ioctl_trans--;
1049 mutex_unlock(&root->fs_info->trans_mutex);
1050
f46b5a66 1051out:
f46b5a66
CH
1052 return ret;
1053}
1054
1055long btrfs_ioctl(struct file *file, unsigned int
1056 cmd, unsigned long arg)
1057{
1058 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1059
1060 switch (cmd) {
1061 case BTRFS_IOC_SNAP_CREATE:
3de4586c
CM
1062 return btrfs_ioctl_snap_create(file, (void __user *)arg, 0);
1063 case BTRFS_IOC_SUBVOL_CREATE:
1064 return btrfs_ioctl_snap_create(file, (void __user *)arg, 1);
f46b5a66
CH
1065 case BTRFS_IOC_DEFRAG:
1066 return btrfs_ioctl_defrag(file);
1067 case BTRFS_IOC_RESIZE:
1068 return btrfs_ioctl_resize(root, (void __user *)arg);
1069 case BTRFS_IOC_ADD_DEV:
1070 return btrfs_ioctl_add_dev(root, (void __user *)arg);
1071 case BTRFS_IOC_RM_DEV:
1072 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
1073 case BTRFS_IOC_BALANCE:
1074 return btrfs_balance(root->fs_info->dev_root);
1075 case BTRFS_IOC_CLONE:
c5c9cd4d
SW
1076 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1077 case BTRFS_IOC_CLONE_RANGE:
1078 return btrfs_ioctl_clone_range(file, arg);
f46b5a66
CH
1079 case BTRFS_IOC_TRANS_START:
1080 return btrfs_ioctl_trans_start(file);
1081 case BTRFS_IOC_TRANS_END:
1082 return btrfs_ioctl_trans_end(file);
1083 case BTRFS_IOC_SYNC:
ea8c2819 1084 btrfs_start_delalloc_inodes(root);
f46b5a66
CH
1085 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1086 return 0;
1087 }
1088
1089 return -ENOTTY;
1090}