2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-08-17 01:05:06 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-05-12 19:17:51 +00:00
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
2022-07-14 02:17:01 +03:00
|
|
|
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
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
|
|
|
*/
|
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
#include <AK/Singleton.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>
|
2021-09-07 13:39:11 +02:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-03-01 21:46:51 +02:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2023-02-24 20:10:59 +02:00
|
|
|
#include <Kernel/Library/KBufferBuilder.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/SharedInodeVMObject.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2023-02-24 19:45:37 +02:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-11-09 11:39:58 +01:00
|
|
|
static Singleton<SpinlockProtected<Inode::AllInstancesList, LockRank::None>> s_all_instances;
|
2020-07-09 13:51:58 -06:00
|
|
|
|
2022-11-09 11:39:58 +01:00
|
|
|
SpinlockProtected<Inode::AllInstancesList, LockRank::None>& Inode::all_instances()
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-08-17 01:05:06 +02:00
|
|
|
return s_all_instances;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 23:28:59 -04:00
|
|
|
void Inode::sync_all()
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2023-03-07 12:25:00 +01:00
|
|
|
Vector<NonnullRefPtr<Inode>, 32> inodes;
|
2021-08-17 01:05:06 +02:00
|
|
|
Inode::all_instances().with([&](auto& all_inodes) {
|
|
|
|
for (auto& inode : all_inodes) {
|
2019-08-08 13:40:58 +02:00
|
|
|
if (inode.is_metadata_dirty())
|
|
|
|
inodes.append(inode);
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
2021-08-17 01:05:06 +02:00
|
|
|
});
|
2019-05-16 03:02:37 +02:00
|
|
|
|
|
|
|
for (auto& inode : inodes) {
|
2023-03-06 17:56:28 +01:00
|
|
|
(void)inode->flush_metadata();
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 23:28:59 -04:00
|
|
|
void Inode::sync()
|
|
|
|
{
|
2023-11-19 19:15:37 -06:00
|
|
|
(void)flush_metadata();
|
2023-08-01 07:48:43 +01:00
|
|
|
auto result = fs().flush_writes();
|
|
|
|
if (result.is_error()) {
|
|
|
|
// TODO: Figure out how to propagate error to a higher function.
|
|
|
|
}
|
2021-09-11 23:28:59 -04:00
|
|
|
}
|
|
|
|
|
2022-08-21 16:17:13 +02:00
|
|
|
ErrorOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Credentials const& credentials, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
|
2020-01-15 13:59:50 +03:00
|
|
|
{
|
|
|
|
// 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.
|
2023-04-16 18:37:55 +02:00
|
|
|
|
|
|
|
// Make sure that our assumptions about the path length hold up.
|
|
|
|
// Note that this doesn't mean that the reported size can be trusted, some inodes just report zero.
|
|
|
|
VERIFY(size() <= MAXPATHLEN);
|
|
|
|
|
|
|
|
Array<u8, MAXPATHLEN> contents;
|
|
|
|
auto read_bytes = TRY(read_until_filled_or_end(0, contents.size(), UserOrKernelBuffer::for_kernel_buffer(contents.data()), nullptr));
|
|
|
|
return VirtualFileSystem::the().resolve_path(credentials, StringView { contents.span().trim(read_bytes) }, 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)
|
|
|
|
{
|
2021-08-17 01:05:06 +02:00
|
|
|
Inode::all_instances().with([&](auto& all_inodes) { all_inodes.append(*this); });
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Inode::~Inode()
|
|
|
|
{
|
2022-02-03 01:39:49 +01:00
|
|
|
m_watchers.for_each([&](auto& watcher) {
|
2021-07-21 21:23:39 +02:00
|
|
|
watcher->unregister_by_inode({}, identifier());
|
2022-02-03 01:39:49 +01:00
|
|
|
});
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::will_be_destroyed()
|
|
|
|
{
|
2021-07-18 01:13:34 +02:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2019-05-16 03:02:37 +02:00
|
|
|
if (m_metadata_dirty)
|
2021-10-21 17:01:52 +02:00
|
|
|
(void)flush_metadata();
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2024-02-10 12:49:31 +02:00
|
|
|
ErrorOr<void> Inode::truncate(u64 size)
|
|
|
|
{
|
|
|
|
MutexLocker locker(m_inode_lock);
|
|
|
|
return truncate_locked(size);
|
|
|
|
}
|
|
|
|
|
2022-08-06 04:22:20 +03:00
|
|
|
ErrorOr<size_t> Inode::write_bytes(off_t offset, size_t length, UserOrKernelBuffer const& target_buffer, OpenFileDescription* open_description)
|
|
|
|
{
|
|
|
|
MutexLocker locker(m_inode_lock);
|
2024-02-23 17:18:25 +02:00
|
|
|
return prepare_and_write_bytes_locked(offset, length, target_buffer, open_description);
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<size_t> Inode::prepare_and_write_bytes_locked(off_t offset, size_t length, UserOrKernelBuffer const& target_buffer, OpenFileDescription* open_description)
|
|
|
|
{
|
|
|
|
VERIFY(m_inode_lock.is_locked());
|
2022-08-06 04:22:20 +03:00
|
|
|
TRY(prepare_to_write_data());
|
|
|
|
return write_bytes_locked(offset, length, target_buffer, open_description);
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<size_t> Inode::read_bytes(off_t offset, size_t length, UserOrKernelBuffer& buffer, OpenFileDescription* open_description) const
|
|
|
|
{
|
|
|
|
MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
|
|
|
|
return read_bytes_locked(offset, length, buffer, open_description);
|
|
|
|
}
|
|
|
|
|
2023-04-16 18:48:38 +02:00
|
|
|
ErrorOr<size_t> Inode::read_until_filled_or_end(off_t offset, size_t length, UserOrKernelBuffer buffer, OpenFileDescription* open_description) const
|
|
|
|
{
|
|
|
|
auto remaining_length = length;
|
|
|
|
|
|
|
|
while (remaining_length > 0) {
|
|
|
|
auto filled_bytes = TRY(read_bytes(offset, remaining_length, buffer, open_description));
|
|
|
|
if (filled_bytes == 0)
|
|
|
|
break;
|
|
|
|
offset += filled_bytes;
|
|
|
|
remaining_length -= filled_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length - remaining_length;
|
|
|
|
}
|
|
|
|
|
2023-03-13 22:11:13 +01:00
|
|
|
ErrorOr<void> Inode::update_timestamps([[maybe_unused]] Optional<UnixDateTime> atime, [[maybe_unused]] Optional<UnixDateTime> ctime, [[maybe_unused]] Optional<UnixDateTime> mtime)
|
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-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> 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
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> 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
|
|
|
}
|
|
|
|
|
2022-02-14 01:46:34 +02:00
|
|
|
ErrorOr<void> Inode::set_shared_vmobject(Memory::SharedInodeVMObject& vmobject)
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2021-07-18 01:13:34 +02:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2022-02-14 01:46:34 +02:00
|
|
|
m_shared_vmobject = TRY(vmobject.try_make_weak_ptr<Memory::SharedInodeVMObject>());
|
|
|
|
return {};
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<LocalSocket> Inode::bound_socket() const
|
2022-02-07 12:57:57 +01:00
|
|
|
{
|
2023-02-12 00:49:15 -07:00
|
|
|
return m_bound_socket.strong_ref();
|
2022-02-07 12:57:57 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 03:02:37 +02:00
|
|
|
bool Inode::bind_socket(LocalSocket& socket)
|
|
|
|
{
|
2021-07-18 01:13:34 +02:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2022-02-07 12:57:57 +01:00
|
|
|
if (m_bound_socket)
|
2020-01-30 22:15:45 +01:00
|
|
|
return false;
|
2022-02-07 12:57:57 +01:00
|
|
|
m_bound_socket = socket;
|
2019-05-16 03:02:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::unbind_socket()
|
|
|
|
{
|
2021-07-18 01:13:34 +02:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2022-02-07 12:57:57 +01:00
|
|
|
if (!m_bound_socket)
|
2020-01-30 22:15:45 +01:00
|
|
|
return false;
|
2022-02-07 12:57:57 +01:00
|
|
|
m_bound_socket = nullptr;
|
2019-05-16 03:02:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-22 20:01:11 +02:00
|
|
|
|
2022-01-25 15:13:59 +02:00
|
|
|
ErrorOr<void> Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
|
2019-07-22 20:01:11 +02:00
|
|
|
{
|
2022-02-03 01:39:49 +01:00
|
|
|
return m_watchers.with([&](auto& watchers) -> ErrorOr<void> {
|
|
|
|
VERIFY(!watchers.contains(&watcher));
|
|
|
|
TRY(watchers.try_set(&watcher));
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-22 20:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
|
|
|
|
{
|
2022-02-03 01:39:49 +01:00
|
|
|
m_watchers.with([&](auto& watchers) {
|
|
|
|
VERIFY(watchers.contains(&watcher));
|
|
|
|
watchers.remove(&watcher);
|
|
|
|
});
|
2019-07-22 20:01:11 +02:00
|
|
|
}
|
|
|
|
|
2023-03-10 07:53:02 +01:00
|
|
|
ErrorOr<NonnullRefPtr<FIFO>> Inode::fifo()
|
2020-07-16 15:23:03 -06:00
|
|
|
{
|
2021-07-18 01:13:34 +02:00
|
|
|
MutexLocker 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
|
2021-09-07 13:56:10 +02:00
|
|
|
if (!m_fifo)
|
|
|
|
m_fifo = TRY(FIFO::try_create(metadata().uid));
|
2020-07-16 15:23:03 -06:00
|
|
|
|
2023-03-10 07:53:02 +01:00
|
|
|
return NonnullRefPtr { *m_fifo };
|
2020-07-16 15:23:03 -06:00
|
|
|
}
|
|
|
|
|
2019-07-22 20:01:11 +02:00
|
|
|
void Inode::set_metadata_dirty(bool metadata_dirty)
|
|
|
|
{
|
2021-07-18 01:13:34 +02:00
|
|
|
MutexLocker 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.
|
2022-02-03 01:39:49 +01:00
|
|
|
m_watchers.for_each([&](auto& watcher) {
|
2021-07-21 21:23:39 +02:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::MetadataModified);
|
2022-02-03 01:39:49 +01:00
|
|
|
});
|
2019-07-22 20:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2022-01-11 22:04:53 +02:00
|
|
|
void Inode::did_add_child(InodeIdentifier, StringView name)
|
2021-05-12 19:17:51 +00:00
|
|
|
{
|
2022-02-03 01:39:49 +01:00
|
|
|
m_watchers.for_each([&](auto& watcher) {
|
2021-07-21 21:23:39 +02:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildCreated, name);
|
2022-02-03 01:39:49 +01:00
|
|
|
});
|
2021-05-12 19:17:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 22:04:53 +02:00
|
|
|
void Inode::did_remove_child(InodeIdentifier, StringView name)
|
2021-05-12 19:17:51 +00:00
|
|
|
{
|
|
|
|
if (name == "." || name == "..") {
|
|
|
|
// These are just aliases and are not interesting to userspace.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-03 01:39:49 +01:00
|
|
|
m_watchers.for_each([&](auto& watcher) {
|
2021-07-21 21:23:39 +02:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildDeleted, name);
|
2022-02-03 01:39:49 +01:00
|
|
|
});
|
2021-05-12 19:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::did_modify_contents()
|
2020-07-04 13:36:55 +02:00
|
|
|
{
|
2022-06-18 19:08:18 +02:00
|
|
|
// FIXME: What happens if this fails?
|
|
|
|
// ENOTIMPL would be a meaningless error to return here
|
2022-11-22 21:01:45 +01:00
|
|
|
auto now = kgettimeofday();
|
|
|
|
(void)update_timestamps({}, now, now);
|
2022-06-18 19:08:18 +02:00
|
|
|
|
2022-02-03 01:39:49 +01:00
|
|
|
m_watchers.for_each([&](auto& watcher) {
|
2021-07-21 21:23:39 +02:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ContentModified);
|
2022-02-03 01:39:49 +01:00
|
|
|
});
|
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
|
|
|
{
|
2022-02-03 01:39:49 +01:00
|
|
|
m_watchers.for_each([&](auto& watcher) {
|
2021-07-21 21:23:39 +02:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::Deleted);
|
2022-02-03 01:39:49 +01:00
|
|
|
});
|
2020-07-04 13:36:55 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> Inode::prepare_to_write_data()
|
2020-04-04 19:46:55 +02:00
|
|
|
{
|
2022-07-27 21:42:16 +03:00
|
|
|
VERIFY(m_inode_lock.is_locked());
|
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));
|
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-04-04 19:46:55 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<Memory::SharedInodeVMObject> Inode::shared_vmobject() const
|
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
|
|
|
{
|
2021-07-18 01:13:34 +02:00
|
|
|
MutexLocker 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();
|
|
|
|
}
|
|
|
|
|
2021-07-18 23:29:56 -06:00
|
|
|
template<typename T>
|
|
|
|
static inline bool range_overlap(T start1, T len1, T start2, T len2)
|
|
|
|
{
|
|
|
|
return ((start1 < start2 + len2) || len2 == 0) && ((start2 < start1 + len1) || len1 == 0);
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
static inline ErrorOr<void> normalize_flock(OpenFileDescription const& description, flock& lock)
|
2021-07-18 23:29:56 -06:00
|
|
|
{
|
|
|
|
off_t start;
|
|
|
|
switch (lock.l_whence) {
|
|
|
|
case SEEK_SET:
|
|
|
|
start = lock.l_start;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
start = description.offset() + lock.l_start;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
// FIXME: Implement SEEK_END and negative lengths.
|
|
|
|
return ENOTSUP;
|
|
|
|
default:
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
lock = { lock.l_type, SEEK_SET, start, lock.l_len, 0 };
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2021-07-18 23:29:56 -06:00
|
|
|
}
|
|
|
|
|
2022-10-01 19:28:30 +00:00
|
|
|
bool Inode::can_apply_flock(flock const& new_lock, Optional<OpenFileDescription const&> description) const
|
2021-07-18 23:29:56 -06:00
|
|
|
{
|
|
|
|
VERIFY(new_lock.l_whence == SEEK_SET);
|
|
|
|
|
2022-05-13 12:20:57 +02:00
|
|
|
if (new_lock.l_type == F_UNLCK)
|
2022-07-14 02:17:01 +03:00
|
|
|
return true;
|
2022-05-13 12:20:57 +02:00
|
|
|
|
2022-07-14 02:17:01 +03:00
|
|
|
return m_flocks.with([&](auto& flocks) {
|
2022-02-03 17:28:45 +01:00
|
|
|
for (auto const& lock : flocks) {
|
|
|
|
if (!range_overlap(lock.start, lock.len, new_lock.l_start, new_lock.l_len))
|
|
|
|
continue;
|
2021-07-18 23:29:56 -06:00
|
|
|
|
2022-10-01 19:28:30 +00:00
|
|
|
// There are two cases where we can attempt downgrade:
|
|
|
|
//
|
|
|
|
// 1) We're the owner of this lock. The downgrade will immediately
|
|
|
|
// succeed.
|
|
|
|
// 2) We're not the owner of this lock. Our downgrade attempt will
|
|
|
|
// fail, and the thread will start blocking on an FlockBlocker.
|
|
|
|
//
|
|
|
|
// For the first case, we get the description from try_apply_flock
|
|
|
|
// below. For the second case, the check below would always be
|
|
|
|
// false, so there is no need to store the description in the
|
|
|
|
// blocker in the first place.
|
2022-02-03 17:28:45 +01:00
|
|
|
if (new_lock.l_type == F_RDLCK && lock.type == F_WRLCK)
|
2022-10-01 19:28:30 +00:00
|
|
|
return description.has_value() && lock.owner == &description.value() && lock.start == new_lock.l_start && lock.len == new_lock.l_len;
|
2021-07-18 23:29:56 -06:00
|
|
|
|
2022-02-03 17:28:45 +01:00
|
|
|
if (new_lock.l_type == F_WRLCK)
|
2022-07-14 02:17:01 +03:00
|
|
|
return false;
|
2022-02-03 17:28:45 +01:00
|
|
|
}
|
2022-07-14 02:17:01 +03:00
|
|
|
return true;
|
2022-02-03 17:28:45 +01:00
|
|
|
});
|
2021-07-18 23:29:56 -06:00
|
|
|
}
|
|
|
|
|
2022-10-01 19:28:30 +00:00
|
|
|
ErrorOr<bool> Inode::try_apply_flock(Process const& process, OpenFileDescription const& description, flock const& new_lock)
|
2021-07-18 23:29:56 -06:00
|
|
|
{
|
2022-07-14 02:17:01 +03:00
|
|
|
return m_flocks.with([&](auto& flocks) -> ErrorOr<bool> {
|
2022-10-01 19:28:30 +00:00
|
|
|
if (!can_apply_flock(new_lock, description))
|
2022-07-14 02:17:01 +03:00
|
|
|
return false;
|
2021-07-18 23:29:56 -06:00
|
|
|
|
2022-10-01 19:28:30 +00:00
|
|
|
bool did_manipulate_lock = false;
|
|
|
|
for (size_t i = 0; i < flocks.size(); ++i) {
|
|
|
|
auto const& lock = flocks[i];
|
|
|
|
|
|
|
|
bool is_potential_downgrade = new_lock.l_type == F_RDLCK && lock.type == F_WRLCK;
|
|
|
|
bool is_potential_unlock = new_lock.l_type == F_UNLCK;
|
2022-05-13 12:20:57 +02:00
|
|
|
|
2022-10-01 19:28:30 +00:00
|
|
|
bool is_lock_owner = &description == lock.owner;
|
|
|
|
bool lock_range_exactly_matches = lock.start == new_lock.l_start && lock.len == new_lock.l_len;
|
|
|
|
bool can_manage_this_lock = is_lock_owner && lock_range_exactly_matches;
|
2022-07-14 02:17:01 +03:00
|
|
|
|
2022-10-01 19:28:30 +00:00
|
|
|
if ((is_potential_downgrade || is_potential_unlock) && can_manage_this_lock) {
|
|
|
|
flocks.remove(i);
|
|
|
|
did_manipulate_lock = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-18 23:29:56 -06:00
|
|
|
}
|
|
|
|
|
2022-10-01 19:28:30 +00:00
|
|
|
if (new_lock.l_type != F_UNLCK)
|
|
|
|
TRY(flocks.try_append(Flock { new_lock.l_start, new_lock.l_len, &description, process.pid().value(), new_lock.l_type }));
|
|
|
|
|
|
|
|
if (did_manipulate_lock)
|
|
|
|
m_flock_blocker_set.unblock_all_blockers_whose_conditions_are_met();
|
|
|
|
|
|
|
|
// Judging by the Linux implementation, unlocking a non-existent lock
|
|
|
|
// also works.
|
2022-07-14 02:17:01 +03:00
|
|
|
return true;
|
2022-02-03 17:28:45 +01:00
|
|
|
});
|
2021-07-18 23:29:56 -06:00
|
|
|
}
|
|
|
|
|
2022-07-14 02:17:01 +03:00
|
|
|
ErrorOr<void> Inode::apply_flock(Process const& process, OpenFileDescription const& description, Userspace<flock const*> input_lock, ShouldBlock should_block)
|
|
|
|
{
|
|
|
|
auto new_lock = TRY(copy_typed_from_user(input_lock));
|
|
|
|
TRY(normalize_flock(description, new_lock));
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
auto success = TRY(try_apply_flock(process, description, new_lock));
|
|
|
|
if (success)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (should_block == ShouldBlock::No)
|
|
|
|
return EAGAIN;
|
|
|
|
|
|
|
|
if (Thread::current()->block<Thread::FlockBlocker>({}, *this, new_lock).was_interrupted())
|
|
|
|
return EINTR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> Inode::get_flock(OpenFileDescription const& description, Userspace<flock*> reference_lock) const
|
2021-07-18 23:29:56 -06:00
|
|
|
{
|
2021-09-05 16:08:27 +02:00
|
|
|
flock lookup = {};
|
2021-09-05 17:38:37 +02:00
|
|
|
TRY(copy_from_user(&lookup, reference_lock));
|
2021-09-05 16:08:27 +02:00
|
|
|
TRY(normalize_flock(description, lookup));
|
2021-07-18 23:29:56 -06:00
|
|
|
|
2022-02-03 17:28:45 +01:00
|
|
|
return m_flocks.with([&](auto& flocks) {
|
|
|
|
for (auto const& lock : flocks) {
|
|
|
|
if (!range_overlap(lock.start, lock.len, lookup.l_start, lookup.l_len))
|
|
|
|
continue;
|
2021-07-18 23:29:56 -06:00
|
|
|
|
2022-06-17 19:06:05 +02:00
|
|
|
// Locks with the same owner can't conflict with each other.
|
|
|
|
if (lock.pid == Process::current().pid())
|
|
|
|
continue;
|
|
|
|
|
2022-02-03 17:28:45 +01:00
|
|
|
if ((lookup.l_type == F_RDLCK && lock.type == F_WRLCK) || lookup.l_type == F_WRLCK) {
|
|
|
|
lookup = { lock.type, SEEK_SET, lock.start, lock.len, lock.pid };
|
|
|
|
return copy_to_user(reference_lock, &lookup);
|
|
|
|
}
|
2021-07-18 23:29:56 -06:00
|
|
|
}
|
|
|
|
|
2022-02-03 17:28:45 +01:00
|
|
|
lookup.l_type = F_UNLCK;
|
|
|
|
return copy_to_user(reference_lock, &lookup);
|
|
|
|
});
|
2021-07-18 23:29:56 -06:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
void Inode::remove_flocks_for_description(OpenFileDescription const& description)
|
2021-07-18 23:29:56 -06:00
|
|
|
{
|
2022-02-03 17:28:45 +01:00
|
|
|
m_flocks.with([&](auto& flocks) {
|
|
|
|
flocks.remove_all_matching([&](auto& entry) { return entry.owner == &description; });
|
|
|
|
});
|
2021-07-18 23:29:56 -06:00
|
|
|
}
|
2022-02-03 17:28:45 +01:00
|
|
|
|
2022-02-03 01:39:49 +01:00
|
|
|
bool Inode::has_watchers() const
|
|
|
|
{
|
|
|
|
return !m_watchers.with([&](auto& watchers) { return watchers.is_empty(); });
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|