xref: /freebsd/tools/test/stress2/misc/mountu.sh (revision 1603881667360c015f6685131f2f25474fa67a72)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2012 Peter Holm <pho@FreeBSD.org>
7# Copyright (c) 2019 Dell EMC Isilon
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29#
30
31# Change mount point from rw to ro with a file mapped rw
32# Currently fails for NFS
33
34# Page fault seen:
35# https://people.freebsd.org/~pho/stress/log/mountu.txt
36# Fixed by: r285039.
37
38[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
39
40. ../default.cfg
41
42here=`pwd`
43cd /tmp
44sed '1,/^EOF/d' < $here/$0 > mountu.c
45mycc -o mountu -Wall -Wextra -O2 mountu.c || exit 1
46rm -f mountu.c
47
48pstat() {
49	local pid
50	pid=`ps ax | grep -v grep | grep /tmp/mountu | awk '{print $1}'`
51	[ -n "$pid" ] && procstat -v $pid
52}
53
54ck() {
55	if mount | grep $mntpoint | grep -q "read-only"; then
56		if pstat $!| awk "\$2 == \"$map\"" | grep -q " rw-"; then
57			echo
58			echo "$1 difference"
59			mount | grep $mntpoint
60			printf "RW mount mapping and RO mount mapping:\n%s\n" "$r"
61			pstat $! | awk "\$2 == \"$map\""
62			status=$((status + 1))
63		fi
64	else
65		echo "$1 mount point RO did not succeed"
66		mount | grep $mntpoint
67		status=$((status + 1))
68	fi
69}
70
71status=0
72file=$mntpoint/mountu.sh.file
73mapfile=/tmp/mountu.sh.map
74mount | grep -q "$mntpoint " && umount $mntpoint
75mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
76mdconfig -a -t swap -s 100m -u $mdstart
77bsdlabel -w md$mdstart auto
78newfs $newfs_flags md${mdstart}$part > /dev/null
79mount /dev/md${mdstart}$part $mntpoint
80chmod 777 $mntpoint
81
82# ufs
83exec 5>$mapfile
84/tmp/mountu UFS $file &
85pid=$!
86sleep 1
87map=`cat $mapfile`; rm $mapfile; exec 5>&-
88
89r=`pstat $! | awk "\\$2 == \"$map\""`
90mount -u -o ro $mntpoint 2>/dev/null || mount -fu -o ro $mntpoint
91ck UFS
92mount -u -o rw $mntpoint
93rm -f $file
94wait $pid
95s=$?
96[ $s -ne 139 ] && { echo "UFS exit status is $s"; status=1; }
97while mount | grep -q "$mntpoint "; do
98	umount $mntpoint || sleep 1
99done
100mdconfig -d -u $mdstart
101
102# nfs
103if ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1; then
104	mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export \
105	    $mntpoint
106	sleep .2
107	rm -f $file
108	/tmp/mountu NFS $file &
109	pid=$!
110	sleep 1
111
112	r=`pstat $! | awk "\\$2 == \"$map\""`
113	mount -u -o ro $mntpoint 2>/dev/null ||
114	    mount -fu -o ro $mntpoint 2>/dev/null
115	ck NFS
116	wait $pid
117	s=$?
118	[ $s -ne 139 ] && { echo "NFS exit status is $s"; status=1; }
119
120	mount -u -o rw $mntpoint 2>/dev/null
121	sleep .2
122	[ -f $file ] && rm -f $file
123	umount $mntpoint || umount $mntpoint
124fi
125
126# msdos
127if [ -x /sbin/mount_msdosfs ]; then
128	mdconfig -a -t swap -s 100m -u $mdstart
129	bsdlabel -w md$mdstart auto
130	newfs_msdos -F 16 -b 8192 /dev/md${mdstart}$part > /dev/null 2>&1
131	mount_msdosfs -m 777 /dev/md${mdstart}$part $mntpoint
132	/tmp/mountu MSDOS $file &
133	pid=$!
134
135	sleep 1
136	r=`pstat $! | awk "\\$2 == \"$map\""`
137	mount -u -o ro $mntpoint 2>/dev/null || mount -fu -o ro $mntpoint
138	ck MSDOS
139	wait $pid
140	s=$?
141	[ $s -ne 139 ] && { echo "MSDOS exit status is $s"; status=1; }
142	mount -u -o rw $mntpoint
143	rm -f $file
144
145	while mount | grep -q "$mntpoint "; do
146		umount $mntpoint || sleep 1
147	done
148	mdconfig -d -u $mdstart
149fi
150
151# tmpfs
152mount -t tmpfs null $mntpoint
153chmod 777 $mntpoint
154
155/tmp/mountu TMPFS $file &
156pid=$!
157
158sleep 1
159r=`pstat $! | awk "\\$2 == \"$map\""`
160mount -u -o ro $mntpoint 2>/dev/null || mount -fu -o ro $mntpoint
161ck TMPFS
162sleep 1
163mount -u -o rw $mntpoint
164rm -f $file
165wait $pid
166s=$?
167[ $s -ne 139 ] && { echo "TMPFS exit status is $s"; status=1; }
168while mount | grep -q "$mntpoint "; do
169	umount $mntpoint || sleep 1
170done
171
172rm -f /tmp/mountu
173exit 0
174EOF
175/* kib@ noted:
176   UFS/NFS/msdosfs reclaim vnode on rw->ro forced remount, and
177   change the type of the underying object to OBJT_DEAD, but leave
178   the pages on the object queue and installed in the page tables.
179   Applications can read/write already mapped pages, but cannot
180   page in new pages, cannot observe possible further modifications
181   to already mapped pages (if ro->rw remount happen later), and
182   their updates to pages are not flushed to file.
183
184   It is impossible to mimic this behaviour for tmpfs.
185 */
186#include <sys/param.h>
187#include <sys/mman.h>
188#include <sys/ucontext.h>
189
190#include <err.h>
191#include <errno.h>
192#include <fcntl.h>
193#include <pwd.h>
194#include <signal.h>
195#include <stdio.h>
196#include <stdlib.h>
197#include <string.h>
198#include <unistd.h>
199
200#define STARTADDR 0x0U
201#define ADRSPACE 0x0640000U
202
203static void
204sighandler(int signo, siginfo_t *si, void *uc1)
205{
206	ucontext_t *uc;
207
208	uc = uc1;
209	printf("SIG%s at %p, addr %p\n", sys_signame[signo], si->si_addr,
210#if defined(__i386__)
211	    (void *)uc->uc_mcontext.mc_eip);
212#else
213	    (void *)uc->uc_mcontext.mc_rip);
214#endif
215	exit(1);
216}
217
218int
219main(int argc __unused, char **argv)
220{
221	struct passwd *pw;
222	struct sigaction sa;
223	void *p;
224	size_t len;
225	int fd;
226	char *name, *path;
227	volatile char *c;
228
229	memset(&sa, 0, sizeof(sa));
230	sa.sa_sigaction = sighandler;
231	sa.sa_flags = SA_SIGINFO;
232	if (sigaction(SIGSEGV, &sa, NULL) == -1)
233		err(1, "sigaction(SIGSEGV)");
234	if (sigaction(SIGBUS, &sa, NULL) == -1)
235		err(1, "sigaction(SIGBUS)");
236
237	if ((pw = getpwnam("nobody")) == NULL)
238		err(1, "no such user: nobody");
239
240	if (setgroups(1, &pw->pw_gid) ||
241	    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
242	    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
243		err(1, "Can't drop privileges to \"nobody\"");
244	endpwent();
245
246	p = (void *)STARTADDR;
247	len = ADRSPACE;
248
249	name = argv[1];
250	path = argv[2];
251	if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1)
252		err(1,"open(%s)", path);
253	if (ftruncate(fd, len) == -1)
254		err(1, "ftruncate");
255	if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
256			MAP_FAILED) {
257		if (errno == ENOMEM) {
258			warn("mmap()");
259			return (1);
260		}
261		err(1, "mmap(1)");
262	}
263	dprintf(5, "%p\n", p);
264
265	for (c = p; (void *)c < p + len; c += PAGE_SIZE) {
266		*c = 1;
267	}
268
269	close(fd);
270	sleep(5);
271	fprintf(stderr, "%s: Late read start.\n", name);
272	for (c = p; (void *)c < p + len; c += PAGE_SIZE) {
273		*c;
274	}
275	fprintf(stderr, "%s: Late read complete.\n", name);
276
277	fprintf(stderr, "%s: Late write start.\n", name);
278	for (c = p; (void *)c < p + len; c += PAGE_SIZE) {
279		*c = 1;
280	}
281	fprintf(stderr, "%s: Late write complete.\n", name);
282
283	return (0);
284}
285