FileSystem.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <AK/Assertions.h>
  2. #include <AK/HashMap.h>
  3. #include <AK/StringBuilder.h>
  4. #include <Kernel/FileSystem/FileSystem.h>
  5. #include <Kernel/FileSystem/Inode.h>
  6. #include <Kernel/Net/LocalSocket.h>
  7. #include <Kernel/VM/MemoryManager.h>
  8. #include <LibC/errno_numbers.h>
  9. static u32 s_lastFileSystemID;
  10. static HashMap<u32, FS*>* s_fs_map;
  11. static HashMap<u32, FS*>& all_fses()
  12. {
  13. if (!s_fs_map)
  14. s_fs_map = new HashMap<u32, FS*>();
  15. return *s_fs_map;
  16. }
  17. FS::FS()
  18. : m_fsid(++s_lastFileSystemID)
  19. {
  20. all_fses().set(m_fsid, this);
  21. }
  22. FS::~FS()
  23. {
  24. all_fses().remove(m_fsid);
  25. }
  26. FS* FS::from_fsid(u32 id)
  27. {
  28. auto it = all_fses().find(id);
  29. if (it != all_fses().end())
  30. return (*it).value;
  31. return nullptr;
  32. }
  33. FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, u8 ft)
  34. : name_length(strlen(n))
  35. , inode(i)
  36. , file_type(ft)
  37. {
  38. memcpy(name, n, name_length);
  39. name[name_length] = '\0';
  40. }
  41. FS::DirectoryEntry::DirectoryEntry(const char* n, int nl, InodeIdentifier i, u8 ft)
  42. : name_length(nl)
  43. , inode(i)
  44. , file_type(ft)
  45. {
  46. memcpy(name, n, nl);
  47. name[nl] = '\0';
  48. }
  49. void FS::sync()
  50. {
  51. Inode::sync();
  52. NonnullRefPtrVector<FS, 32> fses;
  53. {
  54. InterruptDisabler disabler;
  55. for (auto& it : all_fses())
  56. fses.append(*it.value);
  57. }
  58. for (auto& fs : fses)
  59. fs.flush_writes();
  60. }
  61. void FS::lock_all()
  62. {
  63. for (auto& it : all_fses()) {
  64. it.value->m_lock.lock();
  65. }
  66. }