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