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