Add a method that limits the remaining length of a `Segment` without moving its offset. This complements `Segment::advance`, which can skip data at the front but cannot trim data at the back, and gives callers a way to clip a segment to a maximum byte count before handing it to the existing `copy_to_page` / `copy_from_page` / `zero_page` helpers, which already bound themselves by `Segment::len()`. This is needed by rnull's partial bad-block I/O path, which needs to clamp per-segment work to a sector boundary computed from the bad-block range. Signed-off-by: Andreas Hindborg --- rust/kernel/block/bio/vec.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rust/kernel/block/bio/vec.rs b/rust/kernel/block/bio/vec.rs index 99ab164d4038..61d83a07397f 100644 --- a/rust/kernel/block/bio/vec.rs +++ b/rust/kernel/block/bio/vec.rs @@ -81,6 +81,18 @@ pub fn advance(&mut self, count: u32) -> Result { Ok(()) } + /// Limit the remaining length of the segment. + /// + /// Shortens the segment to at most `new_len` bytes. If `new_len` is + /// greater than or equal to the current remaining length, the segment is + /// left unchanged. The offset is not modified, so subsequent copy + /// operations still start from the current position. + pub fn truncate(&mut self, new_len: u32) { + if new_len < self.len() { + self.bio_vec.bv_len = new_len; + } + } + /// Copy data of this segment into `dst_page`. /// /// Copies data from the current offset to the next page boundary. That is `PAGE_SIZE - -- 2.51.2