From patchwork Thu Feb 2 20:44:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 52164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp464146wrn; Thu, 2 Feb 2023 12:45:55 -0800 (PST) X-Google-Smtp-Source: AK7set9HkIfA7t1zrYJYhpVFU9jjStpw7j0sMBUKlh2e2tmTMo5KWYSxHugvWZB/mVR01yqy7ZWP X-Received: by 2002:a05:6a20:2a24:b0:be:e494:4185 with SMTP id e36-20020a056a202a2400b000bee4944185mr6929385pzh.53.1675370754940; Thu, 02 Feb 2023 12:45:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675370754; cv=none; d=google.com; s=arc-20160816; b=PAunOqTJXp/olhcHENI/wbSvbyScHi9S+6l6TYc3APd+cMBF0Is/gzHRTgSJHNZHyV 3XWNgHwouMUGA7msN46AuUUs+JeCrnP1rFWxsWVXF9VV5S1/cy5oQCnUJ9QE4rahIb70 jEMrn5hwVlTjA932P7N64V0xtM0o+/vGFT3DxcBzw/wjwtUK11k6TBdN/ikqcop8LOp1 CBIeJjYFwnR+b5mVNo4Iwe8eNQzc2Y/PC0nf/tRJiK+jlv3yfGEJnzLDlVHXepw1I12J 0olMc2ntpkGxK2PpYiCrcujaBoUlZhzMpm2TdpgZivjSqd1Q/nh4WxZfabd2K9KLupMM SK7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2Gu1J17vUOqah9snYcpOpRqgXozaOqIS6UOuMS3TPDU=; b=uEhgQiYqtxTFqTmvU+w0/7LK4/olgnEO0v1dcy+ClpJKOkJAeGykAUS+GyoEFoliIY 5UsCVqb+ZJC7hJnuSIGXD0fR9E+nprB7//SWd2y3yYf/CBEuq7GO23on5+LeMqY5T6Qn n0yzBOXTdAVh8jDHBhaEOLEa8o4TofcXYxJ1pSoC2meVCPRwkplQpoLe+IqNv0dvOkW4 7jGc2jPenoNu0l1r3YyxML78YBrggz7PtmoALYjV4mPdBwwzvaCp1MS6PQ7ss4bc29j4 4gqGczkjgjs7hPCj5JQljxo44LxzAg5IHEqutOPbHQwsmVrLfC+zYN6yFbFQbi+O8pBx HGSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=nBecBO+l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h10-20020a63384a000000b0047856c2a3dbsi550233pgn.257.2023.02.02.12.45.42; Thu, 02 Feb 2023 12:45:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=nBecBO+l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232981AbjBBUom (ORCPT + 99 others); Thu, 2 Feb 2023 15:44:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232515AbjBBUog (ORCPT ); Thu, 2 Feb 2023 15:44:36 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C68266598; Thu, 2 Feb 2023 12:44:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=2Gu1J17vUOqah9snYcpOpRqgXozaOqIS6UOuMS3TPDU=; b=nBecBO+le/dfpA61aDSji/IT8c Jtpk5VIkJ0m1vBtuaxptXdJ/oIIbbAH0pqvZqcVxvttuZlRtLDKXIhaa23N1SjMEkbKDNYis6ShNF Rsig0553S1/uylbIfEMobwRyeLj+CgHeUmJPqVvGzNqX88Jd57qQtQegCyncP+63tRKILRWPihjb5 yCGI0RNgt3IIXMzIMmRWkUrf5vPGvU8xF2ShjG7/W+zpd49ew3/BhME1TE88PpbjIZsdOKHx7QGXD WfS4Y1QKwclnEZiFBnuwCJT7n6IYUwe/ERhek6N/j+EKTi458KfQRGCiCnV353LqlnIg/JOWmJmF3 QwgiXE+Q==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNgRb-00Di7p-Pr; Thu, 02 Feb 2023 20:44:31 +0000 From: "Matthew Wilcox (Oracle)" To: linux-fsdevel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , linux-afs@lists.infradead.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-mm@kvack.org, Hugh Dickins , linux-kernel@vger.kernel.org, fstests@vger.kernel.org Subject: [PATCH 6/5] generic: test ftruncate zeroes bytes after EOF Date: Thu, 2 Feb 2023 20:44:28 +0000 Message-Id: <20230202204428.3267832-7-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230202204428.3267832-1-willy@infradead.org> References: <20230202204428.3267832-1-willy@infradead.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756753564838843499?= X-GMAIL-MSGID: =?utf-8?q?1756753564838843499?= https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html specifies that "If the file size is increased, the extended area shall appear as if it were zero-filled." Many filesystems do not currently do this for the portion of the page after EOF. Signed-off-by: Matthew Wilcox (Oracle) --- .gitignore | 1 + src/Makefile | 2 +- src/truncate-zero.c | 50 +++++++++++++++++++++++++++++++++++++++++++ tests/generic/707 | 31 +++++++++++++++++++++++++++ tests/generic/707.out | 2 ++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/truncate-zero.c create mode 100755 tests/generic/707 create mode 100644 tests/generic/707.out diff --git a/.gitignore b/.gitignore index a6f433f1..6aa5bca9 100644 --- a/.gitignore +++ b/.gitignore @@ -169,6 +169,7 @@ tags /src/test-nextquota /src/testx /src/trunc +/src/truncate-zero /src/truncfile /src/unwritten_mmap /src/unwritten_sync diff --git a/src/Makefile b/src/Makefile index afdf6b30..83ca11ac 100644 --- a/src/Makefile +++ b/src/Makefile @@ -19,7 +19,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ t_ofd_locks t_mmap_collision mmap-write-concurrent \ t_get_file_time t_create_short_dirs t_create_long_dirs t_enospc \ t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale \ - t_mmap_cow_memory_failure fake-dump-rootino + t_mmap_cow_memory_failure fake-dump-rootino truncate-zero LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \ diff --git a/src/truncate-zero.c b/src/truncate-zero.c new file mode 100644 index 00000000..67f53912 --- /dev/null +++ b/src/truncate-zero.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + char *buf; + int i, fd; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + fd = open(argv[1], O_RDWR | O_CREAT, 0644); + if (fd < 0) { + perror(argv[1]); + return 1; + } + + if (ftruncate(fd, 1) < 0) { + perror("ftruncate"); + return 1; + } + + buf = mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (buf == MAP_FAILED) { + perror("mmap"); + return 1; + } + + memset(buf, 'a', 10); + + if (ftruncate(fd, 5) < 0) { + perror("ftruncate"); + return 1; + } + + if (memcmp(buf, "a\0\0\0\0", 5) == 0) + return 0; + + fprintf(stderr, "Truncation did not zero new bytes:\n"); + for (i = 0; i < 5; i++) + fprintf(stderr, "%#x ", buf[i]); + fputc('\n', stderr); + + return 2; +} diff --git a/tests/generic/707 b/tests/generic/707 new file mode 100755 index 00000000..ddc82a9a --- /dev/null +++ b/tests/generic/707 @@ -0,0 +1,31 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2023 Matthew Wilcox for Oracle. All Rights Reserved. +# +# FS QA Test 707 +# +# Test whether we obey this part of POSIX-2017 ftruncate: +# "If the file size is increased, the extended area shall appear as if +# it were zero-filled" +# +. ./common/preamble +_begin_fstest auto quick posix + +_supported_fs generic +_require_test +_require_test_program "truncate-zero" + +test_file=$TEST_DIR/test.$seq + +_cleanup() +{ + cd / + rm -f $test_file +} + +$here/src/truncate-zero $test_file > $seqres.full 2>&1 || + _fail "truncate zero failed!" + +echo "Silence is golden" +status=0 +exit diff --git a/tests/generic/707.out b/tests/generic/707.out new file mode 100644 index 00000000..8e57a1d8 --- /dev/null +++ b/tests/generic/707.out @@ -0,0 +1,2 @@ +QA output created by 707 +Silence is golden