4 * Copyright (C) 1997-2002 Russell King
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.
10 #include <linux/buffer_head.h>
11 #include <asm/unaligned.h>
15 * The ADFS map is basically a set of sectors. Each sector is called a
16 * zone which contains a bitstream made up of variable sized fragments.
17 * Each bit refers to a set of bytes in the filesystem, defined by
18 * log2bpmb. This may be larger or smaller than the sector size, but
19 * the overall size it describes will always be a round number of
20 * sectors. A fragment id is always idlen bits long.
23 * +---------+-------//---------+---+
24 * | frag id | 0000....000000 | 1 |
25 * +---------+-------//---------+---+
27 * The physical disk space used by a fragment is taken from the start of
28 * the fragment id up to and including the '1' bit - ie, idlen + n + 1
31 * A fragment id can be repeated multiple times in the whole map for
32 * large or fragmented files. The first map zone a fragment starts in
33 * is given by fragment id / ids_per_zone - this allows objects to start
34 * from any zone on the disk.
36 * Free space is described by a linked list of fragments. Each free
37 * fragment describes free space in the same way as the other fragments,
38 * however, the frag id specifies an offset (in map bits) from the end
39 * of this fragment to the start of the next free fragment.
41 * Objects stored on the disk are allocated object ids (we use these as
42 * our inode numbers.) Object ids contain a fragment id and an optional
43 * offset. This allows a directory fragment to contain small files
44 * associated with that directory.
50 static DEFINE_RWLOCK(adfs_map_lock);
53 * This is fun. We need to load up to 19 bits from the map at an
54 * arbitary bit alignment. (We're limited to 19 bits by F+ version 2).
56 #define GET_FRAG_ID(_map,_start,_idmask) \
58 unsigned char *_m = _map + (_start >> 3); \
59 u32 _frag = get_unaligned_le32(_m); \
60 _frag >>= (_start & 7); \
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
68 * gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
71 lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
72 const unsigned int frag_id, unsigned int *offset)
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;
82 frag = GET_FRAG_ID(map, start, idmask);
83 mapptr = start + idlen;
86 * find end of fragment
89 __le32 *_map = (__le32 *)map;
90 u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
92 mapptr = (mapptr & ~31) + 32;
93 if (mapptr >= mapsize)
95 v = le32_to_cpu(_map[mapptr >> 5]);
98 mapptr += 1 + ffz(~v);
105 } while (mapptr < mapsize);
109 printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
110 frag, start, mapptr);
115 int length = mapptr - start;
116 if (*offset >= length) {
121 return start + *offset;
125 * Scan the free space map, for this zone, calculating the total
126 * number of map bits in each free space fragment.
128 * Note: idmask is limited to 15 bits [3.2]
131 scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
133 const unsigned int mapsize = dm->dm_endbit + 32;
134 const unsigned int idlen = asb->s_idlen;
135 const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
136 const u32 idmask = (1 << frag_idlen) - 1;
137 unsigned char *map = dm->dm_bh->b_data;
138 unsigned int start = 8, mapptr;
140 unsigned long total = 0;
145 frag = GET_FRAG_ID(map, start, idmask);
148 * If the freelink is null, then no free fragments
149 * exist in this zone.
160 frag = GET_FRAG_ID(map, start, idmask);
161 mapptr = start + idlen;
164 * find end of fragment
167 __le32 *_map = (__le32 *)map;
168 u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
170 mapptr = (mapptr & ~31) + 32;
171 if (mapptr >= mapsize)
173 v = le32_to_cpu(_map[mapptr >> 5]);
176 mapptr += 1 + ffz(~v);
179 total += mapptr - start;
180 } while (frag >= idlen + 1);
183 printk(KERN_ERR "adfs: undersized free fragment\n");
187 printk(KERN_ERR "adfs: oversized free fragment\n");
192 scan_map(struct adfs_sb_info *asb, unsigned int zone,
193 const unsigned int frag_id, unsigned int mapoff)
195 const unsigned int idlen = asb->s_idlen;
196 struct adfs_discmap *dm, *dm_end;
199 dm = asb->s_map + zone;
200 zone = asb->s_map_size;
201 dm_end = asb->s_map + zone;
204 result = lookup_zone(dm, idlen, frag_id, &mapoff);
212 } while (--zone > 0);
216 result -= dm->dm_startbit;
217 result += dm->dm_startblk;
223 * calculate the amount of free blocks in the map.
226 * total_free = E(free_in_zone_n)
230 adfs_map_free(struct super_block *sb)
232 struct adfs_sb_info *asb = ADFS_SB(sb);
233 struct adfs_discmap *dm;
234 unsigned int total = 0;
238 zone = asb->s_map_size;
241 total += scan_free_map(asb, dm++);
242 } while (--zone > 0);
244 return signed_asl(total, asb->s_map2blk);
248 adfs_map_lookup(struct super_block *sb, unsigned int frag_id,
251 struct adfs_sb_info *asb = ADFS_SB(sb);
252 unsigned int zone, mapoff;
256 * map & root fragment is special - it starts in the center of the
257 * disk. The other fragments start at zone (frag / ids_per_zone)
259 if (frag_id == ADFS_ROOT_FRAG)
260 zone = asb->s_map_size >> 1;
262 zone = frag_id / asb->s_ids_per_zone;
264 if (zone >= asb->s_map_size)
267 /* Convert sector offset to map offset */
268 mapoff = signed_asl(offset, -asb->s_map2blk);
270 read_lock(&adfs_map_lock);
271 result = scan_map(asb, zone, frag_id, mapoff);
272 read_unlock(&adfs_map_lock);
277 /* Calculate sector offset into map block */
278 secoff = offset - signed_asl(mapoff, asb->s_map2blk);
279 return secoff + signed_asl(result, asb->s_map2blk);
282 adfs_error(sb, "fragment 0x%04x at offset %d not found in map",
287 adfs_error(sb, "invalid fragment 0x%04x (zone = %d, max = %d)",
288 frag_id, zone, asb->s_map_size);