map.c 9.8 KB

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