xref: /openbmc/linux/fs/adfs/map.c (revision 792314f8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/adfs/map.c
4  *
5  *  Copyright (C) 1997-2002 Russell King
6  */
7 #include <linux/slab.h>
8 #include <linux/statfs.h>
9 #include <asm/unaligned.h>
10 #include "adfs.h"
11 
12 /*
13  * The ADFS map is basically a set of sectors.  Each sector is called a
14  * zone which contains a bitstream made up of variable sized fragments.
15  * Each bit refers to a set of bytes in the filesystem, defined by
16  * log2bpmb.  This may be larger or smaller than the sector size, but
17  * the overall size it describes will always be a round number of
18  * sectors.  A fragment id is always idlen bits long.
19  *
20  *  < idlen > <       n        > <1>
21  * +---------+-------//---------+---+
22  * | frag id |  0000....000000  | 1 |
23  * +---------+-------//---------+---+
24  *
25  * The physical disk space used by a fragment is taken from the start of
26  * the fragment id up to and including the '1' bit - ie, idlen + n + 1
27  * bits.
28  *
29  * A fragment id can be repeated multiple times in the whole map for
30  * large or fragmented files.  The first map zone a fragment starts in
31  * is given by fragment id / ids_per_zone - this allows objects to start
32  * from any zone on the disk.
33  *
34  * Free space is described by a linked list of fragments.  Each free
35  * fragment describes free space in the same way as the other fragments,
36  * however, the frag id specifies an offset (in map bits) from the end
37  * of this fragment to the start of the next free fragment.
38  *
39  * Objects stored on the disk are allocated object ids (we use these as
40  * our inode numbers.)  Object ids contain a fragment id and an optional
41  * offset.  This allows a directory fragment to contain small files
42  * associated with that directory.
43  */
44 
45 /*
46  * For the future...
47  */
48 static DEFINE_RWLOCK(adfs_map_lock);
49 
50 /*
51  * This is fun.  We need to load up to 19 bits from the map at an
52  * arbitrary bit alignment.  (We're limited to 19 bits by F+ version 2).
53  */
54 #define GET_FRAG_ID(_map,_start,_idmask)				\
55 	({								\
56 		unsigned char *_m = _map + (_start >> 3);		\
57 		u32 _frag = get_unaligned_le32(_m);			\
58 		_frag >>= (_start & 7);					\
59 		_frag & _idmask;					\
60 	})
61 
62 /*
63  * return the map bit offset of the fragment frag_id in the zone dm.
64  * Note that the loop is optimised for best asm code - look at the
65  * output of:
66  *  gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
67  */
68 static int lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
69 		       const u32 frag_id, unsigned int *offset)
70 {
71 	const unsigned int endbit = dm->dm_endbit;
72 	const u32 idmask = (1 << idlen) - 1;
73 	unsigned char *map = dm->dm_bh->b_data;
74 	unsigned int start = dm->dm_startbit;
75 	unsigned int fragend;
76 	u32 frag;
77 
78 	do {
79 		frag = GET_FRAG_ID(map, start, idmask);
80 
81 		fragend = find_next_bit_le(map, endbit, start + idlen);
82 		if (fragend >= endbit)
83 			goto error;
84 
85 		if (frag == frag_id) {
86 			unsigned int length = fragend + 1 - start;
87 
88 			if (*offset < length)
89 				return start + *offset;
90 			*offset -= length;
91 		}
92 
93 		start = fragend + 1;
94 	} while (start < endbit);
95 	return -1;
96 
97 error:
98 	printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
99 		frag, start, fragend);
100 	return -1;
101 }
102 
103 /*
104  * Scan the free space map, for this zone, calculating the total
105  * number of map bits in each free space fragment.
106  *
107  * Note: idmask is limited to 15 bits [3.2]
108  */
109 static unsigned int
110 scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
111 {
112 	const unsigned int endbit = dm->dm_endbit;
113 	const unsigned int idlen  = asb->s_idlen;
114 	const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
115 	const u32 idmask = (1 << frag_idlen) - 1;
116 	unsigned char *map = dm->dm_bh->b_data;
117 	unsigned int start = 8, fragend;
118 	u32 frag;
119 	unsigned long total = 0;
120 
121 	/*
122 	 * get fragment id
123 	 */
124 	frag = GET_FRAG_ID(map, start, idmask);
125 
126 	/*
127 	 * If the freelink is null, then no free fragments
128 	 * exist in this zone.
129 	 */
130 	if (frag == 0)
131 		return 0;
132 
133 	do {
134 		start += frag;
135 
136 		frag = GET_FRAG_ID(map, start, idmask);
137 
138 		fragend = find_next_bit_le(map, endbit, start + idlen);
139 		if (fragend >= endbit)
140 			goto error;
141 
142 		total += fragend + 1 - start;
143 	} while (frag >= idlen + 1);
144 
145 	if (frag != 0)
146 		printk(KERN_ERR "adfs: undersized free fragment\n");
147 
148 	return total;
149 error:
150 	printk(KERN_ERR "adfs: oversized free fragment\n");
151 	return 0;
152 }
153 
154 static int scan_map(struct adfs_sb_info *asb, unsigned int zone,
155 		    const u32 frag_id, unsigned int mapoff)
156 {
157 	const unsigned int idlen = asb->s_idlen;
158 	struct adfs_discmap *dm, *dm_end;
159 	int result;
160 
161 	dm	= asb->s_map + zone;
162 	zone	= asb->s_map_size;
163 	dm_end	= asb->s_map + zone;
164 
165 	do {
166 		result = lookup_zone(dm, idlen, frag_id, &mapoff);
167 
168 		if (result != -1)
169 			goto found;
170 
171 		dm ++;
172 		if (dm == dm_end)
173 			dm = asb->s_map;
174 	} while (--zone > 0);
175 
176 	return -1;
177 found:
178 	result -= dm->dm_startbit;
179 	result += dm->dm_startblk;
180 
181 	return result;
182 }
183 
184 /*
185  * calculate the amount of free blocks in the map.
186  *
187  *              n=1
188  *  total_free = E(free_in_zone_n)
189  *              nzones
190  */
191 void adfs_map_statfs(struct super_block *sb, struct kstatfs *buf)
192 {
193 	struct adfs_sb_info *asb = ADFS_SB(sb);
194 	struct adfs_discrecord *dr = adfs_map_discrecord(asb->s_map);
195 	struct adfs_discmap *dm;
196 	unsigned int total = 0;
197 	unsigned int zone;
198 
199 	dm   = asb->s_map;
200 	zone = asb->s_map_size;
201 
202 	do {
203 		total += scan_free_map(asb, dm++);
204 	} while (--zone > 0);
205 
206 	buf->f_blocks  = adfs_disc_size(dr) >> sb->s_blocksize_bits;
207 	buf->f_files   = asb->s_ids_per_zone * asb->s_map_size;
208 	buf->f_bavail  =
209 	buf->f_bfree   = signed_asl(total, asb->s_map2blk);
210 }
211 
212 int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset)
213 {
214 	struct adfs_sb_info *asb = ADFS_SB(sb);
215 	unsigned int zone, mapoff;
216 	int result;
217 
218 	/*
219 	 * map & root fragment is special - it starts in the center of the
220 	 * disk.  The other fragments start at zone (frag / ids_per_zone)
221 	 */
222 	if (frag_id == ADFS_ROOT_FRAG)
223 		zone = asb->s_map_size >> 1;
224 	else
225 		zone = frag_id / asb->s_ids_per_zone;
226 
227 	if (zone >= asb->s_map_size)
228 		goto bad_fragment;
229 
230 	/* Convert sector offset to map offset */
231 	mapoff = signed_asl(offset, -asb->s_map2blk);
232 
233 	read_lock(&adfs_map_lock);
234 	result = scan_map(asb, zone, frag_id, mapoff);
235 	read_unlock(&adfs_map_lock);
236 
237 	if (result > 0) {
238 		unsigned int secoff;
239 
240 		/* Calculate sector offset into map block */
241 		secoff = offset - signed_asl(mapoff, asb->s_map2blk);
242 		return secoff + signed_asl(result, asb->s_map2blk);
243 	}
244 
245 	adfs_error(sb, "fragment 0x%04x at offset %d not found in map",
246 		   frag_id, offset);
247 	return 0;
248 
249 bad_fragment:
250 	adfs_error(sb, "invalid fragment 0x%04x (zone = %d, max = %d)",
251 		   frag_id, zone, asb->s_map_size);
252 	return 0;
253 }
254 
255 static unsigned char adfs_calczonecheck(struct super_block *sb, unsigned char *map)
256 {
257 	unsigned int v0, v1, v2, v3;
258 	int i;
259 
260 	v0 = v1 = v2 = v3 = 0;
261 	for (i = sb->s_blocksize - 4; i; i -= 4) {
262 		v0 += map[i]     + (v3 >> 8);
263 		v3 &= 0xff;
264 		v1 += map[i + 1] + (v0 >> 8);
265 		v0 &= 0xff;
266 		v2 += map[i + 2] + (v1 >> 8);
267 		v1 &= 0xff;
268 		v3 += map[i + 3] + (v2 >> 8);
269 		v2 &= 0xff;
270 	}
271 	v0 +=           v3 >> 8;
272 	v1 += map[1] + (v0 >> 8);
273 	v2 += map[2] + (v1 >> 8);
274 	v3 += map[3] + (v2 >> 8);
275 
276 	return v0 ^ v1 ^ v2 ^ v3;
277 }
278 
279 static int adfs_checkmap(struct super_block *sb, struct adfs_discmap *dm)
280 {
281 	unsigned char crosscheck = 0, zonecheck = 1;
282 	int i;
283 
284 	for (i = 0; i < ADFS_SB(sb)->s_map_size; i++) {
285 		unsigned char *map;
286 
287 		map = dm[i].dm_bh->b_data;
288 
289 		if (adfs_calczonecheck(sb, map) != map[0]) {
290 			adfs_error(sb, "zone %d fails zonecheck", i);
291 			zonecheck = 0;
292 		}
293 		crosscheck ^= map[3];
294 	}
295 	if (crosscheck != 0xff)
296 		adfs_error(sb, "crosscheck != 0xff");
297 	return crosscheck == 0xff && zonecheck;
298 }
299 
300 /*
301  * Layout the map - the first zone contains a copy of the disc record,
302  * and the last zone must be limited to the size of the filesystem.
303  */
304 static void adfs_map_layout(struct adfs_discmap *dm, unsigned int nzones,
305 			    struct adfs_discrecord *dr)
306 {
307 	unsigned int zone, zone_size;
308 	u64 size;
309 
310 	zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
311 
312 	dm[0].dm_bh       = NULL;
313 	dm[0].dm_startblk = 0;
314 	dm[0].dm_startbit = 32 + ADFS_DR_SIZE_BITS;
315 	dm[0].dm_endbit   = 32 + zone_size;
316 
317 	for (zone = 1; zone < nzones; zone++) {
318 		dm[zone].dm_bh       = NULL;
319 		dm[zone].dm_startblk = zone * zone_size - ADFS_DR_SIZE_BITS;
320 		dm[zone].dm_startbit = 32;
321 		dm[zone].dm_endbit   = 32 + zone_size;
322 	}
323 
324 	size = adfs_disc_size(dr) >> dr->log2bpmb;
325 	size -= (nzones - 1) * zone_size - ADFS_DR_SIZE_BITS;
326 	dm[nzones - 1].dm_endbit = 32 + size;
327 }
328 
329 static int adfs_map_read(struct adfs_discmap *dm, struct super_block *sb,
330 			 unsigned int map_addr, unsigned int nzones)
331 {
332 	unsigned int zone;
333 
334 	for (zone = 0; zone < nzones; zone++) {
335 		dm[zone].dm_bh = sb_bread(sb, map_addr + zone);
336 		if (!dm[zone].dm_bh)
337 			return -EIO;
338 	}
339 
340 	return 0;
341 }
342 
343 static void adfs_map_relse(struct adfs_discmap *dm, unsigned int nzones)
344 {
345 	unsigned int zone;
346 
347 	for (zone = 0; zone < nzones; zone++)
348 		brelse(dm[zone].dm_bh);
349 }
350 
351 struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr)
352 {
353 	struct adfs_sb_info *asb = ADFS_SB(sb);
354 	struct adfs_discmap *dm;
355 	unsigned int map_addr, zone_size, nzones;
356 	int ret;
357 
358 	nzones    = asb->s_map_size;
359 	zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
360 	map_addr  = (nzones >> 1) * zone_size -
361 		     ((nzones > 1) ? ADFS_DR_SIZE_BITS : 0);
362 	map_addr  = signed_asl(map_addr, asb->s_map2blk);
363 
364 	asb->s_ids_per_zone = zone_size / (asb->s_idlen + 1);
365 
366 	dm = kmalloc_array(nzones, sizeof(*dm), GFP_KERNEL);
367 	if (dm == NULL) {
368 		adfs_error(sb, "not enough memory");
369 		return ERR_PTR(-ENOMEM);
370 	}
371 
372 	adfs_map_layout(dm, nzones, dr);
373 
374 	ret = adfs_map_read(dm, sb, map_addr, nzones);
375 	if (ret) {
376 		adfs_error(sb, "unable to read map");
377 		goto error_free;
378 	}
379 
380 	if (adfs_checkmap(sb, dm))
381 		return dm;
382 
383 	adfs_error(sb, "map corrupted");
384 
385 error_free:
386 	adfs_map_relse(dm, nzones);
387 	kfree(dm);
388 	return ERR_PTR(-EIO);
389 }
390 
391 void adfs_free_map(struct super_block *sb)
392 {
393 	struct adfs_sb_info *asb = ADFS_SB(sb);
394 
395 	adfs_map_relse(asb->s_map, asb->s_map_size);
396 	kfree(asb->s_map);
397 }
398