VirtualFileSystem.cpp 23 KB

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