VirtualFileSystem.cpp 21 KB

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