Skip to content

Commit c3a5422

Browse files
Andreas Hindborgaxboe
authored andcommitted
rust: block: use NullTerminatedFormatter
Use the new `NullTerminatedFormatter` to write the name of a `GenDisk` to the name buffer. This new formatter automatically adds a trailing null marker after the written characters, so we don't need to append that at the call site any longer. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Link: https://lore.kernel.org/r/20250902-rnull-up-v6-16-v7-8-b5212cc89b98@kernel.org Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent f4b72f1 commit c3a5422

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

rust/kernel/block/mq/gen_disk.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
88
use crate::{
99
bindings,
10-
block::mq::{raw_writer::RawWriter, Operations, TagSet},
10+
block::mq::{Operations, TagSet},
1111
error::{self, from_err_ptr, Result},
12+
prelude::*,
1213
static_lock_class,
14+
str::NullTerminatedFormatter,
1315
sync::Arc,
1416
};
1517
use core::fmt::{self, Write};
@@ -143,14 +145,14 @@ impl GenDiskBuilder {
143145
// SAFETY: `gendisk` is a valid pointer as we initialized it above
144146
unsafe { (*gendisk).fops = &TABLE };
145147

146-
let mut raw_writer = RawWriter::from_array(
148+
let mut writer = NullTerminatedFormatter::new(
147149
// SAFETY: `gendisk` points to a valid and initialized instance. We
148150
// have exclusive access, since the disk is not added to the VFS
149151
// yet.
150152
unsafe { &mut (*gendisk).disk_name },
151-
)?;
152-
raw_writer.write_fmt(name)?;
153-
raw_writer.write_char('\0')?;
153+
)
154+
.ok_or(EINVAL)?;
155+
writer.write_fmt(name)?;
154156

155157
// SAFETY: `gendisk` points to a valid and initialized instance of
156158
// `struct gendisk`. `set_capacity` takes a lock to synchronize this

rust/kernel/block/mq/raw_writer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl<'a> RawWriter<'a> {
2424
Ok(Self { buffer, pos: 0 })
2525
}
2626

27+
#[expect(dead_code)]
2728
pub(crate) fn from_array<const N: usize>(
2829
a: &'a mut [crate::ffi::c_char; N],
2930
) -> Result<RawWriter<'a>> {

rust/kernel/str.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,6 @@ pub(crate) struct NullTerminatedFormatter<'a> {
886886

887887
impl<'a> NullTerminatedFormatter<'a> {
888888
/// Create a new [`Self`] instance.
889-
#[expect(dead_code)]
890889
pub(crate) fn new(buffer: &'a mut [u8]) -> Option<NullTerminatedFormatter<'a>> {
891890
*(buffer.first_mut()?) = 0;
892891

0 commit comments

Comments
 (0)