VirtualFileSystem.cpp 25 KB

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