-
Notifications
You must be signed in to change notification settings - Fork 1
/
ialloc.c
284 lines (235 loc) · 6.69 KB
/
ialloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
*
* ialloc.c
*
* R.K.Raja
*
* (C) Copyright 2002, 2003.
* All rights reserved.
*
*/
#include <linux/fs.h>
#include <rkfs.h>
int rkfs_free_inode(struct inode *vfs_inode,
unsigned short *res_icount, unsigned short *res_iblkno)
{
struct super_block *vfs_sb = NULL;
struct buffer_head *bh = NULL;
struct rkfs_super_block *rkfs_dsb = NULL;
unsigned short rkfs_sb_index = 0, rkfs_sb_count = 0;
unsigned short bit = 0, blkno = 0, itable_index = 0;
int err = -EIO;
*res_icount = 0;
*res_iblkno = 0;
if (!vfs_inode) {
rkfs_bug("VFS inode is NULL\n");
goto out;
}
rkfs_debug("Inode %ld\n", vfs_inode->i_ino);
if (!(vfs_sb = vfs_inode->i_sb)) {
rkfs_bug("VFS superblock is NULL\n");
goto out;
}
lock_super(vfs_sb);
clear_inode(vfs_inode);
if (is_bad_inode(vfs_inode)) {
rkfs_bug("Cannot free bad inode\n");
goto unlock_and_out;
}
rkfs_sb_count = vfs_sb->u.rkfs_sb.s_sb_count;
rkfs_sb_index = vfs_inode->i_ino / RKFS_MIN_BLOCKS;
if (rkfs_sb_index > (rkfs_sb_count - 1)) {
rkfs_bug("Inode %ld is bad (invalid %s sb index)\n",
vfs_inode->i_ino, RKFS_NAME);
goto unlock_and_out;
}
if (!(bh = vfs_sb->u.rkfs_sb.s_sbh[rkfs_sb_index])) {
rkfs_bug("%s superblock not in memory\n", RKFS_NAME);
goto unlock_and_out;
}
rkfs_dsb = (struct rkfs_super_block *)((char *)bh->b_data);
if (rkfs_dsb->s_fsid != RKFS_ID) {
rkfs_bug("No valid %s superblock found\n", RKFS_NAME);
goto unlock_and_out;
}
bit = vfs_inode->i_ino % RKFS_MIN_BLOCKS;
if (rkfs_sb_index) {
if (bit == 0) {
rkfs_bug
("Inode %ld (bit %d) is bad (can't free resv. inode)\n",
vfs_inode->i_ino, bit);
goto unlock_and_out;
}
} else {
if (bit < (RKFS_FIRST_INODE - 1)) {
rkfs_bug
("Inode %ld (bit %d) is bad (can't free resv. inode)\n",
vfs_inode->i_ino, bit);
goto unlock_and_out;
}
}
itable_index = bit / RKFS_INODES_PER_BLOCK;
if (!(blkno = rkfs_dsb->s_itable_map[itable_index][0])) {
rkfs_bug("Inode %ld is bad (no inode block)\n",
vfs_inode->i_ino);
goto unlock_and_out;
}
rkfs_debug("Freeing inode %ld (bit %d)\n", vfs_inode->i_ino, bit);
if (!(rkfs_clear_bit(bit, rkfs_dsb->s_inode_map))) {
rkfs_bug("Inode %ld (bit %d) is already free\n",
vfs_inode->i_ino, bit);
goto unlock_and_out;
}
rkfs_dsb->s_itable_map[itable_index][1] -= 1;
rkfs_debug("Inode count at index: %d is %d\n", itable_index,
rkfs_dsb->s_itable_map[itable_index][1]);
*res_icount = rkfs_dsb->s_itable_map[itable_index][1];
*res_iblkno = blkno;
if (*res_icount == 0)
rkfs_dsb->s_itable_map[itable_index][0] = 0;
mark_buffer_dirty(bh);
if (vfs_sb->s_flags & MS_SYNCHRONOUS) {
ll_rw_block(WRITE, 1, &bh);
wait_on_buffer(bh);
}
unlock_super(vfs_sb);
rkfs_debug("Inode %ld (bit %d) freed\n", vfs_inode->i_ino, bit);
return 0;
unlock_and_out:
unlock_super(vfs_sb);
out:
FAILED;
return err;
}
int rkfs_new_inode(struct inode *vfs_pinode, int mode,
struct inode **vfs_cinode)
{
struct super_block *vfs_sb = NULL;
struct buffer_head *bh = NULL;
struct rkfs_super_block *rkfs_dsb = NULL;
unsigned short i = 0, blkno = 0, bit = 0, itable_index = 0;
unsigned short rkfs_sb_count = 0, rkfs_sb_index = 0;
unsigned short fi_found = 0;
unsigned long cino = 0;
int err = -EIO;
rkfs_debug("New inode requested...\n");
if (!vfs_pinode) {
rkfs_bug("VFS inode is NULL\n");
goto out;
}
*vfs_cinode = NULL;
if (!(vfs_sb = vfs_pinode->i_sb)) {
rkfs_bug("VFS superblock is NULL\n");
goto out;
}
lock_super(vfs_sb);
if (!(*vfs_cinode = new_inode(vfs_sb))) {
rkfs_debug("Can't create new empty VFS inode\n");
err = -ENOSPC;
goto unlock_and_out;
}
err = -EIO;
rkfs_sb_count = vfs_sb->u.rkfs_sb.s_sb_count;
rkfs_sb_index = vfs_pinode->i_ino / RKFS_MIN_BLOCKS;
if (rkfs_sb_index > (rkfs_sb_count - 1)) {
rkfs_bug("Parent inode %ld is bad (invalid %s sb index)\n",
vfs_pinode->i_ino, RKFS_NAME);
goto put_unlock_and_out;
}
while (1) {
if (!(bh = vfs_sb->u.rkfs_sb.s_sbh[rkfs_sb_index])) {
rkfs_bug("%s superblock not in memory\n", RKFS_NAME);
goto put_unlock_and_out;
}
rkfs_dsb = (struct rkfs_super_block *)((char *)bh->b_data);
if (rkfs_dsb->s_fsid != RKFS_ID) {
rkfs_bug("No valid %s superblock found\n", RKFS_NAME);
goto put_unlock_and_out;
}
if (!i) {
bit = vfs_pinode->i_ino % RKFS_MIN_BLOCKS;
itable_index = bit / RKFS_INODES_PER_BLOCK;
if (!(blkno = rkfs_dsb->s_itable_map[itable_index][0])) {
rkfs_bug
("Parent inode %ld (bit %d) is bad (no inode blk)\n",
vfs_pinode->i_ino, bit);
goto put_unlock_and_out;
}
}
bit = rkfs_find_first_zero_bit(rkfs_dsb->s_inode_map,
RKFS_MIN_BLOCKS);
if (rkfs_sb_index && bit > 0 && bit < RKFS_MIN_BLOCKS) {
fi_found = 1;
break;
}
if (!rkfs_sb_index && bit >= RKFS_FIRST_INODE &&
bit < RKFS_MIN_BLOCKS) {
fi_found = 1;
break;
}
if (!i) {
rkfs_sb_index = 0;
i++;
} else
rkfs_sb_index++;
if (rkfs_sb_index >= rkfs_sb_count)
break;
}
if (!fi_found) {
rkfs_debug("No free inode found in the device %s\n",
bdevname(vfs_pinode->i_dev));
err = -ENOSPC;
goto put_unlock_and_out;
}
itable_index = bit / RKFS_INODES_PER_BLOCK;
if (!(blkno = rkfs_dsb->s_itable_map[itable_index][0])) {
rkfs_debug("Allocating new inode block...\n");
err = rkfs_new_inode_block(vfs_pinode, &blkno);
if (err) {
rkfs_debug("Can't get new inode block\n");
goto put_unlock_and_out;
}
rkfs_dsb->s_itable_map[itable_index][0] = blkno;
}
cino = bit + (rkfs_sb_index * RKFS_MIN_BLOCKS);
if (rkfs_set_bit(bit, rkfs_dsb->s_inode_map)) {
rkfs_bug("New inode %ld (bit %d) is already in use\n", cino,
bit);
err = -EIO;
goto put_unlock_and_out;
}
rkfs_dsb->s_itable_map[itable_index][1] += 1;
rkfs_debug("Inode count at index: %d is %d\n", itable_index,
rkfs_dsb->s_itable_map[itable_index][1]);
mark_buffer_dirty(bh);
if (vfs_sb->s_flags & MS_SYNCHRONOUS) {
ll_rw_block(WRITE, 1, &bh);
wait_on_buffer(bh);
}
(*vfs_cinode)->i_ino = cino;
(*vfs_cinode)->i_uid = current->fsuid;
(*vfs_cinode)->i_gid = (vfs_pinode->i_mode & S_ISGID) ?
vfs_pinode->i_gid : current->fsgid;
(*vfs_cinode)->i_mode = mode;
(*vfs_cinode)->i_mtime = CURRENT_TIME;
(*vfs_cinode)->i_ctime = CURRENT_TIME;
(*vfs_cinode)->i_atime = CURRENT_TIME;
(*vfs_cinode)->i_size = 0;
(*vfs_cinode)->i_blocks = 0;
(*vfs_cinode)->i_blksize = PAGE_SIZE;
for (i = 0; i < RKFS_N_BLOCKS; i++)
(*vfs_cinode)->u.rkfs_i.i_block[i] = 0;
insert_inode_hash(*vfs_cinode);
mark_inode_dirty(*vfs_cinode);
unlock_super(vfs_sb);
rkfs_debug("New inode is: %ld\n", (*vfs_cinode)->i_ino);
return 0;
put_unlock_and_out:
iput(*vfs_cinode);
unlock_and_out:
unlock_super(vfs_sb);
out:
FAILED;
return err;
}