VirtualFileSystem.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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<CharacterDevice>&& 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 (!inode) {
  117. if (options & O_CREAT)
  118. return create(path, error, options, mode, base);
  119. return nullptr;
  120. }
  121. auto metadata = inode->metadata();
  122. if (!(options & O_DONT_OPEN_DEVICE) && metadata.is_character_device()) {
  123. auto it = m_character_devices.find(encoded_device(metadata.major_device, metadata.minor_device));
  124. if (it == m_character_devices.end()) {
  125. kprintf("VFS::open: no such character device %u,%u\n", metadata.major_device, metadata.minor_device);
  126. return nullptr;
  127. }
  128. auto descriptor = (*it).value->open(error, options);
  129. descriptor->set_original_inode(Badge<VFS>(), move(inode));
  130. return descriptor;
  131. }
  132. return FileDescriptor::create(move(inode));
  133. }
  134. RetainPtr<FileDescriptor> VFS::create(const String& path, int& error, int options, mode_t mode, Inode& base)
  135. {
  136. (void) options;
  137. error = -EWHYTHO;
  138. if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
  139. // Turn it into a regular file. (This feels rather hackish.)
  140. mode |= 0100000;
  141. }
  142. // FIXME: This won't work nicely across mount boundaries.
  143. FileSystemPath p(path);
  144. if (!p.is_valid()) {
  145. error = -EINVAL;
  146. return nullptr;
  147. }
  148. InodeIdentifier parent_dir;
  149. auto existing_file = resolve_path(path, base.identifier(), error, 0, &parent_dir);
  150. if (existing_file.is_valid()) {
  151. error = -EEXIST;
  152. return nullptr;
  153. }
  154. if (!parent_dir.is_valid()) {
  155. error = -ENOENT;
  156. return nullptr;
  157. }
  158. if (error != -ENOENT) {
  159. return nullptr;
  160. }
  161. dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  162. auto new_file = parent_dir.fs()->create_inode(parent_dir, 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. // FIXME: This won't work nicely across mount boundaries.
  172. FileSystemPath p(path);
  173. if (!p.is_valid()) {
  174. error = -EINVAL;
  175. return false;
  176. }
  177. InodeIdentifier parent_dir;
  178. auto existing_dir = resolve_path(path, base.identifier(), error, 0, &parent_dir);
  179. if (existing_dir.is_valid()) {
  180. error = -EEXIST;
  181. return false;
  182. }
  183. if (!parent_dir.is_valid()) {
  184. error = -ENOENT;
  185. return false;
  186. }
  187. if (error != -ENOENT) {
  188. return false;
  189. }
  190. dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
  191. auto new_dir = parent_dir.fs()->create_directory(parent_dir, p.basename(), mode, error);
  192. if (new_dir) {
  193. error = 0;
  194. return true;
  195. }
  196. return false;
  197. }
  198. bool VFS::chmod(const String& path, mode_t mode, Inode& base, int& error)
  199. {
  200. error = -EWHYTHO;
  201. // FIXME: This won't work nicely across mount boundaries.
  202. FileSystemPath p(path);
  203. if (!p.is_valid()) {
  204. error = -EINVAL;
  205. return false;
  206. }
  207. InodeIdentifier parent_dir;
  208. auto inode_id = resolve_path(path, base.identifier(), error, 0, &parent_dir);
  209. if (!inode_id.is_valid()) {
  210. error = -ENOENT;
  211. return false;
  212. }
  213. auto inode = get_inode(inode_id);
  214. // FIXME: Permission checks.
  215. // Only change the permission bits.
  216. mode = (inode->mode() & ~04777) | (mode & 04777);
  217. kprintf("VFS::chmod(): %u:%u mode %o\n", inode_id.fsid(), inode_id.index(), mode);
  218. if (!inode->chmod(mode, error))
  219. return false;
  220. error = 0;
  221. return true;
  222. }
  223. bool VFS::unlink(const String& path, Inode& base, int& error)
  224. {
  225. error = -EWHYTHO;
  226. // FIXME: This won't work nicely across mount boundaries.
  227. FileSystemPath p(path);
  228. if (!p.is_valid()) {
  229. error = -EINVAL;
  230. return false;
  231. }
  232. InodeIdentifier parent_dir;
  233. auto inode_id = resolve_path(path, base.identifier(), error, 0, &parent_dir);
  234. if (!inode_id.is_valid()) {
  235. error = -ENOENT;
  236. return false;
  237. }
  238. auto inode = get_inode(inode_id);
  239. if (inode->is_directory()) {
  240. error = -EISDIR;
  241. return false;
  242. }
  243. auto parent_inode = get_inode(parent_dir);
  244. // FIXME: The reverse_lookup here can definitely be avoided.
  245. if (!parent_inode->remove_child(parent_inode->reverse_lookup(inode_id), error))
  246. return false;
  247. error = 0;
  248. return true;
  249. }
  250. bool VFS::rmdir(const String& path, Inode& base, int& error)
  251. {
  252. error = -EWHYTHO;
  253. // FIXME: This won't work nicely across mount boundaries.
  254. FileSystemPath p(path);
  255. if (!p.is_valid()) {
  256. error = -EINVAL;
  257. return false;
  258. }
  259. InodeIdentifier parent_dir;
  260. auto inode_id = resolve_path(path, base.identifier(), error, 0, &parent_dir);
  261. if (!inode_id.is_valid()) {
  262. error = -ENOENT;
  263. return false;
  264. }
  265. if (inode_id.fs()->is_readonly()) {
  266. error = -EROFS;
  267. return false;
  268. }
  269. // FIXME: We should return EINVAL if the last component of the path is "."
  270. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  271. auto inode = get_inode(inode_id);
  272. if (!inode->is_directory()) {
  273. error = -ENOTDIR;
  274. return false;
  275. }
  276. if (inode->directory_entry_count() != 2) {
  277. error = -ENOTEMPTY;
  278. return false;
  279. }
  280. auto parent_inode = get_inode(parent_dir);
  281. ASSERT(parent_inode);
  282. dbgprintf("VFS::rmdir: Removing inode %u:%u from parent %u:%u\n", inode_id.fsid(), inode_id.index(), parent_dir.fsid(), parent_dir.index());
  283. // To do:
  284. // - Remove '.' in target (--child.link_count)
  285. // - Remove '..' in target (--parent.link_count)
  286. // - Remove target from its parent (--parent.link_count)
  287. if (!inode->remove_child(".", error))
  288. return false;
  289. if (!inode->remove_child("..", error))
  290. return false;
  291. // FIXME: The reverse_lookup here can definitely be avoided.
  292. if (!parent_inode->remove_child(parent_inode->reverse_lookup(inode_id), error))
  293. return false;
  294. error = 0;
  295. return true;
  296. }
  297. InodeIdentifier VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode, int& error)
  298. {
  299. auto symlink_contents = symlink_inode.read_entire();
  300. if (!symlink_contents)
  301. return { };
  302. auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
  303. #ifdef VFS_DEBUG
  304. kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
  305. #endif
  306. return resolve_path(linkee, base, error);
  307. }
  308. RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  309. {
  310. if (!inode_id.is_valid())
  311. return nullptr;
  312. return inode_id.fs()->get_inode(inode_id);
  313. }
  314. String VFS::absolute_path(InodeIdentifier inode_id)
  315. {
  316. auto inode = get_inode(inode_id);
  317. if (!inode)
  318. return { };
  319. return absolute_path(*inode);
  320. }
  321. String VFS::absolute_path(Inode& core_inode)
  322. {
  323. int error;
  324. Vector<InodeIdentifier> lineage;
  325. RetainPtr<Inode> inode = &core_inode;
  326. while (inode->identifier() != root_inode_id()) {
  327. if (auto* mount = find_mount_for_guest(inode->identifier()))
  328. lineage.append(mount->host());
  329. else
  330. lineage.append(inode->identifier());
  331. InodeIdentifier parent_id;
  332. if (inode->is_directory()) {
  333. parent_id = resolve_path("..", inode->identifier(), error);
  334. } else {
  335. parent_id = inode->parent()->identifier();
  336. }
  337. ASSERT(parent_id.is_valid());
  338. inode = get_inode(parent_id);
  339. }
  340. if (lineage.is_empty())
  341. return "/";
  342. lineage.append(root_inode_id());
  343. StringBuilder builder;
  344. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  345. auto& child = lineage[i - 1];
  346. auto parent = lineage[i];
  347. if (auto* mount = find_mount_for_host(parent))
  348. parent = mount->guest();
  349. builder.append('/');
  350. auto parent_inode = get_inode(parent);
  351. builder.append(parent_inode->reverse_lookup(child));
  352. }
  353. return builder.to_string();
  354. }
  355. InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
  356. {
  357. if (path.is_empty()) {
  358. error = -EINVAL;
  359. return { };
  360. }
  361. auto parts = path.split('/');
  362. InodeIdentifier crumb_id;
  363. if (path[0] == '/')
  364. crumb_id = root_inode_id();
  365. else
  366. crumb_id = base.is_valid() ? base : root_inode_id();
  367. if (parent_id)
  368. *parent_id = crumb_id;
  369. for (unsigned i = 0; i < parts.size(); ++i) {
  370. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  371. auto& part = parts[i];
  372. if (part.is_empty())
  373. break;
  374. auto crumb_inode = get_inode(crumb_id);
  375. if (!crumb_inode) {
  376. #ifdef VFS_DEBUG
  377. kprintf("invalid metadata\n");
  378. #endif
  379. error = -EIO;
  380. return { };
  381. }
  382. auto metadata = crumb_inode->metadata();
  383. if (!metadata.is_directory()) {
  384. #ifdef VFS_DEBUG
  385. kprintf("parent of <%s> not directory, it's inode %u:%u / %u:%u, mode: %u, size: %u\n", part.characters(), inode.fsid(), inode.index(), metadata.inode.fsid(), metadata.inode.index(), metadata.mode, metadata.size);
  386. #endif
  387. error = -ENOTDIR;
  388. return { };
  389. }
  390. auto parent = crumb_id;
  391. crumb_id = crumb_inode->lookup(part);
  392. if (!crumb_id.is_valid()) {
  393. #ifdef VFS_DEBUG
  394. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  395. #endif
  396. error = -ENOENT;
  397. return { };
  398. }
  399. #ifdef VFS_DEBUG
  400. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  401. #endif
  402. if (auto mount = find_mount_for_host(crumb_id)) {
  403. #ifdef VFS_DEBUG
  404. kprintf(" -- is host\n");
  405. #endif
  406. crumb_id = mount->guest();
  407. }
  408. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  409. #ifdef VFS_DEBUG
  410. kprintf(" -- is guest\n");
  411. #endif
  412. auto mount = find_mount_for_guest(crumb_id);
  413. auto dir_inode = get_inode(mount->host());
  414. crumb_id = dir_inode->lookup("..");
  415. }
  416. crumb_inode = get_inode(crumb_id);
  417. metadata = crumb_inode->metadata();
  418. if (metadata.is_directory()) {
  419. if (i != parts.size() - 1) {
  420. if (parent_id)
  421. *parent_id = crumb_id;
  422. }
  423. }
  424. if (metadata.is_symlink()) {
  425. if (i == parts.size() - 1) {
  426. if (options & O_NOFOLLOW) {
  427. error = -ELOOP;
  428. return { };
  429. }
  430. if (options & O_NOFOLLOW_NOERROR)
  431. return crumb_id;
  432. }
  433. crumb_id = resolve_symbolic_link(parent, *crumb_inode, error);
  434. if (!crumb_id.is_valid()) {
  435. kprintf("Symbolic link resolution failed :(\n");
  436. return { };
  437. }
  438. }
  439. }
  440. return crumb_id;
  441. }
  442. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  443. : m_host(host)
  444. , m_guest(guest_fs->root_inode())
  445. , m_guest_fs(move(guest_fs))
  446. {
  447. }
  448. void VFS::register_character_device(CharacterDevice& device)
  449. {
  450. m_character_devices.set(encoded_device(device.major(), device.minor()), &device);
  451. }
  452. void VFS::unregister_character_device(CharacterDevice& device)
  453. {
  454. m_character_devices.remove(encoded_device(device.major(), device.minor()));
  455. }
  456. CharacterDevice* VFS::get_device(unsigned major, unsigned minor)
  457. {
  458. auto it = m_character_devices.find(encoded_device(major, minor));
  459. if (it == m_character_devices.end())
  460. return nullptr;
  461. return (*it).value;
  462. }
  463. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  464. {
  465. for (auto& mount : m_mounts) {
  466. callback(*mount);
  467. }
  468. }
  469. void VFS::sync()
  470. {
  471. FS::sync();
  472. }