FileSystem.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }
  67. void FS::set_block_size(int block_size)
  68. {
  69. ASSERT(block_size > 0);
  70. if (block_size == m_block_size)
  71. return;
  72. m_block_size = block_size;
  73. }