VirtualFileSystem.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. if (!isSocket(mode) && !isFIFO(mode) && !isBlockDevice(mode) && !isCharacterDevice(mode)) {
  142. // Turn it into a regular file. (This feels rather hackish.)
  143. mode |= 0100000;
  144. }
  145. // FIXME: This won't work nicely across mount boundaries.
  146. FileSystemPath p(path);
  147. if (!p.is_valid()) {
  148. error = -EINVAL;
  149. return nullptr;
  150. }
  151. InodeIdentifier parent_dir;
  152. auto existing_file = resolve_path(path, base, error, 0, &parent_dir);
  153. if (existing_file.is_valid()) {
  154. error = -EEXIST;
  155. return nullptr;
  156. }
  157. if (!parent_dir.is_valid()) {
  158. error = -ENOENT;
  159. return nullptr;
  160. }
  161. if (error != -ENOENT) {
  162. return nullptr;
  163. }
  164. dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  165. auto new_file = parent_dir.fs()->create_inode(parent_dir, p.basename(), mode, 0, error);
  166. if (!new_file)
  167. return nullptr;
  168. error = 0;
  169. return FileDescriptor::create(move(new_file));
  170. }
  171. bool VFS::mkdir(const String& path, mode_t mode, InodeIdentifier base, int& error)
  172. {
  173. error = -EWHYTHO;
  174. // FIXME: This won't work nicely across mount boundaries.
  175. FileSystemPath p(path);
  176. if (!p.is_valid()) {
  177. error = -EINVAL;
  178. return false;
  179. }
  180. InodeIdentifier parent_dir;
  181. auto existing_dir = resolve_path(path, base, error, 0, &parent_dir);
  182. if (existing_dir.is_valid()) {
  183. error = -EEXIST;
  184. return false;
  185. }
  186. if (!parent_dir.is_valid()) {
  187. error = -ENOENT;
  188. return false;
  189. }
  190. if (error != -ENOENT) {
  191. return false;
  192. }
  193. dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  194. auto new_dir = base.fs()->create_directory(parent_dir, p.basename(), mode, error);
  195. if (new_dir) {
  196. error = 0;
  197. return true;
  198. }
  199. return false;
  200. }
  201. bool VFS::chmod(const String& path, mode_t mode, Inode& base, int& error)
  202. {
  203. error = -EWHYTHO;
  204. // FIXME: This won't work nicely across mount boundaries.
  205. FileSystemPath p(path);
  206. if (!p.is_valid()) {
  207. error = -EINVAL;
  208. return false;
  209. }
  210. InodeIdentifier parent_dir;
  211. auto inode_id = resolve_path(path, base.identifier(), error, 0, &parent_dir);
  212. if (!inode_id.is_valid()) {
  213. error = -ENOENT;
  214. return false;
  215. }
  216. auto inode = get_inode(inode_id);
  217. // FIXME: Permission checks.
  218. // Only change the permission bits.
  219. mode = (inode->mode() & ~04777) | (mode & 04777);
  220. kprintf("VFS::chmod(): %u:%u mode %o\n", inode_id.fsid(), inode_id.index(), mode);
  221. if (!inode->chmod(mode, error))
  222. return false;
  223. error = 0;
  224. return true;
  225. }
  226. bool VFS::unlink(const String& path, Inode& base, int& error)
  227. {
  228. error = -EWHYTHO;
  229. // FIXME: This won't work nicely across mount boundaries.
  230. FileSystemPath p(path);
  231. if (!p.is_valid()) {
  232. error = -EINVAL;
  233. return false;
  234. }
  235. InodeIdentifier parent_dir;
  236. auto inode_id = resolve_path(path, base.identifier(), error, 0, &parent_dir);
  237. if (!inode_id.is_valid()) {
  238. error = -ENOENT;
  239. return false;
  240. }
  241. auto inode = get_inode(inode_id);
  242. if (inode->is_directory()) {
  243. error = -EISDIR;
  244. return false;
  245. }
  246. auto parent_inode = get_inode(parent_dir);
  247. // FIXME: The reverse_lookup here can definitely be avoided.
  248. if (!parent_inode->remove_child(parent_inode->reverse_lookup(inode_id), error))
  249. return false;
  250. error = 0;
  251. return true;
  252. }
  253. bool VFS::rmdir(const String& path, Inode& base, int& error)
  254. {
  255. error = -EWHYTHO;
  256. // FIXME: This won't work nicely across mount boundaries.
  257. FileSystemPath p(path);
  258. if (!p.is_valid()) {
  259. error = -EINVAL;
  260. return false;
  261. }
  262. InodeIdentifier parent_dir;
  263. auto inode_id = resolve_path(path, base.identifier(), error, 0, &parent_dir);
  264. if (!inode_id.is_valid()) {
  265. error = -ENOENT;
  266. return false;
  267. }
  268. if (inode_id.fs()->is_readonly()) {
  269. error = -EROFS;
  270. return false;
  271. }
  272. // FIXME: We should return EINVAL if the last component of the path is "."
  273. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  274. auto inode = get_inode(inode_id);
  275. if (!inode->is_directory()) {
  276. error = -ENOTDIR;
  277. return false;
  278. }
  279. if (inode->directory_entry_count() != 2) {
  280. error = -ENOTEMPTY;
  281. return false;
  282. }
  283. auto parent_inode = get_inode(parent_dir);
  284. ASSERT(parent_inode);
  285. dbgprintf("VFS::rmdir: Removing inode %u:%u from parent %u:%u\n", inode_id.fsid(), inode_id.index(), parent_dir.fsid(), parent_dir.index());
  286. // To do:
  287. // - Remove '.' in target (--child.link_count)
  288. // - Remove '..' in target (--parent.link_count)
  289. // - Remove target from its parent (--parent.link_count)
  290. if (!inode->remove_child(".", error))
  291. return false;
  292. if (!inode->remove_child("..", error))
  293. return false;
  294. // FIXME: The reverse_lookup here can definitely be avoided.
  295. if (!parent_inode->remove_child(parent_inode->reverse_lookup(inode_id), error))
  296. return false;
  297. error = 0;
  298. return true;
  299. }
  300. InodeIdentifier VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode, int& error)
  301. {
  302. auto symlink_contents = symlink_inode.read_entire();
  303. if (!symlink_contents)
  304. return { };
  305. auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
  306. #ifdef VFS_DEBUG
  307. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  308. #endif
  309. return resolve_path(linkee, base, error);
  310. }
  311. RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  312. {
  313. if (!inode_id.is_valid())
  314. return nullptr;
  315. return inode_id.fs()->get_inode(inode_id);
  316. }
  317. String VFS::absolute_path(Inode& core_inode)
  318. {
  319. int error;
  320. Vector<InodeIdentifier> lineage;
  321. RetainPtr<Inode> inode = &core_inode;
  322. while (inode->identifier() != root_inode_id()) {
  323. if (auto* mount = find_mount_for_guest(inode->identifier()))
  324. lineage.append(mount->host());
  325. else
  326. lineage.append(inode->identifier());
  327. InodeIdentifier parent_id;
  328. if (inode->is_directory()) {
  329. parent_id = resolve_path("..", inode->identifier(), error);
  330. } else {
  331. parent_id = inode->parent()->identifier();
  332. }
  333. ASSERT(parent_id.is_valid());
  334. inode = get_inode(parent_id);
  335. }
  336. if (lineage.is_empty())
  337. return "/";
  338. lineage.append(root_inode_id());
  339. StringBuilder builder;
  340. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  341. auto& child = lineage[i - 1];
  342. auto parent = lineage[i];
  343. if (auto* mount = find_mount_for_host(parent))
  344. parent = mount->guest();
  345. builder.append('/');
  346. auto parent_inode = get_inode(parent);
  347. builder.append(parent_inode->reverse_lookup(child));
  348. }
  349. return builder.build();
  350. }
  351. InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
  352. {
  353. if (path.is_empty()) {
  354. error = -EINVAL;
  355. return { };
  356. }
  357. auto parts = path.split('/');
  358. InodeIdentifier crumb_id;
  359. if (path[0] == '/')
  360. crumb_id = root_inode_id();
  361. else
  362. crumb_id = base.is_valid() ? base : root_inode_id();
  363. if (parent_id)
  364. *parent_id = crumb_id;
  365. for (unsigned i = 0; i < parts.size(); ++i) {
  366. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  367. auto& part = parts[i];
  368. if (part.is_empty())
  369. break;
  370. auto crumb_inode = get_inode(crumb_id);
  371. if (!crumb_inode) {
  372. #ifdef VFS_DEBUG
  373. kprintf("invalid metadata\n");
  374. #endif
  375. error = -EIO;
  376. return { };
  377. }
  378. auto metadata = crumb_inode->metadata();
  379. if (!metadata.isDirectory()) {
  380. #ifdef VFS_DEBUG
  381. 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);
  382. #endif
  383. error = -ENOTDIR;
  384. return { };
  385. }
  386. auto parent = crumb_id;
  387. crumb_id = crumb_inode->lookup(part);
  388. if (!crumb_id.is_valid()) {
  389. #ifdef VFS_DEBUG
  390. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  391. #endif
  392. error = -ENOENT;
  393. return { };
  394. }
  395. #ifdef VFS_DEBUG
  396. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  397. #endif
  398. if (auto mount = find_mount_for_host(crumb_id)) {
  399. #ifdef VFS_DEBUG
  400. kprintf(" -- is host\n");
  401. #endif
  402. crumb_id = mount->guest();
  403. }
  404. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  405. #ifdef VFS_DEBUG
  406. kprintf(" -- is guest\n");
  407. #endif
  408. auto mount = find_mount_for_guest(crumb_id);
  409. auto dir_inode = get_inode(mount->host());
  410. crumb_id = dir_inode->lookup("..");
  411. }
  412. crumb_inode = get_inode(crumb_id);
  413. metadata = crumb_inode->metadata();
  414. if (metadata.isDirectory()) {
  415. if (i != parts.size() - 1) {
  416. if (parent_id)
  417. *parent_id = crumb_id;
  418. }
  419. }
  420. if (metadata.isSymbolicLink()) {
  421. if (i == parts.size() - 1) {
  422. if (options & O_NOFOLLOW) {
  423. error = -ELOOP;
  424. return { };
  425. }
  426. if (options & O_NOFOLLOW_NOERROR)
  427. return crumb_id;
  428. }
  429. crumb_id = resolve_symbolic_link(parent, *crumb_inode, error);
  430. if (!crumb_id.is_valid()) {
  431. kprintf("Symbolic link resolution failed :(\n");
  432. return { };
  433. }
  434. }
  435. }
  436. return crumb_id;
  437. }
  438. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  439. : m_host(host)
  440. , m_guest(guest_fs->root_inode())
  441. , m_guest_fs(move(guest_fs))
  442. {
  443. }
  444. void VFS::register_character_device(CharacterDevice& device)
  445. {
  446. m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
  447. }
  448. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  449. {
  450. for (auto& mount : m_mounts) {
  451. callback(*mount);
  452. }
  453. }
  454. void VFS::sync()
  455. {
  456. FS::sync();
  457. }