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