VirtualFileSystem.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. #include "VirtualFileSystem.h"
  2. #include "FileDescriptor.h"
  3. #include "FileSystem.h"
  4. #include <AK/StringBuilder.h>
  5. #include <AK/kmalloc.h>
  6. #include <AK/kstdio.h>
  7. #include <AK/ktime.h>
  8. #include "CharacterDevice.h"
  9. #include <LibC/errno_numbers.h>
  10. //#define VFS_DEBUG
  11. static VFS* s_the;
  12. #ifndef SERENITY
  13. typedef int InterruptDisabler;
  14. #endif
  15. VFS& VFS::the()
  16. {
  17. ASSERT(s_the);
  18. return *s_the;
  19. }
  20. void VFS::initialize_globals()
  21. {
  22. s_the = nullptr;
  23. FileSystem::initializeGlobals();
  24. }
  25. VFS::VFS()
  26. {
  27. #ifdef VFS_DEBUG
  28. kprintf("VFS: Constructing VFS\n");
  29. #endif
  30. s_the = this;
  31. m_max_vnode_count = 16;
  32. m_nodes = reinterpret_cast<Vnode*>(kmalloc(sizeof(Vnode) * max_vnode_count()));
  33. memset(m_nodes, 0, sizeof(Vnode) * max_vnode_count());
  34. for (unsigned i = 0; i < m_max_vnode_count; ++i)
  35. m_vnode_freelist.append(&m_nodes[i]);
  36. }
  37. VFS::~VFS()
  38. {
  39. kprintf("VFS: ~VirtualFileSystem with %u nodes allocated\n", allocated_vnode_count());
  40. // FIXME: m_nodes is never freed. Does it matter though?
  41. }
  42. auto VFS::makeNode(InodeIdentifier inode) -> RetainPtr<Vnode>
  43. {
  44. auto metadata = inode.metadata();
  45. if (!metadata.isValid())
  46. return nullptr;
  47. auto core_inode = inode.fileSystem()->get_inode(inode);
  48. if (core_inode)
  49. core_inode->m_metadata = metadata;
  50. InterruptDisabler disabler;
  51. CharacterDevice* characterDevice = nullptr;
  52. if (metadata.isCharacterDevice()) {
  53. auto it = m_character_devices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
  54. if (it != m_character_devices.end()) {
  55. characterDevice = (*it).value;
  56. } else {
  57. kprintf("VFS: makeNode() no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
  58. return nullptr;
  59. }
  60. }
  61. auto vnode = allocateNode();
  62. ASSERT(vnode);
  63. FileSystem* fileSystem = inode.fileSystem();
  64. fileSystem->retain();
  65. vnode->inode = inode;
  66. vnode->m_core_inode = move(core_inode);
  67. vnode->m_cachedMetadata = metadata;
  68. #ifdef VFS_DEBUG
  69. kprintf("makeNode: inode=%u, size=%u, mode=%o, uid=%u, gid=%u\n", inode.index(), metadata.size, metadata.mode, metadata.uid, metadata.gid);
  70. #endif
  71. m_inode2vnode.set(inode, vnode.ptr());
  72. vnode->m_characterDevice = characterDevice;
  73. return vnode;
  74. }
  75. auto VFS::makeNode(CharacterDevice& device) -> RetainPtr<Vnode>
  76. {
  77. InterruptDisabler disabler;
  78. auto vnode = allocateNode();
  79. ASSERT(vnode);
  80. #ifdef VFS_DEBUG
  81. kprintf("makeNode: device=%p (%u,%u)\n", &device, device.major(), device.minor());
  82. #endif
  83. m_device2vnode.set(encodedDevice(device.major(), device.minor()), vnode.ptr());
  84. vnode->m_characterDevice = &device;
  85. return vnode;
  86. }
  87. auto VFS::get_or_create_node(InodeIdentifier inode) -> RetainPtr<Vnode>
  88. {
  89. {
  90. InterruptDisabler disabler;
  91. auto it = m_inode2vnode.find(inode);
  92. if (it != m_inode2vnode.end())
  93. return (*it).value;
  94. }
  95. return makeNode(inode);
  96. }
  97. auto VFS::get_or_create_node(CharacterDevice& device) -> RetainPtr<Vnode>
  98. {
  99. {
  100. InterruptDisabler disabler;
  101. auto it = m_device2vnode.find(encodedDevice(device.major(), device.minor()));
  102. if (it != m_device2vnode.end())
  103. return (*it).value;
  104. }
  105. return makeNode(device);
  106. }
  107. bool VFS::mount(RetainPtr<FileSystem>&& fileSystem, const String& path)
  108. {
  109. ASSERT(fileSystem);
  110. int error;
  111. auto inode = resolve_path(path, error);
  112. if (!inode.isValid()) {
  113. kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
  114. return false;
  115. }
  116. kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", fileSystem->class_name(), fileSystem.ptr(), path.characters(), inode.index());
  117. // FIXME: check that this is not already a mount point
  118. auto mount = make<Mount>(inode, move(fileSystem));
  119. m_mounts.append(move(mount));
  120. return true;
  121. }
  122. bool VFS::mount_root(RetainPtr<FileSystem>&& fileSystem)
  123. {
  124. if (m_root_vnode) {
  125. kprintf("VFS: mount_root can't mount another root\n");
  126. return false;
  127. }
  128. auto mount = make<Mount>(InodeIdentifier(), move(fileSystem));
  129. auto node = makeNode(mount->guest());
  130. if (!node->inUse()) {
  131. kprintf("VFS: root inode for / is not in use :(\n");
  132. return false;
  133. }
  134. if (!node->metadata().isDirectory()) {
  135. kprintf("VFS: root inode for / is not a directory :(\n");
  136. return false;
  137. }
  138. m_root_vnode = move(node);
  139. kprintf("VFS: mounted root on %s{%p}\n",
  140. m_root_vnode->fileSystem()->class_name(),
  141. m_root_vnode->fileSystem());
  142. m_mounts.append(move(mount));
  143. return true;
  144. }
  145. auto VFS::allocateNode() -> RetainPtr<Vnode>
  146. {
  147. if (m_vnode_freelist.isEmpty()) {
  148. kprintf("VFS: allocateNode has no nodes left\n");
  149. return nullptr;
  150. }
  151. auto* node = m_vnode_freelist.takeLast();
  152. ASSERT(node->retainCount == 0);
  153. node->retainCount = 1;
  154. node->m_vfs = this;
  155. node->m_vmo = nullptr;
  156. return adopt(*node);
  157. }
  158. void VFS::freeNode(Vnode* node)
  159. {
  160. InterruptDisabler disabler;
  161. ASSERT(node);
  162. ASSERT(node->inUse());
  163. if (node->inode.isValid()) {
  164. m_inode2vnode.remove(node->inode);
  165. node->inode.fileSystem()->release();
  166. node->inode = InodeIdentifier();
  167. }
  168. if (node->m_characterDevice) {
  169. m_device2vnode.remove(encodedDevice(node->m_characterDevice->major(), node->m_characterDevice->minor()));
  170. node->m_characterDevice = nullptr;
  171. }
  172. node->m_vfs = nullptr;
  173. node->m_vmo = nullptr;
  174. m_vnode_freelist.append(move(node));
  175. }
  176. #ifndef SERENITY
  177. bool VFS::isDirectory(const String& path, InodeIdentifier base)
  178. {
  179. int error;
  180. auto inode = resolve_path(path, error, base);
  181. if (!inode.isValid())
  182. return false;
  183. return inode.metadata().isDirectory();
  184. }
  185. #endif
  186. auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
  187. {
  188. for (auto& mount : m_mounts) {
  189. if (mount->host() == inode)
  190. return mount.ptr();
  191. }
  192. return nullptr;
  193. }
  194. auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
  195. {
  196. for (auto& mount : m_mounts) {
  197. if (mount->guest() == inode)
  198. return mount.ptr();
  199. }
  200. return nullptr;
  201. }
  202. bool VFS::is_vfs_root(InodeIdentifier inode) const
  203. {
  204. return inode == m_root_vnode->inode;
  205. }
  206. void VFS::traverse_directory_inode(CoreInode& dir_inode, Function<bool(const FileSystem::DirectoryEntry&)> callback)
  207. {
  208. dir_inode.traverse_as_directory([&] (const FileSystem::DirectoryEntry& entry) {
  209. InodeIdentifier resolvedInode;
  210. if (auto mount = find_mount_for_host(entry.inode))
  211. resolvedInode = mount->guest();
  212. else
  213. resolvedInode = entry.inode;
  214. if (dir_inode.identifier().isRootInode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
  215. auto mount = find_mount_for_guest(entry.inode);
  216. ASSERT(mount);
  217. resolvedInode = mount->host();
  218. }
  219. callback(FileSystem::DirectoryEntry(entry.name, entry.name_length, resolvedInode, entry.fileType));
  220. return true;
  221. });
  222. }
  223. #ifndef SERENITY
  224. void VFS::listDirectory(const String& path, InodeIdentifier base)
  225. {
  226. int error;
  227. auto directoryInode = resolve_path(path, error, base);
  228. if (!directoryInode.isValid())
  229. return;
  230. kprintf("VFS: ls %s -> %s %02u:%08u\n", path.characters(), directoryInode.fileSystem()->class_name(), directoryInode.fsid(), directoryInode.index());
  231. enumerateDirectoryInode(directoryInode, [&] (const FileSystem::DirectoryEntry& entry) {
  232. const char* nameColorBegin = "";
  233. const char* nameColorEnd = "";
  234. auto metadata = entry.inode.metadata();
  235. ASSERT(metadata.isValid());
  236. if (metadata.isDirectory()) {
  237. nameColorBegin = "\033[34;1m";
  238. nameColorEnd = "\033[0m";
  239. } else if (metadata.isSymbolicLink()) {
  240. nameColorBegin = "\033[36;1m";
  241. nameColorEnd = "\033[0m";
  242. }
  243. if (metadata.isSticky()) {
  244. nameColorBegin = "\033[42;30m";
  245. nameColorEnd = "\033[0m";
  246. }
  247. if (metadata.isCharacterDevice() || metadata.isBlockDevice()) {
  248. nameColorBegin = "\033[33;1m";
  249. nameColorEnd = "\033[0m";
  250. }
  251. kprintf("%02u:%08u ",
  252. metadata.inode.fsid(),
  253. metadata.inode.index());
  254. if (metadata.isDirectory())
  255. kprintf("d");
  256. else if (metadata.isSymbolicLink())
  257. kprintf("l");
  258. else if (metadata.isBlockDevice())
  259. kprintf("b");
  260. else if (metadata.isCharacterDevice())
  261. kprintf("c");
  262. else if (metadata.isSocket())
  263. kprintf("s");
  264. else if (metadata.isFIFO())
  265. kprintf("f");
  266. else if (metadata.isRegularFile())
  267. kprintf("-");
  268. else
  269. kprintf("?");
  270. kprintf("%c%c%c%c%c%c%c%c",
  271. metadata.mode & 00400 ? 'r' : '-',
  272. metadata.mode & 00200 ? 'w' : '-',
  273. metadata.mode & 00100 ? 'x' : '-',
  274. metadata.mode & 00040 ? 'r' : '-',
  275. metadata.mode & 00020 ? 'w' : '-',
  276. metadata.mode & 00010 ? 'x' : '-',
  277. metadata.mode & 00004 ? 'r' : '-',
  278. metadata.mode & 00002 ? 'w' : '-'
  279. );
  280. if (metadata.isSticky())
  281. kprintf("t");
  282. else
  283. kprintf("%c", metadata.mode & 00001 ? 'x' : '-');
  284. if (metadata.isCharacterDevice() || metadata.isBlockDevice()) {
  285. char buf[16];
  286. ksprintf(buf, "%u, %u", metadata.majorDevice, metadata.minorDevice);
  287. kprintf("%12s ", buf);
  288. } else {
  289. kprintf("%12lld ", metadata.size);
  290. }
  291. kprintf("\033[30;1m");
  292. time_t mtime = metadata.mtime;
  293. auto tm = *klocaltime(&mtime);
  294. kprintf("%04u-%02u-%02u %02u:%02u:%02u ",
  295. tm.tm_year + 1900,
  296. tm.tm_mon + 1,
  297. tm.tm_mday,
  298. tm.tm_hour,
  299. tm.tm_min,
  300. tm.tm_sec);
  301. kprintf("\033[0m");
  302. kprintf("%s%s%s",
  303. nameColorBegin,
  304. entry.name,
  305. nameColorEnd);
  306. if (metadata.isDirectory()) {
  307. kprintf("/");
  308. } else if (metadata.isSymbolicLink()) {
  309. auto symlinkContents = directoryInode.fileSystem()->readEntireInode(metadata.inode);
  310. kprintf(" -> %s", String((const char*)symlinkContents.pointer(), symlinkContents.size()).characters());
  311. }
  312. kprintf("\n");
  313. return true;
  314. });
  315. }
  316. void VFS::listDirectoryRecursively(const String& path, InodeIdentifier base)
  317. {
  318. int error;
  319. auto directory = resolve_path(path, error, base);
  320. if (!directory.isValid())
  321. return;
  322. kprintf("%s\n", path.characters());
  323. enumerateDirectoryInode(directory, [&] (const FileSystem::DirectoryEntry& entry) {
  324. auto metadata = entry.inode.metadata();
  325. if (metadata.isDirectory()) {
  326. if (entry.name != "." && entry.name != "..") {
  327. char buf[4096];
  328. ksprintf(buf, "%s/%s", path.characters(), entry.name);
  329. listDirectoryRecursively(buf, base);
  330. }
  331. } else {
  332. kprintf("%s/%s\n", path.characters(), entry.name);
  333. }
  334. return true;
  335. });
  336. }
  337. #endif
  338. bool VFS::touch(const String& path)
  339. {
  340. int error;
  341. auto inode = resolve_path(path, error);
  342. if (!inode.isValid())
  343. return false;
  344. return inode.fileSystem()->set_mtime(inode, ktime(nullptr));
  345. }
  346. RetainPtr<FileDescriptor> VFS::open(CharacterDevice& device, int options)
  347. {
  348. // FIXME: Respect options.
  349. (void) options;
  350. auto vnode = get_or_create_node(device);
  351. if (!vnode)
  352. return nullptr;
  353. return FileDescriptor::create(move(vnode));
  354. }
  355. RetainPtr<FileDescriptor> VFS::open(const String& path, int& error, int options, InodeIdentifier base)
  356. {
  357. auto inode = resolve_path(path, error, base, options);
  358. if (!inode.isValid())
  359. return nullptr;
  360. auto vnode = get_or_create_node(inode);
  361. if (!vnode)
  362. return nullptr;
  363. return FileDescriptor::create(move(vnode));
  364. }
  365. RetainPtr<FileDescriptor> VFS::create(const String& path, InodeIdentifier base)
  366. {
  367. // FIXME: Do the real thing, not just this fake thing!
  368. (void) path;
  369. (void) base;
  370. m_root_vnode->fileSystem()->create_inode(m_root_vnode->fileSystem()->rootInode(), "empty", 0100644, 0);
  371. return nullptr;
  372. }
  373. RetainPtr<FileDescriptor> VFS::mkdir(const String& path, InodeIdentifier base)
  374. {
  375. // FIXME: Do the real thing, not just this fake thing!
  376. (void) path;
  377. (void) base;
  378. m_root_vnode->fileSystem()->create_directory(m_root_vnode->fileSystem()->rootInode(), "mydir", 0400755);
  379. return nullptr;
  380. }
  381. InodeIdentifier VFS::resolveSymbolicLink(InodeIdentifier base, InodeIdentifier symlinkInode, int& error)
  382. {
  383. auto symlinkContents = symlinkInode.readEntireFile();
  384. if (!symlinkContents)
  385. return { };
  386. auto linkee = String((const char*)symlinkContents.pointer(), symlinkContents.size());
  387. #ifdef VFS_DEBUG
  388. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  389. #endif
  390. return resolve_path(linkee, error, base);
  391. }
  392. RetainPtr<CoreInode> VFS::get_inode(InodeIdentifier inode_id)
  393. {
  394. if (!inode_id.isValid())
  395. return nullptr;
  396. return inode_id.fileSystem()->get_inode(inode_id);
  397. }
  398. String VFS::absolute_path(CoreInode& core_inode)
  399. {
  400. int error;
  401. Vector<InodeIdentifier> lineage;
  402. RetainPtr<CoreInode> inode = &core_inode;
  403. while (inode->identifier() != m_root_vnode->inode) {
  404. if (auto* mount = find_mount_for_guest(inode->identifier()))
  405. lineage.append(mount->host());
  406. else
  407. lineage.append(inode->identifier());
  408. InodeIdentifier parent_id;
  409. if (inode->is_directory()) {
  410. parent_id = resolve_path("..", error, inode->identifier());
  411. } else {
  412. parent_id = inode->fs().find_parent_of_inode(inode->identifier());
  413. }
  414. ASSERT(parent_id.isValid());
  415. inode = get_inode(parent_id);
  416. }
  417. if (lineage.isEmpty())
  418. return "/";
  419. lineage.append(m_root_vnode->inode);
  420. StringBuilder builder;
  421. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  422. auto& child = lineage[i - 1];
  423. auto parent = lineage[i];
  424. if (auto* mount = find_mount_for_host(parent))
  425. parent = mount->guest();
  426. builder.append('/');
  427. auto parent_inode = get_inode(parent);
  428. builder.append(parent_inode->reverse_lookup(child));
  429. }
  430. return builder.build();
  431. }
  432. InodeIdentifier VFS::resolve_path(const String& path, int& error, InodeIdentifier base, int options)
  433. {
  434. if (path.isEmpty())
  435. return { };
  436. auto parts = path.split('/');
  437. InodeIdentifier crumb_id;
  438. if (path[0] == '/')
  439. crumb_id = m_root_vnode->inode;
  440. else
  441. crumb_id = base.isValid() ? base : m_root_vnode->inode;
  442. for (unsigned i = 0; i < parts.size(); ++i) {
  443. bool inode_was_root_at_head_of_loop = crumb_id.isRootInode();
  444. auto& part = parts[i];
  445. if (part.isEmpty())
  446. break;
  447. auto metadata = crumb_id.metadata();
  448. if (!metadata.isValid()) {
  449. #ifdef VFS_DEBUG
  450. kprintf("invalid metadata\n");
  451. #endif
  452. error = -EIO;
  453. return { };
  454. }
  455. if (!metadata.isDirectory()) {
  456. #ifdef VFS_DEBUG
  457. kprintf("parent of <%s> not directory, it's inode %u:%u / %u:%u, mode: %u, size: %u\n", part.characters(), inode.fsid(), inode.index(), metadata.inode.fsid(), metadata.inode.index(), metadata.mode, metadata.size);
  458. #endif
  459. error = -EIO;
  460. return { };
  461. }
  462. auto parent = crumb_id;
  463. auto dir_inode = get_inode(crumb_id);
  464. crumb_id = dir_inode->lookup(part);
  465. if (!crumb_id.isValid()) {
  466. #ifdef VFS_DEBUG
  467. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  468. #endif
  469. error = -ENOENT;
  470. return { };
  471. }
  472. #ifdef VFS_DEBUG
  473. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  474. #endif
  475. if (auto mount = find_mount_for_host(crumb_id)) {
  476. #ifdef VFS_DEBUG
  477. kprintf(" -- is host\n");
  478. #endif
  479. crumb_id = mount->guest();
  480. }
  481. if (inode_was_root_at_head_of_loop && crumb_id.isRootInode() && !is_vfs_root(crumb_id) && part == "..") {
  482. #ifdef VFS_DEBUG
  483. kprintf(" -- is guest\n");
  484. #endif
  485. auto mount = find_mount_for_guest(crumb_id);
  486. auto dir_inode = get_inode(mount->host());
  487. crumb_id = dir_inode->lookup("..");
  488. }
  489. metadata = crumb_id.metadata();
  490. if (metadata.isSymbolicLink()) {
  491. if (i == parts.size() - 1) {
  492. if (options & O_NOFOLLOW) {
  493. error = -ELOOP;
  494. return { };
  495. }
  496. if (options & O_NOFOLLOW_NOERROR)
  497. return crumb_id;
  498. }
  499. crumb_id = resolveSymbolicLink(parent, crumb_id, error);
  500. if (!crumb_id.isValid()) {
  501. kprintf("Symbolic link resolution failed :(\n");
  502. return { };
  503. }
  504. }
  505. }
  506. return crumb_id;
  507. }
  508. void Vnode::retain()
  509. {
  510. InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
  511. ++retainCount;
  512. }
  513. void Vnode::release()
  514. {
  515. InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
  516. ASSERT(retainCount);
  517. if (--retainCount == 0) {
  518. m_vfs->freeNode(this);
  519. }
  520. }
  521. const InodeMetadata& Vnode::metadata() const
  522. {
  523. if (m_core_inode)
  524. return m_core_inode->metadata();
  525. if (!m_cachedMetadata.isValid())
  526. m_cachedMetadata = inode.metadata();
  527. return m_cachedMetadata;
  528. }
  529. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FileSystem>&& guest_fs)
  530. : m_host(host)
  531. , m_guest(guest_fs->rootInode())
  532. , m_guest_fs(move(guest_fs))
  533. {
  534. }
  535. void VFS::register_character_device(CharacterDevice& device)
  536. {
  537. m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
  538. }
  539. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  540. {
  541. for (auto& mount : m_mounts) {
  542. callback(*mount);
  543. }
  544. }