xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/linktest/linktest.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker#!/bin/sh
2*49cdfc7eSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0-or-later
3*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) International Business Machines Corp., 2000
4*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) Linux Test Project, 2012-2019
5*49cdfc7eSAndroid Build Coastguard Worker# Regression test for max links per file
6*49cdfc7eSAndroid Build Coastguard Worker# Author: Ngie Cooper <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard WorkerTST_NEEDS_TMPDIR=1
9*49cdfc7eSAndroid Build Coastguard WorkerTST_TESTFUNC=do_test
10*49cdfc7eSAndroid Build Coastguard WorkerTST_OPTS="a:s:"
11*49cdfc7eSAndroid Build Coastguard WorkerTST_PARSE_ARGS=parse_args
12*49cdfc7eSAndroid Build Coastguard WorkerTST_USAGE=usage
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Workerhard_links=1000
15*49cdfc7eSAndroid Build Coastguard Workersoft_links=1000
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Workerusage()
18*49cdfc7eSAndroid Build Coastguard Worker{
19*49cdfc7eSAndroid Build Coastguard Worker	echo "Usage: linktest.sh {-a n} {-s n}"
20*49cdfc7eSAndroid Build Coastguard Worker	echo "-a n    Hard link count"
21*49cdfc7eSAndroid Build Coastguard Worker	echo "-s n    Soft link count"
22*49cdfc7eSAndroid Build Coastguard Worker}
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Workerparse_args()
25*49cdfc7eSAndroid Build Coastguard Worker{
26*49cdfc7eSAndroid Build Coastguard Worker	tst_is_int "$2" || tst_brk TBROK "-$1 must be integer ($2)"
27*49cdfc7eSAndroid Build Coastguard Worker	[ "$2" -ge 0 ] || tst_brk TBROK "-$1 must be >= 0 ($2)"
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker	case $1 in
30*49cdfc7eSAndroid Build Coastguard Worker	a) hard_links=$2;;
31*49cdfc7eSAndroid Build Coastguard Worker	s) soft_links=$2;;
32*49cdfc7eSAndroid Build Coastguard Worker	esac
33*49cdfc7eSAndroid Build Coastguard Worker}
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Workerdo_link()
36*49cdfc7eSAndroid Build Coastguard Worker{
37*49cdfc7eSAndroid Build Coastguard Worker	local prefix="$1"
38*49cdfc7eSAndroid Build Coastguard Worker	local ln_opts="$2"
39*49cdfc7eSAndroid Build Coastguard Worker	local limit="$3"
40*49cdfc7eSAndroid Build Coastguard Worker	local prefix_msg="$4"
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker	local lerrors=0
43*49cdfc7eSAndroid Build Coastguard Worker	local i=0
44*49cdfc7eSAndroid Build Coastguard Worker	local rtype="TFAIL"
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker	tst_res TINFO "test $prefix_msg link, limit: $limit"
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker	cd "${prefix}link.$$"
49*49cdfc7eSAndroid Build Coastguard Worker	while [ $i -lt $limit ]; do
50*49cdfc7eSAndroid Build Coastguard Worker		if ! ln $ln_opts "$PWD/${prefix}file" ${prefix}file${i}; then
51*49cdfc7eSAndroid Build Coastguard Worker			lerrors=$((lerrors + 1))
52*49cdfc7eSAndroid Build Coastguard Worker		fi
53*49cdfc7eSAndroid Build Coastguard Worker		i=$((i + 1))
54*49cdfc7eSAndroid Build Coastguard Worker	done
55*49cdfc7eSAndroid Build Coastguard Worker	cd ..
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker	[ $lerrors -eq 0 ] && rtype="TPASS"
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker	tst_res $rtype "errors: $lerrors"
60*49cdfc7eSAndroid Build Coastguard Worker}
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Workerdo_test()
63*49cdfc7eSAndroid Build Coastguard Worker{
64*49cdfc7eSAndroid Build Coastguard Worker	mkdir hlink.$$ slink.$$
65*49cdfc7eSAndroid Build Coastguard Worker	touch hlink.$$/hfile slink.$$/sfile
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker	do_link "s" "-s" $soft_links "symbolic"
68*49cdfc7eSAndroid Build Coastguard Worker	do_link "h"   "" $hard_links "hard"
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker	rm -rf hlink.$$ slink.$$
71*49cdfc7eSAndroid Build Coastguard Worker}
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker. tst_test.sh
74*49cdfc7eSAndroid Build Coastguard Workertst_run
75