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