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