2 * linux/fs/hpfs/alloc.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * HPFS bitmap operations
11 static int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec);
14 * Check if a sector is allocated in bitmap
15 * This is really slow. Turned on only if chk==2
18 static int chk_if_allocated(struct super_block *s, secno sec, char *msg)
20 struct quad_buffer_head qbh;
22 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "chk"))) goto fail;
23 if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f)) & 1) {
24 hpfs_error(s, "sector '%s' - %08x not allocated in bitmap", msg, sec);
28 if (sec >= hpfs_sb(s)->sb_dirband_start && sec < hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
29 unsigned ssec = (sec - hpfs_sb(s)->sb_dirband_start) / 4;
30 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto fail;
31 if ((bmp[ssec >> 5] >> (ssec & 0x1f)) & 1) {
32 hpfs_error(s, "sector '%s' - %08x not allocated in directory bitmap", msg, sec);
45 * Check if sector(s) have proper number and additionally check if they're
46 * allocated in bitmap.
49 int hpfs_chk_sectors(struct super_block *s, secno start, int len, char *msg)
51 if (start + len < start || start < 0x12 ||
52 start + len > hpfs_sb(s)->sb_fs_size) {
53 hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start);
56 if (hpfs_sb(s)->sb_chk>=2) {
58 for (i = 0; i < len; i++)
59 if (chk_if_allocated(s, start + i, msg)) return 1;
64 static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward)
66 struct quad_buffer_head qbh;
68 unsigned bs = near & ~0x3fff;
69 unsigned nr = (near & 0x3fff) & ~(n - 1);
74 if (n != 1 && n != 4) {
75 hpfs_error(s, "Bad allocation size: %d", n);
80 if (!(bmp = hpfs_map_bitmap(s, near >> 14, &qbh, "aib"))) goto uls;
82 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto uls;
84 if (!tstbits(bmp, nr, n + forward)) {
88 /*if (!tstbits(bmp, nr + n, n + forward)) {
93 while ((a = tstbits(bmp, q, n + forward)) != 0) {
95 if (n != 1) q = ((q-1)&~(n-1))+n;
101 } else if (q > nr) break;
108 /*for (i = nr + 1; i != nr; i++, i &= 0x1ff) {*/
111 if (!bmp[i]) goto cont;
112 if (n + forward >= 0x3f && bmp[i] != -1) goto cont;
115 unsigned k = bmp[i-1];
116 while (k & 0x80000000) {
120 if (n != 1) q = ((q-1)&~(n-1))+n;
121 while ((a = tstbits(bmp, q, n + forward)) != 0) {
123 if (n != 1) q = ((q-1)&~(n-1))+n;
135 if (hpfs_sb(s)->sb_chk && ((ret >> 14) != (bs >> 14) || (bmp[(ret & 0x3fff) >> 5] | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) {
136 hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret);
140 bmp[(ret & 0x3fff) >> 5] &= ~(((1 << n) - 1) << (ret & 0x1f));
141 hpfs_mark_4buffers_dirty(&qbh);
151 * Allocation strategy: 1) search place near the sector specified
152 * 2) search bitmap where free sectors last found
153 * 3) search all bitmaps
154 * 4) search all bitmaps ignoring number of pre-allocated
158 secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward, int lock)
163 struct hpfs_sb_info *sbi = hpfs_sb(s);
170 if (lock) hpfs_lock_creation(s);
171 n_bmps = (sbi->sb_fs_size + 0x4000 - 1) >> 14;
172 if (near && near < sbi->sb_fs_size) {
173 if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
174 near_bmp = near >> 14;
175 } else near_bmp = n_bmps / 2;
178 if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
182 if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
184 if (!f_p) if (forward > sbi->sb_max_fwd_alloc) forward = sbi->sb_max_fwd_alloc;
186 for (i = 0; i < n_bmps; i++) {
187 if (near_bmp+i < n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i) << 14, n, forward)))) {
188 sbi->sb_c_bitmap = near_bmp+i;
192 if (near_bmp-i-1 >= 0 && ((sec = alloc_in_bmp(s, (near_bmp-i-1) << 14, n, forward)))) {
193 sbi->sb_c_bitmap = near_bmp-i-1;
197 if (near_bmp+i >= n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i-n_bmps) << 14, n, forward)))) {
198 sbi->sb_c_bitmap = near_bmp+i-n_bmps;
202 if (i == 1 && sbi->sb_c_bitmap != -1 && ((sec = alloc_in_bmp(s, (sbi->sb_c_bitmap) << 14, n, forward)))) {
208 sbi->sb_max_fwd_alloc = forward * 3 / 4;
216 for (i = 0; i < forward; i++) {
217 if (!hpfs_alloc_if_possible_nolock(s, sec + i + 1)) {
218 hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
224 if (lock) hpfs_unlock_creation(s);
228 static secno alloc_in_dirband(struct super_block *s, secno near, int lock)
232 struct hpfs_sb_info *sbi = hpfs_sb(s);
233 if (nr < sbi->sb_dirband_start)
234 nr = sbi->sb_dirband_start;
235 if (nr >= sbi->sb_dirband_start + sbi->sb_dirband_size)
236 nr = sbi->sb_dirband_start + sbi->sb_dirband_size - 4;
237 nr -= sbi->sb_dirband_start;
239 if (lock) hpfs_lock_creation(s);
240 sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
241 if (lock) hpfs_unlock_creation(s);
243 return ((sec & 0x3fff) << 2) + sbi->sb_dirband_start;
246 /* Alloc sector if it's free */
248 static int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec)
250 struct quad_buffer_head qbh;
253 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
254 if (bmp[(sec & 0x3fff) >> 5] & (1 << (sec & 0x1f))) {
255 bmp[(sec & 0x3fff) >> 5] &= ~(1 << (sec & 0x1f));
256 hpfs_mark_4buffers_dirty(&qbh);
267 int hpfs_alloc_if_possible(struct super_block *s, secno sec)
270 hpfs_lock_creation(s);
271 r = hpfs_alloc_if_possible_nolock(s, sec);
272 hpfs_unlock_creation(s);
276 /* Free sectors in bitmaps */
278 void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
280 struct quad_buffer_head qbh;
282 struct hpfs_sb_info *sbi = hpfs_sb(s);
286 hpfs_error(s, "Trying to free reserved sector %08x", sec);
290 sbi->sb_max_fwd_alloc += n > 0xffff ? 0xffff : n;
291 if (sbi->sb_max_fwd_alloc > 0xffffff) sbi->sb_max_fwd_alloc = 0xffffff;
293 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
298 if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f) & 1)) {
299 hpfs_error(s, "sector %08x not allocated", sec);
304 bmp[(sec & 0x3fff) >> 5] |= 1 << (sec & 0x1f);
306 hpfs_mark_4buffers_dirty(&qbh);
311 if (!(++sec & 0x3fff)) {
312 hpfs_mark_4buffers_dirty(&qbh);
320 * Check if there are at least n free dnodes on the filesystem.
321 * Called before adding to dnode. If we run out of space while
322 * splitting dnodes, it would corrupt dnode tree.
325 int hpfs_check_free_dnodes(struct super_block *s, int n)
327 int n_bmps = (hpfs_sb(s)->sb_fs_size + 0x4000 - 1) >> 14;
328 int b = hpfs_sb(s)->sb_c_bitmap & 0x0fffffff;
331 struct quad_buffer_head qbh;
332 if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
333 for (j = 0; j < 512; j++) {
335 if (!bmp[j]) continue;
336 for (k = bmp[j]; k; k >>= 1) if (k & 1) if (!--n) {
344 if (hpfs_sb(s)->sb_c_bitmap != -1) {
345 bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
350 if (i >= n_bmps) return 1;
351 bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
354 for (j = 0; j < 512; j++) {
356 if (!bmp[j]) continue;
357 for (k = 0xf; k; k <<= 4)
358 if ((bmp[j] & k) == k) {
371 void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
373 if (hpfs_sb(s)->sb_chk) if (dno & 3) {
374 hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
377 if (dno < hpfs_sb(s)->sb_dirband_start ||
378 dno >= hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
379 hpfs_free_sectors(s, dno, 4);
381 struct quad_buffer_head qbh;
383 unsigned ssec = (dno - hpfs_sb(s)->sb_dirband_start) / 4;
385 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
389 bmp[ssec >> 5] |= 1 << (ssec & 0x1f);
390 hpfs_mark_4buffers_dirty(&qbh);
396 struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
397 dnode_secno *dno, struct quad_buffer_head *qbh,
401 if (hpfs_count_one_bitmap(s, hpfs_sb(s)->sb_dmap) > FREE_DNODES_ADD) {
402 if (!(*dno = alloc_in_dirband(s, near, lock)))
403 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock))) return NULL;
405 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock)))
406 if (!(*dno = alloc_in_dirband(s, near, lock))) return NULL;
408 if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
409 hpfs_free_dnode(s, *dno);
413 d->magic = DNODE_MAGIC;
423 struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
424 struct buffer_head **bh)
427 if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD, 1))) return NULL;
428 if (!(f = hpfs_get_sector(s, *fno, bh))) {
429 hpfs_free_sectors(s, *fno, 1);
433 f->magic = FNODE_MAGIC;
435 f->btree.n_free_nodes = 8;
436 f->btree.first_free = 8;
440 struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
441 struct buffer_head **bh)
444 if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD, 1))) return NULL;
445 if (!(a = hpfs_get_sector(s, *ano, bh))) {
446 hpfs_free_sectors(s, *ano, 1);
450 a->magic = ANODE_MAGIC;
452 a->btree.n_free_nodes = 40;
453 a->btree.n_used_nodes = 0;
454 a->btree.first_free = 8;