1 /*
2  *  linux/fs/hpfs/alloc.c
3  *
4  *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5  *
6  *  HPFS bitmap operations
7  */
8 
9 #include "hpfs_fn.h"
10 
11 /*
12  * Check if a sector is allocated in bitmap
13  * This is really slow. Turned on only if chk==2
14  */
15 
chk_if_allocated(struct super_block * s,secno sec,char * msg)16 static int chk_if_allocated(struct super_block *s, secno sec, char *msg)
17 {
18 	struct quad_buffer_head qbh;
19 	unsigned *bmp;
20 	if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "chk"))) goto fail;
21 	if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f)) & 1) {
22 		hpfs_error(s, "sector '%s' - %08x not allocated in bitmap", msg, sec);
23 		goto fail1;
24 	}
25 	hpfs_brelse4(&qbh);
26 	if (sec >= s->s_hpfs_dirband_start && sec < s->s_hpfs_dirband_start + s->s_hpfs_dirband_size) {
27 		unsigned ssec = (sec - s->s_hpfs_dirband_start) / 4;
28 		if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto fail;
29 		if ((bmp[ssec >> 5] >> (ssec & 0x1f)) & 1) {
30 			hpfs_error(s, "sector '%s' - %08x not allocated in directory bitmap", msg, sec);
31 			goto fail1;
32 		}
33 		hpfs_brelse4(&qbh);
34 	}
35 	return 0;
36 	fail1:
37 	hpfs_brelse4(&qbh);
38 	fail:
39 	return 1;
40 }
41 
42 /*
43  * Check if sector(s) have proper number and additionally check if they're
44  * allocated in bitmap.
45  */
46 
hpfs_chk_sectors(struct super_block * s,secno start,int len,char * msg)47 int hpfs_chk_sectors(struct super_block *s, secno start, int len, char *msg)
48 {
49 	if (start + len < start || start < 0x12 ||
50 	    start + len > s->s_hpfs_fs_size) {
51 	    	hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start);
52 		return 1;
53 	}
54 	if (s->s_hpfs_chk>=2) {
55 		int i;
56 		for (i = 0; i < len; i++)
57 			if (chk_if_allocated(s, start + i, msg)) return 1;
58 	}
59 	return 0;
60 }
61 
alloc_in_bmp(struct super_block * s,secno near,unsigned n,unsigned forward)62 static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward)
63 {
64 	struct quad_buffer_head qbh;
65 	unsigned *bmp;
66 	unsigned bs = near & ~0x3fff;
67 	unsigned nr = (near & 0x3fff) & ~(n - 1);
68 	/*unsigned mnr;*/
69 	unsigned i, q;
70 	int a, b;
71 	secno ret = 0;
72 	if (n != 1 && n != 4) {
73 		hpfs_error(s, "Bad allocation size: %d", n);
74 		return 0;
75 	}
76 	lock_super(s);
77 	if (bs != ~0x3fff) {
78 		if (!(bmp = hpfs_map_bitmap(s, near >> 14, &qbh, "aib"))) goto uls;
79 	} else {
80 		if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto uls;
81 	}
82 	if (!tstbits(bmp, nr, n + forward)) {
83 		ret = bs + nr;
84 		goto rt;
85 	}
86 	/*if (!tstbits(bmp, nr + n, n + forward)) {
87 		ret = bs + nr + n;
88 		goto rt;
89 	}*/
90 	q = nr + n; b = 0;
91 	while ((a = tstbits(bmp, q, n + forward))) {
92 		q += a;
93 		if (n != 1) q = ((q-1)&~(n-1))+n;
94 		if (!b) {
95 			if (q>>5 != nr>>5) {
96 				b = 1;
97 				q = nr & 0x1f;
98 			}
99 		} else if (q > nr) break;
100 	}
101 	if (!a) {
102 		ret = bs + q;
103 		goto rt;
104 	}
105 	nr >>= 5;
106 	/*for (i = nr + 1; i != nr; i++, i &= 0x1ff) {*/
107 	i = nr;
108 	do {
109 		if (!bmp[i]) goto cont;
110 		if (n + forward >= 0x3f && bmp[i] != -1) goto cont;
111 		q = i<<5;
112 		if (i > 0) {
113 			unsigned k = bmp[i-1];
114 			while (k & 0x80000000) {
115 				q--; k <<= 1;
116 			}
117 		}
118 		if (n != 1) q = ((q-1)&~(n-1))+n;
119 		while ((a = tstbits(bmp, q, n + forward))) {
120 			q += a;
121 			if (n != 1) q = ((q-1)&~(n-1))+n;
122 			if (q>>5 > i) break;
123 		}
124 		if (!a) {
125 			ret = bs + q;
126 			goto rt;
127 		}
128 		cont:
129 		i++, i &= 0x1ff;
130 	} while (i != nr);
131 	rt:
132 	if (ret) {
133 		if (s->s_hpfs_chk && ((ret >> 14) != (bs >> 14) || (bmp[(ret & 0x3fff) >> 5] | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) {
134 			hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret);
135 			ret = 0;
136 			goto b;
137 		}
138 		bmp[(ret & 0x3fff) >> 5] &= ~(((1 << n) - 1) << (ret & 0x1f));
139 		hpfs_mark_4buffers_dirty(&qbh);
140 	}
141 	b:
142 	hpfs_brelse4(&qbh);
143 	uls:
144 	unlock_super(s);
145 	return ret;
146 }
147 
148 /*
149  * Allocation strategy:	1) search place near the sector specified
150  *			2) search bitmap where free sectors last found
151  *			3) search all bitmaps
152  *			4) search all bitmaps ignoring number of pre-allocated
153  *				sectors
154  */
155 
hpfs_alloc_sector(struct super_block * s,secno near,unsigned n,int forward,int lock)156 secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward, int lock)
157 {
158 	secno sec;
159 	int i;
160 	unsigned n_bmps;
161 	int f_p = 0;
162 	int near_bmp;
163 	if (forward < 0) {
164 		forward = -forward;
165 		f_p = 1;
166 	}
167 	if (lock) hpfs_lock_creation(s);
168 	n_bmps = (s->s_hpfs_fs_size + 0x4000 - 1) >> 14;
169 	if (near && near < s->s_hpfs_fs_size) {
170 		if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
171 		near_bmp = near >> 14;
172 	} else near_bmp = n_bmps / 2;
173 	/*
174 	if (b != -1) {
175 		if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
176 			b &= 0x0fffffff;
177 			goto ret;
178 		}
179 		if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
180 	}
181 	*/
182 	if (!f_p) if (forward > s->s_hpfs_max_fwd_alloc) forward = s->s_hpfs_max_fwd_alloc;
183 	less_fwd:
184 	for (i = 0; i < n_bmps; i++) {
185 		if (near_bmp+i < n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i) << 14, n, forward)))) {
186 			s->s_hpfs_c_bitmap = near_bmp+i;
187 			goto ret;
188 		}
189 		if (!forward) {
190 			if (near_bmp-i-1 >= 0 && ((sec = alloc_in_bmp(s, (near_bmp-i-1) << 14, n, forward)))) {
191 				s->s_hpfs_c_bitmap = near_bmp-i-1;
192 				goto ret;
193 			}
194 		} else {
195 			if (near_bmp+i >= n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i-n_bmps) << 14, n, forward)))) {
196 				s->s_hpfs_c_bitmap = near_bmp+i-n_bmps;
197 				goto ret;
198 			}
199 		}
200 		if (i == 1 && s->s_hpfs_c_bitmap != -1 && ((sec = alloc_in_bmp(s, (s->s_hpfs_c_bitmap) << 14, n, forward)))) {
201 			goto ret;
202 		}
203 	}
204 	if (!f_p) {
205 		if (forward) {
206 			s->s_hpfs_max_fwd_alloc = forward * 3 / 4;
207 			forward /= 2;
208 			goto less_fwd;
209 		}
210 	}
211 	sec = 0;
212 	ret:
213 	if (sec && f_p) {
214 		for (i = 0; i < forward; i++) {
215 			if (!hpfs_alloc_if_possible_nolock(s, sec + i + 1)) {
216 				hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
217 				sec = 0;
218 				break;
219 			}
220 		}
221 	}
222 	if (lock) hpfs_unlock_creation(s);
223 	return sec;
224 }
225 
alloc_in_dirband(struct super_block * s,secno near,int lock)226 static secno alloc_in_dirband(struct super_block *s, secno near, int lock)
227 {
228 	unsigned nr = near;
229 	secno sec;
230 	if (nr < s->s_hpfs_dirband_start)
231 		nr = s->s_hpfs_dirband_start;
232 	if (nr >= s->s_hpfs_dirband_start + s->s_hpfs_dirband_size)
233 		nr = s->s_hpfs_dirband_start + s->s_hpfs_dirband_size - 4;
234 	nr -= s->s_hpfs_dirband_start;
235 	nr >>= 2;
236 	if (lock) hpfs_lock_creation(s);
237 	sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
238 	if (lock) hpfs_unlock_creation(s);
239 	if (!sec) return 0;
240 	return ((sec & 0x3fff) << 2) + s->s_hpfs_dirband_start;
241 }
242 
243 /* Alloc sector if it's free */
244 
hpfs_alloc_if_possible_nolock(struct super_block * s,secno sec)245 int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec)
246 {
247 	struct quad_buffer_head qbh;
248 	unsigned *bmp;
249 	lock_super(s);
250 	if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
251 	if (bmp[(sec & 0x3fff) >> 5] & (1 << (sec & 0x1f))) {
252 		bmp[(sec & 0x3fff) >> 5] &= ~(1 << (sec & 0x1f));
253 		hpfs_mark_4buffers_dirty(&qbh);
254 		hpfs_brelse4(&qbh);
255 		unlock_super(s);
256 		return 1;
257 	}
258 	hpfs_brelse4(&qbh);
259 	end:
260 	unlock_super(s);
261 	return 0;
262 }
263 
hpfs_alloc_if_possible(struct super_block * s,secno sec)264 int hpfs_alloc_if_possible(struct super_block *s, secno sec)
265 {
266 	int r;
267 	hpfs_lock_creation(s);
268 	r = hpfs_alloc_if_possible_nolock(s, sec);
269 	hpfs_unlock_creation(s);
270 	return r;
271 }
272 
273 /* Free sectors in bitmaps */
274 
hpfs_free_sectors(struct super_block * s,secno sec,unsigned n)275 void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
276 {
277 	struct quad_buffer_head qbh;
278 	unsigned *bmp;
279 	/*printk("2 - ");*/
280 	if (!n) return;
281 	if (sec < 0x12) {
282 		hpfs_error(s, "Trying to free reserved sector %08x", sec);
283 		return;
284 	}
285 	lock_super(s);
286 	s->s_hpfs_max_fwd_alloc += n > 0xffff ? 0xffff : n;
287 	if (s->s_hpfs_max_fwd_alloc > 0xffffff) s->s_hpfs_max_fwd_alloc = 0xffffff;
288 	new_map:
289 	if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
290 		unlock_super(s);
291 		return;
292 	}
293 	new_tst:
294 	if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f) & 1)) {
295 		hpfs_error(s, "sector %08x not allocated", sec);
296 		hpfs_brelse4(&qbh);
297 		unlock_super(s);
298 		return;
299 	}
300 	bmp[(sec & 0x3fff) >> 5] |= 1 << (sec & 0x1f);
301 	if (!--n) {
302 		hpfs_mark_4buffers_dirty(&qbh);
303 		hpfs_brelse4(&qbh);
304 		unlock_super(s);
305 		return;
306 	}
307 	if (!(++sec & 0x3fff)) {
308 		hpfs_mark_4buffers_dirty(&qbh);
309 		hpfs_brelse4(&qbh);
310 		goto new_map;
311 	}
312 	goto new_tst;
313 }
314 
315 /*
316  * Check if there are at least n free dnodes on the filesystem.
317  * Called before adding to dnode. If we run out of space while
318  * splitting dnodes, it would corrupt dnode tree.
319  */
320 
hpfs_check_free_dnodes(struct super_block * s,int n)321 int hpfs_check_free_dnodes(struct super_block *s, int n)
322 {
323 	int n_bmps = (s->s_hpfs_fs_size + 0x4000 - 1) >> 14;
324 	int b = s->s_hpfs_c_bitmap & 0x0fffffff;
325 	int i, j;
326 	unsigned *bmp;
327 	struct quad_buffer_head qbh;
328 	if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
329 		for (j = 0; j < 512; j++) {
330 			unsigned k;
331 			if (!bmp[j]) continue;
332 			for (k = bmp[j]; k; k >>= 1) if (k & 1) if (!--n) {
333 				hpfs_brelse4(&qbh);
334 				return 0;
335 			}
336 		}
337 	}
338 	hpfs_brelse4(&qbh);
339 	i = 0;
340 	if (s->s_hpfs_c_bitmap != -1) {
341 		bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
342 		goto chk_bmp;
343 	}
344 	chk_next:
345 	if (i == b) i++;
346 	if (i >= n_bmps) return 1;
347 	bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
348 	chk_bmp:
349 	if (bmp) {
350 		for (j = 0; j < 512; j++) {
351 			unsigned k;
352 			if (!bmp[j]) continue;
353 			for (k = 0xf; k; k <<= 4)
354 				if ((bmp[j] & k) == k) {
355 					if (!--n) {
356 						hpfs_brelse4(&qbh);
357 						return 0;
358 					}
359 				}
360 		}
361 		hpfs_brelse4(&qbh);
362 	}
363 	i++;
364 	goto chk_next;
365 }
366 
hpfs_free_dnode(struct super_block * s,dnode_secno dno)367 void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
368 {
369 	if (s->s_hpfs_chk) if (dno & 3) {
370 		hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
371 		return;
372 	}
373 	if (dno < s->s_hpfs_dirband_start ||
374 	    dno >= s->s_hpfs_dirband_start + s->s_hpfs_dirband_size) {
375 		hpfs_free_sectors(s, dno, 4);
376 	} else {
377 		struct quad_buffer_head qbh;
378 		unsigned *bmp;
379 		unsigned ssec = (dno - s->s_hpfs_dirband_start) / 4;
380 		lock_super(s);
381 		if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
382 			unlock_super(s);
383 			return;
384 		}
385 		bmp[ssec >> 5] |= 1 << (ssec & 0x1f);
386 		hpfs_mark_4buffers_dirty(&qbh);
387 		hpfs_brelse4(&qbh);
388 		unlock_super(s);
389 	}
390 }
391 
hpfs_alloc_dnode(struct super_block * s,secno near,dnode_secno * dno,struct quad_buffer_head * qbh,int lock)392 struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
393 			 dnode_secno *dno, struct quad_buffer_head *qbh,
394 			 int lock)
395 {
396 	struct dnode *d;
397 	if (hpfs_count_one_bitmap(s, s->s_hpfs_dmap) > FREE_DNODES_ADD) {
398 		if (!(*dno = alloc_in_dirband(s, near, lock)))
399 			if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock))) return NULL;
400 	} else {
401 		if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock)))
402 			if (!(*dno = alloc_in_dirband(s, near, lock))) return NULL;
403 	}
404 	if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
405 		hpfs_free_dnode(s, *dno);
406 		return NULL;
407 	}
408 	memset(d, 0, 2048);
409 	d->magic = DNODE_MAGIC;
410 	d->first_free = 52;
411 	d->dirent[0] = 32;
412 	d->dirent[2] = 8;
413 	d->dirent[30] = 1;
414 	d->dirent[31] = 255;
415 	d->self = *dno;
416 	return d;
417 }
418 
hpfs_alloc_fnode(struct super_block * s,secno near,fnode_secno * fno,struct buffer_head ** bh)419 struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
420 			  struct buffer_head **bh)
421 {
422 	struct fnode *f;
423 	if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD, 1))) return NULL;
424 	if (!(f = hpfs_get_sector(s, *fno, bh))) {
425 		hpfs_free_sectors(s, *fno, 1);
426 		return NULL;
427 	}
428 	memset(f, 0, 512);
429 	f->magic = FNODE_MAGIC;
430 	f->ea_offs = 0xc4;
431 	f->btree.n_free_nodes = 8;
432 	f->btree.first_free = 8;
433 	return f;
434 }
435 
hpfs_alloc_anode(struct super_block * s,secno near,anode_secno * ano,struct buffer_head ** bh)436 struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
437 			  struct buffer_head **bh)
438 {
439 	struct anode *a;
440 	if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD, 1))) return NULL;
441 	if (!(a = hpfs_get_sector(s, *ano, bh))) {
442 		hpfs_free_sectors(s, *ano, 1);
443 		return NULL;
444 	}
445 	memset(a, 0, 512);
446 	a->magic = ANODE_MAGIC;
447 	a->self = *ano;
448 	a->btree.n_free_nodes = 40;
449 	a->btree.n_used_nodes = 0;
450 	a->btree.first_free = 8;
451 	return a;
452 }
453