Skip to content

Conversation

@Vitus213
Copy link
Contributor

@Vitus213 Vitus213 commented Aug 19, 2025

Note

Implements a full loop device stack (/dev/loop-control, /dev/loopX) with IOCTLs, lifecycle management, safe unregister, devfs integration, and unit tests.

  • Kernel Block Subsystem:
    • Loop Device: New module driver/block/loop_device/* implementing LoopDevice, LoopControlDevice, LoopManager, LoopDeviceDriver, and IOCTLs (LOOP_SET/GET_STATUS(64), LOOP_SET_FD, LOOP_CLR_FD, LOOP_CHANGE_FD, LOOP_SET_CAPACITY).
    • State & I/O Handling: State machine (Unbound/Bound/Rundown/Draining/Deleting), active I/O tracking with drain-on-delete, read-only enforcement, offset/size-limit handling with atomic updates.
    • Integration: Exported via kernel/src/driver/block/mod.rs; new majors LOOP_MAJOR and LOOP_CONTROL_MAJOR.
  • DevFS & Device Registration:
    • Register /dev/loop-control (char) at /dev; register /dev/loopN (block) at /dev with special-case unregister logic.
    • gendisk: enable byte read_at/write_at, dynamic metadata sizing, and forward ioctl to LoopDevice.
    • BlockDevManager: Two-phase unregister() with devfs rollback for consistency; uses vectors to stage/remove gendisks.
  • Utilities:
    • ida: add alloc_specific(id) for fixed minor allocation used by loop manager.
    • vfs/file.rs: add FilePrivateData::Loop variant.
  • Docs:
    • Add docs/kernel/device/loop_device.md and index entry describing architecture and design.
  • User-space Tests:
    • New user/apps/c_unitest/test_loop.c covering creation, binding, status change, capacity, concurrent I/O deletion, leak detection, and deletion behavior.

Written by Cursor Bugbot for commit 9752ea7. This will update automatically on new commits. Configure here.

@Vitus213 Vitus213 added the enhancement New feature or request label Aug 19, 2025
@sparkzky sparkzky marked this pull request as draft August 19, 2025 13:48
@Vitus213 Vitus213 marked this pull request as ready for review November 2, 2025 16:41
Copilot AI review requested due to automatic review settings November 2, 2025 16:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR upgrades the Rust toolchain from nightly-2024-11-05 to nightly-2025-08-10 and introduces a loop device driver implementation for DragonOS.

Key changes include:

  • Toolchain version update across all Makefiles and configuration files
  • New loop device driver with complete device management infrastructure
  • Integration of loop devices into the DevFS and block device management systems

Reviewed Changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
user/apps/*/Makefile Updated Rust toolchain version from nightly-2024-11-05 to nightly-2025-08-10
tools/nix-dev-shell/flake.nix Updated Rust toolchain version in Nix dev shell configuration
tools/Makefile Updated cargo toolchain version for check command
build-scripts/Makefile Updated cargo toolchain version for build and check commands
kernel/src/driver/base/block/loop_device.rs New file implementing loop device driver with control device, manager, and device structures
kernel/src/driver/base/block/mod.rs Added loop_device module and removed blank line
kernel/src/driver/base/block/manager.rs Implemented unregister functionality for block devices
kernel/src/driver/base/block/gendisk/mod.rs Added read/write support, dynamic metadata, and ioctl forwarding to underlying loop devices
kernel/src/driver/base/device/device_number.rs Added LOOP_MAJOR constant (7) for loop devices
kernel/src/filesystem/vfs/file.rs Added Loop variant to FilePrivateData enum with typo in comment
kernel/src/filesystem/devfs/mod.rs Added special handling for loop-control and loop* devices in registration and removal
user/apps/c_unitest/test_loop.c New test file for loop device functionality
config/app-blocklist.toml Blocked NovaShell app with typo in toolchain version
Comments suppressed due to low confidence (1)

kernel/src/driver/base/block/loop_device.rs:1

  • Inconsistent indentation: line 47 has incorrect spacing (3 spaces instead of 4) compared to line 46.
use crate::{

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Vitus213 Vitus213 self-assigned this Nov 9, 2025
@Vitus213 Vitus213 requested a review from fslongjin November 9, 2025 09:06
@Vitus213 Vitus213 requested a review from fslongjin December 11, 2025 15:20
- 在IdAllocator中新增alloc_specific方法,允许按用户指定的ID进行分配
- 重构LoopManager,移除next_free_minor字段,简化minor号管理逻辑
- 修改设备查找和创建逻辑,直接使用ID作为minor号,提高代码简洁性

Signed-off-by: longjin <longjin@DragonOS.org>
@github-actions github-actions bot added documentation Improvements or additions to documentation and removed enhancement New feature or request labels Dec 17, 2025
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is being reviewed by Cursor Bugbot

Details

You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Dec 17, 2025
- Make LOOP_SET_STATUS64 update offset/limit and file_size atomically to
  avoid new offset with stale size during concurrent I/O.
- Allow Draining -> Rundown rollback on drain timeout to prevent devices
  getting stuck in Draining and leaking resources.
- Implement LoopDevice/LoopControlDevice IndexNode::fs() to avoid todo!()
  panics by returning DevFS (with /dev fallback).
- Make BlockDevManager::unregister transactional: unregister from devfs
  first, rollback on partial failures, then clear manager/meta state.

Signed-off-by: longjin <longjin@DragonOS.org>
- Introduced a new method `calc_effective_size` to centralize the logic for calculating the effective size based on total size, offset, and size limit.
- Updated `set_file_locked` and `recalc_effective_size` methods to utilize the new calculation method, improving code clarity and reducing duplication.
- Enhanced error handling during effective size recalculation to ensure stability under high concurrency scenarios.
- Added comments to clarify the locking strategy and the importance of atomic updates to prevent inconsistent states.

This refactor aims to improve maintainability and reliability of loop device operations.

Signed-off-by: longjin <longjin@DragonOS.org>
…e and LoopDevice

- Replaced `todo!()` with actual implementation for the `fs` method in both `LoopControlDevice` and `LoopDevice`.
- The new implementation retrieves the filesystem from the device inode, ensuring it is properly set and upgraded.

This change enhances the functionality of loop devices by providing a valid filesystem reference, improving overall stability and reliability.

Signed-off-by: longjin <longjin@DragonOS.org>
…wo-phase rollback mechanism

- Improved the `unregister` method in `BlockDevManager` to implement a two-phase approach for safely unregistering devices from the manager and devfs.
- Added detailed comments to clarify the importance of maintaining atomicity and recoverability during the unregistration process.
- Ensured that device metadata is only cleared after successful unregistration of all devfs nodes, preventing inconsistent states.

This change enhances the reliability of device management by ensuring that failures during unregistration do not leave the system in an unrecoverable state.

Signed-off-by: longjin <longjin@DragonOS.org>
- Introduced a new inline method `device_reusable` to determine if a loop device is in a reusable state, specifically checking for the `Unbound` state.
- Updated device allocation logic to utilize the new method, ensuring that devices in `Rundown`, `Draining`, or `Deleting` states are not mistakenly considered available for reuse.
- Enhanced comments for clarity on device state management during allocation and reuse processes.

This refactor aims to improve the reliability of loop device management by preventing the allocation of devices that are in the process of being deleted.
- Updated the LoopDevice implementation to ensure that the Deleting state returns Ok idempotently, preventing devices from getting stuck in a zombie state after a failed deletion attempt.
- Enhanced comments to clarify the importance of maintaining device state consistency during the unregistration process from BlockDevManager, allowing for retries without resource leaks.

This fix improves the reliability of loop device management by addressing potential deadlock scenarios during device removal.
- Updated the LoopDevice implementation to handle the transition to the Deleting state idempotently, preventing errors when multiple threads attempt to set the state concurrently.
- Enhanced comments to clarify the rationale behind the idempotent handling, ensuring consistent device state management during deletion.
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Dec 18, 2025
- Added a new `LoopStatus` structure to represent legacy loop device status information, including fields for name and encryption key sizes.
- Implemented validation logic for `LoopStatus` parameters to ensure correct offset and flag values during state updates.
- Updated `set_status` and `get_status` methods to handle `LoopStatus`, maintaining compatibility with legacy loop device operations.

This enhancement improves the handling of legacy loop device states and ensures proper validation of input parameters, contributing to overall system stability.
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Dec 18, 2025
- Introduced a new type `KernelOldDevT` for compatibility with legacy loop device structures.
- Updated `LoopStatus` and `LoopStatus64` to use `KernelOldDevT` for device identifiers, ensuring consistency with Linux UAPI.
- Enhanced the test suite for loop devices, improving readability and structure of test output.
- Added comprehensive tests for loop device operations, including read/write, capacity changes, and deletion scenarios.

These changes improve the robustness and maintainability of loop device management, ensuring better alignment with Linux standards and enhancing testing capabilities.
};
drop(inner);
self.recalc_effective_size()?;
Ok(())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: State inconsistency if recalc_effective_size fails after binding

In bind_file, the lock is dropped and then recalc_effective_size() is called. If recalc_effective_size() fails (e.g., due to a transient metadata read failure), the function returns an error but the device state has already been set to Bound with file_inode set. The caller receives an error suggesting binding failed, but subsequent bind attempts would get EBUSY since the device is now Bound. This leaves the device in a usable but caller-confusing state where the error return doesn't match the actual device state.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants