VirtualFileSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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::initialize_globals();
  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 core_inode = inode.fs()->get_inode(inode);
  43. if (!core_inode)
  44. return nullptr;
  45. auto& metadata = core_inode->metadata();
  46. InterruptDisabler disabler;
  47. CharacterDevice* characterDevice = nullptr;
  48. if (metadata.isCharacterDevice()) {
  49. auto it = m_character_devices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
  50. if (it != m_character_devices.end()) {
  51. characterDevice = (*it).value;
  52. } else {
  53. kprintf("VFS: makeNode() no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
  54. return nullptr;
  55. }
  56. }
  57. auto vnode = allocateNode();
  58. ASSERT(vnode);
  59. FS* fileSystem = inode.fs();
  60. fileSystem->retain();
  61. vnode->inode = inode;
  62. vnode->m_core_inode = move(core_inode);
  63. vnode->m_cachedMetadata = metadata;
  64. #ifdef VFS_DEBUG
  65. kprintf("makeNode: inode=%u, size=%u, mode=%o, uid=%u, gid=%u\n", inode.index(), metadata.size, metadata.mode, metadata.uid, metadata.gid);
  66. #endif
  67. m_inode2vnode.set(inode, vnode.ptr());
  68. vnode->m_characterDevice = characterDevice;
  69. return vnode;
  70. }
  71. auto VFS::makeNode(CharacterDevice& device) -> RetainPtr<Vnode>
  72. {
  73. InterruptDisabler disabler;
  74. auto vnode = allocateNode();
  75. ASSERT(vnode);
  76. #ifdef VFS_DEBUG
  77. kprintf("makeNode: device=%p (%u,%u)\n", &device, device.major(), device.minor());
  78. #endif
  79. m_device2vnode.set(encodedDevice(device.major(), device.minor()), vnode.ptr());
  80. vnode->m_characterDevice = &device;
  81. return vnode;
  82. }
  83. auto VFS::get_or_create_node(InodeIdentifier inode) -> RetainPtr<Vnode>
  84. {
  85. {
  86. InterruptDisabler disabler;
  87. auto it = m_inode2vnode.find(inode);
  88. if (it != m_inode2vnode.end())
  89. return (*it).value;
  90. }
  91. return makeNode(inode);
  92. }
  93. auto VFS::get_or_create_node(CharacterDevice& device) -> RetainPtr<Vnode>
  94. {
  95. {
  96. InterruptDisabler disabler;
  97. auto it = m_device2vnode.find(encodedDevice(device.major(), device.minor()));
  98. if (it != m_device2vnode.end())
  99. return (*it).value;
  100. }
  101. return makeNode(device);
  102. }
  103. InodeIdentifier VFS::root_inode_id() const
  104. {
  105. return m_root_vnode->inode;
  106. }
  107. bool VFS::mount(RetainPtr<FS>&& fileSystem, const String& path)
  108. {
  109. ASSERT(fileSystem);
  110. int error;
  111. auto inode = resolve_path(path, root_inode_id(), error);
  112. if (!inode.is_valid()) {
  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<FS>&& 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->fs()->class_name(),
  141. m_root_vnode->fs());
  142. m_mounts.append(move(mount));
  143. return true;
  144. }
  145. auto VFS::allocateNode() -> RetainPtr<Vnode>
  146. {
  147. if (m_vnode_freelist.is_empty()) {
  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.is_valid()) {
  164. m_inode2vnode.remove(node->inode);
  165. node->inode.fs()->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. auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
  177. {
  178. for (auto& mount : m_mounts) {
  179. if (mount->host() == inode)
  180. return mount.ptr();
  181. }
  182. return nullptr;
  183. }
  184. auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
  185. {
  186. for (auto& mount : m_mounts) {
  187. if (mount->guest() == inode)
  188. return mount.ptr();
  189. }
  190. return nullptr;
  191. }
  192. bool VFS::is_vfs_root(InodeIdentifier inode) const
  193. {
  194. return inode == m_root_vnode->inode;
  195. }
  196. void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
  197. {
  198. dir_inode.traverse_as_directory([&] (const FS::DirectoryEntry& entry) {
  199. InodeIdentifier resolvedInode;
  200. if (auto mount = find_mount_for_host(entry.inode))
  201. resolvedInode = mount->guest();
  202. else
  203. resolvedInode = entry.inode;
  204. if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
  205. auto mount = find_mount_for_guest(entry.inode);
  206. ASSERT(mount);
  207. resolvedInode = mount->host();
  208. }
  209. callback(FS::DirectoryEntry(entry.name, entry.name_length, resolvedInode, entry.fileType));
  210. return true;
  211. });
  212. }
  213. RetainPtr<FileDescriptor> VFS::open(CharacterDevice& device, int options)
  214. {
  215. // FIXME: Respect options.
  216. (void) options;
  217. auto vnode = get_or_create_node(device);
  218. if (!vnode)
  219. return nullptr;
  220. return FileDescriptor::create(move(vnode));
  221. }
  222. RetainPtr<FileDescriptor> VFS::open(const String& path, int& error, int options, InodeIdentifier base)
  223. {
  224. auto inode = resolve_path(path, base, error, options);
  225. if (!inode.is_valid())
  226. return nullptr;
  227. auto vnode = get_or_create_node(inode);
  228. if (!vnode)
  229. return nullptr;
  230. return FileDescriptor::create(move(vnode));
  231. }
  232. RetainPtr<FileDescriptor> VFS::create(const String& path, InodeIdentifier base, int& error)
  233. {
  234. // FIXME: Do the real thing, not just this fake thing!
  235. (void) path;
  236. (void) base;
  237. m_root_vnode->fs()->create_inode(m_root_vnode->fs()->root_inode(), "empty", 0100644, 0, error);
  238. return nullptr;
  239. }
  240. bool VFS::mkdir(const String& path, mode_t mode, InodeIdentifier base, int& error)
  241. {
  242. error = EWHYTHO;
  243. // FIXME: This won't work nicely across mount boundaries.
  244. FileSystemPath p(path);
  245. if (!p.is_valid()) {
  246. error = -EINVAL;
  247. return false;
  248. }
  249. InodeIdentifier parent_dir;
  250. auto existing_dir = resolve_path(path, base, error, 0, &parent_dir);
  251. if (existing_dir.is_valid()) {
  252. error = -EEXIST;
  253. return false;
  254. }
  255. if (!parent_dir.is_valid()) {
  256. error = -ENOENT;
  257. return false;
  258. }
  259. if (error != -ENOENT) {
  260. return false;
  261. }
  262. dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  263. auto new_dir = base.fs()->create_directory(parent_dir, p.basename(), mode, error);
  264. if (new_dir.is_valid()) {
  265. error = 0;
  266. return true;
  267. }
  268. return false;
  269. }
  270. InodeIdentifier VFS::resolveSymbolicLink(InodeIdentifier base, InodeIdentifier symlinkInode, int& error)
  271. {
  272. auto symlinkContents = symlinkInode.read_entire_file();
  273. if (!symlinkContents)
  274. return { };
  275. auto linkee = String((const char*)symlinkContents.pointer(), symlinkContents.size());
  276. #ifdef VFS_DEBUG
  277. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  278. #endif
  279. return resolve_path(linkee, base, error);
  280. }
  281. RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  282. {
  283. if (!inode_id.is_valid())
  284. return nullptr;
  285. return inode_id.fs()->get_inode(inode_id);
  286. }
  287. String VFS::absolute_path(Inode& core_inode)
  288. {
  289. int error;
  290. Vector<InodeIdentifier> lineage;
  291. RetainPtr<Inode> inode = &core_inode;
  292. while (inode->identifier() != m_root_vnode->inode) {
  293. if (auto* mount = find_mount_for_guest(inode->identifier()))
  294. lineage.append(mount->host());
  295. else
  296. lineage.append(inode->identifier());
  297. InodeIdentifier parent_id;
  298. if (inode->is_directory()) {
  299. parent_id = resolve_path("..", inode->identifier(), error);
  300. } else {
  301. parent_id = inode->fs().find_parent_of_inode(inode->identifier());
  302. }
  303. ASSERT(parent_id.is_valid());
  304. inode = get_inode(parent_id);
  305. }
  306. if (lineage.is_empty())
  307. return "/";
  308. lineage.append(m_root_vnode->inode);
  309. StringBuilder builder;
  310. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  311. auto& child = lineage[i - 1];
  312. auto parent = lineage[i];
  313. if (auto* mount = find_mount_for_host(parent))
  314. parent = mount->guest();
  315. builder.append('/');
  316. auto parent_inode = get_inode(parent);
  317. builder.append(parent_inode->reverse_lookup(child));
  318. }
  319. return builder.build();
  320. }
  321. InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* deepest_dir)
  322. {
  323. if (path.is_empty()) {
  324. error = -EINVAL;
  325. return { };
  326. }
  327. auto parts = path.split('/');
  328. InodeIdentifier crumb_id;
  329. if (path[0] == '/')
  330. crumb_id = m_root_vnode->inode;
  331. else
  332. crumb_id = base.is_valid() ? base : m_root_vnode->inode;
  333. if (deepest_dir)
  334. *deepest_dir = crumb_id;
  335. for (unsigned i = 0; i < parts.size(); ++i) {
  336. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  337. auto& part = parts[i];
  338. if (part.is_empty())
  339. break;
  340. auto crumb_inode = get_inode(crumb_id);
  341. ASSERT(crumb_inode);
  342. auto metadata = crumb_inode->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. crumb_id = crumb_inode->lookup(part);
  359. if (!crumb_id.is_valid()) {
  360. #ifdef VFS_DEBUG
  361. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  362. #endif
  363. error = -ENOENT;
  364. return { };
  365. }
  366. #ifdef VFS_DEBUG
  367. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  368. #endif
  369. if (auto mount = find_mount_for_host(crumb_id)) {
  370. #ifdef VFS_DEBUG
  371. kprintf(" -- is host\n");
  372. #endif
  373. crumb_id = mount->guest();
  374. }
  375. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  376. #ifdef VFS_DEBUG
  377. kprintf(" -- is guest\n");
  378. #endif
  379. auto mount = find_mount_for_guest(crumb_id);
  380. auto dir_inode = get_inode(mount->host());
  381. crumb_id = dir_inode->lookup("..");
  382. }
  383. crumb_inode = get_inode(crumb_id);
  384. metadata = crumb_inode->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. return m_cachedMetadata;
  425. }
  426. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  427. : m_host(host)
  428. , m_guest(guest_fs->root_inode())
  429. , m_guest_fs(move(guest_fs))
  430. {
  431. }
  432. void VFS::register_character_device(CharacterDevice& device)
  433. {
  434. m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
  435. }
  436. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  437. {
  438. for (auto& mount : m_mounts) {
  439. callback(*mount);
  440. }
  441. }
  442. void VFS::sync()
  443. {
  444. FS::sync();
  445. }