xref: /freebsd/tools/test/stress2/misc/renameat2.sh (revision 05cbd5fc2b481d6503c69be95fe9a6d9a287bc6d)
1#!/bin/sh
2
3#
4# Copyright (c) 2026 Peter Holm <pho@FreeBSD.org>
5#
6# SPDX-License-Identifier: BSD-2-Clause
7#
8
9# Simple renameat2 test scenario
10
11[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
12
13. ../default.cfg
14err=0
15prog=$(basename "$0" .sh)
16
17cat > /tmp/$prog.c <<EOF
18#include <sys/stat.h>
19#include <err.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <unistd.h>
24
25int
26main(void)
27{
28	int fd;
29	char *f1 = "f1";
30	char *f2 = "f2";
31	char *t1 = "t1";
32
33	if ((fd = open(f1, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
34		err(1, "open(%s)", f1);
35	close(fd);
36	if ((fd = open(f2, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
37		err(1, "open(%s)", f2);
38	close(fd);
39
40	if (renameat2(AT_FDCWD, f1, AT_FDCWD, t1, AT_RENAME_NOREPLACE) == -1)
41		err(1, "renameat2(%s, %s)", f1, t1);
42	if (renameat2(AT_FDCWD, f2, AT_FDCWD, t1, AT_RENAME_NOREPLACE) == 0)
43		errx(1, "renameat2(%s, %s)", f2, t1);
44	if (errno != EEXIST && errno != EOPNOTSUPP)
45		err(1, "renameat2(%s, %s)", f2, t1);
46	if (unlink(f2) != 0)
47		err(1, "unlink(%s)", f2);
48	if (unlink(t1) != 0)
49		err(1, "unlink(%s)", t1);
50}
51EOF
52cc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c || exit 1
53
54echo ufs
55set -eu
56mount | grep -q "on $mntpoint " && umount -f $mntpoint
57mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
58mdconfig -s 1g -u $mdstart
59newfs $newfs_flags /dev/md$mdstart > /dev/null
60mount /dev/md$mdstart $mntpoint
61set +e
62
63cd $mntpoint
64/tmp/$prog || err=$((err+1))
65cd -
66umount $mntpoint
67mdconfig -d -u $mdstart
68
69echo tmpfs
70mount -t tmpfs dummy $mntpoint
71cd $mntpoint
72/tmp/$prog || err=$((err+1))
73cd -
74umount $mntpoint
75
76echo msdosfs
77if [ -x /sbin/mount_msdosfs ]; then
78	mdconfig -a -t swap -s 1g -u $mdstart
79	gpart create -s bsd md$mdstart > /dev/null
80	gpart add -t freebsd-ufs md$mdstart > /dev/null
81	part=a
82	newfs_msdos -F 16 -b 8192 /dev/md${mdstart}$part > /dev/null 2>&1
83	mount_msdosfs -m 777 /dev/md${mdstart}$part $mntpoint
84
85	cd $mntpoint
86	/tmp/$prog || err=$((err+1))
87	cd -
88	umount $mntpoint
89fi
90echo zfs
91u1=$mdstart
92u2=$((u1 + 1))
93
94mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
95mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
96
97mdconfig -s 1g -u $u1
98mdconfig -s 1g -u $u2
99
100zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
101[ -d /stress2_tank ] && rm -rf /stress2_tank
102zpool create stress2_tank raidz md$u1 md$u2
103zfs create stress2_tank/test
104
105cd /stress2_tank/test
106/tmp/$prog || err=$((err+1))
107cd -
108
109zfs umount stress2_tank/test
110zfs destroy -r stress2_tank
111zpool destroy stress2_tank
112mdconfig -d -u $u1
113mdconfig -d -u $u2
114
115rm -f /tmp/$prog /tmp/$prog.c
116exit $err
117