VirtualFileSystem.cpp 25 KB

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