Add test for file truncation over NFS
PATCH 1/1 Add test for file truncation over NFS
Some older kernels have a bug in NFS where the client caches that a file does not exist after unsuccessful access to it and then does not pass O_TRUNC flag to the server even though the file may have been created by another client in the mean time. This may result in new data being merged with old file contents instead of replacing them.
--- runtest/net.nfs | 11 ++++++++ testcases/network/nfs/nfs_stress/nfs09.sh | 34 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100755 testcases/network/nfs/nfs_stress/nfs09.sh
runtest/net.nfs +11 -0
@@ -83,6 +83,17 @@ nfs08_v40_ip6t nfs08.sh -6 -v 4 -t tcp nfs08_v41_ip6t nfs08.sh -6 -v 4.1 -t tcp nfs08_v42_ip6t nfs08.sh -6 -v 4.2 -t tcp +nfs09_v30_ip4u nfs09.sh -v 3 -t udp +nfs09_v30_ip4t nfs09.sh -v 3 -t tcp +nfs09_v40_ip4t nfs09.sh -v 4 -t tcp +nfs09_v41_ip4t nfs09.sh -v 4.1 -t tcp +nfs09_v42_ip4t nfs09.sh -v 4.2 -t tcp +nfs09_v30_ip6u nfs09.sh -6 -v 3 -t udp +nfs09_v30_ip6t nfs09.sh -6 -v 3 -t tcp +nfs09_v40_ip6t nfs09.sh -6 -v 4 -t tcp +nfs09_v41_ip6t nfs09.sh -6 -v 4.1 -t tcp +nfs09_v42_ip6t nfs09.sh -6 -v 4.2 -t tcp + nfslock01_v30_ip4u nfslock01.sh -v 3 -t udp nfslock01_v30_ip4t nfslock01.sh -v 3 -t tcp nfslock01_v40_ip4t nfslock01.sh -v 4 -t tcp
testcases/network/nfs/nfs_stress/nfs09.sh new file +34 -0
@@ -0,0 +1,34 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (C) 2024 SUSE LLC <[email protected]> +# +# DESCRIPTION: Check that stale NFS client cache does not prevent file +# truncation. +# +# 1. Through NFS, access metadata of a file that does not exist +# 2. Create the file on the remote end (bypassing NFS) +# 3. Through NFS, try to overwrite the file with shorter data + +TST_TESTFUNC="do_test" + +do_test() +{ + local local_file="testfile" + local remote_file="$(nfs_get_remote_path)/$local_file" + local testmsg='File truncated' + local data + + EXPECT_FAIL "ls -l '$local_file'" + tst_rhost_run -c "echo -n 'File rewritten not' >'$remote_file'" + echo -n "$testmsg" >"$local_file" + data=$(tst_rhost_run -c "cat '$remote_file'") + + if [ "$data" != "$testmsg" ]; then + tst_res TFAIL "Wrong file contents, expected '$testmsg', got '$data'" + else + tst_res TPASS "File was truncated correctly" + fi +} + +. nfs_lib.sh +tst_run
Signature · 5 lines
2.42.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Martin,
Merged, thank you!
Quoted 6 lines
Some older kernels have a bug in NFS where the client caches that a file does not exist after unsuccessful access to it and then does not pass
O_TRUNCflag to the server even though the file may have been created by another client in the mean time. This may result in new data being merged with old file contents instead of replacing them.
...
runtest/net.nfs nfs08_v40_ip6t nfs08.sh -6 -v 4 -t tcp · line 87
Quoted 7 lines
+++ b/runtest/net.nfs
runtest/net.nfs +2
-0@@ -83,6 +83,17 @@ nfs08_v40_ip6t nfs08.sh -6 -v 4 -t tcp nfs08_v41_ip6t nfs08.sh -6 -v 4.1 -t tcp nfs08_v42_ip6t nfs08.sh -6 -v 4.2 -t tcp +nfs09_v30_ip4u nfs09.sh -v 3 -t udp +nfs09_v30_ip4t nfs09.sh -v 3 -t tcp
For a record, this is the test which fails. And NFSv3 is affected on all kernels, including latest stable 6.7.4 and 6.8.0-rc1 (both openSUSE packages).
Quoted 8 lines
runtest/net.nfs +8
-0+nfs09_v40_ip4t nfs09.sh -v 4 -t tcp +nfs09_v41_ip4t nfs09.sh -v 4.1 -t tcp +nfs09_v42_ip4t nfs09.sh -v 4.2 -t tcp +nfs09_v30_ip6u nfs09.sh -6 -v 3 -t udp +nfs09_v30_ip6t nfs09.sh -6 -v 3 -t tcp +nfs09_v40_ip6t nfs09.sh -6 -v 4 -t tcp +nfs09_v41_ip6t nfs09.sh -6 -v 4.1 -t tcp +nfs09_v42_ip6t nfs09.sh -6 -v 4.2 -t tcp
...
Quoted 1 line
+++
b/testcases/network/nfs/nfs_stress/nfs09.sh
...
testcases/network/nfs/nfs_stress/nfs09.sh line 23
Quoted 10 lines
testcases/network/nfs/nfs_stress/nfs09.sh new file +10
-0+do_test() +{ + local local_file="testfile" + local remote_file="$(nfs_get_remote_path)/$local_file" + local testmsg='File truncated' + local data + + EXPECT_FAIL "ls -l '$local_file'" + tst_rhost_run -c "echo -n 'File rewritten not' >'$remote_file'" + echo -n "$testmsg" >"$local_file"
echo is considered unportable (printf is preferred), but "echo -n" should be hopefully safe nowadays.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp