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