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