remove unused function btrfs_ilookup
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
36#include <linux/bit_spinlock.h>
37#include <linux/version.h>
38#include <linux/xattr.h>
7ea394f1 39#include <linux/vmalloc.h>
f46b5a66
CH
40#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
46#include "volumes.h"
925baedd 47#include "locking.h"
f46b5a66
CH
48
49
50
51static noinline int create_subvol(struct btrfs_root *root, char *name,
52 int namelen)
53{
54 struct btrfs_trans_handle *trans;
55 struct btrfs_key key;
56 struct btrfs_root_item root_item;
57 struct btrfs_inode_item *inode_item;
58 struct extent_buffer *leaf;
59 struct btrfs_root *new_root = root;
60 struct inode *dir;
61 int ret;
62 int err;
63 u64 objectid;
64 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
65 unsigned long nr = 1;
66
f46b5a66
CH
67 ret = btrfs_check_free_space(root, 1, 0);
68 if (ret)
69 goto fail_commit;
70
71 trans = btrfs_start_transaction(root, 1);
72 BUG_ON(!trans);
73
74 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
75 0, &objectid);
76 if (ret)
77 goto fail;
78
925baedd
CM
79 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
80 objectid, trans->transid, 0, 0,
81 0, 0);
8e8a1e31
JB
82 if (IS_ERR(leaf)) {
83 ret = PTR_ERR(leaf);
84 goto fail;
85 }
f46b5a66
CH
86
87 btrfs_set_header_nritems(leaf, 0);
88 btrfs_set_header_level(leaf, 0);
89 btrfs_set_header_bytenr(leaf, leaf->start);
90 btrfs_set_header_generation(leaf, trans->transid);
91 btrfs_set_header_owner(leaf, objectid);
92
93 write_extent_buffer(leaf, root->fs_info->fsid,
94 (unsigned long)btrfs_header_fsid(leaf),
95 BTRFS_FSID_SIZE);
96 btrfs_mark_buffer_dirty(leaf);
97
98 inode_item = &root_item.inode;
99 memset(inode_item, 0, sizeof(*inode_item));
100 inode_item->generation = cpu_to_le64(1);
101 inode_item->size = cpu_to_le64(3);
102 inode_item->nlink = cpu_to_le32(1);
103 inode_item->nblocks = cpu_to_le64(1);
104 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
105
106 btrfs_set_root_bytenr(&root_item, leaf->start);
107 btrfs_set_root_level(&root_item, 0);
108 btrfs_set_root_refs(&root_item, 1);
109 btrfs_set_root_used(&root_item, 0);
110
111 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
112 root_item.drop_level = 0;
113
925baedd 114 btrfs_tree_unlock(leaf);
f46b5a66
CH
115 free_extent_buffer(leaf);
116 leaf = NULL;
117
118 btrfs_set_root_dirid(&root_item, new_dirid);
119
120 key.objectid = objectid;
121 key.offset = 1;
122 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
123 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
124 &root_item);
125 if (ret)
126 goto fail;
127
128 /*
129 * insert the directory item
130 */
131 key.offset = (u64)-1;
132 dir = root->fs_info->sb->s_root->d_inode;
133 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
134 name, namelen, dir->i_ino, &key,
aec7477b 135 BTRFS_FT_DIR, 0);
f46b5a66
CH
136 if (ret)
137 goto fail;
138
139 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
140 name, namelen, objectid,
aec7477b 141 root->fs_info->sb->s_root->d_inode->i_ino, 0);
f46b5a66
CH
142 if (ret)
143 goto fail;
144
145 ret = btrfs_commit_transaction(trans, root);
146 if (ret)
147 goto fail_commit;
148
149 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
150 BUG_ON(!new_root);
151
152 trans = btrfs_start_transaction(new_root, 1);
153 BUG_ON(!trans);
154
155 ret = btrfs_create_subvol_root(new_root, trans, new_dirid,
156 BTRFS_I(dir)->block_group);
157 if (ret)
158 goto fail;
159
160 /* Invalidate existing dcache entry for new subvolume. */
161 btrfs_invalidate_dcache_root(root, name, namelen);
162
163fail:
164 nr = trans->blocks_used;
165 err = btrfs_commit_transaction(trans, new_root);
166 if (err && !ret)
167 ret = err;
168fail_commit:
f46b5a66 169 btrfs_btree_balance_dirty(root, nr);
f46b5a66
CH
170 return ret;
171}
172
173static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
174{
175 struct btrfs_pending_snapshot *pending_snapshot;
176 struct btrfs_trans_handle *trans;
177 int ret;
178 int err;
179 unsigned long nr = 0;
180
181 if (!root->ref_cows)
182 return -EINVAL;
183
f46b5a66
CH
184 ret = btrfs_check_free_space(root, 1, 0);
185 if (ret)
186 goto fail_unlock;
187
188 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
189 if (!pending_snapshot) {
190 ret = -ENOMEM;
191 goto fail_unlock;
192 }
193 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
194 if (!pending_snapshot->name) {
195 ret = -ENOMEM;
196 kfree(pending_snapshot);
197 goto fail_unlock;
198 }
199 memcpy(pending_snapshot->name, name, namelen);
200 pending_snapshot->name[namelen] = '\0';
201 trans = btrfs_start_transaction(root, 1);
202 BUG_ON(!trans);
203 pending_snapshot->root = root;
204 list_add(&pending_snapshot->list,
205 &trans->transaction->pending_snapshots);
206 ret = btrfs_update_inode(trans, root, root->inode);
207 err = btrfs_commit_transaction(trans, root);
208
209fail_unlock:
f46b5a66 210 btrfs_btree_balance_dirty(root, nr);
f46b5a66
CH
211 return ret;
212}
213
214int btrfs_defrag_file(struct file *file)
215{
216 struct inode *inode = fdentry(file)->d_inode;
217 struct btrfs_root *root = BTRFS_I(inode)->root;
218 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3eaa2885 219 struct btrfs_ordered_extent *ordered;
f46b5a66
CH
220 struct page *page;
221 unsigned long last_index;
222 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
223 unsigned long total_read = 0;
224 u64 page_start;
225 u64 page_end;
226 unsigned long i;
227 int ret;
228
f46b5a66 229 ret = btrfs_check_free_space(root, inode->i_size, 0);
f46b5a66
CH
230 if (ret)
231 return -ENOSPC;
232
233 mutex_lock(&inode->i_mutex);
234 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
235 for (i = 0; i <= last_index; i++) {
236 if (total_read % ra_pages == 0) {
237 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
238 min(last_index, i + ra_pages - 1));
239 }
240 total_read++;
3eaa2885 241again:
f46b5a66
CH
242 page = grab_cache_page(inode->i_mapping, i);
243 if (!page)
244 goto out_unlock;
245 if (!PageUptodate(page)) {
246 btrfs_readpage(NULL, page);
247 lock_page(page);
248 if (!PageUptodate(page)) {
249 unlock_page(page);
250 page_cache_release(page);
251 goto out_unlock;
252 }
253 }
254
f46b5a66 255 wait_on_page_writeback(page);
f46b5a66
CH
256
257 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
258 page_end = page_start + PAGE_CACHE_SIZE - 1;
f46b5a66 259 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3eaa2885
CM
260
261 ordered = btrfs_lookup_ordered_extent(inode, page_start);
262 if (ordered) {
263 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
264 unlock_page(page);
265 page_cache_release(page);
266 btrfs_start_ordered_extent(inode, ordered, 1);
267 btrfs_put_ordered_extent(ordered);
268 goto again;
269 }
270 set_page_extent_mapped(page);
271
f87f057b
CM
272 /*
273 * this makes sure page_mkwrite is called on the
274 * page if it is dirtied again later
275 */
276 clear_page_dirty_for_io(page);
277
ea8c2819 278 btrfs_set_extent_delalloc(inode, page_start, page_end);
f46b5a66
CH
279
280 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
281 set_page_dirty(page);
282 unlock_page(page);
283 page_cache_release(page);
284 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
285 }
286
287out_unlock:
288 mutex_unlock(&inode->i_mutex);
289 return 0;
290}
291
292/*
293 * Called inside transaction, so use GFP_NOFS
294 */
295
296static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
297{
298 u64 new_size;
299 u64 old_size;
300 u64 devid = 1;
301 struct btrfs_ioctl_vol_args *vol_args;
302 struct btrfs_trans_handle *trans;
303 struct btrfs_device *device = NULL;
304 char *sizestr;
305 char *devstr = NULL;
306 int ret = 0;
307 int namelen;
308 int mod = 0;
309
310 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
311
312 if (!vol_args)
313 return -ENOMEM;
314
315 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
316 ret = -EFAULT;
317 goto out;
318 }
5516e595
MF
319
320 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 321 namelen = strlen(vol_args->name);
f46b5a66 322
7d9eb12c 323 mutex_lock(&root->fs_info->volume_mutex);
f46b5a66
CH
324 sizestr = vol_args->name;
325 devstr = strchr(sizestr, ':');
326 if (devstr) {
327 char *end;
328 sizestr = devstr + 1;
329 *devstr = '\0';
330 devstr = vol_args->name;
331 devid = simple_strtoull(devstr, &end, 10);
332 printk(KERN_INFO "resizing devid %llu\n", devid);
333 }
334 device = btrfs_find_device(root, devid, NULL);
335 if (!device) {
336 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
337 ret = -EINVAL;
338 goto out_unlock;
339 }
340 if (!strcmp(sizestr, "max"))
341 new_size = device->bdev->bd_inode->i_size;
342 else {
343 if (sizestr[0] == '-') {
344 mod = -1;
345 sizestr++;
346 } else if (sizestr[0] == '+') {
347 mod = 1;
348 sizestr++;
349 }
350 new_size = btrfs_parse_size(sizestr);
351 if (new_size == 0) {
352 ret = -EINVAL;
353 goto out_unlock;
354 }
355 }
356
357 old_size = device->total_bytes;
358
359 if (mod < 0) {
360 if (new_size > old_size) {
361 ret = -EINVAL;
362 goto out_unlock;
363 }
364 new_size = old_size - new_size;
365 } else if (mod > 0) {
366 new_size = old_size + new_size;
367 }
368
369 if (new_size < 256 * 1024 * 1024) {
370 ret = -EINVAL;
371 goto out_unlock;
372 }
373 if (new_size > device->bdev->bd_inode->i_size) {
374 ret = -EFBIG;
375 goto out_unlock;
376 }
377
378 do_div(new_size, root->sectorsize);
379 new_size *= root->sectorsize;
380
381 printk(KERN_INFO "new size for %s is %llu\n",
382 device->name, (unsigned long long)new_size);
383
384 if (new_size > old_size) {
385 trans = btrfs_start_transaction(root, 1);
386 ret = btrfs_grow_device(trans, device, new_size);
387 btrfs_commit_transaction(trans, root);
388 } else {
389 ret = btrfs_shrink_device(device, new_size);
390 }
391
392out_unlock:
7d9eb12c 393 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
394out:
395 kfree(vol_args);
396 return ret;
397}
398
399static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
400 void __user *arg)
401{
402 struct btrfs_ioctl_vol_args *vol_args;
403 struct btrfs_dir_item *di;
404 struct btrfs_path *path;
405 u64 root_dirid;
406 int namelen;
407 int ret;
408
409 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
410
411 if (!vol_args)
412 return -ENOMEM;
413
414 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
415 ret = -EFAULT;
416 goto out;
417 }
418
5516e595 419 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 420 namelen = strlen(vol_args->name);
f46b5a66
CH
421 if (strchr(vol_args->name, '/')) {
422 ret = -EINVAL;
423 goto out;
424 }
425
426 path = btrfs_alloc_path();
427 if (!path) {
428 ret = -ENOMEM;
429 goto out;
430 }
431
432 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
f46b5a66
CH
433 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
434 path, root_dirid,
435 vol_args->name, namelen, 0);
f46b5a66
CH
436 btrfs_free_path(path);
437
438 if (di && !IS_ERR(di)) {
439 ret = -EEXIST;
440 goto out;
441 }
442
443 if (IS_ERR(di)) {
444 ret = PTR_ERR(di);
445 goto out;
446 }
447
a2135011 448 mutex_lock(&root->fs_info->drop_mutex);
f46b5a66
CH
449 if (root == root->fs_info->tree_root)
450 ret = create_subvol(root, vol_args->name, namelen);
451 else
452 ret = create_snapshot(root, vol_args->name, namelen);
a2135011 453 mutex_unlock(&root->fs_info->drop_mutex);
f46b5a66
CH
454out:
455 kfree(vol_args);
456 return ret;
457}
458
459static int btrfs_ioctl_defrag(struct file *file)
460{
461 struct inode *inode = fdentry(file)->d_inode;
462 struct btrfs_root *root = BTRFS_I(inode)->root;
463
464 switch (inode->i_mode & S_IFMT) {
465 case S_IFDIR:
f46b5a66
CH
466 btrfs_defrag_root(root, 0);
467 btrfs_defrag_root(root->fs_info->extent_root, 0);
f46b5a66
CH
468 break;
469 case S_IFREG:
470 btrfs_defrag_file(file);
471 break;
472 }
473
474 return 0;
475}
476
477long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
478{
479 struct btrfs_ioctl_vol_args *vol_args;
480 int ret;
481
482 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
483
484 if (!vol_args)
485 return -ENOMEM;
486
487 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
488 ret = -EFAULT;
489 goto out;
490 }
5516e595 491 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
492 ret = btrfs_init_new_device(root, vol_args->name);
493
494out:
495 kfree(vol_args);
496 return ret;
497}
498
499long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
500{
501 struct btrfs_ioctl_vol_args *vol_args;
502 int ret;
503
504 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
505
506 if (!vol_args)
507 return -ENOMEM;
508
509 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
510 ret = -EFAULT;
511 goto out;
512 }
5516e595 513 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
514 ret = btrfs_rm_device(root, vol_args->name);
515
516out:
517 kfree(vol_args);
518 return ret;
519}
520
f46b5a66
CH
521long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
522{
523 struct inode *inode = fdentry(file)->d_inode;
524 struct btrfs_root *root = BTRFS_I(inode)->root;
525 struct file *src_file;
526 struct inode *src;
527 struct btrfs_trans_handle *trans;
ae01a0ab 528 struct btrfs_ordered_extent *ordered;
f46b5a66 529 struct btrfs_path *path;
f46b5a66 530 struct extent_buffer *leaf;
ae01a0ab
YZ
531 char *buf;
532 struct btrfs_key key;
533 struct btrfs_key new_key;
534 u32 size;
f46b5a66
CH
535 u32 nritems;
536 int slot;
ae01a0ab 537 int ret;
f46b5a66
CH
538
539 src_file = fget(src_fd);
540 if (!src_file)
541 return -EBADF;
542 src = src_file->f_dentry->d_inode;
543
ae01a0ab
YZ
544 ret = -EISDIR;
545 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
546 goto out_fput;
547
f46b5a66 548 ret = -EXDEV;
ae01a0ab
YZ
549 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
550 goto out_fput;
551
552 ret = -ENOMEM;
553 buf = vmalloc(btrfs_level_size(root, 0));
554 if (!buf)
555 goto out_fput;
556
557 path = btrfs_alloc_path();
558 if (!path) {
559 vfree(buf);
f46b5a66 560 goto out_fput;
ae01a0ab
YZ
561 }
562 path->reada = 2;
f46b5a66
CH
563
564 if (inode < src) {
565 mutex_lock(&inode->i_mutex);
566 mutex_lock(&src->i_mutex);
567 } else {
568 mutex_lock(&src->i_mutex);
569 mutex_lock(&inode->i_mutex);
570 }
571
572 ret = -ENOTEMPTY;
573 if (inode->i_size)
574 goto out_unlock;
575
576 /* do any pending delalloc/csum calc on src, one way or
577 another, and lock file content */
578 while (1) {
f46b5a66 579 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
ae01a0ab
YZ
580 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
581 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
f46b5a66
CH
582 break;
583 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
ae01a0ab
YZ
584 if (ordered)
585 btrfs_put_ordered_extent(ordered);
586 btrfs_wait_ordered_range(src, 0, (u64)-1);
f46b5a66
CH
587 }
588
ae01a0ab
YZ
589 trans = btrfs_start_transaction(root, 1);
590 BUG_ON(!trans);
591
f46b5a66 592 key.objectid = src->i_ino;
ae01a0ab
YZ
593 key.type = BTRFS_EXTENT_DATA_KEY;
594 key.offset = 0;
f46b5a66
CH
595
596 while (1) {
597 /*
598 * note the key will change type as we walk through the
599 * tree.
600 */
601 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
602 if (ret < 0)
603 goto out;
604
ae01a0ab
YZ
605 nritems = btrfs_header_nritems(path->nodes[0]);
606 if (path->slots[0] >= nritems) {
f46b5a66
CH
607 ret = btrfs_next_leaf(root, path);
608 if (ret < 0)
609 goto out;
610 if (ret > 0)
611 break;
ae01a0ab 612 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
613 }
614 leaf = path->nodes[0];
615 slot = path->slots[0];
f46b5a66 616
ae01a0ab 617 btrfs_item_key_to_cpu(leaf, &key, slot);
f46b5a66
CH
618 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
619 key.objectid != src->i_ino)
620 break;
621
622 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
623 struct btrfs_file_extent_item *extent;
624 int found_type;
ae01a0ab 625
f46b5a66
CH
626 extent = btrfs_item_ptr(leaf, slot,
627 struct btrfs_file_extent_item);
628 found_type = btrfs_file_extent_type(leaf, extent);
629 if (found_type == BTRFS_FILE_EXTENT_REG) {
f46b5a66
CH
630 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
631 extent);
632 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
633 extent);
f46b5a66
CH
634 /* ds == 0 means there's a hole */
635 if (ds != 0) {
ae01a0ab 636 ret = btrfs_inc_extent_ref(trans, root,
f46b5a66
CH
637 ds, dl,
638 root->root_key.objectid,
639 trans->transid,
ae01a0ab
YZ
640 inode->i_ino, key.offset);
641 if (ret)
642 goto out;
f46b5a66 643 }
f46b5a66 644 }
ae01a0ab 645 }
f46b5a66 646
ae01a0ab
YZ
647 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY ||
648 btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
649 size = btrfs_item_size_nr(leaf, slot);
650 read_extent_buffer(leaf, buf,
651 btrfs_item_ptr_offset(leaf, slot),
652 size);
653 btrfs_release_path(root, path);
654 memcpy(&new_key, &key, sizeof(new_key));
655 new_key.objectid = inode->i_ino;
656 ret = btrfs_insert_item(trans, root, &new_key,
657 buf, size);
658 BUG_ON(ret);
659 } else {
660 btrfs_release_path(root, path);
f46b5a66
CH
661 }
662 key.offset++;
f46b5a66 663 }
f46b5a66
CH
664 ret = 0;
665out:
ae01a0ab
YZ
666 btrfs_release_path(root, path);
667 if (ret == 0) {
668 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
669 inode->i_blocks = src->i_blocks;
670 btrfs_i_size_write(inode, src->i_size);
671 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
672 ret = btrfs_update_inode(trans, root, inode);
673 }
f46b5a66 674 btrfs_end_transaction(trans, root);
ae01a0ab
YZ
675 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
676 if (ret)
677 vmtruncate(inode, 0);
f46b5a66
CH
678out_unlock:
679 mutex_unlock(&src->i_mutex);
680 mutex_unlock(&inode->i_mutex);
ae01a0ab
YZ
681 vfree(buf);
682 btrfs_free_path(path);
f46b5a66
CH
683out_fput:
684 fput(src_file);
685 return ret;
686}
687
688/*
689 * there are many ways the trans_start and trans_end ioctls can lead
690 * to deadlocks. They should only be used by applications that
691 * basically own the machine, and have a very in depth understanding
692 * of all the possible deadlocks and enospc problems.
693 */
694long btrfs_ioctl_trans_start(struct file *file)
695{
696 struct inode *inode = fdentry(file)->d_inode;
697 struct btrfs_root *root = BTRFS_I(inode)->root;
698 struct btrfs_trans_handle *trans;
699 int ret = 0;
700
df5b5520
CH
701 if (!capable(CAP_SYS_ADMIN))
702 return -EPERM;
703
f46b5a66
CH
704 if (file->private_data) {
705 ret = -EINPROGRESS;
706 goto out;
707 }
9ca9ee09
SW
708
709 mutex_lock(&root->fs_info->trans_mutex);
710 root->fs_info->open_ioctl_trans++;
711 mutex_unlock(&root->fs_info->trans_mutex);
712
713 trans = btrfs_start_ioctl_transaction(root, 0);
f46b5a66
CH
714 if (trans)
715 file->private_data = trans;
716 else
717 ret = -ENOMEM;
718 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
719out:
f46b5a66
CH
720 return ret;
721}
722
723/*
724 * there are many ways the trans_start and trans_end ioctls can lead
725 * to deadlocks. They should only be used by applications that
726 * basically own the machine, and have a very in depth understanding
727 * of all the possible deadlocks and enospc problems.
728 */
729long btrfs_ioctl_trans_end(struct file *file)
730{
731 struct inode *inode = fdentry(file)->d_inode;
732 struct btrfs_root *root = BTRFS_I(inode)->root;
733 struct btrfs_trans_handle *trans;
734 int ret = 0;
735
f46b5a66
CH
736 trans = file->private_data;
737 if (!trans) {
738 ret = -EINVAL;
739 goto out;
740 }
741 btrfs_end_transaction(trans, root);
742 file->private_data = 0;
9ca9ee09
SW
743
744 mutex_lock(&root->fs_info->trans_mutex);
745 root->fs_info->open_ioctl_trans--;
746 mutex_unlock(&root->fs_info->trans_mutex);
747
f46b5a66 748out:
f46b5a66
CH
749 return ret;
750}
751
752long btrfs_ioctl(struct file *file, unsigned int
753 cmd, unsigned long arg)
754{
755 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
756
757 switch (cmd) {
758 case BTRFS_IOC_SNAP_CREATE:
759 return btrfs_ioctl_snap_create(root, (void __user *)arg);
760 case BTRFS_IOC_DEFRAG:
761 return btrfs_ioctl_defrag(file);
762 case BTRFS_IOC_RESIZE:
763 return btrfs_ioctl_resize(root, (void __user *)arg);
764 case BTRFS_IOC_ADD_DEV:
765 return btrfs_ioctl_add_dev(root, (void __user *)arg);
766 case BTRFS_IOC_RM_DEV:
767 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
768 case BTRFS_IOC_BALANCE:
769 return btrfs_balance(root->fs_info->dev_root);
770 case BTRFS_IOC_CLONE:
771 return btrfs_ioctl_clone(file, arg);
772 case BTRFS_IOC_TRANS_START:
773 return btrfs_ioctl_trans_start(file);
774 case BTRFS_IOC_TRANS_END:
775 return btrfs_ioctl_trans_end(file);
776 case BTRFS_IOC_SYNC:
ea8c2819 777 btrfs_start_delalloc_inodes(root);
f46b5a66
CH
778 btrfs_sync_fs(file->f_dentry->d_sb, 1);
779 return 0;
780 }
781
782 return -ENOTTY;
783}