FileSystem.h 2.6 KB

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