FileSystem.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <Kernel/FileSystem/InodeIdentifier.h>
  31. #include <Kernel/KResult.h>
  32. #include <Kernel/Lock.h>
  33. #include <Kernel/UnixTypes.h>
  34. namespace Kernel {
  35. static const u32 mepoch = 476763780;
  36. class Inode;
  37. class FileDescription;
  38. class LocalSocket;
  39. class VMObject;
  40. class FS : public RefCounted<FS> {
  41. friend class Inode;
  42. public:
  43. virtual ~FS();
  44. unsigned fsid() const { return m_fsid; }
  45. static FS* from_fsid(u32);
  46. static void sync();
  47. static void lock_all();
  48. virtual bool initialize() = 0;
  49. virtual const char* class_name() const = 0;
  50. virtual InodeIdentifier root_inode() const = 0;
  51. virtual bool supports_watchers() const { return false; }
  52. bool is_readonly() const { return m_readonly; }
  53. virtual unsigned total_block_count() const { return 0; }
  54. virtual unsigned free_block_count() const { return 0; }
  55. virtual unsigned total_inode_count() const { return 0; }
  56. virtual unsigned free_inode_count() const { return 0; }
  57. virtual KResult prepare_to_unmount() const { return KSuccess; }
  58. // FIXME: This data structure is very clunky and unpleasant. Replace it with something nicer.
  59. struct DirectoryEntry {
  60. DirectoryEntry(const char* name, InodeIdentifier, u8 file_type);
  61. DirectoryEntry(const char* name, size_t name_length, InodeIdentifier, u8 file_type);
  62. char name[256];
  63. size_t name_length { 0 };
  64. InodeIdentifier inode;
  65. u8 file_type { 0 };
  66. };
  67. virtual KResultOr<NonnullRefPtr<Inode>> create_inode(InodeIdentifier parent_id, const String& name, mode_t, off_t size, dev_t, uid_t, gid_t) = 0;
  68. virtual KResult create_directory(InodeIdentifier parent_inode, const String& name, mode_t, uid_t, gid_t) = 0;
  69. virtual RefPtr<Inode> get_inode(InodeIdentifier) const = 0;
  70. virtual void flush_writes() {}
  71. int block_size() const { return m_block_size; }
  72. virtual bool is_file_backed() const { return false; }
  73. protected:
  74. FS();
  75. void set_block_size(int);
  76. mutable Lock m_lock { "FS" };
  77. private:
  78. unsigned m_fsid { 0 };
  79. int m_block_size { 0 };
  80. bool m_readonly { false };
  81. };
  82. inline FS* InodeIdentifier::fs()
  83. {
  84. return FS::from_fsid(m_fsid);
  85. }
  86. inline const FS* InodeIdentifier::fs() const
  87. {
  88. return FS::from_fsid(m_fsid);
  89. }
  90. inline bool InodeIdentifier::is_root_inode() const
  91. {
  92. return (*this) == fs()->root_inode();
  93. }
  94. }
  95. namespace AK {
  96. template<>
  97. struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> {
  98. static unsigned hash(const Kernel::InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index()); }
  99. };
  100. }