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/errno.h>
12 #include <linux/adfs_fs.h>
13 #include <linux/spinlock.h>
14 #include <linux/buffer_head.h>
16 #include <asm/unaligned.h>
21 * The ADFS map is basically a set of sectors. Each sector is called a
22 * zone which contains a bitstream made up of variable sized fragments.
23 * Each bit refers to a set of bytes in the filesystem, defined by
24 * log2bpmb. This may be larger or smaller than the sector size, but
25 * the overall size it describes will always be a round number of
26 * sectors. A fragment id is always idlen bits long.
29 * +---------+-------//---------+---+
30 * | frag id | 0000....000000 | 1 |
31 * +---------+-------//---------+---+
33 * The physical disk space used by a fragment is taken from the start of
34 * the fragment id up to and including the '1' bit - ie, idlen + n + 1
37 * A fragment id can be repeated multiple times in the whole map for
38 * large or fragmented files. The first map zone a fragment starts in
39 * is given by fragment id / ids_per_zone - this allows objects to start
40 * from any zone on the disk.
42 * Free space is described by a linked list of fragments. Each free
43 * fragment describes free space in the same way as the other fragments,
44 * however, the frag id specifies an offset (in map bits) from the end
45 * of this fragment to the start of the next free fragment.
47 * Objects stored on the disk are allocated object ids (we use these as
48 * our inode numbers.) Object ids contain a fragment id and an optional
49 * offset. This allows a directory fragment to contain small files
50 * associated with that directory.
56 static DEFINE_RWLOCK(adfs_map_lock);
59 * This is fun. We need to load up to 19 bits from the map at an
60 * arbitary bit alignment. (We're limited to 19 bits by F+ version 2).
62 #define GET_FRAG_ID(_map,_start,_idmask) \
64 unsigned char *_m = _map + (_start >> 3); \
65 u32 _frag = get_unaligned((u32 *)_m); \
66 _frag >>= (_start & 7); \
71 * return the map bit offset of the fragment frag_id in the zone dm.
72 * Note that the loop is optimised for best asm code - look at the
74 * gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
77 lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
78 const unsigned int frag_id, unsigned int *offset)
80 const unsigned int mapsize = dm->dm_endbit;
81 const u32 idmask = (1 << idlen) - 1;
82 unsigned char *map = dm->dm_bh->b_data + 4;
83 unsigned int start = dm->dm_startbit;
88 frag = GET_FRAG_ID(map, start, idmask);
89 mapptr = start + idlen;
92 * find end of fragment
95 __le32 *_map = (__le32 *)map;
96 u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
98 mapptr = (mapptr & ~31) + 32;
99 if (mapptr >= mapsize)
101 v = le32_to_cpu(_map[mapptr >> 5]);
104 mapptr += 1 + ffz(~v);
111 } while (mapptr < mapsize);
115 printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
116 frag, start, mapptr);
121 int length = mapptr - start;
122 if (*offset >= length) {
127 return start + *offset;
131 * Scan the free space map, for this zone, calculating the total
132 * number of map bits in each free space fragment.
134 * Note: idmask is limited to 15 bits [3.2]
137 scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
139 const unsigned int mapsize = dm->dm_endbit + 32;
140 const unsigned int idlen = asb->s_idlen;
141 const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
142 const u32 idmask = (1 << frag_idlen) - 1;
143 unsigned char *map = dm->dm_bh->b_data;
144 unsigned int start = 8, mapptr;
146 unsigned long total = 0;
151 frag = GET_FRAG_ID(map, start, idmask);
154 * If the freelink is null, then no free fragments
155 * exist in this zone.
166 frag = GET_FRAG_ID(map, start, idmask);
167 mapptr = start + idlen;
170 * find end of fragment
173 __le32 *_map = (__le32 *)map;
174 u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
176 mapptr = (mapptr & ~31) + 32;
177 if (mapptr >= mapsize)
179 v = le32_to_cpu(_map[mapptr >> 5]);
182 mapptr += 1 + ffz(~v);
185 total += mapptr - start;
186 } while (frag >= idlen + 1);
189 printk(KERN_ERR "adfs: undersized free fragment\n");
193 printk(KERN_ERR "adfs: oversized free fragment\n");
198 scan_map(struct adfs_sb_info *asb, unsigned int zone,
199 const unsigned int frag_id, unsigned int mapoff)
201 const unsigned int idlen = asb->s_idlen;
202 struct adfs_discmap *dm, *dm_end;
205 dm = asb->s_map + zone;
206 zone = asb->s_map_size;
207 dm_end = asb->s_map + zone;
210 result = lookup_zone(dm, idlen, frag_id, &mapoff);
218 } while (--zone > 0);
222 result -= dm->dm_startbit;
223 result += dm->dm_startblk;
229 * calculate the amount of free blocks in the map.
232 * total_free = E(free_in_zone_n)
236 adfs_map_free(struct super_block *sb)
238 struct adfs_sb_info *asb = ADFS_SB(sb);
239 struct adfs_discmap *dm;
240 unsigned int total = 0;
244 zone = asb->s_map_size;
247 total += scan_free_map(asb, dm++);
248 } while (--zone > 0);
250 return signed_asl(total, asb->s_map2blk);
254 adfs_map_lookup(struct super_block *sb, unsigned int frag_id,
257 struct adfs_sb_info *asb = ADFS_SB(sb);
258 unsigned int zone, mapoff;
262 * map & root fragment is special - it starts in the center of the
263 * disk. The other fragments start at zone (frag / ids_per_zone)
265 if (frag_id == ADFS_ROOT_FRAG)
266 zone = asb->s_map_size >> 1;
268 zone = frag_id / asb->s_ids_per_zone;
270 if (zone >= asb->s_map_size)
273 /* Convert sector offset to map offset */
274 mapoff = signed_asl(offset, -asb->s_map2blk);
276 read_lock(&adfs_map_lock);
277 result = scan_map(asb, zone, frag_id, mapoff);
278 read_unlock(&adfs_map_lock);
283 /* Calculate sector offset into map block */
284 secoff = offset - signed_asl(mapoff, asb->s_map2blk);
285 return secoff + signed_asl(result, asb->s_map2blk);
288 adfs_error(sb, "fragment 0x%04x at offset %d not found in map",
293 adfs_error(sb, "invalid fragment 0x%04x (zone = %d, max = %d)",
294 frag_id, zone, asb->s_map_size);