1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/tests/functional/mv_files/mv_files.cfg
32
33#
34# Determine whether this version of the ksh being
35# executed has a bug where the limit of background
36# processes of 25.
37#
38function check_bg_procs_limit_num
39{
40echo "#!/usr/bin/ksh" > /var/tmp/exitsZero.ksh
41echo  "exit 0" >> /var/tmp/exitsZero.ksh
42chmod 777 /var/tmp/exitsZero.ksh
43
44cat <<EOF > /var/tmp/testbackgprocs.ksh
45#!/usr/bin/ksh
46#
47# exitsZero.ksh is a one line script
48# that exit with status of "0"
49#
50
51PIDS=""
52typeset -i i=1
53while [ \$i -le 50 ]
54do
55	/var/tmp/exitsZero.ksh &
56        PIDS="\$PIDS \$!"
57        (( i = i + 1 ))
58done
59
60sleep 1
61
62for pid in \$PIDS
63do
64	wait \$pid
65        (( \$? == 127 )) && exit 1
66done
67exit 0
68EOF
69
70ksh /var/tmp/testbackgprocs.ksh
71if [[ $? -eq 1 ]]; then
72#
73# Current ksh being executed has a limit
74# of 25 background processes.
75#
76	return 1
77else
78        return 0
79fi
80}
81
82function init_setup
83{
84
85	typeset disklist=$1
86
87        create_pool $TESTPOOL "$disklist"
88
89	if ! is_global_zone ; then
90		reexport_pool
91	fi
92
93        rm -rf $TESTDIR  || log_unresolved Could not remove $TESTDIR
94        mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR
95
96        rm -rf $TESTDIR_TGT  || log_unresolved Could not remove $TESTDIR_TGT
97        mkdir -p $TESTDIR_TGT || log_unresolved Could not create $TESTDIR_TGT
98
99        log_must zfs create $TESTPOOL/$TESTFS
100        log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
101
102        log_must zfs create $TESTPOOL/$TESTFS_TGT
103        log_must zfs set mountpoint=$TESTDIR_TGT $TESTPOOL/$TESTFS_TGT
104
105        mkdir -p $OLDDIR || log_unresolved Could not create $OLDDIR
106        mkdir -p $NEWDIR_IN_FS || log_unresolved Could not create $NEWDIR_IN_FS
107        mkdir -p $NEWDIR_ACROSS_FS || log_unresolved Could not create $NEWDIR_ACROSS_FS
108
109}
110
111function wait_pid
112{
113	for pid in $1
114	do
115		ps -e | grep $pid >/dev/null 2>&1
116		(( $? == 0 )) && wait $pid
117        done
118}
119
120
121#
122# Generate given number files in a
123# directory of zfs file system
124# $1 - the directory holds the generated files
125# $2 - number of to-be-generated files
126#
127
128function generate_files
129{
130	typeset -i count
131        typeset -i proc_num=0
132
133	if (( $2 == $MVNUMFILES )); then
134		count=1
135	else
136		count=$MVNUMFILES+1
137	fi
138
139        while (( count <= $2 ))
140        do
141                cp /etc/passwd $1/file_$count \
142                         > /dev/null 2>&1 &
143
144                PIDS="$PIDS $!"
145
146                proc_num=`echo $PIDS | wc -w`
147                if (( proc_num >= GANGPIDS )); then
148                        wait_pid "$PIDS"
149                        proc_num=0
150                        PIDS=""
151                fi
152
153               (( count = count + 1 ))
154        done
155
156}
157
158#
159# Move given number files from one directory to
160# another directory in parallel
161# $1 - source directory
162# $2 - target directory
163#
164function mv_files
165{
166
167        find $1 -type f -print | xargs -i \
168                mv {} $2 > /dev/null 2>&1
169}
170
171#
172# Count the files number after moving, and
173# compare it with the original number
174# $1 - directory that to be operated
175# $2 - original files number
176#
177function count_files
178{
179        typeset -i file_num
180        file_num=`find $1  -type f -print | \
181                wc -l`
182        (( file_num != $2 )) && \
183                log_fail "The file number of target directory"\
184                        "$2 is not equal to that of the source "\
185                        "directory $1"
186
187}
188
189#
190# Running the 'mv' test
191# $1 - old directory
192# $2 - new directory
193#
194function mv_test
195{
196        typeset old=$1
197        typeset new=$2
198
199        typeset -i inc_num=$(( MVNUMFILES + MVNUMINCR ))
200        typeset -i num=0
201
202        for num in $MVNUMFILES $inc_num
203        do
204                generate_files $old $num
205
206                mv_files $old $new
207                count_files $new $num
208
209                mv_files $new $old
210                count_files $old $num
211        done
212
213	typeset dir=$(get_device_dir $DISKS)
214        verify_filesys "$TESTPOOL" "$TESTPOOL/$TESTFS" "$dir"
215
216        return 0
217}
218