Linux-2.3.3 and a short hiatus..
[davej-history.git] / fs / hpfs / alloc.c
blob23544fc05da0baca147a273b73d9af1ecd39a9da
1 /*
2 * linux/fs/hpfs/alloc.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * HPFS bitmap operations
7 */
9 #include "hpfs_fn.h"
12 * Check if a sector is allocated in bitmap
13 * This is really slow. Turned on only if chk==2
16 static int chk_if_allocated(struct super_block *s, secno sec, char *msg)
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;
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;
33 hpfs_brelse4(&qbh);
35 return 0;
36 fail1:
37 hpfs_brelse4(&qbh);
38 fail:
39 return 1;
43 * Check if sector(s) have proper number and additionally check if they're
44 * allocated in bitmap.
47 int hpfs_chk_sectors(struct super_block *s, secno start, int len, char *msg)
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;
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;
59 return 0;
62 static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward)
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;
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;
82 /*if (!tstbits(bmp, nr + n, n + forward)) {
83 ret = bs + nr;
84 goto rt;
86 if (!tstbits(bmp, nr + 2*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;
99 } else if (q > nr) break;
101 if (!a) {
102 ret = bs + q;
103 goto rt;
105 nr >>= 5;
106 for (i = nr + 1; i != nr; i++, i &= 0x1ff) {
107 if (!bmp[i]) continue;
108 if (n + forward >= 0x3f && bmp[i] != -1) continue;
109 q = i<<5;
110 if (i > 0) {
111 unsigned k = bmp[i-1];
112 while (k & 0x80000000) {
113 q--; k <<= 1;
116 if (n != 1) q = ((q-1)&~(n-1))+n;
117 while ((a = tstbits(bmp, q, n + forward))) {
118 q += a;
119 if (n != 1) q = ((q-1)&~(n-1))+n;
120 if (q>>5 > i) break;
122 if (!a) {
123 ret = bs + q;
124 goto rt;
128 if (ret) {
129 if (s->s_hpfs_chk && ((ret >> 14) != (bs >> 14) || (bmp[(ret & 0x3fff) >> 5] | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) {
130 hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret);
131 ret = 0;
132 goto b;
134 bmp[(ret & 0x3fff) >> 5] &= ~(((1 << n) - 1) << (ret & 0x1f));
135 hpfs_mark_4buffers_dirty(&qbh);
138 hpfs_brelse4(&qbh);
139 uls:
140 unlock_super(s);
141 return ret;
145 * Allocation strategy: 1) search place near the sector specified
146 * 2) search bitmap where free sectors last found
147 * 3) search all bitmaps
148 * 4) search all bitmaps ignoring number of pre-allocated
149 * sectors
152 secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward, int lock)
154 secno sec;
155 unsigned i;
156 unsigned n_bmps;
157 int b = s->s_hpfs_c_bitmap;
158 int f_p = 0;
159 if (forward < 0) {
160 forward = -forward;
161 f_p = 1;
163 if (lock) hpfs_lock_creation(s);
164 if (near && near < s->s_hpfs_fs_size)
165 if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
166 if (b != -1) {
167 if (b < 0x10000000) if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) goto ret;
168 else if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
170 n_bmps = (s->s_hpfs_fs_size + 0x4000 - 1) >> 14;
171 for (i = 0; i < n_bmps / 2; i++) {
172 if ((sec = alloc_in_bmp(s, (n_bmps/2+i) << 14, n, forward))) {
173 s->s_hpfs_c_bitmap = n_bmps/2+i;
174 goto ret;
176 if ((sec = alloc_in_bmp(s, (n_bmps/2-i-1) << 14, n, forward))) {
177 s->s_hpfs_c_bitmap = n_bmps/2-i-1;
178 goto ret;
181 if ((sec = alloc_in_bmp(s, (n_bmps-1) << 14, n, forward))) {
182 s->s_hpfs_c_bitmap = n_bmps-1;
183 goto ret;
185 if (!f_p) {
186 for (i = 0; i < n_bmps; i++)
187 if ((sec = alloc_in_bmp(s, i << 14, n, 0))) {
188 s->s_hpfs_c_bitmap = 0x10000000 + i;
189 goto ret;
192 sec = 0;
193 ret:
194 if (sec && f_p) {
195 for (i = 0; i < forward; i++) {
196 if (!hpfs_alloc_if_possible_nolock(s, sec + i + 1)) {
197 hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
198 sec = 0;
199 break;
203 if (lock) hpfs_unlock_creation(s);
204 return sec;
207 static secno alloc_in_dirband(struct super_block *s, secno near, int lock)
209 unsigned nr = near;
210 secno sec;
211 if (nr < s->s_hpfs_dirband_start)
212 nr = s->s_hpfs_dirband_start;
213 if (nr >= s->s_hpfs_dirband_start + s->s_hpfs_dirband_size)
214 nr = s->s_hpfs_dirband_start + s->s_hpfs_dirband_size - 4;
215 nr -= s->s_hpfs_dirband_start;
216 nr >>= 2;
217 if (lock) hpfs_lock_creation(s);
218 sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
219 if (lock) hpfs_unlock_creation(s);
220 if (!sec) return 0;
221 return ((sec & 0x3fff) << 2) + s->s_hpfs_dirband_start;
224 /* Alloc sector if it's free */
226 int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec)
228 struct quad_buffer_head qbh;
229 unsigned *bmp;
230 lock_super(s);
231 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
232 if (bmp[(sec & 0x3fff) >> 5] & (1 << (sec & 0x1f))) {
233 bmp[(sec & 0x3fff) >> 5] &= ~(1 << (sec & 0x1f));
234 hpfs_mark_4buffers_dirty(&qbh);
235 hpfs_brelse4(&qbh);
236 unlock_super(s);
237 return 1;
239 hpfs_brelse4(&qbh);
240 end:
241 unlock_super(s);
242 return 0;
245 int hpfs_alloc_if_possible(struct super_block *s, secno sec)
247 int r;
248 hpfs_lock_creation(s);
249 r = hpfs_alloc_if_possible_nolock(s, sec);
250 hpfs_unlock_creation(s);
251 return r;
254 /* Free sectors in bitmaps */
256 void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
258 struct quad_buffer_head qbh;
259 unsigned *bmp;
260 /*printk("2 - ");*/
261 if (!n) return;
262 if (sec < 0x12) {
263 hpfs_error(s, "Trying to free reserved sector %08x", sec);
264 return;
266 lock_super(s);
267 new_map:
268 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
269 unlock_super(s);
270 return;
272 new_tst:
273 if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f) & 1)) {
274 hpfs_error(s, "sector %08x not allocated", sec);
275 hpfs_brelse4(&qbh);
276 unlock_super(s);
277 return;
279 bmp[(sec & 0x3fff) >> 5] |= 1 << (sec & 0x1f);
280 if (!--n) {
281 hpfs_mark_4buffers_dirty(&qbh);
282 hpfs_brelse4(&qbh);
283 unlock_super(s);
284 return;
286 if (!(++sec & 0x3fff)) {
287 hpfs_mark_4buffers_dirty(&qbh);
288 hpfs_brelse4(&qbh);
289 goto new_map;
291 goto new_tst;
295 * Check if there are at least n free dnodes on the filesystem.
296 * Called before adding to dnode. If we run out of space while
297 * splitting dnodes, it would corrupt dnode tree.
300 int hpfs_check_free_dnodes(struct super_block *s, int n)
302 int n_bmps = (s->s_hpfs_fs_size + 0x4000 - 1) >> 14;
303 int b = s->s_hpfs_c_bitmap & 0x0fffffff;
304 int i, j;
305 unsigned *bmp;
306 struct quad_buffer_head qbh;
307 if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
308 for (j = 0; j < 512; j++) {
309 unsigned k;
310 if (!bmp[j]) continue;
311 for (k = bmp[j]; k; k >>= 1) if (k & 1) if (!--n) {
312 hpfs_brelse4(&qbh);
313 return 0;
317 hpfs_brelse4(&qbh);
318 i = 0;
319 if (s->s_hpfs_c_bitmap != -1 ) {
320 bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
321 goto chk_bmp;
323 chk_next:
324 if (i == b) i++;
325 if (i >= n_bmps) return 1;
326 bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
327 chk_bmp:
328 if (bmp) {
329 for (j = 0; j < 512; j++) {
330 unsigned k;
331 if (!bmp[j]) continue;
332 for (k = 0xf; k; k <<= 4)
333 if ((bmp[j] & k) == k) {
334 if (!--n) {
335 hpfs_brelse4(&qbh);
336 return 0;
340 hpfs_brelse4(&qbh);
342 i++;
343 goto chk_next;
346 void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
348 if (s->s_hpfs_chk) if (dno & 3) {
349 hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
350 return;
352 if (dno < s->s_hpfs_dirband_start ||
353 dno >= s->s_hpfs_dirband_start + s->s_hpfs_dirband_size) {
354 hpfs_free_sectors(s, dno, 4);
355 } else {
356 struct quad_buffer_head qbh;
357 unsigned *bmp;
358 unsigned ssec = (dno - s->s_hpfs_dirband_start) / 4;
359 lock_super(s);
360 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
361 unlock_super(s);
362 return;
364 bmp[ssec >> 5] |= 1 << (ssec & 0x1f);
365 hpfs_mark_4buffers_dirty(&qbh);
366 hpfs_brelse4(&qbh);
367 unlock_super(s);
371 struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
372 dnode_secno *dno, struct quad_buffer_head *qbh,
373 int lock)
375 struct dnode *d;
376 if (hpfs_count_one_bitmap(s, s->s_hpfs_dmap) > FREE_DNODES_ADD) {
377 if (!(*dno = alloc_in_dirband(s, near, lock)))
378 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock))) return NULL;
379 } else {
380 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock)))
381 if (!(*dno = alloc_in_dirband(s, near, lock))) return NULL;
383 if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
384 hpfs_free_dnode(s, *dno);
385 return NULL;
387 memset(d, 0, 2048);
388 d->magic = DNODE_MAGIC;
389 d->first_free = 52;
390 d->dirent[0] = 32;
391 d->dirent[2] = 8;
392 d->dirent[30] = 1;
393 d->dirent[31] = 255;
394 d->self = *dno;
395 return d;
398 struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
399 struct buffer_head **bh)
401 struct fnode *f;
402 if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD, 1))) return NULL;
403 if (!(f = hpfs_get_sector(s, *fno, bh))) {
404 hpfs_free_sectors(s, *fno, 1);
405 return NULL;
407 memset(f, 0, 512);
408 f->magic = FNODE_MAGIC;
409 f->ea_offs = 0xc4;
410 f->btree.n_free_nodes = 8;
411 f->btree.first_free = 8;
412 return f;
415 struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
416 struct buffer_head **bh)
418 struct anode *a;
419 if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD, 1))) return NULL;
420 if (!(a = hpfs_get_sector(s, *ano, bh))) {
421 hpfs_free_sectors(s, *ano, 1);
422 return NULL;
424 memset(a, 0, 512);
425 a->magic = ANODE_MAGIC;
426 a->self = *ano;
427 a->btree.n_free_nodes = 40;
428 a->btree.n_used_nodes = 0;
429 a->btree.first_free = 8;
430 return a;