forked from rgouicem/ouichefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
198 lines (174 loc) · 5.55 KB
/
file.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
// SPDX-License-Identifier: GPL-2.0
/*
* ouiche_fs - a simple educational filesystem for Linux
*
* Copyright (C) 2018 Redha Gouicem <[email protected]>
*/
#define pr_fmt(fmt) "ouichefs: " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/buffer_head.h>
#include <linux/mpage.h>
#include "ouichefs.h"
#include "bitmap.h"
/*
* Map the buffer_head passed in argument with the iblock-th block of the file
* represented by inode. If the requested block is not allocated and create is
* true, allocate a new block on disk and map it.
*/
static int ouichefs_file_get_block(struct inode *inode, sector_t iblock,
struct buffer_head *bh_result, int create)
{
struct super_block *sb = inode->i_sb;
struct ouichefs_sb_info *sbi = OUICHEFS_SB(sb);
struct ouichefs_inode_info *ci = OUICHEFS_INODE(inode);
struct ouichefs_file_index_block *index;
struct buffer_head *bh_index;
bool alloc = false;
int ret = 0, bno;
/* If block number exceeds filesize, fail */
if (iblock >= OUICHEFS_BLOCK_SIZE >> 2)
return -EFBIG;
/* Read index block from disk */
bh_index = sb_bread(sb, ci->index_block);
if (!bh_index)
return -EIO;
index = (struct ouichefs_file_index_block *)bh_index->b_data;
/*
* Check if iblock is already allocated. If not and create is true,
* allocate it. Else, get the physical block number.
*/
if (index->blocks[iblock] == 0) {
if (!create)
return 0;
bno = get_free_block(sbi);
if (!bno) {
ret = -ENOSPC;
goto brelse_index;
}
index->blocks[iblock] = bno;
alloc = true;
} else {
bno = index->blocks[iblock];
}
/* Map the physical block to to the given buffer_head */
map_bh(bh_result, sb, bno);
brelse_index:
brelse(bh_index);
return ret;
}
/*
* Called by the page cache to read a page from the physical disk and map it in
* memory.
*/
static int ouichefs_readpage(struct file *file, struct page *page)
{
return mpage_readpage(page, ouichefs_file_get_block);
}
/*
* Called by the page cache to write a dirty page to the physical disk (when
* sync is called or when memory is needed).
*/
static int ouichefs_writepage(struct page *page, struct writeback_control *wbc)
{
return block_write_full_page(page, ouichefs_file_get_block, wbc);
}
/*
* Called by the VFS when a write() syscall occurs on file before writing the
* data in the page cache. This functions checks if the write will be able to
* complete and allocates the necessary blocks through block_write_begin().
*/
static int ouichefs_write_begin(struct file *file,
struct address_space *mapping, loff_t pos,
unsigned int len, unsigned int flags,
struct page **pagep, void **fsdata)
{
struct ouichefs_sb_info *sbi = OUICHEFS_SB(file->f_inode->i_sb);
int err;
uint32_t nr_allocs = 0;
/* Check if the write can be completed (enough space?) */
if (pos + len > OUICHEFS_MAX_FILESIZE)
return -ENOSPC;
nr_allocs = max(pos + len, file->f_inode->i_size) / OUICHEFS_BLOCK_SIZE;
if (nr_allocs > file->f_inode->i_blocks - 1)
nr_allocs -= file->f_inode->i_blocks - 1;
else
nr_allocs = 0;
if (nr_allocs > sbi->nr_free_blocks)
return -ENOSPC;
/* prepare the write */
err = block_write_begin(mapping, pos, len, flags, pagep,
ouichefs_file_get_block);
/* if this failed, reclaim newly allocated blocks */
if (err < 0) {
pr_err("%s:%d: newly allocated blocks reclaim not implemented yet\n",
__func__, __LINE__);
}
return err;
}
/*
* Called by the VFS after writing data from a write() syscall to the page
* cache. This functions updates inode metadata and truncates the file if
* necessary.
*/
static int ouichefs_write_end(struct file *file, struct address_space *mapping,
loff_t pos, unsigned int len, unsigned int copied,
struct page *page, void *fsdata)
{
int ret;
struct inode *inode = file->f_inode;
struct ouichefs_inode_info *ci = OUICHEFS_INODE(inode);
struct super_block *sb = inode->i_sb;
/* Complete the write() */
ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
if (ret < len) {
pr_err("%s:%d: wrote less than asked... what do I do? nothing for now...\n",
__func__, __LINE__);
} else {
uint32_t nr_blocks_old = inode->i_blocks;
/* Update inode metadata */
inode->i_blocks = inode->i_size / OUICHEFS_BLOCK_SIZE + 2;
inode->i_mtime = inode->i_ctime = current_time(inode);
mark_inode_dirty(inode);
/* If file is smaller than before, free unused blocks */
if (nr_blocks_old > inode->i_blocks) {
int i;
struct buffer_head *bh_index;
struct ouichefs_file_index_block *index;
/* Free unused blocks from page cache */
truncate_pagecache(inode, inode->i_size);
/* Read index block to remove unused blocks */
bh_index = sb_bread(sb, ci->index_block);
if (!bh_index) {
pr_err("failed truncating '%s'. we just lost %lu blocks\n",
file->f_path.dentry->d_name.name,
nr_blocks_old - inode->i_blocks);
goto end;
}
index = (struct ouichefs_file_index_block *)
bh_index->b_data;
for (i = inode->i_blocks - 1; i < nr_blocks_old - 1;
i++) {
put_block(OUICHEFS_SB(sb), index->blocks[i]);
index->blocks[i] = 0;
}
mark_buffer_dirty(bh_index);
brelse(bh_index);
}
}
end:
return ret;
}
const struct address_space_operations ouichefs_aops = {
.readpage = ouichefs_readpage,
.writepage = ouichefs_writepage,
.write_begin = ouichefs_write_begin,
.write_end = ouichefs_write_end
};
const struct file_operations ouichefs_file_ops = {
.owner = THIS_MODULE,
.llseek = generic_file_llseek,
.read_iter = generic_file_read_iter,
.write_iter = generic_file_write_iter
};