1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
3f08c3bdfSopenharmony_ci# Copyright (c) Linux Test Project, 2002-2023
4f08c3bdfSopenharmony_ci# Copyright (c) 2016-2018 Oracle and/or its affiliates. All Rights Reserved.
5f08c3bdfSopenharmony_ci# Copyright (c) International Business Machines  Corp., 2001
6f08c3bdfSopenharmony_ci#
7f08c3bdfSopenharmony_ci#  PURPOSE:
8f08c3bdfSopenharmony_ci#           Two processes open FLOCK_IDATA file simultaneously
9f08c3bdfSopenharmony_ci#           each one locks odd and even lines of the file simultaneously
10f08c3bdfSopenharmony_ci#           and fill them with '0's and '1's. After they find eof, the
11f08c3bdfSopenharmony_ci#           datafiles are compared.
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ciTST_SETUP="do_setup"
14f08c3bdfSopenharmony_ciTST_TESTFUNC="do_test"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ciNCHARS=${NCHARS:-64}
17f08c3bdfSopenharmony_ciNLINES=${NLINES:-16384}
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cigenerate_file()
20f08c3bdfSopenharmony_ci{
21f08c3bdfSopenharmony_ci	local file="$1"
22f08c3bdfSopenharmony_ci	local nchars="$2"
23f08c3bdfSopenharmony_ci	local nlines="$3"
24f08c3bdfSopenharmony_ci	local val="$4"
25f08c3bdfSopenharmony_ci	local exp_size="$((nchars*nlines))"
26f08c3bdfSopenharmony_ci	local size
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci	ROD nfs_flock_dgen $file $nchars $nlines $val
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	size="$(wc -c $file | awk '{print $1}')"
31f08c3bdfSopenharmony_ci	if [ $size -ne $exp_size ]; then
32f08c3bdfSopenharmony_ci		tst_brk TBROK "could not create '$file' (size: $size, expected: $exp_size)"
33f08c3bdfSopenharmony_ci	fi
34f08c3bdfSopenharmony_ci}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cido_setup()
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	if ! tst_is_int $NCHARS || [ $NCHARS -lt 1 ]; then
39f08c3bdfSopenharmony_ci		tst_res TBROK "NCHARS must be > 0"
40f08c3bdfSopenharmony_ci	fi
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	if ! tst_is_int $NLINES || [ $NLINES -lt 1 ]; then
43f08c3bdfSopenharmony_ci		tst_res TBROK "NLINES must be > 0"
44f08c3bdfSopenharmony_ci	fi
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	nfs_setup
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	tst_res TINFO "creating test files (chars: $NCHARS, lines: $NLINES)"
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	generate_file flock_data $NCHARS $NLINES 0
51f08c3bdfSopenharmony_ci	generate_file flock_odata $NCHARS $NLINES 1
52f08c3bdfSopenharmony_ci}
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cido_test()
55f08c3bdfSopenharmony_ci{
56f08c3bdfSopenharmony_ci	tst_res TINFO "Testing locking"
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	ROD cp flock_data flock_idata
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	tst_res TINFO "locking 'flock_idata' file and writing data"
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	nfs_flock 0 flock_idata $NCHARS $NLINES &
63f08c3bdfSopenharmony_ci	local pids=$!
64f08c3bdfSopenharmony_ci	nfs_flock 1 flock_idata $NCHARS $NLINES &
65f08c3bdfSopenharmony_ci	pids="$pids $!"
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	tst_res TINFO "waiting for pids: $pids"
68f08c3bdfSopenharmony_ci	for p in $pids; do
69f08c3bdfSopenharmony_ci		wait $p
70f08c3bdfSopenharmony_ci		if [ $? -ne 0 ]; then
71f08c3bdfSopenharmony_ci			tst_brk TFAIL "nfs_lock process failed"
72f08c3bdfSopenharmony_ci		else
73f08c3bdfSopenharmony_ci			tst_res TINFO "$p completed"
74f08c3bdfSopenharmony_ci		fi
75f08c3bdfSopenharmony_ci	done
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	diff flock_odata flock_idata
78f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
79f08c3bdfSopenharmony_ci		tst_res TFAIL "content is different"
80f08c3bdfSopenharmony_ci	else
81f08c3bdfSopenharmony_ci		tst_res TPASS "content is the same"
82f08c3bdfSopenharmony_ci	fi
83f08c3bdfSopenharmony_ci}
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci. nfs_lib.sh
86f08c3bdfSopenharmony_citst_run
87