VirtualFileSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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.fs()->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.fs();
  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.is_valid()) {
  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.is_valid()) {
  166. m_inode2vnode.remove(node->inode);
  167. node->inode.fs()->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().is_root_inode() && !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. RetainPtr<FileDescriptor> VFS::open(CharacterDevice& device, int options)
  216. {
  217. // FIXME: Respect options.
  218. (void) options;
  219. auto vnode = get_or_create_node(device);
  220. if (!vnode)
  221. return nullptr;
  222. return FileDescriptor::create(move(vnode));
  223. }
  224. RetainPtr<FileDescriptor> VFS::open(const String& path, int& error, int options, InodeIdentifier base)
  225. {
  226. auto inode = resolve_path(path, base, error, options);
  227. if (!inode.is_valid())
  228. return nullptr;
  229. auto vnode = get_or_create_node(inode);
  230. if (!vnode)
  231. return nullptr;
  232. return FileDescriptor::create(move(vnode));
  233. }
  234. RetainPtr<FileDescriptor> VFS::create(const String& path, InodeIdentifier base, int& error)
  235. {
  236. // FIXME: Do the real thing, not just this fake thing!
  237. (void) path;
  238. (void) base;
  239. m_root_vnode->fileSystem()->create_inode(m_root_vnode->fileSystem()->root_inode(), "empty", 0100644, 0, error);
  240. return nullptr;
  241. }
  242. bool VFS::mkdir(const String& path, mode_t mode, InodeIdentifier base, int& error)
  243. {
  244. error = EWHYTHO;
  245. // FIXME: This won't work nicely across mount boundaries.
  246. FileSystemPath p(path);
  247. if (!p.is_valid()) {
  248. error = -EINVAL;
  249. return false;
  250. }
  251. InodeIdentifier parent_dir;
  252. auto existing_dir = resolve_path(path, base, error, 0, &parent_dir);
  253. if (existing_dir.is_valid()) {
  254. error = -EEXIST;
  255. return false;
  256. }
  257. if (!parent_dir.is_valid()) {
  258. error = -ENOENT;
  259. return false;
  260. }
  261. if (error != -ENOENT) {
  262. return false;
  263. }
  264. dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  265. auto new_dir = base.fs()->create_directory(parent_dir, p.basename(), mode, error);
  266. if (new_dir.is_valid()) {
  267. error = 0;
  268. return true;
  269. }
  270. return false;
  271. }
  272. InodeIdentifier VFS::resolveSymbolicLink(InodeIdentifier base, InodeIdentifier symlinkInode, int& error)
  273. {
  274. auto symlinkContents = symlinkInode.read_entire_file();
  275. if (!symlinkContents)
  276. return { };
  277. auto linkee = String((const char*)symlinkContents.pointer(), symlinkContents.size());
  278. #ifdef VFS_DEBUG
  279. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  280. #endif
  281. return resolve_path(linkee, base, error);
  282. }
  283. RetainPtr<CoreInode> VFS::get_inode(InodeIdentifier inode_id)
  284. {
  285. if (!inode_id.is_valid())
  286. return nullptr;
  287. return inode_id.fs()->get_inode(inode_id);
  288. }
  289. String VFS::absolute_path(CoreInode& core_inode)
  290. {
  291. int error;
  292. Vector<InodeIdentifier> lineage;
  293. RetainPtr<CoreInode> inode = &core_inode;
  294. while (inode->identifier() != m_root_vnode->inode) {
  295. if (auto* mount = find_mount_for_guest(inode->identifier()))
  296. lineage.append(mount->host());
  297. else
  298. lineage.append(inode->identifier());
  299. InodeIdentifier parent_id;
  300. if (inode->is_directory()) {
  301. parent_id = resolve_path("..", inode->identifier(), error);
  302. } else {
  303. parent_id = inode->fs().find_parent_of_inode(inode->identifier());
  304. }
  305. ASSERT(parent_id.is_valid());
  306. inode = get_inode(parent_id);
  307. }
  308. if (lineage.isEmpty())
  309. return "/";
  310. lineage.append(m_root_vnode->inode);
  311. StringBuilder builder;
  312. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  313. auto& child = lineage[i - 1];
  314. auto parent = lineage[i];
  315. if (auto* mount = find_mount_for_host(parent))
  316. parent = mount->guest();
  317. builder.append('/');
  318. auto parent_inode = get_inode(parent);
  319. builder.append(parent_inode->reverse_lookup(child));
  320. }
  321. return builder.build();
  322. }
  323. InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* deepest_dir)
  324. {
  325. if (path.isEmpty()) {
  326. error = -EINVAL;
  327. return { };
  328. }
  329. auto parts = path.split('/');
  330. InodeIdentifier crumb_id;
  331. if (path[0] == '/')
  332. crumb_id = m_root_vnode->inode;
  333. else
  334. crumb_id = base.is_valid() ? base : m_root_vnode->inode;
  335. if (deepest_dir)
  336. *deepest_dir = crumb_id;
  337. for (unsigned i = 0; i < parts.size(); ++i) {
  338. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  339. auto& part = parts[i];
  340. if (part.isEmpty())
  341. break;
  342. auto metadata = crumb_id.metadata();
  343. if (!metadata.isValid()) {
  344. #ifdef VFS_DEBUG
  345. kprintf("invalid metadata\n");
  346. #endif
  347. error = -EIO;
  348. return { };
  349. }
  350. if (!metadata.isDirectory()) {
  351. #ifdef VFS_DEBUG
  352. 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);
  353. #endif
  354. error = -ENOTDIR;
  355. return { };
  356. }
  357. auto parent = crumb_id;
  358. auto dir_inode = get_inode(crumb_id);
  359. crumb_id = dir_inode->lookup(part);
  360. if (!crumb_id.is_valid()) {
  361. #ifdef VFS_DEBUG
  362. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  363. #endif
  364. error = -ENOENT;
  365. return { };
  366. }
  367. #ifdef VFS_DEBUG
  368. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  369. #endif
  370. if (auto mount = find_mount_for_host(crumb_id)) {
  371. #ifdef VFS_DEBUG
  372. kprintf(" -- is host\n");
  373. #endif
  374. crumb_id = mount->guest();
  375. }
  376. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  377. #ifdef VFS_DEBUG
  378. kprintf(" -- is guest\n");
  379. #endif
  380. auto mount = find_mount_for_guest(crumb_id);
  381. auto dir_inode = get_inode(mount->host());
  382. crumb_id = dir_inode->lookup("..");
  383. }
  384. metadata = crumb_id.metadata();
  385. if (metadata.isDirectory()) {
  386. if (deepest_dir)
  387. *deepest_dir = crumb_id;
  388. }
  389. if (metadata.isSymbolicLink()) {
  390. if (i == parts.size() - 1) {
  391. if (options & O_NOFOLLOW) {
  392. error = -ELOOP;
  393. return { };
  394. }
  395. if (options & O_NOFOLLOW_NOERROR)
  396. return crumb_id;
  397. }
  398. crumb_id = resolveSymbolicLink(parent, crumb_id, error);
  399. if (!crumb_id.is_valid()) {
  400. kprintf("Symbolic link resolution failed :(\n");
  401. return { };
  402. }
  403. }
  404. }
  405. return crumb_id;
  406. }
  407. void Vnode::retain()
  408. {
  409. InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
  410. ++retainCount;
  411. }
  412. void Vnode::release()
  413. {
  414. InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
  415. ASSERT(retainCount);
  416. if (--retainCount == 0) {
  417. m_vfs->freeNode(this);
  418. }
  419. }
  420. const InodeMetadata& Vnode::metadata() const
  421. {
  422. if (m_core_inode)
  423. return m_core_inode->metadata();
  424. if (!m_cachedMetadata.isValid())
  425. m_cachedMetadata = inode.metadata();
  426. return m_cachedMetadata;
  427. }
  428. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  429. : m_host(host)
  430. , m_guest(guest_fs->root_inode())
  431. , m_guest_fs(move(guest_fs))
  432. {
  433. }
  434. void VFS::register_character_device(CharacterDevice& device)
  435. {
  436. m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
  437. }
  438. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  439. {
  440. for (auto& mount : m_mounts) {
  441. callback(*mount);
  442. }
  443. }