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