Btrfs: change how we mount subvolumes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / super.c
blobf878337cee6f753eba0faa6eccd82dd431d6be88
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 "compat.h"
42 #include "ctree.h"
43 #include "disk-io.h"
44 #include "transaction.h"
45 #include "btrfs_inode.h"
46 #include "ioctl.h"
47 #include "print-tree.h"
48 #include "xattr.h"
49 #include "volumes.h"
50 #include "version.h"
51 #include "export.h"
52 #include "compression.h"
54 static const struct super_operations btrfs_super_ops;
56 static void btrfs_put_super(struct super_block *sb)
58 struct btrfs_root *root = btrfs_sb(sb);
59 int ret;
61 ret = close_ctree(root);
62 sb->s_fs_info = NULL;
65 enum {
66 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
67 Opt_nodatacow, Opt_max_extent, Opt_max_inline, Opt_alloc_start,
68 Opt_nobarrier, Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool,
69 Opt_noacl, Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio,
70 Opt_flushoncommit,
71 Opt_discard, Opt_err,
74 static match_table_t tokens = {
75 {Opt_degraded, "degraded"},
76 {Opt_subvol, "subvol=%s"},
77 {Opt_subvolid, "subvolid=%d"},
78 {Opt_device, "device=%s"},
79 {Opt_nodatasum, "nodatasum"},
80 {Opt_nodatacow, "nodatacow"},
81 {Opt_nobarrier, "nobarrier"},
82 {Opt_max_extent, "max_extent=%s"},
83 {Opt_max_inline, "max_inline=%s"},
84 {Opt_alloc_start, "alloc_start=%s"},
85 {Opt_thread_pool, "thread_pool=%d"},
86 {Opt_compress, "compress"},
87 {Opt_compress_force, "compress-force"},
88 {Opt_ssd, "ssd"},
89 {Opt_ssd_spread, "ssd_spread"},
90 {Opt_nossd, "nossd"},
91 {Opt_noacl, "noacl"},
92 {Opt_notreelog, "notreelog"},
93 {Opt_flushoncommit, "flushoncommit"},
94 {Opt_ratio, "metadata_ratio=%d"},
95 {Opt_discard, "discard"},
96 {Opt_err, NULL},
99 u64 btrfs_parse_size(char *str)
101 u64 res;
102 int mult = 1;
103 char *end;
104 char last;
106 res = simple_strtoul(str, &end, 10);
108 last = end[0];
109 if (isalpha(last)) {
110 last = tolower(last);
111 switch (last) {
112 case 'g':
113 mult *= 1024;
114 case 'm':
115 mult *= 1024;
116 case 'k':
117 mult *= 1024;
119 res = res * mult;
121 return res;
125 * Regular mount options parser. Everything that is needed only when
126 * reading in a new superblock is parsed here.
128 int btrfs_parse_options(struct btrfs_root *root, char *options)
130 struct btrfs_fs_info *info = root->fs_info;
131 substring_t args[MAX_OPT_ARGS];
132 char *p, *num, *orig;
133 int intarg;
134 int ret = 0;
136 if (!options)
137 return 0;
140 * strsep changes the string, duplicate it because parse_options
141 * gets called twice
143 options = kstrdup(options, GFP_NOFS);
144 if (!options)
145 return -ENOMEM;
147 orig = options;
149 while ((p = strsep(&options, ",")) != NULL) {
150 int token;
151 if (!*p)
152 continue;
154 token = match_token(p, tokens, args);
155 switch (token) {
156 case Opt_degraded:
157 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
158 btrfs_set_opt(info->mount_opt, DEGRADED);
159 break;
160 case Opt_subvol:
161 case Opt_subvolid:
162 case Opt_device:
164 * These are parsed by btrfs_parse_early_options
165 * and can be happily ignored here.
167 break;
168 case Opt_nodatasum:
169 printk(KERN_INFO "btrfs: setting nodatasum\n");
170 btrfs_set_opt(info->mount_opt, NODATASUM);
171 break;
172 case Opt_nodatacow:
173 printk(KERN_INFO "btrfs: setting nodatacow\n");
174 btrfs_set_opt(info->mount_opt, NODATACOW);
175 btrfs_set_opt(info->mount_opt, NODATASUM);
176 break;
177 case Opt_compress:
178 printk(KERN_INFO "btrfs: use compression\n");
179 btrfs_set_opt(info->mount_opt, COMPRESS);
180 break;
181 case Opt_compress_force:
182 printk(KERN_INFO "btrfs: forcing compression\n");
183 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
184 btrfs_set_opt(info->mount_opt, COMPRESS);
185 break;
186 case Opt_ssd:
187 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
188 btrfs_set_opt(info->mount_opt, SSD);
189 break;
190 case Opt_ssd_spread:
191 printk(KERN_INFO "btrfs: use spread ssd "
192 "allocation scheme\n");
193 btrfs_set_opt(info->mount_opt, SSD);
194 btrfs_set_opt(info->mount_opt, SSD_SPREAD);
195 break;
196 case Opt_nossd:
197 printk(KERN_INFO "btrfs: not using ssd allocation "
198 "scheme\n");
199 btrfs_set_opt(info->mount_opt, NOSSD);
200 btrfs_clear_opt(info->mount_opt, SSD);
201 btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
202 break;
203 case Opt_nobarrier:
204 printk(KERN_INFO "btrfs: turning off barriers\n");
205 btrfs_set_opt(info->mount_opt, NOBARRIER);
206 break;
207 case Opt_thread_pool:
208 intarg = 0;
209 match_int(&args[0], &intarg);
210 if (intarg) {
211 info->thread_pool_size = intarg;
212 printk(KERN_INFO "btrfs: thread pool %d\n",
213 info->thread_pool_size);
215 break;
216 case Opt_max_extent:
217 num = match_strdup(&args[0]);
218 if (num) {
219 info->max_extent = btrfs_parse_size(num);
220 kfree(num);
222 info->max_extent = max_t(u64,
223 info->max_extent, root->sectorsize);
224 printk(KERN_INFO "btrfs: max_extent at %llu\n",
225 (unsigned long long)info->max_extent);
227 break;
228 case Opt_max_inline:
229 num = match_strdup(&args[0]);
230 if (num) {
231 info->max_inline = btrfs_parse_size(num);
232 kfree(num);
234 if (info->max_inline) {
235 info->max_inline = max_t(u64,
236 info->max_inline,
237 root->sectorsize);
239 printk(KERN_INFO "btrfs: max_inline at %llu\n",
240 (unsigned long long)info->max_inline);
242 break;
243 case Opt_alloc_start:
244 num = match_strdup(&args[0]);
245 if (num) {
246 info->alloc_start = btrfs_parse_size(num);
247 kfree(num);
248 printk(KERN_INFO
249 "btrfs: allocations start at %llu\n",
250 (unsigned long long)info->alloc_start);
252 break;
253 case Opt_noacl:
254 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
255 break;
256 case Opt_notreelog:
257 printk(KERN_INFO "btrfs: disabling tree log\n");
258 btrfs_set_opt(info->mount_opt, NOTREELOG);
259 break;
260 case Opt_flushoncommit:
261 printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
262 btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
263 break;
264 case Opt_ratio:
265 intarg = 0;
266 match_int(&args[0], &intarg);
267 if (intarg) {
268 info->metadata_ratio = intarg;
269 printk(KERN_INFO "btrfs: metadata ratio %d\n",
270 info->metadata_ratio);
272 break;
273 case Opt_discard:
274 btrfs_set_opt(info->mount_opt, DISCARD);
275 break;
276 case Opt_err:
277 printk(KERN_INFO "btrfs: unrecognized mount option "
278 "'%s'\n", p);
279 ret = -EINVAL;
280 goto out;
281 default:
282 break;
285 out:
286 kfree(orig);
287 return ret;
291 * Parse mount options that are required early in the mount process.
293 * All other options will be parsed on much later in the mount process and
294 * only when we need to allocate a new super block.
296 static int btrfs_parse_early_options(const char *options, fmode_t flags,
297 void *holder, char **subvol_name, u64 *subvol_objectid,
298 struct btrfs_fs_devices **fs_devices)
300 substring_t args[MAX_OPT_ARGS];
301 char *opts, *p;
302 int error = 0;
303 int intarg;
305 if (!options)
306 goto out;
309 * strsep changes the string, duplicate it because parse_options
310 * gets called twice
312 opts = kstrdup(options, GFP_KERNEL);
313 if (!opts)
314 return -ENOMEM;
316 while ((p = strsep(&opts, ",")) != NULL) {
317 int token;
318 if (!*p)
319 continue;
321 token = match_token(p, tokens, args);
322 switch (token) {
323 case Opt_subvol:
324 *subvol_name = match_strdup(&args[0]);
325 break;
326 case Opt_subvolid:
327 intarg = 0;
328 match_int(&args[0], &intarg);
329 if (intarg)
330 *subvol_objectid = intarg;
331 break;
332 case Opt_device:
333 error = btrfs_scan_one_device(match_strdup(&args[0]),
334 flags, holder, fs_devices);
335 if (error)
336 goto out_free_opts;
337 break;
338 default:
339 break;
343 out_free_opts:
344 kfree(opts);
345 out:
347 * If no subvolume name is specified we use the default one. Allocate
348 * a copy of the string "." here so that code later in the
349 * mount path doesn't care if it's the default volume or another one.
351 if (!*subvol_name) {
352 *subvol_name = kstrdup(".", GFP_KERNEL);
353 if (!*subvol_name)
354 return -ENOMEM;
356 return error;
359 static struct dentry *get_default_root(struct super_block *sb,
360 u64 subvol_objectid)
362 struct btrfs_root *root = sb->s_fs_info;
363 struct btrfs_root *new_root;
364 struct btrfs_dir_item *di;
365 struct btrfs_path *path;
366 struct btrfs_key location;
367 struct inode *inode;
368 struct dentry *dentry;
369 u64 dir_id;
370 int new = 0;
373 * We have a specific subvol we want to mount, just setup location and
374 * go look up the root.
376 if (subvol_objectid) {
377 location.objectid = subvol_objectid;
378 location.type = BTRFS_ROOT_ITEM_KEY;
379 location.offset = (u64)-1;
380 goto find_root;
383 path = btrfs_alloc_path();
384 if (!path)
385 return ERR_PTR(-ENOMEM);
386 path->leave_spinning = 1;
389 * Find the "default" dir item which points to the root item that we
390 * will mount by default if we haven't been given a specific subvolume
391 * to mount.
393 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
394 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
395 if (!di) {
397 * Ok the default dir item isn't there. This is weird since
398 * it's always been there, but don't freak out, just try and
399 * mount to root most subvolume.
401 btrfs_free_path(path);
402 dir_id = BTRFS_FIRST_FREE_OBJECTID;
403 new_root = root->fs_info->fs_root;
404 goto setup_root;
407 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
408 btrfs_free_path(path);
410 find_root:
411 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
412 if (IS_ERR(new_root))
413 return ERR_PTR(PTR_ERR(new_root));
415 if (btrfs_root_refs(&new_root->root_item) == 0)
416 return ERR_PTR(-ENOENT);
418 dir_id = btrfs_root_dirid(&new_root->root_item);
419 setup_root:
420 location.objectid = dir_id;
421 location.type = BTRFS_INODE_ITEM_KEY;
422 location.offset = 0;
424 inode = btrfs_iget(sb, &location, new_root, &new);
425 if (!inode)
426 return ERR_PTR(-ENOMEM);
429 * If we're just mounting the root most subvol put the inode and return
430 * a reference to the dentry. We will have already gotten a reference
431 * to the inode in btrfs_fill_super so we're good to go.
433 if (!new && sb->s_root->d_inode == inode) {
434 iput(inode);
435 return dget(sb->s_root);
438 if (new) {
439 const struct qstr name = { .name = "/", .len = 1 };
442 * New inode, we need to make the dentry a sibling of s_root so
443 * everything gets cleaned up properly on unmount.
445 dentry = d_alloc(sb->s_root, &name);
446 if (!dentry) {
447 iput(inode);
448 return ERR_PTR(-ENOMEM);
450 d_splice_alias(inode, dentry);
451 } else {
453 * We found the inode in cache, just find a dentry for it and
454 * put the reference to the inode we just got.
456 dentry = d_find_alias(inode);
457 iput(inode);
460 return dentry;
463 static int btrfs_fill_super(struct super_block *sb,
464 struct btrfs_fs_devices *fs_devices,
465 void *data, int silent)
467 struct inode *inode;
468 struct dentry *root_dentry;
469 struct btrfs_super_block *disk_super;
470 struct btrfs_root *tree_root;
471 struct btrfs_key key;
472 int err;
474 sb->s_maxbytes = MAX_LFS_FILESIZE;
475 sb->s_magic = BTRFS_SUPER_MAGIC;
476 sb->s_op = &btrfs_super_ops;
477 sb->s_export_op = &btrfs_export_ops;
478 sb->s_xattr = btrfs_xattr_handlers;
479 sb->s_time_gran = 1;
480 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
481 sb->s_flags |= MS_POSIXACL;
482 #endif
484 tree_root = open_ctree(sb, fs_devices, (char *)data);
486 if (IS_ERR(tree_root)) {
487 printk("btrfs: open_ctree failed\n");
488 return PTR_ERR(tree_root);
490 sb->s_fs_info = tree_root;
491 disk_super = &tree_root->fs_info->super_copy;
493 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
494 key.type = BTRFS_INODE_ITEM_KEY;
495 key.offset = 0;
496 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
497 if (IS_ERR(inode)) {
498 err = PTR_ERR(inode);
499 goto fail_close;
502 root_dentry = d_alloc_root(inode);
503 if (!root_dentry) {
504 iput(inode);
505 err = -ENOMEM;
506 goto fail_close;
509 sb->s_root = root_dentry;
511 save_mount_options(sb, data);
512 return 0;
514 fail_close:
515 close_ctree(tree_root);
516 return err;
519 int btrfs_sync_fs(struct super_block *sb, int wait)
521 struct btrfs_trans_handle *trans;
522 struct btrfs_root *root = btrfs_sb(sb);
523 int ret;
525 if (!wait) {
526 filemap_flush(root->fs_info->btree_inode->i_mapping);
527 return 0;
530 btrfs_start_delalloc_inodes(root, 0);
531 btrfs_wait_ordered_extents(root, 0, 0);
533 trans = btrfs_start_transaction(root, 1);
534 ret = btrfs_commit_transaction(trans, root);
535 return ret;
538 static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
540 struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
541 struct btrfs_fs_info *info = root->fs_info;
543 if (btrfs_test_opt(root, DEGRADED))
544 seq_puts(seq, ",degraded");
545 if (btrfs_test_opt(root, NODATASUM))
546 seq_puts(seq, ",nodatasum");
547 if (btrfs_test_opt(root, NODATACOW))
548 seq_puts(seq, ",nodatacow");
549 if (btrfs_test_opt(root, NOBARRIER))
550 seq_puts(seq, ",nobarrier");
551 if (info->max_extent != (u64)-1)
552 seq_printf(seq, ",max_extent=%llu",
553 (unsigned long long)info->max_extent);
554 if (info->max_inline != 8192 * 1024)
555 seq_printf(seq, ",max_inline=%llu",
556 (unsigned long long)info->max_inline);
557 if (info->alloc_start != 0)
558 seq_printf(seq, ",alloc_start=%llu",
559 (unsigned long long)info->alloc_start);
560 if (info->thread_pool_size != min_t(unsigned long,
561 num_online_cpus() + 2, 8))
562 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
563 if (btrfs_test_opt(root, COMPRESS))
564 seq_puts(seq, ",compress");
565 if (btrfs_test_opt(root, NOSSD))
566 seq_puts(seq, ",nossd");
567 if (btrfs_test_opt(root, SSD_SPREAD))
568 seq_puts(seq, ",ssd_spread");
569 else if (btrfs_test_opt(root, SSD))
570 seq_puts(seq, ",ssd");
571 if (btrfs_test_opt(root, NOTREELOG))
572 seq_puts(seq, ",notreelog");
573 if (btrfs_test_opt(root, FLUSHONCOMMIT))
574 seq_puts(seq, ",flushoncommit");
575 if (btrfs_test_opt(root, DISCARD))
576 seq_puts(seq, ",discard");
577 if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
578 seq_puts(seq, ",noacl");
579 return 0;
582 static int btrfs_test_super(struct super_block *s, void *data)
584 struct btrfs_fs_devices *test_fs_devices = data;
585 struct btrfs_root *root = btrfs_sb(s);
587 return root->fs_info->fs_devices == test_fs_devices;
591 * Find a superblock for the given device / mount point.
593 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
594 * for multiple device setup. Make sure to keep it in sync.
596 static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
597 const char *dev_name, void *data, struct vfsmount *mnt)
599 struct block_device *bdev = NULL;
600 struct super_block *s;
601 struct dentry *root;
602 struct btrfs_fs_devices *fs_devices = NULL;
603 fmode_t mode = FMODE_READ;
604 char *subvol_name = NULL;
605 u64 subvol_objectid = 0;
606 int error = 0;
607 int found = 0;
609 if (!(flags & MS_RDONLY))
610 mode |= FMODE_WRITE;
612 error = btrfs_parse_early_options(data, mode, fs_type,
613 &subvol_name, &subvol_objectid,
614 &fs_devices);
615 if (error)
616 return error;
618 error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
619 if (error)
620 goto error_free_subvol_name;
622 error = btrfs_open_devices(fs_devices, mode, fs_type);
623 if (error)
624 goto error_free_subvol_name;
626 if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
627 error = -EACCES;
628 goto error_close_devices;
631 bdev = fs_devices->latest_bdev;
632 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
633 if (IS_ERR(s))
634 goto error_s;
636 if (s->s_root) {
637 if ((flags ^ s->s_flags) & MS_RDONLY) {
638 deactivate_locked_super(s);
639 error = -EBUSY;
640 goto error_close_devices;
643 found = 1;
644 btrfs_close_devices(fs_devices);
645 } else {
646 char b[BDEVNAME_SIZE];
648 s->s_flags = flags;
649 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
650 error = btrfs_fill_super(s, fs_devices, data,
651 flags & MS_SILENT ? 1 : 0);
652 if (error) {
653 deactivate_locked_super(s);
654 goto error_free_subvol_name;
657 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
658 s->s_flags |= MS_ACTIVE;
661 root = get_default_root(s, subvol_objectid);
662 if (IS_ERR(root)) {
663 error = PTR_ERR(root);
664 deactivate_locked_super(s);
665 goto error;
667 /* if they gave us a subvolume name bind mount into that */
668 if (strcmp(subvol_name, ".")) {
669 struct dentry *new_root;
670 mutex_lock(&root->d_inode->i_mutex);
671 new_root = lookup_one_len(subvol_name, root,
672 strlen(subvol_name));
673 mutex_unlock(&root->d_inode->i_mutex);
675 if (IS_ERR(new_root)) {
676 deactivate_locked_super(s);
677 error = PTR_ERR(new_root);
678 dput(root);
679 goto error_close_devices;
681 if (!new_root->d_inode) {
682 dput(root);
683 dput(new_root);
684 deactivate_locked_super(s);
685 error = -ENXIO;
686 goto error_close_devices;
688 dput(root);
689 root = new_root;
692 mnt->mnt_sb = s;
693 mnt->mnt_root = root;
695 kfree(subvol_name);
696 return 0;
698 error_s:
699 error = PTR_ERR(s);
700 error_close_devices:
701 btrfs_close_devices(fs_devices);
702 error_free_subvol_name:
703 kfree(subvol_name);
704 error:
705 return error;
708 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
710 struct btrfs_root *root = btrfs_sb(sb);
711 int ret;
713 ret = btrfs_parse_options(root, data);
714 if (ret)
715 return -EINVAL;
717 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
718 return 0;
720 if (*flags & MS_RDONLY) {
721 sb->s_flags |= MS_RDONLY;
723 ret = btrfs_commit_super(root);
724 WARN_ON(ret);
725 } else {
726 if (root->fs_info->fs_devices->rw_devices == 0)
727 return -EACCES;
729 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
730 return -EINVAL;
732 /* recover relocation */
733 ret = btrfs_recover_relocation(root);
734 WARN_ON(ret);
736 ret = btrfs_cleanup_fs_roots(root->fs_info);
737 WARN_ON(ret);
739 sb->s_flags &= ~MS_RDONLY;
742 return 0;
745 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
747 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
748 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
749 int bits = dentry->d_sb->s_blocksize_bits;
750 __be32 *fsid = (__be32 *)root->fs_info->fsid;
752 buf->f_namelen = BTRFS_NAME_LEN;
753 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
754 buf->f_bfree = buf->f_blocks -
755 (btrfs_super_bytes_used(disk_super) >> bits);
756 buf->f_bavail = buf->f_bfree;
757 buf->f_bsize = dentry->d_sb->s_blocksize;
758 buf->f_type = BTRFS_SUPER_MAGIC;
760 /* We treat it as constant endianness (it doesn't matter _which_)
761 because we want the fsid to come out the same whether mounted
762 on a big-endian or little-endian host */
763 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
764 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
765 /* Mask in the root object ID too, to disambiguate subvols */
766 buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
767 buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
769 return 0;
772 static struct file_system_type btrfs_fs_type = {
773 .owner = THIS_MODULE,
774 .name = "btrfs",
775 .get_sb = btrfs_get_sb,
776 .kill_sb = kill_anon_super,
777 .fs_flags = FS_REQUIRES_DEV,
781 * used by btrfsctl to scan devices when no FS is mounted
783 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
784 unsigned long arg)
786 struct btrfs_ioctl_vol_args *vol;
787 struct btrfs_fs_devices *fs_devices;
788 int ret = -ENOTTY;
790 if (!capable(CAP_SYS_ADMIN))
791 return -EPERM;
793 vol = memdup_user((void __user *)arg, sizeof(*vol));
794 if (IS_ERR(vol))
795 return PTR_ERR(vol);
797 switch (cmd) {
798 case BTRFS_IOC_SCAN_DEV:
799 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
800 &btrfs_fs_type, &fs_devices);
801 break;
804 kfree(vol);
805 return ret;
808 static int btrfs_freeze(struct super_block *sb)
810 struct btrfs_root *root = btrfs_sb(sb);
811 mutex_lock(&root->fs_info->transaction_kthread_mutex);
812 mutex_lock(&root->fs_info->cleaner_mutex);
813 return 0;
816 static int btrfs_unfreeze(struct super_block *sb)
818 struct btrfs_root *root = btrfs_sb(sb);
819 mutex_unlock(&root->fs_info->cleaner_mutex);
820 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
821 return 0;
824 static const struct super_operations btrfs_super_ops = {
825 .drop_inode = btrfs_drop_inode,
826 .delete_inode = btrfs_delete_inode,
827 .put_super = btrfs_put_super,
828 .sync_fs = btrfs_sync_fs,
829 .show_options = btrfs_show_options,
830 .write_inode = btrfs_write_inode,
831 .dirty_inode = btrfs_dirty_inode,
832 .alloc_inode = btrfs_alloc_inode,
833 .destroy_inode = btrfs_destroy_inode,
834 .statfs = btrfs_statfs,
835 .remount_fs = btrfs_remount,
836 .freeze_fs = btrfs_freeze,
837 .unfreeze_fs = btrfs_unfreeze,
840 static const struct file_operations btrfs_ctl_fops = {
841 .unlocked_ioctl = btrfs_control_ioctl,
842 .compat_ioctl = btrfs_control_ioctl,
843 .owner = THIS_MODULE,
846 static struct miscdevice btrfs_misc = {
847 .minor = MISC_DYNAMIC_MINOR,
848 .name = "btrfs-control",
849 .fops = &btrfs_ctl_fops
852 static int btrfs_interface_init(void)
854 return misc_register(&btrfs_misc);
857 static void btrfs_interface_exit(void)
859 if (misc_deregister(&btrfs_misc) < 0)
860 printk(KERN_INFO "misc_deregister failed for control device");
863 static int __init init_btrfs_fs(void)
865 int err;
867 err = btrfs_init_sysfs();
868 if (err)
869 return err;
871 err = btrfs_init_cachep();
872 if (err)
873 goto free_sysfs;
875 err = extent_io_init();
876 if (err)
877 goto free_cachep;
879 err = extent_map_init();
880 if (err)
881 goto free_extent_io;
883 err = btrfs_interface_init();
884 if (err)
885 goto free_extent_map;
887 err = register_filesystem(&btrfs_fs_type);
888 if (err)
889 goto unregister_ioctl;
891 printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
892 return 0;
894 unregister_ioctl:
895 btrfs_interface_exit();
896 free_extent_map:
897 extent_map_exit();
898 free_extent_io:
899 extent_io_exit();
900 free_cachep:
901 btrfs_destroy_cachep();
902 free_sysfs:
903 btrfs_exit_sysfs();
904 return err;
907 static void __exit exit_btrfs_fs(void)
909 btrfs_destroy_cachep();
910 extent_map_exit();
911 extent_io_exit();
912 btrfs_interface_exit();
913 unregister_filesystem(&btrfs_fs_type);
914 btrfs_exit_sysfs();
915 btrfs_cleanup_fs_uuids();
916 btrfs_zlib_exit();
919 module_init(init_btrfs_fs)
920 module_exit(exit_btrfs_fs)
922 MODULE_LICENSE("GPL");