From e5eb76c5bb68581af441bf6c8a00fbf2c078c995 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Thu, 1 May 2008 16:02:03 +0800 Subject: [PATCH] open/close implemented. Signed-off-by: Guanqun Lu --- fs/serv.c | 14 ++++++++++---- lib/fd.c | 23 +++++++++++++++++++---- lib/file.c | 29 ++++++++++++++++++++++++----- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/fs/serv.c b/fs/serv.c index 6f6fd28..6cfbb47 100644 --- a/fs/serv.c +++ b/fs/serv.c @@ -220,17 +220,23 @@ void serve_close(envid_t envid, struct Fsreq_close *rq) { struct OpenFile *o; - int r; + int r = 0; if (debug) cprintf("serve_close %08x %08x\n", envid, rq->req_fileid); - // Close the file. - // o->o_fd unmapped??? if ((r = openfile_lookup(envid, rq->req_fileid, &o)) < 0) - ipc_send(envid, r, 0, 0); + goto out; + // unmap o->o_fd + sys_page_unmap(0, o->o_fd); + // close the file. file_close(o->o_file); + // make the fileid to the original + // so that stale fileid is not available + o->o_fileid = o->o_fileid % MAXOPEN; + +out: ipc_send(envid, 0, 0, 0); } diff --git a/lib/fd.c b/lib/fd.c index ded4b51..481a306 100644 --- a/lib/fd.c +++ b/lib/fd.c @@ -50,9 +50,17 @@ fd2num(struct Fd *fd) int fd_alloc(struct Fd **fd_store) { - // LAB 5: Your code here. + struct Fd *fd; + int i; - panic("fd_alloc not implemented"); + for (i = 0; i < MAXFD; i++) { + fd = INDEX2FD(i); + if (pageref(fd) == 0) { + *fd_store = fd; + return 0; + } + } + *fd_store = 0; return -E_MAX_OPEN; } @@ -65,9 +73,16 @@ fd_alloc(struct Fd **fd_store) int fd_lookup(int fdnum, struct Fd **fd_store) { - // LAB 5: Your code here. + struct Fd *fd; - panic("fd_lookup not implemented"); + if (fdnum >= 0 && fdnum < MAXFD) { + fd = INDEX2FD(fdnum); + if (pageref(fd) > 0) { + *fd_store = fd; + return 0; + } + } + *fd_store = 0; return -E_INVAL; } diff --git a/lib/file.c b/lib/file.c index 448ab10..21933be 100644 --- a/lib/file.c +++ b/lib/file.c @@ -39,9 +39,20 @@ open(const char *path, int mode) // Then map the file data (you may find fmap() helpful). // Return the file descriptor index. // If any step fails, use fd_close to free the file descriptor. + int r; + struct Fd *fd; - // LAB 5: Your code here. - panic("open() unimplemented!"); + if ((r = fd_alloc(&fd)) < 0) + return r; + if ((r = fsipc_open(path, mode, fd)) < 0) { + fd_close(fd, 0); + return r; + } + if ((r = fmap(fd, 0, fd->fd_file.file.f_size)) < 0) { + fd_close(fd, 0); + return r; + } + return fd2num(fd); } // Clean up a file-server file descriptor. @@ -52,9 +63,17 @@ file_close(struct Fd *fd) // Unmap any data mapped for the file, // then tell the file server that we have closed the file // (to free up its resources). + off_t size; + int id, r; + + size = fd->fd_file.file.f_size; + id = fd->fd_file.id; - // LAB 5: Your code here. - panic("close() unimplemented!"); + if ((r = funmap(fd, size, size, 1)) < 0) + return r; + fsipc_close(id); + + return 0; } // Read 'n' bytes from 'fd' at the current seek position into 'buf'. @@ -203,7 +222,7 @@ funmap(struct Fd* fd, off_t oldsize, off_t newsize, bool dirty) ret = r; sys_page_unmap(0, va + i); } - return ret; + return ret; } // Delete a file -- 2.11.4.GIT