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