Skip to content

Commit 47ec18e

Browse files
author
Christoph Hellwig
committed
import the prex 0.9.0 VFS
Various changes to fit into the OSv environment. This also includes a ramfs in-memory full filesystem. Bootfs is rewritten as a simple unpacker into ramfs, and the fs class is modified to wrap the VFS instead of being an abstract subclass.
1 parent 6d94ef8 commit 47ec18e

29 files changed

+3875
-480
lines changed

build.mak

+12-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,18 @@ loader.bin: arch/x64/boot32.o arch/x64/loader32.ld
5757

5858
arch/x64/boot32.o: loader.elf
5959

60-
fs = fs/fs.o fs/bootfs.o bootfs.o
61-
fs += fs/stdio.o
60+
fs = fs/fs.o bootfs.o
61+
62+
fs += fs/vfs/main.o \
63+
fs/vfs/vfs_conf.o \
64+
fs/vfs/vfs_lookup.o \
65+
fs/vfs/vfs_mount.o \
66+
fs/vfs/vfs_vnode.o \
67+
fs/vfs/vfs_task.o \
68+
fs/vfs/vfs_syscalls.o
69+
70+
fs += fs/ramfs/ramfs_vfsops.o \
71+
fs/ramfs/ramfs_vnops.o
6272

6373
drivers = drivers/vga.o drivers/console.o drivers/isa-serial.o
6474
drivers += $(fs)

fs/bootfs.cc

-99
This file was deleted.

fs/bootfs.hh

-53
This file was deleted.

fs/fs.cc

+24-50
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
#include "fs.hh"
2+
#include <fcntl.h>
3+
#include <sys/stat.h>
24

3-
filesystem* rootfs;
4-
5-
namespace std {
6-
7-
template <>
8-
struct hash<file::cache_key> {
9-
size_t operator()(file::cache_key k) const {
10-
return reinterpret_cast<uintptr_t>(k.first.get())
11-
^ std::hash<std::string>()(k.second);
12-
}
13-
};
14-
15-
}
16-
17-
file::file()
18-
: _refs(0)
5+
file::file(int fd)
6+
: _fd(fd)
7+
, _refs(0)
198
{
209
}
2110

2211
file::~file()
2312
{
13+
close(_fd);
2414
}
2515

2616
void file::ref()
@@ -35,51 +25,35 @@ void file::unref()
3525
}
3626
}
3727

38-
filesystem::~filesystem()
28+
uint64_t file::size()
3929
{
40-
}
30+
struct stat st;
4131

42-
fileref filesystem::open(std::string name)
43-
{
44-
dirref d = root();
45-
size_t s = 0, e;
46-
while (d && (e = name.find('/', s)) != name.npos) {
47-
if (s != e) {
48-
d = d->subdir(name.substr(s, e - s));
49-
}
50-
s = e + 1;
51-
}
52-
if (!d) {
53-
return fileref();
54-
}
55-
return d->open(name.substr(s, name.npos));
32+
::__fxstat(1, _fd, &st);
33+
return st.st_size;
5634
}
57-
58-
int file::getdent(struct dirent *d, int idx)
35+
36+
void file::read(void *buffer, uint64_t offset, uint64_t len)
5937
{
60-
return -1;
38+
::lseek(_fd, offset, SEEK_SET);
39+
::read(_fd, buffer, len);
6140
}
6241

63-
fileref dir::open(std::string name)
42+
void file::write(const void* buffer, uint64_t offset, uint64_t len)
6443
{
65-
cache_key key(this, name);
66-
auto ret = _cache.find(key);
67-
if (ret == _cache.end()) {
68-
auto f = do_open(name);
69-
ret = _cache.insert(cache_type::value_type(key, f)).first;
70-
}
71-
return ret->second;
44+
::lseek(_fd, offset, SEEK_SET);
45+
::write(_fd, buffer, len);
7246
}
7347

74-
dirref dir::subdir(std::string name)
48+
filesystem::~filesystem()
7549
{
76-
// trivial implementation, can be overridden
77-
return boost::dynamic_pointer_cast<dir>(open(name));
7850
}
7951

80-
void dir::write(const void* buffer, uint64_t offset, uint64_t len)
52+
fileref filesystem::open(std::string name)
8153
{
82-
abort();
83-
}
54+
int fd = ::open(name.c_str(), O_RDONLY);
55+
if (fd < 0)
56+
return fileref();
8457

85-
file::cache_type file::_cache;
58+
return fileref(new file(fd));
59+
}

fs/fs.hh

+6-29
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,30 @@
88
#include <unordered_map>
99

1010
class file;
11-
class dir;
1211

1312
typedef boost::intrusive_ptr<file> fileref;
14-
typedef boost::intrusive_ptr<dir> dirref;
1513

1614
class file {
1715
public:
18-
file();
19-
virtual ~file();
20-
virtual uint64_t size() = 0;
21-
virtual void read(void *buffer, uint64_t offset, uint64_t len) = 0;
22-
virtual void write(const void* buffer, uint64_t offset, uint64_t len) = 0;
23-
virtual int getdent(struct dirent *d, int idx);
16+
file(int fd);
17+
~file();
18+
uint64_t size();
19+
void read(void *buffer, uint64_t offset, uint64_t len);
20+
void write(const void* buffer, uint64_t offset, uint64_t len);
2421
private:
2522
void ref();
2623
void unref();
24+
int _fd;
2725
private:
2826
unsigned _refs; // FIXME: make atomic
2927
friend void intrusive_ptr_add_ref(file* f) { f->ref(); }
3028
friend void intrusive_ptr_release(file* f) { f->unref(); }
31-
friend class dir;
32-
private:
33-
typedef std::pair<dirref, std::string> cache_key;
34-
// FIXME: an intrusive container
35-
typedef std::unordered_map<cache_key, fileref> cache_type;
36-
static cache_type _cache;
37-
friend struct std::hash<cache_key>;
38-
};
39-
40-
class dir : public file {
41-
public:
42-
fileref open(std::string name);
43-
virtual fileref do_open(std::string name) = 0;
44-
dirref subdir(std::string name);
45-
virtual void write(const void* buffer, uint64_t offset, uint64_t len);
46-
private:
47-
dirref _parent;
48-
std::string _name;
4929
};
5030

5131
class filesystem {
5232
public:
5333
virtual ~filesystem();
54-
virtual dirref root() = 0;
5534
fileref open(std::string name);
5635
};
5736

58-
extern filesystem* rootfs;
59-
6037
#endif

fs/stdio.cc

-20
This file was deleted.

0 commit comments

Comments
 (0)