VirtualFileSystem.cpp 24 KB

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