forked from sysprog21/simplefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
super.c
306 lines (250 loc) · 7.95 KB
/
super.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/buffer_head.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/statfs.h>
#include "simplefs.h"
static struct kmem_cache *simplefs_inode_cache;
int simplefs_init_inode_cache(void)
{
simplefs_inode_cache = kmem_cache_create(
"simplefs_cache", sizeof(struct simplefs_inode_info), 0, 0, NULL);
if (!simplefs_inode_cache)
return -ENOMEM;
return 0;
}
void simplefs_destroy_inode_cache(void)
{
kmem_cache_destroy(simplefs_inode_cache);
}
static struct inode *simplefs_alloc_inode(struct super_block *sb)
{
struct simplefs_inode_info *ci =
kmem_cache_alloc(simplefs_inode_cache, GFP_KERNEL);
if (!ci)
return NULL;
inode_init_once(&ci->vfs_inode);
return &ci->vfs_inode;
}
static void simplefs_destroy_inode(struct inode *inode)
{
struct simplefs_inode_info *ci = SIMPLEFS_INODE(inode);
kmem_cache_free(simplefs_inode_cache, ci);
}
static int simplefs_write_inode(struct inode *inode,
struct writeback_control *wbc)
{
struct simplefs_inode *disk_inode;
struct simplefs_inode_info *ci = SIMPLEFS_INODE(inode);
struct super_block *sb = inode->i_sb;
struct simplefs_sb_info *sbi = SIMPLEFS_SB(sb);
struct buffer_head *bh;
uint32_t ino = inode->i_ino;
uint32_t inode_block = (ino / SIMPLEFS_INODES_PER_BLOCK) + 1;
uint32_t inode_shift = ino % SIMPLEFS_INODES_PER_BLOCK;
if (ino >= sbi->nr_inodes)
return 0;
bh = sb_bread(sb, inode_block);
if (!bh)
return -EIO;
disk_inode = (struct simplefs_inode *) bh->b_data;
disk_inode += inode_shift;
/* update the mode using what the generic inode has */
disk_inode->i_mode = inode->i_mode;
disk_inode->i_uid = i_uid_read(inode);
disk_inode->i_gid = i_gid_read(inode);
disk_inode->i_size = inode->i_size;
disk_inode->i_ctime = inode->i_ctime.tv_sec;
disk_inode->i_atime = inode->i_atime.tv_sec;
disk_inode->i_mtime = inode->i_mtime.tv_sec;
disk_inode->i_blocks = inode->i_blocks;
disk_inode->i_nlink = inode->i_nlink;
disk_inode->ei_block = ci->ei_block;
strncpy(disk_inode->i_data, ci->i_data, sizeof(ci->i_data));
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
return 0;
}
static void simplefs_put_super(struct super_block *sb)
{
struct simplefs_sb_info *sbi = SIMPLEFS_SB(sb);
if (sbi) {
kfree(sbi->ifree_bitmap);
kfree(sbi->bfree_bitmap);
kfree(sbi);
}
}
static int simplefs_sync_fs(struct super_block *sb, int wait)
{
struct simplefs_sb_info *sbi = SIMPLEFS_SB(sb);
struct simplefs_sb_info *disk_sb;
int i;
/* Flush superblock */
struct buffer_head *bh = sb_bread(sb, 0);
if (!bh)
return -EIO;
disk_sb = (struct simplefs_sb_info *) bh->b_data;
disk_sb->nr_blocks = sbi->nr_blocks;
disk_sb->nr_inodes = sbi->nr_inodes;
disk_sb->nr_istore_blocks = sbi->nr_istore_blocks;
disk_sb->nr_ifree_blocks = sbi->nr_ifree_blocks;
disk_sb->nr_bfree_blocks = sbi->nr_bfree_blocks;
disk_sb->nr_free_inodes = sbi->nr_free_inodes;
disk_sb->nr_free_blocks = sbi->nr_free_blocks;
mark_buffer_dirty(bh);
if (wait)
sync_dirty_buffer(bh);
brelse(bh);
/* Flush free inodes bitmask */
for (i = 0; i < sbi->nr_ifree_blocks; i++) {
int idx = sbi->nr_istore_blocks + i + 1;
bh = sb_bread(sb, idx);
if (!bh)
return -EIO;
memcpy(bh->b_data, (void *) sbi->ifree_bitmap + i * SIMPLEFS_BLOCK_SIZE,
SIMPLEFS_BLOCK_SIZE);
mark_buffer_dirty(bh);
if (wait)
sync_dirty_buffer(bh);
brelse(bh);
}
/* Flush free blocks bitmask */
for (i = 0; i < sbi->nr_bfree_blocks; i++) {
int idx = sbi->nr_istore_blocks + sbi->nr_ifree_blocks + i + 1;
bh = sb_bread(sb, idx);
if (!bh)
return -EIO;
memcpy(bh->b_data, (void *) sbi->bfree_bitmap + i * SIMPLEFS_BLOCK_SIZE,
SIMPLEFS_BLOCK_SIZE);
mark_buffer_dirty(bh);
if (wait)
sync_dirty_buffer(bh);
brelse(bh);
}
return 0;
}
static int simplefs_statfs(struct dentry *dentry, struct kstatfs *stat)
{
struct super_block *sb = dentry->d_sb;
struct simplefs_sb_info *sbi = SIMPLEFS_SB(sb);
stat->f_type = SIMPLEFS_MAGIC;
stat->f_bsize = SIMPLEFS_BLOCK_SIZE;
stat->f_blocks = sbi->nr_blocks;
stat->f_bfree = sbi->nr_free_blocks;
stat->f_bavail = sbi->nr_free_blocks;
stat->f_files = sbi->nr_inodes - sbi->nr_free_inodes;
stat->f_ffree = sbi->nr_free_inodes;
stat->f_namelen = SIMPLEFS_FILENAME_LEN;
return 0;
}
static struct super_operations simplefs_super_ops = {
.put_super = simplefs_put_super,
.alloc_inode = simplefs_alloc_inode,
.destroy_inode = simplefs_destroy_inode,
.write_inode = simplefs_write_inode,
.sync_fs = simplefs_sync_fs,
.statfs = simplefs_statfs,
};
/* Fill the struct superblock from partition superblock */
int simplefs_fill_super(struct super_block *sb, void *data, int silent)
{
struct buffer_head *bh = NULL;
struct simplefs_sb_info *csb = NULL;
struct simplefs_sb_info *sbi = NULL;
struct inode *root_inode = NULL;
int ret = 0, i;
/* Init sb */
sb->s_magic = SIMPLEFS_MAGIC;
sb_set_blocksize(sb, SIMPLEFS_BLOCK_SIZE);
sb->s_maxbytes = SIMPLEFS_MAX_FILESIZE;
sb->s_op = &simplefs_super_ops;
/* Read sb from disk */
bh = sb_bread(sb, SIMPLEFS_SB_BLOCK_NR);
if (!bh)
return -EIO;
csb = (struct simplefs_sb_info *) bh->b_data;
/* Check magic number */
if (csb->magic != sb->s_magic) {
pr_err("Wrong magic number\n");
ret = -EINVAL;
goto release;
}
/* Alloc sb_info */
sbi = kzalloc(sizeof(struct simplefs_sb_info), GFP_KERNEL);
if (!sbi) {
ret = -ENOMEM;
goto release;
}
sbi->nr_blocks = csb->nr_blocks;
sbi->nr_inodes = csb->nr_inodes;
sbi->nr_istore_blocks = csb->nr_istore_blocks;
sbi->nr_ifree_blocks = csb->nr_ifree_blocks;
sbi->nr_bfree_blocks = csb->nr_bfree_blocks;
sbi->nr_free_inodes = csb->nr_free_inodes;
sbi->nr_free_blocks = csb->nr_free_blocks;
sb->s_fs_info = sbi;
brelse(bh);
/* Alloc and copy ifree_bitmap */
sbi->ifree_bitmap =
kzalloc(sbi->nr_ifree_blocks * SIMPLEFS_BLOCK_SIZE, GFP_KERNEL);
if (!sbi->ifree_bitmap) {
ret = -ENOMEM;
goto free_sbi;
}
for (i = 0; i < sbi->nr_ifree_blocks; i++) {
int idx = sbi->nr_istore_blocks + i + 1;
bh = sb_bread(sb, idx);
if (!bh) {
ret = -EIO;
goto free_ifree;
}
memcpy((void *) sbi->ifree_bitmap + i * SIMPLEFS_BLOCK_SIZE, bh->b_data,
SIMPLEFS_BLOCK_SIZE);
brelse(bh);
}
/* Alloc and copy bfree_bitmap */
sbi->bfree_bitmap =
kzalloc(sbi->nr_bfree_blocks * SIMPLEFS_BLOCK_SIZE, GFP_KERNEL);
if (!sbi->bfree_bitmap) {
ret = -ENOMEM;
goto free_ifree;
}
for (i = 0; i < sbi->nr_bfree_blocks; i++) {
int idx = sbi->nr_istore_blocks + sbi->nr_ifree_blocks + i + 1;
bh = sb_bread(sb, idx);
if (!bh) {
ret = -EIO;
goto free_bfree;
}
memcpy((void *) sbi->bfree_bitmap + i * SIMPLEFS_BLOCK_SIZE, bh->b_data,
SIMPLEFS_BLOCK_SIZE);
brelse(bh);
}
/* Create root inode */
root_inode = simplefs_iget(sb, 0);
if (IS_ERR(root_inode)) {
ret = PTR_ERR(root_inode);
goto free_bfree;
}
inode_init_owner(root_inode, NULL, root_inode->i_mode);
sb->s_root = d_make_root(root_inode);
if (!sb->s_root) {
ret = -ENOMEM;
goto iput;
}
return 0;
iput:
iput(root_inode);
free_bfree:
kfree(sbi->bfree_bitmap);
free_ifree:
kfree(sbi->ifree_bitmap);
free_sbi:
kfree(sbi);
release:
brelse(bh);
return ret;
}