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