This source file includes following definitions.
- count_used
- minix_free_block
- minix_new_block
- minix_count_free_blocks
- minix_free_inode
- minix_new_inode
- minix_count_free_inodes
1
2
3
4
5
6
7
8
9 #include <linux/sched.h>
10 #include <linux/minix_fs.h>
11 #include <linux/stat.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14
15 #include <asm/bitops.h>
16
17 #define clear_block(addr) \
18 __asm__("cld\n\t" \
19 "rep\n\t" \
20 "stosl" \
21 ::"a" (0),"c" (BLOCK_SIZE/4),"D" ((long) (addr)):"cx","di")
22
23 #define find_first_zero(addr) ({ \
24 int __res; \
25 __asm__("cld\n" \
26 "1:\tlodsl\n\t" \
27 "notl %%eax\n\t" \
28 "bsfl %%eax,%%edx\n\t" \
29 "jne 2f\n\t" \
30 "addl $32,%%ecx\n\t" \
31 "cmpl $8192,%%ecx\n\t" \
32 "jl 1b\n\t" \
33 "xorl %%edx,%%edx\n" \
34 "2:\taddl %%edx,%%ecx" \
35 :"=c" (__res):"0" (0),"S" (addr):"ax","dx","si"); \
36 __res;})
37
38 static int nibblemap[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 };
39
40 static unsigned long count_used(struct buffer_head *map[], unsigned numblocks,
41 unsigned numbits)
42 {
43 unsigned i, j, end, sum = 0;
44 struct buffer_head *bh;
45
46 for (i=0; (i<numblocks) && numbits; i++) {
47 if (!(bh=map[i]))
48 return(0);
49 if (numbits >= (8*BLOCK_SIZE)) {
50 end = BLOCK_SIZE;
51 numbits -= 8*BLOCK_SIZE;
52 } else {
53 int tmp;
54 end = numbits >> 3;
55 numbits &= 0x7;
56 tmp = bh->b_data[end] & ((1<<numbits)-1);
57 sum += nibblemap[tmp&0xf] + nibblemap[(tmp>>4)&0xf];
58 numbits = 0;
59 }
60 for (j=0; j<end; j++)
61 sum += nibblemap[bh->b_data[j] & 0xf]
62 + nibblemap[(bh->b_data[j]>>4)&0xf];
63 }
64 return(sum);
65 }
66
67 void minix_free_block(struct super_block * sb, int block)
68 {
69 struct buffer_head * bh;
70 unsigned int bit,zone;
71
72 if (!sb) {
73 printk("trying to free block on nonexistent device\n");
74 return;
75 }
76 if (block < sb->u.minix_sb.s_firstdatazone ||
77 block >= sb->u.minix_sb.s_nzones) {
78 printk("trying to free block not in datazone\n");
79 return;
80 }
81 bh = get_hash_table(sb->s_dev,block,BLOCK_SIZE);
82 if (bh)
83 bh->b_dirt=0;
84 brelse(bh);
85 zone = block - sb->u.minix_sb.s_firstdatazone + 1;
86 bit = zone & 8191;
87 zone >>= 13;
88 bh = sb->u.minix_sb.s_zmap[zone];
89 if (!bh) {
90 printk("minix_free_block: nonexistent bitmap buffer\n");
91 return;
92 }
93 if (clear_bit(bit,bh->b_data))
94 printk("free_block (%04x:%d): bit already cleared\n",sb->s_dev,block);
95 bh->b_dirt = 1;
96 return;
97 }
98
99 int minix_new_block(struct super_block * sb)
100 {
101 struct buffer_head * bh;
102 int i,j;
103
104 if (!sb) {
105 printk("trying to get new block from nonexistant device\n");
106 return 0;
107 }
108 repeat:
109 j = 8192;
110 for (i=0 ; i<8 ; i++)
111 if ((bh=sb->u.minix_sb.s_zmap[i]) != NULL)
112 if ((j=find_first_zero(bh->b_data))<8192)
113 break;
114 if (i>=8 || !bh || j>=8192)
115 return 0;
116 if (set_bit(j,bh->b_data)) {
117 printk("new_block: bit already set");
118 goto repeat;
119 }
120 bh->b_dirt = 1;
121 j += i*8192 + sb->u.minix_sb.s_firstdatazone-1;
122 if (j < sb->u.minix_sb.s_firstdatazone ||
123 j >= sb->u.minix_sb.s_nzones)
124 return 0;
125 if (!(bh = getblk(sb->s_dev,j,BLOCK_SIZE))) {
126 printk("new_block: cannot get block");
127 return 0;
128 }
129 if (bh->b_count != 1) {
130 printk("new block: count is != 1");
131 return 0;
132 }
133 clear_block(bh->b_data);
134 bh->b_uptodate = 1;
135 bh->b_dirt = 1;
136 brelse(bh);
137 return j;
138 }
139
140 unsigned long minix_count_free_blocks(struct super_block *sb)
141 {
142 return (sb->u.minix_sb.s_nzones - count_used(sb->u.minix_sb.s_zmap,sb->u.minix_sb.s_zmap_blocks,sb->u.minix_sb.s_nzones))
143 << sb->u.minix_sb.s_log_zone_size;
144 }
145
146 void minix_free_inode(struct inode * inode)
147 {
148 struct buffer_head * bh;
149
150 if (!inode)
151 return;
152 if (!inode->i_dev) {
153 printk("free_inode: inode has no device\n");
154 return;
155 }
156 if (inode->i_count != 1) {
157 printk("free_inode: inode has count=%d\n",inode->i_count);
158 return;
159 }
160 if (inode->i_nlink) {
161 printk("free_inode: inode has nlink=%d\n",inode->i_nlink);
162 return;
163 }
164 if (!inode->i_sb) {
165 printk("free_inode: inode on nonexistent device\n");
166 return;
167 }
168 if (inode->i_ino < 1 || inode->i_ino >= inode->i_sb->u.minix_sb.s_ninodes) {
169 printk("free_inode: inode 0 or nonexistent inode\n");
170 return;
171 }
172 if (!(bh=inode->i_sb->u.minix_sb.s_imap[inode->i_ino>>13])) {
173 printk("free_inode: nonexistent imap in superblock\n");
174 return;
175 }
176 if (clear_bit(inode->i_ino&8191,bh->b_data))
177 printk("free_inode: bit %d already cleared.\n",inode->i_ino);
178 bh->b_dirt = 1;
179 clear_inode(inode);
180 }
181
182 struct inode * minix_new_inode(const struct inode * dir)
183 {
184 struct super_block * sb;
185 struct inode * inode;
186 struct buffer_head * bh;
187 int i,j;
188
189 if (!dir || !(inode = get_empty_inode()))
190 return NULL;
191 sb = dir->i_sb;
192 inode->i_sb = sb;
193 inode->i_flags = inode->i_sb->s_flags;
194 j = 8192;
195 for (i=0 ; i<8 ; i++)
196 if ((bh = inode->i_sb->u.minix_sb.s_imap[i]) != NULL)
197 if ((j=find_first_zero(bh->b_data))<8192)
198 break;
199 if (!bh || j >= 8192) {
200 iput(inode);
201 return NULL;
202 }
203 if (set_bit(j,bh->b_data)) {
204 printk("new_inode: bit already set");
205 iput(inode);
206 return NULL;
207 }
208 bh->b_dirt = 1;
209 j += i*8192;
210 if (!j || j >= inode->i_sb->u.minix_sb.s_ninodes) {
211 iput(inode);
212 return NULL;
213 }
214 inode->i_count = 1;
215 inode->i_nlink = 1;
216 inode->i_dev = sb->s_dev;
217 inode->i_uid = current->euid;
218 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->egid;
219 inode->i_dirt = 1;
220 inode->i_ino = j;
221 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
222 inode->i_op = NULL;
223 inode->i_blocks = inode->i_blksize = 0;
224 return inode;
225 }
226
227 unsigned long minix_count_free_inodes(struct super_block *sb)
228 {
229 return sb->u.minix_sb.s_ninodes - count_used(sb->u.minix_sb.s_imap,sb->u.minix_sb.s_imap_blocks,sb->u.minix_sb.s_ninodes);
230 }