map.c 9.7 KB

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