Import 2.4.0-test6pre6
[davej-history.git] / fs / romfs / inode.c
blob8d01f17baeb1c403da200e042974c2dbc658ae49
1 /*
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.
18 * Changes
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
25 * 2.1.47
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
42 * dentries in lookup
43 * clean up page flags setting
44 * (error, uptodate, locking) in
45 * in readpage
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
54 /* todo:
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>
72 #include <linux/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)
81 return a<b ? a : b;
84 static __s32
85 romfs_checksum(void *data, int size)
87 __s32 sum, *ptr;
89 sum = 0; ptr = data;
90 size>>=2;
91 while (size>0) {
92 sum += ntohl(*ptr++);
93 size--;
95 return sum;
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;
106 int sz;
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);
114 if (!bh) {
115 /* XXX merge with other printk? */
116 printk ("romfs: unable to read superblock\n");
117 goto outnobh;
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) {
124 if (!silent)
125 printk ("VFS: Can't find a romfs filesystem on dev "
126 "%s.\n", kdevname(dev));
127 goto out;
129 if (romfs_checksum(rsb, min(sz,512))) {
130 printk ("romfs: bad initial checksum on dev "
131 "%s.\n", kdevname(dev));
132 goto out;
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 */
141 sz = (ROMFH_SIZE +
142 strnlen(rsb->name, ROMFS_MAXFN) + 1 + ROMFH_PAD)
143 & ROMFH_MASK;
145 brelse(bh);
147 s->s_op = &romfs_ops;
148 s->s_root = d_alloc_root(iget(s, sz));
150 if (!s->s_root)
151 goto outnobh;
153 /* Ehrhm; sorry.. :) And thanks to Hans-Joachim Widmaier :) */
154 if (0) {
155 out:
156 brelse(bh);
157 outnobh:
158 s = NULL;
161 return s;
164 /* That's simple too. */
166 static int
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;
174 return 0;
177 /* some helper routines */
179 static int
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)
187 return -1;
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);
194 if (!bh)
195 return -1; /* error */
197 avail = ROMBSIZE - (offset & ROMBMASK);
198 maxsize = min(count, avail);
199 res = strnlen(((char *)bh->b_data)+(offset&ROMBMASK), maxsize);
200 brelse(bh);
202 if (res < maxsize)
203 return res; /* found all of it */
205 while (res < count) {
206 offset += maxsize;
208 bh = bread(i->i_dev, offset>>ROMBSBITS, ROMBSIZE);
209 if (!bh)
210 return -1;
211 maxsize = min(count-res, ROMBSIZE);
212 avail = strnlen(bh->b_data, maxsize);
213 res += avail;
214 brelse(bh);
215 if (avail < maxsize)
216 return res;
218 return res;
221 static int
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)
229 return -1;
231 bh = bread(i->i_dev, offset>>ROMBSBITS, ROMBSIZE);
232 if (!bh)
233 return -1; /* error */
235 avail = ROMBSIZE - (offset & ROMBMASK);
236 maxsize = min(count, avail);
237 memcpy(dest, ((char *)bh->b_data) + (offset & ROMBMASK), maxsize);
238 brelse(bh);
240 res = maxsize; /* all of it */
242 while (res < count) {
243 offset += maxsize;
244 dest += maxsize;
246 bh = bread(i->i_dev, offset>>ROMBSBITS, ROMBSIZE);
247 if (!bh)
248 return -1;
249 maxsize = min(count-res, ROMBSIZE);
250 memcpy(dest, bh->b_data, maxsize);
251 brelse(bh);
252 res += maxsize;
254 return res;
257 static int
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;
263 int j, ino, nextfh;
264 int stored = 0;
265 char fsname[ROMFS_MAXFN]; /* XXX dynamic? */
267 maxoff = i->i_sb->u.romfs_sb.s_maxsize;
269 offset = filp->f_pos;
270 if (!offset) {
271 offset = i->i_ino & ROMFH_MASK;
272 if (romfs_copyfrom(i, &ri, offset, ROMFH_SIZE) <= 0)
273 return stored;
274 offset = ntohl(ri.spec) & ROMFH_MASK;
277 /* Not really failsafe, but we are read-only... */
278 for(;;) {
279 if (!offset || offset >= maxoff) {
280 offset = maxoff;
281 filp->f_pos = offset;
282 return stored;
284 filp->f_pos = offset;
286 /* Fetch inode info */
287 if (romfs_copyfrom(i, &ri, offset, ROMFH_SIZE) <= 0)
288 return stored;
290 j = romfs_strnlen(i, offset+ROMFH_SIZE, sizeof(fsname)-1);
291 if (j < 0)
292 return stored;
294 fsname[j]=0;
295 romfs_copyfrom(i, fsname, offset+ROMFH_SIZE, j);
297 ino = offset;
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) {
302 return stored;
304 stored++;
305 offset = nextfh & ROMFH_MASK;
309 static struct dentry *
310 romfs_lookup(struct inode *dir, struct dentry *dentry)
312 unsigned long offset, maxoff;
313 int fslen, res;
314 struct inode *inode;
315 char fsname[ROMFS_MAXFN]; /* XXX dynamic? */
316 struct romfs_inode ri;
317 const char *name; /* got from dentry */
318 int len;
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)
323 goto out;
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;
334 for(;;) {
335 if (!offset || offset >= maxoff)
336 goto out0;
337 if (romfs_copyfrom(dir, &ri, offset, ROMFH_SIZE) <= 0)
338 goto out;
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) {
343 if (len == fslen) {
344 /* both are shorter, and same size */
345 romfs_copyfrom(dir, fsname, offset+ROMFH_SIZE, len+1);
346 if (strncmp (name, fsname, len) == 0)
347 break;
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);
352 if (len == fslen) {
353 romfs_copyfrom(dir, fsname, offset+ROMFH_SIZE, len+1);
354 if (strncmp(name, fsname, len) == 0)
355 break;
358 /* next entry */
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)))
367 goto outi;
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
375 * systems).
378 out0: inode = NULL;
379 outi: res = 0;
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.
390 static int
391 romfs_readpage(struct file *file, struct page * page)
393 struct inode *inode = (struct inode*)page->mapping->host;
394 unsigned long offset, avail, readlen;
395 void *buf;
396 int result = -EIO;
398 lock_kernel();
399 get_page(page);
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, buf, inode->u.romfs_i.i_dataoffset+offset, readlen) == readlen) {
408 if (readlen < PAGE_SIZE) {
409 memset(buf + readlen,0,PAGE_SIZE-readlen);
411 SetPageUptodate(page);
412 result = 0;
415 if (result) {
416 memset(buf, 0, PAGE_SIZE);
417 SetPageError(page);
419 flush_dcache_page(page);
421 UnlockPage(page);
423 __free_page(page);
424 unlock_kernel();
426 return result;
429 /* Mapping from our types to the kernel */
431 static struct address_space_operations romfs_aops = {
432 readpage: romfs_readpage
435 static struct file_operations romfs_dir_operations = {
436 read: generic_read_dir,
437 readdir: romfs_readdir,
440 static struct inode_operations romfs_dir_inode_operations = {
441 lookup: romfs_lookup,
444 static mode_t romfs_modemap[] =
446 0, S_IFDIR+0644, S_IFREG+0644, S_IFLNK+0777,
447 S_IFBLK+0600, S_IFCHR+0600, S_IFSOCK+0644, S_IFIFO+0644
450 static void
451 romfs_read_inode(struct inode *i)
453 int nextfh, ino;
454 struct romfs_inode ri;
456 ino = i->i_ino & ROMFH_MASK;
457 i->i_mode = 0;
459 /* Loop for finding the real hard link */
460 for(;;) {
461 if (romfs_copyfrom(i, &ri, ino, ROMFH_SIZE) <= 0) {
462 printk("romfs: read error for inode 0x%x\n", ino);
463 return;
465 /* XXX: do romfs_checksum here too (with name) */
467 nextfh = ntohl(ri.next);
468 if ((nextfh & ROMFH_TYPE) != ROMFH_HRD)
469 break;
471 ino = ntohl(ri.spec) & ROMFH_MASK;
474 i->i_nlink = 1; /* Hard to decide.. */
475 i->i_size = ntohl(ri.size);
476 i->i_mtime = i->i_atime = i->i_ctime = 0;
477 i->i_uid = i->i_gid = 0;
479 /* Precalculate the data offset */
480 ino = romfs_strnlen(i, ino+ROMFH_SIZE, ROMFS_MAXFN);
481 if (ino >= 0)
482 ino = ((ROMFH_SIZE+ino+1+ROMFH_PAD)&ROMFH_MASK);
483 else
484 ino = 0;
486 i->u.romfs_i.i_metasize = ino;
487 i->u.romfs_i.i_dataoffset = ino+(i->i_ino&ROMFH_MASK);
489 /* Compute permissions */
490 ino = romfs_modemap[nextfh & ROMFH_TYPE];
491 /* only "normal" files have ops */
492 switch (nextfh & ROMFH_TYPE) {
493 case 1:
494 i->i_size = i->u.romfs_i.i_metasize;
495 i->i_op = &romfs_dir_inode_operations;
496 i->i_fop = &romfs_dir_operations;
497 if (nextfh & ROMFH_EXEC)
498 ino |= S_IXUGO;
499 i->i_mode = ino;
500 break;
501 case 2:
502 i->i_fop = &generic_ro_fops;
503 i->i_data.a_ops = &romfs_aops;
504 if (nextfh & ROMFH_EXEC)
505 ino |= S_IXUGO;
506 i->i_mode = ino;
507 break;
508 case 3:
509 i->i_op = &page_symlink_inode_operations;
510 i->i_data.a_ops = &romfs_aops;
511 i->i_mode = ino | S_IRWXUGO;
512 break;
513 default:
514 /* depending on MBZ for sock/fifos */
515 nextfh = ntohl(ri.spec);
516 nextfh = kdev_t_to_nr(MKDEV(nextfh>>16,nextfh&0xffff));
517 init_special_inode(i, ino, nextfh);
521 static struct super_operations romfs_ops = {
522 read_inode: romfs_read_inode,
523 statfs: romfs_statfs,
526 static DECLARE_FSTYPE_DEV(romfs_fs_type, "romfs", romfs_read_super);
528 static int __init init_romfs_fs(void)
530 return register_filesystem(&romfs_fs_type);
533 static void __exit exit_romfs_fs(void)
535 unregister_filesystem(&romfs_fs_type);
538 /* Yes, works even as a module... :) */
540 EXPORT_NO_SYMBOLS;
542 module_init(init_romfs_fs)
543 module_exit(exit_romfs_fs)