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