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