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