VirtualFileSystem.cpp 25 KB

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