2018-10-10 12:53:07 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/HashMap.h>
|
2018-12-19 23:56:45 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
#include "FileSystem.h"
|
|
|
|
|
2018-10-19 12:20:49 +03:00
|
|
|
static dword s_lastFileSystemID;
|
2018-12-20 02:39:29 +03:00
|
|
|
static HashMap<dword, FS*>* s_fs_map;
|
|
|
|
static HashTable<Inode*>* s_inode_set;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-20 02:39:29 +03:00
|
|
|
static HashMap<dword, FS*>& all_fses()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-12-20 02:39:29 +03:00
|
|
|
if (!s_fs_map)
|
|
|
|
s_fs_map = new HashMap<dword, FS*>();
|
|
|
|
return *s_fs_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HashTable<Inode*>& all_inodes()
|
|
|
|
{
|
|
|
|
if (!s_inode_set)
|
|
|
|
s_inode_set = new HashTable<Inode*>();
|
|
|
|
return *s_inode_set;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
void FS::initialize_globals()
|
2018-10-19 12:20:49 +03:00
|
|
|
{
|
|
|
|
s_lastFileSystemID = 0;
|
2018-12-20 02:39:29 +03:00
|
|
|
s_fs_map = nullptr;
|
|
|
|
s_inode_set = nullptr;
|
2018-10-19 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS::FS()
|
2018-12-03 02:20:00 +03:00
|
|
|
: m_fsid(++s_lastFileSystemID)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-12-20 02:39:29 +03:00
|
|
|
all_fses().set(m_fsid, this);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS::~FS()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-12-20 02:39:29 +03:00
|
|
|
all_fses().remove(m_fsid);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
FS* FS::from_fsid(dword id)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-12-20 02:39:29 +03:00
|
|
|
auto it = all_fses().find(id);
|
|
|
|
if (it != all_fses().end())
|
2018-10-10 12:53:07 +03:00
|
|
|
return (*it).value;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
ByteBuffer Inode::read_entire(FileDescriptor* descriptor)
|
2018-11-14 01:44:54 +03:00
|
|
|
{
|
|
|
|
size_t initial_size = metadata().size ? metadata().size : 4096;
|
2018-12-21 04:10:45 +03:00
|
|
|
auto contents = ByteBuffer::create_uninitialized(initial_size);
|
2018-11-14 01:44:54 +03:00
|
|
|
|
2018-12-03 01:34:50 +03:00
|
|
|
ssize_t nread;
|
2018-11-14 01:44:54 +03:00
|
|
|
byte buffer[4096];
|
|
|
|
byte* out = contents.pointer();
|
|
|
|
Unix::off_t offset = 0;
|
|
|
|
for (;;) {
|
|
|
|
nread = read_bytes(offset, sizeof(buffer), buffer, descriptor);
|
2018-12-03 01:34:50 +03:00
|
|
|
ASSERT(nread <= (ssize_t)sizeof(buffer));
|
2018-11-14 01:44:54 +03:00
|
|
|
if (nread <= 0)
|
|
|
|
break;
|
|
|
|
memcpy(out, buffer, nread);
|
|
|
|
out += nread;
|
|
|
|
offset += nread;
|
2018-12-03 01:34:50 +03:00
|
|
|
ASSERT(offset <= (ssize_t)initial_size); // FIXME: Support dynamically growing the buffer.
|
2018-11-14 01:44:54 +03:00
|
|
|
}
|
|
|
|
if (nread < 0) {
|
2018-12-19 23:56:45 +03:00
|
|
|
kprintf("Inode::read_entire: ERROR: %d\n", nread);
|
2018-11-14 01:44:54 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:54:11 +03:00
|
|
|
contents.trim(offset);
|
2018-10-15 01:16:14 +03:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, byte ft)
|
2018-11-18 16:57:41 +03:00
|
|
|
: name_length(strlen(n))
|
2018-11-13 02:17:30 +03:00
|
|
|
, inode(i)
|
|
|
|
, fileType(ft)
|
|
|
|
{
|
|
|
|
memcpy(name, n, name_length);
|
|
|
|
name[name_length] = '\0';
|
|
|
|
}
|
|
|
|
|
2018-12-03 01:34:50 +03:00
|
|
|
FS::DirectoryEntry::DirectoryEntry(const char* n, size_t nl, InodeIdentifier i, byte ft)
|
2018-11-13 02:17:30 +03:00
|
|
|
: name_length(nl)
|
|
|
|
, inode(i)
|
|
|
|
, fileType(ft)
|
|
|
|
{
|
|
|
|
memcpy(name, n, nl);
|
|
|
|
name[nl] = '\0';
|
|
|
|
}
|
2018-11-13 15:02:39 +03:00
|
|
|
|
2018-12-20 02:39:29 +03:00
|
|
|
Inode::Inode(FS& fs, unsigned index)
|
|
|
|
: m_fs(fs)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
all_inodes().set(this);
|
|
|
|
}
|
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
Inode::~Inode()
|
2018-11-13 15:02:39 +03:00
|
|
|
{
|
2018-12-20 02:39:29 +03:00
|
|
|
all_inodes().remove(this);
|
2018-11-13 15:02:39 +03:00
|
|
|
}
|
2018-12-19 23:14:55 +03:00
|
|
|
|
2018-12-20 00:28:09 +03:00
|
|
|
void Inode::will_be_destroyed()
|
|
|
|
{
|
|
|
|
if (m_metadata_dirty)
|
|
|
|
flush_metadata();
|
|
|
|
}
|
|
|
|
|
2018-12-19 23:56:45 +03:00
|
|
|
int Inode::set_atime(Unix::time_t ts)
|
2018-12-19 23:14:55 +03:00
|
|
|
{
|
2018-12-19 23:56:45 +03:00
|
|
|
if (fs().is_readonly())
|
|
|
|
return -EROFS;
|
|
|
|
if (m_metadata.atime == ts)
|
|
|
|
return 0;
|
|
|
|
m_metadata.atime = ts;
|
2018-12-20 00:28:09 +03:00
|
|
|
m_metadata_dirty = true;
|
2018-12-19 23:56:45 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inode::set_ctime(Unix::time_t ts)
|
|
|
|
{
|
|
|
|
if (fs().is_readonly())
|
|
|
|
return -EROFS;
|
|
|
|
if (m_metadata.ctime == ts)
|
|
|
|
return 0;
|
|
|
|
m_metadata.ctime = ts;
|
2018-12-20 00:28:09 +03:00
|
|
|
m_metadata_dirty = true;
|
2018-12-19 23:56:45 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inode::set_mtime(Unix::time_t ts)
|
|
|
|
{
|
|
|
|
if (fs().is_readonly())
|
|
|
|
return -EROFS;
|
|
|
|
if (m_metadata.mtime == ts)
|
|
|
|
return 0;
|
|
|
|
m_metadata.mtime = ts;
|
2018-12-20 00:28:09 +03:00
|
|
|
m_metadata_dirty = true;
|
2018-12-19 23:56:45 +03:00
|
|
|
return 0;
|
2018-12-19 23:14:55 +03:00
|
|
|
}
|
2018-12-20 02:39:29 +03:00
|
|
|
|
|
|
|
void FS::sync()
|
|
|
|
{
|
|
|
|
for (auto* inode : all_inodes()) {
|
|
|
|
if (inode->is_metadata_dirty())
|
|
|
|
inode->flush_metadata();
|
|
|
|
}
|
|
|
|
}
|