Whamcloud - gitweb
9c6c2cbb238bf7d26457ff39586af0d30b8e2d08
[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
9 ONLY=${ONLY:-"$*"}
10
11 LUSTRE=${LUSTRE:-$(dirname $0)/..}
12 . $LUSTRE/tests/test-framework.sh
13 init_test_env "$@"
14 init_logging
15
16 ALWAYS_EXCEPT="$SANITY_FLR_EXCEPT "
17 # Bug number for skipped test:    LU-14818 LU-11381 LU-14765
18 ALWAYS_EXCEPT+="                  6        201      44c "
19 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
20
21 # skip all tests for PPC until we can get sanity-pfl to pass
22 if [[ $(uname -m) = ppc64 ]]; then
23         skip "Skip FLR testing for PPC clients"
24 fi
25
26 if [[ "$ost1_FSTYPE" == "zfs" ]]; then
27         # bug #:        LU-1941
28         ALWAYS_EXCEPT+=" 49a "
29 fi
30
31 build_test_filter
32
33 [[ "$MDS1_VERSION" -ge $(version_code 2.10.56) ]] ||
34         skip "Need MDS version at least 2.10.56"
35
36 check_and_setup_lustre
37 DIR=${DIR:-$MOUNT}
38 assert_DIR
39 rm -rf $DIR/[Rdfs][0-9]*
40
41 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
42         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
43
44 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
45
46
47 # global array to store mirror IDs
48 declare -a mirror_array
49 get_mirror_ids() {
50         local tf=$1
51         local id
52         local array
53
54         array=()
55         for id in $($LFS getstripe $tf | awk '/lcme_id/{print $2}'); do
56                 array[${#array[@]}]=$((id >> 16))
57         done
58
59         mirror_array=($(printf "%s\n" "${array[@]}" | sort -u))
60
61         echo ${#mirror_array[@]}
62 }
63
64 drop_client_cache() {
65         echo 3 > /proc/sys/vm/drop_caches
66 }
67
68 stop_osts() {
69         local idx
70
71         for idx in "$@"; do
72                 stop ost$idx
73         done
74
75         for idx in "$@"; do
76                 wait_osc_import_state client ost$idx "\(DISCONN\|IDLE\)"
77         done
78 }
79
80 start_osts() {
81         local idx
82
83         for idx in "$@"; do
84                 start ost$idx $(ostdevname $idx) $OST_MOUNT_OPTS ||
85                         error "start ost$idx failed"
86         done
87
88         for idx in "$@"; do
89                 wait_recovery_complete ost$idx
90         done
91 }
92
93 #
94 # Verify mirror count with an expected value for a given file.
95 #
96 verify_mirror_count() {
97         local tf=$1
98         local expected=$2
99         local mirror_count=$($LFS getstripe -N $tf)
100
101         [[ $mirror_count = $expected ]] || {
102                 $LFS getstripe -v $tf
103                 error "verify mirror count failed on $tf:" \
104                       "$mirror_count != $expected"
105         }
106 }
107
108 #
109 # Verify component count with an expected value for a given file.
110 #       $1 coposited layout file
111 #       $2 expected component number
112 #
113 verify_comp_count() {
114         local tf=$1
115         local expected=$2
116         local comp_count=$($LFS getstripe --component-count $tf)
117
118         [[ $comp_count = $expected ]] || {
119                 $LFS getstripe -v $tf
120                 error "verify component count failed on $tf:" \
121                       "$comp_count != $expected"
122         }
123 }
124
125 #
126 # Verify component attribute with an expected value for a given file
127 # and component ID.
128 #
129 verify_comp_attr() {
130         local attr=$1
131         local tf=$2
132         local comp_id=$3
133         local expected=$4
134         local cmd="$LFS getstripe -I$comp_id"
135         local getstripe_cmd="$cmd -v"
136         local value
137
138         case $attr in
139                 stripe-size) cmd+=" -S $tf" ;;
140                 stripe-count) cmd+=" -c $tf" ;;
141                 stripe-index) cmd+=" -i $tf" ;;
142                 pool) cmd+=" -p $tf" ;;
143                 comp-start) cmd+=" --component-start $tf" ;;
144                 comp-end) cmd+=" --component-end $tf" ;;
145                 lcme_flags) cmd+=" $tf | awk '/lcme_flags:/ { print \$2 }'" ;;
146                 *) error "invalid attribute $attr";;
147         esac
148
149         value=$(eval $cmd)
150
151         [ $attr = lcme_flags ] && {
152                 local fl
153                 local expected_list=$(comma_list $expected)
154                 for fl in ${expected_list//,/ }; do
155                         local neg=0
156
157                         [[ ${fl:0:1} = "^" ]] && neg=1
158                         [[ $neg = 1 ]] && fl=${fl:1}
159
160                         $(echo $value | grep -q $fl)
161                         local match=$?
162                         # 0: matched; 1: not matched
163
164                         if  [[ $neg = 0 && $match != 0 ||
165                                $neg = 1 && $match = 0 ]]; then
166                                 $getstripe_cmd $tf
167                                 [[ $neg = 0 ]] && # expect the flag
168                                     error "expected flag '$fl' not in $comp_id"
169                                 [[ $neg = 1 ]] && # not expect the flag
170                                     error "not expected flag '$fl' in $comp_id"
171                         fi
172                 done
173                 return
174         }
175
176         [[ $value = $expected ]] || {
177                 $getstripe_cmd $tf
178                 error "verify $attr failed on $tf: $value != $expected"
179         }
180 }
181
182 #
183 # Verify component extent with expected start and end extent values
184 # for a given file and component ID.
185 #
186 verify_comp_extent() {
187         local tf=$1
188         local comp_id=$2
189         local expected_start=$3
190         local expected_end=$4
191
192         verify_comp_attr comp-start $tf $comp_id $expected_start
193         verify_comp_attr comp-end $tf $comp_id $expected_end
194 }
195
196 #
197 # Verify component attribute with parent directory for a given file
198 # and component ID.
199 #
200 verify_comp_attr_with_parent() {
201         local attr=$1
202         local tf=$2
203         local comp_id=$3
204         local td=$(cd $(dirname $tf); echo $PWD)
205         local tf_cmd="$LFS getstripe -I$comp_id"
206         local td_cmd="$LFS getstripe"
207         local opt
208         local expected
209         local value
210
211         case $attr in
212                 stripe-size) opt="-S" ;;
213                 stripe-count) opt="-c" ;;
214                 pool) opt="-p" ;;
215                 *) error "invalid attribute $attr";;
216         esac
217
218         expected=$($td_cmd $opt $td)
219         [[ $expected = -1 ]] && expected=$OSTCOUNT
220
221         value=$($tf_cmd $opt $tf)
222         [[ $value = -1 ]] && value=$OSTCOUNT
223
224         [[ $value = $expected ]] || {
225                 $td_cmd -d $td
226                 $tf_cmd -v $tf
227                 error "verify $attr failed with parent on $tf:" \
228                       "$value != $expected"
229         }
230 }
231
232 #
233 # Verify component attribute with filesystem-wide default value for a given file
234 # and component ID.
235 #
236 verify_comp_attr_with_default() {
237         local attr=$1
238         local tf=$2
239         local comp_id=$3
240         local tf_cmd="$LFS getstripe -I$comp_id"
241         local opt
242         local expected
243         local value
244
245         case $attr in
246                 stripe-size)
247                         opt="-S"
248                         expected=$($LCTL get_param -n \
249                                    lov.$FSNAME-clilov-*.stripesize)
250                         ;;
251                 stripe-count)
252                         opt="-c"
253                         expected=$($LCTL get_param -n \
254                                    lov.$FSNAME-clilov-*.stripecount)
255                         [[ $expected = -1 ]] && expected=$OSTCOUNT
256                         ;;
257                 *) error "invalid attribute $attr";;
258         esac
259
260         value=$($tf_cmd $opt $tf)
261         [[ $value = -1 ]] && value=$OSTCOUNT
262
263         [[ $value = $expected ]] || {
264                 $tf_cmd -v $tf
265                 error "verify $attr failed with default value on $tf:" \
266                       "$value != $expected"
267         }
268 }
269
270 #
271 # Verify unspecified component attributes for a given file
272 # and component ID.
273 #
274 # This will only verify the inherited attributes:
275 # stripe size, stripe count and OST pool name
276 #
277 verify_comp_attrs() {
278         local tf=$1
279         local comp_id=$2
280
281         verify_comp_attr_with_default stripe-size $tf $comp_id
282         verify_comp_attr_with_default stripe-count $tf $comp_id
283         verify_comp_attr_with_parent pool $tf $comp_id
284 }
285
286 verify_flr_state()
287 {
288         local tf=$1
289         local expected_state=$2
290
291         local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
292         [ $expected_state = $state ] ||
293                 error "expected: $expected_state, actual $state"
294 }
295
296 # command line test cases
297 test_0a() {
298         local td=$DIR/$tdir
299         local tf=$td/$tfile
300         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
301         local mirror_cmd="$LFS mirror create"
302         local id
303         local ids
304         local i
305
306         # create parent directory
307         mkdir $td || error "mkdir $td failed"
308
309         $mirror_cmd $tf &> /dev/null && error "miss -N option"
310
311         $mirror_cmd -N $tf || error "create mirrored file $tf failed"
312         verify_mirror_count $tf 1
313         id=$($LFS getstripe -I $tf)
314         verify_comp_attrs $tf $id
315         verify_comp_extent $tf $id 0 EOF
316
317         $mirror_cmd -N0 $tf-1 &> /dev/null && error "invalid mirror count 0"
318         $mirror_cmd -N$((mirror_count + 1)) $tf-1 &> /dev/null &&
319                 error "invalid mirror count $((mirror_count + 1))"
320
321         $mirror_cmd -N$mirror_count $tf-1 ||
322                 error "create mirrored file $tf-1 failed"
323         verify_mirror_count $tf-1 $mirror_count
324         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
325         for ((i = 0; i < $mirror_count; i++)); do
326                 verify_comp_attrs $tf-1 ${ids[$i]}
327                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
328         done
329
330         $mirror_cmd -N -N2 -N3 -N4 $tf-2 ||
331                 error "create mirrored file $tf-2 failed"
332         verify_mirror_count $tf-2 10
333         ids=($($LFS getstripe $tf-2 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
334         for ((i = 0; i < 10; i++)); do
335                 verify_comp_attrs $tf-2 ${ids[$i]}
336                 verify_comp_extent $tf-2 ${ids[$i]} 0 EOF
337         done
338 }
339 run_test 0a "lfs mirror create with -N option"
340
341 test_0b() {
342         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
343
344         local td=$DIR/$tdir
345         local tf=$td/$tfile
346         local mirror_cmd="$LFS mirror create"
347         local ids
348         local i
349
350         # create a new OST pool
351         local pool_name=$TESTNAME
352         create_pool $FSNAME.$pool_name ||
353                 error "create OST pool $pool_name failed"
354
355         # add OSTs into the pool
356         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
357                 error "add OSTs into pool $pool_name failed"
358
359         # create parent directory
360         mkdir $td || error "mkdir $td failed"
361         $LFS setstripe -S 8M -c -1 -p $pool_name $td ||
362                 error "$LFS setstripe $td failed"
363
364         create_pool $FSNAME.flash || error "create OST pool flash failed"
365         create_pool $FSNAME.archive || error "create OST pool archive failed"
366
367         # create a mirrored file with plain layout mirrors
368         $mirror_cmd -N -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
369                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
370                 error "create mirrored file $tf failed"
371         verify_mirror_count $tf 6
372         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
373         for ((i = 0; i < 6; i++)); do
374                 verify_comp_extent $tf ${ids[$i]} 0 EOF
375         done
376
377         # verify component ${ids[0]}
378         verify_comp_attrs $tf ${ids[0]}
379
380         # verify component ${ids[1]}
381         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
382         verify_comp_attr stripe-count $tf ${ids[1]} 2
383         verify_comp_attr stripe-index $tf ${ids[1]} 2
384
385         # verify component ${ids[2]}
386         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
387         verify_comp_attr stripe-count $tf ${ids[2]} 2
388         verify_comp_attr pool $tf ${ids[2]} flash
389
390         # verify component ${ids[3]}
391         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
392         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
393         verify_comp_attr pool $tf ${ids[3]} flash
394
395         # verify component ${ids[4]}
396         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
397         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
398         verify_comp_attr pool $tf ${ids[4]} archive
399
400         # verify component ${ids[5]}
401         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
402         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
403         verify_comp_attr_with_parent pool $tf ${ids[5]}
404
405         if [ $MDS1_VERSION -ge $(version_code 2.12.55) ]; then
406                 # LU-11022 - remove mirror by pool name
407                 local=cnt cnt=$($LFS getstripe $tf | grep archive | wc -l)
408                 [ "$cnt" != "1" ] && error "unexpected mirror count $cnt"
409                 $LFS mirror delete --pool archive $tf || error "delete mirror"
410                 cnt=$($LFS getstripe $tf | grep archive | wc -l)
411                 [ "$cnt" != "0" ] && error "mirror count after removal: $cnt"
412         fi
413
414         # destroy OST pool
415         destroy_test_pools
416 }
417 run_test 0b "lfs mirror create plain layout mirrors"
418
419 test_0c() {
420         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
421
422         local td=$DIR/$tdir
423         local tf=$td/$tfile
424         local mirror_cmd="$LFS mirror create"
425         local ids
426         local i
427
428         # create a new OST pool
429         local pool_name=$TESTNAME
430         create_pool $FSNAME.$pool_name ||
431                 error "create OST pool $pool_name failed"
432
433         # add OSTs into the pool
434         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
435                 error "add OSTs into pool $pool_name failed"
436
437         # create parent directory
438         mkdir $td || error "mkdir $td failed"
439         $LFS setstripe -E 32M -S 8M -c -1 -p $pool_name -E eof -S 16M $td ||
440                 error "$LFS setstripe $td failed"
441
442         create_pool $FSNAME.flash ||
443                 error "create OST pool flash failed"
444         create_pool $FSNAME.archive ||
445                 error "create OST pool archive failed"
446
447         # create a mirrored file with composite layout mirrors
448         $mirror_cmd -N2 -E 8M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
449                     -N -c 4 -p none \
450                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
451                 error "create mirrored file $tf failed"
452         verify_mirror_count $tf 6
453         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
454
455         # verify components ${ids[0]} and ${ids[2]}
456         for i in 0 2; do
457                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
458                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
459                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
460                 verify_comp_extent $tf ${ids[$i]} 0 8388608
461         done
462
463         # verify components ${ids[1]} and ${ids[3]}
464         for i in 1 3; do
465                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
466                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
467                 verify_comp_attr pool $tf ${ids[$i]} flash
468                 verify_comp_extent $tf ${ids[$i]} 8388608 EOF
469         done
470
471         # verify component ${ids[4]}
472         verify_comp_attr stripe-size $tf ${ids[4]} 4194304
473         verify_comp_attr stripe-count $tf ${ids[4]} 4
474         verify_comp_attr_with_parent pool $tf ${ids[4]}
475         verify_comp_extent $tf ${ids[4]} 0 EOF
476
477         # verify components ${ids[5]}, ${ids[7]} and ${ids[9]}
478         for i in 5 7 9; do
479                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
480                 verify_comp_attr stripe-count $tf ${ids[$i]} 4
481                 verify_comp_attr pool $tf ${ids[$i]} archive
482                 verify_comp_extent $tf ${ids[$i]} 0 536870912
483         done
484
485         # verify components ${ids[6]}, ${ids[8]} and ${ids[10]}
486         for i in 6 8 10; do
487                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
488                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
489                 verify_comp_attr pool $tf ${ids[$i]} archive
490                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
491         done
492
493         # destroy OST pool
494         destroy_test_pools
495 }
496 run_test 0c "lfs mirror create composite layout mirrors"
497
498 test_0d() {
499         local td=$DIR/$tdir
500         local tf=$td/$tfile
501         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
502         local mirror_cmd="$LFS mirror extend"
503         local ids
504         local i
505
506         # create parent directory
507         mkdir $td || error "mkdir $td failed"
508
509         $mirror_cmd $tf &> /dev/null && error "miss -N option"
510         $mirror_cmd -N $tf &> /dev/null && error "$tf does not exist"
511
512         # create a non-mirrored file, convert it to a mirrored file and extend
513         touch $tf || error "touch $tf failed"
514         $mirror_cmd -N $tf || error "convert and extend $tf failed"
515         verify_mirror_count $tf 2
516         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
517         for ((i = 0; i < 2; i++)); do
518                 verify_comp_attrs $tf ${ids[$i]}
519                 verify_comp_extent $tf ${ids[$i]} 0 EOF
520         done
521
522         lfsck_verify_pfid $tf || error "PFID is not set"
523
524         # create a mirrored file and extend it
525         $LFS mirror create -N $tf-1 || error "create mirrored file $tf-1 failed"
526         $LFS mirror create -N $tf-2 || error "create mirrored file $tf-2 failed"
527         $LFS mirror create -N $tf-3 || error "create mirrored file $tf-3 failed"
528
529         $mirror_cmd -N -S 4M -N -f $tf-2 $tf-1 ||
530                 error "extend mirror with -f failed"
531
532         $mirror_cmd -N$((mirror_count - 1)) $tf-3 ||
533                 error "extend mirrored file $tf-3 failed"
534         verify_mirror_count $tf-3 $mirror_count
535         ids=($($LFS getstripe $tf-3 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
536         for ((i = 0; i < $mirror_count; i++)); do
537                 verify_comp_attrs $tf-3 ${ids[$i]}
538                 verify_comp_extent $tf-3 ${ids[$i]} 0 EOF
539         done
540
541         $mirror_cmd -N $tf-3 &> /dev/null
542         rc=$?
543         [[ $rc == 34 ]] ||
544                 error "exceeded maximum mirror count returns $rc not ERANGE(34)"
545 }
546 run_test 0d "lfs mirror extend with -N option"
547
548 test_0e() {
549         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
550
551         local td=$DIR/$tdir
552         local tf=$td/$tfile
553         local mirror_cmd="$LFS mirror extend"
554         local ids
555         local i
556
557         # create parent directory
558         mkdir $td || error "mkdir $td failed"
559
560         create_pool $FSNAME.ssd ||
561                 error "create OST pool ssd failed"
562
563         # create a mirrored file with plain layout mirrors
564         $LFS mirror create -N -S 32M -c 3 -p ssd -i 1 -o 1,2,3 $tf ||
565                 error "create mirrored file $tf failed"
566
567         create_pool $FSNAME.flash ||
568                 error "create OST pool flash failed"
569         create_pool $FSNAME.archive ||
570                 error "create OST pool archive failed"
571
572         # extend the mirrored file with plain layout mirrors
573         $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
574                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
575                 error "extend mirrored file $tf failed"
576         verify_mirror_count $tf 6
577         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
578         for ((i = 0; i < 6; i++)); do
579                 verify_comp_extent $tf ${ids[$i]} 0 EOF
580         done
581
582         # verify component ${ids[0]}
583         verify_comp_attr stripe-size $tf ${ids[0]} 33554432
584         verify_comp_attr stripe-count $tf ${ids[0]} 3
585         verify_comp_attr stripe-index $tf ${ids[0]} 1
586
587         # verify component ${ids[1]}
588         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
589         verify_comp_attr stripe-count $tf ${ids[1]} 2
590         verify_comp_attr stripe-index $tf ${ids[1]} 2
591
592         # verify component ${ids[2]}
593         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
594         verify_comp_attr stripe-count $tf ${ids[2]} 2
595         verify_comp_attr pool $tf ${ids[2]} flash
596
597         # verify component ${ids[3]}
598         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
599         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
600         verify_comp_attr pool $tf ${ids[3]} flash
601
602         # verify component ${ids[4]}
603         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
604         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
605         verify_comp_attr pool $tf ${ids[4]} archive
606
607         # verify component ${ids[5]}
608         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
609         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
610         verify_comp_attr_with_parent pool $tf ${ids[5]}
611 }
612 run_test 0e "lfs mirror extend plain layout mirrors"
613
614 test_0f() {
615         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
616
617         local td=$DIR/$tdir
618         local tf=$td/$tfile
619         local mirror_cmd="$LFS mirror extend"
620         local ids
621         local i
622
623         # create parent directory
624         mkdir $td || error "mkdir $td failed"
625
626         create_pool $FSNAME.ssd ||
627                 error "create OST pool ssd failed"
628
629         # create a mirrored file with composite layout mirror
630         $LFS mirror create -N -E 32M -S 16M -p ssd -E eof -S 32M $tf ||
631                 error "create mirrored file $tf failed"
632
633         create_pool $FSNAME.flash ||
634                 error "create OST pool flash failed"
635         create_pool $FSNAME.archive ||
636                 error "create OST pool archive failed"
637
638         # extend the mirrored file with composite layout mirrors
639         $mirror_cmd -N -p archive \
640                     -N2 -E 8M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
641                     -N -c -1 -p none \
642                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
643                 error "extend mirrored file $tf failed"
644         verify_mirror_count $tf 8
645         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
646
647         # verify component ${ids[0]}
648         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
649         verify_comp_attr_with_default stripe-count $tf ${ids[0]}
650         verify_comp_attr pool $tf ${ids[0]} ssd
651         verify_comp_extent $tf ${ids[0]} 0 33554432
652
653         # verify component ${ids[1]}
654         verify_comp_attr stripe-size $tf ${ids[1]} 33554432
655         verify_comp_attr_with_default stripe-count $tf ${ids[1]}
656         verify_comp_attr pool $tf ${ids[1]} ssd
657         verify_comp_extent $tf ${ids[1]} 33554432 EOF
658
659         # verify component ${ids[2]}
660         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
661         verify_comp_attr_with_default stripe-count $tf ${ids[2]}
662         verify_comp_attr pool $tf ${ids[2]} archive
663         verify_comp_extent $tf ${ids[2]} 0 EOF
664
665         # verify components ${ids[3]} and ${ids[5]}
666         for i in 3 5; do
667                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
668                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
669                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
670                 verify_comp_extent $tf ${ids[$i]} 0 8388608
671         done
672
673         # verify components ${ids[4]} and ${ids[6]}
674         for i in 4 6; do
675                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
676                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
677                 verify_comp_attr pool $tf ${ids[$i]} flash
678                 verify_comp_extent $tf ${ids[$i]} 8388608 EOF
679         done
680
681         # verify component ${ids[7]}
682         verify_comp_attr stripe-size $tf ${ids[7]} 4194304
683         verify_comp_attr stripe-count $tf ${ids[7]} $OSTCOUNT
684         verify_comp_attr_with_parent pool $tf ${ids[7]}
685         verify_comp_extent $tf ${ids[7]} 0 EOF
686
687         # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
688         for i in 8 10 12; do
689                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
690                 verify_comp_attr stripe-count $tf ${ids[$i]} $OSTCOUNT
691                 verify_comp_attr pool $tf ${ids[$i]} archive
692                 verify_comp_extent $tf ${ids[$i]} 0 536870912
693         done
694
695         # verify components ${ids[9]}, ${ids[11]} and ${ids[13]}
696         for i in 9 11 13; do
697                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
698                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
699                 verify_comp_attr pool $tf ${ids[$i]} archive
700                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
701         done
702 }
703 run_test 0f "lfs mirror extend composite layout mirrors"
704
705 test_0g() {
706         local tf=$DIR/$tfile
707
708         ! $LFS mirror create --flags prefer $tf ||
709                 error "creating $tf w/ --flags but w/o -N option should fail"
710
711         ! $LFS mirror create -N --flags foo $tf ||
712                 error "creating $tf with '--flags foo' should fail"
713
714         ! $LFS mirror create -N --flags stale $tf ||
715                 error "creating $tf with '--flags stale' should fail"
716
717         ! $LFS mirror create -N --flags prefer,init $tf ||
718                 error "creating $tf with '--flags prefer,init' should fail"
719
720         ! $LFS mirror create -N --flags ^prefer $tf ||
721                 error "creating $tf with '--flags ^prefer' should fail"
722
723         $LFS mirror create -N -E 1M -S 1M -o0 --flags=prefer -E eof -o1 \
724                            -N -o1 $tf || error "create mirrored file $tf failed"
725
726         verify_comp_attr lcme_flags $tf 0x10001 prefer
727         verify_comp_attr lcme_flags $tf 0x10002 prefer
728
729         # write to the mirrored file and check primary
730         cp /etc/hosts $tf || error "error writing file '$tf'"
731
732         verify_comp_attr lcme_flags $tf 0x20003 stale
733
734         # resync file and check prefer flag
735         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
736
737         cancel_lru_locks osc
738         $LCTL set_param osc.*.stats=clear
739         cat $tf &> /dev/null || error "error reading file '$tf'"
740
741         # verify that the data was provided by OST1 where mirror 1 resides
742         local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-[-0-9a-f]*.stats |
743                         awk '/ost_read/{print $2}')
744         [ -n "$nr_read" ] || error "read was not provided by OST1"
745 }
746 run_test 0g "lfs mirror create flags support"
747
748 test_0h() {
749         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
750                 skip "Need MDS version at least 2.11.57"
751
752         local td=$DIR/$tdir
753         local tf=$td/$tfile
754         local flag
755         local ids
756         local i
757
758         # create parent directory
759         test_mkdir $td || error "mkdir $td failed"
760
761         $LFS setstripe -N -E 1M -S 1M --flags=prefer -E eof -N2 $td ||
762                 error "set default mirrored layout on directory $td failed"
763
764         # verify flags are inherited from the directory
765         touch $tf
766
767         verify_comp_attr lcme_flags $tf 0x10001 prefer
768         verify_comp_attr lcme_flags $tf 0x10002 prefer
769
770         # set flags to the first component
771         ! $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,foo $tf ||
772                 error "setting '^prefer,foo' flags should fail"
773
774         ! $LFS getstripe --component-flags=prefer,foo $tf ||
775                 error "getting component(s) with 'prefer,foo' flags should fail"
776
777         $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,stale $tf
778
779         verify_comp_attr lcme_flags $tf 0x10001 stale
780         verify_comp_attr lcme_flags $tf 0x10002 prefer
781
782         $LFS setstripe --comp-set -I0x10001 --comp-flags=^stale $tf &&
783                 error "clearing 'stale' should fail"
784
785         # write and resync file. It can't resync the file directly because the
786         # file state is still 'ro'
787         cp /etc/hosts $tf || error "error writing file '$tf'"
788         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
789
790         for flag in prefer prefrd prefwr; do
791                 $LFS setstripe --comp-set -I 0x20003 --comp-flags=$flag $tf ||
792                         error "error setting flag $flag"
793
794                 verify_comp_attr lcme_flags $tf 0x20003 $flag
795
796                 $LFS setstripe --comp-set -I 0x20003 --comp-flags=^$flag $tf ||
797                         error "error clearing $flag flag from component 0x20003"
798         done
799
800         # MDS disallows setting stale flag on the last non-stale mirror
801         [[ "$MDS1_VERSION" -ge $(version_code 2.12.57) ]] || return 0
802
803         cp /etc/hosts $tf || error "error writing file '$tf'"
804
805         verify_comp_attr lcme_flags $tf 0x10002 prefer
806         verify_comp_attr lcme_flags $tf 0x20003 stale
807         verify_comp_attr lcme_flags $tf 0x30004 stale
808
809         ! $LFS setstripe --comp-set -I 0x10002 --comp-flags=^prefer,stale $tf \
810                 > /dev/null 2>&1 ||
811                 error "setting stale flag on component 0x10002 should fail"
812
813         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
814
815         $LFS setstripe --comp-set -I 0x10001 --comp-flags=stale $tf ||
816                 error "error setting stale flag on component 0x10001"
817         $LFS setstripe --comp-set -I 0x20003 --comp-flags=stale $tf ||
818                 error "error setting stale flag on component 0x20003"
819
820         ! $LFS setstripe --comp-set -I 0x30004 --comp-flags=stale $tf \
821                 > /dev/null 2>&1 ||
822                 error "setting stale flag on component 0x30004 should fail"
823
824         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
825 }
826 run_test 0h "set, clear and test flags for FLR files"
827
828 test_0j() {
829         $LFS mirror create -N2 $DIR/$tfile || error "create $DIR/$tfile failed"
830
831         cp /etc/hosts $DIR/$tfile || error "write to $DIR/$tfile failed"
832         $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed"
833         cmp /etc/hosts $DIR/$tfile || error "cmp with /etc/hosts failed"
834
835         $LFS mirror read -N2 -o $TMP/$tfile $DIR/$tfile || error "read mirror failed"
836         stack_trap "rm -f $TMP/$tfile"
837         cmp $TMP/$tfile $DIR/$tfile || error "cmp with $TMP/$tfile failed"
838         $LFS mirror write -N2 -i /etc/passwd $DIR/$tfile || error "write failed"
839         $LFS setstripe --comp-set -I 65537 --comp-flags=stale $DIR/$tfile ||
840                 error "set component 1 stale failed"
841         $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed"
842         cmp /etc/passwd $DIR/$tfile || error "cmp with /etc/passwd failed"
843 }
844 run_test 0j "test lfs mirror read/write commands"
845
846 test_0k() {
847         [[ $OSTCOUNT -lt 3 ]] && skip "need >= 3 OSTs" && return
848
849         mkdir $DIR/$tdir
850         # default FLR with 2 mirrors
851         $LFS setstripe -N -c1 -i0 -N -c1 -i1 $DIR/$tdir ||
852                 error "set default layout failed"
853
854         # plain file extension under default FLR dir
855         echo $tfile >> $DIR/$tdir/$tfile.append || error "create $tfile.append failed"
856         echo -n " before extend $tfile.append, mirror count = "
857         $LFS getstripe -N $DIR/$tdir/$tfile.append
858
859         $LFS mirror extend -N -c1 -i2 $DIR/$tdir/$tfile.append ||
860                 error "mirror extend failed"
861         echo -n " after extend $tfile.append, mirror count = "
862         $LFS getstripe -N $DIR/$tdir/$tfile.append
863
864         # normal file extension under default FLR dir
865         touch $DIR/$tdir/$tfile || error "create $tfile failed"
866         echo -n " before extend $tfile, mirror count = "
867         $LFS getstripe -N $DIR/$tdir/$tfile
868
869         $LFS mirror extend -N -c1 -i2 $DIR/$tdir/$tfile ||
870                 error "mirror extend $tfile failed"
871         echo -n " after extend $tfile, mirror count = "
872         $LFS getstripe -N $DIR/$tdir/$tfile
873 }
874 run_test 0k "mirroring a file in directory with default FLR layout"
875
876 test_1() {
877         local tf=$DIR/$tfile
878         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
879         local mirror_create_cmd="$LFS mirror create"
880         local stripes[0]=$OSTCOUNT
881
882         mirror_create_cmd+=" -N -c ${stripes[0]}"
883         for ((i = 1; i < $mirror_count; i++)); do
884                 # add mirrors with different stripes to the file
885                 stripes[$i]=$((RANDOM % OSTCOUNT))
886                 [ ${stripes[$i]} -eq 0 ] && stripes[$i]=1
887
888                 mirror_create_cmd+=" -N -c ${stripes[$i]}"
889         done
890
891         $mirror_create_cmd $tf || error "create mirrored file $tf failed"
892         verify_mirror_count $tf $mirror_count
893
894         # can't create mirrors exceeding LUSTRE_MIRROR_COUNT_MAX
895         $LFS mirror extend -N $tf &&
896                 error "Creating the $((mirror_count+1))th mirror succeeded"
897
898         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
899                         tr '\n' ' '))
900
901         # verify the range of components and stripe counts
902         for ((i = 0; i < $mirror_count; i++)); do
903                 verify_comp_attr stripe-count $tf ${ids[$i]} ${stripes[$i]}
904                 verify_comp_extent $tf ${ids[$i]} 0 EOF
905         done
906 }
907 run_test 1 "create components with setstripe options"
908
909 test_2() {
910         local tf=$DIR/$tfile
911         local tf2=$DIR/$tfile-2
912
913         $LFS setstripe -E 1M -S 1M -E EOF -c 1 $tf
914         $LFS setstripe -E 2M -S 1M -E EOF -c -1 $tf2
915
916         $LFS mirror extend -N -f $tf2 $tf ||
917                 error "merging $tf2 into $tf failed"
918
919         verify_mirror_count $tf 2
920         [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
921 }
922 run_test 2 "create components from existing files"
923
924 test_3() {
925         [[ $MDSCOUNT -lt 2 ]] && skip "need >= 2 MDTs" && return
926
927         for ((i = 0; i < 2; i++)); do
928                 $LFS mkdir -i $i $DIR/$tdir-$i
929                 $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
930         done
931
932         $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
933                 $DIR/$tdir-0/$tfile || error "creating mirrors"
934
935         # mdt doesn't support to cancel layout lock for remote objects, do
936         # it here manually.
937         cancel_lru_locks mdc
938
939         # make sure the mirrorted file was created successfully
940         [[ $($LFS getstripe --component-count $DIR/$tdir-0/$tfile) -eq 2 ]] ||
941                 { $LFS getstripe $DIR/$tdir-0/$tfile;
942                         error "expected 2 components"; }
943
944         # cleanup
945         rm -rf $DIR/$tdir-*
946 }
947 run_test 3 "create components from files located on different MDTs"
948
949 test_4() {
950         local tf=$DIR/$tdir/$tfile
951         local ids=()
952
953         test_mkdir $DIR/$tdir
954
955         # set mirror with setstripe options to directory
956         $LFS mirror create -N2 -E 1M -S 1M -E eof $DIR/$tdir ||
957                 error "set mirror to directory error"
958
959         [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
960                 x"mirrored" ] || error "failed to create mirrored dir"
961
962         touch $tf
963         verify_mirror_count $tf 2
964
965         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
966         verify_comp_extent $tf ${ids[0]} 0 1048576
967         verify_comp_extent $tf ${ids[1]} 1048576 EOF
968
969         # sub directory should inherit mirror setting from parent
970         test_mkdir $DIR/$tdir/td
971         [ x$($LFS getstripe -v $DIR/$tdir/td | awk '/lcm_flags/{print $2}') = \
972                 x"mirrored" ] || error "failed to inherit mirror from parent"
973
974         # mirror extend won't be applied to directory
975         $LFS mirror extend -N2 $DIR/$tdir &&
976                 error "expecting mirror extend failure"
977         true
978 }
979 run_test 4 "Make sure mirror attributes can be inhertied from directory"
980
981 test_5() {
982         local tf=$DIR/$tfile
983         local ids=()
984
985         $MULTIOP $tf oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T12345c ||
986                 error "failed to create file with non-empty layout"
987         $CHECKSTAT -t file -s 12345 $tf || error "size error: expecting 12345"
988
989         $LFS mirror create -N3 $tf || error "failed to attach mirror layout"
990         verify_mirror_count $tf 3
991
992         $CHECKSTAT -t file -s 12345 $tf ||
993                 error "size error after attaching layout "
994 }
995 run_test 5 "Make sure init size work for mirrored layout"
996
997 test_6a() {
998         (( $MDS1_VERSION >= $(version_code 2.12.58) )) ||
999                 skip "MDS version older than 2.12.58"
1000
1001         local tf=$DIR/$tfile
1002
1003         $LFS mirror create -N -E 1M -L mdt -E eof -S 1M -N -E eof $tf ||
1004                 error "failure to create DoM file with mirror"
1005
1006         $LFS mirror create -N -E 1M -S 1M -E eof -N -E 1M -L mdt -E eof $tf ||
1007                 error "failure to create mirrored file with DoM"
1008
1009         $LFS setstripe -E 1M -L mdt -E eof -S 1M $tf ||
1010                 error "failure to create PFL with DoM file"
1011         $LFS mirror extend -N2 $tf ||
1012                 error "failure to extend mirror with DoM"
1013
1014         $LFS setstripe -E 1M -L mdt -E eof -S 1M $tf-1 ||
1015                 error "failure to create PFL with DoM file"
1016         $LFS mirror create -N2 -E 1M -S 1M -E eof $tf-2 ||
1017                 error "failure to create mirrored file"
1018         $LFS mirror extend -N -f $tf-1 $tf-2 ||
1019                 error "failure to extend mirrored file with DoM extent"
1020 }
1021 run_test 6a "DoM and FLR work together"
1022
1023 test_6b() {
1024         (( $MDS1_VERSION >= $(version_code 2.15.58.1) )) ||
1025                 skip "MDS version older than 2.15.58.1"
1026
1027         local tf=$DIR/$tfile
1028
1029         $LFS setstripe -E64K -L mdt -Eeof $tf ||
1030                 error "failure to create PFL with DoM file"
1031         $LFS mirror extend -N -E1M -L mdt -Eeof $tf &&
1032                 error "should not extend mirror with different DoM size"
1033
1034         return 0
1035 }
1036 run_test 6b "extend mirror with different DoM size"
1037
1038 test_7() {
1039         local tf=$DIR/$tfile
1040
1041         # create DoM with setting stripe_size == component size
1042         $LFS mirror create -N -E1M -S1M -L mdt -Eeof $tf ||
1043                 error "failure to create DoM with stripe_size == comp size"
1044         rm -f $tf || error "delete $tf"
1045
1046         # DoM should not inherit previous component stripe_size
1047         $LFS mirror create -N -E4M -S2M -Eeof -N -E1M -L mdt -Eeof $tf ||
1048                 error "DoM component shouldn't inherit previous stripe_size"
1049 }
1050 run_test 7 "Create mirror with DoM component"
1051
1052 test_21() {
1053         local tf=$DIR/$tfile
1054         local tf2=$DIR/$tfile-2
1055
1056         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1057
1058         stack_trap "rm -f $tf $tf2"
1059
1060         $LFS setstripe -E EOF -o 0 $tf
1061         $LFS setstripe -E EOF -o 1 $tf2
1062
1063         local dd_count=$((RANDOM % 20 + 1))
1064         dd if=/dev/zero of=$tf bs=1M count=$dd_count oflag=sync
1065         dd if=/dev/zero of=$tf2 bs=1M count=1 seek=$((dd_count - 1)) oflag=sync
1066
1067         # for zfs - sync OST dataset so that du below will return
1068         # accurate results
1069         [ "$FSTYPE" = "zfs" ] &&
1070                 do_nodes $(comma_list $(osts_nodes)) "$ZPOOL sync"
1071
1072         local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
1073
1074         # add component
1075         $LFS mirror extend -N -f $tf2 $tf ||
1076                 error "merging $tf2 into $tf failed"
1077
1078         # cancel layout lock
1079         cancel_lru_locks mdc
1080
1081         local new_blocks=$(du -k $tf | awk '{print $1}')
1082         [ $new_blocks -eq $blocks ] ||
1083         error "i_blocks error expected: $blocks, actual: $new_blocks"
1084 }
1085 run_test 21 "glimpse should report accurate i_blocks"
1086
1087 get_osc_lock_count() {
1088         local lock_count=0
1089
1090         for idx in "$@"; do
1091                 local osc_name
1092                 local count
1093
1094                 osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'[-0-9a-f]*'
1095                 count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
1096                 lock_count=$((lock_count + count))
1097         done
1098         echo $lock_count
1099 }
1100
1101 test_22() {
1102         local tf=$DIR/$tfile
1103
1104         stack_trap "rm -f $tf"
1105
1106         $LFS setstripe -E EOF -o 0 $tf
1107         dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
1108
1109         # add component, two mirrors located on the same OST ;-)
1110         $LFS mirror extend -N -o 0 $tf ||
1111                 error "extending mirrored file $tf failed"
1112
1113         size_blocks=$(stat --format="%b %s" $tf)
1114
1115         cancel_lru_locks mdc
1116         cancel_lru_locks osc
1117
1118         local new_size_blocks=$(stat --format="%b %s" $tf)
1119
1120         # make sure there is no lock cached
1121         [ $(get_osc_lock_count 1) -eq 0 ] || error "glimpse requests were sent"
1122
1123         [ "$new_size_blocks" = "$size_blocks" ] ||
1124                 echo "size expected: $size_blocks, actual: $new_size_blocks"
1125
1126         rm -f $tmpfile
1127 }
1128 run_test 22 "no glimpse to OSTs for READ_ONLY files"
1129
1130 test_31() {
1131         local tf=$DIR/$tfile
1132
1133         $LFS mirror create -N -o 0 -N -o 1 $tf ||
1134                 error "creating mirrored file $tf failed"
1135
1136         local ost_idx
1137         for ((ost_idx = 1; ost_idx <= 2; ost_idx++)); do
1138                 cancel_lru_locks osc
1139                 stop_osts $ost_idx
1140
1141                 local tmpfile=$(mktemp)
1142                 stat --format="%b %s" $tf > $tmpfile  &
1143                 local pid=$!
1144
1145                 local cnt=0
1146                 while [ $cnt -le 5 ]; do
1147                         kill -0 $pid > /dev/null 2>&1 || break
1148                         sleep 1
1149                         ((cnt += 1))
1150                 done
1151                 kill -0 $pid > /dev/null 2>&1 &&
1152                         error "stat process stuck due to unavailable OSTs"
1153
1154                 # make sure glimpse request has been sent
1155                 [ $(get_osc_lock_count 1 2) -ne 0 ] ||
1156                         error "OST $ost_idx: no glimpse request was sent"
1157
1158                 start_osts $ost_idx
1159         done
1160 }
1161 run_test 31 "make sure glimpse request can be retried"
1162
1163 test_32() {
1164         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1165         rm -f $DIR/$tfile $DIR/$tfile-2
1166
1167         stack_trap "rm -f $DIR/$tfile"
1168
1169         $LFS setstripe -E EOF -o 0 $DIR/$tfile
1170         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=$((RANDOM % 10 + 2))
1171
1172         local fsize=$(stat -c %s $DIR/$tfile)
1173         [[ $fsize -ne 0 ]] || error "file size is (wrongly) zero"
1174
1175         local cksum=$(md5sum $DIR/$tfile)
1176
1177         # create a new mirror in sync mode
1178         $LFS mirror extend -N -o 1 $DIR/$tfile ||
1179                 error "extending mirrored file $DIR/$tfile failed"
1180
1181         # make sure the mirrored file was created successfully
1182         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1183                 { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
1184
1185         drop_client_cache
1186         stop_osts 1
1187
1188         # check size is correct, glimpse request should go to the 2nd mirror
1189         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1190                 error "file size error $fsize vs. $(stat -c %s $DIR/$tfile)"
1191
1192         echo "reading file from the 2nd mirror and verify checksum"
1193         [[ "$cksum" == "$(md5sum $DIR/$tfile)" ]] ||
1194                 error "checksum error: expected $cksum"
1195
1196         start_osts 1
1197 }
1198 run_test 32 "data should be mirrored to newly created mirror"
1199
1200 test_33a() {
1201         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1202
1203         rm -f $DIR/$tfile $DIR/$tfile-2
1204
1205         # create a file with two mirrors
1206         $LFS setstripe -E EOF -o 0 $DIR/$tfile
1207         local max_count=100
1208         local count=0
1209         while [ $count -lt $max_count ]; do
1210                 echo "ost1" >> $DIR/$tfile
1211                 count=$((count + 1));
1212         done
1213
1214         # tmp file that will be used as mirror
1215         $LFS setstripe -E EOF -o 1 $DIR/$tfile-2
1216         count=0
1217         while [ $count -lt $max_count ]; do
1218                 echo "ost2" >> $DIR/$tfile-2
1219                 count=$((count + 1));
1220         done
1221
1222         # create a mirrored file
1223         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
1224                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1225                       "with verification should fail"
1226         $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
1227                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1228                       "without verification failed"
1229
1230         # make sure that $tfile has two mirrors and $tfile-2 does not exist
1231         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1232                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1233
1234         [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
1235
1236         # execpted file size
1237         local fsize=$((5 * max_count))
1238         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1239                 error "mirrored file size is not $fsize"
1240
1241         # read file - all OSTs are available
1242         echo "reading file (data can be provided by any ost)... "
1243         local rs=$(cat $DIR/$tfile | head -1)
1244         [[ "$rs" == "ost1" || "$rs" == "ost2" ]] ||
1245                 error "file content error: expected: \"ost1\" or \"ost2\""
1246
1247         # read file again with ost1 failed
1248         stop_osts 1
1249         drop_client_cache
1250
1251         echo "reading file (data should be provided by ost2)..."
1252         local rs=$(cat $DIR/$tfile | head -1)
1253         [[ "$rs" == "ost2" ]] ||
1254                 error "file content error: expected: \"ost2\", actual: \"$rs\""
1255
1256         # remount ost1
1257         start_osts 1
1258
1259         # read file again with ost2 failed
1260         stop_osts 2
1261         drop_client_cache
1262
1263         # check size, glimpse should work
1264         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1265                 error "mirrored file size is not $fsize"
1266
1267         echo "reading file (data should be provided by ost1)..."
1268         local rs=$(cat $DIR/$tfile | head -1)
1269         [[ "$rs" == "ost1" ]] ||
1270                 error "file content error: expected: \"ost1\", actual: \"$rs\""
1271
1272         start_osts 2
1273 }
1274 run_test 33a "read can choose available mirror to read"
1275
1276 test_33b() {
1277         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1278
1279         rm -f $DIR/$tfile
1280
1281         stack_trap "rm -f $DIR/$tfile" EXIT
1282
1283         # create a file with two mirrors on OST0000 and OST0001
1284         $LFS setstripe -N -Eeof -o0 -N -Eeof -o1 $DIR/$tfile
1285
1286         # make sure that $tfile has two mirrors
1287         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1288                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1289
1290         # write 50M
1291         dd if=/dev/urandom of=$DIR/$tfile bs=2M count=25 ||
1292                 error "write failed for $DIR/$tfile"
1293         $LFS mirror resync $DIR/$tfile || error "resync failed for $DIR/$tfile"
1294         verify_flr_state $DIR/$tfile "ro"
1295         drop_client_cache
1296
1297         ls -l $DIR/$tfile
1298
1299         # read file - all OSTs are available
1300         echo "reading file (data can be provided by any ost)... "
1301         local t1=$SECONDS
1302         time cat $DIR/$tfile > /dev/null || error "read all"
1303         local t2=$SECONDS
1304         ra=$((t2 - t1))
1305
1306         # read file again with ost1 {OST0000} failed
1307         stop_osts 1
1308         drop_client_cache
1309         echo "reading file (data should be provided by ost2)..."
1310         t1=$SECONDS
1311         time cat $DIR/$tfile > /dev/null || error "read ost2"
1312         t2=$SECONDS
1313         r1=$((t2 - t1))
1314
1315         # remount ost1
1316         start_osts 1
1317
1318         # read file again with ost2 {OST0001} failed
1319         stop_osts 2
1320         drop_client_cache
1321
1322         echo "reading file (data should be provided by ost1)..."
1323         t1=$SECONDS
1324         time cat $DIR/$tfile > /dev/null || error "read ost1"
1325         t2=$SECONDS
1326         r2=$((t2 - t1))
1327
1328         # remount ost2
1329         start_osts 2
1330
1331         [ $((r1 * 100)) -gt $((ra * 105)) -a $r1 -gt $((ra + 2)) ] &&
1332                 error "read mirror too slow without ost1, from $ra to $r1"
1333         [ $((r2 * 100)) -gt $((ra * 105)) -a $r2 -gt $((ra + 2)) ] &&
1334                 error "read mirror too slow without ost2, from $ra to $r2"
1335
1336         wait_osc_import_ready client ost2
1337 }
1338 run_test 33b "avoid reading from unhealthy mirror"
1339
1340 test_33c() {
1341         [[ $OSTCOUNT -lt 3 ]] && skip "need >= 3 OSTs" && return
1342
1343         rm -f $DIR/$tfile
1344
1345         stack_trap "rm -f $DIR/$tfile" EXIT
1346
1347         # create a file with two mirrors
1348         # mirror1: {OST0000, OST0001}
1349         # mirror2: {OST0001, OST0002}
1350         $LFS setstripe -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 $DIR/$tfile
1351
1352         # make sure that $tfile has two mirrors
1353         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1354                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1355
1356         # write 50M
1357         dd if=/dev/urandom of=$DIR/$tfile bs=2M count=25 ||
1358                 error "write failed for $DIR/$tfile"
1359         $LFS mirror resync $DIR/$tfile || error "resync failed for $DIR/$tfile"
1360         verify_flr_state $DIR/$tfile "ro"
1361         drop_client_cache
1362
1363         ls -l $DIR/$tfile
1364
1365         # read file - all OSTs are available
1366         echo "reading file (data can be provided by any ost)... "
1367         time cat $DIR/$tfile > /dev/null || error "read all"
1368
1369         # read file again with ost2 (OST0001) failed
1370         stop_osts 2
1371         drop_client_cache
1372
1373         echo "reading file (data should be provided by ost1 and ost3)..."
1374         time cat $DIR/$tfile > /dev/null || error "read ost1 & ost3"
1375
1376         # remount ost2
1377         start_osts 2
1378
1379         wait_osc_import_ready client ost2
1380 }
1381 run_test 33c "keep reading among unhealthy mirrors"
1382
1383 test_34a() {
1384         (( $OSTCOUNT >= 4 )) || skip "need >= 4 OSTs"
1385
1386         stack_trap "rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref"
1387
1388         # reference file
1389         $LFS setstripe -o 0 $DIR/$tfile-ref
1390         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1391
1392         # create a file with two mirrors
1393         $LFS setstripe -E -1 -o 0,1 -S 1M $DIR/$tfile
1394         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1395
1396         $LFS setstripe -E -1 -o 2,3 -S 1M $DIR/$tfile-2
1397         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1398
1399         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1400                 error "mirrored file size is not 3M"
1401
1402         # merge a mirrored file
1403         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1404                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1405
1406         cancel_lru_locks osc
1407
1408         # stop two OSTs, so the 2nd stripe of the 1st mirror and
1409         # the 1st stripe of the 2nd mirror will be inaccessible, ...
1410         stop_osts 2 3
1411
1412         echo "comparing files ... "
1413
1414         # however, read can still return the correct data. It should return
1415         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1416         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1417                 $DIR/$tfile-ref || error "file reading error"
1418
1419         start_osts 2 3
1420 }
1421 run_test 34a "read mirrored file with multiple stripes"
1422
1423 test_34b() {
1424         (( $OSTCOUNT >= 4 )) || skip "need >= 4 OSTs"
1425
1426         stack_trap "rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref"
1427
1428         # reference file
1429         $LFS setstripe -o 0 $DIR/$tfile-ref
1430         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1431
1432         $LFS setstripe -E 1M -S 1M -o 0 -E eof -o 1 $DIR/$tfile
1433         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1434
1435         $LFS setstripe -E 1M -S 1M -o 2 -E eof -o 3 $DIR/$tfile-2
1436         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1437
1438         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1439                 error "mirrored file size is not 3M"
1440
1441         # merge a mirrored file
1442         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1443                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1444
1445         cancel_lru_locks osc
1446
1447         # stop two OSTs, so the 2nd component of the 1st mirror and
1448         # the 1st component of the 2nd mirror will be inaccessible, ...
1449         stop_osts 2 3
1450
1451         echo "comparing files ... "
1452
1453         # however, read can still return the correct data. It should return
1454         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1455         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1456                 $DIR/$tfile-ref || error "file reading error"
1457
1458         start_osts 2 3
1459 }
1460 run_test 34b "read mirrored file with multiple components"
1461
1462 test_35() {
1463         local tf=$DIR/$tfile
1464
1465         $LFS setstripe -E eof $tf
1466
1467         # add an out-of-sync mirror to the file
1468         $LFS mirror extend -N -c 2 $tf ||
1469                 error "extending mirrored file $tf failed"
1470
1471         $MULTIOP $tf oO_WRONLY:c ||
1472                 error "write open a mirrored file failed"
1473
1474         # truncate file should return error
1475         $TRUNCATE $tf 100 || error "error truncating a mirrored file"
1476 }
1477 run_test 35 "allow to write to mirrored files"
1478
1479 get_file_layout_version() {
1480         $LFS getstripe $1 | awk '/lcm_layout_gen/{print $2}'
1481 }
1482
1483 get_ost_layout_version() {
1484         $MULTIOP $1 oXc | awk '/ostlayoutversion/{print $2}'
1485 }
1486
1487 verify_ost_layout_version() {
1488         local tf=$1
1489
1490         # get file layout version
1491         local flv=$(get_file_layout_version $tf)
1492
1493         # layout version from OST objects
1494         local olv=$(get_ost_layout_version $tf)
1495
1496         (( flv >= olv )) || error "layout version mismatch: $flv vs. $olv"
1497 }
1498
1499 create_file_36() {
1500         local tf
1501
1502         for tf in "$@"; do
1503                 $LFS setstripe -E 1M -S 1M -E 2M -E 4M -E eof -c -1 $tf
1504                 $LFS setstripe -E 3M -S 1M -E 6M -E eof -c -1 $tf-tmp
1505
1506                 $LFS mirror extend -N -f $tf-tmp $tf ||
1507                         error "merging $tf-tmp into $tf failed"
1508         done
1509 }
1510
1511 test_36a() {
1512         local tf=$DIR/$tfile
1513
1514         stack_trap "rm -f $tf $tf-2 $tf-3"
1515
1516         create_file_36 $tf $tf-2 $tf-3
1517
1518         [ $($LFS getstripe -N $tf) -gt 1 ] || error "wrong mirror count"
1519
1520         # test case 1 - check file write and verify layout version
1521         $MULTIOP $tf oO_WRONLY:c ||
1522                 error "write open a mirrored file failed"
1523
1524         # write open file should not return error
1525         $MULTIOP $tf oO_WRONLY:w1024Yc || error "write mirrored file error"
1526
1527         # instantiate components should work
1528         dd if=/dev/zero of=$tf bs=1M count=12 || error "write file error"
1529
1530         # verify OST layout version
1531         verify_ost_layout_version $tf
1532
1533         # test case 2
1534         local mds_facet=mds$(($($LFS getstripe -m $tf-2) + 1))
1535
1536         local delay_sec=10
1537         do_facet $mds_facet $LCTL set_param fail_val=$delay_sec
1538
1539         #define OBD_FAIL_FLR_LV_DELAY 0x1A01
1540         do_facet $mds_facet $LCTL set_param fail_loc=0x1A01
1541
1542         # write should take at least $fail_loc seconds and succeed
1543         local st=$(date +%s)
1544         $MULTIOP $tf-2 oO_WRONLY:w1024Yc || error "write mirrored file error"
1545
1546         # verify OST layout version
1547         verify_ost_layout_version $tf
1548
1549         do_facet $mds_facet $LCTL set_param fail_loc=0
1550 }
1551 run_test 36a "write to mirrored files"
1552
1553 test_36b() {
1554         local tf=$DIR/$tfile
1555
1556         (( OST1_VERSION >= $(version_code 2.15.50) )) ||
1557                 skip "Need OST version at least 2.15.50"
1558
1559         (( OSTCOUNT >= 2 )) || skip "need >= 2 OSTs"
1560
1561         # create 2 mirrors using different OSTs
1562         $LFS setstripe -N -c1 -i0 --flags=prefer -N -c1 -i1 $tf ||
1563                 error "create mirrored file"
1564
1565         # write 1M data to one mirror
1566         dd if=/dev/zero of=$tf bs=1M count=1 || error "write file error"
1567         sync
1568
1569         # set prefer mirror to another mirror
1570         $LFS setstripe --comp-set -I0x10001 --comp-flags=^prefer $tf ||
1571                 error "clear prefer mirror error"
1572         $LFS setstripe --comp-set -I0x20002 --comp-flags=prefer $tf ||
1573                 error "set prefer mirror error"
1574
1575         # the second write should not hung
1576         dd if=/dev/zero of=$tf bs=1M count=1 || error "write file error"
1577 }
1578 run_test 36b "write should not hung when prefered mirror is stale"
1579
1580 test_36c() {
1581         local tf=$DIR/$tfile
1582
1583         (( OST1_VERSION >= $(version_code 2.15.50) )) ||
1584                 skip "Need OST version at least 2.15.50"
1585
1586         (( OSTCOUNT >= 2 )) || skip "need >= 2 OSTs"
1587
1588         # create 2 mirrors using different OSTs
1589         $LFS setstripe -N -c1 -i0 --flags=prefer -N -c1 -i1 $tf ||
1590                 error "create mirrored file"
1591         stack_trap "rm -f $tf"
1592
1593         # write it in the background
1594         $MULTIOP $tf Ow4096_w4096c &
1595         local pid=$!
1596
1597         sleep 1
1598
1599         $LFS setstripe --comp-set -I0x10001 --comp-flags=^prefer $tf ||
1600                 error "clear prefer mirror error"
1601         $LFS setstripe --comp-set -I0x20002 --comp-flags=prefer $tf ||
1602                 error "set prefer mirror error"
1603
1604         kill -USR1 $pid
1605         wait $pid
1606 }
1607 run_test 36c "change prefer mirror during write shouldn't hung"
1608
1609 test_36d() {
1610         local tf=$DIR/$tfile
1611
1612         (( OST1_VERSION >= $(version_code 2.15.50) )) ||
1613                 skip "Need OST version at least 2.15.50"
1614
1615         echo " ** create $tf"
1616         $LFS mirror create -N $tf || error "create $tf failed"
1617
1618         for i in 1 2; do
1619                 echo " ** mirror extend $tf ($i/2)"
1620                 $LFS mirror extend -N $tf || error "mirror extend $tf failed"
1621                 flv=$(get_file_layout_version $tf)
1622                 olv=$(get_ost_layout_version $tf)
1623                 echo "    flv=$flv olv=$olv"
1624         done
1625
1626         for i in 1 2; do
1627                 echo " ** write $tf ($i/2)"
1628                 dd if=/dev/zero of=$tf bs=1k count=1 || error "write $tf failed"
1629                 flv=$(get_file_layout_version $tf)
1630                 olv=$(get_ost_layout_version $tf)
1631                 echo "    flv=$flv olv=$olv"
1632                 (( flv == olv )) ||
1633                         error "write update OST layout failed $flv/$olv"
1634         done
1635
1636         echo " ** resync $tf"
1637         $LFS mirror resync $tf || error "mirror resync $tf failed"
1638         flv=$(get_file_layout_version $tf)
1639         olv=$(get_ost_layout_version $tf)
1640         echo "    flv=$flv olv=$olv"
1641
1642         for i in 1 2; do
1643                 echo " ** truncate $tf ($i/2)"
1644                 $TRUNCATE $tf $((1024 * 1024)) || error "truncate $tf fails"
1645                 flv=$(get_file_layout_version $tf)
1646                 olv=$(get_ost_layout_version $tf)
1647                 echo "    flv=$flv olv=$olv"
1648                 (( flv == olv || flv == olv + 1 )) ||
1649                         error "truncate update OST layout failed $flv/$olv"
1650         done
1651
1652         echo " ** resync $tf"
1653         $LFS mirror resync $tf || error "mirror resync $tf failed"
1654         flv=$(get_file_layout_version $tf)
1655         olv=$(get_ost_layout_version $tf)
1656         echo "    flv=$flv olv=$olv"
1657
1658         for i in 1 2; do
1659                 echo " ** write $tf ($i/2)"
1660                 dd if=/dev/zero of=$tf bs=1k count=1 || error "write $tf failed"
1661                 flv=$(get_file_layout_version $tf)
1662                 olv=$(get_ost_layout_version $tf)
1663                 echo "    flv=$flv olv=$olv"
1664                 (( flv == olv )) ||
1665                         error "write update OST layout failed $flv/$olv"
1666         done
1667 }
1668 run_test 36d "write/punch FLR file update OST layout version"
1669
1670 create_files_37() {
1671         local tf
1672         local fsize=$1
1673
1674         echo "create test files with size $fsize .."
1675
1676         shift
1677         for tf in "$@"; do
1678                 $LFS setstripe -E 1M -S 1M -c 1 -E eof -c -1 $tf
1679
1680                 dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
1681                 $TRUNCATE $tf $fsize
1682         done
1683 }
1684
1685 test_37()
1686 {
1687         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
1688                 skip "Need MDS version at least 2.11.57"
1689
1690         local tf=$DIR/$tfile
1691         local tf2=$DIR/$tfile-2
1692         local tf3=$DIR/$tfile-3
1693         local tf4=$DIR/$tfile-4
1694
1695         stack_trap "rm -f $tf $tf2 $tf3 $tf4"
1696
1697         create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
1698         cp $tf $tf4
1699
1700         # assume the mirror id will be 1, 2, and 3
1701         declare -A checksums
1702         checksums[1]=$(cat $tf | md5sum)
1703         checksums[2]=$(cat $tf2 | md5sum)
1704         checksums[3]=$(cat $tf3 | md5sum)
1705
1706         printf '%s\n' "${checksums[@]}"
1707
1708         # merge these files into a mirrored file
1709         $LFS mirror extend --no-verify -N -f $tf2 $tf ||
1710                 error "merging $tf2 into $tf failed"
1711         $LFS mirror extend --no-verify -N -f $tf3 $tf ||
1712                 error "merging $tf3 into $tf failed"
1713
1714         get_mirror_ids $tf
1715
1716         # verify mirror read, checksums should equal to the original files'
1717         echo "Verifying mirror read .."
1718
1719         local sum
1720         for i in "${mirror_array[@]}"; do
1721                 $LCTL set_param ldlm.namespaces.*.lru_size=clear > /dev/null
1722                 sum=$($LFS mirror read -N $i $tf | md5sum)
1723                 [ "$sum" = "${checksums[$i]}" ] ||
1724                         error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
1725         done
1726
1727         # verify mirror write
1728         echo "Verifying mirror write .."
1729         $LFS mirror write -N2 $tf < $tf4
1730
1731         sum=$($LFS mirror read -N2 $tf | md5sum)
1732         [[ "$sum" = "${checksums[1]}" ]] ||
1733                 error "2: mismatch \'${checksums[1]}\' vs. \'$sum\'"
1734
1735         # verify mirror copy, write to this mirrored file will invalidate
1736         # the other two mirrors
1737         echo "Verifying mirror copy .."
1738
1739         local osts=$(comma_list $(osts_nodes))
1740
1741         $LFS mirror copy -i ${mirror_array[0]} -o-1 $tf ||
1742                 error "mirror copy error"
1743
1744         # verify copying is successful by checking checksums
1745         remount_client $MOUNT
1746         for i in "${mirror_array[@]}"; do
1747                 sum=$($LFS mirror read -N $i $tf | md5sum)
1748                 [ "$sum" = "${checksums[1]}" ] ||
1749                         error "$i: mismatch checksum after copy \'$sum\'"
1750         done
1751
1752         rm -f $tf
1753 }
1754 run_test 37 "mirror I/O API verification"
1755
1756 test_38() {
1757         local tf=$DIR/$tfile
1758         local ref=$DIR/${tfile}-ref
1759
1760         stack_trap "rm -f $tf $ref"
1761
1762         $LFS setstripe -E 1M -S 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf ||
1763                 error "creating $tf failed"
1764         $LFS setstripe -E 2M -S 1M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 \
1765                 $tf-2 || error "creating $tf-2 failed"
1766         $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3 ||
1767                 error "creating $tf-3 failed"
1768
1769         # instantiate all components
1770         $LFS mirror extend -N -f $tf-2 $tf ||
1771                 error "merging $tf-2 into $tf failed"
1772         $LFS mirror extend -N -f $tf-3 $tf ||
1773                 error "merging $tf-3 into $tf failed"
1774         $LFS mirror extend -N -c 1 $tf ||
1775                 error "extending mirrored file $tf failed"
1776
1777         verify_flr_state $tf "ro"
1778
1779         dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
1780
1781         local fsize=$((RANDOM << 8 + 1048576))
1782         $TRUNCATE $ref $fsize
1783
1784         local ref_cksum=$(cat $ref | md5sum)
1785
1786         # case 1: verify write to mirrored file & resync work
1787         cp $ref $tf || error "copy from $ref to $f error"
1788         verify_flr_state $tf "wp"
1789
1790         local file_cksum=$(cat $tf | md5sum)
1791         [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
1792
1793         get_mirror_ids $tf
1794         echo "mirror IDs: ${mirror_array[*]}"
1795
1796         local valid_mirror stale_mirror id mirror_cksum
1797         for id in "${mirror_array[@]}"; do
1798                 mirror_cksum=$($LFS mirror read -N $id $tf | md5sum)
1799                 [ "$ref_cksum" == "$mirror_cksum" ] &&
1800                         { valid_mirror=$id; continue; }
1801
1802                 stale_mirror=$id
1803         done
1804
1805         [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
1806         [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
1807
1808         $LFS mirror resync $tf || error "resync failed"
1809         verify_flr_state $tf "ro"
1810
1811         mirror_cksum=$($LFS mirror read -N $stale_mirror $tf | md5sum)
1812         [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
1813
1814         # case 2: inject an error to make mirror_io exit after changing
1815         # the file state to sync_pending so that we can start a concurrent
1816         # write.
1817         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1818         verify_flr_state $tf "wp"
1819
1820         mirror_io resync -e resync_start $tf && error "resync succeeded"
1821         verify_flr_state $tf "sp"
1822
1823         # from sync_pending to write_pending
1824         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1825         verify_flr_state $tf "wp"
1826
1827         mirror_io resync -e resync_start $tf && error "resync succeeded"
1828         verify_flr_state $tf "sp"
1829
1830         # from sync_pending to read_only
1831         $LFS mirror resync $tf || error "resync failed"
1832         verify_flr_state $tf "ro"
1833 }
1834 run_test 38 "resync"
1835
1836 test_39() {
1837         local tf=$DIR/$tfile
1838
1839         rm -f $tf
1840         $LFS mirror create -N2 -E1m -c1 -S1M -E-1 $tf ||
1841         error "create PFL file $tf failed"
1842
1843         verify_mirror_count $tf 2
1844         verify_comp_count $tf 4
1845
1846         rm -f $tf || error "delete $tf failed"
1847 }
1848 run_test 39 "check FLR+PFL (a.k.a. PFLR) creation"
1849
1850 test_40() {
1851         local tf=$DIR/$tfile
1852         local ops
1853
1854         for ops in "conv=notrunc" ""; do
1855                 rm -f $tf
1856
1857                 $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 --flags=prefer \
1858                                    -N -E 1M -E 2M -E 4M -E -1 $tf ||
1859                         error "create PFLR file $tf failed"
1860                 dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
1861                         error "write PFLR file $tf failed"
1862
1863                 lfs getstripe -vy $tf
1864
1865                 local flags
1866
1867                 # file mirror state should be write_pending
1868                 flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1869                 [ $flags = wp ] ||
1870                 error "file mirror state $flags"
1871                 # the 1st component (in mirror 1) should be inited
1872                 verify_comp_attr lcme_flags $tf 0x10001 init
1873                 # the 2nd component (in mirror 1) should be inited
1874                 verify_comp_attr lcme_flags $tf 0x10002 init
1875                 # the 3rd component (in mirror 1) should be uninited
1876                 verify_comp_attr lcme_flags $tf 0x10003 prefer
1877                 # the 4th component (in mirror 2) should be inited
1878                 verify_comp_attr lcme_flags $tf 0x20004 init
1879                 # the 5th component (in mirror 2) should be uninited
1880                 verify_comp_attr lcme_flags $tf 0x20005 0
1881                 # the 6th component (in mirror 2) should be stale
1882                 verify_comp_attr lcme_flags $tf 0x20006 stale
1883                 # the 7th component (in mirror 2) should be uninited
1884                 if [[ x$ops = "xconv=notrunc" ]]; then
1885                         verify_comp_attr lcme_flags $tf 0x20007 0
1886                 elif [[ x$ops = "x" ]]; then
1887                         verify_comp_attr lcme_flags $tf 0x20007 stale
1888                 fi
1889         done
1890
1891         rm -f $tf || error "delete $tf failed"
1892 }
1893 run_test 40 "PFLR rdonly state instantiation check"
1894
1895 test_41() {
1896         local tf=$DIR/$tfile
1897
1898         stack_trap "rm -f $tf $tf-1"
1899
1900         rm -f $tf $tf-1
1901         echo " **create two FLR files $tf $tf-1"
1902         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1903                            -N -E 1M -E 2M -E 3M -E -1 $tf ||
1904                 error "create PFLR file $tf failed"
1905         $LFS mirror create -N -E 2M -S 1M -E eof \
1906                            -N -E 1M -E eof --flags prefer \
1907                            -N -E 4m -E eof $tf-1 ||
1908                 error "create PFLR file $tf-1 failed"
1909
1910         # file should be in ro status
1911         echo " **verify files be RDONLY"
1912         verify_flr_state $tf "ro"
1913         verify_flr_state $tf-1 "ro"
1914
1915         # write data in [0, 2M)
1916         dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
1917                 error "writing $tf failed"
1918         dd if=/dev/urandom of=$tf-1 bs=1M count=4 conv=notrunc ||
1919                 error "writing $tf-1 failed"
1920
1921         local sum0=$(cat $tf-1 | md5sum)
1922
1923         echo " **verify files be WRITE_PENDING"
1924         verify_flr_state $tf "wp"
1925         verify_flr_state $tf-1 "wp"
1926
1927         # file should have stale component
1928         echo " **verify files have stale component"
1929         $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
1930                 error "after writing $tf, it does not contain stale component"
1931         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1932                 error "after writing $tf-1, it does not contain stale component"
1933
1934         echo " **full resync"
1935         $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
1936
1937         echo " **verify $tf-1 data consistency in all mirrors"
1938         for i in 1 2 3; do
1939                 local sum=$($LFS mirror read -N$i $tf-1 | md5sum)
1940                 [[ "$sum" = "$sum0" ]] ||
1941                         error "$tf-1.$i: checksum mismatch: $sum != $sum0"
1942         done
1943
1944         echo " **verify files be RDONLY"
1945         verify_flr_state $tf "ro"
1946         verify_flr_state $tf-1 "ro"
1947
1948         # file should not have stale component
1949         echo " **verify files do not contain stale component"
1950         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1951                 error "after resyncing $tf, it contains stale component"
1952         $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
1953                 error "after resyncing $tf, it contains stale component"
1954
1955         # verify partial resync
1956         echo " **write $tf-1 for partial resync test"
1957         dd if=/dev/zero of=$tf-1 bs=1M count=2 conv=notrunc ||
1958                 error "writing $tf-1 failed"
1959
1960         echo " **only resync mirror 2"
1961         verify_flr_state $tf-1 "wp"
1962         $LFS mirror resync --only 2 $tf-1 ||
1963                 error "resync mirror 2 of $tf-1 failed"
1964         verify_flr_state $tf "ro"
1965
1966         # resync synced mirror
1967         echo " **resync mirror 2 again"
1968         $LFS mirror resync --only 2 $tf-1 ||
1969                 error "resync mirror 2 of $tf-1 failed"
1970         verify_flr_state $tf "ro"
1971         echo " **verify $tf-1 contains stale component"
1972         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1973                 error "after writing $tf-1, it does not contain stale component"
1974
1975         echo " **full resync $tf-1"
1976         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1977         verify_flr_state $tf "ro"
1978         echo " **full resync $tf-1 again"
1979         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1980         echo " **verify $tf-1 does not contain stale component"
1981         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1982                 error "after resyncing $tf, it contains stale component"
1983
1984         return 0
1985 }
1986 run_test 41 "lfs mirror resync check"
1987
1988 test_42() {
1989         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1990
1991         local td=$DIR/$tdir
1992         local tf=$td/$tfile
1993         local mirror_cmd="$LFS mirror verify"
1994         local i
1995
1996         stack_trap "rm -rf $td"
1997
1998         # create parent directory
1999         mkdir $td || error "mkdir $td failed"
2000
2001         $mirror_cmd &> /dev/null && error "no file name given"
2002         $mirror_cmd $tf &> /dev/null && error "cannot stat file $tf"
2003         $mirror_cmd $td &> /dev/null && error "$td is not a regular file"
2004
2005         # create mirrored files
2006         $LFS mirror create -N -E 4M -S 1M -E 10M -E EOF $tf ||
2007                 error "create mirrored file $tf failed"
2008         $LFS mirror create -N -E 2M -S 1M -E EOF \
2009                            -N -E 6M -E 8M -E EOF \
2010                            -N -E 16M -E EOF $tf-1 ||
2011                 error "create mirrored file $tf-1 failed"
2012         $LFS mirror create -N -c 2 -o 1,3 -N -S 2M -c -1 $tf-2 ||
2013                 error "create mirrored file $tf-2 failed"
2014
2015         # write data in [0, 10M)
2016         for i in $tf $tf-1 $tf-2; do
2017                 yes | dd of=$i bs=1M count=10 iflag=fullblock conv=notrunc ||
2018                         error "write $i failed"
2019         done
2020
2021         # resync the mirrored files
2022         $LFS mirror resync $tf-1 $tf-2 ||
2023                 error "resync $tf-1 $tf-2 failed"
2024
2025         # verify the mirrored files
2026         $mirror_cmd $tf-1 $tf-2 ||
2027                 error "verify $tf-1 $tf-2 failed"
2028
2029         get_mirror_ids $tf-1
2030         $mirror_cmd --only ${mirror_array[0]} $tf-1 &> /dev/null &&
2031                 error "at least 2 mirror ids needed with '--only' option"
2032         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 $tf-2 \
2033                 &> /dev/null &&
2034                 error "'--only' option cannot be used upon multiple files"
2035         $mirror_cmd --only 65534,${mirror_array[0]},65535 $tf-1 &&
2036                 error "invalid specified mirror ids"
2037
2038         # change the content of $tf and merge it into $tf-1
2039         for i in 6 10; do
2040                 echo a | dd of=$tf bs=1M seek=$i conv=notrunc ||
2041                         error "change $tf with seek=$i failed"
2042                 echo b | dd of=$tf-1 bs=1M seek=$i conv=notrunc ||
2043                         error "change $tf-1 with seek=$i failed"
2044         done
2045
2046         $LFS mirror resync $tf-1 || error "resync $tf-1 failed"
2047         $LFS mirror extend --no-verify -N -f $tf $tf-1 ||
2048                 error "merge $tf into $tf-1 failed"
2049
2050         # verify the mirrored files
2051         echo "Verify $tf-1 without -v option:"
2052         $mirror_cmd $tf-1 &&
2053                 error "verify $tf-1 should fail" || echo "PASS"
2054
2055         echo "Verify $tf-1 with -v option:"
2056         $mirror_cmd -v $tf-1 &&
2057                 error "verify $tf-1 should fail"
2058
2059         get_mirror_ids $tf-1
2060         echo "Verify $tf-1 with --only option:"
2061         $mirror_cmd -v --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 &&
2062                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
2063                       "${mirror_array[-1]} should fail"
2064
2065         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 ||
2066                 error "verify $tf-1 with mirror ${mirror_array[0]} and" \
2067                       "${mirror_array[1]} should succeed"
2068
2069         # set stale components in $tf-1
2070         for i in 0x40002 0x40003; do
2071                 $LFS setstripe --comp-set -I$i --comp-flags=stale $tf-1 ||
2072                         error "set stale flag on component $i failed"
2073         done
2074
2075         # verify the mirrored file
2076         echo "Verify $tf-1 with stale components:"
2077         $mirror_cmd -vvv $tf-1 ||
2078                 error "verify $tf-1 with stale components should succeed"
2079
2080         echo "Verify $tf-1 with stale components and --only option:"
2081         $mirror_cmd -vvv --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 ||
2082                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
2083                       "${mirror_array[-1]} should succeed"
2084 }
2085 run_test 42 "lfs mirror verify"
2086
2087 # inactivate one OST && write && restore the OST
2088 write_file_43() {
2089         local file=$1
2090         local ost=$2
2091         local PARAM="osp.${FSNAME}-OST000${ost}-osc-M*.active"
2092         local wait
2093
2094         wait=$(do_facet $SINGLEMDS \
2095                 "$LCTL get_param -n lod.*MDT0000-*.qos_maxage")
2096         wait=${wait%%[^0-9]*}
2097
2098         echo "  **deactivate OST$ost, waiting for $((wait*2+2)) seconds"
2099         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 0")
2100         # lod_qos_statfs_update needs 2*$wait seconds to refresh targets statfs
2101         sleep $(($wait * 2 + 2))
2102         echo "  **write $file"
2103         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
2104         echo "  **restore activating OST$ost, waiting for $((wait*2+2)) seconds"
2105         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 1")
2106         sleep $((wait * 2 + 2))
2107
2108         local flags=$($LFS getstripe -v $file | awk '/lcm_flags:/ { print $2 }')
2109         [ $flags = wp ] || error "file mirror state $flags != wp"
2110 }
2111
2112 test_43a() {
2113         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
2114
2115         local tf=$DIR/$tfile
2116         local flags
2117
2118         stack_trap "rm -f $tf"
2119
2120         ##   mirror 0  ost (0, 1)
2121         ##   mirror 1  ost (1, 2)
2122         ##   mirror 2  ost (2, 0)
2123         $LFS mirror create -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 \
2124                 -N -Eeof -c2 -o2,0 $tf ||
2125                 error "create 3 mirrors file $tf failed"
2126
2127         ################## OST0 ###########################################
2128         write_file_43 $tf 0
2129         echo "  **verify components"
2130         verify_comp_attr lcme_flags $tf 0x10001 init,stale
2131         verify_comp_attr lcme_flags $tf 0x20002 init
2132         verify_comp_attr lcme_flags $tf 0x30003 init,stale
2133
2134         # resync
2135         echo "  **resync $tf"
2136         $LFS mirror resync $tf
2137         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
2138         [ $flags = ro ] || error "file mirror state $flags != ro"
2139
2140         ################## OST1 ###########################################
2141         write_file_43 $tf 1
2142         echo "  **verify components"
2143         verify_comp_attr lcme_flags $tf 0x10001 init,stale
2144         verify_comp_attr lcme_flags $tf 0x20002 init,stale
2145         verify_comp_attr lcme_flags $tf 0x30003 init
2146
2147         # resync
2148         echo "  **resync $tf"
2149         $LFS mirror resync $tf
2150         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
2151         [ $flags = ro ] || error "file mirror state $flags != ro"
2152
2153         ################## OST2 ###########################################
2154         write_file_43 $tf 2
2155         echo "  **verify components"
2156         verify_comp_attr lcme_flags $tf 0x10001 init
2157         verify_comp_attr lcme_flags $tf 0x20002 init,stale
2158         verify_comp_attr lcme_flags $tf 0x30003 init,stale
2159 }
2160 run_test 43a "mirror pick on write"
2161
2162 test_43b() {
2163         local tf=$DIR/$tdir/$tfile
2164
2165         test_mkdir $DIR/$tdir
2166         rm -f $tf
2167         stack_trap "rm -rf $tf"
2168
2169         # create 3 mirrors FLR file, the first 2 mirrors are preferred
2170         $LFS setstripe -N -Eeof --flags=prefer -N -Eeof --flags=prefer \
2171                 -N -Eeof $tf || error "create 3 mirrors file $tf failed"
2172         verify_flr_state $tf "ro"
2173
2174         echo " ** write to $tf"
2175         dd if=/dev/zero of=$tf bs=1M count=1 || error "write $tf failed"
2176         verify_flr_state $tf "wp"
2177
2178         echo " ** resync $tf"
2179         $LFS mirror resync $tf || error "resync $tf failed"
2180         verify_flr_state $tf "ro"
2181 }
2182 run_test 43b "allow writing to multiple preferred mirror file"
2183
2184 test_44a() {
2185         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2186         rm -rf $DIR/$tdir
2187         rm -rf $DIR/$tdir-1
2188         local tf=$DIR/$tdir/$tfile
2189         local tf1=$DIR/$tdir-1/$tfile-1
2190
2191         stack_trap "rm -rf $tf $tf1"
2192
2193         $LFS setdirstripe -i 0 -c 1 $DIR/$tdir ||
2194                 error "create directory failed"
2195         $LFS setdirstripe -i 1 -c 1 $DIR/$tdir-1 ||
2196                 error "create remote directory failed"
2197         rm -f $tf $tf1 $tf.mirror~2
2198         # create file with 4 mirrors
2199         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
2200                            -N -E 1M -E 2M -E 3M -E -1 -N2 $tf ||
2201                 error "create PFLR file $tf failed"
2202
2203         # file should be in ro status
2204         verify_flr_state $tf "ro"
2205
2206         # write data in [0, 3M)
2207         dd if=/dev/urandom of=$tf bs=1M count=3 conv=notrunc ||
2208                 error "writing $tf failed"
2209
2210         verify_flr_state $tf "wp"
2211
2212         # disallow destroying the last non-stale mirror
2213         ! $LFS mirror delete --mirror-id 1 $tf > /dev/null 2>&1 ||
2214                 error "destroying mirror 1 should fail"
2215
2216         # synchronize all mirrors of the file
2217         $LFS mirror resync $tf || error "mirror resync $tf failed"
2218
2219         verify_flr_state $tf "ro"
2220
2221         # split mirror 1
2222         $LFS mirror split --mirror-id 1 -f $tf1 $tf ||
2223                 error "split to $tf1 failed"
2224
2225         local idx0=$($LFS getstripe -m $tf)
2226         local idx1=$($LFS getstripe -m $tf1)
2227
2228         [[ x$idx0 == x0 ]] || error "$tf is not on MDT0"
2229         [[ x$idx1 == x1 ]] || error "$tf1 is not on MDT1"
2230
2231         # verify mirror count
2232         verify_mirror_count $tf 3
2233         verify_mirror_count $tf1 1
2234
2235         $LFS mirror split --mirror-id 2 $tf ||
2236                 error "split mirror 2 failed"
2237
2238         verify_mirror_count $tf 2
2239         verify_mirror_count $tf.mirror~2 1
2240
2241         $LFS setstripe --comp-set -I 0x30008 --comp-flags=stale $tf ||
2242                 error "setting stale flag on component 0x30008 failed"
2243
2244         # disallow destroying the last non-stale mirror
2245         ! $LFS mirror split --mirror-id 4 -d $tf > /dev/null 2>&1 ||
2246                 error "destroying mirror 4 should fail"
2247
2248         $LFS mirror resync $tf || error "resynchronizing $tf failed"
2249
2250         $LFS mirror split --mirror-id 3 -d $tf ||
2251                 error "destroying mirror 3 failed"
2252         verify_mirror_count $tf 1
2253
2254         # verify splitted file contains the same content as the orig file does
2255         diff $tf $tf1 || error "splited file $tf1 diffs from $tf"
2256         diff $tf $tf.mirror~2 ||
2257                 error "splited file $tf.mirror~2 diffs from $tf"
2258 }
2259 run_test 44a "lfs mirror split check"
2260
2261 test_44b() {
2262         (( $MDS1_VERSION >= $(version_code 2.14.56) )) ||
2263                 skip "Need MDS version at least 2.14.56"
2264
2265         rm -rf $DIR/$tdir
2266         local tf=$DIR/$tdir/$tfile
2267
2268         mkdir -p $DIR/$tdir || error "create directory failed"
2269
2270         echo XXX > $tf
2271
2272         # create 2 mirrors file
2273         $LFS mirror extend -N -c1 $tf
2274
2275         echo YYY > $tf
2276
2277         verify_flr_state $tf "wp"
2278
2279         local str=$(cat $tf)
2280
2281         [[ $str == "YYY" ]] || error "$tf content is not YYY"
2282
2283         # get the non-stale mirror id
2284         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
2285                         tr '\n' ' '))
2286         local mirror_ids=($($LFS getstripe $tf |
2287                         awk '/lcme_mirror_id/{print $2}' | tr '\n' ' '))
2288         for ((i = 0; i < 2; i++)); do
2289                 $LFS getstripe -I${ids[$i]} --component-flags $tf |
2290                         grep stale > /dev/null || break
2291         done
2292
2293         [[ $i -ge 2 ]] && ( $LFS getstripe $tf; error "no stale mirror" )
2294
2295         $LFS getstripe $tf
2296
2297         # split the updated mirror, should fail
2298         echo "split mirror_id ${mirror_ids[$i]} id ${ids[$i]}, should fail"
2299         $LFS mirror split --mirror-id=${mirror_ids[$i]} $tf &> /dev/null &&
2300                 error "split --mirror-id=${mirror_ids[$i]} $tf should fail"
2301
2302         i=$(( 1 - i ))
2303         # split the stale mirror
2304         echo "split mirror_id ${mirror_ids[$i]} id ${ids[$i]}"
2305         $LFS mirror split --mirror-id=${mirror_ids[$i]} -d $tf ||
2306                 error "mirror split --mirror-id=${mirror_ids[$i]} $tf failed"
2307
2308         echo "make sure there's no stale comp in the file"
2309         # make sure there's no stale comp in the file
2310         $LFS getstripe $tf | awk '/lcme_flags/{print $2}' | grep stale &&
2311                 ( $LFS getstripe $tf; error "stale mirror file" )
2312
2313         str=$(cat $tf)
2314         [[ $str == "YYY" ]] ||
2315                 ( cat $tf; error "$tf content is not YYY after split" )
2316 }
2317 run_test 44b "mirror split does not create stale file"
2318
2319 test_44c() {
2320         local tf=$DIR/$tdir/$tfile
2321
2322         stack_trap "rm -f $tf"
2323
2324         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
2325                 skip "Need MDS version at least 2.14.52"
2326
2327         [ "$FSTYPE" != "zfs" ] || skip "ZFS file's block number is not accurate"
2328
2329         mkdir -p $DIR/$tdir || error "create directroy failed"
2330
2331         dd if=/dev/zero of=$tf bs=1M count=10 || error "dd write $tfile failed"
2332         sync
2333         block1=$(( $(stat -c "%b*%B" $tf) ))
2334         echo " ** before mirror ops, file blocks=$((block1/1024)) KiB"
2335
2336         $LFS mirror extend -N2 -c1 $tf || error "mirror extend $tfile failed"
2337         sync
2338         block2=$(( $(stat -c "%b*%B" $tf) ))
2339         echo " ** after mirror extend, file blocks=$((block2/1024)) KiB"
2340
2341         $LFS mirror split -d --mirror-id=2 $tf ||
2342                 error "mirror split $tfile failed"
2343         $LFS mirror split -d --mirror-id=3 $tf ||
2344                 error "mirror split $tfile failed"
2345         sync
2346         lfs getsom $tf
2347         block3=$(( $(stat -c "%b*%B" $tf) ))
2348         echo " ** after mirror split, file blocks=$((block3/1024)) KiB"
2349
2350         [[ $block1 -eq $block3 ]] ||
2351                 error "mirror split does not reduce block# $block3 != $block1"
2352 }
2353 run_test 44c "lfs mirror split reduces block size of a file"
2354
2355 test_44d() {
2356         local tf=$DIR/$tdir/$tfile
2357         local size1
2358         local size2
2359         local size3
2360         local size4
2361
2362         stack_trap "rm -f $tf"
2363
2364         mkdir -p $DIR/$tdir || error "create directroy failed"
2365
2366         dd if=/dev/zero of=$tf bs=1M count=10 || error "dd write $tfile failed"
2367         sync
2368         size1=$(stat -c "%s" $tf)
2369         echo " ** before mirror ops, file size=$size1"
2370
2371         $LFS mirror extend -N2 -c1 $tf || error "mirror extend $tfile failed"
2372         sync
2373         size2=$(stat -c "%s" $tf)
2374         echo " ** after mirror extend, file size=$size2"
2375
2376         (($size1 == $size2)) ||
2377                 error "mirror extend should not change size, before: $size1, after $size2"
2378
2379         $LFS mirror split -d --mirror-id=2 $tf ||
2380                 error "mirror split $tfile failed"
2381
2382         size2=$(stat -c "%s" $tf)
2383         echo " ** after mirror split, file size=$size2"
2384         (($size1 == $size2)) ||
2385                 error "mirror split should not change size, before: $size1, after $size2"
2386
2387         # Remount client to clear cached size information
2388         remount_client $MOUNT
2389         size2=$(stat -c "%s" $tf)
2390         echo " ** after mirror split & remount, file size=$size2"
2391         (($size1 == $size2)) ||
2392                 error "mirror extend should not change size, before: $size1, after $size2"
2393
2394 }
2395 run_test 44d "lfs mirror split does not break size"
2396
2397 test_44e() {
2398         local tf=$DIR/$tdir/$tfile
2399         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
2400         local size1
2401         local size2
2402
2403         test_mkdir $DIR/$tdir
2404         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
2405                 skip "Need MDS version at least 2.14.52"
2406
2407         $LFS mirror create -N2 $tf || error "create mirrored file $tf failed"
2408
2409         # Disable xattr caching so we can repeatedly check SOM with lfs getsom
2410         $LCTL set_param llite.*.xattr_cache=0
2411         stack_trap "$LCTL set_param llite.*.xattr_cache=1"
2412         stack_trap "rm -rf $tf"
2413
2414         dd if=/dev/zero of=$tf bs=1M count=10 || error "dd write $tfile failed"
2415         sync
2416         size1=$(stat -c "%s" $tf)
2417         echo " ** before mirror resync, file size=$size1"
2418
2419         $LFS mirror resync $tf || error "mirror resync file $tf failed"
2420         size1=$(stat -c "%s" $tf)
2421         size2=$($LFS getsom -s $tf)
2422
2423         $LFS getsom $tf
2424
2425         ((size1 == size2)) ||
2426                 error "mirrored file with strict SOM $size1 != disabled SOM $size2"
2427
2428         # Remount client to clear cached size information
2429         remount_client $MOUNT
2430
2431         save_lustre_params $(get_facets MDS) mdt.*MDT*.enable_strict_som > $p
2432         stack_trap "restore_lustre_params < $p; rm -f $p"
2433         local mds_facet=mds$(($($LFS getstripe -m $tf) + 1))
2434
2435         do_facet $mds_facet $LCTL set_param mdt.*MDT*.enable_strict_som=0
2436
2437         size2=$(stat -c "%s" $tf)
2438         # 'getsom' here is just for debugging
2439         $LFS getsom $tf
2440
2441         ((size2 == size1)) ||
2442                 error "mirrored file in sync with som disabled, size with som disabled ($size2) and without som disabled ($size1) should agree"
2443 }
2444 run_test 44e "basic FLR SOM tests + disable SOM"
2445
2446 test_45() {
2447         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
2448
2449         local file=$DIR/$tdir/$tfile
2450         local dir=$DIR/$tdir/$dir
2451         local temp=$DIR/$tdir/template
2452         rm -rf $DIR/$tdir
2453         test_mkdir $DIR/$tdir
2454
2455         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
2456                 -N -E3m -S1m -Eeof -N -E8m -Eeof $file ||
2457                         error "Create $file failed"
2458
2459         verify_yaml_layout $file $file.copy $temp "1. FLR file"
2460         rm -f $file $file.copy
2461
2462         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
2463                 -N -E3m -S1m -Eeof -N -E8m --flags=prefer -Eeof $file ||
2464                         error "Create $file failed"
2465
2466         verify_yaml_layout $file $file.copy $temp "2. FLR file with flags"
2467 }
2468 run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
2469
2470 verify_46() {
2471         local src=$1
2472         local dst=$2
2473         local msg_prefix=$3
2474
2475         $LFS setstripe --copy=$src $dst || error "setstripe $dst failed"
2476
2477         local layout1=$(get_layout_param $src)
2478         local layout2=$(get_layout_param $dst)
2479         # compare their layout info
2480         [ "$layout1" == "$layout2" ] ||
2481                 error "$msg_prefix $src <=> $dst layouts are not equal"
2482 }
2483
2484 test_46() {
2485         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2486
2487         local file=$DIR/$tdir/$tfile
2488         test_mkdir $DIR/$tdir
2489
2490         ########################### 1. PFL file #############################
2491         echo "  ** 1. PFL file"
2492         rm -f $file
2493         $LFS setstripe -E1m -S 1M -c2 -o0,1 -E2m -c2 -E3m -o1,0 -E4m -c1 -E-1 \
2494                 $file || error "1. Create PFL $file failed"
2495
2496         rm -f $file.copy
2497         verify_46 $file $file.copy "1. PFL file"
2498
2499         ########################### 2. plain file ###########################
2500         echo "  ** 2. plain file"
2501         rm -f $file
2502         $LFS setstripe -c2 -o0,1 -i1 $file ||
2503                 error "2. Create plain $file failed"
2504
2505         rm -f $file.copy
2506         verify_46 $file $file.copy "2. plain file"
2507
2508         ########################### 3. FLR file #############################
2509         echo "  ** 3. FLR file"
2510         rm -f $file
2511         $LFS setstripe -N -E1m -S 1M -c2 -o0,1 -E4m -c1 -Eeof -N -E16m -Eeof \
2512                 $file || error "3. Create FLR $file failed"
2513
2514         rm -f $file.copy
2515         verify_46 $file $file.copy "3. FLR file"
2516
2517         local dir=$DIR/$tdir/dir
2518         ########################### 4. PFL dir ##############################
2519         echo "  ** 4. PFL dir"
2520         test_mkdir $dir
2521         $LFS setstripe -E1m -S 1M -c2 -E2m -c1 -E-1 $dir ||
2522                 error "4. setstripe PFL $dir failed"
2523
2524         test_mkdir $dir.copy
2525         verify_46 $dir $dir.copy "4. PFL dir"
2526
2527         ########################### 5. plain dir ############################
2528         echo "  ** 5. plain dir"
2529         $LFS setstripe -c2 -i-1 $dir || error "5. setstripe plain $dir failed"
2530
2531         verify_46 $dir $dir.copy "5. plain dir"
2532
2533         ########################### 6. FLR dir ##############################
2534         echo "  ** 6. FLR dir"
2535         $LFS setstripe -N -E1m -S 1M -c2 -E2m -c1 -Eeof -N -E4m -Eeof $dir ||
2536                 error "6. setstripe FLR $dir failed"
2537
2538         verify_46 $dir $dir.copy "6. FLR dir"
2539
2540         (( $MDS1_VERSION >= $(version_code 2.13.53.205) )) ||
2541                 echo "server version $MDS1_VERSION does not support SEL" &&
2542                 return 0
2543
2544         ########################### 7. SEL file ##############################
2545         echo "  ** 7. SEL file"
2546         rm -f $file
2547         $LFS setstripe -E256M -S 1M -c2 -o0,1 -z 64M -E-1 -o1,0 -z 128M \
2548                 $file || error "Create $file failed"
2549
2550         rm -f $file.copy
2551         verify_46 $file $file.copy "7. SEL file"
2552
2553         ########################### 8. SEL dir ##############################
2554         echo "  ** 8. SEL dir"
2555         $LFS setstripe -E256M -S 1M -c2 -z 64M -E-1 -z 128M \
2556                 $dir || error "setstripe $dir failed"
2557
2558         verify_46 $dir $dir.copy "8. SEL dir"
2559
2560         ########################### 9. FLR SEL file ##########################
2561         echo "  ** 9. FLR SEL file"
2562         rm -f $file
2563         $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
2564                 -N -E1G -c4 -z128M -E-1 -z256M $file || error "Create $file failed"
2565
2566         rm -f $file.copy
2567         verify_46 $file $file.copy "9. SEL file"
2568
2569         ########################### 10. FLR SEL dir #########################
2570         echo "  ** 10. FLR SEL dir"
2571         $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
2572                 -N -E1G -c4 -z128M -E-1 -z256M $dir || error "Create $file failed"
2573
2574         verify_46 $dir $dir.copy "10. SEL dir"
2575 }
2576 run_test 46 "Verify setstripe --copy option"
2577
2578 test_47() {
2579         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
2580
2581         local file=$DIR/$tdir/$tfile
2582         local ids
2583         local ost
2584         local osts
2585
2586         test_mkdir $DIR/$tdir
2587         stack_trap "rm -f $file"
2588
2589         # test case 1:
2590         rm -f $file
2591         # mirror1: [comp0]ost0,    [comp1]ost1 and ost2
2592         # mirror2: [comp2]    ,    [comp3] should not use ost1 or ost2
2593         $LFS mirror create -N -E2m -c1 -o0 --flags=prefer -Eeof -c2 -o1,2 \
2594                 -N -E2m -c1 -Eeof -c1 $file || error "create FLR $file failed"
2595         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2596
2597         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
2598         $LFS mirror resync $file || error "resync $file failed"
2599
2600         ost=$($LFS getstripe -I${ids[2]} $file | awk '/l_ost_idx/{print $5}')
2601         if [[ x$ost == "x0," ]]; then
2602                 $LFS getstripe $file
2603                 error "component ${ids[2]} objects allocated on $ost " \
2604                       "shouldn't on OST0"
2605         fi
2606
2607         ost=$($LFS getstripe -I${ids[3]} $file | awk '/l_ost_idx/{print $5}')
2608         if [[ x$ost == "x1," || x$ost == "x2," ]]; then
2609                 $LFS getstripe $file
2610                 error "component ${ids[3]} objects allocated on $ost " \
2611                       "shouldn't on OST1 or on OST2"
2612         fi
2613
2614         ## test case 2:
2615         rm -f $file
2616         # mirror1: [comp0]    [comp1]
2617         # mirror2: [comp2]    [comp3]
2618         # mirror3: [comp4]    [comp5]
2619         # mirror4: [comp6]    [comp7]
2620         $LFS mirror create -N4 -E1m -c1 -Eeof -c1 $file ||
2621                 error "create FLR $file failed"
2622         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2623
2624         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
2625         $LFS mirror resync $file || error "resync $file failed"
2626
2627         for ((i = 0; i < 6; i++)); do
2628                 osts[$i]=$($LFS getstripe -I${ids[$i]} $file |
2629                         awk '/l_ost_idx/{print $5}')
2630         done
2631         # comp[0],comp[2],comp[4] should use different osts
2632         if [[ ${osts[0]} == ${osts[2]} || ${osts[0]} == ${osts[4]} ||
2633               ${osts[2]} == ${osts[4]} ]]; then
2634                 $LFS getstripe $file
2635                 error "component ${ids[0]}, ${ids[2]}, ${ids[4]} have objects "\
2636                       "allocated on duplicated OSTs"
2637         fi
2638         # comp[1],comp[3],comp[5] should use different osts
2639         if [[ ${osts[1]} == ${osts[3]} || ${osts[1]} == ${osts[5]} ||
2640               ${osts[3]} == ${osts[5]} ]]; then
2641                 $LFS getstripe $file
2642                 error "component ${ids[1]}, ${ids[3]}, ${ids[5]} have objects "\
2643                       "allocated on duplicated OSTs"
2644         fi
2645
2646         return 0
2647 }
2648 run_test 47 "Verify mirror obj alloc"
2649
2650 test_48() {
2651         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
2652                 skip "Need MDS version at least 2.11.55"
2653
2654         local tf=$DIR/$tfile
2655
2656         stack_trap "rm -f $tf"
2657
2658         echo " ** create 2 mirrors FLR file $tf"
2659         $LFS mirror create -N -E2M -Eeof --flags prefer \
2660                            -N -E1M -Eeof $tf ||
2661                 error "create FLR file $tf failed"
2662
2663         echo " ** write it"
2664         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2665         verify_flr_state $tf "wp"
2666
2667         local sum0=$(md5sum < $tf)
2668
2669         echo " ** resync the file"
2670         $LFS mirror resync $tf
2671
2672         echo " ** snapshot mirror 2"
2673         $LFS setstripe --comp-set -I 0x20003 --comp-flags=nosync $tf
2674
2675         echo " ** write it again"
2676         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2677         echo " ** resync it again"
2678         $LFS mirror resync $tf
2679
2680         verify_flr_state $tf "wp"
2681         verify_comp_attr lcme_flags $tf 0x20003 nosync,stale
2682
2683         local sum1=$($LFS mirror read -N1 $tf | md5sum)
2684         local sum2=$($LFS mirror read -N2 $tf | md5sum)
2685
2686         echo " ** verify mirror 2 doesn't change"
2687         echo "original checksum: $sum0"
2688         echo "mirror 1 checksum: $sum1"
2689         echo "mirror 2 checksum: $sum2"
2690         [[ $sum0 = $sum2 ]] ||
2691                 error "original checksum: $sum0, mirror 2 checksum: $sum2"
2692         echo " ** mirror 2 stripe info"
2693         $LFS getstripe -v --mirror-index=2 $tf
2694
2695         echo " ** resync mirror 2"
2696         $LFS mirror resync --only 2 $tf
2697
2698         verify_flr_state $tf "ro"
2699         verify_comp_attr lcme_flags $tf 0x20003 nosync,^stale
2700
2701         sum1=$($LFS mirror read -N1 $tf | md5sum)
2702         sum2=$($LFS mirror read -N2 $tf | md5sum)
2703
2704         echo " ** verify mirror 2 resync-ed"
2705         echo "original checksum: $sum0"
2706         echo "mirror 1 checksum: $sum1"
2707         echo "mirror 2 checksum: $sum2"
2708         [[ $sum1 = $sum2 ]] ||
2709                 error "mirror 1 checksum: $sum1, mirror 2 checksum: $sum2"
2710         echo " ** mirror 2 stripe info"
2711         $LFS getstripe -v --mirror-index=2 $tf
2712 }
2713 run_test 48 "Verify snapshot mirror"
2714
2715 OLDIFS="$IFS"
2716 cleanup_49() {
2717         trap 0
2718         IFS="$OLDIFS"
2719 }
2720
2721 test_49a() {
2722         (( "$OSTCOUNT" >= "2" )) || skip "needs >= 2 OSTs"
2723         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
2724         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
2725         [[ "$ost1_FSTYPE" != "zfs" ]] ||
2726                 skip "LU-1941: FIEMAP unimplemented on ZFS"
2727
2728         trap cleanup_49 EXIT RETURN
2729
2730         local file=$DIR/$tfile
2731
2732         $LFS setstripe -N -E eof -c1 -o1 -N -E eof -c1 -o0 $file ||
2733                 error "setstripe on $file"
2734         stack_trap "rm -f $file"
2735
2736         dd if=/dev/zero of=$file bs=1M count=1 || error "dd failed for $file"
2737         $LFS mirror resync $file
2738
2739         filefrag -ves $file || error "filefrag $file failed"
2740         filefrag_op=$(filefrag -ve -k $file |
2741                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
2742
2743 #Filesystem type is: bd00bd0
2744 #File size of /mnt/lustre/f49a.sanity-flr is 1048576 (1024 blocks of 1024 bytes)
2745 # ext:     device_logical:        physical_offset: length:  dev: flags:
2746 #   0:        0..    1023:    1572864..   1573887:   1024: 0001: net,eof
2747 #   1:        0..    1023:    1572864..   1573887:   1024: 0000: last,net,eof
2748 #/mnt/lustre/f49a.sanity-flr: 2 extents found
2749
2750         last_lun=$(echo $filefrag_op | cut -d: -f5)
2751         IFS=$'\n'
2752         tot_len=0
2753         num_luns=1
2754         for line in $filefrag_op; do
2755                 frag_lun=$(echo $line | cut -d: -f5)
2756                 ext_len=$(echo $line | cut -d: -f4)
2757                 if [[ "$frag_lun" != "$last_lun" ]]; then
2758                         if (( tot_len != 1024 )); then
2759                                 cleanup_49
2760                                 error "$file: OST$last_lun $tot_len != 1024"
2761                         else
2762                                 (( num_luns += 1 ))
2763                                 tot_len=0
2764                         fi
2765                 fi
2766                 (( tot_len += ext_len ))
2767                 last_lun=$frag_lun
2768         done
2769         if (( num_luns != 2 || tot_len != 1024 )); then
2770                 cleanup_49
2771                 error "$file: $num_luns != 2, $tot_len != 1024 on OST$last_lun"
2772         fi
2773
2774         echo "FIEMAP on $file succeeded"
2775 }
2776 run_test 49a "FIEMAP upon FLR file"
2777
2778 test_50A() {    # EX-2179
2779         mkdir -p $DIR/$tdir
2780
2781         local file=$DIR/$tdir/$tfile
2782
2783         $LFS setstripe -c1 -i0 $file || error "setstripe $file failed"
2784
2785         $LFS mirror extend -N -c1 -i1 $file ||
2786                 error "extending mirror for $file failed"
2787
2788         local olv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
2789
2790         fail mds1
2791
2792         $LFS mirror split -d --mirror-id=1 $file || error "split $file failed"
2793
2794         local flv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
2795
2796         echo "$file layout generation from $olv to $flv"
2797         (( $flv != ($olv + 1) )) &&
2798                 error "split does not increase layout gen from $olv to $flv"
2799
2800         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
2801
2802         $LFS getstripe -v $file || error "getstripe $file failed"
2803 }
2804 run_test 50A "mirror split update layout generation"
2805
2806 test_50a() {
2807         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2808                 skip "OST does not support SEEK_HOLE"
2809         [ "$FSTYPE" != "zfs" ] ||
2810                 skip "lseek for ZFS is not accurate if obj is not committed"
2811
2812         local file=$DIR/$tdir/$tfile
2813         local offset
2814         local sum1
2815         local sum2
2816         local blocks
2817
2818         mkdir -p $DIR/$tdir
2819
2820         echo " ** create striped file $file"
2821         $LFS setstripe -E 1M -c1 -S 1M -E eof -c2 -S1M $file ||
2822                 error "cannot create file with PFL layout"
2823         echo " ** write 1st data chunk at 1M boundary"
2824         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2825                 error "cannot write data at 1M boundary"
2826         echo " ** write 2nd data chunk at 2M boundary"
2827         dd if=/dev/urandom of=$file bs=1k count=20 seek=2041 ||
2828                 error "cannot write data at 2M boundary"
2829         echo " ** create hole at the file end"
2830         $TRUNCATE $file 3700000 || error "truncate fails"
2831
2832         echo " ** verify sparseness"
2833         offset=$(lseek_test -d 1000 $file)
2834         echo "    first data offset: $offset"
2835         [[ $offset == 1000 ]] &&
2836                 error "src: data is not expected at offset $offset"
2837         offset=$(lseek_test -l 3500000 $file)
2838         echo "    hole at the end: $offset"
2839         [[ $offset == 3500000 ]] ||
2840                 error "src: hole is expected at offset $offset"
2841
2842         echo " ** extend the file with new mirror"
2843         # migrate_copy_data() is used
2844         $LFS mirror extend -N -E 2M -S 1M -E 1G -S 2M -E eof $file ||
2845                 error "cannot create mirror"
2846         $LFS getstripe $file | grep lcme_flags | grep stale > /dev/null &&
2847                 error "$file still has stale component"
2848
2849         # check migrate_data_copy() was correct
2850         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2851         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2852         [[ $sum_1 == $sum_2 ]] ||
2853                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2854
2855         # stale first mirror
2856         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale $file
2857         $LFS setstripe --comp-set -I0x10002 --comp-flags=stale $file
2858
2859         echo " ** verify mirror #2 sparseness"
2860         offset=$(lseek_test -d 1000 $file)
2861         echo "    first data offset: $offset"
2862         [[ $offset == 1000 ]] &&
2863                 error "dst: data is not expected at offset $offset"
2864         offset=$(lseek_test -l 3500000 $file)
2865         echo "    hole at the end: $offset"
2866         [[ $offset == 3500000 ]] ||
2867                 error "dst: hole is expected at offset $offset"
2868
2869         echo " ** copy mirror #2 to mirror #1"
2870         $LFS mirror copy -i 2 -o 1 $file || error "mirror copy fails"
2871         $LFS getstripe $file | grep lcme_flags | grep stale > /dev/null &&
2872                 error "$file still has stale component"
2873
2874         # check llapi_mirror_copy_many correctness
2875         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2876         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2877         [[ $sum_1 == $sum_2 ]] ||
2878                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2879
2880         # stale 1st component of mirror #2 before lseek call
2881         $LFS setstripe --comp-set -I0x20001 --comp-flags=stale $file
2882
2883         echo " ** verify mirror #1 sparseness again"
2884         offset=$(lseek_test -d 1000 $file)
2885         echo "    first data offset: $offset"
2886         [[ $offset == 1000 ]] &&
2887                 error "dst: data is not expected at offset $offset"
2888         offset=$(lseek_test -l 3500000 $file)
2889         echo "    hole at the end: $offset"
2890         [[ $offset == 3500000 ]] ||
2891                 error "dst: hole is expected at offset $offset"
2892
2893         cancel_lru_locks osc
2894
2895         blocks=$(stat -c%b $file)
2896         echo " ** final consumed blocks: $blocks"
2897         # for 3.5Mb file consumes ~6000 blocks, use 1000 to check
2898         # that file is still sparse
2899         (( blocks < 1000 )) ||
2900                 error "Mirrored file consumes $blocks blocks"
2901 }
2902 run_test 50a "mirror extend/copy preserves sparseness"
2903
2904 test_50b() {
2905         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2906                 skip "OST does not support SEEK_HOLE"
2907         [ "$FSTYPE" != "zfs" ] ||
2908                 skip "lseek for ZFS is not accurate if obj is not committed"
2909
2910         local file=$DIR/$tdir/$tfile
2911         local offset
2912         local sum1
2913         local sum2
2914         local blocks
2915
2916         mkdir -p $DIR/$tdir
2917         stack_trap "rm -f $file"
2918
2919         echo " ** create mirrored file $file"
2920         $LFS mirror create -N -E1M -c1 -S1M -E eof \
2921                 -N -E2M -S1M -E eof -S2M $file ||
2922                 error "cannot create mirrored file"
2923         echo " ** write data chunk at 1M boundary"
2924         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2925                 error "cannot write data at 1M boundary"
2926         echo " ** create hole at the file end"
2927         $TRUNCATE $file 3700000 || error "truncate fails"
2928
2929         echo " ** verify sparseness"
2930         offset=$(lseek_test -d 1000 $file)
2931         echo "    first data offset: $offset"
2932         [[ $offset == 1000 ]] &&
2933                 error "src: data is not expected at offset $offset"
2934         offset=$(lseek_test -l 3500000 $file)
2935         echo "    hole at the end: $offset"
2936         [[ $offset == 3500000 ]] ||
2937                 error "src: hole is expected at 3500000"
2938
2939         echo " ** resync mirror #2 to mirror #1"
2940         $LFS mirror resync $file
2941
2942         # check llapi_mirror_copy_many correctness
2943         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2944         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2945         [[ $sum_1 == $sum_2 ]] ||
2946                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2947
2948         cancel_lru_locks osc
2949
2950         blocks=$(stat -c%b $file)
2951         echo " ** consumed blocks: $blocks"
2952         # without full punch() support the first component can be not sparse
2953         # but the last one should be, so file should use far fewer blocks
2954         (( blocks < 5000 )) ||
2955                 error "Mirrored file consumes $blocks blocks"
2956
2957         # stale first component in mirror #1
2958         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,nosync $file
2959         echo " ** truncate file down"
2960         $TRUNCATE $file 0
2961         echo " ** write data chunk at 2M boundary"
2962         dd if=/dev/urandom of=$file bs=1k count=20 seek=2041 conv=notrunc ||
2963                 error "cannot write data at 2M boundary"
2964         echo " ** resync mirror #2 to mirror #1 with nosync 1st component"
2965         $LFS mirror resync $file || error "mirror rsync fails"
2966         # first component is still stale
2967         $LFS getstripe $file | grep 'lcme_flags:.*stale' > /dev/null ||
2968                 error "$file still has no stale component"
2969         echo " ** resync mirror #2 to mirror #1 again"
2970         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,^nosync $file
2971         $LFS mirror resync $file || error "mirror rsync fails"
2972         $LFS getstripe $file | grep 'lcme_flags:.*stale' > /dev/null &&
2973                 error "$file still has stale component"
2974
2975         # check llapi_mirror_copy_many correctness
2976         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2977         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2978         [[ $sum_1 == $sum_2 ]] ||
2979                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2980
2981         cancel_lru_locks osc
2982
2983         blocks=$(stat -c%b $file)
2984         echo " ** final consumed blocks: $blocks"
2985         # while the first component can lose sparseness, the last one should
2986         # not, so whole file should still use far fewer blocks in total
2987         (( blocks < 3000 )) ||
2988                 error "Mirrored file consumes $blocks blocks"
2989 }
2990 run_test 50b "mirror rsync handles sparseness"
2991
2992 test_50c() {
2993         local tf=$DIR/$tdir/$tfile
2994
2995         test_mkdir $DIR/$tdir
2996
2997         $LFS setstripe -N2 -c-1 $tf || error "create FLR $tf failed"
2998         verify_flr_state $tf "ro"
2999
3000         if [[ "$FSTYPE" == "ldiskfs" ]]; then
3001                 # ZFS does not support fallocate for now
3002                 out=$(fallocate -p -o 1MiB -l 1MiB $tf 2>&1) ||
3003                         skip_eopnotsupp "$out|punch hole in $tf failed"
3004                 verify_flr_state $tf "wp"
3005         fi
3006
3007         dd if=/dev/zero of=$tf bs=4096 count=4 || error "write $tf failed"
3008         $LFS mirror resync $tf || error "mirror resync $tf failed"
3009         verify_flr_state $tf "ro"
3010
3011         $MULTIOP $tf OSMWUc || error "$MULTIOP $tf failed"
3012         verify_flr_state $tf "wp"
3013 }
3014 run_test 50c "punch_hole/mmap_write stale other mirrors"
3015
3016 test_50d() {
3017         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
3018                 skip "OST does not support SEEK_HOLE"
3019         [ "$FSTYPE" != "zfs" ] ||
3020                 skip "lseek for ZFS is not accurate if obj is not committed"
3021
3022         local file=$DIR/$tdir/$tfile
3023         local offset
3024         local prt
3025         local rc
3026
3027         mkdir -p $DIR/$tdir
3028
3029         echo " ** create mirrored file $file"
3030         $LFS mirror create -N -E1M -c1 -S1M -E eof \
3031                 -N -E2M -S1M -E eof -S2M $file ||
3032                 error "cannot create mirrored file"
3033         echo " ** write data chunk at 1M boundary"
3034         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
3035                 error "cannot write data at 1M boundary"
3036         echo " ** create hole at the file start"
3037         prt=$(fallocate -p -o 0 -l 1M $file 2>&1)
3038         rc=$?
3039
3040         if [[ $rc -eq 0 ]]; then
3041                 verify_flr_state $file "wp"
3042         elif [[ ! $prt =~ unsupported ]]; then
3043                 error "punch hole in $file failed: $prt"
3044         else
3045                 skip "Fallocate punch is not supported: $prt"
3046         fi
3047
3048         echo " ** verify sparseness"
3049         offset=$(lseek_test -d 1000 $file)
3050         echo "    first data offset: $offset"
3051         (( $offset >= 1024 * 1024 )) ||
3052                 error "src: data is not expected at offset $offset"
3053
3054         echo " ** resync mirror #2"
3055         $LFS mirror resync $file
3056
3057         # check llapi_mirror_copy_many correctness
3058         sum_1=$($LFS mirror read -N 1 $file | md5sum)
3059         sum_2=$($LFS mirror read -N 2 $file | md5sum)
3060         [[ $sum_1 == $sum_2 ]] ||
3061                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
3062
3063         cancel_lru_locks osc
3064
3065         # stale first component in mirror #1
3066         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,nosync $file
3067         echo " ** verify sparseness of mirror #2"
3068         offset=$(lseek_test -d 1000 $file)
3069         echo "    first data offset: $offset"
3070         (( $offset >= 1024 * 1024 )) ||
3071                 error "src: data is not expected at offset $offset"
3072 }
3073 run_test 50d "mirror rsync keep holes"
3074
3075 test_60a() {
3076         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
3077                 skip "OST does not support SEEK_HOLE"
3078
3079         local file=$DIR/$tdir/$tfile
3080         local old_size=2147483648 # 2GiB
3081         local new_size
3082
3083         mkdir -p $DIR/$tdir
3084         dd if=/dev/urandom of=$file bs=4096 count=1 seek=$((134217728 / 4096))
3085         $TRUNCATE $file $old_size
3086
3087         $LFS mirror extend -N -c 1 $file
3088         dd if=/dev/urandom of=$file bs=4096 count=1 seek=$((134217728 / 4096)) conv=notrunc
3089         $LFS mirror resync $file
3090
3091         new_size=$(stat --format='%s' $file)
3092         if ((new_size != old_size)); then
3093                 error "new_size ($new_size) is not equal to old_size ($old_size)"
3094         fi
3095 }
3096 run_test 60a "mirror extend sets correct size on sparse file"
3097
3098 get_flr_layout_gen() {
3099         getfattr -n lustre.lov --only-values $tf 2>/dev/null |
3100                 od -tx4 | awk '/000000/ { print "0x"$4; exit; }'
3101 }
3102
3103 check_layout_gen() {
3104         local tf=$1
3105
3106         local v1=$(get_flr_layout_gen $tf)
3107         local v2=$($LFS getstripe -v $tf | awk '/lcm_layout_gen/ { print $2 }')
3108
3109         [[ $v1 -eq $v2 ]] ||
3110                 error "$tf in-memory layout gen $v1 != $v2 after $2"
3111 }
3112
3113 test_60b() {
3114         local tf=$DIR/$tdir/$tfile
3115
3116         test_mkdir $DIR/$tdir
3117
3118         $LFS setstripe -Eeof $tf || error "setstripe $tf failed"
3119
3120         for ((i = 0; i < 20; i++)); do
3121                 $LFS mirror extend -N $tf ||
3122                         error "extending mirror for $tf failed"
3123                 check_layout_gen $tf "extend"
3124
3125                 $LFS mirror split -d --mirror-id=$((i+1)) $tf ||
3126                         error "split $tf failed"
3127                 check_layout_gen $tf "split"
3128         done
3129 }
3130 run_test 60b "mirror merge/split cancel client's in-memory layout gen"
3131
3132 get_times_61() {
3133         stat --format='%X %Y %Z' $file || error "$file: cannot get times"
3134 }
3135
3136 check_times_61() {
3137         local file=$1
3138         local -a old=( $2 $3 $4 )
3139         local -a new
3140
3141         new=( $(get_times_61 $file) )
3142         ((${old[0]} == ${new[0]})) ||
3143                 error "$file: atime: old '${old[0]}' != new '${new[0]}'"
3144
3145         ((${old[1]} == ${new[1]})) ||
3146                 error "$file: mtime: old '${old[1]}' != new '${new[1]}'"
3147 }
3148
3149 test_61a() { # LU-14508
3150         local file=$DIR/$tdir/$tfile
3151         local old_diff=($(do_facet mds1 "$LCTL get_param -n mdd.*.atime_diff"))
3152         local mdts=$(comma_list $(mdts_nodes))
3153         local -a tim
3154         local nap=5
3155
3156         do_nodes $mdts "$LCTL set_param mdd.*.atime_diff=1"
3157         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.atime_diff=$old_diff"
3158
3159         mkdir -p $DIR/$tdir
3160         echo "create $file"
3161         $LFS setstripe -E1M -Eeof $file || error "setstripe $file failed"
3162         echo "create $file-2"
3163         $LFS setstripe -E2M -Eeof $file-2 || error "setstripe $file-2 failed"
3164
3165         echo XXX > $file || error "write $file failed"
3166         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
3167
3168         echo "sleep $nap seconds, then cat $tfile"
3169         sleep $nap
3170         cat $file || error "cat $file failed"
3171
3172         echo "sleep $nap seconds, then re-write $tfile"
3173         sleep $nap
3174         echo XXXX > $file || error "write $file failed"
3175         cp -p $file $file-2 || error "copy $file-2 failed"
3176
3177         echo "sleep $nap seconds"
3178         sleep $nap
3179
3180         tim=( $(get_times_61 $file) )
3181
3182         echo "mirror merge $tfile-2 to $tfile and test timestamps"
3183         $LFS mirror extend -N -f $file-2 $file ||
3184                 error "cannot mirror merge $file-2 to $file"
3185         check_times_61 $file "${tim[@]}"
3186
3187         echo "mirror extend $tfile and test timestamps"
3188         $LFS mirror extend -N -c1 -i1 $file ||
3189                 error "cannot extend mirror $file"
3190         check_times_61 $file "${tim[@]}"
3191
3192         echo "migrate $tfile and test timestamps"
3193         $LFS migrate -n $file || error "cannot migrate $file"
3194         check_times_61 $file "${tim[@]}"
3195
3196         echo "normal user migrate $tfile and test timestamps"
3197         $RUNAS $LFS migrate -n $file || error "cannot migrate $file"
3198         check_times_61 $file "${tim[@]}"
3199 }
3200 run_test 61a "mirror extend and migrate preserve timestamps"
3201
3202 test_61b() { # LU-14508
3203         local file=$DIR/$tdir/$tfile
3204         local -a tim
3205         local nap=5
3206
3207         mkdir -p $DIR/$tdir
3208         echo "create $file"
3209         echo XXX > $file || error "create $file failed"
3210         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
3211
3212         echo "sleep $nap seconds, then cat $tfile"
3213         sleep $nap
3214         cat $file || error "cat $file failed"
3215
3216         echo "sleep $nap seconds, then re-write $tfile"
3217         sleep $nap
3218         echo XXXX > $file || error "write $file failed"
3219
3220         echo "sleep $nap seconds, then test timestamps"
3221         sleep $nap
3222
3223         tim=( $(get_times_61 $file) )
3224
3225         echo "mirror extend $tfile and test timestamps"
3226         $LFS mirror extend -N -c1 -i1 $file ||
3227                 error "cannot extend mirror $file"
3228         check_times_61 $file "${tim[@]}"
3229
3230         echo "mirror split $tfile and test timestamps"
3231         $LFS mirror split -d --mirror-id=1 $file ||
3232                 error "cannot split mirror 1 off $file"
3233         check_times_61 $file "${tim[@]}"
3234
3235         echo "normal user mirror extend $tfile and test timestamps"
3236         $RUNAS $LFS mirror extend -N -c1 -i1 $file ||
3237                 error "cannot extend mirror $file"
3238         check_times_61 $file "${tim[@]}"
3239 }
3240 run_test 61b "mirror extend and split preserve timestamps"
3241
3242 test_61c() { # LU-14508
3243         local file=$DIR/$tdir/$tfile
3244         local -a tim
3245         local nap=5
3246
3247         mkdir -p $DIR/$tdir
3248         echo "create $file"
3249         echo XXX > $file || error "create $file failed"
3250         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
3251
3252         echo "sleep $nap seconds, then cat $tfile"
3253         sleep $nap
3254         cat $file || error "cat $file failed"
3255
3256         echo "sleep $nap seconds, then mirror extend $tfile and write it"
3257         sleep $nap
3258         $LFS mirror extend -N -c1 -i1 $file ||
3259                 error "cannot extend mirror $file"
3260         echo XXXX > $file || error "write $file failed"
3261
3262         echo "sleep $nap seconds, then resync $tfile and test timestamps"
3263         tim=( $(get_times_61 $file) )
3264         sleep $nap
3265         $LFS mirror resync $file || error "cannot resync mirror $file"
3266         check_times_61 $file "${tim[@]}"
3267
3268         echo XXXXXX > $file || error "write $tfile failed"
3269
3270         echo "normal user resync $tfile and test timestamps"
3271         tim=( $(get_times_61 $file) )
3272         $RUNAS $LFS mirror resync $file || error "cannot resync mirror $file"
3273         check_times_61 $file "${tim[@]}"
3274 }
3275 run_test 61c "mirror resync preserves timestamps"
3276
3277 test_62() {
3278         local file=$DIR/$tdir/$tfile
3279
3280         mkdir -p $DIR/$tdir
3281
3282         echo "create mirror file with unknown magic"
3283         #define OBD_FAIL_LOV_COMP_MAGIC 0x1426
3284         # mirror 2 in-memory magic is bad
3285         $LCTL set_param fail_loc=0x1426 fail_val=2
3286         $LFS setstripe -N --flags=prefer -N2 $file ||
3287                 error "failed to create mirror file $file"
3288         magic=$($LFS getstripe -v -I131074 $file | awk '/lmm_magic/{print $2}')
3289         [[ $magic == 0x0BAD0BD0 ]] ||
3290                 error "mirror 2 magic $magic is not bad as expected"
3291         cat /etc/passwd > $file || error "cannot write to $file"
3292         diff /etc/passwd $file || error "read $file error"
3293
3294         rm -f $file
3295
3296         echo "create mirror file with unknown pattern"
3297         #define OBD_FAIL_LOV_COMP_PATTERN 0x1427
3298         # mirror 1 in-memory pattern is bad
3299         $LCTL set_param fail_loc=0x1427 fail_val=1
3300         $LFS setstripe -N -N --flags=prefer $file ||
3301                 error "failed to create mirror file $file"
3302         pattern=$($LFS getstripe -I65537 $file | awk '/lmm_pattern/{print $2}')
3303         [[ $pattern == 502 ]] ||
3304                 error "mirror 1 pattern $pattern is not bad as expected"
3305         cat /etc/passwd > $file || error "cannot write to $file"
3306         diff /etc/passwd $file || error "read $file error"
3307 }
3308 run_test 62 "read/write with unknown type of mirror"
3309
3310 test_70() {
3311         local tf=$DIR/$tdir/$tfile
3312
3313         test_mkdir $DIR/$tdir
3314         stack_trap "rm -f $tf"
3315
3316         while true; do
3317                 rm -f $tf
3318                 $LFS mirror create -N -E 1M -c -1 -E eof -N $tf
3319                 echo xxxx > $tf
3320         done &
3321         c_pid=$!
3322         echo "mirror create pid $c_pid"
3323
3324         while true; do
3325                 $LFS mirror split -d --mirror-id=1 $tf &> /dev/null
3326         done &
3327         s_pid=$!
3328         echo "mirror split pid $s_pid"
3329
3330         echo "mirror create and split race for 60 seconds, should not crash"
3331         sleep 60
3332         kill -9 $c_pid &> /dev/null
3333         kill -9 $s_pid &> /dev/null
3334
3335         true
3336 }
3337 run_test 70 "mirror create and split race"
3338
3339 test_70a() {
3340         local tf=$DIR/$tdir/$tfile
3341
3342         (( $OST1_VERSION >= $(version_code 2.14.51) )) ||
3343                 skip "Need OST version at least 2.14.51"
3344
3345
3346         test_mkdir $DIR/$tdir
3347         stack_trap "rm -f $tf"
3348
3349         $LFS setstripe -N -E1M -c-1 -Eeof -c-1 $tf ||
3350                 error "setstripe $tf failed"
3351
3352         FSXNUM=${FSXNUM:-1000}
3353         $FSX -p 1 -N $FSXNUM -S 0 -M $tf || error "fsx FLR file $tf failed"
3354 }
3355 run_test 70a "flr mode fsx test"
3356
3357 write_file_200() {
3358         local tf=$1
3359
3360         local fsize=$(stat --printf=%s $tf)
3361
3362         while [ -f $ctrl_file ]; do
3363                 local off=$((RANDOM << 8))
3364                 local len=$((RANDOM << 5 + 131072))
3365
3366                 [ $((off + len)) -gt $fsize ] && {
3367                         fsize=$((off + len))
3368                         echo "Extending file size to $fsize .."
3369                 }
3370
3371                 flock -s $lock_file -c \
3372                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
3373                                 { rm -f $ctrl_file;
3374                                   error "failed writing to $off:$len"; }
3375                 sleep 0.$((RANDOM % 2 + 1))
3376         done
3377 }
3378
3379 read_file_200() {
3380         local tf=$1
3381
3382         while [ -f $ctrl_file ]; do
3383                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
3384                         { rm -f $ctrl_file; error "read failed"; }
3385                 sleep 0.$((RANDOM % 2 + 1))
3386         done
3387 }
3388
3389 resync_file_200() {
3390         local tf=$1
3391
3392         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
3393
3394         exec 200<>$lock_file
3395         while [ -f $ctrl_file ]; do
3396                 local lock_taken=false
3397                 local index=$((RANDOM % ${#options[@]}))
3398                 local cmd="mirror_io resync ${options[$index]}"
3399
3400                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
3401
3402                 [ $((RANDOM % 4)) -eq 0 ] && {
3403                         index=0
3404                         lock_taken=true
3405                         echo -n "lock to "
3406                 }
3407
3408                 echo -n "resync file $tf with '$cmd' .."
3409
3410                 if [[ $lock_taken = "true" ]]; then
3411                         flock -x 200 &&
3412                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
3413                         flock -u 200
3414                 else
3415                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
3416                 fi
3417
3418                 sleep 0.$((RANDOM % 8 + 1))
3419         done
3420 }
3421
3422 # this was test_200 before adding "b" and "c" subtests
3423 test_200a() {
3424         local tf=$DIR/$tfile
3425         local tf2=$DIR2/$tfile
3426         local tf3=$DIR3/$tfile
3427
3428         ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
3429         lock_file=$(mktemp /var/lock/FLR.XXXXXX)
3430         stack_trap "rm -f $ctrl_file $lock_file $tf $tf-2 $tf-3"
3431
3432         $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
3433         $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
3434         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
3435
3436         $LFS mirror extend -N -f $tf-2 $tf ||
3437                 error "merging $tf-2 into $tf failed"
3438         $LFS mirror extend -N -f $tf-3 $tf ||
3439                 error "merging $tf-3 into $tf failed"
3440
3441         mkdir -p $MOUNT2 && mount_client $MOUNT2
3442
3443         mkdir -p $MOUNT3 && mount_client $MOUNT3
3444
3445         verify_flr_state $tf3 "ro"
3446
3447         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
3448         $LCTL set_param fail_loc=0x1A03
3449
3450         local mds_idx=mds$(($($LFS getstripe -m $tf) + 1))
3451         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
3452
3453         declare -a pids
3454
3455         write_file_200 $tf &
3456         pids+=($!)
3457
3458         read_file_200 $tf &
3459         pids+=($!)
3460
3461         write_file_200 $tf2 &
3462         pids+=($!)
3463
3464         read_file_200 $tf2 &
3465         pids+=($!)
3466
3467         resync_file_200 $tf3 &
3468         pids+=($!)
3469
3470         local sleep_time=60
3471         [ "$SLOW" = "yes" ] && sleep_time=360
3472         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
3473                 sleep 1
3474                 ((--sleep_time))
3475         done
3476
3477         rm -f $ctrl_file
3478
3479         echo "Waiting ${pids[*]}"
3480         wait "${pids[@]}"
3481
3482         umount_client $MOUNT2
3483         umount_client $MOUNT3
3484
3485         # resync and verify mirrors
3486         $LFS mirror resync $tf || error "final resync failed"
3487         get_mirror_ids $tf
3488
3489         local csum=$($LFS mirror read -N "${mirror_array[0]}" $tf | md5sum)
3490
3491         for id in "${mirror_array[@]:1}"; do
3492                 [ "$($LFS mirror read -N $id $tf | md5sum)" = "$csum" ] ||
3493                         error "checksum error for mirror $id"
3494         done
3495
3496         true
3497 }
3498 run_test 200a "stress test"
3499
3500 test_200b() {
3501         local tf=$DIR/$tfile
3502         local tf2=$DIR2/$tfile
3503         local tf3=$DIR3/$tfile
3504
3505         ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
3506         lock_file=$(mktemp /var/lock/FLR.XXXXXX)
3507         stack_trap "rm -f $ctrl_file $lock_file $tf $tf-2 $tf-3"
3508
3509         $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
3510         $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
3511         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
3512
3513         $LFS mirror extend -N -f $tf-2 $tf ||
3514                 error "merging $tf-2 into $tf failed"
3515         $LFS mirror extend -N -f $tf-3 $tf ||
3516                 error "merging $tf-3 into $tf failed"
3517
3518         mkdir -p $MOUNT2 && mount_client $MOUNT2
3519
3520         mkdir -p $MOUNT3 && mount_client $MOUNT3
3521
3522         verify_flr_state $tf3 "ro"
3523
3524 #define OBD_FAIL_LLITE_PANIC_ON_ESTALE              0x1423
3525         $LCTL set_param fail_loc=0x1423
3526
3527         local -a pids
3528
3529         write_file_200 $tf &
3530         pids+=($!)
3531
3532         read_file_200 $tf &
3533         pids+=($!)
3534
3535         write_file_200 $tf2 &
3536         pids+=($!)
3537
3538         read_file_200 $tf2 &
3539         pids+=($!)
3540
3541         resync_file_200 $tf3 &
3542         pids+=($!)
3543
3544         local sleep_time=60
3545         [ "$SLOW" = "yes" ] && sleep_time=400
3546         sleep $sleep_time
3547         rm -f $ctrl_file
3548
3549         echo "Waiting ${pids[@]}"
3550         wait ${pids[@]}
3551
3552         umount_client $MOUNT2
3553         umount_client $MOUNT3
3554
3555         # resync and verify mirrors
3556         $LFS mirror resync $tf || {
3557                 ps ax
3558                 error "final resync failed"
3559         }
3560         get_mirror_ids $tf
3561
3562         local csum=$($LFS mirror read -N ${mirror_array[0]} $tf | md5sum)
3563         for id in ${mirror_array[@]:1}; do
3564                 [ "$($LFS mirror read -N $id $tf | md5sum)" = "$csum" ] ||
3565                         error "checksum error for mirror $id"
3566         done
3567
3568         true
3569 }
3570 run_test 200b "racing IO, mirror extend and resync"
3571
3572 test_200c() {
3573         (( MDS1_VERSION >= $(version_code 2.15.53) )) ||
3574                 skip "Need MDS version at least 2.15.53"
3575
3576         local tf=$DIR/$tfile
3577         local tf2=$DIR2/$tfile
3578
3579         mkdir -p $MOUNT2 && mount_client $MOUNT2
3580         stack_trap "umount_client $MOUNT2"
3581         stack_trap "rm -f $tf"
3582
3583         $LFS df
3584
3585         dd if=/dev/urandom of=$tf bs=1M count=2 || error "can't write"
3586         local mdt_idx
3587         mdt_idx=$($LFS getstripe -m $tf)
3588
3589         cancel_lru_locks mdc
3590         cancel_lru_locks osc
3591
3592         # start a process modifying file, block it just
3593         # before layout lock acquisition
3594 #define OBD_FAIL_MDS_DELAY_OPEN          0x175
3595         do_facet mds$((mdt_idx+1)) $LCTL set_param fail_loc=0x80000175 fail_val=10
3596         #log "dd to stale replica"
3597         dd if=/dev/urandom of=$tf bs=1M count=2 oflag=direct conv=notrunc &
3598         local PID=$!
3599         sleep 0.5
3600
3601         # make a replica
3602         log "mirror extend"
3603         $LFS mirror extend -N -c -1 $tf2 || {
3604                 ps ax
3605                 error "can't mirror"
3606         }
3607         log "mirror extend done"
3608         do_facet mds$((mdt_idx+1)) $LCTL set_param fail_loc=0 fail_val=0
3609
3610         # wait for blocking dd to complete and modify file
3611         wait $PID || error "2nd dd failed"
3612         log "dd completed"
3613
3614         verify_mirror_count $tf 2
3615
3616         $LFS getstripe $tf | grep -q lcme_flags.*stale || {
3617                 $LFS getstripe $tf
3618                 $LFS getstripe $tf2
3619                 error "both replicas are still in sync"
3620         }
3621
3622         $LFS mirror verify -vvv $tf || {
3623                 $LFS getstripe $tf
3624                 error "corrupted in-sync file"
3625         }
3626 }
3627 run_test 200c "layout change racing with open: LOVEA changes"
3628
3629 cleanup_test_201() {
3630         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
3631
3632         umount_client $MOUNT2
3633 }
3634
3635 test_201() {
3636         local delay=${RESYNC_DELAY:-5}
3637
3638         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
3639                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
3640
3641         stack_trap cleanup_test_201 EXIT
3642
3643         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
3644                         changelog_register -n)
3645
3646         mkdir -p $MOUNT2 && mount_client $MOUNT2
3647
3648         local index=0
3649         while :; do
3650                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
3651                 [ -z "$log" ] && { sleep 1; continue; }
3652
3653                 index=$(echo $log | awk '{print $1}')
3654                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
3655                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
3656                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
3657
3658                 ((++index))
3659                 [ -z "$file" ] && continue
3660
3661                 local now=$(date +%s)
3662
3663                 echo "file: $file $fid was modified at $ts, now: $now, " \
3664                      "will be resynced at $((ts+delay))"
3665
3666                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
3667
3668                 $LFS mirror resync $file
3669                 echo "$file resync done"
3670         done
3671 }
3672 run_test 201 "FLR data mover"
3673
3674 test_202() {
3675         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
3676
3677         local tf=$DIR/$tfile
3678         local ids
3679
3680         $LFS setstripe -E 1M -S 1M -c 1 $tf
3681         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
3682         verify_comp_attr stripe-count $tf ${ids[0]} 1
3683
3684         $LFS setstripe --component-add -E 2M -c $OSTCOUNT $tf
3685         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
3686         verify_comp_attr stripe-count $tf ${ids[0]} 1
3687         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
3688
3689         dd if=/dev/zero of=$tf bs=1M count=2
3690         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
3691         verify_comp_attr stripe-count $tf ${ids[0]} 1
3692         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
3693 }
3694 run_test 202 "lfs setstripe --add-component wide striping"
3695
3696 test_203() {
3697         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
3698                 skip "Need MDS version at least 2.11.55"
3699         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
3700
3701         local tf=$DIR/$tfile
3702
3703         #create 2 mirrors
3704         $LFS mirror create -N2 -c1 $tf || error "create FLR file $tf"
3705         #delete first mirror
3706         $LFS mirror delete --mirror-id=1 $tf || error "delete first mirror"
3707
3708         $LFS getstripe $tf
3709         local old_id=$($LFS getstripe --mirror-id=2 -I $tf)
3710         local count=$($LFS getstripe --mirror-id=2 -c $tf) ||
3711                 error "getstripe count of mirror 2"
3712         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
3713
3714         #extend a mirror with 2 OSTs
3715         $LFS mirror extend -N -c2 $tf || error "extend mirror"
3716         $LFS getstripe $tf
3717
3718         local new_id=$($LFS getstripe --mirror-id=2 -I $tf)
3719         count=$($LFS getstripe --mirror-id=2 -c $tf) ||
3720                 error "getstripe count of mirror 2"
3721         [[ x$old_id = x$new_id ]] ||
3722                 error "mirror 2 changed ID from $old_id to $new_id"
3723         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
3724
3725         count=$($LFS getstripe --mirror-id=3 -c $tf) ||
3726                 error "getstripe count of mirror 3"
3727         [[ x$count = x2 ]] || error "mirror 3 stripe count $count is not 2"
3728 }
3729 run_test 203 "mirror file preserve mirror ID"
3730
3731 # Simple test of FLR + self-extending layout, SEL in non-primary mirror
3732 test_204a() {
3733         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3734                 skip "skipped for lustre < $SEL_VER"
3735
3736         local comp_file=$DIR/$tdir/$tfile
3737         local flg_opts=""
3738         local found=""
3739
3740         test_mkdir $DIR/$tdir
3741         stack_trap "rm -f $comp_file"
3742
3743         # first mirror is 0-10M, then 10M-(-1), second mirror is 1M followed
3744         # by extension space to -1
3745         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E-1 -z64M $comp_file ||
3746                 error "Create $comp_file failed"
3747
3748         # Write to first component, extending & staling second mirror
3749         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
3750                 error "dd to extend + stale failed"
3751
3752         $LFS getstripe $comp_file
3753
3754         flg_opts="--component-flags init,stale"
3755         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3756         [ $found -eq 1 ] || error "write: Second comp end incorrect"
3757
3758         flg_opts="--component-flags extension"
3759         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3760         [ $found -eq 1 ] || error "write: Third comp start incorrect"
3761
3762         # mirror resync should not change the extents
3763         $LFS mirror resync $comp_file
3764
3765         flg_opts="--component-flags init"
3766         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3767         [ $found -eq 1 ] || error "resync: Second comp end incorrect"
3768
3769         flg_opts="--component-flags extension"
3770         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3771         [ $found -eq 1 ] || error "resync: Third comp start incorrect"
3772
3773         sel_layout_sanity $comp_file 5
3774 }
3775 run_test 204a "FLR write/stale/resync tests with self-extending mirror"
3776
3777 # Simple test of FLR + self-extending layout, SEL in primary mirror
3778 test_204b() {
3779         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3780                 skip "skipped for lustre < $SEL_VER"
3781
3782         local comp_file=$DIR/$tdir/$tfile
3783         local flg_opts=""
3784         local found=""
3785
3786         test_mkdir $DIR/$tdir
3787         stack_trap "rm -f $comp_file"
3788
3789         # first mirror is 1M followed by extension space to -1, second mirror
3790         # is 0-10M, then 10M-(-1),
3791         $LFS setstripe -N -E 1M -E-1 -z64M -N -E 10M -E-1 $comp_file ||
3792                 error "Create $comp_file failed"
3793
3794         # Write to first component, extending first component & staling
3795         # other mirror
3796         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
3797                 error "dd to extend + stale failed"
3798
3799         $LFS getstripe $comp_file
3800
3801         flg_opts="--component-flags init"
3802         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3803         [ $found -eq 1 ] || error "write: First comp end incorrect"
3804
3805         flg_opts="--component-flags extension"
3806         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3807         [ $found -eq 1 ] || error "write: Second comp start incorrect"
3808
3809         flg_opts="--component-flags init,stale"
3810         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
3811         [ $found -eq 1 ] || error "write: First mirror comp flags incorrect"
3812
3813         # This component is staled because it overlaps the extended first
3814         # component of the primary mirror, even though it doesn't overlap
3815         # the actual write - thus not inited.
3816         flg_opts="--component-flags stale"
3817         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
3818         [ $found -eq 1 ] || error "write: Second mirror comp flags incorrect"
3819
3820         # mirror resync should not change the extents
3821         $LFS mirror resync $comp_file
3822
3823         $LFS getstripe $comp_file
3824
3825         flg_opts="--component-flags init"
3826         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3827         [ $found -eq 1 ] || error "resync: First comp end incorrect"
3828
3829         flg_opts="--component-flags extension"
3830         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3831         [ $found -eq 1 ] || error "resync: Second comp start incorrect"
3832
3833         flg_opts="--component-flags init"
3834         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
3835         [ $found -eq 1 ] || error "resync: First mirror comp flags incorrect"
3836
3837         flg_opts="--component-flags init"
3838         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
3839         [ $found -eq 1 ] || error "resync: Second mirror comp flags incorrect"
3840
3841         sel_layout_sanity $comp_file 5
3842 }
3843 run_test 204b "FLR write/stale/resync tests with self-extending primary"
3844
3845 # FLR + SEL failed extension & component removal
3846 # extension space in second mirror
3847 test_204c() {
3848         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3849         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3850                 skip "skipped for lustre < $SEL_VER"
3851
3852         local comp_file=$DIR/$tdir/$tfile
3853         local found=""
3854         local ost_idx1=0
3855         local ost_name=$(ostname_from_index $ost_idx1)
3856
3857         test_mkdir $DIR/$tdir
3858         stack_trap "rm -f $comp_file"
3859
3860         # first mirror is is 0-10M, then 10M-(-1), second mirror is 0-1M, then
3861         # extension space from 1M to 1G, then normal space to -1
3862         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E 1G -i $ost_idx1 -z 64M \
3863                 -E -1 $comp_file || error "Create $comp_file failed"
3864
3865         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
3866         sleep_maxage
3867
3868         # write to first comp (0 - 10M) of mirror 1, extending + staling
3869         # first + second comp of mirror 2
3870         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3871         RC=$?
3872
3873         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
3874         sleep_maxage
3875
3876         [ $RC -eq 0 ] || error "dd to extend + stale failed"
3877
3878         $LFS getstripe $comp_file
3879
3880         found=$($LFS find --component-start 0m --component-end 1m \
3881                 --comp-flags init,stale $comp_file | wc -l)
3882         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
3883
3884         found=$($LFS find --component-start 1m --component-end EOF \
3885                 --comp-flags stale,^init $comp_file | wc -l)
3886         [ $found -eq 1 ] || error "write: Second mirror comp incorrect"
3887
3888         local mirror_id=$($LFS getstripe --component-start=1m   \
3889                          --component-end=EOF $comp_file |       \
3890                          grep lcme_mirror_id | awk '{ print $2 }')
3891
3892         [[ $mirror_id -eq 2 ]] ||
3893                 error "component not in correct mirror? $mirror_id"
3894
3895         $LFS mirror resync $comp_file
3896
3897         $LFS getstripe $comp_file
3898
3899         # component dimensions should not change from resync
3900         found=$($LFS find --component-start 1m --component-end EOF \
3901                 --component-flags init $comp_file | wc -l)
3902         [ $found -eq 1 ] || error "resync: Second mirror comp incorrect"
3903
3904         sel_layout_sanity $comp_file 4
3905 }
3906 run_test 204c "FLR write/stale/resync test with component removal"
3907
3908 # Successful repeated component in primary mirror
3909 test_204d() {
3910         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3911         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3912                 skip "skipped for lustre < $SEL_VER"
3913
3914         local comp_file=$DIR/$tdir/$tfile
3915         local found=""
3916
3917         wait_delete_completed
3918         wait_mds_ost_sync
3919         test_mkdir $DIR/$tdir
3920         stack_trap "rm -f $comp_file"
3921
3922         # first mirror is 64M followed by extension space to -1, second mirror
3923         # is 0-10M, then 10M-(-1)
3924         $LFS setstripe -N -E-1 -z64M -N -E 10M -E-1 $comp_file ||
3925                 error "Create $comp_file failed"
3926
3927         local ost_idx1=$($LFS getstripe -I65537 -i $comp_file)
3928         local ost_name=$(ostname_from_index $ost_idx1)
3929         # degrade OST for first comp so we won't extend there
3930         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3931                 obdfilter.$ost_name.degraded=1
3932         sleep_maxage
3933
3934         # Write beyond first component, causing repeat & stale second mirror
3935         dd if=/dev/zero bs=1M count=1 seek=66 of=$comp_file conv=notrunc
3936         RC=$?
3937
3938         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3939                 obdfilter.$ost_name.degraded=0
3940         sleep_maxage
3941
3942         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
3943
3944         $LFS getstripe $comp_file
3945
3946         found=$($LFS find --component-start 64m --component-end 128m \
3947                 --component-flags init $comp_file | wc -l)
3948         [ $found -eq 1 ] || error "write: Repeat comp incorrect"
3949
3950         local ost_idx2=$($LFS getstripe --component-start=64m           \
3951                          --component-end=128m --component-flags=init    \
3952                          -i $comp_file)
3953         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
3954         local mirror_id=$($LFS getstripe --component-start=64m          \
3955                          --component-end=128m --component-flags=init    \
3956                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3957         [[ $mirror_id -eq 1 ]] ||
3958                 error "component not in correct mirror: $mirror_id, not 1"
3959
3960         $LFS mirror resync $comp_file
3961
3962         $LFS getstripe $comp_file
3963
3964         # component dimensions should not change from resync
3965         found=$($LFS find --component-start 0m --component-end 64m \
3966                 --component-flags init $comp_file | wc -l)
3967         [ $found -eq 1 ] || error "resync: first comp incorrect"
3968         found=$($LFS find --component-start 64m --component-end 128m \
3969                 --component-flags init $comp_file | wc -l)
3970         [ $found -eq 1 ] || error "resync: repeat comp incorrect"
3971
3972         sel_layout_sanity $comp_file 5
3973 }
3974 run_test 204d "FLR write/stale/resync sel test with repeated comp"
3975
3976 # Successful repeated component, SEL in non-primary mirror
3977 test_204e() {
3978         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3979         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3980                 skip "skipped for lustre < $SEL_VER"
3981
3982         local comp_file=$DIR/$tdir/$tfile
3983         local found=""
3984
3985         wait_delete_completed
3986         wait_mds_ost_sync
3987
3988         test_mkdir $DIR/$tdir
3989         stack_trap "rm -f $comp_file"
3990
3991         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
3992         # space to -1 (-z 64M, so first comp is 0-64M)
3993         # Note: we have to place both 1st components on OST0, otherwise 2 OSTs
3994         # will be not enough - one will be degraded, the other is used on
3995         # an overlapping mirror.
3996         $LFS setstripe -N -E 100M -i 0 -E-1 -N -E-1 -i 0 -z 64M $comp_file ||
3997                 error "Create $comp_file failed"
3998
3999         local ost_idx1=$($LFS getstripe --component-start=0 \
4000                          --component-end=64m -i $comp_file)
4001         local ost_name=$(ostname_from_index $ost_idx1)
4002         # degrade OST for first comp of 2nd mirror so we won't extend there
4003         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
4004                 obdfilter.$ost_name.degraded=1
4005         sleep_maxage
4006
4007         $LFS getstripe $comp_file
4008
4009         # Write to first component, stale & instantiate second mirror components
4010         # overlapping with the written component (0-100M);
4011         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
4012         RC=$?
4013
4014         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
4015                 obdfilter.$ost_name.degraded=0
4016         sleep_maxage
4017         $LFS getstripe $comp_file
4018
4019         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
4020
4021         found=$($LFS find --component-start 0m --component-end 64m \
4022                 --component-flags init,stale $comp_file | wc -l)
4023         [ $found -eq 1 ] || error "write: first comp incorrect"
4024
4025         # was repeated due to degraded ost
4026         found=$($LFS find --component-start 64m --component-end 128m \
4027                 --component-flags init,stale $comp_file | wc -l)
4028         [ $found -eq 1 ] || error "write: repeated comp incorrect"
4029
4030         local ost_idx2=$($LFS getstripe --component-start=64m           \
4031                          --component-end=128m --component-flags=init    \
4032                          -i $comp_file)
4033         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
4034         local mirror_id=$($LFS getstripe --component-start=0m           \
4035                          --component-end=64m --component-flags=init     \
4036                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
4037         [[ $mirror_id -eq 2 ]] ||
4038                 error "component not in correct mirror? $mirror_id"
4039
4040         $LFS mirror resync $comp_file
4041
4042         $LFS getstripe $comp_file
4043
4044         # component dimensions should not change from resync
4045         found=$($LFS find --component-start 0m --component-end 64m \
4046                 --component-flags init,^stale $comp_file | wc -l)
4047         [ $found -eq 1 ] || error "resync: first comp incorrect"
4048         found=$($LFS find --component-start 64m --component-end 128m \
4049                 --component-flags init,^stale $comp_file | wc -l)
4050         [ $found -eq 1 ] || error "resync: repeated comp incorrect"
4051
4052         sel_layout_sanity $comp_file 5
4053 }
4054 run_test 204e "FLR write/stale/resync sel test with repeated comp"
4055
4056 # FLR + SEL: failed repeated component, SEL in non-primary mirror
4057 test_204f() {
4058         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
4059         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
4060                 skip "skipped for lustre < $SEL_VER"
4061
4062         local comp_file=$DIR/$tdir/$tfile
4063         local found=""
4064
4065         wait_delete_completed
4066         wait_mds_ost_sync
4067         test_mkdir $DIR/$tdir
4068         stack_trap "rm -f $comp_file"
4069
4070         pool_add $TESTNAME || error "Pool creation failed"
4071         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
4072
4073         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
4074         # space to -1 (-z 64M, so first comp is 0-64M)
4075         $LFS setstripe -N -E 100M -E-1 -N --pool="$TESTNAME" \
4076                 -E-1 -c 1 -z 64M $comp_file || error "Create $comp_file failed"
4077
4078         local ost_name0=$(ostname_from_index 0)
4079         local ost_name1=$(ostname_from_index 1)
4080
4081         # degrade both OSTs in pool, so we'll try to repeat, then fail and
4082         # extend original comp
4083         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=1
4084         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=1
4085         sleep_maxage
4086
4087         # a write to the 1st component, 100M length, which will try to stale
4088         # the first 100M of mirror 2, attempting to extend its 0-64M component
4089         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
4090         RC=$?
4091
4092         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=0
4093         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=0
4094         sleep_maxage
4095
4096         [ $RC -eq 0 ] || error "dd to extend mirror comp failed"
4097
4098         $LFS getstripe $comp_file
4099
4100         found=$($LFS find --component-start 0m --component-end 128m \
4101                 --component-flags init,stale $comp_file | wc -l)
4102         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
4103
4104         local mirror_id=$($LFS getstripe --component-start=0m           \
4105                          --component-end=128m --component-flags=init    \
4106                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
4107
4108         [[ $mirror_id -eq 2 ]] ||
4109                 error "component not in correct mirror? $mirror_id, not 2"
4110
4111         $LFS mirror resync $comp_file
4112
4113         $LFS getstripe $comp_file
4114
4115         # component dimensions should not change from resync
4116         found=$($LFS find --component-start 0m --component-end 128m \
4117                 --component-flags init,^stale $comp_file | wc -l)
4118         [ $found -eq 1 ] || error "resync: First mirror comp incorrect"
4119
4120         sel_layout_sanity $comp_file 4
4121 }
4122 run_test 204f "FLR write/stale/resync sel w/forced extension"
4123
4124 function test_205() {
4125         local tf=$DIR/$tfile
4126         local mirrors
4127
4128         $LFS setstripe -c1 $tf
4129         $LFS mirror extend -N $tf
4130         mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
4131         (( $mirrors == 2 )) || error "no new mirror was created?"
4132
4133         $LFS mirror extend -N --flags=prefer $tf
4134         mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
4135         (( $mirrors == 3 )) || error "no new mirror was created?"
4136
4137         $($LFS getstripe $tf | grep lcme_flags: | tail -1 | grep -q prefer) ||
4138                 error "prefer flag was not set on the new mirror"
4139 }
4140 run_test 205 "lfs mirror extend to set prefer flag"
4141
4142 function test_206() {
4143         # create a new OST pool
4144         local pool_name=$TESTNAME
4145
4146         create_pool $FSNAME.$pool_name ||
4147                 error "create OST pool $pool_name failed"
4148         # add OSTs into the pool
4149         pool_add_targets $pool_name 0 1 ||
4150                 error "add OSTs into pool $pool_name failed"
4151
4152         $LFS setstripe -c1 --pool=$pool_name $DIR/$tfile ||
4153                 error "can't setstripe"
4154         $LFS mirror extend -N $DIR/$tfile ||
4155                 error "can't create replica"
4156         if $LFS getstripe $DIR/$tfile | grep -q prefer ; then
4157                 $LFS getstripe $DIR/$tfile
4158                 error "prefer found"
4159         fi
4160         $LFS setstripe --comp-set --comp-flags=prefer -p $pool_name $DIR/$tfile || {
4161                 $LFS getstripe $DIR/$tfile
4162                 error "can't setstripe prefer"
4163         }
4164
4165         if ! $LFS getstripe $DIR/$tfile | grep -q prefer ; then
4166                 $LFS getstripe $DIR/$tfile
4167                 error "no prefer found"
4168         fi
4169
4170         # destroy OST pool
4171         destroy_test_pools
4172 }
4173 run_test 206 "lfs setstripe -pool .. --comp-flags=.. "
4174
4175 test_207() {
4176        local file=$DIR/$tfile
4177        local tmpfile=$DIR/$tfile-tt
4178
4179         [ $MDS1_VERSION -lt $(version_code 2.14.50) ] &&
4180                 skip "Need MDS version at least 2.14.50"
4181
4182         stack_trap "rm -f $tmpfile $file"
4183
4184         # generate data for verification
4185         dd if=/dev/urandom of=$tmpfile bs=1M count=1 ||
4186                 error "can't generate file with random data"
4187
4188         # create a mirrored file with one stale replica
4189         $LFS mirror create -N -S 4M -c 2 -N -S 1M -c -1 $file ||
4190                 error "create mirrored file $file failed"
4191         get_mirror_ids $file
4192         echo "mirror IDs: ${mirror_array[*]}"
4193
4194         dd if=$tmpfile of=$file bs=1M || error "can't copy"
4195         get_mirror_ids $file
4196         echo "mirror IDs: ${mirror_array[*]}"
4197
4198         drop_client_cache
4199         cmp $tmpfile $file || error "files don't match"
4200         get_mirror_ids $file
4201         echo "mirror IDs: ${mirror_array[*]}"
4202
4203         # mirror creation should work fine
4204         $LFS mirror extend -N -S 8M -c -1 $file ||
4205                 error "mirror extend $file failed"
4206
4207         get_mirror_ids $file
4208         echo "mirror IDs: ${mirror_array[*]}"
4209
4210         drop_client_cache
4211         $LFS mirror verify -v $file || error "verification failed"
4212         cmp $tmpfile $file || error "files don't match"
4213 }
4214 run_test 207 "create another replica with existing out-of-sync one"
4215
4216 function check_ost_used() {
4217         local ddarg
4218         local ost
4219         local i
4220         local file=$1
4221         local io=$2
4222
4223         shift 2
4224
4225         cancel_lru_locks osc # to drop pages
4226         cancel_lru_locks mdc # to refresh layout
4227         # XXX: cancel_lru_locks mdc doesn't work
4228         # XXX: need a better way to reload the layout
4229         umount_client $MOUNT || error "umount failed"
4230         mount_client $MOUNT || error "mount failed"
4231
4232         # refresh non-rotation status on MDTs
4233         sleep 10
4234         touch $DIR/$tfile-temp
4235         rm -f $DIR/$tfile-temp
4236         # refresh non-rotational status on the client
4237         $LFS df >&/dev/null
4238         sleep 2
4239
4240         $LCTL set_param osc.*.stats=clear >/dev/null
4241         if [[ $io == "read" ]]; then
4242                 ddarg="if=$file of=/dev/null"
4243         elif [[ $io == "write" ]]; then
4244                 ddarg="if=/dev/zero of=$file"
4245         else
4246                 error "unknown type $io"
4247         fi
4248         dd $ddarg bs=8M count=1 || error "can't $io $file"
4249         cancel_lru_locks osc
4250
4251         # check only specified OSTs got reads
4252         for ((ost = 0; ost < $OSTCOUNT; ost++)); do
4253                 local nr=$($LCTL get_param -n \
4254                         osc.$FSNAME-OST000$ost-osc-[-0-9a-f]*.stats |
4255                         awk "/ost_$io/{print \$2}")
4256                 nr=${nr:-0}
4257                 if [[ " $* " =~ $ost ]]; then
4258                         (( nr > 0 )) || error "expected reads on $ost"
4259                 else
4260                         (( nr == 0 )) || error "unexpected $nr reads on $ost"
4261                 fi
4262         done
4263 }
4264
4265 test_208a() {
4266         local tf=$DIR/$tfile
4267         local osts=$(comma_list $(osts_nodes))
4268
4269         (( $OSTCOUNT >= 4 )) || skip "needs >= 4 OSTs"
4270         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
4271                 skip "Need MDS version at least 2.14.55"
4272
4273         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
4274
4275         save_lustre_params $(get_facets OST) osd*.*OST*.nonrotational > $p
4276         stack_trap "restore_lustre_params < $p; rm -f $p"
4277
4278         stack_trap "rm -f $tf"
4279         $LFS setstripe -i0 -c1 $tf || error "can't setstripe"
4280         dd if=/dev/zero of=$tf bs=8M count=1 || error "can't dd (1)"
4281         $LFS mirror extend -N -c1 -o1 $tf || error "can't create mirror"
4282         $LFS mirror extend -N -c2 -o 2,3 $tf || error "can't create mirror"
4283         $LFS mirror resync $tf || error "can't resync"
4284         $LFS getstripe $tf
4285
4286         log "set OST0000 non-rotational"
4287         do_nodes $osts \
4288                 $LCTL set_param osd*.*OST*.nonrotational=0
4289         do_nodes $osts \
4290                 $LCTL set_param osd*.*OST0000*.nonrotational=1
4291         check_ost_used $tf read 0
4292
4293         log "set OST0002 and OST0003 non-rotational, two fast OSTs is better"
4294         do_nodes $osts \
4295                 $LCTL set_param osd*.*OST*.nonrotational=0
4296         do_nodes $osts \
4297                 $LCTL set_param osd*.*OST0002*.nonrotational=1 \
4298                         osd*.*OST0003*.nonrotational=1
4299         check_ost_used $tf read 2 3
4300
4301         log "set mirror 1 on OST0001 preferred"
4302         $LFS setstripe --comp-set -I 0x20001 --comp-flags=prefer $tf ||
4303                 error "can't set prefer"
4304         check_ost_used $tf read 1
4305 }
4306 run_test 208a "mirror selection to prefer non-rotational devices for reads"
4307
4308 test_208b() {
4309         local tf=$DIR/$tfile
4310         local osts=$(comma_list $(osts_nodes))
4311
4312         (( $OSTCOUNT >= 4 )) || skip "needs >= 4 OSTs"
4313         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
4314                 skip "Need MDS version at least 2.14.55"
4315
4316         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
4317
4318         save_lustre_params $(get_facets OST) osd*.*OST*.nonrotational > $p
4319         stack_trap "restore_lustre_params < $p; rm -f $p"
4320
4321         stack_trap "rm -f $tf"
4322         $LFS setstripe -i0 -c1 -S1M $tf || error "can't setstripe"
4323         dd if=/dev/zero of=$tf bs=8M count=1 || error "can't dd (1)"
4324         $LFS mirror extend -N -c1 -o1 $tf || error "can't create mirror"
4325         $LFS mirror extend -N -c2 -o 2,3 $tf || error "can't create mirror"
4326         $LFS mirror resync $tf || error "can't resync"
4327         $LFS getstripe $tf | grep -q flags.*stale && error "still stale"
4328
4329         log "set OST0000 non-rotational"
4330         do_nodes $osts \
4331                 $LCTL set_param osd*.*OST*.nonrotational=0
4332         do_nodes $osts \
4333                 $LCTL set_param osd*.*OST0000*.nonrotational=1
4334         check_ost_used $tf write 0
4335         $LFS mirror resync $tf || error "can't resync"
4336
4337         log "set OST0002 and OST0003 non-rotational, two fast OSTs is better"
4338         do_nodes $osts \
4339                 $LCTL set_param osd*.*OST*.nonrotational=0
4340         do_nodes $osts \
4341                 $LCTL set_param osd*.*OST0002*.nonrotational=1 \
4342                         osd*.*OST0003*.nonrotational=1
4343         check_ost_used $tf write 2 3
4344         $LFS mirror resync $tf || error "can't resync"
4345
4346         log "set mirror 1 on OST0001 preferred"
4347         $LFS setstripe --comp-set -I 0x20001 --comp-flags=prefer $tf ||
4348                 error "can't set prefer"
4349         check_ost_used $tf write 1
4350 }
4351 run_test 208b "mirror selection to prefer non-rotational devices for writes"
4352
4353 test_209a() {
4354         local tf=$DIR/$tfile
4355         local tmpfile="$TMP/$TESTSUITE-$TESTNAME-multiop.output"
4356         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
4357         local osts=$(comma_list $(osts_nodes))
4358
4359         stack_trap "rm -f $tmpfile"
4360
4361         mkdir -p $MOUNT2 && mount_client $MOUNT2
4362         stack_trap "umount_client $MOUNT2"
4363
4364         # to make replica on ost1 preferred for new writes
4365         save_lustre_params $(get_facets OST) osd*.*OST*.nonrotational > $p
4366         stack_trap "restore_lustre_params < $p; rm -f $p"
4367         do_nodes $osts \
4368                 $LCTL set_param osd*.*OST*.nonrotational=0
4369         do_nodes $osts \
4370                 $LCTL set_param osd*.*OST0001*.nonrotational=1
4371
4372         $LFS setstripe -c1 -i0 $tf || errro "can't create $tf"
4373         echo "AAAA" >$tf
4374         $LFS mirror extend -N -o1 $tf || error "can't make replica"
4375         log "replicated file created"
4376
4377         cancel_lru_locks mdc
4378         cancel_lru_locks osc
4379
4380         log "open(O_RDONLY) and first read from OST"
4381         $MULTIOP $tf vvoO_RDONLY:r4_z0r4_z0r4c >$tmpfile &
4382         PID=$!
4383         sleep 1
4384         log "first read complete"
4385
4386         echo "BBBB" | dd bs=1 count=4 of=$DIR2/$tfile conv=notrunc ||
4387                 error "can't write BBBB"
4388         log "BBBB written which made replica on ost1 stale"
4389
4390         log "fast read from pagecache in the original process"
4391         kill -USR1 $PID
4392         sleep 1
4393
4394         log "read via $DIR2 new open(2)"
4395         $MULTIOP $DIR2/$tfile vvoO_RDONLY:r4c
4396
4397         log "fast read from pagecache after 5s in the original process"
4398         sleep 5
4399         kill -USR1 $PID
4400         wait $PID
4401         cat $tmpfile
4402         local nr=$(grep "BBBB" $tmpfile | wc -l)
4403         (( nr == 2 )) || {
4404                 cat $tmpfile
4405                 error "$nr != 2"
4406         }
4407
4408         log "read via new open(2)"
4409         $MULTIOP $tf vvoO_RDONLY:r4c
4410 }
4411 run_test 209a "skip fast reads after layout invalidation"
4412
4413 function sum_ost_reads() {
4414         $LCTL get_param -n osc.$FSNAME-OST*-osc-[-0-9a-f]*.stats |
4415                 awk '/^ost_read/{sum=sum+$2}END{print sum}'
4416 }
4417
4418 test_209b() {
4419         local tf=$DIR/$tfile
4420
4421         dd if=/dev/zero of=$tf bs=4k count=2 || error "can't create file"
4422         cancel_lru_locks osc
4423         echo "the very first read"
4424         cat $tf >/dev/null || error "can't read"
4425
4426         # cancel layout lock
4427         cancel_lru_locks mdc
4428
4429         # now read again, data must be in the cache, so no ost reads
4430         $LCTL set_param osc.*.stats=clear >/dev/null
4431         echo "read with warm cache"
4432         cat $tf >/dev/null || error "can't read"
4433         nr=$(sum_ost_reads)
4434         (( nr == 0 )) || error "reads with warm cache"
4435
4436         # now verify we can catch reads at all
4437         cancel_lru_locks osc
4438         cat $tf >/dev/null || error "can't read"
4439         nr=$(sum_ost_reads)
4440         (( nr > 0 )) || error "no reads with cold cache"
4441 }
4442 run_test 209b "pagecache can be used after LL cancellation"
4443
4444 test_210a() {
4445         local tf=$DIR/$tfile
4446
4447         stack_trap "rm -f $tf"
4448         dd if=/dev/zero of=$tf bs=1M count=1 || error "can't dd"
4449 #define OBD_FAIL_LOV_INVALID_OSTIDX                 0x1428
4450         do_facet mds1 "$LCTL set_param fail_loc=0x1428"
4451         $LFS mirror extend -N $tf || error "can't mirror"
4452         $LFS getstripe -v $tf
4453         stat $tf || error "can't stat"
4454 }
4455 run_test 210a "handle broken mirrored lovea"
4456
4457 test_210b() {
4458         local tf=$DIR/$tfile
4459         local after
4460         local mirrorred
4461         local before
4462
4463         [ "$FSTYPE" != "zfs" ] || skip "ZFS file number is not accurate"
4464
4465         stack_trap "rm -f $tf"
4466
4467         # XXX: how to avoid precreate
4468         wait_delete_completed
4469         $LFS df -i | grep OST
4470         before=$($LFS df -i|grep OST|awk '{sum=sum+$3}END{print sum}')
4471         echo "before file creation: $before"
4472         $LFS setstripe -c 2 $tf || error "can't create file"
4473         dd if=/dev/zero of=$tf bs=1M count=1 || error "can't dd"
4474
4475 #define OBD_FAIL_LOV_INVALID_OSTIDX                 0x1428
4476         do_facet mds1 "$LCTL set_param fail_loc=0x1428"
4477         $LFS mirror extend -N $tf || error "can't mirror"
4478         $LFS df -i | grep OST
4479         mirrored=$($LFS df -i|grep OST|awk '{sum=sum+$3}END{print sum}')
4480         echo "after mirror: $mirrored"
4481
4482         rm $tf || error "can't remove"
4483         [[ -f $tf ]] && error "rm failed"
4484         wait_delete_completed
4485
4486         $LFS df -i | grep OST
4487         after=$($LFS df -i|grep OST|awk '{sum=sum+$3}END{print sum}')
4488         echo "after rm: $after"
4489         (( after < before )) || error "something went wrong with unlink"
4490 }
4491 run_test 210b "handle broken mirrored lovea (unlink)"
4492
4493 complete_test $SECONDS
4494 check_and_cleanup_lustre
4495 exit_status