xref: /linux/fs/jffs2/super.c (revision af8e51644a70f612974a6e767fa7d896d3c23f88)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@infradead.org>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  */
11 
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/fs.h>
20 #include <linux/err.h>
21 #include <linux/mount.h>
22 #include <linux/fs_context.h>
23 #include <linux/fs_parser.h>
24 #include <linux/jffs2.h>
25 #include <linux/pagemap.h>
26 #include <linux/mtd/super.h>
27 #include <linux/ctype.h>
28 #include <linux/namei.h>
29 #include <linux/seq_file.h>
30 #include <linux/exportfs.h>
31 #include "compr.h"
32 #include "nodelist.h"
33 
34 static void jffs2_put_super(struct super_block *);
35 
36 static struct kmem_cache *jffs2_inode_cachep;
37 
38 static struct inode *jffs2_alloc_inode(struct super_block *sb)
39 {
40 	struct jffs2_inode_info *f;
41 
42 	f = alloc_inode_sb(sb, jffs2_inode_cachep, GFP_KERNEL);
43 	if (!f)
44 		return NULL;
45 	return &f->vfs_inode;
46 }
47 
48 static void jffs2_free_inode(struct inode *inode)
49 {
50 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
51 
52 	kfree(f->target);
53 	kmem_cache_free(jffs2_inode_cachep, f);
54 }
55 
56 static void jffs2_i_init_once(void *foo)
57 {
58 	struct jffs2_inode_info *f = foo;
59 
60 	mutex_init(&f->sem);
61 	f->target = NULL;
62 	inode_init_once(&f->vfs_inode);
63 }
64 
65 static const char *jffs2_compr_name(unsigned int compr)
66 {
67 	switch (compr) {
68 	case JFFS2_COMPR_MODE_NONE:
69 		return "none";
70 #ifdef CONFIG_JFFS2_LZO
71 	case JFFS2_COMPR_MODE_FORCELZO:
72 		return "lzo";
73 #endif
74 #ifdef CONFIG_JFFS2_ZLIB
75 	case JFFS2_COMPR_MODE_FORCEZLIB:
76 		return "zlib";
77 #endif
78 	default:
79 		/* should never happen; programmer error */
80 		WARN_ON(1);
81 		return "";
82 	}
83 }
84 
85 static int jffs2_show_options(struct seq_file *s, struct dentry *root)
86 {
87 	struct jffs2_sb_info *c = JFFS2_SB_INFO(root->d_sb);
88 	struct jffs2_mount_opts *opts = &c->mount_opts;
89 
90 	if (opts->override_compr)
91 		seq_printf(s, ",compr=%s", jffs2_compr_name(opts->compr));
92 	if (opts->set_rp_size)
93 		seq_printf(s, ",rp_size=%u", opts->rp_size / 1024);
94 
95 	return 0;
96 }
97 
98 static int jffs2_sync_fs(struct super_block *sb, int wait)
99 {
100 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
101 
102 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
103 	if (jffs2_is_writebuffered(c))
104 		cancel_delayed_work_sync(&c->wbuf_dwork);
105 #endif
106 
107 	mutex_lock(&c->alloc_sem);
108 	jffs2_flush_wbuf_pad(c);
109 	mutex_unlock(&c->alloc_sem);
110 	return 0;
111 }
112 
113 static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
114 					 uint32_t generation)
115 {
116 	/* We don't care about i_generation. We'll destroy the flash
117 	   before we start re-using inode numbers anyway. And even
118 	   if that wasn't true, we'd have other problems...*/
119 	return jffs2_iget(sb, ino);
120 }
121 
122 static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
123 					 int fh_len, int fh_type)
124 {
125         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
126                                     jffs2_nfs_get_inode);
127 }
128 
129 static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
130 					 int fh_len, int fh_type)
131 {
132         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
133                                     jffs2_nfs_get_inode);
134 }
135 
136 static struct dentry *jffs2_get_parent(struct dentry *child)
137 {
138 	struct jffs2_inode_info *f;
139 	uint32_t pino;
140 
141 	BUG_ON(!d_is_dir(child));
142 
143 	f = JFFS2_INODE_INFO(d_inode(child));
144 
145 	pino = f->inocache->pino_nlink;
146 
147 	JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
148 		    f->inocache->ino, pino);
149 
150 	return d_obtain_alias(jffs2_iget(child->d_sb, pino));
151 }
152 
153 static const struct export_operations jffs2_export_ops = {
154 	.encode_fh = generic_encode_ino32_fh,
155 	.get_parent = jffs2_get_parent,
156 	.fh_to_dentry = jffs2_fh_to_dentry,
157 	.fh_to_parent = jffs2_fh_to_parent,
158 };
159 
160 /*
161  * JFFS2 mount options.
162  *
163  * Opt_source: The source device
164  * Opt_override_compr: override default compressor
165  * Opt_rp_size: size of reserved pool in KiB
166  */
167 enum {
168 	Opt_override_compr,
169 	Opt_rp_size,
170 };
171 
172 static const struct constant_table jffs2_param_compr[] = {
173 	{"none",	JFFS2_COMPR_MODE_NONE },
174 #ifdef CONFIG_JFFS2_LZO
175 	{"lzo",		JFFS2_COMPR_MODE_FORCELZO },
176 #endif
177 #ifdef CONFIG_JFFS2_ZLIB
178 	{"zlib",	JFFS2_COMPR_MODE_FORCEZLIB },
179 #endif
180 	{}
181 };
182 
183 static const struct fs_parameter_spec jffs2_fs_parameters[] = {
184 	fsparam_enum	("compr",	Opt_override_compr, jffs2_param_compr),
185 	fsparam_u32	("rp_size",	Opt_rp_size),
186 	{}
187 };
188 
189 static int jffs2_parse_param(struct fs_context *fc, struct fs_parameter *param)
190 {
191 	struct fs_parse_result result;
192 	struct jffs2_sb_info *c = fc->s_fs_info;
193 	int opt;
194 
195 	opt = fs_parse(fc, jffs2_fs_parameters, param, &result);
196 	if (opt < 0)
197 		return opt;
198 
199 	switch (opt) {
200 	case Opt_override_compr:
201 		c->mount_opts.compr = result.uint_32;
202 		c->mount_opts.override_compr = true;
203 		break;
204 	case Opt_rp_size:
205 		if (result.uint_32 > UINT_MAX / 1024)
206 			return invalf(fc, "jffs2: rp_size unrepresentable");
207 		c->mount_opts.rp_size = result.uint_32 * 1024;
208 		c->mount_opts.set_rp_size = true;
209 		break;
210 	default:
211 		return -EINVAL;
212 	}
213 
214 	return 0;
215 }
216 
217 static inline void jffs2_update_mount_opts(struct fs_context *fc)
218 {
219 	struct jffs2_sb_info *new_c = fc->s_fs_info;
220 	struct jffs2_sb_info *c = JFFS2_SB_INFO(fc->root->d_sb);
221 
222 	mutex_lock(&c->alloc_sem);
223 	if (new_c->mount_opts.override_compr) {
224 		c->mount_opts.override_compr = new_c->mount_opts.override_compr;
225 		c->mount_opts.compr = new_c->mount_opts.compr;
226 	}
227 	if (new_c->mount_opts.set_rp_size) {
228 		c->mount_opts.set_rp_size = new_c->mount_opts.set_rp_size;
229 		c->mount_opts.rp_size = new_c->mount_opts.rp_size;
230 	}
231 	mutex_unlock(&c->alloc_sem);
232 }
233 
234 static int jffs2_reconfigure(struct fs_context *fc)
235 {
236 	struct super_block *sb = fc->root->d_sb;
237 
238 	sync_filesystem(sb);
239 	jffs2_update_mount_opts(fc);
240 
241 	return jffs2_do_remount_fs(sb, fc);
242 }
243 
244 static const struct super_operations jffs2_super_operations =
245 {
246 	.alloc_inode =	jffs2_alloc_inode,
247 	.free_inode =	jffs2_free_inode,
248 	.put_super =	jffs2_put_super,
249 	.statfs =	jffs2_statfs,
250 	.evict_inode =	jffs2_evict_inode,
251 	.dirty_inode =	jffs2_dirty_inode,
252 	.show_options =	jffs2_show_options,
253 	.sync_fs =	jffs2_sync_fs,
254 };
255 
256 /*
257  * fill in the superblock
258  */
259 static int jffs2_fill_super(struct super_block *sb, struct fs_context *fc)
260 {
261 	struct jffs2_sb_info *c = sb->s_fs_info;
262 
263 	jffs2_dbg(1, "jffs2_get_sb_mtd():"
264 		  " New superblock for device %d (\"%s\")\n",
265 		  sb->s_mtd->index, sb->s_mtd->name);
266 
267 	c->mtd = sb->s_mtd;
268 	c->os_priv = sb;
269 
270 	if (c->mount_opts.rp_size > c->mtd->size)
271 		return invalf(fc, "jffs2: Too large reserve pool specified, max is %llu KB",
272 			      c->mtd->size / 1024);
273 
274 	/* Initialize JFFS2 superblock locks, the further initialization will
275 	 * be done later */
276 	mutex_init(&c->alloc_sem);
277 	mutex_init(&c->erase_free_sem);
278 	init_waitqueue_head(&c->erase_wait);
279 	init_waitqueue_head(&c->inocache_wq);
280 	spin_lock_init(&c->erase_completion_lock);
281 	spin_lock_init(&c->inocache_lock);
282 
283 	sb->s_op = &jffs2_super_operations;
284 	sb->s_export_op = &jffs2_export_ops;
285 	sb->s_flags = sb->s_flags | SB_NOATIME;
286 	sb->s_xattr = jffs2_xattr_handlers;
287 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
288 	sb->s_flags |= SB_POSIXACL;
289 #endif
290 	return jffs2_do_fill_super(sb, fc);
291 }
292 
293 static int jffs2_get_tree(struct fs_context *fc)
294 {
295 	return get_tree_mtd(fc, jffs2_fill_super);
296 }
297 
298 static void jffs2_free_fc(struct fs_context *fc)
299 {
300 	kfree(fc->s_fs_info);
301 }
302 
303 static const struct fs_context_operations jffs2_context_ops = {
304 	.free		= jffs2_free_fc,
305 	.parse_param	= jffs2_parse_param,
306 	.get_tree	= jffs2_get_tree,
307 	.reconfigure	= jffs2_reconfigure,
308 };
309 
310 static int jffs2_init_fs_context(struct fs_context *fc)
311 {
312 	struct jffs2_sb_info *ctx;
313 
314 	ctx = kzalloc(sizeof(struct jffs2_sb_info), GFP_KERNEL);
315 	if (!ctx)
316 		return -ENOMEM;
317 
318 	fc->s_fs_info = ctx;
319 	fc->ops = &jffs2_context_ops;
320 	return 0;
321 }
322 
323 static void jffs2_put_super (struct super_block *sb)
324 {
325 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
326 
327 	jffs2_dbg(2, "%s()\n", __func__);
328 
329 	mutex_lock(&c->alloc_sem);
330 	jffs2_flush_wbuf_pad(c);
331 	mutex_unlock(&c->alloc_sem);
332 
333 	jffs2_sum_exit(c);
334 
335 	jffs2_free_ino_caches(c);
336 	jffs2_free_raw_node_refs(c);
337 	kvfree(c->blocks);
338 	jffs2_flash_cleanup(c);
339 	kfree(c->inocache_list);
340 	jffs2_clear_xattr_subsystem(c);
341 	mtd_sync(c->mtd);
342 	jffs2_dbg(1, "%s(): returning\n", __func__);
343 }
344 
345 static void jffs2_kill_sb(struct super_block *sb)
346 {
347 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
348 	if (c && !sb_rdonly(sb))
349 		jffs2_stop_garbage_collect_thread(c);
350 	kill_mtd_super(sb);
351 	kfree(c);
352 }
353 
354 static struct file_system_type jffs2_fs_type = {
355 	.owner =	THIS_MODULE,
356 	.name =		"jffs2",
357 	.init_fs_context = jffs2_init_fs_context,
358 	.parameters =	jffs2_fs_parameters,
359 	.kill_sb =	jffs2_kill_sb,
360 };
361 MODULE_ALIAS_FS("jffs2");
362 
363 static int __init init_jffs2_fs(void)
364 {
365 	int ret;
366 
367 	/* Paranoia checks for on-medium structures. If we ask GCC
368 	   to pack them with __attribute__((packed)) then it _also_
369 	   assumes that they're not aligned -- so it emits crappy
370 	   code on some architectures. Ideally we want an attribute
371 	   which means just 'no padding', without the alignment
372 	   thing. But GCC doesn't have that -- we have to just
373 	   hope the structs are the right sizes, instead. */
374 	BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
375 	BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
376 	BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
377 	BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
378 
379 	pr_info("version 2.2."
380 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
381 	       " (NAND)"
382 #endif
383 #ifdef CONFIG_JFFS2_SUMMARY
384 	       " (SUMMARY) "
385 #endif
386 	       " © 2001-2006 Red Hat, Inc.\n");
387 
388 	jffs2_inode_cachep = kmem_cache_create("jffs2_i",
389 					     sizeof(struct jffs2_inode_info),
390 					     0, (SLAB_RECLAIM_ACCOUNT|
391 						SLAB_ACCOUNT),
392 					     jffs2_i_init_once);
393 	if (!jffs2_inode_cachep) {
394 		pr_err("error: Failed to initialise inode cache\n");
395 		return -ENOMEM;
396 	}
397 	ret = jffs2_compressors_init();
398 	if (ret) {
399 		pr_err("error: Failed to initialise compressors\n");
400 		goto out;
401 	}
402 	ret = jffs2_create_slab_caches();
403 	if (ret) {
404 		pr_err("error: Failed to initialise slab caches\n");
405 		goto out_compressors;
406 	}
407 	ret = register_filesystem(&jffs2_fs_type);
408 	if (ret) {
409 		pr_err("error: Failed to register filesystem\n");
410 		goto out_slab;
411 	}
412 	return 0;
413 
414  out_slab:
415 	jffs2_destroy_slab_caches();
416  out_compressors:
417 	jffs2_compressors_exit();
418  out:
419 	kmem_cache_destroy(jffs2_inode_cachep);
420 	return ret;
421 }
422 
423 static void __exit exit_jffs2_fs(void)
424 {
425 	unregister_filesystem(&jffs2_fs_type);
426 	jffs2_destroy_slab_caches();
427 	jffs2_compressors_exit();
428 
429 	/*
430 	 * Make sure all delayed rcu free inodes are flushed before we
431 	 * destroy cache.
432 	 */
433 	rcu_barrier();
434 	kmem_cache_destroy(jffs2_inode_cachep);
435 }
436 
437 module_init(init_jffs2_fs);
438 module_exit(exit_jffs2_fs);
439 
440 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
441 MODULE_AUTHOR("Red Hat, Inc.");
442 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
443 		       // the sake of this tag. It's Free Software.
444