map.c 8.4 KB

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