VirtualFileSystem.cpp 24 KB

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