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