1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1997-2002 Russell King
7 #include <linux/buffer_head.h>
8 #include <asm/unaligned.h>
12 * The ADFS map is basically a set of sectors. Each sector is called a
13 * zone which contains a bitstream made up of variable sized fragments.
14 * Each bit refers to a set of bytes in the filesystem, defined by
15 * log2bpmb. This may be larger or smaller than the sector size, but
16 * the overall size it describes will always be a round number of
17 * sectors. A fragment id is always idlen bits long.
20 * +---------+-------//---------+---+
21 * | frag id | 0000....000000 | 1 |
22 * +---------+-------//---------+---+
24 * The physical disk space used by a fragment is taken from the start of
25 * the fragment id up to and including the '1' bit - ie, idlen + n + 1
28 * A fragment id can be repeated multiple times in the whole map for
29 * large or fragmented files. The first map zone a fragment starts in
30 * is given by fragment id / ids_per_zone - this allows objects to start
31 * from any zone on the disk.
33 * Free space is described by a linked list of fragments. Each free
34 * fragment describes free space in the same way as the other fragments,
35 * however, the frag id specifies an offset (in map bits) from the end
36 * of this fragment to the start of the next free fragment.
38 * Objects stored on the disk are allocated object ids (we use these as
39 * our inode numbers.) Object ids contain a fragment id and an optional
40 * offset. This allows a directory fragment to contain small files
41 * associated with that directory.
47 static DEFINE_RWLOCK(adfs_map_lock);
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).
53 #define GET_FRAG_ID(_map,_start,_idmask) \
55 unsigned char *_m = _map + (_start >> 3); \
56 u32 _frag = get_unaligned_le32(_m); \
57 _frag >>= (_start & 7); \
62 * return the map bit offset of the fragment frag_id in the zone dm.
63 * Note that the loop is optimised for best asm code - look at the
65 * gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
68 lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
69 const unsigned int frag_id, unsigned int *offset)
71 const unsigned int mapsize = dm->dm_endbit;
72 const u32 idmask = (1 << idlen) - 1;
73 unsigned char *map = dm->dm_bh->b_data + 4;
74 unsigned int start = dm->dm_startbit;
79 frag = GET_FRAG_ID(map, start, idmask);
80 mapptr = start + idlen;
83 * find end of fragment
86 __le32 *_map = (__le32 *)map;
87 u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
89 mapptr = (mapptr & ~31) + 32;
90 if (mapptr >= mapsize)
92 v = le32_to_cpu(_map[mapptr >> 5]);
95 mapptr += 1 + ffz(~v);
102 } while (mapptr < mapsize);
106 printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
107 frag, start, mapptr);
112 int length = mapptr - start;
113 if (*offset >= length) {
118 return start + *offset;
122 * Scan the free space map, for this zone, calculating the total
123 * number of map bits in each free space fragment.
125 * Note: idmask is limited to 15 bits [3.2]
128 scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
130 const unsigned int mapsize = dm->dm_endbit + 32;
131 const unsigned int idlen = asb->s_idlen;
132 const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
133 const u32 idmask = (1 << frag_idlen) - 1;
134 unsigned char *map = dm->dm_bh->b_data;
135 unsigned int start = 8, mapptr;
137 unsigned long total = 0;
142 frag = GET_FRAG_ID(map, start, idmask);
145 * If the freelink is null, then no free fragments
146 * exist in this zone.
157 frag = GET_FRAG_ID(map, start, idmask);
158 mapptr = start + idlen;
161 * find end of fragment
164 __le32 *_map = (__le32 *)map;
165 u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
167 mapptr = (mapptr & ~31) + 32;
168 if (mapptr >= mapsize)
170 v = le32_to_cpu(_map[mapptr >> 5]);
173 mapptr += 1 + ffz(~v);
176 total += mapptr - start;
177 } while (frag >= idlen + 1);
180 printk(KERN_ERR "adfs: undersized free fragment\n");
184 printk(KERN_ERR "adfs: oversized free fragment\n");
189 scan_map(struct adfs_sb_info *asb, unsigned int zone,
190 const unsigned int frag_id, unsigned int mapoff)
192 const unsigned int idlen = asb->s_idlen;
193 struct adfs_discmap *dm, *dm_end;
196 dm = asb->s_map + zone;
197 zone = asb->s_map_size;
198 dm_end = asb->s_map + zone;
201 result = lookup_zone(dm, idlen, frag_id, &mapoff);
209 } while (--zone > 0);
213 result -= dm->dm_startbit;
214 result += dm->dm_startblk;
220 * calculate the amount of free blocks in the map.
223 * total_free = E(free_in_zone_n)
227 adfs_map_free(struct super_block *sb)
229 struct adfs_sb_info *asb = ADFS_SB(sb);
230 struct adfs_discmap *dm;
231 unsigned int total = 0;
235 zone = asb->s_map_size;
238 total += scan_free_map(asb, dm++);
239 } while (--zone > 0);
241 return signed_asl(total, asb->s_map2blk);
245 adfs_map_lookup(struct super_block *sb, unsigned int frag_id,
248 struct adfs_sb_info *asb = ADFS_SB(sb);
249 unsigned int zone, mapoff;
253 * map & root fragment is special - it starts in the center of the
254 * disk. The other fragments start at zone (frag / ids_per_zone)
256 if (frag_id == ADFS_ROOT_FRAG)
257 zone = asb->s_map_size >> 1;
259 zone = frag_id / asb->s_ids_per_zone;
261 if (zone >= asb->s_map_size)
264 /* Convert sector offset to map offset */
265 mapoff = signed_asl(offset, -asb->s_map2blk);
267 read_lock(&adfs_map_lock);
268 result = scan_map(asb, zone, frag_id, mapoff);
269 read_unlock(&adfs_map_lock);
274 /* Calculate sector offset into map block */
275 secoff = offset - signed_asl(mapoff, asb->s_map2blk);
276 return secoff + signed_asl(result, asb->s_map2blk);
279 adfs_error(sb, "fragment 0x%04x at offset %d not found in map",
284 adfs_error(sb, "invalid fragment 0x%04x (zone = %d, max = %d)",
285 frag_id, zone, asb->s_map_size);