From nobody Sat Oct 4 00:22:24 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78B612ECD38 for ; Fri, 22 Aug 2025 08:42:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852175; cv=none; b=TSwX/h/18IGsdqBQJ6pozBLDmTSJTaxZc9gcGgKGWcEAt77eIHAuve8LTB7a4ZKorYS9/g1+Z20/xfW0rQMoJeVxaf900ckQWGUb08mr5wwmTv0MiKEIAy44uKAESEyzoKnfEcLuq3QgTqxPvTFpw39J/igaz30zzo1/kbDze60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852175; c=relaxed/simple; bh=pEFhG0haAU3GTGryN2WieVcKPQ715Oa917yCHNmiuvA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uv81ABKyrXXlPJwH7KFDbIt8kgXIzJaMqmIkJN/v8rXZMO8NALxLtGHLffD+02MncdguRrKtCAt/hBPUCQ13Oqj9fipgx3FcpgF0BdM934fhwm3pDr3BAcjvC4Kveh3FcTZsQrAWsgYDAwLtY7OqdeHDa4tFHgYAXjcf4TFxWkA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WHGEX1jO; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WHGEX1jO" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-45a1b0ccb6cso9725595e9.3 for ; Fri, 22 Aug 2025 01:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755852172; x=1756456972; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Qjj30volqfs6w7L9vkmy/27FL2MqFj9qU1x6ao4EmPI=; b=WHGEX1jOZxZtkELQiJrt61F8xZKWglR60cTGY4ggHiamOVjhPRLuZqkdrrEYA3T6hq DVSduN8arok6ZTD0XqFmQqFHOSV7YL7gxVbknBRzSOf276ljOBceW76E/tYeEk4Pm/av Iz5eJXOVxDfihWHp2R71udM2Z7lVou3ofIpRjF8QaeoTy8cbFIQv2A4jt2olRCgN4z23 xMSpPa4TXaEr5tpVfGjqBqlYUFEaTIAqJtTh/6DmNHDzlUQpSJiuH1eHmqNaWT0RZrFM XSz/4p0Te+ZCirM2s1xudmkm9xNROdI+7B1Xf3vaHqjo9QcezAvWPqHFfYAs9qLJaNWq f9IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755852172; x=1756456972; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Qjj30volqfs6w7L9vkmy/27FL2MqFj9qU1x6ao4EmPI=; b=UY/LhnCMJfbf8bZTCzGPL8YlaFnVLB5yuEUfUBBB476RBbKCZmlU6zqzQGMdkyFHFD /cxq93eZyPMdbuf7GgNMYgzmcPDptLXMAdKuZuor8V1KoV6X1wk17h5EIo0NaVyMn0MI pnPuwIYN8+lwBTfs/XYfzgcGgPQ11vva9YBufRkRyHpLH6HM8OHIS26SPk0P6PWPvs0p cjzvQ8AuQrA3jg4Wn5gaYO8FpZbR4oYSAzZJ5WkyBWv0bYNJgg8Nb8hUOKeEY3oH4jQC 7bbBVZNMq1GFoc7B8DxM+HGYp18lHWW6FlviR3jMchiaRW60psyEenz8FIBrb+EXSRkR 3TWg== X-Forwarded-Encrypted: i=1; AJvYcCVBnw6GPtgl2YgzbwbbYwHFuTF1olsyDArt09YSlX7uJ1Z6dUYOscXpFBBYV01N45yUgwHLi7W4UtVwNzY=@vger.kernel.org X-Gm-Message-State: AOJu0YwLxXXLqKwGHDj7Ihcf5eJR0+YQiThbLoA8932GFbloW31ZyWjR vPZcc8pX+VjBIQa8SoEd2H+L+bid1wG8PX/dRuv5zKn6aTFIgVPSvrdkYyHx64DRpHcqZ/ZMasX AYO/AR3DUqX4GFy+VRA== X-Google-Smtp-Source: AGHT+IHxHQFqWDULQYicIlutHzD9qOw+A69Wxr2UrhdnZx18qXPqIyxmWSW/+I752DN8ZrWOOX9rLabeqfTz+2s= X-Received: from wmox22.prod.google.com ([2002:a05:600c:1796:b0:459:dbaa:93aa]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:314c:b0:45b:43cc:e55a with SMTP id 5b1f17b1804b1-45b517cca69mr11960315e9.37.1755852171849; Fri, 22 Aug 2025 01:42:51 -0700 (PDT) Date: Fri, 22 Aug 2025 08:42:34 +0000 In-Reply-To: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3559; i=aliceryhl@google.com; h=from:subject:message-id; bh=pEFhG0haAU3GTGryN2WieVcKPQ715Oa917yCHNmiuvA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoqC2FrtlThqcOSRNsIBpt3SBxjJlChXDoE5MX/ Z0qjh7A8iOJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaKgthQAKCRAEWL7uWMY5 Rr9XD/4hIWjTh4NRA9TG7NZx0jJFcqPCUg2D2dCla9YcILf6jp8Oja4o3k/O1mTB5QXzgxlZCxC HGM0Uw0WLbwtciMXD+Z/yJBCN7WutdGdsw7xMlW2TYa3vmLuE0jEM20LcnGg5OrICQW8JvlQ4GG I6sBoNu/D9G8X/QW9c7+U6Js4iG8MwVcSEjb2vJ9fOFuHh/+4gnIL9gA4VtOftDZg5X1K+XqH83 GSL6kfTiDC1o5ZkJlOfyQ59s9vj73Yy0oXpmstIX5CRkY8xrQZStLyeV/dORliKUBcgzk4h1f/8 TNe+m+sUUoHfpFXRv6SfzmqqRZsaT48FJZn94mNQ335CC+Pnz2pE7ypb7JM+L+JSOiOdpc2VE1D 1sgpNcX0OClBZ54/f7f0sSlymGL4nZYi8OFWCZRhBNTov3610Yk22mjG/dDqE7lJiFLZ2JcTTr1 DgmkF4c1GVvDJERN29KIXZkStlX5budcF3y9aG06O5jjtK4hE3EDqnpLbU3lwhmtagdCslqGFDT GSHidoSQFxp02+DiBbXiK2wsefcDSFNxT5k2+n0AomBQ8h1KJY4YYl6/OTs9x0/7YsCbbELIByJ dvjgp8LhBgS33XJsx2IBZx4n4GicXbBiwqxQSRLZB2T1Vyvu0Xig8v+GOE7i39/4hUiP3PUlJly sKR+4GFmCOO/NhA== X-Mailer: b4 0.14.2 Message-ID: <20250822-iov-iter-v5-3-6ce4819c2977@google.com> Subject: [PATCH v5 3/5] rust: fs: add Kiocb struct From: Alice Ryhl To: Greg Kroah-Hartman , Alexander Viro , Arnd Bergmann , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Matthew Maurer , Lee Jones , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Benno Lossin , Christian Brauner Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This adds a very simple Kiocb struct that lets you access the inner file's private data and the file position. For now, nothing else is supported. Cc: Christian Brauner Reviewed-by: Andreas Hindborg Signed-off-by: Alice Ryhl --- rust/kernel/fs.rs | 3 +++ rust/kernel/fs/kiocb.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 71 insertions(+) diff --git a/rust/kernel/fs.rs b/rust/kernel/fs.rs index 0121b38c59e63d01a89f22c8ef6983ef5c3234de..6ba6bdf143cb991c6e78215178e= b585260215da0 100644 --- a/rust/kernel/fs.rs +++ b/rust/kernel/fs.rs @@ -6,3 +6,6 @@ =20 pub mod file; pub use self::file::{File, LocalFile}; + +mod kiocb; +pub use self::kiocb::Kiocb; diff --git a/rust/kernel/fs/kiocb.rs b/rust/kernel/fs/kiocb.rs new file mode 100644 index 0000000000000000000000000000000000000000..84c936cd69b0e9b490d54c87d8c= 7279b27d4476a --- /dev/null +++ b/rust/kernel/fs/kiocb.rs @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Kernel IO callbacks. +//! +//! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h) + +use core::marker::PhantomData; +use core::ptr::NonNull; +use kernel::types::ForeignOwnable; + +/// Wrapper for the kernel's `struct kiocb`. +/// +/// Currently this abstractions is incomplete and is essentially just a tu= ple containing a +/// reference to a file and a file position. +/// +/// The type `T` represents the filesystem or driver specific data associa= ted with the file. +/// +/// # Invariants +/// +/// `inner` points at a valid `struct kiocb` whose file has the type `T` a= s its private data. +pub struct Kiocb<'a, T> { + inner: NonNull, + _phantom: PhantomData<&'a T>, +} + +impl<'a, T: ForeignOwnable> Kiocb<'a, T> { + /// Create a `Kiocb` from a raw pointer. + /// + /// # Safety + /// + /// The pointer must reference a valid `struct kiocb` for the duration= of `'a`. The private + /// data of the file must be `T`. + pub unsafe fn from_raw(kiocb: *mut bindings::kiocb) -> Self { + Self { + // SAFETY: If a pointer is valid it is not null. + inner: unsafe { NonNull::new_unchecked(kiocb) }, + _phantom: PhantomData, + } + } + + /// Access the underlying `struct kiocb` directly. + pub fn as_raw(&self) -> *mut bindings::kiocb { + self.inner.as_ptr() + } + + /// Get the filesystem or driver specific data associated with the fil= e. + pub fn file(&self) -> ::Borrowed<'a> { + // SAFETY: We have shared access to this kiocb and hence the under= lying file, so we can + // read the file's private data. + let private =3D unsafe { (*(*self.as_raw()).ki_filp).private_data = }; + // SAFETY: The kiocb has shared access to the private data. + unsafe { ::borrow(private) } + } + + /// Gets the current value of `ki_pos`. + pub fn ki_pos(&self) -> i64 { + // SAFETY: We have shared access to the kiocb, so we can read its = `ki_pos` field. + unsafe { (*self.as_raw()).ki_pos } + } + + /// Gets a mutable reference to the `ki_pos` field. + pub fn ki_pos_mut(&mut self) -> &mut i64 { + // SAFETY: We have exclusive access to the kiocb, so we can write = to `ki_pos`. + unsafe { &mut (*self.as_raw()).ki_pos } + } +} --=20 2.51.0.rc2.233.g662b1ed5c5-goog