summaryrefslogtreecommitdiffstats
path: root/fs/adfs/map.c
blob: 0c6507411ab9ad408bf3794132a1f960685aef9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 *  linux/fs/adfs/map.c
 *
 * Copyright (C) 1997-1999 Russell King
 */
#include <linux/version.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/adfs_fs.h>

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
#include <linux/spinlock.h>
#else
#include <asm/spinlock.h>
#endif

#include "adfs.h"

/*
 * For the future...
 */
static rwlock_t adfs_map_lock;

/*
 * return the map bit offset of the fragment frag_id in
 * the zone dm.
 * Note that the loop is optimised for best asm code -
 * look at the output of:
 *  gcc -D__KERNEL__ -O2 -I../../include -o - -S map.c
 */
static int
lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
	    const unsigned int frag_id, unsigned int *offset)
{
	const unsigned int mapsize = dm->dm_endbit;
	const unsigned int idmask = (1 << idlen) - 1;
	unsigned long *map = ((unsigned long *)dm->dm_bh->b_data) + 1;
	unsigned int start = dm->dm_startbit;
	unsigned int mapptr;

	do {
		unsigned long frag;

		/*
		 * get fragment id
		 */
		{
			unsigned long v2;
			unsigned int tmp;

			tmp = start >> 5;

			frag = le32_to_cpu(map[tmp]);
			v2   = le32_to_cpu(map[tmp + 1]);

			tmp  = start & 31;

			frag = (frag >> tmp) | (v2 << (32 - tmp));

			frag &= idmask;
		}

		mapptr = start + idlen;

		/*
		 * find end of fragment
		 */
		{
			unsigned long v2;

			while ((v2 = map[mapptr >> 5] >> (mapptr & 31)) == 0) {
				mapptr = (mapptr & ~31) + 32;
				if (mapptr >= mapsize)
					goto error;
			}

			mapptr += 1 + ffz(~v2);
		}

		if (frag == frag_id)
			goto found;
again:
		start = mapptr;
	} while (mapptr < mapsize);

error:
	return -1;

found:
	{
		int length = mapptr - start;
		if (*offset >= length) {
			*offset -= length;
			goto again;
		}
	}
	return start + *offset;
}

/*
 * Scan the free space map, for this zone, calculating the total
 * number of map bits in each free space fragment.
 *
 * Note: idmask is limited to 15 bits [3.2]
 */
static unsigned int
scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
{
	const unsigned int mapsize = dm->dm_endbit + 32;
	const unsigned int idlen  = asb->s_idlen;
	const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
	const unsigned int idmask = (1 << frag_idlen) - 1;
	unsigned long *map = (unsigned long *)dm->dm_bh->b_data;
	unsigned int start = 8, mapptr;
	unsigned long frag;
	unsigned long total = 0;

	/*
	 * get fragment id
	 */
	{
		unsigned long v2;
		unsigned int tmp;

		tmp = start >> 5;

		frag = le32_to_cpu(map[tmp]);
		v2   = le32_to_cpu(map[tmp + 1]);

		tmp  = start & 31;

		frag = (frag >> tmp) | (v2 << (32 - tmp));

		frag &= idmask;
	}

	/*
	 * If the freelink is null, then no free fragments
	 * exist in this zone.
	 */
	if (frag == 0)
		return 0;

	do {
		start += frag;

		/*
		 * get fragment id
		 */
		{
			unsigned long v2;
			unsigned int tmp;

			tmp = start >> 5;

			frag = le32_to_cpu(map[tmp]);
			v2   = le32_to_cpu(map[tmp + 1]);

			tmp  = start & 31;

			frag = (frag >> tmp) | (v2 << (32 - tmp));

			frag &= idmask;
		}

		mapptr = start + idlen;

		/*
		 * find end of fragment
		 */
		{
			unsigned long v2;

			while ((v2 = map[mapptr >> 5] >> (mapptr & 31)) == 0) {
				mapptr = (mapptr & ~31) + 32;
				if (mapptr >= mapsize)
					goto error;
			}

			mapptr += 1 + ffz(~v2);
		}

		total += mapptr - start;
	} while (frag >= idlen + 1);

	if (frag != 0)
		printk(KERN_ERR "adfs: undersized free fragment\n");

	return total;
error:
	printk(KERN_ERR "adfs: oversized free fragment\n");
	return 0;
}

static int
scan_map(struct adfs_sb_info *asb, unsigned int zone,
	 const unsigned int frag_id, unsigned int mapoff)
{
	const unsigned int idlen = asb->s_idlen;
	struct adfs_discmap *dm, *dm_end;
	int result;

	dm	= asb->s_map + zone;
	zone	= asb->s_map_size;
	dm_end	= asb->s_map + zone;

	do {
		result = lookup_zone(dm, idlen, frag_id, &mapoff);

		if (result != -1)
			goto found;

		dm ++;
		if (dm == dm_end)
			dm = asb->s_map;
	} while (--zone > 0);

	return -1;
found:
	result -= dm->dm_startbit;
	result += dm->dm_startblk;

	return result;
}

/*
 * calculate the amount of free blocks in the map.
 *
 *              n=1
 *  total_free = E(free_in_zone_n)
 *              nzones
 */
unsigned int
adfs_map_free(struct super_block *sb)
{
	struct adfs_sb_info *asb = &sb->u.adfs_sb;
	struct adfs_discmap *dm;
	unsigned int total = 0;
	unsigned int zone;

	dm   = asb->s_map;
	zone = asb->s_map_size;

	do {
		total += scan_free_map(asb, dm++);
	} while (--zone > 0);

	return signed_asl(total, asb->s_map2blk);
}

int adfs_map_lookup (struct super_block *sb, int frag_id, int offset)
{
	struct adfs_sb_info *asb = &sb->u.adfs_sb;
	unsigned int zone, mapoff;
	int result;

	/*
	 * map & root fragment is special - it starts in the center of the
	 * disk.  The other fragments start at zone (frag / ids_per_zone)
	 */
	if (frag_id == ADFS_ROOT_FRAG)
		zone = asb->s_map_size >> 1;
	else
		zone = frag_id / asb->s_ids_per_zone;

	if (zone >= asb->s_map_size)
		goto bad_fragment;

	/* Convert sector offset to map offset */
	mapoff = signed_asl(offset, -asb->s_map2blk);

	read_lock(&adfs_map_lock);
	result = scan_map(asb, zone, frag_id, mapoff);
	read_unlock(&adfs_map_lock);

	if (result > 0) {
		unsigned int secoff;

		/* Calculate sector offset into map block */
		secoff = offset - signed_asl(mapoff, asb->s_map2blk);
		return secoff + signed_asl(result, asb->s_map2blk);
	}

	adfs_error(sb, "fragment %04X at offset %d not found in map",
		   frag_id, offset);
	return 0;

bad_fragment:
	adfs_error(sb, "fragment %X is invalid (zone = %d, max = %d)",
		   frag_id, zone, asb->s_map_size);
	return 0;
}