VirtualFileSystem.cpp 21 KB

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