forked from sysprog21/simplefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
219 lines (193 loc) · 7.05 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#define pr_fmt(fmt) "simplefs: " fmt
#include <linux/buffer_head.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mpage.h>
#include "bitmap.h"
#include "simplefs.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 simplefs_file_get_block(struct inode *inode,
sector_t iblock,
struct buffer_head *bh_result,
int create)
{
struct super_block *sb = inode->i_sb;
struct simplefs_sb_info *sbi = SIMPLEFS_SB(sb);
struct simplefs_inode_info *ci = SIMPLEFS_INODE(inode);
struct simplefs_file_ei_block *index;
struct buffer_head *bh_index;
bool alloc = false;
int ret = 0, bno;
uint32_t extent;
/* If block number exceeds filesize, fail */
if (iblock >= SIMPLEFS_MAX_BLOCKS_PER_EXTENT * SIMPLEFS_MAX_EXTENTS)
return -EFBIG;
/* Read directory block from disk */
bh_index = sb_bread(sb, ci->dir_block);
if (!bh_index)
return -EIO;
index = (struct simplefs_file_ei_block *) bh_index->b_data;
extent = simplefs_ext_search(index, iblock);
if (extent == -1) {
ret = -EFBIG;
goto brelse_index;
}
/*
* Check if iblock is already allocated. If not and create is true,
* allocate it. Else, get the physical block number.
*/
if (index->extents[extent].ee_start == 0) {
if (!create)
return 0;
bno = get_free_blocks(sbi, 8);
if (!bno) {
ret = -ENOSPC;
goto brelse_index;
}
index->extents[extent].ee_start = bno;
index->extents[extent].ee_len = 8;
index->extents[extent].ee_block =
extent ? index->extents[extent - 1].ee_block +
index->extents[extent - 1].ee_len
: 0;
alloc = true;
} else {
bno = index->extents[extent].ee_start + iblock -
index->extents[extent].ee_block;
}
/* 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 simplefs_readpage(struct file *file, struct page *page)
{
return mpage_readpage(page, simplefs_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 simplefs_writepage(struct page *page, struct writeback_control *wbc)
{
return block_write_full_page(page, simplefs_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 simplefs_write_begin(struct file *file,
struct address_space *mapping,
loff_t pos,
unsigned int len,
unsigned int flags,
struct page **pagep,
void **fsdata)
{
struct simplefs_sb_info *sbi = SIMPLEFS_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 > SIMPLEFS_MAX_FILESIZE)
return -ENOSPC;
nr_allocs = max(pos + len, file->f_inode->i_size) / SIMPLEFS_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,
simplefs_file_get_block);
/* if this failed, reclaim newly allocated blocks */
if (err < 0)
pr_err("newly allocated blocks reclaim not implemented yet\n");
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 simplefs_write_end(struct file *file,
struct address_space *mapping,
loff_t pos,
unsigned int len,
unsigned int copied,
struct page *page,
void *fsdata)
{
struct inode *inode = file->f_inode;
struct simplefs_inode_info *ci = SIMPLEFS_INODE(inode);
struct super_block *sb = inode->i_sb;
uint32_t nr_blocks_old;
/* Complete the write() */
int ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
if (ret < len) {
pr_err("wrote less than requested.");
return ret;
}
nr_blocks_old = inode->i_blocks;
/* Update inode metadata */
inode->i_blocks = inode->i_size / SIMPLEFS_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 simplefs_file_ei_block *index;
uint32_t first_ext;
/* Free unused blocks from page cache */
truncate_pagecache(inode, inode->i_size);
/* Read ei_block to remove unused blocks */
bh_index = sb_bread(sb, ci->ei_block);
if (!bh_index) {
pr_err("failed truncating '%s'. we just lost %llu blocks\n",
file->f_path.dentry->d_name.name,
nr_blocks_old - inode->i_blocks);
goto end;
}
index = (struct simplefs_file_ei_block *) bh_index->b_data;
first_ext = simplefs_ext_search(index, inode->i_blocks - 1);
/* Reserve unused block in last extent */
if (inode->i_blocks - 1 != index->extents[first_ext].ee_block)
first_ext++;
for (i = first_ext; i < SIMPLEFS_MAX_EXTENTS; i++) {
if (!index->extents[i].ee_start)
break;
put_blocks(SIMPLEFS_SB(sb), index->extents[i].ee_start,
index->extents[i].ee_len);
memset(&index->extents[i], 0, sizeof(struct simplefs_extent));
}
mark_buffer_dirty(bh_index);
brelse(bh_index);
}
end:
return ret;
}
const struct address_space_operations simplefs_aops = {
.readpage = simplefs_readpage,
.writepage = simplefs_writepage,
.write_begin = simplefs_write_begin,
.write_end = simplefs_write_end,
};
const struct file_operations simplefs_file_ops = {
.llseek = generic_file_llseek,
.owner = THIS_MODULE,
.read_iter = generic_file_read_iter,
.write_iter = generic_file_write_iter,
.fsync = generic_file_fsync,
};