FileSystem.cpp 3.1 KB

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