1/* Block- or MTD-based romfs
2 *
3 * Copyright © 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * Derived from: ROMFS file system, Linux implementation
7 *
8 * Copyright © 1997-1999  Janos Farkas <chexum@shadow.banki.hu>
9 *
10 * Using parts of the minix filesystem
11 * Copyright © 1991, 1992  Linus Torvalds
12 *
13 * and parts of the affs filesystem additionally
14 * Copyright © 1993  Ray Burr
15 * Copyright © 1996  Hans-Joachim Widmaier
16 *
17 * Changes
18 *					Changed for 2.1.19 modules
19 *	Jan 1997			Initial release
20 *	Jun 1997			2.1.43+ changes
21 *					Proper page locking in readpage
22 *					Changed to work with 2.1.45+ fs
23 *	Jul 1997			Fixed follow_link
24 *			2.1.47
25 *					lookup shouldn't return -ENOENT
26 *					from Horst von Brand:
27 *					  fail on wrong checksum
28 *					  double unlock_super was possible
29 *					  correct namelen for statfs
30 *					spotted by Bill Hawes:
31 *					  readlink shouldn't iput()
32 *	Jun 1998	2.1.106		from Avery Pennarun: glibc scandir()
33 *					  exposed a problem in readdir
34 *			2.1.107		code-freeze spellchecker run
35 *	Aug 1998			2.1.118+ VFS changes
36 *	Sep 1998	2.1.122		another VFS change (follow_link)
37 *	Apr 1999	2.2.7		no more EBADF checking in
38 *					  lookup/readdir, use ERR_PTR
39 *	Jun 1999	2.3.6		d_alloc_root use changed
40 *			2.3.9		clean up usage of ENOENT/negative
41 *					  dentries in lookup
42 *					clean up page flags setting
43 *					  (error, uptodate, locking) in
44 *					  in readpage
45 *					use init_special_inode for
46 *					  fifos/sockets (and streamline) in
47 *					  read_inode, fix _ops table order
48 *	Aug 1999	2.3.16		__initfunc() => __init change
49 *	Oct 1999	2.3.24		page->owner hack obsoleted
50 *	Nov 1999	2.3.27		2.3.25+ page->offset => index change
51 *
52 *
53 * This program is free software; you can redistribute it and/or
54 * modify it under the terms of the GNU General Public Licence
55 * as published by the Free Software Foundation; either version
56 * 2 of the Licence, or (at your option) any later version.
57 */
58
59#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
60
61#include <linux/module.h>
62#include <linux/string.h>
63#include <linux/fs.h>
64#include <linux/time.h>
65#include <linux/slab.h>
66#include <linux/init.h>
67#include <linux/blkdev.h>
68#include <linux/parser.h>
69#include <linux/mount.h>
70#include <linux/namei.h>
71#include <linux/statfs.h>
72#include <linux/mtd/super.h>
73#include <linux/ctype.h>
74#include <linux/highmem.h>
75#include <linux/pagemap.h>
76#include <linux/uaccess.h>
77#include "internal.h"
78
79static struct kmem_cache *romfs_inode_cachep;
80
81static const umode_t romfs_modemap[8] = {
82	0,			/* hard link */
83	S_IFDIR  | 0644,	/* directory */
84	S_IFREG  | 0644,	/* regular file */
85	S_IFLNK  | 0777,	/* symlink */
86	S_IFBLK  | 0600,	/* blockdev */
87	S_IFCHR  | 0600,	/* chardev */
88	S_IFSOCK | 0644,	/* socket */
89	S_IFIFO  | 0644		/* FIFO */
90};
91
92static const unsigned char romfs_dtype_table[] = {
93	DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_SOCK, DT_FIFO
94};
95
96static struct inode *romfs_iget(struct super_block *sb, unsigned long pos);
97
98/*
99 * read a page worth of data from the image
100 */
101static int romfs_readpage(struct file *file, struct page *page)
102{
103	struct inode *inode = page->mapping->host;
104	loff_t offset, size;
105	unsigned long fillsize, pos;
106	void *buf;
107	int ret;
108
109	buf = kmap(page);
110	if (!buf)
111		return -ENOMEM;
112
113	/* 32 bit warning -- but not for us :) */
114	offset = page_offset(page);
115	size = i_size_read(inode);
116	fillsize = 0;
117	ret = 0;
118	if (offset < size) {
119		size -= offset;
120		fillsize = size > PAGE_SIZE ? PAGE_SIZE : size;
121
122		pos = ROMFS_I(inode)->i_dataoffset + offset;
123
124		ret = romfs_dev_read(inode->i_sb, pos, buf, fillsize);
125		if (ret < 0) {
126			SetPageError(page);
127			fillsize = 0;
128			ret = -EIO;
129		}
130	}
131
132	if (fillsize < PAGE_SIZE)
133		memset(buf + fillsize, 0, PAGE_SIZE - fillsize);
134	if (ret == 0)
135		SetPageUptodate(page);
136
137	flush_dcache_page(page);
138	kunmap(page);
139	unlock_page(page);
140	return ret;
141}
142
143static const struct address_space_operations romfs_aops = {
144	.readpage	= romfs_readpage
145};
146
147/*
148 * read the entries from a directory
149 */
150static int romfs_readdir(struct file *file, struct dir_context *ctx)
151{
152	struct inode *i = file_inode(file);
153	struct romfs_inode ri;
154	unsigned long offset, maxoff;
155	int j, ino, nextfh;
156	char fsname[ROMFS_MAXFN];	/* XXX dynamic? */
157	int ret;
158
159	maxoff = romfs_maxsize(i->i_sb);
160
161	offset = ctx->pos;
162	if (!offset) {
163		offset = i->i_ino & ROMFH_MASK;
164		ret = romfs_dev_read(i->i_sb, offset, &ri, ROMFH_SIZE);
165		if (ret < 0)
166			goto out;
167		offset = be32_to_cpu(ri.spec) & ROMFH_MASK;
168	}
169
170	/* Not really failsafe, but we are read-only... */
171	for (;;) {
172		if (!offset || offset >= maxoff) {
173			offset = maxoff;
174			ctx->pos = offset;
175			goto out;
176		}
177		ctx->pos = offset;
178
179		/* Fetch inode info */
180		ret = romfs_dev_read(i->i_sb, offset, &ri, ROMFH_SIZE);
181		if (ret < 0)
182			goto out;
183
184		j = romfs_dev_strnlen(i->i_sb, offset + ROMFH_SIZE,
185				      sizeof(fsname) - 1);
186		if (j < 0)
187			goto out;
188
189		ret = romfs_dev_read(i->i_sb, offset + ROMFH_SIZE, fsname, j);
190		if (ret < 0)
191			goto out;
192		fsname[j] = '\0';
193
194		ino = offset;
195		nextfh = be32_to_cpu(ri.next);
196		if ((nextfh & ROMFH_TYPE) == ROMFH_HRD)
197			ino = be32_to_cpu(ri.spec);
198		if (!dir_emit(ctx, fsname, j, ino,
199			    romfs_dtype_table[nextfh & ROMFH_TYPE]))
200			goto out;
201
202		offset = nextfh & ROMFH_MASK;
203	}
204out:
205	return 0;
206}
207
208/*
209 * look up an entry in a directory
210 */
211static struct dentry *romfs_lookup(struct inode *dir, struct dentry *dentry,
212				   unsigned int flags)
213{
214	unsigned long offset, maxoff;
215	struct inode *inode;
216	struct romfs_inode ri;
217	const char *name;		/* got from dentry */
218	int len, ret;
219
220	offset = dir->i_ino & ROMFH_MASK;
221	ret = romfs_dev_read(dir->i_sb, offset, &ri, ROMFH_SIZE);
222	if (ret < 0)
223		goto error;
224
225	/* search all the file entries in the list starting from the one
226	 * pointed to by the directory's special data */
227	maxoff = romfs_maxsize(dir->i_sb);
228	offset = be32_to_cpu(ri.spec) & ROMFH_MASK;
229
230	name = dentry->d_name.name;
231	len = dentry->d_name.len;
232
233	for (;;) {
234		if (!offset || offset >= maxoff)
235			goto out0;
236
237		ret = romfs_dev_read(dir->i_sb, offset, &ri, sizeof(ri));
238		if (ret < 0)
239			goto error;
240
241		/* try to match the first 16 bytes of name */
242		ret = romfs_dev_strcmp(dir->i_sb, offset + ROMFH_SIZE, name,
243				       len);
244		if (ret < 0)
245			goto error;
246		if (ret == 1)
247			break;
248
249		/* next entry */
250		offset = be32_to_cpu(ri.next) & ROMFH_MASK;
251	}
252
253	/* Hard link handling */
254	if ((be32_to_cpu(ri.next) & ROMFH_TYPE) == ROMFH_HRD)
255		offset = be32_to_cpu(ri.spec) & ROMFH_MASK;
256
257	inode = romfs_iget(dir->i_sb, offset);
258	if (IS_ERR(inode)) {
259		ret = PTR_ERR(inode);
260		goto error;
261	}
262	goto outi;
263
264	/*
265	 * it's a bit funky, _lookup needs to return an error code
266	 * (negative) or a NULL, both as a dentry.  ENOENT should not
267	 * be returned, instead we need to create a negative dentry by
268	 * d_add(dentry, NULL); and return 0 as no error.
269	 * (Although as I see, it only matters on writable file
270	 * systems).
271	 */
272out0:
273	inode = NULL;
274outi:
275	d_add(dentry, inode);
276	ret = 0;
277error:
278	return ERR_PTR(ret);
279}
280
281static const struct file_operations romfs_dir_operations = {
282	.read		= generic_read_dir,
283	.iterate	= romfs_readdir,
284	.llseek		= default_llseek,
285};
286
287static const struct inode_operations romfs_dir_inode_operations = {
288	.lookup		= romfs_lookup,
289};
290
291/*
292 * get a romfs inode based on its position in the image (which doubles as the
293 * inode number)
294 */
295static struct inode *romfs_iget(struct super_block *sb, unsigned long pos)
296{
297	struct romfs_inode_info *inode;
298	struct romfs_inode ri;
299	struct inode *i;
300	unsigned long nlen;
301	unsigned nextfh;
302	int ret;
303	umode_t mode;
304
305	/* we might have to traverse a chain of "hard link" file entries to get
306	 * to the actual file */
307	for (;;) {
308		ret = romfs_dev_read(sb, pos, &ri, sizeof(ri));
309		if (ret < 0)
310			goto error;
311
312		/* XXX: do romfs_checksum here too (with name) */
313
314		nextfh = be32_to_cpu(ri.next);
315		if ((nextfh & ROMFH_TYPE) != ROMFH_HRD)
316			break;
317
318		pos = be32_to_cpu(ri.spec) & ROMFH_MASK;
319	}
320
321	/* determine the length of the filename */
322	nlen = romfs_dev_strnlen(sb, pos + ROMFH_SIZE, ROMFS_MAXFN);
323	if (IS_ERR_VALUE(nlen))
324		goto eio;
325
326	/* get an inode for this image position */
327	i = iget_locked(sb, pos);
328	if (!i)
329		return ERR_PTR(-ENOMEM);
330
331	if (!(i->i_state & I_NEW))
332		return i;
333
334	/* precalculate the data offset */
335	inode = ROMFS_I(i);
336	inode->i_metasize = (ROMFH_SIZE + nlen + 1 + ROMFH_PAD) & ROMFH_MASK;
337	inode->i_dataoffset = pos + inode->i_metasize;
338
339	set_nlink(i, 1);		/* Hard to decide.. */
340	i->i_size = be32_to_cpu(ri.size);
341	i->i_mtime.tv_sec = i->i_atime.tv_sec = i->i_ctime.tv_sec = 0;
342	i->i_mtime.tv_nsec = i->i_atime.tv_nsec = i->i_ctime.tv_nsec = 0;
343
344	/* set up mode and ops */
345	mode = romfs_modemap[nextfh & ROMFH_TYPE];
346
347	switch (nextfh & ROMFH_TYPE) {
348	case ROMFH_DIR:
349		i->i_size = ROMFS_I(i)->i_metasize;
350		i->i_op = &romfs_dir_inode_operations;
351		i->i_fop = &romfs_dir_operations;
352		if (nextfh & ROMFH_EXEC)
353			mode |= S_IXUGO;
354		break;
355	case ROMFH_REG:
356		i->i_fop = &romfs_ro_fops;
357		i->i_data.a_ops = &romfs_aops;
358		if (nextfh & ROMFH_EXEC)
359			mode |= S_IXUGO;
360		break;
361	case ROMFH_SYM:
362		i->i_op = &page_symlink_inode_operations;
363		i->i_data.a_ops = &romfs_aops;
364		mode |= S_IRWXUGO;
365		break;
366	default:
367		/* depending on MBZ for sock/fifos */
368		nextfh = be32_to_cpu(ri.spec);
369		init_special_inode(i, mode, MKDEV(nextfh >> 16,
370						  nextfh & 0xffff));
371		break;
372	}
373
374	i->i_mode = mode;
375
376	unlock_new_inode(i);
377	return i;
378
379eio:
380	ret = -EIO;
381error:
382	pr_err("read error for inode 0x%lx\n", pos);
383	return ERR_PTR(ret);
384}
385
386/*
387 * allocate a new inode
388 */
389static struct inode *romfs_alloc_inode(struct super_block *sb)
390{
391	struct romfs_inode_info *inode;
392
393	inode = kmem_cache_alloc(romfs_inode_cachep, GFP_KERNEL);
394	return inode ? &inode->vfs_inode : NULL;
395}
396
397/*
398 * return a spent inode to the slab cache
399 */
400static void romfs_i_callback(struct rcu_head *head)
401{
402	struct inode *inode = container_of(head, struct inode, i_rcu);
403
404	kmem_cache_free(romfs_inode_cachep, ROMFS_I(inode));
405}
406
407static void romfs_destroy_inode(struct inode *inode)
408{
409	call_rcu(&inode->i_rcu, romfs_i_callback);
410}
411
412/*
413 * get filesystem statistics
414 */
415static int romfs_statfs(struct dentry *dentry, struct kstatfs *buf)
416{
417	struct super_block *sb = dentry->d_sb;
418	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
419
420	buf->f_type = ROMFS_MAGIC;
421	buf->f_namelen = ROMFS_MAXFN;
422	buf->f_bsize = ROMBSIZE;
423	buf->f_bfree = buf->f_bavail = buf->f_ffree;
424	buf->f_blocks =
425		(romfs_maxsize(dentry->d_sb) + ROMBSIZE - 1) >> ROMBSBITS;
426	buf->f_fsid.val[0] = (u32)id;
427	buf->f_fsid.val[1] = (u32)(id >> 32);
428	return 0;
429}
430
431/*
432 * remounting must involve read-only
433 */
434static int romfs_remount(struct super_block *sb, int *flags, char *data)
435{
436	sync_filesystem(sb);
437	*flags |= MS_RDONLY;
438	return 0;
439}
440
441static const struct super_operations romfs_super_ops = {
442	.alloc_inode	= romfs_alloc_inode,
443	.destroy_inode	= romfs_destroy_inode,
444	.statfs		= romfs_statfs,
445	.remount_fs	= romfs_remount,
446};
447
448/*
449 * checksum check on part of a romfs filesystem
450 */
451static __u32 romfs_checksum(const void *data, int size)
452{
453	const __be32 *ptr = data;
454	__u32 sum;
455
456	sum = 0;
457	size >>= 2;
458	while (size > 0) {
459		sum += be32_to_cpu(*ptr++);
460		size--;
461	}
462	return sum;
463}
464
465/*
466 * fill in the superblock
467 */
468static int romfs_fill_super(struct super_block *sb, void *data, int silent)
469{
470	struct romfs_super_block *rsb;
471	struct inode *root;
472	unsigned long pos, img_size;
473	const char *storage;
474	size_t len;
475	int ret;
476
477#ifdef CONFIG_BLOCK
478	if (!sb->s_mtd) {
479		sb_set_blocksize(sb, ROMBSIZE);
480	} else {
481		sb->s_blocksize = ROMBSIZE;
482		sb->s_blocksize_bits = blksize_bits(ROMBSIZE);
483	}
484#endif
485
486	sb->s_maxbytes = 0xFFFFFFFF;
487	sb->s_magic = ROMFS_MAGIC;
488	sb->s_flags |= MS_RDONLY | MS_NOATIME;
489	sb->s_op = &romfs_super_ops;
490
491	/* read the image superblock and check it */
492	rsb = kmalloc(512, GFP_KERNEL);
493	if (!rsb)
494		return -ENOMEM;
495
496	sb->s_fs_info = (void *) 512;
497	ret = romfs_dev_read(sb, 0, rsb, 512);
498	if (ret < 0)
499		goto error_rsb;
500
501	img_size = be32_to_cpu(rsb->size);
502
503	if (sb->s_mtd && img_size > sb->s_mtd->size)
504		goto error_rsb_inval;
505
506	sb->s_fs_info = (void *) img_size;
507
508	if (rsb->word0 != ROMSB_WORD0 || rsb->word1 != ROMSB_WORD1 ||
509	    img_size < ROMFH_SIZE) {
510		if (!silent)
511			pr_warn("VFS: Can't find a romfs filesystem on dev %s.\n",
512			       sb->s_id);
513		goto error_rsb_inval;
514	}
515
516	if (romfs_checksum(rsb, min_t(size_t, img_size, 512))) {
517		pr_err("bad initial checksum on dev %s.\n", sb->s_id);
518		goto error_rsb_inval;
519	}
520
521	storage = sb->s_mtd ? "MTD" : "the block layer";
522
523	len = strnlen(rsb->name, ROMFS_MAXFN);
524	if (!silent)
525		pr_notice("Mounting image '%*.*s' through %s\n",
526			  (unsigned) len, (unsigned) len, rsb->name, storage);
527
528	kfree(rsb);
529	rsb = NULL;
530
531	/* find the root directory */
532	pos = (ROMFH_SIZE + len + 1 + ROMFH_PAD) & ROMFH_MASK;
533
534	root = romfs_iget(sb, pos);
535	if (IS_ERR(root))
536		return PTR_ERR(root);
537
538	sb->s_root = d_make_root(root);
539	if (!sb->s_root)
540		return -ENOMEM;
541
542	return 0;
543
544error_rsb_inval:
545	ret = -EINVAL;
546error_rsb:
547	kfree(rsb);
548	return ret;
549}
550
551/*
552 * get a superblock for mounting
553 */
554static struct dentry *romfs_mount(struct file_system_type *fs_type,
555			int flags, const char *dev_name,
556			void *data)
557{
558	struct dentry *ret = ERR_PTR(-EINVAL);
559
560#ifdef CONFIG_ROMFS_ON_MTD
561	ret = mount_mtd(fs_type, flags, dev_name, data, romfs_fill_super);
562#endif
563#ifdef CONFIG_ROMFS_ON_BLOCK
564	if (ret == ERR_PTR(-EINVAL))
565		ret = mount_bdev(fs_type, flags, dev_name, data,
566				  romfs_fill_super);
567#endif
568	return ret;
569}
570
571/*
572 * destroy a romfs superblock in the appropriate manner
573 */
574static void romfs_kill_sb(struct super_block *sb)
575{
576#ifdef CONFIG_ROMFS_ON_MTD
577	if (sb->s_mtd) {
578		kill_mtd_super(sb);
579		return;
580	}
581#endif
582#ifdef CONFIG_ROMFS_ON_BLOCK
583	if (sb->s_bdev) {
584		kill_block_super(sb);
585		return;
586	}
587#endif
588}
589
590static struct file_system_type romfs_fs_type = {
591	.owner		= THIS_MODULE,
592	.name		= "romfs",
593	.mount		= romfs_mount,
594	.kill_sb	= romfs_kill_sb,
595	.fs_flags	= FS_REQUIRES_DEV,
596};
597MODULE_ALIAS_FS("romfs");
598
599/*
600 * inode storage initialiser
601 */
602static void romfs_i_init_once(void *_inode)
603{
604	struct romfs_inode_info *inode = _inode;
605
606	inode_init_once(&inode->vfs_inode);
607}
608
609/*
610 * romfs module initialisation
611 */
612static int __init init_romfs_fs(void)
613{
614	int ret;
615
616	pr_info("ROMFS MTD (C) 2007 Red Hat, Inc.\n");
617
618	romfs_inode_cachep =
619		kmem_cache_create("romfs_i",
620				  sizeof(struct romfs_inode_info), 0,
621				  SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
622				  romfs_i_init_once);
623
624	if (!romfs_inode_cachep) {
625		pr_err("Failed to initialise inode cache\n");
626		return -ENOMEM;
627	}
628	ret = register_filesystem(&romfs_fs_type);
629	if (ret) {
630		pr_err("Failed to register filesystem\n");
631		goto error_register;
632	}
633	return 0;
634
635error_register:
636	kmem_cache_destroy(romfs_inode_cachep);
637	return ret;
638}
639
640/*
641 * romfs module removal
642 */
643static void __exit exit_romfs_fs(void)
644{
645	unregister_filesystem(&romfs_fs_type);
646	/*
647	 * Make sure all delayed rcu free inodes are flushed before we
648	 * destroy cache.
649	 */
650	rcu_barrier();
651	kmem_cache_destroy(romfs_inode_cachep);
652}
653
654module_init(init_romfs_fs);
655module_exit(exit_romfs_fs);
656
657MODULE_DESCRIPTION("Direct-MTD Capable RomFS");
658MODULE_AUTHOR("Red Hat, Inc.");
659MODULE_LICENSE("GPL"); /* Actually dual-licensed, but it doesn't matter for */
660