-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.c
More file actions
394 lines (347 loc) · 11.5 KB
/
file.c
File metadata and controls
394 lines (347 loc) · 11.5 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "lightfs.h"
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include <linux/mpage.h>
static __u32 physical_to_logical(__u32 physical, struct super_block *sb)
{
struct lightfs_superblock *sbi = sb->s_fs_info;
return physical * (sbi->block_size / LIGHTFS_LOGICAL_BS) - ((sbi->block_size / LIGHTFS_LOGICAL_BS) - 1);
}
char *get_block(struct super_block *sb, __u32 num)
{
struct buffer_head **bh = NULL;
struct lightfs_superblock *sbi = sb->s_fs_info;
int logical_per_physical = (sbi->block_size / LIGHTFS_LOGICAL_BS);
size_t inode_bmap_no;
size_t data_bmap_no;
size_t inode_lb_no;
inode_bmap_no = sbi->inode_block_num / LIGHTFS_LOGICAL_BS + 1;
data_bmap_no = sbi->data_block_num / LIGHTFS_LOGICAL_BS + 1;
if((sbi->inode_block_num % 4) == 0) {
inode_lb_no = sbi->inode_block_num / 4;
} else {
inode_lb_no = sbi->inode_block_num / 4 + 1;
}
size_t db_offset = 1 + data_bmap_no + inode_bmap_no + inode_lb_no + (num * 4);
//TODO: validate the calculation method
char *buf = (char *)kmalloc(sbi->block_size, GFP_KERNEL);
if(!buf) {
return NULL;
}
unsigned int i;
for(i = 0; i<logical_per_physical; i++) {
bh[i] = sb_bread(sb, db_offset+i);
if(!bh)
{
kfree(buf);
return NULL;
}
}
buf = blkcpy(bh, sbi);
return buf;
}
static int lightfs_file_get_block(struct inode *inode,
sector_t iblock,
struct buffer_head *bh_result,
int create)
{
__u32 blk_num = (__u32)iblock;
bh_result = alloc_buffer_head(GFP_KERNEL);
if(!bh_result)
{
printk(KERN_ERR "Error while allocating buffer head at get_block\n");
return -EINVAL;
}
memset(bh_result, 0, sizeof(struct buffer_head));
struct super_block *sb = inode->i_sb;
struct lightfs_superblock *sbi = sb->s_fs_info;
char *res;
res = get_block(sb, blk_num);
if(res == NULL) {
printk(KERN_ERR "Error 1 in file_get_block\n");
brelse(bh_result);
return -EIO;
}
bh_result->b_blocknr = iblock;
bh_result->b_bdev = inode->i_sb->s_bdev;
bh_result->b_size = sbi->block_size;
bh_result->b_data = res;
return 0;
}
static int lightfs_get_logical(struct inode *inode,
sector_t iblock,
struct buffer_head *bh_result,
int create)
{
__u32 b_num = (__u32)iblock;
struct super_block *sb = inode->i_sb;
struct lightfs_superblock *sbi = sb->s_fs_info;
struct buffer_head *bh = NULL;
size_t ino;
int ino_shift = (sbi->inode_block_num * sizeof(struct lightfs_inode)) % LIGHTFS_LOGICAL_BS;
if(ino_shift == 0)
ino = ((sbi->inode_block_num * sizeof(struct lightfs_inode)) / LIGHTFS_LOGICAL_BS);
else
ino = ((sbi->inode_block_num * sizeof(struct lightfs_inode)) / LIGHTFS_LOGICAL_BS) + 1;
loff_t start_block = 1 + ((sbi->data_block_num / LIGHTFS_LOGICAL_BS) + 1) + ((sbi->inode_block_num / LIGHTFS_LOGICAL_BS) + 1) + ino + b_num;
bh = sb_bread(sb, start_block);
if(!bh){
return -EINVAL;
}
bh_result = bh;
return 0;
}
char *blkcpy(struct buffer_head **bh, struct lightfs_superblock *sbi)
{
char *buf=(char *)kmalloc(sbi->block_size, GFP_KERNEL);
unsigned int i;
for(i=0; i<(sbi->block_size / LIGHTFS_LOGICAL_BS); i++){
memcpy(buf+(i*1024), bh[i]->b_data, LIGHTFS_LOGICAL_BS);
printk(KERN_INFO "Copying logical block %d to buffer\n", i);
}
printk(KERN_INFO "Copy of one block complete\n");
for(i=0; i<(sbi->block_size / LIGHTFS_LOGICAL_BS); i++){
brelse(bh[i]);
}
return buf;
}
int sync_block(struct super_block *sb, __u32 block_no, char *buf)
{
if (!sb || !block_no || !buf) return -EINVAL;
unsigned int i;
struct buffer_head **bh;
struct lightfs_superblock *sbi = sb->s_fs_info;
bh = get_block_bh(sb, block_no);
if(!bh) {
return -EIO;
}
for(i=0; i<(sbi->block_size / LIGHTFS_LOGICAL_BS); i++) {
memcpy(bh[i]->b_data, buf+(i*1024), LIGHTFS_LOGICAL_BS);
printk(KERN_INFO "syncing logical block %d with disk\n", i);
mark_buffer_dirty(bh[i]);
brelse(bh[i]);
}
if(change_bbitmap(sb, block_no) < 0) {
kfree(buf);
return -EFAULT;
}
kfree(buf);
return 0;
}
//additional cleanup job for block, soon to change & deprecated
void block_cleanup(struct buffer_head **bh, struct lightfs_superblock *sbi)
{
int blk_num = sbi->block_size / LIGHTFS_LOGICAL_BS;
unsigned int i;
for(i = 0; i<blk_num; i++) {
mark_buffer_dirty(bh[i]);
brelse(bh[i]);
}
}
static int lightfs_open(struct inode *inode, struct file *file)
{
struct lightfs_inode_info *i_info = inode->i_private;
struct super_block *sb = inode->i_sb;
bool wronly = (file->f_flags & O_WRONLY);
bool rdwr = (file->f_flags & O_RDWR);
bool trunc = (file->f_flags & O_TRUNC);
if((wronly || rdwr) && trunc && inode->i_size) {
//TODO: Think of a trunication method
}
return 0;
}
static ssize_t lightfs_read(struct file *file,
char __user *buf,
size_t len,
loff_t *ppos)
{
struct inode *inode = file->f_mapping->host;
struct lightfs_inode_info *ci = inode->i_private;
struct address_space *mapping = inode->i_mapping;
struct super_block *sb = inode->i_sb;
struct lightfs_superblock *sbi = sb->s_fs_info;
ssize_t ret = 0;
loff_t pos = *ppos;
loff_t size = i_size_read(inode);
loff_t start_block = pos / sbi->block_size;
__u32 **b_num = ci->block;
char *kbuf = (char *)kmalloc(size, GFP_KERNEL);
char *block;
if(b_num[1] == NULL)
return -ENOENT;
__u32 number_of_blocks;
loff_t data_shift = size % sbi->block_size;
if(data_shift == 0) {
number_of_blocks = size / sbi->block_size;
} else {
number_of_blocks = size / sbi->block_size + 1;
}
char *dbuf = (char *)kmalloc(number_of_blocks * sbi->block_size, GFP_KERNEL);
uint i;
for(i=0; i<number_of_blocks; i++) {
block = get_block(sb, *b_num[i+start_block]);
if(block == NULL){
kfree(kbuf);
return -EINVAL;
}
kfree(block);
}
memcpy(kbuf, dbuf + data_shift, size);
if (data_shift + size > number_of_blocks * sbi->block_size) {
kfree(kbuf);
kfree(dbuf);
return -EINVAL;
}
if(copy_to_user(buf, kbuf, size)) {
kfree(kbuf);
kfree(dbuf);
return -EFAULT;
}
ret += size;
len -= size;
pos += size;
return ret;
}
static ssize_t lightfs_write(struct file *file,
const char __user *buf,
size_t len,
loff_t *ppos)
{
struct inode *inode = file->f_inode;
struct super_block *sb = inode->i_sb;
struct lightfs_superblock *sbi = sb->s_fs_info;
struct lightfs_inode_info *ci = inode->i_private;
struct buffer_head *bh;
loff_t pos = *ppos;
size_t size = i_size_read(inode);
ssize_t ret = 0;
__u32 number_of_blocks;
loff_t start_block = pos / sbi->block_size;
loff_t block_shift = size % sbi->block_size;
if (!file || !buf || !ppos)
return -EINVAL;
if(block_shift == 0) {
number_of_blocks = size / sbi->block_size;
} else {
number_of_blocks = size / sbi->block_size + 1;
}
__u32 **b_num = ci->block;
char *dat = (char *)kmalloc(size, GFP_KERNEL);
if(!dat) {
return -ENOMEM;
}
if(copy_from_user(dat, buf, len)) {
kfree(dat);
return -EFAULT;
}
size_t i;
__u32 block_size = sbi->block_size;
char *block;
for(i = 0; i<number_of_blocks; i++) {
block = get_block(sb, *(b_num[i + start_block]));
memcpy(block, dat + i * sbi->block_size, block_size);
sync_block(sb, *(b_num[i+start_block]), block);
}
ret += size;
len -= size;
pos += size;
*ppos = pos;
kfree(dat);
return ret;
}
struct buffer_head **get_block_bh(struct super_block *sb, __u32 num)
{
struct buffer_head **bh = NULL;
struct lightfs_superblock *sbi = sb->s_fs_info;
int logical_per_physical = (sbi->block_size / LIGHTFS_LOGICAL_BS);
size_t inode_bmap_no;
size_t data_bmap_no;
size_t inode_lb_no;
inode_bmap_no = sbi->inode_block_num / LIGHTFS_LOGICAL_BS + 1;
data_bmap_no = sbi->data_block_num / LIGHTFS_LOGICAL_BS + 1;
if((sbi->inode_block_num % 4) == 0) {
inode_lb_no = sbi->inode_block_num / 4;
} else {
inode_lb_no = sbi->inode_block_num / 4 + 1;
}
size_t db_offset = 1 + data_bmap_no + inode_bmap_no + inode_lb_no + (num * 4);
//TODO: validate the calculation method
unsigned int i;
for(i = 0; i<logical_per_physical; i++) {
bh[i] = sb_bread(sb, db_offset+i);
if(!bh) {
return NULL;
}
}
return bh;
}
static void lightfs_readahead(struct readahead_control *rac)
{
return mpage_readahead(rac, lightfs_file_get_block);
}
//TODO: since the physical block size and logical block sizes differ, it is required to think of a page mapping and writing code.
static int lightfs_writepage(struct page *page, struct writeback_control *wbc)
{
struct inode *ino = page->mapping->host;
struct lightfs_inode_info *ci = ino->i_private;
struct super_block *sb = ino->i_sb;
struct lightfs_superblock *sbi = sb->s_fs_info;
char *block;
loff_t offset = page_offset(page);
sector_t start_in_block = offset / sbi->block_size;
sector_t number_of_blocks;
sector_t block_shift = offset % sbi->block_size;
int err;
if(block_shift == 0)
number_of_blocks = PAGE_SIZE / sbi->block_size;
else
number_of_blocks = PAGE_SIZE / sbi->block_size + 1;
__u32 block_list[number_of_blocks];
sector_t number_of_logical;
int i;
lock_page(page);
char *page_data = kmap(page);
for(i=0; i<number_of_blocks; i++){
block_list[i] = *(ci->block[start_in_block + i]);
}
block = get_block(sb, block_list[0]);
memcpy(block + block_shift, page_data, PAGE_SIZE - block_shift);
sync_block(sb, block_list[0], block);
if(number_of_blocks == 2) {
block = get_block(sb, block_list[1]);
memcpy(block, page_data + (PAGE_SIZE - block_shift), block_shift);
sync_block(sb, block_list[1], block);
}
kunmap(page);
unlock_page(page);
err = 0;
lock_page(page);
unlock_page(page);
return err;
}
//Since most block I/O prep is done on lightfs_write, this code is mainly for checking permission and filesize.
static int lightfs_write_begin(struct file *file,
struct address_space *mapping,
loff_t pos,
unsigned int len,
struct folio **foliop,
void **fsdata)
{
if(len > LIGHTFS_MAX_FSIZE) {
return -ENOSPC;
}
return 0;
}
const struct file_operations lightfs_file_operations = {
.owner = THIS_MODULE,
.open = &lightfs_open,
.read = lightfs_read,
.write = lightfs_write,
.llseek = generic_file_llseek,
.fsync = generic_file_fsync,
};
const struct address_space_operations lightfs_addr_ops = {
.readahead = lightfs_readahead,
.write_begin = lightfs_write_begin,
.write_end = generic_write_end,
};