ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / hpfs / alloc.c
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
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 >= hpfs_sb(s)->sb_dirband_start && sec < hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
27                 unsigned ssec = (sec - hpfs_sb(s)->sb_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         
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 > hpfs_sb(s)->sb_fs_size) {
51                 hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start);
52                 return 1;
53         }
54         if (hpfs_sb(s)->sb_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
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, n + forward)) {
83                 ret = bs + nr;
84                 goto rt;
85         }
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;
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                 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;
114                         }
115                 }
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;
121                 }
122                 if (!a) {
123                         ret = bs + q;
124                         goto rt;
125                 }
126         }
127         rt:
128         if (ret) {
129                 if (hpfs_sb(s)->sb_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;
133                 }
134                 bmp[(ret & 0x3fff) >> 5] &= ~(((1 << n) - 1) << (ret & 0x1f));
135                 hpfs_mark_4buffers_dirty(&qbh);
136         }
137         b:
138         hpfs_brelse4(&qbh);
139         uls:
140         unlock_super(s);
141         return ret;
142 }
143
144 /*
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
150  */
151
152 secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward, int lock)
153 {
154         secno sec;
155         unsigned i;
156         unsigned n_bmps;
157         struct hpfs_sb_info *sbi = hpfs_sb(s);
158         int b = sbi->sb_c_bitmap;
159         int f_p = 0;
160         if (forward < 0) {
161                 forward = -forward;
162                 f_p = 1;
163         }
164         if (lock) hpfs_lock_creation(s);
165         if (near && near < sbi->sb_fs_size)
166                 if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
167         if (b != -1) {
168                 if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
169                         b &= 0x0fffffff;
170                         goto ret;
171                 }
172                 if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
173         }       
174         n_bmps = (sbi->sb_fs_size + 0x4000 - 1) >> 14;
175         for (i = 0; i < n_bmps / 2; i++) {
176                 if ((sec = alloc_in_bmp(s, (n_bmps/2+i) << 14, n, forward))) {
177                         sbi->sb_c_bitmap = n_bmps/2+i;
178                         goto ret;
179                 }       
180                 if ((sec = alloc_in_bmp(s, (n_bmps/2-i-1) << 14, n, forward))) {
181                         sbi->sb_c_bitmap = n_bmps/2-i-1;
182                         goto ret;
183                 }
184         }
185         if ((sec = alloc_in_bmp(s, (n_bmps-1) << 14, n, forward))) {
186                 sbi->sb_c_bitmap = n_bmps-1;
187                 goto ret;
188         }
189         if (!f_p) {
190                 for (i = 0; i < n_bmps; i++)
191                         if ((sec = alloc_in_bmp(s, i << 14, n, 0))) {
192                                 sbi->sb_c_bitmap = 0x10000000 + i;
193                                 goto ret;
194                         }
195         }
196         sec = 0;
197         ret:
198         if (sec && f_p) {
199                 for (i = 0; i < forward; i++) {
200                         if (!hpfs_alloc_if_possible_nolock(s, sec + i + 1)) {
201                                 hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
202                                 sec = 0;
203                                 break;
204                         }
205                 }
206         }
207         if (lock) hpfs_unlock_creation(s);
208         return sec;
209 }
210
211 static secno alloc_in_dirband(struct super_block *s, secno near, int lock)
212 {
213         unsigned nr = near;
214         secno sec;
215         struct hpfs_sb_info *sbi = hpfs_sb(s);
216         if (nr < sbi->sb_dirband_start)
217                 nr = sbi->sb_dirband_start;
218         if (nr >= sbi->sb_dirband_start + sbi->sb_dirband_size)
219                 nr = sbi->sb_dirband_start + sbi->sb_dirband_size - 4;
220         nr -= sbi->sb_dirband_start;
221         nr >>= 2;
222         if (lock) hpfs_lock_creation(s);
223         sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
224         if (lock) hpfs_unlock_creation(s);
225         if (!sec) return 0;
226         return ((sec & 0x3fff) << 2) + sbi->sb_dirband_start;
227 }
228
229 /* Alloc sector if it's free */
230
231 int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec)
232 {
233         struct quad_buffer_head qbh;
234         unsigned *bmp;
235         lock_super(s);
236         if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
237         if (bmp[(sec & 0x3fff) >> 5] & (1 << (sec & 0x1f))) {
238                 bmp[(sec & 0x3fff) >> 5] &= ~(1 << (sec & 0x1f));
239                 hpfs_mark_4buffers_dirty(&qbh);
240                 hpfs_brelse4(&qbh);
241                 unlock_super(s);
242                 return 1;
243         }
244         hpfs_brelse4(&qbh);
245         end:
246         unlock_super(s);
247         return 0;
248 }
249
250 int hpfs_alloc_if_possible(struct super_block *s, secno sec)
251 {
252         int r;
253         hpfs_lock_creation(s);
254         r = hpfs_alloc_if_possible_nolock(s, sec);
255         hpfs_unlock_creation(s);
256         return r;
257 }
258
259 /* Free sectors in bitmaps */
260
261 void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
262 {
263         struct quad_buffer_head qbh;
264         unsigned *bmp;
265         /*printk("2 - ");*/
266         if (!n) return;
267         if (sec < 0x12) {
268                 hpfs_error(s, "Trying to free reserved sector %08x", sec);
269                 return;
270         }
271         lock_super(s);
272         new_map:
273         if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
274                 unlock_super(s);
275                 return;
276         }       
277         new_tst:
278         if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f) & 1)) {
279                 hpfs_error(s, "sector %08x not allocated", sec);
280                 hpfs_brelse4(&qbh);
281                 unlock_super(s);
282                 return;
283         }
284         bmp[(sec & 0x3fff) >> 5] |= 1 << (sec & 0x1f);
285         if (!--n) {
286                 hpfs_mark_4buffers_dirty(&qbh);
287                 hpfs_brelse4(&qbh);
288                 unlock_super(s);
289                 return;
290         }       
291         if (!(++sec & 0x3fff)) {
292                 hpfs_mark_4buffers_dirty(&qbh);
293                 hpfs_brelse4(&qbh);
294                 goto new_map;
295         }
296         goto new_tst;
297 }
298
299 /*
300  * Check if there are at least n free dnodes on the filesystem.
301  * Called before adding to dnode. If we run out of space while
302  * splitting dnodes, it would corrupt dnode tree.
303  */
304
305 int hpfs_check_free_dnodes(struct super_block *s, int n)
306 {
307         int n_bmps = (hpfs_sb(s)->sb_fs_size + 0x4000 - 1) >> 14;
308         int b = hpfs_sb(s)->sb_c_bitmap & 0x0fffffff;
309         int i, j;
310         unsigned *bmp;
311         struct quad_buffer_head qbh;
312         if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
313                 for (j = 0; j < 512; j++) {
314                         unsigned k;
315                         if (!bmp[j]) continue;
316                         for (k = bmp[j]; k; k >>= 1) if (k & 1) if (!--n) {
317                                 hpfs_brelse4(&qbh);
318                                 return 0;
319                         }
320                 }
321         }
322         hpfs_brelse4(&qbh);
323         i = 0;
324         if (hpfs_sb(s)->sb_c_bitmap != -1 ) {
325                 bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
326                 goto chk_bmp;
327         }
328         chk_next:
329         if (i == b) i++;
330         if (i >= n_bmps) return 1;
331         bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
332         chk_bmp:
333         if (bmp) {
334                 for (j = 0; j < 512; j++) {
335                         unsigned k;
336                         if (!bmp[j]) continue;
337                         for (k = 0xf; k; k <<= 4)
338                                 if ((bmp[j] & k) == k) {
339                                         if (!--n) {
340                                                 hpfs_brelse4(&qbh);
341                                                 return 0;
342                                         }
343                                 }
344                 }
345                 hpfs_brelse4(&qbh);
346         }
347         i++;
348         goto chk_next;
349 }
350
351 void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
352 {
353         if (hpfs_sb(s)->sb_chk) if (dno & 3) {
354                 hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
355                 return;
356         }
357         if (dno < hpfs_sb(s)->sb_dirband_start ||
358             dno >= hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
359                 hpfs_free_sectors(s, dno, 4);
360         } else {
361                 struct quad_buffer_head qbh;
362                 unsigned *bmp;
363                 unsigned ssec = (dno - hpfs_sb(s)->sb_dirband_start) / 4;
364                 lock_super(s);
365                 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
366                         unlock_super(s);
367                         return;
368                 }
369                 bmp[ssec >> 5] |= 1 << (ssec & 0x1f);
370                 hpfs_mark_4buffers_dirty(&qbh);
371                 hpfs_brelse4(&qbh);
372                 unlock_super(s);
373         }
374 }
375
376 struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
377                          dnode_secno *dno, struct quad_buffer_head *qbh,
378                          int lock)
379 {
380         struct dnode *d;
381         if (hpfs_count_one_bitmap(s, hpfs_sb(s)->sb_dmap) > FREE_DNODES_ADD) {
382                 if (!(*dno = alloc_in_dirband(s, near, lock)))
383                         if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock))) return NULL;
384         } else {
385                 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock)))
386                         if (!(*dno = alloc_in_dirband(s, near, lock))) return NULL;
387         }
388         if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
389                 hpfs_free_dnode(s, *dno);
390                 return NULL;
391         }
392         memset(d, 0, 2048);
393         d->magic = DNODE_MAGIC;
394         d->first_free = 52;
395         d->dirent[0] = 32;
396         d->dirent[2] = 8;
397         d->dirent[30] = 1;
398         d->dirent[31] = 255;
399         d->self = *dno;
400         return d;
401 }
402
403 struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
404                           struct buffer_head **bh)
405 {
406         struct fnode *f;
407         if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD, 1))) return NULL;
408         if (!(f = hpfs_get_sector(s, *fno, bh))) {
409                 hpfs_free_sectors(s, *fno, 1);
410                 return NULL;
411         }       
412         memset(f, 0, 512);
413         f->magic = FNODE_MAGIC;
414         f->ea_offs = 0xc4;
415         f->btree.n_free_nodes = 8;
416         f->btree.first_free = 8;
417         return f;
418 }
419
420 struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
421                           struct buffer_head **bh)
422 {
423         struct anode *a;
424         if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD, 1))) return NULL;
425         if (!(a = hpfs_get_sector(s, *ano, bh))) {
426                 hpfs_free_sectors(s, *ano, 1);
427                 return NULL;
428         }
429         memset(a, 0, 512);
430         a->magic = ANODE_MAGIC;
431         a->self = *ano;
432         a->btree.n_free_nodes = 40;
433         a->btree.n_used_nodes = 0;
434         a->btree.first_free = 8;
435         return a;
436 }