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