map.c 7.0 KB

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