2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-08-17 02:05:06 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-05-12 22:17:51 +03:00
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-27 14:44:26 +03:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Singleton.h>
|
2019-05-16 04:02:37 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2020-11-07 10:38:48 +03:00
|
|
|
#include <Kernel/API/InodeWatcherEvent.h>
|
2020-03-01 22:46:51 +03:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2019-07-22 21:01:11 +03:00
|
|
|
#include <Kernel/FileSystem/InodeWatcher.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-03-01 22:46:51 +03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2020-08-11 20:47:24 +03:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/SharedInodeVMObject.h>
|
2019-05-16 04:02:37 +03:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2021-07-19 08:29:56 +03:00
|
|
|
#include <Kernel/Process.h>
|
2019-05-16 04:02:37 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-22 02:37:17 +03:00
|
|
|
static Singleton<SpinlockProtected<Inode::AllInstancesList>> s_all_instances;
|
2020-07-09 22:51:58 +03:00
|
|
|
|
2021-08-22 02:37:17 +03:00
|
|
|
SpinlockProtected<Inode::AllInstancesList>& Inode::all_instances()
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2021-08-17 02:05:06 +03:00
|
|
|
return s_all_instances;
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:28:59 +03:00
|
|
|
void Inode::sync_all()
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2019-06-27 14:44:26 +03:00
|
|
|
NonnullRefPtrVector<Inode, 32> inodes;
|
2021-08-17 02:05:06 +03:00
|
|
|
Inode::all_instances().with([&](auto& all_inodes) {
|
|
|
|
for (auto& inode : all_inodes) {
|
2019-08-08 14:40:58 +03:00
|
|
|
if (inode.is_metadata_dirty())
|
|
|
|
inodes.append(inode);
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
2021-08-17 02:05:06 +03:00
|
|
|
});
|
2019-05-16 04:02:37 +03:00
|
|
|
|
|
|
|
for (auto& inode : inodes) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(inode.is_metadata_dirty());
|
2021-10-21 18:01:52 +03:00
|
|
|
(void)inode.flush_metadata();
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:28:59 +03:00
|
|
|
void Inode::sync()
|
|
|
|
{
|
|
|
|
if (is_metadata_dirty())
|
2021-10-21 18:01:52 +03:00
|
|
|
(void)flush_metadata();
|
2021-09-12 06:28:59 +03:00
|
|
|
fs().flush_writes();
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(OpenFileDescription* description) const
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2021-09-07 16:54:23 +03:00
|
|
|
auto builder = TRY(KBufferBuilder::try_create());
|
2019-05-16 04:02:37 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 buffer[4096];
|
2019-05-16 04:02:37 +03:00
|
|
|
off_t offset = 0;
|
|
|
|
for (;;) {
|
2020-09-12 06:11:07 +03:00
|
|
|
auto buf = UserOrKernelBuffer::for_kernel_buffer(buffer);
|
2021-09-05 17:08:27 +03:00
|
|
|
auto nread = TRY(read_bytes(offset, sizeof(buffer), buf, description));
|
2021-06-16 17:44:15 +03:00
|
|
|
VERIFY(nread <= sizeof(buffer));
|
2021-06-17 12:16:11 +03:00
|
|
|
if (nread == 0)
|
2019-05-16 04:02:37 +03:00
|
|
|
break;
|
2021-09-06 19:24:13 +03:00
|
|
|
TRY(builder.append((const char*)buffer, nread));
|
2019-05-16 04:02:37 +03:00
|
|
|
offset += nread;
|
2021-06-16 17:44:15 +03:00
|
|
|
if (nread < sizeof(buffer))
|
2020-01-16 00:09:45 +03:00
|
|
|
break;
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:10:10 +03:00
|
|
|
auto entire_file = builder.build();
|
|
|
|
if (!entire_file)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOMEM;
|
2020-12-18 16:10:10 +03:00
|
|
|
return entire_file.release_nonnull();
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(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.
|
2021-09-05 17:08:27 +03:00
|
|
|
auto contents = TRY(read_entire());
|
2021-09-08 19:29:52 +03:00
|
|
|
return VirtualFileSystem::the().resolve_path(StringView { contents->bytes() }, base, out_parent, options, symlink_recursion_level);
|
2020-01-15 13:59:50 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:20:38 +03:00
|
|
|
Inode::Inode(FileSystem& fs, InodeIndex index)
|
|
|
|
: m_file_system(fs)
|
2019-05-16 04:02:37 +03:00
|
|
|
, m_index(index)
|
|
|
|
{
|
2021-08-17 02:05:06 +03:00
|
|
|
Inode::all_instances().with([&](auto& all_inodes) { all_inodes.append(*this); });
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Inode::~Inode()
|
|
|
|
{
|
2021-05-12 22:17:51 +03:00
|
|
|
for (auto& watcher : m_watchers) {
|
2021-07-21 22:23:39 +03:00
|
|
|
watcher->unregister_by_inode({}, identifier());
|
2021-05-12 22:17:51 +03:00
|
|
|
}
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::will_be_destroyed()
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2019-05-16 04:02:37 +03:00
|
|
|
if (m_metadata_dirty)
|
2021-10-21 18:01:52 +03:00
|
|
|
(void)flush_metadata();
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::set_atime(time_t)
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2021-04-30 16:51:06 +03:00
|
|
|
return ENOTIMPL;
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::set_ctime(time_t)
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2021-04-30 16:51:06 +03:00
|
|
|
return ENOTIMPL;
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::set_mtime(time_t)
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2021-04-30 16:51:06 +03:00
|
|
|
return ENOTIMPL;
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::increment_link_count()
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOTIMPL;
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::decrement_link_count()
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOTIMPL;
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
void Inode::set_shared_vmobject(Memory::SharedInodeVMObject& vmobject)
|
2019-05-16 04:02:37 +03:00
|
|
|
{
|
2021-07-18 02:13:34 +03: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-30 01:26:13 +03:00
|
|
|
m_shared_vmobject = vmobject;
|
2019-05-16 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::bind_socket(LocalSocket& socket)
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2020-01-31 00:15:45 +03:00
|
|
|
if (m_socket)
|
|
|
|
return false;
|
2019-05-16 04:02:37 +03:00
|
|
|
m_socket = socket;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::unbind_socket()
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2020-01-31 00:15:45 +03:00
|
|
|
if (!m_socket)
|
|
|
|
return false;
|
2019-05-16 04:02:37 +03:00
|
|
|
m_socket = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-22 21:01:11 +03:00
|
|
|
|
|
|
|
void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2021-07-21 22:23:39 +03:00
|
|
|
VERIFY(!m_watchers.contains(&watcher));
|
|
|
|
m_watchers.set(&watcher);
|
2019-07-22 21:01:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2021-07-21 22:23:39 +03:00
|
|
|
VERIFY(m_watchers.contains(&watcher));
|
|
|
|
m_watchers.remove(&watcher);
|
2019-07-22 21:01:11 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullRefPtr<FIFO>> Inode::fifo()
|
2020-07-17 00:23:03 +03:00
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(metadata().is_fifo());
|
2020-07-17 00:23:03 +03:00
|
|
|
|
|
|
|
// FIXME: Release m_fifo when it is closed by all readers and writers
|
2021-09-07 14:56:10 +03:00
|
|
|
if (!m_fifo)
|
|
|
|
m_fifo = TRY(FIFO::try_create(metadata().uid));
|
2020-07-17 00:23:03 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
return NonnullRefPtr { *m_fifo };
|
2020-07-17 00:23:03 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 21:01:11 +03:00
|
|
|
void Inode::set_metadata_dirty(bool metadata_dirty)
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2021-01-17 23:32:59 +03:00
|
|
|
|
|
|
|
if (metadata_dirty) {
|
|
|
|
// Sanity check.
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!fs().is_readonly());
|
2021-01-17 23:32:59 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 21:01:11 +03: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-07-21 22:23:39 +03:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::MetadataModified);
|
2019-07-22 21:01:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-05-12 22:17:51 +03:00
|
|
|
void Inode::did_add_child(InodeIdentifier const&, String const& name)
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2021-05-12 22:17:51 +03:00
|
|
|
|
|
|
|
for (auto& watcher : m_watchers) {
|
2021-07-21 22:23:39 +03:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildCreated, name);
|
2021-05-12 22:17:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::did_remove_child(InodeIdentifier const&, String const& name)
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2021-05-12 22:17:51 +03:00
|
|
|
|
|
|
|
if (name == "." || name == "..") {
|
|
|
|
// These are just aliases and are not interesting to userspace.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& watcher : m_watchers) {
|
2021-07-21 22:23:39 +03:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildDeleted, name);
|
2021-05-12 22:17:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::did_modify_contents()
|
2020-07-04 14:36:55 +03:00
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2020-07-04 14:36:55 +03:00
|
|
|
for (auto& watcher : m_watchers) {
|
2021-07-21 22:23:39 +03:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ContentModified);
|
2020-07-04 14:36:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 22:17:51 +03:00
|
|
|
void Inode::did_delete_self()
|
2020-07-04 14:36:55 +03:00
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2020-07-04 14:36:55 +03:00
|
|
|
for (auto& watcher : m_watchers) {
|
2021-07-21 22:23:39 +03:00
|
|
|
watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::Deleted);
|
2020-07-04 14:36:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::prepare_to_write_data()
|
2020-04-04 20:46:55 +03:00
|
|
|
{
|
|
|
|
// FIXME: It's a poor design that filesystems are expected to call this before writing out data.
|
2021-07-11 01:25:24 +03:00
|
|
|
// We should funnel everything through an interface at the VirtualFileSystem layer so this can happen from a single place.
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_inode_lock);
|
2020-04-04 20:46:55 +03:00
|
|
|
if (fs().is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2020-04-04 20:46:55 +03:00
|
|
|
auto metadata = this->metadata();
|
|
|
|
if (metadata.is_setuid() || metadata.is_setgid()) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Inode::prepare_to_write_data(): Stripping SUID/SGID bits from {}", identifier());
|
2020-04-04 20:46:55 +03:00
|
|
|
return chmod(metadata.mode & ~(04000 | 02000));
|
|
|
|
}
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-04-04 20:46:55 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
RefPtr<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-30 01:26:13 +03:00
|
|
|
{
|
2021-07-18 02:13:34 +03: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-30 01:26:13 +03:00
|
|
|
return m_shared_vmobject.strong_ref();
|
|
|
|
}
|
|
|
|
|
2021-07-19 08:29:56 +03: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 02:51:39 +03:00
|
|
|
static inline ErrorOr<void> normalize_flock(OpenFileDescription const& description, flock& lock)
|
2021-07-19 08:29:56 +03: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 02:51:39 +03:00
|
|
|
return {};
|
2021-07-19 08:29:56 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::can_apply_flock(OpenFileDescription const& description, flock const& new_lock) const
|
2021-07-19 08:29:56 +03:00
|
|
|
{
|
|
|
|
VERIFY(new_lock.l_whence == SEEK_SET);
|
|
|
|
|
|
|
|
MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
|
|
|
|
|
|
|
|
if (new_lock.l_type == F_UNLCK) {
|
|
|
|
for (auto& lock : m_flocks) {
|
|
|
|
if (&description == lock.owner && lock.start == new_lock.l_start && lock.len == new_lock.l_len)
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2021-07-19 08:29:56 +03:00
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& lock : m_flocks) {
|
|
|
|
if (!range_overlap(lock.start, lock.len, new_lock.l_start, new_lock.l_len))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (new_lock.l_type == F_RDLCK && lock.type == F_WRLCK)
|
|
|
|
return EAGAIN;
|
|
|
|
|
|
|
|
if (new_lock.l_type == F_WRLCK)
|
|
|
|
return EAGAIN;
|
|
|
|
}
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2021-07-19 08:29:56 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::apply_flock(Process const& process, OpenFileDescription const& description, Userspace<flock const*> input_lock)
|
2021-07-19 08:29:56 +03:00
|
|
|
{
|
2021-09-05 17:08:27 +03:00
|
|
|
flock new_lock = {};
|
2021-09-05 18:38:37 +03:00
|
|
|
TRY(copy_from_user(&new_lock, input_lock));
|
2021-09-05 17:08:27 +03:00
|
|
|
TRY(normalize_flock(description, new_lock));
|
2021-07-19 08:29:56 +03:00
|
|
|
|
|
|
|
MutexLocker locker(m_inode_lock);
|
|
|
|
|
2021-09-05 17:08:27 +03:00
|
|
|
TRY(can_apply_flock(description, new_lock));
|
2021-07-19 08:29:56 +03:00
|
|
|
|
|
|
|
if (new_lock.l_type == F_UNLCK) {
|
|
|
|
for (size_t i = 0; i < m_flocks.size(); ++i) {
|
|
|
|
if (&description == m_flocks[i].owner && m_flocks[i].start == new_lock.l_start && m_flocks[i].len == new_lock.l_len) {
|
|
|
|
m_flocks.remove(i);
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2021-07-19 08:29:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-11-18 15:33:09 +03:00
|
|
|
TRY(m_flocks.try_append(Flock { new_lock.l_start, new_lock.l_len, &description, process.pid().value(), new_lock.l_type }));
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2021-07-19 08:29:56 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Inode::get_flock(OpenFileDescription const& description, Userspace<flock*> reference_lock) const
|
2021-07-19 08:29:56 +03:00
|
|
|
{
|
2021-09-05 17:08:27 +03:00
|
|
|
flock lookup = {};
|
2021-09-05 18:38:37 +03:00
|
|
|
TRY(copy_from_user(&lookup, reference_lock));
|
2021-09-05 17:08:27 +03:00
|
|
|
TRY(normalize_flock(description, lookup));
|
2021-07-19 08:29:56 +03:00
|
|
|
|
|
|
|
MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
|
|
|
|
|
|
|
|
for (auto& lock : m_flocks) {
|
|
|
|
if (!range_overlap(lock.start, lock.len, lookup.l_start, lookup.l_len))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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 };
|
2021-09-05 18:38:37 +03:00
|
|
|
return copy_to_user(reference_lock, &lookup);
|
2021-07-19 08:29:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lookup.l_type = F_UNLCK;
|
2021-09-05 18:38:37 +03:00
|
|
|
return copy_to_user(reference_lock, &lookup);
|
2021-07-19 08:29:56 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
void Inode::remove_flocks_for_description(OpenFileDescription const& description)
|
2021-07-19 08:29:56 +03:00
|
|
|
{
|
|
|
|
MutexLocker locker(m_inode_lock);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_flocks.size(); ++i) {
|
|
|
|
if (&description == m_flocks[i].owner)
|
|
|
|
m_flocks.remove(i--);
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|