2 * linux/fs/minix/bitmap.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Modified for 680x0 by Hamish Macdonald
9 * Fixed for 680x0 by Andreas Schwab
12 /* bitmap.c contains the code that handles the inode and block bitmaps */
15 #include <linux/buffer_head.h>
16 #include <linux/bitops.h>
17 #include <linux/sched.h>
19 static const int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
21 static DEFINE_SPINLOCK(bitmap_lock);
23 static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits)
25 unsigned i, j, sum = 0;
26 struct buffer_head *bh;
28 for (i=0; i<numblocks-1; i++) {
31 for (j=0; j<bh->b_size; j++)
32 sum += nibblemap[bh->b_data[j] & 0xf]
33 + nibblemap[(bh->b_data[j]>>4) & 0xf];
36 if (numblocks==0 || !(bh=map[numblocks-1]))
38 i = ((numbits - (numblocks-1) * bh->b_size * 8) / 16) * 2;
40 sum += nibblemap[bh->b_data[j] & 0xf]
41 + nibblemap[(bh->b_data[j]>>4) & 0xf];
46 i = *(__u16 *)(&bh->b_data[j]) | ~((1<<i) - 1);
47 sum += nibblemap[i & 0xf] + nibblemap[(i>>4) & 0xf];
48 sum += nibblemap[(i>>8) & 0xf] + nibblemap[(i>>12) & 0xf];
53 void minix_free_block(struct inode *inode, unsigned long block)
55 struct super_block *sb = inode->i_sb;
56 struct minix_sb_info *sbi = minix_sb(sb);
57 struct buffer_head *bh;
58 int k = sb->s_blocksize_bits + 3;
59 unsigned long bit, zone;
61 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
62 printk("Trying to free block not in datazone\n");
65 zone = block - sbi->s_firstdatazone + 1;
66 bit = zone & ((1<<k) - 1);
68 if (zone >= sbi->s_zmap_blocks) {
69 printk("minix_free_block: nonexistent bitmap buffer\n");
72 bh = sbi->s_zmap[zone];
73 spin_lock(&bitmap_lock);
74 if (!minix_test_and_clear_bit(bit, bh->b_data))
75 printk("minix_free_block (%s:%lu): bit already cleared\n",
77 spin_unlock(&bitmap_lock);
78 mark_buffer_dirty(bh);
82 int minix_new_block(struct inode * inode)
84 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
85 int bits_per_zone = 8 * inode->i_sb->s_blocksize;
88 for (i = 0; i < sbi->s_zmap_blocks; i++) {
89 struct buffer_head *bh = sbi->s_zmap[i];
92 spin_lock(&bitmap_lock);
93 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
94 if (j < bits_per_zone) {
95 minix_set_bit(j, bh->b_data);
96 spin_unlock(&bitmap_lock);
97 mark_buffer_dirty(bh);
98 j += i * bits_per_zone + sbi->s_firstdatazone-1;
99 if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
103 spin_unlock(&bitmap_lock);
108 unsigned long minix_count_free_blocks(struct minix_sb_info *sbi)
110 return (count_free(sbi->s_zmap, sbi->s_zmap_blocks,
111 sbi->s_nzones - sbi->s_firstdatazone + 1)
112 << sbi->s_log_zone_size);
116 minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
119 struct minix_sb_info *sbi = minix_sb(sb);
120 struct minix_inode *p;
122 if (!ino || ino > sbi->s_ninodes) {
123 printk("Bad inode number on dev %s: %ld is out of range\n",
124 sb->s_id, (long)ino);
128 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
129 ino / MINIX_INODES_PER_BLOCK;
130 *bh = sb_bread(sb, block);
132 printk("Unable to read inode block\n");
135 p = (void *)(*bh)->b_data;
136 return p + ino % MINIX_INODES_PER_BLOCK;
139 struct minix2_inode *
140 minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
143 struct minix_sb_info *sbi = minix_sb(sb);
144 struct minix2_inode *p;
145 int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
148 if (!ino || ino > sbi->s_ninodes) {
149 printk("Bad inode number on dev %s: %ld is out of range\n",
150 sb->s_id, (long)ino);
154 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
155 ino / minix2_inodes_per_block;
156 *bh = sb_bread(sb, block);
158 printk("Unable to read inode block\n");
161 p = (void *)(*bh)->b_data;
162 return p + ino % minix2_inodes_per_block;
165 /* Clear the link count and mode of a deleted inode on disk. */
167 static void minix_clear_inode(struct inode *inode)
169 struct buffer_head *bh = NULL;
171 if (INODE_VERSION(inode) == MINIX_V1) {
172 struct minix_inode *raw_inode;
173 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
175 raw_inode->i_nlinks = 0;
176 raw_inode->i_mode = 0;
179 struct minix2_inode *raw_inode;
180 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
182 raw_inode->i_nlinks = 0;
183 raw_inode->i_mode = 0;
187 mark_buffer_dirty(bh);
192 void minix_free_inode(struct inode * inode)
194 struct super_block *sb = inode->i_sb;
195 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
196 struct buffer_head *bh;
197 int k = sb->s_blocksize_bits + 3;
198 unsigned long ino, bit;
201 if (ino < 1 || ino > sbi->s_ninodes) {
202 printk("minix_free_inode: inode 0 or nonexistent inode\n");
205 bit = ino & ((1<<k) - 1);
207 if (ino >= sbi->s_imap_blocks) {
208 printk("minix_free_inode: nonexistent imap in superblock\n");
212 minix_clear_inode(inode); /* clear on-disk copy */
214 bh = sbi->s_imap[ino];
215 spin_lock(&bitmap_lock);
216 if (!minix_test_and_clear_bit(bit, bh->b_data))
217 printk("minix_free_inode: bit %lu already cleared\n", bit);
218 spin_unlock(&bitmap_lock);
219 mark_buffer_dirty(bh);
221 clear_inode(inode); /* clear in-memory copy */
224 struct inode * minix_new_inode(const struct inode * dir, int * error)
226 struct super_block *sb = dir->i_sb;
227 struct minix_sb_info *sbi = minix_sb(sb);
228 struct inode *inode = new_inode(sb);
229 struct buffer_head * bh;
230 int bits_per_zone = 8 * sb->s_blocksize;
241 spin_lock(&bitmap_lock);
242 for (i = 0; i < sbi->s_imap_blocks; i++) {
244 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
245 if (j < bits_per_zone)
248 if (!bh || j >= bits_per_zone) {
249 spin_unlock(&bitmap_lock);
253 if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
254 spin_unlock(&bitmap_lock);
255 printk("minix_new_inode: bit already set\n");
259 spin_unlock(&bitmap_lock);
260 mark_buffer_dirty(bh);
261 j += i * bits_per_zone;
262 if (!j || j > sbi->s_ninodes) {
266 inode->i_uid = current_fsuid();
267 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current_fsgid();
269 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
271 memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
272 insert_inode_hash(inode);
273 mark_inode_dirty(inode);
279 unsigned long minix_count_free_inodes(struct minix_sb_info *sbi)
281 return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1);