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