2 * linux/fs/hpfs/alloc.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * HPFS bitmap operations
11 static void hpfs_claim_alloc(struct super_block
*s
, secno sec
)
13 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
14 if (sbi
->sb_n_free
!= (unsigned)-1) {
15 if (unlikely(!sbi
->sb_n_free
)) {
16 hpfs_error(s
, "free count underflow, allocating sector %08x", sec
);
24 static void hpfs_claim_free(struct super_block
*s
, secno sec
)
26 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
27 if (sbi
->sb_n_free
!= (unsigned)-1) {
28 if (unlikely(sbi
->sb_n_free
>= sbi
->sb_fs_size
)) {
29 hpfs_error(s
, "free count overflow, freeing sector %08x", sec
);
37 static void hpfs_claim_dirband_alloc(struct super_block
*s
, secno sec
)
39 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
40 if (sbi
->sb_n_free_dnodes
!= (unsigned)-1) {
41 if (unlikely(!sbi
->sb_n_free_dnodes
)) {
42 hpfs_error(s
, "dirband free count underflow, allocating sector %08x", sec
);
43 sbi
->sb_n_free_dnodes
= -1;
46 sbi
->sb_n_free_dnodes
--;
50 static void hpfs_claim_dirband_free(struct super_block
*s
, secno sec
)
52 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
53 if (sbi
->sb_n_free_dnodes
!= (unsigned)-1) {
54 if (unlikely(sbi
->sb_n_free_dnodes
>= sbi
->sb_dirband_size
/ 4)) {
55 hpfs_error(s
, "dirband free count overflow, freeing sector %08x", sec
);
56 sbi
->sb_n_free_dnodes
= -1;
59 sbi
->sb_n_free_dnodes
++;
64 * Check if a sector is allocated in bitmap
65 * This is really slow. Turned on only if chk==2
68 static int chk_if_allocated(struct super_block
*s
, secno sec
, char *msg
)
70 struct quad_buffer_head qbh
;
72 if (!(bmp
= hpfs_map_bitmap(s
, sec
>> 14, &qbh
, "chk"))) goto fail
;
73 if ((le32_to_cpu(bmp
[(sec
& 0x3fff) >> 5]) >> (sec
& 0x1f)) & 1) {
74 hpfs_error(s
, "sector '%s' - %08x not allocated in bitmap", msg
, sec
);
78 if (sec
>= hpfs_sb(s
)->sb_dirband_start
&& sec
< hpfs_sb(s
)->sb_dirband_start
+ hpfs_sb(s
)->sb_dirband_size
) {
79 unsigned ssec
= (sec
- hpfs_sb(s
)->sb_dirband_start
) / 4;
80 if (!(bmp
= hpfs_map_dnode_bitmap(s
, &qbh
))) goto fail
;
81 if ((le32_to_cpu(bmp
[ssec
>> 5]) >> (ssec
& 0x1f)) & 1) {
82 hpfs_error(s
, "sector '%s' - %08x not allocated in directory bitmap", msg
, sec
);
95 * Check if sector(s) have proper number and additionally check if they're
96 * allocated in bitmap.
99 int hpfs_chk_sectors(struct super_block
*s
, secno start
, int len
, char *msg
)
101 if (start
+ len
< start
|| start
< 0x12 ||
102 start
+ len
> hpfs_sb(s
)->sb_fs_size
) {
103 hpfs_error(s
, "sector(s) '%s' badly placed at %08x", msg
, start
);
106 if (hpfs_sb(s
)->sb_chk
>=2) {
108 for (i
= 0; i
< len
; i
++)
109 if (chk_if_allocated(s
, start
+ i
, msg
)) return 1;
114 static secno
alloc_in_bmp(struct super_block
*s
, secno near
, unsigned n
, unsigned forward
)
116 struct quad_buffer_head qbh
;
118 unsigned bs
= near
& ~0x3fff;
119 unsigned nr
= (near
& 0x3fff) & ~(n
- 1);
124 if (n
!= 1 && n
!= 4) {
125 hpfs_error(s
, "Bad allocation size: %d", n
);
129 if (!(bmp
= hpfs_map_bitmap(s
, near
>> 14, &qbh
, "aib"))) goto uls
;
131 if (!(bmp
= hpfs_map_dnode_bitmap(s
, &qbh
))) goto uls
;
133 if (!tstbits(bmp
, nr
, n
+ forward
)) {
138 while ((a
= tstbits(bmp
, q
, n
+ forward
)) != 0) {
140 if (n
!= 1) q
= ((q
-1)&~(n
-1))+n
;
146 } else if (q
> nr
) break;
153 /*for (i = nr + 1; i != nr; i++, i &= 0x1ff) */
156 if (!le32_to_cpu(bmp
[i
])) goto cont
;
157 if (n
+ forward
>= 0x3f && le32_to_cpu(bmp
[i
]) != 0xffffffff) goto cont
;
160 unsigned k
= le32_to_cpu(bmp
[i
-1]);
161 while (k
& 0x80000000) {
165 if (n
!= 1) q
= ((q
-1)&~(n
-1))+n
;
166 while ((a
= tstbits(bmp
, q
, n
+ forward
)) != 0) {
168 if (n
!= 1) q
= ((q
-1)&~(n
-1))+n
;
180 if (hpfs_sb(s
)->sb_chk
&& ((ret
>> 14) != (bs
>> 14) || (le32_to_cpu(bmp
[(ret
& 0x3fff) >> 5]) | ~(((1 << n
) - 1) << (ret
& 0x1f))) != 0xffffffff)) {
181 hpfs_error(s
, "Allocation doesn't work! Wanted %d, allocated at %08x", n
, ret
);
185 bmp
[(ret
& 0x3fff) >> 5] &= cpu_to_le32(~(((1 << n
) - 1) << (ret
& 0x1f)));
186 hpfs_mark_4buffers_dirty(&qbh
);
195 * Allocation strategy: 1) search place near the sector specified
196 * 2) search bitmap where free sectors last found
197 * 3) search all bitmaps
198 * 4) search all bitmaps ignoring number of pre-allocated
202 secno
hpfs_alloc_sector(struct super_block
*s
, secno near
, unsigned n
, int forward
)
207 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
214 n_bmps
= (sbi
->sb_fs_size
+ 0x4000 - 1) >> 14;
215 if (near
&& near
< sbi
->sb_fs_size
) {
216 if ((sec
= alloc_in_bmp(s
, near
, n
, f_p
? forward
: forward
/4))) goto ret
;
217 near_bmp
= near
>> 14;
218 } else near_bmp
= n_bmps
/ 2;
221 if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
225 if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
227 if (!f_p
) if (forward
> sbi
->sb_max_fwd_alloc
) forward
= sbi
->sb_max_fwd_alloc
;
229 for (i
= 0; i
< n_bmps
; i
++) {
230 if (near_bmp
+i
< n_bmps
&& ((sec
= alloc_in_bmp(s
, (near_bmp
+i
) << 14, n
, forward
)))) {
231 sbi
->sb_c_bitmap
= near_bmp
+i
;
235 if (near_bmp
-i
-1 >= 0 && ((sec
= alloc_in_bmp(s
, (near_bmp
-i
-1) << 14, n
, forward
)))) {
236 sbi
->sb_c_bitmap
= near_bmp
-i
-1;
240 if (near_bmp
+i
>= n_bmps
&& ((sec
= alloc_in_bmp(s
, (near_bmp
+i
-n_bmps
) << 14, n
, forward
)))) {
241 sbi
->sb_c_bitmap
= near_bmp
+i
-n_bmps
;
245 if (i
== 1 && sbi
->sb_c_bitmap
!= -1 && ((sec
= alloc_in_bmp(s
, (sbi
->sb_c_bitmap
) << 14, n
, forward
)))) {
251 sbi
->sb_max_fwd_alloc
= forward
* 3 / 4;
261 hpfs_claim_alloc(s
, sec
+ i
);
262 while (unlikely(++i
< n
));
265 for (i
= 0; i
< forward
; i
++) {
266 if (!hpfs_alloc_if_possible(s
, sec
+ n
+ i
)) {
267 hpfs_error(s
, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward
, sec
, i
);
276 static secno
alloc_in_dirband(struct super_block
*s
, secno near
)
280 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
281 if (nr
< sbi
->sb_dirband_start
)
282 nr
= sbi
->sb_dirband_start
;
283 if (nr
>= sbi
->sb_dirband_start
+ sbi
->sb_dirband_size
)
284 nr
= sbi
->sb_dirband_start
+ sbi
->sb_dirband_size
- 4;
285 nr
-= sbi
->sb_dirband_start
;
287 sec
= alloc_in_bmp(s
, (~0x3fff) | nr
, 1, 0);
289 hpfs_claim_dirband_alloc(s
, sec
);
290 return ((sec
& 0x3fff) << 2) + sbi
->sb_dirband_start
;
293 /* Alloc sector if it's free */
295 int hpfs_alloc_if_possible(struct super_block
*s
, secno sec
)
297 struct quad_buffer_head qbh
;
299 if (!(bmp
= hpfs_map_bitmap(s
, sec
>> 14, &qbh
, "aip"))) goto end
;
300 if (le32_to_cpu(bmp
[(sec
& 0x3fff) >> 5]) & (1 << (sec
& 0x1f))) {
301 bmp
[(sec
& 0x3fff) >> 5] &= cpu_to_le32(~(1 << (sec
& 0x1f)));
302 hpfs_mark_4buffers_dirty(&qbh
);
304 hpfs_claim_alloc(s
, sec
);
312 /* Free sectors in bitmaps */
314 void hpfs_free_sectors(struct super_block
*s
, secno sec
, unsigned n
)
316 struct quad_buffer_head qbh
;
318 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
322 hpfs_error(s
, "Trying to free reserved sector %08x", sec
);
325 sbi
->sb_max_fwd_alloc
+= n
> 0xffff ? 0xffff : n
;
326 if (sbi
->sb_max_fwd_alloc
> 0xffffff) sbi
->sb_max_fwd_alloc
= 0xffffff;
328 if (!(bmp
= hpfs_map_bitmap(s
, sec
>> 14, &qbh
, "free"))) {
332 if ((le32_to_cpu(bmp
[(sec
& 0x3fff) >> 5]) >> (sec
& 0x1f) & 1)) {
333 hpfs_error(s
, "sector %08x not allocated", sec
);
337 bmp
[(sec
& 0x3fff) >> 5] |= cpu_to_le32(1 << (sec
& 0x1f));
338 hpfs_claim_free(s
, sec
);
340 hpfs_mark_4buffers_dirty(&qbh
);
344 if (!(++sec
& 0x3fff)) {
345 hpfs_mark_4buffers_dirty(&qbh
);
353 * Check if there are at least n free dnodes on the filesystem.
354 * Called before adding to dnode. If we run out of space while
355 * splitting dnodes, it would corrupt dnode tree.
358 int hpfs_check_free_dnodes(struct super_block
*s
, int n
)
360 int n_bmps
= (hpfs_sb(s
)->sb_fs_size
+ 0x4000 - 1) >> 14;
361 int b
= hpfs_sb(s
)->sb_c_bitmap
& 0x0fffffff;
364 struct quad_buffer_head qbh
;
365 if ((bmp
= hpfs_map_dnode_bitmap(s
, &qbh
))) {
366 for (j
= 0; j
< 512; j
++) {
368 if (!le32_to_cpu(bmp
[j
])) continue;
369 for (k
= le32_to_cpu(bmp
[j
]); k
; k
>>= 1) if (k
& 1) if (!--n
) {
377 if (hpfs_sb(s
)->sb_c_bitmap
!= -1) {
378 bmp
= hpfs_map_bitmap(s
, b
, &qbh
, "chkdn1");
383 if (i
>= n_bmps
) return 1;
384 bmp
= hpfs_map_bitmap(s
, i
, &qbh
, "chkdn2");
387 for (j
= 0; j
< 512; j
++) {
389 if (!le32_to_cpu(bmp
[j
])) continue;
390 for (k
= 0xf; k
; k
<<= 4)
391 if ((le32_to_cpu(bmp
[j
]) & k
) == k
) {
404 void hpfs_free_dnode(struct super_block
*s
, dnode_secno dno
)
406 if (hpfs_sb(s
)->sb_chk
) if (dno
& 3) {
407 hpfs_error(s
, "hpfs_free_dnode: dnode %08x not aligned", dno
);
410 if (dno
< hpfs_sb(s
)->sb_dirband_start
||
411 dno
>= hpfs_sb(s
)->sb_dirband_start
+ hpfs_sb(s
)->sb_dirband_size
) {
412 hpfs_free_sectors(s
, dno
, 4);
414 struct quad_buffer_head qbh
;
416 unsigned ssec
= (dno
- hpfs_sb(s
)->sb_dirband_start
) / 4;
417 if (!(bmp
= hpfs_map_dnode_bitmap(s
, &qbh
))) {
420 bmp
[ssec
>> 5] |= cpu_to_le32(1 << (ssec
& 0x1f));
421 hpfs_mark_4buffers_dirty(&qbh
);
423 hpfs_claim_dirband_free(s
, dno
);
427 struct dnode
*hpfs_alloc_dnode(struct super_block
*s
, secno near
,
428 dnode_secno
*dno
, struct quad_buffer_head
*qbh
)
431 if (hpfs_get_free_dnodes(s
) > FREE_DNODES_ADD
) {
432 if (!(*dno
= alloc_in_dirband(s
, near
)))
433 if (!(*dno
= hpfs_alloc_sector(s
, near
, 4, 0))) return NULL
;
435 if (!(*dno
= hpfs_alloc_sector(s
, near
, 4, 0)))
436 if (!(*dno
= alloc_in_dirband(s
, near
))) return NULL
;
438 if (!(d
= hpfs_get_4sectors(s
, *dno
, qbh
))) {
439 hpfs_free_dnode(s
, *dno
);
443 d
->magic
= cpu_to_le32(DNODE_MAGIC
);
444 d
->first_free
= cpu_to_le32(52);
449 d
->self
= cpu_to_le32(*dno
);
453 struct fnode
*hpfs_alloc_fnode(struct super_block
*s
, secno near
, fnode_secno
*fno
,
454 struct buffer_head
**bh
)
457 if (!(*fno
= hpfs_alloc_sector(s
, near
, 1, FNODE_ALLOC_FWD
))) return NULL
;
458 if (!(f
= hpfs_get_sector(s
, *fno
, bh
))) {
459 hpfs_free_sectors(s
, *fno
, 1);
463 f
->magic
= cpu_to_le32(FNODE_MAGIC
);
464 f
->ea_offs
= cpu_to_le16(0xc4);
465 f
->btree
.n_free_nodes
= 8;
466 f
->btree
.first_free
= cpu_to_le16(8);
470 struct anode
*hpfs_alloc_anode(struct super_block
*s
, secno near
, anode_secno
*ano
,
471 struct buffer_head
**bh
)
474 if (!(*ano
= hpfs_alloc_sector(s
, near
, 1, ANODE_ALLOC_FWD
))) return NULL
;
475 if (!(a
= hpfs_get_sector(s
, *ano
, bh
))) {
476 hpfs_free_sectors(s
, *ano
, 1);
480 a
->magic
= cpu_to_le32(ANODE_MAGIC
);
481 a
->self
= cpu_to_le32(*ano
);
482 a
->btree
.n_free_nodes
= 40;
483 a
->btree
.n_used_nodes
= 0;
484 a
->btree
.first_free
= cpu_to_le16(8);