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