]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/btrfs/ioctl.c
Btrfs: check return value of btrfs_cow_block()
[mirror_ubuntu-jammy-kernel.git] / fs / btrfs / ioctl.c
CommitLineData
f46b5a66
CH
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
cb8e7090 24#include <linux/fsnotify.h>
f46b5a66
CH
25#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
f46b5a66 30#include <linux/backing-dev.h>
cb8e7090 31#include <linux/mount.h>
f46b5a66 32#include <linux/mpage.h>
cb8e7090 33#include <linux/namei.h>
f46b5a66
CH
34#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
cb8e7090 39#include <linux/security.h>
f46b5a66 40#include <linux/xattr.h>
7ea394f1 41#include <linux/vmalloc.h>
5a0e3ad6 42#include <linux/slab.h>
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;
600a45e1 865 struct extent_io_tree *tree;
3b16a4e3 866 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
4cb5300b
CM
867
868 if (isize == 0)
869 return 0;
870 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
871
872 ret = btrfs_delalloc_reserve_space(inode,
873 num_pages << PAGE_CACHE_SHIFT);
874 if (ret)
875 return ret;
4cb5300b 876 i_done = 0;
600a45e1 877 tree = &BTRFS_I(inode)->io_tree;
4cb5300b
CM
878
879 /* step one, lock all the pages */
880 for (i = 0; i < num_pages; i++) {
881 struct page *page;
600a45e1 882again:
a94733d0 883 page = find_or_create_page(inode->i_mapping,
600a45e1 884 start_index + i, mask);
4cb5300b
CM
885 if (!page)
886 break;
887
600a45e1
MX
888 page_start = page_offset(page);
889 page_end = page_start + PAGE_CACHE_SIZE - 1;
890 while (1) {
891 lock_extent(tree, page_start, page_end, GFP_NOFS);
892 ordered = btrfs_lookup_ordered_extent(inode,
893 page_start);
894 unlock_extent(tree, page_start, page_end, GFP_NOFS);
895 if (!ordered)
896 break;
897
898 unlock_page(page);
899 btrfs_start_ordered_extent(inode, ordered, 1);
900 btrfs_put_ordered_extent(ordered);
901 lock_page(page);
902 }
903
4cb5300b
CM
904 if (!PageUptodate(page)) {
905 btrfs_readpage(NULL, page);
906 lock_page(page);
907 if (!PageUptodate(page)) {
908 unlock_page(page);
909 page_cache_release(page);
910 ret = -EIO;
911 break;
912 }
913 }
600a45e1 914
4cb5300b
CM
915 isize = i_size_read(inode);
916 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
600a45e1 917 if (!isize || page->index > file_end) {
4cb5300b
CM
918 /* whoops, we blew past eof, skip this page */
919 unlock_page(page);
920 page_cache_release(page);
921 break;
922 }
600a45e1
MX
923
924 if (page->mapping != inode->i_mapping) {
925 unlock_page(page);
926 page_cache_release(page);
927 goto again;
928 }
929
4cb5300b
CM
930 pages[i] = page;
931 i_done++;
932 }
933 if (!i_done || ret)
934 goto out;
935
936 if (!(inode->i_sb->s_flags & MS_ACTIVE))
937 goto out;
938
939 /*
940 * so now we have a nice long stream of locked
941 * and up to date pages, lets wait on them
942 */
943 for (i = 0; i < i_done; i++)
944 wait_on_page_writeback(pages[i]);
945
946 page_start = page_offset(pages[0]);
947 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
948
949 lock_extent_bits(&BTRFS_I(inode)->io_tree,
950 page_start, page_end - 1, 0, &cached_state,
951 GFP_NOFS);
4cb5300b
CM
952 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
953 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
954 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
955 GFP_NOFS);
956
957 if (i_done != num_pages) {
9e0baf60
JB
958 spin_lock(&BTRFS_I(inode)->lock);
959 BTRFS_I(inode)->outstanding_extents++;
960 spin_unlock(&BTRFS_I(inode)->lock);
4cb5300b
CM
961 btrfs_delalloc_release_space(inode,
962 (num_pages - i_done) << PAGE_CACHE_SHIFT);
963 }
964
965
966 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
967 &cached_state);
968
969 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
970 page_start, page_end - 1, &cached_state,
971 GFP_NOFS);
972
973 for (i = 0; i < i_done; i++) {
974 clear_page_dirty_for_io(pages[i]);
975 ClearPageChecked(pages[i]);
976 set_page_extent_mapped(pages[i]);
977 set_page_dirty(pages[i]);
978 unlock_page(pages[i]);
979 page_cache_release(pages[i]);
980 }
981 return i_done;
982out:
983 for (i = 0; i < i_done; i++) {
984 unlock_page(pages[i]);
985 page_cache_release(pages[i]);
986 }
987 btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
988 return ret;
989
990}
991
992int btrfs_defrag_file(struct inode *inode, struct file *file,
993 struct btrfs_ioctl_defrag_range_args *range,
994 u64 newer_than, unsigned long max_to_defrag)
995{
996 struct btrfs_root *root = BTRFS_I(inode)->root;
1a419d85 997 struct btrfs_super_block *disk_super;
4cb5300b 998 struct file_ra_state *ra = NULL;
f46b5a66 999 unsigned long last_index;
151a31b2 1000 u64 isize = i_size_read(inode);
1a419d85 1001 u64 features;
940100a4
CM
1002 u64 last_len = 0;
1003 u64 skip = 0;
1004 u64 defrag_end = 0;
4cb5300b 1005 u64 newer_off = range->start;
f46b5a66 1006 unsigned long i;
008873ea 1007 unsigned long ra_index = 0;
f46b5a66 1008 int ret;
4cb5300b 1009 int defrag_count = 0;
1a419d85 1010 int compress_type = BTRFS_COMPRESS_ZLIB;
4cb5300b 1011 int extent_thresh = range->extent_thresh;
008873ea
LZ
1012 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1013 int cluster = max_cluster;
4cb5300b
CM
1014 u64 new_align = ~((u64)128 * 1024 - 1);
1015 struct page **pages = NULL;
1016
1017 if (extent_thresh == 0)
1018 extent_thresh = 256 * 1024;
1a419d85
LZ
1019
1020 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1021 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1022 return -EINVAL;
1023 if (range->compress_type)
1024 compress_type = range->compress_type;
1025 }
f46b5a66 1026
151a31b2 1027 if (isize == 0)
940100a4
CM
1028 return 0;
1029
4cb5300b
CM
1030 /*
1031 * if we were not given a file, allocate a readahead
1032 * context
1033 */
1034 if (!file) {
1035 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1036 if (!ra)
1037 return -ENOMEM;
1038 file_ra_state_init(ra, inode->i_mapping);
1039 } else {
1040 ra = &file->f_ra;
1041 }
1042
008873ea 1043 pages = kmalloc(sizeof(struct page *) * max_cluster,
4cb5300b
CM
1044 GFP_NOFS);
1045 if (!pages) {
1046 ret = -ENOMEM;
1047 goto out_ra;
1048 }
1049
1050 /* find the last page to defrag */
1e701a32 1051 if (range->start + range->len > range->start) {
151a31b2 1052 last_index = min_t(u64, isize - 1,
1e701a32
CM
1053 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1054 } else {
151a31b2 1055 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1e701a32
CM
1056 }
1057
4cb5300b
CM
1058 if (newer_than) {
1059 ret = find_new_extents(root, inode, newer_than,
1060 &newer_off, 64 * 1024);
1061 if (!ret) {
1062 range->start = newer_off;
1063 /*
1064 * we always align our defrag to help keep
1065 * the extents in the file evenly spaced
1066 */
1067 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
4cb5300b
CM
1068 } else
1069 goto out_ra;
1070 } else {
1071 i = range->start >> PAGE_CACHE_SHIFT;
1072 }
1073 if (!max_to_defrag)
7ec31b54 1074 max_to_defrag = last_index + 1;
4cb5300b 1075
2a0f7f57
LZ
1076 /*
1077 * make writeback starts from i, so the defrag range can be
1078 * written sequentially.
1079 */
1080 if (i < inode->i_mapping->writeback_index)
1081 inode->i_mapping->writeback_index = i;
1082
f7f43cc8
CM
1083 while (i <= last_index && defrag_count < max_to_defrag &&
1084 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1085 PAGE_CACHE_SHIFT)) {
4cb5300b
CM
1086 /*
1087 * make sure we stop running if someone unmounts
1088 * the FS
1089 */
1090 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1091 break;
1092
1093 if (!newer_than &&
1094 !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1e701a32 1095 PAGE_CACHE_SIZE,
4cb5300b 1096 extent_thresh,
1e701a32 1097 &last_len, &skip,
940100a4
CM
1098 &defrag_end)) {
1099 unsigned long next;
1100 /*
1101 * the should_defrag function tells us how much to skip
1102 * bump our counter by the suggested amount
1103 */
1104 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1105 i = max(i + 1, next);
1106 continue;
1107 }
008873ea
LZ
1108
1109 if (!newer_than) {
1110 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1111 PAGE_CACHE_SHIFT) - i;
1112 cluster = min(cluster, max_cluster);
1113 } else {
1114 cluster = max_cluster;
1115 }
1116
1e701a32 1117 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1a419d85 1118 BTRFS_I(inode)->force_compress = compress_type;
940100a4 1119
008873ea
LZ
1120 if (i + cluster > ra_index) {
1121 ra_index = max(i, ra_index);
1122 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1123 cluster);
1124 ra_index += max_cluster;
1125 }
940100a4 1126
008873ea 1127 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
4cb5300b
CM
1128 if (ret < 0)
1129 goto out_ra;
1130
1131 defrag_count += ret;
1132 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
4cb5300b
CM
1133
1134 if (newer_than) {
1135 if (newer_off == (u64)-1)
1136 break;
1137
1138 newer_off = max(newer_off + 1,
1139 (u64)i << PAGE_CACHE_SHIFT);
1140
1141 ret = find_new_extents(root, inode,
1142 newer_than, &newer_off,
1143 64 * 1024);
1144 if (!ret) {
1145 range->start = newer_off;
1146 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
4cb5300b
CM
1147 } else {
1148 break;
f46b5a66 1149 }
4cb5300b 1150 } else {
008873ea 1151 if (ret > 0) {
cbcc8326 1152 i += ret;
008873ea
LZ
1153 last_len += ret << PAGE_CACHE_SHIFT;
1154 } else {
cbcc8326 1155 i++;
008873ea
LZ
1156 last_len = 0;
1157 }
f46b5a66 1158 }
f46b5a66
CH
1159 }
1160
1e701a32
CM
1161 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1162 filemap_flush(inode->i_mapping);
1163
1164 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1165 /* the filemap_flush will queue IO into the worker threads, but
1166 * we have to make sure the IO is actually started and that
1167 * ordered extents get created before we return
1168 */
1169 atomic_inc(&root->fs_info->async_submit_draining);
1170 while (atomic_read(&root->fs_info->nr_async_submits) ||
1171 atomic_read(&root->fs_info->async_delalloc_pages)) {
1172 wait_event(root->fs_info->async_submit_wait,
1173 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1174 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1175 }
1176 atomic_dec(&root->fs_info->async_submit_draining);
1177
1178 mutex_lock(&inode->i_mutex);
261507a0 1179 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1e701a32
CM
1180 mutex_unlock(&inode->i_mutex);
1181 }
1182
6c41761f 1183 disk_super = root->fs_info->super_copy;
1a419d85
LZ
1184 features = btrfs_super_incompat_flags(disk_super);
1185 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1186 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1187 btrfs_set_super_incompat_flags(disk_super, features);
1188 }
1189
60ccf82f 1190 ret = defrag_count;
940100a4 1191
4cb5300b
CM
1192out_ra:
1193 if (!file)
1194 kfree(ra);
1195 kfree(pages);
940100a4 1196 return ret;
f46b5a66
CH
1197}
1198
76dda93c
YZ
1199static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1200 void __user *arg)
f46b5a66
CH
1201{
1202 u64 new_size;
1203 u64 old_size;
1204 u64 devid = 1;
1205 struct btrfs_ioctl_vol_args *vol_args;
1206 struct btrfs_trans_handle *trans;
1207 struct btrfs_device *device = NULL;
1208 char *sizestr;
1209 char *devstr = NULL;
1210 int ret = 0;
f46b5a66
CH
1211 int mod = 0;
1212
c146afad
YZ
1213 if (root->fs_info->sb->s_flags & MS_RDONLY)
1214 return -EROFS;
1215
e441d54d
CM
1216 if (!capable(CAP_SYS_ADMIN))
1217 return -EPERM;
1218
c9e9f97b
ID
1219 mutex_lock(&root->fs_info->volume_mutex);
1220 if (root->fs_info->balance_ctl) {
1221 printk(KERN_INFO "btrfs: balance in progress\n");
1222 ret = -EINVAL;
1223 goto out;
1224 }
1225
dae7b665 1226 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
1227 if (IS_ERR(vol_args)) {
1228 ret = PTR_ERR(vol_args);
1229 goto out;
1230 }
5516e595
MF
1231
1232 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 1233
f46b5a66
CH
1234 sizestr = vol_args->name;
1235 devstr = strchr(sizestr, ':');
1236 if (devstr) {
1237 char *end;
1238 sizestr = devstr + 1;
1239 *devstr = '\0';
1240 devstr = vol_args->name;
1241 devid = simple_strtoull(devstr, &end, 10);
5bb14682 1242 printk(KERN_INFO "btrfs: resizing devid %llu\n",
21380931 1243 (unsigned long long)devid);
f46b5a66 1244 }
2b82032c 1245 device = btrfs_find_device(root, devid, NULL, NULL);
f46b5a66 1246 if (!device) {
5bb14682 1247 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
21380931 1248 (unsigned long long)devid);
f46b5a66 1249 ret = -EINVAL;
c9e9f97b 1250 goto out_free;
f46b5a66
CH
1251 }
1252 if (!strcmp(sizestr, "max"))
1253 new_size = device->bdev->bd_inode->i_size;
1254 else {
1255 if (sizestr[0] == '-') {
1256 mod = -1;
1257 sizestr++;
1258 } else if (sizestr[0] == '+') {
1259 mod = 1;
1260 sizestr++;
1261 }
91748467 1262 new_size = memparse(sizestr, NULL);
f46b5a66
CH
1263 if (new_size == 0) {
1264 ret = -EINVAL;
c9e9f97b 1265 goto out_free;
f46b5a66
CH
1266 }
1267 }
1268
1269 old_size = device->total_bytes;
1270
1271 if (mod < 0) {
1272 if (new_size > old_size) {
1273 ret = -EINVAL;
c9e9f97b 1274 goto out_free;
f46b5a66
CH
1275 }
1276 new_size = old_size - new_size;
1277 } else if (mod > 0) {
1278 new_size = old_size + new_size;
1279 }
1280
1281 if (new_size < 256 * 1024 * 1024) {
1282 ret = -EINVAL;
c9e9f97b 1283 goto out_free;
f46b5a66
CH
1284 }
1285 if (new_size > device->bdev->bd_inode->i_size) {
1286 ret = -EFBIG;
c9e9f97b 1287 goto out_free;
f46b5a66
CH
1288 }
1289
1290 do_div(new_size, root->sectorsize);
1291 new_size *= root->sectorsize;
1292
5bb14682 1293 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
f46b5a66
CH
1294 device->name, (unsigned long long)new_size);
1295
1296 if (new_size > old_size) {
a22285a6 1297 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
1298 if (IS_ERR(trans)) {
1299 ret = PTR_ERR(trans);
c9e9f97b 1300 goto out_free;
98d5dc13 1301 }
f46b5a66
CH
1302 ret = btrfs_grow_device(trans, device, new_size);
1303 btrfs_commit_transaction(trans, root);
ece7d20e 1304 } else if (new_size < old_size) {
f46b5a66
CH
1305 ret = btrfs_shrink_device(device, new_size);
1306 }
1307
c9e9f97b 1308out_free:
f46b5a66 1309 kfree(vol_args);
c9e9f97b
ID
1310out:
1311 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
1312 return ret;
1313}
1314
72fd032e
SW
1315static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1316 char *name,
1317 unsigned long fd,
1318 int subvol,
b83cc969
LZ
1319 u64 *transid,
1320 bool readonly)
f46b5a66 1321{
cb8e7090 1322 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3de4586c 1323 struct file *src_file;
f46b5a66 1324 int namelen;
3de4586c 1325 int ret = 0;
f46b5a66 1326
c146afad
YZ
1327 if (root->fs_info->sb->s_flags & MS_RDONLY)
1328 return -EROFS;
1329
72fd032e
SW
1330 namelen = strlen(name);
1331 if (strchr(name, '/')) {
f46b5a66
CH
1332 ret = -EINVAL;
1333 goto out;
1334 }
1335
16780cab
CM
1336 if (name[0] == '.' &&
1337 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1338 ret = -EEXIST;
1339 goto out;
1340 }
1341
3de4586c 1342 if (subvol) {
72fd032e 1343 ret = btrfs_mksubvol(&file->f_path, name, namelen,
b83cc969 1344 NULL, transid, readonly);
cb8e7090 1345 } else {
3de4586c 1346 struct inode *src_inode;
72fd032e 1347 src_file = fget(fd);
3de4586c
CM
1348 if (!src_file) {
1349 ret = -EINVAL;
1350 goto out;
1351 }
1352
1353 src_inode = src_file->f_path.dentry->d_inode;
1354 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
d397712b
CM
1355 printk(KERN_INFO "btrfs: Snapshot src from "
1356 "another FS\n");
3de4586c
CM
1357 ret = -EINVAL;
1358 fput(src_file);
1359 goto out;
1360 }
72fd032e
SW
1361 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1362 BTRFS_I(src_inode)->root,
b83cc969 1363 transid, readonly);
3de4586c 1364 fput(src_file);
cb8e7090 1365 }
f46b5a66 1366out:
72fd032e
SW
1367 return ret;
1368}
1369
1370static noinline int btrfs_ioctl_snap_create(struct file *file,
fa0d2b9b 1371 void __user *arg, int subvol)
72fd032e 1372{
fa0d2b9b 1373 struct btrfs_ioctl_vol_args *vol_args;
72fd032e
SW
1374 int ret;
1375
fa0d2b9b
LZ
1376 vol_args = memdup_user(arg, sizeof(*vol_args));
1377 if (IS_ERR(vol_args))
1378 return PTR_ERR(vol_args);
1379 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
72fd032e 1380
fa0d2b9b 1381 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
b83cc969
LZ
1382 vol_args->fd, subvol,
1383 NULL, false);
fdfb1e4f 1384
fa0d2b9b
LZ
1385 kfree(vol_args);
1386 return ret;
1387}
fdfb1e4f 1388
fa0d2b9b
LZ
1389static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1390 void __user *arg, int subvol)
1391{
1392 struct btrfs_ioctl_vol_args_v2 *vol_args;
1393 int ret;
1394 u64 transid = 0;
1395 u64 *ptr = NULL;
b83cc969 1396 bool readonly = false;
75eaa0e2 1397
fa0d2b9b
LZ
1398 vol_args = memdup_user(arg, sizeof(*vol_args));
1399 if (IS_ERR(vol_args))
1400 return PTR_ERR(vol_args);
1401 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
75eaa0e2 1402
b83cc969
LZ
1403 if (vol_args->flags &
1404 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1405 ret = -EOPNOTSUPP;
fa0d2b9b 1406 goto out;
72fd032e 1407 }
fa0d2b9b
LZ
1408
1409 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1410 ptr = &transid;
b83cc969
LZ
1411 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1412 readonly = true;
fa0d2b9b
LZ
1413
1414 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
b83cc969
LZ
1415 vol_args->fd, subvol,
1416 ptr, readonly);
fa0d2b9b
LZ
1417
1418 if (ret == 0 && ptr &&
1419 copy_to_user(arg +
1420 offsetof(struct btrfs_ioctl_vol_args_v2,
1421 transid), ptr, sizeof(*ptr)))
1422 ret = -EFAULT;
fdfb1e4f 1423out:
f46b5a66
CH
1424 kfree(vol_args);
1425 return ret;
1426}
1427
0caa102d
LZ
1428static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1429 void __user *arg)
1430{
1431 struct inode *inode = fdentry(file)->d_inode;
1432 struct btrfs_root *root = BTRFS_I(inode)->root;
1433 int ret = 0;
1434 u64 flags = 0;
1435
33345d01 1436 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
0caa102d
LZ
1437 return -EINVAL;
1438
1439 down_read(&root->fs_info->subvol_sem);
1440 if (btrfs_root_readonly(root))
1441 flags |= BTRFS_SUBVOL_RDONLY;
1442 up_read(&root->fs_info->subvol_sem);
1443
1444 if (copy_to_user(arg, &flags, sizeof(flags)))
1445 ret = -EFAULT;
1446
1447 return ret;
1448}
1449
1450static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1451 void __user *arg)
1452{
1453 struct inode *inode = fdentry(file)->d_inode;
1454 struct btrfs_root *root = BTRFS_I(inode)->root;
1455 struct btrfs_trans_handle *trans;
1456 u64 root_flags;
1457 u64 flags;
1458 int ret = 0;
1459
1460 if (root->fs_info->sb->s_flags & MS_RDONLY)
1461 return -EROFS;
1462
33345d01 1463 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
0caa102d
LZ
1464 return -EINVAL;
1465
1466 if (copy_from_user(&flags, arg, sizeof(flags)))
1467 return -EFAULT;
1468
b4dc2b8c 1469 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
0caa102d
LZ
1470 return -EINVAL;
1471
1472 if (flags & ~BTRFS_SUBVOL_RDONLY)
1473 return -EOPNOTSUPP;
1474
2e149670 1475 if (!inode_owner_or_capable(inode))
b4dc2b8c
LZ
1476 return -EACCES;
1477
0caa102d
LZ
1478 down_write(&root->fs_info->subvol_sem);
1479
1480 /* nothing to do */
1481 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1482 goto out;
1483
1484 root_flags = btrfs_root_flags(&root->root_item);
1485 if (flags & BTRFS_SUBVOL_RDONLY)
1486 btrfs_set_root_flags(&root->root_item,
1487 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1488 else
1489 btrfs_set_root_flags(&root->root_item,
1490 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1491
1492 trans = btrfs_start_transaction(root, 1);
1493 if (IS_ERR(trans)) {
1494 ret = PTR_ERR(trans);
1495 goto out_reset;
1496 }
1497
b4dc2b8c 1498 ret = btrfs_update_root(trans, root->fs_info->tree_root,
0caa102d
LZ
1499 &root->root_key, &root->root_item);
1500
1501 btrfs_commit_transaction(trans, root);
1502out_reset:
1503 if (ret)
1504 btrfs_set_root_flags(&root->root_item, root_flags);
1505out:
1506 up_write(&root->fs_info->subvol_sem);
1507 return ret;
1508}
1509
76dda93c
YZ
1510/*
1511 * helper to check if the subvolume references other subvolumes
1512 */
1513static noinline int may_destroy_subvol(struct btrfs_root *root)
1514{
1515 struct btrfs_path *path;
1516 struct btrfs_key key;
1517 int ret;
1518
1519 path = btrfs_alloc_path();
1520 if (!path)
1521 return -ENOMEM;
1522
1523 key.objectid = root->root_key.objectid;
1524 key.type = BTRFS_ROOT_REF_KEY;
1525 key.offset = (u64)-1;
1526
1527 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1528 &key, path, 0, 0);
1529 if (ret < 0)
1530 goto out;
1531 BUG_ON(ret == 0);
1532
1533 ret = 0;
1534 if (path->slots[0] > 0) {
1535 path->slots[0]--;
1536 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1537 if (key.objectid == root->root_key.objectid &&
1538 key.type == BTRFS_ROOT_REF_KEY)
1539 ret = -ENOTEMPTY;
1540 }
1541out:
1542 btrfs_free_path(path);
1543 return ret;
1544}
1545
ac8e9819
CM
1546static noinline int key_in_sk(struct btrfs_key *key,
1547 struct btrfs_ioctl_search_key *sk)
1548{
abc6e134
CM
1549 struct btrfs_key test;
1550 int ret;
1551
1552 test.objectid = sk->min_objectid;
1553 test.type = sk->min_type;
1554 test.offset = sk->min_offset;
1555
1556 ret = btrfs_comp_cpu_keys(key, &test);
1557 if (ret < 0)
ac8e9819 1558 return 0;
abc6e134
CM
1559
1560 test.objectid = sk->max_objectid;
1561 test.type = sk->max_type;
1562 test.offset = sk->max_offset;
1563
1564 ret = btrfs_comp_cpu_keys(key, &test);
1565 if (ret > 0)
ac8e9819
CM
1566 return 0;
1567 return 1;
1568}
1569
1570static noinline int copy_to_sk(struct btrfs_root *root,
1571 struct btrfs_path *path,
1572 struct btrfs_key *key,
1573 struct btrfs_ioctl_search_key *sk,
1574 char *buf,
1575 unsigned long *sk_offset,
1576 int *num_found)
1577{
1578 u64 found_transid;
1579 struct extent_buffer *leaf;
1580 struct btrfs_ioctl_search_header sh;
1581 unsigned long item_off;
1582 unsigned long item_len;
1583 int nritems;
1584 int i;
1585 int slot;
ac8e9819
CM
1586 int ret = 0;
1587
1588 leaf = path->nodes[0];
1589 slot = path->slots[0];
1590 nritems = btrfs_header_nritems(leaf);
1591
1592 if (btrfs_header_generation(leaf) > sk->max_transid) {
1593 i = nritems;
1594 goto advance_key;
1595 }
1596 found_transid = btrfs_header_generation(leaf);
1597
1598 for (i = slot; i < nritems; i++) {
1599 item_off = btrfs_item_ptr_offset(leaf, i);
1600 item_len = btrfs_item_size_nr(leaf, i);
1601
1602 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1603 item_len = 0;
1604
1605 if (sizeof(sh) + item_len + *sk_offset >
1606 BTRFS_SEARCH_ARGS_BUFSIZE) {
1607 ret = 1;
1608 goto overflow;
1609 }
1610
1611 btrfs_item_key_to_cpu(leaf, key, i);
1612 if (!key_in_sk(key, sk))
1613 continue;
1614
1615 sh.objectid = key->objectid;
1616 sh.offset = key->offset;
1617 sh.type = key->type;
1618 sh.len = item_len;
1619 sh.transid = found_transid;
1620
1621 /* copy search result header */
1622 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1623 *sk_offset += sizeof(sh);
1624
1625 if (item_len) {
1626 char *p = buf + *sk_offset;
1627 /* copy the item */
1628 read_extent_buffer(leaf, p,
1629 item_off, item_len);
1630 *sk_offset += item_len;
ac8e9819 1631 }
e2156867 1632 (*num_found)++;
ac8e9819
CM
1633
1634 if (*num_found >= sk->nr_items)
1635 break;
1636 }
1637advance_key:
abc6e134
CM
1638 ret = 0;
1639 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
ac8e9819 1640 key->offset++;
abc6e134
CM
1641 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1642 key->offset = 0;
ac8e9819 1643 key->type++;
abc6e134
CM
1644 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1645 key->offset = 0;
1646 key->type = 0;
ac8e9819 1647 key->objectid++;
abc6e134
CM
1648 } else
1649 ret = 1;
ac8e9819 1650overflow:
ac8e9819
CM
1651 return ret;
1652}
1653
1654static noinline int search_ioctl(struct inode *inode,
1655 struct btrfs_ioctl_search_args *args)
1656{
1657 struct btrfs_root *root;
1658 struct btrfs_key key;
1659 struct btrfs_key max_key;
1660 struct btrfs_path *path;
1661 struct btrfs_ioctl_search_key *sk = &args->key;
1662 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1663 int ret;
1664 int num_found = 0;
1665 unsigned long sk_offset = 0;
1666
1667 path = btrfs_alloc_path();
1668 if (!path)
1669 return -ENOMEM;
1670
1671 if (sk->tree_id == 0) {
1672 /* search the root of the inode that was passed */
1673 root = BTRFS_I(inode)->root;
1674 } else {
1675 key.objectid = sk->tree_id;
1676 key.type = BTRFS_ROOT_ITEM_KEY;
1677 key.offset = (u64)-1;
1678 root = btrfs_read_fs_root_no_name(info, &key);
1679 if (IS_ERR(root)) {
1680 printk(KERN_ERR "could not find root %llu\n",
1681 sk->tree_id);
1682 btrfs_free_path(path);
1683 return -ENOENT;
1684 }
1685 }
1686
1687 key.objectid = sk->min_objectid;
1688 key.type = sk->min_type;
1689 key.offset = sk->min_offset;
1690
1691 max_key.objectid = sk->max_objectid;
1692 max_key.type = sk->max_type;
1693 max_key.offset = sk->max_offset;
1694
1695 path->keep_locks = 1;
1696
1697 while(1) {
1698 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1699 sk->min_transid);
1700 if (ret != 0) {
1701 if (ret > 0)
1702 ret = 0;
1703 goto err;
1704 }
1705 ret = copy_to_sk(root, path, &key, sk, args->buf,
1706 &sk_offset, &num_found);
b3b4aa74 1707 btrfs_release_path(path);
ac8e9819
CM
1708 if (ret || num_found >= sk->nr_items)
1709 break;
1710
1711 }
1712 ret = 0;
1713err:
1714 sk->nr_items = num_found;
1715 btrfs_free_path(path);
1716 return ret;
1717}
1718
1719static noinline int btrfs_ioctl_tree_search(struct file *file,
1720 void __user *argp)
1721{
1722 struct btrfs_ioctl_search_args *args;
1723 struct inode *inode;
1724 int ret;
1725
1726 if (!capable(CAP_SYS_ADMIN))
1727 return -EPERM;
1728
2354d08f
JL
1729 args = memdup_user(argp, sizeof(*args));
1730 if (IS_ERR(args))
1731 return PTR_ERR(args);
ac8e9819 1732
ac8e9819
CM
1733 inode = fdentry(file)->d_inode;
1734 ret = search_ioctl(inode, args);
1735 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1736 ret = -EFAULT;
1737 kfree(args);
1738 return ret;
1739}
1740
98d377a0 1741/*
ac8e9819
CM
1742 * Search INODE_REFs to identify path name of 'dirid' directory
1743 * in a 'tree_id' tree. and sets path name to 'name'.
1744 */
98d377a0
TH
1745static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1746 u64 tree_id, u64 dirid, char *name)
1747{
1748 struct btrfs_root *root;
1749 struct btrfs_key key;
ac8e9819 1750 char *ptr;
98d377a0
TH
1751 int ret = -1;
1752 int slot;
1753 int len;
1754 int total_len = 0;
1755 struct btrfs_inode_ref *iref;
1756 struct extent_buffer *l;
1757 struct btrfs_path *path;
1758
1759 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1760 name[0]='\0';
1761 return 0;
1762 }
1763
1764 path = btrfs_alloc_path();
1765 if (!path)
1766 return -ENOMEM;
1767
ac8e9819 1768 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
98d377a0
TH
1769
1770 key.objectid = tree_id;
1771 key.type = BTRFS_ROOT_ITEM_KEY;
1772 key.offset = (u64)-1;
1773 root = btrfs_read_fs_root_no_name(info, &key);
1774 if (IS_ERR(root)) {
1775 printk(KERN_ERR "could not find root %llu\n", tree_id);
8ad6fcab
CM
1776 ret = -ENOENT;
1777 goto out;
98d377a0
TH
1778 }
1779
1780 key.objectid = dirid;
1781 key.type = BTRFS_INODE_REF_KEY;
8ad6fcab 1782 key.offset = (u64)-1;
98d377a0
TH
1783
1784 while(1) {
1785 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1786 if (ret < 0)
1787 goto out;
1788
1789 l = path->nodes[0];
1790 slot = path->slots[0];
8ad6fcab
CM
1791 if (ret > 0 && slot > 0)
1792 slot--;
98d377a0
TH
1793 btrfs_item_key_to_cpu(l, &key, slot);
1794
1795 if (ret > 0 && (key.objectid != dirid ||
ac8e9819
CM
1796 key.type != BTRFS_INODE_REF_KEY)) {
1797 ret = -ENOENT;
98d377a0 1798 goto out;
ac8e9819 1799 }
98d377a0
TH
1800
1801 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1802 len = btrfs_inode_ref_name_len(l, iref);
1803 ptr -= len + 1;
1804 total_len += len + 1;
ac8e9819 1805 if (ptr < name)
98d377a0
TH
1806 goto out;
1807
1808 *(ptr + len) = '/';
1809 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1810
1811 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1812 break;
1813
b3b4aa74 1814 btrfs_release_path(path);
98d377a0 1815 key.objectid = key.offset;
8ad6fcab 1816 key.offset = (u64)-1;
98d377a0 1817 dirid = key.objectid;
98d377a0 1818 }
ac8e9819 1819 if (ptr < name)
98d377a0 1820 goto out;
77906a50 1821 memmove(name, ptr, total_len);
98d377a0
TH
1822 name[total_len]='\0';
1823 ret = 0;
1824out:
1825 btrfs_free_path(path);
ac8e9819
CM
1826 return ret;
1827}
1828
1829static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1830 void __user *argp)
1831{
1832 struct btrfs_ioctl_ino_lookup_args *args;
1833 struct inode *inode;
1834 int ret;
1835
1836 if (!capable(CAP_SYS_ADMIN))
1837 return -EPERM;
1838
2354d08f
JL
1839 args = memdup_user(argp, sizeof(*args));
1840 if (IS_ERR(args))
1841 return PTR_ERR(args);
c2b96929 1842
ac8e9819
CM
1843 inode = fdentry(file)->d_inode;
1844
1b53ac4d
CM
1845 if (args->treeid == 0)
1846 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1847
ac8e9819
CM
1848 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1849 args->treeid, args->objectid,
1850 args->name);
1851
1852 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1853 ret = -EFAULT;
1854
1855 kfree(args);
98d377a0
TH
1856 return ret;
1857}
1858
76dda93c
YZ
1859static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1860 void __user *arg)
1861{
1862 struct dentry *parent = fdentry(file);
1863 struct dentry *dentry;
1864 struct inode *dir = parent->d_inode;
1865 struct inode *inode;
1866 struct btrfs_root *root = BTRFS_I(dir)->root;
1867 struct btrfs_root *dest = NULL;
1868 struct btrfs_ioctl_vol_args *vol_args;
1869 struct btrfs_trans_handle *trans;
1870 int namelen;
1871 int ret;
1872 int err = 0;
1873
76dda93c
YZ
1874 vol_args = memdup_user(arg, sizeof(*vol_args));
1875 if (IS_ERR(vol_args))
1876 return PTR_ERR(vol_args);
1877
1878 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1879 namelen = strlen(vol_args->name);
1880 if (strchr(vol_args->name, '/') ||
1881 strncmp(vol_args->name, "..", namelen) == 0) {
1882 err = -EINVAL;
1883 goto out;
1884 }
1885
1886 err = mnt_want_write(file->f_path.mnt);
1887 if (err)
1888 goto out;
1889
1890 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1891 dentry = lookup_one_len(vol_args->name, parent, namelen);
1892 if (IS_ERR(dentry)) {
1893 err = PTR_ERR(dentry);
1894 goto out_unlock_dir;
1895 }
1896
1897 if (!dentry->d_inode) {
1898 err = -ENOENT;
1899 goto out_dput;
1900 }
1901
1902 inode = dentry->d_inode;
4260f7c7
SW
1903 dest = BTRFS_I(inode)->root;
1904 if (!capable(CAP_SYS_ADMIN)){
1905 /*
1906 * Regular user. Only allow this with a special mount
1907 * option, when the user has write+exec access to the
1908 * subvol root, and when rmdir(2) would have been
1909 * allowed.
1910 *
1911 * Note that this is _not_ check that the subvol is
1912 * empty or doesn't contain data that we wouldn't
1913 * otherwise be able to delete.
1914 *
1915 * Users who want to delete empty subvols should try
1916 * rmdir(2).
1917 */
1918 err = -EPERM;
1919 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1920 goto out_dput;
1921
1922 /*
1923 * Do not allow deletion if the parent dir is the same
1924 * as the dir to be deleted. That means the ioctl
1925 * must be called on the dentry referencing the root
1926 * of the subvol, not a random directory contained
1927 * within it.
1928 */
1929 err = -EINVAL;
1930 if (root == dest)
1931 goto out_dput;
1932
1933 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1934 if (err)
1935 goto out_dput;
1936
1937 /* check if subvolume may be deleted by a non-root user */
1938 err = btrfs_may_delete(dir, dentry, 1);
1939 if (err)
1940 goto out_dput;
1941 }
1942
33345d01 1943 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
76dda93c
YZ
1944 err = -EINVAL;
1945 goto out_dput;
1946 }
1947
76dda93c
YZ
1948 mutex_lock(&inode->i_mutex);
1949 err = d_invalidate(dentry);
1950 if (err)
1951 goto out_unlock;
1952
1953 down_write(&root->fs_info->subvol_sem);
1954
1955 err = may_destroy_subvol(dest);
1956 if (err)
1957 goto out_up_write;
1958
a22285a6
YZ
1959 trans = btrfs_start_transaction(root, 0);
1960 if (IS_ERR(trans)) {
1961 err = PTR_ERR(trans);
d327099a 1962 goto out_up_write;
a22285a6
YZ
1963 }
1964 trans->block_rsv = &root->fs_info->global_block_rsv;
1965
76dda93c
YZ
1966 ret = btrfs_unlink_subvol(trans, root, dir,
1967 dest->root_key.objectid,
1968 dentry->d_name.name,
1969 dentry->d_name.len);
1970 BUG_ON(ret);
1971
1972 btrfs_record_root_in_trans(trans, dest);
1973
1974 memset(&dest->root_item.drop_progress, 0,
1975 sizeof(dest->root_item.drop_progress));
1976 dest->root_item.drop_level = 0;
1977 btrfs_set_root_refs(&dest->root_item, 0);
1978
d68fc57b
YZ
1979 if (!xchg(&dest->orphan_item_inserted, 1)) {
1980 ret = btrfs_insert_orphan_item(trans,
1981 root->fs_info->tree_root,
1982 dest->root_key.objectid);
1983 BUG_ON(ret);
1984 }
76dda93c 1985
531cb13f 1986 ret = btrfs_end_transaction(trans, root);
76dda93c
YZ
1987 BUG_ON(ret);
1988 inode->i_flags |= S_DEAD;
1989out_up_write:
1990 up_write(&root->fs_info->subvol_sem);
1991out_unlock:
1992 mutex_unlock(&inode->i_mutex);
1993 if (!err) {
efefb143 1994 shrink_dcache_sb(root->fs_info->sb);
76dda93c
YZ
1995 btrfs_invalidate_inodes(dest);
1996 d_delete(dentry);
1997 }
1998out_dput:
1999 dput(dentry);
2000out_unlock_dir:
2001 mutex_unlock(&dir->i_mutex);
2002 mnt_drop_write(file->f_path.mnt);
2003out:
2004 kfree(vol_args);
2005 return err;
2006}
2007
1e701a32 2008static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
f46b5a66
CH
2009{
2010 struct inode *inode = fdentry(file)->d_inode;
2011 struct btrfs_root *root = BTRFS_I(inode)->root;
1e701a32 2012 struct btrfs_ioctl_defrag_range_args *range;
c146afad
YZ
2013 int ret;
2014
b83cc969
LZ
2015 if (btrfs_root_readonly(root))
2016 return -EROFS;
2017
c146afad
YZ
2018 ret = mnt_want_write(file->f_path.mnt);
2019 if (ret)
2020 return ret;
f46b5a66
CH
2021
2022 switch (inode->i_mode & S_IFMT) {
2023 case S_IFDIR:
e441d54d
CM
2024 if (!capable(CAP_SYS_ADMIN)) {
2025 ret = -EPERM;
2026 goto out;
2027 }
8929ecfa
YZ
2028 ret = btrfs_defrag_root(root, 0);
2029 if (ret)
2030 goto out;
2031 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
f46b5a66
CH
2032 break;
2033 case S_IFREG:
e441d54d
CM
2034 if (!(file->f_mode & FMODE_WRITE)) {
2035 ret = -EINVAL;
2036 goto out;
2037 }
1e701a32
CM
2038
2039 range = kzalloc(sizeof(*range), GFP_KERNEL);
2040 if (!range) {
2041 ret = -ENOMEM;
2042 goto out;
2043 }
2044
2045 if (argp) {
2046 if (copy_from_user(range, argp,
2047 sizeof(*range))) {
2048 ret = -EFAULT;
2049 kfree(range);
683be16e 2050 goto out;
1e701a32
CM
2051 }
2052 /* compression requires us to start the IO */
2053 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2054 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2055 range->extent_thresh = (u32)-1;
2056 }
2057 } else {
2058 /* the rest are all set to zero by kzalloc */
2059 range->len = (u64)-1;
2060 }
4cb5300b
CM
2061 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2062 range, 0, 0);
2063 if (ret > 0)
2064 ret = 0;
1e701a32 2065 kfree(range);
f46b5a66 2066 break;
8929ecfa
YZ
2067 default:
2068 ret = -EINVAL;
f46b5a66 2069 }
e441d54d 2070out:
ab67b7c1 2071 mnt_drop_write(file->f_path.mnt);
e441d54d 2072 return ret;
f46b5a66
CH
2073}
2074
b2950863 2075static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
2076{
2077 struct btrfs_ioctl_vol_args *vol_args;
2078 int ret;
2079
e441d54d
CM
2080 if (!capable(CAP_SYS_ADMIN))
2081 return -EPERM;
2082
c9e9f97b
ID
2083 mutex_lock(&root->fs_info->volume_mutex);
2084 if (root->fs_info->balance_ctl) {
2085 printk(KERN_INFO "btrfs: balance in progress\n");
2086 ret = -EINVAL;
2087 goto out;
2088 }
2089
dae7b665 2090 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2091 if (IS_ERR(vol_args)) {
2092 ret = PTR_ERR(vol_args);
2093 goto out;
2094 }
f46b5a66 2095
5516e595 2096 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
2097 ret = btrfs_init_new_device(root, vol_args->name);
2098
f46b5a66 2099 kfree(vol_args);
c9e9f97b
ID
2100out:
2101 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
2102 return ret;
2103}
2104
b2950863 2105static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
2106{
2107 struct btrfs_ioctl_vol_args *vol_args;
2108 int ret;
2109
e441d54d
CM
2110 if (!capable(CAP_SYS_ADMIN))
2111 return -EPERM;
2112
c146afad
YZ
2113 if (root->fs_info->sb->s_flags & MS_RDONLY)
2114 return -EROFS;
2115
c9e9f97b
ID
2116 mutex_lock(&root->fs_info->volume_mutex);
2117 if (root->fs_info->balance_ctl) {
2118 printk(KERN_INFO "btrfs: balance in progress\n");
2119 ret = -EINVAL;
2120 goto out;
2121 }
2122
dae7b665 2123 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2124 if (IS_ERR(vol_args)) {
2125 ret = PTR_ERR(vol_args);
2126 goto out;
2127 }
f46b5a66 2128
5516e595 2129 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
2130 ret = btrfs_rm_device(root, vol_args->name);
2131
f46b5a66 2132 kfree(vol_args);
c9e9f97b
ID
2133out:
2134 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
2135 return ret;
2136}
2137
475f6387
JS
2138static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2139{
027ed2f0 2140 struct btrfs_ioctl_fs_info_args *fi_args;
475f6387
JS
2141 struct btrfs_device *device;
2142 struct btrfs_device *next;
2143 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
027ed2f0 2144 int ret = 0;
475f6387
JS
2145
2146 if (!capable(CAP_SYS_ADMIN))
2147 return -EPERM;
2148
027ed2f0
LZ
2149 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2150 if (!fi_args)
2151 return -ENOMEM;
2152
2153 fi_args->num_devices = fs_devices->num_devices;
2154 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
475f6387
JS
2155
2156 mutex_lock(&fs_devices->device_list_mutex);
2157 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
027ed2f0
LZ
2158 if (device->devid > fi_args->max_id)
2159 fi_args->max_id = device->devid;
475f6387
JS
2160 }
2161 mutex_unlock(&fs_devices->device_list_mutex);
2162
027ed2f0
LZ
2163 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2164 ret = -EFAULT;
475f6387 2165
027ed2f0
LZ
2166 kfree(fi_args);
2167 return ret;
475f6387
JS
2168}
2169
2170static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2171{
2172 struct btrfs_ioctl_dev_info_args *di_args;
2173 struct btrfs_device *dev;
2174 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2175 int ret = 0;
2176 char *s_uuid = NULL;
2177 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2178
2179 if (!capable(CAP_SYS_ADMIN))
2180 return -EPERM;
2181
2182 di_args = memdup_user(arg, sizeof(*di_args));
2183 if (IS_ERR(di_args))
2184 return PTR_ERR(di_args);
2185
2186 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2187 s_uuid = di_args->uuid;
2188
2189 mutex_lock(&fs_devices->device_list_mutex);
2190 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2191 mutex_unlock(&fs_devices->device_list_mutex);
2192
2193 if (!dev) {
2194 ret = -ENODEV;
2195 goto out;
2196 }
2197
2198 di_args->devid = dev->devid;
2199 di_args->bytes_used = dev->bytes_used;
2200 di_args->total_bytes = dev->total_bytes;
2201 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2202 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2203
2204out:
2205 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2206 ret = -EFAULT;
2207
2208 kfree(di_args);
2209 return ret;
2210}
2211
76dda93c
YZ
2212static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2213 u64 off, u64 olen, u64 destoff)
f46b5a66
CH
2214{
2215 struct inode *inode = fdentry(file)->d_inode;
2216 struct btrfs_root *root = BTRFS_I(inode)->root;
2217 struct file *src_file;
2218 struct inode *src;
2219 struct btrfs_trans_handle *trans;
f46b5a66 2220 struct btrfs_path *path;
f46b5a66 2221 struct extent_buffer *leaf;
ae01a0ab
YZ
2222 char *buf;
2223 struct btrfs_key key;
f46b5a66
CH
2224 u32 nritems;
2225 int slot;
ae01a0ab 2226 int ret;
c5c9cd4d
SW
2227 u64 len = olen;
2228 u64 bs = root->fs_info->sb->s_blocksize;
2229 u64 hint_byte;
d20f7043 2230
c5c9cd4d
SW
2231 /*
2232 * TODO:
2233 * - split compressed inline extents. annoying: we need to
2234 * decompress into destination's address_space (the file offset
2235 * may change, so source mapping won't do), then recompress (or
2236 * otherwise reinsert) a subrange.
2237 * - allow ranges within the same file to be cloned (provided
2238 * they don't overlap)?
2239 */
2240
e441d54d 2241 /* the destination must be opened for writing */
2ebc3464 2242 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
e441d54d
CM
2243 return -EINVAL;
2244
b83cc969
LZ
2245 if (btrfs_root_readonly(root))
2246 return -EROFS;
2247
c146afad
YZ
2248 ret = mnt_want_write(file->f_path.mnt);
2249 if (ret)
2250 return ret;
2251
c5c9cd4d 2252 src_file = fget(srcfd);
ab67b7c1
YZ
2253 if (!src_file) {
2254 ret = -EBADF;
2255 goto out_drop_write;
2256 }
5dc64164 2257
f46b5a66
CH
2258 src = src_file->f_dentry->d_inode;
2259
c5c9cd4d
SW
2260 ret = -EINVAL;
2261 if (src == inode)
2262 goto out_fput;
2263
5dc64164
DR
2264 /* the src must be open for reading */
2265 if (!(src_file->f_mode & FMODE_READ))
2266 goto out_fput;
2267
0e7b824c
LZ
2268 /* don't make the dst file partly checksummed */
2269 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2270 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2271 goto out_fput;
2272
ae01a0ab
YZ
2273 ret = -EISDIR;
2274 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2275 goto out_fput;
2276
f46b5a66 2277 ret = -EXDEV;
ae01a0ab
YZ
2278 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2279 goto out_fput;
2280
2281 ret = -ENOMEM;
2282 buf = vmalloc(btrfs_level_size(root, 0));
2283 if (!buf)
2284 goto out_fput;
2285
2286 path = btrfs_alloc_path();
2287 if (!path) {
2288 vfree(buf);
f46b5a66 2289 goto out_fput;
ae01a0ab
YZ
2290 }
2291 path->reada = 2;
f46b5a66
CH
2292
2293 if (inode < src) {
fccdae43
SW
2294 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2295 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
f46b5a66 2296 } else {
fccdae43
SW
2297 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2298 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
f46b5a66
CH
2299 }
2300
c5c9cd4d
SW
2301 /* determine range to clone */
2302 ret = -EINVAL;
2ebc3464 2303 if (off + len > src->i_size || off + len < off)
f46b5a66 2304 goto out_unlock;
c5c9cd4d
SW
2305 if (len == 0)
2306 olen = len = src->i_size - off;
2307 /* if we extend to eof, continue to block boundary */
2308 if (off + len == src->i_size)
2a6b8dae 2309 len = ALIGN(src->i_size, bs) - off;
c5c9cd4d
SW
2310
2311 /* verify the end result is block aligned */
2a6b8dae
LZ
2312 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2313 !IS_ALIGNED(destoff, bs))
c5c9cd4d
SW
2314 goto out_unlock;
2315
d525e8ab
LZ
2316 if (destoff > inode->i_size) {
2317 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2318 if (ret)
2319 goto out_unlock;
2320 }
2321
71ef0786
LZ
2322 /* truncate page cache pages from target inode range */
2323 truncate_inode_pages_range(&inode->i_data, destoff,
2324 PAGE_CACHE_ALIGN(destoff + len) - 1);
2325
f46b5a66
CH
2326 /* do any pending delalloc/csum calc on src, one way or
2327 another, and lock file content */
2328 while (1) {
31840ae1 2329 struct btrfs_ordered_extent *ordered;
c5c9cd4d 2330 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
9a019196
SW
2331 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2332 if (!ordered &&
2333 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2334 EXTENT_DELALLOC, 0, NULL))
f46b5a66 2335 break;
c5c9cd4d 2336 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
ae01a0ab
YZ
2337 if (ordered)
2338 btrfs_put_ordered_extent(ordered);
9a019196 2339 btrfs_wait_ordered_range(src, off, len);
f46b5a66
CH
2340 }
2341
c5c9cd4d 2342 /* clone data */
33345d01 2343 key.objectid = btrfs_ino(src);
ae01a0ab
YZ
2344 key.type = BTRFS_EXTENT_DATA_KEY;
2345 key.offset = 0;
f46b5a66
CH
2346
2347 while (1) {
2348 /*
2349 * note the key will change type as we walk through the
2350 * tree.
2351 */
a22285a6 2352 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
f46b5a66
CH
2353 if (ret < 0)
2354 goto out;
2355
ae01a0ab
YZ
2356 nritems = btrfs_header_nritems(path->nodes[0]);
2357 if (path->slots[0] >= nritems) {
f46b5a66
CH
2358 ret = btrfs_next_leaf(root, path);
2359 if (ret < 0)
2360 goto out;
2361 if (ret > 0)
2362 break;
ae01a0ab 2363 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
2364 }
2365 leaf = path->nodes[0];
2366 slot = path->slots[0];
f46b5a66 2367
ae01a0ab 2368 btrfs_item_key_to_cpu(leaf, &key, slot);
d20f7043 2369 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
33345d01 2370 key.objectid != btrfs_ino(src))
f46b5a66
CH
2371 break;
2372
c5c9cd4d
SW
2373 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2374 struct btrfs_file_extent_item *extent;
2375 int type;
31840ae1
ZY
2376 u32 size;
2377 struct btrfs_key new_key;
c5c9cd4d
SW
2378 u64 disko = 0, diskl = 0;
2379 u64 datao = 0, datal = 0;
2380 u8 comp;
b5384d48 2381 u64 endoff;
31840ae1
ZY
2382
2383 size = btrfs_item_size_nr(leaf, slot);
2384 read_extent_buffer(leaf, buf,
2385 btrfs_item_ptr_offset(leaf, slot),
2386 size);
c5c9cd4d
SW
2387
2388 extent = btrfs_item_ptr(leaf, slot,
2389 struct btrfs_file_extent_item);
2390 comp = btrfs_file_extent_compression(leaf, extent);
2391 type = btrfs_file_extent_type(leaf, extent);
c8a894d7
CM
2392 if (type == BTRFS_FILE_EXTENT_REG ||
2393 type == BTRFS_FILE_EXTENT_PREALLOC) {
d397712b
CM
2394 disko = btrfs_file_extent_disk_bytenr(leaf,
2395 extent);
2396 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2397 extent);
c5c9cd4d 2398 datao = btrfs_file_extent_offset(leaf, extent);
d397712b
CM
2399 datal = btrfs_file_extent_num_bytes(leaf,
2400 extent);
c5c9cd4d
SW
2401 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2402 /* take upper bound, may be compressed */
2403 datal = btrfs_file_extent_ram_bytes(leaf,
2404 extent);
2405 }
b3b4aa74 2406 btrfs_release_path(path);
31840ae1 2407
050006a7 2408 if (key.offset + datal <= off ||
c5c9cd4d
SW
2409 key.offset >= off+len)
2410 goto next;
2411
31840ae1 2412 memcpy(&new_key, &key, sizeof(new_key));
33345d01 2413 new_key.objectid = btrfs_ino(inode);
4d728ec7
LZ
2414 if (off <= key.offset)
2415 new_key.offset = key.offset + destoff - off;
2416 else
2417 new_key.offset = destoff;
31840ae1 2418
b6f3409b
SW
2419 /*
2420 * 1 - adjusting old extent (we may have to split it)
2421 * 1 - add new extent
2422 * 1 - inode update
2423 */
2424 trans = btrfs_start_transaction(root, 3);
a22285a6
YZ
2425 if (IS_ERR(trans)) {
2426 ret = PTR_ERR(trans);
2427 goto out;
2428 }
2429
c8a894d7
CM
2430 if (type == BTRFS_FILE_EXTENT_REG ||
2431 type == BTRFS_FILE_EXTENT_PREALLOC) {
d72c0842
LZ
2432 /*
2433 * a | --- range to clone ---| b
2434 * | ------------- extent ------------- |
2435 */
2436
2437 /* substract range b */
2438 if (key.offset + datal > off + len)
2439 datal = off + len - key.offset;
2440
2441 /* substract range a */
a22285a6
YZ
2442 if (off > key.offset) {
2443 datao += off - key.offset;
2444 datal -= off - key.offset;
2445 }
2446
a22285a6
YZ
2447 ret = btrfs_drop_extents(trans, inode,
2448 new_key.offset,
2449 new_key.offset + datal,
2450 &hint_byte, 1);
2451 BUG_ON(ret);
2452
c5c9cd4d
SW
2453 ret = btrfs_insert_empty_item(trans, root, path,
2454 &new_key, size);
a22285a6 2455 BUG_ON(ret);
c5c9cd4d
SW
2456
2457 leaf = path->nodes[0];
2458 slot = path->slots[0];
2459 write_extent_buffer(leaf, buf,
31840ae1
ZY
2460 btrfs_item_ptr_offset(leaf, slot),
2461 size);
ae01a0ab 2462
c5c9cd4d 2463 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 2464 struct btrfs_file_extent_item);
c5c9cd4d 2465
c5c9cd4d
SW
2466 /* disko == 0 means it's a hole */
2467 if (!disko)
2468 datao = 0;
c5c9cd4d
SW
2469
2470 btrfs_set_file_extent_offset(leaf, extent,
2471 datao);
2472 btrfs_set_file_extent_num_bytes(leaf, extent,
2473 datal);
2474 if (disko) {
2475 inode_add_bytes(inode, datal);
ae01a0ab 2476 ret = btrfs_inc_extent_ref(trans, root,
5d4f98a2
YZ
2477 disko, diskl, 0,
2478 root->root_key.objectid,
33345d01 2479 btrfs_ino(inode),
66d7e7f0
AJ
2480 new_key.offset - datao,
2481 0);
31840ae1 2482 BUG_ON(ret);
f46b5a66 2483 }
c5c9cd4d
SW
2484 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2485 u64 skip = 0;
2486 u64 trim = 0;
2487 if (off > key.offset) {
2488 skip = off - key.offset;
2489 new_key.offset += skip;
2490 }
d397712b 2491
c5c9cd4d
SW
2492 if (key.offset + datal > off+len)
2493 trim = key.offset + datal - (off+len);
d397712b 2494
c5c9cd4d 2495 if (comp && (skip || trim)) {
c5c9cd4d 2496 ret = -EINVAL;
a22285a6 2497 btrfs_end_transaction(trans, root);
c5c9cd4d
SW
2498 goto out;
2499 }
2500 size -= skip + trim;
2501 datal -= skip + trim;
a22285a6
YZ
2502
2503 ret = btrfs_drop_extents(trans, inode,
2504 new_key.offset,
2505 new_key.offset + datal,
2506 &hint_byte, 1);
2507 BUG_ON(ret);
2508
c5c9cd4d
SW
2509 ret = btrfs_insert_empty_item(trans, root, path,
2510 &new_key, size);
a22285a6 2511 BUG_ON(ret);
c5c9cd4d
SW
2512
2513 if (skip) {
d397712b
CM
2514 u32 start =
2515 btrfs_file_extent_calc_inline_size(0);
c5c9cd4d
SW
2516 memmove(buf+start, buf+start+skip,
2517 datal);
2518 }
2519
2520 leaf = path->nodes[0];
2521 slot = path->slots[0];
2522 write_extent_buffer(leaf, buf,
2523 btrfs_item_ptr_offset(leaf, slot),
2524 size);
2525 inode_add_bytes(inode, datal);
f46b5a66 2526 }
c5c9cd4d
SW
2527
2528 btrfs_mark_buffer_dirty(leaf);
b3b4aa74 2529 btrfs_release_path(path);
c5c9cd4d 2530
a22285a6 2531 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
b5384d48
SW
2532
2533 /*
2534 * we round up to the block size at eof when
2535 * determining which extents to clone above,
2536 * but shouldn't round up the file size
2537 */
2538 endoff = new_key.offset + datal;
5f3888ff
LZ
2539 if (endoff > destoff+olen)
2540 endoff = destoff+olen;
b5384d48
SW
2541 if (endoff > inode->i_size)
2542 btrfs_i_size_write(inode, endoff);
2543
a22285a6
YZ
2544 ret = btrfs_update_inode(trans, root, inode);
2545 BUG_ON(ret);
2546 btrfs_end_transaction(trans, root);
2547 }
d397712b 2548next:
b3b4aa74 2549 btrfs_release_path(path);
f46b5a66 2550 key.offset++;
f46b5a66 2551 }
f46b5a66
CH
2552 ret = 0;
2553out:
b3b4aa74 2554 btrfs_release_path(path);
c5c9cd4d 2555 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
f46b5a66
CH
2556out_unlock:
2557 mutex_unlock(&src->i_mutex);
2558 mutex_unlock(&inode->i_mutex);
ae01a0ab
YZ
2559 vfree(buf);
2560 btrfs_free_path(path);
f46b5a66
CH
2561out_fput:
2562 fput(src_file);
ab67b7c1
YZ
2563out_drop_write:
2564 mnt_drop_write(file->f_path.mnt);
f46b5a66
CH
2565 return ret;
2566}
2567
7a865e8a 2568static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
c5c9cd4d
SW
2569{
2570 struct btrfs_ioctl_clone_range_args args;
2571
7a865e8a 2572 if (copy_from_user(&args, argp, sizeof(args)))
c5c9cd4d
SW
2573 return -EFAULT;
2574 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2575 args.src_length, args.dest_offset);
2576}
2577
f46b5a66
CH
2578/*
2579 * there are many ways the trans_start and trans_end ioctls can lead
2580 * to deadlocks. They should only be used by applications that
2581 * basically own the machine, and have a very in depth understanding
2582 * of all the possible deadlocks and enospc problems.
2583 */
b2950863 2584static long btrfs_ioctl_trans_start(struct file *file)
f46b5a66
CH
2585{
2586 struct inode *inode = fdentry(file)->d_inode;
2587 struct btrfs_root *root = BTRFS_I(inode)->root;
2588 struct btrfs_trans_handle *trans;
1ab86aed 2589 int ret;
f46b5a66 2590
1ab86aed 2591 ret = -EPERM;
df5b5520 2592 if (!capable(CAP_SYS_ADMIN))
1ab86aed 2593 goto out;
df5b5520 2594
1ab86aed
SW
2595 ret = -EINPROGRESS;
2596 if (file->private_data)
f46b5a66 2597 goto out;
9ca9ee09 2598
b83cc969
LZ
2599 ret = -EROFS;
2600 if (btrfs_root_readonly(root))
2601 goto out;
2602
c146afad
YZ
2603 ret = mnt_want_write(file->f_path.mnt);
2604 if (ret)
2605 goto out;
2606
a4abeea4 2607 atomic_inc(&root->fs_info->open_ioctl_trans);
9ca9ee09 2608
1ab86aed 2609 ret = -ENOMEM;
7a7eaa40 2610 trans = btrfs_start_ioctl_transaction(root);
abd30bb0 2611 if (IS_ERR(trans))
1ab86aed
SW
2612 goto out_drop;
2613
2614 file->private_data = trans;
2615 return 0;
2616
2617out_drop:
a4abeea4 2618 atomic_dec(&root->fs_info->open_ioctl_trans);
1ab86aed 2619 mnt_drop_write(file->f_path.mnt);
f46b5a66 2620out:
f46b5a66
CH
2621 return ret;
2622}
2623
6ef5ed0d
JB
2624static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2625{
2626 struct inode *inode = fdentry(file)->d_inode;
2627 struct btrfs_root *root = BTRFS_I(inode)->root;
2628 struct btrfs_root *new_root;
2629 struct btrfs_dir_item *di;
2630 struct btrfs_trans_handle *trans;
2631 struct btrfs_path *path;
2632 struct btrfs_key location;
2633 struct btrfs_disk_key disk_key;
2634 struct btrfs_super_block *disk_super;
2635 u64 features;
2636 u64 objectid = 0;
2637 u64 dir_id;
2638
2639 if (!capable(CAP_SYS_ADMIN))
2640 return -EPERM;
2641
2642 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2643 return -EFAULT;
2644
2645 if (!objectid)
2646 objectid = root->root_key.objectid;
2647
2648 location.objectid = objectid;
2649 location.type = BTRFS_ROOT_ITEM_KEY;
2650 location.offset = (u64)-1;
2651
2652 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2653 if (IS_ERR(new_root))
2654 return PTR_ERR(new_root);
2655
2656 if (btrfs_root_refs(&new_root->root_item) == 0)
2657 return -ENOENT;
2658
2659 path = btrfs_alloc_path();
2660 if (!path)
2661 return -ENOMEM;
2662 path->leave_spinning = 1;
2663
2664 trans = btrfs_start_transaction(root, 1);
98d5dc13 2665 if (IS_ERR(trans)) {
6ef5ed0d 2666 btrfs_free_path(path);
98d5dc13 2667 return PTR_ERR(trans);
6ef5ed0d
JB
2668 }
2669
6c41761f 2670 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
6ef5ed0d
JB
2671 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2672 dir_id, "default", 7, 1);
cf1e99a4 2673 if (IS_ERR_OR_NULL(di)) {
6ef5ed0d
JB
2674 btrfs_free_path(path);
2675 btrfs_end_transaction(trans, root);
2676 printk(KERN_ERR "Umm, you don't have the default dir item, "
2677 "this isn't going to work\n");
2678 return -ENOENT;
2679 }
2680
2681 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2682 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2683 btrfs_mark_buffer_dirty(path->nodes[0]);
2684 btrfs_free_path(path);
2685
6c41761f 2686 disk_super = root->fs_info->super_copy;
6ef5ed0d
JB
2687 features = btrfs_super_incompat_flags(disk_super);
2688 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2689 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2690 btrfs_set_super_incompat_flags(disk_super, features);
2691 }
2692 btrfs_end_transaction(trans, root);
2693
2694 return 0;
2695}
2696
bf5fc093
JB
2697static void get_block_group_info(struct list_head *groups_list,
2698 struct btrfs_ioctl_space_info *space)
2699{
2700 struct btrfs_block_group_cache *block_group;
2701
2702 space->total_bytes = 0;
2703 space->used_bytes = 0;
2704 space->flags = 0;
2705 list_for_each_entry(block_group, groups_list, list) {
2706 space->flags = block_group->flags;
2707 space->total_bytes += block_group->key.offset;
2708 space->used_bytes +=
2709 btrfs_block_group_used(&block_group->item);
2710 }
2711}
2712
1406e432
JB
2713long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2714{
2715 struct btrfs_ioctl_space_args space_args;
2716 struct btrfs_ioctl_space_info space;
2717 struct btrfs_ioctl_space_info *dest;
7fde62bf 2718 struct btrfs_ioctl_space_info *dest_orig;
13f2696f 2719 struct btrfs_ioctl_space_info __user *user_dest;
1406e432 2720 struct btrfs_space_info *info;
bf5fc093
JB
2721 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2722 BTRFS_BLOCK_GROUP_SYSTEM,
2723 BTRFS_BLOCK_GROUP_METADATA,
2724 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2725 int num_types = 4;
7fde62bf 2726 int alloc_size;
1406e432 2727 int ret = 0;
51788b1b 2728 u64 slot_count = 0;
bf5fc093 2729 int i, c;
1406e432
JB
2730
2731 if (copy_from_user(&space_args,
2732 (struct btrfs_ioctl_space_args __user *)arg,
2733 sizeof(space_args)))
2734 return -EFAULT;
2735
bf5fc093
JB
2736 for (i = 0; i < num_types; i++) {
2737 struct btrfs_space_info *tmp;
2738
2739 info = NULL;
2740 rcu_read_lock();
2741 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2742 list) {
2743 if (tmp->flags == types[i]) {
2744 info = tmp;
2745 break;
2746 }
2747 }
2748 rcu_read_unlock();
2749
2750 if (!info)
2751 continue;
2752
2753 down_read(&info->groups_sem);
2754 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2755 if (!list_empty(&info->block_groups[c]))
2756 slot_count++;
2757 }
2758 up_read(&info->groups_sem);
2759 }
7fde62bf
CM
2760
2761 /* space_slots == 0 means they are asking for a count */
2762 if (space_args.space_slots == 0) {
2763 space_args.total_spaces = slot_count;
2764 goto out;
2765 }
bf5fc093 2766
51788b1b 2767 slot_count = min_t(u64, space_args.space_slots, slot_count);
bf5fc093 2768
7fde62bf 2769 alloc_size = sizeof(*dest) * slot_count;
bf5fc093 2770
7fde62bf
CM
2771 /* we generally have at most 6 or so space infos, one for each raid
2772 * level. So, a whole page should be more than enough for everyone
2773 */
2774 if (alloc_size > PAGE_CACHE_SIZE)
2775 return -ENOMEM;
2776
1406e432 2777 space_args.total_spaces = 0;
7fde62bf
CM
2778 dest = kmalloc(alloc_size, GFP_NOFS);
2779 if (!dest)
2780 return -ENOMEM;
2781 dest_orig = dest;
1406e432 2782
7fde62bf 2783 /* now we have a buffer to copy into */
bf5fc093
JB
2784 for (i = 0; i < num_types; i++) {
2785 struct btrfs_space_info *tmp;
2786
51788b1b
DR
2787 if (!slot_count)
2788 break;
2789
bf5fc093
JB
2790 info = NULL;
2791 rcu_read_lock();
2792 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2793 list) {
2794 if (tmp->flags == types[i]) {
2795 info = tmp;
2796 break;
2797 }
2798 }
2799 rcu_read_unlock();
7fde62bf 2800
bf5fc093
JB
2801 if (!info)
2802 continue;
2803 down_read(&info->groups_sem);
2804 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2805 if (!list_empty(&info->block_groups[c])) {
2806 get_block_group_info(&info->block_groups[c],
2807 &space);
2808 memcpy(dest, &space, sizeof(space));
2809 dest++;
2810 space_args.total_spaces++;
51788b1b 2811 slot_count--;
bf5fc093 2812 }
51788b1b
DR
2813 if (!slot_count)
2814 break;
bf5fc093
JB
2815 }
2816 up_read(&info->groups_sem);
1406e432 2817 }
1406e432 2818
7fde62bf
CM
2819 user_dest = (struct btrfs_ioctl_space_info *)
2820 (arg + sizeof(struct btrfs_ioctl_space_args));
2821
2822 if (copy_to_user(user_dest, dest_orig, alloc_size))
2823 ret = -EFAULT;
2824
2825 kfree(dest_orig);
2826out:
2827 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1406e432
JB
2828 ret = -EFAULT;
2829
2830 return ret;
2831}
2832
f46b5a66
CH
2833/*
2834 * there are many ways the trans_start and trans_end ioctls can lead
2835 * to deadlocks. They should only be used by applications that
2836 * basically own the machine, and have a very in depth understanding
2837 * of all the possible deadlocks and enospc problems.
2838 */
2839long btrfs_ioctl_trans_end(struct file *file)
2840{
2841 struct inode *inode = fdentry(file)->d_inode;
2842 struct btrfs_root *root = BTRFS_I(inode)->root;
2843 struct btrfs_trans_handle *trans;
f46b5a66 2844
f46b5a66 2845 trans = file->private_data;
1ab86aed
SW
2846 if (!trans)
2847 return -EINVAL;
b214107e 2848 file->private_data = NULL;
9ca9ee09 2849
1ab86aed
SW
2850 btrfs_end_transaction(trans, root);
2851
a4abeea4 2852 atomic_dec(&root->fs_info->open_ioctl_trans);
9ca9ee09 2853
cfc8ea87 2854 mnt_drop_write(file->f_path.mnt);
1ab86aed 2855 return 0;
f46b5a66
CH
2856}
2857
46204592
SW
2858static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2859{
2860 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2861 struct btrfs_trans_handle *trans;
2862 u64 transid;
db5b493a 2863 int ret;
46204592
SW
2864
2865 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
2866 if (IS_ERR(trans))
2867 return PTR_ERR(trans);
46204592 2868 transid = trans->transid;
db5b493a 2869 ret = btrfs_commit_transaction_async(trans, root, 0);
8b2b2d3c
TI
2870 if (ret) {
2871 btrfs_end_transaction(trans, root);
db5b493a 2872 return ret;
8b2b2d3c 2873 }
46204592
SW
2874
2875 if (argp)
2876 if (copy_to_user(argp, &transid, sizeof(transid)))
2877 return -EFAULT;
2878 return 0;
2879}
2880
2881static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2882{
2883 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2884 u64 transid;
2885
2886 if (argp) {
2887 if (copy_from_user(&transid, argp, sizeof(transid)))
2888 return -EFAULT;
2889 } else {
2890 transid = 0; /* current trans */
2891 }
2892 return btrfs_wait_for_commit(root, transid);
2893}
2894
475f6387
JS
2895static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2896{
2897 int ret;
2898 struct btrfs_ioctl_scrub_args *sa;
2899
2900 if (!capable(CAP_SYS_ADMIN))
2901 return -EPERM;
2902
2903 sa = memdup_user(arg, sizeof(*sa));
2904 if (IS_ERR(sa))
2905 return PTR_ERR(sa);
2906
2907 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
8628764e 2908 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
475f6387
JS
2909
2910 if (copy_to_user(arg, sa, sizeof(*sa)))
2911 ret = -EFAULT;
2912
2913 kfree(sa);
2914 return ret;
2915}
2916
2917static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2918{
2919 if (!capable(CAP_SYS_ADMIN))
2920 return -EPERM;
2921
2922 return btrfs_scrub_cancel(root);
2923}
2924
2925static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2926 void __user *arg)
2927{
2928 struct btrfs_ioctl_scrub_args *sa;
2929 int ret;
2930
2931 if (!capable(CAP_SYS_ADMIN))
2932 return -EPERM;
2933
2934 sa = memdup_user(arg, sizeof(*sa));
2935 if (IS_ERR(sa))
2936 return PTR_ERR(sa);
2937
2938 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2939
2940 if (copy_to_user(arg, sa, sizeof(*sa)))
2941 ret = -EFAULT;
2942
2943 kfree(sa);
2944 return ret;
2945}
2946
d7728c96
JS
2947static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
2948{
2949 int ret = 0;
2950 int i;
740c3d22 2951 u64 rel_ptr;
d7728c96 2952 int size;
806468f8 2953 struct btrfs_ioctl_ino_path_args *ipa = NULL;
d7728c96
JS
2954 struct inode_fs_paths *ipath = NULL;
2955 struct btrfs_path *path;
2956
2957 if (!capable(CAP_SYS_ADMIN))
2958 return -EPERM;
2959
2960 path = btrfs_alloc_path();
2961 if (!path) {
2962 ret = -ENOMEM;
2963 goto out;
2964 }
2965
2966 ipa = memdup_user(arg, sizeof(*ipa));
2967 if (IS_ERR(ipa)) {
2968 ret = PTR_ERR(ipa);
2969 ipa = NULL;
2970 goto out;
2971 }
2972
2973 size = min_t(u32, ipa->size, 4096);
2974 ipath = init_ipath(size, root, path);
2975 if (IS_ERR(ipath)) {
2976 ret = PTR_ERR(ipath);
2977 ipath = NULL;
2978 goto out;
2979 }
2980
2981 ret = paths_from_inode(ipa->inum, ipath);
2982 if (ret < 0)
2983 goto out;
2984
2985 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
745c4d8e
JM
2986 rel_ptr = ipath->fspath->val[i] -
2987 (u64)(unsigned long)ipath->fspath->val;
740c3d22 2988 ipath->fspath->val[i] = rel_ptr;
d7728c96
JS
2989 }
2990
745c4d8e
JM
2991 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
2992 (void *)(unsigned long)ipath->fspath, size);
d7728c96
JS
2993 if (ret) {
2994 ret = -EFAULT;
2995 goto out;
2996 }
2997
2998out:
2999 btrfs_free_path(path);
3000 free_ipath(ipath);
3001 kfree(ipa);
3002
3003 return ret;
3004}
3005
3006static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3007{
3008 struct btrfs_data_container *inodes = ctx;
3009 const size_t c = 3 * sizeof(u64);
3010
3011 if (inodes->bytes_left >= c) {
3012 inodes->bytes_left -= c;
3013 inodes->val[inodes->elem_cnt] = inum;
3014 inodes->val[inodes->elem_cnt + 1] = offset;
3015 inodes->val[inodes->elem_cnt + 2] = root;
3016 inodes->elem_cnt += 3;
3017 } else {
3018 inodes->bytes_missing += c - inodes->bytes_left;
3019 inodes->bytes_left = 0;
3020 inodes->elem_missed += 3;
3021 }
3022
3023 return 0;
3024}
3025
3026static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3027 void __user *arg)
3028{
3029 int ret = 0;
3030 int size;
4692cf58 3031 u64 extent_item_pos;
d7728c96
JS
3032 struct btrfs_ioctl_logical_ino_args *loi;
3033 struct btrfs_data_container *inodes = NULL;
3034 struct btrfs_path *path = NULL;
3035 struct btrfs_key key;
3036
3037 if (!capable(CAP_SYS_ADMIN))
3038 return -EPERM;
3039
3040 loi = memdup_user(arg, sizeof(*loi));
3041 if (IS_ERR(loi)) {
3042 ret = PTR_ERR(loi);
3043 loi = NULL;
3044 goto out;
3045 }
3046
3047 path = btrfs_alloc_path();
3048 if (!path) {
3049 ret = -ENOMEM;
3050 goto out;
3051 }
3052
3053 size = min_t(u32, loi->size, 4096);
3054 inodes = init_data_container(size);
3055 if (IS_ERR(inodes)) {
3056 ret = PTR_ERR(inodes);
3057 inodes = NULL;
3058 goto out;
3059 }
3060
3061 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
4692cf58 3062 btrfs_release_path(path);
d7728c96
JS
3063
3064 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3065 ret = -ENOENT;
3066 if (ret < 0)
3067 goto out;
3068
4692cf58 3069 extent_item_pos = loi->logical - key.objectid;
d7728c96 3070 ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
4692cf58
JS
3071 extent_item_pos, build_ino_list,
3072 inodes);
d7728c96
JS
3073
3074 if (ret < 0)
3075 goto out;
3076
745c4d8e
JM
3077 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3078 (void *)(unsigned long)inodes, size);
d7728c96
JS
3079 if (ret)
3080 ret = -EFAULT;
3081
3082out:
3083 btrfs_free_path(path);
3084 kfree(inodes);
3085 kfree(loi);
3086
3087 return ret;
3088}
3089
19a39dce 3090void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
c9e9f97b
ID
3091 struct btrfs_ioctl_balance_args *bargs)
3092{
3093 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3094
3095 bargs->flags = bctl->flags;
3096
837d5b6e
ID
3097 if (atomic_read(&fs_info->balance_running))
3098 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3099 if (atomic_read(&fs_info->balance_pause_req))
3100 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
a7e99c69
ID
3101 if (atomic_read(&fs_info->balance_cancel_req))
3102 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
837d5b6e 3103
c9e9f97b
ID
3104 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3105 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3106 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
19a39dce
ID
3107
3108 if (lock) {
3109 spin_lock(&fs_info->balance_lock);
3110 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3111 spin_unlock(&fs_info->balance_lock);
3112 } else {
3113 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3114 }
c9e9f97b
ID
3115}
3116
3117static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3118{
3119 struct btrfs_fs_info *fs_info = root->fs_info;
3120 struct btrfs_ioctl_balance_args *bargs;
3121 struct btrfs_balance_control *bctl;
3122 int ret;
3123
3124 if (!capable(CAP_SYS_ADMIN))
3125 return -EPERM;
3126
3127 if (fs_info->sb->s_flags & MS_RDONLY)
3128 return -EROFS;
3129
3130 mutex_lock(&fs_info->volume_mutex);
3131 mutex_lock(&fs_info->balance_mutex);
3132
3133 if (arg) {
3134 bargs = memdup_user(arg, sizeof(*bargs));
3135 if (IS_ERR(bargs)) {
3136 ret = PTR_ERR(bargs);
3137 goto out;
3138 }
de322263
ID
3139
3140 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3141 if (!fs_info->balance_ctl) {
3142 ret = -ENOTCONN;
3143 goto out_bargs;
3144 }
3145
3146 bctl = fs_info->balance_ctl;
3147 spin_lock(&fs_info->balance_lock);
3148 bctl->flags |= BTRFS_BALANCE_RESUME;
3149 spin_unlock(&fs_info->balance_lock);
3150
3151 goto do_balance;
3152 }
c9e9f97b
ID
3153 } else {
3154 bargs = NULL;
3155 }
3156
837d5b6e
ID
3157 if (fs_info->balance_ctl) {
3158 ret = -EINPROGRESS;
3159 goto out_bargs;
3160 }
3161
c9e9f97b
ID
3162 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3163 if (!bctl) {
3164 ret = -ENOMEM;
3165 goto out_bargs;
3166 }
3167
3168 bctl->fs_info = fs_info;
3169 if (arg) {
3170 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3171 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3172 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3173
3174 bctl->flags = bargs->flags;
f43ffb60
ID
3175 } else {
3176 /* balance everything - no filters */
3177 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
c9e9f97b
ID
3178 }
3179
de322263 3180do_balance:
c9e9f97b
ID
3181 ret = btrfs_balance(bctl, bargs);
3182 /*
837d5b6e
ID
3183 * bctl is freed in __cancel_balance or in free_fs_info if
3184 * restriper was paused all the way until unmount
c9e9f97b
ID
3185 */
3186 if (arg) {
3187 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3188 ret = -EFAULT;
3189 }
3190
3191out_bargs:
3192 kfree(bargs);
3193out:
3194 mutex_unlock(&fs_info->balance_mutex);
3195 mutex_unlock(&fs_info->volume_mutex);
3196 return ret;
3197}
3198
837d5b6e
ID
3199static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3200{
3201 if (!capable(CAP_SYS_ADMIN))
3202 return -EPERM;
3203
3204 switch (cmd) {
3205 case BTRFS_BALANCE_CTL_PAUSE:
3206 return btrfs_pause_balance(root->fs_info);
a7e99c69
ID
3207 case BTRFS_BALANCE_CTL_CANCEL:
3208 return btrfs_cancel_balance(root->fs_info);
837d5b6e
ID
3209 }
3210
3211 return -EINVAL;
3212}
3213
19a39dce
ID
3214static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3215 void __user *arg)
3216{
3217 struct btrfs_fs_info *fs_info = root->fs_info;
3218 struct btrfs_ioctl_balance_args *bargs;
3219 int ret = 0;
3220
3221 if (!capable(CAP_SYS_ADMIN))
3222 return -EPERM;
3223
3224 mutex_lock(&fs_info->balance_mutex);
3225 if (!fs_info->balance_ctl) {
3226 ret = -ENOTCONN;
3227 goto out;
3228 }
3229
3230 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3231 if (!bargs) {
3232 ret = -ENOMEM;
3233 goto out;
3234 }
3235
3236 update_ioctl_balance_args(fs_info, 1, bargs);
3237
3238 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3239 ret = -EFAULT;
3240
3241 kfree(bargs);
3242out:
3243 mutex_unlock(&fs_info->balance_mutex);
3244 return ret;
3245}
3246
f46b5a66
CH
3247long btrfs_ioctl(struct file *file, unsigned int
3248 cmd, unsigned long arg)
3249{
3250 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4bcabaa3 3251 void __user *argp = (void __user *)arg;
f46b5a66
CH
3252
3253 switch (cmd) {
6cbff00f
CH
3254 case FS_IOC_GETFLAGS:
3255 return btrfs_ioctl_getflags(file, argp);
3256 case FS_IOC_SETFLAGS:
3257 return btrfs_ioctl_setflags(file, argp);
3258 case FS_IOC_GETVERSION:
3259 return btrfs_ioctl_getversion(file, argp);
f7039b1d
LD
3260 case FITRIM:
3261 return btrfs_ioctl_fitrim(file, argp);
f46b5a66 3262 case BTRFS_IOC_SNAP_CREATE:
fa0d2b9b 3263 return btrfs_ioctl_snap_create(file, argp, 0);
fdfb1e4f 3264 case BTRFS_IOC_SNAP_CREATE_V2:
fa0d2b9b 3265 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3de4586c 3266 case BTRFS_IOC_SUBVOL_CREATE:
fa0d2b9b 3267 return btrfs_ioctl_snap_create(file, argp, 1);
76dda93c
YZ
3268 case BTRFS_IOC_SNAP_DESTROY:
3269 return btrfs_ioctl_snap_destroy(file, argp);
0caa102d
LZ
3270 case BTRFS_IOC_SUBVOL_GETFLAGS:
3271 return btrfs_ioctl_subvol_getflags(file, argp);
3272 case BTRFS_IOC_SUBVOL_SETFLAGS:
3273 return btrfs_ioctl_subvol_setflags(file, argp);
6ef5ed0d
JB
3274 case BTRFS_IOC_DEFAULT_SUBVOL:
3275 return btrfs_ioctl_default_subvol(file, argp);
f46b5a66 3276 case BTRFS_IOC_DEFRAG:
1e701a32
CM
3277 return btrfs_ioctl_defrag(file, NULL);
3278 case BTRFS_IOC_DEFRAG_RANGE:
3279 return btrfs_ioctl_defrag(file, argp);
f46b5a66 3280 case BTRFS_IOC_RESIZE:
4bcabaa3 3281 return btrfs_ioctl_resize(root, argp);
f46b5a66 3282 case BTRFS_IOC_ADD_DEV:
4bcabaa3 3283 return btrfs_ioctl_add_dev(root, argp);
f46b5a66 3284 case BTRFS_IOC_RM_DEV:
4bcabaa3 3285 return btrfs_ioctl_rm_dev(root, argp);
475f6387
JS
3286 case BTRFS_IOC_FS_INFO:
3287 return btrfs_ioctl_fs_info(root, argp);
3288 case BTRFS_IOC_DEV_INFO:
3289 return btrfs_ioctl_dev_info(root, argp);
f46b5a66 3290 case BTRFS_IOC_BALANCE:
c9e9f97b 3291 return btrfs_ioctl_balance(root, NULL);
f46b5a66 3292 case BTRFS_IOC_CLONE:
c5c9cd4d
SW
3293 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3294 case BTRFS_IOC_CLONE_RANGE:
7a865e8a 3295 return btrfs_ioctl_clone_range(file, argp);
f46b5a66
CH
3296 case BTRFS_IOC_TRANS_START:
3297 return btrfs_ioctl_trans_start(file);
3298 case BTRFS_IOC_TRANS_END:
3299 return btrfs_ioctl_trans_end(file);
ac8e9819
CM
3300 case BTRFS_IOC_TREE_SEARCH:
3301 return btrfs_ioctl_tree_search(file, argp);
3302 case BTRFS_IOC_INO_LOOKUP:
3303 return btrfs_ioctl_ino_lookup(file, argp);
d7728c96
JS
3304 case BTRFS_IOC_INO_PATHS:
3305 return btrfs_ioctl_ino_to_path(root, argp);
3306 case BTRFS_IOC_LOGICAL_INO:
3307 return btrfs_ioctl_logical_to_ino(root, argp);
1406e432
JB
3308 case BTRFS_IOC_SPACE_INFO:
3309 return btrfs_ioctl_space_info(root, argp);
f46b5a66
CH
3310 case BTRFS_IOC_SYNC:
3311 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3312 return 0;
46204592
SW
3313 case BTRFS_IOC_START_SYNC:
3314 return btrfs_ioctl_start_sync(file, argp);
3315 case BTRFS_IOC_WAIT_SYNC:
3316 return btrfs_ioctl_wait_sync(file, argp);
475f6387
JS
3317 case BTRFS_IOC_SCRUB:
3318 return btrfs_ioctl_scrub(root, argp);
3319 case BTRFS_IOC_SCRUB_CANCEL:
3320 return btrfs_ioctl_scrub_cancel(root, argp);
3321 case BTRFS_IOC_SCRUB_PROGRESS:
3322 return btrfs_ioctl_scrub_progress(root, argp);
c9e9f97b
ID
3323 case BTRFS_IOC_BALANCE_V2:
3324 return btrfs_ioctl_balance(root, argp);
837d5b6e
ID
3325 case BTRFS_IOC_BALANCE_CTL:
3326 return btrfs_ioctl_balance_ctl(root, arg);
19a39dce
ID
3327 case BTRFS_IOC_BALANCE_PROGRESS:
3328 return btrfs_ioctl_balance_progress(root, argp);
f46b5a66
CH
3329 }
3330
3331 return -ENOTTY;
3332}