map.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * linux/fs/hpfs/map.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * mapping structures to memory with some minimal checks
  7. */
  8. #include "hpfs_fn.h"
  9. unsigned *hpfs_map_dnode_bitmap(struct super_block *s, struct quad_buffer_head *qbh)
  10. {
  11. return hpfs_map_4sectors(s, hpfs_sb(s)->sb_dmap, qbh, 0);
  12. }
  13. unsigned int *hpfs_map_bitmap(struct super_block *s, unsigned bmp_block,
  14. struct quad_buffer_head *qbh, char *id)
  15. {
  16. secno sec;
  17. if (hpfs_sb(s)->sb_chk) if (bmp_block * 16384 > hpfs_sb(s)->sb_fs_size) {
  18. hpfs_error(s, "hpfs_map_bitmap called with bad parameter: %08x at %s", bmp_block, id);
  19. return NULL;
  20. }
  21. sec = le32_to_cpu(hpfs_sb(s)->sb_bmp_dir[bmp_block]);
  22. if (!sec || sec > hpfs_sb(s)->sb_fs_size-4) {
  23. hpfs_error(s, "invalid bitmap block pointer %08x -> %08x at %s", bmp_block, sec, id);
  24. return NULL;
  25. }
  26. return hpfs_map_4sectors(s, sec, qbh, 4);
  27. }
  28. /*
  29. * Load first code page into kernel memory, return pointer to 256-byte array,
  30. * first 128 bytes are uppercasing table for chars 128-255, next 128 bytes are
  31. * lowercasing table
  32. */
  33. unsigned char *hpfs_load_code_page(struct super_block *s, secno cps)
  34. {
  35. struct buffer_head *bh;
  36. secno cpds;
  37. unsigned cpi;
  38. unsigned char *ptr;
  39. unsigned char *cp_table;
  40. int i;
  41. struct code_page_data *cpd;
  42. struct code_page_directory *cp = hpfs_map_sector(s, cps, &bh, 0);
  43. if (!cp) return NULL;
  44. if (le32_to_cpu(cp->magic) != CP_DIR_MAGIC) {
  45. printk("HPFS: Code page directory magic doesn't match (magic = %08x)\n", le32_to_cpu(cp->magic));
  46. brelse(bh);
  47. return NULL;
  48. }
  49. if (!le32_to_cpu(cp->n_code_pages)) {
  50. printk("HPFS: n_code_pages == 0\n");
  51. brelse(bh);
  52. return NULL;
  53. }
  54. cpds = le32_to_cpu(cp->array[0].code_page_data);
  55. cpi = le16_to_cpu(cp->array[0].index);
  56. brelse(bh);
  57. if (cpi >= 3) {
  58. printk("HPFS: Code page index out of array\n");
  59. return NULL;
  60. }
  61. if (!(cpd = hpfs_map_sector(s, cpds, &bh, 0))) return NULL;
  62. if (le16_to_cpu(cpd->offs[cpi]) > 0x178) {
  63. printk("HPFS: Code page index out of sector\n");
  64. brelse(bh);
  65. return NULL;
  66. }
  67. ptr = (unsigned char *)cpd + le16_to_cpu(cpd->offs[cpi]) + 6;
  68. if (!(cp_table = kmalloc(256, GFP_KERNEL))) {
  69. printk("HPFS: out of memory for code page table\n");
  70. brelse(bh);
  71. return NULL;
  72. }
  73. memcpy(cp_table, ptr, 128);
  74. brelse(bh);
  75. /* Try to build lowercasing table from uppercasing one */
  76. for (i=128; i<256; i++) cp_table[i]=i;
  77. for (i=128; i<256; i++) if (cp_table[i-128]!=i && cp_table[i-128]>=128)
  78. cp_table[cp_table[i-128]] = i;
  79. return cp_table;
  80. }
  81. secno *hpfs_load_bitmap_directory(struct super_block *s, secno bmp)
  82. {
  83. struct buffer_head *bh;
  84. int n = (hpfs_sb(s)->sb_fs_size + 0x200000 - 1) >> 21;
  85. int i;
  86. secno *b;
  87. if (!(b = kmalloc(n * 512, GFP_KERNEL))) {
  88. printk("HPFS: can't allocate memory for bitmap directory\n");
  89. return NULL;
  90. }
  91. for (i=0;i<n;i++) {
  92. secno *d = hpfs_map_sector(s, bmp+i, &bh, n - i - 1);
  93. if (!d) {
  94. kfree(b);
  95. return NULL;
  96. }
  97. memcpy((char *)b + 512 * i, d, 512);
  98. brelse(bh);
  99. }
  100. return b;
  101. }
  102. /*
  103. * Load fnode to memory
  104. */
  105. struct fnode *hpfs_map_fnode(struct super_block *s, ino_t ino, struct buffer_head **bhp)
  106. {
  107. struct fnode *fnode;
  108. if (hpfs_sb(s)->sb_chk) if (hpfs_chk_sectors(s, ino, 1, "fnode")) {
  109. return NULL;
  110. }
  111. if ((fnode = hpfs_map_sector(s, ino, bhp, FNODE_RD_AHEAD))) {
  112. if (hpfs_sb(s)->sb_chk) {
  113. struct extended_attribute *ea;
  114. struct extended_attribute *ea_end;
  115. if (le32_to_cpu(fnode->magic) != FNODE_MAGIC) {
  116. hpfs_error(s, "bad magic on fnode %08lx",
  117. (unsigned long)ino);
  118. goto bail;
  119. }
  120. if (!fnode->dirflag) {
  121. if ((unsigned)fnode->btree.n_used_nodes + (unsigned)fnode->btree.n_free_nodes !=
  122. (fnode->btree.internal ? 12 : 8)) {
  123. hpfs_error(s,
  124. "bad number of nodes in fnode %08lx",
  125. (unsigned long)ino);
  126. goto bail;
  127. }
  128. if (le16_to_cpu(fnode->btree.first_free) !=
  129. 8 + fnode->btree.n_used_nodes * (fnode->btree.internal ? 8 : 12)) {
  130. hpfs_error(s,
  131. "bad first_free pointer in fnode %08lx",
  132. (unsigned long)ino);
  133. goto bail;
  134. }
  135. }
  136. if (le16_to_cpu(fnode->ea_size_s) && (le16_to_cpu(fnode->ea_offs) < 0xc4 ||
  137. le16_to_cpu(fnode->ea_offs) + le16_to_cpu(fnode->acl_size_s) + le16_to_cpu(fnode->ea_size_s) > 0x200)) {
  138. hpfs_error(s,
  139. "bad EA info in fnode %08lx: ea_offs == %04x ea_size_s == %04x",
  140. (unsigned long)ino,
  141. le16_to_cpu(fnode->ea_offs), le16_to_cpu(fnode->ea_size_s));
  142. goto bail;
  143. }
  144. ea = fnode_ea(fnode);
  145. ea_end = fnode_end_ea(fnode);
  146. while (ea != ea_end) {
  147. if (ea > ea_end) {
  148. hpfs_error(s, "bad EA in fnode %08lx",
  149. (unsigned long)ino);
  150. goto bail;
  151. }
  152. ea = next_ea(ea);
  153. }
  154. }
  155. }
  156. return fnode;
  157. bail:
  158. brelse(*bhp);
  159. return NULL;
  160. }
  161. struct anode *hpfs_map_anode(struct super_block *s, anode_secno ano, struct buffer_head **bhp)
  162. {
  163. struct anode *anode;
  164. if (hpfs_sb(s)->sb_chk) if (hpfs_chk_sectors(s, ano, 1, "anode")) return NULL;
  165. if ((anode = hpfs_map_sector(s, ano, bhp, ANODE_RD_AHEAD)))
  166. if (hpfs_sb(s)->sb_chk) {
  167. if (le32_to_cpu(anode->magic) != ANODE_MAGIC) {
  168. hpfs_error(s, "bad magic on anode %08x", ano);
  169. goto bail;
  170. }
  171. if (le32_to_cpu(anode->self) != ano) {
  172. hpfs_error(s, "self pointer invalid on anode %08x", ano);
  173. goto bail;
  174. }
  175. if ((unsigned)anode->btree.n_used_nodes + (unsigned)anode->btree.n_free_nodes !=
  176. (anode->btree.internal ? 60 : 40)) {
  177. hpfs_error(s, "bad number of nodes in anode %08x", ano);
  178. goto bail;
  179. }
  180. if (le16_to_cpu(anode->btree.first_free) !=
  181. 8 + anode->btree.n_used_nodes * (anode->btree.internal ? 8 : 12)) {
  182. hpfs_error(s, "bad first_free pointer in anode %08x", ano);
  183. goto bail;
  184. }
  185. }
  186. return anode;
  187. bail:
  188. brelse(*bhp);
  189. return NULL;
  190. }
  191. /*
  192. * Load dnode to memory and do some checks
  193. */
  194. struct dnode *hpfs_map_dnode(struct super_block *s, unsigned secno,
  195. struct quad_buffer_head *qbh)
  196. {
  197. struct dnode *dnode;
  198. if (hpfs_sb(s)->sb_chk) {
  199. if (hpfs_chk_sectors(s, secno, 4, "dnode")) return NULL;
  200. if (secno & 3) {
  201. hpfs_error(s, "dnode %08x not byte-aligned", secno);
  202. return NULL;
  203. }
  204. }
  205. if ((dnode = hpfs_map_4sectors(s, secno, qbh, DNODE_RD_AHEAD)))
  206. if (hpfs_sb(s)->sb_chk) {
  207. unsigned p, pp = 0;
  208. unsigned char *d = (unsigned char *)dnode;
  209. int b = 0;
  210. if (le32_to_cpu(dnode->magic) != DNODE_MAGIC) {
  211. hpfs_error(s, "bad magic on dnode %08x", secno);
  212. goto bail;
  213. }
  214. if (le32_to_cpu(dnode->self) != secno)
  215. hpfs_error(s, "bad self pointer on dnode %08x self = %08x", secno, le32_to_cpu(dnode->self));
  216. /* Check dirents - bad dirents would cause infinite
  217. loops or shooting to memory */
  218. if (le32_to_cpu(dnode->first_free) > 2048) {
  219. hpfs_error(s, "dnode %08x has first_free == %08x", secno, le32_to_cpu(dnode->first_free));
  220. goto bail;
  221. }
  222. for (p = 20; p < le32_to_cpu(dnode->first_free); p += d[p] + (d[p+1] << 8)) {
  223. struct hpfs_dirent *de = (struct hpfs_dirent *)((char *)dnode + p);
  224. if (le16_to_cpu(de->length) > 292 || (le16_to_cpu(de->length) < 32) || (le16_to_cpu(de->length) & 3) || p + le16_to_cpu(de->length) > 2048) {
  225. hpfs_error(s, "bad dirent size in dnode %08x, dirent %03x, last %03x", secno, p, pp);
  226. goto bail;
  227. }
  228. if (((31 + de->namelen + de->down*4 + 3) & ~3) != le16_to_cpu(de->length)) {
  229. if (((31 + de->namelen + de->down*4 + 3) & ~3) < le16_to_cpu(de->length) && s->s_flags & MS_RDONLY) goto ok;
  230. hpfs_error(s, "namelen does not match dirent size in dnode %08x, dirent %03x, last %03x", secno, p, pp);
  231. goto bail;
  232. }
  233. ok:
  234. if (hpfs_sb(s)->sb_chk >= 2) b |= 1 << de->down;
  235. if (de->down) if (de_down_pointer(de) < 0x10) {
  236. hpfs_error(s, "bad down pointer in dnode %08x, dirent %03x, last %03x", secno, p, pp);
  237. goto bail;
  238. }
  239. pp = p;
  240. }
  241. if (p != le32_to_cpu(dnode->first_free)) {
  242. hpfs_error(s, "size on last dirent does not match first_free; dnode %08x", secno);
  243. goto bail;
  244. }
  245. if (d[pp + 30] != 1 || d[pp + 31] != 255) {
  246. hpfs_error(s, "dnode %08x does not end with \\377 entry", secno);
  247. goto bail;
  248. }
  249. if (b == 3) printk("HPFS: warning: unbalanced dnode tree, dnode %08x; see hpfs.txt 4 more info\n", secno);
  250. }
  251. return dnode;
  252. bail:
  253. hpfs_brelse4(qbh);
  254. return NULL;
  255. }
  256. dnode_secno hpfs_fnode_dno(struct super_block *s, ino_t ino)
  257. {
  258. struct buffer_head *bh;
  259. struct fnode *fnode;
  260. dnode_secno dno;
  261. fnode = hpfs_map_fnode(s, ino, &bh);
  262. if (!fnode)
  263. return 0;
  264. dno = le32_to_cpu(fnode->u.external[0].disk_secno);
  265. brelse(bh);
  266. return dno;
  267. }