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 <asm/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
<BLOCK_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)*BLOCK_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
, int block
)
53 struct super_block
* sb
= inode
->i_sb
;
54 struct minix_sb_info
* sbi
= minix_sb(sb
);
55 struct buffer_head
* bh
;
56 unsigned int bit
,zone
;
58 if (block
< sbi
->s_firstdatazone
|| block
>= sbi
->s_nzones
) {
59 printk("trying to free block not in datazone\n");
62 zone
= block
- sbi
->s_firstdatazone
+ 1;
65 if (zone
>= sbi
->s_zmap_blocks
) {
66 printk("minix_free_block: nonexistent bitmap buffer\n");
69 bh
= sbi
->s_zmap
[zone
];
71 if (!minix_test_and_clear_bit(bit
,bh
->b_data
))
72 printk("free_block (%s:%d): bit already cleared\n",
75 mark_buffer_dirty(bh
);
79 int minix_new_block(struct inode
* inode
)
81 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
84 for (i
= 0; i
< sbi
->s_zmap_blocks
; i
++) {
85 struct buffer_head
*bh
= sbi
->s_zmap
[i
];
89 if ((j
= minix_find_first_zero_bit(bh
->b_data
, 8192)) < 8192) {
90 minix_set_bit(j
,bh
->b_data
);
92 mark_buffer_dirty(bh
);
93 j
+= i
*8192 + sbi
->s_firstdatazone
-1;
94 if (j
< sbi
->s_firstdatazone
|| j
>= sbi
->s_nzones
)
103 unsigned long minix_count_free_blocks(struct minix_sb_info
*sbi
)
105 return (count_free(sbi
->s_zmap
, sbi
->s_zmap_blocks
,
106 sbi
->s_nzones
- sbi
->s_firstdatazone
+ 1)
107 << sbi
->s_log_zone_size
);
111 minix_V1_raw_inode(struct super_block
*sb
, ino_t ino
, struct buffer_head
**bh
)
114 struct minix_sb_info
*sbi
= minix_sb(sb
);
115 struct minix_inode
*p
;
117 if (!ino
|| ino
> sbi
->s_ninodes
) {
118 printk("Bad inode number on dev %s: %ld is out of range\n",
119 sb
->s_id
, (long)ino
);
123 block
= 2 + sbi
->s_imap_blocks
+ sbi
->s_zmap_blocks
+
124 ino
/ MINIX_INODES_PER_BLOCK
;
125 *bh
= sb_bread(sb
, block
);
127 printk("unable to read i-node block\n");
130 p
= (void *)(*bh
)->b_data
;
131 return p
+ ino
% MINIX_INODES_PER_BLOCK
;
134 struct minix2_inode
*
135 minix_V2_raw_inode(struct super_block
*sb
, ino_t ino
, struct buffer_head
**bh
)
138 struct minix_sb_info
*sbi
= minix_sb(sb
);
139 struct minix2_inode
*p
;
142 if (!ino
|| ino
> sbi
->s_ninodes
) {
143 printk("Bad inode number on dev %s: %ld is out of range\n",
144 sb
->s_id
, (long)ino
);
148 block
= 2 + sbi
->s_imap_blocks
+ sbi
->s_zmap_blocks
+
149 ino
/ MINIX2_INODES_PER_BLOCK
;
150 *bh
= sb_bread(sb
, block
);
152 printk("unable to read i-node block\n");
155 p
= (void *)(*bh
)->b_data
;
156 return p
+ ino
% MINIX2_INODES_PER_BLOCK
;
159 /* Clear the link count and mode of a deleted inode on disk. */
161 static void minix_clear_inode(struct inode
*inode
)
163 struct buffer_head
*bh
;
164 if (INODE_VERSION(inode
) == MINIX_V1
) {
165 struct minix_inode
*raw_inode
;
166 raw_inode
= minix_V1_raw_inode(inode
->i_sb
, inode
->i_ino
, &bh
);
168 raw_inode
->i_nlinks
= 0;
169 raw_inode
->i_mode
= 0;
172 struct minix2_inode
*raw_inode
;
173 raw_inode
= minix_V2_raw_inode(inode
->i_sb
, inode
->i_ino
, &bh
);
175 raw_inode
->i_nlinks
= 0;
176 raw_inode
->i_mode
= 0;
180 mark_buffer_dirty(bh
);
185 void minix_free_inode(struct inode
* inode
)
187 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
188 struct buffer_head
* bh
;
191 if (inode
->i_ino
< 1 || inode
->i_ino
> sbi
->s_ninodes
) {
192 printk("free_inode: inode 0 or nonexistent inode\n");
196 if ((ino
>> 13) >= sbi
->s_imap_blocks
) {
197 printk("free_inode: nonexistent imap in superblock\n");
201 bh
= sbi
->s_imap
[ino
>> 13];
202 minix_clear_inode(inode
);
205 if (!minix_test_and_clear_bit(ino
& 8191, bh
->b_data
))
206 printk("free_inode: bit %lu already cleared.\n",ino
);
208 mark_buffer_dirty(bh
);
211 struct inode
* minix_new_inode(const struct inode
* dir
, int * error
)
213 struct super_block
*sb
= dir
->i_sb
;
214 struct minix_sb_info
*sbi
= minix_sb(sb
);
215 struct inode
*inode
= new_inode(sb
);
216 struct buffer_head
* bh
;
227 for (i
= 0; i
< sbi
->s_imap_blocks
; i
++) {
229 if ((j
= minix_find_first_zero_bit(bh
->b_data
, 8192)) < 8192)
232 if (!bh
|| j
>= 8192) {
237 if (minix_test_and_set_bit(j
,bh
->b_data
)) { /* shouldn't happen */
238 printk("new_inode: bit already set");
244 mark_buffer_dirty(bh
);
246 if (!j
|| j
> sbi
->s_ninodes
) {
250 inode
->i_uid
= current
->fsuid
;
251 inode
->i_gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
253 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
254 inode
->i_blocks
= inode
->i_blksize
= 0;
255 memset(&minix_i(inode
)->u
, 0, sizeof(minix_i(inode
)->u
));
256 insert_inode_hash(inode
);
257 mark_inode_dirty(inode
);
263 unsigned long minix_count_free_inodes(struct minix_sb_info
*sbi
)
265 return count_free(sbi
->s_imap
, sbi
->s_imap_blocks
, sbi
->s_ninodes
+ 1);