Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2680381lqo; Tue, 14 May 2024 06:22:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXh2qgydGZ9l3i5G3/XTmU8T/AkqZjw5SA0xX5gaWNC0kiqMqd1Ayn1+tsZ+WWKkSbX5gdtMhFTmZGMiIbuBHTaNNS1Qgbuixe+uuvPtg== X-Google-Smtp-Source: AGHT+IHFjmluTM55f/Q2qkwCr3Jd9MBAKEIBz8rPG9UOqc71vVxvl6zvaP/aHcjXIVFkm5/lKP8F X-Received: by 2002:a05:6122:4695:b0:4d4:ef9:71b0 with SMTP id 71dfb90a1353d-4df882cb6b1mr10207418e0c.7.1715692948721; Tue, 14 May 2024 06:22:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715692948; cv=pass; d=google.com; s=arc-20160816; b=BDsTlfviAtAI/AwOpSIBMM1F5GVH0M8gG4USKfTZ4cpejV1OcW1gTNMhKdF9IDqb6A Rx/qlMm7SozBMlubjWcMX8LKN5mE5cLEB8afWWDwkrrDulAlDA8Gp2vzxrx45ny870cn wr3RcbMU3R9ClyfBRlRMZAjx9WanJ6pkgX+pHzTzWrFKPTqcj5YlD0Kt6QXNCSQs9D0z qPxxk8CR2lME/S7v/ODpULSVXjMPv3tphY8+BA2r8CLmonnTHxwiHw5YW1rxXMOHACI+ IwerPNgFCm8m1JQAasyByVlQ8RTCyuxqBRCO7V4b7aS6ttIsEyVB03bPCiYIk2ztG/Sa ATdQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=6cg/flIhVL4ROGj8221a6AJ0d2eWqZBDXs4XmumwzXA=; fh=WDmj25GHxtpixvuo6nmW7Hb+f6ygI3ZNOZ2qh2WR1/8=; b=n3vt6KGjcr5lcKICNhqm8pUVIl3ArXu6+idvl61fgoiOK80wrgAJPAE9aNrRoJO7lB qqjB7EPHYDiK2dEjvdYeohVyqD3M+6Jl1a4VBNVKOLqEazKHgyaoNIDT3B9rUc4yj8c+ qO9CCYrOjZhlnITgGKgcLy1GkyuJQu6nZHU5GPm+AHcZR/nY3z0F3Y6um4ovG6NCciFE 4FwwOBYhn0bFVZMwzs48KDng7M7X458VYBNx+upe63Jj5RypXsnW/nVub7FswfPNpea+ btXsTmCnbra8P5bLJ9h1ndGRc7rR01mU94ASXCd5C4Er0h8q0HuRJ+oDYZqLq+u3N3gq 1u1g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=hxj6gUQJ; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-178726-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178726-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id ada2fe7eead31-4806cc93e88si1891035137.661.2024.05.14.06.22.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 06:22:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-178726-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=hxj6gUQJ; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-178726-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178726-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5AB6E1C208DE for ; Tue, 14 May 2024 13:22:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8427115697A; Tue, 14 May 2024 13:17:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hxj6gUQJ" Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 8D8AF154BF7; Tue, 14 May 2024 13:17:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715692674; cv=none; b=bobyU1dqXvwt8GqzZFAGq0tBkPSv2y4AV60j+IFuuo+5x8mfSYT/Bkkrv6GkjnxS5FFqULMzvulwtnp3N44qZDM7ZSoPBTJrLwbVDTjayar4WYH0XB1Uup6PFVCcX/MGfbaJUBqO46NJGSJ3dX0TJeng6L3wwkhQVQ/C4PazOi4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715692674; c=relaxed/simple; bh=kKoj43kcpdB4g3eGetsZvcZ682MK2Cdc/C4K6JHs0D0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kC8dt+/inpeqWj+StXS0Wbh8fiRbeq3egpdP/y3P14EzNum89EupqTubs/SmmvvUEzIoWWnkkBIfjcwaFIt0CNGV9uB+SvC0MS5Cr29/0E9jHp2l6zW7VYPgPjWbQu8A1nwI/ZhIyRGjdcXujcq4zRGLUCRDIIJl7tfPRpcDCj0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hxj6gUQJ; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1eb24e3a2d9so48931195ad.1; Tue, 14 May 2024 06:17:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715692672; x=1716297472; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6cg/flIhVL4ROGj8221a6AJ0d2eWqZBDXs4XmumwzXA=; b=hxj6gUQJb6Tag+KIDmXd974CfLRaMZWfpVcYwpI7oO5uZ6WUVXXFjE1KjqzAVCxK9L i7nzgnJkLy0RjCdV/csTcl0y5PcQoJYedLRXe+5w5vwTTRg/AIef+UUXuyyPpZcnVftT SICpU5yArnjwyxStrugubwhPlNk9hV+Fsx8f1IYZC2KE1150VWC8KAJCwS914gkOQvE/ vznN+S1qX3aFw/CbjSQYFb1EzW2D2eJwZ2usqNUtXiTe1h/CiGL1Kf23MUIUsE+RCp2j KemWN6028qlZ4pkqn58qV9ec0bdaUJm+wVXWfoJlW+mX3bTq+9FZCDxZKlQakAwZ7uG1 jbww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715692672; x=1716297472; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6cg/flIhVL4ROGj8221a6AJ0d2eWqZBDXs4XmumwzXA=; b=EdZUIK/L/rbSEMCo1VI3vafa/35D9hEwtnqMm1wx5mLnPDWn6UDl9/zF4qGbHbBHMq iUIWEdiJpDjjYoDZCLxcA7jhRBohXJelN4+72WcwIXa0hd5MV1cEOmtFknpSQZcy8Iqz g/E0NFUewehbrE9nj4gO/TXwkSkqSBZXFWBy785SQB9xv/pVEjvUh5Lf2g4d1P5oIv32 7gig3u8CqB5Ql4+/Svk/Nd5cUvF0J+KZ0m7ANj0dVTjVF7mKamymN7q0mtPIqDCGfY8h FVmFuXS5qF9TEWFlmxaZLDg4D+uXm28A+PszhaQOKTWvkDPv4rMrGQ87BYtHoKrFl8yi XaUQ== X-Forwarded-Encrypted: i=1; AJvYcCWCb0vV3vHOXp8W+8COSXOUfvw9NotIHiPVOMi+mOGjOB67y/wDmT8iqBnx2rAI9mRSjTIIWO8JAeK42ProazHLSc7Zo+7aU0Nl0vgG/GDOrhD0U5nYqZqMzW+My8l9dCRggqveFjglv3HFbIBwSqGDgg4C5Ay5LNdbkdHZjiwcuXOxKxRWtUGCMNey X-Gm-Message-State: AOJu0Yww6p97ckM1DBAcKRxAZYSZFAnLF6eqiu/FEHE0ug40upgoUECr 4m1/7nMQHNcfFM6cFiUcyaGx76+hLT+QMTFsdC3VUnNHLiz4mpA1 X-Received: by 2002:a17:902:76c4:b0:1e4:3909:47c0 with SMTP id d9443c01a7336-1ef4404e0c5mr153954985ad.62.1715692671864; Tue, 14 May 2024 06:17:51 -0700 (PDT) Received: from wedsonaf-dev.. ([50.204.89.32]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-1ef0b9d18a4sm97277335ad.56.2024.05.14.06.17.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 06:17:51 -0700 (PDT) From: Wedson Almeida Filho To: Alexander Viro , Christian Brauner , Matthew Wilcox , Dave Chinner Cc: Kent Overstreet , Greg Kroah-Hartman , linux-fsdevel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: [RFC PATCH v2 13/30] rust: fs: introduce `file::Operations::read` Date: Tue, 14 May 2024 10:16:54 -0300 Message-Id: <20240514131711.379322-14-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240514131711.379322-1-wedsonaf@gmail.com> References: <20240514131711.379322-1-wedsonaf@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Wedson Almeida Filho This allows file systems to customise their behaviour when callers want to read from a file. Signed-off-by: Wedson Almeida Filho --- rust/kernel/fs/file.rs | 35 ++++++++++++++++++++++++++++++++++- rust/kernel/user.rs | 1 - samples/rust/rust_rofs.rs | 6 +++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs index 77eb6d230568..2ba456a1eee1 100644 --- a/rust/kernel/fs/file.rs +++ b/rust/kernel/fs/file.rs @@ -12,6 +12,7 @@ bindings, error::{code::*, from_result, Error, Result}, types::{ARef, AlwaysRefCounted, Locked, Opaque}, + user, }; use core::{marker::PhantomData, mem::ManuallyDrop, ptr}; use macros::vtable; @@ -324,6 +325,15 @@ pub trait Operations { /// File system that these operations are compatible with. type FileSystem: FileSystem + ?Sized; + /// Reads data from this file into the caller's buffer. + fn read( + _file: &File, + _buffer: &mut user::Writer, + _offset: &mut Offset, + ) -> Result { + Err(EINVAL) + } + /// Seeks the file to the given offset. fn seek(_file: &File, _offset: Offset, _whence: Whence) -> Result { Err(EINVAL) @@ -356,7 +366,11 @@ impl Table { } else { None }, - read: None, + read: if T::HAS_READ { + Some(Self::read_callback) + } else { + None + }, write: None, read_iter: None, write_iter: None, @@ -407,6 +421,25 @@ impl Table { }) } + unsafe extern "C" fn read_callback( + file_ptr: *mut bindings::file, + ptr: *mut core::ffi::c_char, + len: usize, + offset: *mut bindings::loff_t, + ) -> isize { + from_result(|| { + // SAFETY: The C API guarantees that `file` is valid for the duration of the + // callback. Since this callback is specifically for filesystem T, we know `T` + // is the right filesystem. + let file = unsafe { File::from_raw(file_ptr) }; + let mut writer = user::Writer::new(ptr, len); + + // SAFETY: The C API guarantees that `offset` is valid for read and write. + let read = T::read(file, &mut writer, unsafe { &mut *offset })?; + Ok(isize::try_from(read)?) + }) + } + unsafe extern "C" fn read_dir_callback( file_ptr: *mut bindings::file, ctx_ptr: *mut bindings::dir_context, diff --git a/rust/kernel/user.rs b/rust/kernel/user.rs index 35a673ebcd58..20fb887f4640 100644 --- a/rust/kernel/user.rs +++ b/rust/kernel/user.rs @@ -11,7 +11,6 @@ pub struct Writer { } impl Writer { - #[allow(dead_code)] pub(crate) fn new(ptr: *mut i8, len: usize) -> Self { Self { ptr: ptr.cast::(), diff --git a/samples/rust/rust_rofs.rs b/samples/rust/rust_rofs.rs index abec084360da..f4be5908369c 100644 --- a/samples/rust/rust_rofs.rs +++ b/samples/rust/rust_rofs.rs @@ -4,7 +4,7 @@ use kernel::fs::{dentry, file, file::File, inode, inode::INode, sb::SuperBlock, Offset}; use kernel::prelude::*; -use kernel::{c_str, fs, time::UNIX_EPOCH, types::Either, types::Locked}; +use kernel::{c_str, fs, time::UNIX_EPOCH, types::Either, types::Locked, user}; kernel::module_fs! { type: RoFs, @@ -80,6 +80,10 @@ fn seek(file: &File, offset: Offset, whence: file::Whence) -> Result, _: &mut user::Writer, _: &mut Offset) -> Result { + Err(EISDIR) + } + fn read_dir( _file: &File, inode: &Locked<&INode, inode::ReadSem>, -- 2.34.1