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 https://opensource.org/licenses/CDDL-1.0.
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
33function init_setup
34{
35
36	typeset disklist=$1
37
38        create_pool $TESTPOOL "$disklist"
39
40	if ! is_global_zone ; then
41		reexport_pool
42	fi
43
44        rm -rf $TESTDIR  || log_unresolved Could not remove $TESTDIR
45        mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR
46
47        rm -rf $TESTDIR_TGT  || log_unresolved Could not remove $TESTDIR_TGT
48        mkdir -p $TESTDIR_TGT || log_unresolved Could not create $TESTDIR_TGT
49
50        log_must zfs create $TESTPOOL/$TESTFS
51        log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
52
53        log_must zfs create $TESTPOOL/$TESTFS_TGT
54        log_must zfs set mountpoint=$TESTDIR_TGT $TESTPOOL/$TESTFS_TGT
55
56        mkdir -p $OLDDIR || log_unresolved Could not create $OLDDIR
57        mkdir -p $NEWDIR_IN_FS || log_unresolved Could not create $NEWDIR_IN_FS
58        mkdir -p $NEWDIR_ACROSS_FS || log_unresolved Could not create $NEWDIR_ACROSS_FS
59
60}
61
62#
63# Generate given number files in a
64# directory of zfs file system
65# $1 - the directory holds the generated files
66# $2 - number of to-be-generated files
67#
68
69function generate_files
70{
71	typeset -i count
72        typeset -i proc_num=0
73
74	if (( $2 == $MVNUMFILES )); then
75		count=1
76	else
77		count=$MVNUMFILES+1
78	fi
79
80        while (( count <= $2 ))
81        do
82                cp /etc/passwd $1/file_$count \
83                         > /dev/null 2>&1 &
84
85                (( proc_num = proc_num + 1 ))
86
87                if (( proc_num >= GANGPIDS )); then
88                        wait
89                        proc_num=0
90                fi
91
92               (( count = count + 1 ))
93        done
94
95        wait
96}
97
98#
99# Move given number files from one directory to
100# another directory in parallel
101# $1 - source directory
102# $2 - target directory
103#
104function mv_files
105{
106        find $1 -type f -exec mv {} $2 \; > /dev/null 2>&1
107}
108
109#
110# Count the files number after moving, and
111# compare it with the original number
112# $1 - directory that to be operated
113# $2 - original files number
114#
115function count_files
116{
117        (( $(find $1 -type f -print | wc -l) != $2 )) && \
118                log_fail "The file number of target directory"\
119                        "$2 is not equal to that of the source "\
120                        "directory $1"
121
122}
123
124#
125# Running the 'mv' test
126# $1 - old directory
127# $2 - new directory
128#
129function mv_test
130{
131        typeset old=$1
132        typeset new=$2
133
134        typeset -i inc_num=$(( MVNUMFILES + MVNUMINCR ))
135        typeset -i num=0
136
137        for num in $MVNUMFILES $inc_num
138        do
139                generate_files $old $num
140
141                mv_files $old $new
142                count_files $new $num
143
144                mv_files $new $old
145                count_files $old $num
146        done
147
148	typeset dir=$(get_device_dir $DISKS)
149        verify_filesys "$TESTPOOL" "$TESTPOOL/$TESTFS" "$dir"
150
151        return 0
152}
153