Btrfs: add btrfs_trim_fs() to handle FITRIM
[deliverable/linux.git] / fs / btrfs / ioctl.c
CommitLineData
f46b5a66
CH
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
cb8e7090 24#include <linux/fsnotify.h>
f46b5a66
CH
25#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
f46b5a66 30#include <linux/backing-dev.h>
cb8e7090 31#include <linux/mount.h>
f46b5a66 32#include <linux/mpage.h>
cb8e7090 33#include <linux/namei.h>
f46b5a66
CH
34#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
cb8e7090 39#include <linux/security.h>
f46b5a66 40#include <linux/xattr.h>
7ea394f1 41#include <linux/vmalloc.h>
5a0e3ad6 42#include <linux/slab.h>
f7039b1d 43#include <linux/blkdev.h>
4b4e25f2 44#include "compat.h"
f46b5a66
CH
45#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
925baedd 52#include "locking.h"
f46b5a66 53
6cbff00f
CH
54/* Mask out flags that are inappropriate for the given type of inode. */
55static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
56{
57 if (S_ISDIR(mode))
58 return flags;
59 else if (S_ISREG(mode))
60 return flags & ~FS_DIRSYNC_FL;
61 else
62 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
63}
64
65/*
66 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 */
68static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
69{
70 unsigned int iflags = 0;
71
72 if (flags & BTRFS_INODE_SYNC)
73 iflags |= FS_SYNC_FL;
74 if (flags & BTRFS_INODE_IMMUTABLE)
75 iflags |= FS_IMMUTABLE_FL;
76 if (flags & BTRFS_INODE_APPEND)
77 iflags |= FS_APPEND_FL;
78 if (flags & BTRFS_INODE_NODUMP)
79 iflags |= FS_NODUMP_FL;
80 if (flags & BTRFS_INODE_NOATIME)
81 iflags |= FS_NOATIME_FL;
82 if (flags & BTRFS_INODE_DIRSYNC)
83 iflags |= FS_DIRSYNC_FL;
84
85 return iflags;
86}
87
88/*
89 * Update inode->i_flags based on the btrfs internal flags.
90 */
91void btrfs_update_iflags(struct inode *inode)
92{
93 struct btrfs_inode *ip = BTRFS_I(inode);
94
95 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
96
97 if (ip->flags & BTRFS_INODE_SYNC)
98 inode->i_flags |= S_SYNC;
99 if (ip->flags & BTRFS_INODE_IMMUTABLE)
100 inode->i_flags |= S_IMMUTABLE;
101 if (ip->flags & BTRFS_INODE_APPEND)
102 inode->i_flags |= S_APPEND;
103 if (ip->flags & BTRFS_INODE_NOATIME)
104 inode->i_flags |= S_NOATIME;
105 if (ip->flags & BTRFS_INODE_DIRSYNC)
106 inode->i_flags |= S_DIRSYNC;
107}
108
109/*
110 * Inherit flags from the parent inode.
111 *
112 * Unlike extN we don't have any flags we don't want to inherit currently.
113 */
114void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
115{
0b4dcea5
CM
116 unsigned int flags;
117
118 if (!dir)
119 return;
120
121 flags = BTRFS_I(dir)->flags;
6cbff00f
CH
122
123 if (S_ISREG(inode->i_mode))
124 flags &= ~BTRFS_INODE_DIRSYNC;
125 else if (!S_ISDIR(inode->i_mode))
126 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
127
128 BTRFS_I(inode)->flags = flags;
129 btrfs_update_iflags(inode);
130}
131
132static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
133{
134 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
135 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
136
137 if (copy_to_user(arg, &flags, sizeof(flags)))
138 return -EFAULT;
139 return 0;
140}
141
75e7cb7f
LB
142static int check_flags(unsigned int flags)
143{
144 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
145 FS_NOATIME_FL | FS_NODUMP_FL | \
146 FS_SYNC_FL | FS_DIRSYNC_FL | \
147 FS_NOCOMP_FL | FS_COMPR_FL | \
148 FS_NOCOW_FL | FS_COW_FL))
149 return -EOPNOTSUPP;
150
151 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
152 return -EINVAL;
153
154 if ((flags & FS_NOCOW_FL) && (flags & FS_COW_FL))
155 return -EINVAL;
156
157 return 0;
158}
159
6cbff00f
CH
160static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
161{
162 struct inode *inode = file->f_path.dentry->d_inode;
163 struct btrfs_inode *ip = BTRFS_I(inode);
164 struct btrfs_root *root = ip->root;
165 struct btrfs_trans_handle *trans;
166 unsigned int flags, oldflags;
167 int ret;
168
b83cc969
LZ
169 if (btrfs_root_readonly(root))
170 return -EROFS;
171
6cbff00f
CH
172 if (copy_from_user(&flags, arg, sizeof(flags)))
173 return -EFAULT;
174
75e7cb7f
LB
175 ret = check_flags(flags);
176 if (ret)
177 return ret;
f46b5a66 178
6cbff00f
CH
179 if (!is_owner_or_cap(inode))
180 return -EACCES;
181
182 mutex_lock(&inode->i_mutex);
183
184 flags = btrfs_mask_flags(inode->i_mode, flags);
185 oldflags = btrfs_flags_to_ioctl(ip->flags);
186 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
187 if (!capable(CAP_LINUX_IMMUTABLE)) {
188 ret = -EPERM;
189 goto out_unlock;
190 }
191 }
192
193 ret = mnt_want_write(file->f_path.mnt);
194 if (ret)
195 goto out_unlock;
196
197 if (flags & FS_SYNC_FL)
198 ip->flags |= BTRFS_INODE_SYNC;
199 else
200 ip->flags &= ~BTRFS_INODE_SYNC;
201 if (flags & FS_IMMUTABLE_FL)
202 ip->flags |= BTRFS_INODE_IMMUTABLE;
203 else
204 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
205 if (flags & FS_APPEND_FL)
206 ip->flags |= BTRFS_INODE_APPEND;
207 else
208 ip->flags &= ~BTRFS_INODE_APPEND;
209 if (flags & FS_NODUMP_FL)
210 ip->flags |= BTRFS_INODE_NODUMP;
211 else
212 ip->flags &= ~BTRFS_INODE_NODUMP;
213 if (flags & FS_NOATIME_FL)
214 ip->flags |= BTRFS_INODE_NOATIME;
215 else
216 ip->flags &= ~BTRFS_INODE_NOATIME;
217 if (flags & FS_DIRSYNC_FL)
218 ip->flags |= BTRFS_INODE_DIRSYNC;
219 else
220 ip->flags &= ~BTRFS_INODE_DIRSYNC;
221
75e7cb7f
LB
222 /*
223 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
224 * flag may be changed automatically if compression code won't make
225 * things smaller.
226 */
227 if (flags & FS_NOCOMP_FL) {
228 ip->flags &= ~BTRFS_INODE_COMPRESS;
229 ip->flags |= BTRFS_INODE_NOCOMPRESS;
230 } else if (flags & FS_COMPR_FL) {
231 ip->flags |= BTRFS_INODE_COMPRESS;
232 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
233 }
234 if (flags & FS_NOCOW_FL)
235 ip->flags |= BTRFS_INODE_NODATACOW;
236 else if (flags & FS_COW_FL)
237 ip->flags &= ~BTRFS_INODE_NODATACOW;
6cbff00f
CH
238
239 trans = btrfs_join_transaction(root, 1);
3612b495 240 BUG_ON(IS_ERR(trans));
6cbff00f
CH
241
242 ret = btrfs_update_inode(trans, root, inode);
243 BUG_ON(ret);
244
245 btrfs_update_iflags(inode);
246 inode->i_ctime = CURRENT_TIME;
247 btrfs_end_transaction(trans, root);
248
249 mnt_drop_write(file->f_path.mnt);
250 out_unlock:
251 mutex_unlock(&inode->i_mutex);
252 return 0;
253}
254
255static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
256{
257 struct inode *inode = file->f_path.dentry->d_inode;
258
259 return put_user(inode->i_generation, arg);
260}
f46b5a66 261
f7039b1d
LD
262static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
263{
264 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
265 struct btrfs_fs_info *fs_info = root->fs_info;
266 struct btrfs_device *device;
267 struct request_queue *q;
268 struct fstrim_range range;
269 u64 minlen = ULLONG_MAX;
270 u64 num_devices = 0;
271 int ret;
272
273 if (!capable(CAP_SYS_ADMIN))
274 return -EPERM;
275
276 mutex_lock(&fs_info->fs_devices->device_list_mutex);
277 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
278 if (!device->bdev)
279 continue;
280 q = bdev_get_queue(device->bdev);
281 if (blk_queue_discard(q)) {
282 num_devices++;
283 minlen = min((u64)q->limits.discard_granularity,
284 minlen);
285 }
286 }
287 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
288 if (!num_devices)
289 return -EOPNOTSUPP;
290
291 if (copy_from_user(&range, arg, sizeof(range)))
292 return -EFAULT;
293
294 range.minlen = max(range.minlen, minlen);
295 ret = btrfs_trim_fs(root, &range);
296 if (ret < 0)
297 return ret;
298
299 if (copy_to_user(arg, &range, sizeof(range)))
300 return -EFAULT;
301
302 return 0;
303}
304
cb8e7090
CH
305static noinline int create_subvol(struct btrfs_root *root,
306 struct dentry *dentry,
72fd032e
SW
307 char *name, int namelen,
308 u64 *async_transid)
f46b5a66
CH
309{
310 struct btrfs_trans_handle *trans;
311 struct btrfs_key key;
312 struct btrfs_root_item root_item;
313 struct btrfs_inode_item *inode_item;
314 struct extent_buffer *leaf;
76dda93c 315 struct btrfs_root *new_root;
6a912213
JB
316 struct dentry *parent = dget_parent(dentry);
317 struct inode *dir;
f46b5a66
CH
318 int ret;
319 int err;
320 u64 objectid;
321 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3de4586c 322 u64 index = 0;
f46b5a66 323
a22285a6
YZ
324 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
325 0, &objectid);
6a912213
JB
326 if (ret) {
327 dput(parent);
a22285a6 328 return ret;
6a912213
JB
329 }
330
331 dir = parent->d_inode;
332
9ed74f2d
JB
333 /*
334 * 1 - inode item
335 * 2 - refs
336 * 1 - root item
337 * 2 - dir items
338 */
a22285a6 339 trans = btrfs_start_transaction(root, 6);
6a912213
JB
340 if (IS_ERR(trans)) {
341 dput(parent);
a22285a6 342 return PTR_ERR(trans);
6a912213 343 }
f46b5a66 344
5d4f98a2
YZ
345 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
346 0, objectid, NULL, 0, 0, 0);
8e8a1e31
JB
347 if (IS_ERR(leaf)) {
348 ret = PTR_ERR(leaf);
349 goto fail;
350 }
f46b5a66 351
5d4f98a2 352 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
f46b5a66
CH
353 btrfs_set_header_bytenr(leaf, leaf->start);
354 btrfs_set_header_generation(leaf, trans->transid);
5d4f98a2 355 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
f46b5a66
CH
356 btrfs_set_header_owner(leaf, objectid);
357
358 write_extent_buffer(leaf, root->fs_info->fsid,
359 (unsigned long)btrfs_header_fsid(leaf),
360 BTRFS_FSID_SIZE);
5d4f98a2
YZ
361 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
362 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
363 BTRFS_UUID_SIZE);
f46b5a66
CH
364 btrfs_mark_buffer_dirty(leaf);
365
366 inode_item = &root_item.inode;
367 memset(inode_item, 0, sizeof(*inode_item));
368 inode_item->generation = cpu_to_le64(1);
369 inode_item->size = cpu_to_le64(3);
370 inode_item->nlink = cpu_to_le32(1);
a76a3cd4 371 inode_item->nbytes = cpu_to_le64(root->leafsize);
f46b5a66
CH
372 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
373
374 btrfs_set_root_bytenr(&root_item, leaf->start);
84234f3a 375 btrfs_set_root_generation(&root_item, trans->transid);
f46b5a66
CH
376 btrfs_set_root_level(&root_item, 0);
377 btrfs_set_root_refs(&root_item, 1);
86b9f2ec 378 btrfs_set_root_used(&root_item, leaf->len);
80ff3856 379 btrfs_set_root_last_snapshot(&root_item, 0);
f46b5a66
CH
380
381 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
382 root_item.drop_level = 0;
383
925baedd 384 btrfs_tree_unlock(leaf);
f46b5a66
CH
385 free_extent_buffer(leaf);
386 leaf = NULL;
387
388 btrfs_set_root_dirid(&root_item, new_dirid);
389
390 key.objectid = objectid;
5d4f98a2 391 key.offset = 0;
f46b5a66
CH
392 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
393 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
394 &root_item);
395 if (ret)
396 goto fail;
397
76dda93c
YZ
398 key.offset = (u64)-1;
399 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
400 BUG_ON(IS_ERR(new_root));
401
402 btrfs_record_root_in_trans(trans, new_root);
403
404 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
405 BTRFS_I(dir)->block_group);
f46b5a66
CH
406 /*
407 * insert the directory item
408 */
3de4586c
CM
409 ret = btrfs_set_inode_index(dir, &index);
410 BUG_ON(ret);
411
412 ret = btrfs_insert_dir_item(trans, root,
f46b5a66 413 name, namelen, dir->i_ino, &key,
3de4586c 414 BTRFS_FT_DIR, index);
f46b5a66
CH
415 if (ret)
416 goto fail;
0660b5af 417
52c26179
YZ
418 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
419 ret = btrfs_update_inode(trans, root, dir);
420 BUG_ON(ret);
421
0660b5af 422 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4df27c4d 423 objectid, root->root_key.objectid,
0660b5af 424 dir->i_ino, index, name, namelen);
0660b5af 425
76dda93c 426 BUG_ON(ret);
f46b5a66 427
76dda93c 428 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
f46b5a66 429fail:
6a912213 430 dput(parent);
72fd032e
SW
431 if (async_transid) {
432 *async_transid = trans->transid;
433 err = btrfs_commit_transaction_async(trans, root, 1);
434 } else {
435 err = btrfs_commit_transaction(trans, root);
436 }
f46b5a66
CH
437 if (err && !ret)
438 ret = err;
f46b5a66
CH
439 return ret;
440}
441
72fd032e 442static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
b83cc969
LZ
443 char *name, int namelen, u64 *async_transid,
444 bool readonly)
f46b5a66 445{
2e4bfab9 446 struct inode *inode;
6a912213 447 struct dentry *parent;
f46b5a66
CH
448 struct btrfs_pending_snapshot *pending_snapshot;
449 struct btrfs_trans_handle *trans;
2e4bfab9 450 int ret;
f46b5a66
CH
451
452 if (!root->ref_cows)
453 return -EINVAL;
454
3de4586c 455 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
a22285a6
YZ
456 if (!pending_snapshot)
457 return -ENOMEM;
458
459 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
3de4586c 460 pending_snapshot->dentry = dentry;
f46b5a66 461 pending_snapshot->root = root;
b83cc969 462 pending_snapshot->readonly = readonly;
a22285a6
YZ
463
464 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 goto fail;
468 }
469
470 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
471 BUG_ON(ret);
472
f46b5a66
CH
473 list_add(&pending_snapshot->list,
474 &trans->transaction->pending_snapshots);
72fd032e
SW
475 if (async_transid) {
476 *async_transid = trans->transid;
477 ret = btrfs_commit_transaction_async(trans,
478 root->fs_info->extent_root, 1);
479 } else {
480 ret = btrfs_commit_transaction(trans,
481 root->fs_info->extent_root);
482 }
2e4bfab9 483 BUG_ON(ret);
a22285a6
YZ
484
485 ret = pending_snapshot->error;
486 if (ret)
487 goto fail;
488
66b4ffd1
JB
489 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
490 if (ret)
491 goto fail;
f46b5a66 492
6a912213
JB
493 parent = dget_parent(dentry);
494 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
495 dput(parent);
2e4bfab9
YZ
496 if (IS_ERR(inode)) {
497 ret = PTR_ERR(inode);
498 goto fail;
499 }
500 BUG_ON(!inode);
501 d_instantiate(dentry, inode);
502 ret = 0;
503fail:
a22285a6 504 kfree(pending_snapshot);
f46b5a66
CH
505 return ret;
506}
507
4260f7c7
SW
508/* copy of check_sticky in fs/namei.c()
509* It's inline, so penalty for filesystems that don't use sticky bit is
510* minimal.
511*/
512static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
513{
514 uid_t fsuid = current_fsuid();
515
516 if (!(dir->i_mode & S_ISVTX))
517 return 0;
518 if (inode->i_uid == fsuid)
519 return 0;
520 if (dir->i_uid == fsuid)
521 return 0;
522 return !capable(CAP_FOWNER);
523}
524
525/* copy of may_delete in fs/namei.c()
526 * Check whether we can remove a link victim from directory dir, check
527 * whether the type of victim is right.
528 * 1. We can't do it if dir is read-only (done in permission())
529 * 2. We should have write and exec permissions on dir
530 * 3. We can't remove anything from append-only dir
531 * 4. We can't do anything with immutable dir (done in permission())
532 * 5. If the sticky bit on dir is set we should either
533 * a. be owner of dir, or
534 * b. be owner of victim, or
535 * c. have CAP_FOWNER capability
536 * 6. If the victim is append-only or immutable we can't do antyhing with
537 * links pointing to it.
538 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
539 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
540 * 9. We can't remove a root or mountpoint.
541 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
542 * nfs_async_unlink().
543 */
544
545static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
546{
547 int error;
548
549 if (!victim->d_inode)
550 return -ENOENT;
551
552 BUG_ON(victim->d_parent->d_inode != dir);
553 audit_inode_child(victim, dir);
554
555 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
556 if (error)
557 return error;
558 if (IS_APPEND(dir))
559 return -EPERM;
560 if (btrfs_check_sticky(dir, victim->d_inode)||
561 IS_APPEND(victim->d_inode)||
562 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
563 return -EPERM;
564 if (isdir) {
565 if (!S_ISDIR(victim->d_inode->i_mode))
566 return -ENOTDIR;
567 if (IS_ROOT(victim))
568 return -EBUSY;
569 } else if (S_ISDIR(victim->d_inode->i_mode))
570 return -EISDIR;
571 if (IS_DEADDIR(dir))
572 return -ENOENT;
573 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
574 return -EBUSY;
575 return 0;
576}
577
cb8e7090
CH
578/* copy of may_create in fs/namei.c() */
579static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
580{
581 if (child->d_inode)
582 return -EEXIST;
583 if (IS_DEADDIR(dir))
584 return -ENOENT;
585 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
586}
587
588/*
589 * Create a new subvolume below @parent. This is largely modeled after
590 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
591 * inside this filesystem so it's quite a bit simpler.
592 */
76dda93c
YZ
593static noinline int btrfs_mksubvol(struct path *parent,
594 char *name, int namelen,
72fd032e 595 struct btrfs_root *snap_src,
b83cc969 596 u64 *async_transid, bool readonly)
cb8e7090 597{
76dda93c 598 struct inode *dir = parent->dentry->d_inode;
cb8e7090
CH
599 struct dentry *dentry;
600 int error;
601
76dda93c 602 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
cb8e7090
CH
603
604 dentry = lookup_one_len(name, parent->dentry, namelen);
605 error = PTR_ERR(dentry);
606 if (IS_ERR(dentry))
607 goto out_unlock;
608
609 error = -EEXIST;
610 if (dentry->d_inode)
611 goto out_dput;
612
cb8e7090
CH
613 error = mnt_want_write(parent->mnt);
614 if (error)
615 goto out_dput;
616
76dda93c 617 error = btrfs_may_create(dir, dentry);
cb8e7090
CH
618 if (error)
619 goto out_drop_write;
620
76dda93c
YZ
621 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
622
623 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
624 goto out_up_read;
625
3de4586c 626 if (snap_src) {
72fd032e 627 error = create_snapshot(snap_src, dentry,
b83cc969 628 name, namelen, async_transid, readonly);
3de4586c 629 } else {
76dda93c 630 error = create_subvol(BTRFS_I(dir)->root, dentry,
72fd032e 631 name, namelen, async_transid);
3de4586c 632 }
76dda93c
YZ
633 if (!error)
634 fsnotify_mkdir(dir, dentry);
635out_up_read:
636 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
cb8e7090
CH
637out_drop_write:
638 mnt_drop_write(parent->mnt);
639out_dput:
640 dput(dentry);
641out_unlock:
76dda93c 642 mutex_unlock(&dir->i_mutex);
cb8e7090
CH
643 return error;
644}
645
940100a4 646static int should_defrag_range(struct inode *inode, u64 start, u64 len,
1e701a32
CM
647 int thresh, u64 *last_len, u64 *skip,
648 u64 *defrag_end)
940100a4
CM
649{
650 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
651 struct extent_map *em = NULL;
652 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
653 int ret = 1;
654
1e701a32
CM
655
656 if (thresh == 0)
657 thresh = 256 * 1024;
658
940100a4
CM
659 /*
660 * make sure that once we start defragging and extent, we keep on
661 * defragging it
662 */
663 if (start < *defrag_end)
664 return 1;
665
666 *skip = 0;
667
668 /*
669 * hopefully we have this extent in the tree already, try without
670 * the full extent lock
671 */
672 read_lock(&em_tree->lock);
673 em = lookup_extent_mapping(em_tree, start, len);
674 read_unlock(&em_tree->lock);
675
676 if (!em) {
677 /* get the big lock and read metadata off disk */
678 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
679 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
680 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
681
6cf8bfbf 682 if (IS_ERR(em))
940100a4
CM
683 return 0;
684 }
685
686 /* this will cover holes, and inline extents */
687 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
688 ret = 0;
689
690 /*
691 * we hit a real extent, if it is big don't bother defragging it again
692 */
1e701a32 693 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
940100a4
CM
694 ret = 0;
695
696 /*
697 * last_len ends up being a counter of how many bytes we've defragged.
698 * every time we choose not to defrag an extent, we reset *last_len
699 * so that the next tiny extent will force a defrag.
700 *
701 * The end result of this is that tiny extents before a single big
702 * extent will force at least part of that big extent to be defragged.
703 */
704 if (ret) {
705 *last_len += len;
706 *defrag_end = extent_map_end(em);
707 } else {
708 *last_len = 0;
709 *skip = extent_map_end(em);
710 *defrag_end = 0;
711 }
712
713 free_extent_map(em);
714 return ret;
715}
716
1e701a32
CM
717static int btrfs_defrag_file(struct file *file,
718 struct btrfs_ioctl_defrag_range_args *range)
f46b5a66
CH
719{
720 struct inode *inode = fdentry(file)->d_inode;
721 struct btrfs_root *root = BTRFS_I(inode)->root;
722 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3eaa2885 723 struct btrfs_ordered_extent *ordered;
f46b5a66 724 struct page *page;
1a419d85 725 struct btrfs_super_block *disk_super;
f46b5a66
CH
726 unsigned long last_index;
727 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
728 unsigned long total_read = 0;
1a419d85 729 u64 features;
f46b5a66
CH
730 u64 page_start;
731 u64 page_end;
940100a4
CM
732 u64 last_len = 0;
733 u64 skip = 0;
734 u64 defrag_end = 0;
f46b5a66
CH
735 unsigned long i;
736 int ret;
1a419d85
LZ
737 int compress_type = BTRFS_COMPRESS_ZLIB;
738
739 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
740 if (range->compress_type > BTRFS_COMPRESS_TYPES)
741 return -EINVAL;
742 if (range->compress_type)
743 compress_type = range->compress_type;
744 }
f46b5a66 745
940100a4
CM
746 if (inode->i_size == 0)
747 return 0;
748
1e701a32
CM
749 if (range->start + range->len > range->start) {
750 last_index = min_t(u64, inode->i_size - 1,
751 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
752 } else {
753 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
754 }
755
756 i = range->start >> PAGE_CACHE_SHIFT;
940100a4
CM
757 while (i <= last_index) {
758 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1e701a32
CM
759 PAGE_CACHE_SIZE,
760 range->extent_thresh,
761 &last_len, &skip,
940100a4
CM
762 &defrag_end)) {
763 unsigned long next;
764 /*
765 * the should_defrag function tells us how much to skip
766 * bump our counter by the suggested amount
767 */
768 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
769 i = max(i + 1, next);
770 continue;
771 }
f46b5a66 772
f46b5a66
CH
773 if (total_read % ra_pages == 0) {
774 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
775 min(last_index, i + ra_pages - 1));
776 }
777 total_read++;
940100a4 778 mutex_lock(&inode->i_mutex);
1e701a32 779 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1a419d85 780 BTRFS_I(inode)->force_compress = compress_type;
940100a4 781
0ca1f7ce
YZ
782 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
783 if (ret)
784 goto err_unlock;
3eaa2885 785again:
940100a4
CM
786 if (inode->i_size == 0 ||
787 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
788 ret = 0;
789 goto err_reservations;
790 }
791
f46b5a66 792 page = grab_cache_page(inode->i_mapping, i);
0ca1f7ce
YZ
793 if (!page) {
794 ret = -ENOMEM;
940100a4 795 goto err_reservations;
0ca1f7ce 796 }
940100a4 797
f46b5a66
CH
798 if (!PageUptodate(page)) {
799 btrfs_readpage(NULL, page);
800 lock_page(page);
801 if (!PageUptodate(page)) {
802 unlock_page(page);
803 page_cache_release(page);
0ca1f7ce 804 ret = -EIO;
940100a4 805 goto err_reservations;
f46b5a66
CH
806 }
807 }
808
940100a4
CM
809 if (page->mapping != inode->i_mapping) {
810 unlock_page(page);
811 page_cache_release(page);
812 goto again;
813 }
814
f46b5a66 815 wait_on_page_writeback(page);
f46b5a66 816
940100a4 817 if (PageDirty(page)) {
0ca1f7ce 818 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
940100a4
CM
819 goto loop_unlock;
820 }
821
f46b5a66
CH
822 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
823 page_end = page_start + PAGE_CACHE_SIZE - 1;
f46b5a66 824 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3eaa2885
CM
825
826 ordered = btrfs_lookup_ordered_extent(inode, page_start);
827 if (ordered) {
828 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
829 unlock_page(page);
830 page_cache_release(page);
831 btrfs_start_ordered_extent(inode, ordered, 1);
832 btrfs_put_ordered_extent(ordered);
833 goto again;
834 }
835 set_page_extent_mapped(page);
836
f87f057b
CM
837 /*
838 * this makes sure page_mkwrite is called on the
839 * page if it is dirtied again later
840 */
841 clear_page_dirty_for_io(page);
940100a4
CM
842 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
843 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
844 EXTENT_DO_ACCOUNTING, GFP_NOFS);
f87f057b 845
2ac55d41 846 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
940100a4 847 ClearPageChecked(page);
f46b5a66 848 set_page_dirty(page);
a1ed835e 849 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
940100a4
CM
850
851loop_unlock:
f46b5a66
CH
852 unlock_page(page);
853 page_cache_release(page);
940100a4
CM
854 mutex_unlock(&inode->i_mutex);
855
f46b5a66 856 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
940100a4 857 i++;
f46b5a66
CH
858 }
859
1e701a32
CM
860 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
861 filemap_flush(inode->i_mapping);
862
863 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
864 /* the filemap_flush will queue IO into the worker threads, but
865 * we have to make sure the IO is actually started and that
866 * ordered extents get created before we return
867 */
868 atomic_inc(&root->fs_info->async_submit_draining);
869 while (atomic_read(&root->fs_info->nr_async_submits) ||
870 atomic_read(&root->fs_info->async_delalloc_pages)) {
871 wait_event(root->fs_info->async_submit_wait,
872 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
873 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
874 }
875 atomic_dec(&root->fs_info->async_submit_draining);
876
877 mutex_lock(&inode->i_mutex);
261507a0 878 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1e701a32
CM
879 mutex_unlock(&inode->i_mutex);
880 }
881
1a419d85
LZ
882 disk_super = &root->fs_info->super_copy;
883 features = btrfs_super_incompat_flags(disk_super);
884 if (range->compress_type == BTRFS_COMPRESS_LZO) {
885 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
886 btrfs_set_super_incompat_flags(disk_super, features);
887 }
888
f46b5a66 889 return 0;
940100a4
CM
890
891err_reservations:
0ca1f7ce
YZ
892 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
893err_unlock:
940100a4 894 mutex_unlock(&inode->i_mutex);
940100a4 895 return ret;
f46b5a66
CH
896}
897
76dda93c
YZ
898static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
899 void __user *arg)
f46b5a66
CH
900{
901 u64 new_size;
902 u64 old_size;
903 u64 devid = 1;
904 struct btrfs_ioctl_vol_args *vol_args;
905 struct btrfs_trans_handle *trans;
906 struct btrfs_device *device = NULL;
907 char *sizestr;
908 char *devstr = NULL;
909 int ret = 0;
f46b5a66
CH
910 int mod = 0;
911
c146afad
YZ
912 if (root->fs_info->sb->s_flags & MS_RDONLY)
913 return -EROFS;
914
e441d54d
CM
915 if (!capable(CAP_SYS_ADMIN))
916 return -EPERM;
917
dae7b665
LZ
918 vol_args = memdup_user(arg, sizeof(*vol_args));
919 if (IS_ERR(vol_args))
920 return PTR_ERR(vol_args);
5516e595
MF
921
922 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 923
7d9eb12c 924 mutex_lock(&root->fs_info->volume_mutex);
f46b5a66
CH
925 sizestr = vol_args->name;
926 devstr = strchr(sizestr, ':');
927 if (devstr) {
928 char *end;
929 sizestr = devstr + 1;
930 *devstr = '\0';
931 devstr = vol_args->name;
932 devid = simple_strtoull(devstr, &end, 10);
21380931
JB
933 printk(KERN_INFO "resizing devid %llu\n",
934 (unsigned long long)devid);
f46b5a66 935 }
2b82032c 936 device = btrfs_find_device(root, devid, NULL, NULL);
f46b5a66 937 if (!device) {
21380931
JB
938 printk(KERN_INFO "resizer unable to find device %llu\n",
939 (unsigned long long)devid);
f46b5a66
CH
940 ret = -EINVAL;
941 goto out_unlock;
942 }
943 if (!strcmp(sizestr, "max"))
944 new_size = device->bdev->bd_inode->i_size;
945 else {
946 if (sizestr[0] == '-') {
947 mod = -1;
948 sizestr++;
949 } else if (sizestr[0] == '+') {
950 mod = 1;
951 sizestr++;
952 }
91748467 953 new_size = memparse(sizestr, NULL);
f46b5a66
CH
954 if (new_size == 0) {
955 ret = -EINVAL;
956 goto out_unlock;
957 }
958 }
959
960 old_size = device->total_bytes;
961
962 if (mod < 0) {
963 if (new_size > old_size) {
964 ret = -EINVAL;
965 goto out_unlock;
966 }
967 new_size = old_size - new_size;
968 } else if (mod > 0) {
969 new_size = old_size + new_size;
970 }
971
972 if (new_size < 256 * 1024 * 1024) {
973 ret = -EINVAL;
974 goto out_unlock;
975 }
976 if (new_size > device->bdev->bd_inode->i_size) {
977 ret = -EFBIG;
978 goto out_unlock;
979 }
980
981 do_div(new_size, root->sectorsize);
982 new_size *= root->sectorsize;
983
984 printk(KERN_INFO "new size for %s is %llu\n",
985 device->name, (unsigned long long)new_size);
986
987 if (new_size > old_size) {
a22285a6 988 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
989 if (IS_ERR(trans)) {
990 ret = PTR_ERR(trans);
991 goto out_unlock;
992 }
f46b5a66
CH
993 ret = btrfs_grow_device(trans, device, new_size);
994 btrfs_commit_transaction(trans, root);
995 } else {
996 ret = btrfs_shrink_device(device, new_size);
997 }
998
999out_unlock:
7d9eb12c 1000 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
1001 kfree(vol_args);
1002 return ret;
1003}
1004
72fd032e
SW
1005static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1006 char *name,
1007 unsigned long fd,
1008 int subvol,
b83cc969
LZ
1009 u64 *transid,
1010 bool readonly)
f46b5a66 1011{
cb8e7090 1012 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3de4586c 1013 struct file *src_file;
f46b5a66 1014 int namelen;
3de4586c 1015 int ret = 0;
f46b5a66 1016
c146afad
YZ
1017 if (root->fs_info->sb->s_flags & MS_RDONLY)
1018 return -EROFS;
1019
72fd032e
SW
1020 namelen = strlen(name);
1021 if (strchr(name, '/')) {
f46b5a66
CH
1022 ret = -EINVAL;
1023 goto out;
1024 }
1025
3de4586c 1026 if (subvol) {
72fd032e 1027 ret = btrfs_mksubvol(&file->f_path, name, namelen,
b83cc969 1028 NULL, transid, readonly);
cb8e7090 1029 } else {
3de4586c 1030 struct inode *src_inode;
72fd032e 1031 src_file = fget(fd);
3de4586c
CM
1032 if (!src_file) {
1033 ret = -EINVAL;
1034 goto out;
1035 }
1036
1037 src_inode = src_file->f_path.dentry->d_inode;
1038 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
d397712b
CM
1039 printk(KERN_INFO "btrfs: Snapshot src from "
1040 "another FS\n");
3de4586c
CM
1041 ret = -EINVAL;
1042 fput(src_file);
1043 goto out;
1044 }
72fd032e
SW
1045 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1046 BTRFS_I(src_inode)->root,
b83cc969 1047 transid, readonly);
3de4586c 1048 fput(src_file);
cb8e7090 1049 }
f46b5a66 1050out:
72fd032e
SW
1051 return ret;
1052}
1053
1054static noinline int btrfs_ioctl_snap_create(struct file *file,
fa0d2b9b 1055 void __user *arg, int subvol)
72fd032e 1056{
fa0d2b9b 1057 struct btrfs_ioctl_vol_args *vol_args;
72fd032e
SW
1058 int ret;
1059
fa0d2b9b
LZ
1060 vol_args = memdup_user(arg, sizeof(*vol_args));
1061 if (IS_ERR(vol_args))
1062 return PTR_ERR(vol_args);
1063 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
72fd032e 1064
fa0d2b9b 1065 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
b83cc969
LZ
1066 vol_args->fd, subvol,
1067 NULL, false);
fdfb1e4f 1068
fa0d2b9b
LZ
1069 kfree(vol_args);
1070 return ret;
1071}
fdfb1e4f 1072
fa0d2b9b
LZ
1073static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1074 void __user *arg, int subvol)
1075{
1076 struct btrfs_ioctl_vol_args_v2 *vol_args;
1077 int ret;
1078 u64 transid = 0;
1079 u64 *ptr = NULL;
b83cc969 1080 bool readonly = false;
75eaa0e2 1081
fa0d2b9b
LZ
1082 vol_args = memdup_user(arg, sizeof(*vol_args));
1083 if (IS_ERR(vol_args))
1084 return PTR_ERR(vol_args);
1085 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
75eaa0e2 1086
b83cc969
LZ
1087 if (vol_args->flags &
1088 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1089 ret = -EOPNOTSUPP;
fa0d2b9b 1090 goto out;
72fd032e 1091 }
fa0d2b9b
LZ
1092
1093 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1094 ptr = &transid;
b83cc969
LZ
1095 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1096 readonly = true;
fa0d2b9b
LZ
1097
1098 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
b83cc969
LZ
1099 vol_args->fd, subvol,
1100 ptr, readonly);
fa0d2b9b
LZ
1101
1102 if (ret == 0 && ptr &&
1103 copy_to_user(arg +
1104 offsetof(struct btrfs_ioctl_vol_args_v2,
1105 transid), ptr, sizeof(*ptr)))
1106 ret = -EFAULT;
fdfb1e4f 1107out:
f46b5a66
CH
1108 kfree(vol_args);
1109 return ret;
1110}
1111
0caa102d
LZ
1112static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1113 void __user *arg)
1114{
1115 struct inode *inode = fdentry(file)->d_inode;
1116 struct btrfs_root *root = BTRFS_I(inode)->root;
1117 int ret = 0;
1118 u64 flags = 0;
1119
1120 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1121 return -EINVAL;
1122
1123 down_read(&root->fs_info->subvol_sem);
1124 if (btrfs_root_readonly(root))
1125 flags |= BTRFS_SUBVOL_RDONLY;
1126 up_read(&root->fs_info->subvol_sem);
1127
1128 if (copy_to_user(arg, &flags, sizeof(flags)))
1129 ret = -EFAULT;
1130
1131 return ret;
1132}
1133
1134static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1135 void __user *arg)
1136{
1137 struct inode *inode = fdentry(file)->d_inode;
1138 struct btrfs_root *root = BTRFS_I(inode)->root;
1139 struct btrfs_trans_handle *trans;
1140 u64 root_flags;
1141 u64 flags;
1142 int ret = 0;
1143
1144 if (root->fs_info->sb->s_flags & MS_RDONLY)
1145 return -EROFS;
1146
1147 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1148 return -EINVAL;
1149
1150 if (copy_from_user(&flags, arg, sizeof(flags)))
1151 return -EFAULT;
1152
b4dc2b8c 1153 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
0caa102d
LZ
1154 return -EINVAL;
1155
1156 if (flags & ~BTRFS_SUBVOL_RDONLY)
1157 return -EOPNOTSUPP;
1158
b4dc2b8c
LZ
1159 if (!is_owner_or_cap(inode))
1160 return -EACCES;
1161
0caa102d
LZ
1162 down_write(&root->fs_info->subvol_sem);
1163
1164 /* nothing to do */
1165 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1166 goto out;
1167
1168 root_flags = btrfs_root_flags(&root->root_item);
1169 if (flags & BTRFS_SUBVOL_RDONLY)
1170 btrfs_set_root_flags(&root->root_item,
1171 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1172 else
1173 btrfs_set_root_flags(&root->root_item,
1174 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1175
1176 trans = btrfs_start_transaction(root, 1);
1177 if (IS_ERR(trans)) {
1178 ret = PTR_ERR(trans);
1179 goto out_reset;
1180 }
1181
b4dc2b8c 1182 ret = btrfs_update_root(trans, root->fs_info->tree_root,
0caa102d
LZ
1183 &root->root_key, &root->root_item);
1184
1185 btrfs_commit_transaction(trans, root);
1186out_reset:
1187 if (ret)
1188 btrfs_set_root_flags(&root->root_item, root_flags);
1189out:
1190 up_write(&root->fs_info->subvol_sem);
1191 return ret;
1192}
1193
76dda93c
YZ
1194/*
1195 * helper to check if the subvolume references other subvolumes
1196 */
1197static noinline int may_destroy_subvol(struct btrfs_root *root)
1198{
1199 struct btrfs_path *path;
1200 struct btrfs_key key;
1201 int ret;
1202
1203 path = btrfs_alloc_path();
1204 if (!path)
1205 return -ENOMEM;
1206
1207 key.objectid = root->root_key.objectid;
1208 key.type = BTRFS_ROOT_REF_KEY;
1209 key.offset = (u64)-1;
1210
1211 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1212 &key, path, 0, 0);
1213 if (ret < 0)
1214 goto out;
1215 BUG_ON(ret == 0);
1216
1217 ret = 0;
1218 if (path->slots[0] > 0) {
1219 path->slots[0]--;
1220 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1221 if (key.objectid == root->root_key.objectid &&
1222 key.type == BTRFS_ROOT_REF_KEY)
1223 ret = -ENOTEMPTY;
1224 }
1225out:
1226 btrfs_free_path(path);
1227 return ret;
1228}
1229
ac8e9819
CM
1230static noinline int key_in_sk(struct btrfs_key *key,
1231 struct btrfs_ioctl_search_key *sk)
1232{
abc6e134
CM
1233 struct btrfs_key test;
1234 int ret;
1235
1236 test.objectid = sk->min_objectid;
1237 test.type = sk->min_type;
1238 test.offset = sk->min_offset;
1239
1240 ret = btrfs_comp_cpu_keys(key, &test);
1241 if (ret < 0)
ac8e9819 1242 return 0;
abc6e134
CM
1243
1244 test.objectid = sk->max_objectid;
1245 test.type = sk->max_type;
1246 test.offset = sk->max_offset;
1247
1248 ret = btrfs_comp_cpu_keys(key, &test);
1249 if (ret > 0)
ac8e9819
CM
1250 return 0;
1251 return 1;
1252}
1253
1254static noinline int copy_to_sk(struct btrfs_root *root,
1255 struct btrfs_path *path,
1256 struct btrfs_key *key,
1257 struct btrfs_ioctl_search_key *sk,
1258 char *buf,
1259 unsigned long *sk_offset,
1260 int *num_found)
1261{
1262 u64 found_transid;
1263 struct extent_buffer *leaf;
1264 struct btrfs_ioctl_search_header sh;
1265 unsigned long item_off;
1266 unsigned long item_len;
1267 int nritems;
1268 int i;
1269 int slot;
1270 int found = 0;
1271 int ret = 0;
1272
1273 leaf = path->nodes[0];
1274 slot = path->slots[0];
1275 nritems = btrfs_header_nritems(leaf);
1276
1277 if (btrfs_header_generation(leaf) > sk->max_transid) {
1278 i = nritems;
1279 goto advance_key;
1280 }
1281 found_transid = btrfs_header_generation(leaf);
1282
1283 for (i = slot; i < nritems; i++) {
1284 item_off = btrfs_item_ptr_offset(leaf, i);
1285 item_len = btrfs_item_size_nr(leaf, i);
1286
1287 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1288 item_len = 0;
1289
1290 if (sizeof(sh) + item_len + *sk_offset >
1291 BTRFS_SEARCH_ARGS_BUFSIZE) {
1292 ret = 1;
1293 goto overflow;
1294 }
1295
1296 btrfs_item_key_to_cpu(leaf, key, i);
1297 if (!key_in_sk(key, sk))
1298 continue;
1299
1300 sh.objectid = key->objectid;
1301 sh.offset = key->offset;
1302 sh.type = key->type;
1303 sh.len = item_len;
1304 sh.transid = found_transid;
1305
1306 /* copy search result header */
1307 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1308 *sk_offset += sizeof(sh);
1309
1310 if (item_len) {
1311 char *p = buf + *sk_offset;
1312 /* copy the item */
1313 read_extent_buffer(leaf, p,
1314 item_off, item_len);
1315 *sk_offset += item_len;
ac8e9819 1316 }
90fdde14 1317 found++;
ac8e9819
CM
1318
1319 if (*num_found >= sk->nr_items)
1320 break;
1321 }
1322advance_key:
abc6e134
CM
1323 ret = 0;
1324 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
ac8e9819 1325 key->offset++;
abc6e134
CM
1326 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1327 key->offset = 0;
ac8e9819 1328 key->type++;
abc6e134
CM
1329 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1330 key->offset = 0;
1331 key->type = 0;
ac8e9819 1332 key->objectid++;
abc6e134
CM
1333 } else
1334 ret = 1;
ac8e9819
CM
1335overflow:
1336 *num_found += found;
1337 return ret;
1338}
1339
1340static noinline int search_ioctl(struct inode *inode,
1341 struct btrfs_ioctl_search_args *args)
1342{
1343 struct btrfs_root *root;
1344 struct btrfs_key key;
1345 struct btrfs_key max_key;
1346 struct btrfs_path *path;
1347 struct btrfs_ioctl_search_key *sk = &args->key;
1348 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1349 int ret;
1350 int num_found = 0;
1351 unsigned long sk_offset = 0;
1352
1353 path = btrfs_alloc_path();
1354 if (!path)
1355 return -ENOMEM;
1356
1357 if (sk->tree_id == 0) {
1358 /* search the root of the inode that was passed */
1359 root = BTRFS_I(inode)->root;
1360 } else {
1361 key.objectid = sk->tree_id;
1362 key.type = BTRFS_ROOT_ITEM_KEY;
1363 key.offset = (u64)-1;
1364 root = btrfs_read_fs_root_no_name(info, &key);
1365 if (IS_ERR(root)) {
1366 printk(KERN_ERR "could not find root %llu\n",
1367 sk->tree_id);
1368 btrfs_free_path(path);
1369 return -ENOENT;
1370 }
1371 }
1372
1373 key.objectid = sk->min_objectid;
1374 key.type = sk->min_type;
1375 key.offset = sk->min_offset;
1376
1377 max_key.objectid = sk->max_objectid;
1378 max_key.type = sk->max_type;
1379 max_key.offset = sk->max_offset;
1380
1381 path->keep_locks = 1;
1382
1383 while(1) {
1384 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1385 sk->min_transid);
1386 if (ret != 0) {
1387 if (ret > 0)
1388 ret = 0;
1389 goto err;
1390 }
1391 ret = copy_to_sk(root, path, &key, sk, args->buf,
1392 &sk_offset, &num_found);
1393 btrfs_release_path(root, path);
1394 if (ret || num_found >= sk->nr_items)
1395 break;
1396
1397 }
1398 ret = 0;
1399err:
1400 sk->nr_items = num_found;
1401 btrfs_free_path(path);
1402 return ret;
1403}
1404
1405static noinline int btrfs_ioctl_tree_search(struct file *file,
1406 void __user *argp)
1407{
1408 struct btrfs_ioctl_search_args *args;
1409 struct inode *inode;
1410 int ret;
1411
1412 if (!capable(CAP_SYS_ADMIN))
1413 return -EPERM;
1414
2354d08f
JL
1415 args = memdup_user(argp, sizeof(*args));
1416 if (IS_ERR(args))
1417 return PTR_ERR(args);
ac8e9819 1418
ac8e9819
CM
1419 inode = fdentry(file)->d_inode;
1420 ret = search_ioctl(inode, args);
1421 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1422 ret = -EFAULT;
1423 kfree(args);
1424 return ret;
1425}
1426
98d377a0 1427/*
ac8e9819
CM
1428 * Search INODE_REFs to identify path name of 'dirid' directory
1429 * in a 'tree_id' tree. and sets path name to 'name'.
1430 */
98d377a0
TH
1431static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1432 u64 tree_id, u64 dirid, char *name)
1433{
1434 struct btrfs_root *root;
1435 struct btrfs_key key;
ac8e9819 1436 char *ptr;
98d377a0
TH
1437 int ret = -1;
1438 int slot;
1439 int len;
1440 int total_len = 0;
1441 struct btrfs_inode_ref *iref;
1442 struct extent_buffer *l;
1443 struct btrfs_path *path;
1444
1445 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1446 name[0]='\0';
1447 return 0;
1448 }
1449
1450 path = btrfs_alloc_path();
1451 if (!path)
1452 return -ENOMEM;
1453
ac8e9819 1454 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
98d377a0
TH
1455
1456 key.objectid = tree_id;
1457 key.type = BTRFS_ROOT_ITEM_KEY;
1458 key.offset = (u64)-1;
1459 root = btrfs_read_fs_root_no_name(info, &key);
1460 if (IS_ERR(root)) {
1461 printk(KERN_ERR "could not find root %llu\n", tree_id);
8ad6fcab
CM
1462 ret = -ENOENT;
1463 goto out;
98d377a0
TH
1464 }
1465
1466 key.objectid = dirid;
1467 key.type = BTRFS_INODE_REF_KEY;
8ad6fcab 1468 key.offset = (u64)-1;
98d377a0
TH
1469
1470 while(1) {
1471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1472 if (ret < 0)
1473 goto out;
1474
1475 l = path->nodes[0];
1476 slot = path->slots[0];
8ad6fcab
CM
1477 if (ret > 0 && slot > 0)
1478 slot--;
98d377a0
TH
1479 btrfs_item_key_to_cpu(l, &key, slot);
1480
1481 if (ret > 0 && (key.objectid != dirid ||
ac8e9819
CM
1482 key.type != BTRFS_INODE_REF_KEY)) {
1483 ret = -ENOENT;
98d377a0 1484 goto out;
ac8e9819 1485 }
98d377a0
TH
1486
1487 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1488 len = btrfs_inode_ref_name_len(l, iref);
1489 ptr -= len + 1;
1490 total_len += len + 1;
ac8e9819 1491 if (ptr < name)
98d377a0
TH
1492 goto out;
1493
1494 *(ptr + len) = '/';
1495 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1496
1497 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1498 break;
1499
1500 btrfs_release_path(root, path);
1501 key.objectid = key.offset;
8ad6fcab 1502 key.offset = (u64)-1;
98d377a0
TH
1503 dirid = key.objectid;
1504
1505 }
ac8e9819 1506 if (ptr < name)
98d377a0 1507 goto out;
ac8e9819 1508 memcpy(name, ptr, total_len);
98d377a0
TH
1509 name[total_len]='\0';
1510 ret = 0;
1511out:
1512 btrfs_free_path(path);
ac8e9819
CM
1513 return ret;
1514}
1515
1516static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1517 void __user *argp)
1518{
1519 struct btrfs_ioctl_ino_lookup_args *args;
1520 struct inode *inode;
1521 int ret;
1522
1523 if (!capable(CAP_SYS_ADMIN))
1524 return -EPERM;
1525
2354d08f
JL
1526 args = memdup_user(argp, sizeof(*args));
1527 if (IS_ERR(args))
1528 return PTR_ERR(args);
c2b96929 1529
ac8e9819
CM
1530 inode = fdentry(file)->d_inode;
1531
1b53ac4d
CM
1532 if (args->treeid == 0)
1533 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1534
ac8e9819
CM
1535 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1536 args->treeid, args->objectid,
1537 args->name);
1538
1539 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1540 ret = -EFAULT;
1541
1542 kfree(args);
98d377a0
TH
1543 return ret;
1544}
1545
76dda93c
YZ
1546static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1547 void __user *arg)
1548{
1549 struct dentry *parent = fdentry(file);
1550 struct dentry *dentry;
1551 struct inode *dir = parent->d_inode;
1552 struct inode *inode;
1553 struct btrfs_root *root = BTRFS_I(dir)->root;
1554 struct btrfs_root *dest = NULL;
1555 struct btrfs_ioctl_vol_args *vol_args;
1556 struct btrfs_trans_handle *trans;
1557 int namelen;
1558 int ret;
1559 int err = 0;
1560
76dda93c
YZ
1561 vol_args = memdup_user(arg, sizeof(*vol_args));
1562 if (IS_ERR(vol_args))
1563 return PTR_ERR(vol_args);
1564
1565 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1566 namelen = strlen(vol_args->name);
1567 if (strchr(vol_args->name, '/') ||
1568 strncmp(vol_args->name, "..", namelen) == 0) {
1569 err = -EINVAL;
1570 goto out;
1571 }
1572
1573 err = mnt_want_write(file->f_path.mnt);
1574 if (err)
1575 goto out;
1576
1577 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1578 dentry = lookup_one_len(vol_args->name, parent, namelen);
1579 if (IS_ERR(dentry)) {
1580 err = PTR_ERR(dentry);
1581 goto out_unlock_dir;
1582 }
1583
1584 if (!dentry->d_inode) {
1585 err = -ENOENT;
1586 goto out_dput;
1587 }
1588
1589 inode = dentry->d_inode;
4260f7c7
SW
1590 dest = BTRFS_I(inode)->root;
1591 if (!capable(CAP_SYS_ADMIN)){
1592 /*
1593 * Regular user. Only allow this with a special mount
1594 * option, when the user has write+exec access to the
1595 * subvol root, and when rmdir(2) would have been
1596 * allowed.
1597 *
1598 * Note that this is _not_ check that the subvol is
1599 * empty or doesn't contain data that we wouldn't
1600 * otherwise be able to delete.
1601 *
1602 * Users who want to delete empty subvols should try
1603 * rmdir(2).
1604 */
1605 err = -EPERM;
1606 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1607 goto out_dput;
1608
1609 /*
1610 * Do not allow deletion if the parent dir is the same
1611 * as the dir to be deleted. That means the ioctl
1612 * must be called on the dentry referencing the root
1613 * of the subvol, not a random directory contained
1614 * within it.
1615 */
1616 err = -EINVAL;
1617 if (root == dest)
1618 goto out_dput;
1619
1620 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1621 if (err)
1622 goto out_dput;
1623
1624 /* check if subvolume may be deleted by a non-root user */
1625 err = btrfs_may_delete(dir, dentry, 1);
1626 if (err)
1627 goto out_dput;
1628 }
1629
76dda93c
YZ
1630 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1631 err = -EINVAL;
1632 goto out_dput;
1633 }
1634
76dda93c
YZ
1635 mutex_lock(&inode->i_mutex);
1636 err = d_invalidate(dentry);
1637 if (err)
1638 goto out_unlock;
1639
1640 down_write(&root->fs_info->subvol_sem);
1641
1642 err = may_destroy_subvol(dest);
1643 if (err)
1644 goto out_up_write;
1645
a22285a6
YZ
1646 trans = btrfs_start_transaction(root, 0);
1647 if (IS_ERR(trans)) {
1648 err = PTR_ERR(trans);
d327099a 1649 goto out_up_write;
a22285a6
YZ
1650 }
1651 trans->block_rsv = &root->fs_info->global_block_rsv;
1652
76dda93c
YZ
1653 ret = btrfs_unlink_subvol(trans, root, dir,
1654 dest->root_key.objectid,
1655 dentry->d_name.name,
1656 dentry->d_name.len);
1657 BUG_ON(ret);
1658
1659 btrfs_record_root_in_trans(trans, dest);
1660
1661 memset(&dest->root_item.drop_progress, 0,
1662 sizeof(dest->root_item.drop_progress));
1663 dest->root_item.drop_level = 0;
1664 btrfs_set_root_refs(&dest->root_item, 0);
1665
d68fc57b
YZ
1666 if (!xchg(&dest->orphan_item_inserted, 1)) {
1667 ret = btrfs_insert_orphan_item(trans,
1668 root->fs_info->tree_root,
1669 dest->root_key.objectid);
1670 BUG_ON(ret);
1671 }
76dda93c 1672
531cb13f 1673 ret = btrfs_end_transaction(trans, root);
76dda93c
YZ
1674 BUG_ON(ret);
1675 inode->i_flags |= S_DEAD;
1676out_up_write:
1677 up_write(&root->fs_info->subvol_sem);
1678out_unlock:
1679 mutex_unlock(&inode->i_mutex);
1680 if (!err) {
efefb143 1681 shrink_dcache_sb(root->fs_info->sb);
76dda93c
YZ
1682 btrfs_invalidate_inodes(dest);
1683 d_delete(dentry);
1684 }
1685out_dput:
1686 dput(dentry);
1687out_unlock_dir:
1688 mutex_unlock(&dir->i_mutex);
1689 mnt_drop_write(file->f_path.mnt);
1690out:
1691 kfree(vol_args);
1692 return err;
1693}
1694
1e701a32 1695static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
f46b5a66
CH
1696{
1697 struct inode *inode = fdentry(file)->d_inode;
1698 struct btrfs_root *root = BTRFS_I(inode)->root;
1e701a32 1699 struct btrfs_ioctl_defrag_range_args *range;
c146afad
YZ
1700 int ret;
1701
b83cc969
LZ
1702 if (btrfs_root_readonly(root))
1703 return -EROFS;
1704
c146afad
YZ
1705 ret = mnt_want_write(file->f_path.mnt);
1706 if (ret)
1707 return ret;
f46b5a66
CH
1708
1709 switch (inode->i_mode & S_IFMT) {
1710 case S_IFDIR:
e441d54d
CM
1711 if (!capable(CAP_SYS_ADMIN)) {
1712 ret = -EPERM;
1713 goto out;
1714 }
8929ecfa
YZ
1715 ret = btrfs_defrag_root(root, 0);
1716 if (ret)
1717 goto out;
1718 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
f46b5a66
CH
1719 break;
1720 case S_IFREG:
e441d54d
CM
1721 if (!(file->f_mode & FMODE_WRITE)) {
1722 ret = -EINVAL;
1723 goto out;
1724 }
1e701a32
CM
1725
1726 range = kzalloc(sizeof(*range), GFP_KERNEL);
1727 if (!range) {
1728 ret = -ENOMEM;
1729 goto out;
1730 }
1731
1732 if (argp) {
1733 if (copy_from_user(range, argp,
1734 sizeof(*range))) {
1735 ret = -EFAULT;
1736 kfree(range);
683be16e 1737 goto out;
1e701a32
CM
1738 }
1739 /* compression requires us to start the IO */
1740 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1741 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1742 range->extent_thresh = (u32)-1;
1743 }
1744 } else {
1745 /* the rest are all set to zero by kzalloc */
1746 range->len = (u64)-1;
1747 }
8929ecfa 1748 ret = btrfs_defrag_file(file, range);
1e701a32 1749 kfree(range);
f46b5a66 1750 break;
8929ecfa
YZ
1751 default:
1752 ret = -EINVAL;
f46b5a66 1753 }
e441d54d 1754out:
ab67b7c1 1755 mnt_drop_write(file->f_path.mnt);
e441d54d 1756 return ret;
f46b5a66
CH
1757}
1758
b2950863 1759static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
1760{
1761 struct btrfs_ioctl_vol_args *vol_args;
1762 int ret;
1763
e441d54d
CM
1764 if (!capable(CAP_SYS_ADMIN))
1765 return -EPERM;
1766
dae7b665
LZ
1767 vol_args = memdup_user(arg, sizeof(*vol_args));
1768 if (IS_ERR(vol_args))
1769 return PTR_ERR(vol_args);
f46b5a66 1770
5516e595 1771 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
1772 ret = btrfs_init_new_device(root, vol_args->name);
1773
f46b5a66
CH
1774 kfree(vol_args);
1775 return ret;
1776}
1777
b2950863 1778static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
1779{
1780 struct btrfs_ioctl_vol_args *vol_args;
1781 int ret;
1782
e441d54d
CM
1783 if (!capable(CAP_SYS_ADMIN))
1784 return -EPERM;
1785
c146afad
YZ
1786 if (root->fs_info->sb->s_flags & MS_RDONLY)
1787 return -EROFS;
1788
dae7b665
LZ
1789 vol_args = memdup_user(arg, sizeof(*vol_args));
1790 if (IS_ERR(vol_args))
1791 return PTR_ERR(vol_args);
f46b5a66 1792
5516e595 1793 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
1794 ret = btrfs_rm_device(root, vol_args->name);
1795
f46b5a66
CH
1796 kfree(vol_args);
1797 return ret;
1798}
1799
76dda93c
YZ
1800static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1801 u64 off, u64 olen, u64 destoff)
f46b5a66
CH
1802{
1803 struct inode *inode = fdentry(file)->d_inode;
1804 struct btrfs_root *root = BTRFS_I(inode)->root;
1805 struct file *src_file;
1806 struct inode *src;
1807 struct btrfs_trans_handle *trans;
f46b5a66 1808 struct btrfs_path *path;
f46b5a66 1809 struct extent_buffer *leaf;
ae01a0ab
YZ
1810 char *buf;
1811 struct btrfs_key key;
f46b5a66
CH
1812 u32 nritems;
1813 int slot;
ae01a0ab 1814 int ret;
c5c9cd4d
SW
1815 u64 len = olen;
1816 u64 bs = root->fs_info->sb->s_blocksize;
1817 u64 hint_byte;
d20f7043 1818
c5c9cd4d
SW
1819 /*
1820 * TODO:
1821 * - split compressed inline extents. annoying: we need to
1822 * decompress into destination's address_space (the file offset
1823 * may change, so source mapping won't do), then recompress (or
1824 * otherwise reinsert) a subrange.
1825 * - allow ranges within the same file to be cloned (provided
1826 * they don't overlap)?
1827 */
1828
e441d54d 1829 /* the destination must be opened for writing */
2ebc3464 1830 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
e441d54d
CM
1831 return -EINVAL;
1832
b83cc969
LZ
1833 if (btrfs_root_readonly(root))
1834 return -EROFS;
1835
c146afad
YZ
1836 ret = mnt_want_write(file->f_path.mnt);
1837 if (ret)
1838 return ret;
1839
c5c9cd4d 1840 src_file = fget(srcfd);
ab67b7c1
YZ
1841 if (!src_file) {
1842 ret = -EBADF;
1843 goto out_drop_write;
1844 }
5dc64164 1845
f46b5a66
CH
1846 src = src_file->f_dentry->d_inode;
1847
c5c9cd4d
SW
1848 ret = -EINVAL;
1849 if (src == inode)
1850 goto out_fput;
1851
5dc64164
DR
1852 /* the src must be open for reading */
1853 if (!(src_file->f_mode & FMODE_READ))
1854 goto out_fput;
1855
ae01a0ab
YZ
1856 ret = -EISDIR;
1857 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1858 goto out_fput;
1859
f46b5a66 1860 ret = -EXDEV;
ae01a0ab
YZ
1861 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1862 goto out_fput;
1863
1864 ret = -ENOMEM;
1865 buf = vmalloc(btrfs_level_size(root, 0));
1866 if (!buf)
1867 goto out_fput;
1868
1869 path = btrfs_alloc_path();
1870 if (!path) {
1871 vfree(buf);
f46b5a66 1872 goto out_fput;
ae01a0ab
YZ
1873 }
1874 path->reada = 2;
f46b5a66
CH
1875
1876 if (inode < src) {
fccdae43
SW
1877 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1878 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
f46b5a66 1879 } else {
fccdae43
SW
1880 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1881 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
f46b5a66
CH
1882 }
1883
c5c9cd4d
SW
1884 /* determine range to clone */
1885 ret = -EINVAL;
2ebc3464 1886 if (off + len > src->i_size || off + len < off)
f46b5a66 1887 goto out_unlock;
c5c9cd4d
SW
1888 if (len == 0)
1889 olen = len = src->i_size - off;
1890 /* if we extend to eof, continue to block boundary */
1891 if (off + len == src->i_size)
2a6b8dae 1892 len = ALIGN(src->i_size, bs) - off;
c5c9cd4d
SW
1893
1894 /* verify the end result is block aligned */
2a6b8dae
LZ
1895 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1896 !IS_ALIGNED(destoff, bs))
c5c9cd4d
SW
1897 goto out_unlock;
1898
f46b5a66
CH
1899 /* do any pending delalloc/csum calc on src, one way or
1900 another, and lock file content */
1901 while (1) {
31840ae1 1902 struct btrfs_ordered_extent *ordered;
c5c9cd4d 1903 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
9a019196
SW
1904 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1905 if (!ordered &&
1906 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1907 EXTENT_DELALLOC, 0, NULL))
f46b5a66 1908 break;
c5c9cd4d 1909 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
ae01a0ab
YZ
1910 if (ordered)
1911 btrfs_put_ordered_extent(ordered);
9a019196 1912 btrfs_wait_ordered_range(src, off, len);
f46b5a66
CH
1913 }
1914
c5c9cd4d 1915 /* clone data */
f46b5a66 1916 key.objectid = src->i_ino;
ae01a0ab
YZ
1917 key.type = BTRFS_EXTENT_DATA_KEY;
1918 key.offset = 0;
f46b5a66
CH
1919
1920 while (1) {
1921 /*
1922 * note the key will change type as we walk through the
1923 * tree.
1924 */
a22285a6 1925 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
f46b5a66
CH
1926 if (ret < 0)
1927 goto out;
1928
ae01a0ab
YZ
1929 nritems = btrfs_header_nritems(path->nodes[0]);
1930 if (path->slots[0] >= nritems) {
f46b5a66
CH
1931 ret = btrfs_next_leaf(root, path);
1932 if (ret < 0)
1933 goto out;
1934 if (ret > 0)
1935 break;
ae01a0ab 1936 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
1937 }
1938 leaf = path->nodes[0];
1939 slot = path->slots[0];
f46b5a66 1940
ae01a0ab 1941 btrfs_item_key_to_cpu(leaf, &key, slot);
d20f7043 1942 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
f46b5a66
CH
1943 key.objectid != src->i_ino)
1944 break;
1945
c5c9cd4d
SW
1946 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1947 struct btrfs_file_extent_item *extent;
1948 int type;
31840ae1
ZY
1949 u32 size;
1950 struct btrfs_key new_key;
c5c9cd4d
SW
1951 u64 disko = 0, diskl = 0;
1952 u64 datao = 0, datal = 0;
1953 u8 comp;
b5384d48 1954 u64 endoff;
31840ae1
ZY
1955
1956 size = btrfs_item_size_nr(leaf, slot);
1957 read_extent_buffer(leaf, buf,
1958 btrfs_item_ptr_offset(leaf, slot),
1959 size);
c5c9cd4d
SW
1960
1961 extent = btrfs_item_ptr(leaf, slot,
1962 struct btrfs_file_extent_item);
1963 comp = btrfs_file_extent_compression(leaf, extent);
1964 type = btrfs_file_extent_type(leaf, extent);
c8a894d7
CM
1965 if (type == BTRFS_FILE_EXTENT_REG ||
1966 type == BTRFS_FILE_EXTENT_PREALLOC) {
d397712b
CM
1967 disko = btrfs_file_extent_disk_bytenr(leaf,
1968 extent);
1969 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1970 extent);
c5c9cd4d 1971 datao = btrfs_file_extent_offset(leaf, extent);
d397712b
CM
1972 datal = btrfs_file_extent_num_bytes(leaf,
1973 extent);
c5c9cd4d
SW
1974 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1975 /* take upper bound, may be compressed */
1976 datal = btrfs_file_extent_ram_bytes(leaf,
1977 extent);
1978 }
31840ae1
ZY
1979 btrfs_release_path(root, path);
1980
050006a7 1981 if (key.offset + datal <= off ||
c5c9cd4d
SW
1982 key.offset >= off+len)
1983 goto next;
1984
31840ae1
ZY
1985 memcpy(&new_key, &key, sizeof(new_key));
1986 new_key.objectid = inode->i_ino;
4d728ec7
LZ
1987 if (off <= key.offset)
1988 new_key.offset = key.offset + destoff - off;
1989 else
1990 new_key.offset = destoff;
31840ae1 1991
a22285a6
YZ
1992 trans = btrfs_start_transaction(root, 1);
1993 if (IS_ERR(trans)) {
1994 ret = PTR_ERR(trans);
1995 goto out;
1996 }
1997
c8a894d7
CM
1998 if (type == BTRFS_FILE_EXTENT_REG ||
1999 type == BTRFS_FILE_EXTENT_PREALLOC) {
a22285a6
YZ
2000 if (off > key.offset) {
2001 datao += off - key.offset;
2002 datal -= off - key.offset;
2003 }
2004
2005 if (key.offset + datal > off + len)
2006 datal = off + len - key.offset;
2007
2008 ret = btrfs_drop_extents(trans, inode,
2009 new_key.offset,
2010 new_key.offset + datal,
2011 &hint_byte, 1);
2012 BUG_ON(ret);
2013
c5c9cd4d
SW
2014 ret = btrfs_insert_empty_item(trans, root, path,
2015 &new_key, size);
a22285a6 2016 BUG_ON(ret);
c5c9cd4d
SW
2017
2018 leaf = path->nodes[0];
2019 slot = path->slots[0];
2020 write_extent_buffer(leaf, buf,
31840ae1
ZY
2021 btrfs_item_ptr_offset(leaf, slot),
2022 size);
ae01a0ab 2023
c5c9cd4d 2024 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 2025 struct btrfs_file_extent_item);
c5c9cd4d 2026
c5c9cd4d
SW
2027 /* disko == 0 means it's a hole */
2028 if (!disko)
2029 datao = 0;
c5c9cd4d
SW
2030
2031 btrfs_set_file_extent_offset(leaf, extent,
2032 datao);
2033 btrfs_set_file_extent_num_bytes(leaf, extent,
2034 datal);
2035 if (disko) {
2036 inode_add_bytes(inode, datal);
ae01a0ab 2037 ret = btrfs_inc_extent_ref(trans, root,
5d4f98a2
YZ
2038 disko, diskl, 0,
2039 root->root_key.objectid,
2040 inode->i_ino,
2041 new_key.offset - datao);
31840ae1 2042 BUG_ON(ret);
f46b5a66 2043 }
c5c9cd4d
SW
2044 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2045 u64 skip = 0;
2046 u64 trim = 0;
2047 if (off > key.offset) {
2048 skip = off - key.offset;
2049 new_key.offset += skip;
2050 }
d397712b 2051
c5c9cd4d
SW
2052 if (key.offset + datal > off+len)
2053 trim = key.offset + datal - (off+len);
d397712b 2054
c5c9cd4d 2055 if (comp && (skip || trim)) {
c5c9cd4d 2056 ret = -EINVAL;
a22285a6 2057 btrfs_end_transaction(trans, root);
c5c9cd4d
SW
2058 goto out;
2059 }
2060 size -= skip + trim;
2061 datal -= skip + trim;
a22285a6
YZ
2062
2063 ret = btrfs_drop_extents(trans, inode,
2064 new_key.offset,
2065 new_key.offset + datal,
2066 &hint_byte, 1);
2067 BUG_ON(ret);
2068
c5c9cd4d
SW
2069 ret = btrfs_insert_empty_item(trans, root, path,
2070 &new_key, size);
a22285a6 2071 BUG_ON(ret);
c5c9cd4d
SW
2072
2073 if (skip) {
d397712b
CM
2074 u32 start =
2075 btrfs_file_extent_calc_inline_size(0);
c5c9cd4d
SW
2076 memmove(buf+start, buf+start+skip,
2077 datal);
2078 }
2079
2080 leaf = path->nodes[0];
2081 slot = path->slots[0];
2082 write_extent_buffer(leaf, buf,
2083 btrfs_item_ptr_offset(leaf, slot),
2084 size);
2085 inode_add_bytes(inode, datal);
f46b5a66 2086 }
c5c9cd4d
SW
2087
2088 btrfs_mark_buffer_dirty(leaf);
a22285a6 2089 btrfs_release_path(root, path);
c5c9cd4d 2090
a22285a6 2091 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
b5384d48
SW
2092
2093 /*
2094 * we round up to the block size at eof when
2095 * determining which extents to clone above,
2096 * but shouldn't round up the file size
2097 */
2098 endoff = new_key.offset + datal;
5f3888ff
LZ
2099 if (endoff > destoff+olen)
2100 endoff = destoff+olen;
b5384d48
SW
2101 if (endoff > inode->i_size)
2102 btrfs_i_size_write(inode, endoff);
2103
a22285a6
YZ
2104 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2105 ret = btrfs_update_inode(trans, root, inode);
2106 BUG_ON(ret);
2107 btrfs_end_transaction(trans, root);
2108 }
d397712b 2109next:
31840ae1 2110 btrfs_release_path(root, path);
f46b5a66 2111 key.offset++;
f46b5a66 2112 }
f46b5a66
CH
2113 ret = 0;
2114out:
ae01a0ab 2115 btrfs_release_path(root, path);
c5c9cd4d 2116 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
f46b5a66
CH
2117out_unlock:
2118 mutex_unlock(&src->i_mutex);
2119 mutex_unlock(&inode->i_mutex);
ae01a0ab
YZ
2120 vfree(buf);
2121 btrfs_free_path(path);
f46b5a66
CH
2122out_fput:
2123 fput(src_file);
ab67b7c1
YZ
2124out_drop_write:
2125 mnt_drop_write(file->f_path.mnt);
f46b5a66
CH
2126 return ret;
2127}
2128
7a865e8a 2129static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
c5c9cd4d
SW
2130{
2131 struct btrfs_ioctl_clone_range_args args;
2132
7a865e8a 2133 if (copy_from_user(&args, argp, sizeof(args)))
c5c9cd4d
SW
2134 return -EFAULT;
2135 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2136 args.src_length, args.dest_offset);
2137}
2138
f46b5a66
CH
2139/*
2140 * there are many ways the trans_start and trans_end ioctls can lead
2141 * to deadlocks. They should only be used by applications that
2142 * basically own the machine, and have a very in depth understanding
2143 * of all the possible deadlocks and enospc problems.
2144 */
b2950863 2145static long btrfs_ioctl_trans_start(struct file *file)
f46b5a66
CH
2146{
2147 struct inode *inode = fdentry(file)->d_inode;
2148 struct btrfs_root *root = BTRFS_I(inode)->root;
2149 struct btrfs_trans_handle *trans;
1ab86aed 2150 int ret;
f46b5a66 2151
1ab86aed 2152 ret = -EPERM;
df5b5520 2153 if (!capable(CAP_SYS_ADMIN))
1ab86aed 2154 goto out;
df5b5520 2155
1ab86aed
SW
2156 ret = -EINPROGRESS;
2157 if (file->private_data)
f46b5a66 2158 goto out;
9ca9ee09 2159
b83cc969
LZ
2160 ret = -EROFS;
2161 if (btrfs_root_readonly(root))
2162 goto out;
2163
c146afad
YZ
2164 ret = mnt_want_write(file->f_path.mnt);
2165 if (ret)
2166 goto out;
2167
9ca9ee09
SW
2168 mutex_lock(&root->fs_info->trans_mutex);
2169 root->fs_info->open_ioctl_trans++;
2170 mutex_unlock(&root->fs_info->trans_mutex);
2171
1ab86aed 2172 ret = -ENOMEM;
9ca9ee09 2173 trans = btrfs_start_ioctl_transaction(root, 0);
abd30bb0 2174 if (IS_ERR(trans))
1ab86aed
SW
2175 goto out_drop;
2176
2177 file->private_data = trans;
2178 return 0;
2179
2180out_drop:
2181 mutex_lock(&root->fs_info->trans_mutex);
2182 root->fs_info->open_ioctl_trans--;
2183 mutex_unlock(&root->fs_info->trans_mutex);
2184 mnt_drop_write(file->f_path.mnt);
f46b5a66 2185out:
f46b5a66
CH
2186 return ret;
2187}
2188
6ef5ed0d
JB
2189static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2190{
2191 struct inode *inode = fdentry(file)->d_inode;
2192 struct btrfs_root *root = BTRFS_I(inode)->root;
2193 struct btrfs_root *new_root;
2194 struct btrfs_dir_item *di;
2195 struct btrfs_trans_handle *trans;
2196 struct btrfs_path *path;
2197 struct btrfs_key location;
2198 struct btrfs_disk_key disk_key;
2199 struct btrfs_super_block *disk_super;
2200 u64 features;
2201 u64 objectid = 0;
2202 u64 dir_id;
2203
2204 if (!capable(CAP_SYS_ADMIN))
2205 return -EPERM;
2206
2207 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2208 return -EFAULT;
2209
2210 if (!objectid)
2211 objectid = root->root_key.objectid;
2212
2213 location.objectid = objectid;
2214 location.type = BTRFS_ROOT_ITEM_KEY;
2215 location.offset = (u64)-1;
2216
2217 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2218 if (IS_ERR(new_root))
2219 return PTR_ERR(new_root);
2220
2221 if (btrfs_root_refs(&new_root->root_item) == 0)
2222 return -ENOENT;
2223
2224 path = btrfs_alloc_path();
2225 if (!path)
2226 return -ENOMEM;
2227 path->leave_spinning = 1;
2228
2229 trans = btrfs_start_transaction(root, 1);
98d5dc13 2230 if (IS_ERR(trans)) {
6ef5ed0d 2231 btrfs_free_path(path);
98d5dc13 2232 return PTR_ERR(trans);
6ef5ed0d
JB
2233 }
2234
2235 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2236 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2237 dir_id, "default", 7, 1);
cf1e99a4 2238 if (IS_ERR_OR_NULL(di)) {
6ef5ed0d
JB
2239 btrfs_free_path(path);
2240 btrfs_end_transaction(trans, root);
2241 printk(KERN_ERR "Umm, you don't have the default dir item, "
2242 "this isn't going to work\n");
2243 return -ENOENT;
2244 }
2245
2246 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2247 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2248 btrfs_mark_buffer_dirty(path->nodes[0]);
2249 btrfs_free_path(path);
2250
2251 disk_super = &root->fs_info->super_copy;
2252 features = btrfs_super_incompat_flags(disk_super);
2253 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2254 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2255 btrfs_set_super_incompat_flags(disk_super, features);
2256 }
2257 btrfs_end_transaction(trans, root);
2258
2259 return 0;
2260}
2261
bf5fc093
JB
2262static void get_block_group_info(struct list_head *groups_list,
2263 struct btrfs_ioctl_space_info *space)
2264{
2265 struct btrfs_block_group_cache *block_group;
2266
2267 space->total_bytes = 0;
2268 space->used_bytes = 0;
2269 space->flags = 0;
2270 list_for_each_entry(block_group, groups_list, list) {
2271 space->flags = block_group->flags;
2272 space->total_bytes += block_group->key.offset;
2273 space->used_bytes +=
2274 btrfs_block_group_used(&block_group->item);
2275 }
2276}
2277
1406e432
JB
2278long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2279{
2280 struct btrfs_ioctl_space_args space_args;
2281 struct btrfs_ioctl_space_info space;
2282 struct btrfs_ioctl_space_info *dest;
7fde62bf
CM
2283 struct btrfs_ioctl_space_info *dest_orig;
2284 struct btrfs_ioctl_space_info *user_dest;
1406e432 2285 struct btrfs_space_info *info;
bf5fc093
JB
2286 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2287 BTRFS_BLOCK_GROUP_SYSTEM,
2288 BTRFS_BLOCK_GROUP_METADATA,
2289 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2290 int num_types = 4;
7fde62bf 2291 int alloc_size;
1406e432 2292 int ret = 0;
51788b1b 2293 u64 slot_count = 0;
bf5fc093 2294 int i, c;
1406e432
JB
2295
2296 if (copy_from_user(&space_args,
2297 (struct btrfs_ioctl_space_args __user *)arg,
2298 sizeof(space_args)))
2299 return -EFAULT;
2300
bf5fc093
JB
2301 for (i = 0; i < num_types; i++) {
2302 struct btrfs_space_info *tmp;
2303
2304 info = NULL;
2305 rcu_read_lock();
2306 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2307 list) {
2308 if (tmp->flags == types[i]) {
2309 info = tmp;
2310 break;
2311 }
2312 }
2313 rcu_read_unlock();
2314
2315 if (!info)
2316 continue;
2317
2318 down_read(&info->groups_sem);
2319 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2320 if (!list_empty(&info->block_groups[c]))
2321 slot_count++;
2322 }
2323 up_read(&info->groups_sem);
2324 }
7fde62bf
CM
2325
2326 /* space_slots == 0 means they are asking for a count */
2327 if (space_args.space_slots == 0) {
2328 space_args.total_spaces = slot_count;
2329 goto out;
2330 }
bf5fc093 2331
51788b1b 2332 slot_count = min_t(u64, space_args.space_slots, slot_count);
bf5fc093 2333
7fde62bf 2334 alloc_size = sizeof(*dest) * slot_count;
bf5fc093 2335
7fde62bf
CM
2336 /* we generally have at most 6 or so space infos, one for each raid
2337 * level. So, a whole page should be more than enough for everyone
2338 */
2339 if (alloc_size > PAGE_CACHE_SIZE)
2340 return -ENOMEM;
2341
1406e432 2342 space_args.total_spaces = 0;
7fde62bf
CM
2343 dest = kmalloc(alloc_size, GFP_NOFS);
2344 if (!dest)
2345 return -ENOMEM;
2346 dest_orig = dest;
1406e432 2347
7fde62bf 2348 /* now we have a buffer to copy into */
bf5fc093
JB
2349 for (i = 0; i < num_types; i++) {
2350 struct btrfs_space_info *tmp;
2351
51788b1b
DR
2352 if (!slot_count)
2353 break;
2354
bf5fc093
JB
2355 info = NULL;
2356 rcu_read_lock();
2357 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2358 list) {
2359 if (tmp->flags == types[i]) {
2360 info = tmp;
2361 break;
2362 }
2363 }
2364 rcu_read_unlock();
7fde62bf 2365
bf5fc093
JB
2366 if (!info)
2367 continue;
2368 down_read(&info->groups_sem);
2369 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2370 if (!list_empty(&info->block_groups[c])) {
2371 get_block_group_info(&info->block_groups[c],
2372 &space);
2373 memcpy(dest, &space, sizeof(space));
2374 dest++;
2375 space_args.total_spaces++;
51788b1b 2376 slot_count--;
bf5fc093 2377 }
51788b1b
DR
2378 if (!slot_count)
2379 break;
bf5fc093
JB
2380 }
2381 up_read(&info->groups_sem);
1406e432 2382 }
1406e432 2383
7fde62bf
CM
2384 user_dest = (struct btrfs_ioctl_space_info *)
2385 (arg + sizeof(struct btrfs_ioctl_space_args));
2386
2387 if (copy_to_user(user_dest, dest_orig, alloc_size))
2388 ret = -EFAULT;
2389
2390 kfree(dest_orig);
2391out:
2392 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1406e432
JB
2393 ret = -EFAULT;
2394
2395 return ret;
2396}
2397
f46b5a66
CH
2398/*
2399 * there are many ways the trans_start and trans_end ioctls can lead
2400 * to deadlocks. They should only be used by applications that
2401 * basically own the machine, and have a very in depth understanding
2402 * of all the possible deadlocks and enospc problems.
2403 */
2404long btrfs_ioctl_trans_end(struct file *file)
2405{
2406 struct inode *inode = fdentry(file)->d_inode;
2407 struct btrfs_root *root = BTRFS_I(inode)->root;
2408 struct btrfs_trans_handle *trans;
f46b5a66 2409
f46b5a66 2410 trans = file->private_data;
1ab86aed
SW
2411 if (!trans)
2412 return -EINVAL;
b214107e 2413 file->private_data = NULL;
9ca9ee09 2414
1ab86aed
SW
2415 btrfs_end_transaction(trans, root);
2416
9ca9ee09
SW
2417 mutex_lock(&root->fs_info->trans_mutex);
2418 root->fs_info->open_ioctl_trans--;
2419 mutex_unlock(&root->fs_info->trans_mutex);
2420
cfc8ea87 2421 mnt_drop_write(file->f_path.mnt);
1ab86aed 2422 return 0;
f46b5a66
CH
2423}
2424
46204592
SW
2425static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2426{
2427 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2428 struct btrfs_trans_handle *trans;
2429 u64 transid;
db5b493a 2430 int ret;
46204592
SW
2431
2432 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
2433 if (IS_ERR(trans))
2434 return PTR_ERR(trans);
46204592 2435 transid = trans->transid;
db5b493a
TI
2436 ret = btrfs_commit_transaction_async(trans, root, 0);
2437 if (ret)
2438 return ret;
46204592
SW
2439
2440 if (argp)
2441 if (copy_to_user(argp, &transid, sizeof(transid)))
2442 return -EFAULT;
2443 return 0;
2444}
2445
2446static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2447{
2448 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2449 u64 transid;
2450
2451 if (argp) {
2452 if (copy_from_user(&transid, argp, sizeof(transid)))
2453 return -EFAULT;
2454 } else {
2455 transid = 0; /* current trans */
2456 }
2457 return btrfs_wait_for_commit(root, transid);
2458}
2459
f46b5a66
CH
2460long btrfs_ioctl(struct file *file, unsigned int
2461 cmd, unsigned long arg)
2462{
2463 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4bcabaa3 2464 void __user *argp = (void __user *)arg;
f46b5a66
CH
2465
2466 switch (cmd) {
6cbff00f
CH
2467 case FS_IOC_GETFLAGS:
2468 return btrfs_ioctl_getflags(file, argp);
2469 case FS_IOC_SETFLAGS:
2470 return btrfs_ioctl_setflags(file, argp);
2471 case FS_IOC_GETVERSION:
2472 return btrfs_ioctl_getversion(file, argp);
f7039b1d
LD
2473 case FITRIM:
2474 return btrfs_ioctl_fitrim(file, argp);
f46b5a66 2475 case BTRFS_IOC_SNAP_CREATE:
fa0d2b9b 2476 return btrfs_ioctl_snap_create(file, argp, 0);
fdfb1e4f 2477 case BTRFS_IOC_SNAP_CREATE_V2:
fa0d2b9b 2478 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3de4586c 2479 case BTRFS_IOC_SUBVOL_CREATE:
fa0d2b9b 2480 return btrfs_ioctl_snap_create(file, argp, 1);
76dda93c
YZ
2481 case BTRFS_IOC_SNAP_DESTROY:
2482 return btrfs_ioctl_snap_destroy(file, argp);
0caa102d
LZ
2483 case BTRFS_IOC_SUBVOL_GETFLAGS:
2484 return btrfs_ioctl_subvol_getflags(file, argp);
2485 case BTRFS_IOC_SUBVOL_SETFLAGS:
2486 return btrfs_ioctl_subvol_setflags(file, argp);
6ef5ed0d
JB
2487 case BTRFS_IOC_DEFAULT_SUBVOL:
2488 return btrfs_ioctl_default_subvol(file, argp);
f46b5a66 2489 case BTRFS_IOC_DEFRAG:
1e701a32
CM
2490 return btrfs_ioctl_defrag(file, NULL);
2491 case BTRFS_IOC_DEFRAG_RANGE:
2492 return btrfs_ioctl_defrag(file, argp);
f46b5a66 2493 case BTRFS_IOC_RESIZE:
4bcabaa3 2494 return btrfs_ioctl_resize(root, argp);
f46b5a66 2495 case BTRFS_IOC_ADD_DEV:
4bcabaa3 2496 return btrfs_ioctl_add_dev(root, argp);
f46b5a66 2497 case BTRFS_IOC_RM_DEV:
4bcabaa3 2498 return btrfs_ioctl_rm_dev(root, argp);
f46b5a66
CH
2499 case BTRFS_IOC_BALANCE:
2500 return btrfs_balance(root->fs_info->dev_root);
2501 case BTRFS_IOC_CLONE:
c5c9cd4d
SW
2502 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2503 case BTRFS_IOC_CLONE_RANGE:
7a865e8a 2504 return btrfs_ioctl_clone_range(file, argp);
f46b5a66
CH
2505 case BTRFS_IOC_TRANS_START:
2506 return btrfs_ioctl_trans_start(file);
2507 case BTRFS_IOC_TRANS_END:
2508 return btrfs_ioctl_trans_end(file);
ac8e9819
CM
2509 case BTRFS_IOC_TREE_SEARCH:
2510 return btrfs_ioctl_tree_search(file, argp);
2511 case BTRFS_IOC_INO_LOOKUP:
2512 return btrfs_ioctl_ino_lookup(file, argp);
1406e432
JB
2513 case BTRFS_IOC_SPACE_INFO:
2514 return btrfs_ioctl_space_info(root, argp);
f46b5a66
CH
2515 case BTRFS_IOC_SYNC:
2516 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2517 return 0;
46204592
SW
2518 case BTRFS_IOC_START_SYNC:
2519 return btrfs_ioctl_start_sync(file, argp);
2520 case BTRFS_IOC_WAIT_SYNC:
2521 return btrfs_ioctl_wait_sync(file, argp);
f46b5a66
CH
2522 }
2523
2524 return -ENOTTY;
2525}
This page took 0.218118 seconds and 5 git commands to generate.