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