FileSystem.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/Singleton.h>
  29. #include <AK/StringBuilder.h>
  30. #include <AK/StringView.h>
  31. #include <Kernel/FileSystem/FileSystem.h>
  32. #include <Kernel/FileSystem/Inode.h>
  33. #include <Kernel/Net/LocalSocket.h>
  34. #include <Kernel/VM/MemoryManager.h>
  35. #include <LibC/errno_numbers.h>
  36. namespace Kernel {
  37. static u32 s_lastFileSystemID;
  38. static AK::Singleton<HashMap<u32, FS*>> s_fs_map;
  39. static HashMap<u32, FS*>& all_fses()
  40. {
  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::DirectoryEntryView::DirectoryEntryView(const StringView& n, InodeIdentifier i, u8 ft)
  60. : name(n)
  61. , inode(i)
  62. , file_type(ft)
  63. {
  64. }
  65. void FS::sync()
  66. {
  67. Inode::sync();
  68. NonnullRefPtrVector<FS, 32> fses;
  69. {
  70. InterruptDisabler disabler;
  71. for (auto& it : all_fses())
  72. fses.append(*it.value);
  73. }
  74. for (auto& fs : fses)
  75. fs.flush_writes();
  76. }
  77. void FS::lock_all()
  78. {
  79. for (auto& it : all_fses()) {
  80. it.value->m_lock.lock();
  81. }
  82. }
  83. void FS::set_block_size(size_t block_size)
  84. {
  85. ASSERT(block_size > 0);
  86. if (block_size == m_block_size)
  87. return;
  88. m_block_size = block_size;
  89. }
  90. }