FileSystem.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/Arch/x86/InterruptDisabler.h>
  12. #include <Kernel/FileSystem/FileSystem.h>
  13. #include <Kernel/FileSystem/Inode.h>
  14. #include <Kernel/Net/LocalSocket.h>
  15. #include <Kernel/VM/MemoryManager.h>
  16. #include <LibC/errno_numbers.h>
  17. namespace Kernel {
  18. static u32 s_lastFileSystemID;
  19. static AK::Singleton<HashMap<u32, FS*>> s_fs_map;
  20. static HashMap<u32, FS*>& all_fses()
  21. {
  22. return *s_fs_map;
  23. }
  24. FS::FS()
  25. : m_fsid(++s_lastFileSystemID)
  26. {
  27. all_fses().set(m_fsid, this);
  28. }
  29. FS::~FS()
  30. {
  31. all_fses().remove(m_fsid);
  32. }
  33. FS* FS::from_fsid(u32 id)
  34. {
  35. auto it = all_fses().find(id);
  36. if (it != all_fses().end())
  37. return (*it).value;
  38. return nullptr;
  39. }
  40. FS::DirectoryEntryView::DirectoryEntryView(const StringView& n, InodeIdentifier i, u8 ft)
  41. : name(n)
  42. , inode(i)
  43. , file_type(ft)
  44. {
  45. }
  46. void FS::sync()
  47. {
  48. Inode::sync();
  49. NonnullRefPtrVector<FS, 32> fses;
  50. {
  51. InterruptDisabler disabler;
  52. for (auto& it : all_fses())
  53. fses.append(*it.value);
  54. }
  55. for (auto& fs : fses)
  56. fs.flush_writes();
  57. }
  58. void FS::lock_all()
  59. {
  60. for (auto& it : all_fses()) {
  61. it.value->m_lock.lock();
  62. }
  63. }
  64. }