1# vim: filetype=sh
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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27#
28# Determine whether this version of the ksh being
29# executed has a bug where the limit of background
30# processes of 25.
31#
32function check_bg_procs_limit_num
33{
34$ECHO "#!/usr/local/bin/ksh93" > $TMPDIR/exitsZero.ksh
35$ECHO  "exit 0" >> $TMPDIR/exitsZero.ksh
36$CHMOD 777 $TMPDIR/exitsZero.ksh
37
38$CAT <<EOF > $TMPDIR/testbackgprocs.ksh
39#!/usr/local/bin/ksh93
40#
41# exitsZero.ksh is a one line script
42# that exit with status of "0"
43#
44
45PIDS=""
46typeset -i i=1
47while [ \$i -le 50 ]
48do
49	$TMPDIR/exitsZero.ksh &
50        PIDS="\$PIDS \$!"
51        (( i = i + 1 ))
52done
53
54\$SLEEP 1
55
56for pid in \$PIDS
57do
58	\$WAIT \$pid
59        (( \$? == 127 )) && exit 1
60done
61exit 0
62EOF
63
64$KSH93 $TMPDIR/testbackgprocs.ksh
65if [[ $? -eq 1 ]]; then
66#
67# Current ksh being executed has a limit
68# of 25 background processes.
69#
70	return 1
71else
72        return 0
73fi
74}
75
76function init_setup
77{
78
79	typeset disklist=$1
80
81        create_pool $TESTPOOL "$disklist"
82
83	if ! is_global_zone ; then
84		reexport_pool
85	fi
86
87        $RM -rf $TESTDIR  || log_unresolved Could not remove $TESTDIR
88        $MKDIR -p $TESTDIR || log_unresolved Could not create $TESTDIR
89
90        $RM -rf $TESTDIR_TGT  || log_unresolved Could not remove $TESTDIR_TGT
91        $MKDIR -p $TESTDIR_TGT || log_unresolved Could not create $TESTDIR_TGT
92
93        log_must $ZFS create $TESTPOOL/$TESTFS
94        log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
95
96        log_must $ZFS create $TESTPOOL/$TESTFS_TGT
97        log_must $ZFS set mountpoint=$TESTDIR_TGT $TESTPOOL/$TESTFS_TGT
98
99        $MKDIR -p $OLDDIR || log_unresolved Could not create $OLDDIR
100        $MKDIR -p $NEWDIR_IN_FS || log_unresolved Could not create $NEWDIR_IN_FS
101        $MKDIR -p $NEWDIR_ACROSS_FS || log_unresolved Could not create $NEWDIR_ACROSS_FS
102
103}
104
105function wait_pid
106{
107	for pid in $1
108	do
109		$PS -e | $GREP $pid >/dev/null 2>&1
110      	 	(( $? == 0 )) && $WAIT $pid
111        done
112}
113
114
115#
116# Generate given number files in a
117# directory of zfs file system
118# $1 - the directory holds the generated files
119# $2 - number of to-be-generated files
120#
121
122function generate_files
123{
124	typeset -i count
125        typeset -i proc_num=0
126
127	if (( $2 == $MVNUMFILES )); then
128		count=1
129	else
130		count=$MVNUMFILES+1
131	fi
132
133        while (( count <= $2 ))
134        do
135                $CP /etc/passwd $1/file_$count \
136                         > /dev/null 2>&1 &
137
138                PIDS="$PIDS $!"
139
140                proc_num=`$ECHO $PIDS | $WC -w`
141                if (( proc_num >= GANGPIDS )); then
142                        wait_pid "$PIDS"
143                        proc_num=0
144                        PIDS=""
145                fi
146
147               (( count = count + 1 ))
148        done
149
150}
151
152#
153# Move given number files from one directory to
154# another directory in parallel
155# $1 - source directory
156# $2 - target directory
157#
158function mv_files
159{
160        $FIND $1 -type f -print | xargs -J % \
161                $MV % $2 > /dev/null 2>&1
162}
163
164#
165# Count the files number after moving, and
166# compare it with the original number
167# $1 - directory that to be operated
168# $2 - original files number
169#
170function count_files
171{
172        typeset -i file_num
173        file_num=`$FIND $1  -type f -print | \
174                wc -l`
175        (( file_num != $2 )) && \
176                log_fail "The file number of target directory"\
177                        "$2 is not equal to that of the source "\
178                        "directory $1"
179
180}
181
182#
183# Running the 'mv' test
184# $1 - old directory
185# $2 - new directory
186#
187function mv_test
188{
189        typeset old=$1
190        typeset new=$2
191
192        typeset -i inc_num=$(( MVNUMFILES + MVNUMINCR ))
193        typeset -i num=0
194
195        for num in $MVNUMFILES $inc_num
196        do
197                generate_files $old $num
198
199                mv_files $old $new
200                count_files $new $num
201
202                mv_files $new $old
203                count_files $old $num
204        done
205
206	typeset dir=$(get_device_dir $DISKS)
207        verify_filesys "$TESTPOOL" "$TESTPOOL/$TESTFS" "$dir"
208
209        return 0
210}
211
212