blob: 82e1bf101fe68bb342384b72639edf423d9b661b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/adfs/map.c
4 *
5 * Copyright (C) 1997-2002 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Russell Kingf75d3982019-12-09 11:08:28 +00007#include <linux/slab.h>
Russell Kinge6160e42019-12-09 11:08:34 +00008#include <linux/statfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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 */
48static DEFINE_RWLOCK(adfs_map_lock);
49
50/*
51 * This is fun. We need to load up to 19 bits from the map at an
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052 * arbitrary bit alignment. (We're limited to 19 bits by F+ version 2).
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54#define GET_FRAG_ID(_map,_start,_idmask) \
55 ({ \
56 unsigned char *_m = _map + (_start >> 3); \
Al Viro224c8862009-06-08 00:46:40 -040057 u32 _frag = get_unaligned_le32(_m); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 _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 */
Russell King5ed70bb2019-06-04 14:49:57 +010068static int lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
69 const u32 frag_id, unsigned int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Russell King197ba3c2019-12-09 11:08:49 +000071 const unsigned int endbit = dm->dm_endbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 const u32 idmask = (1 << idlen) - 1;
Russell King197ba3c2019-12-09 11:08:49 +000073 unsigned char *map = dm->dm_bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 unsigned int start = dm->dm_startbit;
Russell King792314f2019-12-09 11:08:54 +000075 unsigned int fragend;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 u32 frag;
77
78 do {
79 frag = GET_FRAG_ID(map, start, idmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Russell King792314f2019-12-09 11:08:54 +000081 fragend = find_next_bit_le(map, endbit, start + idlen);
82 if (fragend >= endbit)
83 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Russell King792314f2019-12-09 11:08:54 +000085 if (frag == frag_id) {
86 unsigned int length = fragend + 1 - start;
87
88 if (*offset < length)
89 return start + *offset;
90 *offset -= length;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
Russell King792314f2019-12-09 11:08:54 +000093 start = fragend + 1;
94 } while (start < endbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -1;
96
97error:
98 printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
Russell King792314f2019-12-09 11:08:54 +000099 frag, start, fragend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
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 */
109static unsigned int
110scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
111{
Russell King197ba3c2019-12-09 11:08:49 +0000112 const unsigned int endbit = dm->dm_endbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 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;
Russell King792314f2019-12-09 11:08:54 +0000117 unsigned int start = 8, fragend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 frag = GET_FRAG_ID(map, start, idmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Russell King792314f2019-12-09 11:08:54 +0000138 fragend = find_next_bit_le(map, endbit, start + idlen);
139 if (fragend >= endbit)
140 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Russell King792314f2019-12-09 11:08:54 +0000142 total += fragend + 1 - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 } while (frag >= idlen + 1);
144
145 if (frag != 0)
146 printk(KERN_ERR "adfs: undersized free fragment\n");
147
148 return total;
149error:
150 printk(KERN_ERR "adfs: oversized free fragment\n");
151 return 0;
152}
153
Russell King5ed70bb2019-06-04 14:49:57 +0100154static int scan_map(struct adfs_sb_info *asb, unsigned int zone,
155 const u32 frag_id, unsigned int mapoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
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;
177found:
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 */
Russell Kinge6160e42019-12-09 11:08:34 +0000191void adfs_map_statfs(struct super_block *sb, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct adfs_sb_info *asb = ADFS_SB(sb);
Russell Kinge6160e42019-12-09 11:08:34 +0000194 struct adfs_discrecord *dr = adfs_map_discrecord(asb->s_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 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
Russell Kinge6160e42019-12-09 11:08:34 +0000206 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Russell King5ed70bb2019-06-04 14:49:57 +0100212int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
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
249bad_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}
Russell Kingf75d3982019-12-09 11:08:28 +0000254
255static 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
279static 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
Russell King6092b6b2019-12-09 11:08:39 +0000300/*
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 */
304static 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;
Russell King197ba3c2019-12-09 11:08:49 +0000314 dm[0].dm_startbit = 32 + ADFS_DR_SIZE_BITS;
315 dm[0].dm_endbit = 32 + zone_size;
Russell King6092b6b2019-12-09 11:08:39 +0000316
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;
Russell King197ba3c2019-12-09 11:08:49 +0000320 dm[zone].dm_startbit = 32;
321 dm[zone].dm_endbit = 32 + zone_size;
Russell King6092b6b2019-12-09 11:08:39 +0000322 }
323
324 size = adfs_disc_size(dr) >> dr->log2bpmb;
325 size -= (nzones - 1) * zone_size - ADFS_DR_SIZE_BITS;
Russell King197ba3c2019-12-09 11:08:49 +0000326 dm[nzones - 1].dm_endbit = 32 + size;
Russell King6092b6b2019-12-09 11:08:39 +0000327}
328
329static 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
343static 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
Russell Kingf75d3982019-12-09 11:08:28 +0000351struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr)
352{
Russell King6092b6b2019-12-09 11:08:39 +0000353 struct adfs_sb_info *asb = ADFS_SB(sb);
Russell Kingf75d3982019-12-09 11:08:28 +0000354 struct adfs_discmap *dm;
355 unsigned int map_addr, zone_size, nzones;
Russell King6092b6b2019-12-09 11:08:39 +0000356 int ret;
Russell Kingf75d3982019-12-09 11:08:28 +0000357
Russell Kingf6f14a02019-12-09 11:08:59 +0000358 nzones = dr->nzones | dr->nzones_high << 8;
Russell Kingf75d3982019-12-09 11:08:28 +0000359 zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
Russell Kingf75d3982019-12-09 11:08:28 +0000360
Russell Kingf6f14a02019-12-09 11:08:59 +0000361 asb->s_idlen = dr->idlen;
362 asb->s_map_size = nzones;
363 asb->s_map2blk = dr->log2bpmb - dr->log2secsize;
364 asb->s_log2sharesize = dr->log2sharesize;
Russell Kingf75d3982019-12-09 11:08:28 +0000365 asb->s_ids_per_zone = zone_size / (asb->s_idlen + 1);
366
Russell Kingf6f14a02019-12-09 11:08:59 +0000367 map_addr = (nzones >> 1) * zone_size -
368 ((nzones > 1) ? ADFS_DR_SIZE_BITS : 0);
369 map_addr = signed_asl(map_addr, asb->s_map2blk);
370
Russell Kingf75d3982019-12-09 11:08:28 +0000371 dm = kmalloc_array(nzones, sizeof(*dm), GFP_KERNEL);
372 if (dm == NULL) {
373 adfs_error(sb, "not enough memory");
374 return ERR_PTR(-ENOMEM);
375 }
376
Russell King6092b6b2019-12-09 11:08:39 +0000377 adfs_map_layout(dm, nzones, dr);
Russell Kingf75d3982019-12-09 11:08:28 +0000378
Russell King6092b6b2019-12-09 11:08:39 +0000379 ret = adfs_map_read(dm, sb, map_addr, nzones);
380 if (ret) {
381 adfs_error(sb, "unable to read map");
382 goto error_free;
Russell Kingf75d3982019-12-09 11:08:28 +0000383 }
384
Russell Kingf75d3982019-12-09 11:08:28 +0000385 if (adfs_checkmap(sb, dm))
386 return dm;
387
388 adfs_error(sb, "map corrupted");
389
390error_free:
Russell King6092b6b2019-12-09 11:08:39 +0000391 adfs_map_relse(dm, nzones);
Russell Kingf75d3982019-12-09 11:08:28 +0000392 kfree(dm);
393 return ERR_PTR(-EIO);
394}
Russell King7b195262019-12-09 11:08:44 +0000395
396void adfs_free_map(struct super_block *sb)
397{
398 struct adfs_sb_info *asb = ADFS_SB(sb);
399
400 adfs_map_relse(asb->s_map, asb->s_map_size);
401 kfree(asb->s_map);
402}