FileSystem.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #pragma once
  27. #include <AK/RefCounted.h>
  28. #include <AK/RefPtr.h>
  29. #include <AK/String.h>
  30. #include <AK/StringView.h>
  31. #include <Kernel/FileSystem/InodeIdentifier.h>
  32. #include <Kernel/KResult.h>
  33. #include <Kernel/Lock.h>
  34. #include <Kernel/UnixTypes.h>
  35. #include <Kernel/UserOrKernelBuffer.h>
  36. namespace Kernel {
  37. static const u32 mepoch = 476763780;
  38. class Inode;
  39. class FileDescription;
  40. class LocalSocket;
  41. class VMObject;
  42. class FS : public RefCounted<FS> {
  43. friend class Inode;
  44. public:
  45. virtual ~FS();
  46. unsigned fsid() const { return m_fsid; }
  47. static FS* from_fsid(u32);
  48. static void sync();
  49. static void lock_all();
  50. virtual bool initialize() = 0;
  51. virtual const char* class_name() const = 0;
  52. virtual NonnullRefPtr<Inode> root_inode() const = 0;
  53. virtual bool supports_watchers() const { return false; }
  54. bool is_readonly() const { return m_readonly; }
  55. virtual unsigned total_block_count() const { return 0; }
  56. virtual unsigned free_block_count() const { return 0; }
  57. virtual unsigned total_inode_count() const { return 0; }
  58. virtual unsigned free_inode_count() const { return 0; }
  59. virtual KResult prepare_to_unmount() const { return KSuccess; }
  60. struct DirectoryEntryView {
  61. DirectoryEntryView(const StringView& name, InodeIdentifier, u8 file_type);
  62. StringView name;
  63. InodeIdentifier inode;
  64. u8 file_type { 0 };
  65. };
  66. virtual void flush_writes() {}
  67. size_t block_size() const { return m_block_size; }
  68. virtual bool is_file_backed() const { return false; }
  69. // Converts file types that are used internally by the filesystem to DT_* types
  70. virtual u8 internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const { return entry.file_type; }
  71. protected:
  72. FS();
  73. void set_block_size(size_t);
  74. mutable Lock m_lock { "FS" };
  75. private:
  76. unsigned m_fsid { 0 };
  77. size_t m_block_size { 0 };
  78. bool m_readonly { false };
  79. };
  80. inline FS* InodeIdentifier::fs()
  81. {
  82. return FS::from_fsid(m_fsid);
  83. }
  84. inline const FS* InodeIdentifier::fs() const
  85. {
  86. return FS::from_fsid(m_fsid);
  87. }
  88. }
  89. namespace AK {
  90. template<>
  91. struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> {
  92. static unsigned hash(const Kernel::InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index()); }
  93. };
  94. }