diff --git a/include/fs/fat32.h b/include/fs/fat32.h index f9282289e1e0017c9054192fb09cd9631c74a36c..4e7b517a913ce0709981f90ffa9bc696cd2853ca 100644 --- a/include/fs/fat32.h +++ b/include/fs/fat32.h @@ -92,7 +92,6 @@ typedef struct FAT32LongDirectory { #define CLUS_SIZE(fs) ((fs)->superBlock.bytes_per_clus) unsigned char checkSum(unsigned char *pFcbName); -void fat32Init(); void fat32Test(); typedef struct Dirent Dirent; diff --git a/include/fs/fd.h b/include/fs/fd.h index c78c1606a563d7da011df09f029ba793febd2bd5..7868f7eaaa6d98cd9d988bf927d9b6ffc347e5bc 100644 --- a/include/fs/fd.h +++ b/include/fs/fd.h @@ -3,14 +3,14 @@ #include <fs/fat32.h> #include <fs/fs.h> -#include <types.h> #include <fs/pipe.h> +#include <types.h> #define FDNUM 1024 struct Fd { Dirent *dirent; - struct Pipe* pipe; + struct Pipe *pipe; int type; uint offset; uint flags; diff --git a/include/fs/fs.h b/include/fs/fs.h index 94f6ff7592b5b69114da67375e6e92917831f718..edb2823988e57a62c7718da6beed60b920ccc47c 100644 --- a/include/fs/fs.h +++ b/include/fs/fs.h @@ -26,7 +26,7 @@ struct Dirent { // æ“作系统相关的数æ®ç»“æž„ enum { ZERO = 10, OSRELEASE = 12, NONE = 15 } dev; FileSystem *head; // 仅用于是挂载点的目录,指å‘该挂载点所对应的文件系统 - u32 off; // 在上一个目录项ä¸çš„内容å移,用于写回 + u32 off; // 在上一个目录项ä¸çš„内容å移,用于写回 LIST_ENTRY(Dirent) direntLink; // 自己的链接 struct DirentList childList; // åDirent列表 @@ -66,7 +66,7 @@ struct FileSystem { char name[8]; SuperBlock superBlock; // è¶…çº§å— Dirent root; // root项 - struct Dirent *image; // mount对应的文件æè¿°ç¬¦ + struct Dirent *image; // mount对应的文件æè¿°ç¬¦ int deviceNumber; // 对应真实设备的编å·ï¼Œæš‚ä¸ä½¿ç”¨ struct Buffer *(*get)(struct FileSystem *fs, u64 blockNum); // 读å–FS的一个Buffer // å¼ºåˆ¶è§„å®šï¼šä¼ å…¥çš„fså³ä¸ºæœ¬èº«çš„fs diff --git a/include/fs/pipe.h b/include/fs/pipe.h index 90fca028275bf209aab5e1620f5019b4530258a6..aff9bda434b781721cd966e2b02537802c8f6a02 100644 --- a/include/fs/pipe.h +++ b/include/fs/pipe.h @@ -6,9 +6,9 @@ struct Proc; struct Pipe { - u32 count; - u64 pipeReadPos; // read position - u64 pipeWritePos; // write position + u32 count; + u64 pipeReadPos; // read position + u64 pipeWritePos; // write position u8 pipeBuf[PIPE_BUF_SIZE]; // data buffer struct Proc *waitProc; }; diff --git a/include/lib/log.h b/include/lib/log.h index 373a59a6888b52cf95b5058bbe1bb6b83e8d4ed8..b6f03c04a5227b1d521ec2728ca5280521fe2626 100644 --- a/include/lib/log.h +++ b/include/lib/log.h @@ -37,7 +37,7 @@ void _warn(const char *, int, const char *, const char *, ...); * @brief è¦å‘Šæ—¥å¿—输出 */ #define warn(...) \ - if (1) \ + if (0) \ do { \ _warn(__FILE__, __LINE__, __func__, __VA_ARGS__); \ } while (0) diff --git a/kern/boot/main.c b/kern/boot/main.c index 9e67cd1d7f95c86add4c175a756b5d145292914b..c25522f0914b3cf9911099fcacc30a3badffbe74 100644 --- a/kern/boot/main.c +++ b/kern/boot/main.c @@ -6,6 +6,7 @@ #include <dev/virtio.h> #include <fs/buf.h> #include <fs/fat32.h> +#include <fs/vfs.h> #include <lib/printf.h> #include <mm/mmu.h> #include <mm/pmm.h> @@ -106,12 +107,12 @@ void main() { // bufTest(7); direntInit(); - fat32Init(); + initRootFs(); // fat32Test(); // testProcRun(); procInit(); - // PROC_CREATE(test_init, 1); + PROC_CREATE(test_init, 1); struct Proc *proc = PROC_CREATE(test_while, 2); // PROC_CREATE(test_pipe, 2); // PROC_CREATE(test_execve, 1); diff --git a/kern/fs/fat32.c b/kern/fs/fat32.c index 17d87939b459105737c8ed08b4a2ab762b21b556..a901ebcfb63dbd422c0377465e449c3cc7dfa6df 100644 --- a/kern/fs/fat32.c +++ b/kern/fs/fat32.c @@ -76,7 +76,6 @@ void initRootFs() { // mount之åŽï¼Œç›®å½•ä¸åŽŸæœ‰çš„æ–‡ä»¶å°†è¢«æš‚æ—¶å–代为挂载的文件系统内的内容,umountæ—¶ä¼šé‡æ–°å‡ºçް void mountFs() { - } // 簇缓冲区:簇最大为128个BUF_SIZE diff --git a/kern/fs/fd.c b/kern/fs/fd.c index 7ac349c2aa84508af72bf7d7053c93492de73307..683fd280517158bc94e9ebac2038c837eceb6f5a 100644 --- a/kern/fs/fd.c +++ b/kern/fs/fd.c @@ -1,13 +1,13 @@ #include <dev/sbi.h> #include <fs/fat32.h> #include <fs/fd.h> +#include <fs/pipe.h> #include <fs/vfs.h> #include <lib/log.h> #include <lib/string.h> #include <lib/transfer.h> #include <proc/proc.h> #include <proc/sleep.h> -#include <fs/pipe.h> static uint fdBitMap[FDNUM / 32] = {0}; struct Fd fds[FDNUM]; @@ -86,7 +86,7 @@ int closeFd(int fd) { warn("kern fd is wrong, please check\n"); return -1; } else { - kernFd = myProc()->fdList[fd]; + kernFd = myProc()->fdList[fd]; freeFd(kernFd); myProc()->fdList[fd] = -1; return 0; @@ -98,25 +98,25 @@ int closeFd(int fd) { * @brief å°†å†…æ ¸fd引用计数å‡ä¸€ */ void freeFd(uint i) { - struct Pipe * p; + struct Pipe *p; assert(i >= 0 && i < FDNUM); citesNum[i] -= 1; if (citesNum[i] == 0) { - // TODO 这里åŽç»è¦ç»§ç»ç‰¹æ®Šåˆ¤ï¼Œæ˜¯file对应的fdå…³é—还是pipe对应的fdå…³é— - // TODO 如果是file,是å¦è¦ç»§ç»è€ƒè™‘怎么回收Dirent - // TODO 如果是pipe对应的fdå…³é—,则需è¦å›žæ”¶struct pipeå¯¹åº”çš„å†…å˜ + // TODO 这里åŽç»è¦ç»§ç»ç‰¹æ®Šåˆ¤ï¼Œæ˜¯file对应的fdå…³é—还是pipe对应的fdå…³é— + // TODO 如果是file,是å¦è¦ç»§ç»è€ƒè™‘怎么回收Dirent + // TODO 如果是pipe对应的fdå…³é—,则需è¦å›žæ”¶struct pipeå¯¹åº”çš„å†…å˜ int index = i >> 5; int inner = i & 31; fdBitMap[index] &= ~(1 << inner); - if (fds[i].type == dev_pipe) { - p = fds[i].pipe; - p->count -= 1; - if (p && p->count == 0) { - kvmFree((u64)fds[i].pipe); //释放pipe结构体所在的物ç†å†…å˜ - } - } + if (fds[i].type == dev_pipe) { + p = fds[i].pipe; + p->count -= 1; + if (p && p->count == 0) { + kvmFree((u64)fds[i].pipe); //释放pipe结构体所在的物ç†å†…å˜ + } + } fds[i].dirent = NULL; - fds[i].pipe = NULL; + fds[i].pipe = NULL; fds[i].type = 0; fds[i].offset = 0; fds[i].flags = 0; @@ -129,7 +129,7 @@ int read(int fd, u64 buf, size_t count) { Dirent *dirent; int n, i; char ch; - struct Pipe *p; + struct Pipe *p; if (fd < 0 || fd >= MAX_FD_COUNT) { warn("read param fd is wrong, please check\n"); @@ -163,17 +163,20 @@ int read(int fd, u64 buf, size_t count) { fds[kernFd].offset += count; return count; } else { - // fd 的类型是 pipe - p = fds[kernFd].pipe; - for (i = 0; i < count; i++) { - while (p->pipeReadPos == p->pipeWritePos) { - if (i > 0 || pipeIsClose(fd) == 1) { - // TODO 返回之å‰åˆ¤æ–是å¦å†™ç«¯æ£åœ¨é˜»å¡žï¼Œæ˜¯ï¼Œå°±å”¤é†’ - fds[kernFd].offset += i; - return i; - } else { - // TODO è¿™é‡Œæ„æ€æ˜¯è¯»ä¸äº†ï¼Œéœ€è¦é˜»å¡žè¯»pipe的进程 - warn("pipe is empty, can\'t be read. sleep!\n"); + // fd 的类型是 pipe + p = fds[kernFd].pipe; + for (i = 0; i < count; i++) { + while (p->pipeReadPos == p->pipeWritePos) { + if (i > 0 || pipeIsClose(fd) == 1) { + // TODO + // 返回之å‰åˆ¤æ–是å¦å†™ç«¯æ£åœ¨é˜»å¡žï¼Œæ˜¯ï¼Œå°±å”¤é†’ + fds[kernFd].offset += i; + return i; + } else { + // TODO + // è¿™é‡Œæ„æ€æ˜¯è¯»ä¸äº†ï¼Œéœ€è¦é˜»å¡žè¯»pipe的进程 + warn("pipe is empty, can\'t be read. " + "sleep!\n"); p->waitProc = myProc(); myProc()->pipeWait.i = i; @@ -183,16 +186,16 @@ int read(int fd, u64 buf, size_t count) { myProc()->pipeWait.buf = buf; myProc()->pipeWait.fd = fd; naiveSleep(myProc(), "pipe"); - return -1; - } - } - ch = p->pipeBuf[p->pipeReadPos % PIPE_BUF_SIZE]; - copyOut((buf + i), &ch, 1); - p->pipeReadPos++; - } - // TODO 返回之å‰åˆ¤æ–是å¦å†™ç«¯æ£åœ¨é˜»å¡žï¼Œæ˜¯ï¼Œå°±å”¤é†’ - fds[kernFd].offset += count; - return count; + return -1; + } + } + ch = p->pipeBuf[p->pipeReadPos % PIPE_BUF_SIZE]; + copyOut((buf + i), &ch, 1); + p->pipeReadPos++; + } + // TODO 返回之å‰åˆ¤æ–是å¦å†™ç«¯æ£åœ¨é˜»å¡žï¼Œæ˜¯ï¼Œå°±å”¤é†’ + fds[kernFd].offset += count; + return count; } } } @@ -234,7 +237,7 @@ int write(int fd, u64 buf, size_t count) { Dirent *dirent; int n, i; char ch; - struct Pipe *p; + struct Pipe *p; if (fd < 0 || fd >= MAX_FD_COUNT) { warn("write param fd is wrong, please check\n"); return -1; @@ -266,22 +269,24 @@ int write(int fd, u64 buf, size_t count) { return count; } else { p = fds[kernFd].pipe; - for (i = 0; i < count; i++) { - while (p->pipeWritePos - p->pipeReadPos == PIPE_BUF_SIZE) { - if (pipeIsClose(fd) == 1) { - // TODO wirteè¿”å›žå€¼éœ€è¦æŸ¥çœ‹linux手册,确认没写完且读端关é—的返回值 - fds[kernFd].offset += i; - return i; - } else { - // TODO è¿™é‡Œæ„æ€æ˜¯å†™ä¸äº†ï¼Œéœ€è¦é˜»å¡žå†™pipe的进程 - return -1; - } - } - copyIn((buf + i), &ch, 1); - p->pipeBuf[p->pipeWritePos % PIPE_BUF_SIZE] = ch; - p->pipeWritePos++; - } - // TODO 判æ–读端是å¦é˜»å¡žï¼Œæ˜¯ï¼Œå°±å”¤é†’读端 + for (i = 0; i < count; i++) { + while (p->pipeWritePos - p->pipeReadPos == PIPE_BUF_SIZE) { + if (pipeIsClose(fd) == 1) { + // TODO + // wirteè¿”å›žå€¼éœ€è¦æŸ¥çœ‹linux手册,确认没写完且读端关é—的返回值 + fds[kernFd].offset += i; + return i; + } else { + // TODO + // è¿™é‡Œæ„æ€æ˜¯å†™ä¸äº†ï¼Œéœ€è¦é˜»å¡žå†™pipe的进程 + return -1; + } + } + copyIn((buf + i), &ch, 1); + p->pipeBuf[p->pipeWritePos % PIPE_BUF_SIZE] = ch; + p->pipeWritePos++; + } + // TODO 判æ–读端是å¦é˜»å¡žï¼Œæ˜¯ï¼Œå°±å”¤é†’读端 fds[kernFd].offset += count; if (p->waitProc) { @@ -444,18 +449,17 @@ int fileStatFd(int fd, u64 pkstat) { return 0; } - int pipeIsClose(int fd) { - // 由调用者ä¿è¯fd一定有效 - int kernFd = myProc()->fdList[fd]; - struct Pipe *p = fds[kernFd].pipe; - if (p != NULL) { - if (p->count <= 1) { - return 1; - } else { - return 0; - } - } else { - return 0; - } + // 由调用者ä¿è¯fd一定有效 + int kernFd = myProc()->fdList[fd]; + struct Pipe *p = fds[kernFd].pipe; + if (p != NULL) { + if (p->count <= 1) { + return 1; + } else { + return 0; + } + } else { + return 0; + } } diff --git a/kern/fs/pipe.c b/kern/fs/pipe.c index 0bea560e0c42e96c85a1b2b2ce1ba728e1a04f28..52b1514d6f91804d2fbb7e0a535a01ae27b0e128 100644 --- a/kern/fs/pipe.c +++ b/kern/fs/pipe.c @@ -1,68 +1,67 @@ #include <fs/fd.h> -#include <proc/proc.h> -#include <mm/vmm.h> #include <fs/pipe.h> #include <lib/log.h> #include <lib/string.h> +#include <mm/vmm.h> +#include <proc/proc.h> int pipe(int fd[2]) { - int fd1 = -1, fd2 = -1; - int kernfd1 = -1, kernfd2 = -1; - int i; - u64 pipeAlloc; + int fd1 = -1, fd2 = -1; + int kernfd1 = -1, kernfd2 = -1; + int i; + u64 pipeAlloc; - for (i = 0; i < MAX_FD_COUNT; i++) { - if (myProc()->fdList[i] == -1) { - fd1 = i; - break; - } - } - for (i = 0; i < MAX_FD_COUNT; i++) { - if (myProc()->fdList[i] == -1 && i != fd1) { - fd2 = i; - break; - } - } - if (fd1 < 0 || fd2 < 0) { + for (i = 0; i < MAX_FD_COUNT; i++) { + if (myProc()->fdList[i] == -1) { + fd1 = i; + break; + } + } + for (i = 0; i < MAX_FD_COUNT; i++) { + if (myProc()->fdList[i] == -1 && i != fd1) { + fd2 = i; + break; + } + } + if (fd1 < 0 || fd2 < 0) { warn("no free fd in proc fdList\n"); return -1; } else { - kernfd1 = fdAlloc(); - if (kernfd1 < 0) { - warn("no free fd in os\n"); - return 1; - } - kernfd2 = fdAlloc(); - if (kernfd2 < 0) { - warn("no free fd in os\n"); - freeFd(kernfd1); - return 1; - } - - pipeAlloc = kvmAlloc(); - struct Pipe *p = (struct Pipe *)pipeAlloc; - p->count = 2; - p->pipeReadPos = 0; - p->pipeWritePos = 0; - memset(p->pipeBuf, 0, PIPE_BUF_SIZE); + kernfd1 = fdAlloc(); + if (kernfd1 < 0) { + warn("no free fd in os\n"); + return 1; + } + kernfd2 = fdAlloc(); + if (kernfd2 < 0) { + warn("no free fd in os\n"); + freeFd(kernfd1); + return 1; + } - fds[kernfd1].dirent = NULL; - fds[kernfd1].pipe = (struct Pipe *)pipeAlloc; - fds[kernfd1].type = dev_pipe; - fds[kernfd1].flags = O_RDONLY; - fds[kernfd1].offset = 0; - myProc()->fdList[fd1] = kernfd1; + pipeAlloc = kvmAlloc(); + struct Pipe *p = (struct Pipe *)pipeAlloc; + p->count = 2; + p->pipeReadPos = 0; + p->pipeWritePos = 0; + memset(p->pipeBuf, 0, PIPE_BUF_SIZE); + fds[kernfd1].dirent = NULL; + fds[kernfd1].pipe = (struct Pipe *)pipeAlloc; + fds[kernfd1].type = dev_pipe; + fds[kernfd1].flags = O_RDONLY; + fds[kernfd1].offset = 0; + myProc()->fdList[fd1] = kernfd1; - fds[kernfd2].dirent = NULL; - fds[kernfd2].pipe = (struct Pipe *)pipeAlloc; - fds[kernfd2].type = dev_pipe; - fds[kernfd2].flags = O_WRONLY; - fds[kernfd2].offset = 0; - myProc()->fdList[fd2] = kernfd2; + fds[kernfd2].dirent = NULL; + fds[kernfd2].pipe = (struct Pipe *)pipeAlloc; + fds[kernfd2].type = dev_pipe; + fds[kernfd2].flags = O_WRONLY; + fds[kernfd2].offset = 0; + myProc()->fdList[fd2] = kernfd2; - fd[0] = fd1; - fd[1] = fd2; - return 0; - } + fd[0] = fd1; + fd[1] = fd2; + return 0; + } } diff --git a/kern/proc/sleep.c b/kern/proc/sleep.c index 7193fe9c32cf91f584cc20ca83a33b42c7deb039..36624279c0d28fcf9d03f020a1db9fff39f0f41e 100644 --- a/kern/proc/sleep.c +++ b/kern/proc/sleep.c @@ -5,8 +5,8 @@ #include <lib/printf.h> #include <lib/queue.h> #include <lib/string.h> -#include <proc/schedule.h> #include <proc/proc.h> +#include <proc/schedule.h> struct ProcList procSleepList = {NULL}; diff --git a/kern/trap/syscall.c b/kern/trap/syscall.c index 0cdd98e170a34085d4edb7a7f07e9ffc5bc5252b..bc425e7128ee10a70d2e7f7d85b547dcd0a6f4d9 100644 --- a/kern/trap/syscall.c +++ b/kern/trap/syscall.c @@ -394,7 +394,7 @@ static void *syscallTable[] = { [SYS_linkat] = sysLinkAt, [SYS_unlinkat] = sysUnLinkAt, [SYS_fstat] = sysFstat, - [SYS_pipe2] = sysPipe2, + [SYS_pipe2] = sysPipe2, }; /** diff --git a/user/test_pipe.c b/user/test_pipe.c index 410d38a7b837e7556fd0c557a4f47d2ab5ad1f91..f702ef795950f87db26f24159fbda2b6c877503a 100644 --- a/user/test_pipe.c +++ b/user/test_pipe.c @@ -1,8 +1,8 @@ #include <mm/memlayout.h> #include <stddef.h> #include <stdio.h> -#include <syscall.h> #include <string.h> +#include <syscall.h> #include <syscallDataStruct.h> #include <unistd.h>