]> git.ipfire.org Git - people/ms/linux.git/blame - fs/btrfs/ioctl.c
Btrfs: return immediately if tree log mod is not necessary
[people/ms/linux.git] / fs / btrfs / ioctl.c
CommitLineData
f46b5a66
CH
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
cb8e7090 24#include <linux/fsnotify.h>
f46b5a66
CH
25#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
f46b5a66 30#include <linux/backing-dev.h>
cb8e7090 31#include <linux/mount.h>
f46b5a66 32#include <linux/mpage.h>
cb8e7090 33#include <linux/namei.h>
f46b5a66
CH
34#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
cb8e7090 39#include <linux/security.h>
f46b5a66 40#include <linux/xattr.h>
7ea394f1 41#include <linux/vmalloc.h>
5a0e3ad6 42#include <linux/slab.h>
f7039b1d 43#include <linux/blkdev.h>
8ea05e3a 44#include <linux/uuid.h>
55e301fd 45#include <linux/btrfs.h>
416161db 46#include <linux/uaccess.h>
f46b5a66
CH
47#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
f46b5a66
CH
51#include "print-tree.h"
52#include "volumes.h"
925baedd 53#include "locking.h"
581bb050 54#include "inode-map.h"
d7728c96 55#include "backref.h"
606686ee 56#include "rcu-string.h"
31db9f7c 57#include "send.h"
3f6bcfbd 58#include "dev-replace.h"
3b02a68a 59#include "sysfs.h"
f46b5a66 60
416161db
MF
61static int btrfs_clone(struct inode *src, struct inode *inode,
62 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
63
6cbff00f
CH
64/* Mask out flags that are inappropriate for the given type of inode. */
65static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
66{
67 if (S_ISDIR(mode))
68 return flags;
69 else if (S_ISREG(mode))
70 return flags & ~FS_DIRSYNC_FL;
71 else
72 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
73}
74
75/*
76 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
77 */
78static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
79{
80 unsigned int iflags = 0;
81
82 if (flags & BTRFS_INODE_SYNC)
83 iflags |= FS_SYNC_FL;
84 if (flags & BTRFS_INODE_IMMUTABLE)
85 iflags |= FS_IMMUTABLE_FL;
86 if (flags & BTRFS_INODE_APPEND)
87 iflags |= FS_APPEND_FL;
88 if (flags & BTRFS_INODE_NODUMP)
89 iflags |= FS_NODUMP_FL;
90 if (flags & BTRFS_INODE_NOATIME)
91 iflags |= FS_NOATIME_FL;
92 if (flags & BTRFS_INODE_DIRSYNC)
93 iflags |= FS_DIRSYNC_FL;
d0092bdd
LZ
94 if (flags & BTRFS_INODE_NODATACOW)
95 iflags |= FS_NOCOW_FL;
96
97 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98 iflags |= FS_COMPR_FL;
99 else if (flags & BTRFS_INODE_NOCOMPRESS)
100 iflags |= FS_NOCOMP_FL;
6cbff00f
CH
101
102 return iflags;
103}
104
105/*
106 * Update inode->i_flags based on the btrfs internal flags.
107 */
108void btrfs_update_iflags(struct inode *inode)
109{
110 struct btrfs_inode *ip = BTRFS_I(inode);
111
112 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
113
114 if (ip->flags & BTRFS_INODE_SYNC)
115 inode->i_flags |= S_SYNC;
116 if (ip->flags & BTRFS_INODE_IMMUTABLE)
117 inode->i_flags |= S_IMMUTABLE;
118 if (ip->flags & BTRFS_INODE_APPEND)
119 inode->i_flags |= S_APPEND;
120 if (ip->flags & BTRFS_INODE_NOATIME)
121 inode->i_flags |= S_NOATIME;
122 if (ip->flags & BTRFS_INODE_DIRSYNC)
123 inode->i_flags |= S_DIRSYNC;
124}
125
126/*
127 * Inherit flags from the parent inode.
128 *
e27425d6 129 * Currently only the compression flags and the cow flags are inherited.
6cbff00f
CH
130 */
131void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
132{
0b4dcea5
CM
133 unsigned int flags;
134
135 if (!dir)
136 return;
137
138 flags = BTRFS_I(dir)->flags;
6cbff00f 139
e27425d6
JB
140 if (flags & BTRFS_INODE_NOCOMPRESS) {
141 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143 } else if (flags & BTRFS_INODE_COMPRESS) {
144 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
146 }
147
213490b3 148 if (flags & BTRFS_INODE_NODATACOW) {
e27425d6 149 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
213490b3
LB
150 if (S_ISREG(inode->i_mode))
151 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
152 }
6cbff00f 153
6cbff00f
CH
154 btrfs_update_iflags(inode);
155}
156
157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
496ad9aa 159 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
6cbff00f
CH
160 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
75e7cb7f
LB
167static int check_flags(unsigned int flags)
168{
169 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
170 FS_NOATIME_FL | FS_NODUMP_FL | \
171 FS_SYNC_FL | FS_DIRSYNC_FL | \
e1e8fb6a
LZ
172 FS_NOCOMP_FL | FS_COMPR_FL |
173 FS_NOCOW_FL))
75e7cb7f
LB
174 return -EOPNOTSUPP;
175
176 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
177 return -EINVAL;
178
75e7cb7f
LB
179 return 0;
180}
181
6cbff00f
CH
182static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
183{
496ad9aa 184 struct inode *inode = file_inode(file);
6cbff00f
CH
185 struct btrfs_inode *ip = BTRFS_I(inode);
186 struct btrfs_root *root = ip->root;
187 struct btrfs_trans_handle *trans;
188 unsigned int flags, oldflags;
189 int ret;
f062abf0
LZ
190 u64 ip_oldflags;
191 unsigned int i_oldflags;
7e97b8da 192 umode_t mode;
6cbff00f 193
b83cc969
LZ
194 if (btrfs_root_readonly(root))
195 return -EROFS;
196
6cbff00f
CH
197 if (copy_from_user(&flags, arg, sizeof(flags)))
198 return -EFAULT;
199
75e7cb7f
LB
200 ret = check_flags(flags);
201 if (ret)
202 return ret;
f46b5a66 203
2e149670 204 if (!inode_owner_or_capable(inode))
6cbff00f
CH
205 return -EACCES;
206
e7848683
JK
207 ret = mnt_want_write_file(file);
208 if (ret)
209 return ret;
210
6cbff00f
CH
211 mutex_lock(&inode->i_mutex);
212
f062abf0
LZ
213 ip_oldflags = ip->flags;
214 i_oldflags = inode->i_flags;
7e97b8da 215 mode = inode->i_mode;
f062abf0 216
6cbff00f
CH
217 flags = btrfs_mask_flags(inode->i_mode, flags);
218 oldflags = btrfs_flags_to_ioctl(ip->flags);
219 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
220 if (!capable(CAP_LINUX_IMMUTABLE)) {
221 ret = -EPERM;
222 goto out_unlock;
223 }
224 }
225
6cbff00f
CH
226 if (flags & FS_SYNC_FL)
227 ip->flags |= BTRFS_INODE_SYNC;
228 else
229 ip->flags &= ~BTRFS_INODE_SYNC;
230 if (flags & FS_IMMUTABLE_FL)
231 ip->flags |= BTRFS_INODE_IMMUTABLE;
232 else
233 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
234 if (flags & FS_APPEND_FL)
235 ip->flags |= BTRFS_INODE_APPEND;
236 else
237 ip->flags &= ~BTRFS_INODE_APPEND;
238 if (flags & FS_NODUMP_FL)
239 ip->flags |= BTRFS_INODE_NODUMP;
240 else
241 ip->flags &= ~BTRFS_INODE_NODUMP;
242 if (flags & FS_NOATIME_FL)
243 ip->flags |= BTRFS_INODE_NOATIME;
244 else
245 ip->flags &= ~BTRFS_INODE_NOATIME;
246 if (flags & FS_DIRSYNC_FL)
247 ip->flags |= BTRFS_INODE_DIRSYNC;
248 else
249 ip->flags &= ~BTRFS_INODE_DIRSYNC;
7e97b8da
DS
250 if (flags & FS_NOCOW_FL) {
251 if (S_ISREG(mode)) {
252 /*
253 * It's safe to turn csums off here, no extents exist.
254 * Otherwise we want the flag to reflect the real COW
255 * status of the file and will not set it.
256 */
257 if (inode->i_size == 0)
258 ip->flags |= BTRFS_INODE_NODATACOW
259 | BTRFS_INODE_NODATASUM;
260 } else {
261 ip->flags |= BTRFS_INODE_NODATACOW;
262 }
263 } else {
264 /*
265 * Revert back under same assuptions as above
266 */
267 if (S_ISREG(mode)) {
268 if (inode->i_size == 0)
269 ip->flags &= ~(BTRFS_INODE_NODATACOW
270 | BTRFS_INODE_NODATASUM);
271 } else {
272 ip->flags &= ~BTRFS_INODE_NODATACOW;
273 }
274 }
6cbff00f 275
75e7cb7f
LB
276 /*
277 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
278 * flag may be changed automatically if compression code won't make
279 * things smaller.
280 */
281 if (flags & FS_NOCOMP_FL) {
282 ip->flags &= ~BTRFS_INODE_COMPRESS;
283 ip->flags |= BTRFS_INODE_NOCOMPRESS;
284 } else if (flags & FS_COMPR_FL) {
285 ip->flags |= BTRFS_INODE_COMPRESS;
286 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
ebcb904d
LZ
287 } else {
288 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
75e7cb7f 289 }
6cbff00f 290
4da6f1a3 291 trans = btrfs_start_transaction(root, 1);
f062abf0
LZ
292 if (IS_ERR(trans)) {
293 ret = PTR_ERR(trans);
294 goto out_drop;
295 }
6cbff00f 296
306424cc 297 btrfs_update_iflags(inode);
0c4d2d95 298 inode_inc_iversion(inode);
306424cc 299 inode->i_ctime = CURRENT_TIME;
6cbff00f 300 ret = btrfs_update_inode(trans, root, inode);
6cbff00f 301
6cbff00f 302 btrfs_end_transaction(trans, root);
f062abf0
LZ
303 out_drop:
304 if (ret) {
305 ip->flags = ip_oldflags;
306 inode->i_flags = i_oldflags;
307 }
6cbff00f 308
6cbff00f
CH
309 out_unlock:
310 mutex_unlock(&inode->i_mutex);
e7848683 311 mnt_drop_write_file(file);
2d4e6f6a 312 return ret;
6cbff00f
CH
313}
314
315static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
316{
496ad9aa 317 struct inode *inode = file_inode(file);
6cbff00f
CH
318
319 return put_user(inode->i_generation, arg);
320}
f46b5a66 321
f7039b1d
LD
322static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
323{
54563d41 324 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
f7039b1d
LD
325 struct btrfs_device *device;
326 struct request_queue *q;
327 struct fstrim_range range;
328 u64 minlen = ULLONG_MAX;
329 u64 num_devices = 0;
815745cf 330 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
f7039b1d
LD
331 int ret;
332
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
335
1f78160c
XG
336 rcu_read_lock();
337 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
338 dev_list) {
f7039b1d
LD
339 if (!device->bdev)
340 continue;
341 q = bdev_get_queue(device->bdev);
342 if (blk_queue_discard(q)) {
343 num_devices++;
344 minlen = min((u64)q->limits.discard_granularity,
345 minlen);
346 }
347 }
1f78160c 348 rcu_read_unlock();
f4c697e6 349
f7039b1d
LD
350 if (!num_devices)
351 return -EOPNOTSUPP;
f7039b1d
LD
352 if (copy_from_user(&range, arg, sizeof(range)))
353 return -EFAULT;
e515c18b
LC
354 if (range.start > total_bytes ||
355 range.len < fs_info->sb->s_blocksize)
f4c697e6 356 return -EINVAL;
f7039b1d 357
f4c697e6 358 range.len = min(range.len, total_bytes - range.start);
f7039b1d 359 range.minlen = max(range.minlen, minlen);
815745cf 360 ret = btrfs_trim_fs(fs_info->tree_root, &range);
f7039b1d
LD
361 if (ret < 0)
362 return ret;
363
364 if (copy_to_user(arg, &range, sizeof(range)))
365 return -EFAULT;
366
367 return 0;
368}
369
dd5f9615
SB
370int btrfs_is_empty_uuid(u8 *uuid)
371{
46e0f66a
CM
372 int i;
373
374 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
375 if (uuid[i])
376 return 0;
377 }
378 return 1;
dd5f9615
SB
379}
380
d5c12070 381static noinline int create_subvol(struct inode *dir,
cb8e7090 382 struct dentry *dentry,
72fd032e 383 char *name, int namelen,
6f72c7e2 384 u64 *async_transid,
8696c533 385 struct btrfs_qgroup_inherit *inherit)
f46b5a66
CH
386{
387 struct btrfs_trans_handle *trans;
388 struct btrfs_key key;
389 struct btrfs_root_item root_item;
390 struct btrfs_inode_item *inode_item;
391 struct extent_buffer *leaf;
d5c12070 392 struct btrfs_root *root = BTRFS_I(dir)->root;
76dda93c 393 struct btrfs_root *new_root;
d5c12070 394 struct btrfs_block_rsv block_rsv;
8ea05e3a 395 struct timespec cur_time = CURRENT_TIME;
f46b5a66
CH
396 int ret;
397 int err;
398 u64 objectid;
399 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3de4586c 400 u64 index = 0;
d5c12070 401 u64 qgroup_reserved;
8ea05e3a 402 uuid_le new_uuid;
f46b5a66 403
581bb050 404 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
2fbe8c8a 405 if (ret)
a22285a6 406 return ret;
6a912213 407
d5c12070 408 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
9ed74f2d 409 /*
d5c12070
MX
410 * The same as the snapshot creation, please see the comment
411 * of create_snapshot().
9ed74f2d 412 */
d5c12070 413 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
dd5f9615 414 8, &qgroup_reserved, false);
d5c12070
MX
415 if (ret)
416 return ret;
417
418 trans = btrfs_start_transaction(root, 0);
419 if (IS_ERR(trans)) {
420 ret = PTR_ERR(trans);
421 goto out;
422 }
423 trans->block_rsv = &block_rsv;
424 trans->bytes_reserved = block_rsv.size;
f46b5a66 425
8696c533 426 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
6f72c7e2
AJ
427 if (ret)
428 goto fail;
429
5d4f98a2 430 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
5581a51a 431 0, objectid, NULL, 0, 0, 0);
8e8a1e31
JB
432 if (IS_ERR(leaf)) {
433 ret = PTR_ERR(leaf);
434 goto fail;
435 }
f46b5a66 436
5d4f98a2 437 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
f46b5a66
CH
438 btrfs_set_header_bytenr(leaf, leaf->start);
439 btrfs_set_header_generation(leaf, trans->transid);
5d4f98a2 440 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
f46b5a66
CH
441 btrfs_set_header_owner(leaf, objectid);
442
0a4e5586 443 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
f46b5a66 444 BTRFS_FSID_SIZE);
5d4f98a2 445 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
b308bc2f 446 btrfs_header_chunk_tree_uuid(leaf),
5d4f98a2 447 BTRFS_UUID_SIZE);
f46b5a66
CH
448 btrfs_mark_buffer_dirty(leaf);
449
8ea05e3a
AB
450 memset(&root_item, 0, sizeof(root_item));
451
f46b5a66 452 inode_item = &root_item.inode;
3cae210f
QW
453 btrfs_set_stack_inode_generation(inode_item, 1);
454 btrfs_set_stack_inode_size(inode_item, 3);
455 btrfs_set_stack_inode_nlink(inode_item, 1);
456 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
457 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
f46b5a66 458
3cae210f
QW
459 btrfs_set_root_flags(&root_item, 0);
460 btrfs_set_root_limit(&root_item, 0);
461 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
08fe4db1 462
f46b5a66 463 btrfs_set_root_bytenr(&root_item, leaf->start);
84234f3a 464 btrfs_set_root_generation(&root_item, trans->transid);
f46b5a66
CH
465 btrfs_set_root_level(&root_item, 0);
466 btrfs_set_root_refs(&root_item, 1);
86b9f2ec 467 btrfs_set_root_used(&root_item, leaf->len);
80ff3856 468 btrfs_set_root_last_snapshot(&root_item, 0);
f46b5a66 469
8ea05e3a
AB
470 btrfs_set_root_generation_v2(&root_item,
471 btrfs_root_generation(&root_item));
472 uuid_le_gen(&new_uuid);
473 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
3cae210f
QW
474 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
475 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
8ea05e3a
AB
476 root_item.ctime = root_item.otime;
477 btrfs_set_root_ctransid(&root_item, trans->transid);
478 btrfs_set_root_otransid(&root_item, trans->transid);
f46b5a66 479
925baedd 480 btrfs_tree_unlock(leaf);
f46b5a66
CH
481 free_extent_buffer(leaf);
482 leaf = NULL;
483
484 btrfs_set_root_dirid(&root_item, new_dirid);
485
486 key.objectid = objectid;
5d4f98a2 487 key.offset = 0;
f46b5a66
CH
488 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
489 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
490 &root_item);
491 if (ret)
492 goto fail;
493
76dda93c
YZ
494 key.offset = (u64)-1;
495 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
79787eaa
JM
496 if (IS_ERR(new_root)) {
497 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
498 ret = PTR_ERR(new_root);
499 goto fail;
500 }
76dda93c
YZ
501
502 btrfs_record_root_in_trans(trans, new_root);
503
d82a6f1d 504 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
ce598979
MF
505 if (ret) {
506 /* We potentially lose an unused inode item here */
79787eaa 507 btrfs_abort_transaction(trans, root, ret);
ce598979
MF
508 goto fail;
509 }
510
f46b5a66
CH
511 /*
512 * insert the directory item
513 */
3de4586c 514 ret = btrfs_set_inode_index(dir, &index);
79787eaa
JM
515 if (ret) {
516 btrfs_abort_transaction(trans, root, ret);
517 goto fail;
518 }
3de4586c
CM
519
520 ret = btrfs_insert_dir_item(trans, root,
16cdcec7 521 name, namelen, dir, &key,
3de4586c 522 BTRFS_FT_DIR, index);
79787eaa
JM
523 if (ret) {
524 btrfs_abort_transaction(trans, root, ret);
f46b5a66 525 goto fail;
79787eaa 526 }
0660b5af 527
52c26179
YZ
528 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
529 ret = btrfs_update_inode(trans, root, dir);
530 BUG_ON(ret);
531
0660b5af 532 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4df27c4d 533 objectid, root->root_key.objectid,
33345d01 534 btrfs_ino(dir), index, name, namelen);
76dda93c 535 BUG_ON(ret);
f46b5a66 536
dd5f9615
SB
537 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
538 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
539 objectid);
540 if (ret)
541 btrfs_abort_transaction(trans, root, ret);
542
f46b5a66 543fail:
d5c12070
MX
544 trans->block_rsv = NULL;
545 trans->bytes_reserved = 0;
72fd032e
SW
546 if (async_transid) {
547 *async_transid = trans->transid;
548 err = btrfs_commit_transaction_async(trans, root, 1);
00d71c9c
MX
549 if (err)
550 err = btrfs_commit_transaction(trans, root);
72fd032e
SW
551 } else {
552 err = btrfs_commit_transaction(trans, root);
553 }
f46b5a66
CH
554 if (err && !ret)
555 ret = err;
1a65e24b
CM
556
557 if (!ret)
558 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
d5c12070
MX
559out:
560 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
f46b5a66
CH
561 return ret;
562}
563
e9662f70
MX
564static int create_snapshot(struct btrfs_root *root, struct inode *dir,
565 struct dentry *dentry, char *name, int namelen,
566 u64 *async_transid, bool readonly,
567 struct btrfs_qgroup_inherit *inherit)
f46b5a66 568{
2e4bfab9 569 struct inode *inode;
f46b5a66
CH
570 struct btrfs_pending_snapshot *pending_snapshot;
571 struct btrfs_trans_handle *trans;
2e4bfab9 572 int ret;
f46b5a66
CH
573
574 if (!root->ref_cows)
575 return -EINVAL;
576
6a03843d
MX
577 ret = btrfs_start_delalloc_inodes(root, 0);
578 if (ret)
579 return ret;
580
b0244199 581 btrfs_wait_ordered_extents(root, -1);
6a03843d 582
3de4586c 583 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
a22285a6
YZ
584 if (!pending_snapshot)
585 return -ENOMEM;
586
66d8f3dd
MX
587 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
588 BTRFS_BLOCK_RSV_TEMP);
d5c12070
MX
589 /*
590 * 1 - parent dir inode
591 * 2 - dir entries
592 * 1 - root item
593 * 2 - root ref/backref
594 * 1 - root of snapshot
dd5f9615 595 * 1 - UUID item
d5c12070
MX
596 */
597 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
dd5f9615 598 &pending_snapshot->block_rsv, 8,
ee3441b4
JM
599 &pending_snapshot->qgroup_reserved,
600 false);
d5c12070
MX
601 if (ret)
602 goto out;
603
3de4586c 604 pending_snapshot->dentry = dentry;
f46b5a66 605 pending_snapshot->root = root;
b83cc969 606 pending_snapshot->readonly = readonly;
e9662f70 607 pending_snapshot->dir = dir;
8696c533 608 pending_snapshot->inherit = inherit;
a22285a6 609
d5c12070 610 trans = btrfs_start_transaction(root, 0);
a22285a6
YZ
611 if (IS_ERR(trans)) {
612 ret = PTR_ERR(trans);
613 goto fail;
614 }
615
8351583e 616 spin_lock(&root->fs_info->trans_lock);
f46b5a66
CH
617 list_add(&pending_snapshot->list,
618 &trans->transaction->pending_snapshots);
8351583e 619 spin_unlock(&root->fs_info->trans_lock);
72fd032e
SW
620 if (async_transid) {
621 *async_transid = trans->transid;
622 ret = btrfs_commit_transaction_async(trans,
623 root->fs_info->extent_root, 1);
00d71c9c
MX
624 if (ret)
625 ret = btrfs_commit_transaction(trans, root);
72fd032e
SW
626 } else {
627 ret = btrfs_commit_transaction(trans,
628 root->fs_info->extent_root);
629 }
aec8030a 630 if (ret)
c37b2b62 631 goto fail;
a22285a6
YZ
632
633 ret = pending_snapshot->error;
634 if (ret)
635 goto fail;
636
66b4ffd1
JB
637 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
638 if (ret)
639 goto fail;
f46b5a66 640
2fbe8c8a 641 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
2e4bfab9
YZ
642 if (IS_ERR(inode)) {
643 ret = PTR_ERR(inode);
644 goto fail;
645 }
646 BUG_ON(!inode);
647 d_instantiate(dentry, inode);
648 ret = 0;
649fail:
d5c12070
MX
650 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
651 &pending_snapshot->block_rsv,
652 pending_snapshot->qgroup_reserved);
653out:
a22285a6 654 kfree(pending_snapshot);
f46b5a66
CH
655 return ret;
656}
657
4260f7c7
SW
658/* copy of check_sticky in fs/namei.c()
659* It's inline, so penalty for filesystems that don't use sticky bit is
660* minimal.
661*/
662static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
663{
2f2f43d3 664 kuid_t fsuid = current_fsuid();
4260f7c7
SW
665
666 if (!(dir->i_mode & S_ISVTX))
667 return 0;
2f2f43d3 668 if (uid_eq(inode->i_uid, fsuid))
4260f7c7 669 return 0;
2f2f43d3 670 if (uid_eq(dir->i_uid, fsuid))
4260f7c7
SW
671 return 0;
672 return !capable(CAP_FOWNER);
673}
674
675/* copy of may_delete in fs/namei.c()
676 * Check whether we can remove a link victim from directory dir, check
677 * whether the type of victim is right.
678 * 1. We can't do it if dir is read-only (done in permission())
679 * 2. We should have write and exec permissions on dir
680 * 3. We can't remove anything from append-only dir
681 * 4. We can't do anything with immutable dir (done in permission())
682 * 5. If the sticky bit on dir is set we should either
683 * a. be owner of dir, or
684 * b. be owner of victim, or
685 * c. have CAP_FOWNER capability
686 * 6. If the victim is append-only or immutable we can't do antyhing with
687 * links pointing to it.
688 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
689 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
690 * 9. We can't remove a root or mountpoint.
691 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
692 * nfs_async_unlink().
693 */
694
67871254 695static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
4260f7c7
SW
696{
697 int error;
698
699 if (!victim->d_inode)
700 return -ENOENT;
701
702 BUG_ON(victim->d_parent->d_inode != dir);
4fa6b5ec 703 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
4260f7c7
SW
704
705 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
706 if (error)
707 return error;
708 if (IS_APPEND(dir))
709 return -EPERM;
710 if (btrfs_check_sticky(dir, victim->d_inode)||
711 IS_APPEND(victim->d_inode)||
712 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
713 return -EPERM;
714 if (isdir) {
715 if (!S_ISDIR(victim->d_inode->i_mode))
716 return -ENOTDIR;
717 if (IS_ROOT(victim))
718 return -EBUSY;
719 } else if (S_ISDIR(victim->d_inode->i_mode))
720 return -EISDIR;
721 if (IS_DEADDIR(dir))
722 return -ENOENT;
723 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
724 return -EBUSY;
725 return 0;
726}
727
cb8e7090
CH
728/* copy of may_create in fs/namei.c() */
729static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
730{
731 if (child->d_inode)
732 return -EEXIST;
733 if (IS_DEADDIR(dir))
734 return -ENOENT;
735 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
736}
737
738/*
739 * Create a new subvolume below @parent. This is largely modeled after
740 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
741 * inside this filesystem so it's quite a bit simpler.
742 */
76dda93c
YZ
743static noinline int btrfs_mksubvol(struct path *parent,
744 char *name, int namelen,
72fd032e 745 struct btrfs_root *snap_src,
6f72c7e2 746 u64 *async_transid, bool readonly,
8696c533 747 struct btrfs_qgroup_inherit *inherit)
cb8e7090 748{
76dda93c 749 struct inode *dir = parent->dentry->d_inode;
cb8e7090
CH
750 struct dentry *dentry;
751 int error;
752
5c50c9b8
DS
753 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
754 if (error == -EINTR)
755 return error;
cb8e7090
CH
756
757 dentry = lookup_one_len(name, parent->dentry, namelen);
758 error = PTR_ERR(dentry);
759 if (IS_ERR(dentry))
760 goto out_unlock;
761
762 error = -EEXIST;
763 if (dentry->d_inode)
764 goto out_dput;
765
76dda93c 766 error = btrfs_may_create(dir, dentry);
cb8e7090 767 if (error)
a874a63e 768 goto out_dput;
cb8e7090 769
9c52057c
CM
770 /*
771 * even if this name doesn't exist, we may get hash collisions.
772 * check for them now when we can safely fail
773 */
774 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
775 dir->i_ino, name,
776 namelen);
777 if (error)
778 goto out_dput;
779
76dda93c
YZ
780 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
781
782 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
783 goto out_up_read;
784
3de4586c 785 if (snap_src) {
e9662f70 786 error = create_snapshot(snap_src, dir, dentry, name, namelen,
6f72c7e2 787 async_transid, readonly, inherit);
3de4586c 788 } else {
d5c12070
MX
789 error = create_subvol(dir, dentry, name, namelen,
790 async_transid, inherit);
3de4586c 791 }
76dda93c
YZ
792 if (!error)
793 fsnotify_mkdir(dir, dentry);
794out_up_read:
795 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
cb8e7090
CH
796out_dput:
797 dput(dentry);
798out_unlock:
76dda93c 799 mutex_unlock(&dir->i_mutex);
cb8e7090
CH
800 return error;
801}
802
4cb5300b
CM
803/*
804 * When we're defragging a range, we don't want to kick it off again
805 * if it is really just waiting for delalloc to send it down.
806 * If we find a nice big extent or delalloc range for the bytes in the
807 * file you want to defrag, we return 0 to let you know to skip this
808 * part of the file
809 */
810static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
811{
812 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
813 struct extent_map *em = NULL;
814 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
815 u64 end;
816
817 read_lock(&em_tree->lock);
818 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
819 read_unlock(&em_tree->lock);
820
821 if (em) {
822 end = extent_map_end(em);
823 free_extent_map(em);
824 if (end - offset > thresh)
825 return 0;
826 }
827 /* if we already have a nice delalloc here, just stop */
828 thresh /= 2;
829 end = count_range_bits(io_tree, &offset, offset + thresh,
830 thresh, EXTENT_DELALLOC, 1);
831 if (end >= thresh)
832 return 0;
833 return 1;
834}
835
836/*
837 * helper function to walk through a file and find extents
838 * newer than a specific transid, and smaller than thresh.
839 *
840 * This is used by the defragging code to find new and small
841 * extents
842 */
843static int find_new_extents(struct btrfs_root *root,
844 struct inode *inode, u64 newer_than,
845 u64 *off, int thresh)
846{
847 struct btrfs_path *path;
848 struct btrfs_key min_key;
4cb5300b
CM
849 struct extent_buffer *leaf;
850 struct btrfs_file_extent_item *extent;
851 int type;
852 int ret;
a4689d2b 853 u64 ino = btrfs_ino(inode);
4cb5300b
CM
854
855 path = btrfs_alloc_path();
856 if (!path)
857 return -ENOMEM;
858
a4689d2b 859 min_key.objectid = ino;
4cb5300b
CM
860 min_key.type = BTRFS_EXTENT_DATA_KEY;
861 min_key.offset = *off;
862
4cb5300b
CM
863 path->keep_locks = 1;
864
67871254 865 while (1) {
6174d3cb 866 ret = btrfs_search_forward(root, &min_key, path, newer_than);
4cb5300b
CM
867 if (ret != 0)
868 goto none;
a4689d2b 869 if (min_key.objectid != ino)
4cb5300b
CM
870 goto none;
871 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
872 goto none;
873
874 leaf = path->nodes[0];
875 extent = btrfs_item_ptr(leaf, path->slots[0],
876 struct btrfs_file_extent_item);
877
878 type = btrfs_file_extent_type(leaf, extent);
879 if (type == BTRFS_FILE_EXTENT_REG &&
880 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
881 check_defrag_in_cache(inode, min_key.offset, thresh)) {
882 *off = min_key.offset;
883 btrfs_free_path(path);
884 return 0;
885 }
886
887 if (min_key.offset == (u64)-1)
888 goto none;
889
890 min_key.offset++;
891 btrfs_release_path(path);
892 }
893none:
894 btrfs_free_path(path);
895 return -ENOENT;
896}
897
6c282eb4 898static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
17ce6ef8
LB
899{
900 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6c282eb4
LZ
901 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
902 struct extent_map *em;
903 u64 len = PAGE_CACHE_SIZE;
17ce6ef8 904
6c282eb4
LZ
905 /*
906 * hopefully we have this extent in the tree already, try without
907 * the full extent lock
908 */
17ce6ef8 909 read_lock(&em_tree->lock);
6c282eb4 910 em = lookup_extent_mapping(em_tree, start, len);
17ce6ef8
LB
911 read_unlock(&em_tree->lock);
912
6c282eb4
LZ
913 if (!em) {
914 /* get the big lock and read metadata off disk */
915 lock_extent(io_tree, start, start + len - 1);
916 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
917 unlock_extent(io_tree, start, start + len - 1);
918
919 if (IS_ERR(em))
920 return NULL;
921 }
922
923 return em;
924}
17ce6ef8 925
6c282eb4
LZ
926static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
927{
928 struct extent_map *next;
929 bool ret = true;
930
931 /* this is the last extent */
932 if (em->start + em->len >= i_size_read(inode))
933 return false;
934
935 next = defrag_lookup_extent(inode, em->start + em->len);
936 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
937 ret = false;
938
939 free_extent_map(next);
17ce6ef8
LB
940 return ret;
941}
942
6c282eb4 943static int should_defrag_range(struct inode *inode, u64 start, int thresh,
a43a2111
AM
944 u64 *last_len, u64 *skip, u64 *defrag_end,
945 int compress)
940100a4 946{
6c282eb4 947 struct extent_map *em;
940100a4 948 int ret = 1;
6c282eb4 949 bool next_mergeable = true;
940100a4
CM
950
951 /*
008873ea 952 * make sure that once we start defragging an extent, we keep on
940100a4
CM
953 * defragging it
954 */
955 if (start < *defrag_end)
956 return 1;
957
958 *skip = 0;
959
6c282eb4
LZ
960 em = defrag_lookup_extent(inode, start);
961 if (!em)
962 return 0;
940100a4
CM
963
964 /* this will cover holes, and inline extents */
17ce6ef8 965 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
940100a4 966 ret = 0;
17ce6ef8
LB
967 goto out;
968 }
969
6c282eb4 970 next_mergeable = defrag_check_next_extent(inode, em);
940100a4
CM
971
972 /*
6c282eb4
LZ
973 * we hit a real extent, if it is big or the next extent is not a
974 * real extent, don't bother defragging it
940100a4 975 */
a43a2111 976 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
6c282eb4 977 (em->len >= thresh || !next_mergeable))
940100a4 978 ret = 0;
17ce6ef8 979out:
940100a4
CM
980 /*
981 * last_len ends up being a counter of how many bytes we've defragged.
982 * every time we choose not to defrag an extent, we reset *last_len
983 * so that the next tiny extent will force a defrag.
984 *
985 * The end result of this is that tiny extents before a single big
986 * extent will force at least part of that big extent to be defragged.
987 */
988 if (ret) {
940100a4
CM
989 *defrag_end = extent_map_end(em);
990 } else {
991 *last_len = 0;
992 *skip = extent_map_end(em);
993 *defrag_end = 0;
994 }
995
996 free_extent_map(em);
997 return ret;
998}
999
4cb5300b
CM
1000/*
1001 * it doesn't do much good to defrag one or two pages
1002 * at a time. This pulls in a nice chunk of pages
1003 * to COW and defrag.
1004 *
1005 * It also makes sure the delalloc code has enough
1006 * dirty data to avoid making new small extents as part
1007 * of the defrag
1008 *
1009 * It's a good idea to start RA on this range
1010 * before calling this.
1011 */
1012static int cluster_pages_for_defrag(struct inode *inode,
1013 struct page **pages,
1014 unsigned long start_index,
1015 int num_pages)
f46b5a66 1016{
4cb5300b
CM
1017 unsigned long file_end;
1018 u64 isize = i_size_read(inode);
1019 u64 page_start;
1020 u64 page_end;
1f12bd06 1021 u64 page_cnt;
4cb5300b
CM
1022 int ret;
1023 int i;
1024 int i_done;
3eaa2885 1025 struct btrfs_ordered_extent *ordered;
4cb5300b 1026 struct extent_state *cached_state = NULL;
600a45e1 1027 struct extent_io_tree *tree;
3b16a4e3 1028 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
4cb5300b 1029
4cb5300b 1030 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
1f12bd06
LB
1031 if (!isize || start_index > file_end)
1032 return 0;
1033
1034 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
4cb5300b
CM
1035
1036 ret = btrfs_delalloc_reserve_space(inode,
1f12bd06 1037 page_cnt << PAGE_CACHE_SHIFT);
4cb5300b
CM
1038 if (ret)
1039 return ret;
4cb5300b 1040 i_done = 0;
600a45e1 1041 tree = &BTRFS_I(inode)->io_tree;
4cb5300b
CM
1042
1043 /* step one, lock all the pages */
1f12bd06 1044 for (i = 0; i < page_cnt; i++) {
4cb5300b 1045 struct page *page;
600a45e1 1046again:
a94733d0 1047 page = find_or_create_page(inode->i_mapping,
600a45e1 1048 start_index + i, mask);
4cb5300b
CM
1049 if (!page)
1050 break;
1051
600a45e1
MX
1052 page_start = page_offset(page);
1053 page_end = page_start + PAGE_CACHE_SIZE - 1;
1054 while (1) {
d0082371 1055 lock_extent(tree, page_start, page_end);
600a45e1
MX
1056 ordered = btrfs_lookup_ordered_extent(inode,
1057 page_start);
d0082371 1058 unlock_extent(tree, page_start, page_end);
600a45e1
MX
1059 if (!ordered)
1060 break;
1061
1062 unlock_page(page);
1063 btrfs_start_ordered_extent(inode, ordered, 1);
1064 btrfs_put_ordered_extent(ordered);
1065 lock_page(page);
1f12bd06
LB
1066 /*
1067 * we unlocked the page above, so we need check if
1068 * it was released or not.
1069 */
1070 if (page->mapping != inode->i_mapping) {
1071 unlock_page(page);
1072 page_cache_release(page);
1073 goto again;
1074 }
600a45e1
MX
1075 }
1076
4cb5300b
CM
1077 if (!PageUptodate(page)) {
1078 btrfs_readpage(NULL, page);
1079 lock_page(page);
1080 if (!PageUptodate(page)) {
1081 unlock_page(page);
1082 page_cache_release(page);
1083 ret = -EIO;
1084 break;
1085 }
1086 }
600a45e1 1087
600a45e1
MX
1088 if (page->mapping != inode->i_mapping) {
1089 unlock_page(page);
1090 page_cache_release(page);
1091 goto again;
1092 }
1093
4cb5300b
CM
1094 pages[i] = page;
1095 i_done++;
1096 }
1097 if (!i_done || ret)
1098 goto out;
1099
1100 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1101 goto out;
1102
1103 /*
1104 * so now we have a nice long stream of locked
1105 * and up to date pages, lets wait on them
1106 */
1107 for (i = 0; i < i_done; i++)
1108 wait_on_page_writeback(pages[i]);
1109
1110 page_start = page_offset(pages[0]);
1111 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1112
1113 lock_extent_bits(&BTRFS_I(inode)->io_tree,
d0082371 1114 page_start, page_end - 1, 0, &cached_state);
4cb5300b
CM
1115 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1116 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
9e8a4a8b
LB
1117 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1118 &cached_state, GFP_NOFS);
4cb5300b 1119
1f12bd06 1120 if (i_done != page_cnt) {
9e0baf60
JB
1121 spin_lock(&BTRFS_I(inode)->lock);
1122 BTRFS_I(inode)->outstanding_extents++;
1123 spin_unlock(&BTRFS_I(inode)->lock);
4cb5300b 1124 btrfs_delalloc_release_space(inode,
1f12bd06 1125 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
4cb5300b
CM
1126 }
1127
1128
9e8a4a8b
LB
1129 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1130 &cached_state, GFP_NOFS);
4cb5300b
CM
1131
1132 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1133 page_start, page_end - 1, &cached_state,
1134 GFP_NOFS);
1135
1136 for (i = 0; i < i_done; i++) {
1137 clear_page_dirty_for_io(pages[i]);
1138 ClearPageChecked(pages[i]);
1139 set_page_extent_mapped(pages[i]);
1140 set_page_dirty(pages[i]);
1141 unlock_page(pages[i]);
1142 page_cache_release(pages[i]);
1143 }
1144 return i_done;
1145out:
1146 for (i = 0; i < i_done; i++) {
1147 unlock_page(pages[i]);
1148 page_cache_release(pages[i]);
1149 }
1f12bd06 1150 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
4cb5300b
CM
1151 return ret;
1152
1153}
1154
1155int btrfs_defrag_file(struct inode *inode, struct file *file,
1156 struct btrfs_ioctl_defrag_range_args *range,
1157 u64 newer_than, unsigned long max_to_defrag)
1158{
1159 struct btrfs_root *root = BTRFS_I(inode)->root;
4cb5300b 1160 struct file_ra_state *ra = NULL;
f46b5a66 1161 unsigned long last_index;
151a31b2 1162 u64 isize = i_size_read(inode);
940100a4
CM
1163 u64 last_len = 0;
1164 u64 skip = 0;
1165 u64 defrag_end = 0;
4cb5300b 1166 u64 newer_off = range->start;
f46b5a66 1167 unsigned long i;
008873ea 1168 unsigned long ra_index = 0;
f46b5a66 1169 int ret;
4cb5300b 1170 int defrag_count = 0;
1a419d85 1171 int compress_type = BTRFS_COMPRESS_ZLIB;
4cb5300b 1172 int extent_thresh = range->extent_thresh;
008873ea
LZ
1173 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1174 int cluster = max_cluster;
4cb5300b
CM
1175 u64 new_align = ~((u64)128 * 1024 - 1);
1176 struct page **pages = NULL;
1177
0abd5b17
LB
1178 if (isize == 0)
1179 return 0;
1180
1181 if (range->start >= isize)
1182 return -EINVAL;
1a419d85
LZ
1183
1184 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1185 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1186 return -EINVAL;
1187 if (range->compress_type)
1188 compress_type = range->compress_type;
1189 }
f46b5a66 1190
0abd5b17
LB
1191 if (extent_thresh == 0)
1192 extent_thresh = 256 * 1024;
940100a4 1193
4cb5300b
CM
1194 /*
1195 * if we were not given a file, allocate a readahead
1196 * context
1197 */
1198 if (!file) {
1199 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1200 if (!ra)
1201 return -ENOMEM;
1202 file_ra_state_init(ra, inode->i_mapping);
1203 } else {
1204 ra = &file->f_ra;
1205 }
1206
d9b0d9ba 1207 pages = kmalloc_array(max_cluster, sizeof(struct page *),
4cb5300b
CM
1208 GFP_NOFS);
1209 if (!pages) {
1210 ret = -ENOMEM;
1211 goto out_ra;
1212 }
1213
1214 /* find the last page to defrag */
1e701a32 1215 if (range->start + range->len > range->start) {
151a31b2 1216 last_index = min_t(u64, isize - 1,
1e701a32
CM
1217 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1218 } else {
151a31b2 1219 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1e701a32
CM
1220 }
1221
4cb5300b
CM
1222 if (newer_than) {
1223 ret = find_new_extents(root, inode, newer_than,
1224 &newer_off, 64 * 1024);
1225 if (!ret) {
1226 range->start = newer_off;
1227 /*
1228 * we always align our defrag to help keep
1229 * the extents in the file evenly spaced
1230 */
1231 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
4cb5300b
CM
1232 } else
1233 goto out_ra;
1234 } else {
1235 i = range->start >> PAGE_CACHE_SHIFT;
1236 }
1237 if (!max_to_defrag)
7ec31b54 1238 max_to_defrag = last_index + 1;
4cb5300b 1239
2a0f7f57
LZ
1240 /*
1241 * make writeback starts from i, so the defrag range can be
1242 * written sequentially.
1243 */
1244 if (i < inode->i_mapping->writeback_index)
1245 inode->i_mapping->writeback_index = i;
1246
f7f43cc8
CM
1247 while (i <= last_index && defrag_count < max_to_defrag &&
1248 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1249 PAGE_CACHE_SHIFT)) {
4cb5300b
CM
1250 /*
1251 * make sure we stop running if someone unmounts
1252 * the FS
1253 */
1254 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1255 break;
1256
210549eb
DS
1257 if (btrfs_defrag_cancelled(root->fs_info)) {
1258 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1259 ret = -EAGAIN;
1260 break;
1261 }
1262
66c26892 1263 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
6c282eb4 1264 extent_thresh, &last_len, &skip,
a43a2111
AM
1265 &defrag_end, range->flags &
1266 BTRFS_DEFRAG_RANGE_COMPRESS)) {
940100a4
CM
1267 unsigned long next;
1268 /*
1269 * the should_defrag function tells us how much to skip
1270 * bump our counter by the suggested amount
1271 */
1272 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1273 i = max(i + 1, next);
1274 continue;
1275 }
008873ea
LZ
1276
1277 if (!newer_than) {
1278 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1279 PAGE_CACHE_SHIFT) - i;
1280 cluster = min(cluster, max_cluster);
1281 } else {
1282 cluster = max_cluster;
1283 }
1284
008873ea
LZ
1285 if (i + cluster > ra_index) {
1286 ra_index = max(i, ra_index);
1287 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1288 cluster);
1289 ra_index += max_cluster;
1290 }
940100a4 1291
ecb8bea8 1292 mutex_lock(&inode->i_mutex);
633085c7
FDBM
1293 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1294 BTRFS_I(inode)->force_compress = compress_type;
008873ea 1295 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
ecb8bea8
LB
1296 if (ret < 0) {
1297 mutex_unlock(&inode->i_mutex);
4cb5300b 1298 goto out_ra;
ecb8bea8 1299 }
4cb5300b
CM
1300
1301 defrag_count += ret;
d0e1d66b 1302 balance_dirty_pages_ratelimited(inode->i_mapping);
ecb8bea8 1303 mutex_unlock(&inode->i_mutex);
4cb5300b
CM
1304
1305 if (newer_than) {
1306 if (newer_off == (u64)-1)
1307 break;
1308
e1f041e1
LB
1309 if (ret > 0)
1310 i += ret;
1311
4cb5300b
CM
1312 newer_off = max(newer_off + 1,
1313 (u64)i << PAGE_CACHE_SHIFT);
1314
1315 ret = find_new_extents(root, inode,
1316 newer_than, &newer_off,
1317 64 * 1024);
1318 if (!ret) {
1319 range->start = newer_off;
1320 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
4cb5300b
CM
1321 } else {
1322 break;
f46b5a66 1323 }
4cb5300b 1324 } else {
008873ea 1325 if (ret > 0) {
cbcc8326 1326 i += ret;
008873ea
LZ
1327 last_len += ret << PAGE_CACHE_SHIFT;
1328 } else {
cbcc8326 1329 i++;
008873ea
LZ
1330 last_len = 0;
1331 }
f46b5a66 1332 }
f46b5a66
CH
1333 }
1334
1e701a32
CM
1335 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1336 filemap_flush(inode->i_mapping);
1337
1338 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1339 /* the filemap_flush will queue IO into the worker threads, but
1340 * we have to make sure the IO is actually started and that
1341 * ordered extents get created before we return
1342 */
1343 atomic_inc(&root->fs_info->async_submit_draining);
1344 while (atomic_read(&root->fs_info->nr_async_submits) ||
1345 atomic_read(&root->fs_info->async_delalloc_pages)) {
1346 wait_event(root->fs_info->async_submit_wait,
1347 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1348 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1349 }
1350 atomic_dec(&root->fs_info->async_submit_draining);
1e701a32
CM
1351 }
1352
1a419d85 1353 if (range->compress_type == BTRFS_COMPRESS_LZO) {
2b0ce2c2 1354 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1a419d85
LZ
1355 }
1356
60ccf82f 1357 ret = defrag_count;
940100a4 1358
4cb5300b 1359out_ra:
633085c7
FDBM
1360 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1361 mutex_lock(&inode->i_mutex);
1362 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1363 mutex_unlock(&inode->i_mutex);
1364 }
4cb5300b
CM
1365 if (!file)
1366 kfree(ra);
1367 kfree(pages);
940100a4 1368 return ret;
f46b5a66
CH
1369}
1370
198605a8 1371static noinline int btrfs_ioctl_resize(struct file *file,
76dda93c 1372 void __user *arg)
f46b5a66
CH
1373{
1374 u64 new_size;
1375 u64 old_size;
1376 u64 devid = 1;
496ad9aa 1377 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
f46b5a66
CH
1378 struct btrfs_ioctl_vol_args *vol_args;
1379 struct btrfs_trans_handle *trans;
1380 struct btrfs_device *device = NULL;
1381 char *sizestr;
1382 char *devstr = NULL;
1383 int ret = 0;
f46b5a66
CH
1384 int mod = 0;
1385
e441d54d
CM
1386 if (!capable(CAP_SYS_ADMIN))
1387 return -EPERM;
1388
198605a8
MX
1389 ret = mnt_want_write_file(file);
1390 if (ret)
1391 return ret;
1392
5ac00add
SB
1393 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1394 1)) {
97547676 1395 mnt_drop_write_file(file);
e57138b3 1396 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
c9e9f97b
ID
1397 }
1398
5ac00add 1399 mutex_lock(&root->fs_info->volume_mutex);
dae7b665 1400 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
1401 if (IS_ERR(vol_args)) {
1402 ret = PTR_ERR(vol_args);
1403 goto out;
1404 }
5516e595
MF
1405
1406 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 1407
f46b5a66
CH
1408 sizestr = vol_args->name;
1409 devstr = strchr(sizestr, ':');
1410 if (devstr) {
1411 char *end;
1412 sizestr = devstr + 1;
1413 *devstr = '\0';
1414 devstr = vol_args->name;
1415 devid = simple_strtoull(devstr, &end, 10);
dfd79829
MX
1416 if (!devid) {
1417 ret = -EINVAL;
1418 goto out_free;
1419 }
c1c9ff7c 1420 printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
f46b5a66 1421 }
dba60f3f 1422
aa1b8cd4 1423 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
f46b5a66 1424 if (!device) {
5bb14682 1425 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
c1c9ff7c 1426 devid);
dfd79829 1427 ret = -ENODEV;
c9e9f97b 1428 goto out_free;
f46b5a66 1429 }
dba60f3f
MX
1430
1431 if (!device->writeable) {
4e42ae1b 1432 printk(KERN_INFO "btrfs: resizer unable to apply on "
dba60f3f 1433 "readonly device %llu\n",
c1c9ff7c 1434 devid);
dfd79829 1435 ret = -EPERM;
4e42ae1b
LB
1436 goto out_free;
1437 }
1438
f46b5a66
CH
1439 if (!strcmp(sizestr, "max"))
1440 new_size = device->bdev->bd_inode->i_size;
1441 else {
1442 if (sizestr[0] == '-') {
1443 mod = -1;
1444 sizestr++;
1445 } else if (sizestr[0] == '+') {
1446 mod = 1;
1447 sizestr++;
1448 }
91748467 1449 new_size = memparse(sizestr, NULL);
f46b5a66
CH
1450 if (new_size == 0) {
1451 ret = -EINVAL;
c9e9f97b 1452 goto out_free;
f46b5a66
CH
1453 }
1454 }
1455
63a212ab 1456 if (device->is_tgtdev_for_dev_replace) {
dfd79829 1457 ret = -EPERM;
63a212ab
SB
1458 goto out_free;
1459 }
1460
f46b5a66
CH
1461 old_size = device->total_bytes;
1462
1463 if (mod < 0) {
1464 if (new_size > old_size) {
1465 ret = -EINVAL;
c9e9f97b 1466 goto out_free;
f46b5a66
CH
1467 }
1468 new_size = old_size - new_size;
1469 } else if (mod > 0) {
1470 new_size = old_size + new_size;
1471 }
1472
1473 if (new_size < 256 * 1024 * 1024) {
1474 ret = -EINVAL;
c9e9f97b 1475 goto out_free;
f46b5a66
CH
1476 }
1477 if (new_size > device->bdev->bd_inode->i_size) {
1478 ret = -EFBIG;
c9e9f97b 1479 goto out_free;
f46b5a66
CH
1480 }
1481
1482 do_div(new_size, root->sectorsize);
1483 new_size *= root->sectorsize;
1484
606686ee 1485 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
c1c9ff7c 1486 rcu_str_deref(device->name), new_size);
f46b5a66
CH
1487
1488 if (new_size > old_size) {
a22285a6 1489 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
1490 if (IS_ERR(trans)) {
1491 ret = PTR_ERR(trans);
c9e9f97b 1492 goto out_free;
98d5dc13 1493 }
f46b5a66
CH
1494 ret = btrfs_grow_device(trans, device, new_size);
1495 btrfs_commit_transaction(trans, root);
ece7d20e 1496 } else if (new_size < old_size) {
f46b5a66 1497 ret = btrfs_shrink_device(device, new_size);
0253f40e 1498 } /* equal, nothing need to do */
f46b5a66 1499
c9e9f97b 1500out_free:
f46b5a66 1501 kfree(vol_args);
c9e9f97b
ID
1502out:
1503 mutex_unlock(&root->fs_info->volume_mutex);
5ac00add 1504 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
18f39c41 1505 mnt_drop_write_file(file);
f46b5a66
CH
1506 return ret;
1507}
1508
72fd032e 1509static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
6f72c7e2
AJ
1510 char *name, unsigned long fd, int subvol,
1511 u64 *transid, bool readonly,
8696c533 1512 struct btrfs_qgroup_inherit *inherit)
f46b5a66 1513{
f46b5a66 1514 int namelen;
3de4586c 1515 int ret = 0;
f46b5a66 1516
a874a63e
LB
1517 ret = mnt_want_write_file(file);
1518 if (ret)
1519 goto out;
1520
72fd032e
SW
1521 namelen = strlen(name);
1522 if (strchr(name, '/')) {
f46b5a66 1523 ret = -EINVAL;
a874a63e 1524 goto out_drop_write;
f46b5a66
CH
1525 }
1526
16780cab
CM
1527 if (name[0] == '.' &&
1528 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1529 ret = -EEXIST;
a874a63e 1530 goto out_drop_write;
16780cab
CM
1531 }
1532
3de4586c 1533 if (subvol) {
72fd032e 1534 ret = btrfs_mksubvol(&file->f_path, name, namelen,
6f72c7e2 1535 NULL, transid, readonly, inherit);
cb8e7090 1536 } else {
2903ff01 1537 struct fd src = fdget(fd);
3de4586c 1538 struct inode *src_inode;
2903ff01 1539 if (!src.file) {
3de4586c 1540 ret = -EINVAL;
a874a63e 1541 goto out_drop_write;
3de4586c
CM
1542 }
1543
496ad9aa
AV
1544 src_inode = file_inode(src.file);
1545 if (src_inode->i_sb != file_inode(file)->i_sb) {
d397712b
CM
1546 printk(KERN_INFO "btrfs: Snapshot src from "
1547 "another FS\n");
3de4586c 1548 ret = -EINVAL;
ecd18815
AV
1549 } else {
1550 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1551 BTRFS_I(src_inode)->root,
1552 transid, readonly, inherit);
3de4586c 1553 }
2903ff01 1554 fdput(src);
cb8e7090 1555 }
a874a63e
LB
1556out_drop_write:
1557 mnt_drop_write_file(file);
f46b5a66 1558out:
72fd032e
SW
1559 return ret;
1560}
1561
1562static noinline int btrfs_ioctl_snap_create(struct file *file,
fa0d2b9b 1563 void __user *arg, int subvol)
72fd032e 1564{
fa0d2b9b 1565 struct btrfs_ioctl_vol_args *vol_args;
72fd032e
SW
1566 int ret;
1567
fa0d2b9b
LZ
1568 vol_args = memdup_user(arg, sizeof(*vol_args));
1569 if (IS_ERR(vol_args))
1570 return PTR_ERR(vol_args);
1571 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
72fd032e 1572
fa0d2b9b 1573 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
b83cc969 1574 vol_args->fd, subvol,
6f72c7e2 1575 NULL, false, NULL);
fdfb1e4f 1576
fa0d2b9b
LZ
1577 kfree(vol_args);
1578 return ret;
1579}
fdfb1e4f 1580
fa0d2b9b
LZ
1581static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1582 void __user *arg, int subvol)
1583{
1584 struct btrfs_ioctl_vol_args_v2 *vol_args;
1585 int ret;
1586 u64 transid = 0;
1587 u64 *ptr = NULL;
b83cc969 1588 bool readonly = false;
6f72c7e2 1589 struct btrfs_qgroup_inherit *inherit = NULL;
75eaa0e2 1590
fa0d2b9b
LZ
1591 vol_args = memdup_user(arg, sizeof(*vol_args));
1592 if (IS_ERR(vol_args))
1593 return PTR_ERR(vol_args);
1594 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
75eaa0e2 1595
b83cc969 1596 if (vol_args->flags &
6f72c7e2
AJ
1597 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1598 BTRFS_SUBVOL_QGROUP_INHERIT)) {
b83cc969 1599 ret = -EOPNOTSUPP;
fa0d2b9b 1600 goto out;
72fd032e 1601 }
fa0d2b9b
LZ
1602
1603 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1604 ptr = &transid;
b83cc969
LZ
1605 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1606 readonly = true;
6f72c7e2
AJ
1607 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1608 if (vol_args->size > PAGE_CACHE_SIZE) {
1609 ret = -EINVAL;
1610 goto out;
1611 }
1612 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1613 if (IS_ERR(inherit)) {
1614 ret = PTR_ERR(inherit);
1615 goto out;
1616 }
1617 }
fa0d2b9b
LZ
1618
1619 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
6f72c7e2 1620 vol_args->fd, subvol, ptr,
8696c533 1621 readonly, inherit);
fa0d2b9b
LZ
1622
1623 if (ret == 0 && ptr &&
1624 copy_to_user(arg +
1625 offsetof(struct btrfs_ioctl_vol_args_v2,
1626 transid), ptr, sizeof(*ptr)))
1627 ret = -EFAULT;
fdfb1e4f 1628out:
f46b5a66 1629 kfree(vol_args);
6f72c7e2 1630 kfree(inherit);
f46b5a66
CH
1631 return ret;
1632}
1633
0caa102d
LZ
1634static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1635 void __user *arg)
1636{
496ad9aa 1637 struct inode *inode = file_inode(file);
0caa102d
LZ
1638 struct btrfs_root *root = BTRFS_I(inode)->root;
1639 int ret = 0;
1640 u64 flags = 0;
1641
33345d01 1642 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
0caa102d
LZ
1643 return -EINVAL;
1644
1645 down_read(&root->fs_info->subvol_sem);
1646 if (btrfs_root_readonly(root))
1647 flags |= BTRFS_SUBVOL_RDONLY;
1648 up_read(&root->fs_info->subvol_sem);
1649
1650 if (copy_to_user(arg, &flags, sizeof(flags)))
1651 ret = -EFAULT;
1652
1653 return ret;
1654}
1655
1656static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1657 void __user *arg)
1658{
496ad9aa 1659 struct inode *inode = file_inode(file);
0caa102d
LZ
1660 struct btrfs_root *root = BTRFS_I(inode)->root;
1661 struct btrfs_trans_handle *trans;
1662 u64 root_flags;
1663 u64 flags;
1664 int ret = 0;
1665
b9ca0664
LB
1666 ret = mnt_want_write_file(file);
1667 if (ret)
1668 goto out;
0caa102d 1669
b9ca0664
LB
1670 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1671 ret = -EINVAL;
1672 goto out_drop_write;
1673 }
0caa102d 1674
b9ca0664
LB
1675 if (copy_from_user(&flags, arg, sizeof(flags))) {
1676 ret = -EFAULT;
1677 goto out_drop_write;
1678 }
0caa102d 1679
b9ca0664
LB
1680 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1681 ret = -EINVAL;
1682 goto out_drop_write;
1683 }
0caa102d 1684
b9ca0664
LB
1685 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1686 ret = -EOPNOTSUPP;
1687 goto out_drop_write;
1688 }
0caa102d 1689
b9ca0664
LB
1690 if (!inode_owner_or_capable(inode)) {
1691 ret = -EACCES;
1692 goto out_drop_write;
1693 }
b4dc2b8c 1694
0caa102d
LZ
1695 down_write(&root->fs_info->subvol_sem);
1696
1697 /* nothing to do */
1698 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
b9ca0664 1699 goto out_drop_sem;
0caa102d
LZ
1700
1701 root_flags = btrfs_root_flags(&root->root_item);
1702 if (flags & BTRFS_SUBVOL_RDONLY)
1703 btrfs_set_root_flags(&root->root_item,
1704 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1705 else
1706 btrfs_set_root_flags(&root->root_item,
1707 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1708
1709 trans = btrfs_start_transaction(root, 1);
1710 if (IS_ERR(trans)) {
1711 ret = PTR_ERR(trans);
1712 goto out_reset;
1713 }
1714
b4dc2b8c 1715 ret = btrfs_update_root(trans, root->fs_info->tree_root,
0caa102d
LZ
1716 &root->root_key, &root->root_item);
1717
1718 btrfs_commit_transaction(trans, root);
1719out_reset:
1720 if (ret)
1721 btrfs_set_root_flags(&root->root_item, root_flags);
b9ca0664 1722out_drop_sem:
0caa102d 1723 up_write(&root->fs_info->subvol_sem);
b9ca0664
LB
1724out_drop_write:
1725 mnt_drop_write_file(file);
1726out:
0caa102d
LZ
1727 return ret;
1728}
1729
76dda93c
YZ
1730/*
1731 * helper to check if the subvolume references other subvolumes
1732 */
1733static noinline int may_destroy_subvol(struct btrfs_root *root)
1734{
1735 struct btrfs_path *path;
175a2b87 1736 struct btrfs_dir_item *di;
76dda93c 1737 struct btrfs_key key;
175a2b87 1738 u64 dir_id;
76dda93c
YZ
1739 int ret;
1740
1741 path = btrfs_alloc_path();
1742 if (!path)
1743 return -ENOMEM;
1744
175a2b87
JB
1745 /* Make sure this root isn't set as the default subvol */
1746 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1747 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1748 dir_id, "default", 7, 0);
1749 if (di && !IS_ERR(di)) {
1750 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1751 if (key.objectid == root->root_key.objectid) {
1752 ret = -ENOTEMPTY;
1753 goto out;
1754 }
1755 btrfs_release_path(path);
1756 }
1757
76dda93c
YZ
1758 key.objectid = root->root_key.objectid;
1759 key.type = BTRFS_ROOT_REF_KEY;
1760 key.offset = (u64)-1;
1761
1762 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1763 &key, path, 0, 0);
1764 if (ret < 0)
1765 goto out;
1766 BUG_ON(ret == 0);
1767
1768 ret = 0;
1769 if (path->slots[0] > 0) {
1770 path->slots[0]--;
1771 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1772 if (key.objectid == root->root_key.objectid &&
1773 key.type == BTRFS_ROOT_REF_KEY)
1774 ret = -ENOTEMPTY;
1775 }
1776out:
1777 btrfs_free_path(path);
1778 return ret;
1779}
1780
ac8e9819
CM
1781static noinline int key_in_sk(struct btrfs_key *key,
1782 struct btrfs_ioctl_search_key *sk)
1783{
abc6e134
CM
1784 struct btrfs_key test;
1785 int ret;
1786
1787 test.objectid = sk->min_objectid;
1788 test.type = sk->min_type;
1789 test.offset = sk->min_offset;
1790
1791 ret = btrfs_comp_cpu_keys(key, &test);
1792 if (ret < 0)
ac8e9819 1793 return 0;
abc6e134
CM
1794
1795 test.objectid = sk->max_objectid;
1796 test.type = sk->max_type;
1797 test.offset = sk->max_offset;
1798
1799 ret = btrfs_comp_cpu_keys(key, &test);
1800 if (ret > 0)
ac8e9819
CM
1801 return 0;
1802 return 1;
1803}
1804
1805static noinline int copy_to_sk(struct btrfs_root *root,
1806 struct btrfs_path *path,
1807 struct btrfs_key *key,
1808 struct btrfs_ioctl_search_key *sk,
1809 char *buf,
1810 unsigned long *sk_offset,
1811 int *num_found)
1812{
1813 u64 found_transid;
1814 struct extent_buffer *leaf;
1815 struct btrfs_ioctl_search_header sh;
1816 unsigned long item_off;
1817 unsigned long item_len;
1818 int nritems;
1819 int i;
1820 int slot;
ac8e9819
CM
1821 int ret = 0;
1822
1823 leaf = path->nodes[0];
1824 slot = path->slots[0];
1825 nritems = btrfs_header_nritems(leaf);
1826
1827 if (btrfs_header_generation(leaf) > sk->max_transid) {
1828 i = nritems;
1829 goto advance_key;
1830 }
1831 found_transid = btrfs_header_generation(leaf);
1832
1833 for (i = slot; i < nritems; i++) {
1834 item_off = btrfs_item_ptr_offset(leaf, i);
1835 item_len = btrfs_item_size_nr(leaf, i);
1836
03b71c6c
GP
1837 btrfs_item_key_to_cpu(leaf, key, i);
1838 if (!key_in_sk(key, sk))
1839 continue;
1840
1841 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
ac8e9819
CM
1842 item_len = 0;
1843
1844 if (sizeof(sh) + item_len + *sk_offset >
1845 BTRFS_SEARCH_ARGS_BUFSIZE) {
1846 ret = 1;
1847 goto overflow;
1848 }
1849
ac8e9819
CM
1850 sh.objectid = key->objectid;
1851 sh.offset = key->offset;
1852 sh.type = key->type;
1853 sh.len = item_len;
1854 sh.transid = found_transid;
1855
1856 /* copy search result header */
1857 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1858 *sk_offset += sizeof(sh);
1859
1860 if (item_len) {
1861 char *p = buf + *sk_offset;
1862 /* copy the item */
1863 read_extent_buffer(leaf, p,
1864 item_off, item_len);
1865 *sk_offset += item_len;
ac8e9819 1866 }
e2156867 1867 (*num_found)++;
ac8e9819
CM
1868
1869 if (*num_found >= sk->nr_items)
1870 break;
1871 }
1872advance_key:
abc6e134
CM
1873 ret = 0;
1874 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
ac8e9819 1875 key->offset++;
abc6e134
CM
1876 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1877 key->offset = 0;
ac8e9819 1878 key->type++;
abc6e134
CM
1879 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1880 key->offset = 0;
1881 key->type = 0;
ac8e9819 1882 key->objectid++;
abc6e134
CM
1883 } else
1884 ret = 1;
ac8e9819 1885overflow:
ac8e9819
CM
1886 return ret;
1887}
1888
1889static noinline int search_ioctl(struct inode *inode,
1890 struct btrfs_ioctl_search_args *args)
1891{
1892 struct btrfs_root *root;
1893 struct btrfs_key key;
ac8e9819
CM
1894 struct btrfs_path *path;
1895 struct btrfs_ioctl_search_key *sk = &args->key;
1896 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1897 int ret;
1898 int num_found = 0;
1899 unsigned long sk_offset = 0;
1900
1901 path = btrfs_alloc_path();
1902 if (!path)
1903 return -ENOMEM;
1904
1905 if (sk->tree_id == 0) {
1906 /* search the root of the inode that was passed */
1907 root = BTRFS_I(inode)->root;
1908 } else {
1909 key.objectid = sk->tree_id;
1910 key.type = BTRFS_ROOT_ITEM_KEY;
1911 key.offset = (u64)-1;
1912 root = btrfs_read_fs_root_no_name(info, &key);
1913 if (IS_ERR(root)) {
1914 printk(KERN_ERR "could not find root %llu\n",
1915 sk->tree_id);
1916 btrfs_free_path(path);
1917 return -ENOENT;
1918 }
1919 }
1920
1921 key.objectid = sk->min_objectid;
1922 key.type = sk->min_type;
1923 key.offset = sk->min_offset;
1924
ac8e9819
CM
1925 path->keep_locks = 1;
1926
67871254 1927 while (1) {
6174d3cb 1928 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
ac8e9819
CM
1929 if (ret != 0) {
1930 if (ret > 0)
1931 ret = 0;
1932 goto err;
1933 }
1934 ret = copy_to_sk(root, path, &key, sk, args->buf,
1935 &sk_offset, &num_found);
b3b4aa74 1936 btrfs_release_path(path);
ac8e9819
CM
1937 if (ret || num_found >= sk->nr_items)
1938 break;
1939
1940 }
1941 ret = 0;
1942err:
1943 sk->nr_items = num_found;
1944 btrfs_free_path(path);
1945 return ret;
1946}
1947
1948static noinline int btrfs_ioctl_tree_search(struct file *file,
1949 void __user *argp)
1950{
1951 struct btrfs_ioctl_search_args *args;
1952 struct inode *inode;
1953 int ret;
1954
1955 if (!capable(CAP_SYS_ADMIN))
1956 return -EPERM;
1957
2354d08f
JL
1958 args = memdup_user(argp, sizeof(*args));
1959 if (IS_ERR(args))
1960 return PTR_ERR(args);
ac8e9819 1961
496ad9aa 1962 inode = file_inode(file);
ac8e9819
CM
1963 ret = search_ioctl(inode, args);
1964 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1965 ret = -EFAULT;
1966 kfree(args);
1967 return ret;
1968}
1969
98d377a0 1970/*
ac8e9819
CM
1971 * Search INODE_REFs to identify path name of 'dirid' directory
1972 * in a 'tree_id' tree. and sets path name to 'name'.
1973 */
98d377a0
TH
1974static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1975 u64 tree_id, u64 dirid, char *name)
1976{
1977 struct btrfs_root *root;
1978 struct btrfs_key key;
ac8e9819 1979 char *ptr;
98d377a0
TH
1980 int ret = -1;
1981 int slot;
1982 int len;
1983 int total_len = 0;
1984 struct btrfs_inode_ref *iref;
1985 struct extent_buffer *l;
1986 struct btrfs_path *path;
1987
1988 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1989 name[0]='\0';
1990 return 0;
1991 }
1992
1993 path = btrfs_alloc_path();
1994 if (!path)
1995 return -ENOMEM;
1996
ac8e9819 1997 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
98d377a0
TH
1998
1999 key.objectid = tree_id;
2000 key.type = BTRFS_ROOT_ITEM_KEY;
2001 key.offset = (u64)-1;
2002 root = btrfs_read_fs_root_no_name(info, &key);
2003 if (IS_ERR(root)) {
2004 printk(KERN_ERR "could not find root %llu\n", tree_id);
8ad6fcab
CM
2005 ret = -ENOENT;
2006 goto out;
98d377a0
TH
2007 }
2008
2009 key.objectid = dirid;
2010 key.type = BTRFS_INODE_REF_KEY;
8ad6fcab 2011 key.offset = (u64)-1;
98d377a0 2012
67871254 2013 while (1) {
98d377a0
TH
2014 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2015 if (ret < 0)
2016 goto out;
18674c6c
FDBM
2017 else if (ret > 0) {
2018 ret = btrfs_previous_item(root, path, dirid,
2019 BTRFS_INODE_REF_KEY);
2020 if (ret < 0)
2021 goto out;
2022 else if (ret > 0) {
2023 ret = -ENOENT;
2024 goto out;
2025 }
2026 }
98d377a0
TH
2027
2028 l = path->nodes[0];
2029 slot = path->slots[0];
2030 btrfs_item_key_to_cpu(l, &key, slot);
2031
98d377a0
TH
2032 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2033 len = btrfs_inode_ref_name_len(l, iref);
2034 ptr -= len + 1;
2035 total_len += len + 1;
a696cf35
FDBM
2036 if (ptr < name) {
2037 ret = -ENAMETOOLONG;
98d377a0 2038 goto out;
a696cf35 2039 }
98d377a0
TH
2040
2041 *(ptr + len) = '/';
67871254 2042 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
98d377a0
TH
2043
2044 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2045 break;
2046
b3b4aa74 2047 btrfs_release_path(path);
98d377a0 2048 key.objectid = key.offset;
8ad6fcab 2049 key.offset = (u64)-1;
98d377a0 2050 dirid = key.objectid;
98d377a0 2051 }
77906a50 2052 memmove(name, ptr, total_len);
67871254 2053 name[total_len] = '\0';
98d377a0
TH
2054 ret = 0;
2055out:
2056 btrfs_free_path(path);
ac8e9819
CM
2057 return ret;
2058}
2059
2060static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2061 void __user *argp)
2062{
2063 struct btrfs_ioctl_ino_lookup_args *args;
2064 struct inode *inode;
2065 int ret;
2066
2067 if (!capable(CAP_SYS_ADMIN))
2068 return -EPERM;
2069
2354d08f
JL
2070 args = memdup_user(argp, sizeof(*args));
2071 if (IS_ERR(args))
2072 return PTR_ERR(args);
c2b96929 2073
496ad9aa 2074 inode = file_inode(file);
ac8e9819 2075
1b53ac4d
CM
2076 if (args->treeid == 0)
2077 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2078
ac8e9819
CM
2079 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2080 args->treeid, args->objectid,
2081 args->name);
2082
2083 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2084 ret = -EFAULT;
2085
2086 kfree(args);
98d377a0
TH
2087 return ret;
2088}
2089
76dda93c
YZ
2090static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2091 void __user *arg)
2092{
54563d41 2093 struct dentry *parent = file->f_path.dentry;
76dda93c
YZ
2094 struct dentry *dentry;
2095 struct inode *dir = parent->d_inode;
2096 struct inode *inode;
2097 struct btrfs_root *root = BTRFS_I(dir)->root;
2098 struct btrfs_root *dest = NULL;
2099 struct btrfs_ioctl_vol_args *vol_args;
2100 struct btrfs_trans_handle *trans;
c58aaad2
MX
2101 struct btrfs_block_rsv block_rsv;
2102 u64 qgroup_reserved;
76dda93c
YZ
2103 int namelen;
2104 int ret;
2105 int err = 0;
2106
76dda93c
YZ
2107 vol_args = memdup_user(arg, sizeof(*vol_args));
2108 if (IS_ERR(vol_args))
2109 return PTR_ERR(vol_args);
2110
2111 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2112 namelen = strlen(vol_args->name);
2113 if (strchr(vol_args->name, '/') ||
2114 strncmp(vol_args->name, "..", namelen) == 0) {
2115 err = -EINVAL;
2116 goto out;
2117 }
2118
a561be71 2119 err = mnt_want_write_file(file);
76dda93c
YZ
2120 if (err)
2121 goto out;
2122
5c50c9b8
DS
2123 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2124 if (err == -EINTR)
e43f998e 2125 goto out_drop_write;
76dda93c
YZ
2126 dentry = lookup_one_len(vol_args->name, parent, namelen);
2127 if (IS_ERR(dentry)) {
2128 err = PTR_ERR(dentry);
2129 goto out_unlock_dir;
2130 }
2131
2132 if (!dentry->d_inode) {
2133 err = -ENOENT;
2134 goto out_dput;
2135 }
2136
2137 inode = dentry->d_inode;
4260f7c7 2138 dest = BTRFS_I(inode)->root;
67871254 2139 if (!capable(CAP_SYS_ADMIN)) {
4260f7c7
SW
2140 /*
2141 * Regular user. Only allow this with a special mount
2142 * option, when the user has write+exec access to the
2143 * subvol root, and when rmdir(2) would have been
2144 * allowed.
2145 *
2146 * Note that this is _not_ check that the subvol is
2147 * empty or doesn't contain data that we wouldn't
2148 * otherwise be able to delete.
2149 *
2150 * Users who want to delete empty subvols should try
2151 * rmdir(2).
2152 */
2153 err = -EPERM;
2154 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2155 goto out_dput;
2156
2157 /*
2158 * Do not allow deletion if the parent dir is the same
2159 * as the dir to be deleted. That means the ioctl
2160 * must be called on the dentry referencing the root
2161 * of the subvol, not a random directory contained
2162 * within it.
2163 */
2164 err = -EINVAL;
2165 if (root == dest)
2166 goto out_dput;
2167
2168 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2169 if (err)
2170 goto out_dput;
4260f7c7
SW
2171 }
2172
5c39da5b
MX
2173 /* check if subvolume may be deleted by a user */
2174 err = btrfs_may_delete(dir, dentry, 1);
2175 if (err)
2176 goto out_dput;
2177
33345d01 2178 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
76dda93c
YZ
2179 err = -EINVAL;
2180 goto out_dput;
2181 }
2182
76dda93c
YZ
2183 mutex_lock(&inode->i_mutex);
2184 err = d_invalidate(dentry);
2185 if (err)
2186 goto out_unlock;
2187
2188 down_write(&root->fs_info->subvol_sem);
2189
2190 err = may_destroy_subvol(dest);
2191 if (err)
2192 goto out_up_write;
2193
c58aaad2
MX
2194 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2195 /*
2196 * One for dir inode, two for dir entries, two for root
2197 * ref/backref.
2198 */
2199 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
ee3441b4 2200 5, &qgroup_reserved, true);
c58aaad2
MX
2201 if (err)
2202 goto out_up_write;
2203
a22285a6
YZ
2204 trans = btrfs_start_transaction(root, 0);
2205 if (IS_ERR(trans)) {
2206 err = PTR_ERR(trans);
c58aaad2 2207 goto out_release;
a22285a6 2208 }
c58aaad2
MX
2209 trans->block_rsv = &block_rsv;
2210 trans->bytes_reserved = block_rsv.size;
a22285a6 2211
76dda93c
YZ
2212 ret = btrfs_unlink_subvol(trans, root, dir,
2213 dest->root_key.objectid,
2214 dentry->d_name.name,
2215 dentry->d_name.len);
79787eaa
JM
2216 if (ret) {
2217 err = ret;
2218 btrfs_abort_transaction(trans, root, ret);
2219 goto out_end_trans;
2220 }
76dda93c
YZ
2221
2222 btrfs_record_root_in_trans(trans, dest);
2223
2224 memset(&dest->root_item.drop_progress, 0,
2225 sizeof(dest->root_item.drop_progress));
2226 dest->root_item.drop_level = 0;
2227 btrfs_set_root_refs(&dest->root_item, 0);
2228
d68fc57b
YZ
2229 if (!xchg(&dest->orphan_item_inserted, 1)) {
2230 ret = btrfs_insert_orphan_item(trans,
2231 root->fs_info->tree_root,
2232 dest->root_key.objectid);
79787eaa
JM
2233 if (ret) {
2234 btrfs_abort_transaction(trans, root, ret);
2235 err = ret;
2236 goto out_end_trans;
2237 }
d68fc57b 2238 }
dd5f9615
SB
2239
2240 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2241 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2242 dest->root_key.objectid);
2243 if (ret && ret != -ENOENT) {
2244 btrfs_abort_transaction(trans, root, ret);
2245 err = ret;
2246 goto out_end_trans;
2247 }
2248 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2249 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2250 dest->root_item.received_uuid,
2251 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2252 dest->root_key.objectid);
2253 if (ret && ret != -ENOENT) {
2254 btrfs_abort_transaction(trans, root, ret);
2255 err = ret;
2256 goto out_end_trans;
2257 }
2258 }
2259
79787eaa 2260out_end_trans:
c58aaad2
MX
2261 trans->block_rsv = NULL;
2262 trans->bytes_reserved = 0;
531cb13f 2263 ret = btrfs_end_transaction(trans, root);
79787eaa
JM
2264 if (ret && !err)
2265 err = ret;
76dda93c 2266 inode->i_flags |= S_DEAD;
c58aaad2
MX
2267out_release:
2268 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
76dda93c
YZ
2269out_up_write:
2270 up_write(&root->fs_info->subvol_sem);
2271out_unlock:
2272 mutex_unlock(&inode->i_mutex);
2273 if (!err) {
efefb143 2274 shrink_dcache_sb(root->fs_info->sb);
76dda93c
YZ
2275 btrfs_invalidate_inodes(dest);
2276 d_delete(dentry);
fa6ac876
LB
2277
2278 /* the last ref */
2279 if (dest->cache_inode) {
2280 iput(dest->cache_inode);
2281 dest->cache_inode = NULL;
2282 }
76dda93c
YZ
2283 }
2284out_dput:
2285 dput(dentry);
2286out_unlock_dir:
2287 mutex_unlock(&dir->i_mutex);
e43f998e 2288out_drop_write:
2a79f17e 2289 mnt_drop_write_file(file);
76dda93c
YZ
2290out:
2291 kfree(vol_args);
2292 return err;
2293}
2294
1e701a32 2295static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
f46b5a66 2296{
496ad9aa 2297 struct inode *inode = file_inode(file);
f46b5a66 2298 struct btrfs_root *root = BTRFS_I(inode)->root;
1e701a32 2299 struct btrfs_ioctl_defrag_range_args *range;
c146afad
YZ
2300 int ret;
2301
25122d15
ID
2302 ret = mnt_want_write_file(file);
2303 if (ret)
2304 return ret;
b83cc969 2305
25122d15
ID
2306 if (btrfs_root_readonly(root)) {
2307 ret = -EROFS;
2308 goto out;
5ac00add 2309 }
f46b5a66
CH
2310
2311 switch (inode->i_mode & S_IFMT) {
2312 case S_IFDIR:
e441d54d
CM
2313 if (!capable(CAP_SYS_ADMIN)) {
2314 ret = -EPERM;
2315 goto out;
2316 }
de78b51a 2317 ret = btrfs_defrag_root(root);
8929ecfa
YZ
2318 if (ret)
2319 goto out;
de78b51a 2320 ret = btrfs_defrag_root(root->fs_info->extent_root);
f46b5a66
CH
2321 break;
2322 case S_IFREG:
e441d54d
CM
2323 if (!(file->f_mode & FMODE_WRITE)) {
2324 ret = -EINVAL;
2325 goto out;
2326 }
1e701a32
CM
2327
2328 range = kzalloc(sizeof(*range), GFP_KERNEL);
2329 if (!range) {
2330 ret = -ENOMEM;
2331 goto out;
2332 }
2333
2334 if (argp) {
2335 if (copy_from_user(range, argp,
2336 sizeof(*range))) {
2337 ret = -EFAULT;
2338 kfree(range);
683be16e 2339 goto out;
1e701a32
CM
2340 }
2341 /* compression requires us to start the IO */
2342 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2343 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2344 range->extent_thresh = (u32)-1;
2345 }
2346 } else {
2347 /* the rest are all set to zero by kzalloc */
2348 range->len = (u64)-1;
2349 }
496ad9aa 2350 ret = btrfs_defrag_file(file_inode(file), file,
4cb5300b
CM
2351 range, 0, 0);
2352 if (ret > 0)
2353 ret = 0;
1e701a32 2354 kfree(range);
f46b5a66 2355 break;
8929ecfa
YZ
2356 default:
2357 ret = -EINVAL;
f46b5a66 2358 }
e441d54d 2359out:
25122d15 2360 mnt_drop_write_file(file);
e441d54d 2361 return ret;
f46b5a66
CH
2362}
2363
b2950863 2364static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
2365{
2366 struct btrfs_ioctl_vol_args *vol_args;
2367 int ret;
2368
e441d54d
CM
2369 if (!capable(CAP_SYS_ADMIN))
2370 return -EPERM;
2371
5ac00add
SB
2372 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2373 1)) {
e57138b3 2374 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
c9e9f97b
ID
2375 }
2376
5ac00add 2377 mutex_lock(&root->fs_info->volume_mutex);
dae7b665 2378 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2379 if (IS_ERR(vol_args)) {
2380 ret = PTR_ERR(vol_args);
2381 goto out;
2382 }
f46b5a66 2383
5516e595 2384 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
2385 ret = btrfs_init_new_device(root, vol_args->name);
2386
f46b5a66 2387 kfree(vol_args);
c9e9f97b
ID
2388out:
2389 mutex_unlock(&root->fs_info->volume_mutex);
5ac00add 2390 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
f46b5a66
CH
2391 return ret;
2392}
2393
da24927b 2394static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
f46b5a66 2395{
496ad9aa 2396 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
f46b5a66
CH
2397 struct btrfs_ioctl_vol_args *vol_args;
2398 int ret;
2399
e441d54d
CM
2400 if (!capable(CAP_SYS_ADMIN))
2401 return -EPERM;
2402
da24927b
MX
2403 ret = mnt_want_write_file(file);
2404 if (ret)
2405 return ret;
c146afad 2406
dae7b665 2407 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2408 if (IS_ERR(vol_args)) {
2409 ret = PTR_ERR(vol_args);
2410 goto out;
2411 }
f46b5a66 2412
5516e595 2413 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 2414
183860f6
AJ
2415 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2416 1)) {
2417 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2418 goto out;
2419 }
2420
2421 mutex_lock(&root->fs_info->volume_mutex);
2422 ret = btrfs_rm_device(root, vol_args->name);
c9e9f97b 2423 mutex_unlock(&root->fs_info->volume_mutex);
5ac00add 2424 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
183860f6
AJ
2425
2426out:
2427 kfree(vol_args);
4ac20c70 2428 mnt_drop_write_file(file);
f46b5a66
CH
2429 return ret;
2430}
2431
475f6387
JS
2432static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2433{
027ed2f0 2434 struct btrfs_ioctl_fs_info_args *fi_args;
475f6387
JS
2435 struct btrfs_device *device;
2436 struct btrfs_device *next;
2437 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
027ed2f0 2438 int ret = 0;
475f6387
JS
2439
2440 if (!capable(CAP_SYS_ADMIN))
2441 return -EPERM;
2442
027ed2f0
LZ
2443 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2444 if (!fi_args)
2445 return -ENOMEM;
2446
f7171750 2447 mutex_lock(&fs_devices->device_list_mutex);
027ed2f0
LZ
2448 fi_args->num_devices = fs_devices->num_devices;
2449 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
475f6387 2450
475f6387 2451 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
027ed2f0
LZ
2452 if (device->devid > fi_args->max_id)
2453 fi_args->max_id = device->devid;
475f6387
JS
2454 }
2455 mutex_unlock(&fs_devices->device_list_mutex);
2456
027ed2f0
LZ
2457 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2458 ret = -EFAULT;
475f6387 2459
027ed2f0
LZ
2460 kfree(fi_args);
2461 return ret;
475f6387
JS
2462}
2463
2464static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2465{
2466 struct btrfs_ioctl_dev_info_args *di_args;
2467 struct btrfs_device *dev;
2468 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2469 int ret = 0;
2470 char *s_uuid = NULL;
475f6387
JS
2471
2472 if (!capable(CAP_SYS_ADMIN))
2473 return -EPERM;
2474
2475 di_args = memdup_user(arg, sizeof(*di_args));
2476 if (IS_ERR(di_args))
2477 return PTR_ERR(di_args);
2478
dd5f9615 2479 if (!btrfs_is_empty_uuid(di_args->uuid))
475f6387
JS
2480 s_uuid = di_args->uuid;
2481
2482 mutex_lock(&fs_devices->device_list_mutex);
aa1b8cd4 2483 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
475f6387
JS
2484
2485 if (!dev) {
2486 ret = -ENODEV;
2487 goto out;
2488 }
2489
2490 di_args->devid = dev->devid;
2491 di_args->bytes_used = dev->bytes_used;
2492 di_args->total_bytes = dev->total_bytes;
2493 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
a27202fb 2494 if (dev->name) {
606686ee
JB
2495 struct rcu_string *name;
2496
2497 rcu_read_lock();
2498 name = rcu_dereference(dev->name);
2499 strncpy(di_args->path, name->str, sizeof(di_args->path));
2500 rcu_read_unlock();
a27202fb
JM
2501 di_args->path[sizeof(di_args->path) - 1] = 0;
2502 } else {
99ba55ad 2503 di_args->path[0] = '\0';
a27202fb 2504 }
475f6387
JS
2505
2506out:
55793c0d 2507 mutex_unlock(&fs_devices->device_list_mutex);
475f6387
JS
2508 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2509 ret = -EFAULT;
2510
2511 kfree(di_args);
2512 return ret;
2513}
2514
416161db
MF
2515static struct page *extent_same_get_page(struct inode *inode, u64 off)
2516{
2517 struct page *page;
2518 pgoff_t index;
2519 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2520
2521 index = off >> PAGE_CACHE_SHIFT;
2522
2523 page = grab_cache_page(inode->i_mapping, index);
2524 if (!page)
2525 return NULL;
2526
2527 if (!PageUptodate(page)) {
2528 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2529 0))
2530 return NULL;
2531 lock_page(page);
2532 if (!PageUptodate(page)) {
2533 unlock_page(page);
2534 page_cache_release(page);
2535 return NULL;
2536 }
2537 }
2538 unlock_page(page);
2539
2540 return page;
2541}
2542
77fe20dc
MF
2543static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2544{
2545 /* do any pending delalloc/csum calc on src, one way or
2546 another, and lock file content */
2547 while (1) {
2548 struct btrfs_ordered_extent *ordered;
2549 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2550 ordered = btrfs_lookup_first_ordered_extent(inode,
2551 off + len - 1);
2552 if (!ordered &&
2553 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2554 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2555 break;
2556 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2557 if (ordered)
2558 btrfs_put_ordered_extent(ordered);
2559 btrfs_wait_ordered_range(inode, off, len);
2560 }
2561}
2562
416161db
MF
2563static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2564 struct inode *inode2, u64 loff2, u64 len)
2565{
2566 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2567 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2568
2569 mutex_unlock(&inode1->i_mutex);
2570 mutex_unlock(&inode2->i_mutex);
2571}
2572
2573static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2574 struct inode *inode2, u64 loff2, u64 len)
2575{
2576 if (inode1 < inode2) {
2577 swap(inode1, inode2);
2578 swap(loff1, loff2);
2579 }
2580
2581 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2582 lock_extent_range(inode1, loff1, len);
2583 if (inode1 != inode2) {
2584 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2585 lock_extent_range(inode2, loff2, len);
2586 }
2587}
2588
2589static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2590 u64 dst_loff, u64 len)
2591{
2592 int ret = 0;
2593 struct page *src_page, *dst_page;
2594 unsigned int cmp_len = PAGE_CACHE_SIZE;
2595 void *addr, *dst_addr;
2596
2597 while (len) {
2598 if (len < PAGE_CACHE_SIZE)
2599 cmp_len = len;
2600
2601 src_page = extent_same_get_page(src, loff);
2602 if (!src_page)
2603 return -EINVAL;
2604 dst_page = extent_same_get_page(dst, dst_loff);
2605 if (!dst_page) {
2606 page_cache_release(src_page);
2607 return -EINVAL;
2608 }
2609 addr = kmap_atomic(src_page);
2610 dst_addr = kmap_atomic(dst_page);
2611
2612 flush_dcache_page(src_page);
2613 flush_dcache_page(dst_page);
2614
2615 if (memcmp(addr, dst_addr, cmp_len))
2616 ret = BTRFS_SAME_DATA_DIFFERS;
2617
2618 kunmap_atomic(addr);
2619 kunmap_atomic(dst_addr);
2620 page_cache_release(src_page);
2621 page_cache_release(dst_page);
2622
2623 if (ret)
2624 break;
2625
2626 loff += cmp_len;
2627 dst_loff += cmp_len;
2628 len -= cmp_len;
2629 }
2630
2631 return ret;
2632}
2633
2634static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2635{
2636 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2637
2638 if (off + len > inode->i_size || off + len < off)
2639 return -EINVAL;
2640 /* Check that we are block aligned - btrfs_clone() requires this */
2641 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2642 return -EINVAL;
2643
2644 return 0;
2645}
2646
2647static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2648 struct inode *dst, u64 dst_loff)
2649{
2650 int ret;
2651
2652 /*
2653 * btrfs_clone() can't handle extents in the same file
2654 * yet. Once that works, we can drop this check and replace it
2655 * with a check for the same inode, but overlapping extents.
2656 */
2657 if (src == dst)
2658 return -EINVAL;
2659
2660 btrfs_double_lock(src, loff, dst, dst_loff, len);
2661
2662 ret = extent_same_check_offsets(src, loff, len);
2663 if (ret)
2664 goto out_unlock;
2665
2666 ret = extent_same_check_offsets(dst, dst_loff, len);
2667 if (ret)
2668 goto out_unlock;
2669
2670 /* don't make the dst file partly checksummed */
2671 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2672 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2673 ret = -EINVAL;
2674 goto out_unlock;
2675 }
2676
2677 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2678 if (ret == 0)
2679 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2680
2681out_unlock:
2682 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2683
2684 return ret;
2685}
2686
2687#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2688
2689static long btrfs_ioctl_file_extent_same(struct file *file,
2690 void __user *argp)
2691{
cbf8b8ca
MF
2692 struct btrfs_ioctl_same_args tmp;
2693 struct btrfs_ioctl_same_args *same;
2694 struct btrfs_ioctl_same_extent_info *info;
416161db
MF
2695 struct inode *src = file->f_dentry->d_inode;
2696 struct file *dst_file = NULL;
2697 struct inode *dst;
2698 u64 off;
2699 u64 len;
2700 int i;
2701 int ret;
cbf8b8ca 2702 unsigned long size;
416161db
MF
2703 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2704 bool is_admin = capable(CAP_SYS_ADMIN);
2705
2706 if (!(file->f_mode & FMODE_READ))
2707 return -EINVAL;
2708
2709 ret = mnt_want_write_file(file);
2710 if (ret)
2711 return ret;
2712
cbf8b8ca 2713 if (copy_from_user(&tmp,
416161db 2714 (struct btrfs_ioctl_same_args __user *)argp,
cbf8b8ca 2715 sizeof(tmp))) {
416161db
MF
2716 ret = -EFAULT;
2717 goto out;
2718 }
2719
cbf8b8ca
MF
2720 size = sizeof(tmp) +
2721 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2722
229eed43 2723 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
cbf8b8ca 2724
229eed43
GB
2725 if (IS_ERR(same)) {
2726 ret = PTR_ERR(same);
cbf8b8ca
MF
2727 goto out;
2728 }
2729
2730 off = same->logical_offset;
2731 len = same->length;
416161db
MF
2732
2733 /*
2734 * Limit the total length we will dedupe for each operation.
2735 * This is intended to bound the total time spent in this
2736 * ioctl to something sane.
2737 */
2738 if (len > BTRFS_MAX_DEDUPE_LEN)
2739 len = BTRFS_MAX_DEDUPE_LEN;
2740
2741 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2742 /*
2743 * Btrfs does not support blocksize < page_size. As a
2744 * result, btrfs_cmp_data() won't correctly handle
2745 * this situation without an update.
2746 */
2747 ret = -EINVAL;
2748 goto out;
2749 }
2750
2751 ret = -EISDIR;
2752 if (S_ISDIR(src->i_mode))
2753 goto out;
2754
2755 ret = -EACCES;
2756 if (!S_ISREG(src->i_mode))
2757 goto out;
2758
cbf8b8ca
MF
2759 /* pre-format output fields to sane values */
2760 for (i = 0; i < same->dest_count; i++) {
2761 same->info[i].bytes_deduped = 0ULL;
2762 same->info[i].status = 0;
2763 }
416161db 2764
cbf8b8ca
MF
2765 ret = 0;
2766 for (i = 0; i < same->dest_count; i++) {
2767 info = &same->info[i];
416161db 2768
cbf8b8ca 2769 dst_file = fget(info->fd);
416161db 2770 if (!dst_file) {
cbf8b8ca 2771 info->status = -EBADF;
416161db
MF
2772 goto next;
2773 }
2774
2775 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
cbf8b8ca 2776 info->status = -EINVAL;
416161db
MF
2777 goto next;
2778 }
2779
cbf8b8ca 2780 info->status = -EXDEV;
416161db
MF
2781 if (file->f_path.mnt != dst_file->f_path.mnt)
2782 goto next;
2783
2784 dst = dst_file->f_dentry->d_inode;
2785 if (src->i_sb != dst->i_sb)
2786 goto next;
2787
2788 if (S_ISDIR(dst->i_mode)) {
cbf8b8ca 2789 info->status = -EISDIR;
416161db
MF
2790 goto next;
2791 }
2792
2793 if (!S_ISREG(dst->i_mode)) {
cbf8b8ca 2794 info->status = -EACCES;
416161db
MF
2795 goto next;
2796 }
2797
cbf8b8ca
MF
2798 info->status = btrfs_extent_same(src, off, len, dst,
2799 info->logical_offset);
2800 if (info->status == 0)
2801 info->bytes_deduped += len;
416161db
MF
2802
2803next:
2804 if (dst_file)
2805 fput(dst_file);
416161db
MF
2806 }
2807
cbf8b8ca
MF
2808 ret = copy_to_user(argp, same, size);
2809 if (ret)
2810 ret = -EFAULT;
2811
416161db
MF
2812out:
2813 mnt_drop_write_file(file);
2814 return ret;
2815}
2816
32b7c687
MF
2817/**
2818 * btrfs_clone() - clone a range from inode file to another
2819 *
2820 * @src: Inode to clone from
2821 * @inode: Inode to clone to
2822 * @off: Offset within source to start clone from
2823 * @olen: Original length, passed by user, of range to clone
2824 * @olen_aligned: Block-aligned value of olen, extent_same uses
2825 * identical values here
2826 * @destoff: Offset within @inode to start clone
2827 */
2828static int btrfs_clone(struct inode *src, struct inode *inode,
2829 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
f46b5a66 2830{
f46b5a66 2831 struct btrfs_root *root = BTRFS_I(inode)->root;
32b7c687 2832 struct btrfs_path *path = NULL;
f46b5a66 2833 struct extent_buffer *leaf;
32b7c687
MF
2834 struct btrfs_trans_handle *trans;
2835 char *buf = NULL;
ae01a0ab 2836 struct btrfs_key key;
f46b5a66
CH
2837 u32 nritems;
2838 int slot;
ae01a0ab 2839 int ret;
32b7c687 2840 u64 len = olen_aligned;
ae01a0ab
YZ
2841
2842 ret = -ENOMEM;
2843 buf = vmalloc(btrfs_level_size(root, 0));
2844 if (!buf)
32b7c687 2845 return ret;
ae01a0ab
YZ
2846
2847 path = btrfs_alloc_path();
2848 if (!path) {
2849 vfree(buf);
32b7c687 2850 return ret;
d525e8ab
LZ
2851 }
2852
32b7c687 2853 path->reada = 2;
c5c9cd4d 2854 /* clone data */
33345d01 2855 key.objectid = btrfs_ino(src);
ae01a0ab
YZ
2856 key.type = BTRFS_EXTENT_DATA_KEY;
2857 key.offset = 0;
f46b5a66
CH
2858
2859 while (1) {
2860 /*
2861 * note the key will change type as we walk through the
2862 * tree.
2863 */
362a20c5
DS
2864 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2865 0, 0);
f46b5a66
CH
2866 if (ret < 0)
2867 goto out;
2868
ae01a0ab
YZ
2869 nritems = btrfs_header_nritems(path->nodes[0]);
2870 if (path->slots[0] >= nritems) {
362a20c5 2871 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
f46b5a66
CH
2872 if (ret < 0)
2873 goto out;
2874 if (ret > 0)
2875 break;
ae01a0ab 2876 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
2877 }
2878 leaf = path->nodes[0];
2879 slot = path->slots[0];
f46b5a66 2880
ae01a0ab 2881 btrfs_item_key_to_cpu(leaf, &key, slot);
d20f7043 2882 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
33345d01 2883 key.objectid != btrfs_ino(src))
f46b5a66
CH
2884 break;
2885
c5c9cd4d
SW
2886 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2887 struct btrfs_file_extent_item *extent;
2888 int type;
31840ae1
ZY
2889 u32 size;
2890 struct btrfs_key new_key;
c5c9cd4d
SW
2891 u64 disko = 0, diskl = 0;
2892 u64 datao = 0, datal = 0;
2893 u8 comp;
b5384d48 2894 u64 endoff;
31840ae1
ZY
2895
2896 size = btrfs_item_size_nr(leaf, slot);
2897 read_extent_buffer(leaf, buf,
2898 btrfs_item_ptr_offset(leaf, slot),
2899 size);
c5c9cd4d
SW
2900
2901 extent = btrfs_item_ptr(leaf, slot,
2902 struct btrfs_file_extent_item);
2903 comp = btrfs_file_extent_compression(leaf, extent);
2904 type = btrfs_file_extent_type(leaf, extent);
c8a894d7
CM
2905 if (type == BTRFS_FILE_EXTENT_REG ||
2906 type == BTRFS_FILE_EXTENT_PREALLOC) {
d397712b
CM
2907 disko = btrfs_file_extent_disk_bytenr(leaf,
2908 extent);
2909 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2910 extent);
c5c9cd4d 2911 datao = btrfs_file_extent_offset(leaf, extent);
d397712b
CM
2912 datal = btrfs_file_extent_num_bytes(leaf,
2913 extent);
c5c9cd4d
SW
2914 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2915 /* take upper bound, may be compressed */
2916 datal = btrfs_file_extent_ram_bytes(leaf,
2917 extent);
2918 }
b3b4aa74 2919 btrfs_release_path(path);
31840ae1 2920
050006a7 2921 if (key.offset + datal <= off ||
aa42ffd9 2922 key.offset >= off + len - 1)
c5c9cd4d
SW
2923 goto next;
2924
31840ae1 2925 memcpy(&new_key, &key, sizeof(new_key));
33345d01 2926 new_key.objectid = btrfs_ino(inode);
4d728ec7
LZ
2927 if (off <= key.offset)
2928 new_key.offset = key.offset + destoff - off;
2929 else
2930 new_key.offset = destoff;
31840ae1 2931
b6f3409b
SW
2932 /*
2933 * 1 - adjusting old extent (we may have to split it)
2934 * 1 - add new extent
2935 * 1 - inode update
2936 */
2937 trans = btrfs_start_transaction(root, 3);
a22285a6
YZ
2938 if (IS_ERR(trans)) {
2939 ret = PTR_ERR(trans);
2940 goto out;
2941 }
2942
c8a894d7
CM
2943 if (type == BTRFS_FILE_EXTENT_REG ||
2944 type == BTRFS_FILE_EXTENT_PREALLOC) {
d72c0842
LZ
2945 /*
2946 * a | --- range to clone ---| b
2947 * | ------------- extent ------------- |
2948 */
2949
2950 /* substract range b */
2951 if (key.offset + datal > off + len)
2952 datal = off + len - key.offset;
2953
2954 /* substract range a */
a22285a6
YZ
2955 if (off > key.offset) {
2956 datao += off - key.offset;
2957 datal -= off - key.offset;
2958 }
2959
5dc562c5 2960 ret = btrfs_drop_extents(trans, root, inode,
a22285a6
YZ
2961 new_key.offset,
2962 new_key.offset + datal,
2671485d 2963 1);
79787eaa
JM
2964 if (ret) {
2965 btrfs_abort_transaction(trans, root,
2966 ret);
2967 btrfs_end_transaction(trans, root);
2968 goto out;
2969 }
a22285a6 2970
c5c9cd4d
SW
2971 ret = btrfs_insert_empty_item(trans, root, path,
2972 &new_key, size);
79787eaa
JM
2973 if (ret) {
2974 btrfs_abort_transaction(trans, root,
2975 ret);
2976 btrfs_end_transaction(trans, root);
2977 goto out;
2978 }
c5c9cd4d
SW
2979
2980 leaf = path->nodes[0];
2981 slot = path->slots[0];
2982 write_extent_buffer(leaf, buf,
31840ae1
ZY
2983 btrfs_item_ptr_offset(leaf, slot),
2984 size);
ae01a0ab 2985
c5c9cd4d 2986 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 2987 struct btrfs_file_extent_item);
c5c9cd4d 2988
c5c9cd4d
SW
2989 /* disko == 0 means it's a hole */
2990 if (!disko)
2991 datao = 0;
c5c9cd4d
SW
2992
2993 btrfs_set_file_extent_offset(leaf, extent,
2994 datao);
2995 btrfs_set_file_extent_num_bytes(leaf, extent,
2996 datal);
2997 if (disko) {
2998 inode_add_bytes(inode, datal);
ae01a0ab 2999 ret = btrfs_inc_extent_ref(trans, root,
5d4f98a2
YZ
3000 disko, diskl, 0,
3001 root->root_key.objectid,
33345d01 3002 btrfs_ino(inode),
66d7e7f0
AJ
3003 new_key.offset - datao,
3004 0);
79787eaa
JM
3005 if (ret) {
3006 btrfs_abort_transaction(trans,
3007 root,
3008 ret);
3009 btrfs_end_transaction(trans,
3010 root);
3011 goto out;
3012
3013 }
f46b5a66 3014 }
c5c9cd4d
SW
3015 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3016 u64 skip = 0;
3017 u64 trim = 0;
3018 if (off > key.offset) {
3019 skip = off - key.offset;
3020 new_key.offset += skip;
3021 }
d397712b 3022
aa42ffd9
LB
3023 if (key.offset + datal > off + len)
3024 trim = key.offset + datal - (off + len);
d397712b 3025
c5c9cd4d 3026 if (comp && (skip || trim)) {
c5c9cd4d 3027 ret = -EINVAL;
a22285a6 3028 btrfs_end_transaction(trans, root);
c5c9cd4d
SW
3029 goto out;
3030 }
3031 size -= skip + trim;
3032 datal -= skip + trim;
a22285a6 3033
5dc562c5 3034 ret = btrfs_drop_extents(trans, root, inode,
a22285a6
YZ
3035 new_key.offset,
3036 new_key.offset + datal,
2671485d 3037 1);
79787eaa
JM
3038 if (ret) {
3039 btrfs_abort_transaction(trans, root,
3040 ret);
3041 btrfs_end_transaction(trans, root);
3042 goto out;
3043 }
a22285a6 3044
c5c9cd4d
SW
3045 ret = btrfs_insert_empty_item(trans, root, path,
3046 &new_key, size);
79787eaa
JM
3047 if (ret) {
3048 btrfs_abort_transaction(trans, root,
3049 ret);
3050 btrfs_end_transaction(trans, root);
3051 goto out;
3052 }
c5c9cd4d
SW
3053
3054 if (skip) {
d397712b
CM
3055 u32 start =
3056 btrfs_file_extent_calc_inline_size(0);
c5c9cd4d
SW
3057 memmove(buf+start, buf+start+skip,
3058 datal);
3059 }
3060
3061 leaf = path->nodes[0];
3062 slot = path->slots[0];
3063 write_extent_buffer(leaf, buf,
3064 btrfs_item_ptr_offset(leaf, slot),
3065 size);
3066 inode_add_bytes(inode, datal);
f46b5a66 3067 }
c5c9cd4d
SW
3068
3069 btrfs_mark_buffer_dirty(leaf);
b3b4aa74 3070 btrfs_release_path(path);
c5c9cd4d 3071
0c4d2d95 3072 inode_inc_iversion(inode);
a22285a6 3073 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
b5384d48
SW
3074
3075 /*
3076 * we round up to the block size at eof when
3077 * determining which extents to clone above,
3078 * but shouldn't round up the file size
3079 */
3080 endoff = new_key.offset + datal;
5f3888ff
LZ
3081 if (endoff > destoff+olen)
3082 endoff = destoff+olen;
b5384d48
SW
3083 if (endoff > inode->i_size)
3084 btrfs_i_size_write(inode, endoff);
3085
a22285a6 3086 ret = btrfs_update_inode(trans, root, inode);
79787eaa
JM
3087 if (ret) {
3088 btrfs_abort_transaction(trans, root, ret);
3089 btrfs_end_transaction(trans, root);
3090 goto out;
3091 }
3092 ret = btrfs_end_transaction(trans, root);
a22285a6 3093 }
d397712b 3094next:
b3b4aa74 3095 btrfs_release_path(path);
f46b5a66 3096 key.offset++;
f46b5a66 3097 }
f46b5a66 3098 ret = 0;
32b7c687 3099
f46b5a66 3100out:
b3b4aa74 3101 btrfs_release_path(path);
32b7c687
MF
3102 btrfs_free_path(path);
3103 vfree(buf);
3104 return ret;
3105}
3106
3107static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3108 u64 off, u64 olen, u64 destoff)
3109{
54563d41 3110 struct inode *inode = file_inode(file);
32b7c687
MF
3111 struct btrfs_root *root = BTRFS_I(inode)->root;
3112 struct fd src_file;
3113 struct inode *src;
3114 int ret;
3115 u64 len = olen;
3116 u64 bs = root->fs_info->sb->s_blocksize;
3117 int same_inode = 0;
3118
3119 /*
3120 * TODO:
3121 * - split compressed inline extents. annoying: we need to
3122 * decompress into destination's address_space (the file offset
3123 * may change, so source mapping won't do), then recompress (or
3124 * otherwise reinsert) a subrange.
3125 * - allow ranges within the same file to be cloned (provided
3126 * they don't overlap)?
3127 */
3128
3129 /* the destination must be opened for writing */
3130 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3131 return -EINVAL;
3132
3133 if (btrfs_root_readonly(root))
3134 return -EROFS;
3135
3136 ret = mnt_want_write_file(file);
3137 if (ret)
3138 return ret;
3139
3140 src_file = fdget(srcfd);
3141 if (!src_file.file) {
3142 ret = -EBADF;
3143 goto out_drop_write;
3144 }
3145
3146 ret = -EXDEV;
3147 if (src_file.file->f_path.mnt != file->f_path.mnt)
3148 goto out_fput;
3149
3150 src = file_inode(src_file.file);
3151
3152 ret = -EINVAL;
3153 if (src == inode)
3154 same_inode = 1;
3155
3156 /* the src must be open for reading */
3157 if (!(src_file.file->f_mode & FMODE_READ))
3158 goto out_fput;
3159
3160 /* don't make the dst file partly checksummed */
3161 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3162 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3163 goto out_fput;
3164
3165 ret = -EISDIR;
3166 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3167 goto out_fput;
3168
3169 ret = -EXDEV;
3170 if (src->i_sb != inode->i_sb)
3171 goto out_fput;
3172
3173 if (!same_inode) {
3174 if (inode < src) {
3175 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3176 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3177 } else {
3178 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3179 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3180 }
3181 } else {
3182 mutex_lock(&src->i_mutex);
3183 }
3184
3185 /* determine range to clone */
3186 ret = -EINVAL;
3187 if (off + len > src->i_size || off + len < off)
3188 goto out_unlock;
3189 if (len == 0)
3190 olen = len = src->i_size - off;
3191 /* if we extend to eof, continue to block boundary */
3192 if (off + len == src->i_size)
3193 len = ALIGN(src->i_size, bs) - off;
3194
3195 /* verify the end result is block aligned */
3196 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3197 !IS_ALIGNED(destoff, bs))
3198 goto out_unlock;
3199
3200 /* verify if ranges are overlapped within the same file */
3201 if (same_inode) {
3202 if (destoff + len > off && destoff < off + len)
3203 goto out_unlock;
3204 }
3205
3206 if (destoff > inode->i_size) {
3207 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3208 if (ret)
3209 goto out_unlock;
3210 }
3211
3212 /* truncate page cache pages from target inode range */
3213 truncate_inode_pages_range(&inode->i_data, destoff,
3214 PAGE_CACHE_ALIGN(destoff + len) - 1);
3215
3216 lock_extent_range(src, off, len);
3217
3218 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3219
aa42ffd9 3220 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
f46b5a66
CH
3221out_unlock:
3222 mutex_unlock(&src->i_mutex);
a96fbc72
LB
3223 if (!same_inode)
3224 mutex_unlock(&inode->i_mutex);
f46b5a66 3225out_fput:
2903ff01 3226 fdput(src_file);
ab67b7c1 3227out_drop_write:
2a79f17e 3228 mnt_drop_write_file(file);
f46b5a66
CH
3229 return ret;
3230}
3231
7a865e8a 3232static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
c5c9cd4d
SW
3233{
3234 struct btrfs_ioctl_clone_range_args args;
3235
7a865e8a 3236 if (copy_from_user(&args, argp, sizeof(args)))
c5c9cd4d
SW
3237 return -EFAULT;
3238 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3239 args.src_length, args.dest_offset);
3240}
3241
f46b5a66
CH
3242/*
3243 * there are many ways the trans_start and trans_end ioctls can lead
3244 * to deadlocks. They should only be used by applications that
3245 * basically own the machine, and have a very in depth understanding
3246 * of all the possible deadlocks and enospc problems.
3247 */
b2950863 3248static long btrfs_ioctl_trans_start(struct file *file)
f46b5a66 3249{
496ad9aa 3250 struct inode *inode = file_inode(file);
f46b5a66
CH
3251 struct btrfs_root *root = BTRFS_I(inode)->root;
3252 struct btrfs_trans_handle *trans;
1ab86aed 3253 int ret;
f46b5a66 3254
1ab86aed 3255 ret = -EPERM;
df5b5520 3256 if (!capable(CAP_SYS_ADMIN))
1ab86aed 3257 goto out;
df5b5520 3258
1ab86aed
SW
3259 ret = -EINPROGRESS;
3260 if (file->private_data)
f46b5a66 3261 goto out;
9ca9ee09 3262
b83cc969
LZ
3263 ret = -EROFS;
3264 if (btrfs_root_readonly(root))
3265 goto out;
3266
a561be71 3267 ret = mnt_want_write_file(file);
c146afad
YZ
3268 if (ret)
3269 goto out;
3270
a4abeea4 3271 atomic_inc(&root->fs_info->open_ioctl_trans);
9ca9ee09 3272
1ab86aed 3273 ret = -ENOMEM;
7a7eaa40 3274 trans = btrfs_start_ioctl_transaction(root);
abd30bb0 3275 if (IS_ERR(trans))
1ab86aed
SW
3276 goto out_drop;
3277
3278 file->private_data = trans;
3279 return 0;
3280
3281out_drop:
a4abeea4 3282 atomic_dec(&root->fs_info->open_ioctl_trans);
2a79f17e 3283 mnt_drop_write_file(file);
f46b5a66 3284out:
f46b5a66
CH
3285 return ret;
3286}
3287
6ef5ed0d
JB
3288static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3289{
496ad9aa 3290 struct inode *inode = file_inode(file);
6ef5ed0d
JB
3291 struct btrfs_root *root = BTRFS_I(inode)->root;
3292 struct btrfs_root *new_root;
3293 struct btrfs_dir_item *di;
3294 struct btrfs_trans_handle *trans;
3295 struct btrfs_path *path;
3296 struct btrfs_key location;
3297 struct btrfs_disk_key disk_key;
6ef5ed0d
JB
3298 u64 objectid = 0;
3299 u64 dir_id;
3c04ce01 3300 int ret;
6ef5ed0d
JB
3301
3302 if (!capable(CAP_SYS_ADMIN))
3303 return -EPERM;
3304
3c04ce01
MX
3305 ret = mnt_want_write_file(file);
3306 if (ret)
3307 return ret;
3308
3309 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3310 ret = -EFAULT;
3311 goto out;
3312 }
6ef5ed0d
JB
3313
3314 if (!objectid)
1cecf579 3315 objectid = BTRFS_FS_TREE_OBJECTID;
6ef5ed0d
JB
3316
3317 location.objectid = objectid;
3318 location.type = BTRFS_ROOT_ITEM_KEY;
3319 location.offset = (u64)-1;
3320
3321 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
3c04ce01
MX
3322 if (IS_ERR(new_root)) {
3323 ret = PTR_ERR(new_root);
3324 goto out;
3325 }
6ef5ed0d 3326
6ef5ed0d 3327 path = btrfs_alloc_path();
3c04ce01
MX
3328 if (!path) {
3329 ret = -ENOMEM;
3330 goto out;
3331 }
6ef5ed0d
JB
3332 path->leave_spinning = 1;
3333
3334 trans = btrfs_start_transaction(root, 1);
98d5dc13 3335 if (IS_ERR(trans)) {
6ef5ed0d 3336 btrfs_free_path(path);
3c04ce01
MX
3337 ret = PTR_ERR(trans);
3338 goto out;
6ef5ed0d
JB
3339 }
3340
6c41761f 3341 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
6ef5ed0d
JB
3342 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3343 dir_id, "default", 7, 1);
cf1e99a4 3344 if (IS_ERR_OR_NULL(di)) {
6ef5ed0d
JB
3345 btrfs_free_path(path);
3346 btrfs_end_transaction(trans, root);
3347 printk(KERN_ERR "Umm, you don't have the default dir item, "
3348 "this isn't going to work\n");
3c04ce01
MX
3349 ret = -ENOENT;
3350 goto out;
6ef5ed0d
JB
3351 }
3352
3353 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3354 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3355 btrfs_mark_buffer_dirty(path->nodes[0]);
3356 btrfs_free_path(path);
3357
2b0ce2c2 3358 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
6ef5ed0d 3359 btrfs_end_transaction(trans, root);
3c04ce01
MX
3360out:
3361 mnt_drop_write_file(file);
3362 return ret;
6ef5ed0d
JB
3363}
3364
5af3e8cc
SB
3365void btrfs_get_block_group_info(struct list_head *groups_list,
3366 struct btrfs_ioctl_space_info *space)
bf5fc093
JB
3367{
3368 struct btrfs_block_group_cache *block_group;
3369
3370 space->total_bytes = 0;
3371 space->used_bytes = 0;
3372 space->flags = 0;
3373 list_for_each_entry(block_group, groups_list, list) {
3374 space->flags = block_group->flags;
3375 space->total_bytes += block_group->key.offset;
3376 space->used_bytes +=
3377 btrfs_block_group_used(&block_group->item);
3378 }
3379}
3380
48a3b636 3381static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1406e432
JB
3382{
3383 struct btrfs_ioctl_space_args space_args;
3384 struct btrfs_ioctl_space_info space;
3385 struct btrfs_ioctl_space_info *dest;
7fde62bf 3386 struct btrfs_ioctl_space_info *dest_orig;
13f2696f 3387 struct btrfs_ioctl_space_info __user *user_dest;
1406e432 3388 struct btrfs_space_info *info;
bf5fc093
JB
3389 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3390 BTRFS_BLOCK_GROUP_SYSTEM,
3391 BTRFS_BLOCK_GROUP_METADATA,
3392 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3393 int num_types = 4;
7fde62bf 3394 int alloc_size;
1406e432 3395 int ret = 0;
51788b1b 3396 u64 slot_count = 0;
bf5fc093 3397 int i, c;
1406e432
JB
3398
3399 if (copy_from_user(&space_args,
3400 (struct btrfs_ioctl_space_args __user *)arg,
3401 sizeof(space_args)))
3402 return -EFAULT;
3403
bf5fc093
JB
3404 for (i = 0; i < num_types; i++) {
3405 struct btrfs_space_info *tmp;
3406
3407 info = NULL;
3408 rcu_read_lock();
3409 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3410 list) {
3411 if (tmp->flags == types[i]) {
3412 info = tmp;
3413 break;
3414 }
3415 }
3416 rcu_read_unlock();
3417
3418 if (!info)
3419 continue;
3420
3421 down_read(&info->groups_sem);
3422 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3423 if (!list_empty(&info->block_groups[c]))
3424 slot_count++;
3425 }
3426 up_read(&info->groups_sem);
3427 }
7fde62bf
CM
3428
3429 /* space_slots == 0 means they are asking for a count */
3430 if (space_args.space_slots == 0) {
3431 space_args.total_spaces = slot_count;
3432 goto out;
3433 }
bf5fc093 3434
51788b1b 3435 slot_count = min_t(u64, space_args.space_slots, slot_count);
bf5fc093 3436
7fde62bf 3437 alloc_size = sizeof(*dest) * slot_count;
bf5fc093 3438
7fde62bf
CM
3439 /* we generally have at most 6 or so space infos, one for each raid
3440 * level. So, a whole page should be more than enough for everyone
3441 */
3442 if (alloc_size > PAGE_CACHE_SIZE)
3443 return -ENOMEM;
3444
1406e432 3445 space_args.total_spaces = 0;
7fde62bf
CM
3446 dest = kmalloc(alloc_size, GFP_NOFS);
3447 if (!dest)
3448 return -ENOMEM;
3449 dest_orig = dest;
1406e432 3450
7fde62bf 3451 /* now we have a buffer to copy into */
bf5fc093
JB
3452 for (i = 0; i < num_types; i++) {
3453 struct btrfs_space_info *tmp;
3454
51788b1b
DR
3455 if (!slot_count)
3456 break;
3457
bf5fc093
JB
3458 info = NULL;
3459 rcu_read_lock();
3460 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3461 list) {
3462 if (tmp->flags == types[i]) {
3463 info = tmp;
3464 break;
3465 }
3466 }
3467 rcu_read_unlock();
7fde62bf 3468
bf5fc093
JB
3469 if (!info)
3470 continue;
3471 down_read(&info->groups_sem);
3472 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3473 if (!list_empty(&info->block_groups[c])) {
5af3e8cc
SB
3474 btrfs_get_block_group_info(
3475 &info->block_groups[c], &space);
bf5fc093
JB
3476 memcpy(dest, &space, sizeof(space));
3477 dest++;
3478 space_args.total_spaces++;
51788b1b 3479 slot_count--;
bf5fc093 3480 }
51788b1b
DR
3481 if (!slot_count)
3482 break;
bf5fc093
JB
3483 }
3484 up_read(&info->groups_sem);
1406e432 3485 }
1406e432 3486
2eec6c81 3487 user_dest = (struct btrfs_ioctl_space_info __user *)
7fde62bf
CM
3488 (arg + sizeof(struct btrfs_ioctl_space_args));
3489
3490 if (copy_to_user(user_dest, dest_orig, alloc_size))
3491 ret = -EFAULT;
3492
3493 kfree(dest_orig);
3494out:
3495 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1406e432
JB
3496 ret = -EFAULT;
3497
3498 return ret;
3499}
3500
01e219e8
JM
3501static long btrfs_ioctl_global_rsv(struct btrfs_root *root, void __user *arg)
3502{
3503 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
3504 u64 reserved;
3505
3506 spin_lock(&block_rsv->lock);
3507 reserved = block_rsv->reserved;
3508 spin_unlock(&block_rsv->lock);
3509
3510 if (arg && copy_to_user(arg, &reserved, sizeof(reserved)))
3511 return -EFAULT;
3512 return 0;
3513}
3514
f46b5a66
CH
3515/*
3516 * there are many ways the trans_start and trans_end ioctls can lead
3517 * to deadlocks. They should only be used by applications that
3518 * basically own the machine, and have a very in depth understanding
3519 * of all the possible deadlocks and enospc problems.
3520 */
3521long btrfs_ioctl_trans_end(struct file *file)
3522{
496ad9aa 3523 struct inode *inode = file_inode(file);
f46b5a66
CH
3524 struct btrfs_root *root = BTRFS_I(inode)->root;
3525 struct btrfs_trans_handle *trans;
f46b5a66 3526
f46b5a66 3527 trans = file->private_data;
1ab86aed
SW
3528 if (!trans)
3529 return -EINVAL;
b214107e 3530 file->private_data = NULL;
9ca9ee09 3531
1ab86aed
SW
3532 btrfs_end_transaction(trans, root);
3533
a4abeea4 3534 atomic_dec(&root->fs_info->open_ioctl_trans);
9ca9ee09 3535
2a79f17e 3536 mnt_drop_write_file(file);
1ab86aed 3537 return 0;
f46b5a66
CH
3538}
3539
9a8c28be
MX
3540static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3541 void __user *argp)
46204592 3542{
46204592
SW
3543 struct btrfs_trans_handle *trans;
3544 u64 transid;
db5b493a 3545 int ret;
46204592 3546
d4edf39b 3547 trans = btrfs_attach_transaction_barrier(root);
ff7c1d33
MX
3548 if (IS_ERR(trans)) {
3549 if (PTR_ERR(trans) != -ENOENT)
3550 return PTR_ERR(trans);
3551
3552 /* No running transaction, don't bother */
3553 transid = root->fs_info->last_trans_committed;
3554 goto out;
3555 }
46204592 3556 transid = trans->transid;
db5b493a 3557 ret = btrfs_commit_transaction_async(trans, root, 0);
8b2b2d3c
TI
3558 if (ret) {
3559 btrfs_end_transaction(trans, root);
db5b493a 3560 return ret;
8b2b2d3c 3561 }
ff7c1d33 3562out:
46204592
SW
3563 if (argp)
3564 if (copy_to_user(argp, &transid, sizeof(transid)))
3565 return -EFAULT;
3566 return 0;
3567}
3568
9a8c28be
MX
3569static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3570 void __user *argp)
46204592 3571{
46204592
SW
3572 u64 transid;
3573
3574 if (argp) {
3575 if (copy_from_user(&transid, argp, sizeof(transid)))
3576 return -EFAULT;
3577 } else {
3578 transid = 0; /* current trans */
3579 }
3580 return btrfs_wait_for_commit(root, transid);
3581}
3582
b8e95489 3583static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
475f6387 3584{
496ad9aa 3585 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
475f6387 3586 struct btrfs_ioctl_scrub_args *sa;
b8e95489 3587 int ret;
475f6387
JS
3588
3589 if (!capable(CAP_SYS_ADMIN))
3590 return -EPERM;
3591
3592 sa = memdup_user(arg, sizeof(*sa));
3593 if (IS_ERR(sa))
3594 return PTR_ERR(sa);
3595
b8e95489
MX
3596 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3597 ret = mnt_want_write_file(file);
3598 if (ret)
3599 goto out;
3600 }
3601
aa1b8cd4 3602 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
63a212ab
SB
3603 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3604 0);
475f6387
JS
3605
3606 if (copy_to_user(arg, sa, sizeof(*sa)))
3607 ret = -EFAULT;
3608
b8e95489
MX
3609 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3610 mnt_drop_write_file(file);
3611out:
475f6387
JS
3612 kfree(sa);
3613 return ret;
3614}
3615
3616static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3617{
3618 if (!capable(CAP_SYS_ADMIN))
3619 return -EPERM;
3620
aa1b8cd4 3621 return btrfs_scrub_cancel(root->fs_info);
475f6387
JS
3622}
3623
3624static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3625 void __user *arg)
3626{
3627 struct btrfs_ioctl_scrub_args *sa;
3628 int ret;
3629
3630 if (!capable(CAP_SYS_ADMIN))
3631 return -EPERM;
3632
3633 sa = memdup_user(arg, sizeof(*sa));
3634 if (IS_ERR(sa))
3635 return PTR_ERR(sa);
3636
3637 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3638
3639 if (copy_to_user(arg, sa, sizeof(*sa)))
3640 ret = -EFAULT;
3641
3642 kfree(sa);
3643 return ret;
3644}
3645
c11d2c23 3646static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
b27f7c0c 3647 void __user *arg)
c11d2c23
SB
3648{
3649 struct btrfs_ioctl_get_dev_stats *sa;
3650 int ret;
3651
c11d2c23
SB
3652 sa = memdup_user(arg, sizeof(*sa));
3653 if (IS_ERR(sa))
3654 return PTR_ERR(sa);
3655
b27f7c0c
DS
3656 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3657 kfree(sa);
3658 return -EPERM;
3659 }
3660
3661 ret = btrfs_get_dev_stats(root, sa);
c11d2c23
SB
3662
3663 if (copy_to_user(arg, sa, sizeof(*sa)))
3664 ret = -EFAULT;
3665
3666 kfree(sa);
3667 return ret;
3668}
3669
3f6bcfbd
SB
3670static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3671{
3672 struct btrfs_ioctl_dev_replace_args *p;
3673 int ret;
3674
3675 if (!capable(CAP_SYS_ADMIN))
3676 return -EPERM;
3677
3678 p = memdup_user(arg, sizeof(*p));
3679 if (IS_ERR(p))
3680 return PTR_ERR(p);
3681
3682 switch (p->cmd) {
3683 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
adfa97cb
ID
3684 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3685 ret = -EROFS;
3686 goto out;
3687 }
3f6bcfbd
SB
3688 if (atomic_xchg(
3689 &root->fs_info->mutually_exclusive_operation_running,
3690 1)) {
e57138b3 3691 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3f6bcfbd
SB
3692 } else {
3693 ret = btrfs_dev_replace_start(root, p);
3694 atomic_set(
3695 &root->fs_info->mutually_exclusive_operation_running,
3696 0);
3697 }
3698 break;
3699 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3700 btrfs_dev_replace_status(root->fs_info, p);
3701 ret = 0;
3702 break;
3703 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3704 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3705 break;
3706 default:
3707 ret = -EINVAL;
3708 break;
3709 }
3710
3711 if (copy_to_user(arg, p, sizeof(*p)))
3712 ret = -EFAULT;
adfa97cb 3713out:
3f6bcfbd
SB
3714 kfree(p);
3715 return ret;
3716}
3717
d7728c96
JS
3718static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3719{
3720 int ret = 0;
3721 int i;
740c3d22 3722 u64 rel_ptr;
d7728c96 3723 int size;
806468f8 3724 struct btrfs_ioctl_ino_path_args *ipa = NULL;
d7728c96
JS
3725 struct inode_fs_paths *ipath = NULL;
3726 struct btrfs_path *path;
3727
82b22ac8 3728 if (!capable(CAP_DAC_READ_SEARCH))
d7728c96
JS
3729 return -EPERM;
3730
3731 path = btrfs_alloc_path();
3732 if (!path) {
3733 ret = -ENOMEM;
3734 goto out;
3735 }
3736
3737 ipa = memdup_user(arg, sizeof(*ipa));
3738 if (IS_ERR(ipa)) {
3739 ret = PTR_ERR(ipa);
3740 ipa = NULL;
3741 goto out;
3742 }
3743
3744 size = min_t(u32, ipa->size, 4096);
3745 ipath = init_ipath(size, root, path);
3746 if (IS_ERR(ipath)) {
3747 ret = PTR_ERR(ipath);
3748 ipath = NULL;
3749 goto out;
3750 }
3751
3752 ret = paths_from_inode(ipa->inum, ipath);
3753 if (ret < 0)
3754 goto out;
3755
3756 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
745c4d8e
JM
3757 rel_ptr = ipath->fspath->val[i] -
3758 (u64)(unsigned long)ipath->fspath->val;
740c3d22 3759 ipath->fspath->val[i] = rel_ptr;
d7728c96
JS
3760 }
3761
745c4d8e
JM
3762 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3763 (void *)(unsigned long)ipath->fspath, size);
d7728c96
JS
3764 if (ret) {
3765 ret = -EFAULT;
3766 goto out;
3767 }
3768
3769out:
3770 btrfs_free_path(path);
3771 free_ipath(ipath);
3772 kfree(ipa);
3773
3774 return ret;
3775}
3776
3777static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3778{
3779 struct btrfs_data_container *inodes = ctx;
3780 const size_t c = 3 * sizeof(u64);
3781
3782 if (inodes->bytes_left >= c) {
3783 inodes->bytes_left -= c;
3784 inodes->val[inodes->elem_cnt] = inum;
3785 inodes->val[inodes->elem_cnt + 1] = offset;
3786 inodes->val[inodes->elem_cnt + 2] = root;
3787 inodes->elem_cnt += 3;
3788 } else {
3789 inodes->bytes_missing += c - inodes->bytes_left;
3790 inodes->bytes_left = 0;
3791 inodes->elem_missed += 3;
3792 }
3793
3794 return 0;
3795}
3796
3797static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3798 void __user *arg)
3799{
3800 int ret = 0;
3801 int size;
d7728c96
JS
3802 struct btrfs_ioctl_logical_ino_args *loi;
3803 struct btrfs_data_container *inodes = NULL;
3804 struct btrfs_path *path = NULL;
d7728c96
JS
3805
3806 if (!capable(CAP_SYS_ADMIN))
3807 return -EPERM;
3808
3809 loi = memdup_user(arg, sizeof(*loi));
3810 if (IS_ERR(loi)) {
3811 ret = PTR_ERR(loi);
3812 loi = NULL;
3813 goto out;
3814 }
3815
3816 path = btrfs_alloc_path();
3817 if (!path) {
3818 ret = -ENOMEM;
3819 goto out;
3820 }
3821
425d17a2 3822 size = min_t(u32, loi->size, 64 * 1024);
d7728c96
JS
3823 inodes = init_data_container(size);
3824 if (IS_ERR(inodes)) {
3825 ret = PTR_ERR(inodes);
3826 inodes = NULL;
3827 goto out;
3828 }
3829
df031f07
LB
3830 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3831 build_ino_list, inodes);
3832 if (ret == -EINVAL)
d7728c96
JS
3833 ret = -ENOENT;
3834 if (ret < 0)
3835 goto out;
3836
745c4d8e
JM
3837 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3838 (void *)(unsigned long)inodes, size);
d7728c96
JS
3839 if (ret)
3840 ret = -EFAULT;
3841
3842out:
3843 btrfs_free_path(path);
425d17a2 3844 vfree(inodes);
d7728c96
JS
3845 kfree(loi);
3846
3847 return ret;
3848}
3849
19a39dce 3850void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
c9e9f97b
ID
3851 struct btrfs_ioctl_balance_args *bargs)
3852{
3853 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3854
3855 bargs->flags = bctl->flags;
3856
837d5b6e
ID
3857 if (atomic_read(&fs_info->balance_running))
3858 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3859 if (atomic_read(&fs_info->balance_pause_req))
3860 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
a7e99c69
ID
3861 if (atomic_read(&fs_info->balance_cancel_req))
3862 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
837d5b6e 3863
c9e9f97b
ID
3864 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3865 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3866 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
19a39dce
ID
3867
3868 if (lock) {
3869 spin_lock(&fs_info->balance_lock);
3870 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3871 spin_unlock(&fs_info->balance_lock);
3872 } else {
3873 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3874 }
c9e9f97b
ID
3875}
3876
9ba1f6e4 3877static long btrfs_ioctl_balance(struct file *file, void __user *arg)
c9e9f97b 3878{
496ad9aa 3879 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
c9e9f97b
ID
3880 struct btrfs_fs_info *fs_info = root->fs_info;
3881 struct btrfs_ioctl_balance_args *bargs;
3882 struct btrfs_balance_control *bctl;
ed0fb78f 3883 bool need_unlock; /* for mut. excl. ops lock */
c9e9f97b
ID
3884 int ret;
3885
3886 if (!capable(CAP_SYS_ADMIN))
3887 return -EPERM;
3888
e54bfa31 3889 ret = mnt_want_write_file(file);
9ba1f6e4
LB
3890 if (ret)
3891 return ret;
3892
ed0fb78f
ID
3893again:
3894 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3895 mutex_lock(&fs_info->volume_mutex);
3896 mutex_lock(&fs_info->balance_mutex);
3897 need_unlock = true;
3898 goto locked;
3899 }
3900
3901 /*
3902 * mut. excl. ops lock is locked. Three possibilites:
3903 * (1) some other op is running
3904 * (2) balance is running
3905 * (3) balance is paused -- special case (think resume)
3906 */
c9e9f97b 3907 mutex_lock(&fs_info->balance_mutex);
ed0fb78f
ID
3908 if (fs_info->balance_ctl) {
3909 /* this is either (2) or (3) */
3910 if (!atomic_read(&fs_info->balance_running)) {
3911 mutex_unlock(&fs_info->balance_mutex);
3912 if (!mutex_trylock(&fs_info->volume_mutex))
3913 goto again;
3914 mutex_lock(&fs_info->balance_mutex);
3915
3916 if (fs_info->balance_ctl &&
3917 !atomic_read(&fs_info->balance_running)) {
3918 /* this is (3) */
3919 need_unlock = false;
3920 goto locked;
3921 }
3922
3923 mutex_unlock(&fs_info->balance_mutex);
3924 mutex_unlock(&fs_info->volume_mutex);
3925 goto again;
3926 } else {
3927 /* this is (2) */
3928 mutex_unlock(&fs_info->balance_mutex);
3929 ret = -EINPROGRESS;
3930 goto out;
3931 }
3932 } else {
3933 /* this is (1) */
3934 mutex_unlock(&fs_info->balance_mutex);
e57138b3 3935 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
ed0fb78f
ID
3936 goto out;
3937 }
3938
3939locked:
3940 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
c9e9f97b
ID
3941
3942 if (arg) {
3943 bargs = memdup_user(arg, sizeof(*bargs));
3944 if (IS_ERR(bargs)) {
3945 ret = PTR_ERR(bargs);
ed0fb78f 3946 goto out_unlock;
c9e9f97b 3947 }
de322263
ID
3948
3949 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3950 if (!fs_info->balance_ctl) {
3951 ret = -ENOTCONN;
3952 goto out_bargs;
3953 }
3954
3955 bctl = fs_info->balance_ctl;
3956 spin_lock(&fs_info->balance_lock);
3957 bctl->flags |= BTRFS_BALANCE_RESUME;
3958 spin_unlock(&fs_info->balance_lock);
3959
3960 goto do_balance;
3961 }
c9e9f97b
ID
3962 } else {
3963 bargs = NULL;
3964 }
3965
ed0fb78f 3966 if (fs_info->balance_ctl) {
837d5b6e
ID
3967 ret = -EINPROGRESS;
3968 goto out_bargs;
3969 }
3970
c9e9f97b
ID
3971 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3972 if (!bctl) {
3973 ret = -ENOMEM;
3974 goto out_bargs;
3975 }
3976
3977 bctl->fs_info = fs_info;
3978 if (arg) {
3979 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3980 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3981 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3982
3983 bctl->flags = bargs->flags;
f43ffb60
ID
3984 } else {
3985 /* balance everything - no filters */
3986 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
c9e9f97b
ID
3987 }
3988
de322263 3989do_balance:
c9e9f97b 3990 /*
ed0fb78f
ID
3991 * Ownership of bctl and mutually_exclusive_operation_running
3992 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3993 * or, if restriper was paused all the way until unmount, in
3994 * free_fs_info. mutually_exclusive_operation_running is
3995 * cleared in __cancel_balance.
c9e9f97b 3996 */
ed0fb78f
ID
3997 need_unlock = false;
3998
3999 ret = btrfs_balance(bctl, bargs);
4000
c9e9f97b
ID
4001 if (arg) {
4002 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4003 ret = -EFAULT;
4004 }
4005
4006out_bargs:
4007 kfree(bargs);
ed0fb78f 4008out_unlock:
c9e9f97b
ID
4009 mutex_unlock(&fs_info->balance_mutex);
4010 mutex_unlock(&fs_info->volume_mutex);
ed0fb78f
ID
4011 if (need_unlock)
4012 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4013out:
e54bfa31 4014 mnt_drop_write_file(file);
c9e9f97b
ID
4015 return ret;
4016}
4017
837d5b6e
ID
4018static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4019{
4020 if (!capable(CAP_SYS_ADMIN))
4021 return -EPERM;
4022
4023 switch (cmd) {
4024 case BTRFS_BALANCE_CTL_PAUSE:
4025 return btrfs_pause_balance(root->fs_info);
a7e99c69
ID
4026 case BTRFS_BALANCE_CTL_CANCEL:
4027 return btrfs_cancel_balance(root->fs_info);
837d5b6e
ID
4028 }
4029
4030 return -EINVAL;
4031}
4032
19a39dce
ID
4033static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4034 void __user *arg)
4035{
4036 struct btrfs_fs_info *fs_info = root->fs_info;
4037 struct btrfs_ioctl_balance_args *bargs;
4038 int ret = 0;
4039
4040 if (!capable(CAP_SYS_ADMIN))
4041 return -EPERM;
4042
4043 mutex_lock(&fs_info->balance_mutex);
4044 if (!fs_info->balance_ctl) {
4045 ret = -ENOTCONN;
4046 goto out;
4047 }
4048
4049 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4050 if (!bargs) {
4051 ret = -ENOMEM;
4052 goto out;
4053 }
4054
4055 update_ioctl_balance_args(fs_info, 1, bargs);
4056
4057 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4058 ret = -EFAULT;
4059
4060 kfree(bargs);
4061out:
4062 mutex_unlock(&fs_info->balance_mutex);
4063 return ret;
4064}
4065
905b0dda 4066static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
5d13a37b 4067{
496ad9aa 4068 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5d13a37b
AJ
4069 struct btrfs_ioctl_quota_ctl_args *sa;
4070 struct btrfs_trans_handle *trans = NULL;
4071 int ret;
4072 int err;
4073
4074 if (!capable(CAP_SYS_ADMIN))
4075 return -EPERM;
4076
905b0dda
MX
4077 ret = mnt_want_write_file(file);
4078 if (ret)
4079 return ret;
5d13a37b
AJ
4080
4081 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
4082 if (IS_ERR(sa)) {
4083 ret = PTR_ERR(sa);
4084 goto drop_write;
4085 }
5d13a37b 4086
7708f029 4087 down_write(&root->fs_info->subvol_sem);
2f232036
JS
4088 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4089 if (IS_ERR(trans)) {
4090 ret = PTR_ERR(trans);
4091 goto out;
5d13a37b
AJ
4092 }
4093
4094 switch (sa->cmd) {
4095 case BTRFS_QUOTA_CTL_ENABLE:
4096 ret = btrfs_quota_enable(trans, root->fs_info);
4097 break;
4098 case BTRFS_QUOTA_CTL_DISABLE:
4099 ret = btrfs_quota_disable(trans, root->fs_info);
4100 break;
5d13a37b
AJ
4101 default:
4102 ret = -EINVAL;
4103 break;
4104 }
4105
2f232036
JS
4106 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4107 if (err && !ret)
4108 ret = err;
5d13a37b
AJ
4109out:
4110 kfree(sa);
7708f029 4111 up_write(&root->fs_info->subvol_sem);
905b0dda
MX
4112drop_write:
4113 mnt_drop_write_file(file);
5d13a37b
AJ
4114 return ret;
4115}
4116
905b0dda 4117static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
5d13a37b 4118{
496ad9aa 4119 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5d13a37b
AJ
4120 struct btrfs_ioctl_qgroup_assign_args *sa;
4121 struct btrfs_trans_handle *trans;
4122 int ret;
4123 int err;
4124
4125 if (!capable(CAP_SYS_ADMIN))
4126 return -EPERM;
4127
905b0dda
MX
4128 ret = mnt_want_write_file(file);
4129 if (ret)
4130 return ret;
5d13a37b
AJ
4131
4132 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
4133 if (IS_ERR(sa)) {
4134 ret = PTR_ERR(sa);
4135 goto drop_write;
4136 }
5d13a37b
AJ
4137
4138 trans = btrfs_join_transaction(root);
4139 if (IS_ERR(trans)) {
4140 ret = PTR_ERR(trans);
4141 goto out;
4142 }
4143
4144 /* FIXME: check if the IDs really exist */
4145 if (sa->assign) {
4146 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4147 sa->src, sa->dst);
4148 } else {
4149 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4150 sa->src, sa->dst);
4151 }
4152
4153 err = btrfs_end_transaction(trans, root);
4154 if (err && !ret)
4155 ret = err;
4156
4157out:
4158 kfree(sa);
905b0dda
MX
4159drop_write:
4160 mnt_drop_write_file(file);
5d13a37b
AJ
4161 return ret;
4162}
4163
905b0dda 4164static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
5d13a37b 4165{
496ad9aa 4166 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5d13a37b
AJ
4167 struct btrfs_ioctl_qgroup_create_args *sa;
4168 struct btrfs_trans_handle *trans;
4169 int ret;
4170 int err;
4171
4172 if (!capable(CAP_SYS_ADMIN))
4173 return -EPERM;
4174
905b0dda
MX
4175 ret = mnt_want_write_file(file);
4176 if (ret)
4177 return ret;
5d13a37b
AJ
4178
4179 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
4180 if (IS_ERR(sa)) {
4181 ret = PTR_ERR(sa);
4182 goto drop_write;
4183 }
5d13a37b 4184
d86e56cf
MX
4185 if (!sa->qgroupid) {
4186 ret = -EINVAL;
4187 goto out;
4188 }
4189
5d13a37b
AJ
4190 trans = btrfs_join_transaction(root);
4191 if (IS_ERR(trans)) {
4192 ret = PTR_ERR(trans);
4193 goto out;
4194 }
4195
4196 /* FIXME: check if the IDs really exist */
4197 if (sa->create) {
4198 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4199 NULL);
4200 } else {
4201 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4202 }
4203
4204 err = btrfs_end_transaction(trans, root);
4205 if (err && !ret)
4206 ret = err;
4207
4208out:
4209 kfree(sa);
905b0dda
MX
4210drop_write:
4211 mnt_drop_write_file(file);
5d13a37b
AJ
4212 return ret;
4213}
4214
905b0dda 4215static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
5d13a37b 4216{
496ad9aa 4217 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5d13a37b
AJ
4218 struct btrfs_ioctl_qgroup_limit_args *sa;
4219 struct btrfs_trans_handle *trans;
4220 int ret;
4221 int err;
4222 u64 qgroupid;
4223
4224 if (!capable(CAP_SYS_ADMIN))
4225 return -EPERM;
4226
905b0dda
MX
4227 ret = mnt_want_write_file(file);
4228 if (ret)
4229 return ret;
5d13a37b
AJ
4230
4231 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
4232 if (IS_ERR(sa)) {
4233 ret = PTR_ERR(sa);
4234 goto drop_write;
4235 }
5d13a37b
AJ
4236
4237 trans = btrfs_join_transaction(root);
4238 if (IS_ERR(trans)) {
4239 ret = PTR_ERR(trans);
4240 goto out;
4241 }
4242
4243 qgroupid = sa->qgroupid;
4244 if (!qgroupid) {
4245 /* take the current subvol as qgroup */
4246 qgroupid = root->root_key.objectid;
4247 }
4248
4249 /* FIXME: check if the IDs really exist */
4250 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4251
4252 err = btrfs_end_transaction(trans, root);
4253 if (err && !ret)
4254 ret = err;
4255
4256out:
4257 kfree(sa);
905b0dda
MX
4258drop_write:
4259 mnt_drop_write_file(file);
5d13a37b
AJ
4260 return ret;
4261}
4262
2f232036
JS
4263static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4264{
6d0379ec 4265 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2f232036
JS
4266 struct btrfs_ioctl_quota_rescan_args *qsa;
4267 int ret;
4268
4269 if (!capable(CAP_SYS_ADMIN))
4270 return -EPERM;
4271
4272 ret = mnt_want_write_file(file);
4273 if (ret)
4274 return ret;
4275
4276 qsa = memdup_user(arg, sizeof(*qsa));
4277 if (IS_ERR(qsa)) {
4278 ret = PTR_ERR(qsa);
4279 goto drop_write;
4280 }
4281
4282 if (qsa->flags) {
4283 ret = -EINVAL;
4284 goto out;
4285 }
4286
4287 ret = btrfs_qgroup_rescan(root->fs_info);
4288
4289out:
4290 kfree(qsa);
4291drop_write:
4292 mnt_drop_write_file(file);
4293 return ret;
4294}
4295
4296static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4297{
6d0379ec 4298 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2f232036
JS
4299 struct btrfs_ioctl_quota_rescan_args *qsa;
4300 int ret = 0;
4301
4302 if (!capable(CAP_SYS_ADMIN))
4303 return -EPERM;
4304
4305 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4306 if (!qsa)
4307 return -ENOMEM;
4308
4309 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4310 qsa->flags = 1;
4311 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4312 }
4313
4314 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4315 ret = -EFAULT;
4316
4317 kfree(qsa);
4318 return ret;
4319}
4320
57254b6e
JS
4321static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4322{
54563d41 4323 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
57254b6e
JS
4324
4325 if (!capable(CAP_SYS_ADMIN))
4326 return -EPERM;
4327
4328 return btrfs_qgroup_wait_for_completion(root->fs_info);
4329}
4330
8ea05e3a
AB
4331static long btrfs_ioctl_set_received_subvol(struct file *file,
4332 void __user *arg)
4333{
4334 struct btrfs_ioctl_received_subvol_args *sa = NULL;
496ad9aa 4335 struct inode *inode = file_inode(file);
8ea05e3a
AB
4336 struct btrfs_root *root = BTRFS_I(inode)->root;
4337 struct btrfs_root_item *root_item = &root->root_item;
4338 struct btrfs_trans_handle *trans;
4339 struct timespec ct = CURRENT_TIME;
4340 int ret = 0;
dd5f9615 4341 int received_uuid_changed;
8ea05e3a
AB
4342
4343 ret = mnt_want_write_file(file);
4344 if (ret < 0)
4345 return ret;
4346
4347 down_write(&root->fs_info->subvol_sem);
4348
4349 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4350 ret = -EINVAL;
4351 goto out;
4352 }
4353
4354 if (btrfs_root_readonly(root)) {
4355 ret = -EROFS;
4356 goto out;
4357 }
4358
4359 if (!inode_owner_or_capable(inode)) {
4360 ret = -EACCES;
4361 goto out;
4362 }
4363
4364 sa = memdup_user(arg, sizeof(*sa));
4365 if (IS_ERR(sa)) {
4366 ret = PTR_ERR(sa);
4367 sa = NULL;
4368 goto out;
4369 }
4370
dd5f9615
SB
4371 /*
4372 * 1 - root item
4373 * 2 - uuid items (received uuid + subvol uuid)
4374 */
4375 trans = btrfs_start_transaction(root, 3);
8ea05e3a
AB
4376 if (IS_ERR(trans)) {
4377 ret = PTR_ERR(trans);
4378 trans = NULL;
4379 goto out;
4380 }
4381
4382 sa->rtransid = trans->transid;
4383 sa->rtime.sec = ct.tv_sec;
4384 sa->rtime.nsec = ct.tv_nsec;
4385
dd5f9615
SB
4386 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4387 BTRFS_UUID_SIZE);
4388 if (received_uuid_changed &&
4389 !btrfs_is_empty_uuid(root_item->received_uuid))
4390 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4391 root_item->received_uuid,
4392 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4393 root->root_key.objectid);
8ea05e3a
AB
4394 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4395 btrfs_set_root_stransid(root_item, sa->stransid);
4396 btrfs_set_root_rtransid(root_item, sa->rtransid);
3cae210f
QW
4397 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4398 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4399 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4400 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
8ea05e3a
AB
4401
4402 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4403 &root->root_key, &root->root_item);
4404 if (ret < 0) {
4405 btrfs_end_transaction(trans, root);
8ea05e3a 4406 goto out;
dd5f9615
SB
4407 }
4408 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4409 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4410 sa->uuid,
4411 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4412 root->root_key.objectid);
4413 if (ret < 0 && ret != -EEXIST) {
4414 btrfs_abort_transaction(trans, root, ret);
8ea05e3a 4415 goto out;
dd5f9615
SB
4416 }
4417 }
4418 ret = btrfs_commit_transaction(trans, root);
4419 if (ret < 0) {
4420 btrfs_abort_transaction(trans, root, ret);
4421 goto out;
8ea05e3a
AB
4422 }
4423
4424 ret = copy_to_user(arg, sa, sizeof(*sa));
4425 if (ret)
4426 ret = -EFAULT;
4427
4428out:
4429 kfree(sa);
4430 up_write(&root->fs_info->subvol_sem);
4431 mnt_drop_write_file(file);
4432 return ret;
4433}
4434
867ab667 4435static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4436{
6d0379ec 4437 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
a1b83ac5 4438 size_t len;
867ab667 4439 int ret;
a1b83ac5
AJ
4440 char label[BTRFS_LABEL_SIZE];
4441
4442 spin_lock(&root->fs_info->super_lock);
4443 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4444 spin_unlock(&root->fs_info->super_lock);
4445
4446 len = strnlen(label, BTRFS_LABEL_SIZE);
867ab667 4447
4448 if (len == BTRFS_LABEL_SIZE) {
4449 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4450 --len);
4451 }
4452
867ab667 4453 ret = copy_to_user(arg, label, len);
867ab667 4454
4455 return ret ? -EFAULT : 0;
4456}
4457
a8bfd4ab 4458static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4459{
6d0379ec 4460 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
a8bfd4ab 4461 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4462 struct btrfs_trans_handle *trans;
4463 char label[BTRFS_LABEL_SIZE];
4464 int ret;
4465
4466 if (!capable(CAP_SYS_ADMIN))
4467 return -EPERM;
4468
4469 if (copy_from_user(label, arg, sizeof(label)))
4470 return -EFAULT;
4471
4472 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4473 pr_err("btrfs: unable to set label with more than %d bytes\n",
4474 BTRFS_LABEL_SIZE - 1);
4475 return -EINVAL;
4476 }
4477
4478 ret = mnt_want_write_file(file);
4479 if (ret)
4480 return ret;
4481
a8bfd4ab 4482 trans = btrfs_start_transaction(root, 0);
4483 if (IS_ERR(trans)) {
4484 ret = PTR_ERR(trans);
4485 goto out_unlock;
4486 }
4487
a1b83ac5 4488 spin_lock(&root->fs_info->super_lock);
a8bfd4ab 4489 strcpy(super_block->label, label);
a1b83ac5 4490 spin_unlock(&root->fs_info->super_lock);
a8bfd4ab 4491 ret = btrfs_end_transaction(trans, root);
4492
4493out_unlock:
a8bfd4ab 4494 mnt_drop_write_file(file);
4495 return ret;
4496}
4497
2eaa055f
JM
4498#define INIT_FEATURE_FLAGS(suffix) \
4499 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4500 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4501 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4502
4503static int btrfs_ioctl_get_supported_features(struct file *file,
4504 void __user *arg)
4505{
4506 static struct btrfs_ioctl_feature_flags features[3] = {
4507 INIT_FEATURE_FLAGS(SUPP),
4508 INIT_FEATURE_FLAGS(SAFE_SET),
4509 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4510 };
4511
4512 if (copy_to_user(arg, &features, sizeof(features)))
4513 return -EFAULT;
4514
4515 return 0;
4516}
4517
4518static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
4519{
4520 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4521 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4522 struct btrfs_ioctl_feature_flags features;
4523
4524 features.compat_flags = btrfs_super_compat_flags(super_block);
4525 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4526 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4527
4528 if (copy_to_user(arg, &features, sizeof(features)))
4529 return -EFAULT;
4530
4531 return 0;
4532}
4533
3b02a68a
JM
4534static int check_feature_bits(struct btrfs_root *root,
4535 enum btrfs_feature_set set,
2eaa055f
JM
4536 u64 change_mask, u64 flags, u64 supported_flags,
4537 u64 safe_set, u64 safe_clear)
4538{
3b02a68a
JM
4539 const char *type = btrfs_feature_set_names[set];
4540 char *names;
2eaa055f
JM
4541 u64 disallowed, unsupported;
4542 u64 set_mask = flags & change_mask;
4543 u64 clear_mask = ~flags & change_mask;
4544
4545 unsupported = set_mask & ~supported_flags;
4546 if (unsupported) {
3b02a68a
JM
4547 names = btrfs_printable_features(set, unsupported);
4548 if (names) {
4549 btrfs_warn(root->fs_info,
4550 "this kernel does not support the %s feature bit%s",
4551 names, strchr(names, ',') ? "s" : "");
4552 kfree(names);
4553 } else
4554 btrfs_warn(root->fs_info,
2eaa055f
JM
4555 "this kernel does not support %s bits 0x%llx",
4556 type, unsupported);
4557 return -EOPNOTSUPP;
4558 }
4559
4560 disallowed = set_mask & ~safe_set;
4561 if (disallowed) {
3b02a68a
JM
4562 names = btrfs_printable_features(set, disallowed);
4563 if (names) {
4564 btrfs_warn(root->fs_info,
4565 "can't set the %s feature bit%s while mounted",
4566 names, strchr(names, ',') ? "s" : "");
4567 kfree(names);
4568 } else
4569 btrfs_warn(root->fs_info,
2eaa055f
JM
4570 "can't set %s bits 0x%llx while mounted",
4571 type, disallowed);
4572 return -EPERM;
4573 }
4574
4575 disallowed = clear_mask & ~safe_clear;
4576 if (disallowed) {
3b02a68a
JM
4577 names = btrfs_printable_features(set, disallowed);
4578 if (names) {
4579 btrfs_warn(root->fs_info,
4580 "can't clear the %s feature bit%s while mounted",
4581 names, strchr(names, ',') ? "s" : "");
4582 kfree(names);
4583 } else
4584 btrfs_warn(root->fs_info,
2eaa055f
JM
4585 "can't clear %s bits 0x%llx while mounted",
4586 type, disallowed);
4587 return -EPERM;
4588 }
4589
4590 return 0;
4591}
4592
4593#define check_feature(root, change_mask, flags, mask_base) \
3b02a68a 4594check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
2eaa055f
JM
4595 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4596 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4597 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4598
4599static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4600{
4601 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4602 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4603 struct btrfs_ioctl_feature_flags flags[2];
4604 struct btrfs_trans_handle *trans;
4605 u64 newflags;
4606 int ret;
4607
4608 if (!capable(CAP_SYS_ADMIN))
4609 return -EPERM;
4610
4611 if (copy_from_user(flags, arg, sizeof(flags)))
4612 return -EFAULT;
4613
4614 /* Nothing to do */
4615 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4616 !flags[0].incompat_flags)
4617 return 0;
4618
4619 ret = check_feature(root, flags[0].compat_flags,
4620 flags[1].compat_flags, COMPAT);
4621 if (ret)
4622 return ret;
4623
4624 ret = check_feature(root, flags[0].compat_ro_flags,
4625 flags[1].compat_ro_flags, COMPAT_RO);
4626 if (ret)
4627 return ret;
4628
4629 ret = check_feature(root, flags[0].incompat_flags,
4630 flags[1].incompat_flags, INCOMPAT);
4631 if (ret)
4632 return ret;
4633
4634 trans = btrfs_start_transaction(root, 1);
4635 if (IS_ERR(trans))
4636 return PTR_ERR(trans);
4637
4638 spin_lock(&root->fs_info->super_lock);
4639 newflags = btrfs_super_compat_flags(super_block);
4640 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4641 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4642 btrfs_set_super_compat_flags(super_block, newflags);
4643
4644 newflags = btrfs_super_compat_ro_flags(super_block);
4645 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4646 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4647 btrfs_set_super_compat_ro_flags(super_block, newflags);
4648
4649 newflags = btrfs_super_incompat_flags(super_block);
4650 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4651 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4652 btrfs_set_super_incompat_flags(super_block, newflags);
4653 spin_unlock(&root->fs_info->super_lock);
4654
4655 return btrfs_end_transaction(trans, root);
4656}
4657
f46b5a66
CH
4658long btrfs_ioctl(struct file *file, unsigned int
4659 cmd, unsigned long arg)
4660{
496ad9aa 4661 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4bcabaa3 4662 void __user *argp = (void __user *)arg;
f46b5a66
CH
4663
4664 switch (cmd) {
6cbff00f
CH
4665 case FS_IOC_GETFLAGS:
4666 return btrfs_ioctl_getflags(file, argp);
4667 case FS_IOC_SETFLAGS:
4668 return btrfs_ioctl_setflags(file, argp);
4669 case FS_IOC_GETVERSION:
4670 return btrfs_ioctl_getversion(file, argp);
f7039b1d
LD
4671 case FITRIM:
4672 return btrfs_ioctl_fitrim(file, argp);
f46b5a66 4673 case BTRFS_IOC_SNAP_CREATE:
fa0d2b9b 4674 return btrfs_ioctl_snap_create(file, argp, 0);
fdfb1e4f 4675 case BTRFS_IOC_SNAP_CREATE_V2:
fa0d2b9b 4676 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3de4586c 4677 case BTRFS_IOC_SUBVOL_CREATE:
fa0d2b9b 4678 return btrfs_ioctl_snap_create(file, argp, 1);
6f72c7e2
AJ
4679 case BTRFS_IOC_SUBVOL_CREATE_V2:
4680 return btrfs_ioctl_snap_create_v2(file, argp, 1);
76dda93c
YZ
4681 case BTRFS_IOC_SNAP_DESTROY:
4682 return btrfs_ioctl_snap_destroy(file, argp);
0caa102d
LZ
4683 case BTRFS_IOC_SUBVOL_GETFLAGS:
4684 return btrfs_ioctl_subvol_getflags(file, argp);
4685 case BTRFS_IOC_SUBVOL_SETFLAGS:
4686 return btrfs_ioctl_subvol_setflags(file, argp);
6ef5ed0d
JB
4687 case BTRFS_IOC_DEFAULT_SUBVOL:
4688 return btrfs_ioctl_default_subvol(file, argp);
f46b5a66 4689 case BTRFS_IOC_DEFRAG:
1e701a32
CM
4690 return btrfs_ioctl_defrag(file, NULL);
4691 case BTRFS_IOC_DEFRAG_RANGE:
4692 return btrfs_ioctl_defrag(file, argp);
f46b5a66 4693 case BTRFS_IOC_RESIZE:
198605a8 4694 return btrfs_ioctl_resize(file, argp);
f46b5a66 4695 case BTRFS_IOC_ADD_DEV:
4bcabaa3 4696 return btrfs_ioctl_add_dev(root, argp);
f46b5a66 4697 case BTRFS_IOC_RM_DEV:
da24927b 4698 return btrfs_ioctl_rm_dev(file, argp);
475f6387
JS
4699 case BTRFS_IOC_FS_INFO:
4700 return btrfs_ioctl_fs_info(root, argp);
4701 case BTRFS_IOC_DEV_INFO:
4702 return btrfs_ioctl_dev_info(root, argp);
f46b5a66 4703 case BTRFS_IOC_BALANCE:
9ba1f6e4 4704 return btrfs_ioctl_balance(file, NULL);
f46b5a66 4705 case BTRFS_IOC_CLONE:
c5c9cd4d
SW
4706 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4707 case BTRFS_IOC_CLONE_RANGE:
7a865e8a 4708 return btrfs_ioctl_clone_range(file, argp);
f46b5a66
CH
4709 case BTRFS_IOC_TRANS_START:
4710 return btrfs_ioctl_trans_start(file);
4711 case BTRFS_IOC_TRANS_END:
4712 return btrfs_ioctl_trans_end(file);
ac8e9819
CM
4713 case BTRFS_IOC_TREE_SEARCH:
4714 return btrfs_ioctl_tree_search(file, argp);
4715 case BTRFS_IOC_INO_LOOKUP:
4716 return btrfs_ioctl_ino_lookup(file, argp);
d7728c96
JS
4717 case BTRFS_IOC_INO_PATHS:
4718 return btrfs_ioctl_ino_to_path(root, argp);
4719 case BTRFS_IOC_LOGICAL_INO:
4720 return btrfs_ioctl_logical_to_ino(root, argp);
1406e432
JB
4721 case BTRFS_IOC_SPACE_INFO:
4722 return btrfs_ioctl_space_info(root, argp);
01e219e8
JM
4723 case BTRFS_IOC_GLOBAL_RSV:
4724 return btrfs_ioctl_global_rsv(root, argp);
9b199859
FDBM
4725 case BTRFS_IOC_SYNC: {
4726 int ret;
4727
91aef86f 4728 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
9b199859
FDBM
4729 if (ret)
4730 return ret;
4731 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4732 return ret;
4733 }
46204592 4734 case BTRFS_IOC_START_SYNC:
9a8c28be 4735 return btrfs_ioctl_start_sync(root, argp);
46204592 4736 case BTRFS_IOC_WAIT_SYNC:
9a8c28be 4737 return btrfs_ioctl_wait_sync(root, argp);
475f6387 4738 case BTRFS_IOC_SCRUB:
b8e95489 4739 return btrfs_ioctl_scrub(file, argp);
475f6387
JS
4740 case BTRFS_IOC_SCRUB_CANCEL:
4741 return btrfs_ioctl_scrub_cancel(root, argp);
4742 case BTRFS_IOC_SCRUB_PROGRESS:
4743 return btrfs_ioctl_scrub_progress(root, argp);
c9e9f97b 4744 case BTRFS_IOC_BALANCE_V2:
9ba1f6e4 4745 return btrfs_ioctl_balance(file, argp);
837d5b6e
ID
4746 case BTRFS_IOC_BALANCE_CTL:
4747 return btrfs_ioctl_balance_ctl(root, arg);
19a39dce
ID
4748 case BTRFS_IOC_BALANCE_PROGRESS:
4749 return btrfs_ioctl_balance_progress(root, argp);
8ea05e3a
AB
4750 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4751 return btrfs_ioctl_set_received_subvol(file, argp);
31db9f7c
AB
4752 case BTRFS_IOC_SEND:
4753 return btrfs_ioctl_send(file, argp);
c11d2c23 4754 case BTRFS_IOC_GET_DEV_STATS:
b27f7c0c 4755 return btrfs_ioctl_get_dev_stats(root, argp);
5d13a37b 4756 case BTRFS_IOC_QUOTA_CTL:
905b0dda 4757 return btrfs_ioctl_quota_ctl(file, argp);
5d13a37b 4758 case BTRFS_IOC_QGROUP_ASSIGN:
905b0dda 4759 return btrfs_ioctl_qgroup_assign(file, argp);
5d13a37b 4760 case BTRFS_IOC_QGROUP_CREATE:
905b0dda 4761 return btrfs_ioctl_qgroup_create(file, argp);
5d13a37b 4762 case BTRFS_IOC_QGROUP_LIMIT:
905b0dda 4763 return btrfs_ioctl_qgroup_limit(file, argp);
2f232036
JS
4764 case BTRFS_IOC_QUOTA_RESCAN:
4765 return btrfs_ioctl_quota_rescan(file, argp);
4766 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4767 return btrfs_ioctl_quota_rescan_status(file, argp);
57254b6e
JS
4768 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4769 return btrfs_ioctl_quota_rescan_wait(file, argp);
3f6bcfbd
SB
4770 case BTRFS_IOC_DEV_REPLACE:
4771 return btrfs_ioctl_dev_replace(root, argp);
867ab667 4772 case BTRFS_IOC_GET_FSLABEL:
4773 return btrfs_ioctl_get_fslabel(file, argp);
a8bfd4ab 4774 case BTRFS_IOC_SET_FSLABEL:
4775 return btrfs_ioctl_set_fslabel(file, argp);
416161db
MF
4776 case BTRFS_IOC_FILE_EXTENT_SAME:
4777 return btrfs_ioctl_file_extent_same(file, argp);
2eaa055f
JM
4778 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
4779 return btrfs_ioctl_get_supported_features(file, argp);
4780 case BTRFS_IOC_GET_FEATURES:
4781 return btrfs_ioctl_get_features(file, argp);
4782 case BTRFS_IOC_SET_FEATURES:
4783 return btrfs_ioctl_set_features(file, argp);
f46b5a66
CH
4784 }
4785
4786 return -ENOTTY;
4787}