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 rwlock_t adfs_map_lock = RW_LOCK_UNLOCKED;
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 u32 v, *_map = (u32 *)map;
97 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
99 mapptr = (mapptr & ~31) + 32;
100 if (mapptr >= mapsize)
102 v = le32_to_cpu(_map[mapptr >> 5]);
105 mapptr += 1 + ffz(~v);
112 } while (mapptr < mapsize);
116 printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
117 frag, start, mapptr);
122 int length = mapptr - start;
123 if (*offset >= length) {
128 return start + *offset;
132 * Scan the free space map, for this zone, calculating the total
133 * number of map bits in each free space fragment.
135 * Note: idmask is limited to 15 bits [3.2]
138 scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
140 const unsigned int mapsize = dm->dm_endbit + 32;
141 const unsigned int idlen = asb->s_idlen;
142 const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
143 const u32 idmask = (1 << frag_idlen) - 1;
144 unsigned char *map = dm->dm_bh->b_data;
145 unsigned int start = 8, mapptr;
147 unsigned long total = 0;
152 frag = GET_FRAG_ID(map, start, idmask);
155 * If the freelink is null, then no free fragments
156 * exist in this zone.
167 frag = GET_FRAG_ID(map, start, idmask);
168 mapptr = start + idlen;
171 * find end of fragment
174 u32 v, *_map = (u32 *)map;
176 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
178 mapptr = (mapptr & ~31) + 32;
179 if (mapptr >= mapsize)
181 v = le32_to_cpu(_map[mapptr >> 5]);
184 mapptr += 1 + ffz(~v);
187 total += mapptr - start;
188 } while (frag >= idlen + 1);
191 printk(KERN_ERR "adfs: undersized free fragment\n");
195 printk(KERN_ERR "adfs: oversized free fragment\n");
200 scan_map(struct adfs_sb_info *asb, unsigned int zone,
201 const unsigned int frag_id, unsigned int mapoff)
203 const unsigned int idlen = asb->s_idlen;
204 struct adfs_discmap *dm, *dm_end;
207 dm = asb->s_map + zone;
208 zone = asb->s_map_size;
209 dm_end = asb->s_map + zone;
212 result = lookup_zone(dm, idlen, frag_id, &mapoff);
220 } while (--zone > 0);
224 result -= dm->dm_startbit;
225 result += dm->dm_startblk;
231 * calculate the amount of free blocks in the map.
234 * total_free = E(free_in_zone_n)
238 adfs_map_free(struct super_block *sb)
240 struct adfs_sb_info *asb = ADFS_SB(sb);
241 struct adfs_discmap *dm;
242 unsigned int total = 0;
246 zone = asb->s_map_size;
249 total += scan_free_map(asb, dm++);
250 } while (--zone > 0);
252 return signed_asl(total, asb->s_map2blk);
256 adfs_map_lookup(struct super_block *sb, unsigned int frag_id,
259 struct adfs_sb_info *asb = ADFS_SB(sb);
260 unsigned int zone, mapoff;
264 * map & root fragment is special - it starts in the center of the
265 * disk. The other fragments start at zone (frag / ids_per_zone)
267 if (frag_id == ADFS_ROOT_FRAG)
268 zone = asb->s_map_size >> 1;
270 zone = frag_id / asb->s_ids_per_zone;
272 if (zone >= asb->s_map_size)
275 /* Convert sector offset to map offset */
276 mapoff = signed_asl(offset, -asb->s_map2blk);
278 read_lock(&adfs_map_lock);
279 result = scan_map(asb, zone, frag_id, mapoff);
280 read_unlock(&adfs_map_lock);
285 /* Calculate sector offset into map block */
286 secoff = offset - signed_asl(mapoff, asb->s_map2blk);
287 return secoff + signed_asl(result, asb->s_map2blk);
290 adfs_error(sb, "fragment 0x%04x at offset %d not found in map",
295 adfs_error(sb, "invalid fragment 0x%04x (zone = %d, max = %d)",
296 frag_id, zone, asb->s_map_size);