VirtualFileSystem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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, mode_t mode, InodeIdentifier base)
  118. {
  119. auto inode_id = resolve_path(path, base, error, options);
  120. auto inode = get_inode(inode_id);
  121. if (!inode) {
  122. if (options & O_CREAT)
  123. return create(path, error, options, mode, base);
  124. return nullptr;
  125. }
  126. auto metadata = inode->metadata();
  127. if (metadata.isCharacterDevice()) {
  128. auto it = m_character_devices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
  129. if (it == m_character_devices.end()) {
  130. kprintf("VFS::open: no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
  131. return nullptr;
  132. }
  133. return (*it).value->open(error, options);
  134. }
  135. return FileDescriptor::create(move(inode));
  136. }
  137. RetainPtr<FileDescriptor> VFS::create(const String& path, int& error, int options, mode_t mode, InodeIdentifier base)
  138. {
  139. (void) options;
  140. error = -EWHYTHO;
  141. // FIXME: This won't work nicely across mount boundaries.
  142. FileSystemPath p(path);
  143. if (!p.is_valid()) {
  144. error = -EINVAL;
  145. return nullptr;
  146. }
  147. InodeIdentifier parent_dir;
  148. auto existing_file = resolve_path(path, base, error, 0, &parent_dir);
  149. if (existing_file.is_valid()) {
  150. error = -EEXIST;
  151. return nullptr;
  152. }
  153. if (!parent_dir.is_valid()) {
  154. error = -ENOENT;
  155. return nullptr;
  156. }
  157. if (error != -ENOENT) {
  158. return nullptr;
  159. }
  160. dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  161. auto new_file = parent_dir.fs()->create_inode(parent_dir, p.basename(), mode, 0, error);
  162. if (!new_file)
  163. return nullptr;
  164. error = 0;
  165. return FileDescriptor::create(move(new_file));
  166. }
  167. bool VFS::mkdir(const String& path, mode_t mode, InodeIdentifier base, int& error)
  168. {
  169. error = -EWHYTHO;
  170. // FIXME: This won't work nicely across mount boundaries.
  171. FileSystemPath p(path);
  172. if (!p.is_valid()) {
  173. error = -EINVAL;
  174. return false;
  175. }
  176. InodeIdentifier parent_dir;
  177. auto existing_dir = resolve_path(path, base, error, 0, &parent_dir);
  178. if (existing_dir.is_valid()) {
  179. error = -EEXIST;
  180. return false;
  181. }
  182. if (!parent_dir.is_valid()) {
  183. error = -ENOENT;
  184. return false;
  185. }
  186. if (error != -ENOENT) {
  187. return false;
  188. }
  189. dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  190. auto new_dir = base.fs()->create_directory(parent_dir, p.basename(), mode, error);
  191. if (new_dir) {
  192. error = 0;
  193. return true;
  194. }
  195. return false;
  196. }
  197. InodeIdentifier VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode, int& error)
  198. {
  199. auto symlink_contents = symlink_inode.read_entire();
  200. if (!symlink_contents)
  201. return { };
  202. auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
  203. #ifdef VFS_DEBUG
  204. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  205. #endif
  206. return resolve_path(linkee, base, error);
  207. }
  208. RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  209. {
  210. if (!inode_id.is_valid())
  211. return nullptr;
  212. return inode_id.fs()->get_inode(inode_id);
  213. }
  214. String VFS::absolute_path(Inode& core_inode)
  215. {
  216. int error;
  217. Vector<InodeIdentifier> lineage;
  218. RetainPtr<Inode> inode = &core_inode;
  219. while (inode->identifier() != root_inode_id()) {
  220. if (auto* mount = find_mount_for_guest(inode->identifier()))
  221. lineage.append(mount->host());
  222. else
  223. lineage.append(inode->identifier());
  224. InodeIdentifier parent_id;
  225. if (inode->is_directory()) {
  226. parent_id = resolve_path("..", inode->identifier(), error);
  227. } else {
  228. parent_id = inode->parent()->identifier();
  229. }
  230. ASSERT(parent_id.is_valid());
  231. inode = get_inode(parent_id);
  232. }
  233. if (lineage.is_empty())
  234. return "/";
  235. lineage.append(root_inode_id());
  236. StringBuilder builder;
  237. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  238. auto& child = lineage[i - 1];
  239. auto parent = lineage[i];
  240. if (auto* mount = find_mount_for_host(parent))
  241. parent = mount->guest();
  242. builder.append('/');
  243. auto parent_inode = get_inode(parent);
  244. builder.append(parent_inode->reverse_lookup(child));
  245. }
  246. return builder.build();
  247. }
  248. InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* deepest_dir)
  249. {
  250. if (path.is_empty()) {
  251. error = -EINVAL;
  252. return { };
  253. }
  254. auto parts = path.split('/');
  255. InodeIdentifier crumb_id;
  256. if (path[0] == '/')
  257. crumb_id = root_inode_id();
  258. else
  259. crumb_id = base.is_valid() ? base : root_inode_id();
  260. if (deepest_dir)
  261. *deepest_dir = crumb_id;
  262. for (unsigned i = 0; i < parts.size(); ++i) {
  263. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  264. auto& part = parts[i];
  265. if (part.is_empty())
  266. break;
  267. auto crumb_inode = get_inode(crumb_id);
  268. if (!crumb_inode) {
  269. #ifdef VFS_DEBUG
  270. kprintf("invalid metadata\n");
  271. #endif
  272. error = -EIO;
  273. return { };
  274. }
  275. auto metadata = crumb_inode->metadata();
  276. if (!metadata.isDirectory()) {
  277. #ifdef VFS_DEBUG
  278. 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);
  279. #endif
  280. error = -ENOTDIR;
  281. return { };
  282. }
  283. auto parent = crumb_id;
  284. crumb_id = crumb_inode->lookup(part);
  285. if (!crumb_id.is_valid()) {
  286. #ifdef VFS_DEBUG
  287. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  288. #endif
  289. error = -ENOENT;
  290. return { };
  291. }
  292. #ifdef VFS_DEBUG
  293. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  294. #endif
  295. if (auto mount = find_mount_for_host(crumb_id)) {
  296. #ifdef VFS_DEBUG
  297. kprintf(" -- is host\n");
  298. #endif
  299. crumb_id = mount->guest();
  300. }
  301. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  302. #ifdef VFS_DEBUG
  303. kprintf(" -- is guest\n");
  304. #endif
  305. auto mount = find_mount_for_guest(crumb_id);
  306. auto dir_inode = get_inode(mount->host());
  307. crumb_id = dir_inode->lookup("..");
  308. }
  309. crumb_inode = get_inode(crumb_id);
  310. metadata = crumb_inode->metadata();
  311. if (metadata.isDirectory()) {
  312. if (deepest_dir)
  313. *deepest_dir = crumb_id;
  314. }
  315. if (metadata.isSymbolicLink()) {
  316. if (i == parts.size() - 1) {
  317. if (options & O_NOFOLLOW) {
  318. error = -ELOOP;
  319. return { };
  320. }
  321. if (options & O_NOFOLLOW_NOERROR)
  322. return crumb_id;
  323. }
  324. crumb_id = resolve_symbolic_link(parent, *crumb_inode, error);
  325. if (!crumb_id.is_valid()) {
  326. kprintf("Symbolic link resolution failed :(\n");
  327. return { };
  328. }
  329. }
  330. }
  331. return crumb_id;
  332. }
  333. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  334. : m_host(host)
  335. , m_guest(guest_fs->root_inode())
  336. , m_guest_fs(move(guest_fs))
  337. {
  338. }
  339. void VFS::register_character_device(CharacterDevice& device)
  340. {
  341. m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
  342. }
  343. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  344. {
  345. for (auto& mount : m_mounts) {
  346. callback(*mount);
  347. }
  348. }
  349. void VFS::sync()
  350. {
  351. FS::sync();
  352. }