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>
|
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-05-16 03:02:37 +02:00
|
|
|
#pragma once
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
#include <AK/Error.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <AK/Function.h>
|
2021-07-21 21:23:39 +02:00
|
|
|
#include <AK/HashTable.h>
|
2021-05-26 02:18:23 -07:00
|
|
|
#include <AK/IntrusiveList.h>
|
2020-07-16 15:23:03 -06:00
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
2020-02-16 01:50:16 +01:00
|
|
|
#include <Kernel/Forward.h>
|
2021-08-17 01:05:06 +02:00
|
|
|
#include <Kernel/Library/ListedRefCounted.h>
|
2022-08-19 20:53:40 +02:00
|
|
|
#include <Kernel/Library/LockWeakPtr.h>
|
2021-07-18 09:10:27 +02:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2022-08-12 09:48:59 -04:00
|
|
|
#include <Kernel/Memory/SharedInodeVMObject.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-07-14 02:17:01 +03:00
|
|
|
enum class ShouldBlock {
|
|
|
|
No = 0,
|
|
|
|
Yes = 1
|
|
|
|
};
|
|
|
|
|
2021-12-29 00:22:14 +02:00
|
|
|
class Inode : public ListedRefCounted<Inode, LockType::Spinlock>
|
2022-08-19 20:53:40 +02:00
|
|
|
, public LockWeakable<Inode> {
|
2021-07-11 00:25:24 +02:00
|
|
|
friend class VirtualFileSystem;
|
2021-07-11 00:20:38 +02:00
|
|
|
friend class FileSystem;
|
2022-07-27 21:42:16 +03:00
|
|
|
friend class InodeFile;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2019-05-16 03:02:37 +02:00
|
|
|
public:
|
|
|
|
virtual ~Inode();
|
|
|
|
|
2022-01-11 00:51:05 +01:00
|
|
|
virtual void remove_from_secondary_lists() { }
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2021-07-11 00:20:38 +02:00
|
|
|
FileSystem& fs() { return m_file_system; }
|
|
|
|
FileSystem const& fs() const { return m_file_system; }
|
2021-11-18 15:11:31 +01:00
|
|
|
FileSystemID fsid() const { return m_file_system.fsid(); }
|
2021-02-12 09:18:47 +01:00
|
|
|
InodeIndex index() const { return m_index; }
|
2019-05-16 03:02:37 +02:00
|
|
|
|
|
|
|
size_t size() const { return metadata().size; }
|
|
|
|
bool is_symlink() const { return metadata().is_symlink(); }
|
|
|
|
bool is_directory() const { return metadata().is_directory(); }
|
|
|
|
bool is_character_device() const { return metadata().is_character_device(); }
|
|
|
|
mode_t mode() const { return metadata().mode; }
|
|
|
|
|
|
|
|
InodeIdentifier identifier() const { return { fsid(), index() }; }
|
|
|
|
virtual InodeMetadata metadata() const = 0;
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<NonnullOwnPtr<KBuffer>> read_entire(OpenFileDescription* = nullptr) const;
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2022-08-06 04:22:20 +03:00
|
|
|
ErrorOr<size_t> write_bytes(off_t, size_t, UserOrKernelBuffer const& data, OpenFileDescription*);
|
|
|
|
ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const;
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> attach(OpenFileDescription&) { return {}; }
|
2021-09-07 13:39:11 +02:00
|
|
|
virtual void detach(OpenFileDescription&) { }
|
|
|
|
virtual void did_seek(OpenFileDescription&, off_t) { }
|
2021-11-10 15:42:39 +01:00
|
|
|
virtual ErrorOr<void> traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const = 0;
|
2022-08-19 20:53:40 +02:00
|
|
|
virtual ErrorOr<NonnullLockRefPtr<Inode>> lookup(StringView name) = 0;
|
|
|
|
virtual ErrorOr<NonnullLockRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, UserID, GroupID) = 0;
|
2021-11-11 00:55:02 +01:00
|
|
|
virtual ErrorOr<void> add_child(Inode&, StringView name, mode_t) = 0;
|
|
|
|
virtual ErrorOr<void> remove_child(StringView name) = 0;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> chmod(mode_t) = 0;
|
|
|
|
virtual ErrorOr<void> chown(UserID, GroupID) = 0;
|
|
|
|
virtual ErrorOr<void> truncate(u64) { return {}; }
|
2022-08-21 16:17:13 +02:00
|
|
|
|
|
|
|
ErrorOr<NonnullRefPtr<Custody>> resolve_as_link(Credentials const&, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const;
|
2021-11-08 00:51:39 +01:00
|
|
|
|
|
|
|
virtual ErrorOr<int> get_block_address(int) { return ENOTSUP; }
|
2021-01-30 13:12:49 -07:00
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<LocalSocket> bound_socket() const;
|
2019-05-16 03:02:37 +02:00
|
|
|
bool bind_socket(LocalSocket&);
|
|
|
|
bool unbind_socket();
|
|
|
|
|
|
|
|
bool is_metadata_dirty() const { return m_metadata_dirty; }
|
|
|
|
|
2022-08-22 13:34:22 +02:00
|
|
|
virtual ErrorOr<void> update_timestamps(Optional<time_t> atime, Optional<time_t> ctime, Optional<time_t> mtime);
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> increment_link_count();
|
|
|
|
virtual ErrorOr<void> decrement_link_count();
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> flush_metadata() = 0;
|
2019-05-16 03:02:37 +02:00
|
|
|
|
|
|
|
void will_be_destroyed();
|
|
|
|
|
2022-02-14 01:46:34 +02:00
|
|
|
ErrorOr<void> set_shared_vmobject(Memory::SharedInodeVMObject&);
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<Memory::SharedInodeVMObject> shared_vmobject() const;
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2021-09-11 23:28:59 -04:00
|
|
|
static void sync_all();
|
|
|
|
void sync();
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2022-02-03 01:39:49 +01:00
|
|
|
bool has_watchers() const;
|
2019-11-04 16:31:46 +01:00
|
|
|
|
2022-01-25 15:13:59 +02:00
|
|
|
ErrorOr<void> register_watcher(Badge<InodeWatcher>, InodeWatcher&);
|
2019-07-22 20:01:11 +02:00
|
|
|
void unregister_watcher(Badge<InodeWatcher>, InodeWatcher&);
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
ErrorOr<NonnullLockRefPtr<FIFO>> fifo();
|
2020-07-16 15:23:03 -06:00
|
|
|
|
2022-07-14 02:17:01 +03:00
|
|
|
bool can_apply_flock(flock const&) const;
|
|
|
|
ErrorOr<void> apply_flock(Process const&, OpenFileDescription const&, Userspace<flock const*>, ShouldBlock);
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> get_flock(OpenFileDescription const&, Userspace<flock*>) const;
|
2021-09-07 13:39:11 +02:00
|
|
|
void remove_flocks_for_description(OpenFileDescription const&);
|
2022-07-14 02:17:01 +03:00
|
|
|
Thread::FlockBlockerSet& flock_blocker_set() { return m_flock_blocker_set; };
|
2021-07-18 23:29:56 -06:00
|
|
|
|
2019-05-16 03:02:37 +02:00
|
|
|
protected:
|
2021-07-11 00:20:38 +02:00
|
|
|
Inode(FileSystem&, InodeIndex);
|
2019-07-22 20:01:11 +02:00
|
|
|
void set_metadata_dirty(bool);
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> prepare_to_write_data();
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2022-01-11 22:04:53 +02:00
|
|
|
void did_add_child(InodeIdentifier child_id, StringView);
|
|
|
|
void did_remove_child(InodeIdentifier child_id, StringView);
|
2021-05-12 19:17:51 +00:00
|
|
|
void did_modify_contents();
|
|
|
|
void did_delete_self();
|
2020-07-04 13:36:55 +02:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
mutable Mutex m_inode_lock { "Inode"sv };
|
2019-05-16 03:02:37 +02:00
|
|
|
|
2022-08-06 04:22:20 +03:00
|
|
|
virtual ErrorOr<size_t> write_bytes_locked(off_t, size_t, UserOrKernelBuffer const& data, OpenFileDescription*) = 0;
|
|
|
|
virtual ErrorOr<size_t> read_bytes_locked(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const = 0;
|
|
|
|
|
2019-05-16 03:02:37 +02:00
|
|
|
private:
|
2022-07-14 02:17:01 +03:00
|
|
|
ErrorOr<bool> try_apply_flock(Process const&, OpenFileDescription const&, flock const&);
|
|
|
|
|
2021-07-11 00:20:38 +02:00
|
|
|
FileSystem& m_file_system;
|
2021-02-12 09:18:47 +01:00
|
|
|
InodeIndex m_index { 0 };
|
2022-08-19 20:53:40 +02:00
|
|
|
LockWeakPtr<Memory::SharedInodeVMObject> m_shared_vmobject;
|
|
|
|
LockRefPtr<LocalSocket> m_bound_socket;
|
2022-08-18 21:46:28 +02:00
|
|
|
SpinlockProtected<HashTable<InodeWatcher*>> m_watchers { LockRank::None };
|
2019-05-16 03:02:37 +02:00
|
|
|
bool m_metadata_dirty { false };
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<FIFO> m_fifo;
|
2021-05-26 02:18:23 -07:00
|
|
|
IntrusiveListNode<Inode> m_inode_list_node;
|
|
|
|
|
2021-07-18 23:29:56 -06:00
|
|
|
struct Flock {
|
|
|
|
off_t start;
|
|
|
|
off_t len;
|
2021-09-07 13:39:11 +02:00
|
|
|
OpenFileDescription const* owner;
|
2021-07-18 23:29:56 -06:00
|
|
|
pid_t pid;
|
2021-09-15 23:46:45 -07:00
|
|
|
short type;
|
2021-07-18 23:29:56 -06:00
|
|
|
};
|
|
|
|
|
2022-07-14 02:17:01 +03:00
|
|
|
Thread::FlockBlockerSet m_flock_blocker_set;
|
2022-08-18 21:46:28 +02:00
|
|
|
SpinlockProtected<Vector<Flock>> m_flocks { LockRank::None };
|
2021-07-18 23:29:56 -06:00
|
|
|
|
2021-05-26 02:18:23 -07:00
|
|
|
public:
|
2021-09-09 16:30:59 +04:30
|
|
|
using AllInstancesList = IntrusiveList<&Inode::m_inode_list_node>;
|
2021-08-22 01:37:17 +02:00
|
|
|
static SpinlockProtected<Inode::AllInstancesList>& all_instances();
|
2019-05-16 03:02:37 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|