^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/hpfs/alloc.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * HPFS bitmap operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "hpfs_fn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static void hpfs_claim_alloc(struct super_block *s, secno sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct hpfs_sb_info *sbi = hpfs_sb(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) if (sbi->sb_n_free != (unsigned)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (unlikely(!sbi->sb_n_free)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) hpfs_error(s, "free count underflow, allocating sector %08x", sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) sbi->sb_n_free = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) sbi->sb_n_free--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static void hpfs_claim_free(struct super_block *s, secno sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct hpfs_sb_info *sbi = hpfs_sb(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (sbi->sb_n_free != (unsigned)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (unlikely(sbi->sb_n_free >= sbi->sb_fs_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) hpfs_error(s, "free count overflow, freeing sector %08x", sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) sbi->sb_n_free = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) sbi->sb_n_free++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static void hpfs_claim_dirband_alloc(struct super_block *s, secno sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct hpfs_sb_info *sbi = hpfs_sb(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (sbi->sb_n_free_dnodes != (unsigned)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (unlikely(!sbi->sb_n_free_dnodes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) hpfs_error(s, "dirband free count underflow, allocating sector %08x", sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) sbi->sb_n_free_dnodes = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) sbi->sb_n_free_dnodes--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static void hpfs_claim_dirband_free(struct super_block *s, secno sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct hpfs_sb_info *sbi = hpfs_sb(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (sbi->sb_n_free_dnodes != (unsigned)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (unlikely(sbi->sb_n_free_dnodes >= sbi->sb_dirband_size / 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) hpfs_error(s, "dirband free count overflow, freeing sector %08x", sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) sbi->sb_n_free_dnodes = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) sbi->sb_n_free_dnodes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Check if a sector is allocated in bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * This is really slow. Turned on only if chk==2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int chk_if_allocated(struct super_block *s, secno sec, char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __le32 *bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "chk"))) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if ((le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) >> (sec & 0x1f)) & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) hpfs_error(s, "sector '%s' - %08x not allocated in bitmap", msg, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (sec >= hpfs_sb(s)->sb_dirband_start && sec < hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned ssec = (sec - hpfs_sb(s)->sb_dirband_start) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if ((le32_to_cpu(bmp[ssec >> 5]) >> (ssec & 0x1f)) & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) hpfs_error(s, "sector '%s' - %08x not allocated in directory bitmap", msg, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) fail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Check if sector(s) have proper number and additionally check if they're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * allocated in bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int hpfs_chk_sectors(struct super_block *s, secno start, int len, char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (start + len < start || start < 0x12 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) start + len > hpfs_sb(s)->sb_fs_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (hpfs_sb(s)->sb_chk>=2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) for (i = 0; i < len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (chk_if_allocated(s, start + i, msg)) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) __le32 *bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unsigned bs = near & ~0x3fff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) unsigned nr = (near & 0x3fff) & ~(n - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*unsigned mnr;*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned i, q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int a, b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) secno ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (n != 1 && n != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) hpfs_error(s, "Bad allocation size: %d", n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (bs != ~0x3fff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!(bmp = hpfs_map_bitmap(s, near >> 14, &qbh, "aib"))) goto uls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto uls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!tstbits(bmp, nr, n + forward)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ret = bs + nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) q = nr + n; b = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) while ((a = tstbits(bmp, q, n + forward)) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) q += a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (n != 1) q = ((q-1)&~(n-1))+n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (!b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (q>>5 != nr>>5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) b = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) q = nr & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } else if (q > nr) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (!a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = bs + q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) goto rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) nr >>= 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*for (i = nr + 1; i != nr; i++, i &= 0x1ff) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) i = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!le32_to_cpu(bmp[i])) goto cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (n + forward >= 0x3f && le32_to_cpu(bmp[i]) != 0xffffffff) goto cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) q = i<<5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (i > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) unsigned k = le32_to_cpu(bmp[i-1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) while (k & 0x80000000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) q--; k <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (n != 1) q = ((q-1)&~(n-1))+n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) while ((a = tstbits(bmp, q, n + forward)) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) q += a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (n != 1) q = ((q-1)&~(n-1))+n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (q>>5 > i) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (!a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ret = bs + q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) cont:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) i++, i &= 0x1ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) } while (i != nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) rt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (hpfs_sb(s)->sb_chk && ((ret >> 14) != (bs >> 14) || (le32_to_cpu(bmp[(ret & 0x3fff) >> 5]) | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) bmp[(ret & 0x3fff) >> 5] &= cpu_to_le32(~(((1 << n) - 1) << (ret & 0x1f)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) hpfs_mark_4buffers_dirty(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) b:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) uls:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * Allocation strategy: 1) search place near the sector specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * 2) search bitmap where free sectors last found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * 3) search all bitmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * 4) search all bitmaps ignoring number of pre-allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * sectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) secno sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned n_bmps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct hpfs_sb_info *sbi = hpfs_sb(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int f_p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int near_bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (forward < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) forward = -forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) f_p = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) n_bmps = (sbi->sb_fs_size + 0x4000 - 1) >> 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (near && near < sbi->sb_fs_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) near_bmp = near >> 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) } else near_bmp = n_bmps / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (b != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) b &= 0x0fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) goto ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (!f_p) if (forward > sbi->sb_max_fwd_alloc) forward = sbi->sb_max_fwd_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) less_fwd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) for (i = 0; i < n_bmps; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (near_bmp+i < n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i) << 14, n, forward)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) sbi->sb_c_bitmap = near_bmp+i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) goto ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!forward) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (near_bmp-i-1 >= 0 && ((sec = alloc_in_bmp(s, (near_bmp-i-1) << 14, n, forward)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) sbi->sb_c_bitmap = near_bmp-i-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) goto ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (near_bmp+i >= n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i-n_bmps) << 14, n, forward)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) sbi->sb_c_bitmap = near_bmp+i-n_bmps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) goto ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (i == 1 && sbi->sb_c_bitmap != -1 && ((sec = alloc_in_bmp(s, (sbi->sb_c_bitmap) << 14, n, forward)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!f_p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (forward) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) sbi->sb_max_fwd_alloc = forward * 3 / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) forward /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) goto less_fwd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) hpfs_claim_alloc(s, sec + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) while (unlikely(++i < n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (sec && f_p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) for (i = 0; i < forward; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (!hpfs_alloc_if_possible(s, sec + n + i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static secno alloc_in_dirband(struct super_block *s, secno near)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) unsigned nr = near;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) secno sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct hpfs_sb_info *sbi = hpfs_sb(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (nr < sbi->sb_dirband_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) nr = sbi->sb_dirband_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (nr >= sbi->sb_dirband_start + sbi->sb_dirband_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) nr = sbi->sb_dirband_start + sbi->sb_dirband_size - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) nr -= sbi->sb_dirband_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) nr >>= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (!sec) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) hpfs_claim_dirband_alloc(s, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return ((sec & 0x3fff) << 2) + sbi->sb_dirband_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* Alloc sector if it's free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int hpfs_alloc_if_possible(struct super_block *s, secno sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) __le32 *bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) & (1 << (sec & 0x1f))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) bmp[(sec & 0x3fff) >> 5] &= cpu_to_le32(~(1 << (sec & 0x1f)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) hpfs_mark_4buffers_dirty(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) hpfs_claim_alloc(s, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* Free sectors in bitmaps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) __le32 *bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct hpfs_sb_info *sbi = hpfs_sb(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /*pr_info("2 - ");*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!n) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (sec < 0x12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) hpfs_error(s, "Trying to free reserved sector %08x", sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) sbi->sb_max_fwd_alloc += n > 0xffff ? 0xffff : n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (sbi->sb_max_fwd_alloc > 0xffffff) sbi->sb_max_fwd_alloc = 0xffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) new_map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) new_tst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if ((le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) >> (sec & 0x1f) & 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) hpfs_error(s, "sector %08x not allocated", sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) bmp[(sec & 0x3fff) >> 5] |= cpu_to_le32(1 << (sec & 0x1f));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) hpfs_claim_free(s, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (!--n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) hpfs_mark_4buffers_dirty(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (!(++sec & 0x3fff)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) hpfs_mark_4buffers_dirty(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) goto new_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto new_tst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * Check if there are at least n free dnodes on the filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * Called before adding to dnode. If we run out of space while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * splitting dnodes, it would corrupt dnode tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int hpfs_check_free_dnodes(struct super_block *s, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int n_bmps = (hpfs_sb(s)->sb_fs_size + 0x4000 - 1) >> 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int b = hpfs_sb(s)->sb_c_bitmap & 0x0fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) __le32 *bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) for (j = 0; j < 512; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) unsigned k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (!le32_to_cpu(bmp[j])) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) for (k = le32_to_cpu(bmp[j]); k; k >>= 1) if (k & 1) if (!--n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (hpfs_sb(s)->sb_c_bitmap != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) goto chk_bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) chk_next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (i == b) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (i >= n_bmps) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) chk_bmp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (bmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) for (j = 0; j < 512; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) u32 k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (!le32_to_cpu(bmp[j])) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) for (k = 0xf; k; k <<= 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if ((le32_to_cpu(bmp[j]) & k) == k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (!--n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) goto chk_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (hpfs_sb(s)->sb_chk) if (dno & 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (dno < hpfs_sb(s)->sb_dirband_start ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) dno >= hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) hpfs_free_sectors(s, dno, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) __le32 *bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) unsigned ssec = (dno - hpfs_sb(s)->sb_dirband_start) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) bmp[ssec >> 5] |= cpu_to_le32(1 << (ssec & 0x1f));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) hpfs_mark_4buffers_dirty(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) hpfs_claim_dirband_free(s, dno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) dnode_secno *dno, struct quad_buffer_head *qbh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) struct dnode *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (hpfs_get_free_dnodes(s) > FREE_DNODES_ADD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (!(*dno = alloc_in_dirband(s, near)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (!(*dno = hpfs_alloc_sector(s, near, 4, 0))) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (!(*dno = hpfs_alloc_sector(s, near, 4, 0)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (!(*dno = alloc_in_dirband(s, near))) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) hpfs_free_dnode(s, *dno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) memset(d, 0, 2048);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) d->magic = cpu_to_le32(DNODE_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) d->first_free = cpu_to_le32(52);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) d->dirent[0] = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) d->dirent[2] = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) d->dirent[30] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) d->dirent[31] = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) d->self = cpu_to_le32(*dno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) struct buffer_head **bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct fnode *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD))) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (!(f = hpfs_get_sector(s, *fno, bh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) hpfs_free_sectors(s, *fno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) memset(f, 0, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) f->magic = cpu_to_le32(FNODE_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) f->ea_offs = cpu_to_le16(0xc4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) f->btree.n_free_nodes = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) f->btree.first_free = cpu_to_le16(8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct buffer_head **bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct anode *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD))) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (!(a = hpfs_get_sector(s, *ano, bh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) hpfs_free_sectors(s, *ano, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) memset(a, 0, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) a->magic = cpu_to_le32(ANODE_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) a->self = cpu_to_le32(*ano);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) a->btree.n_free_nodes = 40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) a->btree.n_used_nodes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) a->btree.first_free = cpu_to_le16(8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) static unsigned find_run(__le32 *bmp, unsigned *idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) unsigned len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) while (tstbits(bmp, *idx, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) (*idx)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (unlikely(*idx >= 0x4000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) while (!tstbits(bmp, *idx + len, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static int do_trim(struct super_block *s, secno start, unsigned len, secno limit_start, secno limit_end, unsigned minlen, unsigned *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) secno end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (fatal_signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) end = start + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (start < limit_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) start = limit_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (end > limit_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) end = limit_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (start >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (end - start < minlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) err = sb_issue_discard(s, start, end - start, GFP_NOFS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) *result += end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) int hpfs_trim_fs(struct super_block *s, u64 start, u64 end, u64 minlen, unsigned *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) struct hpfs_sb_info *sbi = hpfs_sb(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) unsigned idx, len, start_bmp, end_bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) __le32 *bmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) *result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (!end || end > sbi->sb_fs_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) end = sbi->sb_fs_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (start >= sbi->sb_fs_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (minlen > 0x4000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (start < sbi->sb_dirband_start + sbi->sb_dirband_size && end > sbi->sb_dirband_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) hpfs_lock(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (sb_rdonly(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) err = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) goto unlock_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) goto unlock_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) while ((len = find_run(bmp, &idx)) && !err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) err = do_trim(s, sbi->sb_dirband_start + idx * 4, len * 4, start, end, minlen, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) idx += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) unlock_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) hpfs_unlock(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) start_bmp = start >> 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) end_bmp = (end + 0x3fff) >> 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) while (start_bmp < end_bmp && !err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) hpfs_lock(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (sb_rdonly(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) err = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) goto unlock_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (!(bmp = hpfs_map_bitmap(s, start_bmp, &qbh, "trim"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) goto unlock_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) while ((len = find_run(bmp, &idx)) && !err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) err = do_trim(s, (start_bmp << 14) + idx, len, start, end, minlen, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) idx += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) unlock_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) hpfs_unlock(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) start_bmp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }