-
Notifications
You must be signed in to change notification settings - Fork 0
/
super.c
360 lines (300 loc) · 8.48 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// SPDX-License-Identifier: GPL-2.0
/*
* ouiche_fs - a simple educational filesystem for Linux
*
* Copyright (C) 2018 Redha Gouicem <redha.gouicem@lip6.fr>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include <linux/statfs.h>
#include "ouichefs.h"
static struct kmem_cache *ouichefs_inode_cache;
int ouichefs_init_inode_cache(void)
{
ouichefs_inode_cache =
kmem_cache_create("ouichefs_cache",
sizeof(struct ouichefs_inode_info), 0,
0, NULL);
if (!ouichefs_inode_cache)
return -ENOMEM;
return 0;
}
void ouichefs_destroy_inode_cache(void)
{
kmem_cache_destroy(ouichefs_inode_cache);
}
static struct inode *ouichefs_alloc_inode(struct super_block *sb)
{
struct ouichefs_inode_info *ci;
/* ci = kzalloc(sizeof(struct ouichefs_inode_info), GFP_KERNEL); */
ci = kmem_cache_alloc(ouichefs_inode_cache, GFP_KERNEL);
if (!ci)
return NULL;
inode_init_once(&ci->vfs_inode);
return &ci->vfs_inode;
}
static void ouichefs_destroy_inode(struct inode *inode)
{
struct ouichefs_inode_info *ci;
ci = OUICHEFS_INODE(inode);
kmem_cache_free(ouichefs_inode_cache, ci);
}
static int ouichefs_write_inode(struct inode *inode,
struct writeback_control *wbc)
{
struct ouichefs_inode *disk_inode;
struct ouichefs_inode_info *ci = OUICHEFS_INODE(inode);
struct super_block *sb = inode->i_sb;
struct ouichefs_sb_info *sbi = OUICHEFS_SB(sb);
struct buffer_head *bh;
uint32_t ino = inode->i_ino;
uint32_t inode_block = (ino / OUICHEFS_INODES_PER_BLOCK) + 1;
uint32_t inode_shift = ino % OUICHEFS_INODES_PER_BLOCK;
if (ino >= sbi->nr_inodes)
return 0;
bh = sb_bread(sb, inode_block);
if (!bh)
return -EIO;
disk_inode = (struct ouichefs_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->index_block = ci->index_block;
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
return 0;
}
static void ouichefs_put_super(struct super_block *sb)
{
struct ouichefs_sb_info *sbi = OUICHEFS_SB(sb);
if (sbi) {
kfree(sbi->ifree_bitmap);
kfree(sbi->bfree_bitmap);
kfree(sbi->b_refcount);
kfree(sbi);
}
}
static int ouichefs_sync_fs(struct super_block *sb, int wait)
{
struct buffer_head *bh;
struct ouichefs_sb_info *sbi = OUICHEFS_SB(sb);
struct ouichefs_sb_info *disk_sb;
int i;
/* Flush superblock */
bh = sb_bread(sb, 0);
if (!bh)
return -EIO;
disk_sb = (struct ouichefs_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;
disk_sb->nr_refcount_blocks = sbi->nr_refcount_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 * OUICHEFS_BLOCK_SIZE,
OUICHEFS_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 * OUICHEFS_BLOCK_SIZE,
OUICHEFS_BLOCK_SIZE);
mark_buffer_dirty(bh);
if (wait)
sync_dirty_buffer(bh);
brelse(bh);
}
for (i = 0; i < sbi->nr_refcount_blocks; i++) {
int idx = sbi->nr_istore_blocks + sbi->nr_ifree_blocks
+ sbi->nr_bfree_blocks + i + 1;
bh = sb_bread(sb, idx);
if (!bh) {
pr_warn("Error reading refcount block");
return -EIO;
}
memcpy(bh->b_data,
(void *)sbi->b_refcount + i * OUICHEFS_BLOCK_SIZE,
OUICHEFS_BLOCK_SIZE);
mark_buffer_dirty(bh);
if (wait)
sync_dirty_buffer(bh);
brelse(bh);
}
return 0;
}
static int ouichefs_statfs(struct dentry *dentry, struct kstatfs *stat)
{
struct super_block *sb = dentry->d_sb;
struct ouichefs_sb_info *sbi = OUICHEFS_SB(sb);
stat->f_type = OUICHEFS_MAGIC;
stat->f_bsize = OUICHEFS_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 = OUICHEFS_FILENAME_LEN;
return 0;
}
static struct super_operations ouichefs_super_ops = {
.put_super = ouichefs_put_super,
.alloc_inode = ouichefs_alloc_inode,
.destroy_inode = ouichefs_destroy_inode,
.write_inode = ouichefs_write_inode,
.sync_fs = ouichefs_sync_fs,
.statfs = ouichefs_statfs,
};
/* Fill the struct superblock from partition superblock */
int ouichefs_fill_super(struct super_block *sb, void *data, int silent)
{
struct buffer_head *bh = NULL;
struct ouichefs_sb_info *csb = NULL;
struct ouichefs_sb_info *sbi = NULL;
struct inode *root_inode = NULL;
int ret = 0, i;
/* Init sb */
sb->s_magic = OUICHEFS_MAGIC;
sb_set_blocksize(sb, OUICHEFS_BLOCK_SIZE);
sb->s_maxbytes = OUICHEFS_MAX_FILESIZE;
sb->s_op = &ouichefs_super_ops;
/* Read sb from disk */
bh = sb_bread(sb, OUICHEFS_SB_BLOCK_NR);
if (!bh)
return -EIO;
csb = (struct ouichefs_sb_info *)bh->b_data;
/* Check magic number */
if (csb->magic != sb->s_magic) {
pr_err("Wrong magic number\n");
ret = -EPERM;
goto release;
}
/* Alloc sb_info */
sbi = kzalloc(sizeof(struct ouichefs_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;
sbi->nr_refcount_blocks = csb->nr_refcount_blocks;
sb->s_fs_info = sbi;
brelse(bh);
/* Alloc and copy ifree_bitmap */
sbi->ifree_bitmap = kzalloc(sbi->nr_ifree_blocks * OUICHEFS_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 * OUICHEFS_BLOCK_SIZE,
bh->b_data, OUICHEFS_BLOCK_SIZE);
brelse(bh);
}
/* Alloc and copy bfree_bitmap */
sbi->bfree_bitmap = kzalloc(sbi->nr_bfree_blocks * OUICHEFS_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 * OUICHEFS_BLOCK_SIZE,
bh->b_data, OUICHEFS_BLOCK_SIZE);
brelse(bh);
}
sbi->b_refcount = kzalloc(sbi->nr_refcount_blocks * OUICHEFS_BLOCK_SIZE,
GFP_KERNEL);
if (!sbi->b_refcount) {
ret = -ENOMEM;
goto free_bfree;
}
for (i = 0; i < sbi->nr_bfree_blocks; i++) {
int idx = sbi->nr_istore_blocks + sbi->nr_ifree_blocks + sbi->nr_refcount_blocks + i + 1;
bh = sb_bread(sb, idx);
if (!bh) {
ret = -EIO;
goto free_bref;
}
memcpy((void *)sbi->b_refcount + i * OUICHEFS_BLOCK_SIZE,
bh->b_data, OUICHEFS_BLOCK_SIZE);
brelse(bh);
}
/* Create root inode */
root_inode = ouichefs_iget(sb, 0);
if (IS_ERR(root_inode)) {
ret = PTR_ERR(root_inode);
goto free_bref;
}
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_bref:
kfree(sbi->b_refcount);
free_bfree:
kfree(sbi->bfree_bitmap);
free_ifree:
kfree(sbi->ifree_bitmap);
free_sbi:
kfree(sbi);
release:
brelse(bh);
return ret;
}