1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/hpfs/alloc.c
5 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
7 * HPFS bitmap operations
12 static void hpfs_claim_alloc(struct super_block *s, secno sec)
14 struct hpfs_sb_info *sbi = hpfs_sb(s);
15 if (sbi->sb_n_free != (unsigned)-1) {
16 if (unlikely(!sbi->sb_n_free)) {
17 hpfs_error(s, "free count underflow, allocating sector %08x", sec);
25 static void hpfs_claim_free(struct super_block *s, secno sec)
27 struct hpfs_sb_info *sbi = hpfs_sb(s);
28 if (sbi->sb_n_free != (unsigned)-1) {
29 if (unlikely(sbi->sb_n_free >= sbi->sb_fs_size)) {
30 hpfs_error(s, "free count overflow, freeing sector %08x", sec);
38 static void hpfs_claim_dirband_alloc(struct super_block *s, secno sec)
40 struct hpfs_sb_info *sbi = hpfs_sb(s);
41 if (sbi->sb_n_free_dnodes != (unsigned)-1) {
42 if (unlikely(!sbi->sb_n_free_dnodes)) {
43 hpfs_error(s, "dirband free count underflow, allocating sector %08x", sec);
44 sbi->sb_n_free_dnodes = -1;
47 sbi->sb_n_free_dnodes--;
51 static void hpfs_claim_dirband_free(struct super_block *s, secno sec)
53 struct hpfs_sb_info *sbi = hpfs_sb(s);
54 if (sbi->sb_n_free_dnodes != (unsigned)-1) {
55 if (unlikely(sbi->sb_n_free_dnodes >= sbi->sb_dirband_size / 4)) {
56 hpfs_error(s, "dirband free count overflow, freeing sector %08x", sec);
57 sbi->sb_n_free_dnodes = -1;
60 sbi->sb_n_free_dnodes++;
65 * Check if a sector is allocated in bitmap
66 * This is really slow. Turned on only if chk==2
69 static int chk_if_allocated(struct super_block *s, secno sec, char *msg)
71 struct quad_buffer_head qbh;
73 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "chk"))) goto fail;
74 if ((le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) >> (sec & 0x1f)) & 1) {
75 hpfs_error(s, "sector '%s' - %08x not allocated in bitmap", msg, sec);
79 if (sec >= hpfs_sb(s)->sb_dirband_start && sec < hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
80 unsigned ssec = (sec - hpfs_sb(s)->sb_dirband_start) / 4;
81 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto fail;
82 if ((le32_to_cpu(bmp[ssec >> 5]) >> (ssec & 0x1f)) & 1) {
83 hpfs_error(s, "sector '%s' - %08x not allocated in directory bitmap", msg, sec);
96 * Check if sector(s) have proper number and additionally check if they're
97 * allocated in bitmap.
100 int hpfs_chk_sectors(struct super_block *s, secno start, int len, char *msg)
102 if (start + len < start || start < 0x12 ||
103 start + len > hpfs_sb(s)->sb_fs_size) {
104 hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start);
107 if (hpfs_sb(s)->sb_chk>=2) {
109 for (i = 0; i < len; i++)
110 if (chk_if_allocated(s, start + i, msg)) return 1;
115 static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward)
117 struct quad_buffer_head qbh;
119 unsigned bs = near & ~0x3fff;
120 unsigned nr = (near & 0x3fff) & ~(n - 1);
125 if (n != 1 && n != 4) {
126 hpfs_error(s, "Bad allocation size: %d", n);
130 if (!(bmp = hpfs_map_bitmap(s, near >> 14, &qbh, "aib"))) goto uls;
132 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto uls;
134 if (!tstbits(bmp, nr, n + forward)) {
139 while ((a = tstbits(bmp, q, n + forward)) != 0) {
141 if (n != 1) q = ((q-1)&~(n-1))+n;
147 } else if (q > nr) break;
154 /*for (i = nr + 1; i != nr; i++, i &= 0x1ff) */
157 if (!le32_to_cpu(bmp[i])) goto cont;
158 if (n + forward >= 0x3f && le32_to_cpu(bmp[i]) != 0xffffffff) goto cont;
161 unsigned k = le32_to_cpu(bmp[i-1]);
162 while (k & 0x80000000) {
166 if (n != 1) q = ((q-1)&~(n-1))+n;
167 while ((a = tstbits(bmp, q, n + forward)) != 0) {
169 if (n != 1) q = ((q-1)&~(n-1))+n;
181 if (hpfs_sb(s)->sb_chk && ((ret >> 14) != (bs >> 14) || (le32_to_cpu(bmp[(ret & 0x3fff) >> 5]) | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) {
182 hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret);
186 bmp[(ret & 0x3fff) >> 5] &= cpu_to_le32(~(((1 << n) - 1) << (ret & 0x1f)));
187 hpfs_mark_4buffers_dirty(&qbh);
196 * Allocation strategy: 1) search place near the sector specified
197 * 2) search bitmap where free sectors last found
198 * 3) search all bitmaps
199 * 4) search all bitmaps ignoring number of pre-allocated
203 secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward)
208 struct hpfs_sb_info *sbi = hpfs_sb(s);
215 n_bmps = (sbi->sb_fs_size + 0x4000 - 1) >> 14;
216 if (near && near < sbi->sb_fs_size) {
217 if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
218 near_bmp = near >> 14;
219 } else near_bmp = n_bmps / 2;
222 if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
226 if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
228 if (!f_p) if (forward > sbi->sb_max_fwd_alloc) forward = sbi->sb_max_fwd_alloc;
230 for (i = 0; i < n_bmps; i++) {
231 if (near_bmp+i < n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i) << 14, n, forward)))) {
232 sbi->sb_c_bitmap = near_bmp+i;
236 if (near_bmp-i-1 >= 0 && ((sec = alloc_in_bmp(s, (near_bmp-i-1) << 14, n, forward)))) {
237 sbi->sb_c_bitmap = near_bmp-i-1;
241 if (near_bmp+i >= n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i-n_bmps) << 14, n, forward)))) {
242 sbi->sb_c_bitmap = near_bmp+i-n_bmps;
246 if (i == 1 && sbi->sb_c_bitmap != -1 && ((sec = alloc_in_bmp(s, (sbi->sb_c_bitmap) << 14, n, forward)))) {
252 sbi->sb_max_fwd_alloc = forward * 3 / 4;
262 hpfs_claim_alloc(s, sec + i);
263 while (unlikely(++i < n));
266 for (i = 0; i < forward; i++) {
267 if (!hpfs_alloc_if_possible(s, sec + n + i)) {
268 hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
277 static secno alloc_in_dirband(struct super_block *s, secno near)
281 struct hpfs_sb_info *sbi = hpfs_sb(s);
282 if (nr < sbi->sb_dirband_start)
283 nr = sbi->sb_dirband_start;
284 if (nr >= sbi->sb_dirband_start + sbi->sb_dirband_size)
285 nr = sbi->sb_dirband_start + sbi->sb_dirband_size - 4;
286 nr -= sbi->sb_dirband_start;
288 sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
290 hpfs_claim_dirband_alloc(s, sec);
291 return ((sec & 0x3fff) << 2) + sbi->sb_dirband_start;
294 /* Alloc sector if it's free */
296 int hpfs_alloc_if_possible(struct super_block *s, secno sec)
298 struct quad_buffer_head qbh;
300 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
301 if (le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) & (1 << (sec & 0x1f))) {
302 bmp[(sec & 0x3fff) >> 5] &= cpu_to_le32(~(1 << (sec & 0x1f)));
303 hpfs_mark_4buffers_dirty(&qbh);
305 hpfs_claim_alloc(s, sec);
313 /* Free sectors in bitmaps */
315 void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
317 struct quad_buffer_head qbh;
319 struct hpfs_sb_info *sbi = hpfs_sb(s);
323 hpfs_error(s, "Trying to free reserved sector %08x", sec);
326 sbi->sb_max_fwd_alloc += n > 0xffff ? 0xffff : n;
327 if (sbi->sb_max_fwd_alloc > 0xffffff) sbi->sb_max_fwd_alloc = 0xffffff;
329 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
333 if ((le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) >> (sec & 0x1f) & 1)) {
334 hpfs_error(s, "sector %08x not allocated", sec);
338 bmp[(sec & 0x3fff) >> 5] |= cpu_to_le32(1 << (sec & 0x1f));
339 hpfs_claim_free(s, sec);
341 hpfs_mark_4buffers_dirty(&qbh);
345 if (!(++sec & 0x3fff)) {
346 hpfs_mark_4buffers_dirty(&qbh);
354 * Check if there are at least n free dnodes on the filesystem.
355 * Called before adding to dnode. If we run out of space while
356 * splitting dnodes, it would corrupt dnode tree.
359 int hpfs_check_free_dnodes(struct super_block *s, int n)
361 int n_bmps = (hpfs_sb(s)->sb_fs_size + 0x4000 - 1) >> 14;
362 int b = hpfs_sb(s)->sb_c_bitmap & 0x0fffffff;
365 struct quad_buffer_head qbh;
366 if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
367 for (j = 0; j < 512; j++) {
369 if (!le32_to_cpu(bmp[j])) continue;
370 for (k = le32_to_cpu(bmp[j]); k; k >>= 1) if (k & 1) if (!--n) {
378 if (hpfs_sb(s)->sb_c_bitmap != -1) {
379 bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
384 if (i >= n_bmps) return 1;
385 bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
388 for (j = 0; j < 512; j++) {
390 if (!le32_to_cpu(bmp[j])) continue;
391 for (k = 0xf; k; k <<= 4)
392 if ((le32_to_cpu(bmp[j]) & k) == k) {
405 void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
407 if (hpfs_sb(s)->sb_chk) if (dno & 3) {
408 hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
411 if (dno < hpfs_sb(s)->sb_dirband_start ||
412 dno >= hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
413 hpfs_free_sectors(s, dno, 4);
415 struct quad_buffer_head qbh;
417 unsigned ssec = (dno - hpfs_sb(s)->sb_dirband_start) / 4;
418 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
421 bmp[ssec >> 5] |= cpu_to_le32(1 << (ssec & 0x1f));
422 hpfs_mark_4buffers_dirty(&qbh);
424 hpfs_claim_dirband_free(s, dno);
428 struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
429 dnode_secno *dno, struct quad_buffer_head *qbh)
432 if (hpfs_get_free_dnodes(s) > FREE_DNODES_ADD) {
433 if (!(*dno = alloc_in_dirband(s, near)))
434 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0))) return NULL;
436 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0)))
437 if (!(*dno = alloc_in_dirband(s, near))) return NULL;
439 if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
440 hpfs_free_dnode(s, *dno);
444 d->magic = cpu_to_le32(DNODE_MAGIC);
445 d->first_free = cpu_to_le32(52);
450 d->self = cpu_to_le32(*dno);
454 struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
455 struct buffer_head **bh)
458 if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD))) return NULL;
459 if (!(f = hpfs_get_sector(s, *fno, bh))) {
460 hpfs_free_sectors(s, *fno, 1);
464 f->magic = cpu_to_le32(FNODE_MAGIC);
465 f->ea_offs = cpu_to_le16(0xc4);
466 f->btree.n_free_nodes = 8;
467 f->btree.first_free = cpu_to_le16(8);
471 struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
472 struct buffer_head **bh)
475 if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD))) return NULL;
476 if (!(a = hpfs_get_sector(s, *ano, bh))) {
477 hpfs_free_sectors(s, *ano, 1);
481 a->magic = cpu_to_le32(ANODE_MAGIC);
482 a->self = cpu_to_le32(*ano);
483 a->btree.n_free_nodes = 40;
484 a->btree.n_used_nodes = 0;
485 a->btree.first_free = cpu_to_le16(8);
489 static unsigned find_run(__le32 *bmp, unsigned *idx)
492 while (tstbits(bmp, *idx, 1)) {
494 if (unlikely(*idx >= 0x4000))
498 while (!tstbits(bmp, *idx + len, 1))
503 static int do_trim(struct super_block *s, secno start, unsigned len, secno limit_start, secno limit_end, unsigned minlen, unsigned *result)
507 if (fatal_signal_pending(current))
510 if (start < limit_start)
516 if (end - start < minlen)
518 err = sb_issue_discard(s, start, end - start, GFP_NOFS, 0);
521 *result += end - start;
525 int hpfs_trim_fs(struct super_block *s, u64 start, u64 end, u64 minlen, unsigned *result)
528 struct hpfs_sb_info *sbi = hpfs_sb(s);
529 unsigned idx, len, start_bmp, end_bmp;
531 struct quad_buffer_head qbh;
534 if (!end || end > sbi->sb_fs_size)
535 end = sbi->sb_fs_size;
536 if (start >= sbi->sb_fs_size)
540 if (start < sbi->sb_dirband_start + sbi->sb_dirband_size && end > sbi->sb_dirband_start) {
546 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
551 while ((len = find_run(bmp, &idx)) && !err) {
552 err = do_trim(s, sbi->sb_dirband_start + idx * 4, len * 4, start, end, minlen, result);
559 start_bmp = start >> 14;
560 end_bmp = (end + 0x3fff) >> 14;
561 while (start_bmp < end_bmp && !err) {
567 if (!(bmp = hpfs_map_bitmap(s, start_bmp, &qbh, "trim"))) {
572 while ((len = find_run(bmp, &idx)) && !err) {
573 err = do_trim(s, (start_bmp << 14) + idx, len, start, end, minlen, result);