VirtualFileSystem.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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>&& fileSystem, const String& path)
  39. {
  40. ASSERT(fileSystem);
  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", fileSystem->class_name(), fileSystem.ptr(), path.characters(), inode.index());
  48. // FIXME: check that this is not already a mount point
  49. auto mount = make<Mount>(inode, move(fileSystem));
  50. m_mounts.append(move(mount));
  51. return true;
  52. }
  53. bool VFS::mount_root(RetainPtr<FS>&& fileSystem)
  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(fileSystem));
  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 resolvedInode;
  97. if (auto mount = find_mount_for_host(entry.inode))
  98. resolvedInode = mount->guest();
  99. else
  100. resolvedInode = 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. resolvedInode = mount->host();
  105. }
  106. callback(FS::DirectoryEntry(entry.name, entry.name_length, resolvedInode, entry.fileType));
  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, InodeIdentifier base)
  118. {
  119. auto inode_id = resolve_path(path, base, 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.isCharacterDevice()) {
  128. auto it = m_character_devices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
  129. if (it == m_character_devices.end()) {
  130. kprintf("VFS::open: no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
  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, InodeIdentifier base)
  140. {
  141. (void) options;
  142. error = -EWHYTHO;
  143. if (!isSocket(mode) && !isFIFO(mode) && !isBlockDevice(mode) && !isCharacterDevice(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, 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, InodeIdentifier 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, 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 = base.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(Inode& core_inode)
  320. {
  321. int error;
  322. Vector<InodeIdentifier> lineage;
  323. RetainPtr<Inode> inode = &core_inode;
  324. while (inode->identifier() != root_inode_id()) {
  325. if (auto* mount = find_mount_for_guest(inode->identifier()))
  326. lineage.append(mount->host());
  327. else
  328. lineage.append(inode->identifier());
  329. InodeIdentifier parent_id;
  330. if (inode->is_directory()) {
  331. parent_id = resolve_path("..", inode->identifier(), error);
  332. } else {
  333. parent_id = inode->parent()->identifier();
  334. }
  335. ASSERT(parent_id.is_valid());
  336. inode = get_inode(parent_id);
  337. }
  338. if (lineage.is_empty())
  339. return "/";
  340. lineage.append(root_inode_id());
  341. StringBuilder builder;
  342. for (size_t i = lineage.size() - 1; i >= 1; --i) {
  343. auto& child = lineage[i - 1];
  344. auto parent = lineage[i];
  345. if (auto* mount = find_mount_for_host(parent))
  346. parent = mount->guest();
  347. builder.append('/');
  348. auto parent_inode = get_inode(parent);
  349. builder.append(parent_inode->reverse_lookup(child));
  350. }
  351. return builder.build();
  352. }
  353. InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
  354. {
  355. if (path.is_empty()) {
  356. error = -EINVAL;
  357. return { };
  358. }
  359. auto parts = path.split('/');
  360. InodeIdentifier crumb_id;
  361. if (path[0] == '/')
  362. crumb_id = root_inode_id();
  363. else
  364. crumb_id = base.is_valid() ? base : root_inode_id();
  365. if (parent_id)
  366. *parent_id = crumb_id;
  367. for (unsigned i = 0; i < parts.size(); ++i) {
  368. bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
  369. auto& part = parts[i];
  370. if (part.is_empty())
  371. break;
  372. auto crumb_inode = get_inode(crumb_id);
  373. if (!crumb_inode) {
  374. #ifdef VFS_DEBUG
  375. kprintf("invalid metadata\n");
  376. #endif
  377. error = -EIO;
  378. return { };
  379. }
  380. auto metadata = crumb_inode->metadata();
  381. if (!metadata.isDirectory()) {
  382. #ifdef VFS_DEBUG
  383. 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);
  384. #endif
  385. error = -ENOTDIR;
  386. return { };
  387. }
  388. auto parent = crumb_id;
  389. crumb_id = crumb_inode->lookup(part);
  390. if (!crumb_id.is_valid()) {
  391. #ifdef VFS_DEBUG
  392. kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
  393. #endif
  394. error = -ENOENT;
  395. return { };
  396. }
  397. #ifdef VFS_DEBUG
  398. kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
  399. #endif
  400. if (auto mount = find_mount_for_host(crumb_id)) {
  401. #ifdef VFS_DEBUG
  402. kprintf(" -- is host\n");
  403. #endif
  404. crumb_id = mount->guest();
  405. }
  406. if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
  407. #ifdef VFS_DEBUG
  408. kprintf(" -- is guest\n");
  409. #endif
  410. auto mount = find_mount_for_guest(crumb_id);
  411. auto dir_inode = get_inode(mount->host());
  412. crumb_id = dir_inode->lookup("..");
  413. }
  414. crumb_inode = get_inode(crumb_id);
  415. metadata = crumb_inode->metadata();
  416. if (metadata.isDirectory()) {
  417. if (i != parts.size() - 1) {
  418. if (parent_id)
  419. *parent_id = crumb_id;
  420. }
  421. }
  422. if (metadata.isSymbolicLink()) {
  423. if (i == parts.size() - 1) {
  424. if (options & O_NOFOLLOW) {
  425. error = -ELOOP;
  426. return { };
  427. }
  428. if (options & O_NOFOLLOW_NOERROR)
  429. return crumb_id;
  430. }
  431. crumb_id = resolve_symbolic_link(parent, *crumb_inode, error);
  432. if (!crumb_id.is_valid()) {
  433. kprintf("Symbolic link resolution failed :(\n");
  434. return { };
  435. }
  436. }
  437. }
  438. return crumb_id;
  439. }
  440. VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
  441. : m_host(host)
  442. , m_guest(guest_fs->root_inode())
  443. , m_guest_fs(move(guest_fs))
  444. {
  445. }
  446. void VFS::register_character_device(CharacterDevice& device)
  447. {
  448. m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
  449. }
  450. void VFS::unregister_character_device(CharacterDevice& device)
  451. {
  452. m_character_devices.remove(encodedDevice(device.major(), device.minor()));
  453. }
  454. CharacterDevice* VFS::get_device(unsigned major, unsigned minor)
  455. {
  456. auto it = m_character_devices.find(encodedDevice(major, minor));
  457. if (it == m_character_devices.end())
  458. return nullptr;
  459. return (*it).value;
  460. }
  461. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  462. {
  463. for (auto& mount : m_mounts) {
  464. callback(*mount);
  465. }
  466. }
  467. void VFS::sync()
  468. {
  469. FS::sync();
  470. }