xref: /openbmc/linux/fs/jffs2/super.c (revision ac4cfdd6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * JFFS2 -- Journalling Flash File System, Version 2.
31da177e4SLinus Torvalds  *
4c00c310eSDavid Woodhouse  * Copyright © 2001-2007 Red Hat, Inc.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Created by David Woodhouse <dwmw2@infradead.org>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * For licensing information, see the file 'LICENCE' in this directory.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
15405f5571SAlexey Dobriyan #include <linux/smp_lock.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/list.h>
181da177e4SLinus Torvalds #include <linux/fs.h>
199c74034fSArtem Bityutskiy #include <linux/err.h>
201da177e4SLinus Torvalds #include <linux/mount.h>
211da177e4SLinus Torvalds #include <linux/jffs2.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
23acaebfd8SDavid Howells #include <linux/mtd/super.h>
241da177e4SLinus Torvalds #include <linux/ctype.h>
251da177e4SLinus Torvalds #include <linux/namei.h>
265f556aabSDavid Woodhouse #include <linux/exportfs.h>
271da177e4SLinus Torvalds #include "compr.h"
281da177e4SLinus Torvalds #include "nodelist.h"
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static void jffs2_put_super(struct super_block *);
311da177e4SLinus Torvalds 
32e18b890bSChristoph Lameter static struct kmem_cache *jffs2_inode_cachep;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static struct inode *jffs2_alloc_inode(struct super_block *sb)
351da177e4SLinus Torvalds {
364e571abaSDavid Woodhouse 	struct jffs2_inode_info *f;
374e571abaSDavid Woodhouse 
384e571abaSDavid Woodhouse 	f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
394e571abaSDavid Woodhouse 	if (!f)
401da177e4SLinus Torvalds 		return NULL;
414e571abaSDavid Woodhouse 	return &f->vfs_inode;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static void jffs2_destroy_inode(struct inode *inode)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds 
4951cc5068SAlexey Dobriyan static void jffs2_i_init_once(void *foo)
501da177e4SLinus Torvalds {
514e571abaSDavid Woodhouse 	struct jffs2_inode_info *f = foo;
521da177e4SLinus Torvalds 
534e571abaSDavid Woodhouse 	mutex_init(&f->sem);
544e571abaSDavid Woodhouse 	inode_init_once(&f->vfs_inode);
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
5701ba6875SChristoph Hellwig static void jffs2_write_super(struct super_block *sb)
5801ba6875SChristoph Hellwig {
5901ba6875SChristoph Hellwig 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
60ebc1ac16SChristoph Hellwig 
61ebc1ac16SChristoph Hellwig 	lock_super(sb);
6201ba6875SChristoph Hellwig 	sb->s_dirt = 0;
6301ba6875SChristoph Hellwig 
64ebc1ac16SChristoph Hellwig 	if (!(sb->s_flags & MS_RDONLY)) {
6501ba6875SChristoph Hellwig 		D1(printk(KERN_DEBUG "jffs2_write_super()\n"));
6601ba6875SChristoph Hellwig 		jffs2_garbage_collect_trigger(c);
6701ba6875SChristoph Hellwig 		jffs2_erase_pending_blocks(c, 0);
6801ba6875SChristoph Hellwig 		jffs2_flush_wbuf_gc(c, 0);
6901ba6875SChristoph Hellwig 	}
7001ba6875SChristoph Hellwig 
71ebc1ac16SChristoph Hellwig 	unlock_super(sb);
72ebc1ac16SChristoph Hellwig }
73ebc1ac16SChristoph Hellwig 
741da177e4SLinus Torvalds static int jffs2_sync_fs(struct super_block *sb, int wait)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
771da177e4SLinus Torvalds 
78d579ed00SChristoph Hellwig 	jffs2_write_super(sb);
79d579ed00SChristoph Hellwig 
80ced22070SDavid Woodhouse 	mutex_lock(&c->alloc_sem);
811da177e4SLinus Torvalds 	jffs2_flush_wbuf_pad(c);
82ced22070SDavid Woodhouse 	mutex_unlock(&c->alloc_sem);
831da177e4SLinus Torvalds 	return 0;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
865f556aabSDavid Woodhouse static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
875f556aabSDavid Woodhouse 					 uint32_t generation)
885f556aabSDavid Woodhouse {
895f556aabSDavid Woodhouse 	/* We don't care about i_generation. We'll destroy the flash
905f556aabSDavid Woodhouse 	   before we start re-using inode numbers anyway. And even
915f556aabSDavid Woodhouse 	   if that wasn't true, we'd have other problems...*/
925f556aabSDavid Woodhouse 	return jffs2_iget(sb, ino);
935f556aabSDavid Woodhouse }
945f556aabSDavid Woodhouse 
955f556aabSDavid Woodhouse static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
965f556aabSDavid Woodhouse 					 int fh_len, int fh_type)
975f556aabSDavid Woodhouse {
985f556aabSDavid Woodhouse         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
995f556aabSDavid Woodhouse                                     jffs2_nfs_get_inode);
1005f556aabSDavid Woodhouse }
1015f556aabSDavid Woodhouse 
1025f556aabSDavid Woodhouse static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
1035f556aabSDavid Woodhouse 					 int fh_len, int fh_type)
1045f556aabSDavid Woodhouse {
1055f556aabSDavid Woodhouse         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1065f556aabSDavid Woodhouse                                     jffs2_nfs_get_inode);
1075f556aabSDavid Woodhouse }
1085f556aabSDavid Woodhouse 
1095f556aabSDavid Woodhouse static struct dentry *jffs2_get_parent(struct dentry *child)
1105f556aabSDavid Woodhouse {
1115f556aabSDavid Woodhouse 	struct jffs2_inode_info *f;
1125f556aabSDavid Woodhouse 	uint32_t pino;
1135f556aabSDavid Woodhouse 
1145f556aabSDavid Woodhouse 	BUG_ON(!S_ISDIR(child->d_inode->i_mode));
1155f556aabSDavid Woodhouse 
1165f556aabSDavid Woodhouse 	f = JFFS2_INODE_INFO(child->d_inode);
1175f556aabSDavid Woodhouse 
1185f556aabSDavid Woodhouse 	pino = f->inocache->pino_nlink;
1195f556aabSDavid Woodhouse 
1205f556aabSDavid Woodhouse 	JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
1215f556aabSDavid Woodhouse 		    f->inocache->ino, pino);
1225f556aabSDavid Woodhouse 
1235f556aabSDavid Woodhouse 	return d_obtain_alias(jffs2_iget(child->d_inode->i_sb, pino));
1245f556aabSDavid Woodhouse }
1255f556aabSDavid Woodhouse 
126ac4cfdd6SAlexey Dobriyan static const struct export_operations jffs2_export_ops = {
1275f556aabSDavid Woodhouse 	.get_parent = jffs2_get_parent,
1285f556aabSDavid Woodhouse 	.fh_to_dentry = jffs2_fh_to_dentry,
1295f556aabSDavid Woodhouse 	.fh_to_parent = jffs2_fh_to_parent,
1305f556aabSDavid Woodhouse };
1315f556aabSDavid Woodhouse 
132ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations jffs2_super_operations =
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	.alloc_inode =	jffs2_alloc_inode,
1351da177e4SLinus Torvalds 	.destroy_inode =jffs2_destroy_inode,
1361da177e4SLinus Torvalds 	.put_super =	jffs2_put_super,
1371da177e4SLinus Torvalds 	.write_super =	jffs2_write_super,
1381da177e4SLinus Torvalds 	.statfs =	jffs2_statfs,
1391da177e4SLinus Torvalds 	.remount_fs =	jffs2_remount_fs,
1401da177e4SLinus Torvalds 	.clear_inode =	jffs2_clear_inode,
1411da177e4SLinus Torvalds 	.dirty_inode =	jffs2_dirty_inode,
1421da177e4SLinus Torvalds 	.sync_fs =	jffs2_sync_fs,
1431da177e4SLinus Torvalds };
1441da177e4SLinus Torvalds 
145acaebfd8SDavid Howells /*
146acaebfd8SDavid Howells  * fill in the superblock
147acaebfd8SDavid Howells  */
148acaebfd8SDavid Howells static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct jffs2_sb_info *c;
151acaebfd8SDavid Howells 
152acaebfd8SDavid Howells 	D1(printk(KERN_DEBUG "jffs2_get_sb_mtd():"
153acaebfd8SDavid Howells 		  " New superblock for device %d (\"%s\")\n",
154acaebfd8SDavid Howells 		  sb->s_mtd->index, sb->s_mtd->name));
1551da177e4SLinus Torvalds 
156f8314dc6SPanagiotis Issaris 	c = kzalloc(sizeof(*c), GFP_KERNEL);
1571da177e4SLinus Torvalds 	if (!c)
158454e2398SDavid Howells 		return -ENOMEM;
1591da177e4SLinus Torvalds 
160acaebfd8SDavid Howells 	c->mtd = sb->s_mtd;
161acaebfd8SDavid Howells 	c->os_priv = sb;
162acaebfd8SDavid Howells 	sb->s_fs_info = c;
1631da177e4SLinus Torvalds 
164acaebfd8SDavid Howells 	/* Initialize JFFS2 superblock locks, the further initialization will
165acaebfd8SDavid Howells 	 * be done later */
166ced22070SDavid Woodhouse 	mutex_init(&c->alloc_sem);
167ced22070SDavid Woodhouse 	mutex_init(&c->erase_free_sem);
168b6220598SArtem B. Bityuckiy 	init_waitqueue_head(&c->erase_wait);
169b6220598SArtem B. Bityuckiy 	init_waitqueue_head(&c->inocache_wq);
170b6220598SArtem B. Bityuckiy 	spin_lock_init(&c->erase_completion_lock);
171b6220598SArtem B. Bityuckiy 	spin_lock_init(&c->inocache_lock);
172b6220598SArtem B. Bityuckiy 
1731da177e4SLinus Torvalds 	sb->s_op = &jffs2_super_operations;
1745f556aabSDavid Woodhouse 	sb->s_export_op = &jffs2_export_ops;
175acaebfd8SDavid Howells 	sb->s_flags = sb->s_flags | MS_NOATIME;
176aa98d7cfSKaiGai Kohei 	sb->s_xattr = jffs2_xattr_handlers;
177aa98d7cfSKaiGai Kohei #ifdef CONFIG_JFFS2_FS_POSIX_ACL
178aa98d7cfSKaiGai Kohei 	sb->s_flags |= MS_POSIXACL;
179aa98d7cfSKaiGai Kohei #endif
180acaebfd8SDavid Howells 	return jffs2_do_fill_super(sb, data, silent);
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
183454e2398SDavid Howells static int jffs2_get_sb(struct file_system_type *fs_type,
1841da177e4SLinus Torvalds 			int flags, const char *dev_name,
185454e2398SDavid Howells 			void *data, struct vfsmount *mnt)
1861da177e4SLinus Torvalds {
187acaebfd8SDavid Howells 	return get_sb_mtd(fs_type, flags, dev_name, data, jffs2_fill_super,
188acaebfd8SDavid Howells 			  mnt);
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds static void jffs2_put_super (struct super_block *sb)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
1961da177e4SLinus Torvalds 
1976cfd0148SChristoph Hellwig 	lock_kernel();
1986cfd0148SChristoph Hellwig 
1998c85e125SChristoph Hellwig 	if (sb->s_dirt)
2008c85e125SChristoph Hellwig 		jffs2_write_super(sb);
2018c85e125SChristoph Hellwig 
202ced22070SDavid Woodhouse 	mutex_lock(&c->alloc_sem);
2031da177e4SLinus Torvalds 	jffs2_flush_wbuf_pad(c);
204ced22070SDavid Woodhouse 	mutex_unlock(&c->alloc_sem);
205e631ddbaSFerenc Havasi 
206e631ddbaSFerenc Havasi 	jffs2_sum_exit(c);
207e631ddbaSFerenc Havasi 
2081da177e4SLinus Torvalds 	jffs2_free_ino_caches(c);
2091da177e4SLinus Torvalds 	jffs2_free_raw_node_refs(c);
2104ce1f562SFerenc Havasi 	if (jffs2_blocks_use_vmalloc(c))
2111da177e4SLinus Torvalds 		vfree(c->blocks);
2121da177e4SLinus Torvalds 	else
2131da177e4SLinus Torvalds 		kfree(c->blocks);
2141da177e4SLinus Torvalds 	jffs2_flash_cleanup(c);
2151da177e4SLinus Torvalds 	kfree(c->inocache_list);
216aa98d7cfSKaiGai Kohei 	jffs2_clear_xattr_subsystem(c);
2171da177e4SLinus Torvalds 	if (c->mtd->sync)
2181da177e4SLinus Torvalds 		c->mtd->sync(c->mtd);
2191da177e4SLinus Torvalds 
2206cfd0148SChristoph Hellwig 	unlock_kernel();
2216cfd0148SChristoph Hellwig 
2221da177e4SLinus Torvalds 	D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static void jffs2_kill_sb(struct super_block *sb)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
228a69dde91SArtem B. Bityuckiy 	if (!(sb->s_flags & MS_RDONLY))
229a69dde91SArtem B. Bityuckiy 		jffs2_stop_garbage_collect_thread(c);
230acaebfd8SDavid Howells 	kill_mtd_super(sb);
2311da177e4SLinus Torvalds 	kfree(c);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static struct file_system_type jffs2_fs_type = {
2351da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
2361da177e4SLinus Torvalds 	.name =		"jffs2",
2371da177e4SLinus Torvalds 	.get_sb =	jffs2_get_sb,
2381da177e4SLinus Torvalds 	.kill_sb =	jffs2_kill_sb,
2391da177e4SLinus Torvalds };
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds static int __init init_jffs2_fs(void)
2421da177e4SLinus Torvalds {
2431da177e4SLinus Torvalds 	int ret;
2441da177e4SLinus Torvalds 
2453e68fbb5SDavid Woodhouse 	/* Paranoia checks for on-medium structures. If we ask GCC
2463e68fbb5SDavid Woodhouse 	   to pack them with __attribute__((packed)) then it _also_
2473e68fbb5SDavid Woodhouse 	   assumes that they're not aligned -- so it emits crappy
2483e68fbb5SDavid Woodhouse 	   code on some architectures. Ideally we want an attribute
2493e68fbb5SDavid Woodhouse 	   which means just 'no padding', without the alignment
2503e68fbb5SDavid Woodhouse 	   thing. But GCC doesn't have that -- we have to just
2513e68fbb5SDavid Woodhouse 	   hope the structs are the right sizes, instead. */
2522ecd05aeSAlexey Dobriyan 	BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
2532ecd05aeSAlexey Dobriyan 	BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
2542ecd05aeSAlexey Dobriyan 	BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
2552ecd05aeSAlexey Dobriyan 	BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
2563e68fbb5SDavid Woodhouse 
2571da177e4SLinus Torvalds 	printk(KERN_INFO "JFFS2 version 2.2."
2582f82ce1eSAndrew Victor #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
2591da177e4SLinus Torvalds 	       " (NAND)"
2601da177e4SLinus Torvalds #endif
261e631ddbaSFerenc Havasi #ifdef CONFIG_JFFS2_SUMMARY
262e631ddbaSFerenc Havasi 	       " (SUMMARY) "
263e631ddbaSFerenc Havasi #endif
264c00c310eSDavid Woodhouse 	       " © 2001-2006 Red Hat, Inc.\n");
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	jffs2_inode_cachep = kmem_cache_create("jffs2_i",
2671da177e4SLinus Torvalds 					     sizeof(struct jffs2_inode_info),
268fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
269fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
27020c2df83SPaul Mundt 					     jffs2_i_init_once);
2711da177e4SLinus Torvalds 	if (!jffs2_inode_cachep) {
2721da177e4SLinus Torvalds 		printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
2731da177e4SLinus Torvalds 		return -ENOMEM;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	ret = jffs2_compressors_init();
2761da177e4SLinus Torvalds 	if (ret) {
2771da177e4SLinus Torvalds 		printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
2781da177e4SLinus Torvalds 		goto out;
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 	ret = jffs2_create_slab_caches();
2811da177e4SLinus Torvalds 	if (ret) {
2821da177e4SLinus Torvalds 		printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
2831da177e4SLinus Torvalds 		goto out_compressors;
2841da177e4SLinus Torvalds 	}
2851da177e4SLinus Torvalds 	ret = register_filesystem(&jffs2_fs_type);
2861da177e4SLinus Torvalds 	if (ret) {
2871da177e4SLinus Torvalds 		printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
2881da177e4SLinus Torvalds 		goto out_slab;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 	return 0;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds  out_slab:
2931da177e4SLinus Torvalds 	jffs2_destroy_slab_caches();
2941da177e4SLinus Torvalds  out_compressors:
2951da177e4SLinus Torvalds 	jffs2_compressors_exit();
2961da177e4SLinus Torvalds  out:
2971da177e4SLinus Torvalds 	kmem_cache_destroy(jffs2_inode_cachep);
2981da177e4SLinus Torvalds 	return ret;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds static void __exit exit_jffs2_fs(void)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	unregister_filesystem(&jffs2_fs_type);
3041da177e4SLinus Torvalds 	jffs2_destroy_slab_caches();
3051da177e4SLinus Torvalds 	jffs2_compressors_exit();
3061da177e4SLinus Torvalds 	kmem_cache_destroy(jffs2_inode_cachep);
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds module_init(init_jffs2_fs);
3101da177e4SLinus Torvalds module_exit(exit_jffs2_fs);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds MODULE_DESCRIPTION("The Journalling Flash File System, v2");
3131da177e4SLinus Torvalds MODULE_AUTHOR("Red Hat, Inc.");
3141da177e4SLinus Torvalds MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
3151da177e4SLinus Torvalds 		       // the sake of this tag. It's Free Software.
316