Whamcloud - gitweb
c26c20371dbe0221ce71a49bf598056254ddea64
[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-11381
18 ALWAYS_EXCEPT+="                  201 "
19 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
20
21 # skip all tests for PPC until we can get sanity-pfl to pass
22 if [[ $(uname -m) = ppc64 ]]; then
23         skip "Skip FLR testing for PPC clients"
24 fi
25
26 if [[ "$ost1_FSTYPE" == "zfs" ]]; then
27         # bug #:        LU-1941
28         ALWAYS_EXCEPT+="49a"
29 fi
30
31 build_test_filter
32
33 [[ "$MDS1_VERSION" -ge $(version_code 2.10.56) ]] ||
34         skip "Need MDS version at least 2.10.56"
35
36 check_and_setup_lustre
37 DIR=${DIR:-$MOUNT}
38 assert_DIR
39 rm -rf $DIR/[Rdfs][0-9]*
40
41 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
42         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
43
44 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
45
46
47 # global array to store mirror IDs
48 declare -a mirror_array
49 get_mirror_ids() {
50         local tf=$1
51         local id
52         local array
53
54         array=()
55         for id in $($LFS getstripe $tf | awk '/lcme_id/{print $2}'); do
56                 array[${#array[@]}]=$((id >> 16))
57         done
58
59         mirror_array=($(printf "%s\n" "${array[@]}" | sort -u))
60
61         echo ${#mirror_array[@]}
62 }
63
64 drop_client_cache() {
65         echo 3 > /proc/sys/vm/drop_caches
66 }
67
68 stop_osts() {
69         local idx
70
71         for idx in "$@"; do
72                 stop ost$idx
73         done
74
75         for idx in "$@"; do
76                 wait_osc_import_state client ost$idx "\(DISCONN\|IDLE\)"
77         done
78 }
79
80 start_osts() {
81         local idx
82
83         for idx in "$@"; do
84                 start ost$idx $(ostdevname $idx) $OST_MOUNT_OPTS ||
85                         error "start ost$idx failed"
86         done
87
88         for idx in "$@"; do
89                 wait_recovery_complete ost$idx
90         done
91 }
92
93 #
94 # Verify mirror count with an expected value for a given file.
95 #
96 verify_mirror_count() {
97         local tf=$1
98         local expected=$2
99         local mirror_count=$($LFS getstripe -N $tf)
100
101         [[ $mirror_count = $expected ]] || {
102                 $LFS getstripe -v $tf
103                 error "verify mirror count failed on $tf:" \
104                       "$mirror_count != $expected"
105         }
106 }
107
108 #
109 # Verify component count with an expected value for a given file.
110 #       $1 coposited layout file
111 #       $2 expected component number
112 #
113 verify_comp_count() {
114         local tf=$1
115         local expected=$2
116         local comp_count=$($LFS getstripe --component-count $tf)
117
118         [[ $comp_count = $expected ]] || {
119                 $LFS getstripe -v $tf
120                 error "verify component count failed on $tf:" \
121                       "$comp_count != $expected"
122         }
123 }
124
125 #
126 # Verify component attribute with an expected value for a given file
127 # and component ID.
128 #
129 verify_comp_attr() {
130         local attr=$1
131         local tf=$2
132         local comp_id=$3
133         local expected=$4
134         local cmd="$LFS getstripe -I$comp_id"
135         local getstripe_cmd="$cmd -v"
136         local value
137
138         case $attr in
139                 stripe-size) cmd+=" -S $tf" ;;
140                 stripe-count) cmd+=" -c $tf" ;;
141                 stripe-index) cmd+=" -i $tf" ;;
142                 pool) cmd+=" -p $tf" ;;
143                 comp-start) cmd+=" --component-start $tf" ;;
144                 comp-end) cmd+=" --component-end $tf" ;;
145                 lcme_flags) cmd+=" $tf | awk '/lcme_flags:/ { print \$2 }'" ;;
146                 *) error "invalid attribute $attr";;
147         esac
148
149         value=$(eval $cmd)
150
151         [ $attr = lcme_flags ] && {
152                 local fl
153                 local expected_list=$(comma_list $expected)
154                 for fl in ${expected_list//,/ }; do
155                         local neg=0
156
157                         [[ ${fl:0:1} = "^" ]] && neg=1
158                         [[ $neg = 1 ]] && fl=${fl:1}
159
160                         $(echo $value | grep -q $fl)
161                         local match=$?
162                         # 0: matched; 1: not matched
163
164                         if  [[ $neg = 0 && $match != 0 ||
165                                $neg = 1 && $match = 0 ]]; then
166                                 $getstripe_cmd $tf
167                                 [[ $neg = 0 ]] && # expect the flag
168                                     error "expected flag '$fl' not in $comp_id"
169                                 [[ $neg = 1 ]] && # not expect the flag
170                                     error "not expected flag '$fl' in $comp_id"
171                         fi
172                 done
173                 return
174         }
175
176         [[ $value = $expected ]] || {
177                 $getstripe_cmd $tf
178                 error "verify $attr failed on $tf: $value != $expected"
179         }
180 }
181
182 #
183 # Verify component extent with expected start and end extent values
184 # for a given file and component ID.
185 #
186 verify_comp_extent() {
187         local tf=$1
188         local comp_id=$2
189         local expected_start=$3
190         local expected_end=$4
191
192         verify_comp_attr comp-start $tf $comp_id $expected_start
193         verify_comp_attr comp-end $tf $comp_id $expected_end
194 }
195
196 #
197 # Verify component attribute with parent directory for a given file
198 # and component ID.
199 #
200 verify_comp_attr_with_parent() {
201         local attr=$1
202         local tf=$2
203         local comp_id=$3
204         local td=$(cd $(dirname $tf); echo $PWD)
205         local tf_cmd="$LFS getstripe -I$comp_id"
206         local td_cmd="$LFS getstripe"
207         local opt
208         local expected
209         local value
210
211         case $attr in
212                 stripe-size) opt="-S" ;;
213                 stripe-count) opt="-c" ;;
214                 pool) opt="-p" ;;
215                 *) error "invalid attribute $attr";;
216         esac
217
218         expected=$($td_cmd $opt $td)
219         [[ $expected = -1 ]] && expected=$OSTCOUNT
220
221         value=$($tf_cmd $opt $tf)
222         [[ $value = -1 ]] && value=$OSTCOUNT
223
224         [[ $value = $expected ]] || {
225                 $td_cmd -d $td
226                 $tf_cmd -v $tf
227                 error "verify $attr failed with parent on $tf:" \
228                       "$value != $expected"
229         }
230 }
231
232 #
233 # Verify component attribute with filesystem-wide default value for a given file
234 # and component ID.
235 #
236 verify_comp_attr_with_default() {
237         local attr=$1
238         local tf=$2
239         local comp_id=$3
240         local tf_cmd="$LFS getstripe -I$comp_id"
241         local opt
242         local expected
243         local value
244
245         case $attr in
246                 stripe-size)
247                         opt="-S"
248                         expected=$($LCTL get_param -n \
249                                    lov.$FSNAME-clilov-*.stripesize)
250                         ;;
251                 stripe-count)
252                         opt="-c"
253                         expected=$($LCTL get_param -n \
254                                    lov.$FSNAME-clilov-*.stripecount)
255                         [[ $expected = -1 ]] && expected=$OSTCOUNT
256                         ;;
257                 *) error "invalid attribute $attr";;
258         esac
259
260         value=$($tf_cmd $opt $tf)
261         [[ $value = -1 ]] && value=$OSTCOUNT
262
263         [[ $value = $expected ]] || {
264                 $tf_cmd -v $tf
265                 error "verify $attr failed with default value on $tf:" \
266                       "$value != $expected"
267         }
268 }
269
270 #
271 # Verify unspecified component attributes for a given file
272 # and component ID.
273 #
274 # This will only verify the inherited attributes:
275 # stripe size, stripe count and OST pool name
276 #
277 verify_comp_attrs() {
278         local tf=$1
279         local comp_id=$2
280
281         verify_comp_attr_with_default stripe-size $tf $comp_id
282         verify_comp_attr_with_default stripe-count $tf $comp_id
283         verify_comp_attr_with_parent pool $tf $comp_id
284 }
285
286 verify_flr_state()
287 {
288         local tf=$1
289         local expected_state=$2
290
291         local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
292         [ $expected_state = $state ] ||
293                 error "expected: $expected_state, actual $state"
294 }
295
296 # command line test cases
297 test_0a() {
298         local td=$DIR/$tdir
299         local tf=$td/$tfile
300         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
301         local mirror_cmd="$LFS mirror create"
302         local id
303         local ids
304         local i
305
306         # create parent directory
307         mkdir $td || error "mkdir $td failed"
308
309         $mirror_cmd $tf &> /dev/null && error "miss -N option"
310
311         $mirror_cmd -N $tf || error "create mirrored file $tf failed"
312         verify_mirror_count $tf 1
313         id=$($LFS getstripe -I $tf)
314         verify_comp_attrs $tf $id
315         verify_comp_extent $tf $id 0 EOF
316
317         $mirror_cmd -N0 $tf-1 &> /dev/null && error "invalid mirror count 0"
318         $mirror_cmd -N$((mirror_count + 1)) $tf-1 &> /dev/null &&
319                 error "invalid mirror count $((mirror_count + 1))"
320
321         $mirror_cmd -N$mirror_count $tf-1 ||
322                 error "create mirrored file $tf-1 failed"
323         verify_mirror_count $tf-1 $mirror_count
324         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
325         for ((i = 0; i < $mirror_count; i++)); do
326                 verify_comp_attrs $tf-1 ${ids[$i]}
327                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
328         done
329
330         $mirror_cmd -N -N2 -N3 -N4 $tf-2 ||
331                 error "create mirrored file $tf-2 failed"
332         verify_mirror_count $tf-2 10
333         ids=($($LFS getstripe $tf-2 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
334         for ((i = 0; i < 10; i++)); do
335                 verify_comp_attrs $tf-2 ${ids[$i]}
336                 verify_comp_extent $tf-2 ${ids[$i]} 0 EOF
337         done
338 }
339 run_test 0a "lfs mirror create with -N option"
340
341 test_0b() {
342         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
343
344         local td=$DIR/$tdir
345         local tf=$td/$tfile
346         local mirror_cmd="$LFS mirror create"
347         local ids
348         local i
349
350         # create a new OST pool
351         local pool_name=$TESTNAME
352         create_pool $FSNAME.$pool_name ||
353                 error "create OST pool $pool_name failed"
354
355         # add OSTs into the pool
356         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
357                 error "add OSTs into pool $pool_name failed"
358
359         # create parent directory
360         mkdir $td || error "mkdir $td failed"
361         $LFS setstripe -S 8M -c -1 -p $pool_name $td ||
362                 error "$LFS setstripe $td failed"
363
364         # create a mirrored file with plain layout mirrors
365         $mirror_cmd -N -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
366                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
367                 error "create mirrored file $tf failed"
368         verify_mirror_count $tf 6
369         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
370         for ((i = 0; i < 6; i++)); do
371                 verify_comp_extent $tf ${ids[$i]} 0 EOF
372         done
373
374         # verify component ${ids[0]}
375         verify_comp_attrs $tf ${ids[0]}
376
377         # verify component ${ids[1]}
378         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
379         verify_comp_attr stripe-count $tf ${ids[1]} 2
380         verify_comp_attr stripe-index $tf ${ids[1]} 2
381
382         # verify component ${ids[2]}
383         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
384         verify_comp_attr stripe-count $tf ${ids[2]} 2
385         verify_comp_attr pool $tf ${ids[2]} flash
386
387         # verify component ${ids[3]}
388         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
389         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
390         verify_comp_attr pool $tf ${ids[3]} flash
391
392         # verify component ${ids[4]}
393         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
394         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
395         verify_comp_attr pool $tf ${ids[4]} archive
396
397         # verify component ${ids[5]}
398         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
399         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
400         verify_comp_attr_with_parent pool $tf ${ids[5]}
401
402         if [ $MDS1_VERSION -ge $(version_code 2.12.55) ]; then
403                 # LU-11022 - remove mirror by pool name
404                 local=cnt cnt=$($LFS getstripe $tf | grep archive | wc -l)
405                 [ "$cnt" != "1" ] && error "unexpected mirror count $cnt"
406                 $LFS mirror delete --pool archive $tf || error "delete mirror"
407                 cnt=$($LFS getstripe $tf | grep archive | wc -l)
408                 [ "$cnt" != "0" ] && error "mirror count after removal: $cnt"
409         fi
410
411         # destroy OST pool
412         destroy_test_pools
413 }
414 run_test 0b "lfs mirror create plain layout mirrors"
415
416 test_0c() {
417         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
418
419         local td=$DIR/$tdir
420         local tf=$td/$tfile
421         local mirror_cmd="$LFS mirror create"
422         local ids
423         local i
424
425         # create a new OST pool
426         local pool_name=$TESTNAME
427         create_pool $FSNAME.$pool_name ||
428                 error "create OST pool $pool_name failed"
429
430         # add OSTs into the pool
431         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
432                 error "add OSTs into pool $pool_name failed"
433
434         # create parent directory
435         mkdir $td || error "mkdir $td failed"
436         $LFS setstripe -E 32M -S 8M -c -1 -p $pool_name -E eof -S 16M $td ||
437                 error "$LFS setstripe $td failed"
438
439         # create a mirrored file with composite layout mirrors
440         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
441                     -N -c 4 -p none \
442                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
443                 error "create mirrored file $tf failed"
444         verify_mirror_count $tf 6
445         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
446
447         # verify components ${ids[0]} and ${ids[2]}
448         for i in 0 2; do
449                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
450                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
451                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
452                 verify_comp_extent $tf ${ids[$i]} 0 4194304
453         done
454
455         # verify components ${ids[1]} and ${ids[3]}
456         for i in 1 3; do
457                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
458                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
459                 verify_comp_attr pool $tf ${ids[$i]} flash
460                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
461         done
462
463         # verify component ${ids[4]}
464         verify_comp_attr stripe-size $tf ${ids[4]} 4194304
465         verify_comp_attr stripe-count $tf ${ids[4]} 4
466         verify_comp_attr_with_parent pool $tf ${ids[4]}
467         verify_comp_extent $tf ${ids[4]} 0 EOF
468
469         # verify components ${ids[5]}, ${ids[7]} and ${ids[9]}
470         for i in 5 7 9; do
471                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
472                 verify_comp_attr stripe-count $tf ${ids[$i]} 4
473                 verify_comp_attr pool $tf ${ids[$i]} archive
474                 verify_comp_extent $tf ${ids[$i]} 0 536870912
475         done
476
477         # verify components ${ids[6]}, ${ids[8]} and ${ids[10]}
478         for i in 6 8 10; do
479                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
480                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
481                 verify_comp_attr pool $tf ${ids[$i]} archive
482                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
483         done
484
485         # destroy OST pool
486         destroy_test_pools
487 }
488 run_test 0c "lfs mirror create composite layout mirrors"
489
490 test_0d() {
491         local td=$DIR/$tdir
492         local tf=$td/$tfile
493         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
494         local mirror_cmd="$LFS mirror extend"
495         local ids
496         local i
497
498         # create parent directory
499         mkdir $td || error "mkdir $td failed"
500
501         $mirror_cmd $tf &> /dev/null && error "miss -N option"
502         $mirror_cmd -N $tf &> /dev/null && error "$tf does not exist"
503
504         # create a non-mirrored file, convert it to a mirrored file and extend
505         touch $tf || error "touch $tf failed"
506         $mirror_cmd -N $tf || error "convert and extend $tf failed"
507         verify_mirror_count $tf 2
508         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
509         for ((i = 0; i < 2; i++)); do
510                 verify_comp_attrs $tf ${ids[$i]}
511                 verify_comp_extent $tf ${ids[$i]} 0 EOF
512         done
513
514         lfsck_verify_pfid $tf || error "PFID is not set"
515
516         # create a mirrored file and extend it
517         $LFS mirror create -N $tf-1 || error "create mirrored file $tf-1 failed"
518         $LFS mirror create -N $tf-2 || error "create mirrored file $tf-2 failed"
519
520         $mirror_cmd -N -S 4M -N -f $tf-2 $tf-1 &> /dev/null &&
521                 error "setstripe options should not be specified with -f option"
522
523         $mirror_cmd -N$((mirror_count - 1)) $tf-1 ||
524                 error "extend mirrored file $tf-1 failed"
525         verify_mirror_count $tf-1 $mirror_count
526         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
527         for ((i = 0; i < $mirror_count; i++)); do
528                 verify_comp_attrs $tf-1 ${ids[$i]}
529                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
530         done
531
532         $mirror_cmd -N $tf-1 &> /dev/null &&
533                 error "exceeded maximum mirror count $mirror_count" || true
534 }
535 run_test 0d "lfs mirror extend with -N option"
536
537 test_0e() {
538         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
539
540         local td=$DIR/$tdir
541         local tf=$td/$tfile
542         local mirror_cmd="$LFS mirror extend"
543         local ids
544         local i
545
546         # create parent directory
547         mkdir $td || error "mkdir $td failed"
548
549         # create a mirrored file with plain layout mirrors
550         $LFS mirror create -N -S 32M -c 3 -p ssd -i 1 -o 1,2,3 $tf ||
551                 error "create mirrored file $tf failed"
552
553         # extend the mirrored file with plain layout mirrors
554         $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
555                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
556                 error "extend mirrored file $tf failed"
557         verify_mirror_count $tf 6
558         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
559         for ((i = 0; i < 6; i++)); do
560                 verify_comp_extent $tf ${ids[$i]} 0 EOF
561         done
562
563         # verify component ${ids[0]}
564         verify_comp_attr stripe-size $tf ${ids[0]} 33554432
565         verify_comp_attr stripe-count $tf ${ids[0]} 3
566         verify_comp_attr stripe-index $tf ${ids[0]} 1
567
568         # verify component ${ids[1]}
569         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
570         verify_comp_attr stripe-count $tf ${ids[1]} 2
571         verify_comp_attr stripe-index $tf ${ids[1]} 2
572
573         # verify component ${ids[2]}
574         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
575         verify_comp_attr stripe-count $tf ${ids[2]} 2
576         verify_comp_attr pool $tf ${ids[2]} flash
577
578         # verify component ${ids[3]}
579         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
580         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
581         verify_comp_attr pool $tf ${ids[3]} flash
582
583         # verify component ${ids[4]}
584         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
585         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
586         verify_comp_attr pool $tf ${ids[4]} archive
587
588         # verify component ${ids[5]}
589         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
590         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
591         verify_comp_attr_with_parent pool $tf ${ids[5]}
592 }
593 run_test 0e "lfs mirror extend plain layout mirrors"
594
595 test_0f() {
596         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
597
598         local td=$DIR/$tdir
599         local tf=$td/$tfile
600         local mirror_cmd="$LFS mirror extend"
601         local ids
602         local i
603
604         # create parent directory
605         mkdir $td || error "mkdir $td failed"
606
607         # create a mirrored file with composite layout mirror
608         $LFS mirror create -N -E 32M -S 16M -p ssd -E eof -S 32M $tf ||
609                 error "create mirrored file $tf failed"
610
611         # extend the mirrored file with composite layout mirrors
612         $mirror_cmd -N -p archive \
613                     -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
614                     -N -c -1 -p none \
615                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
616                 error "extend mirrored file $tf failed"
617         verify_mirror_count $tf 8
618         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
619
620         # verify component ${ids[0]}
621         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
622         verify_comp_attr_with_default stripe-count $tf ${ids[0]}
623         verify_comp_attr pool $tf ${ids[0]} ssd
624         verify_comp_extent $tf ${ids[0]} 0 33554432
625
626         # verify component ${ids[1]}
627         verify_comp_attr stripe-size $tf ${ids[1]} 33554432
628         verify_comp_attr_with_default stripe-count $tf ${ids[1]}
629         verify_comp_attr pool $tf ${ids[1]} ssd
630         verify_comp_extent $tf ${ids[1]} 33554432 EOF
631
632         # verify component ${ids[2]}
633         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
634         verify_comp_attr_with_default stripe-count $tf ${ids[2]}
635         verify_comp_attr pool $tf ${ids[2]} archive
636         verify_comp_extent $tf ${ids[2]} 0 EOF
637
638         # verify components ${ids[3]} and ${ids[5]}
639         for i in 3 5; do
640                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
641                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
642                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
643                 verify_comp_extent $tf ${ids[$i]} 0 4194304
644         done
645
646         # verify components ${ids[4]} and ${ids[6]}
647         for i in 4 6; do
648                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
649                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
650                 verify_comp_attr pool $tf ${ids[$i]} flash
651                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
652         done
653
654         # verify component ${ids[7]}
655         verify_comp_attr stripe-size $tf ${ids[7]} 4194304
656         verify_comp_attr stripe-count $tf ${ids[7]} $OSTCOUNT
657         verify_comp_attr_with_parent pool $tf ${ids[7]}
658         verify_comp_extent $tf ${ids[7]} 0 EOF
659
660         # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
661         for i in 8 10 12; do
662                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
663                 verify_comp_attr stripe-count $tf ${ids[$i]} $OSTCOUNT
664                 verify_comp_attr pool $tf ${ids[$i]} archive
665                 verify_comp_extent $tf ${ids[$i]} 0 536870912
666         done
667
668         # verify components ${ids[9]}, ${ids[11]} and ${ids[13]}
669         for i in 9 11 13; do
670                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
671                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
672                 verify_comp_attr pool $tf ${ids[$i]} archive
673                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
674         done
675 }
676 run_test 0f "lfs mirror extend composite layout mirrors"
677
678 test_0g() {
679         local tf=$DIR/$tfile
680
681         ! $LFS mirror create --flags prefer $tf ||
682                 error "creating $tf w/ --flags but w/o -N option should fail"
683
684         ! $LFS mirror create -N --flags foo $tf ||
685                 error "creating $tf with '--flags foo' should fail"
686
687         ! $LFS mirror create -N --flags stale $tf ||
688                 error "creating $tf with '--flags stale' should fail"
689
690         ! $LFS mirror create -N --flags prefer,init $tf ||
691                 error "creating $tf with '--flags prefer,init' should fail"
692
693         ! $LFS mirror create -N --flags ^prefer $tf ||
694                 error "creating $tf with '--flags ^prefer' should fail"
695
696         $LFS mirror create -N -E 1M -S 1M -o0 --flags=prefer -E eof -o1 \
697                            -N -o1 $tf || error "create mirrored file $tf failed"
698
699         verify_comp_attr lcme_flags $tf 0x10001 prefer
700         verify_comp_attr lcme_flags $tf 0x10002 prefer
701
702         # write to the mirrored file and check primary
703         cp /etc/hosts $tf || error "error writing file '$tf'"
704
705         verify_comp_attr lcme_flags $tf 0x20003 stale
706
707         # resync file and check prefer flag
708         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
709
710         cancel_lru_locks osc
711         $LCTL set_param osc.*.stats=clear
712         cat $tf &> /dev/null || error "error reading file '$tf'"
713
714         # verify that the data was provided by OST1 where mirror 1 resides
715         local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-[-0-9a-f]*.stats |
716                         awk '/ost_read/{print $2}')
717         [ -n "$nr_read" ] || error "read was not provided by OST1"
718 }
719 run_test 0g "lfs mirror create flags support"
720
721 test_0h() {
722         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
723                 skip "Need MDS version at least 2.11.57"
724
725         local td=$DIR/$tdir
726         local tf=$td/$tfile
727         local ids
728         local i
729
730         # create parent directory
731         test_mkdir $td || error "mkdir $td failed"
732
733         $LFS setstripe -N -E 1M -S 1M --flags=prefer -E eof -N2 $td ||
734                 error "set default mirrored layout on directory $td failed"
735
736         # verify flags are inherited from the directory
737         touch $tf
738
739         verify_comp_attr lcme_flags $tf 0x10001 prefer
740         verify_comp_attr lcme_flags $tf 0x10002 prefer
741
742         # set flags to the first component
743         ! $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,foo $tf ||
744                 error "setting '^prefer,foo' flags should fail"
745
746         ! $LFS getstripe --component-flags=prefer,foo $tf ||
747                 error "getting component(s) with 'prefer,foo' flags should fail"
748
749         $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,stale $tf
750
751         verify_comp_attr lcme_flags $tf 0x10001 stale
752         verify_comp_attr lcme_flags $tf 0x10002 prefer
753
754         $LFS setstripe --comp-set -I0x10001 --comp-flags=^stale $tf &&
755                 error "clearing 'stale' should fail"
756
757         # write and resync file. It can't resync the file directly because the
758         # file state is still 'ro'
759         cp /etc/hosts $tf || error "error writing file '$tf'"
760         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
761
762         $LFS setstripe --comp-set -I 0x20003 --comp-flags=prefer $tf ||
763                 error "error setting flag prefer"
764
765         verify_comp_attr lcme_flags $tf 0x20003 prefer
766
767         $LFS setstripe --comp-set -I 0x20003 --comp-flags=^prefer $tf ||
768                 error "error clearing prefer flag from component 0x20003"
769
770         # MDS disallows setting stale flag on the last non-stale mirror
771         [[ "$MDS1_VERSION" -ge $(version_code 2.12.57) ]] || return 0
772
773         cp /etc/hosts $tf || error "error writing file '$tf'"
774
775         verify_comp_attr lcme_flags $tf 0x10002 prefer
776         verify_comp_attr lcme_flags $tf 0x20003 stale
777         verify_comp_attr lcme_flags $tf 0x30004 stale
778
779         ! $LFS setstripe --comp-set -I 0x10002 --comp-flags=^prefer,stale $tf \
780                 > /dev/null 2>&1 ||
781                 error "setting stale flag on component 0x10002 should fail"
782
783         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
784
785         $LFS setstripe --comp-set -I 0x10001 --comp-flags=stale $tf ||
786                 error "error setting stale flag on component 0x10001"
787         $LFS setstripe --comp-set -I 0x20003 --comp-flags=stale $tf ||
788                 error "error setting stale flag on component 0x20003"
789
790         ! $LFS setstripe --comp-set -I 0x30004 --comp-flags=stale $tf \
791                 > /dev/null 2>&1 ||
792                 error "setting stale flag on component 0x30004 should fail"
793
794         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
795 }
796 run_test 0h "set, clear and test flags for FLR files"
797
798 test_0j() {
799         $LFS mirror create -N2 $DIR/$tfile || error "create $DIR/$tfile failed"
800
801         cp /etc/hosts $DIR/$tfile || error "write to $DIR/$tfile failed"
802         $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed"
803         cmp /etc/hosts $DIR/$tfile || error "cmp with /etc/hosts failed"
804
805         $LFS mirror read -N2 -o $TMP/$tfile $DIR/$tfile || "read mirror failed"
806         stack_trap "rm -f $TMP/$tfile"
807         cmp $TMP/$tfile $DIR/$tfile || error "cmp with $TMP/$tfile failed"
808         $LFS mirror write -N2 -i /etc/passwd $DIR/$tfile || "write failed"
809         $LFS setstripe --comp-set -I 65537 --comp-flags=stale $DIR/$tfile ||
810                 error "set component 1 stale failed"
811         $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed"
812         cmp /etc/passwd $DIR/$tfile || error "cmp with /etc/passwd failed"
813 }
814 run_test 0j "test lfs mirror read/write commands"
815
816 test_1() {
817         local tf=$DIR/$tfile
818         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
819         local mirror_create_cmd="$LFS mirror create"
820         local stripes[0]=$OSTCOUNT
821
822         mirror_create_cmd+=" -N -c ${stripes[0]}"
823         for ((i = 1; i < $mirror_count; i++)); do
824                 # add mirrors with different stripes to the file
825                 stripes[$i]=$((RANDOM % OSTCOUNT))
826                 [ ${stripes[$i]} -eq 0 ] && stripes[$i]=1
827
828                 mirror_create_cmd+=" -N -c ${stripes[$i]}"
829         done
830
831         $mirror_create_cmd $tf || error "create mirrored file $tf failed"
832         verify_mirror_count $tf $mirror_count
833
834         # can't create mirrors exceeding LUSTRE_MIRROR_COUNT_MAX
835         $LFS mirror extend -N $tf &&
836                 error "Creating the $((mirror_count+1))th mirror succeeded"
837
838         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
839                         tr '\n' ' '))
840
841         # verify the range of components and stripe counts
842         for ((i = 0; i < $mirror_count; i++)); do
843                 verify_comp_attr stripe-count $tf ${ids[$i]} ${stripes[$i]}
844                 verify_comp_extent $tf ${ids[$i]} 0 EOF
845         done
846 }
847 run_test 1 "create components with setstripe options"
848
849 test_2() {
850         local tf=$DIR/$tfile
851         local tf2=$DIR/$tfile-2
852
853         $LFS setstripe -E 1M -S 1M -E EOF -c 1 $tf
854         $LFS setstripe -E 2M -S 1M -E EOF -c -1 $tf2
855
856         $LFS mirror extend -N -f $tf2 $tf ||
857                 error "merging $tf2 into $tf failed"
858
859         verify_mirror_count $tf 2
860         [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
861 }
862 run_test 2 "create components from existing files"
863
864 test_3() {
865         [[ $MDSCOUNT -lt 2 ]] && skip "need >= 2 MDTs" && return
866
867         for ((i = 0; i < 2; i++)); do
868                 $LFS mkdir -i $i $DIR/$tdir-$i
869                 $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
870         done
871
872         $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
873                 $DIR/$tdir-0/$tfile || error "creating mirrors"
874
875         # mdt doesn't support to cancel layout lock for remote objects, do
876         # it here manually.
877         cancel_lru_locks mdc
878
879         # make sure the mirrorted file was created successfully
880         [[ $($LFS getstripe --component-count $DIR/$tdir-0/$tfile) -eq 2 ]] ||
881                 { $LFS getstripe $DIR/$tdir-0/$tfile;
882                         error "expected 2 components"; }
883
884         # cleanup
885         rm -rf $DIR/$tdir-*
886 }
887 run_test 3 "create components from files located on different MDTs"
888
889 test_4() {
890         local tf=$DIR/$tdir/$tfile
891         local ids=()
892
893         test_mkdir $DIR/$tdir
894
895         # set mirror with setstripe options to directory
896         $LFS mirror create -N2 -E 1M -S 1M -E eof $DIR/$tdir ||
897                 error "set mirror to directory error"
898
899         [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
900                 x"mirrored" ] || error "failed to create mirrored dir"
901
902         touch $tf
903         verify_mirror_count $tf 2
904
905         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
906         verify_comp_extent $tf ${ids[0]} 0 1048576
907         verify_comp_extent $tf ${ids[1]} 1048576 EOF
908
909         # sub directory should inherit mirror setting from parent
910         test_mkdir $DIR/$tdir/td
911         [ x$($LFS getstripe -v $DIR/$tdir/td | awk '/lcm_flags/{print $2}') = \
912                 x"mirrored" ] || error "failed to inherit mirror from parent"
913
914         # mirror extend won't be applied to directory
915         $LFS mirror extend -N2 $DIR/$tdir &&
916                 error "expecting mirror extend failure"
917         true
918 }
919 run_test 4 "Make sure mirror attributes can be inhertied from directory"
920
921 test_5() {
922         local tf=$DIR/$tfile
923         local ids=()
924
925         $MULTIOP $tf oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T12345c ||
926                 error "failed to create file with non-empty layout"
927         $CHECKSTAT -t file -s 12345 $tf || error "size error: expecting 12345"
928
929         $LFS mirror create -N3 $tf || error "failed to attach mirror layout"
930         verify_mirror_count $tf 3
931
932         $CHECKSTAT -t file -s 12345 $tf ||
933                 error "size error after attaching layout "
934 }
935 run_test 5 "Make sure init size work for mirrored layout"
936
937 # LU=10112: disable dom+flr for phase 1
938 test_6() {
939         local tf=$DIR/$tfile
940
941         $LFS mirror create -N -E 1M -S 1M -L mdt -E eof -N -E eof $tf &&
942                 error "expect failure to create mirrored file with DoM"
943
944         $LFS mirror create -N -E 1M -S 1M -E eof -N -E 1M -L mdt -E eof $tf &&
945                 error "expect failure to create mirrored file with DoM"
946
947         $LFS setstripe -E 1M -S 1M -L mdt -E eof $tf
948         $LFS mirror extend -N2 $tf &&
949                 error "expect failure to extend mirror with DoM"
950
951         $LFS mirror create -N2 -E 1M -S 1M -E eof $tf-2
952         $LFS mirror extend -N -f $tf $tf-2 &&
953                 error "expect failure to extend mirrored file with DoM extent"
954
955         true
956 }
957 run_test 6 "DoM and FLR won't co-exist for phase 1"
958
959 test_21() {
960         local tf=$DIR/$tfile
961         local tf2=$DIR/$tfile-2
962
963         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
964
965         $LFS setstripe -E EOF -o 0 $tf
966         $LFS setstripe -E EOF -o 1 $tf2
967
968         local dd_count=$((RANDOM % 20 + 1))
969         dd if=/dev/zero of=$tf bs=1M count=$dd_count oflag=sync
970         dd if=/dev/zero of=$tf2 bs=1M count=1 seek=$((dd_count - 1)) oflag=sync
971
972         # for zfs - sync OST dataset so that du below will return
973         # accurate results
974         [ "$FSTYPE" = "zfs" ] &&
975                 do_nodes $(comma_list $(osts_nodes)) "$ZPOOL sync"
976
977         local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
978
979         # add component
980         $LFS mirror extend -N -f $tf2 $tf ||
981                 error "merging $tf2 into $tf failed"
982
983         # cancel layout lock
984         cancel_lru_locks mdc
985
986         local new_blocks=$(du -k $tf | awk '{print $1}')
987         [ $new_blocks -eq $blocks ] ||
988         error "i_blocks error expected: $blocks, actual: $new_blocks"
989 }
990 run_test 21 "glimpse should report accurate i_blocks"
991
992 get_osc_lock_count() {
993         local lock_count=0
994
995         for idx in "$@"; do
996                 local osc_name
997                 local count
998
999                 osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'[-0-9a-f]*'
1000                 count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
1001                 lock_count=$((lock_count + count))
1002         done
1003         echo $lock_count
1004 }
1005
1006 test_22() {
1007         local tf=$DIR/$tfile
1008
1009         $LFS setstripe -E EOF -o 0 $tf
1010         dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
1011
1012         # add component, two mirrors located on the same OST ;-)
1013         $LFS mirror extend -N -o 0 $tf ||
1014                 error "extending mirrored file $tf failed"
1015
1016         size_blocks=$(stat --format="%b %s" $tf)
1017
1018         cancel_lru_locks mdc
1019         cancel_lru_locks osc
1020
1021         local new_size_blocks=$(stat --format="%b %s" $tf)
1022
1023         # make sure there is no lock cached
1024         [ $(get_osc_lock_count 1) -eq 0 ] || error "glimpse requests were sent"
1025
1026         [ "$new_size_blocks" = "$size_blocks" ] ||
1027                 echo "size expected: $size_blocks, actual: $new_size_blocks"
1028
1029         rm -f $tmpfile
1030 }
1031 run_test 22 "no glimpse to OSTs for READ_ONLY files"
1032
1033 test_31() {
1034         local tf=$DIR/$tfile
1035
1036         $LFS mirror create -N -o 0 -N -o 1 $tf ||
1037                 error "creating mirrored file $tf failed"
1038
1039         #define OBD_FAIL_GLIMPSE_IMMUTABLE 0x1A00
1040         $LCTL set_param fail_loc=0x1A00
1041
1042         local ost_idx
1043         for ((ost_idx = 1; ost_idx <= 2; ost_idx++)); do
1044                 cancel_lru_locks osc
1045                 stop_osts $ost_idx
1046
1047                 local tmpfile=$(mktemp)
1048                 stat --format="%b %s" $tf > $tmpfile  &
1049                 local pid=$!
1050
1051                 local cnt=0
1052                 while [ $cnt -le 5 ]; do
1053                         kill -0 $pid > /dev/null 2>&1 || break
1054                         sleep 1
1055                         ((cnt += 1))
1056                 done
1057                 kill -0 $pid > /dev/null 2>&1 &&
1058                         error "stat process stuck due to unavailable OSTs"
1059
1060                 # make sure glimpse request has been sent
1061                 [ $(get_osc_lock_count 1 2) -ne 0 ] ||
1062                         error "OST $ost_idx: no glimpse request was sent"
1063
1064                 start_osts $ost_idx
1065         done
1066 }
1067 run_test 31 "make sure glimpse request can be retried"
1068
1069 test_32() {
1070         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1071         rm -f $DIR/$tfile $DIR/$tfile-2
1072
1073         $LFS setstripe -E EOF -o 0 $DIR/$tfile
1074         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=$((RANDOM % 10 + 2))
1075
1076         local fsize=$(stat -c %s $DIR/$tfile)
1077         [[ $fsize -ne 0 ]] || error "file size is (wrongly) zero"
1078
1079         local cksum=$(md5sum $DIR/$tfile)
1080
1081         # create a new mirror in sync mode
1082         $LFS mirror extend -N -o 1 $DIR/$tfile ||
1083                 error "extending mirrored file $DIR/$tfile failed"
1084
1085         # make sure the mirrored file was created successfully
1086         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1087                 { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
1088
1089         drop_client_cache
1090         stop_osts 1
1091
1092         # check size is correct, glimpse request should go to the 2nd mirror
1093         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1094                 error "file size error $fsize vs. $(stat -c %s $DIR/$tfile)"
1095
1096         echo "reading file from the 2nd mirror and verify checksum"
1097         [[ "$cksum" == "$(md5sum $DIR/$tfile)" ]] ||
1098                 error "checksum error: expected $cksum"
1099
1100         start_osts 1
1101 }
1102 run_test 32 "data should be mirrored to newly created mirror"
1103
1104 test_33a() {
1105         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1106
1107         rm -f $DIR/$tfile $DIR/$tfile-2
1108
1109         # create a file with two mirrors
1110         $LFS setstripe -E EOF -o 0 $DIR/$tfile
1111         local max_count=100
1112         local count=0
1113         while [ $count -lt $max_count ]; do
1114                 echo "ost1" >> $DIR/$tfile
1115                 count=$((count + 1));
1116         done
1117
1118         # tmp file that will be used as mirror
1119         $LFS setstripe -E EOF -o 1 $DIR/$tfile-2
1120         count=0
1121         while [ $count -lt $max_count ]; do
1122                 echo "ost2" >> $DIR/$tfile-2
1123                 count=$((count + 1));
1124         done
1125
1126         # create a mirrored file
1127         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
1128                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1129                       "with verification should fail"
1130         $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
1131                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1132                       "without verification failed"
1133
1134         # make sure that $tfile has two mirrors and $tfile-2 does not exist
1135         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1136                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1137
1138         [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
1139
1140         # execpted file size
1141         local fsize=$((5 * max_count))
1142         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1143                 error "mirrored file size is not $fsize"
1144
1145         # read file - all OSTs are available
1146         echo "reading file (data can be provided by any ost)... "
1147         local rs=$(cat $DIR/$tfile | head -1)
1148         [[ "$rs" == "ost1" || "$rs" == "ost2" ]] ||
1149                 error "file content error: expected: \"ost1\" or \"ost2\""
1150
1151         # read file again with ost1 failed
1152         stop_osts 1
1153         drop_client_cache
1154
1155         echo "reading file (data should be provided by ost2)..."
1156         local rs=$(cat $DIR/$tfile | head -1)
1157         [[ "$rs" == "ost2" ]] ||
1158                 error "file content error: expected: \"ost2\", actual: \"$rs\""
1159
1160         # remount ost1
1161         start_osts 1
1162
1163         # read file again with ost2 failed
1164         stop_osts 2
1165         drop_client_cache
1166
1167         # check size, glimpse should work
1168         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1169                 error "mirrored file size is not $fsize"
1170
1171         echo "reading file (data should be provided by ost1)..."
1172         local rs=$(cat $DIR/$tfile | head -1)
1173         [[ "$rs" == "ost1" ]] ||
1174                 error "file content error: expected: \"ost1\", actual: \"$rs\""
1175
1176         start_osts 2
1177 }
1178 run_test 33a "read can choose available mirror to read"
1179
1180 test_33b() {
1181         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1182
1183         rm -f $DIR/$tfile
1184
1185         stack_trap "rm -f $DIR/$tfile" EXIT
1186
1187         # create a file with two mirrors on OST0000 and OST0001
1188         $LFS setstripe -N -Eeof -o0 -N -Eeof -o1 $DIR/$tfile
1189
1190         # make sure that $tfile has two mirrors
1191         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1192                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1193
1194         # write 50M
1195         dd if=/dev/urandom of=$DIR/$tfile bs=2M count=25 ||
1196                 error "write failed for $DIR/$tfile"
1197         $LFS mirror resync $DIR/$tfile || error "resync failed for $DIR/$tfile"
1198         verify_flr_state $DIR/$tfile "ro"
1199         drop_client_cache
1200
1201         ls -l $DIR/$tfile
1202
1203         # read file - all OSTs are available
1204         echo "reading file (data can be provided by any ost)... "
1205         local t1=$SECONDS
1206         time cat $DIR/$tfile > /dev/null || error "read all"
1207         local t2=$SECONDS
1208         ra=$((t2 - t1))
1209
1210         # read file again with ost1 {OST0000} failed
1211         stop_osts 1
1212         drop_client_cache
1213         echo "reading file (data should be provided by ost2)..."
1214         t1=$SECONDS
1215         time cat $DIR/$tfile > /dev/null || error "read ost2"
1216         t2=$SECONDS
1217         r1=$((t2 - t1))
1218
1219         # remount ost1
1220         start_osts 1
1221
1222         # read file again with ost2 {OST0001} failed
1223         stop_osts 2
1224         drop_client_cache
1225
1226         echo "reading file (data should be provided by ost1)..."
1227         t1=$SECONDS
1228         time cat $DIR/$tfile > /dev/null || error "read ost1"
1229         t2=$SECONDS
1230         r2=$((t2 - t1))
1231
1232         # remount ost2
1233         start_osts 2
1234
1235         [ $((r1 * 100)) -gt $((ra * 105)) -a $r1 -gt $((ra + 2)) ] &&
1236                 error "read mirror too slow without ost1, from $ra to $r1"
1237         [ $((r2 * 100)) -gt $((ra * 105)) -a $r2 -gt $((ra + 2)) ] &&
1238                 error "read mirror too slow without ost2, from $ra to $r2"
1239
1240         wait_osc_import_ready client ost2
1241 }
1242 run_test 33b "avoid reading from unhealthy mirror"
1243
1244 test_33c() {
1245         [[ $OSTCOUNT -lt 3 ]] && skip "need >= 3 OSTs" && return
1246
1247         rm -f $DIR/$tfile
1248
1249         stack_trap "rm -f $DIR/$tfile" EXIT
1250
1251         # create a file with two mirrors
1252         # mirror1: {OST0000, OST0001}
1253         # mirror2: {OST0001, OST0002}
1254         $LFS setstripe -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 $DIR/$tfile
1255
1256         # make sure that $tfile has two mirrors
1257         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1258                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1259
1260         # write 50M
1261         dd if=/dev/urandom of=$DIR/$tfile bs=2M count=25 ||
1262                 error "write failed for $DIR/$tfile"
1263         $LFS mirror resync $DIR/$tfile || error "resync failed for $DIR/$tfile"
1264         verify_flr_state $DIR/$tfile "ro"
1265         drop_client_cache
1266
1267         ls -l $DIR/$tfile
1268
1269         # read file - all OSTs are available
1270         echo "reading file (data can be provided by any ost)... "
1271         time cat $DIR/$tfile > /dev/null || error "read all"
1272
1273         # read file again with ost2 (OST0001) failed
1274         stop_osts 2
1275         drop_client_cache
1276
1277         echo "reading file (data should be provided by ost1 and ost3)..."
1278         time cat $DIR/$tfile > /dev/null || error "read ost1 & ost3"
1279
1280         # remount ost2
1281         start_osts 2
1282
1283         wait_osc_import_ready client ost2
1284 }
1285 run_test 33c "keep reading among unhealthy mirrors"
1286
1287 test_34a() {
1288         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1289
1290         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1291
1292         # reference file
1293         $LFS setstripe -o 0 $DIR/$tfile-ref
1294         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1295
1296         # create a file with two mirrors
1297         $LFS setstripe -E -1 -o 0,1 -S 1M $DIR/$tfile
1298         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1299
1300         $LFS setstripe -E -1 -o 2,3 -S 1M $DIR/$tfile-2
1301         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1302
1303         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1304                 error "mirrored file size is not 3M"
1305
1306         # merge a mirrored file
1307         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1308                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1309
1310         cancel_lru_locks osc
1311
1312         # stop two OSTs, so the 2nd stripe of the 1st mirror and
1313         # the 1st stripe of the 2nd mirror will be inaccessible, ...
1314         stop_osts 2 3
1315
1316         echo "comparing files ... "
1317
1318         # however, read can still return the correct data. It should return
1319         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1320         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1321                 $DIR/$tfile-ref || error "file reading error"
1322
1323         start_osts 2 3
1324 }
1325 run_test 34a "read mirrored file with multiple stripes"
1326
1327 test_34b() {
1328         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1329
1330         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1331
1332         # reference file
1333         $LFS setstripe -o 0 $DIR/$tfile-ref
1334         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1335
1336         $LFS setstripe -E 1M -S 1M -o 0 -E eof -o 1 $DIR/$tfile
1337         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1338
1339         $LFS setstripe -E 1M -S 1M -o 2 -E eof -o 3 $DIR/$tfile-2
1340         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1341
1342         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1343                 error "mirrored file size is not 3M"
1344
1345         # merge a mirrored file
1346         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1347                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1348
1349         cancel_lru_locks osc
1350
1351         # stop two OSTs, so the 2nd component of the 1st mirror and
1352         # the 1st component of the 2nd mirror will be inaccessible, ...
1353         stop_osts 2 3
1354
1355         echo "comparing files ... "
1356
1357         # however, read can still return the correct data. It should return
1358         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1359         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1360                 $DIR/$tfile-ref || error "file reading error"
1361
1362         start_osts 2 3
1363 }
1364 run_test 34b "read mirrored file with multiple components"
1365
1366 test_35() {
1367         local tf=$DIR/$tfile
1368
1369         $LFS setstripe -E eof $tf
1370
1371         # add an out-of-sync mirror to the file
1372         $LFS mirror extend -N -c 2 $tf ||
1373                 error "extending mirrored file $tf failed"
1374
1375         $MULTIOP $tf oO_WRONLY:c ||
1376                 error "write open a mirrored file failed"
1377
1378         # truncate file should return error
1379         $TRUNCATE $tf 100 || error "error truncating a mirrored file"
1380 }
1381 run_test 35 "allow to write to mirrored files"
1382
1383 verify_ost_layout_version() {
1384         local tf=$1
1385
1386         # get file layout version
1387         local flv=$($LFS getstripe $tf | awk '/lcm_layout_gen/{print $2}')
1388
1389         # layout version from OST objects
1390         local olv=$($MULTIOP $tf oXc | awk '/ostlayoutversion/{print $2}')
1391
1392         [ $flv -eq $olv ] || error "layout version mismatch: $flv vs. $olv"
1393 }
1394
1395 create_file_36() {
1396         local tf
1397
1398         for tf in "$@"; do
1399                 $LFS setstripe -E 1M -S 1M -E 2M -E 4M -E eof -c -1 $tf
1400                 $LFS setstripe -E 3M -S 1M -E 6M -E eof -c -1 $tf-tmp
1401
1402                 $LFS mirror extend -N -f $tf-tmp $tf ||
1403                         error "merging $tf-tmp into $tf failed"
1404         done
1405 }
1406
1407 test_36() {
1408         local tf=$DIR/$tfile
1409
1410         create_file_36 $tf $tf-2 $tf-3
1411
1412         [ $($LFS getstripe -N $tf) -gt 1 ] || error "wrong mirror count"
1413
1414         # test case 1 - check file write and verify layout version
1415         $MULTIOP $tf oO_WRONLY:c ||
1416                 error "write open a mirrored file failed"
1417
1418         # write open file should not return error
1419         $MULTIOP $tf oO_WRONLY:w1024Yc || error "write mirrored file error"
1420
1421         # instantiate components should work
1422         dd if=/dev/zero of=$tf bs=1M count=12 || error "write file error"
1423
1424         # verify OST layout version
1425         verify_ost_layout_version $tf
1426
1427         # test case 2
1428         local mds_facet=mds$(($($LFS getstripe -m $tf-2) + 1))
1429
1430         local delay_sec=10
1431         do_facet $mds_facet $LCTL set_param fail_val=$delay_sec
1432
1433         #define OBD_FAIL_FLR_LV_DELAY 0x1A01
1434         do_facet $mds_facet $LCTL set_param fail_loc=0x1A01
1435
1436         # write should take at least $fail_loc seconds and succeed
1437         local st=$(date +%s)
1438         $MULTIOP $tf-2 oO_WRONLY:w1024Yc || error "write mirrored file error"
1439
1440         [ $(date +%s) -ge $((st+delay_sec)) ] ||
1441                 error "write finished before layout version is transmitted"
1442
1443         # verify OST layout version
1444         verify_ost_layout_version $tf
1445
1446         do_facet $mds_facet $LCTL set_param fail_loc=0
1447
1448         # test case 3
1449         mds_idx=mds$(($($LFS getstripe -m $tf-3) + 1))
1450
1451         #define OBD_FAIL_FLR_LV_INC 0x1A02
1452         do_facet $mds_facet $LCTL set_param fail_loc=0x1A02
1453
1454         # write open file should return error
1455         $MULTIOP $tf-3 oO_WRONLY:O_SYNC:w1024c &&
1456                 error "write a mirrored file succeeded" || true
1457
1458         do_facet $mds_facet $LCTL set_param fail_loc=0
1459 }
1460 run_test 36 "write to mirrored files"
1461
1462 create_files_37() {
1463         local tf
1464         local fsize=$1
1465
1466         echo "create test files with size $fsize .."
1467
1468         shift
1469         for tf in "$@"; do
1470                 $LFS setstripe -E 1M -S 1M -c 1 -E eof -c -1 $tf
1471
1472                 dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
1473                 $TRUNCATE $tf $fsize
1474         done
1475 }
1476
1477 test_37()
1478 {
1479         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
1480                 skip "Need MDS version at least 2.11.57"
1481
1482         local tf=$DIR/$tfile
1483         local tf2=$DIR/$tfile-2
1484         local tf3=$DIR/$tfile-3
1485         local tf4=$DIR/$tfile-4
1486
1487         create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
1488         rm -f $tf4
1489         cp $tf $tf4
1490
1491         # assume the mirror id will be 1, 2, and 3
1492         declare -A checksums
1493         checksums[1]=$(cat $tf | md5sum)
1494         checksums[2]=$(cat $tf2 | md5sum)
1495         checksums[3]=$(cat $tf3 | md5sum)
1496
1497         printf '%s\n' "${checksums[@]}"
1498
1499         # merge these files into a mirrored file
1500         $LFS mirror extend --no-verify -N -f $tf2 $tf ||
1501                 error "merging $tf2 into $tf failed"
1502         $LFS mirror extend --no-verify -N -f $tf3 $tf ||
1503                 error "merging $tf3 into $tf failed"
1504
1505         get_mirror_ids $tf
1506
1507         # verify mirror read, checksums should equal to the original files'
1508         echo "Verifying mirror read .."
1509
1510         local sum
1511         for i in ${mirror_array[@]}; do
1512                 $LCTL set_param ldlm.namespaces.*.lru_size=clear > /dev/null
1513                 sum=$($LFS mirror read -N $i $tf | md5sum)
1514                 [ "$sum" = "${checksums[$i]}" ] ||
1515                         error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
1516         done
1517
1518         # verify mirror write
1519         echo "Verifying mirror write .."
1520         $LFS mirror write -N2 $tf < $tf4
1521
1522         sum=$($LFS mirror read -N2 $tf | md5sum)
1523         [[ "$sum" = "${checksums[1]}" ]] ||
1524                 error "2: mismatch \'${checksums[1]}\' vs. \'$sum\'"
1525
1526         # verify mirror copy, write to this mirrored file will invalidate
1527         # the other two mirrors
1528         echo "Verifying mirror copy .."
1529
1530         local osts=$(comma_list $(osts_nodes))
1531
1532         $LFS mirror copy -i ${mirror_array[0]} -o-1 $tf ||
1533                 error "mirror copy error"
1534
1535         # verify copying is successful by checking checksums
1536         remount_client $MOUNT
1537         for i in ${mirror_array[@]}; do
1538                 sum=$($LFS mirror read -N $i $tf | md5sum)
1539                 [ "$sum" = "${checksums[1]}" ] ||
1540                         error "$i: mismatch checksum after copy \'$sum\'"
1541         done
1542
1543         rm -f $tf
1544 }
1545 run_test 37 "mirror I/O API verification"
1546
1547 test_38() {
1548         local tf=$DIR/$tfile
1549         local ref=$DIR/${tfile}-ref
1550
1551         $LFS setstripe -E 1M -S 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf ||
1552                 error "creating $tf failed"
1553         $LFS setstripe -E 2M -S 1M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 \
1554                 $tf-2 || error "creating $tf-2 failed"
1555         $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3 ||
1556                 error "creating $tf-3 failed"
1557
1558         # instantiate all components
1559         $LFS mirror extend -N -f $tf-2 $tf ||
1560                 error "merging $tf-2 into $tf failed"
1561         $LFS mirror extend -N -f $tf-3 $tf ||
1562                 error "merging $tf-3 into $tf failed"
1563         $LFS mirror extend -N -c 1 $tf ||
1564                 error "extending mirrored file $tf failed"
1565
1566         verify_flr_state $tf "ro"
1567
1568         dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
1569
1570         local fsize=$((RANDOM << 8 + 1048576))
1571         $TRUNCATE $ref $fsize
1572
1573         local ref_cksum=$(cat $ref | md5sum)
1574
1575         # case 1: verify write to mirrored file & resync work
1576         cp $ref $tf || error "copy from $ref to $f error"
1577         verify_flr_state $tf "wp"
1578
1579         local file_cksum=$(cat $tf | md5sum)
1580         [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
1581
1582         get_mirror_ids $tf
1583         echo "mirror IDs: ${mirror_array[@]}"
1584
1585         local valid_mirror stale_mirror id mirror_cksum
1586         for id in "${mirror_array[@]}"; do
1587                 mirror_cksum=$($LFS mirror read -N $id $tf | md5sum)
1588                 [ "$ref_cksum" == "$mirror_cksum" ] &&
1589                         { valid_mirror=$id; continue; }
1590
1591                 stale_mirror=$id
1592         done
1593
1594         [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
1595         [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
1596
1597         mirror_io resync $tf || error "resync failed"
1598         verify_flr_state $tf "ro"
1599
1600         mirror_cksum=$($LFS mirror read -N $stale_mirror $tf | md5sum)
1601         [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
1602
1603         # case 2: inject an error to make mirror_io exit after changing
1604         # the file state to sync_pending so that we can start a concurrent
1605         # write.
1606         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1607         verify_flr_state $tf "wp"
1608
1609         mirror_io resync -e resync_start $tf && error "resync succeeded"
1610         verify_flr_state $tf "sp"
1611
1612         # from sync_pending to write_pending
1613         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1614         verify_flr_state $tf "wp"
1615
1616         mirror_io resync -e resync_start $tf && error "resync succeeded"
1617         verify_flr_state $tf "sp"
1618
1619         # from sync_pending to read_only
1620         mirror_io resync $tf || error "resync failed"
1621         verify_flr_state $tf "ro"
1622 }
1623 run_test 38 "resync"
1624
1625 test_39() {
1626         local tf=$DIR/$tfile
1627
1628         rm -f $tf
1629         $LFS mirror create -N2 -E1m -c1 -S1M -E-1 $tf ||
1630         error "create PFL file $tf failed"
1631
1632         verify_mirror_count $tf 2
1633         verify_comp_count $tf 4
1634
1635         rm -f $tf || error "delete $tf failed"
1636 }
1637 run_test 39 "check FLR+PFL (a.k.a. PFLR) creation"
1638
1639 test_40() {
1640         local tf=$DIR/$tfile
1641         local ops
1642
1643         for ops in "conv=notrunc" ""; do
1644                 rm -f $tf
1645
1646                 $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 --flags=prefer \
1647                                    -N -E 1M -E 2M -E 4M -E -1 $tf ||
1648                         error "create PFLR file $tf failed"
1649                 dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
1650                         error "write PFLR file $tf failed"
1651
1652                 lfs getstripe -vy $tf
1653
1654                 local flags
1655
1656                 # file mirror state should be write_pending
1657                 flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1658                 [ $flags = wp ] ||
1659                 error "file mirror state $flags"
1660                 # the 1st component (in mirror 1) should be inited
1661                 verify_comp_attr lcme_flags $tf 0x10001 init
1662                 # the 2nd component (in mirror 1) should be inited
1663                 verify_comp_attr lcme_flags $tf 0x10002 init
1664                 # the 3rd component (in mirror 1) should be uninited
1665                 verify_comp_attr lcme_flags $tf 0x10003 prefer
1666                 # the 4th component (in mirror 2) should be inited
1667                 verify_comp_attr lcme_flags $tf 0x20004 init
1668                 # the 5th component (in mirror 2) should be uninited
1669                 verify_comp_attr lcme_flags $tf 0x20005 0
1670                 # the 6th component (in mirror 2) should be stale
1671                 verify_comp_attr lcme_flags $tf 0x20006 stale
1672                 # the 7th component (in mirror 2) should be uninited
1673                 if [[ x$ops = "xconv=notrunc" ]]; then
1674                         verify_comp_attr lcme_flags $tf 0x20007 0
1675                 elif [[ x$ops = "x" ]]; then
1676                         verify_comp_attr lcme_flags $tf 0x20007 stale
1677                 fi
1678         done
1679
1680         rm -f $tf || error "delete $tf failed"
1681 }
1682 run_test 40 "PFLR rdonly state instantiation check"
1683
1684 test_41() {
1685         local tf=$DIR/$tfile
1686
1687         rm -f $tf $tf-1
1688         echo " **create two FLR files $tf $tf-1"
1689         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1690                            -N -E 1M -E 2M -E 3M -E -1 $tf ||
1691                 error "create PFLR file $tf failed"
1692         $LFS mirror create -N -E 2M -S 1M -E eof \
1693                            -N -E 1M -E eof --flags prefer \
1694                            -N -E 4m -E eof $tf-1 ||
1695                 error "create PFLR file $tf-1 failed"
1696
1697         # file should be in ro status
1698         echo " **verify files be RDONLY"
1699         verify_flr_state $tf "ro"
1700         verify_flr_state $tf-1 "ro"
1701
1702         # write data in [0, 2M)
1703         dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
1704                 error "writing $tf failed"
1705         dd if=/dev/urandom of=$tf-1 bs=1M count=4 conv=notrunc ||
1706                 error "writing $tf-1 failed"
1707
1708         local sum0=$(cat $tf-1 | md5sum)
1709
1710         echo " **verify files be WRITE_PENDING"
1711         verify_flr_state $tf "wp"
1712         verify_flr_state $tf-1 "wp"
1713
1714         # file should have stale component
1715         echo " **verify files have stale component"
1716         $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
1717                 error "after writing $tf, it does not contain stale component"
1718         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1719                 error "after writing $tf-1, it does not contain stale component"
1720
1721         echo " **full resync"
1722         $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
1723
1724         echo " **verify $tf-1 data consistency in all mirrors"
1725         for i in 1 2 3; do
1726                 local sum=$($LFS mirror read -N$i $tf-1 | md5sum)
1727                 [[ "$sum" = "$sum0" ]] ||
1728                         error "$tf-1.$i: checksum mismatch: $sum != $sum0"
1729         done
1730
1731         echo " **verify files be RDONLY"
1732         verify_flr_state $tf "ro"
1733         verify_flr_state $tf-1 "ro"
1734
1735         # file should not have stale component
1736         echo " **verify files do not contain stale component"
1737         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1738                 error "after resyncing $tf, it contains stale component"
1739         $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
1740                 error "after resyncing $tf, it contains stale component"
1741
1742         # verify partial resync
1743         echo " **write $tf-1 for partial resync test"
1744         dd if=/dev/zero of=$tf-1 bs=1M count=2 conv=notrunc ||
1745                 error "writing $tf-1 failed"
1746
1747         echo " **only resync mirror 2"
1748         verify_flr_state $tf-1 "wp"
1749         $LFS mirror resync --only 2 $tf-1 ||
1750                 error "resync mirror 2 of $tf-1 failed"
1751         verify_flr_state $tf "ro"
1752
1753         # resync synced mirror
1754         echo " **resync mirror 2 again"
1755         $LFS mirror resync --only 2 $tf-1 ||
1756                 error "resync mirror 2 of $tf-1 failed"
1757         verify_flr_state $tf "ro"
1758         echo " **verify $tf-1 contains stale component"
1759         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1760                 error "after writing $tf-1, it does not contain stale component"
1761
1762         echo " **full resync $tf-1"
1763         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1764         verify_flr_state $tf "ro"
1765         echo " **full resync $tf-1 again"
1766         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1767         echo " **verify $tf-1 does not contain stale component"
1768         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1769                 error "after resyncing $tf, it contains stale component"
1770
1771         return 0
1772 }
1773 run_test 41 "lfs mirror resync check"
1774
1775 test_42() {
1776         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1777
1778         local td=$DIR/$tdir
1779         local tf=$td/$tfile
1780         local mirror_cmd="$LFS mirror verify"
1781         local i
1782
1783         # create parent directory
1784         mkdir $td || error "mkdir $td failed"
1785
1786         $mirror_cmd &> /dev/null && error "no file name given"
1787         $mirror_cmd $tf &> /dev/null && error "cannot stat file $tf"
1788         $mirror_cmd $td &> /dev/null && error "$td is not a regular file"
1789
1790         # create mirrored files
1791         $LFS mirror create -N -E 4M -S 1M -E 10M -E EOF $tf ||
1792                 error "create mirrored file $tf failed"
1793         $LFS mirror create -N -E 2M -S 1M -E EOF \
1794                            -N -E 6M -E 8M -E EOF \
1795                            -N -E 16M -E EOF $tf-1 ||
1796                 error "create mirrored file $tf-1 failed"
1797         $LFS mirror create -N -c 2 -o 1,3 -N -S 2M -c -1 $tf-2 ||
1798                 error "create mirrored file $tf-2 failed"
1799
1800         # write data in [0, 10M)
1801         for i in $tf $tf-1 $tf-2; do
1802                 yes | dd of=$i bs=1M count=10 iflag=fullblock conv=notrunc ||
1803                         error "write $i failed"
1804         done
1805
1806         # resync the mirrored files
1807         $LFS mirror resync $tf-1 $tf-2 ||
1808                 error "resync $tf-1 $tf-2 failed"
1809
1810         # verify the mirrored files
1811         $mirror_cmd $tf-1 $tf-2 ||
1812                 error "verify $tf-1 $tf-2 failed"
1813
1814         get_mirror_ids $tf-1
1815         $mirror_cmd --only ${mirror_array[0]} $tf-1 &> /dev/null &&
1816                 error "at least 2 mirror ids needed with '--only' option"
1817         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 $tf-2 \
1818                 &> /dev/null &&
1819                 error "'--only' option cannot be used upon multiple files"
1820         $mirror_cmd --only 65534,${mirror_array[0]},65535 $tf-1 &&
1821                 error "invalid specified mirror ids"
1822
1823         # change the content of $tf and merge it into $tf-1
1824         for i in 6 10; do
1825                 echo a | dd of=$tf bs=1M seek=$i conv=notrunc ||
1826                         error "change $tf with seek=$i failed"
1827                 echo b | dd of=$tf-1 bs=1M seek=$i conv=notrunc ||
1828                         error "change $tf-1 with seek=$i failed"
1829         done
1830
1831         $LFS mirror resync $tf-1 || error "resync $tf-1 failed"
1832         $LFS mirror extend --no-verify -N -f $tf $tf-1 ||
1833                 error "merge $tf into $tf-1 failed"
1834
1835         # verify the mirrored files
1836         echo "Verify $tf-1 without -v option:"
1837         $mirror_cmd $tf-1 &&
1838                 error "verify $tf-1 should fail" || echo "PASS"
1839
1840         echo "Verify $tf-1 with -v option:"
1841         $mirror_cmd -v $tf-1 &&
1842                 error "verify $tf-1 should fail"
1843
1844         get_mirror_ids $tf-1
1845         echo "Verify $tf-1 with --only option:"
1846         $mirror_cmd -v --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 &&
1847                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1848                       "${mirror_array[-1]} should fail"
1849
1850         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 ||
1851                 error "verify $tf-1 with mirror ${mirror_array[0]} and" \
1852                       "${mirror_array[1]} should succeed"
1853
1854         # set stale components in $tf-1
1855         for i in 0x40002 0x40003; do
1856                 $LFS setstripe --comp-set -I$i --comp-flags=stale $tf-1 ||
1857                         error "set stale flag on component $i failed"
1858         done
1859
1860         # verify the mirrored file
1861         echo "Verify $tf-1 with stale components:"
1862         $mirror_cmd -vvv $tf-1 ||
1863                 error "verify $tf-1 with stale components should succeed"
1864
1865         echo "Verify $tf-1 with stale components and --only option:"
1866         $mirror_cmd -vvv --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 ||
1867                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1868                       "${mirror_array[-1]} should succeed"
1869 }
1870 run_test 42 "lfs mirror verify"
1871
1872 # inactivate one OST && write && restore the OST
1873 write_file_43() {
1874         local file=$1
1875         local ost=$2
1876         local PARAM="osp.${FSNAME}-OST000${ost}-osc-M*.active"
1877         local wait
1878
1879         wait=$(do_facet $SINGLEMDS \
1880                 "$LCTL get_param -n lod.*MDT0000-*.qos_maxage")
1881         wait=${wait%%[^0-9]*}
1882
1883         echo "  **deactivate OST$ost, waiting for $((wait*2+2)) seconds"
1884         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 0")
1885         # lod_qos_statfs_update needs 2*$wait seconds to refresh targets statfs
1886         sleep $(($wait * 2 + 2))
1887         echo "  **write $file"
1888         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
1889         echo "  **restore activating OST$ost, waiting for $((wait*2+2)) seconds"
1890         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 1")
1891         sleep $((wait * 2 + 2))
1892
1893         local flags=$($LFS getstripe -v $file | awk '/lcm_flags:/ { print $2 }')
1894         [ $flags = wp ] || error "file mirror state $flags != wp"
1895 }
1896
1897 test_43a() {
1898         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
1899
1900         local tf=$DIR/$tfile
1901         local flags
1902
1903         rm -f $tf
1904         ##   mirror 0  ost (0, 1)
1905         ##   mirror 1  ost (1, 2)
1906         ##   mirror 2  ost (2, 0)
1907         $LFS mirror create -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 \
1908                 -N -Eeof -c2 -o2,0 $tf ||
1909                 error "create 3 mirrors file $tf failed"
1910
1911         ################## OST0 ###########################################
1912         write_file_43 $tf 0
1913         echo "  **verify components"
1914         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1915         verify_comp_attr lcme_flags $tf 0x20002 init
1916         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1917
1918         # resync
1919         echo "  **resync $tf"
1920         $LFS mirror resync $tf
1921         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1922         [ $flags = ro ] || error "file mirror state $flags != ro"
1923
1924         ################## OST1 ###########################################
1925         write_file_43 $tf 1
1926         echo "  **verify components"
1927         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1928         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1929         verify_comp_attr lcme_flags $tf 0x30003 init
1930
1931         # resync
1932         echo "  **resync $tf"
1933         $LFS mirror resync $tf
1934         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1935         [ $flags = ro ] || error "file mirror state $flags != ro"
1936
1937         ################## OST2 ###########################################
1938         write_file_43 $tf 2
1939         echo "  **verify components"
1940         verify_comp_attr lcme_flags $tf 0x10001 init
1941         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1942         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1943 }
1944 run_test 43a "mirror pick on write"
1945
1946 test_43b() {
1947         local tf=$DIR/$tdir/$tfile
1948
1949         test_mkdir $DIR/$tdir
1950         rm -f $tf
1951
1952         # create 3 mirrors FLR file, the first 2 mirrors are preferred
1953         $LFS setstripe -N -Eeof --flags=prefer -N -Eeof --flags=prefer \
1954                 -N -Eeof $tf || error "create 3 mirrors file $tf failed"
1955         verify_flr_state $tf "ro"
1956
1957         echo " ** write to $tf"
1958         dd if=/dev/zero of=$tf bs=1M count=1 || error "write $tf failed"
1959         verify_flr_state $tf "wp"
1960
1961         echo " ** resync $tf"
1962         $LFS mirror resync $tf || error "resync $tf failed"
1963         verify_flr_state $tf "ro"
1964 }
1965 run_test 43b "allow writing to multiple preferred mirror file"
1966
1967 test_44() {
1968         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1969         rm -rf $DIR/$tdir
1970         rm -rf $DIR/$tdir-1
1971         local tf=$DIR/$tdir/$tfile
1972         local tf1=$DIR/$tdir-1/$tfile-1
1973
1974         $LFS setdirstripe -i 0 -c 1 $DIR/$tdir ||
1975                 error "create directory failed"
1976         $LFS setdirstripe -i 1 -c 1 $DIR/$tdir-1 ||
1977                 error "create remote directory failed"
1978         rm -f $tf $tf1 $tf.mirror~2
1979         # create file with 4 mirrors
1980         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1981                            -N -E 1M -E 2M -E 3M -E -1 -N2 $tf ||
1982                 error "create PFLR file $tf failed"
1983
1984         # file should be in ro status
1985         verify_flr_state $tf "ro"
1986
1987         # write data in [0, 3M)
1988         dd if=/dev/urandom of=$tf bs=1M count=3 conv=notrunc ||
1989                 error "writing $tf failed"
1990
1991         verify_flr_state $tf "wp"
1992
1993         # disallow destroying the last non-stale mirror
1994         ! $LFS mirror delete --mirror-id 1 $tf > /dev/null 2>&1 ||
1995                 error "destroying mirror 1 should fail"
1996
1997         # synchronize all mirrors of the file
1998         $LFS mirror resync $tf || error "mirror resync $tf failed"
1999
2000         verify_flr_state $tf "ro"
2001
2002         # split mirror 1
2003         $LFS mirror split --mirror-id 1 -f $tf1 $tf ||
2004                 error "split to $tf1 failed"
2005
2006         local idx0=$($LFS getstripe -m $tf)
2007         local idx1=$($LFS getstripe -m $tf1)
2008
2009         [[ x$idx0 == x0 ]] || error "$tf is not on MDT0"
2010         [[ x$idx1 == x1 ]] || error "$tf1 is not on MDT1"
2011
2012         # verify mirror count
2013         verify_mirror_count $tf 3
2014         verify_mirror_count $tf1 1
2015
2016         $LFS mirror split --mirror-id 2 $tf ||
2017                 error "split mirror 2 failed"
2018
2019         verify_mirror_count $tf 2
2020         verify_mirror_count $tf.mirror~2 1
2021
2022         $LFS setstripe --comp-set -I 0x30008 --comp-flags=stale $tf ||
2023                 error "setting stale flag on component 0x30008 failed"
2024
2025         # disallow destroying the last non-stale mirror
2026         ! $LFS mirror split --mirror-id 4 -d $tf > /dev/null 2>&1 ||
2027                 error "destroying mirror 4 should fail"
2028
2029         $LFS mirror resync $tf || error "resynchronizing $tf failed"
2030
2031         $LFS mirror split --mirror-id 3 -d $tf ||
2032                 error "destroying mirror 3 failed"
2033         verify_mirror_count $tf 1
2034
2035         # verify splitted file contains the same content as the orig file does
2036         diff $tf $tf1 || error "splited file $tf1 diffs from $tf"
2037         diff $tf $tf.mirror~2 ||
2038                 error "splited file $tf.mirror~2 diffs from $tf"
2039 }
2040 run_test 44 "lfs mirror split check"
2041
2042 test_44c() {
2043         local tf=$DIR/$tdir/$tfile
2044
2045         stack_trap "rm -f $tf"
2046
2047         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
2048                 skip "Need MDS version at least 2.14.52"
2049
2050         [ "$FSTYPE" != "zfs" ] || skip "ZFS file's block number is not accurate"
2051
2052         mkdir -p $DIR/$tdir || error "create directroy failed"
2053
2054         dd if=/dev/zero of=$tf bs=1M count=10 || error "dd write $tfile failed"
2055         sync
2056         block1=$(( $(stat -c "%b*%B" $tf) ))
2057         echo " ** before mirror ops, file blocks=$((block1/1024)) KiB"
2058
2059         $LFS mirror extend -N2 -c1 $tf || error "mirror extend $tfile failed"
2060         sync
2061         block2=$(( $(stat -c "%b*%B" $tf) ))
2062         echo " ** after mirror extend, file blocks=$((block2/1024)) KiB"
2063
2064         $LFS mirror split -d --mirror-id=2 $tf ||
2065                 error "mirror split $tfile failed"
2066         $LFS mirror split -d --mirror-id=3 $tf ||
2067                 error "mirror split $tfile failed"
2068         sync
2069         lfs getsom $tf
2070         block3=$(( $(stat -c "%b*%B" $tf) ))
2071         echo " ** after mirror split, file blocks=$((block3/1024)) KiB"
2072
2073         [[ $block1 -eq $block3 ]] ||
2074                 error "mirror split does not reduce block# $block3 != $block1"
2075 }
2076 run_test 44c "lfs mirror split reduces block size of a file"
2077
2078 test_45() {
2079         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
2080
2081         local file=$DIR/$tdir/$tfile
2082         local dir=$DIR/$tdir/$dir
2083         local temp=$DIR/$tdir/template
2084         rm -rf $DIR/$tdir
2085         test_mkdir $DIR/$tdir
2086
2087         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
2088                 -N -E3m -S1m -Eeof -N -E8m -Eeof $file ||
2089                         error "Create $file failed"
2090
2091         verify_yaml_layout $file $file.copy $temp "1. FLR file"
2092         rm -f $file $file.copy
2093
2094         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
2095                 -N -E3m -S1m -Eeof -N -E8m --flags=prefer -Eeof $file ||
2096                         error "Create $file failed"
2097
2098         verify_yaml_layout $file $file.copy $temp "2. FLR file with flags"
2099 }
2100 run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
2101
2102 verify_46() {
2103         local src=$1
2104         local dst=$2
2105         local msg_prefix=$3
2106
2107         $LFS setstripe --copy=$src $dst || error "setstripe $dst failed"
2108
2109         local layout1=$(get_layout_param $src)
2110         local layout2=$(get_layout_param $dst)
2111         # compare their layout info
2112         [ "$layout1" == "$layout2" ] ||
2113                 error "$msg_prefix $src <=> $dst layouts are not equal"
2114 }
2115
2116 test_46() {
2117         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2118
2119         local file=$DIR/$tdir/$tfile
2120         test_mkdir $DIR/$tdir
2121
2122         ########################### 1. PFL file #############################
2123         echo "  ** 1. PFL file"
2124         rm -f $file
2125         $LFS setstripe -E1m -S 1M -c2 -o0,1 -E2m -c2 -E3m -o1,0 -E4m -c1 -E-1 \
2126                 $file || error "1. Create PFL $file failed"
2127
2128         rm -f $file.copy
2129         verify_46 $file $file.copy "1. PFL file"
2130
2131         ########################### 2. plain file ###########################
2132         echo "  ** 2. plain file"
2133         rm -f $file
2134         $LFS setstripe -c2 -o0,1 -i1 $file ||
2135                 error "2. Create plain $file failed"
2136
2137         rm -f $file.copy
2138         verify_46 $file $file.copy "2. plain file"
2139
2140         ########################### 3. FLR file #############################
2141         echo "  ** 3. FLR file"
2142         rm -f $file
2143         $LFS setstripe -N -E1m -S 1M -c2 -o0,1 -E4m -c1 -Eeof -N -E16m -Eeof \
2144                 $file || error "3. Create FLR $file failed"
2145
2146         rm -f $file.copy
2147         verify_46 $file $file.copy "3. FLR file"
2148
2149         local dir=$DIR/$tdir/dir
2150         ########################### 4. PFL dir ##############################
2151         echo "  ** 4. PFL dir"
2152         test_mkdir $dir
2153         $LFS setstripe -E1m -S 1M -c2 -E2m -c1 -E-1 $dir ||
2154                 error "4. setstripe PFL $dir failed"
2155
2156         test_mkdir $dir.copy
2157         verify_46 $dir $dir.copy "4. PFL dir"
2158
2159         ########################### 5. plain dir ############################
2160         echo "  ** 5. plain dir"
2161         $LFS setstripe -c2 -i-1 $dir || error "5. setstripe plain $dir failed"
2162
2163         verify_46 $dir $dir.copy "5. plain dir"
2164
2165         ########################### 6. FLR dir ##############################
2166         echo "  ** 6. FLR dir"
2167         $LFS setstripe -N -E1m -S 1M -c2 -E2m -c1 -Eeof -N -E4m -Eeof $dir ||
2168                 error "6. setstripe FLR $dir failed"
2169
2170         verify_46 $dir $dir.copy "6. FLR dir"
2171
2172         ########################### 7. SEL file ##############################
2173         echo "  ** 7. SEL file"
2174         rm -f $file
2175         $LFS setstripe -E256M -S 1M -c2 -o0,1 -z 64M -E-1 -o1,0 -z 128M \
2176                 $file || error "Create $file failed"
2177
2178         rm -f $file.copy
2179         verify_46 $file $file.copy "7. SEL file"
2180
2181         ########################### 8. SEL dir ##############################
2182         echo "  ** 8. SEL dir"
2183         $LFS setstripe -E256M -S 1M -c2 -z 64M -E-1 -z 128M \
2184                 $dir || error "setstripe $dir failed"
2185
2186         verify_46 $dir $dir.copy "8. SEL dir"
2187
2188         ########################### 9. FLR SEL file ##########################
2189         echo "  ** 9. FLR SEL file"
2190         rm -f $file
2191         $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
2192                 -N -E1G -c4 -z128M -E-1 -z256M $file || error "Create $file failed"
2193
2194         rm -f $file.copy
2195         verify_46 $file $file.copy "9. SEL file"
2196
2197         ########################### 10. FLR SEL dir #########################
2198         echo "  ** 10. FLR SEL dir"
2199         $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
2200                 -N -E1G -c4 -z128M -E-1 -z256M $dir || error "Create $file failed"
2201
2202         verify_46 $dir $dir.copy "10. SEL dir"
2203 }
2204 run_test 46 "Verify setstripe --copy option"
2205
2206 test_47() {
2207         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
2208
2209         local file=$DIR/$tdir/$tfile
2210         local ids
2211         local ost
2212         local osts
2213
2214         test_mkdir $DIR/$tdir
2215
2216         # test case 1:
2217         rm -f $file
2218         # mirror1: [comp0]ost0,    [comp1]ost1 and ost2
2219         # mirror2: [comp2]    ,    [comp3] should not use ost1 or ost2
2220         $LFS mirror create -N -E2m -c1 -o0 --flags=prefer -Eeof -c2 -o1,2 \
2221                 -N -E2m -c1 -Eeof -c1 $file || error "create FLR $file failed"
2222         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2223
2224         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
2225         $LFS mirror resync $file || error "resync $file failed"
2226
2227         ost=$($LFS getstripe -I${ids[2]} $file | awk '/l_ost_idx/{print $5}')
2228         if [[ x$ost == "x0," ]]; then
2229                 $LFS getstripe $file
2230                 error "component ${ids[2]} objects allocated on $ost " \
2231                       "shouldn't on OST0"
2232         fi
2233
2234         ost=$($LFS getstripe -I${ids[3]} $file | awk '/l_ost_idx/{print $5}')
2235         if [[ x$ost == "x1," || x$ost == "x2," ]]; then
2236                 $LFS getstripe $file
2237                 error "component ${ids[3]} objects allocated on $ost " \
2238                       "shouldn't on OST1 or on OST2"
2239         fi
2240
2241         ## test case 2:
2242         rm -f $file
2243         # mirror1: [comp0]    [comp1]
2244         # mirror2: [comp2]    [comp3]
2245         # mirror3: [comp4]    [comp5]
2246         # mirror4: [comp6]    [comp7]
2247         $LFS mirror create -N4 -E1m -c1 -Eeof -c1 $file ||
2248                 error "create FLR $file failed"
2249         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2250
2251         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
2252         $LFS mirror resync $file || error "resync $file failed"
2253
2254         for ((i = 0; i < 6; i++)); do
2255                 osts[$i]=$($LFS getstripe -I${ids[$i]} $file |
2256                         awk '/l_ost_idx/{print $5}')
2257         done
2258         # comp[0],comp[2],comp[4] should use different osts
2259         if [[ ${osts[0]} == ${osts[2]} || ${osts[0]} == ${osts[4]} ||
2260               ${osts[2]} == ${osts[4]} ]]; then
2261                 $LFS getstripe $file
2262                 error "component ${ids[0]}, ${ids[2]}, ${ids[4]} have objects "\
2263                       "allocated on duplicated OSTs"
2264         fi
2265         # comp[1],comp[3],comp[5] should use different osts
2266         if [[ ${osts[1]} == ${osts[3]} || ${osts[1]} == ${osts[5]} ||
2267               ${osts[3]} == ${osts[5]} ]]; then
2268                 $LFS getstripe $file
2269                 error "component ${ids[1]}, ${ids[3]}, ${ids[5]} have objects "\
2270                       "allocated on duplicated OSTs"
2271         fi
2272
2273         return 0
2274 }
2275 run_test 47 "Verify mirror obj alloc"
2276
2277 test_48() {
2278         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
2279                 skip "Need MDS version at least 2.11.55"
2280
2281         local tf=$DIR/$tfile
2282
2283         rm -f $tf
2284         echo " ** create 2 mirrors FLR file $tf"
2285         $LFS mirror create -N -E2M -Eeof --flags prefer \
2286                            -N -E1M -Eeof $tf ||
2287                 error "create FLR file $tf failed"
2288
2289         echo " ** write it"
2290         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2291         verify_flr_state $tf "wp"
2292
2293         local sum0=$(md5sum < $tf)
2294
2295         echo " ** resync the file"
2296         $LFS mirror resync $tf
2297
2298         echo " ** snapshot mirror 2"
2299         $LFS setstripe --comp-set -I 0x20003 --comp-flags=nosync $tf
2300
2301         echo " ** write it again"
2302         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2303         echo " ** resync it again"
2304         $LFS mirror resync $tf
2305
2306         verify_flr_state $tf "wp"
2307         verify_comp_attr lcme_flags $tf 0x20003 nosync,stale
2308
2309         local sum1=$($LFS mirror read -N1 $tf | md5sum)
2310         local sum2=$($LFS mirror read -N2 $tf | md5sum)
2311
2312         echo " ** verify mirror 2 doesn't change"
2313         echo "original checksum: $sum0"
2314         echo "mirror 1 checksum: $sum1"
2315         echo "mirror 2 checksum: $sum2"
2316         [[ $sum0 = $sum2 ]] ||
2317                 error "original checksum: $sum0, mirror 2 checksum: $sum2"
2318         echo " ** mirror 2 stripe info"
2319         $LFS getstripe -v --mirror-index=2 $tf
2320
2321         echo " ** resync mirror 2"
2322         $LFS mirror resync --only 2 $tf
2323
2324         verify_flr_state $tf "ro"
2325         verify_comp_attr lcme_flags $tf 0x20003 nosync,^stale
2326
2327         sum1=$($LFS mirror read -N1 $tf | md5sum)
2328         sum2=$($LFS mirror read -N2 $tf | md5sum)
2329
2330         echo " ** verify mirror 2 resync-ed"
2331         echo "original checksum: $sum0"
2332         echo "mirror 1 checksum: $sum1"
2333         echo "mirror 2 checksum: $sum2"
2334         [[ $sum1 = $sum2 ]] ||
2335                 error "mirror 1 checksum: $sum1, mirror 2 checksum: $sum2"
2336         echo " ** mirror 2 stripe info"
2337         $LFS getstripe -v --mirror-index=2 $tf
2338 }
2339 run_test 48 "Verify snapshot mirror"
2340
2341 OLDIFS="$IFS"
2342 cleanup_49() {
2343         trap 0
2344         IFS="$OLDIFS"
2345 }
2346
2347 test_49a() {
2348         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
2349
2350         trap cleanup_49 EXIT RETURN
2351
2352         local file=$DIR/$tfile
2353
2354         $LFS setstripe -N -E eof -c1 -o1 -N -E eof -c1 -o0 $file ||
2355                 error "setstripe on $file"
2356
2357         dd if=/dev/zero of=$file bs=1M count=1 || error "dd failed for $file"
2358         $LFS mirror resync $file
2359
2360         filefrag -ves $file || error "filefrag $file failed"
2361         filefrag_op=$(filefrag -ve -k $file |
2362                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
2363
2364 #Filesystem type is: bd00bd0
2365 #File size of /mnt/lustre/f49a.sanity-flr is 1048576 (1024 blocks of 1024 bytes)
2366 # ext:     device_logical:        physical_offset: length:  dev: flags:
2367 #   0:        0..    1023:    1572864..   1573887:   1024: 0001: net,eof
2368 #   1:        0..    1023:    1572864..   1573887:   1024: 0000: last,net,eof
2369 #/mnt/lustre/f49a.sanity-flr: 2 extents found
2370
2371         last_lun=$(echo $filefrag_op | cut -d: -f5)
2372         IFS=$'\n'
2373         tot_len=0
2374         num_luns=1
2375         for line in $filefrag_op; do
2376                 frag_lun=$(echo $line | cut -d: -f5)
2377                 ext_len=$(echo $line | cut -d: -f4)
2378                 if [[ "$frag_lun" != "$last_lun" ]]; then
2379                         if (( tot_len != 1024 )); then
2380                                 cleanup_49
2381                                 error "$file: OST$last_lun $tot_len != 1024"
2382                         else
2383                                 (( num_luns += 1 ))
2384                                 tot_len=0
2385                         fi
2386                 fi
2387                 (( tot_len += ext_len ))
2388                 last_lun=$frag_lun
2389         done
2390         if (( num_luns != 2 || tot_len != 1024 )); then
2391                 cleanup_49
2392                 error "$file: $num_luns != 2, $tot_len != 1024 on OST$last_lun"
2393         fi
2394
2395         echo "FIEMAP on $file succeeded"
2396 }
2397 run_test 49a "FIEMAP upon FLR file"
2398
2399 test_50A() {    # EX-2179
2400         mkdir -p $DIR/$tdir
2401
2402         local file=$DIR/$tdir/$tfile
2403
2404         $LFS setstripe -c1 -i0 $file || error "setstripe $file failed"
2405
2406         $LFS mirror extend -N -c1 -i1 $file ||
2407                 error "extending mirror for $file failed"
2408
2409         local olv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
2410
2411         fail mds1
2412
2413         $LFS mirror split -d --mirror-id=1 $file || error "split $file failed"
2414
2415         local flv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
2416
2417         echo "$file layout generation from $olv to $flv"
2418         (( $flv != ($olv + 1) )) &&
2419                 error "split does not increase layout gen from $olv to $flv"
2420
2421         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
2422
2423         $LFS getstripe -v $file || error "getstripe $file failed"
2424 }
2425 run_test 50A "mirror split update layout generation"
2426
2427 test_50a() {
2428         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2429                 skip "OST does not support SEEK_HOLE"
2430
2431         local file=$DIR/$tdir/$tfile
2432         local offset
2433         local sum1
2434         local sum2
2435         local blocks
2436
2437         mkdir -p $DIR/$tdir
2438
2439         echo " ** create striped file $file"
2440         $LFS setstripe -E 1M -c1 -S 1M -E eof -c2 -S1M $file ||
2441                 error "cannot create file with PFL layout"
2442         echo " ** write 1st data chunk at 1M boundary"
2443         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2444                 error "cannot write data at 1M boundary"
2445         echo " ** write 2nd data chunk at 2M boundary"
2446         dd if=/dev/urandom of=$file bs=1k count=20 seek=2041 ||
2447                 error "cannot write data at 2M boundary"
2448         echo " ** create hole at the file end"
2449         $TRUNCATE $file 3700000 || error "truncate fails"
2450
2451         echo " ** verify sparseness"
2452         offset=$(lseek_test -d 1000 $file)
2453         echo "    first data offset: $offset"
2454         [[ $offset == 1000 ]] &&
2455                 error "src: data is not expected at offset $offset"
2456         offset=$(lseek_test -l 3500000 $file)
2457         echo "    hole at the end: $offset"
2458         [[ $offset == 3500000 ]] ||
2459                 error "src: hole is expected at offset $offset"
2460
2461         echo " ** extend the file with new mirror"
2462         # migrate_copy_data() is used
2463         $LFS mirror extend -N -E 2M -S 1M -E 1G -S 2M -E eof $file ||
2464                 error "cannot create mirror"
2465         $LFS getstripe $file | grep lcme_flags | grep stale > /dev/null &&
2466                 error "$file still has stale component"
2467
2468         # check migrate_data_copy() was correct
2469         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2470         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2471         [[ $sum_1 == $sum_2 ]] ||
2472                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2473
2474         # stale first mirror
2475         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale $file
2476         $LFS setstripe --comp-set -I0x10002 --comp-flags=stale $file
2477
2478         echo " ** verify mirror #2 sparseness"
2479         offset=$(lseek_test -d 1000 $file)
2480         echo "    first data offset: $offset"
2481         [[ $offset == 1000 ]] &&
2482                 error "dst: data is not expected at offset $offset"
2483         offset=$(lseek_test -l 3500000 $file)
2484         echo "    hole at the end: $offset"
2485         [[ $offset == 3500000 ]] ||
2486                 error "dst: hole is expected at offset $offset"
2487
2488         echo " ** copy mirror #2 to mirror #1"
2489         $LFS mirror copy -i 2 -o 1 $file || error "mirror copy fails"
2490         $LFS getstripe $file | grep lcme_flags | grep stale > /dev/null &&
2491                 error "$file still has stale component"
2492
2493         # check llapi_mirror_copy_many correctness
2494         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2495         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2496         [[ $sum_1 == $sum_2 ]] ||
2497                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2498
2499         # stale 1st component of mirror #2 before lseek call
2500         $LFS setstripe --comp-set -I0x20001 --comp-flags=stale $file
2501
2502         echo " ** verify mirror #1 sparseness again"
2503         offset=$(lseek_test -d 1000 $file)
2504         echo "    first data offset: $offset"
2505         [[ $offset == 1000 ]] &&
2506                 error "dst: data is not expected at offset $offset"
2507         offset=$(lseek_test -l 3500000 $file)
2508         echo "    hole at the end: $offset"
2509         [[ $offset == 3500000 ]] ||
2510                 error "dst: hole is expected at offset $offset"
2511
2512         cancel_lru_locks osc
2513
2514         blocks=$(stat -c%b $file)
2515         echo " ** final consumed blocks: $blocks"
2516         # for 3.5Mb file consumes ~6000 blocks, use 1000 to check
2517         # that file is still sparse
2518         (( blocks < 1000 )) ||
2519                 error "Mirrored file consumes $blocks blocks"
2520
2521         rm $file
2522 }
2523 run_test 50a "mirror extend/copy preserves sparseness"
2524
2525 test_50b() {
2526         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2527                 skip "OST does not support SEEK_HOLE"
2528
2529         local file=$DIR/$tdir/$tfile
2530         local offset
2531         local sum1
2532         local sum2
2533         local blocks
2534
2535         mkdir -p $DIR/$tdir
2536
2537         echo " ** create mirrored file $file"
2538         $LFS mirror create -N -E1M -c1 -S1M -E eof \
2539                 -N -E2M -S1M -E eof -S2M $file ||
2540                 error "cannot create mirrored file"
2541         echo " ** write data chunk at 1M boundary"
2542         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2543                 error "cannot write data at 1M boundary"
2544         echo " ** create hole at the file end"
2545         $TRUNCATE $file 3700000 || error "truncate fails"
2546
2547         echo " ** verify sparseness"
2548         offset=$(lseek_test -d 1000 $file)
2549         echo "    first data offset: $offset"
2550         [[ $offset == 1000 ]] &&
2551                 error "src: data is not expected at offset $offset"
2552         offset=$(lseek_test -l 3500000 $file)
2553         echo "    hole at the end: $offset"
2554         [[ $offset == 3500000 ]] ||
2555                 error "src: hole is expected at 3500000"
2556
2557         echo " ** resync mirror #2 to mirror #1"
2558         $LFS mirror resync $file
2559
2560         # check llapi_mirror_copy_many correctness
2561         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2562         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2563         [[ $sum_1 == $sum_2 ]] ||
2564                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2565
2566         cancel_lru_locks osc
2567
2568         blocks=$(stat -c%b $file)
2569         echo " ** consumed blocks: $blocks"
2570         # without full punch() support the first component can be not sparse
2571         # but the last one should be, so file should use far fewer blocks
2572         (( blocks < 5000 )) ||
2573                 error "Mirrored file consumes $blocks blocks"
2574
2575         # stale first component in mirror #1
2576         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,nosync $file
2577         echo " ** truncate file down"
2578         $TRUNCATE $file 0
2579         echo " ** write data chunk at 2M boundary"
2580         dd if=/dev/urandom of=$file bs=1k count=20 seek=2041 conv=notrunc ||
2581                 error "cannot write data at 2M boundary"
2582         echo " ** resync mirror #2 to mirror #1 with nosync 1st component"
2583         $LFS mirror resync $file || error "mirror rsync fails"
2584         # first component is still stale
2585         $LFS getstripe $file | grep 'lcme_flags:.*stale' > /dev/null ||
2586                 error "$file still has no stale component"
2587         echo " ** resync mirror #2 to mirror #1 again"
2588         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,^nosync $file
2589         $LFS mirror resync $file || error "mirror rsync fails"
2590         $LFS getstripe $file | grep 'lcme_flags:.*stale' > /dev/null &&
2591                 error "$file still has stale component"
2592
2593         # check llapi_mirror_copy_many correctness
2594         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2595         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2596         [[ $sum_1 == $sum_2 ]] ||
2597                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2598
2599         cancel_lru_locks osc
2600
2601         blocks=$(stat -c%b $file)
2602         echo " ** final consumed blocks: $blocks"
2603         # while the first component can lose sparseness, the last one should
2604         # not, so whole file should still use far fewer blocks in total
2605         (( blocks < 3000 )) ||
2606                 error "Mirrored file consumes $blocks blocks"
2607         rm $file
2608 }
2609 run_test 50b "mirror rsync handles sparseness"
2610
2611 test_50c() {
2612         local tf=$DIR/$tdir/$tfile
2613
2614         test_mkdir $DIR/$tdir
2615
2616         $LFS setstripe -N2 -c-1 $tf || error "create FLR $tf failed"
2617         verify_flr_state $tf "ro"
2618
2619         if [[ "$FSTYPE" == "ldiskfs" ]]; then
2620                 # ZFS does not support fallocate for now
2621                 fallocate -p -o 1MiB -l 1MiB $tf ||
2622                         error "punch hole in $tf failed"
2623                 verify_flr_state $tf "wp"
2624         fi
2625
2626         dd if=/dev/zero of=$tf bs=4096 count=4 || error "write $tf failed"
2627         $LFS mirror resync $tf || error "mirror resync $tf failed"
2628         verify_flr_state $tf "ro"
2629
2630         $MULTIOP $tf OSMWUc || error "$MULTIOP $tf failed"
2631         verify_flr_state $tf "wp"
2632 }
2633 run_test 50c "punch_hole/mmap_write stale other mirrors"
2634
2635 test_60a() {
2636         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2637                 skip "OST does not support SEEK_HOLE"
2638
2639         local file=$DIR/$tdir/$tfile
2640         local old_size=2147483648 # 2GiB
2641         local new_size
2642
2643         mkdir -p $DIR/$tdir
2644         dd if=/dev/urandom of=$file bs=4096 count=1 seek=$((134217728 / 4096))
2645         $TRUNCATE $file $old_size
2646
2647         $LFS mirror extend -N -c 1 $file
2648         dd if=/dev/urandom of=$file bs=4096 count=1 seek=$((134217728 / 4096)) conv=notrunc
2649         $LFS mirror resync $file
2650
2651         new_size=$(stat --format='%s' $file)
2652         if ((new_size != old_size)); then
2653                 error "new_size ($new_size) is not equal to old_size ($old_size)"
2654         fi
2655
2656         rm $file
2657 }
2658 run_test 60a "mirror extend sets correct size on sparse file"
2659
2660 test_70() {
2661         local tf=$DIR/$tdir/$tfile
2662
2663         test_mkdir $DIR/$tdir
2664
2665         while true; do
2666                 rm -f $tf
2667                 $LFS mirror create -N -E 1M -c -1 -E eof -N $tf
2668                 echo xxxx > $tf
2669         done &
2670         c_pid=$!
2671         echo "mirror create pid $c_pid"
2672
2673         while true; do
2674                 $LFS mirror split -d --mirror-id=1 $tf &> /dev/null
2675         done &
2676         s_pid=$!
2677         echo "mirror split pid $s_pid"
2678
2679         echo "mirror create and split race for 60 seconds, should not crash"
2680         sleep 60
2681         kill -9 $c_pid &> /dev/null
2682         kill -9 $s_pid &> /dev/null
2683
2684         rm -f $tf
2685         true
2686 }
2687 run_test 70 "mirror create and split race"
2688
2689 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
2690 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
2691
2692 write_file_200() {
2693         local tf=$1
2694
2695         local fsize=$(stat --printf=%s $tf)
2696
2697         while [ -f $ctrl_file ]; do
2698                 local off=$((RANDOM << 8))
2699                 local len=$((RANDOM << 5 + 131072))
2700
2701                 [ $((off + len)) -gt $fsize ] && {
2702                         fsize=$((off + len))
2703                         echo "Extending file size to $fsize .."
2704                 }
2705
2706                 flock -s $lock_file -c \
2707                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
2708                                 { rm -f $ctrl_file;
2709                                   error "failed writing to $off:$len"; }
2710                 sleep 0.$((RANDOM % 2 + 1))
2711         done
2712 }
2713
2714 read_file_200() {
2715         local tf=$1
2716
2717         while [ -f $ctrl_file ]; do
2718                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
2719                         { rm -f $ctrl_file; error "read failed"; }
2720                 sleep 0.$((RANDOM % 2 + 1))
2721         done
2722 }
2723
2724 resync_file_200() {
2725         local tf=$1
2726
2727         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
2728
2729         exec 200<>$lock_file
2730         while [ -f $ctrl_file ]; do
2731                 local lock_taken=false
2732                 local index=$((RANDOM % ${#options[@]}))
2733                 local cmd="mirror_io resync ${options[$index]}"
2734
2735                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
2736
2737                 [ $((RANDOM % 4)) -eq 0 ] && {
2738                         index=0
2739                         lock_taken=true
2740                         echo -n "lock to "
2741                 }
2742
2743                 echo -n "resync file $tf with '$cmd' .."
2744
2745                 if [[ $lock_taken = "true" ]]; then
2746                         flock -x 200 -c "$cmd $tf &> /dev/null" &&
2747                                 echo "done" || echo "failed"
2748                         flock -u 200
2749                 else
2750                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
2751                 fi
2752
2753                 sleep 0.$((RANDOM % 8 + 1))
2754         done
2755 }
2756
2757 test_200() {
2758         local tf=$DIR/$tfile
2759         local tf2=$DIR2/$tfile
2760         local tf3=$DIR3/$tfile
2761
2762         $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
2763         $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
2764         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
2765
2766         $LFS mirror extend -N -f $tf-2 $tf ||
2767                 error "merging $tf-2 into $tf failed"
2768         $LFS mirror extend -N -f $tf-3 $tf ||
2769                 error "merging $tf-3 into $tf failed"
2770
2771         mkdir -p $MOUNT2 && mount_client $MOUNT2
2772
2773         mkdir -p $MOUNT3 && mount_client $MOUNT3
2774
2775         verify_flr_state $tf3 "ro"
2776
2777         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
2778         $LCTL set_param fail_loc=0x1A03
2779
2780         local mds_idx=mds$(($($LFS getstripe -m $tf) + 1))
2781         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
2782
2783         declare -a pids
2784
2785         write_file_200 $tf &
2786         pids+=($!)
2787
2788         read_file_200 $tf &
2789         pids+=($!)
2790
2791         write_file_200 $tf2 &
2792         pids+=($!)
2793
2794         read_file_200 $tf2 &
2795         pids+=($!)
2796
2797         resync_file_200 $tf3 &
2798         pids+=($!)
2799
2800         local sleep_time=60
2801         [ "$SLOW" = "yes" ] && sleep_time=360
2802         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
2803                 sleep 1
2804                 ((--sleep_time))
2805         done
2806
2807         rm -f $ctrl_file
2808
2809         echo "Waiting ${pids[@]}"
2810         wait ${pids[@]}
2811
2812         umount_client $MOUNT2
2813         umount_client $MOUNT3
2814
2815         rm -f $lock_file
2816
2817         # resync and verify mirrors
2818         mirror_io resync $tf
2819         get_mirror_ids $tf
2820
2821         local csum=$($LFS mirror read -N ${mirror_array[0]} $tf | md5sum)
2822         for id in ${mirror_array[@]:1}; do
2823                 [ "$($LFS mirror read -N $id $tf | md5sum)" = "$csum" ] ||
2824                         error "checksum error for mirror $id"
2825         done
2826
2827         true
2828 }
2829 run_test 200 "stress test"
2830
2831 cleanup_test_201() {
2832         trap 0
2833         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
2834
2835         umount_client $MOUNT2
2836 }
2837
2838 test_201() {
2839         local delay=${RESYNC_DELAY:-5}
2840
2841         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
2842                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
2843
2844         trap cleanup_test_201 EXIT
2845
2846         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
2847                         changelog_register -n)
2848
2849         mkdir -p $MOUNT2 && mount_client $MOUNT2
2850
2851         local index=0
2852         while :; do
2853                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
2854                 [ -z "$log" ] && { sleep 1; continue; }
2855
2856                 index=$(echo $log | awk '{print $1}')
2857                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
2858                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
2859                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
2860
2861                 ((++index))
2862                 [ -z "$file" ] && continue
2863
2864                 local now=$(date +%s)
2865
2866                 echo "file: $file $fid was modified at $ts, now: $now, " \
2867                      "will be resynced at $((ts+delay))"
2868
2869                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
2870
2871                 mirror_io resync $file
2872                 echo "$file resync done"
2873         done
2874
2875         cleanup_test_201
2876 }
2877 run_test 201 "FLR data mover"
2878
2879 test_202() {
2880         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
2881
2882         local tf=$DIR/$tfile
2883         local ids
2884
2885         $LFS setstripe -E 1M -S 1M -c 1 $tf
2886         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2887         verify_comp_attr stripe-count $tf ${ids[0]} 1
2888
2889         $LFS setstripe --component-add -E 2M -c -1 $tf
2890         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2891         verify_comp_attr stripe-count $tf ${ids[0]} 1
2892         verify_comp_attr stripe-count $tf ${ids[1]} -1
2893
2894         dd if=/dev/zero of=$tf bs=1M count=2
2895         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2896         verify_comp_attr stripe-count $tf ${ids[0]} 1
2897         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
2898 }
2899 run_test 202 "lfs setstripe --add-component wide striping"
2900
2901 test_203() {
2902         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
2903                 skip "Need MDS version at least 2.11.55"
2904         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
2905
2906         local tf=$DIR/$tfile
2907
2908         #create 2 mirrors
2909         $LFS mirror create -N2 -c1 $tf || error "create FLR file $tf"
2910         #delete first mirror
2911         $LFS mirror delete --mirror-id=1 $tf || error "delete first mirror"
2912
2913         $LFS getstripe $tf
2914         local old_id=$($LFS getstripe --mirror-id=2 -I $tf)
2915         local count=$($LFS getstripe --mirror-id=2 -c $tf) ||
2916                 error "getstripe count of mirror 2"
2917         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
2918
2919         #extend a mirror with 2 OSTs
2920         $LFS mirror extend -N -c2 $tf || error "extend mirror"
2921         $LFS getstripe $tf
2922
2923         local new_id=$($LFS getstripe --mirror-id=2 -I $tf)
2924         count=$($LFS getstripe --mirror-id=2 -c $tf) ||
2925                 error "getstripe count of mirror 2"
2926         [[ x$oldid = x$newid ]] ||
2927                 error "mirror 2 changed ID from $old_id to $new_id"
2928         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
2929
2930         count=$($LFS getstripe --mirror-id=3 -c $tf) ||
2931                 error "getstripe count of mirror 3"
2932         [[ x$count = x2 ]] || error "mirror 3 stripe count $count is not 2"
2933 }
2934 run_test 203 "mirror file preserve mirror ID"
2935
2936 # Simple test of FLR + self-extending layout, SEL in non-primary mirror
2937 test_204a() {
2938         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
2939                 skip "skipped for lustre < $SEL_VER"
2940
2941         local comp_file=$DIR/$tdir/$tfile
2942         local flg_opts=""
2943         local found=""
2944
2945         test_mkdir $DIR/$tdir
2946
2947         # first mirror is 0-10M, then 10M-(-1), second mirror is 1M followed
2948         # by extension space to -1
2949         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E-1 -z64M $comp_file ||
2950                 error "Create $comp_file failed"
2951
2952         # Write to first component, extending & staling second mirror
2953         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
2954                 error "dd to extend + stale failed"
2955
2956         $LFS getstripe $comp_file
2957
2958         flg_opts="--component-flags init,stale"
2959         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
2960         [ $found -eq 1 ] || error "write: Second comp end incorrect"
2961
2962         flg_opts="--component-flags extension"
2963         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
2964         [ $found -eq 1 ] || error "write: Third comp start incorrect"
2965
2966         # mirror resync should not change the extents
2967         $LFS mirror resync $comp_file
2968
2969         flg_opts="--component-flags init"
2970         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
2971         [ $found -eq 1 ] || error "resync: Second comp end incorrect"
2972
2973         flg_opts="--component-flags extension"
2974         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
2975         [ $found -eq 1 ] || error "resync: Third comp start incorrect"
2976
2977         sel_layout_sanity $comp_file 5
2978
2979         rm -f $comp_file
2980 }
2981 run_test 204a "FLR write/stale/resync tests with self-extending mirror"
2982
2983 # Simple test of FLR + self-extending layout, SEL in primary mirror
2984 test_204b() {
2985         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
2986                 skip "skipped for lustre < $SEL_VER"
2987
2988         local comp_file=$DIR/$tdir/$tfile
2989         local flg_opts=""
2990         local found=""
2991
2992         test_mkdir $DIR/$tdir
2993
2994         # first mirror is 1M followed by extension space to -1, second mirror
2995         # is 0-10M, then 10M-(-1),
2996         $LFS setstripe -N -E 1M -E-1 -z64M -N -E 10M -E-1 $comp_file ||
2997                 error "Create $comp_file failed"
2998
2999         # Write to first component, extending first component & staling
3000         # other mirror
3001         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
3002                 error "dd to extend + stale failed"
3003
3004         $LFS getstripe $comp_file
3005
3006         flg_opts="--component-flags init"
3007         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3008         [ $found -eq 1 ] || error "write: First comp end incorrect"
3009
3010         flg_opts="--component-flags extension"
3011         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3012         [ $found -eq 1 ] || error "write: Second comp start incorrect"
3013
3014         flg_opts="--component-flags init,stale"
3015         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
3016         [ $found -eq 1 ] || error "write: First mirror comp flags incorrect"
3017
3018         # This component is staled because it overlaps the extended first
3019         # component of the primary mirror, even though it doesn't overlap
3020         # the actual write - thus not inited.
3021         flg_opts="--component-flags stale"
3022         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
3023         [ $found -eq 1 ] || error "write: Second mirror comp flags incorrect"
3024
3025         # mirror resync should not change the extents
3026         $LFS mirror resync $comp_file
3027
3028         $LFS getstripe $comp_file
3029
3030         flg_opts="--component-flags init"
3031         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3032         [ $found -eq 1 ] || error "resync: First comp end incorrect"
3033
3034         flg_opts="--component-flags extension"
3035         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3036         [ $found -eq 1 ] || error "resync: Second comp start incorrect"
3037
3038         flg_opts="--component-flags init"
3039         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
3040         [ $found -eq 1 ] || error "resync: First mirror comp flags incorrect"
3041
3042         flg_opts="--component-flags init"
3043         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
3044         [ $found -eq 1 ] || error "resync: Second mirror comp flags incorrect"
3045
3046         sel_layout_sanity $comp_file 5
3047
3048         rm -f $comp_file
3049 }
3050 run_test 204b "FLR write/stale/resync tests with self-extending primary"
3051
3052 # FLR + SEL failed extension & component removal
3053 # extension space in second mirror
3054 test_204c() {
3055         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3056         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3057                 skip "skipped for lustre < $SEL_VER"
3058
3059         local comp_file=$DIR/$tdir/$tfile
3060         local found=""
3061         local ost_idx1=0
3062         local ost_name=$(ostname_from_index $ost_idx1)
3063
3064         test_mkdir $DIR/$tdir
3065
3066         # first mirror is is 0-10M, then 10M-(-1), second mirror is 0-1M, then
3067         # extension space from 1M to 1G, then normal space to -1
3068         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E 1G -i $ost_idx1 -z 64M \
3069                 -E -1 $comp_file || error "Create $comp_file failed"
3070
3071         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
3072         sleep_maxage
3073
3074         # write to first comp (0 - 10M) of mirror 1, extending + staling
3075         # first + second comp of mirror 2
3076         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3077         RC=$?
3078
3079         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
3080         sleep_maxage
3081
3082         [ $RC -eq 0 ] || error "dd to extend + stale failed"
3083
3084         $LFS getstripe $comp_file
3085
3086         found=$($LFS find --component-start 0m --component-end 1m \
3087                 --comp-flags init,stale $comp_file | wc -l)
3088         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
3089
3090         found=$($LFS find --component-start 1m --component-end EOF \
3091                 --comp-flags stale,^init $comp_file | wc -l)
3092         [ $found -eq 1 ] || error "write: Second mirror comp incorrect"
3093
3094         local mirror_id=$($LFS getstripe --component-start=1m   \
3095                          --component-end=EOF $comp_file |       \
3096                          grep lcme_mirror_id | awk '{ print $2 }')
3097
3098         [[ $mirror_id -eq 2 ]] ||
3099                 error "component not in correct mirror? $mirror_id"
3100
3101         $LFS mirror resync $comp_file
3102
3103         $LFS getstripe $comp_file
3104
3105         # component dimensions should not change from resync
3106         found=$($LFS find --component-start 1m --component-end EOF \
3107                 --component-flags init $comp_file | wc -l)
3108         [ $found -eq 1 ] || error "resync: Second mirror comp incorrect"
3109
3110         sel_layout_sanity $comp_file 4
3111
3112         rm -f $comp_file
3113 }
3114 run_test 204c "FLR write/stale/resync test with component removal"
3115
3116 # Successful repeated component in primary mirror
3117 test_204d() {
3118         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3119         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3120                 skip "skipped for lustre < $SEL_VER"
3121
3122         local comp_file=$DIR/$tdir/$tfile
3123         local found=""
3124
3125         wait_delete_completed
3126         wait_mds_ost_sync
3127         test_mkdir $DIR/$tdir
3128
3129         # first mirror is 64M followed by extension space to -1, second mirror
3130         # is 0-10M, then 10M-(-1)
3131         $LFS setstripe -N -E-1 -z64M -N -E 10M -E-1 $comp_file ||
3132                 error "Create $comp_file failed"
3133
3134         local ost_idx1=$($LFS getstripe -I65537 -i $comp_file)
3135         local ost_name=$(ostname_from_index $ost_idx1)
3136         # degrade OST for first comp so we won't extend there
3137         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3138                 obdfilter.$ost_name.degraded=1
3139         sleep_maxage
3140
3141         # Write beyond first component, causing repeat & stale second mirror
3142         dd if=/dev/zero bs=1M count=1 seek=66 of=$comp_file conv=notrunc
3143         RC=$?
3144
3145         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3146                 obdfilter.$ost_name.degraded=0
3147         sleep_maxage
3148
3149         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
3150
3151         $LFS getstripe $comp_file
3152
3153         found=$($LFS find --component-start 64m --component-end 128m \
3154                 --component-flags init $comp_file | wc -l)
3155         [ $found -eq 1 ] || error "write: Repeat comp incorrect"
3156
3157         local ost_idx2=$($LFS getstripe --component-start=64m           \
3158                          --component-end=128m --component-flags=init    \
3159                          -i $comp_file)
3160         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
3161         local mirror_id=$($LFS getstripe --component-start=64m          \
3162                          --component-end=128m --component-flags=init    \
3163                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3164         [[ $mirror_id -eq 1 ]] ||
3165                 error "component not in correct mirror: $mirror_id, not 1"
3166
3167         $LFS mirror resync $comp_file
3168
3169         $LFS getstripe $comp_file
3170
3171         # component dimensions should not change from resync
3172         found=$($LFS find --component-start 0m --component-end 64m \
3173                 --component-flags init $comp_file | wc -l)
3174         [ $found -eq 1 ] || error "resync: first comp incorrect"
3175         found=$($LFS find --component-start 64m --component-end 128m \
3176                 --component-flags init $comp_file | wc -l)
3177         [ $found -eq 1 ] || error "resync: repeat comp incorrect"
3178
3179         sel_layout_sanity $comp_file 5
3180
3181         rm -f $comp_file
3182 }
3183 run_test 204d "FLR write/stale/resync sel test with repeated comp"
3184
3185 # Successful repeated component, SEL in non-primary mirror
3186 test_204e() {
3187         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3188         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3189                 skip "skipped for lustre < $SEL_VER"
3190
3191         local comp_file=$DIR/$tdir/$tfile
3192         local found=""
3193
3194         wait_delete_completed
3195         wait_mds_ost_sync
3196
3197         test_mkdir $DIR/$tdir
3198
3199         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
3200         # space to -1 (-z 64M, so first comp is 0-64M)
3201         # Note: we have to place both 1st components on OST0, otherwise 2 OSTs
3202         # will be not enough - one will be degraded, the other is used on
3203         # an overlapping mirror.
3204         $LFS setstripe -N -E 100M -i 0 -E-1 -N -E-1 -i 0 -z 64M $comp_file ||
3205                 error "Create $comp_file failed"
3206
3207         local ost_idx1=$($LFS getstripe --component-start=0 \
3208                          --component-end=64m -i $comp_file)
3209         local ost_name=$(ostname_from_index $ost_idx1)
3210         # degrade OST for first comp of 2nd mirror so we won't extend there
3211         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3212                 obdfilter.$ost_name.degraded=1
3213         sleep_maxage
3214
3215         $LFS getstripe $comp_file
3216
3217         # Write to first component, stale & instantiate second mirror components
3218         # overlapping with the written component (0-100M);
3219         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3220         RC=$?
3221
3222         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3223                 obdfilter.$ost_name.degraded=0
3224         sleep_maxage
3225         $LFS getstripe $comp_file
3226
3227         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
3228
3229         found=$($LFS find --component-start 0m --component-end 64m \
3230                 --component-flags init,stale $comp_file | wc -l)
3231         [ $found -eq 1 ] || error "write: first comp incorrect"
3232
3233         # was repeated due to degraded ost
3234         found=$($LFS find --component-start 64m --component-end 128m \
3235                 --component-flags init,stale $comp_file | wc -l)
3236         [ $found -eq 1 ] || error "write: repeated comp incorrect"
3237
3238         local ost_idx2=$($LFS getstripe --component-start=64m           \
3239                          --component-end=128m --component-flags=init    \
3240                          -i $comp_file)
3241         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
3242         local mirror_id=$($LFS getstripe --component-start=0m           \
3243                          --component-end=64m --component-flags=init     \
3244                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3245         [[ $mirror_id -eq 2 ]] ||
3246                 error "component not in correct mirror? $mirror_id"
3247
3248         $LFS mirror resync $comp_file
3249
3250         $LFS getstripe $comp_file
3251
3252         # component dimensions should not change from resync
3253         found=$($LFS find --component-start 0m --component-end 64m \
3254                 --component-flags init,^stale $comp_file | wc -l)
3255         [ $found -eq 1 ] || error "resync: first comp incorrect"
3256         found=$($LFS find --component-start 64m --component-end 128m \
3257                 --component-flags init,^stale $comp_file | wc -l)
3258         [ $found -eq 1 ] || error "resync: repeated comp incorrect"
3259
3260         sel_layout_sanity $comp_file 5
3261
3262         rm -f $comp_file
3263 }
3264 run_test 204e "FLR write/stale/resync sel test with repeated comp"
3265
3266 # FLR + SEL: failed repeated component, SEL in non-primary mirror
3267 test_204f() {
3268         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3269         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3270                 skip "skipped for lustre < $SEL_VER"
3271
3272         local comp_file=$DIR/$tdir/$tfile
3273         local found=""
3274
3275         wait_delete_completed
3276         wait_mds_ost_sync
3277         test_mkdir $DIR/$tdir
3278
3279         pool_add $TESTNAME || error "Pool creation failed"
3280         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
3281
3282         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
3283         # space to -1 (-z 64M, so first comp is 0-64M)
3284         $LFS setstripe -N -E 100M -E-1 -N --pool="$TESTNAME" \
3285                 -E-1 -c 1 -z 64M $comp_file || error "Create $comp_file failed"
3286
3287         local ost_name0=$(ostname_from_index 0)
3288         local ost_name1=$(ostname_from_index 1)
3289
3290         # degrade both OSTs in pool, so we'll try to repeat, then fail and
3291         # extend original comp
3292         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=1
3293         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=1
3294         sleep_maxage
3295
3296         # a write to the 1st component, 100M length, which will try to stale
3297         # the first 100M of mirror 2, attempting to extend its 0-64M component
3298         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3299         RC=$?
3300
3301         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=0
3302         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=0
3303         sleep_maxage
3304
3305         [ $RC -eq 0 ] || error "dd to extend mirror comp failed"
3306
3307         $LFS getstripe $comp_file
3308
3309         found=$($LFS find --component-start 0m --component-end 128m \
3310                 --component-flags init,stale $comp_file | wc -l)
3311         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
3312
3313         local mirror_id=$($LFS getstripe --component-start=0m           \
3314                          --component-end=128m --component-flags=init    \
3315                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3316
3317         [[ $mirror_id -eq 2 ]] ||
3318                 error "component not in correct mirror? $mirror_id, not 2"
3319
3320         $LFS mirror resync $comp_file
3321
3322         $LFS getstripe $comp_file
3323
3324         # component dimensions should not change from resync
3325         found=$($LFS find --component-start 0m --component-end 128m \
3326                 --component-flags init,^stale $comp_file | wc -l)
3327         [ $found -eq 1 ] || error "resync: First mirror comp incorrect"
3328
3329         sel_layout_sanity $comp_file 4
3330
3331         rm -f $comp_file
3332 }
3333 run_test 204f "FLR write/stale/resync sel w/forced extension"
3334
3335 function test_205() {
3336         local tf=$DIR/$tfile
3337         local mirrors
3338
3339         $LFS setstripe -c1 $tf
3340         $LFS mirror extend -N $tf
3341         mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
3342         (( $mirrors == 2 )) || error "no new mirror was created?"
3343
3344         $LFS mirror extend -N --flags=prefer $tf
3345         mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
3346         (( $mirrors == 3 )) || error "no new mirror was created?"
3347
3348         $($LFS getstripe $tf | grep lcme_flags: | tail -1 | grep -q prefer) ||
3349                 error "prefer flag was not set on the new mirror"
3350 }
3351 run_test 205 "lfs mirror extend to set prefer flag"
3352
3353 function test_206() {
3354         # create a new OST pool
3355         local pool_name=$TESTNAME
3356
3357         create_pool $FSNAME.$pool_name ||
3358                 error "create OST pool $pool_name failed"
3359         # add OSTs into the pool
3360         pool_add_targets $pool_name 0 1 ||
3361                 error "add OSTs into pool $pool_name failed"
3362
3363         $LFS setstripe -c1 --pool=$pool_name $DIR/$tfile ||
3364                 error "can't setstripe"
3365         $LFS mirror extend -N $DIR/$tfile ||
3366                 error "can't create replica"
3367         if $LFS getstripe $DIR/$tfile | grep -q prefer ; then
3368                 $LFS getstripe $DIR/$tfile
3369                 error "prefer found"
3370         fi
3371         $LFS setstripe --comp-set --comp-flags=prefer -p $pool_name $DIR/$tfile || {
3372                 $LFS getstripe $DIR/$tfile
3373                 error "can't setstripe prefer"
3374         }
3375
3376         if ! $LFS getstripe $DIR/$tfile | grep -q prefer ; then
3377                 $LFS getstripe $DIR/$tfile
3378                 error "no prefer found"
3379         fi
3380
3381         # destroy OST pool
3382         destroy_test_pools
3383 }
3384 run_test 206 "lfs setstripe -pool .. --comp-flags=.. "
3385
3386 test_207() {
3387        local file=$DIR/$tfile
3388        local tmpfile=$DIR/$tfile-tt
3389
3390         [ $MDS1_VERSION -lt $(version_code 2.14.50) ] &&
3391                 skip "Need MDS version at least 2.14.50"
3392
3393         stack_trap "rm -f $tmpfile $file"
3394
3395         # generate data for verification
3396         dd if=/dev/urandom of=$tmpfile bs=1M count=1 ||
3397                 error "can't generate file with random data"
3398
3399         # create a mirrored file with one stale replica
3400         $LFS mirror create -N -S 4M -c 2 -N -S 1M -c -1 $file ||
3401                 error "create mirrored file $file failed"
3402         get_mirror_ids $file
3403         echo "mirror IDs: ${mirror_array[@]}"
3404
3405         dd if=$tmpfile of=$file bs=1M || error "can't copy"
3406         get_mirror_ids $file
3407         echo "mirror IDs: ${mirror_array[@]}"
3408
3409         drop_client_cache
3410         cmp $tmpfile $file || error "files don't match"
3411         get_mirror_ids $file
3412         echo "mirror IDs: ${mirror_array[@]}"
3413
3414         # mirror creation should work fine
3415         $LFS mirror extend -N -S 8M -c -1 $file ||
3416                 error "mirror extend $file failed"
3417
3418         get_mirror_ids $file
3419         echo "mirror IDs: ${mirror_array[@]}"
3420
3421         drop_client_cache
3422         $LFS mirror verify -v $file || error "verification failed"
3423         cmp $tmpfile $file || error "files don't match"
3424 }
3425 run_test 207 "create another replica with existing out-of-sync one"
3426
3427 complete $SECONDS
3428 check_and_cleanup_lustre
3429 exit_status