]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
rust: block: use `NullTerminatedFormatter`
authorAndreas Hindborg <a.hindborg@kernel.org>
Tue, 2 Sep 2025 09:55:02 +0000 (11:55 +0200)
committerJens Axboe <axboe@kernel.dk>
Tue, 2 Sep 2025 11:23:56 +0000 (05:23 -0600)
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>
rust/kernel/block/mq/gen_disk.rs
rust/kernel/block/mq/raw_writer.rs
rust/kernel/str.rs

index 679ee1bb219509bfc661bee6eeb362f92cb23dfc..20f1d46c774de50293d75c91d3061bee1a7ad5da 100644 (file)
@@ -7,9 +7,11 @@
 
 use crate::{
     bindings,
-    block::mq::{raw_writer::RawWriter, Operations, TagSet},
+    block::mq::{Operations, TagSet},
     error::{self, from_err_ptr, Result},
+    prelude::*,
     static_lock_class,
+    str::NullTerminatedFormatter,
     sync::Arc,
 };
 use core::fmt::{self, Write};
@@ -143,14 +145,14 @@ impl GenDiskBuilder {
         // SAFETY: `gendisk` is a valid pointer as we initialized it above
         unsafe { (*gendisk).fops = &TABLE };
 
-        let mut raw_writer = RawWriter::from_array(
+        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 },
-        )?;
-        raw_writer.write_fmt(name)?;
-        raw_writer.write_char('\0')?;
+        )
+        .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
index 7e2159e4f6a6f7b5de4e414e6b349a4624f3a72e..0aef55703e71d03e385195cc522f4630af7f5a8a 100644 (file)
@@ -24,6 +24,7 @@ impl<'a> RawWriter<'a> {
         Ok(Self { buffer, pos: 0 })
     }
 
+    #[expect(dead_code)]
     pub(crate) fn from_array<const N: usize>(
         a: &'a mut [crate::ffi::c_char; N],
     ) -> Result<RawWriter<'a>> {
index d2f9ebc94b752a1d19a8cfed313b037d6cd7b3a8..5c74e5f776014aba2027dae4e771e31c67e2c346 100644 (file)
@@ -886,7 +886,6 @@ pub(crate) struct NullTerminatedFormatter<'a> {
 
 impl<'a> NullTerminatedFormatter<'a> {
     /// Create a new [`Self`] instance.
-    #[expect(dead_code)]
     pub(crate) fn new(buffer: &'a mut [u8]) -> Option<NullTerminatedFormatter<'a>> {
         *(buffer.first_mut()?) = 0;