^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) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "omfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) unsigned long omfs_count_free(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) unsigned long sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) int nbits = sb->s_blocksize * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) for (i = 0; i < sbi->s_imap_size; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) sum += nbits - bitmap_weight(sbi->s_imap[i], nbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Counts the run of zero bits starting at bit up to max.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * It handles the case where a run might spill over a buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Called with bitmap lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int count_run(unsigned long **addr, int nbits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int addrlen, int bit, int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) for (; addrlen > 0; addrlen--, addr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) x = find_next_bit(*addr, nbits, bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) count += x - bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (x < nbits || count > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return min(count, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return min(count, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Sets or clears the run of count bits starting with bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Called with bitmap lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int set_run(struct super_block *sb, int map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int nbits, int bit, int count, int set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) bh = sb_bread(sb, clus_to_blk(sbi, sbi->s_bitmap_ino) + map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) for (i = 0; i < count; i++, bit++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (bit >= nbits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) map++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) bh = sb_bread(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) clus_to_blk(sbi, sbi->s_bitmap_ino) + map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) set_bit(bit, sbi->s_imap[map]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) set_bit(bit, (unsigned long *)bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) clear_bit(bit, sbi->s_imap[map]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) clear_bit(bit, (unsigned long *)bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Tries to allocate exactly one block. Returns true if successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int omfs_allocate_block(struct super_block *sb, u64 block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int bits_per_entry = 8 * sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned int map, bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) u64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) tmp = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) bit = do_div(tmp, bits_per_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) map = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) mutex_lock(&sbi->s_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (map >= sbi->s_imap_size || test_and_set_bit(bit, sbi->s_imap[map]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (sbi->s_bitmap_ino > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) bh = sb_bread(sb, clus_to_blk(sbi, sbi->s_bitmap_ino) + map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) set_bit(bit, (unsigned long *)bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) mutex_unlock(&sbi->s_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * Tries to allocate a set of blocks. The request size depends on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * type: for inodes, we must allocate sbi->s_mirrors blocks, and for file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * blocks, we try to allocate sbi->s_clustersize, but can always get away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * with just one block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int omfs_allocate_range(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int min_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int max_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) u64 *return_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int *return_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int bits_per_entry = 8 * sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int i, run, bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) mutex_lock(&sbi->s_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) for (i = 0; i < sbi->s_imap_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) while (bit < bits_per_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) bit = find_next_zero_bit(sbi->s_imap[i], bits_per_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (bit == bits_per_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) run = count_run(&sbi->s_imap[i], bits_per_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) sbi->s_imap_size-i, bit, max_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (run >= min_request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) bit += run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *return_block = (u64) i * bits_per_entry + bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *return_size = run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret = set_run(sb, i, bits_per_entry, bit, run, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) mutex_unlock(&sbi->s_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Clears count bits starting at a given block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int omfs_clear_range(struct super_block *sb, u64 block, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int bits_per_entry = 8 * sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) u64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) unsigned int map, bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) tmp = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) bit = do_div(tmp, bits_per_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) map = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (map >= sbi->s_imap_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mutex_lock(&sbi->s_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ret = set_run(sb, map, bits_per_entry, bit, count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) mutex_unlock(&sbi->s_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }