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