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