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 | 41 | #include <linux/xattr.h> |
7ea394f1 | 42 | #include <linux/vmalloc.h> |
4b4e25f2 | 43 | #include "compat.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; | |
3de4586c | 70 | u64 index = 0; |
f46b5a66 CH |
71 | unsigned long nr = 1; |
72 | ||
6a63209f | 73 | ret = btrfs_check_metadata_free_space(root); |
f46b5a66 CH |
74 | if (ret) |
75 | goto fail_commit; | |
76 | ||
77 | trans = btrfs_start_transaction(root, 1); | |
78 | BUG_ON(!trans); | |
79 | ||
80 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, | |
81 | 0, &objectid); | |
82 | if (ret) | |
83 | goto fail; | |
84 | ||
31840ae1 ZY |
85 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
86 | objectid, trans->transid, 0, 0, 0); | |
8e8a1e31 JB |
87 | if (IS_ERR(leaf)) { |
88 | ret = PTR_ERR(leaf); | |
89 | goto fail; | |
90 | } | |
f46b5a66 CH |
91 | |
92 | btrfs_set_header_nritems(leaf, 0); | |
93 | btrfs_set_header_level(leaf, 0); | |
94 | btrfs_set_header_bytenr(leaf, leaf->start); | |
95 | btrfs_set_header_generation(leaf, trans->transid); | |
96 | btrfs_set_header_owner(leaf, objectid); | |
97 | ||
98 | write_extent_buffer(leaf, root->fs_info->fsid, | |
99 | (unsigned long)btrfs_header_fsid(leaf), | |
100 | BTRFS_FSID_SIZE); | |
101 | btrfs_mark_buffer_dirty(leaf); | |
102 | ||
103 | inode_item = &root_item.inode; | |
104 | memset(inode_item, 0, sizeof(*inode_item)); | |
105 | inode_item->generation = cpu_to_le64(1); | |
106 | inode_item->size = cpu_to_le64(3); | |
107 | inode_item->nlink = cpu_to_le32(1); | |
a76a3cd4 | 108 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
f46b5a66 CH |
109 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
110 | ||
111 | btrfs_set_root_bytenr(&root_item, leaf->start); | |
84234f3a | 112 | btrfs_set_root_generation(&root_item, trans->transid); |
f46b5a66 CH |
113 | btrfs_set_root_level(&root_item, 0); |
114 | btrfs_set_root_refs(&root_item, 1); | |
115 | btrfs_set_root_used(&root_item, 0); | |
80ff3856 | 116 | btrfs_set_root_last_snapshot(&root_item, 0); |
f46b5a66 CH |
117 | |
118 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); | |
119 | root_item.drop_level = 0; | |
120 | ||
925baedd | 121 | btrfs_tree_unlock(leaf); |
f46b5a66 CH |
122 | free_extent_buffer(leaf); |
123 | leaf = NULL; | |
124 | ||
125 | btrfs_set_root_dirid(&root_item, new_dirid); | |
126 | ||
127 | key.objectid = objectid; | |
128 | key.offset = 1; | |
129 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
130 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
131 | &root_item); | |
132 | if (ret) | |
133 | goto fail; | |
134 | ||
135 | /* | |
136 | * insert the directory item | |
137 | */ | |
138 | key.offset = (u64)-1; | |
3de4586c CM |
139 | dir = dentry->d_parent->d_inode; |
140 | ret = btrfs_set_inode_index(dir, &index); | |
141 | BUG_ON(ret); | |
142 | ||
143 | ret = btrfs_insert_dir_item(trans, root, | |
f46b5a66 | 144 | name, namelen, dir->i_ino, &key, |
3de4586c | 145 | BTRFS_FT_DIR, index); |
f46b5a66 CH |
146 | if (ret) |
147 | goto fail; | |
0660b5af | 148 | |
52c26179 YZ |
149 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
150 | ret = btrfs_update_inode(trans, root, dir); | |
151 | BUG_ON(ret); | |
152 | ||
0660b5af CM |
153 | /* add the backref first */ |
154 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, | |
155 | objectid, BTRFS_ROOT_BACKREF_KEY, | |
156 | root->root_key.objectid, | |
157 | dir->i_ino, index, name, namelen); | |
158 | ||
159 | BUG_ON(ret); | |
160 | ||
161 | /* now add the forward ref */ | |
162 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, | |
163 | root->root_key.objectid, BTRFS_ROOT_REF_KEY, | |
164 | objectid, | |
165 | dir->i_ino, index, name, namelen); | |
166 | ||
167 | BUG_ON(ret); | |
168 | ||
f46b5a66 CH |
169 | ret = btrfs_commit_transaction(trans, root); |
170 | if (ret) | |
171 | goto fail_commit; | |
172 | ||
3de4586c | 173 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
f46b5a66 CH |
174 | BUG_ON(!new_root); |
175 | ||
176 | trans = btrfs_start_transaction(new_root, 1); | |
177 | BUG_ON(!trans); | |
178 | ||
d2fb3437 | 179 | ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid, |
f46b5a66 CH |
180 | BTRFS_I(dir)->block_group); |
181 | if (ret) | |
182 | goto fail; | |
183 | ||
f46b5a66 CH |
184 | fail: |
185 | nr = trans->blocks_used; | |
186 | err = btrfs_commit_transaction(trans, new_root); | |
187 | if (err && !ret) | |
188 | ret = err; | |
189 | fail_commit: | |
f46b5a66 | 190 | btrfs_btree_balance_dirty(root, nr); |
f46b5a66 CH |
191 | return ret; |
192 | } | |
193 | ||
3de4586c CM |
194 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
195 | char *name, int namelen) | |
f46b5a66 CH |
196 | { |
197 | struct btrfs_pending_snapshot *pending_snapshot; | |
198 | struct btrfs_trans_handle *trans; | |
3de4586c | 199 | int ret = 0; |
f46b5a66 CH |
200 | int err; |
201 | unsigned long nr = 0; | |
202 | ||
203 | if (!root->ref_cows) | |
204 | return -EINVAL; | |
205 | ||
6a63209f | 206 | ret = btrfs_check_metadata_free_space(root); |
f46b5a66 CH |
207 | if (ret) |
208 | goto fail_unlock; | |
209 | ||
3de4586c | 210 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
f46b5a66 CH |
211 | if (!pending_snapshot) { |
212 | ret = -ENOMEM; | |
213 | goto fail_unlock; | |
214 | } | |
215 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); | |
216 | if (!pending_snapshot->name) { | |
217 | ret = -ENOMEM; | |
218 | kfree(pending_snapshot); | |
219 | goto fail_unlock; | |
220 | } | |
221 | memcpy(pending_snapshot->name, name, namelen); | |
222 | pending_snapshot->name[namelen] = '\0'; | |
3de4586c | 223 | pending_snapshot->dentry = dentry; |
f46b5a66 CH |
224 | trans = btrfs_start_transaction(root, 1); |
225 | BUG_ON(!trans); | |
226 | pending_snapshot->root = root; | |
227 | list_add(&pending_snapshot->list, | |
228 | &trans->transaction->pending_snapshots); | |
f46b5a66 CH |
229 | err = btrfs_commit_transaction(trans, root); |
230 | ||
231 | fail_unlock: | |
f46b5a66 | 232 | btrfs_btree_balance_dirty(root, nr); |
f46b5a66 CH |
233 | return ret; |
234 | } | |
235 | ||
cb8e7090 CH |
236 | /* copy of may_create in fs/namei.c() */ |
237 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) | |
238 | { | |
239 | if (child->d_inode) | |
240 | return -EEXIST; | |
241 | if (IS_DEADDIR(dir)) | |
242 | return -ENOENT; | |
243 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
244 | } | |
245 | ||
246 | /* | |
247 | * Create a new subvolume below @parent. This is largely modeled after | |
248 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup | |
249 | * inside this filesystem so it's quite a bit simpler. | |
250 | */ | |
251 | static noinline int btrfs_mksubvol(struct path *parent, char *name, | |
3de4586c CM |
252 | int mode, int namelen, |
253 | struct btrfs_root *snap_src) | |
cb8e7090 CH |
254 | { |
255 | struct dentry *dentry; | |
256 | int error; | |
257 | ||
258 | mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT); | |
259 | ||
260 | dentry = lookup_one_len(name, parent->dentry, namelen); | |
261 | error = PTR_ERR(dentry); | |
262 | if (IS_ERR(dentry)) | |
263 | goto out_unlock; | |
264 | ||
265 | error = -EEXIST; | |
266 | if (dentry->d_inode) | |
267 | goto out_dput; | |
268 | ||
269 | if (!IS_POSIXACL(parent->dentry->d_inode)) | |
ce3b0f8d | 270 | mode &= ~current_umask(); |
3de4586c | 271 | |
cb8e7090 CH |
272 | error = mnt_want_write(parent->mnt); |
273 | if (error) | |
274 | goto out_dput; | |
275 | ||
276 | error = btrfs_may_create(parent->dentry->d_inode, dentry); | |
277 | if (error) | |
278 | goto out_drop_write; | |
279 | ||
cb8e7090 CH |
280 | /* |
281 | * Actually perform the low-level subvolume creation after all | |
282 | * this VFS fuzz. | |
283 | * | |
284 | * Eventually we want to pass in an inode under which we create this | |
285 | * subvolume, but for now all are under the filesystem root. | |
286 | * | |
287 | * Also we should pass on the mode eventually to allow creating new | |
288 | * subvolume with specific mode bits. | |
289 | */ | |
3de4586c | 290 | if (snap_src) { |
ea9e8b11 CM |
291 | struct dentry *dir = dentry->d_parent; |
292 | struct dentry *test = dir->d_parent; | |
293 | struct btrfs_path *path = btrfs_alloc_path(); | |
294 | int ret; | |
295 | u64 test_oid; | |
296 | u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid; | |
297 | ||
298 | test_oid = snap_src->root_key.objectid; | |
299 | ||
300 | ret = btrfs_find_root_ref(snap_src->fs_info->tree_root, | |
301 | path, parent_oid, test_oid); | |
302 | if (ret == 0) | |
303 | goto create; | |
304 | btrfs_release_path(snap_src->fs_info->tree_root, path); | |
305 | ||
306 | /* we need to make sure we aren't creating a directory loop | |
307 | * by taking a snapshot of something that has our current | |
308 | * subvol in its directory tree. So, this loops through | |
309 | * the dentries and checks the forward refs for each subvolume | |
310 | * to see if is references the subvolume where we are | |
311 | * placing this new snapshot. | |
312 | */ | |
d397712b | 313 | while (1) { |
ea9e8b11 CM |
314 | if (!test || |
315 | dir == snap_src->fs_info->sb->s_root || | |
316 | test == snap_src->fs_info->sb->s_root || | |
317 | test->d_inode->i_sb != snap_src->fs_info->sb) { | |
318 | break; | |
319 | } | |
320 | if (S_ISLNK(test->d_inode->i_mode)) { | |
d397712b CM |
321 | printk(KERN_INFO "Btrfs symlink in snapshot " |
322 | "path, failed\n"); | |
ea9e8b11 CM |
323 | error = -EMLINK; |
324 | btrfs_free_path(path); | |
325 | goto out_drop_write; | |
326 | } | |
327 | test_oid = | |
328 | BTRFS_I(test->d_inode)->root->root_key.objectid; | |
329 | ret = btrfs_find_root_ref(snap_src->fs_info->tree_root, | |
330 | path, test_oid, parent_oid); | |
331 | if (ret == 0) { | |
d397712b CM |
332 | printk(KERN_INFO "Btrfs snapshot creation " |
333 | "failed, looping\n"); | |
ea9e8b11 CM |
334 | error = -EMLINK; |
335 | btrfs_free_path(path); | |
336 | goto out_drop_write; | |
337 | } | |
338 | btrfs_release_path(snap_src->fs_info->tree_root, path); | |
339 | test = test->d_parent; | |
340 | } | |
341 | create: | |
342 | btrfs_free_path(path); | |
3de4586c CM |
343 | error = create_snapshot(snap_src, dentry, name, namelen); |
344 | } else { | |
345 | error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, | |
346 | dentry, name, namelen); | |
347 | } | |
cb8e7090 CH |
348 | if (error) |
349 | goto out_drop_write; | |
350 | ||
351 | fsnotify_mkdir(parent->dentry->d_inode, dentry); | |
352 | out_drop_write: | |
353 | mnt_drop_write(parent->mnt); | |
354 | out_dput: | |
355 | dput(dentry); | |
356 | out_unlock: | |
357 | mutex_unlock(&parent->dentry->d_inode->i_mutex); | |
358 | return error; | |
359 | } | |
360 | ||
361 | ||
b2950863 | 362 | static int btrfs_defrag_file(struct file *file) |
f46b5a66 CH |
363 | { |
364 | struct inode *inode = fdentry(file)->d_inode; | |
365 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
366 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
3eaa2885 | 367 | struct btrfs_ordered_extent *ordered; |
f46b5a66 CH |
368 | struct page *page; |
369 | unsigned long last_index; | |
370 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; | |
371 | unsigned long total_read = 0; | |
372 | u64 page_start; | |
373 | u64 page_end; | |
374 | unsigned long i; | |
375 | int ret; | |
376 | ||
6a63209f | 377 | ret = btrfs_check_data_free_space(root, inode, inode->i_size); |
f46b5a66 CH |
378 | if (ret) |
379 | return -ENOSPC; | |
380 | ||
381 | mutex_lock(&inode->i_mutex); | |
382 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; | |
383 | for (i = 0; i <= last_index; i++) { | |
384 | if (total_read % ra_pages == 0) { | |
385 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, | |
386 | min(last_index, i + ra_pages - 1)); | |
387 | } | |
388 | total_read++; | |
3eaa2885 | 389 | again: |
f46b5a66 CH |
390 | page = grab_cache_page(inode->i_mapping, i); |
391 | if (!page) | |
392 | goto out_unlock; | |
393 | if (!PageUptodate(page)) { | |
394 | btrfs_readpage(NULL, page); | |
395 | lock_page(page); | |
396 | if (!PageUptodate(page)) { | |
397 | unlock_page(page); | |
398 | page_cache_release(page); | |
399 | goto out_unlock; | |
400 | } | |
401 | } | |
402 | ||
f46b5a66 | 403 | wait_on_page_writeback(page); |
f46b5a66 CH |
404 | |
405 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; | |
406 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
f46b5a66 | 407 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
3eaa2885 CM |
408 | |
409 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
410 | if (ordered) { | |
411 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
412 | unlock_page(page); | |
413 | page_cache_release(page); | |
414 | btrfs_start_ordered_extent(inode, ordered, 1); | |
415 | btrfs_put_ordered_extent(ordered); | |
416 | goto again; | |
417 | } | |
418 | set_page_extent_mapped(page); | |
419 | ||
f87f057b CM |
420 | /* |
421 | * this makes sure page_mkwrite is called on the | |
422 | * page if it is dirtied again later | |
423 | */ | |
424 | clear_page_dirty_for_io(page); | |
425 | ||
ea8c2819 | 426 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
f46b5a66 CH |
427 | |
428 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
429 | set_page_dirty(page); | |
430 | unlock_page(page); | |
431 | page_cache_release(page); | |
432 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); | |
433 | } | |
434 | ||
435 | out_unlock: | |
436 | mutex_unlock(&inode->i_mutex); | |
437 | return 0; | |
438 | } | |
439 | ||
440 | /* | |
441 | * Called inside transaction, so use GFP_NOFS | |
442 | */ | |
443 | ||
444 | static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) | |
445 | { | |
446 | u64 new_size; | |
447 | u64 old_size; | |
448 | u64 devid = 1; | |
449 | struct btrfs_ioctl_vol_args *vol_args; | |
450 | struct btrfs_trans_handle *trans; | |
451 | struct btrfs_device *device = NULL; | |
452 | char *sizestr; | |
453 | char *devstr = NULL; | |
454 | int ret = 0; | |
455 | int namelen; | |
456 | int mod = 0; | |
457 | ||
c146afad YZ |
458 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
459 | return -EROFS; | |
460 | ||
e441d54d CM |
461 | if (!capable(CAP_SYS_ADMIN)) |
462 | return -EPERM; | |
463 | ||
dae7b665 LZ |
464 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
465 | if (IS_ERR(vol_args)) | |
466 | return PTR_ERR(vol_args); | |
5516e595 MF |
467 | |
468 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
f46b5a66 | 469 | namelen = strlen(vol_args->name); |
f46b5a66 | 470 | |
7d9eb12c | 471 | mutex_lock(&root->fs_info->volume_mutex); |
f46b5a66 CH |
472 | sizestr = vol_args->name; |
473 | devstr = strchr(sizestr, ':'); | |
474 | if (devstr) { | |
475 | char *end; | |
476 | sizestr = devstr + 1; | |
477 | *devstr = '\0'; | |
478 | devstr = vol_args->name; | |
479 | devid = simple_strtoull(devstr, &end, 10); | |
480 | printk(KERN_INFO "resizing devid %llu\n", devid); | |
481 | } | |
2b82032c | 482 | device = btrfs_find_device(root, devid, NULL, NULL); |
f46b5a66 CH |
483 | if (!device) { |
484 | printk(KERN_INFO "resizer unable to find device %llu\n", devid); | |
485 | ret = -EINVAL; | |
486 | goto out_unlock; | |
487 | } | |
488 | if (!strcmp(sizestr, "max")) | |
489 | new_size = device->bdev->bd_inode->i_size; | |
490 | else { | |
491 | if (sizestr[0] == '-') { | |
492 | mod = -1; | |
493 | sizestr++; | |
494 | } else if (sizestr[0] == '+') { | |
495 | mod = 1; | |
496 | sizestr++; | |
497 | } | |
498 | new_size = btrfs_parse_size(sizestr); | |
499 | if (new_size == 0) { | |
500 | ret = -EINVAL; | |
501 | goto out_unlock; | |
502 | } | |
503 | } | |
504 | ||
505 | old_size = device->total_bytes; | |
506 | ||
507 | if (mod < 0) { | |
508 | if (new_size > old_size) { | |
509 | ret = -EINVAL; | |
510 | goto out_unlock; | |
511 | } | |
512 | new_size = old_size - new_size; | |
513 | } else if (mod > 0) { | |
514 | new_size = old_size + new_size; | |
515 | } | |
516 | ||
517 | if (new_size < 256 * 1024 * 1024) { | |
518 | ret = -EINVAL; | |
519 | goto out_unlock; | |
520 | } | |
521 | if (new_size > device->bdev->bd_inode->i_size) { | |
522 | ret = -EFBIG; | |
523 | goto out_unlock; | |
524 | } | |
525 | ||
526 | do_div(new_size, root->sectorsize); | |
527 | new_size *= root->sectorsize; | |
528 | ||
529 | printk(KERN_INFO "new size for %s is %llu\n", | |
530 | device->name, (unsigned long long)new_size); | |
531 | ||
532 | if (new_size > old_size) { | |
533 | trans = btrfs_start_transaction(root, 1); | |
534 | ret = btrfs_grow_device(trans, device, new_size); | |
535 | btrfs_commit_transaction(trans, root); | |
536 | } else { | |
537 | ret = btrfs_shrink_device(device, new_size); | |
538 | } | |
539 | ||
540 | out_unlock: | |
7d9eb12c | 541 | mutex_unlock(&root->fs_info->volume_mutex); |
f46b5a66 CH |
542 | kfree(vol_args); |
543 | return ret; | |
544 | } | |
545 | ||
cb8e7090 | 546 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
3de4586c | 547 | void __user *arg, int subvol) |
f46b5a66 | 548 | { |
cb8e7090 | 549 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
f46b5a66 CH |
550 | struct btrfs_ioctl_vol_args *vol_args; |
551 | struct btrfs_dir_item *di; | |
552 | struct btrfs_path *path; | |
3de4586c | 553 | struct file *src_file; |
f46b5a66 CH |
554 | u64 root_dirid; |
555 | int namelen; | |
3de4586c | 556 | int ret = 0; |
f46b5a66 | 557 | |
c146afad YZ |
558 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
559 | return -EROFS; | |
560 | ||
dae7b665 LZ |
561 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
562 | if (IS_ERR(vol_args)) | |
563 | return PTR_ERR(vol_args); | |
f46b5a66 | 564 | |
5516e595 | 565 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 | 566 | namelen = strlen(vol_args->name); |
f46b5a66 CH |
567 | if (strchr(vol_args->name, '/')) { |
568 | ret = -EINVAL; | |
569 | goto out; | |
570 | } | |
571 | ||
572 | path = btrfs_alloc_path(); | |
573 | if (!path) { | |
574 | ret = -ENOMEM; | |
575 | goto out; | |
576 | } | |
577 | ||
578 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, | |
f46b5a66 CH |
579 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
580 | path, root_dirid, | |
581 | vol_args->name, namelen, 0); | |
f46b5a66 CH |
582 | btrfs_free_path(path); |
583 | ||
584 | if (di && !IS_ERR(di)) { | |
585 | ret = -EEXIST; | |
586 | goto out; | |
587 | } | |
588 | ||
589 | if (IS_ERR(di)) { | |
590 | ret = PTR_ERR(di); | |
591 | goto out; | |
592 | } | |
593 | ||
3de4586c | 594 | if (subvol) { |
cb8e7090 CH |
595 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, |
596 | file->f_path.dentry->d_inode->i_mode, | |
3de4586c | 597 | namelen, NULL); |
cb8e7090 | 598 | } else { |
3de4586c CM |
599 | struct inode *src_inode; |
600 | src_file = fget(vol_args->fd); | |
601 | if (!src_file) { | |
602 | ret = -EINVAL; | |
603 | goto out; | |
604 | } | |
605 | ||
606 | src_inode = src_file->f_path.dentry->d_inode; | |
607 | if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { | |
d397712b CM |
608 | printk(KERN_INFO "btrfs: Snapshot src from " |
609 | "another FS\n"); | |
3de4586c CM |
610 | ret = -EINVAL; |
611 | fput(src_file); | |
612 | goto out; | |
613 | } | |
614 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, | |
615 | file->f_path.dentry->d_inode->i_mode, | |
616 | namelen, BTRFS_I(src_inode)->root); | |
617 | fput(src_file); | |
cb8e7090 CH |
618 | } |
619 | ||
f46b5a66 CH |
620 | out: |
621 | kfree(vol_args); | |
622 | return ret; | |
623 | } | |
624 | ||
625 | static int btrfs_ioctl_defrag(struct file *file) | |
626 | { | |
627 | struct inode *inode = fdentry(file)->d_inode; | |
628 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
c146afad YZ |
629 | int ret; |
630 | ||
631 | ret = mnt_want_write(file->f_path.mnt); | |
632 | if (ret) | |
633 | return ret; | |
f46b5a66 CH |
634 | |
635 | switch (inode->i_mode & S_IFMT) { | |
636 | case S_IFDIR: | |
e441d54d CM |
637 | if (!capable(CAP_SYS_ADMIN)) { |
638 | ret = -EPERM; | |
639 | goto out; | |
640 | } | |
f46b5a66 CH |
641 | btrfs_defrag_root(root, 0); |
642 | btrfs_defrag_root(root->fs_info->extent_root, 0); | |
f46b5a66 CH |
643 | break; |
644 | case S_IFREG: | |
e441d54d CM |
645 | if (!(file->f_mode & FMODE_WRITE)) { |
646 | ret = -EINVAL; | |
647 | goto out; | |
648 | } | |
f46b5a66 CH |
649 | btrfs_defrag_file(file); |
650 | break; | |
651 | } | |
e441d54d | 652 | out: |
ab67b7c1 | 653 | mnt_drop_write(file->f_path.mnt); |
e441d54d | 654 | return ret; |
f46b5a66 CH |
655 | } |
656 | ||
b2950863 | 657 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
f46b5a66 CH |
658 | { |
659 | struct btrfs_ioctl_vol_args *vol_args; | |
660 | int ret; | |
661 | ||
e441d54d CM |
662 | if (!capable(CAP_SYS_ADMIN)) |
663 | return -EPERM; | |
664 | ||
dae7b665 LZ |
665 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
666 | if (IS_ERR(vol_args)) | |
667 | return PTR_ERR(vol_args); | |
f46b5a66 | 668 | |
5516e595 | 669 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 CH |
670 | ret = btrfs_init_new_device(root, vol_args->name); |
671 | ||
f46b5a66 CH |
672 | kfree(vol_args); |
673 | return ret; | |
674 | } | |
675 | ||
b2950863 | 676 | static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) |
f46b5a66 CH |
677 | { |
678 | struct btrfs_ioctl_vol_args *vol_args; | |
679 | int ret; | |
680 | ||
e441d54d CM |
681 | if (!capable(CAP_SYS_ADMIN)) |
682 | return -EPERM; | |
683 | ||
c146afad YZ |
684 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
685 | return -EROFS; | |
686 | ||
dae7b665 LZ |
687 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
688 | if (IS_ERR(vol_args)) | |
689 | return PTR_ERR(vol_args); | |
f46b5a66 | 690 | |
5516e595 | 691 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 CH |
692 | ret = btrfs_rm_device(root, vol_args->name); |
693 | ||
f46b5a66 CH |
694 | kfree(vol_args); |
695 | return ret; | |
696 | } | |
697 | ||
b2950863 CH |
698 | static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
699 | u64 off, u64 olen, u64 destoff) | |
f46b5a66 CH |
700 | { |
701 | struct inode *inode = fdentry(file)->d_inode; | |
702 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
703 | struct file *src_file; | |
704 | struct inode *src; | |
705 | struct btrfs_trans_handle *trans; | |
f46b5a66 | 706 | struct btrfs_path *path; |
f46b5a66 | 707 | struct extent_buffer *leaf; |
ae01a0ab YZ |
708 | char *buf; |
709 | struct btrfs_key key; | |
f46b5a66 CH |
710 | u32 nritems; |
711 | int slot; | |
ae01a0ab | 712 | int ret; |
c5c9cd4d SW |
713 | u64 len = olen; |
714 | u64 bs = root->fs_info->sb->s_blocksize; | |
715 | u64 hint_byte; | |
d20f7043 | 716 | |
c5c9cd4d SW |
717 | /* |
718 | * TODO: | |
719 | * - split compressed inline extents. annoying: we need to | |
720 | * decompress into destination's address_space (the file offset | |
721 | * may change, so source mapping won't do), then recompress (or | |
722 | * otherwise reinsert) a subrange. | |
723 | * - allow ranges within the same file to be cloned (provided | |
724 | * they don't overlap)? | |
725 | */ | |
726 | ||
e441d54d CM |
727 | /* the destination must be opened for writing */ |
728 | if (!(file->f_mode & FMODE_WRITE)) | |
729 | return -EINVAL; | |
730 | ||
c146afad YZ |
731 | ret = mnt_want_write(file->f_path.mnt); |
732 | if (ret) | |
733 | return ret; | |
734 | ||
c5c9cd4d | 735 | src_file = fget(srcfd); |
ab67b7c1 YZ |
736 | if (!src_file) { |
737 | ret = -EBADF; | |
738 | goto out_drop_write; | |
739 | } | |
f46b5a66 CH |
740 | src = src_file->f_dentry->d_inode; |
741 | ||
c5c9cd4d SW |
742 | ret = -EINVAL; |
743 | if (src == inode) | |
744 | goto out_fput; | |
745 | ||
ae01a0ab YZ |
746 | ret = -EISDIR; |
747 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) | |
748 | goto out_fput; | |
749 | ||
f46b5a66 | 750 | ret = -EXDEV; |
ae01a0ab YZ |
751 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
752 | goto out_fput; | |
753 | ||
754 | ret = -ENOMEM; | |
755 | buf = vmalloc(btrfs_level_size(root, 0)); | |
756 | if (!buf) | |
757 | goto out_fput; | |
758 | ||
759 | path = btrfs_alloc_path(); | |
760 | if (!path) { | |
761 | vfree(buf); | |
f46b5a66 | 762 | goto out_fput; |
ae01a0ab YZ |
763 | } |
764 | path->reada = 2; | |
f46b5a66 CH |
765 | |
766 | if (inode < src) { | |
767 | mutex_lock(&inode->i_mutex); | |
768 | mutex_lock(&src->i_mutex); | |
769 | } else { | |
770 | mutex_lock(&src->i_mutex); | |
771 | mutex_lock(&inode->i_mutex); | |
772 | } | |
773 | ||
c5c9cd4d SW |
774 | /* determine range to clone */ |
775 | ret = -EINVAL; | |
776 | if (off >= src->i_size || off + len > src->i_size) | |
f46b5a66 | 777 | goto out_unlock; |
c5c9cd4d SW |
778 | if (len == 0) |
779 | olen = len = src->i_size - off; | |
780 | /* if we extend to eof, continue to block boundary */ | |
781 | if (off + len == src->i_size) | |
782 | len = ((src->i_size + bs-1) & ~(bs-1)) | |
783 | - off; | |
784 | ||
785 | /* verify the end result is block aligned */ | |
786 | if ((off & (bs-1)) || | |
787 | ((off + len) & (bs-1))) | |
788 | goto out_unlock; | |
789 | ||
f46b5a66 CH |
790 | /* do any pending delalloc/csum calc on src, one way or |
791 | another, and lock file content */ | |
792 | while (1) { | |
31840ae1 | 793 | struct btrfs_ordered_extent *ordered; |
c5c9cd4d SW |
794 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
795 | ordered = btrfs_lookup_first_ordered_extent(inode, off+len); | |
ae01a0ab | 796 | if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered) |
f46b5a66 | 797 | break; |
c5c9cd4d | 798 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
ae01a0ab YZ |
799 | if (ordered) |
800 | btrfs_put_ordered_extent(ordered); | |
c5c9cd4d | 801 | btrfs_wait_ordered_range(src, off, off+len); |
f46b5a66 CH |
802 | } |
803 | ||
ae01a0ab YZ |
804 | trans = btrfs_start_transaction(root, 1); |
805 | BUG_ON(!trans); | |
806 | ||
c5c9cd4d SW |
807 | /* punch hole in destination first */ |
808 | btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte); | |
809 | ||
810 | /* clone data */ | |
f46b5a66 | 811 | key.objectid = src->i_ino; |
ae01a0ab YZ |
812 | key.type = BTRFS_EXTENT_DATA_KEY; |
813 | key.offset = 0; | |
f46b5a66 CH |
814 | |
815 | while (1) { | |
816 | /* | |
817 | * note the key will change type as we walk through the | |
818 | * tree. | |
819 | */ | |
820 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); | |
821 | if (ret < 0) | |
822 | goto out; | |
823 | ||
ae01a0ab YZ |
824 | nritems = btrfs_header_nritems(path->nodes[0]); |
825 | if (path->slots[0] >= nritems) { | |
f46b5a66 CH |
826 | ret = btrfs_next_leaf(root, path); |
827 | if (ret < 0) | |
828 | goto out; | |
829 | if (ret > 0) | |
830 | break; | |
ae01a0ab | 831 | nritems = btrfs_header_nritems(path->nodes[0]); |
f46b5a66 CH |
832 | } |
833 | leaf = path->nodes[0]; | |
834 | slot = path->slots[0]; | |
f46b5a66 | 835 | |
ae01a0ab | 836 | btrfs_item_key_to_cpu(leaf, &key, slot); |
d20f7043 | 837 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
f46b5a66 CH |
838 | key.objectid != src->i_ino) |
839 | break; | |
840 | ||
c5c9cd4d SW |
841 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
842 | struct btrfs_file_extent_item *extent; | |
843 | int type; | |
31840ae1 ZY |
844 | u32 size; |
845 | struct btrfs_key new_key; | |
c5c9cd4d SW |
846 | u64 disko = 0, diskl = 0; |
847 | u64 datao = 0, datal = 0; | |
848 | u8 comp; | |
31840ae1 ZY |
849 | |
850 | size = btrfs_item_size_nr(leaf, slot); | |
851 | read_extent_buffer(leaf, buf, | |
852 | btrfs_item_ptr_offset(leaf, slot), | |
853 | size); | |
c5c9cd4d SW |
854 | |
855 | extent = btrfs_item_ptr(leaf, slot, | |
856 | struct btrfs_file_extent_item); | |
857 | comp = btrfs_file_extent_compression(leaf, extent); | |
858 | type = btrfs_file_extent_type(leaf, extent); | |
859 | if (type == BTRFS_FILE_EXTENT_REG) { | |
d397712b CM |
860 | disko = btrfs_file_extent_disk_bytenr(leaf, |
861 | extent); | |
862 | diskl = btrfs_file_extent_disk_num_bytes(leaf, | |
863 | extent); | |
c5c9cd4d | 864 | datao = btrfs_file_extent_offset(leaf, extent); |
d397712b CM |
865 | datal = btrfs_file_extent_num_bytes(leaf, |
866 | extent); | |
c5c9cd4d SW |
867 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
868 | /* take upper bound, may be compressed */ | |
869 | datal = btrfs_file_extent_ram_bytes(leaf, | |
870 | extent); | |
871 | } | |
31840ae1 ZY |
872 | btrfs_release_path(root, path); |
873 | ||
c5c9cd4d SW |
874 | if (key.offset + datal < off || |
875 | key.offset >= off+len) | |
876 | goto next; | |
877 | ||
31840ae1 ZY |
878 | memcpy(&new_key, &key, sizeof(new_key)); |
879 | new_key.objectid = inode->i_ino; | |
c5c9cd4d | 880 | new_key.offset = key.offset + destoff - off; |
31840ae1 | 881 | |
c5c9cd4d SW |
882 | if (type == BTRFS_FILE_EXTENT_REG) { |
883 | ret = btrfs_insert_empty_item(trans, root, path, | |
884 | &new_key, size); | |
885 | if (ret) | |
886 | goto out; | |
887 | ||
888 | leaf = path->nodes[0]; | |
889 | slot = path->slots[0]; | |
890 | write_extent_buffer(leaf, buf, | |
31840ae1 ZY |
891 | btrfs_item_ptr_offset(leaf, slot), |
892 | size); | |
ae01a0ab | 893 | |
c5c9cd4d | 894 | extent = btrfs_item_ptr(leaf, slot, |
f46b5a66 | 895 | struct btrfs_file_extent_item); |
c5c9cd4d SW |
896 | |
897 | if (off > key.offset) { | |
898 | datao += off - key.offset; | |
899 | datal -= off - key.offset; | |
900 | } | |
901 | if (key.offset + datao + datal + key.offset > | |
902 | off + len) | |
903 | datal = off + len - key.offset - datao; | |
904 | /* disko == 0 means it's a hole */ | |
905 | if (!disko) | |
906 | datao = 0; | |
c5c9cd4d SW |
907 | |
908 | btrfs_set_file_extent_offset(leaf, extent, | |
909 | datao); | |
910 | btrfs_set_file_extent_num_bytes(leaf, extent, | |
911 | datal); | |
912 | if (disko) { | |
913 | inode_add_bytes(inode, datal); | |
ae01a0ab | 914 | ret = btrfs_inc_extent_ref(trans, root, |
c5c9cd4d SW |
915 | disko, diskl, leaf->start, |
916 | root->root_key.objectid, | |
917 | trans->transid, | |
918 | inode->i_ino); | |
31840ae1 | 919 | BUG_ON(ret); |
f46b5a66 | 920 | } |
c5c9cd4d SW |
921 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
922 | u64 skip = 0; | |
923 | u64 trim = 0; | |
924 | if (off > key.offset) { | |
925 | skip = off - key.offset; | |
926 | new_key.offset += skip; | |
927 | } | |
d397712b | 928 | |
c5c9cd4d SW |
929 | if (key.offset + datal > off+len) |
930 | trim = key.offset + datal - (off+len); | |
d397712b | 931 | |
c5c9cd4d | 932 | if (comp && (skip || trim)) { |
c5c9cd4d SW |
933 | ret = -EINVAL; |
934 | goto out; | |
935 | } | |
936 | size -= skip + trim; | |
937 | datal -= skip + trim; | |
938 | ret = btrfs_insert_empty_item(trans, root, path, | |
939 | &new_key, size); | |
940 | if (ret) | |
941 | goto out; | |
942 | ||
943 | if (skip) { | |
d397712b CM |
944 | u32 start = |
945 | btrfs_file_extent_calc_inline_size(0); | |
c5c9cd4d SW |
946 | memmove(buf+start, buf+start+skip, |
947 | datal); | |
948 | } | |
949 | ||
950 | leaf = path->nodes[0]; | |
951 | slot = path->slots[0]; | |
952 | write_extent_buffer(leaf, buf, | |
953 | btrfs_item_ptr_offset(leaf, slot), | |
954 | size); | |
955 | inode_add_bytes(inode, datal); | |
f46b5a66 | 956 | } |
c5c9cd4d SW |
957 | |
958 | btrfs_mark_buffer_dirty(leaf); | |
ae01a0ab | 959 | } |
c5c9cd4d | 960 | |
d397712b | 961 | next: |
31840ae1 | 962 | btrfs_release_path(root, path); |
f46b5a66 | 963 | key.offset++; |
f46b5a66 | 964 | } |
f46b5a66 CH |
965 | ret = 0; |
966 | out: | |
ae01a0ab YZ |
967 | btrfs_release_path(root, path); |
968 | if (ret == 0) { | |
969 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
c5c9cd4d SW |
970 | if (destoff + olen > inode->i_size) |
971 | btrfs_i_size_write(inode, destoff + olen); | |
ae01a0ab YZ |
972 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; |
973 | ret = btrfs_update_inode(trans, root, inode); | |
974 | } | |
f46b5a66 | 975 | btrfs_end_transaction(trans, root); |
c5c9cd4d | 976 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
ae01a0ab YZ |
977 | if (ret) |
978 | vmtruncate(inode, 0); | |
f46b5a66 CH |
979 | out_unlock: |
980 | mutex_unlock(&src->i_mutex); | |
981 | mutex_unlock(&inode->i_mutex); | |
ae01a0ab YZ |
982 | vfree(buf); |
983 | btrfs_free_path(path); | |
f46b5a66 CH |
984 | out_fput: |
985 | fput(src_file); | |
ab67b7c1 YZ |
986 | out_drop_write: |
987 | mnt_drop_write(file->f_path.mnt); | |
f46b5a66 CH |
988 | return ret; |
989 | } | |
990 | ||
7a865e8a | 991 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
c5c9cd4d SW |
992 | { |
993 | struct btrfs_ioctl_clone_range_args args; | |
994 | ||
7a865e8a | 995 | if (copy_from_user(&args, argp, sizeof(args))) |
c5c9cd4d SW |
996 | return -EFAULT; |
997 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, | |
998 | args.src_length, args.dest_offset); | |
999 | } | |
1000 | ||
f46b5a66 CH |
1001 | /* |
1002 | * there are many ways the trans_start and trans_end ioctls can lead | |
1003 | * to deadlocks. They should only be used by applications that | |
1004 | * basically own the machine, and have a very in depth understanding | |
1005 | * of all the possible deadlocks and enospc problems. | |
1006 | */ | |
b2950863 | 1007 | static long btrfs_ioctl_trans_start(struct file *file) |
f46b5a66 CH |
1008 | { |
1009 | struct inode *inode = fdentry(file)->d_inode; | |
1010 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1011 | struct btrfs_trans_handle *trans; | |
1012 | int ret = 0; | |
1013 | ||
df5b5520 CH |
1014 | if (!capable(CAP_SYS_ADMIN)) |
1015 | return -EPERM; | |
1016 | ||
f46b5a66 CH |
1017 | if (file->private_data) { |
1018 | ret = -EINPROGRESS; | |
1019 | goto out; | |
1020 | } | |
9ca9ee09 | 1021 | |
c146afad YZ |
1022 | ret = mnt_want_write(file->f_path.mnt); |
1023 | if (ret) | |
1024 | goto out; | |
1025 | ||
9ca9ee09 SW |
1026 | mutex_lock(&root->fs_info->trans_mutex); |
1027 | root->fs_info->open_ioctl_trans++; | |
1028 | mutex_unlock(&root->fs_info->trans_mutex); | |
1029 | ||
1030 | trans = btrfs_start_ioctl_transaction(root, 0); | |
f46b5a66 CH |
1031 | if (trans) |
1032 | file->private_data = trans; | |
1033 | else | |
1034 | ret = -ENOMEM; | |
1035 | /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/ | |
1036 | out: | |
f46b5a66 CH |
1037 | return ret; |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * there are many ways the trans_start and trans_end ioctls can lead | |
1042 | * to deadlocks. They should only be used by applications that | |
1043 | * basically own the machine, and have a very in depth understanding | |
1044 | * of all the possible deadlocks and enospc problems. | |
1045 | */ | |
1046 | long btrfs_ioctl_trans_end(struct file *file) | |
1047 | { | |
1048 | struct inode *inode = fdentry(file)->d_inode; | |
1049 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1050 | struct btrfs_trans_handle *trans; | |
1051 | int ret = 0; | |
1052 | ||
f46b5a66 CH |
1053 | trans = file->private_data; |
1054 | if (!trans) { | |
1055 | ret = -EINVAL; | |
1056 | goto out; | |
1057 | } | |
1058 | btrfs_end_transaction(trans, root); | |
b214107e | 1059 | file->private_data = NULL; |
9ca9ee09 SW |
1060 | |
1061 | mutex_lock(&root->fs_info->trans_mutex); | |
1062 | root->fs_info->open_ioctl_trans--; | |
1063 | mutex_unlock(&root->fs_info->trans_mutex); | |
1064 | ||
cfc8ea87 SW |
1065 | mnt_drop_write(file->f_path.mnt); |
1066 | ||
f46b5a66 | 1067 | out: |
f46b5a66 CH |
1068 | return ret; |
1069 | } | |
1070 | ||
1071 | long btrfs_ioctl(struct file *file, unsigned int | |
1072 | cmd, unsigned long arg) | |
1073 | { | |
1074 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; | |
4bcabaa3 | 1075 | void __user *argp = (void __user *)arg; |
f46b5a66 CH |
1076 | |
1077 | switch (cmd) { | |
1078 | case BTRFS_IOC_SNAP_CREATE: | |
4bcabaa3 | 1079 | return btrfs_ioctl_snap_create(file, argp, 0); |
3de4586c | 1080 | case BTRFS_IOC_SUBVOL_CREATE: |
4bcabaa3 | 1081 | return btrfs_ioctl_snap_create(file, argp, 1); |
f46b5a66 CH |
1082 | case BTRFS_IOC_DEFRAG: |
1083 | return btrfs_ioctl_defrag(file); | |
1084 | case BTRFS_IOC_RESIZE: | |
4bcabaa3 | 1085 | return btrfs_ioctl_resize(root, argp); |
f46b5a66 | 1086 | case BTRFS_IOC_ADD_DEV: |
4bcabaa3 | 1087 | return btrfs_ioctl_add_dev(root, argp); |
f46b5a66 | 1088 | case BTRFS_IOC_RM_DEV: |
4bcabaa3 | 1089 | return btrfs_ioctl_rm_dev(root, argp); |
f46b5a66 CH |
1090 | case BTRFS_IOC_BALANCE: |
1091 | return btrfs_balance(root->fs_info->dev_root); | |
1092 | case BTRFS_IOC_CLONE: | |
c5c9cd4d SW |
1093 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
1094 | case BTRFS_IOC_CLONE_RANGE: | |
7a865e8a | 1095 | return btrfs_ioctl_clone_range(file, argp); |
f46b5a66 CH |
1096 | case BTRFS_IOC_TRANS_START: |
1097 | return btrfs_ioctl_trans_start(file); | |
1098 | case BTRFS_IOC_TRANS_END: | |
1099 | return btrfs_ioctl_trans_end(file); | |
1100 | case BTRFS_IOC_SYNC: | |
1101 | btrfs_sync_fs(file->f_dentry->d_sb, 1); | |
1102 | return 0; | |
1103 | } | |
1104 | ||
1105 | return -ENOTTY; | |
1106 | } |