Linux 4.8.3
[linux/fpc-iii.git] / fs / adfs / map.c
blob6935f05202acb502e8ae9554665ad7ae5f03270f
1 /*
2 * linux/fs/adfs/map.c
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.
9 */
10 #include <linux/buffer_head.h>
11 #include <asm/unaligned.h>
12 #include "adfs.h"
15 * The ADFS map is basically a set of sectors. Each sector is called a
16 * zone which contains a bitstream made up of variable sized fragments.
17 * Each bit refers to a set of bytes in the filesystem, defined by
18 * log2bpmb. This may be larger or smaller than the sector size, but
19 * the overall size it describes will always be a round number of
20 * sectors. A fragment id is always idlen bits long.
22 * < idlen > < n > <1>
23 * +---------+-------//---------+---+
24 * | frag id | 0000....000000 | 1 |
25 * +---------+-------//---------+---+
27 * The physical disk space used by a fragment is taken from the start of
28 * the fragment id up to and including the '1' bit - ie, idlen + n + 1
29 * bits.
31 * A fragment id can be repeated multiple times in the whole map for
32 * large or fragmented files. The first map zone a fragment starts in
33 * is given by fragment id / ids_per_zone - this allows objects to start
34 * from any zone on the disk.
36 * Free space is described by a linked list of fragments. Each free
37 * fragment describes free space in the same way as the other fragments,
38 * however, the frag id specifies an offset (in map bits) from the end
39 * of this fragment to the start of the next free fragment.
41 * Objects stored on the disk are allocated object ids (we use these as
42 * our inode numbers.) Object ids contain a fragment id and an optional
43 * offset. This allows a directory fragment to contain small files
44 * associated with that directory.
48 * For the future...
50 static DEFINE_RWLOCK(adfs_map_lock);
53 * This is fun. We need to load up to 19 bits from the map at an
54 * arbitrary bit alignment. (We're limited to 19 bits by F+ version 2).
56 #define GET_FRAG_ID(_map,_start,_idmask) \
57 ({ \
58 unsigned char *_m = _map + (_start >> 3); \
59 u32 _frag = get_unaligned_le32(_m); \
60 _frag >>= (_start & 7); \
61 _frag & _idmask; \
65 * return the map bit offset of the fragment frag_id in the zone dm.
66 * Note that the loop is optimised for best asm code - look at the
67 * output of:
68 * gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
70 static int
71 lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
72 const unsigned int frag_id, unsigned int *offset)
74 const unsigned int mapsize = dm->dm_endbit;
75 const u32 idmask = (1 << idlen) - 1;
76 unsigned char *map = dm->dm_bh->b_data + 4;
77 unsigned int start = dm->dm_startbit;
78 unsigned int mapptr;
79 u32 frag;
81 do {
82 frag = GET_FRAG_ID(map, start, idmask);
83 mapptr = start + idlen;
86 * find end of fragment
89 __le32 *_map = (__le32 *)map;
90 u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
91 while (v == 0) {
92 mapptr = (mapptr & ~31) + 32;
93 if (mapptr >= mapsize)
94 goto error;
95 v = le32_to_cpu(_map[mapptr >> 5]);
98 mapptr += 1 + ffz(~v);
101 if (frag == frag_id)
102 goto found;
103 again:
104 start = mapptr;
105 } while (mapptr < mapsize);
106 return -1;
108 error:
109 printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
110 frag, start, mapptr);
111 return -1;
113 found:
115 int length = mapptr - start;
116 if (*offset >= length) {
117 *offset -= length;
118 goto again;
121 return start + *offset;
125 * Scan the free space map, for this zone, calculating the total
126 * number of map bits in each free space fragment.
128 * Note: idmask is limited to 15 bits [3.2]
130 static unsigned int
131 scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
133 const unsigned int mapsize = dm->dm_endbit + 32;
134 const unsigned int idlen = asb->s_idlen;
135 const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
136 const u32 idmask = (1 << frag_idlen) - 1;
137 unsigned char *map = dm->dm_bh->b_data;
138 unsigned int start = 8, mapptr;
139 u32 frag;
140 unsigned long total = 0;
143 * get fragment id
145 frag = GET_FRAG_ID(map, start, idmask);
148 * If the freelink is null, then no free fragments
149 * exist in this zone.
151 if (frag == 0)
152 return 0;
154 do {
155 start += frag;
158 * get fragment id
160 frag = GET_FRAG_ID(map, start, idmask);
161 mapptr = start + idlen;
164 * find end of fragment
167 __le32 *_map = (__le32 *)map;
168 u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
169 while (v == 0) {
170 mapptr = (mapptr & ~31) + 32;
171 if (mapptr >= mapsize)
172 goto error;
173 v = le32_to_cpu(_map[mapptr >> 5]);
176 mapptr += 1 + ffz(~v);
179 total += mapptr - start;
180 } while (frag >= idlen + 1);
182 if (frag != 0)
183 printk(KERN_ERR "adfs: undersized free fragment\n");
185 return total;
186 error:
187 printk(KERN_ERR "adfs: oversized free fragment\n");
188 return 0;
191 static int
192 scan_map(struct adfs_sb_info *asb, unsigned int zone,
193 const unsigned int frag_id, unsigned int mapoff)
195 const unsigned int idlen = asb->s_idlen;
196 struct adfs_discmap *dm, *dm_end;
197 int result;
199 dm = asb->s_map + zone;
200 zone = asb->s_map_size;
201 dm_end = asb->s_map + zone;
203 do {
204 result = lookup_zone(dm, idlen, frag_id, &mapoff);
206 if (result != -1)
207 goto found;
209 dm ++;
210 if (dm == dm_end)
211 dm = asb->s_map;
212 } while (--zone > 0);
214 return -1;
215 found:
216 result -= dm->dm_startbit;
217 result += dm->dm_startblk;
219 return result;
223 * calculate the amount of free blocks in the map.
225 * n=1
226 * total_free = E(free_in_zone_n)
227 * nzones
229 unsigned int
230 adfs_map_free(struct super_block *sb)
232 struct adfs_sb_info *asb = ADFS_SB(sb);
233 struct adfs_discmap *dm;
234 unsigned int total = 0;
235 unsigned int zone;
237 dm = asb->s_map;
238 zone = asb->s_map_size;
240 do {
241 total += scan_free_map(asb, dm++);
242 } while (--zone > 0);
244 return signed_asl(total, asb->s_map2blk);
248 adfs_map_lookup(struct super_block *sb, unsigned int frag_id,
249 unsigned int offset)
251 struct adfs_sb_info *asb = ADFS_SB(sb);
252 unsigned int zone, mapoff;
253 int result;
256 * map & root fragment is special - it starts in the center of the
257 * disk. The other fragments start at zone (frag / ids_per_zone)
259 if (frag_id == ADFS_ROOT_FRAG)
260 zone = asb->s_map_size >> 1;
261 else
262 zone = frag_id / asb->s_ids_per_zone;
264 if (zone >= asb->s_map_size)
265 goto bad_fragment;
267 /* Convert sector offset to map offset */
268 mapoff = signed_asl(offset, -asb->s_map2blk);
270 read_lock(&adfs_map_lock);
271 result = scan_map(asb, zone, frag_id, mapoff);
272 read_unlock(&adfs_map_lock);
274 if (result > 0) {
275 unsigned int secoff;
277 /* Calculate sector offset into map block */
278 secoff = offset - signed_asl(mapoff, asb->s_map2blk);
279 return secoff + signed_asl(result, asb->s_map2blk);
282 adfs_error(sb, "fragment 0x%04x at offset %d not found in map",
283 frag_id, offset);
284 return 0;
286 bad_fragment:
287 adfs_error(sb, "invalid fragment 0x%04x (zone = %d, max = %d)",
288 frag_id, zone, asb->s_map_size);
289 return 0;