FileSystem.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/KResult.h>
  12. #include <Kernel/Lock.h>
  13. #include <Kernel/UnixTypes.h>
  14. #include <Kernel/UserOrKernelBuffer.h>
  15. namespace Kernel {
  16. static constexpr u32 mepoch = 476763780;
  17. class Inode;
  18. class FileDescription;
  19. class LocalSocket;
  20. class VMObject;
  21. class FileSystem : public RefCounted<FileSystem> {
  22. friend class Inode;
  23. public:
  24. virtual ~FileSystem();
  25. unsigned fsid() const { return m_fsid; }
  26. static FileSystem* from_fsid(u32);
  27. static void sync();
  28. static void lock_all();
  29. virtual bool initialize() = 0;
  30. virtual const char* class_name() const = 0;
  31. virtual NonnullRefPtr<Inode> root_inode() const = 0;
  32. virtual bool supports_watchers() const { return false; }
  33. bool is_readonly() const { return m_readonly; }
  34. virtual unsigned total_block_count() const { return 0; }
  35. virtual unsigned free_block_count() const { return 0; }
  36. virtual unsigned total_inode_count() const { return 0; }
  37. virtual unsigned free_inode_count() const { return 0; }
  38. virtual KResult prepare_to_unmount() const { return KSuccess; }
  39. struct DirectoryEntryView {
  40. DirectoryEntryView(const StringView& name, InodeIdentifier, u8 file_type);
  41. StringView name;
  42. InodeIdentifier inode;
  43. u8 file_type { 0 };
  44. };
  45. virtual void flush_writes() { }
  46. u64 block_size() const { return m_block_size; }
  47. size_t fragment_size() const { return m_fragment_size; }
  48. virtual bool is_file_backed() const { return false; }
  49. // Converts file types that are used internally by the filesystem to DT_* types
  50. virtual u8 internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const { return entry.file_type; }
  51. protected:
  52. FileSystem();
  53. void set_block_size(u64 size) { m_block_size = size; }
  54. void set_fragment_size(size_t size) { m_fragment_size = size; }
  55. mutable Lock m_lock { "FS" };
  56. private:
  57. unsigned m_fsid { 0 };
  58. u64 m_block_size { 0 };
  59. size_t m_fragment_size { 0 };
  60. bool m_readonly { false };
  61. };
  62. inline FileSystem* InodeIdentifier::fs()
  63. {
  64. return FileSystem::from_fsid(m_fsid);
  65. }
  66. inline const FileSystem* InodeIdentifier::fs() const
  67. {
  68. return FileSystem::from_fsid(m_fsid);
  69. }
  70. }
  71. namespace AK {
  72. template<>
  73. struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> {
  74. static unsigned hash(const Kernel::InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index().value()); }
  75. };
  76. }