-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk-vfs.hh
106 lines (86 loc) · 2.92 KB
/
k-vfs.hh
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
#ifndef CHICKADEE_K_VFS_HH
#define CHICKADEE_K_VFS_HH
#include "kernel.hh"
#include "chickadeefs.hh"
extern spinlock initfs_lock;
struct vnode;
struct pipe_vnode;
struct memfile_vnode;
struct keyboard_console_vnode;
struct diskfile_vnode;
struct file_descriptor;
struct bounded_buffer;
struct memfile;
struct vnode {
spinlock lock_;
std::atomic<int> ref_;
vnode(int ref = 1) : ref_(ref) {
}
virtual uintptr_t read(file_descriptor* f, uintptr_t addr, size_t sz) = 0;
virtual uintptr_t write(file_descriptor* f, uintptr_t addr, size_t sz) = 0;
};
struct pipe_vnode : public vnode {
bounded_buffer* buf_;
pipe_vnode(bounded_buffer* buf, int ref = 1) : vnode(ref), buf_(buf) {
assert(buf_);
}
uintptr_t read(file_descriptor* f, uintptr_t addr, size_t sz) override;
uintptr_t write(file_descriptor* f, uintptr_t addr, size_t sz) override;
};
struct memfile_vnode : public vnode {
memfile* mf_;
memfile_vnode(memfile* mf, int ref = 1) : vnode(ref), mf_(mf) {
assert(mf);
}
uintptr_t read(file_descriptor* f, uintptr_t addr, size_t sz) override;
uintptr_t write(file_descriptor* f, uintptr_t addr, size_t sz) override;
};
struct keyboard_console_vnode : public vnode {
uintptr_t read(file_descriptor* f, uintptr_t addr, size_t sz) override;
uintptr_t write(file_descriptor* f, uintptr_t addr, size_t sz) override;
};
struct diskfile_vnode : public vnode {
chkfs::inode* ino_;
diskfile_vnode(chkfs::inode* ino, int ref = 1) :
vnode(ref), ino_(ino) {
assert(ino_);
}
uintptr_t read(file_descriptor* f, uintptr_t addr, size_t sz) override;
uintptr_t write(file_descriptor* f, uintptr_t addr, size_t sz) override;
};
struct file_descriptor {
inline file_descriptor(int type, int flags, vnode* v) :
ref_(1),
readable_(flags & OF_READ),
writable_(flags & OF_WRITE),
type_(type),
vnode_(v) {
}
enum fd_t {
kbd_cons_t,
memfile_t,
pipe_t,
disk_t
};
spinlock lock_; // protects access to ref_
std::atomic<int> ref_ = 0; // number of processes referencing this
std::atomic<off_t> rpos_ = 0; // current read position
std::atomic<off_t> wpos_ = 0; // current write position
bool readable_ = false; // whether the file is readable
bool writable_ = false; // whether the file is writables
int type_; // the fd_t of this file descriptor
vnode* vnode_ = nullptr;
};
struct bounded_buffer {
spinlock lock_;
wait_queue wq_;
const unsigned cap_ = 128;
char buf_[128];
size_t pos_ = 0;
size_t len_ = 0; // number of characters in buffer
bool write_closed_ = false;
bool read_closed_ = false;
uintptr_t read(char* buf, size_t sz);
uintptr_t write(const char* buf, size_t sz);
};
#endif