2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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;
|
2020-08-24 19:35:19 -06:00
|
|
|
static AK::Singleton<InlineLinkedList<Inode>> s_list;
|
2020-07-09 13:51:58 -06:00
|
|
|
|
2020-11-23 16:19:30 +01:00
|
|
|
SpinLock<u32>& Inode::all_inodes_lock()
|
|
|
|
{
|
|
|
|
return s_all_inodes_lock;
|
|
|
|
}
|
|
|
|
|
2020-08-10 23:55:10 +02:00
|
|
|
InlineLinkedList<Inode>& Inode::all_with_lock()
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2020-07-09 13:51:58 -06:00
|
|
|
ASSERT(s_all_inodes_lock.is_locked());
|
|
|
|
|
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) {
|
2019-06-27 13:44:26 +02:00
|
|
|
ASSERT(inode.is_metadata_dirty());
|
|
|
|
inode.flush_metadata();
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 14:10:10 +01:00
|
|
|
KResultOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(FileDescription* descriptor) 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
|
|
|
|
|
|
|
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 (;;) {
|
2020-09-11 21:11:07 -06:00
|
|
|
auto buf = UserOrKernelBuffer::for_kernel_buffer(buffer);
|
|
|
|
nread = read_bytes(offset, sizeof(buffer), buf, descriptor);
|
|
|
|
if (nread < 0)
|
|
|
|
return KResult(nread);
|
2019-05-16 03:02:37 +02:00
|
|
|
ASSERT(nread <= (ssize_t)sizeof(buffer));
|
|
|
|
if (nread <= 0)
|
|
|
|
break;
|
|
|
|
builder.append((const char*)buffer, nread);
|
|
|
|
offset += nread;
|
2020-01-15 22:09:45 +01:00
|
|
|
if (nread < (ssize_t)sizeof(buffer))
|
|
|
|
break;
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
if (nread < 0) {
|
2020-03-01 21:45:39 +02:00
|
|
|
klog() << "Inode::read_entire: ERROR: " << nread;
|
2020-08-11 19:47:24 +02:00
|
|
|
return KResult(nread);
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 14:10:10 +01:00
|
|
|
auto entire_file = builder.build();
|
|
|
|
if (!entire_file)
|
|
|
|
return KResult(-ENOMEM);
|
|
|
|
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());
|
2020-01-15 13:59:50 +03:00
|
|
|
return VFS::the().resolve_path(path, base, out_parent, options, symlink_recursion_level);
|
|
|
|
}
|
|
|
|
|
2019-05-16 03:02:37 +02:00
|
|
|
Inode::Inode(FS& fs, unsigned index)
|
|
|
|
: m_fs(fs)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
2020-07-09 13:51:58 -06:00
|
|
|
ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
|
2020-08-10 23:55:10 +02: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);
|
2020-08-10 23:55:10 +02:00
|
|
|
all_with_lock().remove(this);
|
2019-05-16 03:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inode::will_be_destroyed()
|
|
|
|
{
|
|
|
|
if (m_metadata_dirty)
|
|
|
|
flush_metadata();
|
|
|
|
}
|
|
|
|
|
2020-09-11 21:11:07 -06:00
|
|
|
void Inode::inode_contents_changed(off_t offset, ssize_t size, const UserOrKernelBuffer& data)
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
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
|
|
|
if (auto shared_vmobject = this->shared_vmobject())
|
|
|
|
shared_vmobject->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)
|
|
|
|
{
|
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
|
|
|
if (auto shared_vmobject = this->shared_vmobject())
|
|
|
|
shared_vmobject->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;
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:26:33 +01:00
|
|
|
KResult Inode::increment_link_count()
|
2019-05-16 03:02:37 +02:00
|
|
|
{
|
2020-02-08 02:26:33 +01:00
|
|
|
return KResult(-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
|
|
|
{
|
2020-02-08 02:26:33 +01:00
|
|
|
return KResult(-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
|
|
|
{
|
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)
|
|
|
|
{
|
2020-01-30 22:15:45 +01:00
|
|
|
LOCKER(m_lock);
|
|
|
|
if (m_socket)
|
|
|
|
return false;
|
2019-05-16 03:02:37 +02:00
|
|
|
m_socket = socket;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::unbind_socket()
|
|
|
|
{
|
2020-01-30 22:15:45 +01:00
|
|
|
LOCKER(m_lock);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:23:03 -06:00
|
|
|
FIFO& Inode::fifo()
|
|
|
|
{
|
|
|
|
ASSERT(metadata().is_fifo());
|
|
|
|
|
|
|
|
// FIXME: Release m_fifo when it is closed by all readers and writers
|
|
|
|
if (!m_fifo)
|
|
|
|
m_fifo = FIFO::create(metadata().uid);
|
|
|
|
|
|
|
|
ASSERT(m_fifo);
|
|
|
|
return *m_fifo;
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:01:11 +02:00
|
|
|
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) {
|
2020-11-07 09:38:48 +02:00
|
|
|
watcher->notify_inode_event({}, InodeWatcherEvent::Type::Modified);
|
2019-07-22 20:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2020-09-19 16:39:52 +02:00
|
|
|
void Inode::did_add_child(const InodeIdentifier& child_id)
|
2020-07-04 13:36:55 +02:00
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
for (auto& watcher : m_watchers) {
|
2020-09-19 16:39:52 +02:00
|
|
|
watcher->notify_child_added({}, child_id);
|
2020-07-04 13:36:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:39:52 +02:00
|
|
|
void Inode::did_remove_child(const InodeIdentifier& child_id)
|
2020-07-04 13:36:55 +02:00
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
for (auto& watcher : m_watchers) {
|
2020-09-19 16:39:52 +02:00
|
|
|
watcher->notify_child_removed({}, child_id);
|
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.
|
|
|
|
// We should funnel everything through an interface at the VFS layer so this can happen from a single place.
|
|
|
|
LOCKER(m_lock);
|
|
|
|
if (fs().is_readonly())
|
|
|
|
return KResult(-EROFS);
|
|
|
|
auto metadata = this->metadata();
|
|
|
|
if (metadata.is_setuid() || metadata.is_setgid()) {
|
|
|
|
dbg() << "Inode::prepare_to_write_data(): Stripping SUID/SGID bits from " << identifier();
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return m_shared_vmobject.strong_ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SharedInodeVMObject> Inode::shared_vmobject() const
|
|
|
|
{
|
|
|
|
return m_shared_vmobject.strong_ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inode::is_shared_vmobject(const SharedInodeVMObject& other) const
|
|
|
|
{
|
|
|
|
return m_shared_vmobject.unsafe_ptr() == &other;
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|