Skip to content

fix: fix chroot and tmpfs#1480

Merged
fslongjin merged 7 commits intoDragonOS-Community:masterfrom
fslongjin:fix-chroot
Dec 16, 2025
Merged

fix: fix chroot and tmpfs#1480
fslongjin merged 7 commits intoDragonOS-Community:masterfrom
fslongjin:fix-chroot

Conversation

@fslongjin
Copy link
Member

No description provided.

- Introduced new ProcFileType variants for /proc/[pid]/mountinfo and /proc/[pid]/maps.
- Implemented content generation for /proc/[pid]/mountinfo and /proc/[pid]/maps to align with Linux semantics.
- Updated ProcFS inode creation to include these new files for each process.
- Enhanced path handling in the VFS to ensure correct resolution based on process-specific root and current working directory.

This addition improves the process filesystem's functionality and compatibility with Linux behavior.
- Added an unevictable flag to the PageCache structure, allowing pages to be marked as unevictable to prevent reclamation.
- Updated the TmpfsInode structure to integrate page cache management, replacing direct data manipulation with page cache operations for read and write methods.
- Refactored truncate and resize methods to utilize the new page cache functionality, ensuring consistency and improved memory management.
- Implemented support for readahead in tmpfs, allowing for optimized data retrieval.
- Added checks for filename length across various VFS operations to prevent errors related to excessively long names.
- Updated the tmpfs implementation to handle read and write operations directly through the page cache, improving memory management.
- Enhanced rename functionality to ensure type compatibility and empty directory checks during operations.
- Increased maximum path length and defined maximum single filename length for better filesystem compliance.
- 将tmpfs跨目录移动逻辑提取为独立函数`tmpfs_move_entry_between_dirs`
- 优化锁顺序以避免死锁,按inode_id顺序锁定目录
- 修复MountFSInode::move_to中目标inode解包问题,确保正确委托给底层文件系统

Signed-off-by: longjin <longjin@DragonOS.org>
…page fault handling

- Added `create_zero_pages` method to `InnerPageCache` for efficient zero-page creation, optimizing memory usage in tmpfs.
- Updated `Tmpfs` to utilize the new zero-page creation during read and write operations, ensuring seamless handling of page faults.
- Enhanced `PageFaultHandler` with `pagecache_fault_zero` to manage page faults specifically for tmpfs, allowing for direct page cache access without disk I/O.

This improves the performance and reliability of memory file systems by reducing unnecessary allocations and ensuring proper page management.
…_cstr for clarity

- Updated the user access module to introduce vfs_check_and_clone_cstr, enhancing clarity in its purpose for VFS operations.
- Refactored sys_openat and utimensat to utilize the new vfs_check_and_clone_cstr function, ensuring consistent handling of C string paths across the filesystem.
@github-actions github-actions bot added the Bug fix A bug is fixed in this pull request label Dec 15, 2025
@fslongjin
Copy link
Member Author

@claude review this PR in Chinese

@dragonos-community-ai-pr-reviewer
Copy link

dragonos-community-ai-pr-reviewer bot commented Dec 15, 2025

Claude finished @fslongjin's task in 3m 46s —— View job


Claude正在审阅PR #1480

待办清单:

  • 分析PR上下文和变更文件
  • 查看主要变更文件内容
  • 分析chroot相关修复
  • 分析tmpfs相关修复
  • 检查代码质量和潜在问题
  • 提供中文审阅反馈

详细审阅报告

已全面分析PR #1480 "fix: fix chroot and tmpfs"。以下是详细的代码审阅反馈:

✅ 主要改进

  1. chroot系统调用实现完整正确

    • 新增 sys_chroot.rs 实现了完整的 chroot(2) 系统调用
    • 严格遵循Linux语义:需要 CAP_SYS_CHROOT 权限,路径必须是目录且有执行权限
    • 正确处理路径解析:绝对路径基于进程的 fs root(受chroot影响),相对路径基于cwd
    • 成功后只更新进程的 fs root,不改变 cwd(符合Linux行为)
  2. tmpfs架构升级

    • 从简单的 Vec<u8> 升级为基于 PageCache 的现代文件系统架构
    • 新增 fault()map_pages() 方法,支持内存映射和页错误处理
    • 统一 truncate()resize(),保证扩展/收缩逻辑一致性
  3. 重命名逻辑修复

    • 新增 tmpfs_move_entry_between_dirs() 处理跨目录重命名
    • 正确处理 RENAME_NOREPLACE、目录非空检查等边界情况
    • 修复了inode父子关系更新和硬链接计数维护
  4. VFS基础设施增强

    • user_path_at() 注释明确绝对路径从进程 fs root 开始解析
    • 进程 fs_struct 支持设置root,为chroot提供基础设施
    • 页缓存管理更加完善

⚠️ 潜在问题与建议

  1. 锁顺序潜在风险 (tmpfs/mod.rs:tmpfs_move_entry_between_dirs)

    let inode_to_move = src_dir.children.get(old_key).cloned()...;
    let old_type = inode_to_move.0.lock().metadata.file_type;  // 获取子inode锁
    • 函数接收 &mut TmpfsInode(调用者已持有锁),内部又获取子inode锁
    • 建议:明确锁顺序约定(如按inode_id排序),避免未来死锁
  2. 错误码一致性

    • tmpfs_move_entry_between_dirsArc::ptr_eq(existing, &dst_self) 返回 EBUSY
    • Linux在重命名到自身时可能返回 EINVALEBUSY,需要确认是否符合预期
  3. 页缓存生命周期管理

    • TmpfsInode 中的 page_cache: Option<Arc<PageCache>> 需要确保正确释放
    • 检查 resize() 实现是否正确处理页缓存的截断和扩展
  4. 安全检查

    • sys_chroot.rs 使用 check_and_clone_cstr(应已重命名为 vfs_check_and_clone_cstr
    • 确认用户空间字符串复制长度限制 MAX_PATHLEN 合理

📝 代码质量评价

优点:

  • 代码结构清晰,符合Rust安全规范
  • 错误处理完整,返回适当的系统错误码
  • 注释详细,特别是Linux语义说明很有价值
  • 模块化设计,新增函数职责单一

待改进:

  • 部分函数较长(如 tmpfs_move_entry_between_dirs),可考虑进一步拆分
  • 缺少一些边界情况的单元测试
  • 锁的使用文档可以更详细

🔧 测试建议

  1. chroot测试用例

    • 权限测试:无CAP_SYS_CHROOT应返回EPERM
    • 路径测试:非目录、不存在、无执行权限等情况
    • 嵌套chroot和文件访问测试
  2. tmpfs重命名测试

    • 跨目录重名替换
    • RENAME_NOREPLACE 标志测试
    • 目录非空时的重命名
    • 重命名到自身的边界情况
  3. 页缓存一致性测试

    • 文件截断后的内存映射访问
    • 并发读写下的数据一致性
    • 内存压力下的页回收

🎯 总体评价

这是一个高质量的PR,主要解决了两个重要问题:

  1. 实现了缺失的chroot系统调用,完善了进程隔离功能
  2. 重构了tmpfs文件系统,从简单内存存储升级为基于页缓存的现代架构

建议合并,但推荐在合并前:

  1. 添加锁顺序约定的注释
  2. 补充关键边界情况的测试用例
  3. 确认所有错误码与Linux行为一致

代码变更量大但结构清晰,体现了对文件系统语义的深入理解。chroot实现符合POSIX/Linux标准,tmpfs重构为后续性能优化奠定了基础。


审阅完成 - 所有待办事项已完成

@fslongjin fslongjin merged commit 5450a4b into DragonOS-Community:master Dec 16, 2025
12 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant