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