1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1997-2002 Russell King
7 #include <asm/unaligned.h>
11 * The ADFS map is basically a set of sectors. Each sector is called a
12 * zone which contains a bitstream made up of variable sized fragments.
13 * Each bit refers to a set of bytes in the filesystem, defined by
14 * log2bpmb. This may be larger or smaller than the sector size, but
15 * the overall size it describes will always be a round number of
16 * sectors. A fragment id is always idlen bits long.
19 * +---------+-------//---------+---+
20 * | frag id | 0000....000000 | 1 |
21 * +---------+-------//---------+---+
23 * The physical disk space used by a fragment is taken from the start of
24 * the fragment id up to and including the '1' bit - ie, idlen + n + 1
27 * A fragment id can be repeated multiple times in the whole map for
28 * large or fragmented files. The first map zone a fragment starts in
29 * is given by fragment id / ids_per_zone - this allows objects to start
30 * from any zone on the disk.
32 * Free space is described by a linked list of fragments. Each free
33 * fragment describes free space in the same way as the other fragments,
34 * however, the frag id specifies an offset (in map bits) from the end
35 * of this fragment to the start of the next free fragment.
37 * Objects stored on the disk are allocated object ids (we use these as
38 * our inode numbers.) Object ids contain a fragment id and an optional
39 * offset. This allows a directory fragment to contain small files
40 * associated with that directory.
46 static DEFINE_RWLOCK(adfs_map_lock
);
49 * This is fun. We need to load up to 19 bits from the map at an
50 * arbitrary bit alignment. (We're limited to 19 bits by F+ version 2).
52 #define GET_FRAG_ID(_map,_start,_idmask) \
54 unsigned char *_m = _map + (_start >> 3); \
55 u32 _frag = get_unaligned_le32(_m); \
56 _frag >>= (_start & 7); \
61 * return the map bit offset of the fragment frag_id in the zone dm.
62 * Note that the loop is optimised for best asm code - look at the
64 * gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
66 static int lookup_zone(const struct adfs_discmap
*dm
, const unsigned int idlen
,
67 const u32 frag_id
, unsigned int *offset
)
69 const unsigned int mapsize
= dm
->dm_endbit
;
70 const u32 idmask
= (1 << idlen
) - 1;
71 unsigned char *map
= dm
->dm_bh
->b_data
+ 4;
72 unsigned int start
= dm
->dm_startbit
;
77 frag
= GET_FRAG_ID(map
, start
, idmask
);
78 mapptr
= start
+ idlen
;
81 * find end of fragment
84 __le32
*_map
= (__le32
*)map
;
85 u32 v
= le32_to_cpu(_map
[mapptr
>> 5]) >> (mapptr
& 31);
87 mapptr
= (mapptr
& ~31) + 32;
88 if (mapptr
>= mapsize
)
90 v
= le32_to_cpu(_map
[mapptr
>> 5]);
93 mapptr
+= 1 + ffz(~v
);
100 } while (mapptr
< mapsize
);
104 printk(KERN_ERR
"adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
105 frag
, start
, mapptr
);
110 int length
= mapptr
- start
;
111 if (*offset
>= length
) {
116 return start
+ *offset
;
120 * Scan the free space map, for this zone, calculating the total
121 * number of map bits in each free space fragment.
123 * Note: idmask is limited to 15 bits [3.2]
126 scan_free_map(struct adfs_sb_info
*asb
, struct adfs_discmap
*dm
)
128 const unsigned int mapsize
= dm
->dm_endbit
+ 32;
129 const unsigned int idlen
= asb
->s_idlen
;
130 const unsigned int frag_idlen
= idlen
<= 15 ? idlen
: 15;
131 const u32 idmask
= (1 << frag_idlen
) - 1;
132 unsigned char *map
= dm
->dm_bh
->b_data
;
133 unsigned int start
= 8, mapptr
;
135 unsigned long total
= 0;
140 frag
= GET_FRAG_ID(map
, start
, idmask
);
143 * If the freelink is null, then no free fragments
144 * exist in this zone.
155 frag
= GET_FRAG_ID(map
, start
, idmask
);
156 mapptr
= start
+ idlen
;
159 * find end of fragment
162 __le32
*_map
= (__le32
*)map
;
163 u32 v
= le32_to_cpu(_map
[mapptr
>> 5]) >> (mapptr
& 31);
165 mapptr
= (mapptr
& ~31) + 32;
166 if (mapptr
>= mapsize
)
168 v
= le32_to_cpu(_map
[mapptr
>> 5]);
171 mapptr
+= 1 + ffz(~v
);
174 total
+= mapptr
- start
;
175 } while (frag
>= idlen
+ 1);
178 printk(KERN_ERR
"adfs: undersized free fragment\n");
182 printk(KERN_ERR
"adfs: oversized free fragment\n");
186 static int scan_map(struct adfs_sb_info
*asb
, unsigned int zone
,
187 const u32 frag_id
, unsigned int mapoff
)
189 const unsigned int idlen
= asb
->s_idlen
;
190 struct adfs_discmap
*dm
, *dm_end
;
193 dm
= asb
->s_map
+ zone
;
194 zone
= asb
->s_map_size
;
195 dm_end
= asb
->s_map
+ zone
;
198 result
= lookup_zone(dm
, idlen
, frag_id
, &mapoff
);
206 } while (--zone
> 0);
210 result
-= dm
->dm_startbit
;
211 result
+= dm
->dm_startblk
;
217 * calculate the amount of free blocks in the map.
220 * total_free = E(free_in_zone_n)
224 adfs_map_free(struct super_block
*sb
)
226 struct adfs_sb_info
*asb
= ADFS_SB(sb
);
227 struct adfs_discmap
*dm
;
228 unsigned int total
= 0;
232 zone
= asb
->s_map_size
;
235 total
+= scan_free_map(asb
, dm
++);
236 } while (--zone
> 0);
238 return signed_asl(total
, asb
->s_map2blk
);
241 int adfs_map_lookup(struct super_block
*sb
, u32 frag_id
, unsigned int offset
)
243 struct adfs_sb_info
*asb
= ADFS_SB(sb
);
244 unsigned int zone
, mapoff
;
248 * map & root fragment is special - it starts in the center of the
249 * disk. The other fragments start at zone (frag / ids_per_zone)
251 if (frag_id
== ADFS_ROOT_FRAG
)
252 zone
= asb
->s_map_size
>> 1;
254 zone
= frag_id
/ asb
->s_ids_per_zone
;
256 if (zone
>= asb
->s_map_size
)
259 /* Convert sector offset to map offset */
260 mapoff
= signed_asl(offset
, -asb
->s_map2blk
);
262 read_lock(&adfs_map_lock
);
263 result
= scan_map(asb
, zone
, frag_id
, mapoff
);
264 read_unlock(&adfs_map_lock
);
269 /* Calculate sector offset into map block */
270 secoff
= offset
- signed_asl(mapoff
, asb
->s_map2blk
);
271 return secoff
+ signed_asl(result
, asb
->s_map2blk
);
274 adfs_error(sb
, "fragment 0x%04x at offset %d not found in map",
279 adfs_error(sb
, "invalid fragment 0x%04x (zone = %d, max = %d)",
280 frag_id
, zone
, asb
->s_map_size
);