Skip to content

Commit 71f55ac

Browse files
committed
tests: add tests for renameat2(2) flags
Since mv(1) doesn't expose the RENAME_* flags we need to have our own variation in the tests/ tree. The tests are fairly obvious functional tests, though in the future (once we solve the d_revalidate issue) we might also add a full-stack overlayfs integration test. Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 4e7d867 commit 71f55ac

File tree

16 files changed

+423
-0
lines changed

16 files changed

+423
-0
lines changed

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ AC_CONFIG_FILES([
226226
tests/zfs-tests/cmd/randfree_file/Makefile
227227
tests/zfs-tests/cmd/randwritecomp/Makefile
228228
tests/zfs-tests/cmd/readmmap/Makefile
229+
tests/zfs-tests/cmd/renameat2/Makefile
229230
tests/zfs-tests/cmd/rename_dir/Makefile
230231
tests/zfs-tests/cmd/rm_lnkcnt_zero_file/Makefile
231232
tests/zfs-tests/cmd/send_doall/Makefile
@@ -375,6 +376,7 @@ AC_CONFIG_FILES([
375376
tests/zfs-tests/tests/functional/refquota/Makefile
376377
tests/zfs-tests/tests/functional/refreserv/Makefile
377378
tests/zfs-tests/tests/functional/removal/Makefile
379+
tests/zfs-tests/tests/functional/renameat2/Makefile
378380
tests/zfs-tests/tests/functional/rename_dirs/Makefile
379381
tests/zfs-tests/tests/functional/replacement/Makefile
380382
tests/zfs-tests/tests/functional/reservation/Makefile

tests/runfiles/linux.run

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ tests = ['projectid_001_pos', 'projectid_002_pos', 'projectid_003_pos',
146146
'projecttree_001_pos', 'projecttree_002_pos', 'projecttree_003_neg']
147147
tags = ['functional', 'projectquota']
148148

149+
[tests/functional/renameat2:Linux]
150+
tests = ['renameat2_noreplace', 'renameat2_exchange', 'renameat2_whiteout']
151+
tags = ['functional', 'renameat2']
152+
149153
[tests/functional/rsend:Linux]
150154
tests = ['send_realloc_dnode_size', 'send_encrypted_files']
151155
tags = ['functional', 'rsend']

tests/test-runner/bin/zts-report.py.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ exec_reason = 'Test user execute permissions required for utilities'
6969
python_reason = 'Python v3.5 or newer required'
7070
python_deps_reason = 'Python modules missing: python-cffi'
7171

72+
#
73+
# Some tests require that the kernel supports renameat2 syscall.
74+
#
75+
renameat2_reason = 'Kernel renameat2 support required'
76+
7277
#
7378
# Some tests require the O_TMPFILE flag which was first introduced in the
7479
# 3.11 kernel.
@@ -234,6 +239,7 @@ maybe = {
234239
'redundancy/redundancy_004_neg': ['FAIL', '7290'],
235240
'redundancy/redundancy_draid_spare3': ['SKIP', known_reason],
236241
'removal/removal_condense_export': ['FAIL', known_reason],
242+
'renameat2/setup': ['SKIP', renameat2_reason],
237243
'reservation/reservation_008_pos': ['FAIL', '7741'],
238244
'reservation/reservation_018_pos': ['FAIL', '5642'],
239245
'rsend/rsend_019_pos': ['FAIL', '6086'],

tests/zfs-tests/cmd/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SUBDIRS = \
2323
nvlist_to_lua \
2424
randwritecomp \
2525
readmmap \
26+
renameat2 \
2627
rename_dir \
2728
rm_lnkcnt_zero_file \
2829
send_doall \
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/renameat2
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include $(top_srcdir)/config/Rules.am
2+
3+
pkgexecdir = $(datadir)/@PACKAGE@/zfs-tests/bin
4+
5+
pkgexec_PROGRAMS = renameat2
6+
renameat2_SOURCES = renameat2.c
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* SPDX-License-Identifier: CDDL-1.0 OR MPL-2.0 */
2+
/*
3+
* CDDL HEADER START
4+
*
5+
* The contents of this file are subject to the terms of the
6+
* Common Development and Distribution License (the "License").
7+
* You may not use this file except in compliance with the License.
8+
*
9+
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10+
* or http://www.opensolaris.org/os/licensing.
11+
* See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*
14+
* When distributing Covered Code, include this CDDL HEADER in each
15+
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16+
* If applicable, add the following below this CDDL HEADER, with the
17+
* fields enclosed by brackets "[]" replaced with your own identifying
18+
* information: Portions Copyright [yyyy] [name of copyright owner]
19+
*
20+
* CDDL HEADER END
21+
*/
22+
23+
/*
24+
* Copyright (C) 2019 Aleksa Sarai <[email protected]>
25+
* Copyright (C) 2019 SUSE LLC
26+
*/
27+
28+
/*
29+
* mv(1) doesn't currently support RENAME_{EXCHANGE,WHITEOUT} so this is a very
30+
* simple renameat2(2) wrapper for the OpenZFS self-tests.
31+
*/
32+
33+
#include <errno.h>
34+
#include <fcntl.h>
35+
#include <unistd.h>
36+
#include <stdio.h>
37+
#include <stdlib.h>
38+
#include <string.h>
39+
#include <sys/syscall.h>
40+
41+
#ifndef SYS_renameat2
42+
#ifdef __NR_renameat2
43+
#define SYS_renameat2 __NR_renameat2
44+
#elif defined(__x86_64__)
45+
#define SYS_renameat2 316
46+
#elif defined(__i386__)
47+
#define SYS_renameat2 353
48+
#elif defined(__arm__) || defined(__aarch64__)
49+
#define SYS_renameat2 382
50+
#else
51+
#error "SYS_renameat2 not known for this architecture."
52+
#endif
53+
#endif
54+
55+
#ifndef RENAME_NOREPLACE
56+
#define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
57+
#endif
58+
#ifndef RENAME_EXCHANGE
59+
#define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
60+
#endif
61+
#ifndef RENAME_WHITEOUT
62+
#define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
63+
#endif
64+
65+
/* glibc doesn't provide renameat2 wrapper, let's use our own */
66+
static int
67+
sys_renameat2(int olddirfd, const char *oldpath,
68+
int newdirfd, const char *newpath, unsigned int flags)
69+
{
70+
int ret = syscall(SYS_renameat2, olddirfd, oldpath, newdirfd, newpath,
71+
flags);
72+
return ((ret < 0) ? -errno : ret);
73+
}
74+
75+
static void
76+
usage(void)
77+
{
78+
fprintf(stderr, "usage: renameat2 [-Cnwx] src dst\n");
79+
exit(1);
80+
}
81+
82+
static void
83+
check(void)
84+
{
85+
int err = sys_renameat2(AT_FDCWD, ".", AT_FDCWD, ".", RENAME_EXCHANGE);
86+
exit(err == -ENOSYS);
87+
}
88+
89+
int
90+
main(int argc, char **argv)
91+
{
92+
char *src, *dst;
93+
int ch, err;
94+
unsigned int flags = 0;
95+
96+
while ((ch = getopt(argc, argv, "Cnwx")) >= 0) {
97+
switch (ch) {
98+
case 'C':
99+
check();
100+
break;
101+
case 'n':
102+
flags |= RENAME_NOREPLACE;
103+
break;
104+
case 'w':
105+
flags |= RENAME_WHITEOUT;
106+
break;
107+
case 'x':
108+
flags |= RENAME_EXCHANGE;
109+
break;
110+
default:
111+
usage();
112+
break;
113+
}
114+
}
115+
116+
argc -= optind;
117+
argv += optind;
118+
119+
if (argc != 2)
120+
usage();
121+
src = argv[0];
122+
dst = argv[1];
123+
124+
err = sys_renameat2(AT_FDCWD, src, AT_FDCWD, dst, flags);
125+
if (err < 0)
126+
fprintf(stderr, "renameat2: %s", strerror(-err));
127+
return (err != 0);
128+
}

tests/zfs-tests/include/commands.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export ZFSTEST_FILES='badsend
216216
randfree_file
217217
randwritecomp
218218
readmmap
219+
renameat2
219220
rename_dir
220221
rm_lnkcnt_zero_file
221222
send_doall

tests/zfs-tests/tests/functional/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ SUBDIRS = \
6262
refquota \
6363
refreserv \
6464
removal \
65+
renameat2 \
6566
rename_dirs \
6667
replacement \
6768
reservation \
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pkgdatadir = $(datadir)/@PACKAGE@/zfs-tests/tests/functional/renameat2
2+
dist_pkgdata_SCRIPTS = \
3+
setup.ksh \
4+
cleanup.ksh \
5+
renameat2_noreplace.ksh \
6+
renameat2_exchange.ksh \
7+
renameat2_whiteout.ksh
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/ksh -p
2+
#
3+
# CDDL HEADER START
4+
#
5+
# The contents of this file are subject to the terms of the
6+
# Common Development and Distribution License (the "License").
7+
# You may not use this file except in compliance with the License.
8+
#
9+
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10+
# or http://www.opensolaris.org/os/licensing.
11+
# See the License for the specific language governing permissions
12+
# and limitations under the License.
13+
#
14+
# When distributing Covered Code, include this CDDL HEADER in each
15+
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16+
# If applicable, add the following below this CDDL HEADER, with the
17+
# fields enclosed by brackets "[]" replaced with your own identifying
18+
# information: Portions Copyright [yyyy] [name of copyright owner]
19+
#
20+
# CDDL HEADER END
21+
#
22+
23+
#
24+
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25+
# Use is subject to license terms.
26+
#
27+
28+
#
29+
# Copyright (c) 2013 by Delphix. All rights reserved.
30+
#
31+
32+
. $STF_SUITE/include/libtest.shlib
33+
34+
default_cleanup
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/ksh -p
2+
# SPDX-License-Identifier: CDDL-1.0 OR MPL-2.0
3+
4+
#
5+
# CDDL HEADER START
6+
#
7+
# The contents of this file are subject to the terms of the
8+
# Common Development and Distribution License (the "License").
9+
# You may not use this file except in compliance with the License.
10+
#
11+
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
12+
# or http://www.opensolaris.org/os/licensing.
13+
# See the License for the specific language governing permissions
14+
# and limitations under the License.
15+
#
16+
# When distributing Covered Code, include this CDDL HEADER in each
17+
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
18+
# If applicable, add the following below this CDDL HEADER, with the
19+
# fields enclosed by brackets "[]" replaced with your own identifying
20+
# information: Portions Copyright [yyyy] [name of copyright owner]
21+
#
22+
# CDDL HEADER END
23+
#
24+
25+
#
26+
# Copyright (C) 2019 Aleksa Sarai <[email protected]>
27+
# Copyright (C) 2019 SUSE LLC
28+
#
29+
30+
. $STF_SUITE/include/libtest.shlib
31+
32+
verify_runnable "both"
33+
34+
function cleanup
35+
{
36+
log_must rm -rf $TESTDIR/*
37+
}
38+
39+
log_assert "ZFS supports RENAME_EXCHANGE."
40+
log_onexit cleanup
41+
42+
cd $TESTDIR
43+
echo "foo" > foo
44+
echo "bar" > bar
45+
46+
# Self-exchange is a no-op.
47+
log_must renameat2 -x foo foo
48+
log_must grep '^foo$' foo
49+
50+
# Basic exchange.
51+
log_must renameat2 -x foo bar
52+
log_must grep '^bar$' foo
53+
log_must grep '^foo$' bar
54+
55+
# And exchange back.
56+
log_must renameat2 -x foo bar
57+
log_must grep '^foo$' foo
58+
log_must grep '^bar$' bar
59+
60+
# Exchange with a bad path should fail.
61+
log_mustnot renameat2 -x bar baz
62+
63+
log_pass "ZFS supports RENAME_EXCHANGE as expected."
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/ksh -p
2+
# SPDX-License-Identifier: CDDL-1.0 OR MPL-2.0
3+
4+
#
5+
# CDDL HEADER START
6+
#
7+
# The contents of this file are subject to the terms of the
8+
# Common Development and Distribution License (the "License").
9+
# You may not use this file except in compliance with the License.
10+
#
11+
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
12+
# or http://www.opensolaris.org/os/licensing.
13+
# See the License for the specific language governing permissions
14+
# and limitations under the License.
15+
#
16+
# When distributing Covered Code, include this CDDL HEADER in each
17+
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
18+
# If applicable, add the following below this CDDL HEADER, with the
19+
# fields enclosed by brackets "[]" replaced with your own identifying
20+
# information: Portions Copyright [yyyy] [name of copyright owner]
21+
#
22+
# CDDL HEADER END
23+
#
24+
25+
#
26+
# Copyright (C) 2019 Aleksa Sarai <[email protected]>
27+
# Copyright (C) 2019 SUSE LLC
28+
#
29+
30+
. $STF_SUITE/include/libtest.shlib
31+
32+
verify_runnable "both"
33+
34+
function cleanup
35+
{
36+
log_must rm -rf $TESTDIR/*
37+
}
38+
39+
log_assert "ZFS supports RENAME_NOREPLACE."
40+
log_onexit cleanup
41+
42+
cd $TESTDIR
43+
touch foo bar
44+
45+
# Clobbers should always fail.
46+
log_mustnot renameat2 -n foo foo
47+
log_mustnot renameat2 -n foo bar
48+
log_mustnot renameat2 -n bar foo
49+
50+
# Regular renames should succeed.
51+
log_must renameat2 -n bar baz
52+
53+
log_pass "ZFS supports RENAME_NOREPLACE as expected."

0 commit comments

Comments
 (0)