inode.c 15 KB

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