Btrfs: kfree correct pointer during mount option parsing
[linux-2.6.git] / fs / btrfs / super.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/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"
53
54 static const struct super_operations btrfs_super_ops;
55
56 static void btrfs_put_super(struct super_block *sb)
57 {
58         struct btrfs_root *root = btrfs_sb(sb);
59         int ret;
60
61         ret = close_ctree(root);
62         sb->s_fs_info = NULL;
63 }
64
65 enum {
66         Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow,
67         Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
68         Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl,
69         Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio,
70         Opt_flushoncommit,
71         Opt_discard, Opt_err,
72 };
73
74 static match_table_t tokens = {
75         {Opt_degraded, "degraded"},
76         {Opt_subvol, "subvol=%s"},
77         {Opt_device, "device=%s"},
78         {Opt_nodatasum, "nodatasum"},
79         {Opt_nodatacow, "nodatacow"},
80         {Opt_nobarrier, "nobarrier"},
81         {Opt_max_extent, "max_extent=%s"},
82         {Opt_max_inline, "max_inline=%s"},
83         {Opt_alloc_start, "alloc_start=%s"},
84         {Opt_thread_pool, "thread_pool=%d"},
85         {Opt_compress, "compress"},
86         {Opt_compress_force, "compress-force"},
87         {Opt_ssd, "ssd"},
88         {Opt_ssd_spread, "ssd_spread"},
89         {Opt_nossd, "nossd"},
90         {Opt_noacl, "noacl"},
91         {Opt_notreelog, "notreelog"},
92         {Opt_flushoncommit, "flushoncommit"},
93         {Opt_ratio, "metadata_ratio=%d"},
94         {Opt_discard, "discard"},
95         {Opt_err, NULL},
96 };
97
98 u64 btrfs_parse_size(char *str)
99 {
100         u64 res;
101         int mult = 1;
102         char *end;
103         char last;
104
105         res = simple_strtoul(str, &end, 10);
106
107         last = end[0];
108         if (isalpha(last)) {
109                 last = tolower(last);
110                 switch (last) {
111                 case 'g':
112                         mult *= 1024;
113                 case 'm':
114                         mult *= 1024;
115                 case 'k':
116                         mult *= 1024;
117                 }
118                 res = res * mult;
119         }
120         return res;
121 }
122
123 /*
124  * Regular mount options parser.  Everything that is needed only when
125  * reading in a new superblock is parsed here.
126  */
127 int btrfs_parse_options(struct btrfs_root *root, char *options)
128 {
129         struct btrfs_fs_info *info = root->fs_info;
130         substring_t args[MAX_OPT_ARGS];
131         char *p, *num, *orig;
132         int intarg;
133         int ret = 0;
134
135         if (!options)
136                 return 0;
137
138         /*
139          * strsep changes the string, duplicate it because parse_options
140          * gets called twice
141          */
142         options = kstrdup(options, GFP_NOFS);
143         if (!options)
144                 return -ENOMEM;
145
146         orig = options;
147
148         while ((p = strsep(&options, ",")) != NULL) {
149                 int token;
150                 if (!*p)
151                         continue;
152
153                 token = match_token(p, tokens, args);
154                 switch (token) {
155                 case Opt_degraded:
156                         printk(KERN_INFO "btrfs: allowing degraded mounts\n");
157                         btrfs_set_opt(info->mount_opt, DEGRADED);
158                         break;
159                 case Opt_subvol:
160                 case Opt_device:
161                         /*
162                          * These are parsed by btrfs_parse_early_options
163                          * and can be happily ignored here.
164                          */
165                         break;
166                 case Opt_nodatasum:
167                         printk(KERN_INFO "btrfs: setting nodatasum\n");
168                         btrfs_set_opt(info->mount_opt, NODATASUM);
169                         break;
170                 case Opt_nodatacow:
171                         printk(KERN_INFO "btrfs: setting nodatacow\n");
172                         btrfs_set_opt(info->mount_opt, NODATACOW);
173                         btrfs_set_opt(info->mount_opt, NODATASUM);
174                         break;
175                 case Opt_compress:
176                         printk(KERN_INFO "btrfs: use compression\n");
177                         btrfs_set_opt(info->mount_opt, COMPRESS);
178                         break;
179                 case Opt_compress_force:
180                         printk(KERN_INFO "btrfs: forcing compression\n");
181                         btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
182                         btrfs_set_opt(info->mount_opt, COMPRESS);
183                         break;
184                 case Opt_ssd:
185                         printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
186                         btrfs_set_opt(info->mount_opt, SSD);
187                         break;
188                 case Opt_ssd_spread:
189                         printk(KERN_INFO "btrfs: use spread ssd "
190                                "allocation scheme\n");
191                         btrfs_set_opt(info->mount_opt, SSD);
192                         btrfs_set_opt(info->mount_opt, SSD_SPREAD);
193                         break;
194                 case Opt_nossd:
195                         printk(KERN_INFO "btrfs: not using ssd allocation "
196                                "scheme\n");
197                         btrfs_set_opt(info->mount_opt, NOSSD);
198                         btrfs_clear_opt(info->mount_opt, SSD);
199                         btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
200                         break;
201                 case Opt_nobarrier:
202                         printk(KERN_INFO "btrfs: turning off barriers\n");
203                         btrfs_set_opt(info->mount_opt, NOBARRIER);
204                         break;
205                 case Opt_thread_pool:
206                         intarg = 0;
207                         match_int(&args[0], &intarg);
208                         if (intarg) {
209                                 info->thread_pool_size = intarg;
210                                 printk(KERN_INFO "btrfs: thread pool %d\n",
211                                        info->thread_pool_size);
212                         }
213                         break;
214                 case Opt_max_extent:
215                         num = match_strdup(&args[0]);
216                         if (num) {
217                                 info->max_extent = btrfs_parse_size(num);
218                                 kfree(num);
219
220                                 info->max_extent = max_t(u64,
221                                         info->max_extent, root->sectorsize);
222                                 printk(KERN_INFO "btrfs: max_extent at %llu\n",
223                                        (unsigned long long)info->max_extent);
224                         }
225                         break;
226                 case Opt_max_inline:
227                         num = match_strdup(&args[0]);
228                         if (num) {
229                                 info->max_inline = btrfs_parse_size(num);
230                                 kfree(num);
231
232                                 if (info->max_inline) {
233                                         info->max_inline = max_t(u64,
234                                                 info->max_inline,
235                                                 root->sectorsize);
236                                 }
237                                 printk(KERN_INFO "btrfs: max_inline at %llu\n",
238                                         (unsigned long long)info->max_inline);
239                         }
240                         break;
241                 case Opt_alloc_start:
242                         num = match_strdup(&args[0]);
243                         if (num) {
244                                 info->alloc_start = btrfs_parse_size(num);
245                                 kfree(num);
246                                 printk(KERN_INFO
247                                         "btrfs: allocations start at %llu\n",
248                                         (unsigned long long)info->alloc_start);
249                         }
250                         break;
251                 case Opt_noacl:
252                         root->fs_info->sb->s_flags &= ~MS_POSIXACL;
253                         break;
254                 case Opt_notreelog:
255                         printk(KERN_INFO "btrfs: disabling tree log\n");
256                         btrfs_set_opt(info->mount_opt, NOTREELOG);
257                         break;
258                 case Opt_flushoncommit:
259                         printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
260                         btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
261                         break;
262                 case Opt_ratio:
263                         intarg = 0;
264                         match_int(&args[0], &intarg);
265                         if (intarg) {
266                                 info->metadata_ratio = intarg;
267                                 printk(KERN_INFO "btrfs: metadata ratio %d\n",
268                                        info->metadata_ratio);
269                         }
270                         break;
271                 case Opt_discard:
272                         btrfs_set_opt(info->mount_opt, DISCARD);
273                         break;
274                 case Opt_err:
275                         printk(KERN_INFO "btrfs: unrecognized mount option "
276                                "'%s'\n", p);
277                         ret = -EINVAL;
278                         goto out;
279                 default:
280                         break;
281                 }
282         }
283 out:
284         kfree(orig);
285         return ret;
286 }
287
288 /*
289  * Parse mount options that are required early in the mount process.
290  *
291  * All other options will be parsed on much later in the mount process and
292  * only when we need to allocate a new super block.
293  */
294 static int btrfs_parse_early_options(const char *options, fmode_t flags,
295                 void *holder, char **subvol_name,
296                 struct btrfs_fs_devices **fs_devices)
297 {
298         substring_t args[MAX_OPT_ARGS];
299         char *opts, *p;
300         int error = 0;
301
302         if (!options)
303                 goto out;
304
305         /*
306          * strsep changes the string, duplicate it because parse_options
307          * gets called twice
308          */
309         opts = kstrdup(options, GFP_KERNEL);
310         if (!opts)
311                 return -ENOMEM;
312
313         while ((p = strsep(&opts, ",")) != NULL) {
314                 int token;
315                 if (!*p)
316                         continue;
317
318                 token = match_token(p, tokens, args);
319                 switch (token) {
320                 case Opt_subvol:
321                         *subvol_name = match_strdup(&args[0]);
322                         break;
323                 case Opt_device:
324                         error = btrfs_scan_one_device(match_strdup(&args[0]),
325                                         flags, holder, fs_devices);
326                         if (error)
327                                 goto out_free_opts;
328                         break;
329                 default:
330                         break;
331                 }
332         }
333
334  out_free_opts:
335         kfree(opts);
336  out:
337         /*
338          * If no subvolume name is specified we use the default one.  Allocate
339          * a copy of the string "." here so that code later in the
340          * mount path doesn't care if it's the default volume or another one.
341          */
342         if (!*subvol_name) {
343                 *subvol_name = kstrdup(".", GFP_KERNEL);
344                 if (!*subvol_name)
345                         return -ENOMEM;
346         }
347         return error;
348 }
349
350 static int btrfs_fill_super(struct super_block *sb,
351                             struct btrfs_fs_devices *fs_devices,
352                             void *data, int silent)
353 {
354         struct inode *inode;
355         struct dentry *root_dentry;
356         struct btrfs_super_block *disk_super;
357         struct btrfs_root *tree_root;
358         struct btrfs_key key;
359         int err;
360
361         sb->s_maxbytes = MAX_LFS_FILESIZE;
362         sb->s_magic = BTRFS_SUPER_MAGIC;
363         sb->s_op = &btrfs_super_ops;
364         sb->s_export_op = &btrfs_export_ops;
365         sb->s_xattr = btrfs_xattr_handlers;
366         sb->s_time_gran = 1;
367 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
368         sb->s_flags |= MS_POSIXACL;
369 #endif
370
371         tree_root = open_ctree(sb, fs_devices, (char *)data);
372
373         if (IS_ERR(tree_root)) {
374                 printk("btrfs: open_ctree failed\n");
375                 return PTR_ERR(tree_root);
376         }
377         sb->s_fs_info = tree_root;
378         disk_super = &tree_root->fs_info->super_copy;
379
380         key.objectid = BTRFS_FIRST_FREE_OBJECTID;
381         key.type = BTRFS_INODE_ITEM_KEY;
382         key.offset = 0;
383         inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root);
384         if (IS_ERR(inode)) {
385                 err = PTR_ERR(inode);
386                 goto fail_close;
387         }
388
389         root_dentry = d_alloc_root(inode);
390         if (!root_dentry) {
391                 iput(inode);
392                 err = -ENOMEM;
393                 goto fail_close;
394         }
395 #if 0
396         /* this does the super kobj at the same time */
397         err = btrfs_sysfs_add_super(tree_root->fs_info);
398         if (err)
399                 goto fail_close;
400 #endif
401
402         sb->s_root = root_dentry;
403
404         save_mount_options(sb, data);
405         return 0;
406
407 fail_close:
408         close_ctree(tree_root);
409         return err;
410 }
411
412 int btrfs_sync_fs(struct super_block *sb, int wait)
413 {
414         struct btrfs_trans_handle *trans;
415         struct btrfs_root *root = btrfs_sb(sb);
416         int ret;
417
418         if (!wait) {
419                 filemap_flush(root->fs_info->btree_inode->i_mapping);
420                 return 0;
421         }
422
423         btrfs_start_delalloc_inodes(root, 0);
424         btrfs_wait_ordered_extents(root, 0, 0);
425
426         trans = btrfs_start_transaction(root, 1);
427         ret = btrfs_commit_transaction(trans, root);
428         return ret;
429 }
430
431 static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
432 {
433         struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
434         struct btrfs_fs_info *info = root->fs_info;
435
436         if (btrfs_test_opt(root, DEGRADED))
437                 seq_puts(seq, ",degraded");
438         if (btrfs_test_opt(root, NODATASUM))
439                 seq_puts(seq, ",nodatasum");
440         if (btrfs_test_opt(root, NODATACOW))
441                 seq_puts(seq, ",nodatacow");
442         if (btrfs_test_opt(root, NOBARRIER))
443                 seq_puts(seq, ",nobarrier");
444         if (info->max_extent != (u64)-1)
445                 seq_printf(seq, ",max_extent=%llu",
446                            (unsigned long long)info->max_extent);
447         if (info->max_inline != 8192 * 1024)
448                 seq_printf(seq, ",max_inline=%llu",
449                            (unsigned long long)info->max_inline);
450         if (info->alloc_start != 0)
451                 seq_printf(seq, ",alloc_start=%llu",
452                            (unsigned long long)info->alloc_start);
453         if (info->thread_pool_size !=  min_t(unsigned long,
454                                              num_online_cpus() + 2, 8))
455                 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
456         if (btrfs_test_opt(root, COMPRESS))
457                 seq_puts(seq, ",compress");
458         if (btrfs_test_opt(root, NOSSD))
459                 seq_puts(seq, ",nossd");
460         if (btrfs_test_opt(root, SSD_SPREAD))
461                 seq_puts(seq, ",ssd_spread");
462         else if (btrfs_test_opt(root, SSD))
463                 seq_puts(seq, ",ssd");
464         if (btrfs_test_opt(root, NOTREELOG))
465                 seq_puts(seq, ",notreelog");
466         if (btrfs_test_opt(root, FLUSHONCOMMIT))
467                 seq_puts(seq, ",flushoncommit");
468         if (btrfs_test_opt(root, DISCARD))
469                 seq_puts(seq, ",discard");
470         if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
471                 seq_puts(seq, ",noacl");
472         return 0;
473 }
474
475 static int btrfs_test_super(struct super_block *s, void *data)
476 {
477         struct btrfs_fs_devices *test_fs_devices = data;
478         struct btrfs_root *root = btrfs_sb(s);
479
480         return root->fs_info->fs_devices == test_fs_devices;
481 }
482
483 /*
484  * Find a superblock for the given device / mount point.
485  *
486  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
487  *        for multiple device setup.  Make sure to keep it in sync.
488  */
489 static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
490                 const char *dev_name, void *data, struct vfsmount *mnt)
491 {
492         char *subvol_name = NULL;
493         struct block_device *bdev = NULL;
494         struct super_block *s;
495         struct dentry *root;
496         struct btrfs_fs_devices *fs_devices = NULL;
497         fmode_t mode = FMODE_READ;
498         int error = 0;
499
500         if (!(flags & MS_RDONLY))
501                 mode |= FMODE_WRITE;
502
503         error = btrfs_parse_early_options(data, mode, fs_type,
504                                           &subvol_name, &fs_devices);
505         if (error)
506                 return error;
507
508         error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
509         if (error)
510                 goto error_free_subvol_name;
511
512         error = btrfs_open_devices(fs_devices, mode, fs_type);
513         if (error)
514                 goto error_free_subvol_name;
515
516         if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
517                 error = -EACCES;
518                 goto error_close_devices;
519         }
520
521         bdev = fs_devices->latest_bdev;
522         s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
523         if (IS_ERR(s))
524                 goto error_s;
525
526         if (s->s_root) {
527                 if ((flags ^ s->s_flags) & MS_RDONLY) {
528                         deactivate_locked_super(s);
529                         error = -EBUSY;
530                         goto error_close_devices;
531                 }
532
533                 btrfs_close_devices(fs_devices);
534         } else {
535                 char b[BDEVNAME_SIZE];
536
537                 s->s_flags = flags;
538                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
539                 error = btrfs_fill_super(s, fs_devices, data,
540                                          flags & MS_SILENT ? 1 : 0);
541                 if (error) {
542                         deactivate_locked_super(s);
543                         goto error_free_subvol_name;
544                 }
545
546                 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
547                 s->s_flags |= MS_ACTIVE;
548         }
549
550         if (!strcmp(subvol_name, "."))
551                 root = dget(s->s_root);
552         else {
553                 mutex_lock(&s->s_root->d_inode->i_mutex);
554                 root = lookup_one_len(subvol_name, s->s_root,
555                                       strlen(subvol_name));
556                 mutex_unlock(&s->s_root->d_inode->i_mutex);
557
558                 if (IS_ERR(root)) {
559                         deactivate_locked_super(s);
560                         error = PTR_ERR(root);
561                         goto error_free_subvol_name;
562                 }
563                 if (!root->d_inode) {
564                         dput(root);
565                         deactivate_locked_super(s);
566                         error = -ENXIO;
567                         goto error_free_subvol_name;
568                 }
569         }
570
571         mnt->mnt_sb = s;
572         mnt->mnt_root = root;
573
574         kfree(subvol_name);
575         return 0;
576
577 error_s:
578         error = PTR_ERR(s);
579 error_close_devices:
580         btrfs_close_devices(fs_devices);
581 error_free_subvol_name:
582         kfree(subvol_name);
583         return error;
584 }
585
586 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
587 {
588         struct btrfs_root *root = btrfs_sb(sb);
589         int ret;
590
591         ret = btrfs_parse_options(root, data);
592         if (ret)
593                 return -EINVAL;
594
595         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
596                 return 0;
597
598         if (*flags & MS_RDONLY) {
599                 sb->s_flags |= MS_RDONLY;
600
601                 ret =  btrfs_commit_super(root);
602                 WARN_ON(ret);
603         } else {
604                 if (root->fs_info->fs_devices->rw_devices == 0)
605                         return -EACCES;
606
607                 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
608                         return -EINVAL;
609
610                 /* recover relocation */
611                 ret = btrfs_recover_relocation(root);
612                 WARN_ON(ret);
613
614                 ret = btrfs_cleanup_fs_roots(root->fs_info);
615                 WARN_ON(ret);
616
617                 sb->s_flags &= ~MS_RDONLY;
618         }
619
620         return 0;
621 }
622
623 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
624 {
625         struct btrfs_root *root = btrfs_sb(dentry->d_sb);
626         struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
627         int bits = dentry->d_sb->s_blocksize_bits;
628         __be32 *fsid = (__be32 *)root->fs_info->fsid;
629
630         buf->f_namelen = BTRFS_NAME_LEN;
631         buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
632         buf->f_bfree = buf->f_blocks -
633                 (btrfs_super_bytes_used(disk_super) >> bits);
634         buf->f_bavail = buf->f_bfree;
635         buf->f_bsize = dentry->d_sb->s_blocksize;
636         buf->f_type = BTRFS_SUPER_MAGIC;
637
638         /* We treat it as constant endianness (it doesn't matter _which_)
639            because we want the fsid to come out the same whether mounted
640            on a big-endian or little-endian host */
641         buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
642         buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
643         /* Mask in the root object ID too, to disambiguate subvols */
644         buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
645         buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
646
647         return 0;
648 }
649
650 static struct file_system_type btrfs_fs_type = {
651         .owner          = THIS_MODULE,
652         .name           = "btrfs",
653         .get_sb         = btrfs_get_sb,
654         .kill_sb        = kill_anon_super,
655         .fs_flags       = FS_REQUIRES_DEV,
656 };
657
658 /*
659  * used by btrfsctl to scan devices when no FS is mounted
660  */
661 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
662                                 unsigned long arg)
663 {
664         struct btrfs_ioctl_vol_args *vol;
665         struct btrfs_fs_devices *fs_devices;
666         int ret = -ENOTTY;
667
668         if (!capable(CAP_SYS_ADMIN))
669                 return -EPERM;
670
671         vol = memdup_user((void __user *)arg, sizeof(*vol));
672         if (IS_ERR(vol))
673                 return PTR_ERR(vol);
674
675         switch (cmd) {
676         case BTRFS_IOC_SCAN_DEV:
677                 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
678                                             &btrfs_fs_type, &fs_devices);
679                 break;
680         }
681
682         kfree(vol);
683         return ret;
684 }
685
686 static int btrfs_freeze(struct super_block *sb)
687 {
688         struct btrfs_root *root = btrfs_sb(sb);
689         mutex_lock(&root->fs_info->transaction_kthread_mutex);
690         mutex_lock(&root->fs_info->cleaner_mutex);
691         return 0;
692 }
693
694 static int btrfs_unfreeze(struct super_block *sb)
695 {
696         struct btrfs_root *root = btrfs_sb(sb);
697         mutex_unlock(&root->fs_info->cleaner_mutex);
698         mutex_unlock(&root->fs_info->transaction_kthread_mutex);
699         return 0;
700 }
701
702 static const struct super_operations btrfs_super_ops = {
703         .drop_inode     = btrfs_drop_inode,
704         .delete_inode   = btrfs_delete_inode,
705         .put_super      = btrfs_put_super,
706         .sync_fs        = btrfs_sync_fs,
707         .show_options   = btrfs_show_options,
708         .write_inode    = btrfs_write_inode,
709         .dirty_inode    = btrfs_dirty_inode,
710         .alloc_inode    = btrfs_alloc_inode,
711         .destroy_inode  = btrfs_destroy_inode,
712         .statfs         = btrfs_statfs,
713         .remount_fs     = btrfs_remount,
714         .freeze_fs      = btrfs_freeze,
715         .unfreeze_fs    = btrfs_unfreeze,
716 };
717
718 static const struct file_operations btrfs_ctl_fops = {
719         .unlocked_ioctl  = btrfs_control_ioctl,
720         .compat_ioctl = btrfs_control_ioctl,
721         .owner   = THIS_MODULE,
722 };
723
724 static struct miscdevice btrfs_misc = {
725         .minor          = MISC_DYNAMIC_MINOR,
726         .name           = "btrfs-control",
727         .fops           = &btrfs_ctl_fops
728 };
729
730 static int btrfs_interface_init(void)
731 {
732         return misc_register(&btrfs_misc);
733 }
734
735 static void btrfs_interface_exit(void)
736 {
737         if (misc_deregister(&btrfs_misc) < 0)
738                 printk(KERN_INFO "misc_deregister failed for control device");
739 }
740
741 static int __init init_btrfs_fs(void)
742 {
743         int err;
744
745         err = btrfs_init_sysfs();
746         if (err)
747                 return err;
748
749         err = btrfs_init_cachep();
750         if (err)
751                 goto free_sysfs;
752
753         err = extent_io_init();
754         if (err)
755                 goto free_cachep;
756
757         err = extent_map_init();
758         if (err)
759                 goto free_extent_io;
760
761         err = btrfs_interface_init();
762         if (err)
763                 goto free_extent_map;
764
765         err = register_filesystem(&btrfs_fs_type);
766         if (err)
767                 goto unregister_ioctl;
768
769         printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
770         return 0;
771
772 unregister_ioctl:
773         btrfs_interface_exit();
774 free_extent_map:
775         extent_map_exit();
776 free_extent_io:
777         extent_io_exit();
778 free_cachep:
779         btrfs_destroy_cachep();
780 free_sysfs:
781         btrfs_exit_sysfs();
782         return err;
783 }
784
785 static void __exit exit_btrfs_fs(void)
786 {
787         btrfs_destroy_cachep();
788         extent_map_exit();
789         extent_io_exit();
790         btrfs_interface_exit();
791         unregister_filesystem(&btrfs_fs_type);
792         btrfs_exit_sysfs();
793         btrfs_cleanup_fs_uuids();
794         btrfs_zlib_exit();
795 }
796
797 module_init(init_btrfs_fs)
798 module_exit(exit_btrfs_fs)
799
800 MODULE_LICENSE("GPL");