original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
6.9 KiB

  1. /*
  2. * linux/fs/adfs/map.c
  3. *
  4. * Copyright (C) 1997-2002 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/buffer_head.h>
  11. #include <asm/unaligned.h>
  12. #include "adfs.h"
  13. /*
  14. * The ADFS map is basically a set of sectors. Each sector is called a
  15. * zone which contains a bitstream made up of variable sized fragments.
  16. * Each bit refers to a set of bytes in the filesystem, defined by
  17. * log2bpmb. This may be larger or smaller than the sector size, but
  18. * the overall size it describes will always be a round number of
  19. * sectors. A fragment id is always idlen bits long.
  20. *
  21. * < idlen > < n > <1>
  22. * +---------+-------//---------+---+
  23. * | frag id | 0000....000000 | 1 |
  24. * +---------+-------//---------+---+
  25. *
  26. * The physical disk space used by a fragment is taken from the start of
  27. * the fragment id up to and including the '1' bit - ie, idlen + n + 1
  28. * bits.
  29. *
  30. * A fragment id can be repeated multiple times in the whole map for
  31. * large or fragmented files. The first map zone a fragment starts in
  32. * is given by fragment id / ids_per_zone - this allows objects to start
  33. * from any zone on the disk.
  34. *
  35. * Free space is described by a linked list of fragments. Each free
  36. * fragment describes free space in the same way as the other fragments,
  37. * however, the frag id specifies an offset (in map bits) from the end
  38. * of this fragment to the start of the next free fragment.
  39. *
  40. * Objects stored on the disk are allocated object ids (we use these as
  41. * our inode numbers.) Object ids contain a fragment id and an optional
  42. * offset. This allows a directory fragment to contain small files
  43. * associated with that directory.
  44. */
  45. /*
  46. * For the future...
  47. */
  48. static DEFINE_RWLOCK(adfs_map_lock);
  49. /*
  50. * This is fun. We need to load up to 19 bits from the map at an
  51. * arbitrary bit alignment. (We're limited to 19 bits by F+ version 2).
  52. */
  53. #define GET_FRAG_ID(_map,_start,_idmask) \
  54. ({ \
  55. unsigned char *_m = _map + (_start >> 3); \
  56. u32 _frag = get_unaligned_le32(_m); \
  57. _frag >>= (_start & 7); \
  58. _frag & _idmask; \
  59. })
  60. /*
  61. * return the map bit offset of the fragment frag_id in the zone dm.
  62. * Note that the loop is optimised for best asm code - look at the
  63. * output of:
  64. * gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
  65. */
  66. static int
  67. lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
  68. const unsigned int frag_id, unsigned int *offset)
  69. {
  70. const unsigned int mapsize = dm->dm_endbit;
  71. const u32 idmask = (1 << idlen) - 1;
  72. unsigned char *map = dm->dm_bh->b_data + 4;
  73. unsigned int start = dm->dm_startbit;
  74. unsigned int mapptr;
  75. u32 frag;
  76. do {
  77. frag = GET_FRAG_ID(map, start, idmask);
  78. mapptr = start + idlen;
  79. /*
  80. * find end of fragment
  81. */
  82. {
  83. __le32 *_map = (__le32 *)map;
  84. u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
  85. while (v == 0) {
  86. mapptr = (mapptr & ~31) + 32;
  87. if (mapptr >= mapsize)
  88. goto error;
  89. v = le32_to_cpu(_map[mapptr >> 5]);
  90. }
  91. mapptr += 1 + ffz(~v);
  92. }
  93. if (frag == frag_id)
  94. goto found;
  95. again:
  96. start = mapptr;
  97. } while (mapptr < mapsize);
  98. return -1;
  99. error:
  100. printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
  101. frag, start, mapptr);
  102. return -1;
  103. found:
  104. {
  105. int length = mapptr - start;
  106. if (*offset >= length) {
  107. *offset -= length;
  108. goto again;
  109. }
  110. }
  111. return start + *offset;
  112. }
  113. /*
  114. * Scan the free space map, for this zone, calculating the total
  115. * number of map bits in each free space fragment.
  116. *
  117. * Note: idmask is limited to 15 bits [3.2]
  118. */
  119. static unsigned int
  120. scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
  121. {
  122. const unsigned int mapsize = dm->dm_endbit + 32;
  123. const unsigned int idlen = asb->s_idlen;
  124. const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
  125. const u32 idmask = (1 << frag_idlen) - 1;
  126. unsigned char *map = dm->dm_bh->b_data;
  127. unsigned int start = 8, mapptr;
  128. u32 frag;
  129. unsigned long total = 0;
  130. /*
  131. * get fragment id
  132. */
  133. frag = GET_FRAG_ID(map, start, idmask);
  134. /*
  135. * If the freelink is null, then no free fragments
  136. * exist in this zone.
  137. */
  138. if (frag == 0)
  139. return 0;
  140. do {
  141. start += frag;
  142. /*
  143. * get fragment id
  144. */
  145. frag = GET_FRAG_ID(map, start, idmask);
  146. mapptr = start + idlen;
  147. /*
  148. * find end of fragment
  149. */
  150. {
  151. __le32 *_map = (__le32 *)map;
  152. u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
  153. while (v == 0) {
  154. mapptr = (mapptr & ~31) + 32;
  155. if (mapptr >= mapsize)
  156. goto error;
  157. v = le32_to_cpu(_map[mapptr >> 5]);
  158. }
  159. mapptr += 1 + ffz(~v);
  160. }
  161. total += mapptr - start;
  162. } while (frag >= idlen + 1);
  163. if (frag != 0)
  164. printk(KERN_ERR "adfs: undersized free fragment\n");
  165. return total;
  166. error:
  167. printk(KERN_ERR "adfs: oversized free fragment\n");
  168. return 0;
  169. }
  170. static int
  171. scan_map(struct adfs_sb_info *asb, unsigned int zone,
  172. const unsigned int frag_id, unsigned int mapoff)
  173. {
  174. const unsigned int idlen = asb->s_idlen;
  175. struct adfs_discmap *dm, *dm_end;
  176. int result;
  177. dm = asb->s_map + zone;
  178. zone = asb->s_map_size;
  179. dm_end = asb->s_map + zone;
  180. do {
  181. result = lookup_zone(dm, idlen, frag_id, &mapoff);
  182. if (result != -1)
  183. goto found;
  184. dm ++;
  185. if (dm == dm_end)
  186. dm = asb->s_map;
  187. } while (--zone > 0);
  188. return -1;
  189. found:
  190. result -= dm->dm_startbit;
  191. result += dm->dm_startblk;
  192. return result;
  193. }
  194. /*
  195. * calculate the amount of free blocks in the map.
  196. *
  197. * n=1
  198. * total_free = E(free_in_zone_n)
  199. * nzones
  200. */
  201. unsigned int
  202. adfs_map_free(struct super_block *sb)
  203. {
  204. struct adfs_sb_info *asb = ADFS_SB(sb);
  205. struct adfs_discmap *dm;
  206. unsigned int total = 0;
  207. unsigned int zone;
  208. dm = asb->s_map;
  209. zone = asb->s_map_size;
  210. do {
  211. total += scan_free_map(asb, dm++);
  212. } while (--zone > 0);
  213. return signed_asl(total, asb->s_map2blk);
  214. }
  215. int
  216. adfs_map_lookup(struct super_block *sb, unsigned int frag_id,
  217. unsigned int offset)
  218. {
  219. struct adfs_sb_info *asb = ADFS_SB(sb);
  220. unsigned int zone, mapoff;
  221. int result;
  222. /*
  223. * map & root fragment is special - it starts in the center of the
  224. * disk. The other fragments start at zone (frag / ids_per_zone)
  225. */
  226. if (frag_id == ADFS_ROOT_FRAG)
  227. zone = asb->s_map_size >> 1;
  228. else
  229. zone = frag_id / asb->s_ids_per_zone;
  230. if (zone >= asb->s_map_size)
  231. goto bad_fragment;
  232. /* Convert sector offset to map offset */
  233. mapoff = signed_asl(offset, -asb->s_map2blk);
  234. read_lock(&adfs_map_lock);
  235. result = scan_map(asb, zone, frag_id, mapoff);
  236. read_unlock(&adfs_map_lock);
  237. if (result > 0) {
  238. unsigned int secoff;
  239. /* Calculate sector offset into map block */
  240. secoff = offset - signed_asl(mapoff, asb->s_map2blk);
  241. return secoff + signed_asl(result, asb->s_map2blk);
  242. }
  243. adfs_error(sb, "fragment 0x%04x at offset %d not found in map",
  244. frag_id, offset);
  245. return 0;
  246. bad_fragment:
  247. adfs_error(sb, "invalid fragment 0x%04x (zone = %d, max = %d)",
  248. frag_id, zone, asb->s_map_size);
  249. return 0;
  250. }