Move DeviceId to separate device-id crate

This commit is contained in:
Qingsong Chen 2025-11-13 02:39:19 +00:00 committed by Tate, Hongliang Tian
parent 9c6dbcee79
commit bb48b3814f
20 changed files with 123 additions and 69 deletions

5
Cargo.lock generated
View File

@ -223,6 +223,7 @@ dependencies = [
"controlled",
"core2",
"cpio-decoder",
"device-id",
"getset",
"hashbrown 0.14.5",
"id-alloc",
@ -665,6 +666,10 @@ dependencies = [
"powerfmt",
]
[[package]]
name = "device-id"
version = "0.1.0"
[[package]]
name = "either"
version = "1.15.0"

View File

@ -33,6 +33,7 @@ members = [
"kernel/libs/aster-rights-proc",
"kernel/libs/aster-util",
"kernel/libs/aster-bigtcp",
"kernel/libs/device-id",
"kernel/libs/jhash",
"kernel/libs/keyable-arc",
"kernel/libs/logo-ascii-art",

View File

@ -183,6 +183,7 @@ NON_OSDK_CRATES := \
kernel/libs/aster-rights-proc \
kernel/libs/atomic-integer-wrapper \
kernel/libs/cpio-decoder \
kernel/libs/device-id \
kernel/libs/int-to-c-enum \
kernel/libs/int-to-c-enum/derive \
kernel/libs/jhash \

View File

@ -32,6 +32,7 @@ aster-rights-proc = { path = "libs/aster-rights-proc" }
aster-util = { path = "libs/aster-util" }
aster-bigtcp = { path = "libs/aster-bigtcp" }
atomic-integer-wrapper = { path = "libs/atomic-integer-wrapper" }
device-id = { path = "libs/device-id" }
id-alloc = { path = "../ostd/libs/id-alloc" }
int-to-c-enum = { path = "libs/int-to-c-enum" }
jhash = { path = "libs/jhash" }

View File

@ -0,0 +1,11 @@
[package]
name = "device-id"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[lints]
workspace = true

View File

@ -0,0 +1,70 @@
// SPDX-License-Identifier: MPL-2.0
//! A module represents Linux kernel device IDs.
//!
//! In Linux, each device is identified by a **major** number and a **minor** number.
//! These two numbers together form a unique identifier for the device, which is used
//! in device files (e.g., `/dev/sda`, `/dev/null`) and system calls like `mknod`.
//!
//! For more information about device ID allocation and usage in Linux, see:
//! <https://www.kernel.org/doc/Documentation/admin-guide/devices.txt>
#![no_std]
#![deny(unsafe_code)]
/// A device ID, containing a major device number and a minor device number.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DeviceId {
major: u32,
minor: u32,
}
impl DeviceId {
/// Creates a device ID from the major device number and the minor device number.
pub fn new(major: u32, minor: u32) -> Self {
Self { major, minor }
}
/// Returns the major device number.
pub fn major(&self) -> u32 {
self.major
}
/// Returns the minor device number.
pub fn minor(&self) -> u32 {
self.minor
}
}
impl DeviceId {
/// Creates a device ID from the encoded `u64` value.
///
/// See [`as_encoded_u64`] for details about how to encode a device ID to a `u64` value.
///
/// [`as_encoded_u64`]: Self::as_encoded_u64
pub fn from_encoded_u64(raw: u64) -> Self {
let major = ((raw >> 32) & 0xffff_f000 | (raw >> 8) & 0x0000_0fff) as u32;
let minor = ((raw >> 12) & 0xffff_ff00 | raw & 0x0000_00ff) as u32;
Self::new(major, minor)
}
/// Encodes the device ID as a `u64` value.
///
/// The lower 32 bits use the same encoding strategy as Linux. See the Linux implementation at:
/// <https://github.com/torvalds/linux/blob/0ff41df1cb268fc69e703a08a57ee14ae967d0ca/include/linux/kdev_t.h#L39-L44>.
///
/// If the major or minor device number is too large, the additional bits will be recorded
/// using the higher 32 bits. Note that as of 2025, the Linux kernel still has no support for
/// 64-bit device IDs:
/// <https://github.com/torvalds/linux/blob/0ff41df1cb268fc69e703a08a57ee14ae967d0ca/include/linux/types.h#L18>.
/// So this encoding follows the implementation in glibc:
/// <https://github.com/bminor/glibc/blob/632d895f3e5d98162f77b9c3c1da4ec19968b671/bits/sysmacros.h#L26-L34>.
pub fn as_encoded_u64(&self) -> u64 {
let major = self.major() as u64;
let minor = self.minor() as u64;
((major & 0xffff_f000) << 32)
| ((major & 0x0000_0fff) << 8)
| ((minor & 0xffff_ff00) << 12)
| (minor & 0x0000_00ff)
}
}

View File

@ -1,9 +1,11 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use crate::{
events::IoEvents,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
inode_handle::FileIo,
utils::StatusFlags,
},

View File

@ -14,13 +14,14 @@ pub mod tdxguest;
use alloc::format;
use device_id::DeviceId;
pub use pty::{new_pty_pair, PtyMaster, PtySlave};
pub use random::Random;
pub use urandom::Urandom;
use crate::{
fs::{
device::{add_node, Device, DeviceId},
device::{add_node, Device},
fs_resolver::FsPath,
path::PerMountFlags,
ramfs::RamFs,

View File

@ -1,9 +1,11 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use crate::{
events::IoEvents,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
inode_handle::FileIo,
utils::StatusFlags,
},

View File

@ -1,10 +1,12 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use super::Urandom;
use crate::{
events::IoEvents,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
inode_handle::FileIo,
utils::StatusFlags,
},

View File

@ -4,6 +4,7 @@ use core::{mem::size_of, time::Duration};
use align_ext::AlignExt;
use aster_util::{field_ptr, safe_ptr::SafePtr};
use device_id::DeviceId;
use ostd::{
mm::{DmaCoherent, FrameAllocOptions, HasPaddr, HasSize, VmIo, PAGE_SIZE},
sync::WaitQueue,
@ -17,7 +18,7 @@ use tdx_guest::{
use crate::{
events::IoEvents,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
inode_handle::FileIo,
utils::{IoctlCmd, StatusFlags},
},

View File

@ -1,9 +1,11 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use crate::{
events::IoEvents,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
inode_handle::FileIo,
utils::StatusFlags,
},

View File

@ -5,6 +5,7 @@ use aster_console::{
mode::{ConsoleMode, KeyboardMode},
AnyConsoleDevice,
};
use device_id::DeviceId;
use ostd::sync::LocalIrqDisabled;
use self::{line_discipline::LineDiscipline, termio::CFontOp};
@ -12,7 +13,7 @@ use crate::{
current_userspace,
events::IoEvents,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
inode_handle::FileIo,
utils::{IoctlCmd, StatusFlags},
},

View File

@ -1,9 +1,11 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use crate::{
events::IoEvents,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
inode_handle::FileIo,
utils::StatusFlags,
},

View File

@ -1,9 +1,11 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use crate::{
events::IoEvents,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
inode_handle::FileIo,
utils::StatusFlags,
},

View File

@ -1,5 +1,7 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use super::inode_handle::FileIo;
use crate::{
fs::{
@ -43,63 +45,6 @@ pub enum DeviceType {
Misc,
}
/// A device ID, containing a major device number and a minor device number.
#[derive(Clone, Copy, Debug)]
pub struct DeviceId {
major: u32,
minor: u32,
}
impl DeviceId {
/// Creates a device ID from the major device number and the minor device number.
pub fn new(major: u32, minor: u32) -> Self {
Self { major, minor }
}
/// Returns the major device number.
pub fn major(&self) -> u32 {
self.major
}
/// Returns the minor device number.
pub fn minor(&self) -> u32 {
self.minor
}
}
impl DeviceId {
/// Creates a device ID from the encoded `u64` value.
///
/// See [`as_encoded_u64`] for details about how to encode a device ID to a `u64` value.
///
/// [`as_encoded_u64`]: Self::as_encoded_u64
pub fn from_encoded_u64(raw: u64) -> Self {
let major = ((raw >> 32) & 0xffff_f000 | (raw >> 8) & 0x0000_0fff) as u32;
let minor = ((raw >> 12) & 0xffff_ff00 | raw & 0x0000_00ff) as u32;
Self::new(major, minor)
}
/// Encodes the device ID as a `u64` value.
///
/// The lower 32 bits use the same encoding strategy as Linux. See the Linux implementation at:
/// <https://github.com/torvalds/linux/blob/0ff41df1cb268fc69e703a08a57ee14ae967d0ca/include/linux/kdev_t.h#L39-L44>.
///
/// If the major or minor device number is too large, the additional bits will be recorded
/// using the higher 32 bits. Note that as of 2025, the Linux kernel still has no support for
/// 64-bit device IDs:
/// <https://github.com/torvalds/linux/blob/0ff41df1cb268fc69e703a08a57ee14ae967d0ca/include/linux/types.h#L18>.
/// So this encoding follows the implementation in glibc:
/// <https://github.com/bminor/glibc/blob/632d895f3e5d98162f77b9c3c1da4ec19968b671/bits/sysmacros.h#L26-L34>.
pub fn as_encoded_u64(&self) -> u64 {
let major = self.major() as u64;
let minor = self.minor() as u64;
((major & 0xffff_f000) << 32)
| ((major & 0x0000_0fff) << 8)
| ((minor & 0xffff_ff00) << 12)
| (minor & 0x0000_00ff)
}
}
/// Adds a device node in `/dev`.
///
/// If the parent path does not exist, it will be created as a directory.

View File

@ -13,7 +13,7 @@ use super::utils::MknodType;
use crate::{
device::PtyMaster,
fs::{
device::{Device, DeviceId, DeviceType},
device::{Device, DeviceType},
registry::{FsProperties, FsType},
utils::{
mkmod, DirEntryVecExt, DirentVisitor, FileSystem, FsFlags, Inode, InodeMode, InodeType,

View File

@ -1,5 +1,7 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use super::*;
use crate::{
events::IoEvents,

View File

@ -1,10 +1,11 @@
// SPDX-License-Identifier: MPL-2.0
use device_id::DeviceId;
use super::SyscallReturn;
use crate::{
device::get_device,
fs::{
device::DeviceId,
file_table::FileDesc,
fs_resolver::{FsPath, AT_FDCWD},
utils::{InodeMode, InodeType, MknodType},

View File

@ -2,9 +2,11 @@
use core::time::Duration;
use device_id::DeviceId;
use super::SyscallReturn;
use crate::{
fs::{device::DeviceId, file_table::FileDesc, fs_resolver::FsPath, utils::Metadata},
fs::{file_table::FileDesc, fs_resolver::FsPath, utils::Metadata},
prelude::*,
syscall::constants::MAX_FILENAME_LEN,
};