map.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/adfs/map.c
  4. *
  5. * Copyright (C) 1997-2002 Russell King
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/statfs.h>
  9. #include <asm/unaligned.h>
  10. #include "adfs.h"
  11. /*
  12. * The ADFS map is basically a set of sectors. Each sector is called a
  13. * zone which contains a bitstream made up of variable sized fragments.
  14. * Each bit refers to a set of bytes in the filesystem, defined by
  15. * log2bpmb. This may be larger or smaller than the sector size, but
  16. * the overall size it describes will always be a round number of
  17. * sectors. A fragment id is always idlen bits long.
  18. *
  19. * < idlen > < n > <1>
  20. * +---------+-------//---------+---+
  21. * | frag id | 0000....000000 | 1 |
  22. * +---------+-------//---------+---+
  23. *
  24. * The physical disk space used by a fragment is taken from the start of
  25. * the fragment id up to and including the '1' bit - ie, idlen + n + 1
  26. * bits.
  27. *
  28. * A fragment id can be repeated multiple times in the whole map for
  29. * large or fragmented files. The first map zone a fragment starts in
  30. * is given by fragment id / ids_per_zone - this allows objects to start
  31. * from any zone on the disk.
  32. *
  33. * Free space is described by a linked list of fragments. Each free
  34. * fragment describes free space in the same way as the other fragments,
  35. * however, the frag id specifies an offset (in map bits) from the end
  36. * of this fragment to the start of the next free fragment.
  37. *
  38. * Objects stored on the disk are allocated object ids (we use these as
  39. * our inode numbers.) Object ids contain a fragment id and an optional
  40. * offset. This allows a directory fragment to contain small files
  41. * associated with that directory.
  42. */
  43. /*
  44. * For the future...
  45. */
  46. static DEFINE_RWLOCK(adfs_map_lock);
  47. /*
  48. * This is fun. We need to load up to 19 bits from the map at an
  49. * arbitrary bit alignment. (We're limited to 19 bits by F+ version 2).
  50. */
  51. #define GET_FRAG_ID(_map,_start,_idmask) \
  52. ({ \
  53. unsigned char *_m = _map + (_start >> 3); \
  54. u32 _frag = get_unaligned_le32(_m); \
  55. _frag >>= (_start & 7); \
  56. _frag & _idmask; \
  57. })
  58. /*
  59. * return the map bit offset of the fragment frag_id in the zone dm.
  60. * Note that the loop is optimised for best asm code - look at the
  61. * output of:
  62. * gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
  63. */
  64. static int lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
  65. const u32 frag_id, unsigned int *offset)
  66. {
  67. const unsigned int endbit = dm->dm_endbit;
  68. const u32 idmask = (1 << idlen) - 1;
  69. unsigned char *map = dm->dm_bh->b_data;
  70. unsigned int start = dm->dm_startbit;
  71. unsigned int freelink, fragend;
  72. u32 frag;
  73. frag = GET_FRAG_ID(map, 8, idmask & 0x7fff);
  74. freelink = frag ? 8 + frag : 0;
  75. do {
  76. frag = GET_FRAG_ID(map, start, idmask);
  77. fragend = find_next_bit_le(map, endbit, start + idlen);
  78. if (fragend >= endbit)
  79. goto error;
  80. if (start == freelink) {
  81. freelink += frag & 0x7fff;
  82. } else if (frag == frag_id) {
  83. unsigned int length = fragend + 1 - start;
  84. if (*offset < length)
  85. return start + *offset;
  86. *offset -= length;
  87. }
  88. start = fragend + 1;
  89. } while (start < endbit);
  90. return -1;
  91. error:
  92. printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
  93. frag, start, fragend);
  94. return -1;
  95. }
  96. /*
  97. * Scan the free space map, for this zone, calculating the total
  98. * number of map bits in each free space fragment.
  99. *
  100. * Note: idmask is limited to 15 bits [3.2]
  101. */
  102. static unsigned int
  103. scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
  104. {
  105. const unsigned int endbit = dm->dm_endbit;
  106. const unsigned int idlen = asb->s_idlen;
  107. const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
  108. const u32 idmask = (1 << frag_idlen) - 1;
  109. unsigned char *map = dm->dm_bh->b_data;
  110. unsigned int start = 8, fragend;
  111. u32 frag;
  112. unsigned long total = 0;
  113. /*
  114. * get fragment id
  115. */
  116. frag = GET_FRAG_ID(map, start, idmask);
  117. /*
  118. * If the freelink is null, then no free fragments
  119. * exist in this zone.
  120. */
  121. if (frag == 0)
  122. return 0;
  123. do {
  124. start += frag;
  125. frag = GET_FRAG_ID(map, start, idmask);
  126. fragend = find_next_bit_le(map, endbit, start + idlen);
  127. if (fragend >= endbit)
  128. goto error;
  129. total += fragend + 1 - start;
  130. } while (frag >= idlen + 1);
  131. if (frag != 0)
  132. printk(KERN_ERR "adfs: undersized free fragment\n");
  133. return total;
  134. error:
  135. printk(KERN_ERR "adfs: oversized free fragment\n");
  136. return 0;
  137. }
  138. static int scan_map(struct adfs_sb_info *asb, unsigned int zone,
  139. const u32 frag_id, unsigned int mapoff)
  140. {
  141. const unsigned int idlen = asb->s_idlen;
  142. struct adfs_discmap *dm, *dm_end;
  143. int result;
  144. dm = asb->s_map + zone;
  145. zone = asb->s_map_size;
  146. dm_end = asb->s_map + zone;
  147. do {
  148. result = lookup_zone(dm, idlen, frag_id, &mapoff);
  149. if (result != -1)
  150. goto found;
  151. dm ++;
  152. if (dm == dm_end)
  153. dm = asb->s_map;
  154. } while (--zone > 0);
  155. return -1;
  156. found:
  157. result -= dm->dm_startbit;
  158. result += dm->dm_startblk;
  159. return result;
  160. }
  161. /*
  162. * calculate the amount of free blocks in the map.
  163. *
  164. * n=1
  165. * total_free = E(free_in_zone_n)
  166. * nzones
  167. */
  168. void adfs_map_statfs(struct super_block *sb, struct kstatfs *buf)
  169. {
  170. struct adfs_sb_info *asb = ADFS_SB(sb);
  171. struct adfs_discrecord *dr = adfs_map_discrecord(asb->s_map);
  172. struct adfs_discmap *dm;
  173. unsigned int total = 0;
  174. unsigned int zone;
  175. dm = asb->s_map;
  176. zone = asb->s_map_size;
  177. do {
  178. total += scan_free_map(asb, dm++);
  179. } while (--zone > 0);
  180. buf->f_blocks = adfs_disc_size(dr) >> sb->s_blocksize_bits;
  181. buf->f_files = asb->s_ids_per_zone * asb->s_map_size;
  182. buf->f_bavail =
  183. buf->f_bfree = signed_asl(total, asb->s_map2blk);
  184. }
  185. int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset)
  186. {
  187. struct adfs_sb_info *asb = ADFS_SB(sb);
  188. unsigned int zone, mapoff;
  189. int result;
  190. /*
  191. * map & root fragment is special - it starts in the center of the
  192. * disk. The other fragments start at zone (frag / ids_per_zone)
  193. */
  194. if (frag_id == ADFS_ROOT_FRAG)
  195. zone = asb->s_map_size >> 1;
  196. else
  197. zone = frag_id / asb->s_ids_per_zone;
  198. if (zone >= asb->s_map_size)
  199. goto bad_fragment;
  200. /* Convert sector offset to map offset */
  201. mapoff = signed_asl(offset, -asb->s_map2blk);
  202. read_lock(&adfs_map_lock);
  203. result = scan_map(asb, zone, frag_id, mapoff);
  204. read_unlock(&adfs_map_lock);
  205. if (result > 0) {
  206. unsigned int secoff;
  207. /* Calculate sector offset into map block */
  208. secoff = offset - signed_asl(mapoff, asb->s_map2blk);
  209. return secoff + signed_asl(result, asb->s_map2blk);
  210. }
  211. adfs_error(sb, "fragment 0x%04x at offset %d not found in map",
  212. frag_id, offset);
  213. return 0;
  214. bad_fragment:
  215. adfs_error(sb, "invalid fragment 0x%04x (zone = %d, max = %d)",
  216. frag_id, zone, asb->s_map_size);
  217. return 0;
  218. }
  219. static unsigned char adfs_calczonecheck(struct super_block *sb, unsigned char *map)
  220. {
  221. unsigned int v0, v1, v2, v3;
  222. int i;
  223. v0 = v1 = v2 = v3 = 0;
  224. for (i = sb->s_blocksize - 4; i; i -= 4) {
  225. v0 += map[i] + (v3 >> 8);
  226. v3 &= 0xff;
  227. v1 += map[i + 1] + (v0 >> 8);
  228. v0 &= 0xff;
  229. v2 += map[i + 2] + (v1 >> 8);
  230. v1 &= 0xff;
  231. v3 += map[i + 3] + (v2 >> 8);
  232. v2 &= 0xff;
  233. }
  234. v0 += v3 >> 8;
  235. v1 += map[1] + (v0 >> 8);
  236. v2 += map[2] + (v1 >> 8);
  237. v3 += map[3] + (v2 >> 8);
  238. return v0 ^ v1 ^ v2 ^ v3;
  239. }
  240. static int adfs_checkmap(struct super_block *sb, struct adfs_discmap *dm)
  241. {
  242. unsigned char crosscheck = 0, zonecheck = 1;
  243. int i;
  244. for (i = 0; i < ADFS_SB(sb)->s_map_size; i++) {
  245. unsigned char *map;
  246. map = dm[i].dm_bh->b_data;
  247. if (adfs_calczonecheck(sb, map) != map[0]) {
  248. adfs_error(sb, "zone %d fails zonecheck", i);
  249. zonecheck = 0;
  250. }
  251. crosscheck ^= map[3];
  252. }
  253. if (crosscheck != 0xff)
  254. adfs_error(sb, "crosscheck != 0xff");
  255. return crosscheck == 0xff && zonecheck;
  256. }
  257. /*
  258. * Layout the map - the first zone contains a copy of the disc record,
  259. * and the last zone must be limited to the size of the filesystem.
  260. */
  261. static void adfs_map_layout(struct adfs_discmap *dm, unsigned int nzones,
  262. struct adfs_discrecord *dr)
  263. {
  264. unsigned int zone, zone_size;
  265. u64 size;
  266. zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
  267. dm[0].dm_bh = NULL;
  268. dm[0].dm_startblk = 0;
  269. dm[0].dm_startbit = 32 + ADFS_DR_SIZE_BITS;
  270. dm[0].dm_endbit = 32 + zone_size;
  271. for (zone = 1; zone < nzones; zone++) {
  272. dm[zone].dm_bh = NULL;
  273. dm[zone].dm_startblk = zone * zone_size - ADFS_DR_SIZE_BITS;
  274. dm[zone].dm_startbit = 32;
  275. dm[zone].dm_endbit = 32 + zone_size;
  276. }
  277. size = adfs_disc_size(dr) >> dr->log2bpmb;
  278. size -= (nzones - 1) * zone_size - ADFS_DR_SIZE_BITS;
  279. dm[nzones - 1].dm_endbit = 32 + size;
  280. }
  281. static int adfs_map_read(struct adfs_discmap *dm, struct super_block *sb,
  282. unsigned int map_addr, unsigned int nzones)
  283. {
  284. unsigned int zone;
  285. for (zone = 0; zone < nzones; zone++) {
  286. dm[zone].dm_bh = sb_bread(sb, map_addr + zone);
  287. if (!dm[zone].dm_bh)
  288. return -EIO;
  289. }
  290. return 0;
  291. }
  292. static void adfs_map_relse(struct adfs_discmap *dm, unsigned int nzones)
  293. {
  294. unsigned int zone;
  295. for (zone = 0; zone < nzones; zone++)
  296. brelse(dm[zone].dm_bh);
  297. }
  298. struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr)
  299. {
  300. struct adfs_sb_info *asb = ADFS_SB(sb);
  301. struct adfs_discmap *dm;
  302. unsigned int map_addr, zone_size, nzones;
  303. int ret;
  304. nzones = dr->nzones | dr->nzones_high << 8;
  305. zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
  306. asb->s_idlen = dr->idlen;
  307. asb->s_map_size = nzones;
  308. asb->s_map2blk = dr->log2bpmb - dr->log2secsize;
  309. asb->s_log2sharesize = dr->log2sharesize;
  310. asb->s_ids_per_zone = zone_size / (asb->s_idlen + 1);
  311. map_addr = (nzones >> 1) * zone_size -
  312. ((nzones > 1) ? ADFS_DR_SIZE_BITS : 0);
  313. map_addr = signed_asl(map_addr, asb->s_map2blk);
  314. dm = kmalloc_array(nzones, sizeof(*dm), GFP_KERNEL);
  315. if (dm == NULL) {
  316. adfs_error(sb, "not enough memory");
  317. return ERR_PTR(-ENOMEM);
  318. }
  319. adfs_map_layout(dm, nzones, dr);
  320. ret = adfs_map_read(dm, sb, map_addr, nzones);
  321. if (ret) {
  322. adfs_error(sb, "unable to read map");
  323. goto error_free;
  324. }
  325. if (adfs_checkmap(sb, dm))
  326. return dm;
  327. adfs_error(sb, "map corrupted");
  328. error_free:
  329. adfs_map_relse(dm, nzones);
  330. kfree(dm);
  331. return ERR_PTR(-EIO);
  332. }
  333. void adfs_free_map(struct super_block *sb)
  334. {
  335. struct adfs_sb_info *asb = ADFS_SB(sb);
  336. adfs_map_relse(asb->s_map, asb->s_map_size);
  337. kfree(asb->s_map);
  338. }