VirtualFileSystem.cpp 15 KB

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