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>
18 #include <linux/sched.h>
20 static int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
22 static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits)
24 unsigned i, j, sum = 0;
25 struct buffer_head *bh;
27 for (i=0; i<numblocks-1; i++) {
30 for (j=0; j<bh->b_size; j++)
31 sum += nibblemap[bh->b_data[j] & 0xf]
32 + nibblemap[(bh->b_data[j]>>4) & 0xf];
35 if (numblocks==0 || !(bh=map[numblocks-1]))
37 i = ((numbits - (numblocks-1) * bh->b_size * 8) / 16) * 2;
39 sum += nibblemap[bh->b_data[j] & 0xf]
40 + nibblemap[(bh->b_data[j]>>4) & 0xf];
45 i = *(__u16 *)(&bh->b_data[j]) | ~((1<<i) - 1);
46 sum += nibblemap[i & 0xf] + nibblemap[(i>>4) & 0xf];
47 sum += nibblemap[(i>>8) & 0xf] + nibblemap[(i>>12) & 0xf];
52 void minix_free_block(struct inode *inode, unsigned long block)
54 struct super_block *sb = inode->i_sb;
55 struct minix_sb_info *sbi = minix_sb(sb);
56 struct buffer_head *bh;
57 int k = sb->s_blocksize_bits + 3;
58 unsigned long bit, zone;
60 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
61 printk("Trying to free block not in datazone\n");
64 zone = block - sbi->s_firstdatazone + 1;
65 bit = zone & ((1<<k) - 1);
67 if (zone >= sbi->s_zmap_blocks) {
68 printk("minix_free_block: nonexistent bitmap buffer\n");
71 bh = sbi->s_zmap[zone];
73 if (!minix_test_and_clear_bit(bit, bh->b_data))
74 printk("minix_free_block (%s:%lu): bit already cleared\n",
77 mark_buffer_dirty(bh);
81 int minix_new_block(struct inode * inode)
83 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
84 int bits_per_zone = 8 * inode->i_sb->s_blocksize;
87 for (i = 0; i < sbi->s_zmap_blocks; i++) {
88 struct buffer_head *bh = sbi->s_zmap[i];
92 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
93 if (j < bits_per_zone) {
94 minix_set_bit(j, bh->b_data);
96 mark_buffer_dirty(bh);
97 j += i * bits_per_zone + sbi->s_firstdatazone-1;
98 if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
107 unsigned long minix_count_free_blocks(struct minix_sb_info *sbi)
109 return (count_free(sbi->s_zmap, sbi->s_zmap_blocks,
110 sbi->s_nzones - sbi->s_firstdatazone + 1)
111 << sbi->s_log_zone_size);
115 minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
118 struct minix_sb_info *sbi = minix_sb(sb);
119 struct minix_inode *p;
121 if (!ino || ino > sbi->s_ninodes) {
122 printk("Bad inode number on dev %s: %ld is out of range\n",
123 sb->s_id, (long)ino);
127 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
128 ino / MINIX_INODES_PER_BLOCK;
129 *bh = sb_bread(sb, block);
131 printk("Unable to read inode block\n");
134 p = (void *)(*bh)->b_data;
135 return p + ino % MINIX_INODES_PER_BLOCK;
138 struct minix2_inode *
139 minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
142 struct minix_sb_info *sbi = minix_sb(sb);
143 struct minix2_inode *p;
144 int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
147 if (!ino || ino > sbi->s_ninodes) {
148 printk("Bad inode number on dev %s: %ld is out of range\n",
149 sb->s_id, (long)ino);
153 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
154 ino / minix2_inodes_per_block;
155 *bh = sb_bread(sb, block);
157 printk("Unable to read inode block\n");
160 p = (void *)(*bh)->b_data;
161 return p + ino % minix2_inodes_per_block;
164 /* Clear the link count and mode of a deleted inode on disk. */
166 static void minix_clear_inode(struct inode *inode)
168 struct buffer_head *bh = NULL;
170 if (INODE_VERSION(inode) == MINIX_V1) {
171 struct minix_inode *raw_inode;
172 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
174 raw_inode->i_nlinks = 0;
175 raw_inode->i_mode = 0;
178 struct minix2_inode *raw_inode;
179 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
181 raw_inode->i_nlinks = 0;
182 raw_inode->i_mode = 0;
186 mark_buffer_dirty(bh);
191 void minix_free_inode(struct inode * inode)
193 struct super_block *sb = inode->i_sb;
194 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
195 struct buffer_head *bh;
196 int k = sb->s_blocksize_bits + 3;
197 unsigned long ino, bit;
200 if (ino < 1 || ino > sbi->s_ninodes) {
201 printk("minix_free_inode: inode 0 or nonexistent inode\n");
204 bit = ino & ((1<<k) - 1);
206 if (ino >= sbi->s_imap_blocks) {
207 printk("minix_free_inode: nonexistent imap in superblock\n");
211 minix_clear_inode(inode); /* clear on-disk copy */
213 bh = sbi->s_imap[ino];
215 if (!minix_test_and_clear_bit(bit, bh->b_data))
216 printk("minix_free_inode: bit %lu already cleared\n", bit);
218 mark_buffer_dirty(bh);
220 clear_inode(inode); /* clear in-memory copy */
223 struct inode * minix_new_inode(const struct inode * dir, int * error)
225 struct super_block *sb = dir->i_sb;
226 struct minix_sb_info *sbi = minix_sb(sb);
227 struct inode *inode = new_inode(sb);
228 struct buffer_head * bh;
229 int bits_per_zone = 8 * sb->s_blocksize;
241 for (i = 0; i < sbi->s_imap_blocks; i++) {
243 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
244 if (j < bits_per_zone)
247 if (!bh || j >= bits_per_zone) {
252 if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
254 printk("minix_new_inode: bit already set\n");
259 mark_buffer_dirty(bh);
260 j += i * bits_per_zone;
261 if (!j || j > sbi->s_ninodes) {
265 inode->i_uid = current->fsuid;
266 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
268 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
270 memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
271 insert_inode_hash(inode);
272 mark_inode_dirty(inode);
278 unsigned long minix_count_free_inodes(struct minix_sb_info *sbi)
280 return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1);