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 "#!/bin/ksh" > $TEST_BASE_DIR/exitsZero.ksh
41echo  "exit 0" >> $TEST_BASE_DIR/exitsZero.ksh
42chmod 777 $TEST_BASE_DIR/exitsZero.ksh
43
44cat <<EOF > $TEST_BASE_DIR/testbackgprocs.ksh
45#!/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	$TEST_BASE_DIR/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 $TEST_BASE_DIR/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
111#
112# Generate given number files in a
113# directory of zfs file system
114# $1 - the directory holds the generated files
115# $2 - number of to-be-generated files
116#
117
118function generate_files
119{
120	typeset -i count
121        typeset -i proc_num=0
122
123	if (( $2 == $MVNUMFILES )); then
124		count=1
125	else
126		count=$MVNUMFILES+1
127	fi
128
129        while (( count <= $2 ))
130        do
131                cp /etc/passwd $1/file_$count \
132                         > /dev/null 2>&1 &
133
134                (( proc_num = proc_num + 1 ))
135
136                if (( proc_num >= GANGPIDS )); then
137                        wait
138                        proc_num=0
139                fi
140
141               (( count = count + 1 ))
142        done
143
144        wait
145}
146
147#
148# Move given number files from one directory to
149# another directory in parallel
150# $1 - source directory
151# $2 - target directory
152#
153function mv_files
154{
155        find $1 -type f -print | xargs -I "{}" \
156                mv {} $2 > /dev/null 2>&1
157}
158
159#
160# Count the files number after moving, and
161# compare it with the original number
162# $1 - directory that to be operated
163# $2 - original files number
164#
165function count_files
166{
167        typeset -i file_num
168        file_num=`find $1  -type f -print | \
169                wc -l`
170        (( file_num != $2 )) && \
171                log_fail "The file number of target directory"\
172                        "$2 is not equal to that of the source "\
173                        "directory $1"
174
175}
176
177#
178# Running the 'mv' test
179# $1 - old directory
180# $2 - new directory
181#
182function mv_test
183{
184        typeset old=$1
185        typeset new=$2
186
187        typeset -i inc_num=$(( MVNUMFILES + MVNUMINCR ))
188        typeset -i num=0
189
190        for num in $MVNUMFILES $inc_num
191        do
192                generate_files $old $num
193
194                mv_files $old $new
195                count_files $new $num
196
197                mv_files $new $old
198                count_files $old $num
199        done
200
201	typeset dir=$(get_device_dir $DISKS)
202        verify_filesys "$TESTPOOL" "$TESTPOOL/$TESTFS" "$dir"
203
204        return 0
205}
206