mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-01-15 11:03:02 +00:00
-----BEGIN PGP SIGNATURE-----
iQJEBAABCAAuFiEEwPw5LcreJtl1+l5K99NY+ylx4KYFAmjbLCgQHGF4Ym9lQGtl
cm5lbC5kawAKCRD301j7KXHgpoY0D/9J+11BC88pBxCrLKv/V2TwCNokRMi0dU3L
r3EUdA46k0oXmvb6ueZqIcfY2e+IX7rdQkaRbh1zRdsNejqHo4548C3ePWGdBAcM
OdNEGfpehO0aD0td1+mK/NxoJMLhbs5QraPanz+SOkGZOKeF+vGCga5PUDivsr5J
16T9yb7i+isENLdAc2RJbZVyAphqHQlo5GHi5ZIKOVi5cNt8GU/q2sQl7NYmGvHd
aq37svvZHFOhLRajP959Fw9WOxEYITewzQ4UYf1FZjUodJUxO+vCnP0ooBQRlyu8
1B4PYWwSE+Vn3GkQE0Om+mzo9AVPOiLmoAWGxdgJBMyEkZndocr46XEslXOufQ1Z
T3Gu19G6jCxcyByNVhjVnaajYKmvSQAy1w75m4XlfqTRm4f9Om+LAJavUk3RuaOL
7lXKQ7Ql1/Tby9Jmf8afjYYXXotNDNku6rz2P3qtOwAA26mNJfgVt0rO+8XGRDe9
ioLbCkTjslYMc/Oh4jSsbrspsVALbaQMq/Dmah8k0EWb4QAHVgCJyGBoff3hOboI
jD6B1enaKOQVgcjWcjm/FjOk3jv2h3v4X26YWQZTvEc/1PnSnST78Zi/ePhzDdmt
sBALUAS37TfTgNMzrhbHl5Zs13k0C0XyANuayuKuo5hlNnC1wbdap+5FZJOmpuOB
YT+VkYnaOA==
=kOmc
-----END PGP SIGNATURE-----
Merge tag 'for-6.18/block-20250929' of git://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux
Pull block updates from Jens Axboe:
- NVMe pull request via Keith:
- FC target fixes (Daniel)
- Authentication fixes and updates (Martin, Chris)
- Admin controller handling (Kamaljit)
- Target lockdep assertions (Max)
- Keep-alive updates for discovery (Alastair)
- Suspend quirk (Georg)
- MD pull request via Yu:
- Add support for a lockless bitmap.
A key feature for the new bitmap are that the IO fastpath is
lockless. If a user issues lots of write IO to the same bitmap
bit in a short time, only the first write has additional overhead
to update bitmap bit, no additional overhead for the following
writes.
By supporting only resync or recover written data, means in the
case creating new array or replacing with a new disk, there is no
need to do a full disk resync/recovery.
- Switch ->getgeo() and ->bios_param() to using struct gendisk rather
than struct block_device.
- Rust block changes via Andreas. This series adds configuration via
configfs and remote completion to the rnull driver. The series also
includes a set of changes to the rust block device driver API: a few
cleanup patches, and a few features supporting the rnull changes.
The series removes the raw buffer formatting logic from
`kernel::block` and improves the logic available in `kernel::string`
to support the same use as the removed logic.
- floppy arch cleanups
- Reduce the number of dereferencing needed for ublk commands
- Restrict supported sockets for nbd. Mostly done to eliminate a class
of issues perpetually reported by syzbot, by using nonsensical socket
setups.
- A few s390 dasd block fixes
- Fix a few issues around atomic writes
- Improve DMA interation for integrity requests
- Improve how iovecs are treated with regards to O_DIRECT aligment
constraints.
We used to require each segment to adhere to the constraints, now
only the request as a whole needs to.
- Clean up and improve p2p support, enabling use of p2p for metadata
payloads
- Improve locking of request lookup, using SRCU where appropriate
- Use page references properly for brd, avoiding very long RCU sections
- Fix ordering of recursively submitted IOs
- Clean up and improve updating nr_requests for a live device
- Various fixes and cleanups
* tag 'for-6.18/block-20250929' of git://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux: (164 commits)
s390/dasd: enforce dma_alignment to ensure proper buffer validation
s390/dasd: Return BLK_STS_INVAL for EINVAL from do_dasd_request
ublk: remove redundant zone op check in ublk_setup_iod()
nvme: Use non zero KATO for persistent discovery connections
nvmet: add safety check for subsys lock
nvme-core: use nvme_is_io_ctrl() for I/O controller check
nvme-core: do ioccsz/iorcsz validation only for I/O controllers
nvme-core: add method to check for an I/O controller
blk-cgroup: fix possible deadlock while configuring policy
blk-mq: fix null-ptr-deref in blk_mq_free_tags() from error path
blk-mq: Fix more tag iteration function documentation
selftests: ublk: fix behavior when fio is not installed
ublk: don't access ublk_queue in ublk_unmap_io()
ublk: pass ublk_io to __ublk_complete_rq()
ublk: don't access ublk_queue in ublk_need_complete_req()
ublk: don't access ublk_queue in ublk_check_commit_and_fetch()
ublk: don't pass ublk_queue to ublk_fetch()
ublk: don't access ublk_queue in ublk_config_io_buf()
ublk: don't access ublk_queue in ublk_check_fetch_buf()
ublk: pass q_id and tag to __ublk_check_and_get_req()
...
227 lines
8.0 KiB
Rust
227 lines
8.0 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//! Generic disk abstraction.
|
|
//!
|
|
//! C header: [`include/linux/blkdev.h`](srctree/include/linux/blkdev.h)
|
|
//! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h)
|
|
|
|
use crate::{
|
|
bindings,
|
|
block::mq::{Operations, TagSet},
|
|
error::{self, from_err_ptr, Result},
|
|
fmt::{self, Write},
|
|
prelude::*,
|
|
static_lock_class,
|
|
str::NullTerminatedFormatter,
|
|
sync::Arc,
|
|
types::{ForeignOwnable, ScopeGuard},
|
|
};
|
|
|
|
/// A builder for [`GenDisk`].
|
|
///
|
|
/// Use this struct to configure and add new [`GenDisk`] to the VFS.
|
|
pub struct GenDiskBuilder {
|
|
rotational: bool,
|
|
logical_block_size: u32,
|
|
physical_block_size: u32,
|
|
capacity_sectors: u64,
|
|
}
|
|
|
|
impl Default for GenDiskBuilder {
|
|
fn default() -> Self {
|
|
Self {
|
|
rotational: false,
|
|
logical_block_size: bindings::PAGE_SIZE as u32,
|
|
physical_block_size: bindings::PAGE_SIZE as u32,
|
|
capacity_sectors: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl GenDiskBuilder {
|
|
/// Create a new instance.
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// Set the rotational media attribute for the device to be built.
|
|
pub fn rotational(mut self, rotational: bool) -> Self {
|
|
self.rotational = rotational;
|
|
self
|
|
}
|
|
|
|
/// Validate block size by verifying that it is between 512 and `PAGE_SIZE`,
|
|
/// and that it is a power of two.
|
|
pub fn validate_block_size(size: u32) -> Result {
|
|
if !(512..=bindings::PAGE_SIZE as u32).contains(&size) || !size.is_power_of_two() {
|
|
Err(error::code::EINVAL)
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// Set the logical block size of the device to be built.
|
|
///
|
|
/// This method will check that block size is a power of two and between 512
|
|
/// and 4096. If not, an error is returned and the block size is not set.
|
|
///
|
|
/// This is the smallest unit the storage device can address. It is
|
|
/// typically 4096 bytes.
|
|
pub fn logical_block_size(mut self, block_size: u32) -> Result<Self> {
|
|
Self::validate_block_size(block_size)?;
|
|
self.logical_block_size = block_size;
|
|
Ok(self)
|
|
}
|
|
|
|
/// Set the physical block size of the device to be built.
|
|
///
|
|
/// This method will check that block size is a power of two and between 512
|
|
/// and 4096. If not, an error is returned and the block size is not set.
|
|
///
|
|
/// This is the smallest unit a physical storage device can write
|
|
/// atomically. It is usually the same as the logical block size but may be
|
|
/// bigger. One example is SATA drives with 4096 byte physical block size
|
|
/// that expose a 512 byte logical block size to the operating system.
|
|
pub fn physical_block_size(mut self, block_size: u32) -> Result<Self> {
|
|
Self::validate_block_size(block_size)?;
|
|
self.physical_block_size = block_size;
|
|
Ok(self)
|
|
}
|
|
|
|
/// Set the capacity of the device to be built, in sectors (512 bytes).
|
|
pub fn capacity_sectors(mut self, capacity: u64) -> Self {
|
|
self.capacity_sectors = capacity;
|
|
self
|
|
}
|
|
|
|
/// Build a new `GenDisk` and add it to the VFS.
|
|
pub fn build<T: Operations>(
|
|
self,
|
|
name: fmt::Arguments<'_>,
|
|
tagset: Arc<TagSet<T>>,
|
|
queue_data: T::QueueData,
|
|
) -> Result<GenDisk<T>> {
|
|
let data = queue_data.into_foreign();
|
|
let recover_data = ScopeGuard::new(|| {
|
|
// SAFETY: T::QueueData was created by the call to `into_foreign()` above
|
|
drop(unsafe { T::QueueData::from_foreign(data) });
|
|
});
|
|
|
|
// SAFETY: `bindings::queue_limits` contain only fields that are valid when zeroed.
|
|
let mut lim: bindings::queue_limits = unsafe { core::mem::zeroed() };
|
|
|
|
lim.logical_block_size = self.logical_block_size;
|
|
lim.physical_block_size = self.physical_block_size;
|
|
if self.rotational {
|
|
lim.features = bindings::BLK_FEAT_ROTATIONAL;
|
|
}
|
|
|
|
// SAFETY: `tagset.raw_tag_set()` points to a valid and initialized tag set
|
|
let gendisk = from_err_ptr(unsafe {
|
|
bindings::__blk_mq_alloc_disk(
|
|
tagset.raw_tag_set(),
|
|
&mut lim,
|
|
data,
|
|
static_lock_class!().as_ptr(),
|
|
)
|
|
})?;
|
|
|
|
const TABLE: bindings::block_device_operations = bindings::block_device_operations {
|
|
submit_bio: None,
|
|
open: None,
|
|
release: None,
|
|
ioctl: None,
|
|
compat_ioctl: None,
|
|
check_events: None,
|
|
unlock_native_capacity: None,
|
|
getgeo: None,
|
|
set_read_only: None,
|
|
swap_slot_free_notify: None,
|
|
report_zones: None,
|
|
devnode: None,
|
|
alternative_gpt_sector: None,
|
|
get_unique_id: None,
|
|
// TODO: Set to THIS_MODULE. Waiting for const_refs_to_static feature to
|
|
// be merged (unstable in rustc 1.78 which is staged for linux 6.10)
|
|
// <https://github.com/rust-lang/rust/issues/119618>
|
|
owner: core::ptr::null_mut(),
|
|
pr_ops: core::ptr::null_mut(),
|
|
free_disk: None,
|
|
poll_bio: None,
|
|
};
|
|
|
|
// SAFETY: `gendisk` is a valid pointer as we initialized it above
|
|
unsafe { (*gendisk).fops = &TABLE };
|
|
|
|
let mut writer = NullTerminatedFormatter::new(
|
|
// SAFETY: `gendisk` points to a valid and initialized instance. We
|
|
// have exclusive access, since the disk is not added to the VFS
|
|
// yet.
|
|
unsafe { &mut (*gendisk).disk_name },
|
|
)
|
|
.ok_or(EINVAL)?;
|
|
writer.write_fmt(name)?;
|
|
|
|
// SAFETY: `gendisk` points to a valid and initialized instance of
|
|
// `struct gendisk`. `set_capacity` takes a lock to synchronize this
|
|
// operation, so we will not race.
|
|
unsafe { bindings::set_capacity(gendisk, self.capacity_sectors) };
|
|
|
|
crate::error::to_result(
|
|
// SAFETY: `gendisk` points to a valid and initialized instance of
|
|
// `struct gendisk`.
|
|
unsafe {
|
|
bindings::device_add_disk(core::ptr::null_mut(), gendisk, core::ptr::null_mut())
|
|
},
|
|
)?;
|
|
|
|
recover_data.dismiss();
|
|
|
|
// INVARIANT: `gendisk` was initialized above.
|
|
// INVARIANT: `gendisk` was added to the VFS via `device_add_disk` above.
|
|
// INVARIANT: `gendisk.queue.queue_data` is set to `data` in the call to
|
|
// `__blk_mq_alloc_disk` above.
|
|
Ok(GenDisk {
|
|
_tagset: tagset,
|
|
gendisk,
|
|
})
|
|
}
|
|
}
|
|
|
|
/// A generic block device.
|
|
///
|
|
/// # Invariants
|
|
///
|
|
/// - `gendisk` must always point to an initialized and valid `struct gendisk`.
|
|
/// - `gendisk` was added to the VFS through a call to
|
|
/// `bindings::device_add_disk`.
|
|
/// - `self.gendisk.queue.queuedata` is initialized by a call to `ForeignOwnable::into_foreign`.
|
|
pub struct GenDisk<T: Operations> {
|
|
_tagset: Arc<TagSet<T>>,
|
|
gendisk: *mut bindings::gendisk,
|
|
}
|
|
|
|
// SAFETY: `GenDisk` is an owned pointer to a `struct gendisk` and an `Arc` to a
|
|
// `TagSet` It is safe to send this to other threads as long as T is Send.
|
|
unsafe impl<T: Operations + Send> Send for GenDisk<T> {}
|
|
|
|
impl<T: Operations> Drop for GenDisk<T> {
|
|
fn drop(&mut self) {
|
|
// SAFETY: By type invariant of `Self`, `self.gendisk` points to a valid
|
|
// and initialized instance of `struct gendisk`, and, `queuedata` was
|
|
// initialized with the result of a call to
|
|
// `ForeignOwnable::into_foreign`.
|
|
let queue_data = unsafe { (*(*self.gendisk).queue).queuedata };
|
|
|
|
// SAFETY: By type invariant, `self.gendisk` points to a valid and
|
|
// initialized instance of `struct gendisk`, and it was previously added
|
|
// to the VFS.
|
|
unsafe { bindings::del_gendisk(self.gendisk) };
|
|
|
|
// SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with
|
|
// a call to `ForeignOwnable::into_foreign` to create `queuedata`.
|
|
// `ForeignOwnable::from_foreign` is only called here.
|
|
drop(unsafe { T::QueueData::from_foreign(queue_data) });
|
|
}
|
|
}
|