VirtualFileSystem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include "VirtualFileSystem.h"
  2. #include "FileHandle.h"
  3. #include "FileSystem.h"
  4. #include <AK/kmalloc.h>
  5. #include <AK/kstdio.h>
  6. #include <AK/ktime.h>
  7. //#define VFS_DEBUG
  8. static dword encodedDevice(unsigned major, unsigned minor)
  9. {
  10. return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
  11. }
  12. static VirtualFileSystem* s_the;
  13. VirtualFileSystem& VirtualFileSystem::the()
  14. {
  15. ASSERT(s_the);
  16. return *s_the;
  17. }
  18. VirtualFileSystem::VirtualFileSystem()
  19. {
  20. s_the = this;
  21. m_maxNodeCount = 16;
  22. m_nodes = reinterpret_cast<Node*>(kmalloc(sizeof(Node) * maxNodeCount()));
  23. memset(m_nodes, 0, sizeof(Node) * maxNodeCount());
  24. for (unsigned i = 0; i < m_maxNodeCount; ++i)
  25. m_nodeFreeList.append(&m_nodes[i]);
  26. }
  27. VirtualFileSystem::~VirtualFileSystem()
  28. {
  29. kprintf("[VFS] ~VirtualFileSystem with %u nodes allocated\n", allocatedNodeCount());
  30. }
  31. auto VirtualFileSystem::makeNode(InodeIdentifier inode) -> RetainPtr<Node>
  32. {
  33. auto metadata = inode.metadata();
  34. if (!metadata.isValid())
  35. return nullptr;
  36. CharacterDevice* characterDevice = nullptr;
  37. if (metadata.isCharacterDevice()) {
  38. auto it = m_characterDevices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
  39. if (it != m_characterDevices.end()) {
  40. characterDevice = (*it).value;
  41. } else {
  42. kprintf("[VFS] makeNode() no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
  43. return nullptr;
  44. }
  45. }
  46. auto vnode = allocateNode();
  47. ASSERT(vnode);
  48. FileSystem* fileSystem = inode.fileSystem();
  49. fileSystem->retain();
  50. vnode->inode = inode;
  51. #ifdef VFS_DEBUG
  52. kprintf("makeNode: inode=%u, size=%u, mode=%o, uid=%u, gid=%u\n", inode.index(), metadata.size, metadata.mode, metadata.uid, metadata.gid);
  53. #endif
  54. m_inode2vnode.set(inode, vnode.ptr());
  55. vnode->m_characterDevice = characterDevice;
  56. return vnode;
  57. }
  58. auto VirtualFileSystem::getOrCreateNode(InodeIdentifier inode) -> RetainPtr<Node>
  59. {
  60. auto it = m_inode2vnode.find(inode);
  61. if (it != m_inode2vnode.end())
  62. return (*it).value;
  63. return makeNode(inode);
  64. }
  65. bool VirtualFileSystem::mount(RetainPtr<FileSystem>&& fileSystem, const String& path)
  66. {
  67. ASSERT(fileSystem);
  68. auto inode = resolvePath(path);
  69. if (!inode.isValid()) {
  70. kprintf("[VFS] mount can't resolve mount point '%s'\n", path.characters());
  71. return false;
  72. }
  73. kprintf("mounting %s{%p} at %s (inode: %u)\n", fileSystem->className(), fileSystem.ptr(), path.characters(), inode.index());
  74. // FIXME: check that this is not already a mount point
  75. auto mount = make<Mount>(inode, move(fileSystem));
  76. m_mounts.append(move(mount));
  77. return true;
  78. }
  79. bool VirtualFileSystem::mountRoot(RetainPtr<FileSystem>&& fileSystem)
  80. {
  81. if (m_rootNode) {
  82. kprintf("[VFS] mountRoot can't mount another root\n");
  83. return false;
  84. }
  85. auto mount = make<Mount>(InodeIdentifier(), move(fileSystem));
  86. auto node = makeNode(mount->guest());
  87. if (!node->inUse()) {
  88. kprintf("[VFS] root inode for / is not in use :(\n");
  89. return false;
  90. }
  91. if (!node->inode.metadata().isDirectory()) {
  92. kprintf("[VFS] root inode for / is not in use :(\n");
  93. return false;
  94. }
  95. m_rootNode = move(node);
  96. kprintf("[VFS] mountRoot mounted %s{%p}\n",
  97. m_rootNode->fileSystem()->className(),
  98. m_rootNode->fileSystem());
  99. m_mounts.append(move(mount));
  100. return true;
  101. }
  102. auto VirtualFileSystem::allocateNode() -> RetainPtr<Node>
  103. {
  104. if (m_nodeFreeList.isEmpty()) {
  105. kprintf("[VFS] allocateNode has no nodes left\n");
  106. return nullptr;
  107. }
  108. auto* node = m_nodeFreeList.takeLast();
  109. ASSERT(node->retainCount == 0);
  110. node->retainCount = 1;
  111. node->vfs = this;
  112. return adopt(*node);
  113. }
  114. void VirtualFileSystem::freeNode(Node* node)
  115. {
  116. ASSERT(node);
  117. ASSERT(node->inUse());
  118. m_inode2vnode.remove(node->inode);
  119. node->inode.fileSystem()->release();
  120. node->inode = InodeIdentifier();
  121. node->m_characterDevice = nullptr;
  122. m_nodeFreeList.append(move(node));
  123. }
  124. bool VirtualFileSystem::isDirectory(const String& path)
  125. {
  126. auto inode = resolvePath(path);
  127. if (!inode.isValid())
  128. return false;
  129. return inode.metadata().isDirectory();
  130. }
  131. auto VirtualFileSystem::findMountForHost(InodeIdentifier inode) -> Mount*
  132. {
  133. for (auto& mount : m_mounts) {
  134. if (mount->host() == inode)
  135. return mount.ptr();
  136. }
  137. return nullptr;
  138. }
  139. auto VirtualFileSystem::findMountForGuest(InodeIdentifier inode) -> Mount*
  140. {
  141. for (auto& mount : m_mounts) {
  142. if (mount->guest() == inode)
  143. return mount.ptr();
  144. }
  145. return nullptr;
  146. }
  147. bool VirtualFileSystem::isRoot(InodeIdentifier inode) const
  148. {
  149. return inode == m_rootNode->inode;
  150. }
  151. template<typename F>
  152. void VirtualFileSystem::enumerateDirectoryInode(InodeIdentifier directoryInode, F func)
  153. {
  154. if (!directoryInode.isValid())
  155. return;
  156. directoryInode.fileSystem()->enumerateDirectoryInode(directoryInode, [&] (const FileSystem::DirectoryEntry& entry) {
  157. InodeIdentifier resolvedInode;
  158. if (auto mount = findMountForHost(entry.inode))
  159. resolvedInode = mount->guest();
  160. else
  161. resolvedInode = entry.inode;
  162. if (directoryInode.isRootInode() && !isRoot(directoryInode) && entry.name == "..") {
  163. auto mount = findMountForGuest(entry.inode);
  164. ASSERT(mount);
  165. resolvedInode = mount->host();
  166. }
  167. func({ entry.name, resolvedInode });
  168. return true;
  169. });
  170. }
  171. void VirtualFileSystem::listDirectory(const String& path)
  172. {
  173. auto directoryInode = resolvePath(path);
  174. if (!directoryInode.isValid())
  175. return;
  176. kprintf("[VFS] ls %s -> %s %02u:%08u\n", path.characters(), directoryInode.fileSystem()->className(), directoryInode.fileSystemID(), directoryInode.index());
  177. enumerateDirectoryInode(directoryInode, [&] (const FileSystem::DirectoryEntry& entry) {
  178. const char* nameColorBegin = "";
  179. const char* nameColorEnd = "";
  180. auto metadata = entry.inode.metadata();
  181. ASSERT(metadata.isValid());
  182. if (metadata.isDirectory()) {
  183. nameColorBegin = "\033[34;1m";
  184. nameColorEnd = "\033[0m";
  185. } else if (metadata.isSymbolicLink()) {
  186. nameColorBegin = "\033[36;1m";
  187. nameColorEnd = "\033[0m";
  188. }
  189. if (metadata.isSticky()) {
  190. nameColorBegin = "\033[42;30m";
  191. nameColorEnd = "\033[0m";
  192. }
  193. if (metadata.isCharacterDevice() || metadata.isBlockDevice()) {
  194. nameColorBegin = "\033[33;1m";
  195. nameColorEnd = "\033[0m";
  196. }
  197. kprintf("%02u:%08u ",
  198. metadata.inode.fileSystemID(),
  199. metadata.inode.index());
  200. if (metadata.isDirectory())
  201. kprintf("d");
  202. else if (metadata.isSymbolicLink())
  203. kprintf("l");
  204. else if (metadata.isBlockDevice())
  205. kprintf("b");
  206. else if (metadata.isCharacterDevice())
  207. kprintf("c");
  208. else if (metadata.isSocket())
  209. kprintf("s");
  210. else if (metadata.isFIFO())
  211. kprintf("f");
  212. else if (metadata.isRegularFile())
  213. kprintf("-");
  214. else
  215. kprintf("?");
  216. kprintf("%c%c%c%c%c%c%c%c",
  217. metadata.mode & 00400 ? 'r' : '-',
  218. metadata.mode & 00200 ? 'w' : '-',
  219. metadata.mode & 00100 ? 'x' : '-',
  220. metadata.mode & 00040 ? 'r' : '-',
  221. metadata.mode & 00020 ? 'w' : '-',
  222. metadata.mode & 00010 ? 'x' : '-',
  223. metadata.mode & 00004 ? 'r' : '-',
  224. metadata.mode & 00002 ? 'w' : '-'
  225. );
  226. if (metadata.isSticky())
  227. kprintf("t");
  228. else
  229. kprintf("%c", metadata.mode & 00001 ? 'x' : '-');
  230. if (metadata.isCharacterDevice() || metadata.isBlockDevice()) {
  231. char buf[16];
  232. ksprintf(buf, "%u, %u", metadata.majorDevice, metadata.minorDevice);
  233. kprintf("%12s ", buf);
  234. } else {
  235. kprintf("%12lld ", metadata.size);
  236. }
  237. kprintf("\033[30;1m");
  238. time_t mtime = metadata.mtime;
  239. auto tm = *klocaltime(&mtime);
  240. kprintf("%04u-%02u-%02u %02u:%02u:%02u ",
  241. tm.tm_year + 1900,
  242. tm.tm_mon + 1,
  243. tm.tm_mday,
  244. tm.tm_hour,
  245. tm.tm_min,
  246. tm.tm_sec);
  247. kprintf("\033[0m");
  248. kprintf("%s%s%s",
  249. nameColorBegin,
  250. entry.name.characters(),
  251. nameColorEnd);
  252. if (metadata.isDirectory()) {
  253. kprintf("/");
  254. } else if (metadata.isSymbolicLink()) {
  255. auto symlinkContents = directoryInode.fileSystem()->readEntireInode(metadata.inode);
  256. kprintf(" -> %s", String((const char*)symlinkContents.pointer(), symlinkContents.size()).characters());
  257. }
  258. kprintf("\n");
  259. return true;
  260. });
  261. }
  262. void VirtualFileSystem::listDirectoryRecursively(const String& path)
  263. {
  264. auto directory = resolvePath(path);
  265. if (!directory.isValid())
  266. return;
  267. kprintf("%s\n", path.characters());
  268. enumerateDirectoryInode(directory, [&] (const FileSystem::DirectoryEntry& entry) {
  269. auto metadata = entry.inode.metadata();
  270. if (metadata.isDirectory()) {
  271. if (entry.name != "." && entry.name != "..") {
  272. char buf[4096];
  273. ksprintf(buf, "%s/%s", path.characters(), entry.name.characters());
  274. listDirectoryRecursively(buf);
  275. }
  276. } else {
  277. kprintf("%s/%s\n", path.characters(), entry.name.characters());
  278. }
  279. });
  280. }
  281. bool VirtualFileSystem::touch(const String& path)
  282. {
  283. auto inode = resolvePath(path);
  284. if (!inode.isValid())
  285. return false;
  286. return inode.fileSystem()->setModificationTime(inode, ktime(nullptr));
  287. }
  288. OwnPtr<FileHandle> VirtualFileSystem::open(const String& path)
  289. {
  290. auto inode = resolvePath(path);
  291. if (!inode.isValid())
  292. return nullptr;
  293. auto vnode = getOrCreateNode(inode);
  294. if (!vnode)
  295. return nullptr;
  296. return make<FileHandle>(move(vnode));
  297. }
  298. OwnPtr<FileHandle> VirtualFileSystem::create(const String& path)
  299. {
  300. // FIXME: Do the real thing, not just this fake thing!
  301. (void) path;
  302. m_rootNode->fileSystem()->createInode(m_rootNode->fileSystem()->rootInode(), "empty", 0100644, 0);
  303. return nullptr;
  304. }
  305. OwnPtr<FileHandle> VirtualFileSystem::mkdir(const String& path)
  306. {
  307. // FIXME: Do the real thing, not just this fake thing!
  308. (void) path;
  309. m_rootNode->fileSystem()->makeDirectory(m_rootNode->fileSystem()->rootInode(), "mydir", 0400755);
  310. return nullptr;
  311. }
  312. InodeIdentifier VirtualFileSystem::resolveSymbolicLink(const String& basePath, InodeIdentifier symlinkInode)
  313. {
  314. auto symlinkContents = symlinkInode.readEntireFile();
  315. if (!symlinkContents)
  316. return { };
  317. char buf[4096];
  318. ksprintf(buf, "/%s/%s", basePath.characters(), String((const char*)symlinkContents.pointer(), symlinkContents.size()).characters());
  319. return resolvePath(buf);
  320. }
  321. InodeIdentifier VirtualFileSystem::resolvePath(const String& path)
  322. {
  323. auto parts = path.split('/');
  324. InodeIdentifier inode = m_rootNode->inode;
  325. for (unsigned i = 0; i < parts.size(); ++i) {
  326. auto& part = parts[i];
  327. auto metadata = inode.metadata();
  328. if (!metadata.isValid()) {
  329. #ifdef VFS_DEBUG
  330. kprintf("invalid metadata\n");
  331. #endif
  332. return InodeIdentifier();
  333. }
  334. if (!metadata.isDirectory()) {
  335. #ifdef VFS_DEBUG
  336. kprintf("not directory\n");
  337. #endif
  338. return InodeIdentifier();
  339. }
  340. inode = inode.fileSystem()->childOfDirectoryInodeWithName(inode, part);
  341. if (!inode.isValid()) {
  342. #ifdef VFS_DEBUG
  343. kprintf("bad child\n");
  344. #endif
  345. return InodeIdentifier();
  346. }
  347. #ifdef VFS_DEBUG
  348. kprintf("<%s> %02u:%08u\n", part.characters(), inode.fileSystemID(), inode.index());
  349. #endif
  350. if (auto mount = findMountForHost(inode)) {
  351. #ifdef VFS_DEBUG
  352. kprintf(" -- is host\n");
  353. #endif
  354. inode = mount->guest();
  355. }
  356. if (inode.isRootInode() && !isRoot(inode) && part == "..") {
  357. #ifdef VFS_DEBUG
  358. kprintf(" -- is guest\n");
  359. #endif
  360. auto mount = findMountForGuest(inode);
  361. inode = mount->host();
  362. inode = inode.fileSystem()->childOfDirectoryInodeWithName(inode, "..");
  363. }
  364. metadata = inode.metadata();
  365. if (metadata.isSymbolicLink()) {
  366. char buf[4096] = "";
  367. char* p = buf;
  368. for (unsigned j = 0; j < i; ++j) {
  369. p += ksprintf(p, "/%s", parts[j].characters());
  370. }
  371. inode = resolveSymbolicLink(buf, inode);
  372. if (!inode.isValid()) {
  373. kprintf("Symbolic link resolution failed :(\n");
  374. return { };
  375. }
  376. }
  377. }
  378. return inode;
  379. }
  380. void VirtualFileSystem::Node::retain()
  381. {
  382. ++retainCount;
  383. }
  384. void VirtualFileSystem::Node::release()
  385. {
  386. ASSERT(retainCount);
  387. if (--retainCount == 0) {
  388. vfs->freeNode(this);
  389. }
  390. }
  391. VirtualFileSystem::Mount::Mount(InodeIdentifier host, RetainPtr<FileSystem>&& guestFileSystem)
  392. : m_host(host)
  393. , m_guest(guestFileSystem->rootInode())
  394. , m_fileSystem(move(guestFileSystem))
  395. {
  396. }
  397. void VirtualFileSystem::registerCharacterDevice(unsigned major, unsigned minor, CharacterDevice& device)
  398. {
  399. m_characterDevices.set(encodedDevice(major, minor), &device);
  400. }