VirtualFileSystem.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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, int flags)
  35. {
  36. auto& inode = mount_point.inode();
  37. dbg() << "VFS: Mounting " << file_system.class_name() << " at " << mount_point.absolute_path() << " (inode: " << inode.identifier() << ") with flags " << flags;
  38. // FIXME: check that this is not already a mount point
  39. Mount mount { file_system, &mount_point, flags };
  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, 0 };
  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 (options & O_EXEC) {
  190. if (!metadata.may_execute(current->process()) || (custody.mount_flags() & MS_NOEXEC))
  191. return KResult(-EACCES);
  192. }
  193. if (metadata.is_device()) {
  194. if (custody.mount_flags() & MS_NODEV)
  195. return KResult(-EACCES);
  196. auto device = Device::get_device(metadata.major_device, metadata.minor_device);
  197. if (device == nullptr) {
  198. return KResult(-ENODEV);
  199. }
  200. auto descriptor_or_error = device->open(options);
  201. if (descriptor_or_error.is_error())
  202. return descriptor_or_error.error();
  203. descriptor_or_error.value()->set_original_inode({}, inode);
  204. return descriptor_or_error;
  205. }
  206. if (should_truncate_file) {
  207. inode.truncate(0);
  208. inode.set_mtime(kgettimeofday().tv_sec);
  209. }
  210. return FileDescription::create(custody);
  211. }
  212. KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
  213. {
  214. if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode))
  215. return KResult(-EINVAL);
  216. RefPtr<Custody> parent_custody;
  217. auto existing_file_or_error = resolve_path(path, base, &parent_custody);
  218. if (!existing_file_or_error.is_error())
  219. return KResult(-EEXIST);
  220. if (!parent_custody)
  221. return KResult(-ENOENT);
  222. if (existing_file_or_error.error() != -ENOENT)
  223. return existing_file_or_error.error();
  224. auto& parent_inode = parent_custody->inode();
  225. if (!parent_inode.metadata().may_write(current->process()))
  226. return KResult(-EACCES);
  227. FileSystemPath p(path);
  228. dbg() << "VFS::mknod: '" << p.basename() << "' mode=" << mode << " dev=" << dev << " in " << parent_inode.identifier();
  229. int error;
  230. auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, dev, current->process().uid(), current->process().gid(), error);
  231. if (!new_file)
  232. return KResult(error);
  233. return KSuccess;
  234. }
  235. KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> owner)
  236. {
  237. (void)options;
  238. if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
  239. // Turn it into a regular file. (This feels rather hackish.)
  240. mode |= 0100000;
  241. }
  242. auto& parent_inode = parent_custody.inode();
  243. if (!parent_inode.metadata().may_write(current->process()))
  244. return KResult(-EACCES);
  245. FileSystemPath p(path);
  246. dbg() << "VFS::create: '" << p.basename() << "' in " << parent_inode.identifier();
  247. int error;
  248. uid_t uid = owner.has_value() ? owner.value().uid : current->process().uid();
  249. gid_t gid = owner.has_value() ? owner.value().gid : current->process().gid();
  250. auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, 0, uid, gid, error);
  251. if (!new_file)
  252. return KResult(error);
  253. auto new_custody = Custody::create(&parent_custody, p.basename(), *new_file, parent_custody.mount_flags());
  254. return FileDescription::create(*new_custody);
  255. }
  256. KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
  257. {
  258. RefPtr<Custody> parent_custody;
  259. auto result = resolve_path(path, base, &parent_custody);
  260. if (!result.is_error())
  261. return KResult(-EEXIST);
  262. if (!parent_custody)
  263. return KResult(-ENOENT);
  264. if (result.error() != -ENOENT)
  265. return result.error();
  266. auto& parent_inode = parent_custody->inode();
  267. if (!parent_inode.metadata().may_write(current->process()))
  268. return KResult(-EACCES);
  269. FileSystemPath p(path);
  270. dbg() << "VFS::mkdir: '" << p.basename() << "' in " << parent_inode.identifier();
  271. int error;
  272. auto new_dir = parent_inode.fs().create_directory(parent_inode.identifier(), p.basename(), mode, current->process().uid(), current->process().gid(), error);
  273. if (new_dir)
  274. return KSuccess;
  275. return KResult(error);
  276. }
  277. KResult VFS::access(StringView path, int mode, Custody& base)
  278. {
  279. auto custody_or_error = resolve_path(path, base);
  280. if (custody_or_error.is_error())
  281. return custody_or_error.error();
  282. auto& custody = *custody_or_error.value();
  283. auto& inode = custody.inode();
  284. auto metadata = inode.metadata();
  285. if (mode & R_OK) {
  286. if (!metadata.may_read(current->process()))
  287. return KResult(-EACCES);
  288. }
  289. if (mode & W_OK) {
  290. if (!metadata.may_write(current->process()))
  291. return KResult(-EACCES);
  292. }
  293. if (mode & X_OK) {
  294. if (!metadata.may_execute(current->process()))
  295. return KResult(-EACCES);
  296. }
  297. return KSuccess;
  298. }
  299. KResultOr<NonnullRefPtr<Custody>> VFS::open_directory(StringView path, Custody& base)
  300. {
  301. auto inode_or_error = resolve_path(path, base);
  302. if (inode_or_error.is_error())
  303. return inode_or_error.error();
  304. auto& custody = *inode_or_error.value();
  305. auto& inode = custody.inode();
  306. if (!inode.is_directory())
  307. return KResult(-ENOTDIR);
  308. if (!inode.metadata().may_execute(current->process()))
  309. return KResult(-EACCES);
  310. return custody;
  311. }
  312. KResult VFS::chmod(Inode& inode, mode_t mode)
  313. {
  314. if (inode.fs().is_readonly())
  315. return KResult(-EROFS);
  316. if (current->process().euid() != inode.metadata().uid && !current->process().is_superuser())
  317. return KResult(-EPERM);
  318. // Only change the permission bits.
  319. mode = (inode.mode() & ~04777u) | (mode & 04777u);
  320. return inode.chmod(mode);
  321. }
  322. KResult VFS::chmod(StringView path, mode_t mode, Custody& base)
  323. {
  324. auto custody_or_error = resolve_path(path, base);
  325. if (custody_or_error.is_error())
  326. return custody_or_error.error();
  327. auto& custody = *custody_or_error.value();
  328. auto& inode = custody.inode();
  329. return chmod(inode, mode);
  330. }
  331. KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
  332. {
  333. RefPtr<Custody> old_parent_custody;
  334. auto old_custody_or_error = resolve_path(old_path, base, &old_parent_custody);
  335. if (old_custody_or_error.is_error())
  336. return old_custody_or_error.error();
  337. auto& old_custody = *old_custody_or_error.value();
  338. auto& old_inode = old_custody.inode();
  339. RefPtr<Custody> new_parent_custody;
  340. auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody);
  341. if (new_custody_or_error.is_error()) {
  342. if (new_custody_or_error.error() != -ENOENT)
  343. return new_custody_or_error.error();
  344. }
  345. auto& old_parent_inode = old_parent_custody->inode();
  346. auto& new_parent_inode = new_parent_custody->inode();
  347. if (&old_parent_inode.fs() != &new_parent_inode.fs())
  348. return KResult(-EXDEV);
  349. if (!new_parent_inode.metadata().may_write(current->process()))
  350. return KResult(-EACCES);
  351. if (!old_parent_inode.metadata().may_write(current->process()))
  352. return KResult(-EACCES);
  353. if (old_parent_inode.metadata().is_sticky()) {
  354. if (!current->process().is_superuser() && old_inode.metadata().uid != current->process().euid())
  355. return KResult(-EACCES);
  356. }
  357. auto new_basename = FileSystemPath(new_path).basename();
  358. if (!new_custody_or_error.is_error()) {
  359. auto& new_custody = *new_custody_or_error.value();
  360. auto& new_inode = new_custody.inode();
  361. // FIXME: Is this really correct? Check what other systems do.
  362. if (&new_inode == &old_inode)
  363. return KSuccess;
  364. if (new_parent_inode.metadata().is_sticky()) {
  365. if (!current->process().is_superuser() && new_inode.metadata().uid != current->process().euid())
  366. return KResult(-EACCES);
  367. }
  368. if (new_inode.is_directory() && !old_inode.is_directory())
  369. return KResult(-EISDIR);
  370. auto result = new_parent_inode.remove_child(new_basename);
  371. if (result.is_error())
  372. return result;
  373. new_custody.did_delete({});
  374. }
  375. auto result = new_parent_inode.add_child(old_inode.identifier(), new_basename, old_inode.mode());
  376. if (result.is_error())
  377. return result;
  378. result = old_parent_inode.remove_child(FileSystemPath(old_path).basename());
  379. if (result.is_error())
  380. return result;
  381. old_custody.did_rename({}, new_basename);
  382. return KSuccess;
  383. }
  384. KResult VFS::chown(Inode& inode, uid_t a_uid, gid_t a_gid)
  385. {
  386. if (inode.fs().is_readonly())
  387. return KResult(-EROFS);
  388. auto metadata = inode.metadata();
  389. if (current->process().euid() != metadata.uid && !current->process().is_superuser())
  390. return KResult(-EPERM);
  391. uid_t new_uid = metadata.uid;
  392. gid_t new_gid = metadata.gid;
  393. if (a_uid != (uid_t)-1) {
  394. if (current->process().euid() != a_uid && !current->process().is_superuser())
  395. return KResult(-EPERM);
  396. new_uid = a_uid;
  397. }
  398. if (a_gid != (gid_t)-1) {
  399. if (!current->process().in_group(a_gid) && !current->process().is_superuser())
  400. return KResult(-EPERM);
  401. new_gid = a_gid;
  402. }
  403. dbg() << "VFS::chown(): inode " << inode.identifier() << " <- uid:" << new_uid << " gid:" << new_gid;
  404. return inode.chown(new_uid, new_gid);
  405. }
  406. KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Custody& base)
  407. {
  408. auto custody_or_error = resolve_path(path, base);
  409. if (custody_or_error.is_error())
  410. return custody_or_error.error();
  411. auto& custody = *custody_or_error.value();
  412. auto& inode = custody.inode();
  413. return chown(inode, a_uid, a_gid);
  414. }
  415. KResult VFS::link(StringView old_path, StringView new_path, Custody& base)
  416. {
  417. auto old_custody_or_error = resolve_path(old_path, base);
  418. if (old_custody_or_error.is_error())
  419. return old_custody_or_error.error();
  420. auto& old_custody = *old_custody_or_error.value();
  421. auto& old_inode = old_custody.inode();
  422. RefPtr<Custody> parent_custody;
  423. auto new_custody_or_error = resolve_path(new_path, base, &parent_custody);
  424. if (!new_custody_or_error.is_error())
  425. return KResult(-EEXIST);
  426. if (!parent_custody)
  427. return KResult(-ENOENT);
  428. auto& parent_inode = parent_custody->inode();
  429. if (parent_inode.fsid() != old_inode.fsid())
  430. return KResult(-EXDEV);
  431. if (parent_inode.fs().is_readonly())
  432. return KResult(-EROFS);
  433. if (!parent_inode.metadata().may_write(current->process()))
  434. return KResult(-EACCES);
  435. return parent_inode.add_child(old_inode.identifier(), FileSystemPath(new_path).basename(), old_inode.mode());
  436. }
  437. KResult VFS::unlink(StringView path, Custody& base)
  438. {
  439. RefPtr<Custody> parent_custody;
  440. auto custody_or_error = resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR);
  441. if (custody_or_error.is_error())
  442. return custody_or_error.error();
  443. auto& custody = *custody_or_error.value();
  444. auto& inode = custody.inode();
  445. if (inode.is_directory())
  446. return KResult(-EISDIR);
  447. auto& parent_inode = parent_custody->inode();
  448. if (!parent_inode.metadata().may_write(current->process()))
  449. return KResult(-EACCES);
  450. if (parent_inode.metadata().is_sticky()) {
  451. if (!current->process().is_superuser() && inode.metadata().uid != current->process().euid())
  452. return KResult(-EACCES);
  453. }
  454. auto result = parent_inode.remove_child(FileSystemPath(path).basename());
  455. if (result.is_error())
  456. return result;
  457. custody.did_delete({});
  458. return KSuccess;
  459. }
  460. KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
  461. {
  462. RefPtr<Custody> parent_custody;
  463. auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
  464. if (!existing_custody_or_error.is_error())
  465. return KResult(-EEXIST);
  466. if (!parent_custody)
  467. return KResult(-ENOENT);
  468. if (existing_custody_or_error.error() != -ENOENT)
  469. return existing_custody_or_error.error();
  470. auto& parent_inode = parent_custody->inode();
  471. if (!parent_inode.metadata().may_write(current->process()))
  472. return KResult(-EACCES);
  473. FileSystemPath p(linkpath);
  474. dbg() << "VFS::symlink: '" << p.basename() << "' (-> '" << target << "') in " << parent_inode.identifier();
  475. int error;
  476. auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), 0120644, 0, 0, current->process().uid(), current->process().gid(), error);
  477. if (!new_file)
  478. return KResult(error);
  479. ssize_t nwritten = new_file->write_bytes(0, target.length(), (const u8*)target.characters_without_null_termination(), nullptr);
  480. if (nwritten < 0)
  481. return KResult(nwritten);
  482. return KSuccess;
  483. }
  484. KResult VFS::rmdir(StringView path, Custody& base)
  485. {
  486. RefPtr<Custody> parent_custody;
  487. auto custody_or_error = resolve_path(path, base, &parent_custody);
  488. if (custody_or_error.is_error())
  489. return KResult(custody_or_error.error());
  490. auto& custody = *custody_or_error.value();
  491. auto& inode = custody.inode();
  492. if (inode.fs().is_readonly())
  493. return KResult(-EROFS);
  494. // FIXME: We should return EINVAL if the last component of the path is "."
  495. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  496. if (!inode.is_directory())
  497. return KResult(-ENOTDIR);
  498. auto& parent_inode = parent_custody->inode();
  499. if (!parent_inode.metadata().may_write(current->process()))
  500. return KResult(-EACCES);
  501. if (inode.directory_entry_count() != 2)
  502. return KResult(-ENOTEMPTY);
  503. auto result = inode.remove_child(".");
  504. if (result.is_error())
  505. return result;
  506. result = inode.remove_child("..");
  507. if (result.is_error())
  508. return result;
  509. return parent_inode.remove_child(FileSystemPath(path).basename());
  510. }
  511. RefPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  512. {
  513. if (!inode_id.is_valid())
  514. return nullptr;
  515. return inode_id.fs()->get_inode(inode_id);
  516. }
  517. VFS::Mount::Mount(FS& guest_fs, Custody* host_custody, int flags)
  518. : m_guest(guest_fs.root_inode())
  519. , m_guest_fs(guest_fs)
  520. , m_host_custody(host_custody)
  521. , m_flags(flags)
  522. {
  523. }
  524. String VFS::Mount::absolute_path() const
  525. {
  526. if (!m_host_custody)
  527. return "/";
  528. return m_host_custody->absolute_path();
  529. }
  530. InodeIdentifier VFS::Mount::host() const
  531. {
  532. if (!m_host_custody)
  533. return {};
  534. return m_host_custody->inode().identifier();
  535. }
  536. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  537. {
  538. for (auto& mount : m_mounts) {
  539. callback(mount);
  540. }
  541. }
  542. void VFS::sync()
  543. {
  544. FS::sync();
  545. }
  546. Custody& VFS::root_custody()
  547. {
  548. if (!m_root_custody)
  549. m_root_custody = Custody::create(nullptr, "", *m_root_inode, 0);
  550. return *m_root_custody;
  551. }
  552. KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path(StringView path, Custody& base, RefPtr<Custody>* parent_custody, int options, int symlink_recursion_level)
  553. {
  554. // FIXME: resolve_path currently doesn't deal with .. and . . If path is ../. and base is /home/anon, it returns
  555. // /home/anon/../. instead of /home .
  556. if (symlink_recursion_level >= symlink_recursion_limit)
  557. return KResult(-ELOOP);
  558. if (path.is_empty())
  559. return KResult(-EINVAL);
  560. auto parts = path.split_view('/', true);
  561. InodeIdentifier crumb_id;
  562. auto& current_root = current->process().root_directory();
  563. NonnullRefPtrVector<Custody, 32> custody_chain;
  564. if (path[0] == '/') {
  565. custody_chain.append(current_root);
  566. crumb_id = current_root.inode().identifier();
  567. } else {
  568. for (auto* custody = &base; custody; custody = custody->parent()) {
  569. // FIXME: Prepending here is not efficient! Fix this.
  570. custody_chain.prepend(*custody);
  571. }
  572. crumb_id = base.inode().identifier();
  573. }
  574. if (parent_custody)
  575. *parent_custody = custody_chain.last();
  576. int mount_flags = custody_chain.last().mount_flags();
  577. for (int i = 0; i < parts.size(); ++i) {
  578. bool inode_was_root_at_head_of_loop = current_root.inode().identifier() == crumb_id;
  579. auto crumb_inode = get_inode(crumb_id);
  580. if (!crumb_inode)
  581. return KResult(-EIO);
  582. auto metadata = crumb_inode->metadata();
  583. if (!metadata.is_directory())
  584. return KResult(-ENOTDIR);
  585. if (!metadata.may_execute(current->process()))
  586. return KResult(-EACCES);
  587. auto& part = parts[i];
  588. if (part.is_empty())
  589. continue;
  590. auto& current_parent = custody_chain.last();
  591. crumb_id = crumb_inode->lookup(part);
  592. if (!crumb_id.is_valid()) {
  593. if (i != parts.size() - 1) {
  594. // We didn't find the filename we were looking for,
  595. // and we didn't even reach the last path part.
  596. // (ENOENT with non-null parent_custody) signals to caller that
  597. // we found the immediate parent of the file, but the file itself
  598. // does not exist yet.
  599. // Since this is not the immediate parent, clear parent_custody.
  600. if (parent_custody)
  601. *parent_custody = nullptr;
  602. }
  603. return KResult(-ENOENT);
  604. }
  605. if (auto mount = find_mount_for_host(crumb_id)) {
  606. crumb_id = mount->guest();
  607. mount_flags = mount->flags();
  608. }
  609. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && crumb_id != current_root.inode().identifier() && part == "..") {
  610. auto mount = find_mount_for_guest(crumb_id);
  611. auto dir_inode = get_inode(mount->host());
  612. ASSERT(dir_inode);
  613. crumb_id = dir_inode->lookup("..");
  614. }
  615. crumb_inode = get_inode(crumb_id);
  616. ASSERT(crumb_inode);
  617. custody_chain.append(Custody::create(&custody_chain.last(), part, *crumb_inode, mount_flags));
  618. metadata = crumb_inode->metadata();
  619. if (metadata.is_directory()) {
  620. if (i != parts.size() - 1) {
  621. if (parent_custody)
  622. *parent_custody = custody_chain.last();
  623. }
  624. }
  625. if (metadata.is_symlink()) {
  626. if (i == parts.size() - 1) {
  627. if (options & O_NOFOLLOW)
  628. return KResult(-ELOOP);
  629. if (options & O_NOFOLLOW_NOERROR)
  630. return custody_chain.last();
  631. }
  632. auto symlink_contents = crumb_inode->read_entire();
  633. if (!symlink_contents)
  634. return KResult(-ENOENT);
  635. auto symlink_path = StringView(symlink_contents.data(), symlink_contents.size());
  636. auto symlink_target = resolve_path(symlink_path, current_parent, parent_custody, options, symlink_recursion_level + 1);
  637. if (symlink_target.is_error())
  638. return symlink_target;
  639. bool have_more_parts = i + 1 < parts.size();
  640. if (i + 1 == parts.size() - 1 && parts[i + 1].is_empty())
  641. have_more_parts = false;
  642. if (!have_more_parts)
  643. return symlink_target;
  644. StringView remaining_path = path.substring_view_starting_from_substring(parts[i + 1]);
  645. return resolve_path(remaining_path, *symlink_target.value(), parent_custody, options, symlink_recursion_level + 1);
  646. }
  647. }
  648. return custody_chain.last();
  649. }