FileSystem.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashMap.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/StringView.h>
  9. #include <Kernel/Arch/x86/InterruptDisabler.h>
  10. #include <Kernel/FileSystem/FileSystem.h>
  11. #include <Kernel/FileSystem/Inode.h>
  12. #include <Kernel/Memory/MemoryManager.h>
  13. #include <Kernel/Net/LocalSocket.h>
  14. namespace Kernel {
  15. static u32 s_lastFileSystemID;
  16. static Singleton<HashMap<u32, FileSystem*>> s_file_system_map;
  17. static HashMap<u32, FileSystem*>& all_file_systems()
  18. {
  19. return *s_file_system_map;
  20. }
  21. FileSystem::FileSystem()
  22. : m_fsid(++s_lastFileSystemID)
  23. {
  24. s_file_system_map->set(m_fsid, this);
  25. }
  26. FileSystem::~FileSystem()
  27. {
  28. s_file_system_map->remove(m_fsid);
  29. }
  30. FileSystem* FileSystem::from_fsid(u32 id)
  31. {
  32. auto it = all_file_systems().find(id);
  33. if (it != all_file_systems().end())
  34. return (*it).value;
  35. return nullptr;
  36. }
  37. FileSystem::DirectoryEntryView::DirectoryEntryView(const StringView& n, InodeIdentifier i, u8 ft)
  38. : name(n)
  39. , inode(i)
  40. , file_type(ft)
  41. {
  42. }
  43. void FileSystem::sync()
  44. {
  45. Inode::sync();
  46. NonnullRefPtrVector<FileSystem, 32> file_systems;
  47. {
  48. InterruptDisabler disabler;
  49. for (auto& it : all_file_systems())
  50. file_systems.append(*it.value);
  51. }
  52. for (auto& fs : file_systems)
  53. fs.flush_writes();
  54. }
  55. void FileSystem::lock_all()
  56. {
  57. for (auto& it : all_file_systems()) {
  58. it.value->m_lock.lock();
  59. }
  60. }
  61. }