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