-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinode.c
334 lines (276 loc) · 9.52 KB
/
inode.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
#include <linux/buffer_head.h>
#include <linux/writeback.h>
#include <asm/ptrace.h>
#include "simplefs.h"
struct simplefs_inode *simplefs_iget_raw(struct super_block *sb, long ino,
struct buffer_head **bh) {
struct simplefs_super_info *sbi = sb->s_fs_info;
int block, bitmap_blocks;
struct simplefs_inode *raw_inode;
printk(KERN_INFO "simplefs_iget_raw\n");
if (ino >= sbi->raw_super.s_inode_blknr * SIMPLEFS_INODES_PER_BLOCK) {
printk(KERN_ERR "Bad inode number on dev %s: %ld is out of range\n", sb->s_id, (long) ino);
return NULL;
}
bitmap_blocks = sbi->raw_super.s_inode_bitmap_blknr + sbi->raw_super.s_block_bitmap_blknr;
block = ino / SIMPLEFS_INODES_PER_BLOCK + bitmap_blocks + 1;
printk(KERN_INFO "simplefs_iget_raw->sb_bread: %d\n", block);
if (!(*bh = sb_bread(sb, block)))
return NULL;
raw_inode = (struct simplefs_inode *)(*bh)->b_data + ino % SIMPLEFS_INODES_PER_BLOCK;
printk(KERN_INFO "simplefs_iget_raw ok: %ld\n", ino);
return raw_inode;
}
struct inode *simplefs_inew(struct super_block *sb, long ino) {
return NULL;
}
struct inode *simplefs_iget(struct super_block *sb, long ino) {
struct buffer_head *bh;
struct simplefs_inode *raw_inode;
struct inode *inode;
struct simplefs_super_info *sbi;
printk(KERN_INFO "simplefs_iget %ld\n", ino);
sbi = sb->s_fs_info;
if (!(inode = iget_locked(sb, ino))) {
printk(KERN_ERR "simplefs_iget failed: iget_locked failed\n");
return NULL;
}
if (!(inode->i_state & I_NEW)) {
printk("%ld is an old inode\n", ino);
return inode;
}
if (!(raw_inode = simplefs_iget_raw(sb, ino, &bh))) {
printk("simplefs_iget: failed to read raw_inode");
goto failed_inode;
}
printk("simplefs_iget raw_inode info: %ld -> %d %d %d %d", ino, raw_inode->i_mode,
raw_inode->i_nlink, raw_inode->i_size, raw_inode->i_time);
inode->i_mode = raw_inode->i_mode;
inode->i_nlink = raw_inode->i_nlink;
inode->i_size = raw_inode->i_size;
inode->i_blocks = inode->i_size >> SIMPLEFS_BLOCKBITS;
inode->i_bytes = inode->i_size % SIMPLEFS_BLOCKSIZE;
inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = raw_inode->i_time;
inode->i_mtime.tv_nsec = 0;
inode->i_ctime.tv_nsec = 0;
inode->i_atime.tv_nsec = 0;
// set inode operations
if (S_ISREG(inode->i_mode)) {
inode->i_op = &simplefs_file_inode_operations;
inode->i_fop = &simplefs_file_operations;
inode->i_mapping->a_ops = &simplefs_aops;
} else if (S_ISDIR(inode->i_mode)) {
inode->i_op = &simplefs_dir_inode_operations;
inode->i_fop = &simplefs_dir_operations;
inode->i_mapping->a_ops = &simplefs_aops;
}
brelse(bh);
unlock_new_inode(inode);
return inode;
failed_inode:
iget_failed(inode);
return NULL;
}
void simplefs_truncate(struct inode *inode) {
struct buffer_head *bh = NULL;
struct simplefs_inode *raw_inode;
int i, blocks;
printk(KERN_INFO "simplefs_truncate start: %ld\n", inode->i_ino);
blocks = (inode->i_size + SIMPLEFS_BLOCKSIZE - 1) >> (SIMPLEFS_BLOCKBITS);
if (!(raw_inode = simplefs_iget_raw(inode->i_sb, inode->i_ino, &bh))) {
printk(KERN_ERR "simplefs_truncate failed: %ld\n", inode->i_ino);
return;
}
for (i = 0; i < blocks; i++) {
bitmap_free_block(inode->i_sb, raw_inode->i_data[i]);
}
inode->i_size = inode->i_blocks = inode->i_bytes = 0;
raw_inode->i_size = 0;
mark_buffer_dirty(bh);
brelse(bh);
printk(KERN_INFO "simplefs_truncate end: %ld\n", inode->i_ino);
dump_stack();
return;
}
int simplefs_free_inode(struct inode *inode) {
bitmap_free_inode(inode->i_sb, inode->i_ino);
printk(KERN_INFO "simplefs_free_inode ok: %ld\n", inode->i_ino);
clear_inode(inode);
return 0;
}
int simplefs_sync_inode(struct inode *inode) {
struct writeback_control wbc = {
.sync_mode = WB_SYNC_ALL,
.nr_to_write = 0, /* sync_fsync did this */
};
return sync_inode(inode, &wbc);
}
int simplefs_get_block(struct inode *inode,
sector_t block, struct buffer_head *bh_result, int create) {
struct buffer_head *bh;
struct simplefs_inode *raw_inode;
long bno;
int err = -EIO;
if (block >= SIMPLEFS_BLOCKS_PER_INODE) {
printk(KERN_ERR "simplefs_get_block failed: too large block number\n");
return -EINVAL;
}
if (!(raw_inode = simplefs_iget_raw(inode->i_sb, inode->i_ino, &bh))) {
printk(KERN_ERR "simplefs_get_block failed: can't get raw inode\n");
return -EIO;
}
if (create && block == (inode->i_size >> SIMPLEFS_BLOCKBITS)) {
if ((bno = bitmap_alloc_block(inode->i_sb)) < 0)
goto bitmap_failed;
raw_inode->i_data[block] = bno;
mark_buffer_dirty(bh);
printk(KERN_INFO "simplefs_get_block create block : %ld %lld %lld %ld\n", inode->i_ino, inode->i_size, block, bno);
}
map_bh(bh_result, inode->i_sb, raw_inode->i_data[block]);
brelse(bh);
return 0;
bitmap_failed:
brelse(bh);
return err;
}
static int simplefs_readpage(struct file *file, struct page *page) {
printk(KERN_INFO "simplefs_readpage\n");
return block_read_full_page(page, simplefs_get_block);
}
static int simplefs_writepage(struct page *page, struct writeback_control *wbc) {
printk(KERN_INFO "simplefs_writepage\n");
return block_write_full_page(page, simplefs_get_block, wbc);
}
static int simplefs_write_begin(struct file *file, struct address_space *mapping, loff_t pos,
unsigned len, unsigned flags, struct page **pagep, void **fsdata) {
*pagep = NULL;
printk(KERN_INFO "simplefs_write_begin: pos:%lld len:%d\n", pos, len);
return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, simplefs_get_block);
}
static sector_t simplefs_bmap(struct address_space *mapping, sector_t block) {
printk(KERN_INFO "simplefs_bmap\n");
return generic_block_bmap(mapping, block, simplefs_get_block);
}
const struct address_space_operations simplefs_aops = {
.readpage = simplefs_readpage,
.writepage = simplefs_writepage,
.sync_page = block_sync_page,
.write_begin = simplefs_write_begin,
.write_end = generic_write_end,
.bmap = simplefs_bmap,
};
void simplefs_put_page(struct page *page) {
kunmap(page);
page_cache_release(page);
}
struct page *simplefs_get_page(struct inode *dir, unsigned long n) {
struct address_space *mapping = dir->i_mapping;
struct page *page = read_mapping_page(mapping, n, NULL);
if (!IS_ERR(page)) {
kmap(page);
if (!PageUptodate(page))
goto fail;
}
return page;
fail:
simplefs_put_page(page);
return ERR_PTR(-EIO);
}
/*
* 0 : not same
* 1 : the same
*/
static inline int namecompare(int len, int maxlen, const char *name, const char *buffer) {
if (len < maxlen && buffer[len])
return 0;
return !memcmp(name, buffer, len);
}
struct simplefs_dentry *simplefs_find_dentry(struct inode *inode,
struct dentry *dentry, struct page **rs_page) {
unsigned long n, npages = inode_pages(inode);
printk(KERN_INFO "simplefs_find_dentry: %s\n", dentry->d_name.name);
for (n = 0; n < npages; n++) {
char *p, *kaddr, *limit;
struct page *page = simplefs_get_page(inode, n);
if (IS_ERR(page)) {
printk(KERN_INFO "page error\n");
continue;
}
p = kaddr = (char *)page_address(page);
limit = kaddr + inode_last_bytes(inode, n) - SIMPLEFS_DENTRY_SIZE;
for ( ; p <= limit; p += SIMPLEFS_DENTRY_SIZE) {
struct simplefs_dentry *de = (struct simplefs_dentry *) p;
if (de->flags == SIMPLEFS_DENTRY_UNUSED) {
printk(KERN_INFO "found an unused dentry: %s\n", de->name);
continue;
}
if (namecompare(dentry->d_name.len, SIMPLEFS_NAME_LEN,
dentry->d_name.name, de->name)) {
printk(KERN_INFO "match dentry: %s %s\n", dentry->d_name.name, de->name);
*rs_page = page;
return de;
}
}
simplefs_put_page(page);
}
return NULL;
}
int simplefs_insert_dentry(struct dentry *dentry, struct inode *inode) {
struct inode *dir = dentry->d_parent->d_inode;
const char *name = dentry->d_name.name;
char *kaddr;
int namelen = dentry->d_name.len;
struct page *page;
struct simplefs_dentry de = {};
int pos, err = 0;
printk(KERN_INFO "simplefs_insert_dentry\n");
page = simplefs_get_page(dir, dir->i_size >> PAGE_CACHE_SHIFT);
if (IS_ERR(page)) {
printk(KERN_ERR "simplefs_insert_dentry failed: i/o error\n");
return -EIO;
}
lock_page(page);
kaddr = (char *)page_address(page) + (dir->i_size & ~PAGE_CACHE_MASK);
pos = page_offset(page) + kaddr - (char *)page_address(page);
block_write_begin(NULL, page->mapping, pos, sizeof(de), 0, &page, NULL, simplefs_get_block);
de.inode = inode->i_ino;
de.flags = SIMPLEFS_DENTRY_INUSED;
memcpy(&de.name, name, namelen);
memcpy(kaddr, &de, sizeof(de));
block_write_end(NULL, page->mapping, pos, sizeof(de), sizeof(de), page, NULL);
i_size_write(dir, pos + sizeof(de));
dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
mark_inode_dirty(dir);
if (IS_DIRSYNC(dir)) {
err = write_one_page(page, 1);
if (!err)
err = simplefs_sync_inode(dir);
} else
unlock_page(page);
simplefs_put_page(page);
printk("simplefs insert dentry: %d\n", err);
return err;
}
int simplefs_delete_dentry(struct simplefs_dentry *de, struct page *page) {
struct address_space *mapping = page->mapping;
struct inode *inode = (struct inode *)mapping->host;
char *kaddr = page_address(page);
loff_t pos = page_offset(page) + (char *)de - kaddr;
int err = 0;
printk(KERN_INFO "simplefs_delete_dentry: %s\n", de->name);
lock_page(page);
err = block_write_begin(NULL, mapping, pos, sizeof(*de), 0, &page, NULL, simplefs_get_block);
BUG_ON(err);
de->flags = SIMPLEFS_DENTRY_UNUSED;
block_write_end(NULL, mapping, pos, sizeof(*de), sizeof(*de), page, NULL);
inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
if (IS_DIRSYNC(inode)) {
err = write_one_page(page, 1);
if (!err)
err = simplefs_sync_inode(inode);
} else
unlock_page(page);
simplefs_put_page(page);
return err;
}