Btrfs: add SNAP_CREATE_ASYNC ioctl
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / btrfs / ioctl.c
CommitLineData
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>
f46b5a66 30#include <linux/backing-dev.h>
cb8e7090 31#include <linux/mount.h>
f46b5a66 32#include <linux/mpage.h>
cb8e7090 33#include <linux/namei.h>
f46b5a66
CH
34#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
cb8e7090 39#include <linux/security.h>
f46b5a66 40#include <linux/xattr.h>
7ea394f1 41#include <linux/vmalloc.h>
5a0e3ad6 42#include <linux/slab.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 52
6cbff00f
CH
53/* Mask out flags that are inappropriate for the given type of inode. */
54static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
55{
56 if (S_ISDIR(mode))
57 return flags;
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
60 else
61 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
62}
63
64/*
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
66 */
67static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
68{
69 unsigned int iflags = 0;
70
71 if (flags & BTRFS_INODE_SYNC)
72 iflags |= FS_SYNC_FL;
73 if (flags & BTRFS_INODE_IMMUTABLE)
74 iflags |= FS_IMMUTABLE_FL;
75 if (flags & BTRFS_INODE_APPEND)
76 iflags |= FS_APPEND_FL;
77 if (flags & BTRFS_INODE_NODUMP)
78 iflags |= FS_NODUMP_FL;
79 if (flags & BTRFS_INODE_NOATIME)
80 iflags |= FS_NOATIME_FL;
81 if (flags & BTRFS_INODE_DIRSYNC)
82 iflags |= FS_DIRSYNC_FL;
83
84 return iflags;
85}
86
87/*
88 * Update inode->i_flags based on the btrfs internal flags.
89 */
90void btrfs_update_iflags(struct inode *inode)
91{
92 struct btrfs_inode *ip = BTRFS_I(inode);
93
94 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
95
96 if (ip->flags & BTRFS_INODE_SYNC)
97 inode->i_flags |= S_SYNC;
98 if (ip->flags & BTRFS_INODE_IMMUTABLE)
99 inode->i_flags |= S_IMMUTABLE;
100 if (ip->flags & BTRFS_INODE_APPEND)
101 inode->i_flags |= S_APPEND;
102 if (ip->flags & BTRFS_INODE_NOATIME)
103 inode->i_flags |= S_NOATIME;
104 if (ip->flags & BTRFS_INODE_DIRSYNC)
105 inode->i_flags |= S_DIRSYNC;
106}
107
108/*
109 * Inherit flags from the parent inode.
110 *
111 * Unlike extN we don't have any flags we don't want to inherit currently.
112 */
113void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
114{
0b4dcea5
CM
115 unsigned int flags;
116
117 if (!dir)
118 return;
119
120 flags = BTRFS_I(dir)->flags;
6cbff00f
CH
121
122 if (S_ISREG(inode->i_mode))
123 flags &= ~BTRFS_INODE_DIRSYNC;
124 else if (!S_ISDIR(inode->i_mode))
125 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
126
127 BTRFS_I(inode)->flags = flags;
128 btrfs_update_iflags(inode);
129}
130
131static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
132{
133 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
134 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
135
136 if (copy_to_user(arg, &flags, sizeof(flags)))
137 return -EFAULT;
138 return 0;
139}
140
141static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
142{
143 struct inode *inode = file->f_path.dentry->d_inode;
144 struct btrfs_inode *ip = BTRFS_I(inode);
145 struct btrfs_root *root = ip->root;
146 struct btrfs_trans_handle *trans;
147 unsigned int flags, oldflags;
148 int ret;
149
150 if (copy_from_user(&flags, arg, sizeof(flags)))
151 return -EFAULT;
152
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL))
156 return -EOPNOTSUPP;
f46b5a66 157
6cbff00f
CH
158 if (!is_owner_or_cap(inode))
159 return -EACCES;
160
161 mutex_lock(&inode->i_mutex);
162
163 flags = btrfs_mask_flags(inode->i_mode, flags);
164 oldflags = btrfs_flags_to_ioctl(ip->flags);
165 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
166 if (!capable(CAP_LINUX_IMMUTABLE)) {
167 ret = -EPERM;
168 goto out_unlock;
169 }
170 }
171
172 ret = mnt_want_write(file->f_path.mnt);
173 if (ret)
174 goto out_unlock;
175
176 if (flags & FS_SYNC_FL)
177 ip->flags |= BTRFS_INODE_SYNC;
178 else
179 ip->flags &= ~BTRFS_INODE_SYNC;
180 if (flags & FS_IMMUTABLE_FL)
181 ip->flags |= BTRFS_INODE_IMMUTABLE;
182 else
183 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
184 if (flags & FS_APPEND_FL)
185 ip->flags |= BTRFS_INODE_APPEND;
186 else
187 ip->flags &= ~BTRFS_INODE_APPEND;
188 if (flags & FS_NODUMP_FL)
189 ip->flags |= BTRFS_INODE_NODUMP;
190 else
191 ip->flags &= ~BTRFS_INODE_NODUMP;
192 if (flags & FS_NOATIME_FL)
193 ip->flags |= BTRFS_INODE_NOATIME;
194 else
195 ip->flags &= ~BTRFS_INODE_NOATIME;
196 if (flags & FS_DIRSYNC_FL)
197 ip->flags |= BTRFS_INODE_DIRSYNC;
198 else
199 ip->flags &= ~BTRFS_INODE_DIRSYNC;
200
201
202 trans = btrfs_join_transaction(root, 1);
203 BUG_ON(!trans);
204
205 ret = btrfs_update_inode(trans, root, inode);
206 BUG_ON(ret);
207
208 btrfs_update_iflags(inode);
209 inode->i_ctime = CURRENT_TIME;
210 btrfs_end_transaction(trans, root);
211
212 mnt_drop_write(file->f_path.mnt);
213 out_unlock:
214 mutex_unlock(&inode->i_mutex);
215 return 0;
216}
217
218static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
219{
220 struct inode *inode = file->f_path.dentry->d_inode;
221
222 return put_user(inode->i_generation, arg);
223}
f46b5a66 224
cb8e7090
CH
225static noinline int create_subvol(struct btrfs_root *root,
226 struct dentry *dentry,
72fd032e
SW
227 char *name, int namelen,
228 u64 *async_transid)
f46b5a66
CH
229{
230 struct btrfs_trans_handle *trans;
231 struct btrfs_key key;
232 struct btrfs_root_item root_item;
233 struct btrfs_inode_item *inode_item;
234 struct extent_buffer *leaf;
76dda93c
YZ
235 struct btrfs_root *new_root;
236 struct inode *dir = dentry->d_parent->d_inode;
f46b5a66
CH
237 int ret;
238 int err;
239 u64 objectid;
240 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3de4586c 241 u64 index = 0;
f46b5a66 242
a22285a6
YZ
243 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
244 0, &objectid);
245 if (ret)
246 return ret;
9ed74f2d
JB
247 /*
248 * 1 - inode item
249 * 2 - refs
250 * 1 - root item
251 * 2 - dir items
252 */
a22285a6
YZ
253 trans = btrfs_start_transaction(root, 6);
254 if (IS_ERR(trans))
255 return PTR_ERR(trans);
f46b5a66 256
5d4f98a2
YZ
257 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
258 0, objectid, NULL, 0, 0, 0);
8e8a1e31
JB
259 if (IS_ERR(leaf)) {
260 ret = PTR_ERR(leaf);
261 goto fail;
262 }
f46b5a66 263
5d4f98a2 264 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
f46b5a66
CH
265 btrfs_set_header_bytenr(leaf, leaf->start);
266 btrfs_set_header_generation(leaf, trans->transid);
5d4f98a2 267 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
f46b5a66
CH
268 btrfs_set_header_owner(leaf, objectid);
269
270 write_extent_buffer(leaf, root->fs_info->fsid,
271 (unsigned long)btrfs_header_fsid(leaf),
272 BTRFS_FSID_SIZE);
5d4f98a2
YZ
273 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
274 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
275 BTRFS_UUID_SIZE);
f46b5a66
CH
276 btrfs_mark_buffer_dirty(leaf);
277
278 inode_item = &root_item.inode;
279 memset(inode_item, 0, sizeof(*inode_item));
280 inode_item->generation = cpu_to_le64(1);
281 inode_item->size = cpu_to_le64(3);
282 inode_item->nlink = cpu_to_le32(1);
a76a3cd4 283 inode_item->nbytes = cpu_to_le64(root->leafsize);
f46b5a66
CH
284 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
285
286 btrfs_set_root_bytenr(&root_item, leaf->start);
84234f3a 287 btrfs_set_root_generation(&root_item, trans->transid);
f46b5a66
CH
288 btrfs_set_root_level(&root_item, 0);
289 btrfs_set_root_refs(&root_item, 1);
86b9f2ec 290 btrfs_set_root_used(&root_item, leaf->len);
80ff3856 291 btrfs_set_root_last_snapshot(&root_item, 0);
f46b5a66
CH
292
293 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
294 root_item.drop_level = 0;
295
925baedd 296 btrfs_tree_unlock(leaf);
f46b5a66
CH
297 free_extent_buffer(leaf);
298 leaf = NULL;
299
300 btrfs_set_root_dirid(&root_item, new_dirid);
301
302 key.objectid = objectid;
5d4f98a2 303 key.offset = 0;
f46b5a66
CH
304 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
305 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
306 &root_item);
307 if (ret)
308 goto fail;
309
76dda93c
YZ
310 key.offset = (u64)-1;
311 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
312 BUG_ON(IS_ERR(new_root));
313
314 btrfs_record_root_in_trans(trans, new_root);
315
316 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
317 BTRFS_I(dir)->block_group);
f46b5a66
CH
318 /*
319 * insert the directory item
320 */
3de4586c
CM
321 ret = btrfs_set_inode_index(dir, &index);
322 BUG_ON(ret);
323
324 ret = btrfs_insert_dir_item(trans, root,
f46b5a66 325 name, namelen, dir->i_ino, &key,
3de4586c 326 BTRFS_FT_DIR, index);
f46b5a66
CH
327 if (ret)
328 goto fail;
0660b5af 329
52c26179
YZ
330 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
331 ret = btrfs_update_inode(trans, root, dir);
332 BUG_ON(ret);
333
0660b5af 334 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4df27c4d 335 objectid, root->root_key.objectid,
0660b5af 336 dir->i_ino, index, name, namelen);
0660b5af 337
76dda93c 338 BUG_ON(ret);
f46b5a66 339
76dda93c 340 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
f46b5a66 341fail:
72fd032e
SW
342 if (async_transid) {
343 *async_transid = trans->transid;
344 err = btrfs_commit_transaction_async(trans, root, 1);
345 } else {
346 err = btrfs_commit_transaction(trans, root);
347 }
f46b5a66
CH
348 if (err && !ret)
349 ret = err;
f46b5a66
CH
350 return ret;
351}
352
72fd032e
SW
353static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
354 char *name, int namelen, u64 *async_transid)
f46b5a66 355{
2e4bfab9 356 struct inode *inode;
f46b5a66
CH
357 struct btrfs_pending_snapshot *pending_snapshot;
358 struct btrfs_trans_handle *trans;
2e4bfab9 359 int ret;
f46b5a66
CH
360
361 if (!root->ref_cows)
362 return -EINVAL;
363
3de4586c 364 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
a22285a6
YZ
365 if (!pending_snapshot)
366 return -ENOMEM;
367
368 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
3de4586c 369 pending_snapshot->dentry = dentry;
f46b5a66 370 pending_snapshot->root = root;
a22285a6
YZ
371
372 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
373 if (IS_ERR(trans)) {
374 ret = PTR_ERR(trans);
375 goto fail;
376 }
377
378 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
379 BUG_ON(ret);
380
f46b5a66
CH
381 list_add(&pending_snapshot->list,
382 &trans->transaction->pending_snapshots);
72fd032e
SW
383 if (async_transid) {
384 *async_transid = trans->transid;
385 ret = btrfs_commit_transaction_async(trans,
386 root->fs_info->extent_root, 1);
387 } else {
388 ret = btrfs_commit_transaction(trans,
389 root->fs_info->extent_root);
390 }
2e4bfab9 391 BUG_ON(ret);
a22285a6
YZ
392
393 ret = pending_snapshot->error;
394 if (ret)
395 goto fail;
396
397 btrfs_orphan_cleanup(pending_snapshot->snap);
f46b5a66 398
2e4bfab9
YZ
399 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
400 if (IS_ERR(inode)) {
401 ret = PTR_ERR(inode);
402 goto fail;
403 }
404 BUG_ON(!inode);
405 d_instantiate(dentry, inode);
406 ret = 0;
407fail:
a22285a6 408 kfree(pending_snapshot);
f46b5a66
CH
409 return ret;
410}
411
cb8e7090
CH
412/* copy of may_create in fs/namei.c() */
413static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
414{
415 if (child->d_inode)
416 return -EEXIST;
417 if (IS_DEADDIR(dir))
418 return -ENOENT;
419 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
420}
421
422/*
423 * Create a new subvolume below @parent. This is largely modeled after
424 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
425 * inside this filesystem so it's quite a bit simpler.
426 */
76dda93c
YZ
427static noinline int btrfs_mksubvol(struct path *parent,
428 char *name, int namelen,
72fd032e
SW
429 struct btrfs_root *snap_src,
430 u64 *async_transid)
cb8e7090 431{
76dda93c 432 struct inode *dir = parent->dentry->d_inode;
cb8e7090
CH
433 struct dentry *dentry;
434 int error;
435
76dda93c 436 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
cb8e7090
CH
437
438 dentry = lookup_one_len(name, parent->dentry, namelen);
439 error = PTR_ERR(dentry);
440 if (IS_ERR(dentry))
441 goto out_unlock;
442
443 error = -EEXIST;
444 if (dentry->d_inode)
445 goto out_dput;
446
cb8e7090
CH
447 error = mnt_want_write(parent->mnt);
448 if (error)
449 goto out_dput;
450
76dda93c 451 error = btrfs_may_create(dir, dentry);
cb8e7090
CH
452 if (error)
453 goto out_drop_write;
454
76dda93c
YZ
455 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
456
457 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
458 goto out_up_read;
459
3de4586c 460 if (snap_src) {
72fd032e
SW
461 error = create_snapshot(snap_src, dentry,
462 name, namelen, async_transid);
3de4586c 463 } else {
76dda93c 464 error = create_subvol(BTRFS_I(dir)->root, dentry,
72fd032e 465 name, namelen, async_transid);
3de4586c 466 }
76dda93c
YZ
467 if (!error)
468 fsnotify_mkdir(dir, dentry);
469out_up_read:
470 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
cb8e7090
CH
471out_drop_write:
472 mnt_drop_write(parent->mnt);
473out_dput:
474 dput(dentry);
475out_unlock:
76dda93c 476 mutex_unlock(&dir->i_mutex);
cb8e7090
CH
477 return error;
478}
479
940100a4 480static int should_defrag_range(struct inode *inode, u64 start, u64 len,
1e701a32
CM
481 int thresh, u64 *last_len, u64 *skip,
482 u64 *defrag_end)
940100a4
CM
483{
484 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
485 struct extent_map *em = NULL;
486 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
487 int ret = 1;
488
1e701a32
CM
489
490 if (thresh == 0)
491 thresh = 256 * 1024;
492
940100a4
CM
493 /*
494 * make sure that once we start defragging and extent, we keep on
495 * defragging it
496 */
497 if (start < *defrag_end)
498 return 1;
499
500 *skip = 0;
501
502 /*
503 * hopefully we have this extent in the tree already, try without
504 * the full extent lock
505 */
506 read_lock(&em_tree->lock);
507 em = lookup_extent_mapping(em_tree, start, len);
508 read_unlock(&em_tree->lock);
509
510 if (!em) {
511 /* get the big lock and read metadata off disk */
512 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
513 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
514 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
515
6cf8bfbf 516 if (IS_ERR(em))
940100a4
CM
517 return 0;
518 }
519
520 /* this will cover holes, and inline extents */
521 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
522 ret = 0;
523
524 /*
525 * we hit a real extent, if it is big don't bother defragging it again
526 */
1e701a32 527 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
940100a4
CM
528 ret = 0;
529
530 /*
531 * last_len ends up being a counter of how many bytes we've defragged.
532 * every time we choose not to defrag an extent, we reset *last_len
533 * so that the next tiny extent will force a defrag.
534 *
535 * The end result of this is that tiny extents before a single big
536 * extent will force at least part of that big extent to be defragged.
537 */
538 if (ret) {
539 *last_len += len;
540 *defrag_end = extent_map_end(em);
541 } else {
542 *last_len = 0;
543 *skip = extent_map_end(em);
544 *defrag_end = 0;
545 }
546
547 free_extent_map(em);
548 return ret;
549}
550
1e701a32
CM
551static int btrfs_defrag_file(struct file *file,
552 struct btrfs_ioctl_defrag_range_args *range)
f46b5a66
CH
553{
554 struct inode *inode = fdentry(file)->d_inode;
555 struct btrfs_root *root = BTRFS_I(inode)->root;
556 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3eaa2885 557 struct btrfs_ordered_extent *ordered;
f46b5a66
CH
558 struct page *page;
559 unsigned long last_index;
560 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
561 unsigned long total_read = 0;
562 u64 page_start;
563 u64 page_end;
940100a4
CM
564 u64 last_len = 0;
565 u64 skip = 0;
566 u64 defrag_end = 0;
f46b5a66
CH
567 unsigned long i;
568 int ret;
569
940100a4
CM
570 if (inode->i_size == 0)
571 return 0;
572
1e701a32
CM
573 if (range->start + range->len > range->start) {
574 last_index = min_t(u64, inode->i_size - 1,
575 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
576 } else {
577 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
578 }
579
580 i = range->start >> PAGE_CACHE_SHIFT;
940100a4
CM
581 while (i <= last_index) {
582 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1e701a32
CM
583 PAGE_CACHE_SIZE,
584 range->extent_thresh,
585 &last_len, &skip,
940100a4
CM
586 &defrag_end)) {
587 unsigned long next;
588 /*
589 * the should_defrag function tells us how much to skip
590 * bump our counter by the suggested amount
591 */
592 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
593 i = max(i + 1, next);
594 continue;
595 }
f46b5a66 596
f46b5a66
CH
597 if (total_read % ra_pages == 0) {
598 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
599 min(last_index, i + ra_pages - 1));
600 }
601 total_read++;
940100a4 602 mutex_lock(&inode->i_mutex);
1e701a32
CM
603 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
604 BTRFS_I(inode)->force_compress = 1;
940100a4 605
0ca1f7ce
YZ
606 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
607 if (ret)
608 goto err_unlock;
3eaa2885 609again:
940100a4
CM
610 if (inode->i_size == 0 ||
611 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
612 ret = 0;
613 goto err_reservations;
614 }
615
f46b5a66 616 page = grab_cache_page(inode->i_mapping, i);
0ca1f7ce
YZ
617 if (!page) {
618 ret = -ENOMEM;
940100a4 619 goto err_reservations;
0ca1f7ce 620 }
940100a4 621
f46b5a66
CH
622 if (!PageUptodate(page)) {
623 btrfs_readpage(NULL, page);
624 lock_page(page);
625 if (!PageUptodate(page)) {
626 unlock_page(page);
627 page_cache_release(page);
0ca1f7ce 628 ret = -EIO;
940100a4 629 goto err_reservations;
f46b5a66
CH
630 }
631 }
632
940100a4
CM
633 if (page->mapping != inode->i_mapping) {
634 unlock_page(page);
635 page_cache_release(page);
636 goto again;
637 }
638
f46b5a66 639 wait_on_page_writeback(page);
f46b5a66 640
940100a4 641 if (PageDirty(page)) {
0ca1f7ce 642 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
940100a4
CM
643 goto loop_unlock;
644 }
645
f46b5a66
CH
646 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
647 page_end = page_start + PAGE_CACHE_SIZE - 1;
f46b5a66 648 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3eaa2885
CM
649
650 ordered = btrfs_lookup_ordered_extent(inode, page_start);
651 if (ordered) {
652 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
653 unlock_page(page);
654 page_cache_release(page);
655 btrfs_start_ordered_extent(inode, ordered, 1);
656 btrfs_put_ordered_extent(ordered);
657 goto again;
658 }
659 set_page_extent_mapped(page);
660
f87f057b
CM
661 /*
662 * this makes sure page_mkwrite is called on the
663 * page if it is dirtied again later
664 */
665 clear_page_dirty_for_io(page);
940100a4
CM
666 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
667 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
668 EXTENT_DO_ACCOUNTING, GFP_NOFS);
f87f057b 669
2ac55d41 670 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
940100a4 671 ClearPageChecked(page);
f46b5a66 672 set_page_dirty(page);
a1ed835e 673 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
940100a4
CM
674
675loop_unlock:
f46b5a66
CH
676 unlock_page(page);
677 page_cache_release(page);
940100a4
CM
678 mutex_unlock(&inode->i_mutex);
679
f46b5a66 680 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
940100a4 681 i++;
f46b5a66
CH
682 }
683
1e701a32
CM
684 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
685 filemap_flush(inode->i_mapping);
686
687 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
688 /* the filemap_flush will queue IO into the worker threads, but
689 * we have to make sure the IO is actually started and that
690 * ordered extents get created before we return
691 */
692 atomic_inc(&root->fs_info->async_submit_draining);
693 while (atomic_read(&root->fs_info->nr_async_submits) ||
694 atomic_read(&root->fs_info->async_delalloc_pages)) {
695 wait_event(root->fs_info->async_submit_wait,
696 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
697 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
698 }
699 atomic_dec(&root->fs_info->async_submit_draining);
700
701 mutex_lock(&inode->i_mutex);
702 BTRFS_I(inode)->force_compress = 0;
703 mutex_unlock(&inode->i_mutex);
704 }
705
f46b5a66 706 return 0;
940100a4
CM
707
708err_reservations:
0ca1f7ce
YZ
709 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
710err_unlock:
940100a4 711 mutex_unlock(&inode->i_mutex);
940100a4 712 return ret;
f46b5a66
CH
713}
714
76dda93c
YZ
715static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
716 void __user *arg)
f46b5a66
CH
717{
718 u64 new_size;
719 u64 old_size;
720 u64 devid = 1;
721 struct btrfs_ioctl_vol_args *vol_args;
722 struct btrfs_trans_handle *trans;
723 struct btrfs_device *device = NULL;
724 char *sizestr;
725 char *devstr = NULL;
726 int ret = 0;
f46b5a66
CH
727 int mod = 0;
728
c146afad
YZ
729 if (root->fs_info->sb->s_flags & MS_RDONLY)
730 return -EROFS;
731
e441d54d
CM
732 if (!capable(CAP_SYS_ADMIN))
733 return -EPERM;
734
dae7b665
LZ
735 vol_args = memdup_user(arg, sizeof(*vol_args));
736 if (IS_ERR(vol_args))
737 return PTR_ERR(vol_args);
5516e595
MF
738
739 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 740
7d9eb12c 741 mutex_lock(&root->fs_info->volume_mutex);
f46b5a66
CH
742 sizestr = vol_args->name;
743 devstr = strchr(sizestr, ':');
744 if (devstr) {
745 char *end;
746 sizestr = devstr + 1;
747 *devstr = '\0';
748 devstr = vol_args->name;
749 devid = simple_strtoull(devstr, &end, 10);
21380931
JB
750 printk(KERN_INFO "resizing devid %llu\n",
751 (unsigned long long)devid);
f46b5a66 752 }
2b82032c 753 device = btrfs_find_device(root, devid, NULL, NULL);
f46b5a66 754 if (!device) {
21380931
JB
755 printk(KERN_INFO "resizer unable to find device %llu\n",
756 (unsigned long long)devid);
f46b5a66
CH
757 ret = -EINVAL;
758 goto out_unlock;
759 }
760 if (!strcmp(sizestr, "max"))
761 new_size = device->bdev->bd_inode->i_size;
762 else {
763 if (sizestr[0] == '-') {
764 mod = -1;
765 sizestr++;
766 } else if (sizestr[0] == '+') {
767 mod = 1;
768 sizestr++;
769 }
91748467 770 new_size = memparse(sizestr, NULL);
f46b5a66
CH
771 if (new_size == 0) {
772 ret = -EINVAL;
773 goto out_unlock;
774 }
775 }
776
777 old_size = device->total_bytes;
778
779 if (mod < 0) {
780 if (new_size > old_size) {
781 ret = -EINVAL;
782 goto out_unlock;
783 }
784 new_size = old_size - new_size;
785 } else if (mod > 0) {
786 new_size = old_size + new_size;
787 }
788
789 if (new_size < 256 * 1024 * 1024) {
790 ret = -EINVAL;
791 goto out_unlock;
792 }
793 if (new_size > device->bdev->bd_inode->i_size) {
794 ret = -EFBIG;
795 goto out_unlock;
796 }
797
798 do_div(new_size, root->sectorsize);
799 new_size *= root->sectorsize;
800
801 printk(KERN_INFO "new size for %s is %llu\n",
802 device->name, (unsigned long long)new_size);
803
804 if (new_size > old_size) {
a22285a6 805 trans = btrfs_start_transaction(root, 0);
f46b5a66
CH
806 ret = btrfs_grow_device(trans, device, new_size);
807 btrfs_commit_transaction(trans, root);
808 } else {
809 ret = btrfs_shrink_device(device, new_size);
810 }
811
812out_unlock:
7d9eb12c 813 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
814 kfree(vol_args);
815 return ret;
816}
817
72fd032e
SW
818static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
819 char *name,
820 unsigned long fd,
821 int subvol,
822 u64 *transid)
f46b5a66 823{
cb8e7090 824 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3de4586c 825 struct file *src_file;
f46b5a66 826 int namelen;
3de4586c 827 int ret = 0;
f46b5a66 828
c146afad
YZ
829 if (root->fs_info->sb->s_flags & MS_RDONLY)
830 return -EROFS;
831
72fd032e
SW
832 namelen = strlen(name);
833 if (strchr(name, '/')) {
f46b5a66
CH
834 ret = -EINVAL;
835 goto out;
836 }
837
3de4586c 838 if (subvol) {
72fd032e
SW
839 ret = btrfs_mksubvol(&file->f_path, name, namelen,
840 NULL, transid);
cb8e7090 841 } else {
3de4586c 842 struct inode *src_inode;
72fd032e 843 src_file = fget(fd);
3de4586c
CM
844 if (!src_file) {
845 ret = -EINVAL;
846 goto out;
847 }
848
849 src_inode = src_file->f_path.dentry->d_inode;
850 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
d397712b
CM
851 printk(KERN_INFO "btrfs: Snapshot src from "
852 "another FS\n");
3de4586c
CM
853 ret = -EINVAL;
854 fput(src_file);
855 goto out;
856 }
72fd032e
SW
857 ret = btrfs_mksubvol(&file->f_path, name, namelen,
858 BTRFS_I(src_inode)->root,
859 transid);
3de4586c 860 fput(src_file);
cb8e7090 861 }
f46b5a66 862out:
72fd032e
SW
863 return ret;
864}
865
866static noinline int btrfs_ioctl_snap_create(struct file *file,
867 void __user *arg, int subvol,
868 int async)
869{
870 struct btrfs_ioctl_vol_args *vol_args = NULL;
871 struct btrfs_ioctl_async_vol_args *async_vol_args = NULL;
872 char *name;
873 u64 fd;
874 u64 transid = 0;
875 int ret;
876
877 if (async) {
878 async_vol_args = memdup_user(arg, sizeof(*async_vol_args));
879 if (IS_ERR(async_vol_args))
880 return PTR_ERR(async_vol_args);
881
882 name = async_vol_args->name;
883 fd = async_vol_args->fd;
884 async_vol_args->name[BTRFS_SNAPSHOT_NAME_MAX] = '\0';
885 } else {
886 vol_args = memdup_user(arg, sizeof(*vol_args));
887 if (IS_ERR(vol_args))
888 return PTR_ERR(vol_args);
889 name = vol_args->name;
890 fd = vol_args->fd;
891 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
892 }
893
894 ret = btrfs_ioctl_snap_create_transid(file, name, fd,
895 subvol, &transid);
896
897 if (!ret && async) {
898 if (copy_to_user(arg +
899 offsetof(struct btrfs_ioctl_async_vol_args,
900 transid), &transid, sizeof(transid)))
901 return -EFAULT;
902 }
903
f46b5a66 904 kfree(vol_args);
72fd032e
SW
905 kfree(async_vol_args);
906
f46b5a66
CH
907 return ret;
908}
909
76dda93c
YZ
910/*
911 * helper to check if the subvolume references other subvolumes
912 */
913static noinline int may_destroy_subvol(struct btrfs_root *root)
914{
915 struct btrfs_path *path;
916 struct btrfs_key key;
917 int ret;
918
919 path = btrfs_alloc_path();
920 if (!path)
921 return -ENOMEM;
922
923 key.objectid = root->root_key.objectid;
924 key.type = BTRFS_ROOT_REF_KEY;
925 key.offset = (u64)-1;
926
927 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
928 &key, path, 0, 0);
929 if (ret < 0)
930 goto out;
931 BUG_ON(ret == 0);
932
933 ret = 0;
934 if (path->slots[0] > 0) {
935 path->slots[0]--;
936 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
937 if (key.objectid == root->root_key.objectid &&
938 key.type == BTRFS_ROOT_REF_KEY)
939 ret = -ENOTEMPTY;
940 }
941out:
942 btrfs_free_path(path);
943 return ret;
944}
945
ac8e9819
CM
946static noinline int key_in_sk(struct btrfs_key *key,
947 struct btrfs_ioctl_search_key *sk)
948{
abc6e134
CM
949 struct btrfs_key test;
950 int ret;
951
952 test.objectid = sk->min_objectid;
953 test.type = sk->min_type;
954 test.offset = sk->min_offset;
955
956 ret = btrfs_comp_cpu_keys(key, &test);
957 if (ret < 0)
ac8e9819 958 return 0;
abc6e134
CM
959
960 test.objectid = sk->max_objectid;
961 test.type = sk->max_type;
962 test.offset = sk->max_offset;
963
964 ret = btrfs_comp_cpu_keys(key, &test);
965 if (ret > 0)
ac8e9819
CM
966 return 0;
967 return 1;
968}
969
970static noinline int copy_to_sk(struct btrfs_root *root,
971 struct btrfs_path *path,
972 struct btrfs_key *key,
973 struct btrfs_ioctl_search_key *sk,
974 char *buf,
975 unsigned long *sk_offset,
976 int *num_found)
977{
978 u64 found_transid;
979 struct extent_buffer *leaf;
980 struct btrfs_ioctl_search_header sh;
981 unsigned long item_off;
982 unsigned long item_len;
983 int nritems;
984 int i;
985 int slot;
986 int found = 0;
987 int ret = 0;
988
989 leaf = path->nodes[0];
990 slot = path->slots[0];
991 nritems = btrfs_header_nritems(leaf);
992
993 if (btrfs_header_generation(leaf) > sk->max_transid) {
994 i = nritems;
995 goto advance_key;
996 }
997 found_transid = btrfs_header_generation(leaf);
998
999 for (i = slot; i < nritems; i++) {
1000 item_off = btrfs_item_ptr_offset(leaf, i);
1001 item_len = btrfs_item_size_nr(leaf, i);
1002
1003 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1004 item_len = 0;
1005
1006 if (sizeof(sh) + item_len + *sk_offset >
1007 BTRFS_SEARCH_ARGS_BUFSIZE) {
1008 ret = 1;
1009 goto overflow;
1010 }
1011
1012 btrfs_item_key_to_cpu(leaf, key, i);
1013 if (!key_in_sk(key, sk))
1014 continue;
1015
1016 sh.objectid = key->objectid;
1017 sh.offset = key->offset;
1018 sh.type = key->type;
1019 sh.len = item_len;
1020 sh.transid = found_transid;
1021
1022 /* copy search result header */
1023 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1024 *sk_offset += sizeof(sh);
1025
1026 if (item_len) {
1027 char *p = buf + *sk_offset;
1028 /* copy the item */
1029 read_extent_buffer(leaf, p,
1030 item_off, item_len);
1031 *sk_offset += item_len;
ac8e9819 1032 }
90fdde14 1033 found++;
ac8e9819
CM
1034
1035 if (*num_found >= sk->nr_items)
1036 break;
1037 }
1038advance_key:
abc6e134
CM
1039 ret = 0;
1040 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
ac8e9819 1041 key->offset++;
abc6e134
CM
1042 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1043 key->offset = 0;
ac8e9819 1044 key->type++;
abc6e134
CM
1045 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1046 key->offset = 0;
1047 key->type = 0;
ac8e9819 1048 key->objectid++;
abc6e134
CM
1049 } else
1050 ret = 1;
ac8e9819
CM
1051overflow:
1052 *num_found += found;
1053 return ret;
1054}
1055
1056static noinline int search_ioctl(struct inode *inode,
1057 struct btrfs_ioctl_search_args *args)
1058{
1059 struct btrfs_root *root;
1060 struct btrfs_key key;
1061 struct btrfs_key max_key;
1062 struct btrfs_path *path;
1063 struct btrfs_ioctl_search_key *sk = &args->key;
1064 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1065 int ret;
1066 int num_found = 0;
1067 unsigned long sk_offset = 0;
1068
1069 path = btrfs_alloc_path();
1070 if (!path)
1071 return -ENOMEM;
1072
1073 if (sk->tree_id == 0) {
1074 /* search the root of the inode that was passed */
1075 root = BTRFS_I(inode)->root;
1076 } else {
1077 key.objectid = sk->tree_id;
1078 key.type = BTRFS_ROOT_ITEM_KEY;
1079 key.offset = (u64)-1;
1080 root = btrfs_read_fs_root_no_name(info, &key);
1081 if (IS_ERR(root)) {
1082 printk(KERN_ERR "could not find root %llu\n",
1083 sk->tree_id);
1084 btrfs_free_path(path);
1085 return -ENOENT;
1086 }
1087 }
1088
1089 key.objectid = sk->min_objectid;
1090 key.type = sk->min_type;
1091 key.offset = sk->min_offset;
1092
1093 max_key.objectid = sk->max_objectid;
1094 max_key.type = sk->max_type;
1095 max_key.offset = sk->max_offset;
1096
1097 path->keep_locks = 1;
1098
1099 while(1) {
1100 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1101 sk->min_transid);
1102 if (ret != 0) {
1103 if (ret > 0)
1104 ret = 0;
1105 goto err;
1106 }
1107 ret = copy_to_sk(root, path, &key, sk, args->buf,
1108 &sk_offset, &num_found);
1109 btrfs_release_path(root, path);
1110 if (ret || num_found >= sk->nr_items)
1111 break;
1112
1113 }
1114 ret = 0;
1115err:
1116 sk->nr_items = num_found;
1117 btrfs_free_path(path);
1118 return ret;
1119}
1120
1121static noinline int btrfs_ioctl_tree_search(struct file *file,
1122 void __user *argp)
1123{
1124 struct btrfs_ioctl_search_args *args;
1125 struct inode *inode;
1126 int ret;
1127
1128 if (!capable(CAP_SYS_ADMIN))
1129 return -EPERM;
1130
2354d08f
JL
1131 args = memdup_user(argp, sizeof(*args));
1132 if (IS_ERR(args))
1133 return PTR_ERR(args);
ac8e9819 1134
ac8e9819
CM
1135 inode = fdentry(file)->d_inode;
1136 ret = search_ioctl(inode, args);
1137 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1138 ret = -EFAULT;
1139 kfree(args);
1140 return ret;
1141}
1142
98d377a0 1143/*
ac8e9819
CM
1144 * Search INODE_REFs to identify path name of 'dirid' directory
1145 * in a 'tree_id' tree. and sets path name to 'name'.
1146 */
98d377a0
TH
1147static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1148 u64 tree_id, u64 dirid, char *name)
1149{
1150 struct btrfs_root *root;
1151 struct btrfs_key key;
ac8e9819 1152 char *ptr;
98d377a0
TH
1153 int ret = -1;
1154 int slot;
1155 int len;
1156 int total_len = 0;
1157 struct btrfs_inode_ref *iref;
1158 struct extent_buffer *l;
1159 struct btrfs_path *path;
1160
1161 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1162 name[0]='\0';
1163 return 0;
1164 }
1165
1166 path = btrfs_alloc_path();
1167 if (!path)
1168 return -ENOMEM;
1169
ac8e9819 1170 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
98d377a0
TH
1171
1172 key.objectid = tree_id;
1173 key.type = BTRFS_ROOT_ITEM_KEY;
1174 key.offset = (u64)-1;
1175 root = btrfs_read_fs_root_no_name(info, &key);
1176 if (IS_ERR(root)) {
1177 printk(KERN_ERR "could not find root %llu\n", tree_id);
8ad6fcab
CM
1178 ret = -ENOENT;
1179 goto out;
98d377a0
TH
1180 }
1181
1182 key.objectid = dirid;
1183 key.type = BTRFS_INODE_REF_KEY;
8ad6fcab 1184 key.offset = (u64)-1;
98d377a0
TH
1185
1186 while(1) {
1187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1188 if (ret < 0)
1189 goto out;
1190
1191 l = path->nodes[0];
1192 slot = path->slots[0];
8ad6fcab
CM
1193 if (ret > 0 && slot > 0)
1194 slot--;
98d377a0
TH
1195 btrfs_item_key_to_cpu(l, &key, slot);
1196
1197 if (ret > 0 && (key.objectid != dirid ||
ac8e9819
CM
1198 key.type != BTRFS_INODE_REF_KEY)) {
1199 ret = -ENOENT;
98d377a0 1200 goto out;
ac8e9819 1201 }
98d377a0
TH
1202
1203 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1204 len = btrfs_inode_ref_name_len(l, iref);
1205 ptr -= len + 1;
1206 total_len += len + 1;
ac8e9819 1207 if (ptr < name)
98d377a0
TH
1208 goto out;
1209
1210 *(ptr + len) = '/';
1211 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1212
1213 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1214 break;
1215
1216 btrfs_release_path(root, path);
1217 key.objectid = key.offset;
8ad6fcab 1218 key.offset = (u64)-1;
98d377a0
TH
1219 dirid = key.objectid;
1220
1221 }
ac8e9819 1222 if (ptr < name)
98d377a0 1223 goto out;
ac8e9819 1224 memcpy(name, ptr, total_len);
98d377a0
TH
1225 name[total_len]='\0';
1226 ret = 0;
1227out:
1228 btrfs_free_path(path);
ac8e9819
CM
1229 return ret;
1230}
1231
1232static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1233 void __user *argp)
1234{
1235 struct btrfs_ioctl_ino_lookup_args *args;
1236 struct inode *inode;
1237 int ret;
1238
1239 if (!capable(CAP_SYS_ADMIN))
1240 return -EPERM;
1241
2354d08f
JL
1242 args = memdup_user(argp, sizeof(*args));
1243 if (IS_ERR(args))
1244 return PTR_ERR(args);
c2b96929 1245
ac8e9819
CM
1246 inode = fdentry(file)->d_inode;
1247
1b53ac4d
CM
1248 if (args->treeid == 0)
1249 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1250
ac8e9819
CM
1251 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1252 args->treeid, args->objectid,
1253 args->name);
1254
1255 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1256 ret = -EFAULT;
1257
1258 kfree(args);
98d377a0
TH
1259 return ret;
1260}
1261
76dda93c
YZ
1262static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1263 void __user *arg)
1264{
1265 struct dentry *parent = fdentry(file);
1266 struct dentry *dentry;
1267 struct inode *dir = parent->d_inode;
1268 struct inode *inode;
1269 struct btrfs_root *root = BTRFS_I(dir)->root;
1270 struct btrfs_root *dest = NULL;
1271 struct btrfs_ioctl_vol_args *vol_args;
1272 struct btrfs_trans_handle *trans;
1273 int namelen;
1274 int ret;
1275 int err = 0;
1276
1277 if (!capable(CAP_SYS_ADMIN))
1278 return -EPERM;
1279
1280 vol_args = memdup_user(arg, sizeof(*vol_args));
1281 if (IS_ERR(vol_args))
1282 return PTR_ERR(vol_args);
1283
1284 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1285 namelen = strlen(vol_args->name);
1286 if (strchr(vol_args->name, '/') ||
1287 strncmp(vol_args->name, "..", namelen) == 0) {
1288 err = -EINVAL;
1289 goto out;
1290 }
1291
1292 err = mnt_want_write(file->f_path.mnt);
1293 if (err)
1294 goto out;
1295
1296 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1297 dentry = lookup_one_len(vol_args->name, parent, namelen);
1298 if (IS_ERR(dentry)) {
1299 err = PTR_ERR(dentry);
1300 goto out_unlock_dir;
1301 }
1302
1303 if (!dentry->d_inode) {
1304 err = -ENOENT;
1305 goto out_dput;
1306 }
1307
1308 inode = dentry->d_inode;
1309 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1310 err = -EINVAL;
1311 goto out_dput;
1312 }
1313
1314 dest = BTRFS_I(inode)->root;
1315
1316 mutex_lock(&inode->i_mutex);
1317 err = d_invalidate(dentry);
1318 if (err)
1319 goto out_unlock;
1320
1321 down_write(&root->fs_info->subvol_sem);
1322
1323 err = may_destroy_subvol(dest);
1324 if (err)
1325 goto out_up_write;
1326
a22285a6
YZ
1327 trans = btrfs_start_transaction(root, 0);
1328 if (IS_ERR(trans)) {
1329 err = PTR_ERR(trans);
d327099a 1330 goto out_up_write;
a22285a6
YZ
1331 }
1332 trans->block_rsv = &root->fs_info->global_block_rsv;
1333
76dda93c
YZ
1334 ret = btrfs_unlink_subvol(trans, root, dir,
1335 dest->root_key.objectid,
1336 dentry->d_name.name,
1337 dentry->d_name.len);
1338 BUG_ON(ret);
1339
1340 btrfs_record_root_in_trans(trans, dest);
1341
1342 memset(&dest->root_item.drop_progress, 0,
1343 sizeof(dest->root_item.drop_progress));
1344 dest->root_item.drop_level = 0;
1345 btrfs_set_root_refs(&dest->root_item, 0);
1346
d68fc57b
YZ
1347 if (!xchg(&dest->orphan_item_inserted, 1)) {
1348 ret = btrfs_insert_orphan_item(trans,
1349 root->fs_info->tree_root,
1350 dest->root_key.objectid);
1351 BUG_ON(ret);
1352 }
76dda93c
YZ
1353
1354 ret = btrfs_commit_transaction(trans, root);
1355 BUG_ON(ret);
1356 inode->i_flags |= S_DEAD;
1357out_up_write:
1358 up_write(&root->fs_info->subvol_sem);
1359out_unlock:
1360 mutex_unlock(&inode->i_mutex);
1361 if (!err) {
efefb143 1362 shrink_dcache_sb(root->fs_info->sb);
76dda93c
YZ
1363 btrfs_invalidate_inodes(dest);
1364 d_delete(dentry);
1365 }
1366out_dput:
1367 dput(dentry);
1368out_unlock_dir:
1369 mutex_unlock(&dir->i_mutex);
1370 mnt_drop_write(file->f_path.mnt);
1371out:
1372 kfree(vol_args);
1373 return err;
1374}
1375
1e701a32 1376static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
f46b5a66
CH
1377{
1378 struct inode *inode = fdentry(file)->d_inode;
1379 struct btrfs_root *root = BTRFS_I(inode)->root;
1e701a32 1380 struct btrfs_ioctl_defrag_range_args *range;
c146afad
YZ
1381 int ret;
1382
1383 ret = mnt_want_write(file->f_path.mnt);
1384 if (ret)
1385 return ret;
f46b5a66
CH
1386
1387 switch (inode->i_mode & S_IFMT) {
1388 case S_IFDIR:
e441d54d
CM
1389 if (!capable(CAP_SYS_ADMIN)) {
1390 ret = -EPERM;
1391 goto out;
1392 }
8929ecfa
YZ
1393 ret = btrfs_defrag_root(root, 0);
1394 if (ret)
1395 goto out;
1396 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
f46b5a66
CH
1397 break;
1398 case S_IFREG:
e441d54d
CM
1399 if (!(file->f_mode & FMODE_WRITE)) {
1400 ret = -EINVAL;
1401 goto out;
1402 }
1e701a32
CM
1403
1404 range = kzalloc(sizeof(*range), GFP_KERNEL);
1405 if (!range) {
1406 ret = -ENOMEM;
1407 goto out;
1408 }
1409
1410 if (argp) {
1411 if (copy_from_user(range, argp,
1412 sizeof(*range))) {
1413 ret = -EFAULT;
1414 kfree(range);
683be16e 1415 goto out;
1e701a32
CM
1416 }
1417 /* compression requires us to start the IO */
1418 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1419 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1420 range->extent_thresh = (u32)-1;
1421 }
1422 } else {
1423 /* the rest are all set to zero by kzalloc */
1424 range->len = (u64)-1;
1425 }
8929ecfa 1426 ret = btrfs_defrag_file(file, range);
1e701a32 1427 kfree(range);
f46b5a66 1428 break;
8929ecfa
YZ
1429 default:
1430 ret = -EINVAL;
f46b5a66 1431 }
e441d54d 1432out:
ab67b7c1 1433 mnt_drop_write(file->f_path.mnt);
e441d54d 1434 return ret;
f46b5a66
CH
1435}
1436
b2950863 1437static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
1438{
1439 struct btrfs_ioctl_vol_args *vol_args;
1440 int ret;
1441
e441d54d
CM
1442 if (!capable(CAP_SYS_ADMIN))
1443 return -EPERM;
1444
dae7b665
LZ
1445 vol_args = memdup_user(arg, sizeof(*vol_args));
1446 if (IS_ERR(vol_args))
1447 return PTR_ERR(vol_args);
f46b5a66 1448
5516e595 1449 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
1450 ret = btrfs_init_new_device(root, vol_args->name);
1451
f46b5a66
CH
1452 kfree(vol_args);
1453 return ret;
1454}
1455
b2950863 1456static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
1457{
1458 struct btrfs_ioctl_vol_args *vol_args;
1459 int ret;
1460
e441d54d
CM
1461 if (!capable(CAP_SYS_ADMIN))
1462 return -EPERM;
1463
c146afad
YZ
1464 if (root->fs_info->sb->s_flags & MS_RDONLY)
1465 return -EROFS;
1466
dae7b665
LZ
1467 vol_args = memdup_user(arg, sizeof(*vol_args));
1468 if (IS_ERR(vol_args))
1469 return PTR_ERR(vol_args);
f46b5a66 1470
5516e595 1471 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
1472 ret = btrfs_rm_device(root, vol_args->name);
1473
f46b5a66
CH
1474 kfree(vol_args);
1475 return ret;
1476}
1477
76dda93c
YZ
1478static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1479 u64 off, u64 olen, u64 destoff)
f46b5a66
CH
1480{
1481 struct inode *inode = fdentry(file)->d_inode;
1482 struct btrfs_root *root = BTRFS_I(inode)->root;
1483 struct file *src_file;
1484 struct inode *src;
1485 struct btrfs_trans_handle *trans;
f46b5a66 1486 struct btrfs_path *path;
f46b5a66 1487 struct extent_buffer *leaf;
ae01a0ab
YZ
1488 char *buf;
1489 struct btrfs_key key;
f46b5a66
CH
1490 u32 nritems;
1491 int slot;
ae01a0ab 1492 int ret;
c5c9cd4d
SW
1493 u64 len = olen;
1494 u64 bs = root->fs_info->sb->s_blocksize;
1495 u64 hint_byte;
d20f7043 1496
c5c9cd4d
SW
1497 /*
1498 * TODO:
1499 * - split compressed inline extents. annoying: we need to
1500 * decompress into destination's address_space (the file offset
1501 * may change, so source mapping won't do), then recompress (or
1502 * otherwise reinsert) a subrange.
1503 * - allow ranges within the same file to be cloned (provided
1504 * they don't overlap)?
1505 */
1506
e441d54d 1507 /* the destination must be opened for writing */
2ebc3464 1508 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
e441d54d
CM
1509 return -EINVAL;
1510
c146afad
YZ
1511 ret = mnt_want_write(file->f_path.mnt);
1512 if (ret)
1513 return ret;
1514
c5c9cd4d 1515 src_file = fget(srcfd);
ab67b7c1
YZ
1516 if (!src_file) {
1517 ret = -EBADF;
1518 goto out_drop_write;
1519 }
5dc64164 1520
f46b5a66
CH
1521 src = src_file->f_dentry->d_inode;
1522
c5c9cd4d
SW
1523 ret = -EINVAL;
1524 if (src == inode)
1525 goto out_fput;
1526
5dc64164
DR
1527 /* the src must be open for reading */
1528 if (!(src_file->f_mode & FMODE_READ))
1529 goto out_fput;
1530
ae01a0ab
YZ
1531 ret = -EISDIR;
1532 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1533 goto out_fput;
1534
f46b5a66 1535 ret = -EXDEV;
ae01a0ab
YZ
1536 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1537 goto out_fput;
1538
1539 ret = -ENOMEM;
1540 buf = vmalloc(btrfs_level_size(root, 0));
1541 if (!buf)
1542 goto out_fput;
1543
1544 path = btrfs_alloc_path();
1545 if (!path) {
1546 vfree(buf);
f46b5a66 1547 goto out_fput;
ae01a0ab
YZ
1548 }
1549 path->reada = 2;
f46b5a66
CH
1550
1551 if (inode < src) {
fccdae43
SW
1552 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1553 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
f46b5a66 1554 } else {
fccdae43
SW
1555 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1556 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
f46b5a66
CH
1557 }
1558
c5c9cd4d
SW
1559 /* determine range to clone */
1560 ret = -EINVAL;
2ebc3464 1561 if (off + len > src->i_size || off + len < off)
f46b5a66 1562 goto out_unlock;
c5c9cd4d
SW
1563 if (len == 0)
1564 olen = len = src->i_size - off;
1565 /* if we extend to eof, continue to block boundary */
1566 if (off + len == src->i_size)
1567 len = ((src->i_size + bs-1) & ~(bs-1))
1568 - off;
1569
1570 /* verify the end result is block aligned */
1571 if ((off & (bs-1)) ||
1572 ((off + len) & (bs-1)))
1573 goto out_unlock;
1574
f46b5a66
CH
1575 /* do any pending delalloc/csum calc on src, one way or
1576 another, and lock file content */
1577 while (1) {
31840ae1 1578 struct btrfs_ordered_extent *ordered;
c5c9cd4d 1579 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
9a019196
SW
1580 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1581 if (!ordered &&
1582 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1583 EXTENT_DELALLOC, 0, NULL))
f46b5a66 1584 break;
c5c9cd4d 1585 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
ae01a0ab
YZ
1586 if (ordered)
1587 btrfs_put_ordered_extent(ordered);
9a019196 1588 btrfs_wait_ordered_range(src, off, len);
f46b5a66
CH
1589 }
1590
c5c9cd4d 1591 /* clone data */
f46b5a66 1592 key.objectid = src->i_ino;
ae01a0ab
YZ
1593 key.type = BTRFS_EXTENT_DATA_KEY;
1594 key.offset = 0;
f46b5a66
CH
1595
1596 while (1) {
1597 /*
1598 * note the key will change type as we walk through the
1599 * tree.
1600 */
a22285a6 1601 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
f46b5a66
CH
1602 if (ret < 0)
1603 goto out;
1604
ae01a0ab
YZ
1605 nritems = btrfs_header_nritems(path->nodes[0]);
1606 if (path->slots[0] >= nritems) {
f46b5a66
CH
1607 ret = btrfs_next_leaf(root, path);
1608 if (ret < 0)
1609 goto out;
1610 if (ret > 0)
1611 break;
ae01a0ab 1612 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
1613 }
1614 leaf = path->nodes[0];
1615 slot = path->slots[0];
f46b5a66 1616
ae01a0ab 1617 btrfs_item_key_to_cpu(leaf, &key, slot);
d20f7043 1618 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
f46b5a66
CH
1619 key.objectid != src->i_ino)
1620 break;
1621
c5c9cd4d
SW
1622 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1623 struct btrfs_file_extent_item *extent;
1624 int type;
31840ae1
ZY
1625 u32 size;
1626 struct btrfs_key new_key;
c5c9cd4d
SW
1627 u64 disko = 0, diskl = 0;
1628 u64 datao = 0, datal = 0;
1629 u8 comp;
b5384d48 1630 u64 endoff;
31840ae1
ZY
1631
1632 size = btrfs_item_size_nr(leaf, slot);
1633 read_extent_buffer(leaf, buf,
1634 btrfs_item_ptr_offset(leaf, slot),
1635 size);
c5c9cd4d
SW
1636
1637 extent = btrfs_item_ptr(leaf, slot,
1638 struct btrfs_file_extent_item);
1639 comp = btrfs_file_extent_compression(leaf, extent);
1640 type = btrfs_file_extent_type(leaf, extent);
c8a894d7
CM
1641 if (type == BTRFS_FILE_EXTENT_REG ||
1642 type == BTRFS_FILE_EXTENT_PREALLOC) {
d397712b
CM
1643 disko = btrfs_file_extent_disk_bytenr(leaf,
1644 extent);
1645 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1646 extent);
c5c9cd4d 1647 datao = btrfs_file_extent_offset(leaf, extent);
d397712b
CM
1648 datal = btrfs_file_extent_num_bytes(leaf,
1649 extent);
c5c9cd4d
SW
1650 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1651 /* take upper bound, may be compressed */
1652 datal = btrfs_file_extent_ram_bytes(leaf,
1653 extent);
1654 }
31840ae1
ZY
1655 btrfs_release_path(root, path);
1656
050006a7 1657 if (key.offset + datal <= off ||
c5c9cd4d
SW
1658 key.offset >= off+len)
1659 goto next;
1660
31840ae1
ZY
1661 memcpy(&new_key, &key, sizeof(new_key));
1662 new_key.objectid = inode->i_ino;
c5c9cd4d 1663 new_key.offset = key.offset + destoff - off;
31840ae1 1664
a22285a6
YZ
1665 trans = btrfs_start_transaction(root, 1);
1666 if (IS_ERR(trans)) {
1667 ret = PTR_ERR(trans);
1668 goto out;
1669 }
1670
c8a894d7
CM
1671 if (type == BTRFS_FILE_EXTENT_REG ||
1672 type == BTRFS_FILE_EXTENT_PREALLOC) {
a22285a6
YZ
1673 if (off > key.offset) {
1674 datao += off - key.offset;
1675 datal -= off - key.offset;
1676 }
1677
1678 if (key.offset + datal > off + len)
1679 datal = off + len - key.offset;
1680
1681 ret = btrfs_drop_extents(trans, inode,
1682 new_key.offset,
1683 new_key.offset + datal,
1684 &hint_byte, 1);
1685 BUG_ON(ret);
1686
c5c9cd4d
SW
1687 ret = btrfs_insert_empty_item(trans, root, path,
1688 &new_key, size);
a22285a6 1689 BUG_ON(ret);
c5c9cd4d
SW
1690
1691 leaf = path->nodes[0];
1692 slot = path->slots[0];
1693 write_extent_buffer(leaf, buf,
31840ae1
ZY
1694 btrfs_item_ptr_offset(leaf, slot),
1695 size);
ae01a0ab 1696
c5c9cd4d 1697 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 1698 struct btrfs_file_extent_item);
c5c9cd4d 1699
c5c9cd4d
SW
1700 /* disko == 0 means it's a hole */
1701 if (!disko)
1702 datao = 0;
c5c9cd4d
SW
1703
1704 btrfs_set_file_extent_offset(leaf, extent,
1705 datao);
1706 btrfs_set_file_extent_num_bytes(leaf, extent,
1707 datal);
1708 if (disko) {
1709 inode_add_bytes(inode, datal);
ae01a0ab 1710 ret = btrfs_inc_extent_ref(trans, root,
5d4f98a2
YZ
1711 disko, diskl, 0,
1712 root->root_key.objectid,
1713 inode->i_ino,
1714 new_key.offset - datao);
31840ae1 1715 BUG_ON(ret);
f46b5a66 1716 }
c5c9cd4d
SW
1717 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1718 u64 skip = 0;
1719 u64 trim = 0;
1720 if (off > key.offset) {
1721 skip = off - key.offset;
1722 new_key.offset += skip;
1723 }
d397712b 1724
c5c9cd4d
SW
1725 if (key.offset + datal > off+len)
1726 trim = key.offset + datal - (off+len);
d397712b 1727
c5c9cd4d 1728 if (comp && (skip || trim)) {
c5c9cd4d 1729 ret = -EINVAL;
a22285a6 1730 btrfs_end_transaction(trans, root);
c5c9cd4d
SW
1731 goto out;
1732 }
1733 size -= skip + trim;
1734 datal -= skip + trim;
a22285a6
YZ
1735
1736 ret = btrfs_drop_extents(trans, inode,
1737 new_key.offset,
1738 new_key.offset + datal,
1739 &hint_byte, 1);
1740 BUG_ON(ret);
1741
c5c9cd4d
SW
1742 ret = btrfs_insert_empty_item(trans, root, path,
1743 &new_key, size);
a22285a6 1744 BUG_ON(ret);
c5c9cd4d
SW
1745
1746 if (skip) {
d397712b
CM
1747 u32 start =
1748 btrfs_file_extent_calc_inline_size(0);
c5c9cd4d
SW
1749 memmove(buf+start, buf+start+skip,
1750 datal);
1751 }
1752
1753 leaf = path->nodes[0];
1754 slot = path->slots[0];
1755 write_extent_buffer(leaf, buf,
1756 btrfs_item_ptr_offset(leaf, slot),
1757 size);
1758 inode_add_bytes(inode, datal);
f46b5a66 1759 }
c5c9cd4d
SW
1760
1761 btrfs_mark_buffer_dirty(leaf);
a22285a6 1762 btrfs_release_path(root, path);
c5c9cd4d 1763
a22285a6 1764 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
b5384d48
SW
1765
1766 /*
1767 * we round up to the block size at eof when
1768 * determining which extents to clone above,
1769 * but shouldn't round up the file size
1770 */
1771 endoff = new_key.offset + datal;
1772 if (endoff > off+olen)
1773 endoff = off+olen;
1774 if (endoff > inode->i_size)
1775 btrfs_i_size_write(inode, endoff);
1776
a22285a6
YZ
1777 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1778 ret = btrfs_update_inode(trans, root, inode);
1779 BUG_ON(ret);
1780 btrfs_end_transaction(trans, root);
1781 }
d397712b 1782next:
31840ae1 1783 btrfs_release_path(root, path);
f46b5a66 1784 key.offset++;
f46b5a66 1785 }
f46b5a66
CH
1786 ret = 0;
1787out:
ae01a0ab 1788 btrfs_release_path(root, path);
c5c9cd4d 1789 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
f46b5a66
CH
1790out_unlock:
1791 mutex_unlock(&src->i_mutex);
1792 mutex_unlock(&inode->i_mutex);
ae01a0ab
YZ
1793 vfree(buf);
1794 btrfs_free_path(path);
f46b5a66
CH
1795out_fput:
1796 fput(src_file);
ab67b7c1
YZ
1797out_drop_write:
1798 mnt_drop_write(file->f_path.mnt);
f46b5a66
CH
1799 return ret;
1800}
1801
7a865e8a 1802static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
c5c9cd4d
SW
1803{
1804 struct btrfs_ioctl_clone_range_args args;
1805
7a865e8a 1806 if (copy_from_user(&args, argp, sizeof(args)))
c5c9cd4d
SW
1807 return -EFAULT;
1808 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1809 args.src_length, args.dest_offset);
1810}
1811
f46b5a66
CH
1812/*
1813 * there are many ways the trans_start and trans_end ioctls can lead
1814 * to deadlocks. They should only be used by applications that
1815 * basically own the machine, and have a very in depth understanding
1816 * of all the possible deadlocks and enospc problems.
1817 */
b2950863 1818static long btrfs_ioctl_trans_start(struct file *file)
f46b5a66
CH
1819{
1820 struct inode *inode = fdentry(file)->d_inode;
1821 struct btrfs_root *root = BTRFS_I(inode)->root;
1822 struct btrfs_trans_handle *trans;
1ab86aed 1823 int ret;
f46b5a66 1824
1ab86aed 1825 ret = -EPERM;
df5b5520 1826 if (!capable(CAP_SYS_ADMIN))
1ab86aed 1827 goto out;
df5b5520 1828
1ab86aed
SW
1829 ret = -EINPROGRESS;
1830 if (file->private_data)
f46b5a66 1831 goto out;
9ca9ee09 1832
c146afad
YZ
1833 ret = mnt_want_write(file->f_path.mnt);
1834 if (ret)
1835 goto out;
1836
9ca9ee09
SW
1837 mutex_lock(&root->fs_info->trans_mutex);
1838 root->fs_info->open_ioctl_trans++;
1839 mutex_unlock(&root->fs_info->trans_mutex);
1840
1ab86aed 1841 ret = -ENOMEM;
9ca9ee09 1842 trans = btrfs_start_ioctl_transaction(root, 0);
1ab86aed
SW
1843 if (!trans)
1844 goto out_drop;
1845
1846 file->private_data = trans;
1847 return 0;
1848
1849out_drop:
1850 mutex_lock(&root->fs_info->trans_mutex);
1851 root->fs_info->open_ioctl_trans--;
1852 mutex_unlock(&root->fs_info->trans_mutex);
1853 mnt_drop_write(file->f_path.mnt);
f46b5a66 1854out:
f46b5a66
CH
1855 return ret;
1856}
1857
6ef5ed0d
JB
1858static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1859{
1860 struct inode *inode = fdentry(file)->d_inode;
1861 struct btrfs_root *root = BTRFS_I(inode)->root;
1862 struct btrfs_root *new_root;
1863 struct btrfs_dir_item *di;
1864 struct btrfs_trans_handle *trans;
1865 struct btrfs_path *path;
1866 struct btrfs_key location;
1867 struct btrfs_disk_key disk_key;
1868 struct btrfs_super_block *disk_super;
1869 u64 features;
1870 u64 objectid = 0;
1871 u64 dir_id;
1872
1873 if (!capable(CAP_SYS_ADMIN))
1874 return -EPERM;
1875
1876 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1877 return -EFAULT;
1878
1879 if (!objectid)
1880 objectid = root->root_key.objectid;
1881
1882 location.objectid = objectid;
1883 location.type = BTRFS_ROOT_ITEM_KEY;
1884 location.offset = (u64)-1;
1885
1886 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1887 if (IS_ERR(new_root))
1888 return PTR_ERR(new_root);
1889
1890 if (btrfs_root_refs(&new_root->root_item) == 0)
1891 return -ENOENT;
1892
1893 path = btrfs_alloc_path();
1894 if (!path)
1895 return -ENOMEM;
1896 path->leave_spinning = 1;
1897
1898 trans = btrfs_start_transaction(root, 1);
1899 if (!trans) {
1900 btrfs_free_path(path);
1901 return -ENOMEM;
1902 }
1903
1904 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
1905 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
1906 dir_id, "default", 7, 1);
cf1e99a4 1907 if (IS_ERR_OR_NULL(di)) {
6ef5ed0d
JB
1908 btrfs_free_path(path);
1909 btrfs_end_transaction(trans, root);
1910 printk(KERN_ERR "Umm, you don't have the default dir item, "
1911 "this isn't going to work\n");
1912 return -ENOENT;
1913 }
1914
1915 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
1916 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
1917 btrfs_mark_buffer_dirty(path->nodes[0]);
1918 btrfs_free_path(path);
1919
1920 disk_super = &root->fs_info->super_copy;
1921 features = btrfs_super_incompat_flags(disk_super);
1922 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
1923 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
1924 btrfs_set_super_incompat_flags(disk_super, features);
1925 }
1926 btrfs_end_transaction(trans, root);
1927
1928 return 0;
1929}
1930
bf5fc093
JB
1931static void get_block_group_info(struct list_head *groups_list,
1932 struct btrfs_ioctl_space_info *space)
1933{
1934 struct btrfs_block_group_cache *block_group;
1935
1936 space->total_bytes = 0;
1937 space->used_bytes = 0;
1938 space->flags = 0;
1939 list_for_each_entry(block_group, groups_list, list) {
1940 space->flags = block_group->flags;
1941 space->total_bytes += block_group->key.offset;
1942 space->used_bytes +=
1943 btrfs_block_group_used(&block_group->item);
1944 }
1945}
1946
1406e432
JB
1947long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1948{
1949 struct btrfs_ioctl_space_args space_args;
1950 struct btrfs_ioctl_space_info space;
1951 struct btrfs_ioctl_space_info *dest;
7fde62bf
CM
1952 struct btrfs_ioctl_space_info *dest_orig;
1953 struct btrfs_ioctl_space_info *user_dest;
1406e432 1954 struct btrfs_space_info *info;
bf5fc093
JB
1955 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
1956 BTRFS_BLOCK_GROUP_SYSTEM,
1957 BTRFS_BLOCK_GROUP_METADATA,
1958 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
1959 int num_types = 4;
7fde62bf 1960 int alloc_size;
1406e432 1961 int ret = 0;
7fde62bf 1962 int slot_count = 0;
bf5fc093 1963 int i, c;
1406e432
JB
1964
1965 if (copy_from_user(&space_args,
1966 (struct btrfs_ioctl_space_args __user *)arg,
1967 sizeof(space_args)))
1968 return -EFAULT;
1969
bf5fc093
JB
1970 for (i = 0; i < num_types; i++) {
1971 struct btrfs_space_info *tmp;
1972
1973 info = NULL;
1974 rcu_read_lock();
1975 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
1976 list) {
1977 if (tmp->flags == types[i]) {
1978 info = tmp;
1979 break;
1980 }
1981 }
1982 rcu_read_unlock();
1983
1984 if (!info)
1985 continue;
1986
1987 down_read(&info->groups_sem);
1988 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
1989 if (!list_empty(&info->block_groups[c]))
1990 slot_count++;
1991 }
1992 up_read(&info->groups_sem);
1993 }
7fde62bf
CM
1994
1995 /* space_slots == 0 means they are asking for a count */
1996 if (space_args.space_slots == 0) {
1997 space_args.total_spaces = slot_count;
1998 goto out;
1999 }
bf5fc093
JB
2000
2001 slot_count = min_t(int, space_args.space_slots, slot_count);
2002
7fde62bf 2003 alloc_size = sizeof(*dest) * slot_count;
bf5fc093 2004
7fde62bf
CM
2005 /* we generally have at most 6 or so space infos, one for each raid
2006 * level. So, a whole page should be more than enough for everyone
2007 */
2008 if (alloc_size > PAGE_CACHE_SIZE)
2009 return -ENOMEM;
2010
1406e432 2011 space_args.total_spaces = 0;
7fde62bf
CM
2012 dest = kmalloc(alloc_size, GFP_NOFS);
2013 if (!dest)
2014 return -ENOMEM;
2015 dest_orig = dest;
1406e432 2016
7fde62bf 2017 /* now we have a buffer to copy into */
bf5fc093
JB
2018 for (i = 0; i < num_types; i++) {
2019 struct btrfs_space_info *tmp;
2020
2021 info = NULL;
2022 rcu_read_lock();
2023 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2024 list) {
2025 if (tmp->flags == types[i]) {
2026 info = tmp;
2027 break;
2028 }
2029 }
2030 rcu_read_unlock();
7fde62bf 2031
bf5fc093
JB
2032 if (!info)
2033 continue;
2034 down_read(&info->groups_sem);
2035 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2036 if (!list_empty(&info->block_groups[c])) {
2037 get_block_group_info(&info->block_groups[c],
2038 &space);
2039 memcpy(dest, &space, sizeof(space));
2040 dest++;
2041 space_args.total_spaces++;
2042 }
2043 }
2044 up_read(&info->groups_sem);
1406e432 2045 }
1406e432 2046
7fde62bf
CM
2047 user_dest = (struct btrfs_ioctl_space_info *)
2048 (arg + sizeof(struct btrfs_ioctl_space_args));
2049
2050 if (copy_to_user(user_dest, dest_orig, alloc_size))
2051 ret = -EFAULT;
2052
2053 kfree(dest_orig);
2054out:
2055 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1406e432
JB
2056 ret = -EFAULT;
2057
2058 return ret;
2059}
2060
f46b5a66
CH
2061/*
2062 * there are many ways the trans_start and trans_end ioctls can lead
2063 * to deadlocks. They should only be used by applications that
2064 * basically own the machine, and have a very in depth understanding
2065 * of all the possible deadlocks and enospc problems.
2066 */
2067long btrfs_ioctl_trans_end(struct file *file)
2068{
2069 struct inode *inode = fdentry(file)->d_inode;
2070 struct btrfs_root *root = BTRFS_I(inode)->root;
2071 struct btrfs_trans_handle *trans;
f46b5a66 2072
f46b5a66 2073 trans = file->private_data;
1ab86aed
SW
2074 if (!trans)
2075 return -EINVAL;
b214107e 2076 file->private_data = NULL;
9ca9ee09 2077
1ab86aed
SW
2078 btrfs_end_transaction(trans, root);
2079
9ca9ee09
SW
2080 mutex_lock(&root->fs_info->trans_mutex);
2081 root->fs_info->open_ioctl_trans--;
2082 mutex_unlock(&root->fs_info->trans_mutex);
2083
cfc8ea87 2084 mnt_drop_write(file->f_path.mnt);
1ab86aed 2085 return 0;
f46b5a66
CH
2086}
2087
46204592
SW
2088static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2089{
2090 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2091 struct btrfs_trans_handle *trans;
2092 u64 transid;
2093
2094 trans = btrfs_start_transaction(root, 0);
2095 transid = trans->transid;
2096 btrfs_commit_transaction_async(trans, root, 0);
2097
2098 if (argp)
2099 if (copy_to_user(argp, &transid, sizeof(transid)))
2100 return -EFAULT;
2101 return 0;
2102}
2103
2104static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2105{
2106 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2107 u64 transid;
2108
2109 if (argp) {
2110 if (copy_from_user(&transid, argp, sizeof(transid)))
2111 return -EFAULT;
2112 } else {
2113 transid = 0; /* current trans */
2114 }
2115 return btrfs_wait_for_commit(root, transid);
2116}
2117
f46b5a66
CH
2118long btrfs_ioctl(struct file *file, unsigned int
2119 cmd, unsigned long arg)
2120{
2121 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4bcabaa3 2122 void __user *argp = (void __user *)arg;
f46b5a66
CH
2123
2124 switch (cmd) {
6cbff00f
CH
2125 case FS_IOC_GETFLAGS:
2126 return btrfs_ioctl_getflags(file, argp);
2127 case FS_IOC_SETFLAGS:
2128 return btrfs_ioctl_setflags(file, argp);
2129 case FS_IOC_GETVERSION:
2130 return btrfs_ioctl_getversion(file, argp);
f46b5a66 2131 case BTRFS_IOC_SNAP_CREATE:
72fd032e
SW
2132 return btrfs_ioctl_snap_create(file, argp, 0, 0);
2133 case BTRFS_IOC_SNAP_CREATE_ASYNC:
2134 return btrfs_ioctl_snap_create(file, argp, 0, 1);
3de4586c 2135 case BTRFS_IOC_SUBVOL_CREATE:
72fd032e 2136 return btrfs_ioctl_snap_create(file, argp, 1, 0);
76dda93c
YZ
2137 case BTRFS_IOC_SNAP_DESTROY:
2138 return btrfs_ioctl_snap_destroy(file, argp);
6ef5ed0d
JB
2139 case BTRFS_IOC_DEFAULT_SUBVOL:
2140 return btrfs_ioctl_default_subvol(file, argp);
f46b5a66 2141 case BTRFS_IOC_DEFRAG:
1e701a32
CM
2142 return btrfs_ioctl_defrag(file, NULL);
2143 case BTRFS_IOC_DEFRAG_RANGE:
2144 return btrfs_ioctl_defrag(file, argp);
f46b5a66 2145 case BTRFS_IOC_RESIZE:
4bcabaa3 2146 return btrfs_ioctl_resize(root, argp);
f46b5a66 2147 case BTRFS_IOC_ADD_DEV:
4bcabaa3 2148 return btrfs_ioctl_add_dev(root, argp);
f46b5a66 2149 case BTRFS_IOC_RM_DEV:
4bcabaa3 2150 return btrfs_ioctl_rm_dev(root, argp);
f46b5a66
CH
2151 case BTRFS_IOC_BALANCE:
2152 return btrfs_balance(root->fs_info->dev_root);
2153 case BTRFS_IOC_CLONE:
c5c9cd4d
SW
2154 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2155 case BTRFS_IOC_CLONE_RANGE:
7a865e8a 2156 return btrfs_ioctl_clone_range(file, argp);
f46b5a66
CH
2157 case BTRFS_IOC_TRANS_START:
2158 return btrfs_ioctl_trans_start(file);
2159 case BTRFS_IOC_TRANS_END:
2160 return btrfs_ioctl_trans_end(file);
ac8e9819
CM
2161 case BTRFS_IOC_TREE_SEARCH:
2162 return btrfs_ioctl_tree_search(file, argp);
2163 case BTRFS_IOC_INO_LOOKUP:
2164 return btrfs_ioctl_ino_lookup(file, argp);
1406e432
JB
2165 case BTRFS_IOC_SPACE_INFO:
2166 return btrfs_ioctl_space_info(root, argp);
f46b5a66
CH
2167 case BTRFS_IOC_SYNC:
2168 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2169 return 0;
46204592
SW
2170 case BTRFS_IOC_START_SYNC:
2171 return btrfs_ioctl_start_sync(file, argp);
2172 case BTRFS_IOC_WAIT_SYNC:
2173 return btrfs_ioctl_wait_sync(file, argp);
f46b5a66
CH
2174 }
2175
2176 return -ENOTTY;
2177}