-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfile.c
272 lines (233 loc) · 6.19 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
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
#include "lib.h"
#include <fs.h>
#define debug 0
static int file_close(struct Fd *fd);
static int file_read(struct Fd *fd, void *buf, u_int n, u_int offset);
static int file_write(struct Fd *fd, const void *buf, u_int n, u_int offset);
static int file_stat(struct Fd *fd, struct Stat *stat);
struct Dev devfile =
{
.dev_id= 'f',
.dev_name= "file",
.dev_read= file_read,
.dev_write= file_write,
.dev_close= file_close,
.dev_stat= file_stat,
};
// Open a file (or directory),
// returning the file descriptor on success, < 0 on failure.
int
open(const char *path, int mode)
{
// Your code here.
struct Fd *fd;
struct Filefd *ffd;
u_int size,fileid;
int r;
u_int va;
u_int i;
//writef("IIIIIIIIIIIIIenter open\n");
//alloc a fd
if((r = fd_alloc(&fd))<0)
{
writef("Without free fd left\n");
return r;
}
//writef("IIIIIIIIIIIopen:come 1\n");
/*
if((r = syscall_mem_alloc(0, (u_int)fd, PTE_P|PTE_U|PTE_W))<0)
{
writef("cannot map the page where the fd locates\n");
return r;
}
writef("open:come 2\n");
*/
//fd is the file descriptor of the opened file
//writef("IIIIIIIIIIIopen:fd = %x\n",fd);
if((r = fsipc_open(path, mode, fd))<0)
{
writef("cannont open file %s\n",path);
return r;
}
va = fd2data(fd); //the start address storing the file's content
ffd = (struct Filefd*)fd;
size = ffd->f_file.f_size;
fileid = ffd->f_fileid;
//writef("open:ffd = %x, size = %x, fileid=%d, va =%x\n",(u_int)ffd, size, fileid,va);
//map the file content into memory
if(size == 0) return fd2num(fd);
for( i = 0; i < size; i+=BY2PG)
{
if((r = fsipc_map(fileid, i, va+i))<0)
{
writef("cannot map the file.\n");
return r;
}
//writef("i=%x, (* vpd)[PDX(%x)]&PTE_P=%x, (* vpt)[VPN(%x)]&PTE_P=%x\n",i,va+i,(* vpd)[PDX(va+i)]&PTE_V,va+i,(* vpt)[VPN(va+i)]&PTE_V);
}
//writef("open:ffd = %x\n",(u_int)ffd);
return fd2num(fd);
//user_panic("open() unimplemented!");
}
// Close a file descriptor
int
file_close(struct Fd *fd)
{
// Your code here.
int r;
struct Filefd *ffd;
u_int va,size,fileid;
u_int i;
ffd = (struct Filefd*)fd;
fileid = ffd->f_fileid;
size = ffd->f_file.f_size;
va = fd2data(fd); //the start address storing the file's content
//tell the file server the dirty page.
for(i = 0; i < size; i += BY2PG)
{
//our framework can't PTE_D
//if(((* vpt)[(va+i)/BY2PG] & PTE_D)!=0)
fsipc_dirty(fileid, i);
}
//request the file server to close the file
if((r = fsipc_close(fileid))<0)
{
writef("cannot close the file\n");
return r;
}
//unmap the content of file
if(size == 0) return 0;
for(i = 0; i < size; i +=BY2PG)
{
if((r = syscall_mem_unmap(0, va+i))<0)
{
writef("cannont unmap the file.\n");
return r;
}
}
//close the file descriptor
fd_close(fd);
return 0;
//user_panic("close() unimplemented!");
}
// Read 'n' bytes from 'fd' at the current seek position into 'buf'.
// Since files are memory-mapped, this amounts to a user_bcopy()
// surrounded by a little red tape to handle the file size and seek pointer.
static int
file_read(struct Fd *fd, void *buf, u_int n, u_int offset)
{
u_int size;
struct Filefd *f;
// writef("file_read() come 1\n");
f = (struct Filefd*)fd;
// avoid reading past the end of file
size = f->f_file.f_size;
if (offset > size)
return 0;
if (offset+n > size)
n = size - offset;
// read the data by copying from the file mapping
// writef("file_read(): bcopy(): src:%x dst:%x len:%x \n",(int)(char*)fd2data(fd)+offset, buf, n);
user_bcopy((char*)fd2data(fd)+offset, buf, n);
// writef("file_read() come 2\n");
return n;
}
// Find the virtual address of the page
// that maps the file block starting at 'offset'.
int
read_map(int fdnum, u_int offset, void **blk)
{
int r;
u_int va;
struct Fd *fd;
if ((r = fd_lookup(fdnum, &fd)) < 0)
return r;
if (fd->fd_dev_id != devfile.dev_id)
return -E_INVAL;
va = fd2data(fd) + offset;
if (offset >= MAXFILESIZE)
return -E_NO_DISK;
//writef("offset=%x, va=%x, (* vpd)[PDX(va)]&PTE_P=%x, (* vpt)[VPN(va)]&PTE_P=%x\n",offset,va,(* vpd)[PDX(va)]&PTE_V,(* vpt)[VPN(va)]&PTE_V);
if (!((* vpd)[PDX(va)]&PTE_V) || !((* vpt)[VPN(va)]&PTE_V))
{
return -E_NO_DISK;
}
*blk = (void*)va;
return 0;
}
// Write 'n' bytes from 'buf' to 'fd' at the current seek position.
static int
file_write(struct Fd *fd, const void *buf, u_int n, u_int offset)
{
int r;
u_int tot;
struct Filefd *f;
f = (struct Filefd*)fd;
// don't write past the maximum file size
tot = offset + n;
if (tot > MAXFILESIZE)
return -E_NO_DISK;
// increase the file's size if necessary
if (tot > f->f_file.f_size) {
if ((r = ftruncate(fd2num(fd), tot)) < 0)
return r;
}
// write the data
user_bcopy(buf, (char*)fd2data(fd)+offset, n);
return n;
}
static int
file_stat(struct Fd *fd, struct Stat *st)
{
struct Filefd *f;
f = (struct Filefd*)fd;
strcpy(st->st_name, f->f_file.f_name);
st->st_size = f->f_file.f_size;
st->st_isdir = f->f_file.f_type==FTYPE_DIR;
return 0;
}
// Truncate or extend an open file to 'size' bytes
int
ftruncate(int fdnum, u_int size)
{
int i, r;
struct Fd *fd;
struct Filefd *f;
u_int oldsize, va, fileid;
if (size > MAXFILESIZE)
return -E_NO_DISK;
if ((r = fd_lookup(fdnum, &fd)) < 0)
return r;
if (fd->fd_dev_id != devfile.dev_id)
return -E_INVAL;
f = (struct Filefd*)fd;
fileid = f->f_fileid;
oldsize = f->f_file.f_size;
if ((r = fsipc_set_size(fileid, size)) < 0)
return r;
va = fd2data(fd);
// Map any new pages needed if extending the file
for (i = ROUND(oldsize, BY2PG); i < ROUND(size, BY2PG); i += BY2PG) {
if ((r = fsipc_map(fileid, i, va+i)) < 0) {
fsipc_set_size(fileid, oldsize);
return r;
}
}
// Unmap pages if truncating the file
for (i = ROUND(size, BY2PG); i < ROUND(oldsize, BY2PG); i+=BY2PG)
if ((r = syscall_mem_unmap(0, va+i)) < 0)
user_panic("ftruncate: syscall_mem_unmap %08x: %e", va+i, r);
return 0;
}
// Delete a file
int
remove(const char *path)
{
return fsipc_remove(path);
}
// Synchronize disk with buffer cache
int
sync(void)
{
return fsipc_sync();
}