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 static int nibblemap[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 };
18
19 static unsigned long count_used(struct buffer_head *map[], unsigned numblocks,
20 unsigned numbits)
21 {
22 unsigned i, j, end, sum = 0;
23 struct buffer_head *bh;
24
25 for (i=0; (i<numblocks) && numbits; i++) {
26 if (!(bh=map[i]))
27 return(0);
28 if (numbits >= (8*BLOCK_SIZE)) {
29 end = BLOCK_SIZE;
30 numbits -= 8*BLOCK_SIZE;
31 } else {
32 int tmp;
33 end = numbits >> 3;
34 numbits &= 0x7;
35 tmp = bh->b_data[end] & ((1<<numbits)-1);
36 sum += nibblemap[tmp&0xf] + nibblemap[(tmp>>4)&0xf];
37 numbits = 0;
38 }
39 for (j=0; j<end; j++)
40 sum += nibblemap[bh->b_data[j] & 0xf]
41 + nibblemap[(bh->b_data[j]>>4)&0xf];
42 }
43 return(sum);
44 }
45
46 void minix_free_block(struct super_block * sb, int block)
47 {
48 struct buffer_head * bh;
49 unsigned int bit,zone;
50
51 if (!sb) {
52 printk("trying to free block on nonexistent device\n");
53 return;
54 }
55 if (block < sb->u.minix_sb.s_firstdatazone ||
56 block >= sb->u.minix_sb.s_nzones) {
57 printk("trying to free block not in datazone\n");
58 return;
59 }
60 bh = get_hash_table(sb->s_dev,block,BLOCK_SIZE);
61 if (bh)
62 bh->b_dirt=0;
63 brelse(bh);
64 zone = block - sb->u.minix_sb.s_firstdatazone + 1;
65 bit = zone & 8191;
66 zone >>= 13;
67 bh = sb->u.minix_sb.s_zmap[zone];
68 if (!bh) {
69 printk("minix_free_block: nonexistent bitmap buffer\n");
70 return;
71 }
72 if (!clear_bit(bit,bh->b_data))
73 printk("free_block (%04x:%d): bit already cleared\n",sb->s_dev,block);
74 mark_buffer_dirty(bh, 1);
75 return;
76 }
77
78 int minix_new_block(struct super_block * sb)
79 {
80 struct buffer_head * bh;
81 int i,j;
82
83 if (!sb) {
84 printk("trying to get new block from nonexistent device\n");
85 return 0;
86 }
87 repeat:
88 j = 8192;
89 for (i=0 ; i<8 ; i++)
90 if ((bh=sb->u.minix_sb.s_zmap[i]) != NULL)
91 if ((j=find_first_zero_bit(bh->b_data, 8192)) < 8192)
92 break;
93 if (i>=8 || !bh || j>=8192)
94 return 0;
95 if (set_bit(j,bh->b_data)) {
96 printk("new_block: bit already set");
97 goto repeat;
98 }
99 mark_buffer_dirty(bh, 1);
100 j += i*8192 + sb->u.minix_sb.s_firstdatazone-1;
101 if (j < sb->u.minix_sb.s_firstdatazone ||
102 j >= sb->u.minix_sb.s_nzones)
103 return 0;
104 if (!(bh = getblk(sb->s_dev,j,BLOCK_SIZE))) {
105 printk("new_block: cannot get block");
106 return 0;
107 }
108 memset(bh->b_data, 0, BLOCK_SIZE);
109 bh->b_uptodate = 1;
110 mark_buffer_dirty(bh, 1);
111 brelse(bh);
112 return j;
113 }
114
115 unsigned long minix_count_free_blocks(struct super_block *sb)
116 {
117 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))
118 << sb->u.minix_sb.s_log_zone_size;
119 }
120
121 void minix_free_inode(struct inode * inode)
122 {
123 struct buffer_head * bh;
124 unsigned long ino;
125
126 if (!inode)
127 return;
128 if (!inode->i_dev) {
129 printk("free_inode: inode has no device\n");
130 return;
131 }
132 if (inode->i_count != 1) {
133 printk("free_inode: inode has count=%d\n",inode->i_count);
134 return;
135 }
136 if (inode->i_nlink) {
137 printk("free_inode: inode has nlink=%d\n",inode->i_nlink);
138 return;
139 }
140 if (!inode->i_sb) {
141 printk("free_inode: inode on nonexistent device\n");
142 return;
143 }
144 if (inode->i_ino < 1 || inode->i_ino >= inode->i_sb->u.minix_sb.s_ninodes) {
145 printk("free_inode: inode 0 or nonexistent inode\n");
146 return;
147 }
148 ino = inode->i_ino;
149 if (!(bh=inode->i_sb->u.minix_sb.s_imap[ino >> 13])) {
150 printk("free_inode: nonexistent imap in superblock\n");
151 return;
152 }
153 clear_inode(inode);
154 if (!clear_bit(ino & 8191, bh->b_data))
155 printk("free_inode: bit %lu already cleared.\n",ino);
156 mark_buffer_dirty(bh, 1);
157 }
158
159 struct inode * minix_new_inode(const struct inode * dir)
160 {
161 struct super_block * sb;
162 struct inode * inode;
163 struct buffer_head * bh;
164 int i,j;
165
166 if (!dir || !(inode = get_empty_inode()))
167 return NULL;
168 sb = dir->i_sb;
169 inode->i_sb = sb;
170 inode->i_flags = inode->i_sb->s_flags;
171 j = 8192;
172 for (i=0 ; i<8 ; i++)
173 if ((bh = inode->i_sb->u.minix_sb.s_imap[i]) != NULL)
174 if ((j=find_first_zero_bit(bh->b_data, 8192)) < 8192)
175 break;
176 if (!bh || j >= 8192) {
177 iput(inode);
178 return NULL;
179 }
180 if (set_bit(j,bh->b_data)) {
181 printk("new_inode: bit already set");
182 iput(inode);
183 return NULL;
184 }
185 mark_buffer_dirty(bh, 1);
186 j += i*8192;
187 if (!j || j >= inode->i_sb->u.minix_sb.s_ninodes) {
188 iput(inode);
189 return NULL;
190 }
191 inode->i_count = 1;
192 inode->i_nlink = 1;
193 inode->i_dev = sb->s_dev;
194 inode->i_uid = current->fsuid;
195 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
196 inode->i_dirt = 1;
197 inode->i_ino = j;
198 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
199 inode->i_op = NULL;
200 inode->i_blocks = inode->i_blksize = 0;
201 insert_inode_hash(inode);
202 return inode;
203 }
204
205 unsigned long minix_count_free_inodes(struct super_block *sb)
206 {
207 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);
208 }