From patchwork Tue Mar 24 13:38:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jozef Pupava X-Patchwork-Id: 1260670 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48msxZ58p0z9sVN for ; Wed, 25 Mar 2020 00:47:14 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 873113C4D35 for ; Tue, 24 Mar 2020 14:47:11 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [IPv6:2001:4b78:1:20::6]) by picard.linux.it (Postfix) with ESMTP id 770A33C29B2 for ; Tue, 24 Mar 2020 14:36:13 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id DCF8A14011AD for ; Tue, 24 Mar 2020 14:36:12 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 3D8DEAC6C for ; Tue, 24 Mar 2020 13:36:12 +0000 (UTC) Date: Tue, 24 Mar 2020 14:38:37 +0100 From: Jozef Pupava To: ltp@lists.linux.it Message-ID: <20200324143837.51d2df15@daedruan> Organization: SUSE X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-6.smtp.seeweb.it X-Mailman-Approved-At: Tue, 24 Mar 2020 14:46:58 +0100 Subject: [LTP] [PATCH 1/2] Update syscalls/fsync02 to new API X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Acked-by: Martin Doucha Signed-off-by: Jozef Pupava Reviewed-by: Petr Vorel --- testcases/kernel/syscalls/fsync/fsync02.c | 235 ++++++++++-------------------- 1 file changed, 77 insertions(+), 158 deletions(-) diff --git a/testcases/kernel/syscalls/fsync/fsync02.c b/testcases/kernel/syscalls/fsync/fsync02.c index 1e5f9ee00..9506b4868 100644 --- a/testcases/kernel/syscalls/fsync/fsync02.c +++ b/testcases/kernel/syscalls/fsync/fsync02.c @@ -1,195 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* - * - * Copyright (c) International Business Machines Corp., 2001 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Copyright (c) Wayne Boyer, International Business Machines Corp., 2001 + * Copyright (c) 2019 SUSE LLC, Jozef Pupava */ /* - * NAME - * fsync02.c - * - * DESCRIPTION - * Create a sparse file, fsync it, and time the fsync - * - * ALGORITHM - * 1. Create a file. - * 2. Write to the file at equally spaced intervals up to a max block - * 3. Check if the time limit was exceeded. - * - * USAGE: - * fsync02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] - * where, -c n : Run n copies concurrently. - * -f : Turn off functionality Testing. - * -i n : Execute test n times. - * -I x : Execute test for x seconds. - * -P x : Pause for x seconds between iterations. - * -t : Turn on syscall timing. - * - * HISTORY - * 07/2001 Ported by Wayne Boyer - * - * RESTRICTIONS - * None + * Test Description: + * Test fsync() return value on test file + * fsync() has to finish within TIME_LIMIT. */ #include -#include +#include #include #include #include -#include #include -#include "test.h" -#include "safe_macros.h" #include +#include "tst_test.h" #define BLOCKSIZE 8192 #define MAXBLKS 262144 #define TIME_LIMIT 120 -char *TCID = "fsync02"; -int TST_TOTAL = 1; - -void setup(void); -void cleanup(void); - char tempfile[40] = ""; -int fd, pid; +char pbuf[BUFSIZ]; +int fd; off_t max_blks = MAXBLKS; struct statvfs stat_buf; -int main(int ac, char **av) -{ - int lc; - - off_t offsetret, offset; - char pbuf[BUFSIZ]; - int ret, max_block = 0; - int i; - time_t time_start, time_end; - double time_delta; - int data_blocks = 0; - long int random_number; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - - tst_count = 0; - - while (max_block <= data_blocks) { - random_number = random(); - max_block = random_number % max_blks; - data_blocks = random_number % 1000 + 1; - } - - for (i = 1; i <= data_blocks; i++) { - offset = i * ((BLOCKSIZE * max_block) / data_blocks); - offset -= BUFSIZ; - if ((offsetret = lseek(fd, offset, SEEK_SET)) != offset) - tst_brkm(TBROK | TERRNO, cleanup, - "lseek failed: %ld, %ld", offsetret, - offset); - if ((ret = write(fd, pbuf, BUFSIZ)) != BUFSIZ) - tst_brkm(TBROK, cleanup, "write failed"); - } - if (time(&time_start) == -1) - tst_brkm(TBROK | TERRNO, cleanup, - "getting start time failed"); - - TEST(fsync(fd)); - - if (time(&time_end) == -1) - tst_brkm(TBROK | TERRNO, cleanup, - "getting end time failed"); - - if (TEST_RETURN == -1) { - tst_resm(TFAIL | TTERRNO, "fsync failed"); - continue; - } - - if (time_end < time_start) - tst_resm(TBROK, - "timer broken end %ld < start %ld", - time_end, time_start); - - if ((time_delta = - difftime(time_end, time_start)) > TIME_LIMIT) - tst_resm(TFAIL, - "fsync took too long: %lf seconds; " - "max_block: %d; data_blocks: %d", - time_delta, max_block, data_blocks); - else - tst_resm(TPASS, "fsync succeeded in an " - "acceptable amount of time"); - - SAFE_FTRUNCATE(cleanup, fd, 0); - } - - sync(); - cleanup(); - tst_exit(); -} - -/* - * setup() - performs all ONE TIME setup for this test. - */ -void setup(void) -{ +static void setup(void) { /* free blocks avail to non-superuser */ unsigned long f_bavail; - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - /* make a temporary directory and cd to it */ - tst_tmpdir(); + fd = SAFE_OPEN("tempfile", O_RDWR | O_CREAT | O_TRUNC, 0777); - sprintf(tempfile, "%s.%d", TCID, pid = getpid()); - srand48(pid); - - if ((fd = open(tempfile, O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1) - tst_brkm(TBROK, cleanup, "open failed"); - - if (fstatvfs(fd, &stat_buf) != 0) - tst_brkm(TBROK, cleanup, "fstatvfs failed"); + if (fstatvfs(fd, &stat_buf) != 0) { + tst_brk(TBROK, "fstatvfs failed"); + } f_bavail = (stat_buf.f_bavail * stat_buf.f_frsize) / BLOCKSIZE; - if (f_bavail && (f_bavail < MAXBLKS)) + if (f_bavail && (f_bavail < MAXBLKS)) { max_blks = f_bavail; + } #ifdef LARGEFILE - if ((fcntl(fd, F_SETFL, O_LARGEFILE)) == -1) - tst_brkm(TBROK | TERRNO, cleanup, - "fcntl(.., O_LARGEFILE) failed"); - - if (write(fd, pbuf, BUFSIZ) != BUFSIZ) - tst_brkm(TBROK | TERRNO, cleanup, "write(fd, pbuf, ..) failed"); + SAFE_FCNTL(fd, F_SETFL, O_LARGEFILE); + SAFE_WRITE(1, fd, pbuf, BUFSIZ); #endif } -void cleanup(void) -{ - if (close(fd) == -1) - tst_resm(TWARN | TERRNO, "close failed"); +static void run(void) { + off_t offset; + int i; + int max_block = 0; + int data_blocks = 0; + time_t time_start, time_end; + double time_delta; + long int random_number; + + while (max_block <= data_blocks) { + random_number = rand(); + max_block = random_number % max_blks; + data_blocks = random_number % 1000 + 1; + } - tst_rmdir(); + for (i = 1; i <= data_blocks; i++) { + offset = i * ((BLOCKSIZE * max_block) / data_blocks); + offset -= BUFSIZ; + SAFE_LSEEK(fd, offset, SEEK_SET); + SAFE_WRITE(1, fd, pbuf, BUFSIZ); + } + time_start = time(0); + + TEST(fsync(fd)); + + time_end = time(0); + + if (time_end == -1) { + tst_res(TFAIL | TTERRNO, "getting end time failed"); + } else if (TST_RET == -1) { + tst_res(TFAIL | TTERRNO, "fsync failed"); + } else if (TST_RET != 0) { + tst_res(TFAIL | TTERRNO, + "fsync failed with unexpected return value"); + } else if (time_end < time_start) { + tst_res(TFAIL, + "timer broken end %ld < start %ld", + time_end, time_start); + } else if ((time_delta = + difftime(time_end, time_start)) > TIME_LIMIT) { + tst_res(TFAIL, + "fsync took too long: %lf seconds; " + "max_block: %d; data_blocks: %d", + time_delta, max_block, data_blocks); + } else { + tst_res(TPASS, + "fsync succeeded in an acceptable amount of time"); + } + SAFE_FTRUNCATE(fd, 0); +} +static void cleanup(void) { + SAFE_CLOSE(fd); } + +static struct tst_test test = { + .test_all = run, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1 +};