inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. /* todo:
  54. * - see Documentation/filesystems/romfs.txt
  55. * - use allocated, not stack memory for file names?
  56. * - considering write access...
  57. * - network (tftp) files?
  58. * - merge back some _op tables
  59. */
  60. /*
  61. * Sorry about some optimizations and for some goto's. I just wanted
  62. * to squeeze some more bytes out of this code.. :)
  63. */
  64. #include <linux/module.h>
  65. #include <linux/types.h>
  66. #include <linux/errno.h>
  67. #include <linux/slab.h>
  68. #include <linux/romfs_fs.h>
  69. #include <linux/fs.h>
  70. #include <linux/init.h>
  71. #include <linux/pagemap.h>
  72. #include <linux/smp_lock.h>
  73. #include <linux/buffer_head.h>
  74. #include <linux/vfs.h>
  75. #include <asm/uaccess.h>
  76. struct romfs_inode_info {
  77. unsigned long i_metasize; /* size of non-data area */
  78. unsigned long i_dataoffset; /* from the start of fs */
  79. struct inode vfs_inode;
  80. };
  81. /* instead of private superblock data */
  82. static inline unsigned long romfs_maxsize(struct super_block *sb)
  83. {
  84. return (unsigned long)sb->s_fs_info;
  85. }
  86. static inline struct romfs_inode_info *ROMFS_I(struct inode *inode)
  87. {
  88. return list_entry(inode, struct romfs_inode_info, vfs_inode);
  89. }
  90. static __u32
  91. romfs_checksum(void *data, int size)
  92. {
  93. __u32 sum;
  94. __be32 *ptr;
  95. sum = 0; ptr = data;
  96. size>>=2;
  97. while (size>0) {
  98. sum += be32_to_cpu(*ptr++);
  99. size--;
  100. }
  101. return sum;
  102. }
  103. static const struct super_operations romfs_ops;
  104. static int romfs_fill_super(struct super_block *s, void *data, int silent)
  105. {
  106. struct buffer_head *bh;
  107. struct romfs_super_block *rsb;
  108. struct inode *root;
  109. int sz;
  110. /* I would parse the options here, but there are none.. :) */
  111. sb_set_blocksize(s, ROMBSIZE);
  112. s->s_maxbytes = 0xFFFFFFFF;
  113. bh = sb_bread(s, 0);
  114. if (!bh) {
  115. /* XXX merge with other printk? */
  116. printk ("romfs: unable to read superblock\n");
  117. goto outnobh;
  118. }
  119. rsb = (struct romfs_super_block *)bh->b_data;
  120. sz = be32_to_cpu(rsb->size);
  121. if (rsb->word0 != ROMSB_WORD0 || rsb->word1 != ROMSB_WORD1
  122. || sz < ROMFH_SIZE) {
  123. if (!silent)
  124. printk ("VFS: Can't find a romfs filesystem on dev "
  125. "%s.\n", s->s_id);
  126. goto out;
  127. }
  128. if (romfs_checksum(rsb, min_t(int, sz, 512))) {
  129. printk ("romfs: bad initial checksum on dev "
  130. "%s.\n", s->s_id);
  131. goto out;
  132. }
  133. s->s_magic = ROMFS_MAGIC;
  134. s->s_fs_info = (void *)(long)sz;
  135. s->s_flags |= MS_RDONLY;
  136. /* Find the start of the fs */
  137. sz = (ROMFH_SIZE +
  138. strnlen(rsb->name, ROMFS_MAXFN) + 1 + ROMFH_PAD)
  139. & ROMFH_MASK;
  140. s->s_op = &romfs_ops;
  141. root = iget(s, sz);
  142. if (!root)
  143. goto out;
  144. s->s_root = d_alloc_root(root);
  145. if (!s->s_root)
  146. goto outiput;
  147. brelse(bh);
  148. return 0;
  149. outiput:
  150. iput(root);
  151. out:
  152. brelse(bh);
  153. outnobh:
  154. return -EINVAL;
  155. }
  156. /* That's simple too. */
  157. static int
  158. romfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  159. {
  160. buf->f_type = ROMFS_MAGIC;
  161. buf->f_bsize = ROMBSIZE;
  162. buf->f_bfree = buf->f_bavail = buf->f_ffree;
  163. buf->f_blocks = (romfs_maxsize(dentry->d_sb)+ROMBSIZE-1)>>ROMBSBITS;
  164. buf->f_namelen = ROMFS_MAXFN;
  165. return 0;
  166. }
  167. /* some helper routines */
  168. static int
  169. romfs_strnlen(struct inode *i, unsigned long offset, unsigned long count)
  170. {
  171. struct buffer_head *bh;
  172. unsigned long avail, maxsize, res;
  173. maxsize = romfs_maxsize(i->i_sb);
  174. if (offset >= maxsize)
  175. return -1;
  176. /* strnlen is almost always valid */
  177. if (count > maxsize || offset+count > maxsize)
  178. count = maxsize-offset;
  179. bh = sb_bread(i->i_sb, offset>>ROMBSBITS);
  180. if (!bh)
  181. return -1; /* error */
  182. avail = ROMBSIZE - (offset & ROMBMASK);
  183. maxsize = min_t(unsigned long, count, avail);
  184. res = strnlen(((char *)bh->b_data)+(offset&ROMBMASK), maxsize);
  185. brelse(bh);
  186. if (res < maxsize)
  187. return res; /* found all of it */
  188. while (res < count) {
  189. offset += maxsize;
  190. bh = sb_bread(i->i_sb, offset>>ROMBSBITS);
  191. if (!bh)
  192. return -1;
  193. maxsize = min_t(unsigned long, count - res, ROMBSIZE);
  194. avail = strnlen(bh->b_data, maxsize);
  195. res += avail;
  196. brelse(bh);
  197. if (avail < maxsize)
  198. return res;
  199. }
  200. return res;
  201. }
  202. static int
  203. romfs_copyfrom(struct inode *i, void *dest, unsigned long offset, unsigned long count)
  204. {
  205. struct buffer_head *bh;
  206. unsigned long avail, maxsize, res;
  207. maxsize = romfs_maxsize(i->i_sb);
  208. if (offset >= maxsize || count > maxsize || offset+count>maxsize)
  209. return -1;
  210. bh = sb_bread(i->i_sb, offset>>ROMBSBITS);
  211. if (!bh)
  212. return -1; /* error */
  213. avail = ROMBSIZE - (offset & ROMBMASK);
  214. maxsize = min_t(unsigned long, count, avail);
  215. memcpy(dest, ((char *)bh->b_data) + (offset & ROMBMASK), maxsize);
  216. brelse(bh);
  217. res = maxsize; /* all of it */
  218. while (res < count) {
  219. offset += maxsize;
  220. dest += maxsize;
  221. bh = sb_bread(i->i_sb, offset>>ROMBSBITS);
  222. if (!bh)
  223. return -1;
  224. maxsize = min_t(unsigned long, count - res, ROMBSIZE);
  225. memcpy(dest, bh->b_data, maxsize);
  226. brelse(bh);
  227. res += maxsize;
  228. }
  229. return res;
  230. }
  231. static unsigned char romfs_dtype_table[] = {
  232. DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_SOCK, DT_FIFO
  233. };
  234. static int
  235. romfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  236. {
  237. struct inode *i = filp->f_path.dentry->d_inode;
  238. struct romfs_inode ri;
  239. unsigned long offset, maxoff;
  240. int j, ino, nextfh;
  241. int stored = 0;
  242. char fsname[ROMFS_MAXFN]; /* XXX dynamic? */
  243. lock_kernel();
  244. maxoff = romfs_maxsize(i->i_sb);
  245. offset = filp->f_pos;
  246. if (!offset) {
  247. offset = i->i_ino & ROMFH_MASK;
  248. if (romfs_copyfrom(i, &ri, offset, ROMFH_SIZE) <= 0)
  249. goto out;
  250. offset = be32_to_cpu(ri.spec) & ROMFH_MASK;
  251. }
  252. /* Not really failsafe, but we are read-only... */
  253. for(;;) {
  254. if (!offset || offset >= maxoff) {
  255. offset = maxoff;
  256. filp->f_pos = offset;
  257. goto out;
  258. }
  259. filp->f_pos = offset;
  260. /* Fetch inode info */
  261. if (romfs_copyfrom(i, &ri, offset, ROMFH_SIZE) <= 0)
  262. goto out;
  263. j = romfs_strnlen(i, offset+ROMFH_SIZE, sizeof(fsname)-1);
  264. if (j < 0)
  265. goto out;
  266. fsname[j]=0;
  267. romfs_copyfrom(i, fsname, offset+ROMFH_SIZE, j);
  268. ino = offset;
  269. nextfh = be32_to_cpu(ri.next);
  270. if ((nextfh & ROMFH_TYPE) == ROMFH_HRD)
  271. ino = be32_to_cpu(ri.spec);
  272. if (filldir(dirent, fsname, j, offset, ino,
  273. romfs_dtype_table[nextfh & ROMFH_TYPE]) < 0) {
  274. goto out;
  275. }
  276. stored++;
  277. offset = nextfh & ROMFH_MASK;
  278. }
  279. out:
  280. unlock_kernel();
  281. return stored;
  282. }
  283. static struct dentry *
  284. romfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  285. {
  286. unsigned long offset, maxoff;
  287. int fslen, res;
  288. struct inode *inode;
  289. char fsname[ROMFS_MAXFN]; /* XXX dynamic? */
  290. struct romfs_inode ri;
  291. const char *name; /* got from dentry */
  292. int len;
  293. res = -EACCES; /* placeholder for "no data here" */
  294. offset = dir->i_ino & ROMFH_MASK;
  295. lock_kernel();
  296. if (romfs_copyfrom(dir, &ri, offset, ROMFH_SIZE) <= 0)
  297. goto out;
  298. maxoff = romfs_maxsize(dir->i_sb);
  299. offset = be32_to_cpu(ri.spec) & ROMFH_MASK;
  300. /* OK, now find the file whose name is in "dentry" in the
  301. * directory specified by "dir". */
  302. name = dentry->d_name.name;
  303. len = dentry->d_name.len;
  304. for(;;) {
  305. if (!offset || offset >= maxoff)
  306. goto out0;
  307. if (romfs_copyfrom(dir, &ri, offset, ROMFH_SIZE) <= 0)
  308. goto out;
  309. /* try to match the first 16 bytes of name */
  310. fslen = romfs_strnlen(dir, offset+ROMFH_SIZE, ROMFH_SIZE);
  311. if (len < ROMFH_SIZE) {
  312. if (len == fslen) {
  313. /* both are shorter, and same size */
  314. romfs_copyfrom(dir, fsname, offset+ROMFH_SIZE, len+1);
  315. if (strncmp (name, fsname, len) == 0)
  316. break;
  317. }
  318. } else if (fslen >= ROMFH_SIZE) {
  319. /* both are longer; XXX optimize max size */
  320. fslen = romfs_strnlen(dir, offset+ROMFH_SIZE, sizeof(fsname)-1);
  321. if (len == fslen) {
  322. romfs_copyfrom(dir, fsname, offset+ROMFH_SIZE, len+1);
  323. if (strncmp(name, fsname, len) == 0)
  324. break;
  325. }
  326. }
  327. /* next entry */
  328. offset = be32_to_cpu(ri.next) & ROMFH_MASK;
  329. }
  330. /* Hard link handling */
  331. if ((be32_to_cpu(ri.next) & ROMFH_TYPE) == ROMFH_HRD)
  332. offset = be32_to_cpu(ri.spec) & ROMFH_MASK;
  333. if ((inode = iget(dir->i_sb, offset)))
  334. goto outi;
  335. /*
  336. * it's a bit funky, _lookup needs to return an error code
  337. * (negative) or a NULL, both as a dentry. ENOENT should not
  338. * be returned, instead we need to create a negative dentry by
  339. * d_add(dentry, NULL); and return 0 as no error.
  340. * (Although as I see, it only matters on writable file
  341. * systems).
  342. */
  343. out0: inode = NULL;
  344. outi: res = 0;
  345. d_add (dentry, inode);
  346. out: unlock_kernel();
  347. return ERR_PTR(res);
  348. }
  349. /*
  350. * Ok, we do readpage, to be able to execute programs. Unfortunately,
  351. * we can't use bmap, since we may have looser alignments.
  352. */
  353. static int
  354. romfs_readpage(struct file *file, struct page * page)
  355. {
  356. struct inode *inode = page->mapping->host;
  357. loff_t offset, avail, readlen;
  358. void *buf;
  359. int result = -EIO;
  360. page_cache_get(page);
  361. lock_kernel();
  362. buf = kmap(page);
  363. if (!buf)
  364. goto err_out;
  365. /* 32 bit warning -- but not for us :) */
  366. offset = page_offset(page);
  367. if (offset < i_size_read(inode)) {
  368. avail = inode->i_size-offset;
  369. readlen = min_t(unsigned long, avail, PAGE_SIZE);
  370. if (romfs_copyfrom(inode, buf, ROMFS_I(inode)->i_dataoffset+offset, readlen) == readlen) {
  371. if (readlen < PAGE_SIZE) {
  372. memset(buf + readlen,0,PAGE_SIZE-readlen);
  373. }
  374. SetPageUptodate(page);
  375. result = 0;
  376. }
  377. }
  378. if (result) {
  379. memset(buf, 0, PAGE_SIZE);
  380. SetPageError(page);
  381. }
  382. flush_dcache_page(page);
  383. unlock_page(page);
  384. kunmap(page);
  385. err_out:
  386. page_cache_release(page);
  387. unlock_kernel();
  388. return result;
  389. }
  390. /* Mapping from our types to the kernel */
  391. static const struct address_space_operations romfs_aops = {
  392. .readpage = romfs_readpage
  393. };
  394. static const struct file_operations romfs_dir_operations = {
  395. .read = generic_read_dir,
  396. .readdir = romfs_readdir,
  397. };
  398. static const struct inode_operations romfs_dir_inode_operations = {
  399. .lookup = romfs_lookup,
  400. };
  401. static mode_t romfs_modemap[] =
  402. {
  403. 0, S_IFDIR+0644, S_IFREG+0644, S_IFLNK+0777,
  404. S_IFBLK+0600, S_IFCHR+0600, S_IFSOCK+0644, S_IFIFO+0644
  405. };
  406. static void
  407. romfs_read_inode(struct inode *i)
  408. {
  409. int nextfh, ino;
  410. struct romfs_inode ri;
  411. ino = i->i_ino & ROMFH_MASK;
  412. i->i_mode = 0;
  413. /* Loop for finding the real hard link */
  414. for(;;) {
  415. if (romfs_copyfrom(i, &ri, ino, ROMFH_SIZE) <= 0) {
  416. printk("romfs: read error for inode 0x%x\n", ino);
  417. return;
  418. }
  419. /* XXX: do romfs_checksum here too (with name) */
  420. nextfh = be32_to_cpu(ri.next);
  421. if ((nextfh & ROMFH_TYPE) != ROMFH_HRD)
  422. break;
  423. ino = be32_to_cpu(ri.spec) & ROMFH_MASK;
  424. }
  425. i->i_nlink = 1; /* Hard to decide.. */
  426. i->i_size = be32_to_cpu(ri.size);
  427. i->i_mtime.tv_sec = i->i_atime.tv_sec = i->i_ctime.tv_sec = 0;
  428. i->i_mtime.tv_nsec = i->i_atime.tv_nsec = i->i_ctime.tv_nsec = 0;
  429. i->i_uid = i->i_gid = 0;
  430. /* Precalculate the data offset */
  431. ino = romfs_strnlen(i, ino+ROMFH_SIZE, ROMFS_MAXFN);
  432. if (ino >= 0)
  433. ino = ((ROMFH_SIZE+ino+1+ROMFH_PAD)&ROMFH_MASK);
  434. else
  435. ino = 0;
  436. ROMFS_I(i)->i_metasize = ino;
  437. ROMFS_I(i)->i_dataoffset = ino+(i->i_ino&ROMFH_MASK);
  438. /* Compute permissions */
  439. ino = romfs_modemap[nextfh & ROMFH_TYPE];
  440. /* only "normal" files have ops */
  441. switch (nextfh & ROMFH_TYPE) {
  442. case 1:
  443. i->i_size = ROMFS_I(i)->i_metasize;
  444. i->i_op = &romfs_dir_inode_operations;
  445. i->i_fop = &romfs_dir_operations;
  446. if (nextfh & ROMFH_EXEC)
  447. ino |= S_IXUGO;
  448. i->i_mode = ino;
  449. break;
  450. case 2:
  451. i->i_fop = &generic_ro_fops;
  452. i->i_data.a_ops = &romfs_aops;
  453. if (nextfh & ROMFH_EXEC)
  454. ino |= S_IXUGO;
  455. i->i_mode = ino;
  456. break;
  457. case 3:
  458. i->i_op = &page_symlink_inode_operations;
  459. i->i_data.a_ops = &romfs_aops;
  460. i->i_mode = ino | S_IRWXUGO;
  461. break;
  462. default:
  463. /* depending on MBZ for sock/fifos */
  464. nextfh = be32_to_cpu(ri.spec);
  465. init_special_inode(i, ino,
  466. MKDEV(nextfh>>16,nextfh&0xffff));
  467. }
  468. }
  469. static struct kmem_cache * romfs_inode_cachep;
  470. static struct inode *romfs_alloc_inode(struct super_block *sb)
  471. {
  472. struct romfs_inode_info *ei;
  473. ei = (struct romfs_inode_info *)kmem_cache_alloc(romfs_inode_cachep, GFP_KERNEL);
  474. if (!ei)
  475. return NULL;
  476. return &ei->vfs_inode;
  477. }
  478. static void romfs_destroy_inode(struct inode *inode)
  479. {
  480. kmem_cache_free(romfs_inode_cachep, ROMFS_I(inode));
  481. }
  482. static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
  483. {
  484. struct romfs_inode_info *ei = (struct romfs_inode_info *) foo;
  485. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  486. SLAB_CTOR_CONSTRUCTOR)
  487. inode_init_once(&ei->vfs_inode);
  488. }
  489. static int init_inodecache(void)
  490. {
  491. romfs_inode_cachep = kmem_cache_create("romfs_inode_cache",
  492. sizeof(struct romfs_inode_info),
  493. 0, (SLAB_RECLAIM_ACCOUNT|
  494. SLAB_MEM_SPREAD),
  495. init_once, NULL);
  496. if (romfs_inode_cachep == NULL)
  497. return -ENOMEM;
  498. return 0;
  499. }
  500. static void destroy_inodecache(void)
  501. {
  502. kmem_cache_destroy(romfs_inode_cachep);
  503. }
  504. static int romfs_remount(struct super_block *sb, int *flags, char *data)
  505. {
  506. *flags |= MS_RDONLY;
  507. return 0;
  508. }
  509. static const struct super_operations romfs_ops = {
  510. .alloc_inode = romfs_alloc_inode,
  511. .destroy_inode = romfs_destroy_inode,
  512. .read_inode = romfs_read_inode,
  513. .statfs = romfs_statfs,
  514. .remount_fs = romfs_remount,
  515. };
  516. static int romfs_get_sb(struct file_system_type *fs_type,
  517. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  518. {
  519. return get_sb_bdev(fs_type, flags, dev_name, data, romfs_fill_super,
  520. mnt);
  521. }
  522. static struct file_system_type romfs_fs_type = {
  523. .owner = THIS_MODULE,
  524. .name = "romfs",
  525. .get_sb = romfs_get_sb,
  526. .kill_sb = kill_block_super,
  527. .fs_flags = FS_REQUIRES_DEV,
  528. };
  529. static int __init init_romfs_fs(void)
  530. {
  531. int err = init_inodecache();
  532. if (err)
  533. goto out1;
  534. err = register_filesystem(&romfs_fs_type);
  535. if (err)
  536. goto out;
  537. return 0;
  538. out:
  539. destroy_inodecache();
  540. out1:
  541. return err;
  542. }
  543. static void __exit exit_romfs_fs(void)
  544. {
  545. unregister_filesystem(&romfs_fs_type);
  546. destroy_inodecache();
  547. }
  548. /* Yes, works even as a module... :) */
  549. module_init(init_romfs_fs)
  550. module_exit(exit_romfs_fs)
  551. MODULE_LICENSE("GPL");