VirtualFileSystem.cpp 16 KB

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