FileSystem.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <AK/Assertions.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/StringBuilder.h>
  29. #include <AK/StringView.h>
  30. #include <Kernel/FileSystem/FileSystem.h>
  31. #include <Kernel/FileSystem/Inode.h>
  32. #include <Kernel/Net/LocalSocket.h>
  33. #include <Kernel/VM/MemoryManager.h>
  34. #include <LibC/errno_numbers.h>
  35. namespace Kernel {
  36. static u32 s_lastFileSystemID;
  37. static HashMap<u32, FS*>* s_fs_map;
  38. static HashMap<u32, FS*>& all_fses()
  39. {
  40. if (!s_fs_map)
  41. s_fs_map = new HashMap<u32, FS*>();
  42. return *s_fs_map;
  43. }
  44. FS::FS()
  45. : m_fsid(++s_lastFileSystemID)
  46. {
  47. all_fses().set(m_fsid, this);
  48. }
  49. FS::~FS()
  50. {
  51. all_fses().remove(m_fsid);
  52. }
  53. FS* FS::from_fsid(u32 id)
  54. {
  55. auto it = all_fses().find(id);
  56. if (it != all_fses().end())
  57. return (*it).value;
  58. return nullptr;
  59. }
  60. FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, u8 ft)
  61. : name_length(strlen(n))
  62. , inode(i)
  63. , file_type(ft)
  64. {
  65. ASSERT(name_length < (int)sizeof(name));
  66. memcpy(name, n, name_length);
  67. name[name_length] = '\0';
  68. }
  69. FS::DirectoryEntry::DirectoryEntry(const char* n, size_t nl, InodeIdentifier i, u8 ft)
  70. : name_length(nl)
  71. , inode(i)
  72. , file_type(ft)
  73. {
  74. ASSERT(name_length < (int)sizeof(name));
  75. memcpy(name, n, nl);
  76. name[nl] = '\0';
  77. }
  78. void FS::sync()
  79. {
  80. Inode::sync();
  81. NonnullRefPtrVector<FS, 32> fses;
  82. {
  83. InterruptDisabler disabler;
  84. for (auto& it : all_fses())
  85. fses.append(*it.value);
  86. }
  87. for (auto& fs : fses)
  88. fs.flush_writes();
  89. }
  90. void FS::lock_all()
  91. {
  92. for (auto& it : all_fses()) {
  93. it.value->m_lock.lock();
  94. }
  95. }
  96. void FS::set_block_size(size_t block_size)
  97. {
  98. ASSERT(block_size > 0);
  99. if (block_size == m_block_size)
  100. return;
  101. m_block_size = block_size;
  102. }
  103. }