]>
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); | |
8e8a1e31 JB |
81 | if (IS_ERR(leaf)) { |
82 | ret = PTR_ERR(leaf); | |
83 | goto fail; | |
84 | } | |
f46b5a66 CH |
85 | |
86 | btrfs_set_header_nritems(leaf, 0); | |
87 | btrfs_set_header_level(leaf, 0); | |
88 | btrfs_set_header_bytenr(leaf, leaf->start); | |
89 | btrfs_set_header_generation(leaf, trans->transid); | |
90 | btrfs_set_header_owner(leaf, objectid); | |
91 | ||
92 | write_extent_buffer(leaf, root->fs_info->fsid, | |
93 | (unsigned long)btrfs_header_fsid(leaf), | |
94 | BTRFS_FSID_SIZE); | |
95 | btrfs_mark_buffer_dirty(leaf); | |
96 | ||
97 | inode_item = &root_item.inode; | |
98 | memset(inode_item, 0, sizeof(*inode_item)); | |
99 | inode_item->generation = cpu_to_le64(1); | |
100 | inode_item->size = cpu_to_le64(3); | |
101 | inode_item->nlink = cpu_to_le32(1); | |
102 | inode_item->nblocks = cpu_to_le64(1); | |
103 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); | |
104 | ||
105 | btrfs_set_root_bytenr(&root_item, leaf->start); | |
106 | btrfs_set_root_level(&root_item, 0); | |
107 | btrfs_set_root_refs(&root_item, 1); | |
108 | btrfs_set_root_used(&root_item, 0); | |
109 | ||
110 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); | |
111 | root_item.drop_level = 0; | |
112 | ||
925baedd | 113 | btrfs_tree_unlock(leaf); |
f46b5a66 CH |
114 | free_extent_buffer(leaf); |
115 | leaf = NULL; | |
116 | ||
117 | btrfs_set_root_dirid(&root_item, new_dirid); | |
118 | ||
119 | key.objectid = objectid; | |
120 | key.offset = 1; | |
121 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
122 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
123 | &root_item); | |
124 | if (ret) | |
125 | goto fail; | |
126 | ||
127 | /* | |
128 | * insert the directory item | |
129 | */ | |
130 | key.offset = (u64)-1; | |
131 | dir = root->fs_info->sb->s_root->d_inode; | |
132 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, | |
133 | name, namelen, dir->i_ino, &key, | |
aec7477b | 134 | BTRFS_FT_DIR, 0); |
f46b5a66 CH |
135 | if (ret) |
136 | goto fail; | |
137 | ||
138 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, | |
139 | name, namelen, objectid, | |
aec7477b | 140 | root->fs_info->sb->s_root->d_inode->i_ino, 0); |
f46b5a66 CH |
141 | if (ret) |
142 | goto fail; | |
143 | ||
144 | ret = btrfs_commit_transaction(trans, root); | |
145 | if (ret) | |
146 | goto fail_commit; | |
147 | ||
148 | new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen); | |
149 | BUG_ON(!new_root); | |
150 | ||
151 | trans = btrfs_start_transaction(new_root, 1); | |
152 | BUG_ON(!trans); | |
153 | ||
154 | ret = btrfs_create_subvol_root(new_root, trans, new_dirid, | |
155 | BTRFS_I(dir)->block_group); | |
156 | if (ret) | |
157 | goto fail; | |
158 | ||
159 | /* Invalidate existing dcache entry for new subvolume. */ | |
160 | btrfs_invalidate_dcache_root(root, name, namelen); | |
161 | ||
162 | fail: | |
163 | nr = trans->blocks_used; | |
164 | err = btrfs_commit_transaction(trans, new_root); | |
165 | if (err && !ret) | |
166 | ret = err; | |
167 | fail_commit: | |
f46b5a66 | 168 | btrfs_btree_balance_dirty(root, nr); |
f46b5a66 CH |
169 | return ret; |
170 | } | |
171 | ||
172 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) | |
173 | { | |
174 | struct btrfs_pending_snapshot *pending_snapshot; | |
175 | struct btrfs_trans_handle *trans; | |
176 | int ret; | |
177 | int err; | |
178 | unsigned long nr = 0; | |
179 | ||
180 | if (!root->ref_cows) | |
181 | return -EINVAL; | |
182 | ||
f46b5a66 CH |
183 | ret = btrfs_check_free_space(root, 1, 0); |
184 | if (ret) | |
185 | goto fail_unlock; | |
186 | ||
187 | pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS); | |
188 | if (!pending_snapshot) { | |
189 | ret = -ENOMEM; | |
190 | goto fail_unlock; | |
191 | } | |
192 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); | |
193 | if (!pending_snapshot->name) { | |
194 | ret = -ENOMEM; | |
195 | kfree(pending_snapshot); | |
196 | goto fail_unlock; | |
197 | } | |
198 | memcpy(pending_snapshot->name, name, namelen); | |
199 | pending_snapshot->name[namelen] = '\0'; | |
200 | trans = btrfs_start_transaction(root, 1); | |
201 | BUG_ON(!trans); | |
202 | pending_snapshot->root = root; | |
203 | list_add(&pending_snapshot->list, | |
204 | &trans->transaction->pending_snapshots); | |
205 | ret = btrfs_update_inode(trans, root, root->inode); | |
206 | err = btrfs_commit_transaction(trans, root); | |
207 | ||
208 | fail_unlock: | |
f46b5a66 | 209 | btrfs_btree_balance_dirty(root, nr); |
f46b5a66 CH |
210 | return ret; |
211 | } | |
212 | ||
213 | int btrfs_defrag_file(struct file *file) | |
214 | { | |
215 | struct inode *inode = fdentry(file)->d_inode; | |
216 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
217 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
3eaa2885 | 218 | struct btrfs_ordered_extent *ordered; |
f46b5a66 CH |
219 | struct page *page; |
220 | unsigned long last_index; | |
221 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; | |
222 | unsigned long total_read = 0; | |
223 | u64 page_start; | |
224 | u64 page_end; | |
225 | unsigned long i; | |
226 | int ret; | |
227 | ||
f46b5a66 | 228 | ret = btrfs_check_free_space(root, inode->i_size, 0); |
f46b5a66 CH |
229 | if (ret) |
230 | return -ENOSPC; | |
231 | ||
232 | mutex_lock(&inode->i_mutex); | |
233 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; | |
234 | for (i = 0; i <= last_index; i++) { | |
235 | if (total_read % ra_pages == 0) { | |
236 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, | |
237 | min(last_index, i + ra_pages - 1)); | |
238 | } | |
239 | total_read++; | |
3eaa2885 | 240 | again: |
f46b5a66 CH |
241 | page = grab_cache_page(inode->i_mapping, i); |
242 | if (!page) | |
243 | goto out_unlock; | |
244 | if (!PageUptodate(page)) { | |
245 | btrfs_readpage(NULL, page); | |
246 | lock_page(page); | |
247 | if (!PageUptodate(page)) { | |
248 | unlock_page(page); | |
249 | page_cache_release(page); | |
250 | goto out_unlock; | |
251 | } | |
252 | } | |
253 | ||
f46b5a66 | 254 | wait_on_page_writeback(page); |
f46b5a66 CH |
255 | |
256 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; | |
257 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
f46b5a66 | 258 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
3eaa2885 CM |
259 | |
260 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
261 | if (ordered) { | |
262 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
263 | unlock_page(page); | |
264 | page_cache_release(page); | |
265 | btrfs_start_ordered_extent(inode, ordered, 1); | |
266 | btrfs_put_ordered_extent(ordered); | |
267 | goto again; | |
268 | } | |
269 | set_page_extent_mapped(page); | |
270 | ||
f87f057b CM |
271 | /* |
272 | * this makes sure page_mkwrite is called on the | |
273 | * page if it is dirtied again later | |
274 | */ | |
275 | clear_page_dirty_for_io(page); | |
276 | ||
f46b5a66 CH |
277 | set_extent_delalloc(io_tree, page_start, |
278 | page_end, GFP_NOFS); | |
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 | ||
287 | out_unlock: | |
288 | mutex_unlock(&inode->i_mutex); | |
289 | return 0; | |
290 | } | |
291 | ||
292 | /* | |
293 | * Called inside transaction, so use GFP_NOFS | |
294 | */ | |
295 | ||
296 | static 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 | ||
392 | out_unlock: | |
7d9eb12c | 393 | mutex_unlock(&root->fs_info->volume_mutex); |
f46b5a66 CH |
394 | out: |
395 | kfree(vol_args); | |
396 | return ret; | |
397 | } | |
398 | ||
399 | static 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 |
454 | out: |
455 | kfree(vol_args); | |
456 | return ret; | |
457 | } | |
458 | ||
459 | static 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 | ||
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 | } | |
5516e595 | 491 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 CH |
492 | ret = btrfs_init_new_device(root, vol_args->name); |
493 | ||
494 | out: | |
495 | kfree(vol_args); | |
496 | return ret; | |
497 | } | |
498 | ||
499 | long 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 | ||
516 | out: | |
517 | kfree(vol_args); | |
518 | return ret; | |
519 | } | |
520 | ||
521 | int dup_item_to_inode(struct btrfs_trans_handle *trans, | |
522 | struct btrfs_root *root, | |
523 | struct btrfs_path *path, | |
524 | struct extent_buffer *leaf, | |
525 | int slot, | |
526 | struct btrfs_key *key, | |
527 | u64 destino) | |
528 | { | |
529 | char *dup; | |
530 | int len = btrfs_item_size_nr(leaf, slot); | |
531 | struct btrfs_key ckey = *key; | |
532 | int ret = 0; | |
533 | ||
534 | dup = kmalloc(len, GFP_NOFS); | |
535 | if (!dup) | |
536 | return -ENOMEM; | |
537 | ||
538 | read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len); | |
539 | btrfs_release_path(root, path); | |
540 | ||
541 | ckey.objectid = destino; | |
542 | ret = btrfs_insert_item(trans, root, &ckey, dup, len); | |
543 | kfree(dup); | |
544 | return ret; | |
545 | } | |
546 | ||
547 | long btrfs_ioctl_clone(struct file *file, unsigned long src_fd) | |
548 | { | |
549 | struct inode *inode = fdentry(file)->d_inode; | |
550 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
551 | struct file *src_file; | |
552 | struct inode *src; | |
553 | struct btrfs_trans_handle *trans; | |
554 | int ret; | |
555 | u64 pos; | |
556 | struct btrfs_path *path; | |
557 | struct btrfs_key key; | |
558 | struct extent_buffer *leaf; | |
559 | u32 nritems; | |
560 | int slot; | |
561 | ||
562 | src_file = fget(src_fd); | |
563 | if (!src_file) | |
564 | return -EBADF; | |
565 | src = src_file->f_dentry->d_inode; | |
566 | ||
567 | ret = -EXDEV; | |
568 | if (src->i_sb != inode->i_sb) | |
569 | goto out_fput; | |
570 | ||
571 | if (inode < src) { | |
572 | mutex_lock(&inode->i_mutex); | |
573 | mutex_lock(&src->i_mutex); | |
574 | } else { | |
575 | mutex_lock(&src->i_mutex); | |
576 | mutex_lock(&inode->i_mutex); | |
577 | } | |
578 | ||
579 | ret = -ENOTEMPTY; | |
580 | if (inode->i_size) | |
581 | goto out_unlock; | |
582 | ||
583 | /* do any pending delalloc/csum calc on src, one way or | |
584 | another, and lock file content */ | |
585 | while (1) { | |
586 | filemap_write_and_wait(src->i_mapping); | |
587 | lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); | |
588 | if (BTRFS_I(src)->delalloc_bytes == 0) | |
589 | break; | |
590 | unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); | |
591 | } | |
592 | ||
f46b5a66 CH |
593 | trans = btrfs_start_transaction(root, 0); |
594 | path = btrfs_alloc_path(); | |
595 | if (!path) { | |
596 | ret = -ENOMEM; | |
597 | goto out; | |
598 | } | |
599 | key.offset = 0; | |
600 | key.type = BTRFS_EXTENT_DATA_KEY; | |
601 | key.objectid = src->i_ino; | |
602 | pos = 0; | |
603 | path->reada = 2; | |
604 | ||
605 | while (1) { | |
606 | /* | |
607 | * note the key will change type as we walk through the | |
608 | * tree. | |
609 | */ | |
610 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); | |
611 | if (ret < 0) | |
612 | goto out; | |
613 | ||
614 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
615 | ret = btrfs_next_leaf(root, path); | |
616 | if (ret < 0) | |
617 | goto out; | |
618 | if (ret > 0) | |
619 | break; | |
620 | } | |
621 | leaf = path->nodes[0]; | |
622 | slot = path->slots[0]; | |
623 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
624 | nritems = btrfs_header_nritems(leaf); | |
625 | ||
626 | if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY || | |
627 | key.objectid != src->i_ino) | |
628 | break; | |
629 | ||
630 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { | |
631 | struct btrfs_file_extent_item *extent; | |
632 | int found_type; | |
633 | pos = key.offset; | |
634 | extent = btrfs_item_ptr(leaf, slot, | |
635 | struct btrfs_file_extent_item); | |
636 | found_type = btrfs_file_extent_type(leaf, extent); | |
637 | if (found_type == BTRFS_FILE_EXTENT_REG) { | |
638 | u64 len = btrfs_file_extent_num_bytes(leaf, | |
639 | extent); | |
640 | u64 ds = btrfs_file_extent_disk_bytenr(leaf, | |
641 | extent); | |
642 | u64 dl = btrfs_file_extent_disk_num_bytes(leaf, | |
643 | extent); | |
644 | u64 off = btrfs_file_extent_offset(leaf, | |
645 | extent); | |
646 | btrfs_insert_file_extent(trans, root, | |
647 | inode->i_ino, pos, | |
648 | ds, dl, len, off); | |
649 | /* ds == 0 means there's a hole */ | |
650 | if (ds != 0) { | |
651 | btrfs_inc_extent_ref(trans, root, | |
652 | ds, dl, | |
653 | root->root_key.objectid, | |
654 | trans->transid, | |
655 | inode->i_ino, pos); | |
656 | } | |
657 | pos = key.offset + len; | |
658 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
659 | ret = dup_item_to_inode(trans, root, path, | |
660 | leaf, slot, &key, | |
661 | inode->i_ino); | |
662 | if (ret) | |
663 | goto out; | |
664 | pos = key.offset + btrfs_item_size_nr(leaf, | |
665 | slot); | |
666 | } | |
667 | } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) { | |
668 | ret = dup_item_to_inode(trans, root, path, leaf, | |
669 | slot, &key, inode->i_ino); | |
670 | ||
671 | if (ret) | |
672 | goto out; | |
673 | } | |
674 | key.offset++; | |
675 | btrfs_release_path(root, path); | |
676 | } | |
677 | ||
678 | ret = 0; | |
679 | out: | |
680 | btrfs_free_path(path); | |
681 | ||
682 | inode->i_blocks = src->i_blocks; | |
683 | i_size_write(inode, src->i_size); | |
684 | btrfs_update_inode(trans, root, inode); | |
685 | ||
686 | unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); | |
687 | ||
688 | btrfs_end_transaction(trans, root); | |
f46b5a66 CH |
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 | if (file->private_data) { |
715 | ret = -EINPROGRESS; | |
716 | goto out; | |
717 | } | |
718 | trans = btrfs_start_transaction(root, 0); | |
719 | if (trans) | |
720 | file->private_data = trans; | |
721 | else | |
722 | ret = -ENOMEM; | |
723 | /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/ | |
724 | out: | |
f46b5a66 CH |
725 | return ret; |
726 | } | |
727 | ||
728 | /* | |
729 | * there are many ways the trans_start and trans_end ioctls can lead | |
730 | * to deadlocks. They should only be used by applications that | |
731 | * basically own the machine, and have a very in depth understanding | |
732 | * of all the possible deadlocks and enospc problems. | |
733 | */ | |
734 | long btrfs_ioctl_trans_end(struct file *file) | |
735 | { | |
736 | struct inode *inode = fdentry(file)->d_inode; | |
737 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
738 | struct btrfs_trans_handle *trans; | |
739 | int ret = 0; | |
740 | ||
f46b5a66 CH |
741 | trans = file->private_data; |
742 | if (!trans) { | |
743 | ret = -EINVAL; | |
744 | goto out; | |
745 | } | |
746 | btrfs_end_transaction(trans, root); | |
747 | file->private_data = 0; | |
748 | out: | |
f46b5a66 CH |
749 | return ret; |
750 | } | |
751 | ||
752 | long 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: | |
777 | btrfs_sync_fs(file->f_dentry->d_sb, 1); | |
778 | return 0; | |
779 | } | |
780 | ||
781 | return -ENOTTY; | |
782 | } |