2019-06-27 13:44:26 +02:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2019-07-22 20:01:11 +02:00
|
|
|
#include <Kernel/FileSystem/InodeWatcher.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2019-08-07 18:06:17 +02:00
|
|
|
#include <Kernel/VM/InodeVMObject.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
|
|
|
|
HashTable<Inode*>& all_inodes()
|
|
|
|
{
|
|
|
|
static HashTable<Inode*>* s_inode_set;
|
|
|
|
if (!s_inode_set)
|
|
|
|
s_inode_set = new HashTable<Inode*>();
|
|
|
|
return *s_inode_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::sync()
|
|
|
|
{
|
2019-06-27 13:44:26 +02:00
|
|
|
NonnullRefPtrVector<Inode, 32> inodes;
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
for (auto* inode : all_inodes()) {
|
|
|
|
if (inode->is_metadata_dirty())
|
|
|
|
inodes.append(*inode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& inode : inodes) {
|
2019-06-27 13:44:26 +02:00
|
|
|
ASSERT(inode.is_metadata_dirty());
|
|
|
|
inode.flush_metadata();
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
ByteBuffer Inode::read_entire(FileDescription* descriptor) const
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
|
|
|
size_t initial_size = metadata().size ? metadata().size : 4096;
|
|
|
|
StringBuilder builder(initial_size);
|
|
|
|
|
|
|
|
ssize_t nread;
|
2019-07-03 21:17:35 +02:00
|
|
|
u8 buffer[4096];
|
2019-05-16 03:02:37 +02:00
|
|
|
off_t offset = 0;
|
|
|
|
for (;;) {
|
|
|
|
nread = read_bytes(offset, sizeof(buffer), buffer, descriptor);
|
|
|
|
ASSERT(nread <= (ssize_t)sizeof(buffer));
|
|
|
|
if (nread <= 0)
|
|
|
|
break;
|
|
|
|
builder.append((const char*)buffer, nread);
|
|
|
|
offset += nread;
|
|
|
|
}
|
|
|
|
if (nread < 0) {
|
|
|
|
kprintf("Inode::read_entire: ERROR: %d\n", nread);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_byte_buffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Inode::fsid() const
|
|
|
|
{
|
|
|
|
return m_fs.fsid();
|
|
|
|
}
|
|
|
|
|
|
|
|
Inode::Inode(FS& fs, unsigned index)
|
|
|
|
: m_fs(fs)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
all_inodes().set(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Inode::~Inode()
|
|
|
|
{
|
|
|
|
all_inodes().remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::will_be_destroyed()
|
|
|
|
{
|
|
|
|
if (m_metadata_dirty)
|
|
|
|
flush_metadata();
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
void Inode::inode_contents_changed(off_t offset, ssize_t size, const u8* data)
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
|
|
|
if (m_vmo)
|
2019-05-31 15:44:04 +02:00
|
|
|
m_vmo->inode_contents_changed({}, offset, size, data);
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::inode_size_changed(size_t old_size, size_t new_size)
|
|
|
|
{
|
|
|
|
if (m_vmo)
|
2019-05-31 15:44:04 +02:00
|
|
|
m_vmo->inode_size_changed({}, old_size, new_size);
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Inode::set_atime(time_t)
|
|
|
|
{
|
|
|
|
return -ENOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inode::set_ctime(time_t)
|
|
|
|
{
|
|
|
|
return -ENOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inode::set_mtime(time_t)
|
|
|
|
{
|
|
|
|
return -ENOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inode::increment_link_count()
|
|
|
|
{
|
|
|
|
return -ENOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inode::decrement_link_count()
|
|
|
|
{
|
|
|
|
return -ENOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::set_vmo(VMObject& vmo)
|
|
|
|
{
|
|
|
|
m_vmo = vmo.make_weak_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::bind_socket(LocalSocket& socket)
|
|
|
|
{
|
|
|
|
ASSERT(!m_socket);
|
|
|
|
m_socket = socket;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::unbind_socket()
|
|
|
|
{
|
|
|
|
ASSERT(m_socket);
|
|
|
|
m_socket = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-22 20:01:11 +02:00
|
|
|
|
|
|
|
void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
ASSERT(!m_watchers.contains(&watcher));
|
|
|
|
m_watchers.set(&watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
ASSERT(m_watchers.contains(&watcher));
|
|
|
|
m_watchers.remove(&watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::set_metadata_dirty(bool metadata_dirty)
|
|
|
|
{
|
|
|
|
if (m_metadata_dirty == metadata_dirty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_metadata_dirty = metadata_dirty;
|
|
|
|
if (m_metadata_dirty) {
|
|
|
|
// FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
|
|
|
|
// We don't always end up on this particular code path, for instance when writing to an ext2fs file.
|
|
|
|
LOCKER(m_lock);
|
|
|
|
for (auto& watcher : m_watchers) {
|
|
|
|
watcher->notify_inode_event({}, InodeWatcher::Event::Type::Modified);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|