VirtualFileSystem.cpp 20 KB

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