assert(page_hash_table != NULL);
memset(page_hash_table, 0, PAGE_SIZE);
}
+
+///
+#define MAX_FILES 1024
+static file_t g_files[MAX_FILES] = {
+ 0,
+};
+
+void init_file(file_t *fp) {
+ fp->f_dentry = NULL;
+ fp->f_flags = 0;
+ fp->f_ops = NULL;
+ fp->f_pos = 0;
+
+ fp->f_state = 0;
+}
+
+void init_files() {
+ for (int i = 0; i < MAX_FILES; i++) {
+ init_file(g_files + i);
+ }
+}
+
+file_t *get_empty_filp() {
+ file_t *fp = NULL;
+
+ for (int i = 0; i < MAX_FILES; i++) {
+ file_t *p = g_files + i;
+ if (p->f_state == 0) {
+ ENTER_CRITICAL_ZONE(EFLAGS);
+
+ if (p->f_state == 0) {
+ p->f_state = 1;
+ fp = p;
+ EXIT_CRITICAL_ZONE(EFLAGS);
+ break;
+ }
+
+ EXIT_CRITICAL_ZONE(EFLAGS);
+ }
+ }
+
+ return fp;
+}
void setup_fs() {
// ext2_setup_fs();
+ void init_files();
+ init_files();
+
void vfs_page_cache_init();
vfs_page_cache_init();
return -EMFILE;
}
-file_t *filp_open(const char *path, int flags, int mode) {
+int filp_open(const char *path, int flags, int mode, file_t **fp) {
int ret = 0;
- return NULL;
+ assert(path != NULL);
+
+ *fp = get_empty_filp();
+ if (*fp == NULL) {
+ return -ENFILE;
+ }
+
+ namei_t ni;
+ path_open_namei(path, flags, mode, &ni);
+
+ (*fp)->f_dentry = ni.path.dentry;
+ (*fp)->f_flags = flags;
+ (*fp)->f_ops = (*fp)->f_dentry->d_inode->i_fops;
+ (*fp)->f_pos = 0;
+
+ assert((*fp)->f_ops != NULL);
+
+ // TODO: 添加open支持
+ assert((*fp)->f_ops->open == NULL);
+
+ return ret;
}
int sysc_open(const char *path, int flags, int mode) {
return fd;
}
- file_t *fp = filp_open(path, flags, mode);
+ file_t *fp;
+
+ int ret = filp_open(path, flags, mode, &fp);
+ if (ret != 0) {
+ return ret;
+ }
current->files.fds[fd] = fp;
- return 0;
+ return fd;
}
// }
static const file_operations_t ramfs_file_operations = {
- .read = 0,
+ .read = vfs_generic_file_read,
.write = vfs_generic_file_write,
};
void *addr = page2va(page);
// printk("memcpy bytes %u index %u\n", bytes, index);
// printk("read addr %x bytes %u index %u offset %u\n", addr, bytes, index, offset);
- memcpy(buf, addr, bytes);
+ memcpy(buf, addr + offset, bytes);
buf += bytes;
offset += bytes;
read = file->f_ops->read;
loff_t pos = file->f_pos;
+ pos = 0; // TODO add sysc_seek
+ // printk("%s pos %lu\n", file->f_dentry->d_inline_name, pos);
ret = read(file, buf, count, &pos);
return ret;
loff_t f_pos;
uint32_t f_flags;
+
+ int f_state; // 0 EMPTY 1 USED
};
// super block
//
ssize_t vfs_generic_file_read(file_t *file, char *buf, size_t size, loff_t *p_pos);
ssize_t vfs_generic_file_write(file_t *file, const char *buf, size_t size, loff_t *p_pos);
+
+// file
+file_t *get_empty_filp();
// ...
// 写入page
- memcpy(addr, buf + pos, bytes);
+ memcpy(addr + offset, buf + pos, bytes);
// printk("write addr %x bytes %u index %u offset %u\n", addr, bytes, index, offset);
// TODO
printk("\n");
{
+ // TODO支持带多层目录的fe_name
+
namei_t ni;
const char *path = fe_name;
const int flags = O_CREAT | O_APPEND;
+#define bufsz 5223
+ static char buf[bufsz] = {'b', 'u', 'f'};
+#if 1
+ int sysc_open(const char *path, int flags, int mode);
+ int fd = sysc_open(path, flags, 0700);
+ assert(fd >= 0);
- // TODO支持带多层目录的fe_name
+ ssize_t sysc_write(int fd, const char *buf, size_t size);
+ sysc_write(fd, fc, fe_filesz);
+ ssize_t sysc_read(int fd, void *buf, size_t count);
+ sysc_read(fd, buf, bufsz);
+#else
path_open_namei(path, flags, S_IFREG, &ni);
file_t file;
vfs_generic_file_write(&file, fc, fe_filesz, &file.f_pos);
file.f_pos = 0;
-#define bufsz 5223
- static char buf[bufsz] = {'b', 'u', 'f'};
+
vfs_generic_file_read(&file, buf, bufsz, &file.f_pos);
+#endif
for (int i = 0; i < bufsz; i++) {
printk("%c", buf[i]);
}