FileSystem.h 4.3 KB

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