super.c 5.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
4
 * Copyright © 2001-2007 Red Hat, Inc.
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Created by David Woodhouse <dwmw2@infradead.org>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/fs.h>
18
#include <linux/err.h>
L
Linus Torvalds 已提交
19 20 21
#include <linux/mount.h>
#include <linux/jffs2.h>
#include <linux/pagemap.h>
22
#include <linux/mtd/super.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29
#include <linux/ctype.h>
#include <linux/namei.h>
#include "compr.h"
#include "nodelist.h"

static void jffs2_put_super(struct super_block *);

30
static struct kmem_cache *jffs2_inode_cachep;
L
Linus Torvalds 已提交
31 32 33

static struct inode *jffs2_alloc_inode(struct super_block *sb)
{
34 35 36 37
	struct jffs2_inode_info *f;

	f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
	if (!f)
L
Linus Torvalds 已提交
38
		return NULL;
39
	return &f->vfs_inode;
L
Linus Torvalds 已提交
40 41 42 43 44 45 46
}

static void jffs2_destroy_inode(struct inode *inode)
{
	kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
}

47
static void jffs2_i_init_once(struct kmem_cache *cachep, void *foo)
L
Linus Torvalds 已提交
48
{
49
	struct jffs2_inode_info *f = foo;
L
Linus Torvalds 已提交
50

51 52
	mutex_init(&f->sem);
	inode_init_once(&f->vfs_inode);
L
Linus Torvalds 已提交
53 54 55 56 57 58
}

static int jffs2_sync_fs(struct super_block *sb, int wait)
{
	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);

59
	mutex_lock(&c->alloc_sem);
L
Linus Torvalds 已提交
60
	jffs2_flush_wbuf_pad(c);
61
	mutex_unlock(&c->alloc_sem);
L
Linus Torvalds 已提交
62 63 64
	return 0;
}

65
static const struct super_operations jffs2_super_operations =
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76 77
{
	.alloc_inode =	jffs2_alloc_inode,
	.destroy_inode =jffs2_destroy_inode,
	.put_super =	jffs2_put_super,
	.write_super =	jffs2_write_super,
	.statfs =	jffs2_statfs,
	.remount_fs =	jffs2_remount_fs,
	.clear_inode =	jffs2_clear_inode,
	.dirty_inode =	jffs2_dirty_inode,
	.sync_fs =	jffs2_sync_fs,
};

78 79 80 81
/*
 * fill in the superblock
 */
static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
L
Linus Torvalds 已提交
82 83
{
	struct jffs2_sb_info *c;
84 85 86 87

	D1(printk(KERN_DEBUG "jffs2_get_sb_mtd():"
		  " New superblock for device %d (\"%s\")\n",
		  sb->s_mtd->index, sb->s_mtd->name));
L
Linus Torvalds 已提交
88

89
	c = kzalloc(sizeof(*c), GFP_KERNEL);
L
Linus Torvalds 已提交
90
	if (!c)
91
		return -ENOMEM;
L
Linus Torvalds 已提交
92

93 94 95
	c->mtd = sb->s_mtd;
	c->os_priv = sb;
	sb->s_fs_info = c;
L
Linus Torvalds 已提交
96

97 98
	/* Initialize JFFS2 superblock locks, the further initialization will
	 * be done later */
99 100
	mutex_init(&c->alloc_sem);
	mutex_init(&c->erase_free_sem);
101 102 103 104 105
	init_waitqueue_head(&c->erase_wait);
	init_waitqueue_head(&c->inocache_wq);
	spin_lock_init(&c->erase_completion_lock);
	spin_lock_init(&c->inocache_lock);

L
Linus Torvalds 已提交
106
	sb->s_op = &jffs2_super_operations;
107
	sb->s_flags = sb->s_flags | MS_NOATIME;
108 109 110 111
	sb->s_xattr = jffs2_xattr_handlers;
#ifdef CONFIG_JFFS2_FS_POSIX_ACL
	sb->s_flags |= MS_POSIXACL;
#endif
112
	return jffs2_do_fill_super(sb, data, silent);
L
Linus Torvalds 已提交
113 114
}

115 116 117
static int jffs2_get_sb(struct file_system_type *fs_type,
			int flags, const char *dev_name,
			void *data, struct vfsmount *mnt)
L
Linus Torvalds 已提交
118
{
119 120
	return get_sb_mtd(fs_type, flags, dev_name, data, jffs2_fill_super,
			  mnt);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128
}

static void jffs2_put_super (struct super_block *sb)
{
	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);

	D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));

129
	mutex_lock(&c->alloc_sem);
L
Linus Torvalds 已提交
130
	jffs2_flush_wbuf_pad(c);
131
	mutex_unlock(&c->alloc_sem);
132 133 134

	jffs2_sum_exit(c);

L
Linus Torvalds 已提交
135 136
	jffs2_free_ino_caches(c);
	jffs2_free_raw_node_refs(c);
137
	if (jffs2_blocks_use_vmalloc(c))
L
Linus Torvalds 已提交
138 139 140 141 142
		vfree(c->blocks);
	else
		kfree(c->blocks);
	jffs2_flash_cleanup(c);
	kfree(c->inocache_list);
143
	jffs2_clear_xattr_subsystem(c);
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152
	if (c->mtd->sync)
		c->mtd->sync(c->mtd);

	D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
}

static void jffs2_kill_sb(struct super_block *sb)
{
	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
153 154
	if (!(sb->s_flags & MS_RDONLY))
		jffs2_stop_garbage_collect_thread(c);
155
	kill_mtd_super(sb);
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169
	kfree(c);
}

static struct file_system_type jffs2_fs_type = {
	.owner =	THIS_MODULE,
	.name =		"jffs2",
	.get_sb =	jffs2_get_sb,
	.kill_sb =	jffs2_kill_sb,
};

static int __init init_jffs2_fs(void)
{
	int ret;

170 171 172 173 174 175 176
	/* Paranoia checks for on-medium structures. If we ask GCC
	   to pack them with __attribute__((packed)) then it _also_
	   assumes that they're not aligned -- so it emits crappy
	   code on some architectures. Ideally we want an attribute
	   which means just 'no padding', without the alignment
	   thing. But GCC doesn't have that -- we have to just
	   hope the structs are the right sizes, instead. */
A
Alexey Dobriyan 已提交
177 178 179 180
	BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
	BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
	BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
	BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
181

L
Linus Torvalds 已提交
182
	printk(KERN_INFO "JFFS2 version 2.2."
183
#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
L
Linus Torvalds 已提交
184
	       " (NAND)"
185 186 187
#endif
#ifdef CONFIG_JFFS2_SUMMARY
	       " (SUMMARY) "
L
Linus Torvalds 已提交
188
#endif
189
	       " © 2001-2006 Red Hat, Inc.\n");
L
Linus Torvalds 已提交
190 191 192

	jffs2_inode_cachep = kmem_cache_create("jffs2_i",
					     sizeof(struct jffs2_inode_info),
193 194
					     0, (SLAB_RECLAIM_ACCOUNT|
						SLAB_MEM_SPREAD),
195
					     jffs2_i_init_once);
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	if (!jffs2_inode_cachep) {
		printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
		return -ENOMEM;
	}
	ret = jffs2_compressors_init();
	if (ret) {
		printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
		goto out;
	}
	ret = jffs2_create_slab_caches();
	if (ret) {
		printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
		goto out_compressors;
	}
	ret = register_filesystem(&jffs2_fs_type);
	if (ret) {
		printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
		goto out_slab;
	}
	return 0;

 out_slab:
	jffs2_destroy_slab_caches();
 out_compressors:
	jffs2_compressors_exit();
 out:
	kmem_cache_destroy(jffs2_inode_cachep);
	return ret;
}

static void __exit exit_jffs2_fs(void)
{
	unregister_filesystem(&jffs2_fs_type);
	jffs2_destroy_slab_caches();
	jffs2_compressors_exit();
	kmem_cache_destroy(jffs2_inode_cachep);
}

module_init(init_jffs2_fs);
module_exit(exit_jffs2_fs);

MODULE_DESCRIPTION("The Journalling Flash File System, v2");
MODULE_AUTHOR("Red Hat, Inc.");
239
MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
L
Linus Torvalds 已提交
240
		       // the sake of this tag. It's Free Software.