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