2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-05-12 19:17:51 +00:00
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-27 13:44:26 +02:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2020-08-24 19:35:19 -06:00
|
|
|
#include <AK/Singleton.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-23 13:45:10 +01:00
|
|
|
#include <AK/StringView.h>
|
2020-11-07 09:38:48 +02:00
|
|
|
#include <Kernel/API/InodeWatcherEvent.h>
|
2020-03-01 21:46:51 +02:00
|
|
|
#include <Kernel/FileSystem/Custody.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>
|
2020-03-01 21:46:51 +02:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2020-08-11 19:47:24 +02:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2020-02-28 20:07:51 +01:00
|
|
|
#include <Kernel/VM/SharedInodeVMObject.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-07-09 13:51:58 -06:00
|
|
|
static SpinLock s_all_inodes_lock;
|
2021-05-26 02:18:23 -07:00
|
|
|
static AK::Singleton<Inode::List> s_list;
|
2020-07-09 13:51:58 -06:00
|
|
|
|
2021-05-26 02:18:23 -07:00
|
|
|
static Inode::List& all_with_lock()
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(s_all_inodes_lock.is_locked());
|
2020-07-09 13:51:58 -06:00
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
return *s_list;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::sync()
|
|
|
|
{
|
2019-06-27 13:44:26 +02:00
|
|
|
NonnullRefPtrVector<Inode, 32> inodes;
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2020-07-09 13:51:58 -06:00
|
|
|
ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
|
2020-08-10 23:55:10 +02:00
|
|
|
for (auto& inode : all_with_lock()) {
|
2019-08-08 13:40:58 +02:00
|
|
|
if (inode.is_metadata_dirty())
|
|
|
|
inodes.append(inode);
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& inode : inodes) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(inode.is_metadata_dirty());
|
2019-06-27 13:44:26 +02:00
|
|
|
inode.flush_metadata();
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 20:36:14 +01:00
|
|
|
KResultOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(FileDescription* description) const
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2020-08-11 19:47:24 +02:00
|
|
|
KBufferBuilder builder;
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2021-06-16 16:44:15 +02:00
|
|
|
size_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 (;;) {
|
2020-09-11 21:11:07 -06:00
|
|
|
auto buf = UserOrKernelBuffer::for_kernel_buffer(buffer);
|
2021-05-01 14:29:39 -07:00
|
|
|
auto result = read_bytes(offset, sizeof(buffer), buf, description);
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
nread = result.value();
|
2021-06-16 16:44:15 +02:00
|
|
|
VERIFY(nread <= sizeof(buffer));
|
2021-06-17 11:16:11 +02:00
|
|
|
if (nread == 0)
|
2019-05-16 03:02:37 +02:00
|
|
|
break;
|
|
|
|
builder.append((const char*)buffer, nread);
|
|
|
|
offset += nread;
|
2021-06-16 16:44:15 +02:00
|
|
|
if (nread < sizeof(buffer))
|
2020-01-15 22:09:45 +01:00
|
|
|
break;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 14:10:10 +01:00
|
|
|
auto entire_file = builder.build();
|
|
|
|
if (!entire_file)
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOMEM;
|
2020-12-18 14:10:10 +01:00
|
|
|
return entire_file.release_nonnull();
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 13:59:50 +03:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
|
|
|
|
{
|
|
|
|
// The default implementation simply treats the stored
|
|
|
|
// contents as a path and resolves that. That is, it
|
|
|
|
// behaves exactly how you would expect a symlink to work.
|
2020-05-26 00:36:11 -07:00
|
|
|
auto contents_or = read_entire();
|
|
|
|
if (contents_or.is_error())
|
|
|
|
return contents_or.error();
|
2020-01-15 13:59:50 +03:00
|
|
|
|
2020-05-26 00:36:11 -07:00
|
|
|
auto& contents = contents_or.value();
|
2020-12-18 14:10:10 +01:00
|
|
|
auto path = StringView(contents->data(), contents->size());
|
2021-07-11 00:25:24 +02:00
|
|
|
return VirtualFileSystem::the().resolve_path(path, base, out_parent, options, symlink_recursion_level);
|
2020-01-15 13:59:50 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 00:20:38 +02:00
|
|
|
Inode::Inode(FileSystem& fs, InodeIndex index)
|
|
|
|
: m_file_system(fs)
|
2019-05-16 03:02:37 +02:00
|
|
|
, m_index(index)
|
|
|
|
{
|
2020-07-09 13:51:58 -06:00
|
|
|
ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
|
2021-05-26 02:18:23 -07:00
|
|
|
all_with_lock().append(*this);
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Inode::~Inode()
|
|
|
|
{
|
2020-07-09 13:51:58 -06:00
|
|
|
ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
|
2021-05-26 02:18:23 -07:00
|
|
|
all_with_lock().remove(*this);
|
2021-05-12 19:17:51 +00:00
|
|
|
|
|
|
|
for (auto& watcher : m_watchers) {
|
|
|
|
watcher->unregister_by_inode({}, identifier());
|
|
|
|
}
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::will_be_destroyed()
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2019-05-16 03:02:37 +02:00
|
|
|
if (m_metadata_dirty)
|
|
|
|
flush_metadata();
|
|
|
|
}
|
|
|
|
|
2021-04-30 15:51:06 +02:00
|
|
|
KResult Inode::set_atime(time_t)
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-04-30 15:51:06 +02:00
|
|
|
return ENOTIMPL;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 15:51:06 +02:00
|
|
|
KResult Inode::set_ctime(time_t)
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-04-30 15:51:06 +02:00
|
|
|
return ENOTIMPL;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 15:51:06 +02:00
|
|
|
KResult Inode::set_mtime(time_t)
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-04-30 15:51:06 +02:00
|
|
|
return ENOTIMPL;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:26:33 +01:00
|
|
|
KResult Inode::increment_link_count()
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOTIMPL;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:26:33 +01:00
|
|
|
KResult Inode::decrement_link_count()
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOTIMPL;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2020-02-28 20:07:51 +01:00
|
|
|
void Inode::set_shared_vmobject(SharedInodeVMObject& vmobject)
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
m_shared_vmobject = vmobject;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::bind_socket(LocalSocket& socket)
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2020-01-30 22:15:45 +01:00
|
|
|
if (m_socket)
|
|
|
|
return false;
|
2019-05-16 03:02:37 +02:00
|
|
|
m_socket = socket;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::unbind_socket()
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2020-01-30 22:15:45 +01:00
|
|
|
if (!m_socket)
|
|
|
|
return false;
|
2019-05-16 03:02:37 +02:00
|
|
|
m_socket = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-22 20:01:11 +02:00
|
|
|
|
|
|
|
void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_watchers.contains(&watcher));
|
2019-07-22 20:01:11 +02:00
|
|
|
m_watchers.set(&watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_watchers.contains(&watcher));
|
2019-07-22 20:01:11 +02:00
|
|
|
m_watchers.remove(&watcher);
|
|
|
|
}
|
|
|
|
|
2020-12-31 02:10:31 +01:00
|
|
|
NonnullRefPtr<FIFO> Inode::fifo()
|
2020-07-16 15:23:03 -06:00
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(metadata().is_fifo());
|
2020-07-16 15:23:03 -06:00
|
|
|
|
|
|
|
// FIXME: Release m_fifo when it is closed by all readers and writers
|
|
|
|
if (!m_fifo)
|
|
|
|
m_fifo = FIFO::create(metadata().uid);
|
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_fifo);
|
2020-07-16 15:23:03 -06:00
|
|
|
return *m_fifo;
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:01:11 +02:00
|
|
|
void Inode::set_metadata_dirty(bool metadata_dirty)
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2021-01-17 21:32:59 +01:00
|
|
|
|
|
|
|
if (metadata_dirty) {
|
|
|
|
// Sanity check.
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!fs().is_readonly());
|
2021-01-17 21:32:59 +01:00
|
|
|
}
|
|
|
|
|
2019-07-22 20:01:11 +02:00
|
|
|
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.
|
|
|
|
for (auto& watcher : m_watchers) {
|
2021-05-12 19:17:51 +00:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::MetadataModified);
|
2019-07-22 20:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2021-05-12 19:17:51 +00:00
|
|
|
void Inode::did_add_child(InodeIdentifier const&, String const& name)
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2021-05-12 19:17:51 +00:00
|
|
|
|
|
|
|
for (auto& watcher : m_watchers) {
|
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildCreated, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::did_remove_child(InodeIdentifier const&, String const& name)
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2021-05-12 19:17:51 +00:00
|
|
|
|
|
|
|
if (name == "." || name == "..") {
|
|
|
|
// These are just aliases and are not interesting to userspace.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& watcher : m_watchers) {
|
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildDeleted, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::did_modify_contents()
|
2020-07-04 13:36:55 +02:00
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2020-07-04 13:36:55 +02:00
|
|
|
for (auto& watcher : m_watchers) {
|
2021-05-12 19:17:51 +00:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ContentModified);
|
2020-07-04 13:36:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 19:17:51 +00:00
|
|
|
void Inode::did_delete_self()
|
2020-07-04 13:36:55 +02:00
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2020-07-04 13:36:55 +02:00
|
|
|
for (auto& watcher : m_watchers) {
|
2021-05-12 19:17:51 +00:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::Deleted);
|
2020-07-04 13:36:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 19:46:55 +02:00
|
|
|
KResult Inode::prepare_to_write_data()
|
|
|
|
{
|
|
|
|
// FIXME: It's a poor design that filesystems are expected to call this before writing out data.
|
2021-07-11 00:25:24 +02:00
|
|
|
// We should funnel everything through an interface at the VirtualFileSystem layer so this can happen from a single place.
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
2020-04-04 19:46:55 +02:00
|
|
|
if (fs().is_readonly())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EROFS;
|
2020-04-04 19:46:55 +02:00
|
|
|
auto metadata = this->metadata();
|
|
|
|
if (metadata.is_setuid() || metadata.is_setgid()) {
|
2021-01-10 15:17:54 +01:00
|
|
|
dbgln("Inode::prepare_to_write_data(): Stripping SUID/SGID bits from {}", identifier());
|
2020-04-04 19:46:55 +02:00
|
|
|
return chmod(metadata.mode & ~(04000 | 02000));
|
|
|
|
}
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
RefPtr<SharedInodeVMObject> Inode::shared_vmobject() const
|
|
|
|
{
|
2021-07-17 21:17:39 +02:00
|
|
|
Locker locker(m_inode_lock);
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
return m_shared_vmobject.strong_ref();
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|