Serenity Operating System
at hosted 129 lines 4.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/RefCounted.h> 30#include <AK/RefPtr.h> 31#include <AK/String.h> 32#include <Kernel/FileSystem/InodeIdentifier.h> 33#include <Kernel/KResult.h> 34#include <Kernel/Lock.h> 35#include <Kernel/UnixTypes.h> 36 37namespace Kernel { 38 39static const u32 mepoch = 476763780; 40 41class Inode; 42class FileDescription; 43class LocalSocket; 44class VMObject; 45 46class FS : public RefCounted<FS> { 47 friend class Inode; 48 49public: 50 virtual ~FS(); 51 52 unsigned fsid() const { return m_fsid; } 53 static FS* from_fsid(u32); 54 static void sync(); 55 static void lock_all(); 56 57 virtual bool initialize() = 0; 58 virtual const char* class_name() const = 0; 59 virtual InodeIdentifier root_inode() const = 0; 60 virtual bool supports_watchers() const { return false; } 61 62 bool is_readonly() const { return m_readonly; } 63 64 virtual unsigned total_block_count() const { return 0; } 65 virtual unsigned free_block_count() const { return 0; } 66 virtual unsigned total_inode_count() const { return 0; } 67 virtual unsigned free_inode_count() const { return 0; } 68 69 virtual KResult prepare_to_unmount() const { return KSuccess; } 70 71 // FIXME: This data structure is very clunky and unpleasant. Replace it with something nicer. 72 struct DirectoryEntry { 73 DirectoryEntry(const char* name, InodeIdentifier, u8 file_type); 74 DirectoryEntry(const char* name, size_t name_length, InodeIdentifier, u8 file_type); 75 char name[256]; 76 size_t name_length { 0 }; 77 InodeIdentifier inode; 78 u8 file_type { 0 }; 79 }; 80 81 virtual KResultOr<NonnullRefPtr<Inode>> create_inode(InodeIdentifier parent_id, const String& name, mode_t, off_t size, dev_t, uid_t, gid_t) = 0; 82 virtual KResult create_directory(InodeIdentifier parent_inode, const String& name, mode_t, uid_t, gid_t) = 0; 83 84 virtual RefPtr<Inode> get_inode(InodeIdentifier) const = 0; 85 86 virtual void flush_writes() {} 87 88 int block_size() const { return m_block_size; } 89 90 virtual bool is_file_backed() const { return false; } 91 92protected: 93 FS(); 94 95 void set_block_size(int); 96 97 mutable Lock m_lock { "FS" }; 98 99private: 100 unsigned m_fsid { 0 }; 101 int m_block_size { 0 }; 102 bool m_readonly { false }; 103}; 104 105inline FS* InodeIdentifier::fs() 106{ 107 return FS::from_fsid(m_fsid); 108} 109 110inline const FS* InodeIdentifier::fs() const 111{ 112 return FS::from_fsid(m_fsid); 113} 114 115inline bool InodeIdentifier::is_root_inode() const 116{ 117 return (*this) == fs()->root_inode(); 118} 119 120} 121 122namespace AK { 123 124template<> 125struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> { 126 static unsigned hash(const Kernel::InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index()); } 127}; 128 129}