xref: /freebsd/tools/test/stress2/misc/truncate8.sh (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Bug 236977 - [msdosfs] returns cached truncated data
31# Fixed by r345847
32
33. ../default.cfg
34[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
35
36dir=/tmp
37odir=`pwd`
38cd $dir
39sed '1,/^EOF/d' < $odir/$0 > $dir/truncate8.c
40mycc -o truncate8 -Wall -Wextra -O0 -g truncate8.c || exit 1
41rm -f truncate8.c
42cd $odir
43
44echo ufs:
45mount | grep -q "$mntpoint " && umount $mntpoint
46mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
47mdconfig -a -t swap -s 1g -u $mdstart
48bsdlabel -w md$mdstart auto
49newfs $newfs_flags md${mdstart}$part > /dev/null
50mount /dev/md${mdstart}$part $mntpoint
51
52(cd $mntpoint; /tmp/truncate8)
53s=$?
54[ $s -ne 0 ] && { echo "UFS exit status is $s"; status=1; }
55while mount | grep -q "$mntpoint "; do
56	umount $mntpoint || sleep 1
57done
58mdconfig -d -u $mdstart
59
60echo nfs:
61if ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1; then
62	mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export \
63	    $mntpoint
64	sleep .2
65	(cd $mntpoint; /tmp/truncate8)
66	s=$?
67	[ $s -ne 0 ] && { echo "NFS exit status is $s"; status=1; }
68
69	umount $mntpoint || umount $mntpoint
70fi
71
72echo msdos:
73if [ -x /sbin/mount_msdosfs ]; then
74	mdconfig -a -t swap -s 1g -u $mdstart
75	bsdlabel -w md$mdstart auto
76	newfs_msdos -F 16 -b 8192 /dev/md${mdstart}$part > /dev/null 2>&1
77	mount_msdosfs -m 777 /dev/md${mdstart}$part $mntpoint
78
79	(cd /mnt; /tmp/truncate8)
80	s=$?
81	[ $s -ne 0 ] && { echo "MSDOS exit status is $s"; status=1; }
82	while mount | grep -q "$mntpoint "; do
83		umount $mntpoint || sleep 1
84	done
85	mdconfig -d -u $mdstart
86fi
87
88echo tmpfs:
89mount -t tmpfs null $mntpoint
90chmod 777 $mntpoint
91
92(cd $mntpoint; /tmp/truncate8)
93s=$?
94[ $s -ne 0 ] && { echo "TMPFS exit status is $s"; status=1; }
95while mount | grep -q "$mntpoint "; do
96	umount $mntpoint || sleep 1
97done
98
99rm -rf /tmp/truncate8
100exit $status
101
102EOF
103#include <sys/param.h>
104#include <sys/mman.h>
105#include <sys/stat.h>
106#include <sys/wait.h>
107
108#include <machine/atomic.h>
109
110#include <err.h>
111#include <errno.h>
112#include <fcntl.h>
113#include <stdio.h>
114#include <stdlib.h>
115#include <time.h>
116#include <unistd.h>
117
118static volatile u_int *share;
119
120#define BSIZE 5120
121#define FSIZE (128 * 1024 * 1024)
122#define PARALLEL 2
123#define RUNTIME (1 * 60)
124#define SYNC 0
125
126static void
127test(void)
128{
129	time_t start;
130	off_t pos;
131	int fd, i, n, r;
132	char *buf, name[128];
133
134	atomic_add_int(&share[SYNC], 1);
135	while (share[SYNC] != PARALLEL)
136		;
137
138	srand48(getpid());
139	buf = malloc(BSIZE);
140	for (i = 0; i < (int)sizeof(buf); i++)
141		buf[i] = 123;
142
143	sprintf(name, "f%05d", getpid());
144	if ((fd = open(name, O_RDWR | O_CREAT, 0640)) == -1)
145		err(1, "%s", name);
146	for (i = 0; i < FSIZE / BSIZE; i++)
147		if (write(fd, buf, BSIZE) != BSIZE)
148			err(1, "write");
149
150	start = time(NULL);
151	while (time(NULL) - start < RUNTIME) {
152		pos = lrand48() % (FSIZE - BSIZE);
153//		fprintf(stderr, "truncate(%jd)\n", (intmax_t)pos);
154		if (ftruncate(fd, pos) == -1)
155			err(1, "ftruncate");
156
157		if (ftruncate(fd, FSIZE) == -1)
158			err(1, "ftruncate");
159		if (lseek(fd, pos, SEEK_SET) == -1)
160			err(1, "lseek");
161		n = 0;
162		while ((r = read(fd, buf, BSIZE)) == BSIZE) {
163			n++;
164			for (i = 0; i < BSIZE; i++) {
165				if (buf[i] != 0)
166					errx(1, "Bad read value @ %jd = %d",
167					    (intmax_t)(pos + i), buf[i]);
168			}
169		}
170		if (r == -1)
171			err(1, "read");
172		if (n != (FSIZE - pos) / BSIZE)
173			fprintf(stderr, "n = %d, target = %d\n", n, (int)(FSIZE - pos) / BSIZE);
174	}
175	unlink(name);
176
177	_exit(0);
178}
179
180int
181main(void)
182{
183	pid_t pids[PARALLEL];
184	size_t len;
185	int e, i, status;
186
187	e = 0;
188	len = PAGE_SIZE;
189	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
190	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
191		err(1, "mmap");
192
193	for (i = 0; i < PARALLEL; i++) {
194		if ((pids[i] = fork()) == 0)
195			test();
196		if (pids[i] == -1)
197			err(1, "fork()");
198	}
199	for (i = 0; i < PARALLEL; i++) {
200		if (waitpid(pids[i], &status, 0) == -1)
201			err(1, "waitpid(%d)", pids[i]);
202		if (status != 0) {
203			if (WIFSIGNALED(status))
204				fprintf(stderr,
205				    "pid %d exit signal %d\n",
206				    pids[i], WTERMSIG(status));
207		}
208		e += status == 0 ? 0 : 1;
209	}
210
211	return (e);
212}
213