VirtualFileSystem.cpp 16 KB

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