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