| 14 | namespace vfs { |
| 15 | |
| 16 | auto MountTable::Mount(const char* path, FileSystem* fs, BlockDevice* device) |
| 17 | -> Expected<void> { |
| 18 | if (path == nullptr || fs == nullptr) { |
| 19 | return std::unexpected(Error(ErrorCode::kInvalidArgument)); |
| 20 | } |
| 21 | |
| 22 | LockGuard<SpinLock> guard(GetVfsState().vfs_lock_); |
| 23 | // 检查挂载点数量 |
| 24 | if (mount_count_ >= kMaxMounts) { |
| 25 | return std::unexpected(Error(ErrorCode::kFsMountFailed)); |
| 26 | } |
| 27 | |
| 28 | // 规范化路径(确保以 / 开头) |
| 29 | if (path[0] != '/') { |
| 30 | return std::unexpected(Error(ErrorCode::kFsInvalidPath)); |
| 31 | } |
| 32 | |
| 33 | // 检查路径是否已被挂载 |
| 34 | for (size_t i = 0; i < mount_count_; ++i) { |
| 35 | if (mounts_[i].active && strcmp(mounts_[i].mount_path, path) == 0) { |
| 36 | return std::unexpected(Error(ErrorCode::kFsAlreadyMounted)); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // 挂载文件系统 |
| 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()); |
| 45 | return std::unexpected(Error(ErrorCode::kFsMountFailed)); |
| 46 | } |
| 47 | |
| 48 | Inode* root_inode = mount_result.value(); |
| 49 | if (root_inode == nullptr || root_inode->type != FileType::kDirectory) { |
| 50 | return std::unexpected(Error(ErrorCode::kFsCorrupted)); |
| 51 | } |
| 52 | |
| 53 | // 为根 inode 创建 dentry |
| 54 | auto root_dentry_ptr = kstd::make_unique<Dentry>(); |
| 55 | if (!root_dentry_ptr) { |
| 56 | fs->Unmount(); |
| 57 | return std::unexpected(Error(ErrorCode::kOutOfMemory)); |
| 58 | } |
| 59 | |
| 60 | root_dentry_ptr->inode = root_inode; |
| 61 | strncpy(root_dentry_ptr->name, "/", sizeof(root_dentry_ptr->name)); |
| 62 | |
| 63 | // 查找挂载点 dentry(如果是非根挂载) |
| 64 | Dentry* mount_dentry = nullptr; |
| 65 | if (strcmp(path, "/") != 0) { |
| 66 | // 需要找到父文件系统中对应的 dentry |
| 67 | // 这里简化处理,实际应该通过 VFS Lookup 找到 |
| 68 | // 暂时设置为 nullptr,表示根挂载 |
| 69 | } |
| 70 | |
| 71 | // 找到空闲挂载点槽位 |
| 72 | size_t slot = 0; |
| 73 | for (; slot < kMaxMounts; ++slot) { |
no test coverage detected