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