FileSystem.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. ASSERT(name_length < (int)sizeof(name));
  39. memcpy(name, n, name_length);
  40. name[name_length] = '\0';
  41. }
  42. FS::DirectoryEntry::DirectoryEntry(const char* n, int nl, InodeIdentifier i, u8 ft)
  43. : name_length(nl)
  44. , inode(i)
  45. , file_type(ft)
  46. {
  47. ASSERT(name_length < (int)sizeof(name));
  48. memcpy(name, n, nl);
  49. name[nl] = '\0';
  50. }
  51. void FS::sync()
  52. {
  53. Inode::sync();
  54. NonnullRefPtrVector<FS, 32> fses;
  55. {
  56. InterruptDisabler disabler;
  57. for (auto& it : all_fses())
  58. fses.append(*it.value);
  59. }
  60. for (auto& fs : fses)
  61. fs.flush_writes();
  62. }
  63. void FS::lock_all()
  64. {
  65. for (auto& it : all_fses()) {
  66. it.value->m_lock.lock();
  67. }
  68. }
  69. void FS::set_block_size(int block_size)
  70. {
  71. ASSERT(block_size > 0);
  72. if (block_size == m_block_size)
  73. return;
  74. m_block_size = block_size;
  75. }