VirtualFileSystem.cpp 23 KB

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