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/smp_lock.h>
16 #include <linux/buffer_head.h>
17 #include <linux/bitops.h>
19 static int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
21 static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits)
23 unsigned i, j, sum = 0;
24 struct buffer_head *bh;
26 for (i=0; i<numblocks-1; i++) {
29 for (j=0; j<bh->b_size; j++)
30 sum += nibblemap[bh->b_data[j] & 0xf]
31 + nibblemap[(bh->b_data[j]>>4) & 0xf];
34 if (numblocks==0 || !(bh=map[numblocks-1]))
36 i = ((numbits - (numblocks-1) * bh->b_size * 8) / 16) * 2;
38 sum += nibblemap[bh->b_data[j] & 0xf]
39 + nibblemap[(bh->b_data[j]>>4) & 0xf];
44 i = *(__u16 *)(&bh->b_data[j]) | ~((1<<i) - 1);
45 sum += nibblemap[i & 0xf] + nibblemap[(i>>4) & 0xf];
46 sum += nibblemap[(i>>8) & 0xf] + nibblemap[(i>>12) & 0xf];
51 void minix_free_block(struct inode *inode, unsigned long block)
53 struct super_block *sb = inode->i_sb;
54 struct minix_sb_info *sbi = minix_sb(sb);
55 struct buffer_head *bh;
56 int k = sb->s_blocksize_bits + 3;
57 unsigned long bit, zone;
59 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
60 printk("Trying to free block not in datazone\n");
63 zone = block - sbi->s_firstdatazone + 1;
64 bit = zone & ((1<<k) - 1);
66 if (zone >= sbi->s_zmap_blocks) {
67 printk("minix_free_block: nonexistent bitmap buffer\n");
70 bh = sbi->s_zmap[zone];
72 if (!minix_test_and_clear_bit(bit, bh->b_data))
73 printk("minix_free_block (%s:%lu): bit already cleared\n",
76 mark_buffer_dirty(bh);
80 int minix_new_block(struct inode * inode)
82 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
83 int bits_per_zone = 8 * inode->i_sb->s_blocksize;
86 for (i = 0; i < sbi->s_zmap_blocks; i++) {
87 struct buffer_head *bh = sbi->s_zmap[i];
91 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
92 if (j < bits_per_zone) {
93 minix_set_bit(j, bh->b_data);
95 mark_buffer_dirty(bh);
96 j += i * bits_per_zone + sbi->s_firstdatazone-1;
97 if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
106 unsigned long minix_count_free_blocks(struct minix_sb_info *sbi)
108 return (count_free(sbi->s_zmap, sbi->s_zmap_blocks,
109 sbi->s_nzones - sbi->s_firstdatazone + 1)
110 << sbi->s_log_zone_size);
114 minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
117 struct minix_sb_info *sbi = minix_sb(sb);
118 struct minix_inode *p;
120 if (!ino || ino > sbi->s_ninodes) {
121 printk("Bad inode number on dev %s: %ld is out of range\n",
122 sb->s_id, (long)ino);
126 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
127 ino / MINIX_INODES_PER_BLOCK;
128 *bh = sb_bread(sb, block);
130 printk("Unable to read inode block\n");
133 p = (void *)(*bh)->b_data;
134 return p + ino % MINIX_INODES_PER_BLOCK;
137 struct minix2_inode *
138 minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
141 struct minix_sb_info *sbi = minix_sb(sb);
142 struct minix2_inode *p;
143 int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
146 if (!ino || ino > sbi->s_ninodes) {
147 printk("Bad inode number on dev %s: %ld is out of range\n",
148 sb->s_id, (long)ino);
152 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
153 ino / minix2_inodes_per_block;
154 *bh = sb_bread(sb, block);
156 printk("Unable to read inode block\n");
159 p = (void *)(*bh)->b_data;
160 return p + ino % minix2_inodes_per_block;
163 /* Clear the link count and mode of a deleted inode on disk. */
165 static void minix_clear_inode(struct inode *inode)
167 struct buffer_head *bh = NULL;
169 if (INODE_VERSION(inode) == MINIX_V1) {
170 struct minix_inode *raw_inode;
171 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
173 raw_inode->i_nlinks = 0;
174 raw_inode->i_mode = 0;
177 struct minix2_inode *raw_inode;
178 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
180 raw_inode->i_nlinks = 0;
181 raw_inode->i_mode = 0;
185 mark_buffer_dirty(bh);
190 void minix_free_inode(struct inode * inode)
192 struct super_block *sb = inode->i_sb;
193 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
194 struct buffer_head *bh;
195 int k = sb->s_blocksize_bits + 3;
196 unsigned long ino, bit;
199 if (ino < 1 || ino > sbi->s_ninodes) {
200 printk("minix_free_inode: inode 0 or nonexistent inode\n");
203 bit = ino & ((1<<k) - 1);
205 if (ino >= sbi->s_imap_blocks) {
206 printk("minix_free_inode: nonexistent imap in superblock\n");
210 minix_clear_inode(inode); /* clear on-disk copy */
212 bh = sbi->s_imap[ino];
214 if (!minix_test_and_clear_bit(bit, bh->b_data))
215 printk("minix_free_inode: bit %lu already cleared\n", bit);
217 mark_buffer_dirty(bh);
219 clear_inode(inode); /* clear in-memory copy */
222 struct inode * minix_new_inode(const struct inode * dir, int * error)
224 struct super_block *sb = dir->i_sb;
225 struct minix_sb_info *sbi = minix_sb(sb);
226 struct inode *inode = new_inode(sb);
227 struct buffer_head * bh;
228 int bits_per_zone = 8 * sb->s_blocksize;
240 for (i = 0; i < sbi->s_imap_blocks; i++) {
242 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
243 if (j < bits_per_zone)
246 if (!bh || j >= bits_per_zone) {
251 if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
253 printk("minix_new_inode: bit already set\n");
258 mark_buffer_dirty(bh);
259 j += i * bits_per_zone;
260 if (!j || j > sbi->s_ninodes) {
264 inode->i_uid = current->fsuid;
265 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
267 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
269 memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
270 insert_inode_hash(inode);
271 mark_inode_dirty(inode);
277 unsigned long minix_count_free_inodes(struct minix_sb_info *sbi)
279 return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1);