2020-01-18 11:38:21 +03: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-05-16 04:02:37 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2020-02-18 12:19:32 +03:00
|
|
|
#include <AK/HashTable.h>
|
2019-08-08 12:08:27 +03:00
|
|
|
#include <AK/InlineLinkedList.h>
|
2019-06-21 19:58:45 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2020-02-16 03:27:42 +03:00
|
|
|
#include <AK/String.h>
|
2019-05-16 04:02:37 +03:00
|
|
|
#include <AK/WeakPtr.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>
|
2019-05-16 04:02:37 +03:00
|
|
|
#include <Kernel/KResult.h>
|
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-08-08 12:08:27 +03:00
|
|
|
class Inode : public RefCounted<Inode>
|
|
|
|
, public Weakable<Inode>
|
|
|
|
, public InlineLinkedListNode<Inode> {
|
2019-05-16 04:02:37 +03:00
|
|
|
friend class VFS;
|
|
|
|
friend class FS;
|
2019-05-28 12:53:16 +03:00
|
|
|
|
2019-05-16 04:02:37 +03:00
|
|
|
public:
|
|
|
|
virtual ~Inode();
|
|
|
|
|
2020-05-26 10:35:11 +03:00
|
|
|
virtual void one_ref_left() { }
|
2019-05-16 04:02:37 +03:00
|
|
|
|
|
|
|
FS& fs() { return m_fs; }
|
|
|
|
const FS& fs() const { return m_fs; }
|
2020-05-18 21:46:02 +03:00
|
|
|
unsigned fsid() const { return m_fs.fsid(); }
|
2019-05-16 04:02:37 +03:00
|
|
|
unsigned index() const { return m_index; }
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-12-18 16:10:10 +03:00
|
|
|
KResultOr<NonnullOwnPtr<KBuffer>> read_entire(FileDescription* = nullptr) const;
|
2019-05-16 04:02:37 +03:00
|
|
|
|
2020-09-17 22:51:09 +03:00
|
|
|
virtual KResult attach(FileDescription&) { return KSuccess; }
|
|
|
|
virtual void detach(FileDescription&) { }
|
|
|
|
virtual void did_seek(FileDescription&, off_t) { }
|
2020-09-12 06:11:07 +03:00
|
|
|
virtual ssize_t read_bytes(off_t, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const = 0;
|
2020-08-18 13:41:27 +03:00
|
|
|
virtual KResult traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const = 0;
|
2020-02-01 11:23:46 +03:00
|
|
|
virtual RefPtr<Inode> lookup(StringView name) = 0;
|
2020-09-12 06:11:07 +03:00
|
|
|
virtual ssize_t write_bytes(off_t, ssize_t, const UserOrKernelBuffer& data, FileDescription*) = 0;
|
2020-06-24 23:35:56 +03:00
|
|
|
virtual KResultOr<NonnullRefPtr<Inode>> create_child(const String& name, mode_t, dev_t, uid_t, gid_t) = 0;
|
|
|
|
virtual KResult add_child(Inode&, const StringView& name, mode_t) = 0;
|
2019-06-09 11:25:19 +03:00
|
|
|
virtual KResult remove_child(const StringView& name) = 0;
|
2020-08-05 11:00:18 +03:00
|
|
|
virtual KResultOr<size_t> directory_entry_count() const = 0;
|
2019-05-16 04:02:37 +03:00
|
|
|
virtual KResult chmod(mode_t) = 0;
|
|
|
|
virtual KResult chown(uid_t, gid_t) = 0;
|
2020-02-08 14:07:04 +03:00
|
|
|
virtual KResult truncate(u64) { return KSuccess; }
|
2020-01-15 13:59:50 +03:00
|
|
|
virtual KResultOr<NonnullRefPtr<Custody>> resolve_as_link(Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0) const;
|
2019-05-16 04:02:37 +03:00
|
|
|
|
|
|
|
LocalSocket* socket() { return m_socket.ptr(); }
|
|
|
|
const LocalSocket* socket() const { return m_socket.ptr(); }
|
|
|
|
bool bind_socket(LocalSocket&);
|
|
|
|
bool unbind_socket();
|
|
|
|
|
2020-01-15 14:03:14 +03:00
|
|
|
virtual FileDescription* preopen_fd() { return nullptr; };
|
|
|
|
|
2019-05-16 04:02:37 +03:00
|
|
|
bool is_metadata_dirty() const { return m_metadata_dirty; }
|
|
|
|
|
|
|
|
virtual int set_atime(time_t);
|
|
|
|
virtual int set_ctime(time_t);
|
|
|
|
virtual int set_mtime(time_t);
|
2020-02-08 04:26:33 +03:00
|
|
|
virtual KResult increment_link_count();
|
|
|
|
virtual KResult decrement_link_count();
|
2019-05-16 04:02:37 +03:00
|
|
|
|
|
|
|
virtual void flush_metadata() = 0;
|
|
|
|
|
|
|
|
void will_be_destroyed();
|
|
|
|
|
2020-02-28 22:07:51 +03:00
|
|
|
void set_shared_vmobject(SharedInodeVMObject&);
|
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
|
|
|
RefPtr<SharedInodeVMObject> shared_vmobject() const;
|
|
|
|
bool is_shared_vmobject(const SharedInodeVMObject&) const;
|
2019-05-16 04:02:37 +03:00
|
|
|
|
2020-08-11 00:55:10 +03:00
|
|
|
static InlineLinkedList<Inode>& all_with_lock();
|
2019-05-16 04:02:37 +03:00
|
|
|
static void sync();
|
|
|
|
|
2019-11-04 18:31:46 +03:00
|
|
|
bool has_watchers() const { return !m_watchers.is_empty(); }
|
|
|
|
|
2019-07-22 21:01:11 +03:00
|
|
|
void register_watcher(Badge<InodeWatcher>, InodeWatcher&);
|
|
|
|
void unregister_watcher(Badge<InodeWatcher>, InodeWatcher&);
|
|
|
|
|
2020-12-31 04:10:31 +03:00
|
|
|
NonnullRefPtr<FIFO> fifo();
|
2020-07-17 00:23:03 +03:00
|
|
|
|
2019-08-08 12:08:27 +03:00
|
|
|
// For InlineLinkedListNode.
|
|
|
|
Inode* m_next { nullptr };
|
|
|
|
Inode* m_prev { nullptr };
|
|
|
|
|
2020-11-23 18:19:30 +03:00
|
|
|
static SpinLock<u32>& all_inodes_lock();
|
|
|
|
|
2019-05-16 04:02:37 +03:00
|
|
|
protected:
|
|
|
|
Inode(FS& fs, unsigned index);
|
2019-07-22 21:01:11 +03:00
|
|
|
void set_metadata_dirty(bool);
|
2020-09-12 06:11:07 +03:00
|
|
|
void inode_contents_changed(off_t, ssize_t, const UserOrKernelBuffer&);
|
2019-05-16 04:02:37 +03:00
|
|
|
void inode_size_changed(size_t old_size, size_t new_size);
|
2020-04-04 20:46:55 +03:00
|
|
|
KResult prepare_to_write_data();
|
2019-05-16 04:02:37 +03:00
|
|
|
|
2020-09-19 17:39:52 +03:00
|
|
|
void did_add_child(const InodeIdentifier&);
|
|
|
|
void did_remove_child(const InodeIdentifier&);
|
2020-07-04 14:36:55 +03:00
|
|
|
|
2019-05-16 04:02:37 +03:00
|
|
|
mutable Lock m_lock { "Inode" };
|
|
|
|
|
|
|
|
private:
|
|
|
|
FS& m_fs;
|
|
|
|
unsigned m_index { 0 };
|
2020-02-28 22:07:51 +03:00
|
|
|
WeakPtr<SharedInodeVMObject> m_shared_vmobject;
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<LocalSocket> m_socket;
|
2019-07-22 21:01:11 +03:00
|
|
|
HashTable<InodeWatcher*> m_watchers;
|
2019-05-16 04:02:37 +03:00
|
|
|
bool m_metadata_dirty { false };
|
2020-07-17 00:23:03 +03:00
|
|
|
RefPtr<FIFO> m_fifo;
|
2019-05-16 04:02:37 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|