VirtualFileSystem.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #include "VirtualFileSystem.h"
  2. #include "FileDescriptor.h"
  3. #include "FileSystem.h"
  4. #include <AK/FileSystemPath.h>
  5. #include <AK/StringBuilder.h>
  6. #include <AK/kmalloc.h>
  7. #include <AK/kstdio.h>
  8. #include <AK/ktime.h>
  9. #include "CharacterDevice.h"
  10. #include <LibC/errno_numbers.h>
  11. #include <Kernel/Process.h>
  12. //#define VFS_DEBUG
  13. static VFS* s_the;
  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. bool VFS::mount(RetainPtr<FS>&& file_system, const String& path)
  35. {
  36. ASSERT(file_system);
  37. int error;
  38. auto inode = resolve_path(path, root_inode_id(), error);
  39. if (!inode.is_valid()) {
  40. kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
  41. return false;
  42. }
  43. kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", file_system->class_name(), file_system.ptr(), path.characters(), inode.index());
  44. // FIXME: check that this is not already a mount point
  45. auto mount = make<Mount>(inode, move(file_system));
  46. m_mounts.append(move(mount));
  47. return true;
  48. }
  49. bool VFS::mount_root(RetainPtr<FS>&& file_system)
  50. {
  51. if (m_root_inode) {
  52. kprintf("VFS: mount_root can't mount another root\n");
  53. return false;
  54. }
  55. auto mount = make<Mount>(InodeIdentifier(), move(file_system));
  56. auto root_inode_id = mount->guest().fs()->root_inode();
  57. auto root_inode = mount->guest().fs()->get_inode(root_inode_id);
  58. if (!root_inode->is_directory()) {
  59. kprintf("VFS: root inode (%02u:%08u) for / is not a directory :(\n", root_inode_id.fsid(), root_inode_id.index());
  60. return false;
  61. }
  62. m_root_inode = move(root_inode);
  63. kprintf("VFS: mounted root on %s{%p}\n",
  64. m_root_inode->fs().class_name(),
  65. &m_root_inode->fs());
  66. m_mounts.append(move(mount));
  67. return true;
  68. }
  69. auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
  70. {
  71. for (auto& mount : m_mounts) {
  72. if (mount->host() == inode)
  73. return mount.ptr();
  74. }
  75. return nullptr;
  76. }
  77. auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
  78. {
  79. for (auto& mount : m_mounts) {
  80. if (mount->guest() == inode)
  81. return mount.ptr();
  82. }
  83. return nullptr;
  84. }
  85. bool VFS::is_vfs_root(InodeIdentifier inode) const
  86. {
  87. return inode == root_inode_id();
  88. }
  89. void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
  90. {
  91. dir_inode.traverse_as_directory([&] (const FS::DirectoryEntry& entry) {
  92. InodeIdentifier resolved_inode;
  93. if (auto mount = find_mount_for_host(entry.inode))
  94. resolved_inode = mount->guest();
  95. else
  96. resolved_inode = entry.inode;
  97. if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
  98. auto mount = find_mount_for_guest(entry.inode);
  99. ASSERT(mount);
  100. resolved_inode = mount->host();
  101. }
  102. callback(FS::DirectoryEntry(entry.name, entry.name_length, resolved_inode, entry.file_type));
  103. return true;
  104. });
  105. }
  106. RetainPtr<FileDescriptor> VFS::open(RetainPtr<Device>&& device, int& error, int options)
  107. {
  108. // FIXME: Respect options.
  109. (void) options;
  110. (void) error;
  111. return FileDescriptor::create(move(device));
  112. }
  113. bool VFS::stat(const String& path, int& error, int options, Inode& base, struct stat& statbuf)
  114. {
  115. auto inode_id = resolve_path(path, base.identifier(), error, options);
  116. if (!inode_id.is_valid())
  117. return false;
  118. error = FileDescriptor::create(get_inode(inode_id))->fstat(&statbuf);
  119. if (error)
  120. return false;
  121. return true;
  122. }
  123. RetainPtr<FileDescriptor> VFS::open(const String& path, int& error, int options, mode_t mode, Inode& base)
  124. {
  125. auto inode_id = resolve_path(path, base.identifier(), error, options);
  126. auto inode = get_inode(inode_id);
  127. if (options & O_CREAT) {
  128. if (!inode)
  129. return create(path, error, options, mode, base);
  130. else if (options & O_EXCL) {
  131. error = -EEXIST;
  132. return nullptr;
  133. }
  134. }
  135. if (!inode)
  136. return nullptr;
  137. auto metadata = inode->metadata();
  138. // NOTE: Read permission is a bit weird, since O_RDONLY == 0,
  139. // so we check if (NOT write_only OR read_and_write)
  140. if (!(options & O_WRONLY) || (options & O_RDWR)) {
  141. if (!metadata.may_read(*current)) {
  142. error = -EACCES;
  143. return nullptr;
  144. }
  145. }
  146. if ((options & O_WRONLY) || (options & O_RDWR)) {
  147. if (!metadata.may_write(*current)) {
  148. error = -EACCES;
  149. return nullptr;
  150. }
  151. }
  152. if (metadata.is_device()) {
  153. auto it = m_devices.find(encoded_device(metadata.major_device, metadata.minor_device));
  154. if (it == m_devices.end()) {
  155. kprintf("VFS::open: no such device %u,%u\n", metadata.major_device, metadata.minor_device);
  156. return nullptr;
  157. }
  158. auto descriptor = (*it).value->open(error, options);
  159. descriptor->set_original_inode(Badge<VFS>(), move(inode));
  160. return descriptor;
  161. }
  162. return FileDescriptor::create(move(inode));
  163. }
  164. RetainPtr<FileDescriptor> VFS::create(const String& path, int& error, int options, mode_t mode, Inode& base)
  165. {
  166. (void) options;
  167. error = -EWHYTHO;
  168. if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
  169. // Turn it into a regular file. (This feels rather hackish.)
  170. mode |= 0100000;
  171. }
  172. RetainPtr<Inode> parent_inode;
  173. auto existing_file = resolve_path_to_inode(path, base, error, &parent_inode);
  174. if (existing_file) {
  175. error = -EEXIST;
  176. return nullptr;
  177. }
  178. if (!parent_inode) {
  179. error = -ENOENT;
  180. return nullptr;
  181. }
  182. if (error != -ENOENT) {
  183. return nullptr;
  184. }
  185. if (!parent_inode->metadata().may_write(*current)) {
  186. error = -EACCES;
  187. return nullptr;
  188. }
  189. FileSystemPath p(path);
  190. dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index());
  191. auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), mode, 0, error);
  192. if (!new_file)
  193. return nullptr;
  194. error = 0;
  195. return FileDescriptor::create(move(new_file));
  196. }
  197. bool VFS::mkdir(const String& path, mode_t mode, Inode& base, int& error)
  198. {
  199. error = -EWHYTHO;
  200. RetainPtr<Inode> parent_inode;
  201. auto existing_dir = resolve_path_to_inode(path, base, error, &parent_inode);
  202. if (existing_dir) {
  203. error = -EEXIST;
  204. return false;
  205. }
  206. if (!parent_inode) {
  207. error = -ENOENT;
  208. return false;
  209. }
  210. if (error != -ENOENT) {
  211. return false;
  212. }
  213. if (!parent_inode->metadata().may_write(*current)) {
  214. error = -EACCES;
  215. return false;
  216. }
  217. FileSystemPath p(path);
  218. dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index());
  219. auto new_dir = parent_inode->fs().create_directory(parent_inode->identifier(), p.basename(), mode, error);
  220. if (new_dir) {
  221. error = 0;
  222. return true;
  223. }
  224. return false;
  225. }
  226. bool VFS::chmod(const String& path, mode_t mode, Inode& base, int& error)
  227. {
  228. error = -EWHYTHO;
  229. auto inode = resolve_path_to_inode(path, base, error);
  230. if (!inode)
  231. return false;
  232. if (inode->fs().is_readonly()) {
  233. error = -EROFS;
  234. return false;
  235. }
  236. if (current->euid() != inode->metadata().uid) {
  237. error = -EPERM;
  238. return false;
  239. }
  240. // Only change the permission bits.
  241. mode = (inode->mode() & ~04777) | (mode & 04777);
  242. kprintf("VFS::chmod(): %u:%u mode %o\n", inode->fsid(), inode->index(), mode);
  243. if (!inode->chmod(mode, error))
  244. return false;
  245. error = 0;
  246. return true;
  247. }
  248. RetainPtr<Inode> VFS::resolve_path_to_inode(const String& path, Inode& base, int& error, RetainPtr<Inode>* parent_inode)
  249. {
  250. // FIXME: This won't work nicely across mount boundaries.
  251. FileSystemPath p(path);
  252. if (!p.is_valid()) {
  253. error = -EINVAL;
  254. return nullptr;
  255. }
  256. InodeIdentifier parent_id;
  257. auto inode_id = resolve_path(path, base.identifier(), error, 0, &parent_id);
  258. if (parent_inode && parent_id.is_valid())
  259. *parent_inode = get_inode(parent_id);
  260. if (!inode_id.is_valid()) {
  261. error = -ENOENT;
  262. return nullptr;
  263. }
  264. return get_inode(inode_id);
  265. }
  266. bool VFS::link(const String& old_path, const String& new_path, Inode& base, int& error)
  267. {
  268. auto old_inode = resolve_path_to_inode(old_path, base, error);
  269. if (!old_inode)
  270. return false;
  271. RetainPtr<Inode> parent_inode;
  272. auto new_inode = resolve_path_to_inode(new_path, base, error, &parent_inode);
  273. if (new_inode) {
  274. error = -EEXIST;
  275. return false;
  276. }
  277. if (!parent_inode) {
  278. error = -ENOENT;
  279. return false;
  280. }
  281. if (parent_inode->fsid() != old_inode->fsid()) {
  282. error = -EXDEV;
  283. return false;
  284. }
  285. if (parent_inode->fs().is_readonly()) {
  286. error = -EROFS;
  287. return false;
  288. }
  289. if (!parent_inode->add_child(old_inode->identifier(), FileSystemPath(old_path).basename(), 0, error))
  290. return false;
  291. error = 0;
  292. return true;
  293. }
  294. bool VFS::unlink(const String& path, Inode& base, int& error)
  295. {
  296. RetainPtr<Inode> parent_inode;
  297. auto inode = resolve_path_to_inode(path, base, error, &parent_inode);
  298. if (!inode)
  299. return false;
  300. if (inode->is_directory()) {
  301. error = -EISDIR;
  302. return false;
  303. }
  304. if (!parent_inode->metadata().may_write(*current)) {
  305. error = -EACCES;
  306. return false;
  307. }
  308. if (!parent_inode->remove_child(FileSystemPath(path).basename(), error))
  309. return false;
  310. error = 0;
  311. return true;
  312. }
  313. bool VFS::rmdir(const String& path, Inode& base, int& error)
  314. {
  315. error = -EWHYTHO;
  316. RetainPtr<Inode> parent_inode;
  317. auto inode = resolve_path_to_inode(path, base, error, &parent_inode);
  318. if (!inode)
  319. return false;
  320. if (inode->fs().is_readonly()) {
  321. error = -EROFS;
  322. return false;
  323. }
  324. // FIXME: We should return EINVAL if the last component of the path is "."
  325. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  326. if (!inode->is_directory()) {
  327. error = -ENOTDIR;
  328. return false;
  329. }
  330. if (!parent_inode->metadata().may_write(*current)) {
  331. error = -EACCES;
  332. return false;
  333. }
  334. if (inode->directory_entry_count() != 2) {
  335. error = -ENOTEMPTY;
  336. return false;
  337. }
  338. dbgprintf("VFS::rmdir: Removing inode %u:%u from parent %u:%u\n", inode->fsid(), inode->index(), parent_inode->fsid(), parent_inode->index());
  339. // To do:
  340. // - Remove '.' in target (--child.link_count)
  341. // - Remove '..' in target (--parent.link_count)
  342. // - Remove target from its parent (--parent.link_count)
  343. if (!inode->remove_child(".", error))
  344. return false;
  345. if (!inode->remove_child("..", error))
  346. return false;
  347. // FIXME: The reverse_lookup here can definitely be avoided.
  348. if (!parent_inode->remove_child(parent_inode->reverse_lookup(inode->identifier()), error))
  349. return false;
  350. error = 0;
  351. return true;
  352. }
  353. InodeIdentifier VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode, int& error)
  354. {
  355. auto symlink_contents = symlink_inode.read_entire();
  356. if (!symlink_contents)
  357. return { };
  358. auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
  359. #ifdef VFS_DEBUG
  360. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  361. #endif
  362. return resolve_path(linkee, base, error);
  363. }
  364. RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  365. {
  366. if (!inode_id.is_valid())
  367. return nullptr;
  368. return inode_id.fs()->get_inode(inode_id);
  369. }
  370. String VFS::absolute_path(InodeIdentifier inode_id)
  371. {
  372. auto inode = get_inode(inode_id);
  373. if (!inode)
  374. return { };
  375. return absolute_path(*inode);
  376. }
  377. String VFS::absolute_path(Inode& core_inode)
  378. {
  379. int error;
  380. Vector<InodeIdentifier> lineage;
  381. RetainPtr<Inode> inode = &core_inode;
  382. while (inode->identifier() != root_inode_id()) {
  383. if (auto* mount = find_mount_for_guest(inode->identifier()))
  384. lineage.append(mount->host());
  385. else
  386. lineage.append(inode->identifier());
  387. InodeIdentifier parent_id;
  388. if (inode->is_directory()) {
  389. parent_id = resolve_path("..", inode->identifier(), error);
  390. } else {
  391. parent_id = inode->parent()->identifier();
  392. }
  393. ASSERT(parent_id.is_valid());
  394. inode = get_inode(parent_id);
  395. }
  396. if (lineage.is_empty())
  397. return "/";
  398. lineage.append(root_inode_id());
  399. StringBuilder builder;
  400. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  401. auto& child = lineage[i - 1];
  402. auto parent = lineage[i];
  403. if (auto* mount = find_mount_for_host(parent))
  404. parent = mount->guest();
  405. builder.append('/');
  406. auto parent_inode = get_inode(parent);
  407. builder.append(parent_inode->reverse_lookup(child));
  408. }
  409. return builder.to_string();
  410. }
  411. InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
  412. {
  413. if (path.is_empty()) {
  414. error = -EINVAL;
  415. return { };
  416. }
  417. auto parts = path.split('/');
  418. InodeIdentifier crumb_id;
  419. if (path[0] == '/')
  420. crumb_id = root_inode_id();
  421. else
  422. crumb_id = base.is_valid() ? base : root_inode_id();
  423. if (parent_id)
  424. *parent_id = crumb_id;
  425. for (unsigned i = 0; i < parts.size(); ++i) {
  426. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  427. auto& part = parts[i];
  428. if (part.is_empty())
  429. break;
  430. auto crumb_inode = get_inode(crumb_id);
  431. if (!crumb_inode) {
  432. #ifdef VFS_DEBUG
  433. kprintf("invalid metadata\n");
  434. #endif
  435. error = -EIO;
  436. return { };
  437. }
  438. auto metadata = crumb_inode->metadata();
  439. if (!metadata.is_directory()) {
  440. #ifdef VFS_DEBUG
  441. kprintf("parent of <%s> not directory, it's inode %u:%u / %u:%u, mode: %u, size: %u\n", part.characters(), crumb_id.fsid(), crumb_id.index(), metadata.inode.fsid(), metadata.inode.index(), metadata.mode, metadata.size);
  442. #endif
  443. error = -ENOTDIR;
  444. return { };
  445. }
  446. if (!metadata.may_execute(*current)) {
  447. error = -EACCES;
  448. return { };
  449. }
  450. auto parent = crumb_id;
  451. crumb_id = crumb_inode->lookup(part);
  452. if (!crumb_id.is_valid()) {
  453. #ifdef VFS_DEBUG
  454. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  455. #endif
  456. error = -ENOENT;
  457. return { };
  458. }
  459. #ifdef VFS_DEBUG
  460. kprintf("<%s> %u:%u\n", part.characters(), crumb_id.fsid(), crumb_id.index());
  461. #endif
  462. if (auto mount = find_mount_for_host(crumb_id)) {
  463. #ifdef VFS_DEBUG
  464. kprintf(" -- is host\n");
  465. #endif
  466. crumb_id = mount->guest();
  467. }
  468. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  469. #ifdef VFS_DEBUG
  470. kprintf(" -- is guest\n");
  471. #endif
  472. auto mount = find_mount_for_guest(crumb_id);
  473. auto dir_inode = get_inode(mount->host());
  474. ASSERT(dir_inode);
  475. crumb_id = dir_inode->lookup("..");
  476. }
  477. crumb_inode = get_inode(crumb_id);
  478. ASSERT(crumb_inode);
  479. metadata = crumb_inode->metadata();
  480. if (metadata.is_directory()) {
  481. if (i != parts.size() - 1) {
  482. if (parent_id)
  483. *parent_id = crumb_id;
  484. }
  485. }
  486. if (metadata.is_symlink()) {
  487. if (i == parts.size() - 1) {
  488. if (options & O_NOFOLLOW) {
  489. error = -ELOOP;
  490. return { };
  491. }
  492. if (options & O_NOFOLLOW_NOERROR)
  493. return crumb_id;
  494. }
  495. crumb_id = resolve_symbolic_link(parent, *crumb_inode, error);
  496. if (!crumb_id.is_valid()) {
  497. kprintf("Symbolic link resolution failed :(\n");
  498. return { };
  499. }
  500. }
  501. }
  502. return crumb_id;
  503. }
  504. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  505. : m_host(host)
  506. , m_guest(guest_fs->root_inode())
  507. , m_guest_fs(move(guest_fs))
  508. {
  509. }
  510. void VFS::register_device(Device& device)
  511. {
  512. m_devices.set(encoded_device(device.major(), device.minor()), &device);
  513. }
  514. void VFS::unregister_device(Device& device)
  515. {
  516. m_devices.remove(encoded_device(device.major(), device.minor()));
  517. }
  518. Device* VFS::get_device(unsigned major, unsigned minor)
  519. {
  520. auto it = m_devices.find(encoded_device(major, minor));
  521. if (it == m_devices.end())
  522. return nullptr;
  523. return (*it).value;
  524. }
  525. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  526. {
  527. for (auto& mount : m_mounts) {
  528. callback(*mount);
  529. }
  530. }
  531. void VFS::sync()
  532. {
  533. FS::sync();
  534. }