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