FileSystem.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <AK/RefPtr.h>
  9. #include <AK/StringView.h>
  10. #include <Kernel/FileSystem/InodeIdentifier.h>
  11. #include <Kernel/Forward.h>
  12. #include <Kernel/KResult.h>
  13. #include <Kernel/Locking/Mutex.h>
  14. #include <Kernel/UnixTypes.h>
  15. #include <Kernel/UserOrKernelBuffer.h>
  16. namespace Kernel {
  17. static constexpr u32 mepoch = 476763780;
  18. class FileSystem : public RefCounted<FileSystem> {
  19. friend class Inode;
  20. public:
  21. virtual ~FileSystem();
  22. unsigned fsid() const { return m_fsid; }
  23. static FileSystem* from_fsid(u32);
  24. static void sync();
  25. static void lock_all();
  26. virtual bool initialize() = 0;
  27. virtual StringView class_name() const = 0;
  28. virtual Inode& root_inode() = 0;
  29. virtual bool supports_watchers() const { return false; }
  30. bool is_readonly() const { return m_readonly; }
  31. virtual unsigned total_block_count() const { return 0; }
  32. virtual unsigned free_block_count() const { return 0; }
  33. virtual unsigned total_inode_count() const { return 0; }
  34. virtual unsigned free_inode_count() const { return 0; }
  35. virtual KResult prepare_to_unmount() { return KSuccess; }
  36. struct DirectoryEntryView {
  37. DirectoryEntryView(const StringView& name, InodeIdentifier, u8 file_type);
  38. StringView name;
  39. InodeIdentifier inode;
  40. u8 file_type { 0 };
  41. };
  42. virtual void flush_writes() { }
  43. u64 block_size() const { return m_block_size; }
  44. size_t fragment_size() const { return m_fragment_size; }
  45. virtual bool is_file_backed() const { return false; }
  46. // Converts file types that are used internally by the filesystem to DT_* types
  47. virtual u8 internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const { return entry.file_type; }
  48. protected:
  49. FileSystem();
  50. void set_block_size(u64 size) { m_block_size = size; }
  51. void set_fragment_size(size_t size) { m_fragment_size = size; }
  52. mutable Mutex m_lock { "FS" };
  53. private:
  54. unsigned m_fsid { 0 };
  55. u64 m_block_size { 0 };
  56. size_t m_fragment_size { 0 };
  57. bool m_readonly { false };
  58. };
  59. inline FileSystem* InodeIdentifier::fs()
  60. {
  61. return FileSystem::from_fsid(m_fsid);
  62. }
  63. inline const FileSystem* InodeIdentifier::fs() const
  64. {
  65. return FileSystem::from_fsid(m_fsid);
  66. }
  67. }
  68. namespace AK {
  69. template<>
  70. struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> {
  71. static unsigned hash(const Kernel::InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index().value()); }
  72. };
  73. }