18 if (path ==
nullptr || fs ==
nullptr) {
24 if (mount_count_ >= kMaxMounts) {
34 for (
size_t i = 0; i < mount_count_; ++i) {
35 if (mounts_[i].active &&
strcmp(mounts_[i].mount_path, path) == 0) {
41 auto mount_result = fs->Mount(device);
42 if (!mount_result.has_value()) {
43 klog::Err(
"MountTable: failed to mount filesystem '{}': {}", fs->GetName(),
44 mount_result.error().message());
48 Inode* root_inode = mount_result.value();
54 auto root_dentry_ptr = kstd::make_unique<Dentry>();
55 if (!root_dentry_ptr) {
60 root_dentry_ptr->inode = root_inode;
61 strncpy(root_dentry_ptr->name,
"/",
sizeof(root_dentry_ptr->name));
64 Dentry* mount_dentry =
nullptr;
65 if (
strcmp(path,
"/") != 0) {
73 for (; slot < kMaxMounts; ++slot) {
74 if (!mounts_[slot].active) {
79 if (slot >= kMaxMounts) {
86 mounts_[slot].mount_path = path;
87 mounts_[slot].mount_dentry = mount_dentry;
88 mounts_[slot].filesystem = fs;
89 mounts_[slot].device = device;
90 mounts_[slot].root_inode = root_inode;
91 mounts_[slot].root_dentry = root_dentry_ptr.release();
92 mounts_[slot].active =
true;
97 if (
strcmp(path,
"/") == 0) {
98 root_mount_ = &mounts_[slot];
104 klog::Info(
"MountTable: mounted '{}' on '{}'", fs->GetName(), path);
159 if (path ==
nullptr || path[0] !=
'/') {
164 size_t best_match_len = 0;
166 for (
size_t i = 0; i < kMaxMounts; ++i) {
167 if (!mounts_[i].active) {
172 if (mp_path ==
nullptr) {
176 size_t mp_len =
strlen(mp_path);
179 if (
strncmp(path, mp_path, mp_len) == 0) {
181 char next_char = path[mp_len];
182 if (next_char ==
'\0' || next_char ==
'/' ||
183 (mp_len == 1 && mp_path[0] ==
'/')) {
185 if (mp_len > best_match_len) {
186 best_match = &mounts_[i];
187 best_match_len = mp_len;
Dentry — 目录项缓存(路径名 ↔ Inode 的映射)