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