]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/minix/bitmap.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | /* | |
8 | * Modified for 680x0 by Hamish Macdonald | |
9 | * Fixed for 680x0 by Andreas Schwab | |
10 | */ | |
11 | ||
12 | /* bitmap.c contains the code that handles the inode and block bitmaps */ | |
13 | ||
14 | #include "minix.h" | |
15 | #include <linux/smp_lock.h> | |
16 | #include <linux/buffer_head.h> | |
17 | #include <linux/bitops.h> | |
18 | ||
19 | static int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 }; | |
20 | ||
21 | static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits) | |
22 | { | |
23 | unsigned i, j, sum = 0; | |
24 | struct buffer_head *bh; | |
25 | ||
26 | for (i=0; i<numblocks-1; i++) { | |
27 | if (!(bh=map[i])) | |
28 | return(0); | |
29 | for (j=0; j<BLOCK_SIZE; j++) | |
30 | sum += nibblemap[bh->b_data[j] & 0xf] | |
31 | + nibblemap[(bh->b_data[j]>>4) & 0xf]; | |
32 | } | |
33 | ||
34 | if (numblocks==0 || !(bh=map[numblocks-1])) | |
35 | return(0); | |
36 | i = ((numbits-(numblocks-1)*BLOCK_SIZE*8)/16)*2; | |
37 | for (j=0; j<i; j++) { | |
38 | sum += nibblemap[bh->b_data[j] & 0xf] | |
39 | + nibblemap[(bh->b_data[j]>>4) & 0xf]; | |
40 | } | |
41 | ||
42 | i = numbits%16; | |
43 | if (i!=0) { | |
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]; | |
47 | } | |
48 | return(sum); | |
49 | } | |
50 | ||
51 | void minix_free_block(struct inode * inode, int block) | |
52 | { | |
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; | |
57 | ||
58 | if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) { | |
11b84487 | 59 | printk("Trying to free block not in datazone\n"); |
1da177e4 LT |
60 | return; |
61 | } | |
62 | zone = block - sbi->s_firstdatazone + 1; | |
63 | bit = zone & 8191; | |
64 | zone >>= 13; | |
65 | if (zone >= sbi->s_zmap_blocks) { | |
66 | printk("minix_free_block: nonexistent bitmap buffer\n"); | |
67 | return; | |
68 | } | |
69 | bh = sbi->s_zmap[zone]; | |
70 | lock_kernel(); | |
71 | if (!minix_test_and_clear_bit(bit,bh->b_data)) | |
72 | printk("free_block (%s:%d): bit already cleared\n", | |
73 | sb->s_id, block); | |
74 | unlock_kernel(); | |
75 | mark_buffer_dirty(bh); | |
76 | return; | |
77 | } | |
78 | ||
79 | int minix_new_block(struct inode * inode) | |
80 | { | |
81 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); | |
82 | int i; | |
83 | ||
84 | for (i = 0; i < sbi->s_zmap_blocks; i++) { | |
85 | struct buffer_head *bh = sbi->s_zmap[i]; | |
86 | int j; | |
87 | ||
88 | lock_kernel(); | |
89 | if ((j = minix_find_first_zero_bit(bh->b_data, 8192)) < 8192) { | |
90 | minix_set_bit(j,bh->b_data); | |
91 | unlock_kernel(); | |
92 | mark_buffer_dirty(bh); | |
93 | j += i*8192 + sbi->s_firstdatazone-1; | |
94 | if (j < sbi->s_firstdatazone || j >= sbi->s_nzones) | |
95 | break; | |
96 | return j; | |
97 | } | |
98 | unlock_kernel(); | |
99 | } | |
100 | return 0; | |
101 | } | |
102 | ||
103 | unsigned long minix_count_free_blocks(struct minix_sb_info *sbi) | |
104 | { | |
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); | |
108 | } | |
109 | ||
110 | struct minix_inode * | |
111 | minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh) | |
112 | { | |
113 | int block; | |
114 | struct minix_sb_info *sbi = minix_sb(sb); | |
115 | struct minix_inode *p; | |
116 | ||
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); | |
120 | return NULL; | |
121 | } | |
122 | ino--; | |
123 | block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks + | |
124 | ino / MINIX_INODES_PER_BLOCK; | |
125 | *bh = sb_bread(sb, block); | |
126 | if (!*bh) { | |
11b84487 | 127 | printk("Unable to read inode block\n"); |
1da177e4 LT |
128 | return NULL; |
129 | } | |
130 | p = (void *)(*bh)->b_data; | |
131 | return p + ino % MINIX_INODES_PER_BLOCK; | |
132 | } | |
133 | ||
134 | struct minix2_inode * | |
135 | minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh) | |
136 | { | |
137 | int block; | |
138 | struct minix_sb_info *sbi = minix_sb(sb); | |
139 | struct minix2_inode *p; | |
140 | ||
141 | *bh = NULL; | |
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); | |
145 | return NULL; | |
146 | } | |
147 | ino--; | |
148 | block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks + | |
149 | ino / MINIX2_INODES_PER_BLOCK; | |
150 | *bh = sb_bread(sb, block); | |
151 | if (!*bh) { | |
11b84487 | 152 | printk("Unable to read inode block\n"); |
1da177e4 LT |
153 | return NULL; |
154 | } | |
155 | p = (void *)(*bh)->b_data; | |
156 | return p + ino % MINIX2_INODES_PER_BLOCK; | |
157 | } | |
158 | ||
159 | /* Clear the link count and mode of a deleted inode on disk. */ | |
160 | ||
161 | static void minix_clear_inode(struct inode *inode) | |
162 | { | |
163 | struct buffer_head *bh = NULL; | |
164 | ||
165 | if (INODE_VERSION(inode) == MINIX_V1) { | |
166 | struct minix_inode *raw_inode; | |
167 | raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh); | |
168 | if (raw_inode) { | |
169 | raw_inode->i_nlinks = 0; | |
170 | raw_inode->i_mode = 0; | |
171 | } | |
172 | } else { | |
173 | struct minix2_inode *raw_inode; | |
174 | raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh); | |
175 | if (raw_inode) { | |
176 | raw_inode->i_nlinks = 0; | |
177 | raw_inode->i_mode = 0; | |
178 | } | |
179 | } | |
180 | if (bh) { | |
181 | mark_buffer_dirty(bh); | |
182 | brelse (bh); | |
183 | } | |
184 | } | |
185 | ||
186 | void minix_free_inode(struct inode * inode) | |
187 | { | |
188 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); | |
189 | struct buffer_head * bh; | |
190 | unsigned long ino; | |
191 | ||
192 | ino = inode->i_ino; | |
193 | if (ino < 1 || ino > sbi->s_ninodes) { | |
194 | printk("minix_free_inode: inode 0 or nonexistent inode\n"); | |
195 | goto out; | |
196 | } | |
197 | if ((ino >> 13) >= sbi->s_imap_blocks) { | |
198 | printk("minix_free_inode: nonexistent imap in superblock\n"); | |
199 | goto out; | |
200 | } | |
201 | ||
202 | minix_clear_inode(inode); /* clear on-disk copy */ | |
203 | ||
204 | bh = sbi->s_imap[ino >> 13]; | |
205 | lock_kernel(); | |
206 | if (!minix_test_and_clear_bit(ino & 8191, bh->b_data)) | |
11b84487 | 207 | printk("minix_free_inode: bit %lu already cleared\n", ino); |
1da177e4 LT |
208 | unlock_kernel(); |
209 | mark_buffer_dirty(bh); | |
210 | out: | |
211 | clear_inode(inode); /* clear in-memory copy */ | |
212 | } | |
213 | ||
214 | struct inode * minix_new_inode(const struct inode * dir, int * error) | |
215 | { | |
216 | struct super_block *sb = dir->i_sb; | |
217 | struct minix_sb_info *sbi = minix_sb(sb); | |
218 | struct inode *inode = new_inode(sb); | |
219 | struct buffer_head * bh; | |
220 | int i,j; | |
221 | ||
222 | if (!inode) { | |
223 | *error = -ENOMEM; | |
224 | return NULL; | |
225 | } | |
226 | j = 8192; | |
227 | bh = NULL; | |
228 | *error = -ENOSPC; | |
229 | lock_kernel(); | |
230 | for (i = 0; i < sbi->s_imap_blocks; i++) { | |
231 | bh = sbi->s_imap[i]; | |
232 | if ((j = minix_find_first_zero_bit(bh->b_data, 8192)) < 8192) | |
233 | break; | |
234 | } | |
235 | if (!bh || j >= 8192) { | |
236 | unlock_kernel(); | |
237 | iput(inode); | |
238 | return NULL; | |
239 | } | |
240 | if (minix_test_and_set_bit(j,bh->b_data)) { /* shouldn't happen */ | |
11b84487 | 241 | printk("new_inode: bit already set\n"); |
1da177e4 LT |
242 | unlock_kernel(); |
243 | iput(inode); | |
244 | return NULL; | |
245 | } | |
246 | unlock_kernel(); | |
247 | mark_buffer_dirty(bh); | |
248 | j += i*8192; | |
249 | if (!j || j > sbi->s_ninodes) { | |
250 | iput(inode); | |
251 | return NULL; | |
252 | } | |
253 | inode->i_uid = current->fsuid; | |
254 | inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid; | |
255 | inode->i_ino = j; | |
256 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; | |
ba52de12 | 257 | inode->i_blocks = 0; |
1da177e4 LT |
258 | memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u)); |
259 | insert_inode_hash(inode); | |
260 | mark_inode_dirty(inode); | |
261 | ||
262 | *error = 0; | |
263 | return inode; | |
264 | } | |
265 | ||
266 | unsigned long minix_count_free_inodes(struct minix_sb_info *sbi) | |
267 | { | |
268 | return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1); | |
269 | } |