Skip to content

Comments

Ctrl cleanup#29

Merged
ming1 merged 7 commits intomainfrom
ctrl-cleanup
Sep 5, 2025
Merged

Ctrl cleanup#29
ming1 merged 7 commits intomainfrom
ctrl-cleanup

Conversation

@ming1
Copy link
Collaborator

@ming1 ming1 commented Sep 5, 2025

405caeb refactor: consolidate error handling patterns and eliminate panics

bd74ec3 refactor: eliminate command preparation boilerplate with helper methods

081bb25 refactor: break down UblkCtrlInner constructor complexity

db0e695 feat: example/null: set UBLK_DEV_F_SINGLE_CPU_AFFINITY default

9e8cbdf refactor: consolidate and improve CPU affinity management in ctrl.rs

7c316b0 refactor: extract JSON management into UblkJsonManager for ctrl.rs cleanup

- Add unified error handling helpers: sys_result_to_error, validate_param,
  validate_queue_id, and validate_thread_id
- Replace scattered error handling patterns with consolidated approaches
- Simplify parameter validation in UblkCtrl::new() using helper functions
- Improve code consistency by standardizing error return patterns
- Reduce code duplication across 25+ error handling locations
- Enhance maintainability by centralizing error logic
…eanup

MAJOR: Extract JSON Management by Create UblkJsonManager for all persistence operations

This commit extracts all JSON-related functionality from UblkCtrlInner into a
dedicated UblkJsonManager struct. This significant refactoring addresses the
following:

Changes:
- Created UblkJsonManager struct to handle all JSON operations
- Moved JSON file I/O, serialization, and deserialization logic
- Consolidated all JSON path management and permission handling
- Extracted target data retrieval methods into the manager
- Fixed JSON build logic to properly handle empty vs populated JSON
- Simplified UblkCtrlInner by delegating to json_manager

Benefits:
- Improved separation of concerns
- Cleaner, more maintainable code structure
- Centralized JSON management logic
- Better error handling for JSON operations
- Consistent file permission management

Tests: All unit tests pass, integration with existing functionality verified

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
- Add AffinityMode enum for unified affinity configuration interface
- Extract affinity helper functions into dedicated UblkCtrlInner impl block:
  - get_queue_affinity_effective(): handles single CPU vs multi-CPU modes
  - select_single_cpu_for_queue(): CPU selection and storage logic
  - create_thread_affinity(): appropriate affinity for thread setup
- Enhance UblkQueueAffinity with utility methods (is_empty, is_single_cpu, get_first_cpu)
- Simplify build_json() affinity logic from 15 lines to 3 lines
- Streamline calculate_queue_affinity() and set_queue_single_affinity() methods

This refactoring reduces code duplication, improves maintainability, and provides
a cleaner API for managing CPU affinity in ublk devices while preserving all
existing functionality.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
- Extract helper methods from 71-line constructor into focused functions:
  - create_device_info(): Device info structure creation
  - open_control_device(): File operations with proper error handling
  - init_queue_data(): Queue data structure initialization
  - handle_device_lifecycle(): Add/recover device logic
  - detect_features(): Driver feature detection
- Add UblkCtrlConfig struct to eliminate too_many_arguments warnings
- Create new(config) method with clean configuration approach
- Add new_with_params() legacy wrapper for backward compatibility
- Remove #[allow(clippy::too_many_arguments)] from main constructor
- Improve error handling with proper propagation and context

Reduces main constructor from 71 lines to 35 lines while maintaining
full backward compatibility and improving code organization.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
- Add 5 UblkCtrlCmdData creation helper methods:
  - new_simple_cmd(): Commands with no data or buffer
  - new_data_cmd(): Commands with data only
  - new_read_buffer_cmd(): Commands with read buffers
  - new_write_buffer_cmd(): Commands with write buffers
  - new_data_buffer_cmd(): Commands with both data and buffer
- Refactor 18 command functions to use helper methods instead of manual struct initialization:
  - add(), del(), del_async(), __get_features(), __read_dev_info(), __read_dev_info2()
  - start(), start_async(), stop(), get_params(), set_params()
  - get_queue_affinity(), __start_user_recover(), end_user_recover(), end_user_recover_async()
- Reduce manual UblkCtrlCmdData struct initializations from ~18 to 2
- Improve code clarity with self-documenting method names
- Eliminate error-prone manual flag combinations
- Ensure consistent command preparation patterns across all functions

This reduces boilerplate code significantly while improving maintainability
and reducing the potential for flag-related bugs in command preparation.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
- Replace critical expect() calls with graceful error handling in JSON operations:
  - dump_json() now uses let Ok(..) else pattern for file operations
  - JSON parsing failures print warnings instead of panicking
- Improve RwLock error handling with poison recovery:
  - get_inner() and get_inner_mut() now recover from poisoned locks
  - Prevents application crashes on lock poisoning scenarios
- Enhance thread communication error handling:
  - Channel send/receive operations now handle failures gracefully
  - Thread spawning continues operation on communication failures
- Fix io_uring submission queue error handling:
  - SQE push operations now check for errors instead of unwrapping
  - Print warnings for submission failures rather than panicking
- Improve error flow patterns:
  - read_dev_info() uses idiomatic or_else() for fallback logic
  - Queue configuration errors now include detailed error context
  - Replace println! with eprintln! for proper error output

Reduces problematic unwrap/expect patterns from ~52 to 20 instances (~38% improvement)
while maintaining functionality and improving system reliability through graceful
error recovery instead of panics.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
@ming1 ming1 merged commit 4178a87 into main Sep 5, 2025
8 checks passed
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.

1 participant