VirtualFileSystem.cpp 21 KB

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