~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/romfs/inode.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * ROMFS file system, Linux implementation
  3  *
  4  * Copyright (C) 1997-1999  Janos Farkas <chexum@shadow.banki.hu>
  5  *
  6  * Using parts of the minix filesystem
  7  * Copyright (C) 1991, 1992  Linus Torvalds
  8  *
  9  * and parts of the affs filesystem additionally
 10  * Copyright (C) 1993  Ray Burr
 11  * Copyright (C) 1996  Hans-Joachim Widmaier
 12  *
 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.
 17  *
 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
 52  */
 53 
 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
 60  */
 61 
 62 /*
 63  * Sorry about some optimizations and for some goto's.  I just wanted
 64  * to squeeze some more bytes out of this code.. :)
 65  */
 66 
 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>
 76 
 77 #include <asm/uaccess.h>
 78 
 79 static int inline min(int a, int b)
 80 {
 81         return a<b ? a : b;
 82 }
 83 
 84 static __s32
 85 romfs_checksum(void *data, int size)
 86 {
 87         __s32 sum, *ptr;
 88 
 89         sum = 0; ptr = data;
 90         size>>=2;
 91         while (size>0) {
 92                 sum += ntohl(*ptr++);
 93                 size--;
 94         }
 95         return sum;
 96 }
 97 
 98 static struct super_operations romfs_ops;
 99 
100 static struct super_block *
101 romfs_read_super(struct super_block *s, void *data, int silent)
102 {
103         struct buffer_head *bh;
104         kdev_t dev = s->s_dev;
105         struct romfs_super_block *rsb;
106         int sz;
107 
108         /* I would parse the options here, but there are none.. :) */
109 
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;
118         }
119 
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;
128         }
129         if (romfs_checksum(rsb, min(sz,512))) {
130                 printk ("romfs: bad initial checksum on dev "
131                         "%s.\n", kdevname(dev));
132                 goto out;
133         }
134 
135         s->s_magic = ROMFS_MAGIC;
136         s->u.romfs_sb.s_maxsize = sz;
137 
138         s->s_flags |= MS_RDONLY;
139 
140         /* Find the start of the fs */
141         sz = (ROMFH_SIZE +
142               strnlen(rsb->name, ROMFS_MAXFN) + 1 + ROMFH_PAD)
143              & ROMFH_MASK;
144 
145         brelse(bh);
146 
147         s->s_op = &romfs_ops;
148         s->s_root = d_alloc_root(iget(s, sz));
149 
150         if (!s->s_root)
151                 goto outnobh;
152 
153         /* Ehrhm; sorry.. :)  And thanks to Hans-Joachim Widmaier  :) */
154         if (0) {
155 out:
156                 brelse(bh);
157 outnobh:
158                 s = NULL;
159         }
160 
161         return s;
162 }
163 
164 /* That's simple too. */
165 
166 static int
167 romfs_statfs(struct super_block *sb, struct statfs *buf)
168 {
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;
175 }
176 
177 /* some helper routines */
178 
179 static int
180 romfs_strnlen(struct inode *i, unsigned long offset, unsigned long count)
181 {
182         struct buffer_head *bh;
183         unsigned long avail, maxsize, res;
184 
185         maxsize = i->i_sb->u.romfs_sb.s_maxsize;
186         if (offset >= maxsize)
187                 return -1;
188 
189         /* strnlen is almost always valid */
190         if (count > maxsize || offset+count > maxsize)
191                 count = maxsize-offset;
192 
193         bh = bread(i->i_dev, offset>>ROMBSBITS, ROMBSIZE);
194         if (!bh)
195                 return -1;              /* error */
196 
197         avail = ROMBSIZE - (offset & ROMBMASK);
198         maxsize = min(count, avail);
199         res = strnlen(((char *)bh->b_data)+(offset&ROMBMASK), maxsize);
200         brelse(bh);
201 
202         if (res < maxsize)
203                 return res;             /* found all of it */
204 
205         while (res < count) {
206                 offset += maxsize;
207 
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;
217         }
218         return res;
219 }
220 
221 static int
222 romfs_copyfrom(struct inode *i, void *dest, unsigned long offset, unsigned long count)
223 {
224         struct buffer_head *bh;
225         unsigned long avail, maxsize, res;
226 
227         maxsize = i->i_sb->u.romfs_sb.s_maxsize;
228         if (offset >= maxsize || count > maxsize || offset+count>maxsize)
229                 return -1;
230 
231         bh = bread(i->i_dev, offset>>ROMBSBITS, ROMBSIZE);
232         if (!bh)
233                 return -1;              /* error */
234 
235         avail = ROMBSIZE - (offset & ROMBMASK);
236         maxsize = min(count, avail);
237         memcpy(dest, ((char *)bh->b_data) + (offset & ROMBMASK), maxsize);
238         brelse(bh);
239 
240         res = maxsize;                  /* all of it */
241 
242         while (res < count) {
243                 offset += maxsize;
244                 dest += maxsize;
245 
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;
253         }
254         return res;
255 }
256 
257 static unsigned char romfs_dtype_table[] = {
258         DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_SOCK, DT_FIFO
259 };
260 
261 static int
262 romfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
263 {
264         struct inode *i = filp->f_dentry->d_inode;
265         struct romfs_inode ri;
266         unsigned long offset, maxoff;
267         int j, ino, nextfh;
268         int stored = 0;
269         char fsname[ROMFS_MAXFN];       /* XXX dynamic? */
270 
271         maxoff = i->i_sb->u.romfs_sb.s_maxsize;
272 
273         offset = filp->f_pos;
274         if (!offset) {
275                 offset = i->i_ino & ROMFH_MASK;
276                 if (romfs_copyfrom(i, &ri, offset, ROMFH_SIZE) <= 0)
277                         return stored;
278                 offset = ntohl(ri.spec) & ROMFH_MASK;
279         }
280 
281         /* Not really failsafe, but we are read-only... */
282         for(;;) {
283                 if (!offset || offset >= maxoff) {
284                         offset = maxoff;
285                         filp->f_pos = offset;
286                         return stored;
287                 }
288                 filp->f_pos = offset;
289 
290                 /* Fetch inode info */
291                 if (romfs_copyfrom(i, &ri, offset, ROMFH_SIZE) <= 0)
292                         return stored;
293 
294                 j = romfs_strnlen(i, offset+ROMFH_SIZE, sizeof(fsname)-1);
295                 if (j < 0)
296                         return stored;
297 
298                 fsname[j]=0;
299                 romfs_copyfrom(i, fsname, offset+ROMFH_SIZE, j);
300 
301                 ino = offset;
302                 nextfh = ntohl(ri.next);
303                 if ((nextfh & ROMFH_TYPE) == ROMFH_HRD)
304                         ino = ntohl(ri.spec);
305                 if (filldir(dirent, fsname, j, offset, ino,
306                             romfs_dtype_table[nextfh & ROMFH_TYPE]) < 0) {
307                         return stored;
308                 }
309                 stored++;
310                 offset = nextfh & ROMFH_MASK;
311         }
312 }
313 
314 static struct dentry *
315 romfs_lookup(struct inode *dir, struct dentry *dentry)
316 {
317         unsigned long offset, maxoff;
318         int fslen, res;
319         struct inode *inode;
320         char fsname[ROMFS_MAXFN];       /* XXX dynamic? */
321         struct romfs_inode ri;
322         const char *name;               /* got from dentry */
323         int len;
324 
325         res = -EACCES;                  /* placeholder for "no data here" */
326         offset = dir->i_ino & ROMFH_MASK;
327         if (romfs_copyfrom(dir, &ri, offset, ROMFH_SIZE) <= 0)
328                 goto out;
329 
330         maxoff = dir->i_sb->u.romfs_sb.s_maxsize;
331         offset = ntohl(ri.spec) & ROMFH_MASK;
332 
333         /* OK, now find the file whose name is in "dentry" in the
334          * directory specified by "dir".  */
335 
336         name = dentry->d_name.name;
337         len = dentry->d_name.len;
338 
339         for(;;) {
340                 if (!offset || offset >= maxoff)
341                         goto out0;
342                 if (romfs_copyfrom(dir, &ri, offset, ROMFH_SIZE) <= 0)
343                         goto out;
344 
345                 /* try to match the first 16 bytes of name */
346                 fslen = romfs_strnlen(dir, offset+ROMFH_SIZE, ROMFH_SIZE);
347                 if (len < ROMFH_SIZE) {
348                         if (len == fslen) {
349                                 /* both are shorter, and same size */
350                                 romfs_copyfrom(dir, fsname, offset+ROMFH_SIZE, len+1);
351                                 if (strncmp (name, fsname, len) == 0)
352                                         break;
353                         }
354                 } else if (fslen >= ROMFH_SIZE) {
355                         /* both are longer; XXX optimize max size */
356                         fslen = romfs_strnlen(dir, offset+ROMFH_SIZE, sizeof(fsname)-1);
357                         if (len == fslen) {
358                                 romfs_copyfrom(dir, fsname, offset+ROMFH_SIZE, len+1);
359                                 if (strncmp(name, fsname, len) == 0)
360                                         break;
361                         }
362                 }
363                 /* next entry */
364                 offset = ntohl(ri.next) & ROMFH_MASK;
365         }
366 
367         /* Hard link handling */
368         if ((ntohl(ri.next) & ROMFH_TYPE) == ROMFH_HRD)
369                 offset = ntohl(ri.spec) & ROMFH_MASK;
370 
371         if ((inode = iget(dir->i_sb, offset)))
372                 goto outi;
373 
374         /*
375          * it's a bit funky, _lookup needs to return an error code
376          * (negative) or a NULL, both as a dentry.  ENOENT should not
377          * be returned, instead we need to create a negative dentry by
378          * d_add(dentry, NULL); and return 0 as no error.
379          * (Although as I see, it only matters on writable file
380          * systems).
381          */
382 
383 out0:   inode = NULL;
384 outi:   res = 0;
385         d_add (dentry, inode);
386 
387 out:    return ERR_PTR(res);
388 }
389 
390 /*
391  * Ok, we do readpage, to be able to execute programs.  Unfortunately,
392  * we can't use bmap, since we may have looser alignments.
393  */
394 
395 static int
396 romfs_readpage(struct file *file, struct page * page)
397 {
398         struct inode *inode = page->mapping->host;
399         unsigned long offset, avail, readlen;
400         void *buf;
401         int result = -EIO;
402 
403         lock_kernel();
404         get_page(page);
405         buf = page_address(page);
406 
407         /* 32 bit warning -- but not for us :) */
408         offset = page->index << PAGE_CACHE_SHIFT;
409         if (offset < inode->i_size) {
410                 avail = inode->i_size-offset;
411                 readlen = min(avail, PAGE_SIZE);
412                 if (romfs_copyfrom(inode, buf, inode->u.romfs_i.i_dataoffset+offset, readlen) == readlen) {
413                         if (readlen < PAGE_SIZE) {
414                                 memset(buf + readlen,0,PAGE_SIZE-readlen);
415                         }
416                         SetPageUptodate(page);
417                         result = 0;
418                 }
419         }
420         if (result) {
421                 memset(buf, 0, PAGE_SIZE);
422                 SetPageError(page);
423         }
424         flush_dcache_page(page);
425 
426         UnlockPage(page);
427 
428         __free_page(page);
429         unlock_kernel();
430 
431         return result;
432 }
433 
434 /* Mapping from our types to the kernel */
435 
436 static struct address_space_operations romfs_aops = {
437         readpage: romfs_readpage
438 };
439 
440 static struct file_operations romfs_dir_operations = {
441         read:           generic_read_dir,
442         readdir:        romfs_readdir,
443 };
444 
445 static struct inode_operations romfs_dir_inode_operations = {
446         lookup:         romfs_lookup,
447 };
448 
449 static mode_t romfs_modemap[] =
450 {
451         0, S_IFDIR+0644, S_IFREG+0644, S_IFLNK+0777,
452         S_IFBLK+0600, S_IFCHR+0600, S_IFSOCK+0644, S_IFIFO+0644
453 };
454 
455 static void
456 romfs_read_inode(struct inode *i)
457 {
458         int nextfh, ino;
459         struct romfs_inode ri;
460 
461         ino = i->i_ino & ROMFH_MASK;
462         i->i_mode = 0;
463 
464         /* Loop for finding the real hard link */
465         for(;;) {
466                 if (romfs_copyfrom(i, &ri, ino, ROMFH_SIZE) <= 0) {
467                         printk("romfs: read error for inode 0x%x\n", ino);
468                         return;
469                 }
470                 /* XXX: do romfs_checksum here too (with name) */
471 
472                 nextfh = ntohl(ri.next);
473                 if ((nextfh & ROMFH_TYPE) != ROMFH_HRD)
474                         break;
475 
476                 ino = ntohl(ri.spec) & ROMFH_MASK;
477         }
478 
479         i->i_nlink = 1;         /* Hard to decide.. */
480         i->i_size = ntohl(ri.size);
481         i->i_mtime = i->i_atime = i->i_ctime = 0;
482         i->i_uid = i->i_gid = 0;
483 
484         /* Precalculate the data offset */
485         ino = romfs_strnlen(i, ino+ROMFH_SIZE, ROMFS_MAXFN);
486         if (ino >= 0)
487                 ino = ((ROMFH_SIZE+ino+1+ROMFH_PAD)&ROMFH_MASK);
488         else
489                 ino = 0;
490 
491         i->u.romfs_i.i_metasize = ino;
492         i->u.romfs_i.i_dataoffset = ino+(i->i_ino&ROMFH_MASK);
493 
494         /* Compute permissions */
495         ino = romfs_modemap[nextfh & ROMFH_TYPE];
496         /* only "normal" files have ops */
497         switch (nextfh & ROMFH_TYPE) {
498                 case 1:
499                         i->i_size = i->u.romfs_i.i_metasize;
500                         i->i_op = &romfs_dir_inode_operations;
501                         i->i_fop = &romfs_dir_operations;
502                         if (nextfh & ROMFH_EXEC)
503                                 ino |= S_IXUGO;
504                         i->i_mode = ino;
505                         break;
506                 case 2:
507                         i->i_fop = &generic_ro_fops;
508                         i->i_data.a_ops = &romfs_aops;
509                         if (nextfh & ROMFH_EXEC)
510                                 ino |= S_IXUGO;
511                         i->i_mode = ino;
512                         break;
513                 case 3:
514                         i->i_op = &page_symlink_inode_operations;
515                         i->i_data.a_ops = &romfs_aops;
516                         i->i_mode = ino | S_IRWXUGO;
517                         break;
518                 default:
519                         /* depending on MBZ for sock/fifos */
520                         nextfh = ntohl(ri.spec);
521                         nextfh = kdev_t_to_nr(MKDEV(nextfh>>16,nextfh&0xffff));
522                         init_special_inode(i, ino, nextfh);
523         }
524 }
525 
526 static struct super_operations romfs_ops = {
527         read_inode:     romfs_read_inode,
528         statfs:         romfs_statfs,
529 };
530 
531 static DECLARE_FSTYPE_DEV(romfs_fs_type, "romfs", romfs_read_super);
532 
533 static int __init init_romfs_fs(void)
534 {
535         return register_filesystem(&romfs_fs_type);
536 }
537 
538 static void __exit exit_romfs_fs(void)
539 {
540         unregister_filesystem(&romfs_fs_type);
541 }
542 
543 /* Yes, works even as a module... :) */
544 
545 EXPORT_NO_SYMBOLS;
546 
547 module_init(init_romfs_fs)
548 module_exit(exit_romfs_fs)
549 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.