2 * ROMFS file system, Linux implementation
4 * Copyright (C) 1997-1999 Janos Farkas <chexum@shadow.banki.hu>
6 * Using parts of the minix filesystem
7 * Copyright (C) 1991, 1992 Linus Torvalds
9 * and parts of the affs filesystem additionally
10 * Copyright (C) 1993 Ray Burr
11 * Copyright (C) 1996 Hans-Joachim Widmaier
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
19 * Changed for 2.1.19 modules
20 * Jan 1997 Initial release
21 * Jun 1997 2.1.43+ changes
22 * Proper page locking in readpage
23 * Changed to work with 2.1.45+ fs
24 * Jul 1997 Fixed follow_link
26 * lookup shouldn't return -ENOENT
27 * from Horst von Brand:
28 * fail on wrong checksum
29 * double unlock_super was possible
30 * correct namelen for statfs
31 * spotted by Bill Hawes:
32 * readlink shouldn't iput()
33 * Jun 1998 2.1.106 from Avery Pennarun: glibc scandir()
34 * exposed a problem in readdir
35 * 2.1.107 code-freeze spellchecker run
36 * Aug 1998 2.1.118+ VFS changes
37 * Sep 1998 2.1.122 another VFS change (follow_link)
38 * Apr 1999 2.2.7 no more EBADF checking in
39 * lookup/readdir, use ERR_PTR
40 * Jun 1999 2.3.6 d_alloc_root use changed
41 * 2.3.9 clean up usage of ENOENT/negative
43 * clean up page flags setting
44 * (error, uptodate, locking) in
46 * use init_special_inode for
47 * fifos/sockets (and streamline) in
48 * read_inode, fix _ops table order
49 * Aug 1999 2.3.16 __initfunc() => __init change
50 * Oct 1999 2.3.24 page->owner hack obsoleted
51 * Nov 1999 2.3.27 2.3.25+ page->offset => index change
55 * - see Documentation/filesystems/romfs.txt
56 * - use allocated, not stack memory for file names?
57 * - considering write access...
58 * - network (tftp) files?
59 * - merge back some _op tables
63 * Sorry about some optimizations and for some goto's. I just wanted
64 * to squeeze some more bytes out of this code.. :)
67 #include <linux/module.h>
68 #include <linux/types.h>
69 #include <linux/errno.h>
70 #include <linux/malloc.h>
71 #include <linux/romfs_fs.h>
73 #include <linux/locks.h>
74 #include <linux/init.h>
75 #include <linux/smp_lock.h>
77 #include <asm/uaccess.h>
79 static int inline min(int a
, int b
)
85 romfs_checksum(void *data
, int size
)
98 static struct super_operations romfs_ops
;
100 static struct super_block
*
101 romfs_read_super(struct super_block
*s
, void *data
, int silent
)
103 struct buffer_head
*bh
;
104 kdev_t dev
= s
->s_dev
;
105 struct romfs_super_block
*rsb
;
108 /* I would parse the options here, but there are none.. :) */
110 set_blocksize(dev
, ROMBSIZE
);
111 s
->s_blocksize
= ROMBSIZE
;
112 s
->s_blocksize_bits
= ROMBSBITS
;
113 bh
= bread(dev
, 0, ROMBSIZE
);
115 /* XXX merge with other printk? */
116 printk ("romfs: unable to read superblock\n");
120 rsb
= (struct romfs_super_block
*)bh
->b_data
;
121 sz
= ntohl(rsb
->size
);
122 if (rsb
->word0
!= ROMSB_WORD0
|| rsb
->word1
!= ROMSB_WORD1
123 || sz
< ROMFH_SIZE
) {
125 printk ("VFS: Can't find a romfs filesystem on dev "
126 "%s.\n", kdevname(dev
));
129 if (romfs_checksum(rsb
, min(sz
,512))) {
130 printk ("romfs: bad initial checksum on dev "
131 "%s.\n", kdevname(dev
));
135 s
->s_magic
= ROMFS_MAGIC
;
136 s
->u
.romfs_sb
.s_maxsize
= sz
;
138 s
->s_flags
|= MS_RDONLY
;
140 /* Find the start of the fs */
142 strnlen(rsb
->name
, ROMFS_MAXFN
) + 1 + ROMFH_PAD
)
147 s
->s_op
= &romfs_ops
;
148 s
->s_root
= d_alloc_root(iget(s
, sz
));
153 /* Ehrhm; sorry.. :) And thanks to Hans-Joachim Widmaier :) */
164 /* That's simple too. */
167 romfs_statfs(struct super_block
*sb
, struct statfs
*buf
)
169 buf
->f_type
= ROMFS_MAGIC
;
170 buf
->f_bsize
= ROMBSIZE
;
171 buf
->f_bfree
= buf
->f_bavail
= buf
->f_ffree
;
172 buf
->f_blocks
= (sb
->u
.romfs_sb
.s_maxsize
+ROMBSIZE
-1)>>ROMBSBITS
;
173 buf
->f_namelen
= ROMFS_MAXFN
;
177 /* some helper routines */
180 romfs_strnlen(struct inode
*i
, unsigned long offset
, unsigned long count
)
182 struct buffer_head
*bh
;
183 unsigned long avail
, maxsize
, res
;
185 maxsize
= i
->i_sb
->u
.romfs_sb
.s_maxsize
;
186 if (offset
>= maxsize
)
189 /* strnlen is almost always valid */
190 if (count
> maxsize
|| offset
+count
> maxsize
)
191 count
= maxsize
-offset
;
193 bh
= bread(i
->i_dev
, offset
>>ROMBSBITS
, ROMBSIZE
);
195 return -1; /* error */
197 avail
= ROMBSIZE
- (offset
& ROMBMASK
);
198 maxsize
= min(count
, avail
);
199 res
= strnlen(((char *)bh
->b_data
)+(offset
&ROMBMASK
), maxsize
);
203 return res
; /* found all of it */
205 while (res
< count
) {
208 bh
= bread(i
->i_dev
, offset
>>ROMBSBITS
, ROMBSIZE
);
211 maxsize
= min(count
-res
, ROMBSIZE
);
212 avail
= strnlen(bh
->b_data
, maxsize
);
222 romfs_copyfrom(struct inode
*i
, void *dest
, unsigned long offset
, unsigned long count
)
224 struct buffer_head
*bh
;
225 unsigned long avail
, maxsize
, res
;
227 maxsize
= i
->i_sb
->u
.romfs_sb
.s_maxsize
;
228 if (offset
>= maxsize
|| count
> maxsize
|| offset
+count
>maxsize
)
231 bh
= bread(i
->i_dev
, offset
>>ROMBSBITS
, ROMBSIZE
);
233 return -1; /* error */
235 avail
= ROMBSIZE
- (offset
& ROMBMASK
);
236 maxsize
= min(count
, avail
);
237 memcpy(dest
, ((char *)bh
->b_data
) + (offset
& ROMBMASK
), maxsize
);
240 res
= maxsize
; /* all of it */
242 while (res
< count
) {
246 bh
= bread(i
->i_dev
, offset
>>ROMBSBITS
, ROMBSIZE
);
249 maxsize
= min(count
-res
, ROMBSIZE
);
250 memcpy(dest
, bh
->b_data
, maxsize
);
258 romfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
260 struct inode
*i
= filp
->f_dentry
->d_inode
;
261 struct romfs_inode ri
;
262 unsigned long offset
, maxoff
;
265 char fsname
[ROMFS_MAXFN
]; /* XXX dynamic? */
267 maxoff
= i
->i_sb
->u
.romfs_sb
.s_maxsize
;
269 offset
= filp
->f_pos
;
271 offset
= i
->i_ino
& ROMFH_MASK
;
272 if (romfs_copyfrom(i
, &ri
, offset
, ROMFH_SIZE
) <= 0)
274 offset
= ntohl(ri
.spec
) & ROMFH_MASK
;
277 /* Not really failsafe, but we are read-only... */
279 if (!offset
|| offset
>= maxoff
) {
281 filp
->f_pos
= offset
;
284 filp
->f_pos
= offset
;
286 /* Fetch inode info */
287 if (romfs_copyfrom(i
, &ri
, offset
, ROMFH_SIZE
) <= 0)
290 j
= romfs_strnlen(i
, offset
+ROMFH_SIZE
, sizeof(fsname
)-1);
295 romfs_copyfrom(i
, fsname
, offset
+ROMFH_SIZE
, j
);
298 nextfh
= ntohl(ri
.next
);
299 if ((nextfh
& ROMFH_TYPE
) == ROMFH_HRD
)
300 ino
= ntohl(ri
.spec
);
301 if (filldir(dirent
, fsname
, j
, offset
, ino
) < 0) {
305 offset
= nextfh
& ROMFH_MASK
;
309 static struct dentry
*
310 romfs_lookup(struct inode
*dir
, struct dentry
*dentry
)
312 unsigned long offset
, maxoff
;
315 char fsname
[ROMFS_MAXFN
]; /* XXX dynamic? */
316 struct romfs_inode ri
;
317 const char *name
; /* got from dentry */
320 res
= -EACCES
; /* placeholder for "no data here" */
321 offset
= dir
->i_ino
& ROMFH_MASK
;
322 if (romfs_copyfrom(dir
, &ri
, offset
, ROMFH_SIZE
) <= 0)
325 maxoff
= dir
->i_sb
->u
.romfs_sb
.s_maxsize
;
326 offset
= ntohl(ri
.spec
) & ROMFH_MASK
;
328 /* OK, now find the file whose name is in "dentry" in the
329 * directory specified by "dir". */
331 name
= dentry
->d_name
.name
;
332 len
= dentry
->d_name
.len
;
335 if (!offset
|| offset
>= maxoff
)
337 if (romfs_copyfrom(dir
, &ri
, offset
, ROMFH_SIZE
) <= 0)
340 /* try to match the first 16 bytes of name */
341 fslen
= romfs_strnlen(dir
, offset
+ROMFH_SIZE
, ROMFH_SIZE
);
342 if (len
< ROMFH_SIZE
) {
344 /* both are shorter, and same size */
345 romfs_copyfrom(dir
, fsname
, offset
+ROMFH_SIZE
, len
+1);
346 if (strncmp (name
, fsname
, len
) == 0)
349 } else if (fslen
>= ROMFH_SIZE
) {
350 /* both are longer; XXX optimize max size */
351 fslen
= romfs_strnlen(dir
, offset
+ROMFH_SIZE
, sizeof(fsname
)-1);
353 romfs_copyfrom(dir
, fsname
, offset
+ROMFH_SIZE
, len
+1);
354 if (strncmp(name
, fsname
, len
) == 0)
359 offset
= ntohl(ri
.next
) & ROMFH_MASK
;
362 /* Hard link handling */
363 if ((ntohl(ri
.next
) & ROMFH_TYPE
) == ROMFH_HRD
)
364 offset
= ntohl(ri
.spec
) & ROMFH_MASK
;
366 if ((inode
= iget(dir
->i_sb
, offset
)))
370 * it's a bit funky, _lookup needs to return an error code
371 * (negative) or a NULL, both as a dentry. ENOENT should not
372 * be returned, instead we need to create a negative dentry by
373 * d_add(dentry, NULL); and return 0 as no error.
374 * (Although as I see, it only matters on writable file
380 d_add (dentry
, inode
);
382 out
: return ERR_PTR(res
);
386 * Ok, we do readpage, to be able to execute programs. Unfortunately,
387 * we can't use bmap, since we may have looser alignments.
391 romfs_readpage(struct file
*file
, struct page
* page
)
393 struct inode
*inode
= (struct inode
*)page
->mapping
->host
;
395 unsigned long offset
, avail
, readlen
;
400 buf
= page_address(page
);
402 /* 32 bit warning -- but not for us :) */
403 offset
= page
->index
<< PAGE_CACHE_SHIFT
;
404 if (offset
< inode
->i_size
) {
405 avail
= inode
->i_size
-offset
;
406 readlen
= min(avail
, PAGE_SIZE
);
407 if (romfs_copyfrom(inode
, (void *)buf
, inode
->u
.romfs_i
.i_dataoffset
+offset
, readlen
) == readlen
) {
408 if (readlen
< PAGE_SIZE
) {
409 memset((void *)(buf
+readlen
),0,PAGE_SIZE
-readlen
);
411 SetPageUptodate(page
);
416 memset((void *)buf
, 0, PAGE_SIZE
);
428 /* Mapping from our types to the kernel */
430 static struct address_space_operations romfs_aops
= {
431 readpage
: romfs_readpage
434 static struct file_operations romfs_dir_operations
= {
435 read
: generic_read_dir
,
436 readdir
: romfs_readdir
,
439 static struct inode_operations romfs_dir_inode_operations
= {
440 lookup
: romfs_lookup
,
443 static mode_t romfs_modemap
[] =
445 0, S_IFDIR
+0644, S_IFREG
+0644, S_IFLNK
+0777,
446 S_IFBLK
+0600, S_IFCHR
+0600, S_IFSOCK
+0644, S_IFIFO
+0644
450 romfs_read_inode(struct inode
*i
)
453 struct romfs_inode ri
;
455 ino
= i
->i_ino
& ROMFH_MASK
;
458 /* Loop for finding the real hard link */
460 if (romfs_copyfrom(i
, &ri
, ino
, ROMFH_SIZE
) <= 0) {
461 printk("romfs: read error for inode 0x%x\n", ino
);
464 /* XXX: do romfs_checksum here too (with name) */
466 nextfh
= ntohl(ri
.next
);
467 if ((nextfh
& ROMFH_TYPE
) != ROMFH_HRD
)
470 ino
= ntohl(ri
.spec
) & ROMFH_MASK
;
473 i
->i_nlink
= 1; /* Hard to decide.. */
474 i
->i_size
= ntohl(ri
.size
);
475 i
->i_mtime
= i
->i_atime
= i
->i_ctime
= 0;
476 i
->i_uid
= i
->i_gid
= 0;
478 /* Precalculate the data offset */
479 ino
= romfs_strnlen(i
, ino
+ROMFH_SIZE
, ROMFS_MAXFN
);
481 ino
= ((ROMFH_SIZE
+ino
+1+ROMFH_PAD
)&ROMFH_MASK
);
485 i
->u
.romfs_i
.i_metasize
= ino
;
486 i
->u
.romfs_i
.i_dataoffset
= ino
+(i
->i_ino
&ROMFH_MASK
);
488 /* Compute permissions */
489 ino
= romfs_modemap
[nextfh
& ROMFH_TYPE
];
490 /* only "normal" files have ops */
491 switch (nextfh
& ROMFH_TYPE
) {
493 i
->i_size
= i
->u
.romfs_i
.i_metasize
;
494 i
->i_op
= &romfs_dir_inode_operations
;
495 i
->i_fop
= &romfs_dir_operations
;
496 if (nextfh
& ROMFH_EXEC
)
501 i
->i_fop
= &generic_ro_fops
;
502 i
->i_data
.a_ops
= &romfs_aops
;
503 if (nextfh
& ROMFH_EXEC
)
508 i
->i_op
= &page_symlink_inode_operations
;
509 i
->i_data
.a_ops
= &romfs_aops
;
510 i
->i_mode
= ino
| S_IRWXUGO
;
513 /* depending on MBZ for sock/fifos */
514 nextfh
= ntohl(ri
.spec
);
515 nextfh
= kdev_t_to_nr(MKDEV(nextfh
>>16,nextfh
&0xffff));
516 init_special_inode(i
, ino
, nextfh
);
520 static struct super_operations romfs_ops
= {
521 read_inode
: romfs_read_inode
,
522 statfs
: romfs_statfs
,
525 static DECLARE_FSTYPE_DEV(romfs_fs_type
, "romfs", romfs_read_super
);
527 static int __init
init_romfs_fs(void)
529 return register_filesystem(&romfs_fs_type
);
532 static void __exit
exit_romfs_fs(void)
534 unregister_filesystem(&romfs_fs_type
);
537 /* Yes, works even as a module... :) */
541 module_init(init_romfs_fs
)
542 module_exit(exit_romfs_fs
)