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. #ifdef VFS_DEBUG
  64. kprintf("makeNode: inode=%u, size=%u, mode=%o, uid=%u, gid=%u\n", inode.index(), metadata.size, metadata.mode, metadata.uid, metadata.gid);
  65. #endif
  66. m_inode2vnode.set(inode, vnode.ptr());
  67. vnode->m_characterDevice = characterDevice;
  68. return vnode;
  69. }
  70. auto VFS::makeNode(CharacterDevice& device) -> RetainPtr<Vnode>
  71. {
  72. InterruptDisabler disabler;
  73. auto vnode = allocateNode();
  74. ASSERT(vnode);
  75. #ifdef VFS_DEBUG
  76. kprintf("makeNode: device=%p (%u,%u)\n", &device, device.major(), device.minor());
  77. #endif
  78. m_device2vnode.set(encodedDevice(device.major(), device.minor()), vnode.ptr());
  79. vnode->m_characterDevice = &device;
  80. return vnode;
  81. }
  82. auto VFS::get_or_create_node(InodeIdentifier inode) -> RetainPtr<Vnode>
  83. {
  84. {
  85. InterruptDisabler disabler;
  86. auto it = m_inode2vnode.find(inode);
  87. if (it != m_inode2vnode.end())
  88. return (*it).value;
  89. }
  90. return makeNode(inode);
  91. }
  92. auto VFS::get_or_create_node(CharacterDevice& device) -> RetainPtr<Vnode>
  93. {
  94. {
  95. InterruptDisabler disabler;
  96. auto it = m_device2vnode.find(encodedDevice(device.major(), device.minor()));
  97. if (it != m_device2vnode.end())
  98. return (*it).value;
  99. }
  100. return makeNode(device);
  101. }
  102. InodeIdentifier VFS::root_inode_id() const
  103. {
  104. return m_root_vnode->inode;
  105. }
  106. bool VFS::mount(RetainPtr<FS>&& fileSystem, const String& path)
  107. {
  108. ASSERT(fileSystem);
  109. int error;
  110. auto inode = resolve_path(path, root_inode_id(), error);
  111. if (!inode.is_valid()) {
  112. kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
  113. return false;
  114. }
  115. kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", fileSystem->class_name(), fileSystem.ptr(), path.characters(), inode.index());
  116. // FIXME: check that this is not already a mount point
  117. auto mount = make<Mount>(inode, move(fileSystem));
  118. m_mounts.append(move(mount));
  119. return true;
  120. }
  121. bool VFS::mount_root(RetainPtr<FS>&& fileSystem)
  122. {
  123. if (m_root_vnode) {
  124. kprintf("VFS: mount_root can't mount another root\n");
  125. return false;
  126. }
  127. auto mount = make<Mount>(InodeIdentifier(), move(fileSystem));
  128. auto node = makeNode(mount->guest());
  129. if (!node->inUse()) {
  130. kprintf("VFS: root inode for / is not in use :(\n");
  131. return false;
  132. }
  133. if (!node->metadata().isDirectory()) {
  134. kprintf("VFS: root inode for / is not a directory :(\n");
  135. return false;
  136. }
  137. m_root_vnode = move(node);
  138. kprintf("VFS: mounted root on %s{%p}\n",
  139. m_root_vnode->fs()->class_name(),
  140. m_root_vnode->fs());
  141. m_mounts.append(move(mount));
  142. return true;
  143. }
  144. auto VFS::allocateNode() -> RetainPtr<Vnode>
  145. {
  146. if (m_vnode_freelist.is_empty()) {
  147. kprintf("VFS: allocateNode has no nodes left\n");
  148. return nullptr;
  149. }
  150. auto* node = m_vnode_freelist.takeLast();
  151. ASSERT(node->retainCount == 0);
  152. node->retainCount = 1;
  153. node->m_vfs = this;
  154. node->m_vmo = nullptr;
  155. return adopt(*node);
  156. }
  157. void VFS::freeNode(Vnode* node)
  158. {
  159. InterruptDisabler disabler;
  160. ASSERT(node);
  161. ASSERT(node->inUse());
  162. if (node->inode.is_valid()) {
  163. m_inode2vnode.remove(node->inode);
  164. node->inode.fs()->release();
  165. node->inode = InodeIdentifier();
  166. node->m_core_inode = nullptr;
  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) {
  265. error = 0;
  266. return true;
  267. }
  268. return false;
  269. }
  270. InodeIdentifier VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode, int& error)
  271. {
  272. auto symlink_contents = symlink_inode.read_entire();
  273. if (!symlink_contents)
  274. return { };
  275. auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.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. if (!crumb_inode) {
  342. #ifdef VFS_DEBUG
  343. kprintf("invalid metadata\n");
  344. #endif
  345. error = -EIO;
  346. return { };
  347. }
  348. auto metadata = crumb_inode->metadata();
  349. if (!metadata.isDirectory()) {
  350. #ifdef VFS_DEBUG
  351. 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);
  352. #endif
  353. error = -ENOTDIR;
  354. return { };
  355. }
  356. auto parent = crumb_id;
  357. crumb_id = crumb_inode->lookup(part);
  358. if (!crumb_id.is_valid()) {
  359. #ifdef VFS_DEBUG
  360. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  361. #endif
  362. error = -ENOENT;
  363. return { };
  364. }
  365. #ifdef VFS_DEBUG
  366. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  367. #endif
  368. if (auto mount = find_mount_for_host(crumb_id)) {
  369. #ifdef VFS_DEBUG
  370. kprintf(" -- is host\n");
  371. #endif
  372. crumb_id = mount->guest();
  373. }
  374. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  375. #ifdef VFS_DEBUG
  376. kprintf(" -- is guest\n");
  377. #endif
  378. auto mount = find_mount_for_guest(crumb_id);
  379. auto dir_inode = get_inode(mount->host());
  380. crumb_id = dir_inode->lookup("..");
  381. }
  382. crumb_inode = get_inode(crumb_id);
  383. metadata = crumb_inode->metadata();
  384. if (metadata.isDirectory()) {
  385. if (deepest_dir)
  386. *deepest_dir = crumb_id;
  387. }
  388. if (metadata.isSymbolicLink()) {
  389. if (i == parts.size() - 1) {
  390. if (options & O_NOFOLLOW) {
  391. error = -ELOOP;
  392. return { };
  393. }
  394. if (options & O_NOFOLLOW_NOERROR)
  395. return crumb_id;
  396. }
  397. crumb_id = resolve_symbolic_link(parent, *crumb_inode, error);
  398. if (!crumb_id.is_valid()) {
  399. kprintf("Symbolic link resolution failed :(\n");
  400. return { };
  401. }
  402. }
  403. }
  404. return crumb_id;
  405. }
  406. void Vnode::retain()
  407. {
  408. InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
  409. ++retainCount;
  410. }
  411. void Vnode::release()
  412. {
  413. InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
  414. ASSERT(retainCount);
  415. if (--retainCount == 0) {
  416. m_vfs->freeNode(this);
  417. }
  418. }
  419. InodeMetadata Vnode::metadata() const
  420. {
  421. if (m_core_inode)
  422. return m_core_inode->metadata();
  423. ASSERT_NOT_REACHED();
  424. return { };
  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. }