2 * linux/fs/hpfs/alloc.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * HPFS bitmap operations
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;
20 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "chk"))) goto fail;
21 if ((le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) >> (sec & 0x1f)) & 1) {
22 hpfs_error(s, "sector '%s' - %08x not allocated in bitmap", msg, sec);
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 ((le32_to_cpu(bmp[ssec >> 5]) >> (ssec & 0x1f)) & 1) {
30 hpfs_error(s, "sector '%s' - %08x not allocated in directory bitmap", msg, sec);
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 > hpfs_sb(s)->sb_fs_size) {
51 hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start);
54 if (hpfs_sb(s)->sb_chk>=2) {
56 for (i = 0; i < len; i++)
57 if (chk_if_allocated(s, start + i, msg)) return 1;
62 static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward)
64 struct quad_buffer_head qbh;
66 unsigned bs = near & ~0x3fff;
67 unsigned nr = (near & 0x3fff) & ~(n - 1);
72 if (n != 1 && n != 4) {
73 hpfs_error(s, "Bad allocation size: %d", n);
77 if (!(bmp = hpfs_map_bitmap(s, near >> 14, &qbh, "aib"))) goto uls;
79 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto uls;
81 if (!tstbits(bmp, nr, n + forward)) {
86 while ((a = tstbits(bmp, q, n + forward)) != 0) {
88 if (n != 1) q = ((q-1)&~(n-1))+n;
94 } else if (q > nr) break;
101 /*for (i = nr + 1; i != nr; i++, i &= 0x1ff) */
104 if (!le32_to_cpu(bmp[i])) goto cont;
105 if (n + forward >= 0x3f && le32_to_cpu(bmp[i]) != 0xffffffff) goto cont;
108 unsigned k = le32_to_cpu(bmp[i-1]);
109 while (k & 0x80000000) {
113 if (n != 1) q = ((q-1)&~(n-1))+n;
114 while ((a = tstbits(bmp, q, n + forward)) != 0) {
116 if (n != 1) q = ((q-1)&~(n-1))+n;
128 if (hpfs_sb(s)->sb_chk && ((ret >> 14) != (bs >> 14) || (le32_to_cpu(bmp[(ret & 0x3fff) >> 5]) | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) {
129 hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret);
133 bmp[(ret & 0x3fff) >> 5] &= cpu_to_le32(~(((1 << n) - 1) << (ret & 0x1f)));
134 hpfs_mark_4buffers_dirty(&qbh);
143 * Allocation strategy: 1) search place near the sector specified
144 * 2) search bitmap where free sectors last found
145 * 3) search all bitmaps
146 * 4) search all bitmaps ignoring number of pre-allocated
150 secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward)
155 struct hpfs_sb_info *sbi = hpfs_sb(s);
162 n_bmps = (sbi->sb_fs_size + 0x4000 - 1) >> 14;
163 if (near && near < sbi->sb_fs_size) {
164 if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
165 near_bmp = near >> 14;
166 } else near_bmp = n_bmps / 2;
169 if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
173 if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
175 if (!f_p) if (forward > sbi->sb_max_fwd_alloc) forward = sbi->sb_max_fwd_alloc;
177 for (i = 0; i < n_bmps; i++) {
178 if (near_bmp+i < n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i) << 14, n, forward)))) {
179 sbi->sb_c_bitmap = near_bmp+i;
183 if (near_bmp-i-1 >= 0 && ((sec = alloc_in_bmp(s, (near_bmp-i-1) << 14, n, forward)))) {
184 sbi->sb_c_bitmap = near_bmp-i-1;
188 if (near_bmp+i >= n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i-n_bmps) << 14, n, forward)))) {
189 sbi->sb_c_bitmap = near_bmp+i-n_bmps;
193 if (i == 1 && sbi->sb_c_bitmap != -1 && ((sec = alloc_in_bmp(s, (sbi->sb_c_bitmap) << 14, n, forward)))) {
199 sbi->sb_max_fwd_alloc = forward * 3 / 4;
207 for (i = 0; i < forward; i++) {
208 if (!hpfs_alloc_if_possible(s, sec + i + 1)) {
209 hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
218 static secno alloc_in_dirband(struct super_block *s, secno near)
222 struct hpfs_sb_info *sbi = hpfs_sb(s);
223 if (nr < sbi->sb_dirband_start)
224 nr = sbi->sb_dirband_start;
225 if (nr >= sbi->sb_dirband_start + sbi->sb_dirband_size)
226 nr = sbi->sb_dirband_start + sbi->sb_dirband_size - 4;
227 nr -= sbi->sb_dirband_start;
229 sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
231 return ((sec & 0x3fff) << 2) + sbi->sb_dirband_start;
234 /* Alloc sector if it's free */
236 int hpfs_alloc_if_possible(struct super_block *s, secno sec)
238 struct quad_buffer_head qbh;
240 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
241 if (le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) & (1 << (sec & 0x1f))) {
242 bmp[(sec & 0x3fff) >> 5] &= cpu_to_le32(~(1 << (sec & 0x1f)));
243 hpfs_mark_4buffers_dirty(&qbh);
252 /* Free sectors in bitmaps */
254 void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
256 struct quad_buffer_head qbh;
258 struct hpfs_sb_info *sbi = hpfs_sb(s);
262 hpfs_error(s, "Trying to free reserved sector %08x", sec);
265 sbi->sb_max_fwd_alloc += n > 0xffff ? 0xffff : n;
266 if (sbi->sb_max_fwd_alloc > 0xffffff) sbi->sb_max_fwd_alloc = 0xffffff;
268 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
272 if ((le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) >> (sec & 0x1f) & 1)) {
273 hpfs_error(s, "sector %08x not allocated", sec);
277 bmp[(sec & 0x3fff) >> 5] |= cpu_to_le32(1 << (sec & 0x1f));
279 hpfs_mark_4buffers_dirty(&qbh);
283 if (!(++sec & 0x3fff)) {
284 hpfs_mark_4buffers_dirty(&qbh);
292 * Check if there are at least n free dnodes on the filesystem.
293 * Called before adding to dnode. If we run out of space while
294 * splitting dnodes, it would corrupt dnode tree.
297 int hpfs_check_free_dnodes(struct super_block *s, int n)
299 int n_bmps = (hpfs_sb(s)->sb_fs_size + 0x4000 - 1) >> 14;
300 int b = hpfs_sb(s)->sb_c_bitmap & 0x0fffffff;
303 struct quad_buffer_head qbh;
304 if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
305 for (j = 0; j < 512; j++) {
307 if (!le32_to_cpu(bmp[j])) continue;
308 for (k = le32_to_cpu(bmp[j]); k; k >>= 1) if (k & 1) if (!--n) {
316 if (hpfs_sb(s)->sb_c_bitmap != -1) {
317 bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
322 if (i >= n_bmps) return 1;
323 bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
326 for (j = 0; j < 512; j++) {
328 if (!le32_to_cpu(bmp[j])) continue;
329 for (k = 0xf; k; k <<= 4)
330 if ((le32_to_cpu(bmp[j]) & k) == k) {
343 void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
345 if (hpfs_sb(s)->sb_chk) if (dno & 3) {
346 hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
349 if (dno < hpfs_sb(s)->sb_dirband_start ||
350 dno >= hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
351 hpfs_free_sectors(s, dno, 4);
353 struct quad_buffer_head qbh;
355 unsigned ssec = (dno - hpfs_sb(s)->sb_dirband_start) / 4;
356 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
359 bmp[ssec >> 5] |= cpu_to_le32(1 << (ssec & 0x1f));
360 hpfs_mark_4buffers_dirty(&qbh);
365 struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
366 dnode_secno *dno, struct quad_buffer_head *qbh)
369 if (hpfs_count_one_bitmap(s, hpfs_sb(s)->sb_dmap) > FREE_DNODES_ADD) {
370 if (!(*dno = alloc_in_dirband(s, near)))
371 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0))) return NULL;
373 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0)))
374 if (!(*dno = alloc_in_dirband(s, near))) return NULL;
376 if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
377 hpfs_free_dnode(s, *dno);
381 d->magic = cpu_to_le32(DNODE_MAGIC);
382 d->first_free = cpu_to_le32(52);
387 d->self = cpu_to_le32(*dno);
391 struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
392 struct buffer_head **bh)
395 if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD))) return NULL;
396 if (!(f = hpfs_get_sector(s, *fno, bh))) {
397 hpfs_free_sectors(s, *fno, 1);
401 f->magic = cpu_to_le32(FNODE_MAGIC);
402 f->ea_offs = cpu_to_le16(0xc4);
403 f->btree.n_free_nodes = 8;
404 f->btree.first_free = cpu_to_le16(8);
408 struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
409 struct buffer_head **bh)
412 if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD))) return NULL;
413 if (!(a = hpfs_get_sector(s, *ano, bh))) {
414 hpfs_free_sectors(s, *ano, 1);
418 a->magic = cpu_to_le32(ANODE_MAGIC);
419 a->self = cpu_to_le32(*ano);
420 a->btree.n_free_nodes = 40;
421 a->btree.n_used_nodes = 0;
422 a->btree.first_free = cpu_to_le16(8);