Btrfs: add mount -o auto_defrag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / super.c
blob28e3cb2607ff4155589d17ff7575768a2c84e467
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/blkdev.h>
20 #include <linux/module.h>
21 #include <linux/buffer_head.h>
22 #include <linux/fs.h>
23 #include <linux/pagemap.h>
24 #include <linux/highmem.h>
25 #include <linux/time.h>
26 #include <linux/init.h>
27 #include <linux/seq_file.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mount.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/parser.h>
37 #include <linux/ctype.h>
38 #include <linux/namei.h>
39 #include <linux/miscdevice.h>
40 #include <linux/magic.h>
41 #include <linux/slab.h>
42 #include "compat.h"
43 #include "delayed-inode.h"
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 "xattr.h"
51 #include "volumes.h"
52 #include "version.h"
53 #include "export.h"
54 #include "compression.h"
56 #define CREATE_TRACE_POINTS
57 #include <trace/events/btrfs.h>
59 static const struct super_operations btrfs_super_ops;
61 static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
62 char nbuf[16])
64 char *errstr = NULL;
66 switch (errno) {
67 case -EIO:
68 errstr = "IO failure";
69 break;
70 case -ENOMEM:
71 errstr = "Out of memory";
72 break;
73 case -EROFS:
74 errstr = "Readonly filesystem";
75 break;
76 default:
77 if (nbuf) {
78 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
79 errstr = nbuf;
81 break;
84 return errstr;
87 static void __save_error_info(struct btrfs_fs_info *fs_info)
90 * today we only save the error info into ram. Long term we'll
91 * also send it down to the disk
93 fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
96 /* NOTE:
97 * We move write_super stuff at umount in order to avoid deadlock
98 * for umount hold all lock.
100 static void save_error_info(struct btrfs_fs_info *fs_info)
102 __save_error_info(fs_info);
105 /* btrfs handle error by forcing the filesystem readonly */
106 static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
108 struct super_block *sb = fs_info->sb;
110 if (sb->s_flags & MS_RDONLY)
111 return;
113 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
114 sb->s_flags |= MS_RDONLY;
115 printk(KERN_INFO "btrfs is forced readonly\n");
120 * __btrfs_std_error decodes expected errors from the caller and
121 * invokes the approciate error response.
123 void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
124 unsigned int line, int errno)
126 struct super_block *sb = fs_info->sb;
127 char nbuf[16];
128 const char *errstr;
131 * Special case: if the error is EROFS, and we're already
132 * under MS_RDONLY, then it is safe here.
134 if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
135 return;
137 errstr = btrfs_decode_error(fs_info, errno, nbuf);
138 printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
139 sb->s_id, function, line, errstr);
140 save_error_info(fs_info);
142 btrfs_handle_error(fs_info);
145 static void btrfs_put_super(struct super_block *sb)
147 struct btrfs_root *root = btrfs_sb(sb);
148 int ret;
150 ret = close_ctree(root);
151 sb->s_fs_info = NULL;
153 (void)ret; /* FIXME: need to fix VFS to return error? */
156 enum {
157 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
158 Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
159 Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
160 Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
161 Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
162 Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
163 Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_err,
166 static match_table_t tokens = {
167 {Opt_degraded, "degraded"},
168 {Opt_subvol, "subvol=%s"},
169 {Opt_subvolid, "subvolid=%d"},
170 {Opt_device, "device=%s"},
171 {Opt_nodatasum, "nodatasum"},
172 {Opt_nodatacow, "nodatacow"},
173 {Opt_nobarrier, "nobarrier"},
174 {Opt_max_inline, "max_inline=%s"},
175 {Opt_alloc_start, "alloc_start=%s"},
176 {Opt_thread_pool, "thread_pool=%d"},
177 {Opt_compress, "compress"},
178 {Opt_compress_type, "compress=%s"},
179 {Opt_compress_force, "compress-force"},
180 {Opt_compress_force_type, "compress-force=%s"},
181 {Opt_ssd, "ssd"},
182 {Opt_ssd_spread, "ssd_spread"},
183 {Opt_nossd, "nossd"},
184 {Opt_noacl, "noacl"},
185 {Opt_notreelog, "notreelog"},
186 {Opt_flushoncommit, "flushoncommit"},
187 {Opt_ratio, "metadata_ratio=%d"},
188 {Opt_discard, "discard"},
189 {Opt_space_cache, "space_cache"},
190 {Opt_clear_cache, "clear_cache"},
191 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
192 {Opt_enospc_debug, "enospc_debug"},
193 {Opt_subvolrootid, "subvolrootid=%d"},
194 {Opt_defrag, "autodefrag"},
195 {Opt_err, NULL},
199 * Regular mount options parser. Everything that is needed only when
200 * reading in a new superblock is parsed here.
202 int btrfs_parse_options(struct btrfs_root *root, char *options)
204 struct btrfs_fs_info *info = root->fs_info;
205 substring_t args[MAX_OPT_ARGS];
206 char *p, *num, *orig;
207 int intarg;
208 int ret = 0;
209 char *compress_type;
210 bool compress_force = false;
212 if (!options)
213 return 0;
216 * strsep changes the string, duplicate it because parse_options
217 * gets called twice
219 options = kstrdup(options, GFP_NOFS);
220 if (!options)
221 return -ENOMEM;
223 orig = options;
225 while ((p = strsep(&options, ",")) != NULL) {
226 int token;
227 if (!*p)
228 continue;
230 token = match_token(p, tokens, args);
231 switch (token) {
232 case Opt_degraded:
233 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
234 btrfs_set_opt(info->mount_opt, DEGRADED);
235 break;
236 case Opt_subvol:
237 case Opt_subvolid:
238 case Opt_subvolrootid:
239 case Opt_device:
241 * These are parsed by btrfs_parse_early_options
242 * and can be happily ignored here.
244 break;
245 case Opt_nodatasum:
246 printk(KERN_INFO "btrfs: setting nodatasum\n");
247 btrfs_set_opt(info->mount_opt, NODATASUM);
248 break;
249 case Opt_nodatacow:
250 printk(KERN_INFO "btrfs: setting nodatacow\n");
251 btrfs_set_opt(info->mount_opt, NODATACOW);
252 btrfs_set_opt(info->mount_opt, NODATASUM);
253 break;
254 case Opt_compress_force:
255 case Opt_compress_force_type:
256 compress_force = true;
257 case Opt_compress:
258 case Opt_compress_type:
259 if (token == Opt_compress ||
260 token == Opt_compress_force ||
261 strcmp(args[0].from, "zlib") == 0) {
262 compress_type = "zlib";
263 info->compress_type = BTRFS_COMPRESS_ZLIB;
264 } else if (strcmp(args[0].from, "lzo") == 0) {
265 compress_type = "lzo";
266 info->compress_type = BTRFS_COMPRESS_LZO;
267 } else {
268 ret = -EINVAL;
269 goto out;
272 btrfs_set_opt(info->mount_opt, COMPRESS);
273 if (compress_force) {
274 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
275 pr_info("btrfs: force %s compression\n",
276 compress_type);
277 } else
278 pr_info("btrfs: use %s compression\n",
279 compress_type);
280 break;
281 case Opt_ssd:
282 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
283 btrfs_set_opt(info->mount_opt, SSD);
284 break;
285 case Opt_ssd_spread:
286 printk(KERN_INFO "btrfs: use spread ssd "
287 "allocation scheme\n");
288 btrfs_set_opt(info->mount_opt, SSD);
289 btrfs_set_opt(info->mount_opt, SSD_SPREAD);
290 break;
291 case Opt_nossd:
292 printk(KERN_INFO "btrfs: not using ssd allocation "
293 "scheme\n");
294 btrfs_set_opt(info->mount_opt, NOSSD);
295 btrfs_clear_opt(info->mount_opt, SSD);
296 btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
297 break;
298 case Opt_nobarrier:
299 printk(KERN_INFO "btrfs: turning off barriers\n");
300 btrfs_set_opt(info->mount_opt, NOBARRIER);
301 break;
302 case Opt_thread_pool:
303 intarg = 0;
304 match_int(&args[0], &intarg);
305 if (intarg) {
306 info->thread_pool_size = intarg;
307 printk(KERN_INFO "btrfs: thread pool %d\n",
308 info->thread_pool_size);
310 break;
311 case Opt_max_inline:
312 num = match_strdup(&args[0]);
313 if (num) {
314 info->max_inline = memparse(num, NULL);
315 kfree(num);
317 if (info->max_inline) {
318 info->max_inline = max_t(u64,
319 info->max_inline,
320 root->sectorsize);
322 printk(KERN_INFO "btrfs: max_inline at %llu\n",
323 (unsigned long long)info->max_inline);
325 break;
326 case Opt_alloc_start:
327 num = match_strdup(&args[0]);
328 if (num) {
329 info->alloc_start = memparse(num, NULL);
330 kfree(num);
331 printk(KERN_INFO
332 "btrfs: allocations start at %llu\n",
333 (unsigned long long)info->alloc_start);
335 break;
336 case Opt_noacl:
337 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
338 break;
339 case Opt_notreelog:
340 printk(KERN_INFO "btrfs: disabling tree log\n");
341 btrfs_set_opt(info->mount_opt, NOTREELOG);
342 break;
343 case Opt_flushoncommit:
344 printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
345 btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
346 break;
347 case Opt_ratio:
348 intarg = 0;
349 match_int(&args[0], &intarg);
350 if (intarg) {
351 info->metadata_ratio = intarg;
352 printk(KERN_INFO "btrfs: metadata ratio %d\n",
353 info->metadata_ratio);
355 break;
356 case Opt_discard:
357 btrfs_set_opt(info->mount_opt, DISCARD);
358 break;
359 case Opt_space_cache:
360 printk(KERN_INFO "btrfs: enabling disk space caching\n");
361 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
362 break;
363 case Opt_clear_cache:
364 printk(KERN_INFO "btrfs: force clearing of disk cache\n");
365 btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
366 break;
367 case Opt_user_subvol_rm_allowed:
368 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
369 break;
370 case Opt_enospc_debug:
371 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
372 break;
373 case Opt_defrag:
374 printk(KERN_INFO "btrfs: enabling auto defrag");
375 btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
376 break;
377 case Opt_err:
378 printk(KERN_INFO "btrfs: unrecognized mount option "
379 "'%s'\n", p);
380 ret = -EINVAL;
381 goto out;
382 default:
383 break;
386 out:
387 kfree(orig);
388 return ret;
392 * Parse mount options that are required early in the mount process.
394 * All other options will be parsed on much later in the mount process and
395 * only when we need to allocate a new super block.
397 static int btrfs_parse_early_options(const char *options, fmode_t flags,
398 void *holder, char **subvol_name, u64 *subvol_objectid,
399 u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
401 substring_t args[MAX_OPT_ARGS];
402 char *opts, *orig, *p;
403 int error = 0;
404 int intarg;
406 if (!options)
407 goto out;
410 * strsep changes the string, duplicate it because parse_options
411 * gets called twice
413 opts = kstrdup(options, GFP_KERNEL);
414 if (!opts)
415 return -ENOMEM;
416 orig = opts;
418 while ((p = strsep(&opts, ",")) != NULL) {
419 int token;
420 if (!*p)
421 continue;
423 token = match_token(p, tokens, args);
424 switch (token) {
425 case Opt_subvol:
426 *subvol_name = match_strdup(&args[0]);
427 break;
428 case Opt_subvolid:
429 intarg = 0;
430 error = match_int(&args[0], &intarg);
431 if (!error) {
432 /* we want the original fs_tree */
433 if (!intarg)
434 *subvol_objectid =
435 BTRFS_FS_TREE_OBJECTID;
436 else
437 *subvol_objectid = intarg;
439 break;
440 case Opt_subvolrootid:
441 intarg = 0;
442 error = match_int(&args[0], &intarg);
443 if (!error) {
444 /* we want the original fs_tree */
445 if (!intarg)
446 *subvol_rootid =
447 BTRFS_FS_TREE_OBJECTID;
448 else
449 *subvol_rootid = intarg;
451 break;
452 case Opt_device:
453 error = btrfs_scan_one_device(match_strdup(&args[0]),
454 flags, holder, fs_devices);
455 if (error)
456 goto out_free_opts;
457 break;
458 default:
459 break;
463 out_free_opts:
464 kfree(orig);
465 out:
467 * If no subvolume name is specified we use the default one. Allocate
468 * a copy of the string "." here so that code later in the
469 * mount path doesn't care if it's the default volume or another one.
471 if (!*subvol_name) {
472 *subvol_name = kstrdup(".", GFP_KERNEL);
473 if (!*subvol_name)
474 return -ENOMEM;
476 return error;
479 static struct dentry *get_default_root(struct super_block *sb,
480 u64 subvol_objectid)
482 struct btrfs_root *root = sb->s_fs_info;
483 struct btrfs_root *new_root;
484 struct btrfs_dir_item *di;
485 struct btrfs_path *path;
486 struct btrfs_key location;
487 struct inode *inode;
488 struct dentry *dentry;
489 u64 dir_id;
490 int new = 0;
493 * We have a specific subvol we want to mount, just setup location and
494 * go look up the root.
496 if (subvol_objectid) {
497 location.objectid = subvol_objectid;
498 location.type = BTRFS_ROOT_ITEM_KEY;
499 location.offset = (u64)-1;
500 goto find_root;
503 path = btrfs_alloc_path();
504 if (!path)
505 return ERR_PTR(-ENOMEM);
506 path->leave_spinning = 1;
509 * Find the "default" dir item which points to the root item that we
510 * will mount by default if we haven't been given a specific subvolume
511 * to mount.
513 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
514 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
515 if (IS_ERR(di)) {
516 btrfs_free_path(path);
517 return ERR_CAST(di);
519 if (!di) {
521 * Ok the default dir item isn't there. This is weird since
522 * it's always been there, but don't freak out, just try and
523 * mount to root most subvolume.
525 btrfs_free_path(path);
526 dir_id = BTRFS_FIRST_FREE_OBJECTID;
527 new_root = root->fs_info->fs_root;
528 goto setup_root;
531 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
532 btrfs_free_path(path);
534 find_root:
535 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
536 if (IS_ERR(new_root))
537 return ERR_CAST(new_root);
539 if (btrfs_root_refs(&new_root->root_item) == 0)
540 return ERR_PTR(-ENOENT);
542 dir_id = btrfs_root_dirid(&new_root->root_item);
543 setup_root:
544 location.objectid = dir_id;
545 location.type = BTRFS_INODE_ITEM_KEY;
546 location.offset = 0;
548 inode = btrfs_iget(sb, &location, new_root, &new);
549 if (IS_ERR(inode))
550 return ERR_CAST(inode);
553 * If we're just mounting the root most subvol put the inode and return
554 * a reference to the dentry. We will have already gotten a reference
555 * to the inode in btrfs_fill_super so we're good to go.
557 if (!new && sb->s_root->d_inode == inode) {
558 iput(inode);
559 return dget(sb->s_root);
562 if (new) {
563 const struct qstr name = { .name = "/", .len = 1 };
566 * New inode, we need to make the dentry a sibling of s_root so
567 * everything gets cleaned up properly on unmount.
569 dentry = d_alloc(sb->s_root, &name);
570 if (!dentry) {
571 iput(inode);
572 return ERR_PTR(-ENOMEM);
574 d_splice_alias(inode, dentry);
575 } else {
577 * We found the inode in cache, just find a dentry for it and
578 * put the reference to the inode we just got.
580 dentry = d_find_alias(inode);
581 iput(inode);
584 return dentry;
587 static int btrfs_fill_super(struct super_block *sb,
588 struct btrfs_fs_devices *fs_devices,
589 void *data, int silent)
591 struct inode *inode;
592 struct dentry *root_dentry;
593 struct btrfs_root *tree_root;
594 struct btrfs_key key;
595 int err;
597 sb->s_maxbytes = MAX_LFS_FILESIZE;
598 sb->s_magic = BTRFS_SUPER_MAGIC;
599 sb->s_op = &btrfs_super_ops;
600 sb->s_d_op = &btrfs_dentry_operations;
601 sb->s_export_op = &btrfs_export_ops;
602 sb->s_xattr = btrfs_xattr_handlers;
603 sb->s_time_gran = 1;
604 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
605 sb->s_flags |= MS_POSIXACL;
606 #endif
608 tree_root = open_ctree(sb, fs_devices, (char *)data);
610 if (IS_ERR(tree_root)) {
611 printk("btrfs: open_ctree failed\n");
612 return PTR_ERR(tree_root);
614 sb->s_fs_info = tree_root;
616 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
617 key.type = BTRFS_INODE_ITEM_KEY;
618 key.offset = 0;
619 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
620 if (IS_ERR(inode)) {
621 err = PTR_ERR(inode);
622 goto fail_close;
625 root_dentry = d_alloc_root(inode);
626 if (!root_dentry) {
627 iput(inode);
628 err = -ENOMEM;
629 goto fail_close;
632 sb->s_root = root_dentry;
634 save_mount_options(sb, data);
635 return 0;
637 fail_close:
638 close_ctree(tree_root);
639 return err;
642 int btrfs_sync_fs(struct super_block *sb, int wait)
644 struct btrfs_trans_handle *trans;
645 struct btrfs_root *root = btrfs_sb(sb);
646 int ret;
648 trace_btrfs_sync_fs(wait);
650 if (!wait) {
651 filemap_flush(root->fs_info->btree_inode->i_mapping);
652 return 0;
655 btrfs_start_delalloc_inodes(root, 0);
656 btrfs_wait_ordered_extents(root, 0, 0);
658 trans = btrfs_start_transaction(root, 0);
659 if (IS_ERR(trans))
660 return PTR_ERR(trans);
661 ret = btrfs_commit_transaction(trans, root);
662 return ret;
665 static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
667 struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
668 struct btrfs_fs_info *info = root->fs_info;
669 char *compress_type;
671 if (btrfs_test_opt(root, DEGRADED))
672 seq_puts(seq, ",degraded");
673 if (btrfs_test_opt(root, NODATASUM))
674 seq_puts(seq, ",nodatasum");
675 if (btrfs_test_opt(root, NODATACOW))
676 seq_puts(seq, ",nodatacow");
677 if (btrfs_test_opt(root, NOBARRIER))
678 seq_puts(seq, ",nobarrier");
679 if (info->max_inline != 8192 * 1024)
680 seq_printf(seq, ",max_inline=%llu",
681 (unsigned long long)info->max_inline);
682 if (info->alloc_start != 0)
683 seq_printf(seq, ",alloc_start=%llu",
684 (unsigned long long)info->alloc_start);
685 if (info->thread_pool_size != min_t(unsigned long,
686 num_online_cpus() + 2, 8))
687 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
688 if (btrfs_test_opt(root, COMPRESS)) {
689 if (info->compress_type == BTRFS_COMPRESS_ZLIB)
690 compress_type = "zlib";
691 else
692 compress_type = "lzo";
693 if (btrfs_test_opt(root, FORCE_COMPRESS))
694 seq_printf(seq, ",compress-force=%s", compress_type);
695 else
696 seq_printf(seq, ",compress=%s", compress_type);
698 if (btrfs_test_opt(root, NOSSD))
699 seq_puts(seq, ",nossd");
700 if (btrfs_test_opt(root, SSD_SPREAD))
701 seq_puts(seq, ",ssd_spread");
702 else if (btrfs_test_opt(root, SSD))
703 seq_puts(seq, ",ssd");
704 if (btrfs_test_opt(root, NOTREELOG))
705 seq_puts(seq, ",notreelog");
706 if (btrfs_test_opt(root, FLUSHONCOMMIT))
707 seq_puts(seq, ",flushoncommit");
708 if (btrfs_test_opt(root, DISCARD))
709 seq_puts(seq, ",discard");
710 if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
711 seq_puts(seq, ",noacl");
712 if (btrfs_test_opt(root, SPACE_CACHE))
713 seq_puts(seq, ",space_cache");
714 if (btrfs_test_opt(root, CLEAR_CACHE))
715 seq_puts(seq, ",clear_cache");
716 if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
717 seq_puts(seq, ",user_subvol_rm_allowed");
718 return 0;
721 static int btrfs_test_super(struct super_block *s, void *data)
723 struct btrfs_root *test_root = data;
724 struct btrfs_root *root = btrfs_sb(s);
727 * If this super block is going away, return false as it
728 * can't match as an existing super block.
730 if (!atomic_read(&s->s_active))
731 return 0;
732 return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
735 static int btrfs_set_super(struct super_block *s, void *data)
737 s->s_fs_info = data;
739 return set_anon_super(s, data);
744 * Find a superblock for the given device / mount point.
746 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
747 * for multiple device setup. Make sure to keep it in sync.
749 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
750 const char *device_name, void *data)
752 struct block_device *bdev = NULL;
753 struct super_block *s;
754 struct dentry *root;
755 struct btrfs_fs_devices *fs_devices = NULL;
756 struct btrfs_root *tree_root = NULL;
757 struct btrfs_fs_info *fs_info = NULL;
758 fmode_t mode = FMODE_READ;
759 char *subvol_name = NULL;
760 u64 subvol_objectid = 0;
761 u64 subvol_rootid = 0;
762 int error = 0;
764 if (!(flags & MS_RDONLY))
765 mode |= FMODE_WRITE;
767 error = btrfs_parse_early_options(data, mode, fs_type,
768 &subvol_name, &subvol_objectid,
769 &subvol_rootid, &fs_devices);
770 if (error)
771 return ERR_PTR(error);
773 error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
774 if (error)
775 goto error_free_subvol_name;
777 error = btrfs_open_devices(fs_devices, mode, fs_type);
778 if (error)
779 goto error_free_subvol_name;
781 if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
782 error = -EACCES;
783 goto error_close_devices;
787 * Setup a dummy root and fs_info for test/set super. This is because
788 * we don't actually fill this stuff out until open_ctree, but we need
789 * it for searching for existing supers, so this lets us do that and
790 * then open_ctree will properly initialize everything later.
792 fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
793 tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
794 if (!fs_info || !tree_root) {
795 error = -ENOMEM;
796 goto error_close_devices;
798 fs_info->tree_root = tree_root;
799 fs_info->fs_devices = fs_devices;
800 tree_root->fs_info = fs_info;
802 bdev = fs_devices->latest_bdev;
803 s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
804 if (IS_ERR(s))
805 goto error_s;
807 if (s->s_root) {
808 if ((flags ^ s->s_flags) & MS_RDONLY) {
809 deactivate_locked_super(s);
810 error = -EBUSY;
811 goto error_close_devices;
814 btrfs_close_devices(fs_devices);
815 kfree(fs_info);
816 kfree(tree_root);
817 } else {
818 char b[BDEVNAME_SIZE];
820 s->s_flags = flags;
821 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
822 error = btrfs_fill_super(s, fs_devices, data,
823 flags & MS_SILENT ? 1 : 0);
824 if (error) {
825 deactivate_locked_super(s);
826 goto error_free_subvol_name;
829 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
830 s->s_flags |= MS_ACTIVE;
833 /* if they gave us a subvolume name bind mount into that */
834 if (strcmp(subvol_name, ".")) {
835 struct dentry *new_root;
837 root = get_default_root(s, subvol_rootid);
838 if (IS_ERR(root)) {
839 error = PTR_ERR(root);
840 deactivate_locked_super(s);
841 goto error_free_subvol_name;
844 mutex_lock(&root->d_inode->i_mutex);
845 new_root = lookup_one_len(subvol_name, root,
846 strlen(subvol_name));
847 mutex_unlock(&root->d_inode->i_mutex);
849 if (IS_ERR(new_root)) {
850 dput(root);
851 deactivate_locked_super(s);
852 error = PTR_ERR(new_root);
853 goto error_free_subvol_name;
855 if (!new_root->d_inode) {
856 dput(root);
857 dput(new_root);
858 deactivate_locked_super(s);
859 error = -ENXIO;
860 goto error_free_subvol_name;
862 dput(root);
863 root = new_root;
864 } else {
865 root = get_default_root(s, subvol_objectid);
866 if (IS_ERR(root)) {
867 error = PTR_ERR(root);
868 deactivate_locked_super(s);
869 goto error_free_subvol_name;
873 kfree(subvol_name);
874 return root;
876 error_s:
877 error = PTR_ERR(s);
878 error_close_devices:
879 btrfs_close_devices(fs_devices);
880 kfree(fs_info);
881 kfree(tree_root);
882 error_free_subvol_name:
883 kfree(subvol_name);
884 return ERR_PTR(error);
887 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
889 struct btrfs_root *root = btrfs_sb(sb);
890 int ret;
892 ret = btrfs_parse_options(root, data);
893 if (ret)
894 return -EINVAL;
896 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
897 return 0;
899 if (*flags & MS_RDONLY) {
900 sb->s_flags |= MS_RDONLY;
902 ret = btrfs_commit_super(root);
903 WARN_ON(ret);
904 } else {
905 if (root->fs_info->fs_devices->rw_devices == 0)
906 return -EACCES;
908 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
909 return -EINVAL;
911 ret = btrfs_cleanup_fs_roots(root->fs_info);
912 WARN_ON(ret);
914 /* recover relocation */
915 ret = btrfs_recover_relocation(root);
916 WARN_ON(ret);
918 sb->s_flags &= ~MS_RDONLY;
921 return 0;
924 /* Used to sort the devices by max_avail(descending sort) */
925 static int btrfs_cmp_device_free_bytes(const void *dev_info1,
926 const void *dev_info2)
928 if (((struct btrfs_device_info *)dev_info1)->max_avail >
929 ((struct btrfs_device_info *)dev_info2)->max_avail)
930 return -1;
931 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
932 ((struct btrfs_device_info *)dev_info2)->max_avail)
933 return 1;
934 else
935 return 0;
939 * sort the devices by max_avail, in which max free extent size of each device
940 * is stored.(Descending Sort)
942 static inline void btrfs_descending_sort_devices(
943 struct btrfs_device_info *devices,
944 size_t nr_devices)
946 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
947 btrfs_cmp_device_free_bytes, NULL);
951 * The helper to calc the free space on the devices that can be used to store
952 * file data.
954 static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
956 struct btrfs_fs_info *fs_info = root->fs_info;
957 struct btrfs_device_info *devices_info;
958 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
959 struct btrfs_device *device;
960 u64 skip_space;
961 u64 type;
962 u64 avail_space;
963 u64 used_space;
964 u64 min_stripe_size;
965 int min_stripes = 1;
966 int i = 0, nr_devices;
967 int ret;
969 nr_devices = fs_info->fs_devices->rw_devices;
970 BUG_ON(!nr_devices);
972 devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
973 GFP_NOFS);
974 if (!devices_info)
975 return -ENOMEM;
977 /* calc min stripe number for data space alloction */
978 type = btrfs_get_alloc_profile(root, 1);
979 if (type & BTRFS_BLOCK_GROUP_RAID0)
980 min_stripes = 2;
981 else if (type & BTRFS_BLOCK_GROUP_RAID1)
982 min_stripes = 2;
983 else if (type & BTRFS_BLOCK_GROUP_RAID10)
984 min_stripes = 4;
986 if (type & BTRFS_BLOCK_GROUP_DUP)
987 min_stripe_size = 2 * BTRFS_STRIPE_LEN;
988 else
989 min_stripe_size = BTRFS_STRIPE_LEN;
991 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
992 if (!device->in_fs_metadata)
993 continue;
995 avail_space = device->total_bytes - device->bytes_used;
997 /* align with stripe_len */
998 do_div(avail_space, BTRFS_STRIPE_LEN);
999 avail_space *= BTRFS_STRIPE_LEN;
1002 * In order to avoid overwritting the superblock on the drive,
1003 * btrfs starts at an offset of at least 1MB when doing chunk
1004 * allocation.
1006 skip_space = 1024 * 1024;
1008 /* user can set the offset in fs_info->alloc_start. */
1009 if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
1010 device->total_bytes)
1011 skip_space = max(fs_info->alloc_start, skip_space);
1014 * btrfs can not use the free space in [0, skip_space - 1],
1015 * we must subtract it from the total. In order to implement
1016 * it, we account the used space in this range first.
1018 ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
1019 &used_space);
1020 if (ret) {
1021 kfree(devices_info);
1022 return ret;
1025 /* calc the free space in [0, skip_space - 1] */
1026 skip_space -= used_space;
1029 * we can use the free space in [0, skip_space - 1], subtract
1030 * it from the total.
1032 if (avail_space && avail_space >= skip_space)
1033 avail_space -= skip_space;
1034 else
1035 avail_space = 0;
1037 if (avail_space < min_stripe_size)
1038 continue;
1040 devices_info[i].dev = device;
1041 devices_info[i].max_avail = avail_space;
1043 i++;
1046 nr_devices = i;
1048 btrfs_descending_sort_devices(devices_info, nr_devices);
1050 i = nr_devices - 1;
1051 avail_space = 0;
1052 while (nr_devices >= min_stripes) {
1053 if (devices_info[i].max_avail >= min_stripe_size) {
1054 int j;
1055 u64 alloc_size;
1057 avail_space += devices_info[i].max_avail * min_stripes;
1058 alloc_size = devices_info[i].max_avail;
1059 for (j = i + 1 - min_stripes; j <= i; j++)
1060 devices_info[j].max_avail -= alloc_size;
1062 i--;
1063 nr_devices--;
1066 kfree(devices_info);
1067 *free_bytes = avail_space;
1068 return 0;
1071 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1073 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
1074 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1075 struct list_head *head = &root->fs_info->space_info;
1076 struct btrfs_space_info *found;
1077 u64 total_used = 0;
1078 u64 total_free_data = 0;
1079 int bits = dentry->d_sb->s_blocksize_bits;
1080 __be32 *fsid = (__be32 *)root->fs_info->fsid;
1081 int ret;
1083 /* holding chunk_muext to avoid allocating new chunks */
1084 mutex_lock(&root->fs_info->chunk_mutex);
1085 rcu_read_lock();
1086 list_for_each_entry_rcu(found, head, list) {
1087 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1088 total_free_data += found->disk_total - found->disk_used;
1089 total_free_data -=
1090 btrfs_account_ro_block_groups_free_space(found);
1093 total_used += found->disk_used;
1095 rcu_read_unlock();
1097 buf->f_namelen = BTRFS_NAME_LEN;
1098 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1099 buf->f_bfree = buf->f_blocks - (total_used >> bits);
1100 buf->f_bsize = dentry->d_sb->s_blocksize;
1101 buf->f_type = BTRFS_SUPER_MAGIC;
1102 buf->f_bavail = total_free_data;
1103 ret = btrfs_calc_avail_data_space(root, &total_free_data);
1104 if (ret) {
1105 mutex_unlock(&root->fs_info->chunk_mutex);
1106 return ret;
1108 buf->f_bavail += total_free_data;
1109 buf->f_bavail = buf->f_bavail >> bits;
1110 mutex_unlock(&root->fs_info->chunk_mutex);
1112 /* We treat it as constant endianness (it doesn't matter _which_)
1113 because we want the fsid to come out the same whether mounted
1114 on a big-endian or little-endian host */
1115 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
1116 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
1117 /* Mask in the root object ID too, to disambiguate subvols */
1118 buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
1119 buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
1121 return 0;
1124 static struct file_system_type btrfs_fs_type = {
1125 .owner = THIS_MODULE,
1126 .name = "btrfs",
1127 .mount = btrfs_mount,
1128 .kill_sb = kill_anon_super,
1129 .fs_flags = FS_REQUIRES_DEV,
1133 * used by btrfsctl to scan devices when no FS is mounted
1135 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
1136 unsigned long arg)
1138 struct btrfs_ioctl_vol_args *vol;
1139 struct btrfs_fs_devices *fs_devices;
1140 int ret = -ENOTTY;
1142 if (!capable(CAP_SYS_ADMIN))
1143 return -EPERM;
1145 vol = memdup_user((void __user *)arg, sizeof(*vol));
1146 if (IS_ERR(vol))
1147 return PTR_ERR(vol);
1149 switch (cmd) {
1150 case BTRFS_IOC_SCAN_DEV:
1151 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
1152 &btrfs_fs_type, &fs_devices);
1153 break;
1156 kfree(vol);
1157 return ret;
1160 static int btrfs_freeze(struct super_block *sb)
1162 struct btrfs_root *root = btrfs_sb(sb);
1163 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1164 mutex_lock(&root->fs_info->cleaner_mutex);
1165 return 0;
1168 static int btrfs_unfreeze(struct super_block *sb)
1170 struct btrfs_root *root = btrfs_sb(sb);
1171 mutex_unlock(&root->fs_info->cleaner_mutex);
1172 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
1173 return 0;
1176 static const struct super_operations btrfs_super_ops = {
1177 .drop_inode = btrfs_drop_inode,
1178 .evict_inode = btrfs_evict_inode,
1179 .put_super = btrfs_put_super,
1180 .sync_fs = btrfs_sync_fs,
1181 .show_options = btrfs_show_options,
1182 .write_inode = btrfs_write_inode,
1183 .dirty_inode = btrfs_dirty_inode,
1184 .alloc_inode = btrfs_alloc_inode,
1185 .destroy_inode = btrfs_destroy_inode,
1186 .statfs = btrfs_statfs,
1187 .remount_fs = btrfs_remount,
1188 .freeze_fs = btrfs_freeze,
1189 .unfreeze_fs = btrfs_unfreeze,
1192 static const struct file_operations btrfs_ctl_fops = {
1193 .unlocked_ioctl = btrfs_control_ioctl,
1194 .compat_ioctl = btrfs_control_ioctl,
1195 .owner = THIS_MODULE,
1196 .llseek = noop_llseek,
1199 static struct miscdevice btrfs_misc = {
1200 .minor = BTRFS_MINOR,
1201 .name = "btrfs-control",
1202 .fops = &btrfs_ctl_fops
1205 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1206 MODULE_ALIAS("devname:btrfs-control");
1208 static int btrfs_interface_init(void)
1210 return misc_register(&btrfs_misc);
1213 static void btrfs_interface_exit(void)
1215 if (misc_deregister(&btrfs_misc) < 0)
1216 printk(KERN_INFO "misc_deregister failed for control device");
1219 static int __init init_btrfs_fs(void)
1221 int err;
1223 err = btrfs_init_sysfs();
1224 if (err)
1225 return err;
1227 err = btrfs_init_compress();
1228 if (err)
1229 goto free_sysfs;
1231 err = btrfs_init_cachep();
1232 if (err)
1233 goto free_compress;
1235 err = extent_io_init();
1236 if (err)
1237 goto free_cachep;
1239 err = extent_map_init();
1240 if (err)
1241 goto free_extent_io;
1243 err = btrfs_delayed_inode_init();
1244 if (err)
1245 goto free_extent_map;
1247 err = btrfs_interface_init();
1248 if (err)
1249 goto free_delayed_inode;
1251 err = register_filesystem(&btrfs_fs_type);
1252 if (err)
1253 goto unregister_ioctl;
1255 printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
1256 return 0;
1258 unregister_ioctl:
1259 btrfs_interface_exit();
1260 free_delayed_inode:
1261 btrfs_delayed_inode_exit();
1262 free_extent_map:
1263 extent_map_exit();
1264 free_extent_io:
1265 extent_io_exit();
1266 free_cachep:
1267 btrfs_destroy_cachep();
1268 free_compress:
1269 btrfs_exit_compress();
1270 free_sysfs:
1271 btrfs_exit_sysfs();
1272 return err;
1275 static void __exit exit_btrfs_fs(void)
1277 btrfs_destroy_cachep();
1278 btrfs_delayed_inode_exit();
1279 extent_map_exit();
1280 extent_io_exit();
1281 btrfs_interface_exit();
1282 unregister_filesystem(&btrfs_fs_type);
1283 btrfs_exit_sysfs();
1284 btrfs_cleanup_fs_uuids();
1285 btrfs_exit_compress();
1288 module_init(init_btrfs_fs)
1289 module_exit(exit_btrfs_fs)
1291 MODULE_LICENSE("GPL");