VirtualFileSystem.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/FileSystemPath.h>
  27. #include <AK/StringBuilder.h>
  28. #include <Kernel/Devices/CharacterDevice.h>
  29. #include <Kernel/FileSystem/Custody.h>
  30. #include <Kernel/FileSystem/DiskBackedFileSystem.h>
  31. #include <Kernel/FileSystem/FileDescription.h>
  32. #include <Kernel/FileSystem/FileSystem.h>
  33. #include <Kernel/FileSystem/VirtualFileSystem.h>
  34. #include <Kernel/Process.h>
  35. #include <LibC/errno_numbers.h>
  36. //#define VFS_DEBUG
  37. static VFS* s_the;
  38. static constexpr int symlink_recursion_limit { 5 }; // FIXME: increase?
  39. VFS& VFS::the()
  40. {
  41. ASSERT(s_the);
  42. return *s_the;
  43. }
  44. VFS::VFS()
  45. {
  46. #ifdef VFS_DEBUG
  47. kprintf("VFS: Constructing VFS\n");
  48. #endif
  49. s_the = this;
  50. }
  51. VFS::~VFS()
  52. {
  53. }
  54. InodeIdentifier VFS::root_inode_id() const
  55. {
  56. ASSERT(m_root_inode);
  57. return m_root_inode->identifier();
  58. }
  59. KResult VFS::mount(FS& file_system, Custody& mount_point, int flags)
  60. {
  61. auto& inode = mount_point.inode();
  62. dbg() << "VFS: Mounting " << file_system.class_name() << " at " << mount_point.absolute_path() << " (inode: " << inode.identifier() << ") with flags " << flags;
  63. // FIXME: check that this is not already a mount point
  64. Mount mount { file_system, &mount_point, flags };
  65. m_mounts.append(move(mount));
  66. mount_point.did_mount_on({});
  67. return KSuccess;
  68. }
  69. KResult VFS::bind_mount(Custody& source, Custody& mount_point, int flags)
  70. {
  71. dbg() << "VFS: Bind-mounting " << source.absolute_path() << " at " << mount_point.absolute_path();
  72. // FIXME: check that this is not already a mount point
  73. Mount mount { source.inode(), mount_point, flags };
  74. m_mounts.append(move(mount));
  75. mount_point.did_mount_on({});
  76. return KSuccess;
  77. }
  78. KResult VFS::unmount(InodeIdentifier guest_inode_id)
  79. {
  80. LOCKER(m_lock);
  81. dbg() << "VFS: unmount called with inode " << guest_inode_id;
  82. for (int i = 0; i < m_mounts.size(); ++i) {
  83. auto& mount = m_mounts.at(i);
  84. if (mount.guest() == guest_inode_id) {
  85. auto result = mount.guest_fs().prepare_to_unmount();
  86. if (result.is_error()) {
  87. dbg() << "VFS: Failed to unmount!";
  88. return result;
  89. }
  90. dbg() << "VFS: found fs " << mount.guest_fs().fsid() << " at mount index " << i << "! Unmounting...";
  91. m_mounts.unstable_remove(i);
  92. return KSuccess;
  93. }
  94. }
  95. dbg() << "VFS: Nothing mounted on inode " << guest_inode_id;
  96. return KResult(-ENODEV);
  97. }
  98. bool VFS::mount_root(FS& file_system)
  99. {
  100. if (m_root_inode) {
  101. kprintf("VFS: mount_root can't mount another root\n");
  102. return false;
  103. }
  104. Mount mount { file_system, nullptr, MS_NODEV | MS_NOSUID };
  105. auto root_inode_id = mount.guest().fs()->root_inode();
  106. auto root_inode = mount.guest().fs()->get_inode(root_inode_id);
  107. if (!root_inode->is_directory()) {
  108. kprintf("VFS: root inode (%02u:%08u) for / is not a directory :(\n", root_inode_id.fsid(), root_inode_id.index());
  109. return false;
  110. }
  111. m_root_inode = move(root_inode);
  112. char device_name[32];
  113. if (m_root_inode->fs().is_disk_backed()) {
  114. auto& device = static_cast<DiskBackedFS&>(m_root_inode->fs()).device();
  115. sprintf(device_name, "%d,%d", device.major(), device.minor());
  116. } else {
  117. sprintf(device_name, "not-a-disk");
  118. }
  119. kprintf("VFS: mounted root on %s (%s)\n", m_root_inode->fs().class_name(), device_name);
  120. m_mounts.append(move(mount));
  121. return true;
  122. }
  123. auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
  124. {
  125. for (auto& mount : m_mounts) {
  126. if (mount.host() == inode)
  127. return &mount;
  128. }
  129. return nullptr;
  130. }
  131. auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
  132. {
  133. for (auto& mount : m_mounts) {
  134. if (mount.guest() == inode)
  135. return &mount;
  136. }
  137. return nullptr;
  138. }
  139. bool VFS::is_vfs_root(InodeIdentifier inode) const
  140. {
  141. return inode == root_inode_id();
  142. }
  143. void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
  144. {
  145. dir_inode.traverse_as_directory([&](const FS::DirectoryEntry& entry) {
  146. InodeIdentifier resolved_inode;
  147. if (auto mount = find_mount_for_host(entry.inode))
  148. resolved_inode = mount->guest();
  149. else
  150. resolved_inode = entry.inode;
  151. // FIXME: This is now broken considering chroot and bind mounts.
  152. if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
  153. auto mount = find_mount_for_guest(entry.inode);
  154. ASSERT(mount);
  155. resolved_inode = mount->host();
  156. }
  157. callback(FS::DirectoryEntry(entry.name, entry.name_length, resolved_inode, entry.file_type));
  158. return true;
  159. });
  160. }
  161. KResult VFS::utime(StringView path, Custody& base, time_t atime, time_t mtime)
  162. {
  163. auto descriptor_or_error = VFS::the().open(move(path), 0, 0, base);
  164. if (descriptor_or_error.is_error())
  165. return descriptor_or_error.error();
  166. auto& inode = *descriptor_or_error.value()->inode();
  167. if (inode.fs().is_readonly())
  168. return KResult(-EROFS);
  169. if (!current->process().is_superuser() && inode.metadata().uid != current->process().euid())
  170. return KResult(-EACCES);
  171. int error = inode.set_atime(atime);
  172. if (error)
  173. return KResult(error);
  174. error = inode.set_mtime(mtime);
  175. if (error)
  176. return KResult(error);
  177. return KSuccess;
  178. }
  179. KResultOr<InodeMetadata> VFS::lookup_metadata(StringView path, Custody& base, int options)
  180. {
  181. auto custody_or_error = resolve_path(path, base, nullptr, options);
  182. if (custody_or_error.is_error())
  183. return custody_or_error.error();
  184. return custody_or_error.value()->inode().metadata();
  185. }
  186. KResultOr<NonnullRefPtr<FileDescription>> VFS::open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> owner)
  187. {
  188. if ((options & O_CREAT) && (options & O_DIRECTORY))
  189. return KResult(-EINVAL);
  190. RefPtr<Custody> parent_custody;
  191. auto custody_or_error = resolve_path(path, base, &parent_custody, options);
  192. if (options & O_CREAT) {
  193. if (!parent_custody)
  194. return KResult(-ENOENT);
  195. if (custody_or_error.is_error()) {
  196. if (custody_or_error.error() != -ENOENT)
  197. return custody_or_error.error();
  198. return create(path, options, mode, *parent_custody, move(owner));
  199. }
  200. if (options & O_EXCL)
  201. return KResult(-EEXIST);
  202. }
  203. if (custody_or_error.is_error())
  204. return custody_or_error.error();
  205. auto& custody = *custody_or_error.value();
  206. auto& inode = custody.inode();
  207. auto metadata = inode.metadata();
  208. if ((options & O_DIRECTORY) && !metadata.is_directory())
  209. return KResult(-ENOTDIR);
  210. bool should_truncate_file = false;
  211. // NOTE: Read permission is a bit weird, since O_RDONLY == 0,
  212. // so we check if (NOT write_only OR read_and_write)
  213. if (!(options & O_WRONLY) || (options & O_RDWR)) {
  214. if (!metadata.may_read(current->process()))
  215. return KResult(-EACCES);
  216. }
  217. if ((options & O_WRONLY) || (options & O_RDWR)) {
  218. if (!metadata.may_write(current->process()))
  219. return KResult(-EACCES);
  220. if (metadata.is_directory())
  221. return KResult(-EISDIR);
  222. should_truncate_file = options & O_TRUNC;
  223. }
  224. if (options & O_EXEC) {
  225. if (!metadata.may_execute(current->process()) || (custody.mount_flags() & MS_NOEXEC))
  226. return KResult(-EACCES);
  227. }
  228. if (auto preopen_fd = inode.preopen_fd())
  229. return *preopen_fd;
  230. if (metadata.is_device()) {
  231. if (custody.mount_flags() & MS_NODEV)
  232. return KResult(-EACCES);
  233. auto device = Device::get_device(metadata.major_device, metadata.minor_device);
  234. if (device == nullptr) {
  235. return KResult(-ENODEV);
  236. }
  237. auto descriptor_or_error = device->open(options);
  238. if (descriptor_or_error.is_error())
  239. return descriptor_or_error.error();
  240. descriptor_or_error.value()->set_original_inode({}, inode);
  241. return descriptor_or_error;
  242. }
  243. if (should_truncate_file) {
  244. inode.truncate(0);
  245. inode.set_mtime(kgettimeofday().tv_sec);
  246. }
  247. auto description = FileDescription::create(custody);
  248. description->set_rw_mode(options);
  249. description->set_file_flags(options);
  250. return description;
  251. }
  252. KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
  253. {
  254. if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode))
  255. return KResult(-EINVAL);
  256. RefPtr<Custody> parent_custody;
  257. auto existing_file_or_error = resolve_path(path, base, &parent_custody);
  258. if (!existing_file_or_error.is_error())
  259. return KResult(-EEXIST);
  260. if (!parent_custody)
  261. return KResult(-ENOENT);
  262. if (existing_file_or_error.error() != -ENOENT)
  263. return existing_file_or_error.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::mknod: '" << p.basename() << "' mode=" << mode << " dev=" << dev << " in " << parent_inode.identifier();
  269. int error;
  270. auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, dev, current->process().uid(), current->process().gid(), error);
  271. if (!new_file)
  272. return KResult(error);
  273. return KSuccess;
  274. }
  275. KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> owner)
  276. {
  277. if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
  278. // Turn it into a regular file. (This feels rather hackish.)
  279. mode |= 0100000;
  280. }
  281. auto& parent_inode = parent_custody.inode();
  282. if (!parent_inode.metadata().may_write(current->process()))
  283. return KResult(-EACCES);
  284. FileSystemPath p(path);
  285. #ifdef VFS_DEBUG
  286. dbg() << "VFS::create: '" << p.basename() << "' in " << parent_inode.identifier();
  287. #endif
  288. int error;
  289. uid_t uid = owner.has_value() ? owner.value().uid : current->process().uid();
  290. gid_t gid = owner.has_value() ? owner.value().gid : current->process().gid();
  291. auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, 0, uid, gid, error);
  292. if (!new_file)
  293. return KResult(error);
  294. auto new_custody = Custody::create(&parent_custody, p.basename(), *new_file, parent_custody.mount_flags());
  295. auto description = FileDescription::create(*new_custody);
  296. description->set_rw_mode(options);
  297. description->set_file_flags(options);
  298. return description;
  299. }
  300. KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
  301. {
  302. RefPtr<Custody> parent_custody;
  303. auto result = resolve_path(path, base, &parent_custody);
  304. if (!result.is_error())
  305. return KResult(-EEXIST);
  306. if (!parent_custody)
  307. return KResult(-ENOENT);
  308. if (result.error() != -ENOENT)
  309. return result.error();
  310. auto& parent_inode = parent_custody->inode();
  311. if (!parent_inode.metadata().may_write(current->process()))
  312. return KResult(-EACCES);
  313. FileSystemPath p(path);
  314. #ifdef VFS_DEBUG
  315. dbg() << "VFS::mkdir: '" << p.basename() << "' in " << parent_inode.identifier();
  316. #endif
  317. int error;
  318. auto new_dir = parent_inode.fs().create_directory(parent_inode.identifier(), p.basename(), mode, current->process().uid(), current->process().gid(), error);
  319. if (new_dir)
  320. return KSuccess;
  321. return KResult(error);
  322. }
  323. KResult VFS::access(StringView path, int mode, Custody& base)
  324. {
  325. auto custody_or_error = resolve_path(path, base);
  326. if (custody_or_error.is_error())
  327. return custody_or_error.error();
  328. auto& custody = *custody_or_error.value();
  329. auto& inode = custody.inode();
  330. auto metadata = inode.metadata();
  331. if (mode & R_OK) {
  332. if (!metadata.may_read(current->process()))
  333. return KResult(-EACCES);
  334. }
  335. if (mode & W_OK) {
  336. if (!metadata.may_write(current->process()))
  337. return KResult(-EACCES);
  338. }
  339. if (mode & X_OK) {
  340. if (!metadata.may_execute(current->process()))
  341. return KResult(-EACCES);
  342. }
  343. return KSuccess;
  344. }
  345. KResultOr<NonnullRefPtr<Custody>> VFS::open_directory(StringView path, Custody& base)
  346. {
  347. auto inode_or_error = resolve_path(path, base);
  348. if (inode_or_error.is_error())
  349. return inode_or_error.error();
  350. auto& custody = *inode_or_error.value();
  351. auto& inode = custody.inode();
  352. if (!inode.is_directory())
  353. return KResult(-ENOTDIR);
  354. if (!inode.metadata().may_execute(current->process()))
  355. return KResult(-EACCES);
  356. return custody;
  357. }
  358. KResult VFS::chmod(Inode& inode, mode_t mode)
  359. {
  360. if (inode.fs().is_readonly())
  361. return KResult(-EROFS);
  362. if (current->process().euid() != inode.metadata().uid && !current->process().is_superuser())
  363. return KResult(-EPERM);
  364. // Only change the permission bits.
  365. mode = (inode.mode() & ~04777u) | (mode & 04777u);
  366. return inode.chmod(mode);
  367. }
  368. KResult VFS::chmod(StringView path, mode_t mode, Custody& base)
  369. {
  370. auto custody_or_error = resolve_path(path, base);
  371. if (custody_or_error.is_error())
  372. return custody_or_error.error();
  373. auto& custody = *custody_or_error.value();
  374. auto& inode = custody.inode();
  375. return chmod(inode, mode);
  376. }
  377. KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
  378. {
  379. RefPtr<Custody> old_parent_custody;
  380. auto old_custody_or_error = resolve_path(old_path, base, &old_parent_custody);
  381. if (old_custody_or_error.is_error())
  382. return old_custody_or_error.error();
  383. auto& old_custody = *old_custody_or_error.value();
  384. auto& old_inode = old_custody.inode();
  385. RefPtr<Custody> new_parent_custody;
  386. auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody);
  387. if (new_custody_or_error.is_error()) {
  388. if (new_custody_or_error.error() != -ENOENT)
  389. return new_custody_or_error.error();
  390. }
  391. auto& old_parent_inode = old_parent_custody->inode();
  392. auto& new_parent_inode = new_parent_custody->inode();
  393. if (&old_parent_inode.fs() != &new_parent_inode.fs())
  394. return KResult(-EXDEV);
  395. if (!new_parent_inode.metadata().may_write(current->process()))
  396. return KResult(-EACCES);
  397. if (!old_parent_inode.metadata().may_write(current->process()))
  398. return KResult(-EACCES);
  399. if (old_parent_inode.metadata().is_sticky()) {
  400. if (!current->process().is_superuser() && old_inode.metadata().uid != current->process().euid())
  401. return KResult(-EACCES);
  402. }
  403. auto new_basename = FileSystemPath(new_path).basename();
  404. if (!new_custody_or_error.is_error()) {
  405. auto& new_custody = *new_custody_or_error.value();
  406. auto& new_inode = new_custody.inode();
  407. // FIXME: Is this really correct? Check what other systems do.
  408. if (&new_inode == &old_inode)
  409. return KSuccess;
  410. if (new_parent_inode.metadata().is_sticky()) {
  411. if (!current->process().is_superuser() && new_inode.metadata().uid != current->process().euid())
  412. return KResult(-EACCES);
  413. }
  414. if (new_inode.is_directory() && !old_inode.is_directory())
  415. return KResult(-EISDIR);
  416. auto result = new_parent_inode.remove_child(new_basename);
  417. if (result.is_error())
  418. return result;
  419. new_custody.did_delete({});
  420. }
  421. auto result = new_parent_inode.add_child(old_inode.identifier(), new_basename, old_inode.mode());
  422. if (result.is_error())
  423. return result;
  424. result = old_parent_inode.remove_child(FileSystemPath(old_path).basename());
  425. if (result.is_error())
  426. return result;
  427. old_custody.did_rename({}, new_basename);
  428. return KSuccess;
  429. }
  430. KResult VFS::chown(Inode& inode, uid_t a_uid, gid_t a_gid)
  431. {
  432. if (inode.fs().is_readonly())
  433. return KResult(-EROFS);
  434. auto metadata = inode.metadata();
  435. if (current->process().euid() != metadata.uid && !current->process().is_superuser())
  436. return KResult(-EPERM);
  437. uid_t new_uid = metadata.uid;
  438. gid_t new_gid = metadata.gid;
  439. if (a_uid != (uid_t)-1) {
  440. if (current->process().euid() != a_uid && !current->process().is_superuser())
  441. return KResult(-EPERM);
  442. new_uid = a_uid;
  443. }
  444. if (a_gid != (gid_t)-1) {
  445. if (!current->process().in_group(a_gid) && !current->process().is_superuser())
  446. return KResult(-EPERM);
  447. new_gid = a_gid;
  448. }
  449. dbg() << "VFS::chown(): inode " << inode.identifier() << " <- uid:" << new_uid << " gid:" << new_gid;
  450. return inode.chown(new_uid, new_gid);
  451. }
  452. KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Custody& base)
  453. {
  454. auto custody_or_error = resolve_path(path, base);
  455. if (custody_or_error.is_error())
  456. return custody_or_error.error();
  457. auto& custody = *custody_or_error.value();
  458. auto& inode = custody.inode();
  459. return chown(inode, a_uid, a_gid);
  460. }
  461. KResult VFS::link(StringView old_path, StringView new_path, Custody& base)
  462. {
  463. auto old_custody_or_error = resolve_path(old_path, base);
  464. if (old_custody_or_error.is_error())
  465. return old_custody_or_error.error();
  466. auto& old_custody = *old_custody_or_error.value();
  467. auto& old_inode = old_custody.inode();
  468. RefPtr<Custody> parent_custody;
  469. auto new_custody_or_error = resolve_path(new_path, base, &parent_custody);
  470. if (!new_custody_or_error.is_error())
  471. return KResult(-EEXIST);
  472. if (!parent_custody)
  473. return KResult(-ENOENT);
  474. auto& parent_inode = parent_custody->inode();
  475. if (parent_inode.fsid() != old_inode.fsid())
  476. return KResult(-EXDEV);
  477. if (parent_inode.fs().is_readonly())
  478. return KResult(-EROFS);
  479. if (!parent_inode.metadata().may_write(current->process()))
  480. return KResult(-EACCES);
  481. if (old_inode.is_directory())
  482. return KResult(-EPERM);
  483. return parent_inode.add_child(old_inode.identifier(), FileSystemPath(new_path).basename(), old_inode.mode());
  484. }
  485. KResult VFS::unlink(StringView path, Custody& base)
  486. {
  487. RefPtr<Custody> parent_custody;
  488. auto custody_or_error = resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR);
  489. if (custody_or_error.is_error())
  490. return custody_or_error.error();
  491. auto& custody = *custody_or_error.value();
  492. auto& inode = custody.inode();
  493. if (inode.is_directory())
  494. return KResult(-EISDIR);
  495. auto& parent_inode = parent_custody->inode();
  496. if (!parent_inode.metadata().may_write(current->process()))
  497. return KResult(-EACCES);
  498. if (parent_inode.metadata().is_sticky()) {
  499. if (!current->process().is_superuser() && inode.metadata().uid != current->process().euid())
  500. return KResult(-EACCES);
  501. }
  502. auto result = parent_inode.remove_child(FileSystemPath(path).basename());
  503. if (result.is_error())
  504. return result;
  505. custody.did_delete({});
  506. return KSuccess;
  507. }
  508. KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
  509. {
  510. RefPtr<Custody> parent_custody;
  511. auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
  512. if (!existing_custody_or_error.is_error())
  513. return KResult(-EEXIST);
  514. if (!parent_custody)
  515. return KResult(-ENOENT);
  516. if (existing_custody_or_error.error() != -ENOENT)
  517. return existing_custody_or_error.error();
  518. auto& parent_inode = parent_custody->inode();
  519. if (!parent_inode.metadata().may_write(current->process()))
  520. return KResult(-EACCES);
  521. FileSystemPath p(linkpath);
  522. dbg() << "VFS::symlink: '" << p.basename() << "' (-> '" << target << "') in " << parent_inode.identifier();
  523. int error;
  524. auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), 0120644, 0, 0, current->process().uid(), current->process().gid(), error);
  525. if (!new_file)
  526. return KResult(error);
  527. ssize_t nwritten = new_file->write_bytes(0, target.length(), (const u8*)target.characters_without_null_termination(), nullptr);
  528. if (nwritten < 0)
  529. return KResult(nwritten);
  530. return KSuccess;
  531. }
  532. KResult VFS::rmdir(StringView path, Custody& base)
  533. {
  534. RefPtr<Custody> parent_custody;
  535. auto custody_or_error = resolve_path(path, base, &parent_custody);
  536. if (custody_or_error.is_error())
  537. return KResult(custody_or_error.error());
  538. auto& custody = *custody_or_error.value();
  539. auto& inode = custody.inode();
  540. if (inode.fs().is_readonly())
  541. return KResult(-EROFS);
  542. // FIXME: We should return EINVAL if the last component of the path is "."
  543. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  544. if (!inode.is_directory())
  545. return KResult(-ENOTDIR);
  546. auto& parent_inode = parent_custody->inode();
  547. if (!parent_inode.metadata().may_write(current->process()))
  548. return KResult(-EACCES);
  549. if (inode.directory_entry_count() != 2)
  550. return KResult(-ENOTEMPTY);
  551. auto result = inode.remove_child(".");
  552. if (result.is_error())
  553. return result;
  554. result = inode.remove_child("..");
  555. if (result.is_error())
  556. return result;
  557. return parent_inode.remove_child(FileSystemPath(path).basename());
  558. }
  559. RefPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  560. {
  561. if (!inode_id.is_valid())
  562. return nullptr;
  563. return inode_id.fs()->get_inode(inode_id);
  564. }
  565. VFS::Mount::Mount(FS& guest_fs, Custody* host_custody, int flags)
  566. : m_guest(guest_fs.root_inode())
  567. , m_guest_fs(guest_fs)
  568. , m_host_custody(host_custody)
  569. , m_flags(flags)
  570. {
  571. }
  572. VFS::Mount::Mount(Inode& source, Custody& host_custody, int flags)
  573. : m_guest(source.identifier())
  574. , m_guest_fs(source.fs())
  575. , m_host_custody(host_custody)
  576. , m_flags(flags)
  577. {
  578. }
  579. String VFS::Mount::absolute_path() const
  580. {
  581. if (!m_host_custody)
  582. return "/";
  583. return m_host_custody->absolute_path();
  584. }
  585. InodeIdentifier VFS::Mount::host() const
  586. {
  587. if (!m_host_custody)
  588. return {};
  589. return m_host_custody->inode().identifier();
  590. }
  591. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  592. {
  593. for (auto& mount : m_mounts) {
  594. callback(mount);
  595. }
  596. }
  597. void VFS::sync()
  598. {
  599. FS::sync();
  600. }
  601. Custody& VFS::root_custody()
  602. {
  603. if (!m_root_custody)
  604. m_root_custody = Custody::create(nullptr, "", *m_root_inode, MS_NODEV | MS_NOSUID);
  605. return *m_root_custody;
  606. }
  607. KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
  608. {
  609. if (symlink_recursion_level >= symlink_recursion_limit)
  610. return KResult(-ELOOP);
  611. if (path.is_empty())
  612. return KResult(-EINVAL);
  613. auto parts = path.split_view('/', true);
  614. auto& current_root = current->process().root_directory();
  615. NonnullRefPtr<Custody> custody = path[0] == '/' ? current_root : base;
  616. for (int i = 0; i < parts.size(); ++i) {
  617. Custody& parent = custody;
  618. auto parent_metadata = parent.inode().metadata();
  619. if (!parent_metadata.is_directory())
  620. return KResult(-ENOTDIR);
  621. // Ensure the current user is allowed to resolve paths inside this directory.
  622. if (!parent_metadata.may_execute(current->process()))
  623. return KResult(-EACCES);
  624. auto& part = parts[i];
  625. bool have_more_parts = i + 1 < parts.size();
  626. if (part == "..") {
  627. // If we encounter a "..", take a step back, but don't go beyond the root.
  628. if (custody->parent())
  629. custody = *custody->parent();
  630. continue;
  631. } else if (part == "." || part.is_empty()) {
  632. continue;
  633. }
  634. // Okay, let's look up this part.
  635. auto child_id = parent.inode().lookup(part);
  636. if (!child_id.is_valid()) {
  637. if (out_parent) {
  638. // ENOENT with a non-null parent custody signals to caller that
  639. // we found the immediate parent of the file, but the file itself
  640. // does not exist yet.
  641. *out_parent = have_more_parts ? nullptr : &parent;
  642. }
  643. return KResult(-ENOENT);
  644. }
  645. int mount_flags_for_child = parent.mount_flags();
  646. // See if there's something mounted on the child; in that case
  647. // we would need to return the guest inode, not the host inode.
  648. if (auto mount = find_mount_for_host(child_id)) {
  649. child_id = mount->guest();
  650. mount_flags_for_child = mount->flags();
  651. }
  652. auto child_inode = get_inode(child_id);
  653. ASSERT(child_inode);
  654. custody = Custody::create(&parent, part, *child_inode, mount_flags_for_child);
  655. if (child_inode->metadata().is_symlink()) {
  656. if (!have_more_parts) {
  657. if (options & O_NOFOLLOW)
  658. return KResult(-ELOOP);
  659. if (options & O_NOFOLLOW_NOERROR)
  660. break;
  661. }
  662. auto symlink_target = child_inode->resolve_as_link(parent, out_parent, options, symlink_recursion_level + 1);
  663. if (symlink_target.is_error() || !have_more_parts)
  664. return symlink_target;
  665. // Now, resolve the remaining path relative to the symlink target.
  666. // We prepend a "." to it to ensure that it's not empty and that
  667. // any initial slashes it might have get interpreted properly.
  668. StringBuilder remaining_path;
  669. remaining_path.append('.');
  670. remaining_path.append(path.substring_view_starting_after_substring(part));
  671. return resolve_path(remaining_path.to_string(), *symlink_target.value(), out_parent, options, symlink_recursion_level + 1);
  672. }
  673. }
  674. if (out_parent)
  675. *out_parent = custody->parent();
  676. return custody;
  677. }