VirtualFileSystem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. }
  30. VFS::~VFS()
  31. {
  32. }
  33. InodeIdentifier VFS::root_inode_id() const
  34. {
  35. ASSERT(m_root_inode);
  36. return m_root_inode->identifier();
  37. }
  38. bool VFS::mount(RetainPtr<FS>&& fileSystem, const String& path)
  39. {
  40. ASSERT(fileSystem);
  41. int error;
  42. auto inode = resolve_path(path, root_inode_id(), error);
  43. if (!inode.is_valid()) {
  44. kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
  45. return false;
  46. }
  47. kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", fileSystem->class_name(), fileSystem.ptr(), path.characters(), inode.index());
  48. // FIXME: check that this is not already a mount point
  49. auto mount = make<Mount>(inode, move(fileSystem));
  50. m_mounts.append(move(mount));
  51. return true;
  52. }
  53. bool VFS::mount_root(RetainPtr<FS>&& fileSystem)
  54. {
  55. if (m_root_inode) {
  56. kprintf("VFS: mount_root can't mount another root\n");
  57. return false;
  58. }
  59. auto mount = make<Mount>(InodeIdentifier(), move(fileSystem));
  60. auto root_inode_id = mount->guest().fs()->root_inode();
  61. auto root_inode = mount->guest().fs()->get_inode(root_inode_id);
  62. if (!root_inode->is_directory()) {
  63. kprintf("VFS: root inode (%02u:%08u) for / is not a directory :(\n", root_inode_id.fsid(), root_inode_id.index());
  64. return false;
  65. }
  66. m_root_inode = move(root_inode);
  67. kprintf("VFS: mounted root on %s{%p}\n",
  68. m_root_inode->fs().class_name(),
  69. &m_root_inode->fs());
  70. m_mounts.append(move(mount));
  71. return true;
  72. }
  73. auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
  74. {
  75. for (auto& mount : m_mounts) {
  76. if (mount->host() == inode)
  77. return mount.ptr();
  78. }
  79. return nullptr;
  80. }
  81. auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
  82. {
  83. for (auto& mount : m_mounts) {
  84. if (mount->guest() == inode)
  85. return mount.ptr();
  86. }
  87. return nullptr;
  88. }
  89. bool VFS::is_vfs_root(InodeIdentifier inode) const
  90. {
  91. return inode == root_inode_id();
  92. }
  93. void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
  94. {
  95. dir_inode.traverse_as_directory([&] (const FS::DirectoryEntry& entry) {
  96. InodeIdentifier resolvedInode;
  97. if (auto mount = find_mount_for_host(entry.inode))
  98. resolvedInode = mount->guest();
  99. else
  100. resolvedInode = entry.inode;
  101. if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
  102. auto mount = find_mount_for_guest(entry.inode);
  103. ASSERT(mount);
  104. resolvedInode = mount->host();
  105. }
  106. callback(FS::DirectoryEntry(entry.name, entry.name_length, resolvedInode, entry.fileType));
  107. return true;
  108. });
  109. }
  110. RetainPtr<FileDescriptor> VFS::open(RetainPtr<CharacterDevice>&& device, int& error, int options)
  111. {
  112. // FIXME: Respect options.
  113. (void) options;
  114. (void) error;
  115. return FileDescriptor::create(move(device));
  116. }
  117. RetainPtr<FileDescriptor> VFS::open(const String& path, int& error, int options, InodeIdentifier base)
  118. {
  119. auto inode_id = resolve_path(path, base, error, options);
  120. auto inode = get_inode(inode_id);
  121. if (!inode)
  122. return nullptr;
  123. auto metadata = inode->metadata();
  124. if (metadata.isCharacterDevice()) {
  125. auto it = m_character_devices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
  126. if (it == m_character_devices.end()) {
  127. kprintf("VFS::open: no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
  128. return nullptr;
  129. }
  130. return (*it).value->open(error, options);
  131. }
  132. return FileDescriptor::create(move(inode));
  133. }
  134. RetainPtr<FileDescriptor> VFS::create(const String& path, InodeIdentifier base, int& error)
  135. {
  136. // FIXME: Do the real thing, not just this fake thing!
  137. (void) path;
  138. (void) base;
  139. m_root_inode->fs().create_inode(m_root_inode->fs().root_inode(), "empty", 0100644, 0, error);
  140. return nullptr;
  141. }
  142. bool VFS::mkdir(const String& path, mode_t mode, InodeIdentifier base, int& error)
  143. {
  144. error = -EWHYTHO;
  145. // FIXME: This won't work nicely across mount boundaries.
  146. FileSystemPath p(path);
  147. if (!p.is_valid()) {
  148. error = -EINVAL;
  149. return false;
  150. }
  151. InodeIdentifier parent_dir;
  152. auto existing_dir = resolve_path(path, base, error, 0, &parent_dir);
  153. if (existing_dir.is_valid()) {
  154. error = -EEXIST;
  155. return false;
  156. }
  157. if (!parent_dir.is_valid()) {
  158. error = -ENOENT;
  159. return false;
  160. }
  161. if (error != -ENOENT) {
  162. return false;
  163. }
  164. dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  165. auto new_dir = base.fs()->create_directory(parent_dir, p.basename(), mode, error);
  166. if (new_dir) {
  167. error = 0;
  168. return true;
  169. }
  170. return false;
  171. }
  172. InodeIdentifier VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode, int& error)
  173. {
  174. auto symlink_contents = symlink_inode.read_entire();
  175. if (!symlink_contents)
  176. return { };
  177. auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
  178. #ifdef VFS_DEBUG
  179. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  180. #endif
  181. return resolve_path(linkee, base, error);
  182. }
  183. RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  184. {
  185. if (!inode_id.is_valid())
  186. return nullptr;
  187. return inode_id.fs()->get_inode(inode_id);
  188. }
  189. String VFS::absolute_path(Inode& core_inode)
  190. {
  191. int error;
  192. Vector<InodeIdentifier> lineage;
  193. RetainPtr<Inode> inode = &core_inode;
  194. while (inode->identifier() != root_inode_id()) {
  195. if (auto* mount = find_mount_for_guest(inode->identifier()))
  196. lineage.append(mount->host());
  197. else
  198. lineage.append(inode->identifier());
  199. InodeIdentifier parent_id;
  200. if (inode->is_directory()) {
  201. parent_id = resolve_path("..", inode->identifier(), error);
  202. } else {
  203. parent_id = inode->parent()->identifier();
  204. }
  205. ASSERT(parent_id.is_valid());
  206. inode = get_inode(parent_id);
  207. }
  208. if (lineage.is_empty())
  209. return "/";
  210. lineage.append(root_inode_id());
  211. StringBuilder builder;
  212. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  213. auto& child = lineage[i - 1];
  214. auto parent = lineage[i];
  215. if (auto* mount = find_mount_for_host(parent))
  216. parent = mount->guest();
  217. builder.append('/');
  218. auto parent_inode = get_inode(parent);
  219. builder.append(parent_inode->reverse_lookup(child));
  220. }
  221. return builder.build();
  222. }
  223. InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* deepest_dir)
  224. {
  225. if (path.is_empty()) {
  226. error = -EINVAL;
  227. return { };
  228. }
  229. auto parts = path.split('/');
  230. InodeIdentifier crumb_id;
  231. if (path[0] == '/')
  232. crumb_id = root_inode_id();
  233. else
  234. crumb_id = base.is_valid() ? base : root_inode_id();
  235. if (deepest_dir)
  236. *deepest_dir = crumb_id;
  237. for (unsigned i = 0; i < parts.size(); ++i) {
  238. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  239. auto& part = parts[i];
  240. if (part.is_empty())
  241. break;
  242. auto crumb_inode = get_inode(crumb_id);
  243. if (!crumb_inode) {
  244. #ifdef VFS_DEBUG
  245. kprintf("invalid metadata\n");
  246. #endif
  247. error = -EIO;
  248. return { };
  249. }
  250. auto metadata = crumb_inode->metadata();
  251. if (!metadata.isDirectory()) {
  252. #ifdef VFS_DEBUG
  253. 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);
  254. #endif
  255. error = -ENOTDIR;
  256. return { };
  257. }
  258. auto parent = crumb_id;
  259. crumb_id = crumb_inode->lookup(part);
  260. if (!crumb_id.is_valid()) {
  261. #ifdef VFS_DEBUG
  262. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  263. #endif
  264. error = -ENOENT;
  265. return { };
  266. }
  267. #ifdef VFS_DEBUG
  268. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  269. #endif
  270. if (auto mount = find_mount_for_host(crumb_id)) {
  271. #ifdef VFS_DEBUG
  272. kprintf(" -- is host\n");
  273. #endif
  274. crumb_id = mount->guest();
  275. }
  276. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  277. #ifdef VFS_DEBUG
  278. kprintf(" -- is guest\n");
  279. #endif
  280. auto mount = find_mount_for_guest(crumb_id);
  281. auto dir_inode = get_inode(mount->host());
  282. crumb_id = dir_inode->lookup("..");
  283. }
  284. crumb_inode = get_inode(crumb_id);
  285. metadata = crumb_inode->metadata();
  286. if (metadata.isDirectory()) {
  287. if (deepest_dir)
  288. *deepest_dir = crumb_id;
  289. }
  290. if (metadata.isSymbolicLink()) {
  291. if (i == parts.size() - 1) {
  292. if (options & O_NOFOLLOW) {
  293. error = -ELOOP;
  294. return { };
  295. }
  296. if (options & O_NOFOLLOW_NOERROR)
  297. return crumb_id;
  298. }
  299. crumb_id = resolve_symbolic_link(parent, *crumb_inode, error);
  300. if (!crumb_id.is_valid()) {
  301. kprintf("Symbolic link resolution failed :(\n");
  302. return { };
  303. }
  304. }
  305. }
  306. return crumb_id;
  307. }
  308. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  309. : m_host(host)
  310. , m_guest(guest_fs->root_inode())
  311. , m_guest_fs(move(guest_fs))
  312. {
  313. }
  314. void VFS::register_character_device(CharacterDevice& device)
  315. {
  316. m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
  317. }
  318. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  319. {
  320. for (auto& mount : m_mounts) {
  321. callback(*mount);
  322. }
  323. }
  324. void VFS::sync()
  325. {
  326. FS::sync();
  327. }