VirtualFileSystem.cpp 26 KB

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