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