]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/btrfs/ioctl.c
Btrfs: add basic restriper infrastructure
[thirdparty/linux.git] / fs / btrfs / ioctl.c
CommitLineData
f46b5a66
CH
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
cb8e7090 24#include <linux/fsnotify.h>
f46b5a66
CH
25#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
f46b5a66 30#include <linux/backing-dev.h>
cb8e7090 31#include <linux/mount.h>
f46b5a66 32#include <linux/mpage.h>
cb8e7090 33#include <linux/namei.h>
f46b5a66
CH
34#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
cb8e7090 39#include <linux/security.h>
f46b5a66 40#include <linux/xattr.h>
7ea394f1 41#include <linux/vmalloc.h>
5a0e3ad6 42#include <linux/slab.h>
f7039b1d 43#include <linux/blkdev.h>
4b4e25f2 44#include "compat.h"
f46b5a66
CH
45#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
925baedd 52#include "locking.h"
581bb050 53#include "inode-map.h"
d7728c96 54#include "backref.h"
f46b5a66 55
6cbff00f
CH
56/* Mask out flags that are inappropriate for the given type of inode. */
57static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
58{
59 if (S_ISDIR(mode))
60 return flags;
61 else if (S_ISREG(mode))
62 return flags & ~FS_DIRSYNC_FL;
63 else
64 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65}
66
67/*
68 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
69 */
70static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
71{
72 unsigned int iflags = 0;
73
74 if (flags & BTRFS_INODE_SYNC)
75 iflags |= FS_SYNC_FL;
76 if (flags & BTRFS_INODE_IMMUTABLE)
77 iflags |= FS_IMMUTABLE_FL;
78 if (flags & BTRFS_INODE_APPEND)
79 iflags |= FS_APPEND_FL;
80 if (flags & BTRFS_INODE_NODUMP)
81 iflags |= FS_NODUMP_FL;
82 if (flags & BTRFS_INODE_NOATIME)
83 iflags |= FS_NOATIME_FL;
84 if (flags & BTRFS_INODE_DIRSYNC)
85 iflags |= FS_DIRSYNC_FL;
d0092bdd
LZ
86 if (flags & BTRFS_INODE_NODATACOW)
87 iflags |= FS_NOCOW_FL;
88
89 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
90 iflags |= FS_COMPR_FL;
91 else if (flags & BTRFS_INODE_NOCOMPRESS)
92 iflags |= FS_NOCOMP_FL;
6cbff00f
CH
93
94 return iflags;
95}
96
97/*
98 * Update inode->i_flags based on the btrfs internal flags.
99 */
100void btrfs_update_iflags(struct inode *inode)
101{
102 struct btrfs_inode *ip = BTRFS_I(inode);
103
104 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
105
106 if (ip->flags & BTRFS_INODE_SYNC)
107 inode->i_flags |= S_SYNC;
108 if (ip->flags & BTRFS_INODE_IMMUTABLE)
109 inode->i_flags |= S_IMMUTABLE;
110 if (ip->flags & BTRFS_INODE_APPEND)
111 inode->i_flags |= S_APPEND;
112 if (ip->flags & BTRFS_INODE_NOATIME)
113 inode->i_flags |= S_NOATIME;
114 if (ip->flags & BTRFS_INODE_DIRSYNC)
115 inode->i_flags |= S_DIRSYNC;
116}
117
118/*
119 * Inherit flags from the parent inode.
120 *
e27425d6 121 * Currently only the compression flags and the cow flags are inherited.
6cbff00f
CH
122 */
123void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
124{
0b4dcea5
CM
125 unsigned int flags;
126
127 if (!dir)
128 return;
129
130 flags = BTRFS_I(dir)->flags;
6cbff00f 131
e27425d6
JB
132 if (flags & BTRFS_INODE_NOCOMPRESS) {
133 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
134 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
135 } else if (flags & BTRFS_INODE_COMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
138 }
139
140 if (flags & BTRFS_INODE_NODATACOW)
141 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6cbff00f 142
6cbff00f
CH
143 btrfs_update_iflags(inode);
144}
145
146static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
147{
148 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
149 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
150
151 if (copy_to_user(arg, &flags, sizeof(flags)))
152 return -EFAULT;
153 return 0;
154}
155
75e7cb7f
LB
156static int check_flags(unsigned int flags)
157{
158 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
159 FS_NOATIME_FL | FS_NODUMP_FL | \
160 FS_SYNC_FL | FS_DIRSYNC_FL | \
e1e8fb6a
LZ
161 FS_NOCOMP_FL | FS_COMPR_FL |
162 FS_NOCOW_FL))
75e7cb7f
LB
163 return -EOPNOTSUPP;
164
165 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
166 return -EINVAL;
167
75e7cb7f
LB
168 return 0;
169}
170
6cbff00f
CH
171static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
172{
173 struct inode *inode = file->f_path.dentry->d_inode;
174 struct btrfs_inode *ip = BTRFS_I(inode);
175 struct btrfs_root *root = ip->root;
176 struct btrfs_trans_handle *trans;
177 unsigned int flags, oldflags;
178 int ret;
179
b83cc969
LZ
180 if (btrfs_root_readonly(root))
181 return -EROFS;
182
6cbff00f
CH
183 if (copy_from_user(&flags, arg, sizeof(flags)))
184 return -EFAULT;
185
75e7cb7f
LB
186 ret = check_flags(flags);
187 if (ret)
188 return ret;
f46b5a66 189
2e149670 190 if (!inode_owner_or_capable(inode))
6cbff00f
CH
191 return -EACCES;
192
193 mutex_lock(&inode->i_mutex);
194
195 flags = btrfs_mask_flags(inode->i_mode, flags);
196 oldflags = btrfs_flags_to_ioctl(ip->flags);
197 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
198 if (!capable(CAP_LINUX_IMMUTABLE)) {
199 ret = -EPERM;
200 goto out_unlock;
201 }
202 }
203
204 ret = mnt_want_write(file->f_path.mnt);
205 if (ret)
206 goto out_unlock;
207
208 if (flags & FS_SYNC_FL)
209 ip->flags |= BTRFS_INODE_SYNC;
210 else
211 ip->flags &= ~BTRFS_INODE_SYNC;
212 if (flags & FS_IMMUTABLE_FL)
213 ip->flags |= BTRFS_INODE_IMMUTABLE;
214 else
215 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
216 if (flags & FS_APPEND_FL)
217 ip->flags |= BTRFS_INODE_APPEND;
218 else
219 ip->flags &= ~BTRFS_INODE_APPEND;
220 if (flags & FS_NODUMP_FL)
221 ip->flags |= BTRFS_INODE_NODUMP;
222 else
223 ip->flags &= ~BTRFS_INODE_NODUMP;
224 if (flags & FS_NOATIME_FL)
225 ip->flags |= BTRFS_INODE_NOATIME;
226 else
227 ip->flags &= ~BTRFS_INODE_NOATIME;
228 if (flags & FS_DIRSYNC_FL)
229 ip->flags |= BTRFS_INODE_DIRSYNC;
230 else
231 ip->flags &= ~BTRFS_INODE_DIRSYNC;
e1e8fb6a
LZ
232 if (flags & FS_NOCOW_FL)
233 ip->flags |= BTRFS_INODE_NODATACOW;
234 else
235 ip->flags &= ~BTRFS_INODE_NODATACOW;
6cbff00f 236
75e7cb7f
LB
237 /*
238 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
239 * flag may be changed automatically if compression code won't make
240 * things smaller.
241 */
242 if (flags & FS_NOCOMP_FL) {
243 ip->flags &= ~BTRFS_INODE_COMPRESS;
244 ip->flags |= BTRFS_INODE_NOCOMPRESS;
245 } else if (flags & FS_COMPR_FL) {
246 ip->flags |= BTRFS_INODE_COMPRESS;
247 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
ebcb904d
LZ
248 } else {
249 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
75e7cb7f 250 }
6cbff00f 251
7a7eaa40 252 trans = btrfs_join_transaction(root);
3612b495 253 BUG_ON(IS_ERR(trans));
6cbff00f 254
306424cc
LZ
255 btrfs_update_iflags(inode);
256 inode->i_ctime = CURRENT_TIME;
6cbff00f
CH
257 ret = btrfs_update_inode(trans, root, inode);
258 BUG_ON(ret);
259
6cbff00f
CH
260 btrfs_end_transaction(trans, root);
261
262 mnt_drop_write(file->f_path.mnt);
2d4e6f6a 263
264 ret = 0;
6cbff00f
CH
265 out_unlock:
266 mutex_unlock(&inode->i_mutex);
2d4e6f6a 267 return ret;
6cbff00f
CH
268}
269
270static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
271{
272 struct inode *inode = file->f_path.dentry->d_inode;
273
274 return put_user(inode->i_generation, arg);
275}
f46b5a66 276
f7039b1d
LD
277static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
278{
279 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
280 struct btrfs_fs_info *fs_info = root->fs_info;
281 struct btrfs_device *device;
282 struct request_queue *q;
283 struct fstrim_range range;
284 u64 minlen = ULLONG_MAX;
285 u64 num_devices = 0;
6c41761f 286 u64 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
f7039b1d
LD
287 int ret;
288
289 if (!capable(CAP_SYS_ADMIN))
290 return -EPERM;
291
1f78160c
XG
292 rcu_read_lock();
293 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
294 dev_list) {
f7039b1d
LD
295 if (!device->bdev)
296 continue;
297 q = bdev_get_queue(device->bdev);
298 if (blk_queue_discard(q)) {
299 num_devices++;
300 minlen = min((u64)q->limits.discard_granularity,
301 minlen);
302 }
303 }
1f78160c 304 rcu_read_unlock();
f4c697e6 305
f7039b1d
LD
306 if (!num_devices)
307 return -EOPNOTSUPP;
f7039b1d
LD
308 if (copy_from_user(&range, arg, sizeof(range)))
309 return -EFAULT;
f4c697e6
LC
310 if (range.start > total_bytes)
311 return -EINVAL;
f7039b1d 312
f4c697e6 313 range.len = min(range.len, total_bytes - range.start);
f7039b1d
LD
314 range.minlen = max(range.minlen, minlen);
315 ret = btrfs_trim_fs(root, &range);
316 if (ret < 0)
317 return ret;
318
319 if (copy_to_user(arg, &range, sizeof(range)))
320 return -EFAULT;
321
322 return 0;
323}
324
cb8e7090
CH
325static noinline int create_subvol(struct btrfs_root *root,
326 struct dentry *dentry,
72fd032e
SW
327 char *name, int namelen,
328 u64 *async_transid)
f46b5a66
CH
329{
330 struct btrfs_trans_handle *trans;
331 struct btrfs_key key;
332 struct btrfs_root_item root_item;
333 struct btrfs_inode_item *inode_item;
334 struct extent_buffer *leaf;
76dda93c 335 struct btrfs_root *new_root;
2fbe8c8a 336 struct dentry *parent = dentry->d_parent;
6a912213 337 struct inode *dir;
f46b5a66
CH
338 int ret;
339 int err;
340 u64 objectid;
341 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3de4586c 342 u64 index = 0;
f46b5a66 343
581bb050 344 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
2fbe8c8a 345 if (ret)
a22285a6 346 return ret;
6a912213
JB
347
348 dir = parent->d_inode;
349
9ed74f2d
JB
350 /*
351 * 1 - inode item
352 * 2 - refs
353 * 1 - root item
354 * 2 - dir items
355 */
a22285a6 356 trans = btrfs_start_transaction(root, 6);
2fbe8c8a 357 if (IS_ERR(trans))
a22285a6 358 return PTR_ERR(trans);
f46b5a66 359
5d4f98a2
YZ
360 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
361 0, objectid, NULL, 0, 0, 0);
8e8a1e31
JB
362 if (IS_ERR(leaf)) {
363 ret = PTR_ERR(leaf);
364 goto fail;
365 }
f46b5a66 366
5d4f98a2 367 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
f46b5a66
CH
368 btrfs_set_header_bytenr(leaf, leaf->start);
369 btrfs_set_header_generation(leaf, trans->transid);
5d4f98a2 370 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
f46b5a66
CH
371 btrfs_set_header_owner(leaf, objectid);
372
373 write_extent_buffer(leaf, root->fs_info->fsid,
374 (unsigned long)btrfs_header_fsid(leaf),
375 BTRFS_FSID_SIZE);
5d4f98a2
YZ
376 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
377 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
378 BTRFS_UUID_SIZE);
f46b5a66
CH
379 btrfs_mark_buffer_dirty(leaf);
380
381 inode_item = &root_item.inode;
382 memset(inode_item, 0, sizeof(*inode_item));
383 inode_item->generation = cpu_to_le64(1);
384 inode_item->size = cpu_to_le64(3);
385 inode_item->nlink = cpu_to_le32(1);
a76a3cd4 386 inode_item->nbytes = cpu_to_le64(root->leafsize);
f46b5a66
CH
387 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
388
08fe4db1
LZ
389 root_item.flags = 0;
390 root_item.byte_limit = 0;
391 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
392
f46b5a66 393 btrfs_set_root_bytenr(&root_item, leaf->start);
84234f3a 394 btrfs_set_root_generation(&root_item, trans->transid);
f46b5a66
CH
395 btrfs_set_root_level(&root_item, 0);
396 btrfs_set_root_refs(&root_item, 1);
86b9f2ec 397 btrfs_set_root_used(&root_item, leaf->len);
80ff3856 398 btrfs_set_root_last_snapshot(&root_item, 0);
f46b5a66
CH
399
400 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
401 root_item.drop_level = 0;
402
925baedd 403 btrfs_tree_unlock(leaf);
f46b5a66
CH
404 free_extent_buffer(leaf);
405 leaf = NULL;
406
407 btrfs_set_root_dirid(&root_item, new_dirid);
408
409 key.objectid = objectid;
5d4f98a2 410 key.offset = 0;
f46b5a66
CH
411 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
412 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
413 &root_item);
414 if (ret)
415 goto fail;
416
76dda93c
YZ
417 key.offset = (u64)-1;
418 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
419 BUG_ON(IS_ERR(new_root));
420
421 btrfs_record_root_in_trans(trans, new_root);
422
d82a6f1d 423 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
f46b5a66
CH
424 /*
425 * insert the directory item
426 */
3de4586c
CM
427 ret = btrfs_set_inode_index(dir, &index);
428 BUG_ON(ret);
429
430 ret = btrfs_insert_dir_item(trans, root,
16cdcec7 431 name, namelen, dir, &key,
3de4586c 432 BTRFS_FT_DIR, index);
f46b5a66
CH
433 if (ret)
434 goto fail;
0660b5af 435
52c26179
YZ
436 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
437 ret = btrfs_update_inode(trans, root, dir);
438 BUG_ON(ret);
439
0660b5af 440 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4df27c4d 441 objectid, root->root_key.objectid,
33345d01 442 btrfs_ino(dir), index, name, namelen);
0660b5af 443
76dda93c 444 BUG_ON(ret);
f46b5a66 445
76dda93c 446 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
f46b5a66 447fail:
72fd032e
SW
448 if (async_transid) {
449 *async_transid = trans->transid;
450 err = btrfs_commit_transaction_async(trans, root, 1);
451 } else {
452 err = btrfs_commit_transaction(trans, root);
453 }
f46b5a66
CH
454 if (err && !ret)
455 ret = err;
f46b5a66
CH
456 return ret;
457}
458
72fd032e 459static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
b83cc969
LZ
460 char *name, int namelen, u64 *async_transid,
461 bool readonly)
f46b5a66 462{
2e4bfab9 463 struct inode *inode;
f46b5a66
CH
464 struct btrfs_pending_snapshot *pending_snapshot;
465 struct btrfs_trans_handle *trans;
2e4bfab9 466 int ret;
f46b5a66
CH
467
468 if (!root->ref_cows)
469 return -EINVAL;
470
3de4586c 471 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
a22285a6
YZ
472 if (!pending_snapshot)
473 return -ENOMEM;
474
475 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
3de4586c 476 pending_snapshot->dentry = dentry;
f46b5a66 477 pending_snapshot->root = root;
b83cc969 478 pending_snapshot->readonly = readonly;
a22285a6
YZ
479
480 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
481 if (IS_ERR(trans)) {
482 ret = PTR_ERR(trans);
483 goto fail;
484 }
485
486 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
487 BUG_ON(ret);
488
8351583e 489 spin_lock(&root->fs_info->trans_lock);
f46b5a66
CH
490 list_add(&pending_snapshot->list,
491 &trans->transaction->pending_snapshots);
8351583e 492 spin_unlock(&root->fs_info->trans_lock);
72fd032e
SW
493 if (async_transid) {
494 *async_transid = trans->transid;
495 ret = btrfs_commit_transaction_async(trans,
496 root->fs_info->extent_root, 1);
497 } else {
498 ret = btrfs_commit_transaction(trans,
499 root->fs_info->extent_root);
500 }
2e4bfab9 501 BUG_ON(ret);
a22285a6
YZ
502
503 ret = pending_snapshot->error;
504 if (ret)
505 goto fail;
506
66b4ffd1
JB
507 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
508 if (ret)
509 goto fail;
f46b5a66 510
2fbe8c8a 511 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
2e4bfab9
YZ
512 if (IS_ERR(inode)) {
513 ret = PTR_ERR(inode);
514 goto fail;
515 }
516 BUG_ON(!inode);
517 d_instantiate(dentry, inode);
518 ret = 0;
519fail:
a22285a6 520 kfree(pending_snapshot);
f46b5a66
CH
521 return ret;
522}
523
4260f7c7
SW
524/* copy of check_sticky in fs/namei.c()
525* It's inline, so penalty for filesystems that don't use sticky bit is
526* minimal.
527*/
528static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
529{
530 uid_t fsuid = current_fsuid();
531
532 if (!(dir->i_mode & S_ISVTX))
533 return 0;
534 if (inode->i_uid == fsuid)
535 return 0;
536 if (dir->i_uid == fsuid)
537 return 0;
538 return !capable(CAP_FOWNER);
539}
540
541/* copy of may_delete in fs/namei.c()
542 * Check whether we can remove a link victim from directory dir, check
543 * whether the type of victim is right.
544 * 1. We can't do it if dir is read-only (done in permission())
545 * 2. We should have write and exec permissions on dir
546 * 3. We can't remove anything from append-only dir
547 * 4. We can't do anything with immutable dir (done in permission())
548 * 5. If the sticky bit on dir is set we should either
549 * a. be owner of dir, or
550 * b. be owner of victim, or
551 * c. have CAP_FOWNER capability
552 * 6. If the victim is append-only or immutable we can't do antyhing with
553 * links pointing to it.
554 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
555 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
556 * 9. We can't remove a root or mountpoint.
557 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
558 * nfs_async_unlink().
559 */
560
561static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
562{
563 int error;
564
565 if (!victim->d_inode)
566 return -ENOENT;
567
568 BUG_ON(victim->d_parent->d_inode != dir);
569 audit_inode_child(victim, dir);
570
571 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
572 if (error)
573 return error;
574 if (IS_APPEND(dir))
575 return -EPERM;
576 if (btrfs_check_sticky(dir, victim->d_inode)||
577 IS_APPEND(victim->d_inode)||
578 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
579 return -EPERM;
580 if (isdir) {
581 if (!S_ISDIR(victim->d_inode->i_mode))
582 return -ENOTDIR;
583 if (IS_ROOT(victim))
584 return -EBUSY;
585 } else if (S_ISDIR(victim->d_inode->i_mode))
586 return -EISDIR;
587 if (IS_DEADDIR(dir))
588 return -ENOENT;
589 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
590 return -EBUSY;
591 return 0;
592}
593
cb8e7090
CH
594/* copy of may_create in fs/namei.c() */
595static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
596{
597 if (child->d_inode)
598 return -EEXIST;
599 if (IS_DEADDIR(dir))
600 return -ENOENT;
601 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
602}
603
604/*
605 * Create a new subvolume below @parent. This is largely modeled after
606 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
607 * inside this filesystem so it's quite a bit simpler.
608 */
76dda93c
YZ
609static noinline int btrfs_mksubvol(struct path *parent,
610 char *name, int namelen,
72fd032e 611 struct btrfs_root *snap_src,
b83cc969 612 u64 *async_transid, bool readonly)
cb8e7090 613{
76dda93c 614 struct inode *dir = parent->dentry->d_inode;
cb8e7090
CH
615 struct dentry *dentry;
616 int error;
617
76dda93c 618 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
cb8e7090
CH
619
620 dentry = lookup_one_len(name, parent->dentry, namelen);
621 error = PTR_ERR(dentry);
622 if (IS_ERR(dentry))
623 goto out_unlock;
624
625 error = -EEXIST;
626 if (dentry->d_inode)
627 goto out_dput;
628
cb8e7090
CH
629 error = mnt_want_write(parent->mnt);
630 if (error)
631 goto out_dput;
632
76dda93c 633 error = btrfs_may_create(dir, dentry);
cb8e7090
CH
634 if (error)
635 goto out_drop_write;
636
76dda93c
YZ
637 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
638
639 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
640 goto out_up_read;
641
3de4586c 642 if (snap_src) {
72fd032e 643 error = create_snapshot(snap_src, dentry,
b83cc969 644 name, namelen, async_transid, readonly);
3de4586c 645 } else {
76dda93c 646 error = create_subvol(BTRFS_I(dir)->root, dentry,
72fd032e 647 name, namelen, async_transid);
3de4586c 648 }
76dda93c
YZ
649 if (!error)
650 fsnotify_mkdir(dir, dentry);
651out_up_read:
652 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
cb8e7090
CH
653out_drop_write:
654 mnt_drop_write(parent->mnt);
655out_dput:
656 dput(dentry);
657out_unlock:
76dda93c 658 mutex_unlock(&dir->i_mutex);
cb8e7090
CH
659 return error;
660}
661
4cb5300b
CM
662/*
663 * When we're defragging a range, we don't want to kick it off again
664 * if it is really just waiting for delalloc to send it down.
665 * If we find a nice big extent or delalloc range for the bytes in the
666 * file you want to defrag, we return 0 to let you know to skip this
667 * part of the file
668 */
669static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
670{
671 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
672 struct extent_map *em = NULL;
673 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
674 u64 end;
675
676 read_lock(&em_tree->lock);
677 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
678 read_unlock(&em_tree->lock);
679
680 if (em) {
681 end = extent_map_end(em);
682 free_extent_map(em);
683 if (end - offset > thresh)
684 return 0;
685 }
686 /* if we already have a nice delalloc here, just stop */
687 thresh /= 2;
688 end = count_range_bits(io_tree, &offset, offset + thresh,
689 thresh, EXTENT_DELALLOC, 1);
690 if (end >= thresh)
691 return 0;
692 return 1;
693}
694
695/*
696 * helper function to walk through a file and find extents
697 * newer than a specific transid, and smaller than thresh.
698 *
699 * This is used by the defragging code to find new and small
700 * extents
701 */
702static int find_new_extents(struct btrfs_root *root,
703 struct inode *inode, u64 newer_than,
704 u64 *off, int thresh)
705{
706 struct btrfs_path *path;
707 struct btrfs_key min_key;
708 struct btrfs_key max_key;
709 struct extent_buffer *leaf;
710 struct btrfs_file_extent_item *extent;
711 int type;
712 int ret;
a4689d2b 713 u64 ino = btrfs_ino(inode);
4cb5300b
CM
714
715 path = btrfs_alloc_path();
716 if (!path)
717 return -ENOMEM;
718
a4689d2b 719 min_key.objectid = ino;
4cb5300b
CM
720 min_key.type = BTRFS_EXTENT_DATA_KEY;
721 min_key.offset = *off;
722
a4689d2b 723 max_key.objectid = ino;
4cb5300b
CM
724 max_key.type = (u8)-1;
725 max_key.offset = (u64)-1;
726
727 path->keep_locks = 1;
728
729 while(1) {
730 ret = btrfs_search_forward(root, &min_key, &max_key,
731 path, 0, newer_than);
732 if (ret != 0)
733 goto none;
a4689d2b 734 if (min_key.objectid != ino)
4cb5300b
CM
735 goto none;
736 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
737 goto none;
738
739 leaf = path->nodes[0];
740 extent = btrfs_item_ptr(leaf, path->slots[0],
741 struct btrfs_file_extent_item);
742
743 type = btrfs_file_extent_type(leaf, extent);
744 if (type == BTRFS_FILE_EXTENT_REG &&
745 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
746 check_defrag_in_cache(inode, min_key.offset, thresh)) {
747 *off = min_key.offset;
748 btrfs_free_path(path);
749 return 0;
750 }
751
752 if (min_key.offset == (u64)-1)
753 goto none;
754
755 min_key.offset++;
756 btrfs_release_path(path);
757 }
758none:
759 btrfs_free_path(path);
760 return -ENOENT;
761}
762
940100a4 763static int should_defrag_range(struct inode *inode, u64 start, u64 len,
1e701a32
CM
764 int thresh, u64 *last_len, u64 *skip,
765 u64 *defrag_end)
940100a4
CM
766{
767 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
768 struct extent_map *em = NULL;
769 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
770 int ret = 1;
771
772 /*
008873ea 773 * make sure that once we start defragging an extent, we keep on
940100a4
CM
774 * defragging it
775 */
776 if (start < *defrag_end)
777 return 1;
778
779 *skip = 0;
780
781 /*
782 * hopefully we have this extent in the tree already, try without
783 * the full extent lock
784 */
785 read_lock(&em_tree->lock);
786 em = lookup_extent_mapping(em_tree, start, len);
787 read_unlock(&em_tree->lock);
788
789 if (!em) {
790 /* get the big lock and read metadata off disk */
791 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
792 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
793 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
794
6cf8bfbf 795 if (IS_ERR(em))
940100a4
CM
796 return 0;
797 }
798
799 /* this will cover holes, and inline extents */
800 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
801 ret = 0;
802
803 /*
804 * we hit a real extent, if it is big don't bother defragging it again
805 */
1e701a32 806 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
940100a4
CM
807 ret = 0;
808
809 /*
810 * last_len ends up being a counter of how many bytes we've defragged.
811 * every time we choose not to defrag an extent, we reset *last_len
812 * so that the next tiny extent will force a defrag.
813 *
814 * The end result of this is that tiny extents before a single big
815 * extent will force at least part of that big extent to be defragged.
816 */
817 if (ret) {
940100a4
CM
818 *defrag_end = extent_map_end(em);
819 } else {
820 *last_len = 0;
821 *skip = extent_map_end(em);
822 *defrag_end = 0;
823 }
824
825 free_extent_map(em);
826 return ret;
827}
828
4cb5300b
CM
829/*
830 * it doesn't do much good to defrag one or two pages
831 * at a time. This pulls in a nice chunk of pages
832 * to COW and defrag.
833 *
834 * It also makes sure the delalloc code has enough
835 * dirty data to avoid making new small extents as part
836 * of the defrag
837 *
838 * It's a good idea to start RA on this range
839 * before calling this.
840 */
841static int cluster_pages_for_defrag(struct inode *inode,
842 struct page **pages,
843 unsigned long start_index,
844 int num_pages)
f46b5a66 845{
4cb5300b
CM
846 unsigned long file_end;
847 u64 isize = i_size_read(inode);
848 u64 page_start;
849 u64 page_end;
850 int ret;
851 int i;
852 int i_done;
3eaa2885 853 struct btrfs_ordered_extent *ordered;
4cb5300b 854 struct extent_state *cached_state = NULL;
3b16a4e3 855 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
4cb5300b
CM
856
857 if (isize == 0)
858 return 0;
859 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
860
660d3f6c 861 mutex_lock(&inode->i_mutex);
4cb5300b
CM
862 ret = btrfs_delalloc_reserve_space(inode,
863 num_pages << PAGE_CACHE_SHIFT);
660d3f6c 864 mutex_unlock(&inode->i_mutex);
4cb5300b
CM
865 if (ret)
866 return ret;
867again:
868 ret = 0;
869 i_done = 0;
870
871 /* step one, lock all the pages */
872 for (i = 0; i < num_pages; i++) {
873 struct page *page;
a94733d0 874 page = find_or_create_page(inode->i_mapping,
3b16a4e3 875 start_index + i, mask);
4cb5300b
CM
876 if (!page)
877 break;
878
879 if (!PageUptodate(page)) {
880 btrfs_readpage(NULL, page);
881 lock_page(page);
882 if (!PageUptodate(page)) {
883 unlock_page(page);
884 page_cache_release(page);
885 ret = -EIO;
886 break;
887 }
888 }
889 isize = i_size_read(inode);
890 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
891 if (!isize || page->index > file_end ||
892 page->mapping != inode->i_mapping) {
893 /* whoops, we blew past eof, skip this page */
894 unlock_page(page);
895 page_cache_release(page);
896 break;
897 }
898 pages[i] = page;
899 i_done++;
900 }
901 if (!i_done || ret)
902 goto out;
903
904 if (!(inode->i_sb->s_flags & MS_ACTIVE))
905 goto out;
906
907 /*
908 * so now we have a nice long stream of locked
909 * and up to date pages, lets wait on them
910 */
911 for (i = 0; i < i_done; i++)
912 wait_on_page_writeback(pages[i]);
913
914 page_start = page_offset(pages[0]);
915 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
916
917 lock_extent_bits(&BTRFS_I(inode)->io_tree,
918 page_start, page_end - 1, 0, &cached_state,
919 GFP_NOFS);
920 ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
921 if (ordered &&
922 ordered->file_offset + ordered->len > page_start &&
923 ordered->file_offset < page_end) {
924 btrfs_put_ordered_extent(ordered);
925 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
926 page_start, page_end - 1,
927 &cached_state, GFP_NOFS);
928 for (i = 0; i < i_done; i++) {
929 unlock_page(pages[i]);
930 page_cache_release(pages[i]);
931 }
932 btrfs_wait_ordered_range(inode, page_start,
933 page_end - page_start);
934 goto again;
935 }
936 if (ordered)
937 btrfs_put_ordered_extent(ordered);
938
939 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
940 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
941 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
942 GFP_NOFS);
943
944 if (i_done != num_pages) {
9e0baf60
JB
945 spin_lock(&BTRFS_I(inode)->lock);
946 BTRFS_I(inode)->outstanding_extents++;
947 spin_unlock(&BTRFS_I(inode)->lock);
4cb5300b
CM
948 btrfs_delalloc_release_space(inode,
949 (num_pages - i_done) << PAGE_CACHE_SHIFT);
950 }
951
952
953 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
954 &cached_state);
955
956 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
957 page_start, page_end - 1, &cached_state,
958 GFP_NOFS);
959
960 for (i = 0; i < i_done; i++) {
961 clear_page_dirty_for_io(pages[i]);
962 ClearPageChecked(pages[i]);
963 set_page_extent_mapped(pages[i]);
964 set_page_dirty(pages[i]);
965 unlock_page(pages[i]);
966 page_cache_release(pages[i]);
967 }
968 return i_done;
969out:
970 for (i = 0; i < i_done; i++) {
971 unlock_page(pages[i]);
972 page_cache_release(pages[i]);
973 }
974 btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
975 return ret;
976
977}
978
979int btrfs_defrag_file(struct inode *inode, struct file *file,
980 struct btrfs_ioctl_defrag_range_args *range,
981 u64 newer_than, unsigned long max_to_defrag)
982{
983 struct btrfs_root *root = BTRFS_I(inode)->root;
1a419d85 984 struct btrfs_super_block *disk_super;
4cb5300b 985 struct file_ra_state *ra = NULL;
f46b5a66 986 unsigned long last_index;
151a31b2 987 u64 isize = i_size_read(inode);
1a419d85 988 u64 features;
940100a4
CM
989 u64 last_len = 0;
990 u64 skip = 0;
991 u64 defrag_end = 0;
4cb5300b 992 u64 newer_off = range->start;
f46b5a66 993 unsigned long i;
008873ea 994 unsigned long ra_index = 0;
f46b5a66 995 int ret;
4cb5300b 996 int defrag_count = 0;
1a419d85 997 int compress_type = BTRFS_COMPRESS_ZLIB;
4cb5300b 998 int extent_thresh = range->extent_thresh;
008873ea
LZ
999 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1000 int cluster = max_cluster;
4cb5300b
CM
1001 u64 new_align = ~((u64)128 * 1024 - 1);
1002 struct page **pages = NULL;
1003
1004 if (extent_thresh == 0)
1005 extent_thresh = 256 * 1024;
1a419d85
LZ
1006
1007 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1008 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1009 return -EINVAL;
1010 if (range->compress_type)
1011 compress_type = range->compress_type;
1012 }
f46b5a66 1013
151a31b2 1014 if (isize == 0)
940100a4
CM
1015 return 0;
1016
4cb5300b
CM
1017 /*
1018 * if we were not given a file, allocate a readahead
1019 * context
1020 */
1021 if (!file) {
1022 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1023 if (!ra)
1024 return -ENOMEM;
1025 file_ra_state_init(ra, inode->i_mapping);
1026 } else {
1027 ra = &file->f_ra;
1028 }
1029
008873ea 1030 pages = kmalloc(sizeof(struct page *) * max_cluster,
4cb5300b
CM
1031 GFP_NOFS);
1032 if (!pages) {
1033 ret = -ENOMEM;
1034 goto out_ra;
1035 }
1036
1037 /* find the last page to defrag */
1e701a32 1038 if (range->start + range->len > range->start) {
151a31b2 1039 last_index = min_t(u64, isize - 1,
1e701a32
CM
1040 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1041 } else {
151a31b2 1042 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1e701a32
CM
1043 }
1044
4cb5300b
CM
1045 if (newer_than) {
1046 ret = find_new_extents(root, inode, newer_than,
1047 &newer_off, 64 * 1024);
1048 if (!ret) {
1049 range->start = newer_off;
1050 /*
1051 * we always align our defrag to help keep
1052 * the extents in the file evenly spaced
1053 */
1054 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
4cb5300b
CM
1055 } else
1056 goto out_ra;
1057 } else {
1058 i = range->start >> PAGE_CACHE_SHIFT;
1059 }
1060 if (!max_to_defrag)
5ca49660 1061 max_to_defrag = last_index;
4cb5300b 1062
2a0f7f57
LZ
1063 /*
1064 * make writeback starts from i, so the defrag range can be
1065 * written sequentially.
1066 */
1067 if (i < inode->i_mapping->writeback_index)
1068 inode->i_mapping->writeback_index = i;
1069
f7f43cc8
CM
1070 while (i <= last_index && defrag_count < max_to_defrag &&
1071 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1072 PAGE_CACHE_SHIFT)) {
4cb5300b
CM
1073 /*
1074 * make sure we stop running if someone unmounts
1075 * the FS
1076 */
1077 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1078 break;
1079
1080 if (!newer_than &&
1081 !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1e701a32 1082 PAGE_CACHE_SIZE,
4cb5300b 1083 extent_thresh,
1e701a32 1084 &last_len, &skip,
940100a4
CM
1085 &defrag_end)) {
1086 unsigned long next;
1087 /*
1088 * the should_defrag function tells us how much to skip
1089 * bump our counter by the suggested amount
1090 */
1091 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1092 i = max(i + 1, next);
1093 continue;
1094 }
008873ea
LZ
1095
1096 if (!newer_than) {
1097 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1098 PAGE_CACHE_SHIFT) - i;
1099 cluster = min(cluster, max_cluster);
1100 } else {
1101 cluster = max_cluster;
1102 }
1103
1e701a32 1104 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1a419d85 1105 BTRFS_I(inode)->force_compress = compress_type;
940100a4 1106
008873ea
LZ
1107 if (i + cluster > ra_index) {
1108 ra_index = max(i, ra_index);
1109 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1110 cluster);
1111 ra_index += max_cluster;
1112 }
940100a4 1113
008873ea 1114 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
4cb5300b
CM
1115 if (ret < 0)
1116 goto out_ra;
1117
1118 defrag_count += ret;
1119 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
4cb5300b
CM
1120
1121 if (newer_than) {
1122 if (newer_off == (u64)-1)
1123 break;
1124
1125 newer_off = max(newer_off + 1,
1126 (u64)i << PAGE_CACHE_SHIFT);
1127
1128 ret = find_new_extents(root, inode,
1129 newer_than, &newer_off,
1130 64 * 1024);
1131 if (!ret) {
1132 range->start = newer_off;
1133 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
4cb5300b
CM
1134 } else {
1135 break;
f46b5a66 1136 }
4cb5300b 1137 } else {
008873ea 1138 if (ret > 0) {
cbcc8326 1139 i += ret;
008873ea
LZ
1140 last_len += ret << PAGE_CACHE_SHIFT;
1141 } else {
cbcc8326 1142 i++;
008873ea
LZ
1143 last_len = 0;
1144 }
f46b5a66 1145 }
f46b5a66
CH
1146 }
1147
1e701a32
CM
1148 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1149 filemap_flush(inode->i_mapping);
1150
1151 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1152 /* the filemap_flush will queue IO into the worker threads, but
1153 * we have to make sure the IO is actually started and that
1154 * ordered extents get created before we return
1155 */
1156 atomic_inc(&root->fs_info->async_submit_draining);
1157 while (atomic_read(&root->fs_info->nr_async_submits) ||
1158 atomic_read(&root->fs_info->async_delalloc_pages)) {
1159 wait_event(root->fs_info->async_submit_wait,
1160 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1161 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1162 }
1163 atomic_dec(&root->fs_info->async_submit_draining);
1164
1165 mutex_lock(&inode->i_mutex);
261507a0 1166 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1e701a32
CM
1167 mutex_unlock(&inode->i_mutex);
1168 }
1169
6c41761f 1170 disk_super = root->fs_info->super_copy;
1a419d85
LZ
1171 features = btrfs_super_incompat_flags(disk_super);
1172 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1173 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1174 btrfs_set_super_incompat_flags(disk_super, features);
1175 }
1176
60ccf82f 1177 ret = defrag_count;
940100a4 1178
4cb5300b
CM
1179out_ra:
1180 if (!file)
1181 kfree(ra);
1182 kfree(pages);
940100a4 1183 return ret;
f46b5a66
CH
1184}
1185
76dda93c
YZ
1186static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1187 void __user *arg)
f46b5a66
CH
1188{
1189 u64 new_size;
1190 u64 old_size;
1191 u64 devid = 1;
1192 struct btrfs_ioctl_vol_args *vol_args;
1193 struct btrfs_trans_handle *trans;
1194 struct btrfs_device *device = NULL;
1195 char *sizestr;
1196 char *devstr = NULL;
1197 int ret = 0;
f46b5a66
CH
1198 int mod = 0;
1199
c146afad
YZ
1200 if (root->fs_info->sb->s_flags & MS_RDONLY)
1201 return -EROFS;
1202
e441d54d
CM
1203 if (!capable(CAP_SYS_ADMIN))
1204 return -EPERM;
1205
c9e9f97b
ID
1206 mutex_lock(&root->fs_info->volume_mutex);
1207 if (root->fs_info->balance_ctl) {
1208 printk(KERN_INFO "btrfs: balance in progress\n");
1209 ret = -EINVAL;
1210 goto out;
1211 }
1212
dae7b665 1213 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
1214 if (IS_ERR(vol_args)) {
1215 ret = PTR_ERR(vol_args);
1216 goto out;
1217 }
5516e595
MF
1218
1219 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 1220
f46b5a66
CH
1221 sizestr = vol_args->name;
1222 devstr = strchr(sizestr, ':');
1223 if (devstr) {
1224 char *end;
1225 sizestr = devstr + 1;
1226 *devstr = '\0';
1227 devstr = vol_args->name;
1228 devid = simple_strtoull(devstr, &end, 10);
5bb14682 1229 printk(KERN_INFO "btrfs: resizing devid %llu\n",
21380931 1230 (unsigned long long)devid);
f46b5a66 1231 }
2b82032c 1232 device = btrfs_find_device(root, devid, NULL, NULL);
f46b5a66 1233 if (!device) {
5bb14682 1234 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
21380931 1235 (unsigned long long)devid);
f46b5a66 1236 ret = -EINVAL;
c9e9f97b 1237 goto out_free;
f46b5a66
CH
1238 }
1239 if (!strcmp(sizestr, "max"))
1240 new_size = device->bdev->bd_inode->i_size;
1241 else {
1242 if (sizestr[0] == '-') {
1243 mod = -1;
1244 sizestr++;
1245 } else if (sizestr[0] == '+') {
1246 mod = 1;
1247 sizestr++;
1248 }
91748467 1249 new_size = memparse(sizestr, NULL);
f46b5a66
CH
1250 if (new_size == 0) {
1251 ret = -EINVAL;
c9e9f97b 1252 goto out_free;
f46b5a66
CH
1253 }
1254 }
1255
1256 old_size = device->total_bytes;
1257
1258 if (mod < 0) {
1259 if (new_size > old_size) {
1260 ret = -EINVAL;
c9e9f97b 1261 goto out_free;
f46b5a66
CH
1262 }
1263 new_size = old_size - new_size;
1264 } else if (mod > 0) {
1265 new_size = old_size + new_size;
1266 }
1267
1268 if (new_size < 256 * 1024 * 1024) {
1269 ret = -EINVAL;
c9e9f97b 1270 goto out_free;
f46b5a66
CH
1271 }
1272 if (new_size > device->bdev->bd_inode->i_size) {
1273 ret = -EFBIG;
c9e9f97b 1274 goto out_free;
f46b5a66
CH
1275 }
1276
1277 do_div(new_size, root->sectorsize);
1278 new_size *= root->sectorsize;
1279
5bb14682 1280 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
f46b5a66
CH
1281 device->name, (unsigned long long)new_size);
1282
1283 if (new_size > old_size) {
a22285a6 1284 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
1285 if (IS_ERR(trans)) {
1286 ret = PTR_ERR(trans);
c9e9f97b 1287 goto out_free;
98d5dc13 1288 }
f46b5a66
CH
1289 ret = btrfs_grow_device(trans, device, new_size);
1290 btrfs_commit_transaction(trans, root);
ece7d20e 1291 } else if (new_size < old_size) {
f46b5a66
CH
1292 ret = btrfs_shrink_device(device, new_size);
1293 }
1294
c9e9f97b 1295out_free:
f46b5a66 1296 kfree(vol_args);
c9e9f97b
ID
1297out:
1298 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
1299 return ret;
1300}
1301
72fd032e
SW
1302static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1303 char *name,
1304 unsigned long fd,
1305 int subvol,
b83cc969
LZ
1306 u64 *transid,
1307 bool readonly)
f46b5a66 1308{
cb8e7090 1309 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3de4586c 1310 struct file *src_file;
f46b5a66 1311 int namelen;
3de4586c 1312 int ret = 0;
f46b5a66 1313
c146afad
YZ
1314 if (root->fs_info->sb->s_flags & MS_RDONLY)
1315 return -EROFS;
1316
72fd032e
SW
1317 namelen = strlen(name);
1318 if (strchr(name, '/')) {
f46b5a66
CH
1319 ret = -EINVAL;
1320 goto out;
1321 }
1322
3de4586c 1323 if (subvol) {
72fd032e 1324 ret = btrfs_mksubvol(&file->f_path, name, namelen,
b83cc969 1325 NULL, transid, readonly);
cb8e7090 1326 } else {
3de4586c 1327 struct inode *src_inode;
72fd032e 1328 src_file = fget(fd);
3de4586c
CM
1329 if (!src_file) {
1330 ret = -EINVAL;
1331 goto out;
1332 }
1333
1334 src_inode = src_file->f_path.dentry->d_inode;
1335 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
d397712b
CM
1336 printk(KERN_INFO "btrfs: Snapshot src from "
1337 "another FS\n");
3de4586c
CM
1338 ret = -EINVAL;
1339 fput(src_file);
1340 goto out;
1341 }
72fd032e
SW
1342 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1343 BTRFS_I(src_inode)->root,
b83cc969 1344 transid, readonly);
3de4586c 1345 fput(src_file);
cb8e7090 1346 }
f46b5a66 1347out:
72fd032e
SW
1348 return ret;
1349}
1350
1351static noinline int btrfs_ioctl_snap_create(struct file *file,
fa0d2b9b 1352 void __user *arg, int subvol)
72fd032e 1353{
fa0d2b9b 1354 struct btrfs_ioctl_vol_args *vol_args;
72fd032e
SW
1355 int ret;
1356
fa0d2b9b
LZ
1357 vol_args = memdup_user(arg, sizeof(*vol_args));
1358 if (IS_ERR(vol_args))
1359 return PTR_ERR(vol_args);
1360 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
72fd032e 1361
fa0d2b9b 1362 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
b83cc969
LZ
1363 vol_args->fd, subvol,
1364 NULL, false);
fdfb1e4f 1365
fa0d2b9b
LZ
1366 kfree(vol_args);
1367 return ret;
1368}
fdfb1e4f 1369
fa0d2b9b
LZ
1370static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1371 void __user *arg, int subvol)
1372{
1373 struct btrfs_ioctl_vol_args_v2 *vol_args;
1374 int ret;
1375 u64 transid = 0;
1376 u64 *ptr = NULL;
b83cc969 1377 bool readonly = false;
75eaa0e2 1378
fa0d2b9b
LZ
1379 vol_args = memdup_user(arg, sizeof(*vol_args));
1380 if (IS_ERR(vol_args))
1381 return PTR_ERR(vol_args);
1382 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
75eaa0e2 1383
b83cc969
LZ
1384 if (vol_args->flags &
1385 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1386 ret = -EOPNOTSUPP;
fa0d2b9b 1387 goto out;
72fd032e 1388 }
fa0d2b9b
LZ
1389
1390 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1391 ptr = &transid;
b83cc969
LZ
1392 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1393 readonly = true;
fa0d2b9b
LZ
1394
1395 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
b83cc969
LZ
1396 vol_args->fd, subvol,
1397 ptr, readonly);
fa0d2b9b
LZ
1398
1399 if (ret == 0 && ptr &&
1400 copy_to_user(arg +
1401 offsetof(struct btrfs_ioctl_vol_args_v2,
1402 transid), ptr, sizeof(*ptr)))
1403 ret = -EFAULT;
fdfb1e4f 1404out:
f46b5a66
CH
1405 kfree(vol_args);
1406 return ret;
1407}
1408
0caa102d
LZ
1409static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1410 void __user *arg)
1411{
1412 struct inode *inode = fdentry(file)->d_inode;
1413 struct btrfs_root *root = BTRFS_I(inode)->root;
1414 int ret = 0;
1415 u64 flags = 0;
1416
33345d01 1417 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
0caa102d
LZ
1418 return -EINVAL;
1419
1420 down_read(&root->fs_info->subvol_sem);
1421 if (btrfs_root_readonly(root))
1422 flags |= BTRFS_SUBVOL_RDONLY;
1423 up_read(&root->fs_info->subvol_sem);
1424
1425 if (copy_to_user(arg, &flags, sizeof(flags)))
1426 ret = -EFAULT;
1427
1428 return ret;
1429}
1430
1431static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1432 void __user *arg)
1433{
1434 struct inode *inode = fdentry(file)->d_inode;
1435 struct btrfs_root *root = BTRFS_I(inode)->root;
1436 struct btrfs_trans_handle *trans;
1437 u64 root_flags;
1438 u64 flags;
1439 int ret = 0;
1440
1441 if (root->fs_info->sb->s_flags & MS_RDONLY)
1442 return -EROFS;
1443
33345d01 1444 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
0caa102d
LZ
1445 return -EINVAL;
1446
1447 if (copy_from_user(&flags, arg, sizeof(flags)))
1448 return -EFAULT;
1449
b4dc2b8c 1450 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
0caa102d
LZ
1451 return -EINVAL;
1452
1453 if (flags & ~BTRFS_SUBVOL_RDONLY)
1454 return -EOPNOTSUPP;
1455
2e149670 1456 if (!inode_owner_or_capable(inode))
b4dc2b8c
LZ
1457 return -EACCES;
1458
0caa102d
LZ
1459 down_write(&root->fs_info->subvol_sem);
1460
1461 /* nothing to do */
1462 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1463 goto out;
1464
1465 root_flags = btrfs_root_flags(&root->root_item);
1466 if (flags & BTRFS_SUBVOL_RDONLY)
1467 btrfs_set_root_flags(&root->root_item,
1468 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1469 else
1470 btrfs_set_root_flags(&root->root_item,
1471 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1472
1473 trans = btrfs_start_transaction(root, 1);
1474 if (IS_ERR(trans)) {
1475 ret = PTR_ERR(trans);
1476 goto out_reset;
1477 }
1478
b4dc2b8c 1479 ret = btrfs_update_root(trans, root->fs_info->tree_root,
0caa102d
LZ
1480 &root->root_key, &root->root_item);
1481
1482 btrfs_commit_transaction(trans, root);
1483out_reset:
1484 if (ret)
1485 btrfs_set_root_flags(&root->root_item, root_flags);
1486out:
1487 up_write(&root->fs_info->subvol_sem);
1488 return ret;
1489}
1490
76dda93c
YZ
1491/*
1492 * helper to check if the subvolume references other subvolumes
1493 */
1494static noinline int may_destroy_subvol(struct btrfs_root *root)
1495{
1496 struct btrfs_path *path;
1497 struct btrfs_key key;
1498 int ret;
1499
1500 path = btrfs_alloc_path();
1501 if (!path)
1502 return -ENOMEM;
1503
1504 key.objectid = root->root_key.objectid;
1505 key.type = BTRFS_ROOT_REF_KEY;
1506 key.offset = (u64)-1;
1507
1508 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1509 &key, path, 0, 0);
1510 if (ret < 0)
1511 goto out;
1512 BUG_ON(ret == 0);
1513
1514 ret = 0;
1515 if (path->slots[0] > 0) {
1516 path->slots[0]--;
1517 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1518 if (key.objectid == root->root_key.objectid &&
1519 key.type == BTRFS_ROOT_REF_KEY)
1520 ret = -ENOTEMPTY;
1521 }
1522out:
1523 btrfs_free_path(path);
1524 return ret;
1525}
1526
ac8e9819
CM
1527static noinline int key_in_sk(struct btrfs_key *key,
1528 struct btrfs_ioctl_search_key *sk)
1529{
abc6e134
CM
1530 struct btrfs_key test;
1531 int ret;
1532
1533 test.objectid = sk->min_objectid;
1534 test.type = sk->min_type;
1535 test.offset = sk->min_offset;
1536
1537 ret = btrfs_comp_cpu_keys(key, &test);
1538 if (ret < 0)
ac8e9819 1539 return 0;
abc6e134
CM
1540
1541 test.objectid = sk->max_objectid;
1542 test.type = sk->max_type;
1543 test.offset = sk->max_offset;
1544
1545 ret = btrfs_comp_cpu_keys(key, &test);
1546 if (ret > 0)
ac8e9819
CM
1547 return 0;
1548 return 1;
1549}
1550
1551static noinline int copy_to_sk(struct btrfs_root *root,
1552 struct btrfs_path *path,
1553 struct btrfs_key *key,
1554 struct btrfs_ioctl_search_key *sk,
1555 char *buf,
1556 unsigned long *sk_offset,
1557 int *num_found)
1558{
1559 u64 found_transid;
1560 struct extent_buffer *leaf;
1561 struct btrfs_ioctl_search_header sh;
1562 unsigned long item_off;
1563 unsigned long item_len;
1564 int nritems;
1565 int i;
1566 int slot;
ac8e9819
CM
1567 int ret = 0;
1568
1569 leaf = path->nodes[0];
1570 slot = path->slots[0];
1571 nritems = btrfs_header_nritems(leaf);
1572
1573 if (btrfs_header_generation(leaf) > sk->max_transid) {
1574 i = nritems;
1575 goto advance_key;
1576 }
1577 found_transid = btrfs_header_generation(leaf);
1578
1579 for (i = slot; i < nritems; i++) {
1580 item_off = btrfs_item_ptr_offset(leaf, i);
1581 item_len = btrfs_item_size_nr(leaf, i);
1582
1583 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1584 item_len = 0;
1585
1586 if (sizeof(sh) + item_len + *sk_offset >
1587 BTRFS_SEARCH_ARGS_BUFSIZE) {
1588 ret = 1;
1589 goto overflow;
1590 }
1591
1592 btrfs_item_key_to_cpu(leaf, key, i);
1593 if (!key_in_sk(key, sk))
1594 continue;
1595
1596 sh.objectid = key->objectid;
1597 sh.offset = key->offset;
1598 sh.type = key->type;
1599 sh.len = item_len;
1600 sh.transid = found_transid;
1601
1602 /* copy search result header */
1603 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1604 *sk_offset += sizeof(sh);
1605
1606 if (item_len) {
1607 char *p = buf + *sk_offset;
1608 /* copy the item */
1609 read_extent_buffer(leaf, p,
1610 item_off, item_len);
1611 *sk_offset += item_len;
ac8e9819 1612 }
e2156867 1613 (*num_found)++;
ac8e9819
CM
1614
1615 if (*num_found >= sk->nr_items)
1616 break;
1617 }
1618advance_key:
abc6e134
CM
1619 ret = 0;
1620 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
ac8e9819 1621 key->offset++;
abc6e134
CM
1622 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1623 key->offset = 0;
ac8e9819 1624 key->type++;
abc6e134
CM
1625 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1626 key->offset = 0;
1627 key->type = 0;
ac8e9819 1628 key->objectid++;
abc6e134
CM
1629 } else
1630 ret = 1;
ac8e9819 1631overflow:
ac8e9819
CM
1632 return ret;
1633}
1634
1635static noinline int search_ioctl(struct inode *inode,
1636 struct btrfs_ioctl_search_args *args)
1637{
1638 struct btrfs_root *root;
1639 struct btrfs_key key;
1640 struct btrfs_key max_key;
1641 struct btrfs_path *path;
1642 struct btrfs_ioctl_search_key *sk = &args->key;
1643 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1644 int ret;
1645 int num_found = 0;
1646 unsigned long sk_offset = 0;
1647
1648 path = btrfs_alloc_path();
1649 if (!path)
1650 return -ENOMEM;
1651
1652 if (sk->tree_id == 0) {
1653 /* search the root of the inode that was passed */
1654 root = BTRFS_I(inode)->root;
1655 } else {
1656 key.objectid = sk->tree_id;
1657 key.type = BTRFS_ROOT_ITEM_KEY;
1658 key.offset = (u64)-1;
1659 root = btrfs_read_fs_root_no_name(info, &key);
1660 if (IS_ERR(root)) {
1661 printk(KERN_ERR "could not find root %llu\n",
1662 sk->tree_id);
1663 btrfs_free_path(path);
1664 return -ENOENT;
1665 }
1666 }
1667
1668 key.objectid = sk->min_objectid;
1669 key.type = sk->min_type;
1670 key.offset = sk->min_offset;
1671
1672 max_key.objectid = sk->max_objectid;
1673 max_key.type = sk->max_type;
1674 max_key.offset = sk->max_offset;
1675
1676 path->keep_locks = 1;
1677
1678 while(1) {
1679 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1680 sk->min_transid);
1681 if (ret != 0) {
1682 if (ret > 0)
1683 ret = 0;
1684 goto err;
1685 }
1686 ret = copy_to_sk(root, path, &key, sk, args->buf,
1687 &sk_offset, &num_found);
b3b4aa74 1688 btrfs_release_path(path);
ac8e9819
CM
1689 if (ret || num_found >= sk->nr_items)
1690 break;
1691
1692 }
1693 ret = 0;
1694err:
1695 sk->nr_items = num_found;
1696 btrfs_free_path(path);
1697 return ret;
1698}
1699
1700static noinline int btrfs_ioctl_tree_search(struct file *file,
1701 void __user *argp)
1702{
1703 struct btrfs_ioctl_search_args *args;
1704 struct inode *inode;
1705 int ret;
1706
1707 if (!capable(CAP_SYS_ADMIN))
1708 return -EPERM;
1709
2354d08f
JL
1710 args = memdup_user(argp, sizeof(*args));
1711 if (IS_ERR(args))
1712 return PTR_ERR(args);
ac8e9819 1713
ac8e9819
CM
1714 inode = fdentry(file)->d_inode;
1715 ret = search_ioctl(inode, args);
1716 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1717 ret = -EFAULT;
1718 kfree(args);
1719 return ret;
1720}
1721
98d377a0 1722/*
ac8e9819
CM
1723 * Search INODE_REFs to identify path name of 'dirid' directory
1724 * in a 'tree_id' tree. and sets path name to 'name'.
1725 */
98d377a0
TH
1726static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1727 u64 tree_id, u64 dirid, char *name)
1728{
1729 struct btrfs_root *root;
1730 struct btrfs_key key;
ac8e9819 1731 char *ptr;
98d377a0
TH
1732 int ret = -1;
1733 int slot;
1734 int len;
1735 int total_len = 0;
1736 struct btrfs_inode_ref *iref;
1737 struct extent_buffer *l;
1738 struct btrfs_path *path;
1739
1740 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1741 name[0]='\0';
1742 return 0;
1743 }
1744
1745 path = btrfs_alloc_path();
1746 if (!path)
1747 return -ENOMEM;
1748
ac8e9819 1749 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
98d377a0
TH
1750
1751 key.objectid = tree_id;
1752 key.type = BTRFS_ROOT_ITEM_KEY;
1753 key.offset = (u64)-1;
1754 root = btrfs_read_fs_root_no_name(info, &key);
1755 if (IS_ERR(root)) {
1756 printk(KERN_ERR "could not find root %llu\n", tree_id);
8ad6fcab
CM
1757 ret = -ENOENT;
1758 goto out;
98d377a0
TH
1759 }
1760
1761 key.objectid = dirid;
1762 key.type = BTRFS_INODE_REF_KEY;
8ad6fcab 1763 key.offset = (u64)-1;
98d377a0
TH
1764
1765 while(1) {
1766 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1767 if (ret < 0)
1768 goto out;
1769
1770 l = path->nodes[0];
1771 slot = path->slots[0];
8ad6fcab
CM
1772 if (ret > 0 && slot > 0)
1773 slot--;
98d377a0
TH
1774 btrfs_item_key_to_cpu(l, &key, slot);
1775
1776 if (ret > 0 && (key.objectid != dirid ||
ac8e9819
CM
1777 key.type != BTRFS_INODE_REF_KEY)) {
1778 ret = -ENOENT;
98d377a0 1779 goto out;
ac8e9819 1780 }
98d377a0
TH
1781
1782 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1783 len = btrfs_inode_ref_name_len(l, iref);
1784 ptr -= len + 1;
1785 total_len += len + 1;
ac8e9819 1786 if (ptr < name)
98d377a0
TH
1787 goto out;
1788
1789 *(ptr + len) = '/';
1790 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1791
1792 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1793 break;
1794
b3b4aa74 1795 btrfs_release_path(path);
98d377a0 1796 key.objectid = key.offset;
8ad6fcab 1797 key.offset = (u64)-1;
98d377a0 1798 dirid = key.objectid;
98d377a0 1799 }
ac8e9819 1800 if (ptr < name)
98d377a0 1801 goto out;
77906a50 1802 memmove(name, ptr, total_len);
98d377a0
TH
1803 name[total_len]='\0';
1804 ret = 0;
1805out:
1806 btrfs_free_path(path);
ac8e9819
CM
1807 return ret;
1808}
1809
1810static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1811 void __user *argp)
1812{
1813 struct btrfs_ioctl_ino_lookup_args *args;
1814 struct inode *inode;
1815 int ret;
1816
1817 if (!capable(CAP_SYS_ADMIN))
1818 return -EPERM;
1819
2354d08f
JL
1820 args = memdup_user(argp, sizeof(*args));
1821 if (IS_ERR(args))
1822 return PTR_ERR(args);
c2b96929 1823
ac8e9819
CM
1824 inode = fdentry(file)->d_inode;
1825
1b53ac4d
CM
1826 if (args->treeid == 0)
1827 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1828
ac8e9819
CM
1829 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1830 args->treeid, args->objectid,
1831 args->name);
1832
1833 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1834 ret = -EFAULT;
1835
1836 kfree(args);
98d377a0
TH
1837 return ret;
1838}
1839
76dda93c
YZ
1840static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1841 void __user *arg)
1842{
1843 struct dentry *parent = fdentry(file);
1844 struct dentry *dentry;
1845 struct inode *dir = parent->d_inode;
1846 struct inode *inode;
1847 struct btrfs_root *root = BTRFS_I(dir)->root;
1848 struct btrfs_root *dest = NULL;
1849 struct btrfs_ioctl_vol_args *vol_args;
1850 struct btrfs_trans_handle *trans;
1851 int namelen;
1852 int ret;
1853 int err = 0;
1854
76dda93c
YZ
1855 vol_args = memdup_user(arg, sizeof(*vol_args));
1856 if (IS_ERR(vol_args))
1857 return PTR_ERR(vol_args);
1858
1859 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1860 namelen = strlen(vol_args->name);
1861 if (strchr(vol_args->name, '/') ||
1862 strncmp(vol_args->name, "..", namelen) == 0) {
1863 err = -EINVAL;
1864 goto out;
1865 }
1866
1867 err = mnt_want_write(file->f_path.mnt);
1868 if (err)
1869 goto out;
1870
1871 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1872 dentry = lookup_one_len(vol_args->name, parent, namelen);
1873 if (IS_ERR(dentry)) {
1874 err = PTR_ERR(dentry);
1875 goto out_unlock_dir;
1876 }
1877
1878 if (!dentry->d_inode) {
1879 err = -ENOENT;
1880 goto out_dput;
1881 }
1882
1883 inode = dentry->d_inode;
4260f7c7
SW
1884 dest = BTRFS_I(inode)->root;
1885 if (!capable(CAP_SYS_ADMIN)){
1886 /*
1887 * Regular user. Only allow this with a special mount
1888 * option, when the user has write+exec access to the
1889 * subvol root, and when rmdir(2) would have been
1890 * allowed.
1891 *
1892 * Note that this is _not_ check that the subvol is
1893 * empty or doesn't contain data that we wouldn't
1894 * otherwise be able to delete.
1895 *
1896 * Users who want to delete empty subvols should try
1897 * rmdir(2).
1898 */
1899 err = -EPERM;
1900 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1901 goto out_dput;
1902
1903 /*
1904 * Do not allow deletion if the parent dir is the same
1905 * as the dir to be deleted. That means the ioctl
1906 * must be called on the dentry referencing the root
1907 * of the subvol, not a random directory contained
1908 * within it.
1909 */
1910 err = -EINVAL;
1911 if (root == dest)
1912 goto out_dput;
1913
1914 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1915 if (err)
1916 goto out_dput;
1917
1918 /* check if subvolume may be deleted by a non-root user */
1919 err = btrfs_may_delete(dir, dentry, 1);
1920 if (err)
1921 goto out_dput;
1922 }
1923
33345d01 1924 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
76dda93c
YZ
1925 err = -EINVAL;
1926 goto out_dput;
1927 }
1928
76dda93c
YZ
1929 mutex_lock(&inode->i_mutex);
1930 err = d_invalidate(dentry);
1931 if (err)
1932 goto out_unlock;
1933
1934 down_write(&root->fs_info->subvol_sem);
1935
1936 err = may_destroy_subvol(dest);
1937 if (err)
1938 goto out_up_write;
1939
a22285a6
YZ
1940 trans = btrfs_start_transaction(root, 0);
1941 if (IS_ERR(trans)) {
1942 err = PTR_ERR(trans);
d327099a 1943 goto out_up_write;
a22285a6
YZ
1944 }
1945 trans->block_rsv = &root->fs_info->global_block_rsv;
1946
76dda93c
YZ
1947 ret = btrfs_unlink_subvol(trans, root, dir,
1948 dest->root_key.objectid,
1949 dentry->d_name.name,
1950 dentry->d_name.len);
1951 BUG_ON(ret);
1952
1953 btrfs_record_root_in_trans(trans, dest);
1954
1955 memset(&dest->root_item.drop_progress, 0,
1956 sizeof(dest->root_item.drop_progress));
1957 dest->root_item.drop_level = 0;
1958 btrfs_set_root_refs(&dest->root_item, 0);
1959
d68fc57b
YZ
1960 if (!xchg(&dest->orphan_item_inserted, 1)) {
1961 ret = btrfs_insert_orphan_item(trans,
1962 root->fs_info->tree_root,
1963 dest->root_key.objectid);
1964 BUG_ON(ret);
1965 }
76dda93c 1966
531cb13f 1967 ret = btrfs_end_transaction(trans, root);
76dda93c
YZ
1968 BUG_ON(ret);
1969 inode->i_flags |= S_DEAD;
1970out_up_write:
1971 up_write(&root->fs_info->subvol_sem);
1972out_unlock:
1973 mutex_unlock(&inode->i_mutex);
1974 if (!err) {
efefb143 1975 shrink_dcache_sb(root->fs_info->sb);
76dda93c
YZ
1976 btrfs_invalidate_inodes(dest);
1977 d_delete(dentry);
1978 }
1979out_dput:
1980 dput(dentry);
1981out_unlock_dir:
1982 mutex_unlock(&dir->i_mutex);
1983 mnt_drop_write(file->f_path.mnt);
1984out:
1985 kfree(vol_args);
1986 return err;
1987}
1988
1e701a32 1989static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
f46b5a66
CH
1990{
1991 struct inode *inode = fdentry(file)->d_inode;
1992 struct btrfs_root *root = BTRFS_I(inode)->root;
1e701a32 1993 struct btrfs_ioctl_defrag_range_args *range;
c146afad
YZ
1994 int ret;
1995
b83cc969
LZ
1996 if (btrfs_root_readonly(root))
1997 return -EROFS;
1998
c146afad
YZ
1999 ret = mnt_want_write(file->f_path.mnt);
2000 if (ret)
2001 return ret;
f46b5a66
CH
2002
2003 switch (inode->i_mode & S_IFMT) {
2004 case S_IFDIR:
e441d54d
CM
2005 if (!capable(CAP_SYS_ADMIN)) {
2006 ret = -EPERM;
2007 goto out;
2008 }
8929ecfa
YZ
2009 ret = btrfs_defrag_root(root, 0);
2010 if (ret)
2011 goto out;
2012 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
f46b5a66
CH
2013 break;
2014 case S_IFREG:
e441d54d
CM
2015 if (!(file->f_mode & FMODE_WRITE)) {
2016 ret = -EINVAL;
2017 goto out;
2018 }
1e701a32
CM
2019
2020 range = kzalloc(sizeof(*range), GFP_KERNEL);
2021 if (!range) {
2022 ret = -ENOMEM;
2023 goto out;
2024 }
2025
2026 if (argp) {
2027 if (copy_from_user(range, argp,
2028 sizeof(*range))) {
2029 ret = -EFAULT;
2030 kfree(range);
683be16e 2031 goto out;
1e701a32
CM
2032 }
2033 /* compression requires us to start the IO */
2034 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2035 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2036 range->extent_thresh = (u32)-1;
2037 }
2038 } else {
2039 /* the rest are all set to zero by kzalloc */
2040 range->len = (u64)-1;
2041 }
4cb5300b
CM
2042 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2043 range, 0, 0);
2044 if (ret > 0)
2045 ret = 0;
1e701a32 2046 kfree(range);
f46b5a66 2047 break;
8929ecfa
YZ
2048 default:
2049 ret = -EINVAL;
f46b5a66 2050 }
e441d54d 2051out:
ab67b7c1 2052 mnt_drop_write(file->f_path.mnt);
e441d54d 2053 return ret;
f46b5a66
CH
2054}
2055
b2950863 2056static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
2057{
2058 struct btrfs_ioctl_vol_args *vol_args;
2059 int ret;
2060
e441d54d
CM
2061 if (!capable(CAP_SYS_ADMIN))
2062 return -EPERM;
2063
c9e9f97b
ID
2064 mutex_lock(&root->fs_info->volume_mutex);
2065 if (root->fs_info->balance_ctl) {
2066 printk(KERN_INFO "btrfs: balance in progress\n");
2067 ret = -EINVAL;
2068 goto out;
2069 }
2070
dae7b665 2071 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2072 if (IS_ERR(vol_args)) {
2073 ret = PTR_ERR(vol_args);
2074 goto out;
2075 }
f46b5a66 2076
5516e595 2077 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
2078 ret = btrfs_init_new_device(root, vol_args->name);
2079
f46b5a66 2080 kfree(vol_args);
c9e9f97b
ID
2081out:
2082 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
2083 return ret;
2084}
2085
b2950863 2086static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
2087{
2088 struct btrfs_ioctl_vol_args *vol_args;
2089 int ret;
2090
e441d54d
CM
2091 if (!capable(CAP_SYS_ADMIN))
2092 return -EPERM;
2093
c146afad
YZ
2094 if (root->fs_info->sb->s_flags & MS_RDONLY)
2095 return -EROFS;
2096
c9e9f97b
ID
2097 mutex_lock(&root->fs_info->volume_mutex);
2098 if (root->fs_info->balance_ctl) {
2099 printk(KERN_INFO "btrfs: balance in progress\n");
2100 ret = -EINVAL;
2101 goto out;
2102 }
2103
dae7b665 2104 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2105 if (IS_ERR(vol_args)) {
2106 ret = PTR_ERR(vol_args);
2107 goto out;
2108 }
f46b5a66 2109
5516e595 2110 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
2111 ret = btrfs_rm_device(root, vol_args->name);
2112
f46b5a66 2113 kfree(vol_args);
c9e9f97b
ID
2114out:
2115 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
2116 return ret;
2117}
2118
475f6387
JS
2119static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2120{
027ed2f0 2121 struct btrfs_ioctl_fs_info_args *fi_args;
475f6387
JS
2122 struct btrfs_device *device;
2123 struct btrfs_device *next;
2124 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
027ed2f0 2125 int ret = 0;
475f6387
JS
2126
2127 if (!capable(CAP_SYS_ADMIN))
2128 return -EPERM;
2129
027ed2f0
LZ
2130 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2131 if (!fi_args)
2132 return -ENOMEM;
2133
2134 fi_args->num_devices = fs_devices->num_devices;
2135 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
475f6387
JS
2136
2137 mutex_lock(&fs_devices->device_list_mutex);
2138 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
027ed2f0
LZ
2139 if (device->devid > fi_args->max_id)
2140 fi_args->max_id = device->devid;
475f6387
JS
2141 }
2142 mutex_unlock(&fs_devices->device_list_mutex);
2143
027ed2f0
LZ
2144 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2145 ret = -EFAULT;
475f6387 2146
027ed2f0
LZ
2147 kfree(fi_args);
2148 return ret;
475f6387
JS
2149}
2150
2151static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2152{
2153 struct btrfs_ioctl_dev_info_args *di_args;
2154 struct btrfs_device *dev;
2155 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2156 int ret = 0;
2157 char *s_uuid = NULL;
2158 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2159
2160 if (!capable(CAP_SYS_ADMIN))
2161 return -EPERM;
2162
2163 di_args = memdup_user(arg, sizeof(*di_args));
2164 if (IS_ERR(di_args))
2165 return PTR_ERR(di_args);
2166
2167 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2168 s_uuid = di_args->uuid;
2169
2170 mutex_lock(&fs_devices->device_list_mutex);
2171 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2172 mutex_unlock(&fs_devices->device_list_mutex);
2173
2174 if (!dev) {
2175 ret = -ENODEV;
2176 goto out;
2177 }
2178
2179 di_args->devid = dev->devid;
2180 di_args->bytes_used = dev->bytes_used;
2181 di_args->total_bytes = dev->total_bytes;
2182 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2183 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2184
2185out:
2186 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2187 ret = -EFAULT;
2188
2189 kfree(di_args);
2190 return ret;
2191}
2192
76dda93c
YZ
2193static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2194 u64 off, u64 olen, u64 destoff)
f46b5a66
CH
2195{
2196 struct inode *inode = fdentry(file)->d_inode;
2197 struct btrfs_root *root = BTRFS_I(inode)->root;
2198 struct file *src_file;
2199 struct inode *src;
2200 struct btrfs_trans_handle *trans;
f46b5a66 2201 struct btrfs_path *path;
f46b5a66 2202 struct extent_buffer *leaf;
ae01a0ab
YZ
2203 char *buf;
2204 struct btrfs_key key;
f46b5a66
CH
2205 u32 nritems;
2206 int slot;
ae01a0ab 2207 int ret;
c5c9cd4d
SW
2208 u64 len = olen;
2209 u64 bs = root->fs_info->sb->s_blocksize;
2210 u64 hint_byte;
d20f7043 2211
c5c9cd4d
SW
2212 /*
2213 * TODO:
2214 * - split compressed inline extents. annoying: we need to
2215 * decompress into destination's address_space (the file offset
2216 * may change, so source mapping won't do), then recompress (or
2217 * otherwise reinsert) a subrange.
2218 * - allow ranges within the same file to be cloned (provided
2219 * they don't overlap)?
2220 */
2221
e441d54d 2222 /* the destination must be opened for writing */
2ebc3464 2223 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
e441d54d
CM
2224 return -EINVAL;
2225
b83cc969
LZ
2226 if (btrfs_root_readonly(root))
2227 return -EROFS;
2228
c146afad
YZ
2229 ret = mnt_want_write(file->f_path.mnt);
2230 if (ret)
2231 return ret;
2232
c5c9cd4d 2233 src_file = fget(srcfd);
ab67b7c1
YZ
2234 if (!src_file) {
2235 ret = -EBADF;
2236 goto out_drop_write;
2237 }
5dc64164 2238
f46b5a66
CH
2239 src = src_file->f_dentry->d_inode;
2240
c5c9cd4d
SW
2241 ret = -EINVAL;
2242 if (src == inode)
2243 goto out_fput;
2244
5dc64164
DR
2245 /* the src must be open for reading */
2246 if (!(src_file->f_mode & FMODE_READ))
2247 goto out_fput;
2248
0e7b824c
LZ
2249 /* don't make the dst file partly checksummed */
2250 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2251 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2252 goto out_fput;
2253
ae01a0ab
YZ
2254 ret = -EISDIR;
2255 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2256 goto out_fput;
2257
f46b5a66 2258 ret = -EXDEV;
ae01a0ab
YZ
2259 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2260 goto out_fput;
2261
2262 ret = -ENOMEM;
2263 buf = vmalloc(btrfs_level_size(root, 0));
2264 if (!buf)
2265 goto out_fput;
2266
2267 path = btrfs_alloc_path();
2268 if (!path) {
2269 vfree(buf);
f46b5a66 2270 goto out_fput;
ae01a0ab
YZ
2271 }
2272 path->reada = 2;
f46b5a66
CH
2273
2274 if (inode < src) {
fccdae43
SW
2275 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2276 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
f46b5a66 2277 } else {
fccdae43
SW
2278 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2279 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
f46b5a66
CH
2280 }
2281
c5c9cd4d
SW
2282 /* determine range to clone */
2283 ret = -EINVAL;
2ebc3464 2284 if (off + len > src->i_size || off + len < off)
f46b5a66 2285 goto out_unlock;
c5c9cd4d
SW
2286 if (len == 0)
2287 olen = len = src->i_size - off;
2288 /* if we extend to eof, continue to block boundary */
2289 if (off + len == src->i_size)
2a6b8dae 2290 len = ALIGN(src->i_size, bs) - off;
c5c9cd4d
SW
2291
2292 /* verify the end result is block aligned */
2a6b8dae
LZ
2293 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2294 !IS_ALIGNED(destoff, bs))
c5c9cd4d
SW
2295 goto out_unlock;
2296
d525e8ab
LZ
2297 if (destoff > inode->i_size) {
2298 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2299 if (ret)
2300 goto out_unlock;
2301 }
2302
71ef0786
LZ
2303 /* truncate page cache pages from target inode range */
2304 truncate_inode_pages_range(&inode->i_data, destoff,
2305 PAGE_CACHE_ALIGN(destoff + len) - 1);
2306
f46b5a66
CH
2307 /* do any pending delalloc/csum calc on src, one way or
2308 another, and lock file content */
2309 while (1) {
31840ae1 2310 struct btrfs_ordered_extent *ordered;
c5c9cd4d 2311 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
9a019196
SW
2312 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2313 if (!ordered &&
2314 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2315 EXTENT_DELALLOC, 0, NULL))
f46b5a66 2316 break;
c5c9cd4d 2317 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
ae01a0ab
YZ
2318 if (ordered)
2319 btrfs_put_ordered_extent(ordered);
9a019196 2320 btrfs_wait_ordered_range(src, off, len);
f46b5a66
CH
2321 }
2322
c5c9cd4d 2323 /* clone data */
33345d01 2324 key.objectid = btrfs_ino(src);
ae01a0ab
YZ
2325 key.type = BTRFS_EXTENT_DATA_KEY;
2326 key.offset = 0;
f46b5a66
CH
2327
2328 while (1) {
2329 /*
2330 * note the key will change type as we walk through the
2331 * tree.
2332 */
a22285a6 2333 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
f46b5a66
CH
2334 if (ret < 0)
2335 goto out;
2336
ae01a0ab
YZ
2337 nritems = btrfs_header_nritems(path->nodes[0]);
2338 if (path->slots[0] >= nritems) {
f46b5a66
CH
2339 ret = btrfs_next_leaf(root, path);
2340 if (ret < 0)
2341 goto out;
2342 if (ret > 0)
2343 break;
ae01a0ab 2344 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
2345 }
2346 leaf = path->nodes[0];
2347 slot = path->slots[0];
f46b5a66 2348
ae01a0ab 2349 btrfs_item_key_to_cpu(leaf, &key, slot);
d20f7043 2350 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
33345d01 2351 key.objectid != btrfs_ino(src))
f46b5a66
CH
2352 break;
2353
c5c9cd4d
SW
2354 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2355 struct btrfs_file_extent_item *extent;
2356 int type;
31840ae1
ZY
2357 u32 size;
2358 struct btrfs_key new_key;
c5c9cd4d
SW
2359 u64 disko = 0, diskl = 0;
2360 u64 datao = 0, datal = 0;
2361 u8 comp;
b5384d48 2362 u64 endoff;
31840ae1
ZY
2363
2364 size = btrfs_item_size_nr(leaf, slot);
2365 read_extent_buffer(leaf, buf,
2366 btrfs_item_ptr_offset(leaf, slot),
2367 size);
c5c9cd4d
SW
2368
2369 extent = btrfs_item_ptr(leaf, slot,
2370 struct btrfs_file_extent_item);
2371 comp = btrfs_file_extent_compression(leaf, extent);
2372 type = btrfs_file_extent_type(leaf, extent);
c8a894d7
CM
2373 if (type == BTRFS_FILE_EXTENT_REG ||
2374 type == BTRFS_FILE_EXTENT_PREALLOC) {
d397712b
CM
2375 disko = btrfs_file_extent_disk_bytenr(leaf,
2376 extent);
2377 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2378 extent);
c5c9cd4d 2379 datao = btrfs_file_extent_offset(leaf, extent);
d397712b
CM
2380 datal = btrfs_file_extent_num_bytes(leaf,
2381 extent);
c5c9cd4d
SW
2382 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2383 /* take upper bound, may be compressed */
2384 datal = btrfs_file_extent_ram_bytes(leaf,
2385 extent);
2386 }
b3b4aa74 2387 btrfs_release_path(path);
31840ae1 2388
050006a7 2389 if (key.offset + datal <= off ||
c5c9cd4d
SW
2390 key.offset >= off+len)
2391 goto next;
2392
31840ae1 2393 memcpy(&new_key, &key, sizeof(new_key));
33345d01 2394 new_key.objectid = btrfs_ino(inode);
4d728ec7
LZ
2395 if (off <= key.offset)
2396 new_key.offset = key.offset + destoff - off;
2397 else
2398 new_key.offset = destoff;
31840ae1 2399
b6f3409b
SW
2400 /*
2401 * 1 - adjusting old extent (we may have to split it)
2402 * 1 - add new extent
2403 * 1 - inode update
2404 */
2405 trans = btrfs_start_transaction(root, 3);
a22285a6
YZ
2406 if (IS_ERR(trans)) {
2407 ret = PTR_ERR(trans);
2408 goto out;
2409 }
2410
c8a894d7
CM
2411 if (type == BTRFS_FILE_EXTENT_REG ||
2412 type == BTRFS_FILE_EXTENT_PREALLOC) {
d72c0842
LZ
2413 /*
2414 * a | --- range to clone ---| b
2415 * | ------------- extent ------------- |
2416 */
2417
2418 /* substract range b */
2419 if (key.offset + datal > off + len)
2420 datal = off + len - key.offset;
2421
2422 /* substract range a */
a22285a6
YZ
2423 if (off > key.offset) {
2424 datao += off - key.offset;
2425 datal -= off - key.offset;
2426 }
2427
a22285a6
YZ
2428 ret = btrfs_drop_extents(trans, inode,
2429 new_key.offset,
2430 new_key.offset + datal,
2431 &hint_byte, 1);
2432 BUG_ON(ret);
2433
c5c9cd4d
SW
2434 ret = btrfs_insert_empty_item(trans, root, path,
2435 &new_key, size);
a22285a6 2436 BUG_ON(ret);
c5c9cd4d
SW
2437
2438 leaf = path->nodes[0];
2439 slot = path->slots[0];
2440 write_extent_buffer(leaf, buf,
31840ae1
ZY
2441 btrfs_item_ptr_offset(leaf, slot),
2442 size);
ae01a0ab 2443
c5c9cd4d 2444 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 2445 struct btrfs_file_extent_item);
c5c9cd4d 2446
c5c9cd4d
SW
2447 /* disko == 0 means it's a hole */
2448 if (!disko)
2449 datao = 0;
c5c9cd4d
SW
2450
2451 btrfs_set_file_extent_offset(leaf, extent,
2452 datao);
2453 btrfs_set_file_extent_num_bytes(leaf, extent,
2454 datal);
2455 if (disko) {
2456 inode_add_bytes(inode, datal);
ae01a0ab 2457 ret = btrfs_inc_extent_ref(trans, root,
5d4f98a2
YZ
2458 disko, diskl, 0,
2459 root->root_key.objectid,
33345d01 2460 btrfs_ino(inode),
5d4f98a2 2461 new_key.offset - datao);
31840ae1 2462 BUG_ON(ret);
f46b5a66 2463 }
c5c9cd4d
SW
2464 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2465 u64 skip = 0;
2466 u64 trim = 0;
2467 if (off > key.offset) {
2468 skip = off - key.offset;
2469 new_key.offset += skip;
2470 }
d397712b 2471
c5c9cd4d
SW
2472 if (key.offset + datal > off+len)
2473 trim = key.offset + datal - (off+len);
d397712b 2474
c5c9cd4d 2475 if (comp && (skip || trim)) {
c5c9cd4d 2476 ret = -EINVAL;
a22285a6 2477 btrfs_end_transaction(trans, root);
c5c9cd4d
SW
2478 goto out;
2479 }
2480 size -= skip + trim;
2481 datal -= skip + trim;
a22285a6
YZ
2482
2483 ret = btrfs_drop_extents(trans, inode,
2484 new_key.offset,
2485 new_key.offset + datal,
2486 &hint_byte, 1);
2487 BUG_ON(ret);
2488
c5c9cd4d
SW
2489 ret = btrfs_insert_empty_item(trans, root, path,
2490 &new_key, size);
a22285a6 2491 BUG_ON(ret);
c5c9cd4d
SW
2492
2493 if (skip) {
d397712b
CM
2494 u32 start =
2495 btrfs_file_extent_calc_inline_size(0);
c5c9cd4d
SW
2496 memmove(buf+start, buf+start+skip,
2497 datal);
2498 }
2499
2500 leaf = path->nodes[0];
2501 slot = path->slots[0];
2502 write_extent_buffer(leaf, buf,
2503 btrfs_item_ptr_offset(leaf, slot),
2504 size);
2505 inode_add_bytes(inode, datal);
f46b5a66 2506 }
c5c9cd4d
SW
2507
2508 btrfs_mark_buffer_dirty(leaf);
b3b4aa74 2509 btrfs_release_path(path);
c5c9cd4d 2510
a22285a6 2511 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
b5384d48
SW
2512
2513 /*
2514 * we round up to the block size at eof when
2515 * determining which extents to clone above,
2516 * but shouldn't round up the file size
2517 */
2518 endoff = new_key.offset + datal;
5f3888ff
LZ
2519 if (endoff > destoff+olen)
2520 endoff = destoff+olen;
b5384d48
SW
2521 if (endoff > inode->i_size)
2522 btrfs_i_size_write(inode, endoff);
2523
a22285a6
YZ
2524 ret = btrfs_update_inode(trans, root, inode);
2525 BUG_ON(ret);
2526 btrfs_end_transaction(trans, root);
2527 }
d397712b 2528next:
b3b4aa74 2529 btrfs_release_path(path);
f46b5a66 2530 key.offset++;
f46b5a66 2531 }
f46b5a66
CH
2532 ret = 0;
2533out:
b3b4aa74 2534 btrfs_release_path(path);
c5c9cd4d 2535 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
f46b5a66
CH
2536out_unlock:
2537 mutex_unlock(&src->i_mutex);
2538 mutex_unlock(&inode->i_mutex);
ae01a0ab
YZ
2539 vfree(buf);
2540 btrfs_free_path(path);
f46b5a66
CH
2541out_fput:
2542 fput(src_file);
ab67b7c1
YZ
2543out_drop_write:
2544 mnt_drop_write(file->f_path.mnt);
f46b5a66
CH
2545 return ret;
2546}
2547
7a865e8a 2548static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
c5c9cd4d
SW
2549{
2550 struct btrfs_ioctl_clone_range_args args;
2551
7a865e8a 2552 if (copy_from_user(&args, argp, sizeof(args)))
c5c9cd4d
SW
2553 return -EFAULT;
2554 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2555 args.src_length, args.dest_offset);
2556}
2557
f46b5a66
CH
2558/*
2559 * there are many ways the trans_start and trans_end ioctls can lead
2560 * to deadlocks. They should only be used by applications that
2561 * basically own the machine, and have a very in depth understanding
2562 * of all the possible deadlocks and enospc problems.
2563 */
b2950863 2564static long btrfs_ioctl_trans_start(struct file *file)
f46b5a66
CH
2565{
2566 struct inode *inode = fdentry(file)->d_inode;
2567 struct btrfs_root *root = BTRFS_I(inode)->root;
2568 struct btrfs_trans_handle *trans;
1ab86aed 2569 int ret;
f46b5a66 2570
1ab86aed 2571 ret = -EPERM;
df5b5520 2572 if (!capable(CAP_SYS_ADMIN))
1ab86aed 2573 goto out;
df5b5520 2574
1ab86aed
SW
2575 ret = -EINPROGRESS;
2576 if (file->private_data)
f46b5a66 2577 goto out;
9ca9ee09 2578
b83cc969
LZ
2579 ret = -EROFS;
2580 if (btrfs_root_readonly(root))
2581 goto out;
2582
c146afad
YZ
2583 ret = mnt_want_write(file->f_path.mnt);
2584 if (ret)
2585 goto out;
2586
a4abeea4 2587 atomic_inc(&root->fs_info->open_ioctl_trans);
9ca9ee09 2588
1ab86aed 2589 ret = -ENOMEM;
7a7eaa40 2590 trans = btrfs_start_ioctl_transaction(root);
abd30bb0 2591 if (IS_ERR(trans))
1ab86aed
SW
2592 goto out_drop;
2593
2594 file->private_data = trans;
2595 return 0;
2596
2597out_drop:
a4abeea4 2598 atomic_dec(&root->fs_info->open_ioctl_trans);
1ab86aed 2599 mnt_drop_write(file->f_path.mnt);
f46b5a66 2600out:
f46b5a66
CH
2601 return ret;
2602}
2603
6ef5ed0d
JB
2604static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2605{
2606 struct inode *inode = fdentry(file)->d_inode;
2607 struct btrfs_root *root = BTRFS_I(inode)->root;
2608 struct btrfs_root *new_root;
2609 struct btrfs_dir_item *di;
2610 struct btrfs_trans_handle *trans;
2611 struct btrfs_path *path;
2612 struct btrfs_key location;
2613 struct btrfs_disk_key disk_key;
2614 struct btrfs_super_block *disk_super;
2615 u64 features;
2616 u64 objectid = 0;
2617 u64 dir_id;
2618
2619 if (!capable(CAP_SYS_ADMIN))
2620 return -EPERM;
2621
2622 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2623 return -EFAULT;
2624
2625 if (!objectid)
2626 objectid = root->root_key.objectid;
2627
2628 location.objectid = objectid;
2629 location.type = BTRFS_ROOT_ITEM_KEY;
2630 location.offset = (u64)-1;
2631
2632 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2633 if (IS_ERR(new_root))
2634 return PTR_ERR(new_root);
2635
2636 if (btrfs_root_refs(&new_root->root_item) == 0)
2637 return -ENOENT;
2638
2639 path = btrfs_alloc_path();
2640 if (!path)
2641 return -ENOMEM;
2642 path->leave_spinning = 1;
2643
2644 trans = btrfs_start_transaction(root, 1);
98d5dc13 2645 if (IS_ERR(trans)) {
6ef5ed0d 2646 btrfs_free_path(path);
98d5dc13 2647 return PTR_ERR(trans);
6ef5ed0d
JB
2648 }
2649
6c41761f 2650 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
6ef5ed0d
JB
2651 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2652 dir_id, "default", 7, 1);
cf1e99a4 2653 if (IS_ERR_OR_NULL(di)) {
6ef5ed0d
JB
2654 btrfs_free_path(path);
2655 btrfs_end_transaction(trans, root);
2656 printk(KERN_ERR "Umm, you don't have the default dir item, "
2657 "this isn't going to work\n");
2658 return -ENOENT;
2659 }
2660
2661 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2662 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2663 btrfs_mark_buffer_dirty(path->nodes[0]);
2664 btrfs_free_path(path);
2665
6c41761f 2666 disk_super = root->fs_info->super_copy;
6ef5ed0d
JB
2667 features = btrfs_super_incompat_flags(disk_super);
2668 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2669 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2670 btrfs_set_super_incompat_flags(disk_super, features);
2671 }
2672 btrfs_end_transaction(trans, root);
2673
2674 return 0;
2675}
2676
bf5fc093
JB
2677static void get_block_group_info(struct list_head *groups_list,
2678 struct btrfs_ioctl_space_info *space)
2679{
2680 struct btrfs_block_group_cache *block_group;
2681
2682 space->total_bytes = 0;
2683 space->used_bytes = 0;
2684 space->flags = 0;
2685 list_for_each_entry(block_group, groups_list, list) {
2686 space->flags = block_group->flags;
2687 space->total_bytes += block_group->key.offset;
2688 space->used_bytes +=
2689 btrfs_block_group_used(&block_group->item);
2690 }
2691}
2692
1406e432
JB
2693long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2694{
2695 struct btrfs_ioctl_space_args space_args;
2696 struct btrfs_ioctl_space_info space;
2697 struct btrfs_ioctl_space_info *dest;
7fde62bf 2698 struct btrfs_ioctl_space_info *dest_orig;
13f2696f 2699 struct btrfs_ioctl_space_info __user *user_dest;
1406e432 2700 struct btrfs_space_info *info;
bf5fc093
JB
2701 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2702 BTRFS_BLOCK_GROUP_SYSTEM,
2703 BTRFS_BLOCK_GROUP_METADATA,
2704 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2705 int num_types = 4;
7fde62bf 2706 int alloc_size;
1406e432 2707 int ret = 0;
51788b1b 2708 u64 slot_count = 0;
bf5fc093 2709 int i, c;
1406e432
JB
2710
2711 if (copy_from_user(&space_args,
2712 (struct btrfs_ioctl_space_args __user *)arg,
2713 sizeof(space_args)))
2714 return -EFAULT;
2715
bf5fc093
JB
2716 for (i = 0; i < num_types; i++) {
2717 struct btrfs_space_info *tmp;
2718
2719 info = NULL;
2720 rcu_read_lock();
2721 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2722 list) {
2723 if (tmp->flags == types[i]) {
2724 info = tmp;
2725 break;
2726 }
2727 }
2728 rcu_read_unlock();
2729
2730 if (!info)
2731 continue;
2732
2733 down_read(&info->groups_sem);
2734 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2735 if (!list_empty(&info->block_groups[c]))
2736 slot_count++;
2737 }
2738 up_read(&info->groups_sem);
2739 }
7fde62bf
CM
2740
2741 /* space_slots == 0 means they are asking for a count */
2742 if (space_args.space_slots == 0) {
2743 space_args.total_spaces = slot_count;
2744 goto out;
2745 }
bf5fc093 2746
51788b1b 2747 slot_count = min_t(u64, space_args.space_slots, slot_count);
bf5fc093 2748
7fde62bf 2749 alloc_size = sizeof(*dest) * slot_count;
bf5fc093 2750
7fde62bf
CM
2751 /* we generally have at most 6 or so space infos, one for each raid
2752 * level. So, a whole page should be more than enough for everyone
2753 */
2754 if (alloc_size > PAGE_CACHE_SIZE)
2755 return -ENOMEM;
2756
1406e432 2757 space_args.total_spaces = 0;
7fde62bf
CM
2758 dest = kmalloc(alloc_size, GFP_NOFS);
2759 if (!dest)
2760 return -ENOMEM;
2761 dest_orig = dest;
1406e432 2762
7fde62bf 2763 /* now we have a buffer to copy into */
bf5fc093
JB
2764 for (i = 0; i < num_types; i++) {
2765 struct btrfs_space_info *tmp;
2766
51788b1b
DR
2767 if (!slot_count)
2768 break;
2769
bf5fc093
JB
2770 info = NULL;
2771 rcu_read_lock();
2772 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2773 list) {
2774 if (tmp->flags == types[i]) {
2775 info = tmp;
2776 break;
2777 }
2778 }
2779 rcu_read_unlock();
7fde62bf 2780
bf5fc093
JB
2781 if (!info)
2782 continue;
2783 down_read(&info->groups_sem);
2784 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2785 if (!list_empty(&info->block_groups[c])) {
2786 get_block_group_info(&info->block_groups[c],
2787 &space);
2788 memcpy(dest, &space, sizeof(space));
2789 dest++;
2790 space_args.total_spaces++;
51788b1b 2791 slot_count--;
bf5fc093 2792 }
51788b1b
DR
2793 if (!slot_count)
2794 break;
bf5fc093
JB
2795 }
2796 up_read(&info->groups_sem);
1406e432 2797 }
1406e432 2798
7fde62bf
CM
2799 user_dest = (struct btrfs_ioctl_space_info *)
2800 (arg + sizeof(struct btrfs_ioctl_space_args));
2801
2802 if (copy_to_user(user_dest, dest_orig, alloc_size))
2803 ret = -EFAULT;
2804
2805 kfree(dest_orig);
2806out:
2807 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1406e432
JB
2808 ret = -EFAULT;
2809
2810 return ret;
2811}
2812
f46b5a66
CH
2813/*
2814 * there are many ways the trans_start and trans_end ioctls can lead
2815 * to deadlocks. They should only be used by applications that
2816 * basically own the machine, and have a very in depth understanding
2817 * of all the possible deadlocks and enospc problems.
2818 */
2819long btrfs_ioctl_trans_end(struct file *file)
2820{
2821 struct inode *inode = fdentry(file)->d_inode;
2822 struct btrfs_root *root = BTRFS_I(inode)->root;
2823 struct btrfs_trans_handle *trans;
f46b5a66 2824
f46b5a66 2825 trans = file->private_data;
1ab86aed
SW
2826 if (!trans)
2827 return -EINVAL;
b214107e 2828 file->private_data = NULL;
9ca9ee09 2829
1ab86aed
SW
2830 btrfs_end_transaction(trans, root);
2831
a4abeea4 2832 atomic_dec(&root->fs_info->open_ioctl_trans);
9ca9ee09 2833
cfc8ea87 2834 mnt_drop_write(file->f_path.mnt);
1ab86aed 2835 return 0;
f46b5a66
CH
2836}
2837
46204592
SW
2838static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2839{
2840 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2841 struct btrfs_trans_handle *trans;
2842 u64 transid;
db5b493a 2843 int ret;
46204592
SW
2844
2845 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
2846 if (IS_ERR(trans))
2847 return PTR_ERR(trans);
46204592 2848 transid = trans->transid;
db5b493a 2849 ret = btrfs_commit_transaction_async(trans, root, 0);
8b2b2d3c
TI
2850 if (ret) {
2851 btrfs_end_transaction(trans, root);
db5b493a 2852 return ret;
8b2b2d3c 2853 }
46204592
SW
2854
2855 if (argp)
2856 if (copy_to_user(argp, &transid, sizeof(transid)))
2857 return -EFAULT;
2858 return 0;
2859}
2860
2861static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2862{
2863 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2864 u64 transid;
2865
2866 if (argp) {
2867 if (copy_from_user(&transid, argp, sizeof(transid)))
2868 return -EFAULT;
2869 } else {
2870 transid = 0; /* current trans */
2871 }
2872 return btrfs_wait_for_commit(root, transid);
2873}
2874
475f6387
JS
2875static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2876{
2877 int ret;
2878 struct btrfs_ioctl_scrub_args *sa;
2879
2880 if (!capable(CAP_SYS_ADMIN))
2881 return -EPERM;
2882
2883 sa = memdup_user(arg, sizeof(*sa));
2884 if (IS_ERR(sa))
2885 return PTR_ERR(sa);
2886
2887 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
8628764e 2888 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
475f6387
JS
2889
2890 if (copy_to_user(arg, sa, sizeof(*sa)))
2891 ret = -EFAULT;
2892
2893 kfree(sa);
2894 return ret;
2895}
2896
2897static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2898{
2899 if (!capable(CAP_SYS_ADMIN))
2900 return -EPERM;
2901
2902 return btrfs_scrub_cancel(root);
2903}
2904
2905static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2906 void __user *arg)
2907{
2908 struct btrfs_ioctl_scrub_args *sa;
2909 int ret;
2910
2911 if (!capable(CAP_SYS_ADMIN))
2912 return -EPERM;
2913
2914 sa = memdup_user(arg, sizeof(*sa));
2915 if (IS_ERR(sa))
2916 return PTR_ERR(sa);
2917
2918 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2919
2920 if (copy_to_user(arg, sa, sizeof(*sa)))
2921 ret = -EFAULT;
2922
2923 kfree(sa);
2924 return ret;
2925}
2926
d7728c96
JS
2927static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
2928{
2929 int ret = 0;
2930 int i;
740c3d22 2931 u64 rel_ptr;
d7728c96 2932 int size;
806468f8 2933 struct btrfs_ioctl_ino_path_args *ipa = NULL;
d7728c96
JS
2934 struct inode_fs_paths *ipath = NULL;
2935 struct btrfs_path *path;
2936
2937 if (!capable(CAP_SYS_ADMIN))
2938 return -EPERM;
2939
2940 path = btrfs_alloc_path();
2941 if (!path) {
2942 ret = -ENOMEM;
2943 goto out;
2944 }
2945
2946 ipa = memdup_user(arg, sizeof(*ipa));
2947 if (IS_ERR(ipa)) {
2948 ret = PTR_ERR(ipa);
2949 ipa = NULL;
2950 goto out;
2951 }
2952
2953 size = min_t(u32, ipa->size, 4096);
2954 ipath = init_ipath(size, root, path);
2955 if (IS_ERR(ipath)) {
2956 ret = PTR_ERR(ipath);
2957 ipath = NULL;
2958 goto out;
2959 }
2960
2961 ret = paths_from_inode(ipa->inum, ipath);
2962 if (ret < 0)
2963 goto out;
2964
2965 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
745c4d8e
JM
2966 rel_ptr = ipath->fspath->val[i] -
2967 (u64)(unsigned long)ipath->fspath->val;
740c3d22 2968 ipath->fspath->val[i] = rel_ptr;
d7728c96
JS
2969 }
2970
745c4d8e
JM
2971 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
2972 (void *)(unsigned long)ipath->fspath, size);
d7728c96
JS
2973 if (ret) {
2974 ret = -EFAULT;
2975 goto out;
2976 }
2977
2978out:
2979 btrfs_free_path(path);
2980 free_ipath(ipath);
2981 kfree(ipa);
2982
2983 return ret;
2984}
2985
2986static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
2987{
2988 struct btrfs_data_container *inodes = ctx;
2989 const size_t c = 3 * sizeof(u64);
2990
2991 if (inodes->bytes_left >= c) {
2992 inodes->bytes_left -= c;
2993 inodes->val[inodes->elem_cnt] = inum;
2994 inodes->val[inodes->elem_cnt + 1] = offset;
2995 inodes->val[inodes->elem_cnt + 2] = root;
2996 inodes->elem_cnt += 3;
2997 } else {
2998 inodes->bytes_missing += c - inodes->bytes_left;
2999 inodes->bytes_left = 0;
3000 inodes->elem_missed += 3;
3001 }
3002
3003 return 0;
3004}
3005
3006static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3007 void __user *arg)
3008{
3009 int ret = 0;
3010 int size;
3011 u64 extent_offset;
3012 struct btrfs_ioctl_logical_ino_args *loi;
3013 struct btrfs_data_container *inodes = NULL;
3014 struct btrfs_path *path = NULL;
3015 struct btrfs_key key;
3016
3017 if (!capable(CAP_SYS_ADMIN))
3018 return -EPERM;
3019
3020 loi = memdup_user(arg, sizeof(*loi));
3021 if (IS_ERR(loi)) {
3022 ret = PTR_ERR(loi);
3023 loi = NULL;
3024 goto out;
3025 }
3026
3027 path = btrfs_alloc_path();
3028 if (!path) {
3029 ret = -ENOMEM;
3030 goto out;
3031 }
3032
3033 size = min_t(u32, loi->size, 4096);
3034 inodes = init_data_container(size);
3035 if (IS_ERR(inodes)) {
3036 ret = PTR_ERR(inodes);
3037 inodes = NULL;
3038 goto out;
3039 }
3040
3041 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
3042
3043 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3044 ret = -ENOENT;
3045 if (ret < 0)
3046 goto out;
3047
3048 extent_offset = loi->logical - key.objectid;
3049 ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
3050 extent_offset, build_ino_list, inodes);
3051
3052 if (ret < 0)
3053 goto out;
3054
745c4d8e
JM
3055 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3056 (void *)(unsigned long)inodes, size);
d7728c96
JS
3057 if (ret)
3058 ret = -EFAULT;
3059
3060out:
3061 btrfs_free_path(path);
3062 kfree(inodes);
3063 kfree(loi);
3064
3065 return ret;
3066}
3067
c9e9f97b
ID
3068void update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3069 struct btrfs_ioctl_balance_args *bargs)
3070{
3071 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3072
3073 bargs->flags = bctl->flags;
3074
3075 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3076 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3077 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3078}
3079
3080static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3081{
3082 struct btrfs_fs_info *fs_info = root->fs_info;
3083 struct btrfs_ioctl_balance_args *bargs;
3084 struct btrfs_balance_control *bctl;
3085 int ret;
3086
3087 if (!capable(CAP_SYS_ADMIN))
3088 return -EPERM;
3089
3090 if (fs_info->sb->s_flags & MS_RDONLY)
3091 return -EROFS;
3092
3093 mutex_lock(&fs_info->volume_mutex);
3094 mutex_lock(&fs_info->balance_mutex);
3095
3096 if (arg) {
3097 bargs = memdup_user(arg, sizeof(*bargs));
3098 if (IS_ERR(bargs)) {
3099 ret = PTR_ERR(bargs);
3100 goto out;
3101 }
3102 } else {
3103 bargs = NULL;
3104 }
3105
3106 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3107 if (!bctl) {
3108 ret = -ENOMEM;
3109 goto out_bargs;
3110 }
3111
3112 bctl->fs_info = fs_info;
3113 if (arg) {
3114 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3115 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3116 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3117
3118 bctl->flags = bargs->flags;
3119 }
3120
3121 ret = btrfs_balance(bctl, bargs);
3122 /*
3123 * bctl is freed in __cancel_balance
3124 */
3125 if (arg) {
3126 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3127 ret = -EFAULT;
3128 }
3129
3130out_bargs:
3131 kfree(bargs);
3132out:
3133 mutex_unlock(&fs_info->balance_mutex);
3134 mutex_unlock(&fs_info->volume_mutex);
3135 return ret;
3136}
3137
f46b5a66
CH
3138long btrfs_ioctl(struct file *file, unsigned int
3139 cmd, unsigned long arg)
3140{
3141 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4bcabaa3 3142 void __user *argp = (void __user *)arg;
f46b5a66
CH
3143
3144 switch (cmd) {
6cbff00f
CH
3145 case FS_IOC_GETFLAGS:
3146 return btrfs_ioctl_getflags(file, argp);
3147 case FS_IOC_SETFLAGS:
3148 return btrfs_ioctl_setflags(file, argp);
3149 case FS_IOC_GETVERSION:
3150 return btrfs_ioctl_getversion(file, argp);
f7039b1d
LD
3151 case FITRIM:
3152 return btrfs_ioctl_fitrim(file, argp);
f46b5a66 3153 case BTRFS_IOC_SNAP_CREATE:
fa0d2b9b 3154 return btrfs_ioctl_snap_create(file, argp, 0);
fdfb1e4f 3155 case BTRFS_IOC_SNAP_CREATE_V2:
fa0d2b9b 3156 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3de4586c 3157 case BTRFS_IOC_SUBVOL_CREATE:
fa0d2b9b 3158 return btrfs_ioctl_snap_create(file, argp, 1);
76dda93c
YZ
3159 case BTRFS_IOC_SNAP_DESTROY:
3160 return btrfs_ioctl_snap_destroy(file, argp);
0caa102d
LZ
3161 case BTRFS_IOC_SUBVOL_GETFLAGS:
3162 return btrfs_ioctl_subvol_getflags(file, argp);
3163 case BTRFS_IOC_SUBVOL_SETFLAGS:
3164 return btrfs_ioctl_subvol_setflags(file, argp);
6ef5ed0d
JB
3165 case BTRFS_IOC_DEFAULT_SUBVOL:
3166 return btrfs_ioctl_default_subvol(file, argp);
f46b5a66 3167 case BTRFS_IOC_DEFRAG:
1e701a32
CM
3168 return btrfs_ioctl_defrag(file, NULL);
3169 case BTRFS_IOC_DEFRAG_RANGE:
3170 return btrfs_ioctl_defrag(file, argp);
f46b5a66 3171 case BTRFS_IOC_RESIZE:
4bcabaa3 3172 return btrfs_ioctl_resize(root, argp);
f46b5a66 3173 case BTRFS_IOC_ADD_DEV:
4bcabaa3 3174 return btrfs_ioctl_add_dev(root, argp);
f46b5a66 3175 case BTRFS_IOC_RM_DEV:
4bcabaa3 3176 return btrfs_ioctl_rm_dev(root, argp);
475f6387
JS
3177 case BTRFS_IOC_FS_INFO:
3178 return btrfs_ioctl_fs_info(root, argp);
3179 case BTRFS_IOC_DEV_INFO:
3180 return btrfs_ioctl_dev_info(root, argp);
f46b5a66 3181 case BTRFS_IOC_BALANCE:
c9e9f97b 3182 return btrfs_ioctl_balance(root, NULL);
f46b5a66 3183 case BTRFS_IOC_CLONE:
c5c9cd4d
SW
3184 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3185 case BTRFS_IOC_CLONE_RANGE:
7a865e8a 3186 return btrfs_ioctl_clone_range(file, argp);
f46b5a66
CH
3187 case BTRFS_IOC_TRANS_START:
3188 return btrfs_ioctl_trans_start(file);
3189 case BTRFS_IOC_TRANS_END:
3190 return btrfs_ioctl_trans_end(file);
ac8e9819
CM
3191 case BTRFS_IOC_TREE_SEARCH:
3192 return btrfs_ioctl_tree_search(file, argp);
3193 case BTRFS_IOC_INO_LOOKUP:
3194 return btrfs_ioctl_ino_lookup(file, argp);
d7728c96
JS
3195 case BTRFS_IOC_INO_PATHS:
3196 return btrfs_ioctl_ino_to_path(root, argp);
3197 case BTRFS_IOC_LOGICAL_INO:
3198 return btrfs_ioctl_logical_to_ino(root, argp);
1406e432
JB
3199 case BTRFS_IOC_SPACE_INFO:
3200 return btrfs_ioctl_space_info(root, argp);
f46b5a66
CH
3201 case BTRFS_IOC_SYNC:
3202 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3203 return 0;
46204592
SW
3204 case BTRFS_IOC_START_SYNC:
3205 return btrfs_ioctl_start_sync(file, argp);
3206 case BTRFS_IOC_WAIT_SYNC:
3207 return btrfs_ioctl_wait_sync(file, argp);
475f6387
JS
3208 case BTRFS_IOC_SCRUB:
3209 return btrfs_ioctl_scrub(root, argp);
3210 case BTRFS_IOC_SCRUB_CANCEL:
3211 return btrfs_ioctl_scrub_cancel(root, argp);
3212 case BTRFS_IOC_SCRUB_PROGRESS:
3213 return btrfs_ioctl_scrub_progress(root, argp);
c9e9f97b
ID
3214 case BTRFS_IOC_BALANCE_V2:
3215 return btrfs_ioctl_balance(root, argp);
f46b5a66
CH
3216 }
3217
3218 return -ENOTTY;
3219}