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