FileSystem.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/AtomicRefCounted.h>
  8. #include <AK/Error.h>
  9. #include <AK/StringView.h>
  10. #include <Kernel/FileSystem/InodeIdentifier.h>
  11. #include <Kernel/Forward.h>
  12. #include <Kernel/Library/LockRefPtr.h>
  13. #include <Kernel/Locking/Mutex.h>
  14. #include <Kernel/UnixTypes.h>
  15. #include <Kernel/UserOrKernelBuffer.h>
  16. namespace Kernel {
  17. class FileSystem : public AtomicRefCounted<FileSystem> {
  18. friend class Inode;
  19. friend class VirtualFileSystem;
  20. public:
  21. virtual ~FileSystem();
  22. FileSystemID fsid() const { return m_fsid; }
  23. static void sync();
  24. static void lock_all();
  25. virtual ErrorOr<void> initialize() = 0;
  26. virtual StringView class_name() const = 0;
  27. virtual Inode& root_inode() = 0;
  28. virtual bool supports_watchers() const { return false; }
  29. bool is_readonly() const { return m_readonly; }
  30. virtual unsigned total_block_count() const { return 0; }
  31. virtual unsigned free_block_count() const { return 0; }
  32. virtual unsigned total_inode_count() const { return 0; }
  33. virtual unsigned free_inode_count() const { return 0; }
  34. ErrorOr<void> prepare_to_unmount();
  35. struct DirectoryEntryView {
  36. DirectoryEntryView(StringView name, InodeIdentifier, u8 file_type);
  37. StringView name;
  38. InodeIdentifier inode;
  39. u8 file_type { 0 };
  40. };
  41. virtual void flush_writes() { }
  42. u64 block_size() const { return m_block_size; }
  43. size_t fragment_size() const { return m_fragment_size; }
  44. virtual bool is_file_backed() const { return false; }
  45. // Converts file types that are used internally by the filesystem to DT_* types
  46. virtual u8 internal_file_type_to_directory_entry_type(DirectoryEntryView const& entry) const { return entry.file_type; }
  47. SpinlockProtected<size_t>& mounted_count(Badge<VirtualFileSystem>) { return m_attach_count; }
  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. virtual ErrorOr<void> prepare_to_clear_last_mount() { return {}; }
  53. mutable Mutex m_lock { "FS"sv };
  54. private:
  55. FileSystemID m_fsid;
  56. u64 m_block_size { 0 };
  57. size_t m_fragment_size { 0 };
  58. bool m_readonly { false };
  59. SpinlockProtected<size_t> m_attach_count { LockRank::FileSystem, 0 };
  60. IntrusiveListNode<FileSystem> m_file_system_node;
  61. };
  62. }
  63. namespace AK {
  64. template<>
  65. struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> {
  66. static unsigned hash(Kernel::InodeIdentifier const& inode) { return pair_int_hash(inode.fsid().value(), inode.index().value()); }
  67. };
  68. }