VirtualFileSystem.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. #include "VirtualFileSystem.h"
  2. #include <Kernel/FileSystem/FileDescriptor.h>
  3. #include "FileSystem.h"
  4. #include <AK/FileSystemPath.h>
  5. #include <AK/StringBuilder.h>
  6. #include <Kernel/Devices/CharacterDevice.h>
  7. #include <LibC/errno_numbers.h>
  8. #include <Kernel/Process.h>
  9. //#define VFS_DEBUG
  10. static VFS* s_the;
  11. VFS& VFS::the()
  12. {
  13. ASSERT(s_the);
  14. return *s_the;
  15. }
  16. VFS::VFS()
  17. {
  18. #ifdef VFS_DEBUG
  19. kprintf("VFS: Constructing VFS\n");
  20. #endif
  21. s_the = this;
  22. }
  23. VFS::~VFS()
  24. {
  25. }
  26. InodeIdentifier VFS::root_inode_id() const
  27. {
  28. ASSERT(m_root_inode);
  29. return m_root_inode->identifier();
  30. }
  31. bool VFS::mount(RetainPtr<FS>&& file_system, StringView path)
  32. {
  33. ASSERT(file_system);
  34. int error;
  35. auto inode = old_resolve_path(path, root_inode_id(), error);
  36. if (!inode.is_valid()) {
  37. kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
  38. return false;
  39. }
  40. kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", file_system->class_name(), file_system.ptr(), path.characters(), inode.index());
  41. // FIXME: check that this is not already a mount point
  42. auto mount = make<Mount>(inode, move(file_system));
  43. m_mounts.append(move(mount));
  44. return true;
  45. }
  46. bool VFS::mount_root(RetainPtr<FS>&& file_system)
  47. {
  48. if (m_root_inode) {
  49. kprintf("VFS: mount_root can't mount another root\n");
  50. return false;
  51. }
  52. auto mount = make<Mount>(InodeIdentifier(), move(file_system));
  53. auto root_inode_id = mount->guest().fs()->root_inode();
  54. auto root_inode = mount->guest().fs()->get_inode(root_inode_id);
  55. if (!root_inode->is_directory()) {
  56. kprintf("VFS: root inode (%02u:%08u) for / is not a directory :(\n", root_inode_id.fsid(), root_inode_id.index());
  57. return false;
  58. }
  59. m_root_inode = move(root_inode);
  60. kprintf("VFS: mounted root on %s{%p}\n",
  61. m_root_inode->fs().class_name(),
  62. &m_root_inode->fs());
  63. m_mounts.append(move(mount));
  64. return true;
  65. }
  66. auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
  67. {
  68. for (auto& mount : m_mounts) {
  69. if (mount->host() == inode)
  70. return mount.ptr();
  71. }
  72. return nullptr;
  73. }
  74. auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
  75. {
  76. for (auto& mount : m_mounts) {
  77. if (mount->guest() == inode)
  78. return mount.ptr();
  79. }
  80. return nullptr;
  81. }
  82. bool VFS::is_vfs_root(InodeIdentifier inode) const
  83. {
  84. return inode == root_inode_id();
  85. }
  86. void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
  87. {
  88. dir_inode.traverse_as_directory([&] (const FS::DirectoryEntry& entry) {
  89. InodeIdentifier resolved_inode;
  90. if (auto mount = find_mount_for_host(entry.inode))
  91. resolved_inode = mount->guest();
  92. else
  93. resolved_inode = entry.inode;
  94. if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
  95. auto mount = find_mount_for_guest(entry.inode);
  96. ASSERT(mount);
  97. resolved_inode = mount->host();
  98. }
  99. callback(FS::DirectoryEntry(entry.name, entry.name_length, resolved_inode, entry.file_type));
  100. return true;
  101. });
  102. }
  103. KResult VFS::utime(StringView path, Inode& base, time_t atime, time_t mtime)
  104. {
  105. auto descriptor_or_error = VFS::the().open(move(path), 0, 0, base);
  106. if (descriptor_or_error.is_error())
  107. return descriptor_or_error.error();
  108. auto& inode = *descriptor_or_error.value()->inode();
  109. if (inode.fs().is_readonly())
  110. return KResult(-EROFS);
  111. if (inode.metadata().uid != current->process().euid())
  112. return KResult(-EACCES);
  113. int error = inode.set_atime(atime);
  114. if (error)
  115. return KResult(error);
  116. error = inode.set_mtime(mtime);
  117. if (error)
  118. return KResult(error);
  119. return KSuccess;
  120. }
  121. KResult VFS::stat(StringView path, int options, Inode& base, struct stat& statbuf)
  122. {
  123. auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options);
  124. if (inode_or_error.is_error())
  125. return inode_or_error.error();
  126. return FileDescriptor::create(inode_or_error.value().ptr())->fstat(statbuf);
  127. }
  128. KResultOr<Retained<FileDescriptor>> VFS::open(StringView path, int options, mode_t mode, Inode& base)
  129. {
  130. auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options);
  131. if (options & O_CREAT) {
  132. if (inode_or_error.is_error())
  133. return create(path, options, mode, base);
  134. if (options & O_EXCL)
  135. return KResult(-EEXIST);
  136. }
  137. if (inode_or_error.is_error())
  138. return inode_or_error.error();
  139. auto inode = inode_or_error.value();
  140. auto metadata = inode->metadata();
  141. bool should_truncate_file = false;
  142. // NOTE: Read permission is a bit weird, since O_RDONLY == 0,
  143. // so we check if (NOT write_only OR read_and_write)
  144. if (!(options & O_WRONLY) || (options & O_RDWR)) {
  145. if (!metadata.may_read(current->process()))
  146. return KResult(-EACCES);
  147. }
  148. if ((options & O_WRONLY) || (options & O_RDWR)) {
  149. if (!metadata.may_write(current->process()))
  150. return KResult(-EACCES);
  151. if (metadata.is_directory())
  152. return KResult(-EISDIR);
  153. should_truncate_file = options & O_TRUNC;
  154. }
  155. if (metadata.is_device()) {
  156. auto it = m_devices.find(encoded_device(metadata.major_device, metadata.minor_device));
  157. if (it == m_devices.end()) {
  158. return KResult(-ENODEV);
  159. }
  160. auto descriptor_or_error = (*it).value->open(options);
  161. if (descriptor_or_error.is_error())
  162. return descriptor_or_error.error();
  163. descriptor_or_error.value()->set_original_inode(Badge<VFS>(), *inode);
  164. return descriptor_or_error;
  165. }
  166. if (should_truncate_file)
  167. inode->truncate(0);
  168. return FileDescriptor::create(*inode);
  169. }
  170. KResultOr<Retained<FileDescriptor>> VFS::create(StringView path, int options, mode_t mode, Inode& base)
  171. {
  172. (void)options;
  173. if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
  174. // Turn it into a regular file. (This feels rather hackish.)
  175. mode |= 0100000;
  176. }
  177. RetainPtr<Inode> parent_inode;
  178. auto existing_file_or_error = resolve_path_to_inode(path, base, &parent_inode);
  179. if (!existing_file_or_error.is_error())
  180. return KResult(-EEXIST);
  181. if (!parent_inode)
  182. return KResult(-ENOENT);
  183. if (existing_file_or_error.error() != -ENOENT)
  184. return existing_file_or_error.error();
  185. if (!parent_inode->metadata().may_write(current->process()))
  186. return KResult(-EACCES);
  187. FileSystemPath p(path);
  188. dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index());
  189. int error;
  190. auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), mode, 0, error);
  191. if (!new_file)
  192. return KResult(error);
  193. return FileDescriptor::create(move(new_file));
  194. }
  195. KResult VFS::mkdir(StringView path, mode_t mode, Inode& base)
  196. {
  197. RetainPtr<Inode> parent_inode;
  198. auto result = resolve_path_to_inode(path, base, &parent_inode);
  199. if (!result.is_error())
  200. return KResult(-EEXIST);
  201. if (!parent_inode)
  202. return KResult(-ENOENT);
  203. if (result.error() != -ENOENT)
  204. return result.error();
  205. if (!parent_inode->metadata().may_write(current->process()))
  206. return KResult(-EACCES);
  207. FileSystemPath p(path);
  208. dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index());
  209. int error;
  210. auto new_dir = parent_inode->fs().create_directory(parent_inode->identifier(), p.basename(), mode, error);
  211. if (new_dir)
  212. return KSuccess;
  213. return KResult(error);
  214. }
  215. KResult VFS::access(StringView path, int mode, Inode& base)
  216. {
  217. auto inode_or_error = resolve_path_to_inode(path, base);
  218. if (inode_or_error.is_error())
  219. return inode_or_error.error();
  220. auto inode = inode_or_error.value();
  221. auto metadata = inode->metadata();
  222. if (mode & R_OK) {
  223. if (!metadata.may_read(current->process()))
  224. return KResult(-EACCES);
  225. }
  226. if (mode & W_OK) {
  227. if (!metadata.may_write(current->process()))
  228. return KResult(-EACCES);
  229. }
  230. if (mode & X_OK) {
  231. if (!metadata.may_execute(current->process()))
  232. return KResult(-EACCES);
  233. }
  234. return KSuccess;
  235. }
  236. KResultOr<Retained<Inode>> VFS::open_directory(StringView path, Inode& base)
  237. {
  238. auto inode_or_error = resolve_path_to_inode(path, base);
  239. if (inode_or_error.is_error())
  240. return inode_or_error.error();
  241. auto inode = inode_or_error.value();
  242. if (!inode->is_directory())
  243. return KResult(-ENOTDIR);
  244. if (!inode->metadata().may_execute(current->process()))
  245. return KResult(-EACCES);
  246. return Retained<Inode>(*inode);
  247. }
  248. KResult VFS::chmod(Inode& inode, mode_t mode)
  249. {
  250. if (inode.fs().is_readonly())
  251. return KResult(-EROFS);
  252. if (current->process().euid() != inode.metadata().uid && !current->process().is_superuser())
  253. return KResult(-EPERM);
  254. // Only change the permission bits.
  255. mode = (inode.mode() & ~04777u) | (mode & 04777u);
  256. return inode.chmod(mode);
  257. }
  258. KResult VFS::chmod(StringView path, mode_t mode, Inode& base)
  259. {
  260. auto inode_or_error = resolve_path_to_inode(path, base);
  261. if (inode_or_error.is_error())
  262. return inode_or_error.error();
  263. auto inode = inode_or_error.value();
  264. return chmod(*inode, mode);
  265. }
  266. KResult VFS::rename(StringView old_path, StringView new_path, Inode& base)
  267. {
  268. RetainPtr<Inode> old_parent_inode;
  269. auto old_inode_or_error = resolve_path_to_inode(old_path, base, &old_parent_inode);
  270. if (old_inode_or_error.is_error())
  271. return old_inode_or_error.error();
  272. auto old_inode = old_inode_or_error.value();
  273. RetainPtr<Inode> new_parent_inode;
  274. auto new_inode_or_error = resolve_path_to_inode(new_path, base, &new_parent_inode);
  275. if (new_inode_or_error.is_error()) {
  276. if (new_inode_or_error.error() != -ENOENT)
  277. return new_inode_or_error.error();
  278. }
  279. if (!new_parent_inode->metadata().may_write(current->process()))
  280. return KResult(-EACCES);
  281. if (!old_parent_inode->metadata().may_write(current->process()))
  282. return KResult(-EACCES);
  283. if (!new_inode_or_error.is_error()) {
  284. auto new_inode = new_inode_or_error.value();
  285. // FIXME: Is this really correct? Check what other systems do.
  286. if (new_inode == old_inode)
  287. return KSuccess;
  288. if (new_inode->is_directory() && !old_inode->is_directory())
  289. return KResult(-EISDIR);
  290. auto result = new_parent_inode->remove_child(FileSystemPath(new_path).basename());
  291. if (result.is_error())
  292. return result;
  293. }
  294. auto result = new_parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0 /* FIXME: file type? */);
  295. if (result.is_error())
  296. return result;
  297. result = old_parent_inode->remove_child(FileSystemPath(old_path).basename());
  298. if (result.is_error())
  299. return result;
  300. return KSuccess;
  301. }
  302. KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Inode& base)
  303. {
  304. auto inode_or_error = resolve_path_to_inode(path, base);
  305. if (inode_or_error.is_error())
  306. return inode_or_error.error();
  307. auto inode = inode_or_error.value();
  308. if (inode->fs().is_readonly())
  309. return KResult(-EROFS);
  310. if (current->process().euid() != inode->metadata().uid && !current->process().is_superuser())
  311. return KResult(-EPERM);
  312. uid_t new_uid = inode->metadata().uid;
  313. gid_t new_gid = inode->metadata().gid;
  314. if (a_uid != (uid_t)-1) {
  315. if (current->process().euid() != a_uid && !current->process().is_superuser())
  316. return KResult(-EPERM);
  317. new_uid = a_uid;
  318. }
  319. if (a_gid != (gid_t)-1) {
  320. if (!current->process().in_group(a_gid) && !current->process().is_superuser())
  321. return KResult(-EPERM);
  322. new_gid = a_gid;
  323. }
  324. dbgprintf("VFS::chown(): inode %u:%u <- uid:%d, gid:%d\n", inode->fsid(), inode->index(), new_uid, new_gid);
  325. return inode->chown(new_uid, new_gid);
  326. }
  327. KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(StringView path, Inode& base, RetainPtr<Inode>* parent_inode, int options)
  328. {
  329. // FIXME: This won't work nicely across mount boundaries.
  330. FileSystemPath p(path);
  331. if (!p.is_valid())
  332. return KResult(-EINVAL);
  333. InodeIdentifier parent_id;
  334. auto result = resolve_path(path, base.identifier(), options, &parent_id);
  335. if (parent_inode && parent_id.is_valid())
  336. *parent_inode = get_inode(parent_id);
  337. if (result.is_error())
  338. return result.error();
  339. return Retained<Inode>(*get_inode(result.value()));
  340. }
  341. KResult VFS::link(StringView old_path, StringView new_path, Inode& base)
  342. {
  343. auto old_inode_or_error = resolve_path_to_inode(old_path, base);
  344. if (old_inode_or_error.is_error())
  345. return old_inode_or_error.error();
  346. auto old_inode = old_inode_or_error.value();
  347. RetainPtr<Inode> parent_inode;
  348. auto new_inode_or_error = resolve_path_to_inode(new_path, base, &parent_inode);
  349. if (!new_inode_or_error.is_error())
  350. return KResult(-EEXIST);
  351. if (!parent_inode)
  352. return KResult(-ENOENT);
  353. if (parent_inode->fsid() != old_inode->fsid())
  354. return KResult(-EXDEV);
  355. if (parent_inode->fs().is_readonly())
  356. return KResult(-EROFS);
  357. if (!parent_inode->metadata().may_write(current->process()))
  358. return KResult(-EACCES);
  359. return parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0);
  360. }
  361. KResult VFS::unlink(StringView path, Inode& base)
  362. {
  363. RetainPtr<Inode> parent_inode;
  364. auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode);
  365. if (inode_or_error.is_error())
  366. return inode_or_error.error();
  367. auto inode = inode_or_error.value();
  368. if (inode->is_directory())
  369. return KResult(-EISDIR);
  370. if (!parent_inode->metadata().may_write(current->process()))
  371. return KResult(-EACCES);
  372. return parent_inode->remove_child(FileSystemPath(path).basename());
  373. }
  374. KResult VFS::symlink(StringView target, StringView linkpath, Inode& base)
  375. {
  376. RetainPtr<Inode> parent_inode;
  377. auto existing_file_or_error = resolve_path_to_inode(linkpath, base, &parent_inode);
  378. if (!existing_file_or_error.is_error())
  379. return KResult(-EEXIST);
  380. if (!parent_inode)
  381. return KResult(-ENOENT);
  382. if (existing_file_or_error.error() != -ENOENT)
  383. return existing_file_or_error.error();
  384. if (!parent_inode->metadata().may_write(current->process()))
  385. return KResult(-EACCES);
  386. FileSystemPath p(linkpath);
  387. dbgprintf("VFS::symlink: '%s' (-> '%s') in %u:%u\n", p.basename().characters(), target.characters(), parent_inode->fsid(), parent_inode->index());
  388. int error;
  389. auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), 0120644, 0, error);
  390. if (!new_file)
  391. return KResult(error);
  392. ssize_t nwritten = new_file->write_bytes(0, target.length(), (const byte*)target.characters(), nullptr);
  393. if (nwritten < 0)
  394. return KResult(nwritten);
  395. return KSuccess;
  396. }
  397. KResult VFS::rmdir(StringView path, Inode& base)
  398. {
  399. RetainPtr<Inode> parent_inode;
  400. auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode);
  401. if (inode_or_error.is_error())
  402. return KResult(inode_or_error.error());
  403. auto inode = inode_or_error.value();
  404. if (inode->fs().is_readonly())
  405. return KResult(-EROFS);
  406. // FIXME: We should return EINVAL if the last component of the path is "."
  407. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  408. if (!inode->is_directory())
  409. return KResult(-ENOTDIR);
  410. if (!parent_inode->metadata().may_write(current->process()))
  411. return KResult(-EACCES);
  412. if (inode->directory_entry_count() != 2)
  413. return KResult(-ENOTEMPTY);
  414. auto result = inode->remove_child(".");
  415. if (result.is_error())
  416. return result;
  417. result = inode->remove_child("..");
  418. if (result.is_error())
  419. return result;
  420. return parent_inode->remove_child(FileSystemPath(path).basename());
  421. }
  422. KResultOr<InodeIdentifier> VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode)
  423. {
  424. auto symlink_contents = symlink_inode.read_entire();
  425. if (!symlink_contents)
  426. return KResult(-ENOENT);
  427. auto linkee = StringView(symlink_contents.pointer(), symlink_contents.size());
  428. #ifdef VFS_DEBUG
  429. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  430. #endif
  431. return resolve_path(linkee, base);
  432. }
  433. RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  434. {
  435. if (!inode_id.is_valid())
  436. return nullptr;
  437. return inode_id.fs()->get_inode(inode_id);
  438. }
  439. KResultOr<String> VFS::absolute_path(InodeIdentifier inode_id)
  440. {
  441. auto inode = get_inode(inode_id);
  442. if (!inode)
  443. return KResult(-EIO);
  444. return absolute_path(*inode);
  445. }
  446. KResultOr<String> VFS::absolute_path(Inode& core_inode)
  447. {
  448. Vector<InodeIdentifier> lineage;
  449. RetainPtr<Inode> inode = &core_inode;
  450. while (inode->identifier() != root_inode_id()) {
  451. if (auto* mount = find_mount_for_guest(inode->identifier()))
  452. lineage.append(mount->host());
  453. else
  454. lineage.append(inode->identifier());
  455. InodeIdentifier parent_id;
  456. if (inode->is_directory()) {
  457. auto result = resolve_path("..", inode->identifier());
  458. if (result.is_error())
  459. return result.error();
  460. parent_id = result.value();
  461. } else {
  462. parent_id = inode->parent()->identifier();
  463. }
  464. if (!parent_id.is_valid())
  465. return KResult(-EIO);
  466. inode = get_inode(parent_id);
  467. }
  468. if (lineage.is_empty())
  469. return "/";
  470. lineage.append(root_inode_id());
  471. StringBuilder builder;
  472. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  473. auto& child = lineage[i - 1];
  474. auto parent = lineage[i];
  475. if (auto* mount = find_mount_for_host(parent))
  476. parent = mount->guest();
  477. builder.append('/');
  478. auto parent_inode = get_inode(parent);
  479. builder.append(parent_inode->reverse_lookup(child));
  480. }
  481. return builder.to_string();
  482. }
  483. KResultOr<InodeIdentifier> VFS::resolve_path(StringView path, InodeIdentifier base, int options, InodeIdentifier* parent_id)
  484. {
  485. if (path.is_empty())
  486. return KResult(-EINVAL);
  487. auto parts = path.split_view('/');
  488. InodeIdentifier crumb_id;
  489. if (path[0] == '/')
  490. crumb_id = root_inode_id();
  491. else
  492. crumb_id = base.is_valid() ? base : root_inode_id();
  493. if (parent_id)
  494. *parent_id = crumb_id;
  495. for (int i = 0; i < parts.size(); ++i) {
  496. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  497. auto& part = parts[i];
  498. if (part.is_empty())
  499. break;
  500. auto crumb_inode = get_inode(crumb_id);
  501. if (!crumb_inode) {
  502. #ifdef VFS_DEBUG
  503. kprintf("invalid metadata\n");
  504. #endif
  505. return KResult(-EIO);
  506. }
  507. auto metadata = crumb_inode->metadata();
  508. if (!metadata.is_directory()) {
  509. #ifdef VFS_DEBUG
  510. kprintf("parent of <%s> not directory, it's inode %u:%u / %u:%u, mode: %u, size: %u\n", part.characters(), crumb_id.fsid(), crumb_id.index(), metadata.inode.fsid(), metadata.inode.index(), metadata.mode, metadata.size);
  511. #endif
  512. return KResult(-ENOTDIR);
  513. }
  514. if (!metadata.may_execute(current->process()))
  515. return KResult(-EACCES);
  516. auto parent = crumb_id;
  517. crumb_id = crumb_inode->lookup(part);
  518. if (!crumb_id.is_valid()) {
  519. #ifdef VFS_DEBUG
  520. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  521. #endif
  522. return KResult(-ENOENT);
  523. }
  524. #ifdef VFS_DEBUG
  525. kprintf("<%s> %u:%u\n", part.characters(), crumb_id.fsid(), crumb_id.index());
  526. #endif
  527. if (auto mount = find_mount_for_host(crumb_id)) {
  528. #ifdef VFS_DEBUG
  529. kprintf(" -- is host\n");
  530. #endif
  531. crumb_id = mount->guest();
  532. }
  533. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  534. #ifdef VFS_DEBUG
  535. kprintf(" -- is guest\n");
  536. #endif
  537. auto mount = find_mount_for_guest(crumb_id);
  538. auto dir_inode = get_inode(mount->host());
  539. ASSERT(dir_inode);
  540. crumb_id = dir_inode->lookup("..");
  541. }
  542. crumb_inode = get_inode(crumb_id);
  543. ASSERT(crumb_inode);
  544. metadata = crumb_inode->metadata();
  545. if (metadata.is_directory()) {
  546. if (i != parts.size() - 1) {
  547. if (parent_id)
  548. *parent_id = crumb_id;
  549. }
  550. }
  551. if (metadata.is_symlink()) {
  552. if (i == parts.size() - 1) {
  553. if (options & O_NOFOLLOW)
  554. return KResult(-ELOOP);
  555. if (options & O_NOFOLLOW_NOERROR)
  556. return crumb_id;
  557. }
  558. auto result = resolve_symbolic_link(parent, *crumb_inode);
  559. if (result.is_error())
  560. return KResult(-ENOENT);
  561. crumb_id = result.value();
  562. ASSERT(crumb_id.is_valid());
  563. }
  564. }
  565. return crumb_id;
  566. }
  567. InodeIdentifier VFS::old_resolve_path(StringView path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
  568. {
  569. auto result = resolve_path(path, base, options, parent_id);
  570. if (result.is_error()) {
  571. error = result.error();
  572. return { };
  573. }
  574. return result.value();
  575. }
  576. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  577. : m_host(host)
  578. , m_guest(guest_fs->root_inode())
  579. , m_guest_fs(move(guest_fs))
  580. {
  581. }
  582. void VFS::register_device(Device& device)
  583. {
  584. m_devices.set(encoded_device(device.major(), device.minor()), &device);
  585. }
  586. void VFS::unregister_device(Device& device)
  587. {
  588. m_devices.remove(encoded_device(device.major(), device.minor()));
  589. }
  590. Device* VFS::get_device(unsigned major, unsigned minor)
  591. {
  592. auto it = m_devices.find(encoded_device(major, minor));
  593. if (it == m_devices.end())
  594. return nullptr;
  595. return (*it).value;
  596. }
  597. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  598. {
  599. for (auto& mount : m_mounts) {
  600. callback(*mount);
  601. }
  602. }
  603. void VFS::sync()
  604. {
  605. FS::sync();
  606. }