From patchwork Tue Jun 28 07:28:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 1649335 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=SsQf2kEY; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LXGRq4YpSz9sGJ for ; Tue, 28 Jun 2022 17:28:55 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 72CA88448E; Tue, 28 Jun 2022 09:28:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.b="SsQf2kEY"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 84B5984477; Tue, 28 Jun 2022 09:28:37 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 496B284464 for ; Tue, 28 Jun 2022 09:28:35 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=wqu@suse.com Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 1879D1FB3C; Tue, 28 Jun 2022 07:28:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1656401315; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EJDsnfm5CD6flKl27ug0ll/8C9SQYioiQTVCJ40ytaU=; b=SsQf2kEYU+3fPNOKwoJhXiwLGpRCNYE4ENLyZkim9hOpoGr6tTbW7QdNZ5/sPgiG8Ma97X /b+QlRy95AGsIJQBbQakogsV7eVkmvAw8E1bwuh9jtVCTv2clu3VpbNGlM6Ar8L/EBjhOE rFClffev3+tFVj8j9w3QS7LDF9X7n6c= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 9DB70139E9; Tue, 28 Jun 2022 07:28:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id MCnEGaCtumJzFQAAMHmgww (envelope-from ); Tue, 28 Jun 2022 07:28:32 +0000 From: Qu Wenruo To: u-boot@lists.denx.de Cc: marek.behun@nic.cz, linux-btrfs@vger.kernel.org, jnhuang95@gmail.com, linux-erofs@lists.ozlabs.org, trini@konsulko.com, joaomarcos.costa@bootlin.com, thomas.petazzoni@bootlin.com, miquel.raynal@bootlin.com Subject: [PATCH RFC 2/8] fs: always get the file size in _fs_read() Date: Tue, 28 Jun 2022 15:28:02 +0800 Message-Id: X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean For _fs_read(), @len == 0 means we read the whole file. And we just pass @len == 0 to make each filesystem to handle it. In fact we have info->size() call to properly get the filesize. So we can not only call info->size() to grab the file_size for len == 0 case, but also detect invalid @len (e.g. @len > file_size) in advance or truncate @len. This behavior also allows us to handle unaligned better in the incoming patches. Signed-off-by: Qu Wenruo --- fs/fs.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/fs/fs.c b/fs/fs.c index 6de1a3eb6d5d..d992cdd6d650 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -579,6 +579,7 @@ static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, { struct fstype_info *info = fs_get_info(fs_type); void *buf; + loff_t file_size; int ret; #ifdef CONFIG_LMB @@ -589,10 +590,26 @@ static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, } #endif - /* - * We don't actually know how many bytes are being read, since len==0 - * means read the whole file. - */ + ret = info->size(filename, &file_size); + if (ret < 0) { + log_err("** Unable to get file size for %s, %d **\n", + filename, ret); + return ret; + } + if (offset >= file_size) { + log_err( + "** Invalid offset, offset (%llu) >= file size (%llu)\n", + offset, file_size); + return -EINVAL; + + } + if (len == 0 || offset + len > file_size) { + if (len > file_size) + log_info( + "** Truncate read length from %llu to %llu, as file size is %llu **\n", + len, file_size, file_size); + len = file_size - offset; + } buf = map_sysmem(addr, len); ret = info->read(filename, buf, offset, len, actread); unmap_sysmem(buf);