1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
4 #include <linux/buffer_head.h>
8 unsigned long omfs_count_free(struct super_block *sb)
11 unsigned long sum = 0;
12 struct omfs_sb_info *sbi = OMFS_SB(sb);
13 int nbits = sb->s_blocksize * 8;
15 for (i = 0; i < sbi->s_imap_size; i++)
16 sum += nbits - bitmap_weight(sbi->s_imap[i], nbits);
22 * Counts the run of zero bits starting at bit up to max.
23 * It handles the case where a run might spill over a buffer.
24 * Called with bitmap lock.
26 static int count_run(unsigned long **addr, int nbits,
27 int addrlen, int bit, int max)
32 for (; addrlen > 0; addrlen--, addr++) {
33 x = find_next_bit(*addr, nbits, bit);
36 if (x < nbits || count > max)
37 return min(count, max);
41 return min(count, max);
45 * Sets or clears the run of count bits starting with bit.
46 * Called with bitmap lock.
48 static int set_run(struct super_block *sb, int map,
49 int nbits, int bit, int count, int set)
53 struct buffer_head *bh;
54 struct omfs_sb_info *sbi = OMFS_SB(sb);
57 bh = sb_bread(sb, clus_to_blk(sbi, sbi->s_bitmap_ino) + map);
61 for (i = 0; i < count; i++, bit++) {
66 mark_buffer_dirty(bh);
69 clus_to_blk(sbi, sbi->s_bitmap_ino) + map);
74 set_bit(bit, sbi->s_imap[map]);
75 set_bit(bit, (unsigned long *)bh->b_data);
77 clear_bit(bit, sbi->s_imap[map]);
78 clear_bit(bit, (unsigned long *)bh->b_data);
81 mark_buffer_dirty(bh);
89 * Tries to allocate exactly one block. Returns true if successful.
91 int omfs_allocate_block(struct super_block *sb, u64 block)
93 struct buffer_head *bh;
94 struct omfs_sb_info *sbi = OMFS_SB(sb);
95 int bits_per_entry = 8 * sb->s_blocksize;
96 unsigned int map, bit;
101 bit = do_div(tmp, bits_per_entry);
104 mutex_lock(&sbi->s_bitmap_lock);
105 if (map >= sbi->s_imap_size || test_and_set_bit(bit, sbi->s_imap[map]))
108 if (sbi->s_bitmap_ino > 0) {
109 bh = sb_bread(sb, clus_to_blk(sbi, sbi->s_bitmap_ino) + map);
113 set_bit(bit, (unsigned long *)bh->b_data);
114 mark_buffer_dirty(bh);
119 mutex_unlock(&sbi->s_bitmap_lock);
125 * Tries to allocate a set of blocks. The request size depends on the
126 * type: for inodes, we must allocate sbi->s_mirrors blocks, and for file
127 * blocks, we try to allocate sbi->s_clustersize, but can always get away
128 * with just one block.
130 int omfs_allocate_range(struct super_block *sb,
136 struct omfs_sb_info *sbi = OMFS_SB(sb);
137 int bits_per_entry = 8 * sb->s_blocksize;
141 mutex_lock(&sbi->s_bitmap_lock);
142 for (i = 0; i < sbi->s_imap_size; i++) {
144 while (bit < bits_per_entry) {
145 bit = find_next_zero_bit(sbi->s_imap[i], bits_per_entry,
148 if (bit == bits_per_entry)
151 run = count_run(&sbi->s_imap[i], bits_per_entry,
152 sbi->s_imap_size-i, bit, max_request);
154 if (run >= min_request)
163 *return_block = (u64) i * bits_per_entry + bit;
165 ret = set_run(sb, i, bits_per_entry, bit, run, 1);
168 mutex_unlock(&sbi->s_bitmap_lock);
173 * Clears count bits starting at a given block.
175 int omfs_clear_range(struct super_block *sb, u64 block, int count)
177 struct omfs_sb_info *sbi = OMFS_SB(sb);
178 int bits_per_entry = 8 * sb->s_blocksize;
180 unsigned int map, bit;
184 bit = do_div(tmp, bits_per_entry);
187 if (map >= sbi->s_imap_size)
190 mutex_lock(&sbi->s_bitmap_lock);
191 ret = set_run(sb, map, bits_per_entry, bit, count, 0);
192 mutex_unlock(&sbi->s_bitmap_lock);