-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathinode.c
More file actions
299 lines (249 loc) · 9.19 KB
/
inode.c
File metadata and controls
299 lines (249 loc) · 9.19 KB
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
#include "khellofs.h"
void hellofs_destroy_inode(struct inode *inode) {
struct hellofs_inode *hellofs_inode = HELLOFS_INODE(inode);
printk(KERN_INFO "Freeing private data of inode %p (%lu)\n",
hellofs_inode, inode->i_ino);
kmem_cache_free(hellofs_inode_cache, hellofs_inode);
}
void hellofs_fill_inode(struct super_block *sb, struct inode *inode,
struct hellofs_inode *hellofs_inode) {
inode->i_mode = hellofs_inode->mode;
inode->i_sb = sb;
inode->i_ino = hellofs_inode->inode_no;
inode->i_op = &hellofs_inode_ops;
// TODO hope we can use hellofs_inode to store timespec
inode->i_atime = inode->i_mtime
= inode->i_ctime
= CURRENT_TIME;
inode->i_private = hellofs_inode;
if (S_ISDIR(hellofs_inode->mode)) {
inode->i_fop = &hellofs_dir_operations;
} else if (S_ISREG(hellofs_inode->mode)) {
inode->i_fop = &hellofs_file_operations;
} else {
printk(KERN_WARNING
"Inode %lu is neither a directory nor a regular file",
inode->i_ino);
inode->i_fop = NULL;
}
/* TODO hellofs_inode->file_size seems not reflected in inode */
}
/* TODO I didn't implement any function to dealloc hellofs_inode */
int hellofs_alloc_hellofs_inode(struct super_block *sb, uint64_t *out_inode_no) {
struct hellofs_superblock *hellofs_sb;
struct buffer_head *bh;
uint64_t i;
int ret;
char *bitmap;
char *slot;
char needle;
hellofs_sb = HELLOFS_SB(sb);
mutex_lock(&hellofs_sb_lock);
bh = sb_bread(sb, HELLOFS_INODE_BITMAP_BLOCK_NO);
BUG_ON(!bh);
bitmap = bh->b_data;
ret = -ENOSPC;
for (i = 0; i < hellofs_sb->inode_table_size; i++) {
slot = bitmap + i / BITS_IN_BYTE;
needle = 1 << (i % BITS_IN_BYTE);
if (0 == (*slot & needle)) {
*out_inode_no = i;
*slot |= needle;
hellofs_sb->inode_count += 1;
ret = 0;
break;
}
}
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
hellofs_save_sb(sb);
mutex_unlock(&hellofs_sb_lock);
return ret;
}
struct hellofs_inode *hellofs_get_hellofs_inode(struct super_block *sb,
uint64_t inode_no) {
struct buffer_head *bh;
struct hellofs_inode *inode;
struct hellofs_inode *inode_buf;
bh = sb_bread(sb, HELLOFS_INODE_TABLE_START_BLOCK_NO + HELLOFS_INODE_BLOCK_OFFSET(sb, inode_no));
BUG_ON(!bh);
inode = (struct hellofs_inode *)(bh->b_data + HELLOFS_INODE_BYTE_OFFSET(sb, inode_no));
inode_buf = kmem_cache_alloc(hellofs_inode_cache, GFP_KERNEL);
memcpy(inode_buf, inode, sizeof(*inode_buf));
brelse(bh);
return inode_buf;
}
void hellofs_save_hellofs_inode(struct super_block *sb,
struct hellofs_inode *inode_buf) {
struct buffer_head *bh;
struct hellofs_inode *inode;
uint64_t inode_no;
inode_no = inode_buf->inode_no;
bh = sb_bread(sb, HELLOFS_INODE_TABLE_START_BLOCK_NO + HELLOFS_INODE_BLOCK_OFFSET(sb, inode_no));
BUG_ON(!bh);
inode = (struct hellofs_inode *)(bh->b_data + HELLOFS_INODE_BYTE_OFFSET(sb, inode_no));
memcpy(inode, inode_buf, sizeof(*inode));
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
}
int hellofs_add_dir_record(struct super_block *sb, struct inode *dir,
struct dentry *dentry, struct inode *inode) {
struct buffer_head *bh;
struct hellofs_inode *parent_hellofs_inode;
struct hellofs_dir_record *dir_record;
parent_hellofs_inode = HELLOFS_INODE(dir);
if (unlikely(parent_hellofs_inode->dir_children_count
>= HELLOFS_DIR_MAX_RECORD(sb))) {
return -ENOSPC;
}
bh = sb_bread(sb, parent_hellofs_inode->data_block_no);
BUG_ON(!bh);
dir_record = (struct hellofs_dir_record *)bh->b_data;
dir_record += parent_hellofs_inode->dir_children_count;
dir_record->inode_no = inode->i_ino;
strcpy(dir_record->filename, dentry->d_name.name);
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
parent_hellofs_inode->dir_children_count += 1;
hellofs_save_hellofs_inode(sb, parent_hellofs_inode);
return 0;
}
int hellofs_alloc_data_block(struct super_block *sb, uint64_t *out_data_block_no) {
struct hellofs_superblock *hellofs_sb;
struct buffer_head *bh;
uint64_t i;
int ret;
char *bitmap;
char *slot;
char needle;
hellofs_sb = HELLOFS_SB(sb);
mutex_lock(&hellofs_sb_lock);
bh = sb_bread(sb, HELLOFS_DATA_BLOCK_BITMAP_BLOCK_NO);
BUG_ON(!bh);
bitmap = bh->b_data;
ret = -ENOSPC;
for (i = 0; i < hellofs_sb->data_block_table_size; i++) {
slot = bitmap + i / BITS_IN_BYTE;
needle = 1 << (i % BITS_IN_BYTE);
if (0 == (*slot & needle)) {
*out_data_block_no
= HELLOFS_DATA_BLOCK_TABLE_START_BLOCK_NO(sb) + i;
*slot |= needle;
hellofs_sb->data_block_count += 1;
ret = 0;
break;
}
}
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
hellofs_save_sb(sb);
mutex_unlock(&hellofs_sb_lock);
return ret;
}
int hellofs_create_inode(struct inode *dir, struct dentry *dentry,
umode_t mode) {
struct super_block *sb;
struct hellofs_superblock *hellofs_sb;
uint64_t inode_no;
struct hellofs_inode *hellofs_inode;
struct inode *inode;
int ret;
sb = dir->i_sb;
hellofs_sb = HELLOFS_SB(sb);
/* Create hellofs_inode */
ret = hellofs_alloc_hellofs_inode(sb, &inode_no);
if (0 != ret) {
printk(KERN_ERR "Unable to allocate on-disk inode. "
"Is inode table full? "
"Inode count: %llu\n",
hellofs_sb->inode_count);
return -ENOSPC;
}
hellofs_inode = kmem_cache_alloc(hellofs_inode_cache, GFP_KERNEL);
hellofs_inode->inode_no = inode_no;
hellofs_inode->mode = mode;
if (S_ISDIR(mode)) {
hellofs_inode->dir_children_count = 0;
} else if (S_ISREG(mode)) {
hellofs_inode->file_size = 0;
} else {
printk(KERN_WARNING
"Inode %llu is neither a directory nor a regular file",
inode_no);
}
/* Allocate data block for the new hellofs_inode */
ret = hellofs_alloc_data_block(sb, &hellofs_inode->data_block_no);
if (0 != ret) {
printk(KERN_ERR "Unable to allocate on-disk data block. "
"Is data block table full? "
"Data block count: %llu\n",
hellofs_sb->data_block_count);
return -ENOSPC;
}
/* Create VFS inode */
inode = new_inode(sb);
if (!inode) {
return -ENOMEM;
}
hellofs_fill_inode(sb, inode, hellofs_inode);
/* Add new inode to parent dir */
ret = hellofs_add_dir_record(sb, dir, dentry, inode);
if (0 != ret) {
printk(KERN_ERR "Failed to add inode %lu to parent dir %lu\n",
inode->i_ino, dir->i_ino);
return -ENOSPC;
}
inode_init_owner(inode, dir, mode);
d_add(dentry, inode);
/* TODO we should free newly allocated inodes when error occurs */
return 0;
}
int hellofs_create(struct inode *dir, struct dentry *dentry,
umode_t mode, bool excl) {
return hellofs_create_inode(dir, dentry, mode);
}
int hellofs_mkdir(struct inode *dir, struct dentry *dentry,
umode_t mode) {
/* @Sankar: The mkdir callback does not have S_IFDIR set.
Even ext2 sets it explicitly. Perhaps this is a bug */
mode |= S_IFDIR;
return hellofs_create_inode(dir, dentry, mode);
}
struct dentry *hellofs_lookup(struct inode *dir,
struct dentry *child_dentry,
unsigned int flags) {
struct hellofs_inode *parent_hellofs_inode = HELLOFS_INODE(dir);
struct super_block *sb = dir->i_sb;
struct buffer_head *bh;
struct hellofs_dir_record *dir_record;
struct hellofs_inode *hellofs_child_inode;
struct inode *child_inode;
uint64_t i;
bh = sb_bread(sb, parent_hellofs_inode->data_block_no);
BUG_ON(!bh);
dir_record = (struct hellofs_dir_record *)bh->b_data;
for (i = 0; i < parent_hellofs_inode->dir_children_count; i++) {
printk(KERN_INFO "hellofs_lookup: i=%llu, dir_record->filename=%s, child_dentry->d_name.name=%s", i, dir_record->filename, child_dentry->d_name.name); // TODO
if (0 == strcmp(dir_record->filename, child_dentry->d_name.name)) {
hellofs_child_inode = hellofs_get_hellofs_inode(sb, dir_record->inode_no);
child_inode = new_inode(sb);
if (!child_inode) {
printk(KERN_ERR "Cannot create new inode. No memory.\n");
return NULL;
}
hellofs_fill_inode(sb, child_inode, hellofs_child_inode);
inode_init_owner(child_inode, dir, hellofs_child_inode->mode);
d_add(child_dentry, child_inode);
return NULL;
}
dir_record++;
}
printk(KERN_ERR
"No inode found for the filename: %s\n",
child_dentry->d_name.name);
return NULL;
}