FileSystem.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/HashMap.h>
  8. #include <AK/Singleton.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/StringView.h>
  11. #include <Kernel/FileSystem/FileSystem.h>
  12. #include <Kernel/FileSystem/Inode.h>
  13. #include <Kernel/Net/LocalSocket.h>
  14. #include <Kernel/VM/MemoryManager.h>
  15. #include <LibC/errno_numbers.h>
  16. namespace Kernel {
  17. static u32 s_lastFileSystemID;
  18. static AK::Singleton<HashMap<u32, FS*>> s_fs_map;
  19. static HashMap<u32, FS*>& all_fses()
  20. {
  21. return *s_fs_map;
  22. }
  23. FS::FS()
  24. : m_fsid(++s_lastFileSystemID)
  25. {
  26. all_fses().set(m_fsid, this);
  27. }
  28. FS::~FS()
  29. {
  30. all_fses().remove(m_fsid);
  31. }
  32. FS* FS::from_fsid(u32 id)
  33. {
  34. auto it = all_fses().find(id);
  35. if (it != all_fses().end())
  36. return (*it).value;
  37. return nullptr;
  38. }
  39. FS::DirectoryEntryView::DirectoryEntryView(const StringView& n, InodeIdentifier i, u8 ft)
  40. : name(n)
  41. , inode(i)
  42. , file_type(ft)
  43. {
  44. }
  45. void FS::sync()
  46. {
  47. Inode::sync();
  48. NonnullRefPtrVector<FS, 32> fses;
  49. {
  50. InterruptDisabler disabler;
  51. for (auto& it : all_fses())
  52. fses.append(*it.value);
  53. }
  54. for (auto& fs : fses)
  55. fs.flush_writes();
  56. }
  57. void FS::lock_all()
  58. {
  59. for (auto& it : all_fses()) {
  60. it.value->m_lock.lock();
  61. }
  62. }
  63. void FS::set_block_size(size_t block_size)
  64. {
  65. VERIFY(block_size > 0);
  66. if (block_size == m_block_size)
  67. return;
  68. m_block_size = block_size;
  69. }
  70. void FS::set_fragment_size(size_t fragment_size)
  71. {
  72. VERIFY(fragment_size > 0);
  73. if (fragment_size == m_fragment_size)
  74. return;
  75. m_fragment_size = fragment_size;
  76. }
  77. }