VirtualFileSystem.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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.unstable_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. if (old_inode.is_directory())
  449. return KResult(-EPERM);
  450. return parent_inode.add_child(old_inode.identifier(), FileSystemPath(new_path).basename(), old_inode.mode());
  451. }
  452. KResult VFS::unlink(StringView path, Custody& base)
  453. {
  454. RefPtr<Custody> parent_custody;
  455. auto custody_or_error = resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR);
  456. if (custody_or_error.is_error())
  457. return custody_or_error.error();
  458. auto& custody = *custody_or_error.value();
  459. auto& inode = custody.inode();
  460. if (inode.is_directory())
  461. return KResult(-EISDIR);
  462. auto& parent_inode = parent_custody->inode();
  463. if (!parent_inode.metadata().may_write(current->process()))
  464. return KResult(-EACCES);
  465. if (parent_inode.metadata().is_sticky()) {
  466. if (!current->process().is_superuser() && inode.metadata().uid != current->process().euid())
  467. return KResult(-EACCES);
  468. }
  469. auto result = parent_inode.remove_child(FileSystemPath(path).basename());
  470. if (result.is_error())
  471. return result;
  472. custody.did_delete({});
  473. return KSuccess;
  474. }
  475. KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
  476. {
  477. RefPtr<Custody> parent_custody;
  478. auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
  479. if (!existing_custody_or_error.is_error())
  480. return KResult(-EEXIST);
  481. if (!parent_custody)
  482. return KResult(-ENOENT);
  483. if (existing_custody_or_error.error() != -ENOENT)
  484. return existing_custody_or_error.error();
  485. auto& parent_inode = parent_custody->inode();
  486. if (!parent_inode.metadata().may_write(current->process()))
  487. return KResult(-EACCES);
  488. FileSystemPath p(linkpath);
  489. dbg() << "VFS::symlink: '" << p.basename() << "' (-> '" << target << "') in " << parent_inode.identifier();
  490. int error;
  491. auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), 0120644, 0, 0, current->process().uid(), current->process().gid(), error);
  492. if (!new_file)
  493. return KResult(error);
  494. ssize_t nwritten = new_file->write_bytes(0, target.length(), (const u8*)target.characters_without_null_termination(), nullptr);
  495. if (nwritten < 0)
  496. return KResult(nwritten);
  497. return KSuccess;
  498. }
  499. KResult VFS::rmdir(StringView path, Custody& base)
  500. {
  501. RefPtr<Custody> parent_custody;
  502. auto custody_or_error = resolve_path(path, base, &parent_custody);
  503. if (custody_or_error.is_error())
  504. return KResult(custody_or_error.error());
  505. auto& custody = *custody_or_error.value();
  506. auto& inode = custody.inode();
  507. if (inode.fs().is_readonly())
  508. return KResult(-EROFS);
  509. // FIXME: We should return EINVAL if the last component of the path is "."
  510. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  511. if (!inode.is_directory())
  512. return KResult(-ENOTDIR);
  513. auto& parent_inode = parent_custody->inode();
  514. if (!parent_inode.metadata().may_write(current->process()))
  515. return KResult(-EACCES);
  516. if (inode.directory_entry_count() != 2)
  517. return KResult(-ENOTEMPTY);
  518. auto result = inode.remove_child(".");
  519. if (result.is_error())
  520. return result;
  521. result = inode.remove_child("..");
  522. if (result.is_error())
  523. return result;
  524. return parent_inode.remove_child(FileSystemPath(path).basename());
  525. }
  526. RefPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  527. {
  528. if (!inode_id.is_valid())
  529. return nullptr;
  530. return inode_id.fs()->get_inode(inode_id);
  531. }
  532. VFS::Mount::Mount(FS& guest_fs, Custody* host_custody, int flags)
  533. : m_guest(guest_fs.root_inode())
  534. , m_guest_fs(guest_fs)
  535. , m_host_custody(host_custody)
  536. , m_flags(flags)
  537. {
  538. }
  539. VFS::Mount::Mount(Inode& source, Custody& host_custody, int flags)
  540. : m_guest(source.identifier())
  541. , m_guest_fs(source.fs())
  542. , m_host_custody(host_custody)
  543. , m_flags(flags)
  544. {
  545. }
  546. String VFS::Mount::absolute_path() const
  547. {
  548. if (!m_host_custody)
  549. return "/";
  550. return m_host_custody->absolute_path();
  551. }
  552. InodeIdentifier VFS::Mount::host() const
  553. {
  554. if (!m_host_custody)
  555. return {};
  556. return m_host_custody->inode().identifier();
  557. }
  558. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  559. {
  560. for (auto& mount : m_mounts) {
  561. callback(mount);
  562. }
  563. }
  564. void VFS::sync()
  565. {
  566. FS::sync();
  567. }
  568. Custody& VFS::root_custody()
  569. {
  570. if (!m_root_custody)
  571. m_root_custody = Custody::create(nullptr, "", *m_root_inode, MS_NODEV | MS_NOSUID);
  572. return *m_root_custody;
  573. }
  574. KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
  575. {
  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. auto& current_root = current->process().root_directory();
  582. NonnullRefPtr<Custody> custody = path[0] == '/' ? current_root : base;
  583. for (int i = 0; i < parts.size(); ++i) {
  584. Custody& parent = custody;
  585. auto parent_metadata = parent.inode().metadata();
  586. if (!parent_metadata.is_directory())
  587. return KResult(-ENOTDIR);
  588. // Ensure the current user is allowed to resolve paths inside this directory.
  589. if (!parent_metadata.may_execute(current->process()))
  590. return KResult(-EACCES);
  591. auto& part = parts[i];
  592. bool have_more_parts = i + 1 < parts.size();
  593. if (part == "..") {
  594. // If we encounter a "..", take a step back, but don't go beyond the root.
  595. if (custody->parent())
  596. custody = *custody->parent();
  597. continue;
  598. } else if (part == "." || part.is_empty()) {
  599. continue;
  600. }
  601. // Okay, let's look up this part.
  602. auto child_id = parent.inode().lookup(part);
  603. if (!child_id.is_valid()) {
  604. if (out_parent) {
  605. // ENOENT with a non-null parent custody signals to caller that
  606. // we found the immediate parent of the file, but the file itself
  607. // does not exist yet.
  608. *out_parent = have_more_parts ? nullptr : &parent;
  609. }
  610. return KResult(-ENOENT);
  611. }
  612. int mount_flags_for_child = parent.mount_flags();
  613. // See if there's something mounted on the child; in that case
  614. // we would need to return the guest inode, not the host inode.
  615. if (auto mount = find_mount_for_host(child_id)) {
  616. child_id = mount->guest();
  617. mount_flags_for_child = mount->flags();
  618. }
  619. auto child_inode = get_inode(child_id);
  620. ASSERT(child_inode);
  621. custody = Custody::create(&parent, part, *child_inode, mount_flags_for_child);
  622. if (child_inode->metadata().is_symlink()) {
  623. if (!have_more_parts) {
  624. if (options & O_NOFOLLOW)
  625. return KResult(-ELOOP);
  626. if (options & O_NOFOLLOW_NOERROR)
  627. break;
  628. }
  629. auto symlink_target = child_inode->resolve_as_link(parent, out_parent, options, symlink_recursion_level + 1);
  630. if (symlink_target.is_error() || !have_more_parts)
  631. return symlink_target;
  632. // Now, resolve the remaining path relative to the symlink target.
  633. // We prepend a "." to it to ensure that it's not empty and that
  634. // any initial slashes it might have get interpreted properly.
  635. StringBuilder remaining_path;
  636. remaining_path.append('.');
  637. remaining_path.append(path.substring_view_starting_after_substring(part));
  638. return resolve_path(remaining_path.to_string(), *symlink_target.value(), out_parent, options, symlink_recursion_level + 1);
  639. }
  640. }
  641. if (out_parent)
  642. *out_parent = custody->parent();
  643. return custody;
  644. }