Whamcloud - gitweb
LU-11206 tests: Use import_ready to check IDLE
[fs/lustre-release.git] / lustre / tests / sanity-flr.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 set -e
6 set +o posix
7
8 SRCDIR=$(dirname $0)
9 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
10
11 ONLY=${ONLY:-"$*"}
12 # Bug number for skipped test:    LU-11381
13 ALWAYS_EXCEPT="$SANITY_FLR_EXCEPT 201"
14 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
15
16 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
17 . $LUSTRE/tests/test-framework.sh
18 init_test_env $@
19 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
20 get_lustre_env
21 init_logging
22
23 [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
24         { skip "Need MDS version at least 2.10.56"; exit 0; }
25
26 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
27         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
28 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
29
30 check_and_setup_lustre
31 DIR=${DIR:-$MOUNT}
32 assert_DIR
33
34 build_test_filter
35
36 assert_DIR
37 rm -rf $DIR/[Rdfs][0-9]*
38
39 # global array to store mirror IDs
40 declare -a mirror_array
41 get_mirror_ids() {
42         local tf=$1
43         local id
44         local array
45
46         array=()
47         for id in $($LFS getstripe $tf | awk '/lcme_id/{print $2}'); do
48                 array[${#array[@]}]=$((id >> 16))
49         done
50
51         mirror_array=($(printf "%s\n" "${array[@]}" | sort -u))
52
53         echo ${#mirror_array[@]}
54 }
55
56 drop_client_cache() {
57         echo 3 > /proc/sys/vm/drop_caches
58 }
59
60 stop_osts() {
61         local idx
62
63         for idx in "$@"; do
64                 stop ost$idx
65         done
66
67         for idx in "$@"; do
68                 wait_osc_import_state client ost$idx "\(DISCONN\|IDLE\)"
69         done
70 }
71
72 start_osts() {
73         local idx
74
75         for idx in "$@"; do
76                 start ost$idx $(ostdevname $idx) $OST_MOUNT_OPTS ||
77                         error "start ost$idx failed"
78         done
79
80         for idx in "$@"; do
81                 wait_recovery_complete ost$idx
82         done
83 }
84
85 #
86 # Verify mirror count with an expected value for a given file.
87 #
88 verify_mirror_count() {
89         local tf=$1
90         local expected=$2
91         local mirror_count=$($LFS getstripe -N $tf)
92
93         [[ $mirror_count = $expected ]] || {
94                 $LFS getstripe -v $tf
95                 error "verify mirror count failed on $tf:" \
96                       "$mirror_count != $expected"
97         }
98 }
99
100 #
101 # Verify component count with an expected value for a given file.
102 #       $1 coposited layout file
103 #       $2 expected component number
104 #
105 verify_comp_count() {
106         local tf=$1
107         local expected=$2
108         local comp_count=$($LFS getstripe --component-count $tf)
109
110         [[ $comp_count = $expected ]] || {
111                 $LFS getstripe -v $tf
112                 error "verify component count failed on $tf:" \
113                       "$comp_count != $expected"
114         }
115 }
116
117 #
118 # Verify component attribute with an expected value for a given file
119 # and component ID.
120 #
121 verify_comp_attr() {
122         local attr=$1
123         local tf=$2
124         local comp_id=$3
125         local expected=$4
126         local cmd="$LFS getstripe -I$comp_id"
127         local getstripe_cmd="$cmd -v"
128         local value
129
130         case $attr in
131                 stripe-size) cmd+=" -S $tf" ;;
132                 stripe-count) cmd+=" -c $tf" ;;
133                 stripe-index) cmd+=" -i $tf" ;;
134                 pool) cmd+=" -p $tf" ;;
135                 comp-start) cmd+=" --component-start $tf" ;;
136                 comp-end) cmd+=" --component-end $tf" ;;
137                 lcme_flags) cmd+=" $tf | awk '/lcme_flags:/ { print \$2 }'" ;;
138                 *) error "invalid attribute $attr";;
139         esac
140
141         value=$(eval $cmd)
142
143         [ $attr = lcme_flags ] && {
144                 local fl
145                 local expected_list=$(comma_list $expected)
146                 for fl in ${expected_list//,/ }; do
147                         local neg=0
148
149                         [[ ${fl:0:1} = "^" ]] && neg=1
150                         [[ $neg = 1 ]] && fl=${fl:1}
151
152                         $(echo $value | grep -q $fl)
153                         local match=$?
154                         # 0: matched; 1: not matched
155
156                         if  [[ $neg = 0 && $match != 0 ||
157                                $neg = 1 && $match = 0 ]]; then
158                                 $getstripe_cmd $tf
159                                 [[ $neg = 0 ]] && # expect the flag
160                                     error "expected flag '$fl' not in $comp_id"
161                                 [[ $neg = 1 ]] && # not expect the flag
162                                     error "not expected flag '$fl' in $comp_id"
163                         fi
164                 done
165                 return
166         }
167
168         [[ $value = $expected ]] || {
169                 $getstripe_cmd $tf
170                 error "verify $attr failed on $tf: $value != $expected"
171         }
172 }
173
174 #
175 # Verify component extent with expected start and end extent values
176 # for a given file and component ID.
177 #
178 verify_comp_extent() {
179         local tf=$1
180         local comp_id=$2
181         local expected_start=$3
182         local expected_end=$4
183
184         verify_comp_attr comp-start $tf $comp_id $expected_start
185         verify_comp_attr comp-end $tf $comp_id $expected_end
186 }
187
188 #
189 # Verify component attribute with parent directory for a given file
190 # and component ID.
191 #
192 verify_comp_attr_with_parent() {
193         local attr=$1
194         local tf=$2
195         local comp_id=$3
196         local td=$(cd $(dirname $tf); echo $PWD)
197         local tf_cmd="$LFS getstripe -I$comp_id"
198         local td_cmd="$LFS getstripe"
199         local opt
200         local expected
201         local value
202
203         case $attr in
204                 stripe-size) opt="-S" ;;
205                 stripe-count) opt="-c" ;;
206                 pool) opt="-p" ;;
207                 *) error "invalid attribute $attr";;
208         esac
209
210         expected=$($td_cmd $opt $td)
211         [[ $expected = -1 ]] && expected=$OSTCOUNT
212
213         value=$($tf_cmd $opt $tf)
214         [[ $value = -1 ]] && value=$OSTCOUNT
215
216         [[ $value = $expected ]] || {
217                 $td_cmd -d $td
218                 $tf_cmd -v $tf
219                 error "verify $attr failed with parent on $tf:" \
220                       "$value != $expected"
221         }
222 }
223
224 #
225 # Verify component attribute with filesystem-wide default value for a given file
226 # and component ID.
227 #
228 verify_comp_attr_with_default() {
229         local attr=$1
230         local tf=$2
231         local comp_id=$3
232         local tf_cmd="$LFS getstripe -I$comp_id"
233         local opt
234         local expected
235         local value
236
237         case $attr in
238                 stripe-size)
239                         opt="-S"
240                         expected=$($LCTL get_param -n \
241                                    lov.$FSNAME-clilov-*.stripesize)
242                         ;;
243                 stripe-count)
244                         opt="-c"
245                         expected=$($LCTL get_param -n \
246                                    lov.$FSNAME-clilov-*.stripecount)
247                         [[ $expected = -1 ]] && expected=$OSTCOUNT
248                         ;;
249                 *) error "invalid attribute $attr";;
250         esac
251
252         value=$($tf_cmd $opt $tf)
253         [[ $value = -1 ]] && value=$OSTCOUNT
254
255         [[ $value = $expected ]] || {
256                 $tf_cmd -v $tf
257                 error "verify $attr failed with default value on $tf:" \
258                       "$value != $expected"
259         }
260 }
261
262 #
263 # Verify unspecified component attributes for a given file
264 # and component ID.
265 #
266 # This will only verify the inherited attributes:
267 # stripe size, stripe count and OST pool name
268 #
269 verify_comp_attrs() {
270         local tf=$1
271         local comp_id=$2
272
273         verify_comp_attr_with_default stripe-size $tf $comp_id
274         verify_comp_attr_with_default stripe-count $tf $comp_id
275         verify_comp_attr_with_parent pool $tf $comp_id
276 }
277
278 # command line test cases
279 test_0a() {
280         local td=$DIR/$tdir
281         local tf=$td/$tfile
282         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
283         local mirror_cmd="$LFS mirror create"
284         local id
285         local ids
286         local i
287
288         # create parent directory
289         mkdir $td || error "mkdir $td failed"
290
291         $mirror_cmd $tf &> /dev/null && error "miss -N option"
292
293         $mirror_cmd -N $tf || error "create mirrored file $tf failed"
294         verify_mirror_count $tf 1
295         id=$($LFS getstripe -I $tf)
296         verify_comp_attrs $tf $id
297         verify_comp_extent $tf $id 0 EOF
298
299         $mirror_cmd -N0 $tf-1 &> /dev/null && error "invalid mirror count 0"
300         $mirror_cmd -N$((mirror_count + 1)) $tf-1 &> /dev/null &&
301                 error "invalid mirror count $((mirror_count + 1))"
302
303         $mirror_cmd -N$mirror_count $tf-1 ||
304                 error "create mirrored file $tf-1 failed"
305         verify_mirror_count $tf-1 $mirror_count
306         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
307         for ((i = 0; i < $mirror_count; i++)); do
308                 verify_comp_attrs $tf-1 ${ids[$i]}
309                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
310         done
311
312         $mirror_cmd -N -N2 -N3 -N4 $tf-2 ||
313                 error "create mirrored file $tf-2 failed"
314         verify_mirror_count $tf-2 10
315         ids=($($LFS getstripe $tf-2 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
316         for ((i = 0; i < 10; i++)); do
317                 verify_comp_attrs $tf-2 ${ids[$i]}
318                 verify_comp_extent $tf-2 ${ids[$i]} 0 EOF
319         done
320 }
321 run_test 0a "lfs mirror create with -N option"
322
323 test_0b() {
324         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
325
326         local td=$DIR/$tdir
327         local tf=$td/$tfile
328         local mirror_cmd="$LFS mirror create"
329         local ids
330         local i
331
332         # create a new OST pool
333         local pool_name=$TESTNAME
334         create_pool $FSNAME.$pool_name ||
335                 error "create OST pool $pool_name failed"
336
337         # add OSTs into the pool
338         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
339                 error "add OSTs into pool $pool_name failed"
340
341         # create parent directory
342         mkdir $td || error "mkdir $td failed"
343         $LFS setstripe -S 8M -c -1 -p $pool_name $td ||
344                 error "$LFS setstripe $td failed"
345
346         # create a mirrored file with plain layout mirrors
347         $mirror_cmd -N -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
348                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
349                 error "create mirrored file $tf failed"
350         verify_mirror_count $tf 6
351         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
352         for ((i = 0; i < 6; i++)); do
353                 verify_comp_extent $tf ${ids[$i]} 0 EOF
354         done
355
356         # verify component ${ids[0]}
357         verify_comp_attrs $tf ${ids[0]}
358
359         # verify component ${ids[1]}
360         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
361         verify_comp_attr stripe-count $tf ${ids[1]} 2
362         verify_comp_attr stripe-index $tf ${ids[1]} 2
363         verify_comp_attr pool $tf ${ids[1]} flash
364
365         # verify component ${ids[2]}
366         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
367         verify_comp_attr stripe-count $tf ${ids[2]} 2
368         verify_comp_attr pool $tf ${ids[2]} flash
369
370         # verify component ${ids[3]}
371         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
372         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
373         verify_comp_attr pool $tf ${ids[3]} flash
374
375         # verify component ${ids[4]}
376         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
377         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
378         verify_comp_attr pool $tf ${ids[4]} archive
379
380         # verify component ${ids[5]}
381         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
382         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
383         verify_comp_attr_with_parent pool $tf ${ids[5]}
384
385         # destroy OST pool
386         destroy_test_pools
387 }
388 run_test 0b "lfs mirror create plain layout mirrors"
389
390 test_0c() {
391         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
392
393         local td=$DIR/$tdir
394         local tf=$td/$tfile
395         local mirror_cmd="$LFS mirror create"
396         local ids
397         local i
398
399         # create a new OST pool
400         local pool_name=$TESTNAME
401         create_pool $FSNAME.$pool_name ||
402                 error "create OST pool $pool_name failed"
403
404         # add OSTs into the pool
405         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
406                 error "add OSTs into pool $pool_name failed"
407
408         # create parent directory
409         mkdir $td || error "mkdir $td failed"
410         $LFS setstripe -E 32M -S 8M -c -1 -p $pool_name -E eof -S 16M $td ||
411                 error "$LFS setstripe $td failed"
412
413         # create a mirrored file with composite layout mirrors
414         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
415                     -N -c 4 -p none \
416                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
417                 error "create mirrored file $tf failed"
418         verify_mirror_count $tf 6
419         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
420
421         # verify components ${ids[0]} and ${ids[2]}
422         for i in 0 2; do
423                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
424                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
425                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
426                 verify_comp_attr pool $tf ${ids[$i]} flash
427                 verify_comp_extent $tf ${ids[$i]} 0 4194304
428         done
429
430         # verify components ${ids[1]} and ${ids[3]}
431         for i in 1 3; do
432                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
433                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
434                 verify_comp_attr pool $tf ${ids[$i]} flash
435                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
436         done
437
438         # verify component ${ids[4]}
439         verify_comp_attr stripe-size $tf ${ids[4]} 4194304
440         verify_comp_attr stripe-count $tf ${ids[4]} 4
441         verify_comp_attr_with_parent pool $tf ${ids[4]}
442         verify_comp_extent $tf ${ids[4]} 0 EOF
443
444         # verify components ${ids[5]}, ${ids[7]} and ${ids[9]}
445         for i in 5 7 9; do
446                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
447                 verify_comp_attr stripe-count $tf ${ids[$i]} 4
448                 verify_comp_attr pool $tf ${ids[$i]} archive
449                 verify_comp_extent $tf ${ids[$i]} 0 536870912
450         done
451
452         # verify components ${ids[6]}, ${ids[8]} and ${ids[10]}
453         for i in 6 8 10; do
454                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
455                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
456                 verify_comp_attr pool $tf ${ids[$i]} archive
457                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
458         done
459
460         # destroy OST pool
461         destroy_test_pools
462 }
463 run_test 0c "lfs mirror create composite layout mirrors"
464
465 test_0d() {
466         local td=$DIR/$tdir
467         local tf=$td/$tfile
468         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
469         local mirror_cmd="$LFS mirror extend"
470         local ids
471         local i
472
473         # create parent directory
474         mkdir $td || error "mkdir $td failed"
475
476         $mirror_cmd $tf &> /dev/null && error "miss -N option"
477         $mirror_cmd -N $tf &> /dev/null && error "$tf does not exist"
478
479         # create a non-mirrored file, convert it to a mirrored file and extend
480         touch $tf || error "touch $tf failed"
481         $mirror_cmd -N $tf || error "convert and extend $tf failed"
482         verify_mirror_count $tf 2
483         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
484         for ((i = 0; i < 2; i++)); do
485                 verify_comp_attrs $tf ${ids[$i]}
486                 verify_comp_extent $tf ${ids[$i]} 0 EOF
487         done
488
489         lfsck_verify_pfid $tf || error "PFID is not set"
490
491         # create a mirrored file and extend it
492         $LFS mirror create -N $tf-1 || error "create mirrored file $tf-1 failed"
493         $LFS mirror create -N $tf-2 || error "create mirrored file $tf-2 failed"
494
495         $mirror_cmd -N -S 4M -N -f $tf-2 $tf-1 &> /dev/null &&
496                 error "setstripe options should not be specified with -f option"
497
498         $mirror_cmd -N$((mirror_count - 1)) $tf-1 ||
499                 error "extend mirrored file $tf-1 failed"
500         verify_mirror_count $tf-1 $mirror_count
501         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
502         for ((i = 0; i < $mirror_count; i++)); do
503                 verify_comp_attrs $tf-1 ${ids[$i]}
504                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
505         done
506
507         $mirror_cmd -N $tf-1 &> /dev/null &&
508                 error "exceeded maximum mirror count $mirror_count" || true
509 }
510 run_test 0d "lfs mirror extend with -N option"
511
512 test_0e() {
513         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
514
515         local td=$DIR/$tdir
516         local tf=$td/$tfile
517         local mirror_cmd="$LFS mirror extend"
518         local ids
519         local i
520
521         # create parent directory
522         mkdir $td || error "mkdir $td failed"
523
524         # create a mirrored file with plain layout mirrors
525         $LFS mirror create -N -S 32M -c 3 -p ssd -i 1 -o 1,2,3 $tf ||
526                 error "create mirrored file $tf failed"
527
528         # extend the mirrored file with plain layout mirrors
529         $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
530                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
531                 error "extend mirrored file $tf failed"
532         verify_mirror_count $tf 6
533         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
534         for ((i = 0; i < 6; i++)); do
535                 verify_comp_extent $tf ${ids[$i]} 0 EOF
536         done
537
538         # verify component ${ids[0]}
539         verify_comp_attr stripe-size $tf ${ids[0]} 33554432
540         verify_comp_attr stripe-count $tf ${ids[0]} 3
541         verify_comp_attr stripe-index $tf ${ids[0]} 1
542         verify_comp_attr pool $tf ${ids[0]} ssd
543
544         # verify component ${ids[1]}
545         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
546         verify_comp_attr stripe-count $tf ${ids[1]} 2
547         verify_comp_attr stripe-index $tf ${ids[1]} 2
548         verify_comp_attr pool $tf ${ids[1]} flash
549
550         # verify component ${ids[2]}
551         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
552         verify_comp_attr stripe-count $tf ${ids[2]} 2
553         verify_comp_attr pool $tf ${ids[2]} flash
554
555         # verify component ${ids[3]}
556         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
557         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
558         verify_comp_attr pool $tf ${ids[3]} flash
559
560         # verify component ${ids[4]}
561         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
562         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
563         verify_comp_attr pool $tf ${ids[4]} archive
564
565         # verify component ${ids[5]}
566         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
567         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
568         verify_comp_attr_with_parent pool $tf ${ids[5]}
569 }
570 run_test 0e "lfs mirror extend plain layout mirrors"
571
572 test_0f() {
573         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
574
575         local td=$DIR/$tdir
576         local tf=$td/$tfile
577         local mirror_cmd="$LFS mirror extend"
578         local ids
579         local i
580
581         # create parent directory
582         mkdir $td || error "mkdir $td failed"
583
584         # create a mirrored file with composite layout mirror
585         $LFS mirror create -N -E 32M -S 16M -p ssd -E eof -S 32M $tf ||
586                 error "create mirrored file $tf failed"
587
588         # extend the mirrored file with composite layout mirrors
589         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
590                     -N -c -1 -p none \
591                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
592                 error "extend mirrored file $tf failed"
593         verify_mirror_count $tf 7
594         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
595
596         # verify component ${ids[0]}
597         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
598         verify_comp_attr_with_default stripe-count $tf ${ids[0]}
599         verify_comp_attr pool $tf ${ids[0]} ssd
600         verify_comp_extent $tf ${ids[0]} 0 33554432
601
602         # verify component ${ids[1]}
603         verify_comp_attr stripe-size $tf ${ids[1]} 33554432
604         verify_comp_attr_with_default stripe-count $tf ${ids[1]}
605         verify_comp_attr pool $tf ${ids[1]} ssd
606         verify_comp_extent $tf ${ids[1]} 33554432 EOF
607
608         # verify components ${ids[2]} and ${ids[4]}
609         for i in 2 4; do
610                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
611                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
612                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
613                 verify_comp_attr pool $tf ${ids[$i]} flash
614                 verify_comp_extent $tf ${ids[$i]} 0 4194304
615         done
616
617         # verify components ${ids[3]} and ${ids[5]}
618         for i in 3 5; do
619                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
620                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
621                 verify_comp_attr pool $tf ${ids[$i]} flash
622                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
623         done
624
625         # verify component ${ids[6]}
626         verify_comp_attr stripe-size $tf ${ids[6]} 4194304
627         verify_comp_attr stripe-count $tf ${ids[6]} $OSTCOUNT
628         verify_comp_attr_with_parent pool $tf ${ids[6]}
629         verify_comp_extent $tf ${ids[6]} 0 EOF
630
631         # verify components ${ids[7]}, ${ids[9]} and ${ids[11]}
632         for i in 7 9 11; do
633                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
634                 verify_comp_attr stripe-count $tf ${ids[$i]} $OSTCOUNT
635                 verify_comp_attr pool $tf ${ids[$i]} archive
636                 verify_comp_extent $tf ${ids[$i]} 0 536870912
637         done
638
639         # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
640         for i in 8 10 12; do
641                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
642                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
643                 verify_comp_attr pool $tf ${ids[$i]} archive
644                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
645         done
646 }
647 run_test 0f "lfs mirror extend composite layout mirrors"
648
649 test_0g() {
650         local tf=$DIR/$tfile
651
652         $LFS mirror create -N -E 1M -S 1M -o0 --flags=prefer -E eof -o1 \
653                            -N -o1 $tf || error "create mirrored file $tf failed"
654
655         verify_comp_attr lcme_flags $tf 0x10001 prefer
656         verify_comp_attr lcme_flags $tf 0x10002 prefer
657
658         # write to the mirrored file and check primary
659         cp /etc/hosts $tf || error "error writing file '$tf'"
660
661         verify_comp_attr lcme_flags $tf 0x20003 stale
662
663         # resync file and check prefer flag
664         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
665
666         cancel_lru_locks osc
667         $LCTL set_param osc.*.stats=clear
668         cat $tf &> /dev/null || error "error reading file '$tf'"
669
670         # verify that the data was provided by OST1 where mirror 1 resides
671         local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-[-0-9a-f]*.stats |
672                         awk '/ost_read/{print $2}')
673         [ -n "$nr_read" ] || error "read was not provided by OST1"
674 }
675 run_test 0g "lfs mirror create flags support"
676
677 test_0h() {
678         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
679                 skip "Need MDS version at least 2.11.57"
680
681         local td=$DIR/$tdir
682         local tf=$td/$tfile
683         local ids
684         local i
685
686         # create parent directory
687         test_mkdir $td || error "mkdir $td failed"
688
689         $LFS setstripe -N -E 1M -S 1M --flags=prefer -E eof -N2 $td ||
690                 error "set default mirrored layout on directory $td failed"
691
692         # verify flags are inherited from the directory
693         touch $tf
694
695         verify_comp_attr lcme_flags $tf 0x10001 prefer
696         verify_comp_attr lcme_flags $tf 0x10002 prefer
697
698         # set flags to the first component
699         $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,stale $tf
700
701         verify_comp_attr lcme_flags $tf 0x10001 stale
702         verify_comp_attr lcme_flags $tf 0x10002 prefer
703
704         $LFS setstripe --comp-set -I0x10001 --comp-flags=^stale $tf &&
705                 error "clearing 'stale' should fail"
706
707         # write and resync file. It can't resync the file directly because the
708         # file state is still 'ro'
709         cp /etc/hosts $tf || error "error writing file '$tf'"
710         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
711
712         $LFS setstripe --comp-set -I 0x20003 --comp-flags=prefer $tf ||
713                 error "error setting flag prefer"
714
715         verify_comp_attr lcme_flags $tf 0x20003 prefer
716 }
717 run_test 0h "set, clear and test flags for FLR files"
718
719 test_1() {
720         local tf=$DIR/$tfile
721         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
722         local mirror_create_cmd="$LFS mirror create"
723         local stripes[0]=$OSTCOUNT
724
725         mirror_create_cmd+=" -N -c ${stripes[0]}"
726         for ((i = 1; i < $mirror_count; i++)); do
727                 # add mirrors with different stripes to the file
728                 stripes[$i]=$((RANDOM % OSTCOUNT))
729                 [ ${stripes[$i]} -eq 0 ] && stripes[$i]=1
730
731                 mirror_create_cmd+=" -N -c ${stripes[$i]}"
732         done
733
734         $mirror_create_cmd $tf || error "create mirrored file $tf failed"
735         verify_mirror_count $tf $mirror_count
736
737         # can't create mirrors exceeding LUSTRE_MIRROR_COUNT_MAX
738         $LFS mirror extend -N $tf &&
739                 error "Creating the $((mirror_count+1))th mirror succeeded"
740
741         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
742                         tr '\n' ' '))
743
744         # verify the range of components and stripe counts
745         for ((i = 0; i < $mirror_count; i++)); do
746                 verify_comp_attr stripe-count $tf ${ids[$i]} ${stripes[$i]}
747                 verify_comp_extent $tf ${ids[$i]} 0 EOF
748         done
749 }
750 run_test 1 "create components with setstripe options"
751
752 test_2() {
753         local tf=$DIR/$tfile
754         local tf2=$DIR/$tfile-2
755
756         $LFS setstripe -E 1M -S 1M -E EOF -c 1 $tf
757         $LFS setstripe -E 2M -S 1M -E EOF -c -1 $tf2
758
759         $LFS mirror extend -N -f $tf2 $tf ||
760                 error "merging $tf2 into $tf failed"
761
762         verify_mirror_count $tf 2
763         [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
764 }
765 run_test 2 "create components from existing files"
766
767 test_3() {
768         [[ $MDSCOUNT -lt 2 ]] && skip "need >= 2 MDTs" && return
769
770         for ((i = 0; i < 2; i++)); do
771                 $LFS mkdir -i $i $DIR/$tdir-$i
772                 $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
773         done
774
775         $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
776                 $DIR/$tdir-0/$tfile || error "creating mirrors"
777
778         # mdt doesn't support to cancel layout lock for remote objects, do
779         # it here manually.
780         cancel_lru_locks mdc
781
782         # make sure the mirrorted file was created successfully
783         [[ $($LFS getstripe --component-count $DIR/$tdir-0/$tfile) -eq 2 ]] ||
784                 { $LFS getstripe $DIR/$tdir-0/$tfile;
785                         error "expected 2 components"; }
786
787         # cleanup
788         rm -rf $DIR/$tdir-*
789 }
790 run_test 3 "create components from files located on different MDTs"
791
792 test_4() {
793         local tf=$DIR/$tdir/$tfile
794         local ids=()
795
796         test_mkdir $DIR/$tdir
797
798         # set mirror with setstripe options to directory
799         $LFS mirror create -N2 -E 1M -S 1M -E eof $DIR/$tdir ||
800                 error "set mirror to directory error"
801
802         [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
803                 x"mirrored" ] || error "failed to create mirrored dir"
804
805         touch $tf
806         verify_mirror_count $tf 2
807
808         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
809         verify_comp_extent $tf ${ids[0]} 0 1048576
810         verify_comp_extent $tf ${ids[1]} 1048576 EOF
811
812         # sub directory should inherit mirror setting from parent
813         test_mkdir $DIR/$tdir/td
814         [ x$($LFS getstripe -v $DIR/$tdir/td | awk '/lcm_flags/{print $2}') = \
815                 x"mirrored" ] || error "failed to inherit mirror from parent"
816
817         # mirror extend won't be applied to directory
818         $LFS mirror extend -N2 $DIR/$tdir &&
819                 error "expecting mirror extend failure"
820         true
821 }
822 run_test 4 "Make sure mirror attributes can be inhertied from directory"
823
824 test_5() {
825         local tf=$DIR/$tfile
826         local ids=()
827
828         $MULTIOP $tf oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T12345c ||
829                 error "failed to create file with non-empty layout"
830         $CHECKSTAT -t file -s 12345 $tf || error "size error: expecting 12345"
831
832         $LFS mirror create -N3 $tf || error "failed to attach mirror layout"
833         verify_mirror_count $tf 3
834
835         $CHECKSTAT -t file -s 12345 $tf ||
836                 error "size error after attaching layout "
837 }
838 run_test 5 "Make sure init size work for mirrored layout"
839
840 # LU=10112: disable dom+flr for phase 1
841 test_6() {
842         local tf=$DIR/$tfile
843
844         $LFS mirror create -N -E 1M -S 1M -L mdt -E eof -N -E eof $tf &&
845                 error "expect failure to create mirrored file with DoM"
846
847         $LFS mirror create -N -E 1M -S 1M -E eof -N -E 1M -L mdt -E eof $tf &&
848                 error "expect failure to create mirrored file with DoM"
849
850         $LFS setstripe -E 1M -S 1M -L mdt -E eof $tf
851         $LFS mirror extend -N2 $tf &&
852                 error "expect failure to extend mirror with DoM"
853
854         $LFS mirror create -N2 -E 1M -S 1M -E eof $tf-2
855         $LFS mirror extend -N -f $tf $tf-2 &&
856                 error "expect failure to extend mirrored file with DoM extent"
857
858         true
859 }
860 run_test 6 "DoM and FLR won't co-exist for phase 1"
861
862 test_21() {
863         local tf=$DIR/$tfile
864         local tf2=$DIR/$tfile-2
865
866         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
867
868         $LFS setstripe -E EOF -o 0 $tf
869         $LFS setstripe -E EOF -o 1 $tf2
870
871         local dd_count=$((RANDOM % 20 + 1))
872         dd if=/dev/zero of=$tf bs=1M count=$dd_count oflag=sync
873         dd if=/dev/zero of=$tf2 bs=1M count=1 seek=$((dd_count - 1)) oflag=sync
874
875         # for zfs - sync OST dataset so that du below will return
876         # accurate results
877         [ "$FSTYPE" = "zfs" ] &&
878                 do_nodes $(comma_list $(osts_nodes)) "$ZPOOL sync"
879
880         local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
881
882         # add component
883         $LFS mirror extend -N -f $tf2 $tf ||
884                 error "merging $tf2 into $tf failed"
885
886         # cancel layout lock
887         cancel_lru_locks mdc
888
889         local new_blocks=$(du -k $tf | awk '{print $1}')
890         [ $new_blocks -eq $blocks ] ||
891         error "i_blocks error expected: $blocks, actual: $new_blocks"
892 }
893 run_test 21 "glimpse should report accurate i_blocks"
894
895 get_osc_lock_count() {
896         local lock_count=0
897
898         for idx in "$@"; do
899                 local osc_name
900                 local count
901
902                 osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'[-0-9a-f]*'
903                 count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
904                 lock_count=$((lock_count + count))
905         done
906         echo $lock_count
907 }
908
909 test_22() {
910         local tf=$DIR/$tfile
911
912         $LFS setstripe -E EOF -o 0 $tf
913         dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
914
915         # add component, two mirrors located on the same OST ;-)
916         $LFS mirror extend -N -o 0 $tf ||
917                 error "extending mirrored file $tf failed"
918
919         size_blocks=$(stat --format="%b %s" $tf)
920
921         cancel_lru_locks mdc
922         cancel_lru_locks osc
923
924         local new_size_blocks=$(stat --format="%b %s" $tf)
925
926         # make sure there is no lock cached
927         [ $(get_osc_lock_count 1) -eq 0 ] || error "glimpse requests were sent"
928
929         [ "$new_size_blocks" = "$size_blocks" ] ||
930                 echo "size expected: $size_blocks, actual: $new_size_blocks"
931
932         rm -f $tmpfile
933 }
934 run_test 22 "no glimpse to OSTs for READ_ONLY files"
935
936 test_31() {
937         local tf=$DIR/$tfile
938
939         $LFS mirror create -N -o 0 -N -o 1 $tf ||
940                 error "creating mirrored file $tf failed"
941
942         #define OBD_FAIL_GLIMPSE_IMMUTABLE 0x1A00
943         $LCTL set_param fail_loc=0x1A00
944
945         local ost_idx
946         for ((ost_idx = 1; ost_idx <= 2; ost_idx++)); do
947                 cancel_lru_locks osc
948                 stop_osts $ost_idx
949
950                 local tmpfile=$(mktemp)
951                 stat --format="%b %s" $tf > $tmpfile  &
952                 local pid=$!
953
954                 local cnt=0
955                 while [ $cnt -le 5 ]; do
956                         kill -0 $pid > /dev/null 2>&1 || break
957                         sleep 1
958                         ((cnt += 1))
959                 done
960                 kill -0 $pid > /dev/null 2>&1 &&
961                         error "stat process stuck due to unavailable OSTs"
962
963                 # make sure glimpse request has been sent
964                 [ $(get_osc_lock_count 1 2) -ne 0 ] ||
965                         error "OST $ost_idx: no glimpse request was sent"
966
967                 start_osts $ost_idx
968         done
969 }
970 run_test 31 "make sure glimpse request can be retried"
971
972 test_32() {
973         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
974         rm -f $DIR/$tfile $DIR/$tfile-2
975
976         $LFS setstripe -E EOF -o 0 $DIR/$tfile
977         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=$((RANDOM % 10 + 2))
978
979         local fsize=$(stat -c %s $DIR/$tfile)
980         [[ $fsize -ne 0 ]] || error "file size is (wrongly) zero"
981
982         local cksum=$(md5sum $DIR/$tfile)
983
984         # create a new mirror in sync mode
985         $LFS mirror extend -N -o 1 $DIR/$tfile ||
986                 error "extending mirrored file $DIR/$tfile failed"
987
988         # make sure the mirrored file was created successfully
989         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
990                 { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
991
992         drop_client_cache
993         stop_osts 1
994
995         # check size is correct, glimpse request should go to the 2nd mirror
996         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
997                 error "file size error $fsize vs. $(stat -c %s $DIR/$tfile)"
998
999         echo "reading file from the 2nd mirror and verify checksum"
1000         [[ "$cksum" == "$(md5sum $DIR/$tfile)" ]] ||
1001                 error "checksum error: expected $cksum"
1002
1003         start_osts 1
1004 }
1005 run_test 32 "data should be mirrored to newly created mirror"
1006
1007 test_33() {
1008         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1009
1010         rm -f $DIR/$tfile $DIR/$tfile-2
1011
1012         # create a file with two mirrors
1013         $LFS setstripe -E EOF -o 0 $DIR/$tfile
1014         local max_count=100
1015         local count=0
1016         while [ $count -lt $max_count ]; do
1017                 echo "ost1" >> $DIR/$tfile
1018                 count=$((count + 1));
1019         done
1020
1021         # tmp file that will be used as mirror
1022         $LFS setstripe -E EOF -o 1 $DIR/$tfile-2
1023         count=0
1024         while [ $count -lt $max_count ]; do
1025                 echo "ost2" >> $DIR/$tfile-2
1026                 count=$((count + 1));
1027         done
1028
1029         # create a mirrored file
1030         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
1031                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1032                       "with verification should fail"
1033         $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
1034                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1035                       "without verification failed"
1036
1037         # make sure that $tfile has two mirrors and $tfile-2 does not exist
1038         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1039                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1040
1041         [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
1042
1043         # execpted file size
1044         local fsize=$((5 * max_count))
1045         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1046                 error "mirrored file size is not $fsize"
1047
1048         # read file - all OSTs are available
1049         echo "reading file (data can be provided by any ost)... "
1050         local rs=$(cat $DIR/$tfile | head -1)
1051         [[ "$rs" == "ost1" || "$rs" == "ost2" ]] ||
1052                 error "file content error: expected: \"ost1\" or \"ost2\""
1053
1054         # read file again with ost1 failed
1055         stop_osts 1
1056         drop_client_cache
1057
1058         echo "reading file (data should be provided by ost2)..."
1059         local rs=$(cat $DIR/$tfile | head -1)
1060         [[ "$rs" == "ost2" ]] ||
1061                 error "file content error: expected: \"ost2\", actual: \"$rs\""
1062
1063         # remount ost1
1064         start_osts 1
1065
1066         # read file again with ost2 failed
1067         $LCTL set_param ldlm.namespaces.lustre-*-osc-[-0-9a-f]*.lru_size=clear
1068
1069         fail ost2 &
1070         sleep 1
1071
1072         # check size, glimpse should work
1073         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1074                 error "mirrored file size is not $fsize"
1075
1076         echo "reading file (data should be provided by ost1)..."
1077         local rs=$(cat $DIR/$tfile | head -1)
1078         [[ "$rs" == "ost1" ]] ||
1079                 error "file content error: expected: \"ost1\", actual: \"$rs\""
1080
1081         wait_osc_import_ready client ost2
1082 }
1083 run_test 33 "read can choose available mirror to read"
1084
1085 test_34a() {
1086         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1087
1088         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1089
1090         # reference file
1091         $LFS setstripe -o 0 $DIR/$tfile-ref
1092         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1093
1094         # create a file with two mirrors
1095         $LFS setstripe -E -1 -o 0,1 -S 1M $DIR/$tfile
1096         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1097
1098         $LFS setstripe -E -1 -o 2,3 -S 1M $DIR/$tfile-2
1099         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1100
1101         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1102                 error "mirrored file size is not 3M"
1103
1104         # merge a mirrored file
1105         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1106                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1107
1108         cancel_lru_locks osc
1109
1110         # stop two OSTs, so the 2nd stripe of the 1st mirror and
1111         # the 1st stripe of the 2nd mirror will be inaccessible, ...
1112         stop_osts 2 3
1113
1114         echo "comparing files ... "
1115
1116         # however, read can still return the correct data. It should return
1117         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1118         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1119                 $DIR/$tfile-ref || error "file reading error"
1120
1121         start_osts 2 3
1122 }
1123 run_test 34a "read mirrored file with multiple stripes"
1124
1125 test_34b() {
1126         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1127
1128         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1129
1130         # reference file
1131         $LFS setstripe -o 0 $DIR/$tfile-ref
1132         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1133
1134         $LFS setstripe -E 1M -S 1M -o 0 -E eof -o 1 $DIR/$tfile
1135         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1136
1137         $LFS setstripe -E 1M -S 1M -o 2 -E eof -o 3 $DIR/$tfile-2
1138         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1139
1140         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1141                 error "mirrored file size is not 3M"
1142
1143         # merge a mirrored file
1144         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1145                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1146
1147         cancel_lru_locks osc
1148
1149         # stop two OSTs, so the 2nd component of the 1st mirror and
1150         # the 1st component of the 2nd mirror will be inaccessible, ...
1151         stop_osts 2 3
1152
1153         echo "comparing files ... "
1154
1155         # however, read can still return the correct data. It should return
1156         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1157         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1158                 $DIR/$tfile-ref || error "file reading error"
1159
1160         start_osts 2 3
1161 }
1162 run_test 34b "read mirrored file with multiple components"
1163
1164 test_35() {
1165         local tf=$DIR/$tfile
1166
1167         $LFS setstripe -E eof $tf
1168
1169         # add an out-of-sync mirror to the file
1170         $LFS mirror extend -N -c 2 $tf ||
1171                 error "extending mirrored file $tf failed"
1172
1173         $MULTIOP $tf oO_WRONLY:c ||
1174                 error "write open a mirrored file failed"
1175
1176         # truncate file should return error
1177         $TRUNCATE $tf 100 || error "error truncating a mirrored file"
1178 }
1179 run_test 35 "allow to write to mirrored files"
1180
1181 verify_ost_layout_version() {
1182         local tf=$1
1183
1184         # get file layout version
1185         local flv=$($LFS getstripe $tf | awk '/lcm_layout_gen/{print $2}')
1186
1187         # layout version from OST objects
1188         local olv=$($MULTIOP $tf oXc | awk '/ostlayoutversion/{print $2}')
1189
1190         [ $flv -eq $olv ] || error "layout version mismatch: $flv vs. $olv"
1191 }
1192
1193 create_file_36() {
1194         local tf
1195
1196         for tf in "$@"; do
1197                 $LFS setstripe -E 1M -S 1M -E 2M -E 4M -E eof -c -1 $tf
1198                 $LFS setstripe -E 3M -S 1M -E 6M -E eof -c -1 $tf-tmp
1199
1200                 $LFS mirror extend -N -f $tf-tmp $tf ||
1201                         error "merging $tf-tmp into $tf failed"
1202         done
1203 }
1204
1205 test_36() {
1206         local tf=$DIR/$tfile
1207
1208         create_file_36 $tf $tf-2 $tf-3
1209
1210         [ $($LFS getstripe -N $tf) -gt 1 ] || error "wrong mirror count"
1211
1212         # test case 1 - check file write and verify layout version
1213         $MULTIOP $tf oO_WRONLY:c ||
1214                 error "write open a mirrored file failed"
1215
1216         # write open file should not return error
1217         $MULTIOP $tf oO_WRONLY:w1024Yc || error "write mirrored file error"
1218
1219         # instantiate components should work
1220         dd if=/dev/zero of=$tf bs=1M count=12 || error "write file error"
1221
1222         # verify OST layout version
1223         verify_ost_layout_version $tf
1224
1225         # test case 2
1226         local mds_facet=mds$(($($LFS getstripe -m $tf-2) + 1))
1227
1228         local delay_sec=10
1229         do_facet $mds_facet $LCTL set_param fail_val=$delay_sec
1230
1231         #define OBD_FAIL_FLR_LV_DELAY 0x1A01
1232         do_facet $mds_facet $LCTL set_param fail_loc=0x1A01
1233
1234         # write should take at least $fail_loc seconds and succeed
1235         local st=$(date +%s)
1236         $MULTIOP $tf-2 oO_WRONLY:w1024Yc || error "write mirrored file error"
1237
1238         [ $(date +%s) -ge $((st+delay_sec)) ] ||
1239                 error "write finished before layout version is transmitted"
1240
1241         # verify OST layout version
1242         verify_ost_layout_version $tf
1243
1244         do_facet $mds_facet $LCTL set_param fail_loc=0
1245
1246         # test case 3
1247         mds_idx=mds$(($($LFS getstripe -m $tf-3) + 1))
1248
1249         #define OBD_FAIL_FLR_LV_INC 0x1A02
1250         do_facet $mds_facet $LCTL set_param fail_loc=0x1A02
1251
1252         # write open file should return error
1253         $MULTIOP $tf-3 oO_WRONLY:O_SYNC:w1024c &&
1254                 error "write a mirrored file succeeded" || true
1255
1256         do_facet $mds_facet $LCTL set_param fail_loc=0
1257 }
1258 run_test 36 "write to mirrored files"
1259
1260 create_files_37() {
1261         local tf
1262         local fsize=$1
1263
1264         echo "create test files with size $fsize .."
1265
1266         shift
1267         for tf in "$@"; do
1268                 $LFS setstripe -E 1M -S 1M -c 1 -E eof -c -1 $tf
1269
1270                 dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
1271                 $TRUNCATE $tf $fsize
1272         done
1273 }
1274
1275 test_37()
1276 {
1277         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
1278                 skip "Need MDS version at least 2.11.57"
1279
1280         local tf=$DIR/$tfile
1281         local tf2=$DIR/$tfile-2
1282         local tf3=$DIR/$tfile-3
1283         local tf4=$DIR/$tfile-4
1284
1285         create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
1286         rm -f $tf4
1287         cp $tf $tf4
1288
1289         # assume the mirror id will be 1, 2, and 3
1290         declare -A checksums
1291         checksums[1]=$(cat $tf | md5sum)
1292         checksums[2]=$(cat $tf2 | md5sum)
1293         checksums[3]=$(cat $tf3 | md5sum)
1294
1295         printf '%s\n' "${checksums[@]}"
1296
1297         # merge these files into a mirrored file
1298         $LFS mirror extend --no-verify -N -f $tf2 $tf ||
1299                 error "merging $tf2 into $tf failed"
1300         $LFS mirror extend --no-verify -N -f $tf3 $tf ||
1301                 error "merging $tf3 into $tf failed"
1302
1303         get_mirror_ids $tf
1304
1305         # verify mirror read, checksums should equal to the original files'
1306         echo "Verifying mirror read .."
1307
1308         local sum
1309         for i in ${mirror_array[@]}; do
1310                 $LCTL set_param ldlm.namespaces.*.lru_size=clear > /dev/null
1311                 sum=$($LFS mirror read -N $i $tf | md5sum)
1312                 [ "$sum" = "${checksums[$i]}" ] ||
1313                         error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
1314         done
1315
1316         # verify mirror write
1317         echo "Verifying mirror write .."
1318         $LFS mirror write -N2 $tf < $tf4
1319
1320         sum=$($LFS mirror read -N2 $tf | md5sum)
1321         [[ "$sum" = "${checksums[1]}" ]] ||
1322                 error "2: mismatch \'${checksums[1]}\' vs. \'$sum\'"
1323
1324         # verify mirror copy, write to this mirrored file will invalidate
1325         # the other two mirrors
1326         echo "Verifying mirror copy .."
1327
1328         local osts=$(comma_list $(osts_nodes))
1329
1330         # define OBD_FAIL_OST_SKIP_LV_CHECK     0x241
1331         do_nodes $osts lctl set_param fail_loc=0x241
1332
1333         mirror_io copy -i ${mirror_array[0]} \
1334                 -t $(echo ${mirror_array[@]:1} | tr ' ' ',') $tf ||
1335                         error "mirror copy error"
1336
1337         do_nodes $osts lctl set_param fail_loc=0
1338
1339         # verify copying is successful by checking checksums
1340         remount_client $MOUNT
1341         for i in ${mirror_array[@]}; do
1342                 sum=$($LFS mirror read -N $i $tf | md5sum)
1343                 [ "$sum" = "${checksums[1]}" ] ||
1344                         error "$i: mismatch checksum after copy"
1345         done
1346
1347         rm -f $tf
1348 }
1349 run_test 37 "mirror I/O API verification"
1350
1351 verify_flr_state()
1352 {
1353         local tf=$1
1354         local expected_state=$2
1355
1356         local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
1357         [ $expected_state = $state ] ||
1358                 error "expected: $expected_state, actual $state"
1359 }
1360
1361 test_38() {
1362         local tf=$DIR/$tfile
1363         local ref=$DIR/${tfile}-ref
1364
1365         $LFS setstripe -E 1M -S 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf ||
1366                 error "creating $tf failed"
1367         $LFS setstripe -E 2M -S 1M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 \
1368                 $tf-2 || error "creating $tf-2 failed"
1369         $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3 ||
1370                 error "creating $tf-3 failed"
1371
1372         # instantiate all components
1373         $LFS mirror extend -N -f $tf-2 $tf ||
1374                 error "merging $tf-2 into $tf failed"
1375         $LFS mirror extend -N -f $tf-3 $tf ||
1376                 error "merging $tf-3 into $tf failed"
1377         $LFS mirror extend -N -c 1 $tf ||
1378                 error "extending mirrored file $tf failed"
1379
1380         verify_flr_state $tf "ro"
1381
1382         dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
1383
1384         local fsize=$((RANDOM << 8 + 1048576))
1385         $TRUNCATE $ref $fsize
1386
1387         local ref_cksum=$(cat $ref | md5sum)
1388
1389         # case 1: verify write to mirrored file & resync work
1390         cp $ref $tf || error "copy from $ref to $f error"
1391         verify_flr_state $tf "wp"
1392
1393         local file_cksum=$(cat $tf | md5sum)
1394         [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
1395
1396         get_mirror_ids $tf
1397         echo "mirror IDs: ${mirror_array[@]}"
1398
1399         local valid_mirror stale_mirror id mirror_cksum
1400         for id in "${mirror_array[@]}"; do
1401                 mirror_cksum=$($LFS mirror read -N $id $tf | md5sum)
1402                 [ "$ref_cksum" == "$mirror_cksum" ] &&
1403                         { valid_mirror=$id; continue; }
1404
1405                 stale_mirror=$id
1406         done
1407
1408         [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
1409         [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
1410
1411         mirror_io resync $tf || error "resync failed"
1412         verify_flr_state $tf "ro"
1413
1414         mirror_cksum=$($LFS mirror read -N $stale_mirror $tf | md5sum)
1415         [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
1416
1417         # case 2: inject an error to make mirror_io exit after changing
1418         # the file state to sync_pending so that we can start a concurrent
1419         # write.
1420         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1421         verify_flr_state $tf "wp"
1422
1423         mirror_io resync -e resync_start $tf && error "resync succeeded"
1424         verify_flr_state $tf "sp"
1425
1426         # from sync_pending to write_pending
1427         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1428         verify_flr_state $tf "wp"
1429
1430         mirror_io resync -e resync_start $tf && error "resync succeeded"
1431         verify_flr_state $tf "sp"
1432
1433         # from sync_pending to read_only
1434         mirror_io resync $tf || error "resync failed"
1435         verify_flr_state $tf "ro"
1436 }
1437 run_test 38 "resync"
1438
1439 test_39() {
1440         local tf=$DIR/$tfile
1441
1442         rm -f $tf
1443         $LFS mirror create -N2 -E1m -c1 -S1M -E-1 $tf ||
1444         error "create PFL file $tf failed"
1445
1446         verify_mirror_count $tf 2
1447         verify_comp_count $tf 4
1448
1449         rm -f $tf || error "delete $tf failed"
1450 }
1451 run_test 39 "check FLR+PFL (a.k.a. PFLR) creation"
1452
1453 test_40() {
1454         local tf=$DIR/$tfile
1455         local ops
1456
1457         for ops in "conv=notrunc" ""; do
1458                 rm -f $tf
1459
1460                 $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 --flags=prefer \
1461                                    -N -E 1M -E 2M -E 4M -E -1 $tf ||
1462                         error "create PFLR file $tf failed"
1463                 dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
1464                         error "write PFLR file $tf failed"
1465
1466                 lfs getstripe -vy $tf
1467
1468                 local flags
1469
1470                 # file mirror state should be write_pending
1471                 flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1472                 [ $flags = wp ] ||
1473                 error "file mirror state $flags"
1474                 # the 1st component (in mirror 1) should be inited
1475                 verify_comp_attr lcme_flags $tf 0x10001 init
1476                 # the 2nd component (in mirror 1) should be inited
1477                 verify_comp_attr lcme_flags $tf 0x10002 init
1478                 # the 3rd component (in mirror 1) should be uninited
1479                 verify_comp_attr lcme_flags $tf 0x10003 prefer
1480                 # the 4th component (in mirror 2) should be inited
1481                 verify_comp_attr lcme_flags $tf 0x20004 init
1482                 # the 5th component (in mirror 2) should be uninited
1483                 verify_comp_attr lcme_flags $tf 0x20005 0
1484                 # the 6th component (in mirror 2) should be stale
1485                 verify_comp_attr lcme_flags $tf 0x20006 stale
1486                 # the 7th component (in mirror 2) should be uninited
1487                 if [[ x$ops = "xconv=notrunc" ]]; then
1488                         verify_comp_attr lcme_flags $tf 0x20007 0
1489                 elif [[ x$ops = "x" ]]; then
1490                         verify_comp_attr lcme_flags $tf 0x20007 stale
1491                 fi
1492         done
1493
1494         rm -f $tf || error "delete $tf failed"
1495 }
1496 run_test 40 "PFLR rdonly state instantiation check"
1497
1498 test_41() {
1499         local tf=$DIR/$tfile
1500
1501         rm -f $tf $tf-1
1502         echo " **create two FLR files $tf $tf-1"
1503         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1504                            -N -E 1M -E 2M -E 3M -E -1 $tf ||
1505                 error "create PFLR file $tf failed"
1506         $LFS mirror create -N -E 2M -S 1M -E eof \
1507                            -N -E 1M -E eof --flags prefer \
1508                            -N -E 4m -E eof $tf-1 ||
1509                 error "create PFLR file $tf-1 failed"
1510
1511         # file should be in ro status
1512         echo " **verify files be RDONLY"
1513         verify_flr_state $tf "ro"
1514         verify_flr_state $tf-1 "ro"
1515
1516         # write data in [0, 2M)
1517         dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
1518                 error "writing $tf failed"
1519         dd if=/dev/urandom of=$tf-1 bs=1M count=4 conv=notrunc ||
1520                 error "writing $tf-1 failed"
1521
1522         local sum0=$(cat $tf-1 | md5sum)
1523
1524         echo " **verify files be WRITE_PENDING"
1525         verify_flr_state $tf "wp"
1526         verify_flr_state $tf-1 "wp"
1527
1528         # file should have stale component
1529         echo " **verify files have stale component"
1530         $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
1531                 error "after writing $tf, it does not contain stale component"
1532         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1533                 error "after writing $tf-1, it does not contain stale component"
1534
1535         echo " **full resync"
1536         $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
1537
1538         echo " **verify $tf-1 data consistency in all mirrors"
1539         for i in 1 2 3; do
1540                 local sum=$($LFS mirror read -N$i $tf-1 | md5sum)
1541                 [[ "$sum" = "$sum0" ]] ||
1542                         error "$tf-1.$i: checksum mismatch: $sum != $sum0"
1543         done
1544
1545         echo " **verify files be RDONLY"
1546         verify_flr_state $tf "ro"
1547         verify_flr_state $tf-1 "ro"
1548
1549         # file should not have stale component
1550         echo " **verify files do not contain stale component"
1551         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1552                 error "after resyncing $tf, it contains stale component"
1553         $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
1554                 error "after resyncing $tf, it contains stale component"
1555
1556         # verify partial resync
1557         echo " **write $tf-1 for partial resync test"
1558         dd if=/dev/zero of=$tf-1 bs=1M count=2 conv=notrunc ||
1559                 error "writing $tf-1 failed"
1560
1561         echo " **only resync mirror 2"
1562         verify_flr_state $tf-1 "wp"
1563         $LFS mirror resync --only 2 $tf-1 ||
1564                 error "resync mirror 2 of $tf-1 failed"
1565         verify_flr_state $tf "ro"
1566
1567         # resync synced mirror
1568         echo " **resync mirror 2 again"
1569         $LFS mirror resync --only 2 $tf-1 ||
1570                 error "resync mirror 2 of $tf-1 failed"
1571         verify_flr_state $tf "ro"
1572         echo " **verify $tf-1 contains stale component"
1573         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1574                 error "after writing $tf-1, it does not contain stale component"
1575
1576         echo " **full resync $tf-1"
1577         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1578         verify_flr_state $tf "ro"
1579         echo " **full resync $tf-1 again"
1580         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1581         echo " **verify $tf-1 does not contain stale component"
1582         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1583                 error "after resyncing $tf, it contains stale component"
1584
1585         return 0
1586 }
1587 run_test 41 "lfs mirror resync check"
1588
1589 test_42() {
1590         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1591
1592         local td=$DIR/$tdir
1593         local tf=$td/$tfile
1594         local mirror_cmd="$LFS mirror verify"
1595         local i
1596
1597         # create parent directory
1598         mkdir $td || error "mkdir $td failed"
1599
1600         $mirror_cmd &> /dev/null && error "no file name given"
1601         $mirror_cmd $tf &> /dev/null && error "cannot stat file $tf"
1602         $mirror_cmd $td &> /dev/null && error "$td is not a regular file"
1603
1604         # create mirrored files
1605         $LFS mirror create -N -E 4M -S 1M -E 10M -E EOF $tf ||
1606                 error "create mirrored file $tf failed"
1607         $LFS mirror create -N -E 2M -S 1M -E EOF \
1608                            -N -E 6M -E 8M -E EOF \
1609                            -N -E 16M -E EOF $tf-1 ||
1610                 error "create mirrored file $tf-1 failed"
1611         $LFS mirror create -N -c 2 -o 1,3 -N -S 2M -c -1 $tf-2 ||
1612                 error "create mirrored file $tf-2 failed"
1613
1614         # write data in [0, 10M)
1615         for i in $tf $tf-1 $tf-2; do
1616                 yes | dd of=$i bs=1M count=10 iflag=fullblock conv=notrunc ||
1617                         error "write $i failed"
1618         done
1619
1620         # resync the mirrored files
1621         $LFS mirror resync $tf-1 $tf-2 ||
1622                 error "resync $tf-1 $tf-2 failed"
1623
1624         # verify the mirrored files
1625         $mirror_cmd $tf-1 $tf-2 ||
1626                 error "verify $tf-1 $tf-2 failed"
1627
1628         get_mirror_ids $tf-1
1629         $mirror_cmd --only ${mirror_array[0]} $tf-1 &> /dev/null &&
1630                 error "at least 2 mirror ids needed with '--only' option"
1631         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 $tf-2 \
1632                 &> /dev/null &&
1633                 error "'--only' option cannot be used upon multiple files"
1634         $mirror_cmd --only 65534,${mirror_array[0]},65535 $tf-1 &&
1635                 error "invalid specified mirror ids"
1636
1637         # change the content of $tf and merge it into $tf-1
1638         for i in 6 10; do
1639                 echo a | dd of=$tf bs=1M seek=$i conv=notrunc ||
1640                         error "change $tf with seek=$i failed"
1641                 echo b | dd of=$tf-1 bs=1M seek=$i conv=notrunc ||
1642                         error "change $tf-1 with seek=$i failed"
1643         done
1644
1645         $LFS mirror resync $tf-1 || error "resync $tf-1 failed"
1646         $LFS mirror extend --no-verify -N -f $tf $tf-1 ||
1647                 error "merge $tf into $tf-1 failed"
1648
1649         # verify the mirrored files
1650         echo "Verify $tf-1 without -v option:"
1651         $mirror_cmd $tf-1 &&
1652                 error "verify $tf-1 should fail" || echo "PASS"
1653
1654         echo "Verify $tf-1 with -v option:"
1655         $mirror_cmd -v $tf-1 &&
1656                 error "verify $tf-1 should fail"
1657
1658         get_mirror_ids $tf-1
1659         echo "Verify $tf-1 with --only option:"
1660         $mirror_cmd -v --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 &&
1661                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1662                       "${mirror_array[-1]} should fail"
1663
1664         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 ||
1665                 error "verify $tf-1 with mirror ${mirror_array[0]} and" \
1666                       "${mirror_array[1]} should succeed"
1667
1668         # set stale components in $tf-1
1669         for i in 0x40002 0x40003; do
1670                 $LFS setstripe --comp-set -I$i --comp-flags=stale $tf-1 ||
1671                         error "set stale flag on component $i failed"
1672         done
1673
1674         # verify the mirrored file
1675         echo "Verify $tf-1 with stale components:"
1676         $mirror_cmd -vvv $tf-1 ||
1677                 error "verify $tf-1 with stale components should succeed"
1678
1679         echo "Verify $tf-1 with stale components and --only option:"
1680         $mirror_cmd -vvv --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 ||
1681                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1682                       "${mirror_array[-1]} should succeed"
1683 }
1684 run_test 42 "lfs mirror verify"
1685
1686 # inactivate one OST && write && restore the OST
1687 write_file_43() {
1688         local file=$1
1689         local ost=$2
1690         local PARAM="osp.${FSNAME}-OST000${ost}-osc-M*.active"
1691         local wait
1692
1693         wait=$(do_facet $SINGLEMDS \
1694                 "$LCTL get_param -n lod.*MDT0000-*.qos_maxage")
1695         wait=${wait%%[^0-9]*}
1696
1697         echo "  **deactivate OST$ost, waiting for $((wait*2+2)) seconds"
1698         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 0")
1699         # lod_qos_statfs_update needs 2*$wait seconds to refresh targets statfs
1700         sleep $(($wait * 2 + 2))
1701         echo "  **write $file"
1702         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
1703         echo "  **restore activating OST$ost, waiting for $((wait*2+2)) seconds"
1704         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 1")
1705         sleep $((wait * 2 + 2))
1706
1707         local flags=$($LFS getstripe -v $file | awk '/lcm_flags:/ { print $2 }')
1708         [ $flags = wp ] || error "file mirror state $flags != wp"
1709 }
1710
1711 test_43() {
1712         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
1713
1714         local tf=$DIR/$tfile
1715         local flags
1716
1717         rm -f $tf
1718         ##   mirror 0  ost (0, 1)
1719         ##   mirror 1  ost (1, 2)
1720         ##   mirror 2  ost (2, 0)
1721         $LFS mirror create -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 \
1722                 -N -Eeof -c2 -o2,0 $tf ||
1723                 error "create 3 mirrors file $tf failed"
1724
1725         ################## OST0 ###########################################
1726         write_file_43 $tf 0
1727         echo "  **verify components"
1728         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1729         verify_comp_attr lcme_flags $tf 0x20002 init
1730         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1731
1732         # resync
1733         echo "  **resync $tf"
1734         $LFS mirror resync $tf
1735         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1736         [ $flags = ro ] || error "file mirror state $flags != ro"
1737
1738         ################## OST1 ###########################################
1739         write_file_43 $tf 1
1740         echo "  **verify components"
1741         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1742         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1743         verify_comp_attr lcme_flags $tf 0x30003 init
1744
1745         # resync
1746         echo "  **resync $tf"
1747         $LFS mirror resync $tf
1748         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1749         [ $flags = ro ] || error "file mirror state $flags != ro"
1750
1751         ################## OST2 ###########################################
1752         write_file_43 $tf 2
1753         echo "  **verify components"
1754         verify_comp_attr lcme_flags $tf 0x10001 init
1755         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1756         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1757 }
1758 run_test 43 "mirror pick on write"
1759
1760 test_44() {
1761         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1762         rm -rf $DIR/$tdir
1763         rm -rf $DIR/$tdir-1
1764         local tf=$DIR/$tdir/$tfile
1765         local tf1=$DIR/$tdir-1/$tfile-1
1766
1767         $LFS setdirstripe -i 0 -c 1 $DIR/$tdir ||
1768                 error "create directory failed"
1769         $LFS setdirstripe -i 1 -c 1 $DIR/$tdir-1 ||
1770                 error "create remote directory failed"
1771         rm -f $tf $tf1 $tf.mirror~2
1772         # create file with 4 mirrors
1773         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1774                            -N -E 1M -E 2M -E 3M -E -1 -N2 $tf ||
1775                 error "create PFLR file $tf failed"
1776
1777         # file should be in ro status
1778         verify_flr_state $tf "ro"
1779
1780         # write data in [0, 3M)
1781         dd if=/dev/urandom of=$tf bs=1M count=3 conv=notrunc ||
1782                 error "writing $tf failed"
1783
1784         verify_flr_state $tf "wp"
1785
1786         # synchronize all mirrors of the file
1787         $LFS mirror resync $tf || error "mirror resync $tf failed"
1788
1789         verify_flr_state $tf "ro"
1790
1791         # split mirror 1
1792         $LFS mirror split --mirror-id 1 -f $tf1 $tf ||
1793                 error "split to $tf1 failed"
1794
1795         local idx0=$($LFS getstripe -m $tf)
1796         local idx1=$($LFS getstripe -m $tf1)
1797
1798         [[ x$idx0 == x0 ]] || error "$tf is not on MDT0"
1799         [[ x$idx1 == x1 ]] || error "$tf1 is not on MDT1"
1800
1801         # verify mirror count
1802         verify_mirror_count $tf 3
1803         verify_mirror_count $tf1 1
1804
1805         $LFS mirror split --mirror-id 2 $tf ||
1806                 error "split mirror 2 failed"
1807
1808         verify_mirror_count $tf 2
1809         verify_mirror_count $tf.mirror~2 1
1810
1811         $LFS mirror split --mirror-id 3 -d $tf ||
1812                 error "split and delte mirror 3 failed"
1813         verify_mirror_count $tf 1
1814
1815         # verify splitted file contains the same content as the orig file does
1816         diff $tf $tf1 || error "splited file $tf1 diffs from $tf"
1817         diff $tf $tf.mirror~2 ||
1818                 error "splited file $tf.mirror~2 diffs from $tf"
1819 }
1820 run_test 44 "lfs mirror split check"
1821
1822 test_45() {
1823         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
1824
1825         local file=$DIR/$tdir/$tfile
1826         local dir=$DIR/$tdir/$dir
1827         local temp=$DIR/$tdir/template
1828         rm -rf $DIR/$tdir
1829         test_mkdir $DIR/$tdir
1830
1831         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
1832                 -N -E3m -S1m -Eeof -N -E8m -Eeof $file ||
1833                         error "Create $file failed"
1834
1835         verify_yaml_layout $file $file.copy $temp "1. FLR file"
1836         rm -f $file $file.copy
1837
1838         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
1839                 -N -E3m -S1m -Eeof -N -E8m --flags=prefer -Eeof $file ||
1840                         error "Create $file failed"
1841
1842         verify_yaml_layout $file $file.copy $temp "2. FLR file with flags"
1843 }
1844 run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
1845
1846 verify_46() {
1847         local src=$1
1848         local dst=$2
1849         local msg_prefix=$3
1850
1851         $LFS setstripe --copy=$src $dst || error "setstripe $dst failed"
1852
1853         local layout1=$(get_layout_param $src)
1854         local layout2=$(get_layout_param $dst)
1855         # compare their layout info
1856         [ "$layout1" == "$layout2" ] ||
1857                 error "$msg_prefix $src <=> $dst layouts are not equal"
1858 }
1859
1860 test_46() {
1861         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1862
1863         local file=$DIR/$tdir/$tfile
1864         test_mkdir $DIR/$tdir
1865
1866         ########################### 1. PFL file #############################
1867         echo "  ** 1. PFL file"
1868         rm -f $file
1869         $LFS setstripe -E1m -S 1M -c2 -o0,1 -E2m -c2 -E3m -o1,0 -E4m -c1 -E-1 \
1870                 $file || error "1. Create PFL $file failed"
1871
1872         rm -f $file.copy
1873         verify_46 $file $file.copy "1. PFL file"
1874
1875         ########################### 2. plain file ###########################
1876         echo "  ** 2. plain file"
1877         rm -f $file
1878         $LFS setstripe -c2 -o0,1 -i1 $file ||
1879                 error "2. Create plain $file failed"
1880
1881         rm -f $file.copy
1882         verify_46 $file $file.copy "2. plain file"
1883
1884         ########################### 3. FLR file #############################
1885         echo "  ** 3. FLR file"
1886         $LFS setstripe -N -E1m -S 1M -c2 -o0,1 -E4m -c1 -Eeof -N -E16m -Eeof \
1887                 $file || error "3. Create FLR $file failed"
1888
1889         rm -f $file.copy
1890         verify_46 $file $file.copy "3. FLR file"
1891
1892         local dir=$DIR/$tdir/dir
1893         ########################### 4. PFL dir ##############################
1894         echo "  ** 4. PFL dir"
1895         test_mkdir $dir
1896         $LFS setstripe -E1m -S 1M -c2 -E2m -c1 -E-1 $dir ||
1897                 error "4. setstripe PFL $dir failed"
1898
1899         test_mkdir $dir.copy
1900         verify_46 $dir $dir.copy "4. PFL dir"
1901
1902         ########################### 5. plain dir ############################
1903         echo "  ** 5. plain dir"
1904         $LFS setstripe -c2 -i-1 $dir || error "5. setstripe plain $dir failed"
1905
1906         verify_46 $dir $dir.copy "5. plain dir"
1907
1908         ########################### 6. FLR dir ##############################
1909         echo "  ** 6. FLR dir"
1910         $LFS setstripe -N -E1m -S 1M -c2 -E2m -c1 -Eeof -N -E4m -Eeof $dir ||
1911                 error "6. setstripe FLR $dir failed"
1912
1913         verify_46 $dir $dir.copy "6. FLR dir"
1914 }
1915 run_test 46 "Verify setstripe --copy option"
1916
1917 test_47() {
1918         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
1919
1920         local file=$DIR/$tdir/$tfile
1921         local ids
1922         local ost
1923         local osts
1924
1925         test_mkdir $DIR/$tdir
1926
1927         # test case 1:
1928         rm -f $file
1929         # mirror1: [comp0]ost0,    [comp1]ost1 and ost2
1930         # mirror2: [comp2]    ,    [comp3] should not use ost1 or ost2
1931         $LFS mirror create -N -E2m -c1 -o0 --flags=prefer -Eeof -c2 -o1,2 \
1932                 -N -E2m -c1 -Eeof -c1 $file || error "create FLR $file failed"
1933         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
1934
1935         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
1936         $LFS mirror resync $file || error "resync $file failed"
1937
1938         ost=$($LFS getstripe -I${ids[2]} $file | awk '/l_ost_idx/{print $5}')
1939         if [[ x$ost == "x0," ]]; then
1940                 $LFS getstripe $file
1941                 error "component ${ids[2]} objects allocated on $ost " \
1942                       "shouldn't on OST0"
1943         fi
1944
1945         ost=$($LFS getstripe -I${ids[3]} $file | awk '/l_ost_idx/{print $5}')
1946         if [[ x$ost == "x1," || x$ost == "x2," ]]; then
1947                 $LFS getstripe $file
1948                 error "component ${ids[3]} objects allocated on $ost " \
1949                       "shouldn't on OST1 or on OST2"
1950         fi
1951
1952         ## test case 2:
1953         rm -f $file
1954         # mirror1: [comp0]    [comp1]
1955         # mirror2: [comp2]    [comp3]
1956         # mirror3: [comp4]    [comp5]
1957         # mirror4: [comp6]    [comp7]
1958         $LFS mirror create -N4 -E1m -c1 -Eeof -c1 $file ||
1959                 error "create FLR $file failed"
1960         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
1961
1962         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
1963         $LFS mirror resync $file || error "resync $file failed"
1964
1965         for ((i = 0; i < 6; i++)); do
1966                 osts[$i]=$($LFS getstripe -I${ids[$i]} $file |
1967                         awk '/l_ost_idx/{print $5}')
1968         done
1969         # comp[0],comp[2],comp[4] should use different osts
1970         if [[ ${osts[0]} == ${osts[2]} || ${osts[0]} == ${osts[4]} ||
1971               ${osts[2]} == ${osts[4]} ]]; then
1972                 $LFS getstripe $file
1973                 error "component ${ids[0]}, ${ids[2]}, ${ids[4]} have objects "\
1974                       "allocated on duplicated OSTs"
1975         fi
1976         # comp[1],comp[3],comp[5] should use different osts
1977         if [[ ${osts[1]} == ${osts[3]} || ${osts[1]} == ${osts[5]} ||
1978               ${osts[3]} == ${osts[5]} ]]; then
1979                 $LFS getstripe $file
1980                 error "component ${ids[1]}, ${ids[3]}, ${ids[5]} have objects "\
1981                       "allocated on duplicated OSTs"
1982         fi
1983
1984         return 0
1985 }
1986 run_test 47 "Verify mirror obj alloc"
1987
1988 test_48() {
1989         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
1990                 skip "Need MDS version at least 2.11.55"
1991
1992         local tf=$DIR/$tfile
1993
1994         rm -f $tf
1995         echo " ** create 2 mirrors FLR file $tf"
1996         $LFS mirror create -N -E2M -Eeof --flags prefer \
1997                            -N -E1M -Eeof $tf ||
1998                 error "create FLR file $tf failed"
1999
2000         echo " ** write it"
2001         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2002         verify_flr_state $tf "wp"
2003
2004         local sum0=$(md5sum < $tf)
2005
2006         echo " ** resync the file"
2007         $LFS mirror resync $tf
2008
2009         echo " ** snapshot mirror 2"
2010         $LFS setstripe --comp-set -I 0x20003 --comp-flags=nosync $tf
2011
2012         echo " ** write it again"
2013         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2014         echo " ** resync it again"
2015         $LFS mirror resync $tf
2016
2017         verify_flr_state $tf "wp"
2018         verify_comp_attr lcme_flags $tf 0x20003 nosync,stale
2019
2020         local sum1=$($LFS mirror read -N1 $tf | md5sum)
2021         local sum2=$($LFS mirror read -N2 $tf | md5sum)
2022
2023         echo " ** verify mirror 2 doesn't change"
2024         echo "original checksum: $sum0"
2025         echo "mirror 1 checksum: $sum1"
2026         echo "mirror 2 checksum: $sum2"
2027         [[ $sum0 = $sum2 ]] ||
2028                 error "original checksum: $sum0, mirror 2 checksum: $sum2"
2029         echo " ** mirror 2 stripe info"
2030         $LFS getstripe -v --mirror-index=2 $tf
2031
2032         echo " ** resync mirror 2"
2033         $LFS mirror resync --only 2 $tf
2034
2035         verify_flr_state $tf "ro"
2036         verify_comp_attr lcme_flags $tf 0x20003 nosync,^stale
2037
2038         sum1=$($LFS mirror read -N1 $tf | md5sum)
2039         sum2=$($LFS mirror read -N2 $tf | md5sum)
2040
2041         echo " ** verify mirror 2 resync-ed"
2042         echo "original checksum: $sum0"
2043         echo "mirror 1 checksum: $sum1"
2044         echo "mirror 2 checksum: $sum2"
2045         [[ $sum1 = $sum2 ]] ||
2046                 error "mirror 1 checksum: $sum1, mirror 2 checksum: $sum2"
2047         echo " ** mirror 2 stripe info"
2048         $LFS getstripe -v --mirror-index=2 $tf
2049 }
2050 run_test 48 "Verify snapshot mirror"
2051
2052 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
2053 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
2054
2055 write_file_200() {
2056         local tf=$1
2057
2058         local fsize=$(stat --printf=%s $tf)
2059
2060         while [ -f $ctrl_file ]; do
2061                 local off=$((RANDOM << 8))
2062                 local len=$((RANDOM << 5 + 131072))
2063
2064                 [ $((off + len)) -gt $fsize ] && {
2065                         fsize=$((off + len))
2066                         echo "Extending file size to $fsize .."
2067                 }
2068
2069                 flock -s $lock_file -c \
2070                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
2071                                 { rm -f $ctrl_file;
2072                                   error "failed writing to $off:$len"; }
2073                 sleep 0.$((RANDOM % 2 + 1))
2074         done
2075 }
2076
2077 read_file_200() {
2078         local tf=$1
2079
2080         while [ -f $ctrl_file ]; do
2081                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
2082                         { rm -f $ctrl_file; error "read failed"; }
2083                 sleep 0.$((RANDOM % 2 + 1))
2084         done
2085 }
2086
2087 resync_file_200() {
2088         local tf=$1
2089
2090         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
2091
2092         exec 200<>$lock_file
2093         while [ -f $ctrl_file ]; do
2094                 local lock_taken=false
2095                 local index=$((RANDOM % ${#options[@]}))
2096                 local cmd="mirror_io resync ${options[$index]}"
2097
2098                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
2099
2100                 [ $((RANDOM % 4)) -eq 0 ] && {
2101                         index=0
2102                         lock_taken=true
2103                         echo -n "lock to "
2104                 }
2105
2106                 echo -n "resync file $tf with '$cmd' .."
2107
2108                 if [[ $lock_taken = "true" ]]; then
2109                         flock -x 200 -c "$cmd $tf &> /dev/null" &&
2110                                 echo "done" || echo "failed"
2111                         flock -u 200
2112                 else
2113                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
2114                 fi
2115
2116                 sleep 0.$((RANDOM % 8 + 1))
2117         done
2118 }
2119
2120 test_200() {
2121         local tf=$DIR/$tfile
2122         local tf2=$DIR2/$tfile
2123         local tf3=$DIR3/$tfile
2124
2125         $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
2126         $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
2127         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
2128
2129         $LFS mirror extend -N -f $tf-2 $tf ||
2130                 error "merging $tf-2 into $tf failed"
2131         $LFS mirror extend -N -f $tf-3 $tf ||
2132                 error "merging $tf-3 into $tf failed"
2133
2134         mkdir -p $MOUNT2 && mount_client $MOUNT2
2135
2136         mkdir -p $MOUNT3 && mount_client $MOUNT3
2137
2138         verify_flr_state $tf3 "ro"
2139
2140         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
2141         $LCTL set_param fail_loc=0x1A03
2142
2143         local mds_idx=mds$(($($LFS getstripe -m $tf) + 1))
2144         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
2145
2146         declare -a pids
2147
2148         write_file_200 $tf &
2149         pids+=($!)
2150
2151         read_file_200 $tf &
2152         pids+=($!)
2153
2154         write_file_200 $tf2 &
2155         pids+=($!)
2156
2157         read_file_200 $tf2 &
2158         pids+=($!)
2159
2160         resync_file_200 $tf3 &
2161         pids+=($!)
2162
2163         local sleep_time=60
2164         [ "$SLOW" = "yes" ] && sleep_time=360
2165         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
2166                 sleep 1
2167                 ((--sleep_time))
2168         done
2169
2170         rm -f $ctrl_file
2171
2172         echo "Waiting ${pids[@]}"
2173         wait ${pids[@]}
2174
2175         umount_client $MOUNT2
2176         umount_client $MOUNT3
2177
2178         rm -f $lock_file
2179
2180         # resync and verify mirrors
2181         mirror_io resync $tf
2182         get_mirror_ids $tf
2183
2184         local csum=$($LFS mirror read -N ${mirror_array[0]} $tf | md5sum)
2185         for id in ${mirror_array[@]:1}; do
2186                 [ "$($LFS mirror read -N $id $tf | md5sum)" = "$csum" ] ||
2187                         error "checksum error for mirror $id"
2188         done
2189
2190         true
2191 }
2192 run_test 200 "stress test"
2193
2194 cleanup_test_201() {
2195         trap 0
2196         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
2197
2198         umount_client $MOUNT2
2199 }
2200
2201 test_201() {
2202         local delay=${RESYNC_DELAY:-5}
2203
2204         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
2205                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
2206
2207         trap cleanup_test_201 EXIT
2208
2209         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
2210                         changelog_register -n)
2211
2212         mkdir -p $MOUNT2 && mount_client $MOUNT2
2213
2214         local index=0
2215         while :; do
2216                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
2217                 [ -z "$log" ] && { sleep 1; continue; }
2218
2219                 index=$(echo $log | awk '{print $1}')
2220                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
2221                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
2222                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
2223
2224                 ((++index))
2225                 [ -z "$file" ] && continue
2226
2227                 local now=$(date +%s)
2228
2229                 echo "file: $file $fid was modified at $ts, now: $now, " \
2230                      "will be resynced at $((ts+delay))"
2231
2232                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
2233
2234                 mirror_io resync $file
2235                 echo "$file resync done"
2236         done
2237
2238         cleanup_test_201
2239 }
2240 run_test 201 "FLR data mover"
2241
2242 test_202() {
2243         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
2244
2245         local tf=$DIR/$tfile
2246         local ids
2247
2248         $LFS setstripe -E 1M -S 1M -c 1 $tf
2249         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2250         verify_comp_attr stripe-count $tf ${ids[0]} 1
2251
2252         $LFS setstripe --component-add -E 2M -c -1 $tf
2253         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2254         verify_comp_attr stripe-count $tf ${ids[0]} 1
2255         verify_comp_attr stripe-count $tf ${ids[1]} -1
2256
2257         dd if=/dev/zero of=$tf bs=1M count=2
2258         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2259         verify_comp_attr stripe-count $tf ${ids[0]} 1
2260         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
2261 }
2262 run_test 202 "lfs setstripe --add-component wide striping"
2263
2264 test_203() {
2265         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
2266                 skip "Need MDS version at least 2.11.55"
2267         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
2268
2269         local tf=$DIR/$tfile
2270
2271         #create 2 mirrors
2272         $LFS mirror create -N2 -c1 $tf || error "create FLR file $tf"
2273         #delete first mirror
2274         $LFS mirror split --mirror-id=1 -d $tf || error "delete first mirror"
2275
2276         $LFS getstripe $tf
2277         local old_id=$($LFS getstripe --mirror-id=2 -I $tf)
2278         local count=$($LFS getstripe --mirror-id=2 -c $tf) ||
2279                 error "getstripe count of mirror 2"
2280         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
2281
2282         #extend a mirror with 2 OSTs
2283         $LFS mirror extend -N -c2 $tf || error "extend mirror"
2284         $LFS getstripe $tf
2285
2286         local new_id=$($LFS getstripe --mirror-id=2 -I $tf)
2287         count=$($LFS getstripe --mirror-id=2 -c $tf) ||
2288                 error "getstripe count of mirror 2"
2289         [[ x$oldid = x$newid ]] ||
2290                 error "mirror 2 changed ID from $old_id to $new_id"
2291         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
2292
2293         count=$($LFS getstripe --mirror-id=3 -c $tf) ||
2294                 error "getstripe count of mirror 3"
2295         [[ x$count = x2 ]] || error "mirror 3 stripe count $count is not 2"
2296 }
2297 run_test 203 "mirror file preserve mirror ID"
2298
2299 complete $SECONDS
2300 check_and_cleanup_lustre
2301 exit_status