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/slab.h>
71 #include <linux/romfs_fs.h>
73 #include <linux/init.h>
74 #include <linux/pagemap.h>
75 #include <linux/smp_lock.h>
76 #include <linux/buffer_head.h>
77 #include <linux/vfs.h>
79 #include <asm/uaccess.h>
81 struct romfs_inode_info
{
82 unsigned long i_metasize
; /* size of non-data area */
83 unsigned long i_dataoffset
; /* from the start of fs */
84 struct inode vfs_inode
;
87 /* instead of private superblock data */
88 static inline unsigned long romfs_maxsize(struct super_block
*sb
)
90 return (unsigned long)sb
->s_fs_info
;
93 static inline struct romfs_inode_info
*ROMFS_I(struct inode
*inode
)
95 return list_entry(inode
, struct romfs_inode_info
, vfs_inode
);
99 romfs_checksum(void *data
, int size
)
107 sum
+= be32_to_cpu(*ptr
++);
113 static struct super_operations romfs_ops
;
115 static int romfs_fill_super(struct super_block
*s
, void *data
, int silent
)
117 struct buffer_head
*bh
;
118 struct romfs_super_block
*rsb
;
122 /* I would parse the options here, but there are none.. :) */
124 sb_set_blocksize(s
, ROMBSIZE
);
125 s
->s_maxbytes
= 0xFFFFFFFF;
129 /* XXX merge with other printk? */
130 printk ("romfs: unable to read superblock\n");
134 rsb
= (struct romfs_super_block
*)bh
->b_data
;
135 sz
= be32_to_cpu(rsb
->size
);
136 if (rsb
->word0
!= ROMSB_WORD0
|| rsb
->word1
!= ROMSB_WORD1
137 || sz
< ROMFH_SIZE
) {
139 printk ("VFS: Can't find a romfs filesystem on dev "
143 if (romfs_checksum(rsb
, min_t(int, sz
, 512))) {
144 printk ("romfs: bad initial checksum on dev "
149 s
->s_magic
= ROMFS_MAGIC
;
150 s
->s_fs_info
= (void *)(long)sz
;
152 s
->s_flags
|= MS_RDONLY
;
154 /* Find the start of the fs */
156 strnlen(rsb
->name
, ROMFS_MAXFN
) + 1 + ROMFH_PAD
)
159 s
->s_op
= &romfs_ops
;
164 s
->s_root
= d_alloc_root(root
);
179 /* That's simple too. */
182 romfs_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
184 buf
->f_type
= ROMFS_MAGIC
;
185 buf
->f_bsize
= ROMBSIZE
;
186 buf
->f_bfree
= buf
->f_bavail
= buf
->f_ffree
;
187 buf
->f_blocks
= (romfs_maxsize(sb
)+ROMBSIZE
-1)>>ROMBSBITS
;
188 buf
->f_namelen
= ROMFS_MAXFN
;
192 /* some helper routines */
195 romfs_strnlen(struct inode
*i
, unsigned long offset
, unsigned long count
)
197 struct buffer_head
*bh
;
198 unsigned long avail
, maxsize
, res
;
200 maxsize
= romfs_maxsize(i
->i_sb
);
201 if (offset
>= maxsize
)
204 /* strnlen is almost always valid */
205 if (count
> maxsize
|| offset
+count
> maxsize
)
206 count
= maxsize
-offset
;
208 bh
= sb_bread(i
->i_sb
, offset
>>ROMBSBITS
);
210 return -1; /* error */
212 avail
= ROMBSIZE
- (offset
& ROMBMASK
);
213 maxsize
= min_t(unsigned long, count
, avail
);
214 res
= strnlen(((char *)bh
->b_data
)+(offset
&ROMBMASK
), maxsize
);
218 return res
; /* found all of it */
220 while (res
< count
) {
223 bh
= sb_bread(i
->i_sb
, offset
>>ROMBSBITS
);
226 maxsize
= min_t(unsigned long, count
- res
, ROMBSIZE
);
227 avail
= strnlen(bh
->b_data
, maxsize
);
237 romfs_copyfrom(struct inode
*i
, void *dest
, unsigned long offset
, unsigned long count
)
239 struct buffer_head
*bh
;
240 unsigned long avail
, maxsize
, res
;
242 maxsize
= romfs_maxsize(i
->i_sb
);
243 if (offset
>= maxsize
|| count
> maxsize
|| offset
+count
>maxsize
)
246 bh
= sb_bread(i
->i_sb
, offset
>>ROMBSBITS
);
248 return -1; /* error */
250 avail
= ROMBSIZE
- (offset
& ROMBMASK
);
251 maxsize
= min_t(unsigned long, count
, avail
);
252 memcpy(dest
, ((char *)bh
->b_data
) + (offset
& ROMBMASK
), maxsize
);
255 res
= maxsize
; /* all of it */
257 while (res
< count
) {
261 bh
= sb_bread(i
->i_sb
, offset
>>ROMBSBITS
);
264 maxsize
= min_t(unsigned long, count
- res
, ROMBSIZE
);
265 memcpy(dest
, bh
->b_data
, maxsize
);
272 static unsigned char romfs_dtype_table
[] = {
273 DT_UNKNOWN
, DT_DIR
, DT_REG
, DT_LNK
, DT_BLK
, DT_CHR
, DT_SOCK
, DT_FIFO
277 romfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
279 struct inode
*i
= filp
->f_dentry
->d_inode
;
280 struct romfs_inode ri
;
281 unsigned long offset
, maxoff
;
284 char fsname
[ROMFS_MAXFN
]; /* XXX dynamic? */
288 maxoff
= romfs_maxsize(i
->i_sb
);
290 offset
= filp
->f_pos
;
292 offset
= i
->i_ino
& ROMFH_MASK
;
293 if (romfs_copyfrom(i
, &ri
, offset
, ROMFH_SIZE
) <= 0)
295 offset
= be32_to_cpu(ri
.spec
) & ROMFH_MASK
;
298 /* Not really failsafe, but we are read-only... */
300 if (!offset
|| offset
>= maxoff
) {
302 filp
->f_pos
= offset
;
305 filp
->f_pos
= offset
;
307 /* Fetch inode info */
308 if (romfs_copyfrom(i
, &ri
, offset
, ROMFH_SIZE
) <= 0)
311 j
= romfs_strnlen(i
, offset
+ROMFH_SIZE
, sizeof(fsname
)-1);
316 romfs_copyfrom(i
, fsname
, offset
+ROMFH_SIZE
, j
);
319 nextfh
= be32_to_cpu(ri
.next
);
320 if ((nextfh
& ROMFH_TYPE
) == ROMFH_HRD
)
321 ino
= be32_to_cpu(ri
.spec
);
322 if (filldir(dirent
, fsname
, j
, offset
, ino
,
323 romfs_dtype_table
[nextfh
& ROMFH_TYPE
]) < 0) {
327 offset
= nextfh
& ROMFH_MASK
;
334 static struct dentry
*
335 romfs_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
337 unsigned long offset
, maxoff
;
340 char fsname
[ROMFS_MAXFN
]; /* XXX dynamic? */
341 struct romfs_inode ri
;
342 const char *name
; /* got from dentry */
345 res
= -EACCES
; /* placeholder for "no data here" */
346 offset
= dir
->i_ino
& ROMFH_MASK
;
348 if (romfs_copyfrom(dir
, &ri
, offset
, ROMFH_SIZE
) <= 0)
351 maxoff
= romfs_maxsize(dir
->i_sb
);
352 offset
= be32_to_cpu(ri
.spec
) & ROMFH_MASK
;
354 /* OK, now find the file whose name is in "dentry" in the
355 * directory specified by "dir". */
357 name
= dentry
->d_name
.name
;
358 len
= dentry
->d_name
.len
;
361 if (!offset
|| offset
>= maxoff
)
363 if (romfs_copyfrom(dir
, &ri
, offset
, ROMFH_SIZE
) <= 0)
366 /* try to match the first 16 bytes of name */
367 fslen
= romfs_strnlen(dir
, offset
+ROMFH_SIZE
, ROMFH_SIZE
);
368 if (len
< ROMFH_SIZE
) {
370 /* both are shorter, and same size */
371 romfs_copyfrom(dir
, fsname
, offset
+ROMFH_SIZE
, len
+1);
372 if (strncmp (name
, fsname
, len
) == 0)
375 } else if (fslen
>= ROMFH_SIZE
) {
376 /* both are longer; XXX optimize max size */
377 fslen
= romfs_strnlen(dir
, offset
+ROMFH_SIZE
, sizeof(fsname
)-1);
379 romfs_copyfrom(dir
, fsname
, offset
+ROMFH_SIZE
, len
+1);
380 if (strncmp(name
, fsname
, len
) == 0)
385 offset
= be32_to_cpu(ri
.next
) & ROMFH_MASK
;
388 /* Hard link handling */
389 if ((be32_to_cpu(ri
.next
) & ROMFH_TYPE
) == ROMFH_HRD
)
390 offset
= be32_to_cpu(ri
.spec
) & ROMFH_MASK
;
392 if ((inode
= iget(dir
->i_sb
, offset
)))
396 * it's a bit funky, _lookup needs to return an error code
397 * (negative) or a NULL, both as a dentry. ENOENT should not
398 * be returned, instead we need to create a negative dentry by
399 * d_add(dentry, NULL); and return 0 as no error.
400 * (Although as I see, it only matters on writable file
406 d_add (dentry
, inode
);
408 out
: unlock_kernel();
413 * Ok, we do readpage, to be able to execute programs. Unfortunately,
414 * we can't use bmap, since we may have looser alignments.
418 romfs_readpage(struct file
*file
, struct page
* page
)
420 struct inode
*inode
= page
->mapping
->host
;
421 loff_t offset
, avail
, readlen
;
425 page_cache_get(page
);
431 /* 32 bit warning -- but not for us :) */
432 offset
= page_offset(page
);
433 if (offset
< i_size_read(inode
)) {
434 avail
= inode
->i_size
-offset
;
435 readlen
= min_t(unsigned long, avail
, PAGE_SIZE
);
436 if (romfs_copyfrom(inode
, buf
, ROMFS_I(inode
)->i_dataoffset
+offset
, readlen
) == readlen
) {
437 if (readlen
< PAGE_SIZE
) {
438 memset(buf
+ readlen
,0,PAGE_SIZE
-readlen
);
440 SetPageUptodate(page
);
445 memset(buf
, 0, PAGE_SIZE
);
448 flush_dcache_page(page
);
454 page_cache_release(page
);
460 /* Mapping from our types to the kernel */
462 static struct address_space_operations romfs_aops
= {
463 .readpage
= romfs_readpage
466 static const struct file_operations romfs_dir_operations
= {
467 .read
= generic_read_dir
,
468 .readdir
= romfs_readdir
,
471 static struct inode_operations romfs_dir_inode_operations
= {
472 .lookup
= romfs_lookup
,
475 static mode_t romfs_modemap
[] =
477 0, S_IFDIR
+0644, S_IFREG
+0644, S_IFLNK
+0777,
478 S_IFBLK
+0600, S_IFCHR
+0600, S_IFSOCK
+0644, S_IFIFO
+0644
482 romfs_read_inode(struct inode
*i
)
485 struct romfs_inode ri
;
487 ino
= i
->i_ino
& ROMFH_MASK
;
490 /* Loop for finding the real hard link */
492 if (romfs_copyfrom(i
, &ri
, ino
, ROMFH_SIZE
) <= 0) {
493 printk("romfs: read error for inode 0x%x\n", ino
);
496 /* XXX: do romfs_checksum here too (with name) */
498 nextfh
= be32_to_cpu(ri
.next
);
499 if ((nextfh
& ROMFH_TYPE
) != ROMFH_HRD
)
502 ino
= be32_to_cpu(ri
.spec
) & ROMFH_MASK
;
505 i
->i_nlink
= 1; /* Hard to decide.. */
506 i
->i_size
= be32_to_cpu(ri
.size
);
507 i
->i_mtime
.tv_sec
= i
->i_atime
.tv_sec
= i
->i_ctime
.tv_sec
= 0;
508 i
->i_mtime
.tv_nsec
= i
->i_atime
.tv_nsec
= i
->i_ctime
.tv_nsec
= 0;
509 i
->i_uid
= i
->i_gid
= 0;
511 /* Precalculate the data offset */
512 ino
= romfs_strnlen(i
, ino
+ROMFH_SIZE
, ROMFS_MAXFN
);
514 ino
= ((ROMFH_SIZE
+ino
+1+ROMFH_PAD
)&ROMFH_MASK
);
518 ROMFS_I(i
)->i_metasize
= ino
;
519 ROMFS_I(i
)->i_dataoffset
= ino
+(i
->i_ino
&ROMFH_MASK
);
521 /* Compute permissions */
522 ino
= romfs_modemap
[nextfh
& ROMFH_TYPE
];
523 /* only "normal" files have ops */
524 switch (nextfh
& ROMFH_TYPE
) {
526 i
->i_size
= ROMFS_I(i
)->i_metasize
;
527 i
->i_op
= &romfs_dir_inode_operations
;
528 i
->i_fop
= &romfs_dir_operations
;
529 if (nextfh
& ROMFH_EXEC
)
534 i
->i_fop
= &generic_ro_fops
;
535 i
->i_data
.a_ops
= &romfs_aops
;
536 if (nextfh
& ROMFH_EXEC
)
541 i
->i_op
= &page_symlink_inode_operations
;
542 i
->i_data
.a_ops
= &romfs_aops
;
543 i
->i_mode
= ino
| S_IRWXUGO
;
546 /* depending on MBZ for sock/fifos */
547 nextfh
= be32_to_cpu(ri
.spec
);
548 init_special_inode(i
, ino
,
549 MKDEV(nextfh
>>16,nextfh
&0xffff));
553 static kmem_cache_t
* romfs_inode_cachep
;
555 static struct inode
*romfs_alloc_inode(struct super_block
*sb
)
557 struct romfs_inode_info
*ei
;
558 ei
= (struct romfs_inode_info
*)kmem_cache_alloc(romfs_inode_cachep
, SLAB_KERNEL
);
561 return &ei
->vfs_inode
;
564 static void romfs_destroy_inode(struct inode
*inode
)
566 kmem_cache_free(romfs_inode_cachep
, ROMFS_I(inode
));
569 static void init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
571 struct romfs_inode_info
*ei
= (struct romfs_inode_info
*) foo
;
573 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
574 SLAB_CTOR_CONSTRUCTOR
)
575 inode_init_once(&ei
->vfs_inode
);
578 static int init_inodecache(void)
580 romfs_inode_cachep
= kmem_cache_create("romfs_inode_cache",
581 sizeof(struct romfs_inode_info
),
582 0, (SLAB_RECLAIM_ACCOUNT
|
585 if (romfs_inode_cachep
== NULL
)
590 static void destroy_inodecache(void)
592 if (kmem_cache_destroy(romfs_inode_cachep
))
593 printk(KERN_INFO
"romfs_inode_cache: not all structures were freed\n");
596 static int romfs_remount(struct super_block
*sb
, int *flags
, char *data
)
602 static struct super_operations romfs_ops
= {
603 .alloc_inode
= romfs_alloc_inode
,
604 .destroy_inode
= romfs_destroy_inode
,
605 .read_inode
= romfs_read_inode
,
606 .statfs
= romfs_statfs
,
607 .remount_fs
= romfs_remount
,
610 static struct super_block
*romfs_get_sb(struct file_system_type
*fs_type
,
611 int flags
, const char *dev_name
, void *data
)
613 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, romfs_fill_super
);
616 static struct file_system_type romfs_fs_type
= {
617 .owner
= THIS_MODULE
,
619 .get_sb
= romfs_get_sb
,
620 .kill_sb
= kill_block_super
,
621 .fs_flags
= FS_REQUIRES_DEV
,
624 static int __init
init_romfs_fs(void)
626 int err
= init_inodecache();
629 err
= register_filesystem(&romfs_fs_type
);
634 destroy_inodecache();
639 static void __exit
exit_romfs_fs(void)
641 unregister_filesystem(&romfs_fs_type
);
642 destroy_inodecache();
645 /* Yes, works even as a module... :) */
647 module_init(init_romfs_fs
)
648 module_exit(exit_romfs_fs
)
649 MODULE_LICENSE("GPL");