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