Whamcloud - gitweb
LU-12296 llite: improve ll_dom_lock_cancel
[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 build_test_filter
22
23 [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
24         { skip "Need MDS version at least 2.10.56"; exit 0; }
25
26 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
27         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
28 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
29
30 check_and_setup_lustre
31 DIR=${DIR:-$MOUNT}
32 assert_DIR
33
34 assert_DIR
35 rm -rf $DIR/[Rdfs][0-9]*
36
37 # global array to store mirror IDs
38 declare -a mirror_array
39 get_mirror_ids() {
40         local tf=$1
41         local id
42         local array
43
44         array=()
45         for id in $($LFS getstripe $tf | awk '/lcme_id/{print $2}'); do
46                 array[${#array[@]}]=$((id >> 16))
47         done
48
49         mirror_array=($(printf "%s\n" "${array[@]}" | sort -u))
50
51         echo ${#mirror_array[@]}
52 }
53
54 drop_client_cache() {
55         echo 3 > /proc/sys/vm/drop_caches
56 }
57
58 stop_osts() {
59         local idx
60
61         for idx in "$@"; do
62                 stop ost$idx
63         done
64
65         for idx in "$@"; do
66                 wait_osc_import_state client ost$idx "\(DISCONN\|IDLE\)"
67         done
68 }
69
70 start_osts() {
71         local idx
72
73         for idx in "$@"; do
74                 start ost$idx $(ostdevname $idx) $OST_MOUNT_OPTS ||
75                         error "start ost$idx failed"
76         done
77
78         for idx in "$@"; do
79                 wait_recovery_complete ost$idx
80         done
81 }
82
83 #
84 # Verify mirror count with an expected value for a given file.
85 #
86 verify_mirror_count() {
87         local tf=$1
88         local expected=$2
89         local mirror_count=$($LFS getstripe -N $tf)
90
91         [[ $mirror_count = $expected ]] || {
92                 $LFS getstripe -v $tf
93                 error "verify mirror count failed on $tf:" \
94                       "$mirror_count != $expected"
95         }
96 }
97
98 #
99 # Verify component count with an expected value for a given file.
100 #       $1 coposited layout file
101 #       $2 expected component number
102 #
103 verify_comp_count() {
104         local tf=$1
105         local expected=$2
106         local comp_count=$($LFS getstripe --component-count $tf)
107
108         [[ $comp_count = $expected ]] || {
109                 $LFS getstripe -v $tf
110                 error "verify component count failed on $tf:" \
111                       "$comp_count != $expected"
112         }
113 }
114
115 #
116 # Verify component attribute with an expected value for a given file
117 # and component ID.
118 #
119 verify_comp_attr() {
120         local attr=$1
121         local tf=$2
122         local comp_id=$3
123         local expected=$4
124         local cmd="$LFS getstripe -I$comp_id"
125         local getstripe_cmd="$cmd -v"
126         local value
127
128         case $attr in
129                 stripe-size) cmd+=" -S $tf" ;;
130                 stripe-count) cmd+=" -c $tf" ;;
131                 stripe-index) cmd+=" -i $tf" ;;
132                 pool) cmd+=" -p $tf" ;;
133                 comp-start) cmd+=" --component-start $tf" ;;
134                 comp-end) cmd+=" --component-end $tf" ;;
135                 lcme_flags) cmd+=" $tf | awk '/lcme_flags:/ { print \$2 }'" ;;
136                 *) error "invalid attribute $attr";;
137         esac
138
139         value=$(eval $cmd)
140
141         [ $attr = lcme_flags ] && {
142                 local fl
143                 local expected_list=$(comma_list $expected)
144                 for fl in ${expected_list//,/ }; do
145                         local neg=0
146
147                         [[ ${fl:0:1} = "^" ]] && neg=1
148                         [[ $neg = 1 ]] && fl=${fl:1}
149
150                         $(echo $value | grep -q $fl)
151                         local match=$?
152                         # 0: matched; 1: not matched
153
154                         if  [[ $neg = 0 && $match != 0 ||
155                                $neg = 1 && $match = 0 ]]; then
156                                 $getstripe_cmd $tf
157                                 [[ $neg = 0 ]] && # expect the flag
158                                     error "expected flag '$fl' not in $comp_id"
159                                 [[ $neg = 1 ]] && # not expect the flag
160                                     error "not expected flag '$fl' in $comp_id"
161                         fi
162                 done
163                 return
164         }
165
166         [[ $value = $expected ]] || {
167                 $getstripe_cmd $tf
168                 error "verify $attr failed on $tf: $value != $expected"
169         }
170 }
171
172 #
173 # Verify component extent with expected start and end extent values
174 # for a given file and component ID.
175 #
176 verify_comp_extent() {
177         local tf=$1
178         local comp_id=$2
179         local expected_start=$3
180         local expected_end=$4
181
182         verify_comp_attr comp-start $tf $comp_id $expected_start
183         verify_comp_attr comp-end $tf $comp_id $expected_end
184 }
185
186 #
187 # Verify component attribute with parent directory for a given file
188 # and component ID.
189 #
190 verify_comp_attr_with_parent() {
191         local attr=$1
192         local tf=$2
193         local comp_id=$3
194         local td=$(cd $(dirname $tf); echo $PWD)
195         local tf_cmd="$LFS getstripe -I$comp_id"
196         local td_cmd="$LFS getstripe"
197         local opt
198         local expected
199         local value
200
201         case $attr in
202                 stripe-size) opt="-S" ;;
203                 stripe-count) opt="-c" ;;
204                 pool) opt="-p" ;;
205                 *) error "invalid attribute $attr";;
206         esac
207
208         expected=$($td_cmd $opt $td)
209         [[ $expected = -1 ]] && expected=$OSTCOUNT
210
211         value=$($tf_cmd $opt $tf)
212         [[ $value = -1 ]] && value=$OSTCOUNT
213
214         [[ $value = $expected ]] || {
215                 $td_cmd -d $td
216                 $tf_cmd -v $tf
217                 error "verify $attr failed with parent on $tf:" \
218                       "$value != $expected"
219         }
220 }
221
222 #
223 # Verify component attribute with filesystem-wide default value for a given file
224 # and component ID.
225 #
226 verify_comp_attr_with_default() {
227         local attr=$1
228         local tf=$2
229         local comp_id=$3
230         local tf_cmd="$LFS getstripe -I$comp_id"
231         local opt
232         local expected
233         local value
234
235         case $attr in
236                 stripe-size)
237                         opt="-S"
238                         expected=$($LCTL get_param -n \
239                                    lov.$FSNAME-clilov-*.stripesize)
240                         ;;
241                 stripe-count)
242                         opt="-c"
243                         expected=$($LCTL get_param -n \
244                                    lov.$FSNAME-clilov-*.stripecount)
245                         [[ $expected = -1 ]] && expected=$OSTCOUNT
246                         ;;
247                 *) error "invalid attribute $attr";;
248         esac
249
250         value=$($tf_cmd $opt $tf)
251         [[ $value = -1 ]] && value=$OSTCOUNT
252
253         [[ $value = $expected ]] || {
254                 $tf_cmd -v $tf
255                 error "verify $attr failed with default value on $tf:" \
256                       "$value != $expected"
257         }
258 }
259
260 #
261 # Verify unspecified component attributes for a given file
262 # and component ID.
263 #
264 # This will only verify the inherited attributes:
265 # stripe size, stripe count and OST pool name
266 #
267 verify_comp_attrs() {
268         local tf=$1
269         local comp_id=$2
270
271         verify_comp_attr_with_default stripe-size $tf $comp_id
272         verify_comp_attr_with_default stripe-count $tf $comp_id
273         verify_comp_attr_with_parent pool $tf $comp_id
274 }
275
276 # command line test cases
277 test_0a() {
278         local td=$DIR/$tdir
279         local tf=$td/$tfile
280         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
281         local mirror_cmd="$LFS mirror create"
282         local id
283         local ids
284         local i
285
286         # create parent directory
287         mkdir $td || error "mkdir $td failed"
288
289         $mirror_cmd $tf &> /dev/null && error "miss -N option"
290
291         $mirror_cmd -N $tf || error "create mirrored file $tf failed"
292         verify_mirror_count $tf 1
293         id=$($LFS getstripe -I $tf)
294         verify_comp_attrs $tf $id
295         verify_comp_extent $tf $id 0 EOF
296
297         $mirror_cmd -N0 $tf-1 &> /dev/null && error "invalid mirror count 0"
298         $mirror_cmd -N$((mirror_count + 1)) $tf-1 &> /dev/null &&
299                 error "invalid mirror count $((mirror_count + 1))"
300
301         $mirror_cmd -N$mirror_count $tf-1 ||
302                 error "create mirrored file $tf-1 failed"
303         verify_mirror_count $tf-1 $mirror_count
304         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
305         for ((i = 0; i < $mirror_count; i++)); do
306                 verify_comp_attrs $tf-1 ${ids[$i]}
307                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
308         done
309
310         $mirror_cmd -N -N2 -N3 -N4 $tf-2 ||
311                 error "create mirrored file $tf-2 failed"
312         verify_mirror_count $tf-2 10
313         ids=($($LFS getstripe $tf-2 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
314         for ((i = 0; i < 10; i++)); do
315                 verify_comp_attrs $tf-2 ${ids[$i]}
316                 verify_comp_extent $tf-2 ${ids[$i]} 0 EOF
317         done
318 }
319 run_test 0a "lfs mirror create with -N option"
320
321 test_0b() {
322         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
323
324         local td=$DIR/$tdir
325         local tf=$td/$tfile
326         local mirror_cmd="$LFS mirror create"
327         local ids
328         local i
329
330         # create a new OST pool
331         local pool_name=$TESTNAME
332         create_pool $FSNAME.$pool_name ||
333                 error "create OST pool $pool_name failed"
334
335         # add OSTs into the pool
336         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
337                 error "add OSTs into pool $pool_name failed"
338
339         # create parent directory
340         mkdir $td || error "mkdir $td failed"
341         $LFS setstripe -S 8M -c -1 -p $pool_name $td ||
342                 error "$LFS setstripe $td failed"
343
344         # create a mirrored file with plain layout mirrors
345         $mirror_cmd -N -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
346                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
347                 error "create mirrored file $tf failed"
348         verify_mirror_count $tf 6
349         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
350         for ((i = 0; i < 6; i++)); do
351                 verify_comp_extent $tf ${ids[$i]} 0 EOF
352         done
353
354         # verify component ${ids[0]}
355         verify_comp_attrs $tf ${ids[0]}
356
357         # verify component ${ids[1]}
358         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
359         verify_comp_attr stripe-count $tf ${ids[1]} 2
360         verify_comp_attr stripe-index $tf ${ids[1]} 2
361         verify_comp_attr pool $tf ${ids[1]} flash
362
363         # verify component ${ids[2]}
364         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
365         verify_comp_attr stripe-count $tf ${ids[2]} 2
366         verify_comp_attr pool $tf ${ids[2]} flash
367
368         # verify component ${ids[3]}
369         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
370         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
371         verify_comp_attr pool $tf ${ids[3]} flash
372
373         # verify component ${ids[4]}
374         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
375         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
376         verify_comp_attr pool $tf ${ids[4]} archive
377
378         # verify component ${ids[5]}
379         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
380         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
381         verify_comp_attr_with_parent pool $tf ${ids[5]}
382
383         # destroy OST pool
384         destroy_test_pools
385 }
386 run_test 0b "lfs mirror create plain layout mirrors"
387
388 test_0c() {
389         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
390
391         local td=$DIR/$tdir
392         local tf=$td/$tfile
393         local mirror_cmd="$LFS mirror create"
394         local ids
395         local i
396
397         # create a new OST pool
398         local pool_name=$TESTNAME
399         create_pool $FSNAME.$pool_name ||
400                 error "create OST pool $pool_name failed"
401
402         # add OSTs into the pool
403         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
404                 error "add OSTs into pool $pool_name failed"
405
406         # create parent directory
407         mkdir $td || error "mkdir $td failed"
408         $LFS setstripe -E 32M -S 8M -c -1 -p $pool_name -E eof -S 16M $td ||
409                 error "$LFS setstripe $td failed"
410
411         # create a mirrored file with composite layout mirrors
412         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
413                     -N -c 4 -p none \
414                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
415                 error "create mirrored file $tf failed"
416         verify_mirror_count $tf 6
417         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
418
419         # verify components ${ids[0]} and ${ids[2]}
420         for i in 0 2; do
421                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
422                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
423                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
424                 verify_comp_attr pool $tf ${ids[$i]} flash
425                 verify_comp_extent $tf ${ids[$i]} 0 4194304
426         done
427
428         # verify components ${ids[1]} and ${ids[3]}
429         for i in 1 3; do
430                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
431                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
432                 verify_comp_attr pool $tf ${ids[$i]} flash
433                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
434         done
435
436         # verify component ${ids[4]}
437         verify_comp_attr stripe-size $tf ${ids[4]} 4194304
438         verify_comp_attr stripe-count $tf ${ids[4]} 4
439         verify_comp_attr_with_parent pool $tf ${ids[4]}
440         verify_comp_extent $tf ${ids[4]} 0 EOF
441
442         # verify components ${ids[5]}, ${ids[7]} and ${ids[9]}
443         for i in 5 7 9; do
444                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
445                 verify_comp_attr stripe-count $tf ${ids[$i]} 4
446                 verify_comp_attr pool $tf ${ids[$i]} archive
447                 verify_comp_extent $tf ${ids[$i]} 0 536870912
448         done
449
450         # verify components ${ids[6]}, ${ids[8]} and ${ids[10]}
451         for i in 6 8 10; do
452                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
453                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
454                 verify_comp_attr pool $tf ${ids[$i]} archive
455                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
456         done
457
458         # destroy OST pool
459         destroy_test_pools
460 }
461 run_test 0c "lfs mirror create composite layout mirrors"
462
463 test_0d() {
464         local td=$DIR/$tdir
465         local tf=$td/$tfile
466         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
467         local mirror_cmd="$LFS mirror extend"
468         local ids
469         local i
470
471         # create parent directory
472         mkdir $td || error "mkdir $td failed"
473
474         $mirror_cmd $tf &> /dev/null && error "miss -N option"
475         $mirror_cmd -N $tf &> /dev/null && error "$tf does not exist"
476
477         # create a non-mirrored file, convert it to a mirrored file and extend
478         touch $tf || error "touch $tf failed"
479         $mirror_cmd -N $tf || error "convert and extend $tf failed"
480         verify_mirror_count $tf 2
481         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
482         for ((i = 0; i < 2; i++)); do
483                 verify_comp_attrs $tf ${ids[$i]}
484                 verify_comp_extent $tf ${ids[$i]} 0 EOF
485         done
486
487         lfsck_verify_pfid $tf || error "PFID is not set"
488
489         # create a mirrored file and extend it
490         $LFS mirror create -N $tf-1 || error "create mirrored file $tf-1 failed"
491         $LFS mirror create -N $tf-2 || error "create mirrored file $tf-2 failed"
492
493         $mirror_cmd -N -S 4M -N -f $tf-2 $tf-1 &> /dev/null &&
494                 error "setstripe options should not be specified with -f option"
495
496         $mirror_cmd -N$((mirror_count - 1)) $tf-1 ||
497                 error "extend mirrored file $tf-1 failed"
498         verify_mirror_count $tf-1 $mirror_count
499         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
500         for ((i = 0; i < $mirror_count; i++)); do
501                 verify_comp_attrs $tf-1 ${ids[$i]}
502                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
503         done
504
505         $mirror_cmd -N $tf-1 &> /dev/null &&
506                 error "exceeded maximum mirror count $mirror_count" || true
507 }
508 run_test 0d "lfs mirror extend with -N option"
509
510 test_0e() {
511         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
512
513         local td=$DIR/$tdir
514         local tf=$td/$tfile
515         local mirror_cmd="$LFS mirror extend"
516         local ids
517         local i
518
519         # create parent directory
520         mkdir $td || error "mkdir $td failed"
521
522         # create a mirrored file with plain layout mirrors
523         $LFS mirror create -N -S 32M -c 3 -p ssd -i 1 -o 1,2,3 $tf ||
524                 error "create mirrored file $tf failed"
525
526         # extend the mirrored file with plain layout mirrors
527         $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
528                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
529                 error "extend mirrored file $tf failed"
530         verify_mirror_count $tf 6
531         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
532         for ((i = 0; i < 6; i++)); do
533                 verify_comp_extent $tf ${ids[$i]} 0 EOF
534         done
535
536         # verify component ${ids[0]}
537         verify_comp_attr stripe-size $tf ${ids[0]} 33554432
538         verify_comp_attr stripe-count $tf ${ids[0]} 3
539         verify_comp_attr stripe-index $tf ${ids[0]} 1
540         verify_comp_attr pool $tf ${ids[0]} ssd
541
542         # verify component ${ids[1]}
543         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
544         verify_comp_attr stripe-count $tf ${ids[1]} 2
545         verify_comp_attr stripe-index $tf ${ids[1]} 2
546         verify_comp_attr pool $tf ${ids[1]} flash
547
548         # verify component ${ids[2]}
549         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
550         verify_comp_attr stripe-count $tf ${ids[2]} 2
551         verify_comp_attr pool $tf ${ids[2]} flash
552
553         # verify component ${ids[3]}
554         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
555         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
556         verify_comp_attr pool $tf ${ids[3]} flash
557
558         # verify component ${ids[4]}
559         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
560         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
561         verify_comp_attr pool $tf ${ids[4]} archive
562
563         # verify component ${ids[5]}
564         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
565         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
566         verify_comp_attr_with_parent pool $tf ${ids[5]}
567 }
568 run_test 0e "lfs mirror extend plain layout mirrors"
569
570 test_0f() {
571         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
572
573         local td=$DIR/$tdir
574         local tf=$td/$tfile
575         local mirror_cmd="$LFS mirror extend"
576         local ids
577         local i
578
579         # create parent directory
580         mkdir $td || error "mkdir $td failed"
581
582         # create a mirrored file with composite layout mirror
583         $LFS mirror create -N -E 32M -S 16M -p ssd -E eof -S 32M $tf ||
584                 error "create mirrored file $tf failed"
585
586         # extend the mirrored file with composite layout mirrors
587         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
588                     -N -c -1 -p none \
589                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
590                 error "extend mirrored file $tf failed"
591         verify_mirror_count $tf 7
592         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
593
594         # verify component ${ids[0]}
595         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
596         verify_comp_attr_with_default stripe-count $tf ${ids[0]}
597         verify_comp_attr pool $tf ${ids[0]} ssd
598         verify_comp_extent $tf ${ids[0]} 0 33554432
599
600         # verify component ${ids[1]}
601         verify_comp_attr stripe-size $tf ${ids[1]} 33554432
602         verify_comp_attr_with_default stripe-count $tf ${ids[1]}
603         verify_comp_attr pool $tf ${ids[1]} ssd
604         verify_comp_extent $tf ${ids[1]} 33554432 EOF
605
606         # verify components ${ids[2]} and ${ids[4]}
607         for i in 2 4; do
608                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
609                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
610                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
611                 verify_comp_attr pool $tf ${ids[$i]} flash
612                 verify_comp_extent $tf ${ids[$i]} 0 4194304
613         done
614
615         # verify components ${ids[3]} and ${ids[5]}
616         for i in 3 5; do
617                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
618                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
619                 verify_comp_attr pool $tf ${ids[$i]} flash
620                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
621         done
622
623         # verify component ${ids[6]}
624         verify_comp_attr stripe-size $tf ${ids[6]} 4194304
625         verify_comp_attr stripe-count $tf ${ids[6]} $OSTCOUNT
626         verify_comp_attr_with_parent pool $tf ${ids[6]}
627         verify_comp_extent $tf ${ids[6]} 0 EOF
628
629         # verify components ${ids[7]}, ${ids[9]} and ${ids[11]}
630         for i in 7 9 11; do
631                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
632                 verify_comp_attr stripe-count $tf ${ids[$i]} $OSTCOUNT
633                 verify_comp_attr pool $tf ${ids[$i]} archive
634                 verify_comp_extent $tf ${ids[$i]} 0 536870912
635         done
636
637         # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
638         for i in 8 10 12; do
639                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
640                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
641                 verify_comp_attr pool $tf ${ids[$i]} archive
642                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
643         done
644 }
645 run_test 0f "lfs mirror extend composite layout mirrors"
646
647 test_0g() {
648         local tf=$DIR/$tfile
649
650         $LFS mirror create -N -E 1M -S 1M -o0 --flags=prefer -E eof -o1 \
651                            -N -o1 $tf || error "create mirrored file $tf failed"
652
653         verify_comp_attr lcme_flags $tf 0x10001 prefer
654         verify_comp_attr lcme_flags $tf 0x10002 prefer
655
656         # write to the mirrored file and check primary
657         cp /etc/hosts $tf || error "error writing file '$tf'"
658
659         verify_comp_attr lcme_flags $tf 0x20003 stale
660
661         # resync file and check prefer flag
662         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
663
664         cancel_lru_locks osc
665         $LCTL set_param osc.*.stats=clear
666         cat $tf &> /dev/null || error "error reading file '$tf'"
667
668         # verify that the data was provided by OST1 where mirror 1 resides
669         local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-[-0-9a-f]*.stats |
670                         awk '/ost_read/{print $2}')
671         [ -n "$nr_read" ] || error "read was not provided by OST1"
672 }
673 run_test 0g "lfs mirror create flags support"
674
675 test_0h() {
676         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
677                 skip "Need MDS version at least 2.11.57"
678
679         local td=$DIR/$tdir
680         local tf=$td/$tfile
681         local ids
682         local i
683
684         # create parent directory
685         test_mkdir $td || error "mkdir $td failed"
686
687         $LFS setstripe -N -E 1M -S 1M --flags=prefer -E eof -N2 $td ||
688                 error "set default mirrored layout on directory $td failed"
689
690         # verify flags are inherited from the directory
691         touch $tf
692
693         verify_comp_attr lcme_flags $tf 0x10001 prefer
694         verify_comp_attr lcme_flags $tf 0x10002 prefer
695
696         # set flags to the first component
697         $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,stale $tf
698
699         verify_comp_attr lcme_flags $tf 0x10001 stale
700         verify_comp_attr lcme_flags $tf 0x10002 prefer
701
702         $LFS setstripe --comp-set -I0x10001 --comp-flags=^stale $tf &&
703                 error "clearing 'stale' should fail"
704
705         # write and resync file. It can't resync the file directly because the
706         # file state is still 'ro'
707         cp /etc/hosts $tf || error "error writing file '$tf'"
708         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
709
710         $LFS setstripe --comp-set -I 0x20003 --comp-flags=prefer $tf ||
711                 error "error setting flag prefer"
712
713         verify_comp_attr lcme_flags $tf 0x20003 prefer
714 }
715 run_test 0h "set, clear and test flags for FLR files"
716
717 test_1() {
718         local tf=$DIR/$tfile
719         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
720         local mirror_create_cmd="$LFS mirror create"
721         local stripes[0]=$OSTCOUNT
722
723         mirror_create_cmd+=" -N -c ${stripes[0]}"
724         for ((i = 1; i < $mirror_count; i++)); do
725                 # add mirrors with different stripes to the file
726                 stripes[$i]=$((RANDOM % OSTCOUNT))
727                 [ ${stripes[$i]} -eq 0 ] && stripes[$i]=1
728
729                 mirror_create_cmd+=" -N -c ${stripes[$i]}"
730         done
731
732         $mirror_create_cmd $tf || error "create mirrored file $tf failed"
733         verify_mirror_count $tf $mirror_count
734
735         # can't create mirrors exceeding LUSTRE_MIRROR_COUNT_MAX
736         $LFS mirror extend -N $tf &&
737                 error "Creating the $((mirror_count+1))th mirror succeeded"
738
739         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
740                         tr '\n' ' '))
741
742         # verify the range of components and stripe counts
743         for ((i = 0; i < $mirror_count; i++)); do
744                 verify_comp_attr stripe-count $tf ${ids[$i]} ${stripes[$i]}
745                 verify_comp_extent $tf ${ids[$i]} 0 EOF
746         done
747 }
748 run_test 1 "create components with setstripe options"
749
750 test_2() {
751         local tf=$DIR/$tfile
752         local tf2=$DIR/$tfile-2
753
754         $LFS setstripe -E 1M -S 1M -E EOF -c 1 $tf
755         $LFS setstripe -E 2M -S 1M -E EOF -c -1 $tf2
756
757         $LFS mirror extend -N -f $tf2 $tf ||
758                 error "merging $tf2 into $tf failed"
759
760         verify_mirror_count $tf 2
761         [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
762 }
763 run_test 2 "create components from existing files"
764
765 test_3() {
766         [[ $MDSCOUNT -lt 2 ]] && skip "need >= 2 MDTs" && return
767
768         for ((i = 0; i < 2; i++)); do
769                 $LFS mkdir -i $i $DIR/$tdir-$i
770                 $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
771         done
772
773         $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
774                 $DIR/$tdir-0/$tfile || error "creating mirrors"
775
776         # mdt doesn't support to cancel layout lock for remote objects, do
777         # it here manually.
778         cancel_lru_locks mdc
779
780         # make sure the mirrorted file was created successfully
781         [[ $($LFS getstripe --component-count $DIR/$tdir-0/$tfile) -eq 2 ]] ||
782                 { $LFS getstripe $DIR/$tdir-0/$tfile;
783                         error "expected 2 components"; }
784
785         # cleanup
786         rm -rf $DIR/$tdir-*
787 }
788 run_test 3 "create components from files located on different MDTs"
789
790 test_4() {
791         local tf=$DIR/$tdir/$tfile
792         local ids=()
793
794         test_mkdir $DIR/$tdir
795
796         # set mirror with setstripe options to directory
797         $LFS mirror create -N2 -E 1M -S 1M -E eof $DIR/$tdir ||
798                 error "set mirror to directory error"
799
800         [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
801                 x"mirrored" ] || error "failed to create mirrored dir"
802
803         touch $tf
804         verify_mirror_count $tf 2
805
806         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
807         verify_comp_extent $tf ${ids[0]} 0 1048576
808         verify_comp_extent $tf ${ids[1]} 1048576 EOF
809
810         # sub directory should inherit mirror setting from parent
811         test_mkdir $DIR/$tdir/td
812         [ x$($LFS getstripe -v $DIR/$tdir/td | awk '/lcm_flags/{print $2}') = \
813                 x"mirrored" ] || error "failed to inherit mirror from parent"
814
815         # mirror extend won't be applied to directory
816         $LFS mirror extend -N2 $DIR/$tdir &&
817                 error "expecting mirror extend failure"
818         true
819 }
820 run_test 4 "Make sure mirror attributes can be inhertied from directory"
821
822 test_5() {
823         local tf=$DIR/$tfile
824         local ids=()
825
826         $MULTIOP $tf oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T12345c ||
827                 error "failed to create file with non-empty layout"
828         $CHECKSTAT -t file -s 12345 $tf || error "size error: expecting 12345"
829
830         $LFS mirror create -N3 $tf || error "failed to attach mirror layout"
831         verify_mirror_count $tf 3
832
833         $CHECKSTAT -t file -s 12345 $tf ||
834                 error "size error after attaching layout "
835 }
836 run_test 5 "Make sure init size work for mirrored layout"
837
838 # LU=10112: disable dom+flr for phase 1
839 test_6() {
840         local tf=$DIR/$tfile
841
842         $LFS mirror create -N -E 1M -S 1M -L mdt -E eof -N -E eof $tf &&
843                 error "expect failure to create mirrored file with DoM"
844
845         $LFS mirror create -N -E 1M -S 1M -E eof -N -E 1M -L mdt -E eof $tf &&
846                 error "expect failure to create mirrored file with DoM"
847
848         $LFS setstripe -E 1M -S 1M -L mdt -E eof $tf
849         $LFS mirror extend -N2 $tf &&
850                 error "expect failure to extend mirror with DoM"
851
852         $LFS mirror create -N2 -E 1M -S 1M -E eof $tf-2
853         $LFS mirror extend -N -f $tf $tf-2 &&
854                 error "expect failure to extend mirrored file with DoM extent"
855
856         true
857 }
858 run_test 6 "DoM and FLR won't co-exist for phase 1"
859
860 test_21() {
861         local tf=$DIR/$tfile
862         local tf2=$DIR/$tfile-2
863
864         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
865
866         $LFS setstripe -E EOF -o 0 $tf
867         $LFS setstripe -E EOF -o 1 $tf2
868
869         local dd_count=$((RANDOM % 20 + 1))
870         dd if=/dev/zero of=$tf bs=1M count=$dd_count oflag=sync
871         dd if=/dev/zero of=$tf2 bs=1M count=1 seek=$((dd_count - 1)) oflag=sync
872
873         # for zfs - sync OST dataset so that du below will return
874         # accurate results
875         [ "$FSTYPE" = "zfs" ] &&
876                 do_nodes $(comma_list $(osts_nodes)) "$ZPOOL sync"
877
878         local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
879
880         # add component
881         $LFS mirror extend -N -f $tf2 $tf ||
882                 error "merging $tf2 into $tf failed"
883
884         # cancel layout lock
885         cancel_lru_locks mdc
886
887         local new_blocks=$(du -k $tf | awk '{print $1}')
888         [ $new_blocks -eq $blocks ] ||
889         error "i_blocks error expected: $blocks, actual: $new_blocks"
890 }
891 run_test 21 "glimpse should report accurate i_blocks"
892
893 get_osc_lock_count() {
894         local lock_count=0
895
896         for idx in "$@"; do
897                 local osc_name
898                 local count
899
900                 osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'[-0-9a-f]*'
901                 count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
902                 lock_count=$((lock_count + count))
903         done
904         echo $lock_count
905 }
906
907 test_22() {
908         local tf=$DIR/$tfile
909
910         $LFS setstripe -E EOF -o 0 $tf
911         dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
912
913         # add component, two mirrors located on the same OST ;-)
914         $LFS mirror extend -N -o 0 $tf ||
915                 error "extending mirrored file $tf failed"
916
917         size_blocks=$(stat --format="%b %s" $tf)
918
919         cancel_lru_locks mdc
920         cancel_lru_locks osc
921
922         local new_size_blocks=$(stat --format="%b %s" $tf)
923
924         # make sure there is no lock cached
925         [ $(get_osc_lock_count 1) -eq 0 ] || error "glimpse requests were sent"
926
927         [ "$new_size_blocks" = "$size_blocks" ] ||
928                 echo "size expected: $size_blocks, actual: $new_size_blocks"
929
930         rm -f $tmpfile
931 }
932 run_test 22 "no glimpse to OSTs for READ_ONLY files"
933
934 test_31() {
935         local tf=$DIR/$tfile
936
937         $LFS mirror create -N -o 0 -N -o 1 $tf ||
938                 error "creating mirrored file $tf failed"
939
940         #define OBD_FAIL_GLIMPSE_IMMUTABLE 0x1A00
941         $LCTL set_param fail_loc=0x1A00
942
943         local ost_idx
944         for ((ost_idx = 1; ost_idx <= 2; ost_idx++)); do
945                 cancel_lru_locks osc
946                 stop_osts $ost_idx
947
948                 local tmpfile=$(mktemp)
949                 stat --format="%b %s" $tf > $tmpfile  &
950                 local pid=$!
951
952                 local cnt=0
953                 while [ $cnt -le 5 ]; do
954                         kill -0 $pid > /dev/null 2>&1 || break
955                         sleep 1
956                         ((cnt += 1))
957                 done
958                 kill -0 $pid > /dev/null 2>&1 &&
959                         error "stat process stuck due to unavailable OSTs"
960
961                 # make sure glimpse request has been sent
962                 [ $(get_osc_lock_count 1 2) -ne 0 ] ||
963                         error "OST $ost_idx: no glimpse request was sent"
964
965                 start_osts $ost_idx
966         done
967 }
968 run_test 31 "make sure glimpse request can be retried"
969
970 test_32() {
971         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
972         rm -f $DIR/$tfile $DIR/$tfile-2
973
974         $LFS setstripe -E EOF -o 0 $DIR/$tfile
975         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=$((RANDOM % 10 + 2))
976
977         local fsize=$(stat -c %s $DIR/$tfile)
978         [[ $fsize -ne 0 ]] || error "file size is (wrongly) zero"
979
980         local cksum=$(md5sum $DIR/$tfile)
981
982         # create a new mirror in sync mode
983         $LFS mirror extend -N -o 1 $DIR/$tfile ||
984                 error "extending mirrored file $DIR/$tfile failed"
985
986         # make sure the mirrored file was created successfully
987         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
988                 { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
989
990         drop_client_cache
991         stop_osts 1
992
993         # check size is correct, glimpse request should go to the 2nd mirror
994         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
995                 error "file size error $fsize vs. $(stat -c %s $DIR/$tfile)"
996
997         echo "reading file from the 2nd mirror and verify checksum"
998         [[ "$cksum" == "$(md5sum $DIR/$tfile)" ]] ||
999                 error "checksum error: expected $cksum"
1000
1001         start_osts 1
1002 }
1003 run_test 32 "data should be mirrored to newly created mirror"
1004
1005 test_33() {
1006         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1007
1008         rm -f $DIR/$tfile $DIR/$tfile-2
1009
1010         # create a file with two mirrors
1011         $LFS setstripe -E EOF -o 0 $DIR/$tfile
1012         local max_count=100
1013         local count=0
1014         while [ $count -lt $max_count ]; do
1015                 echo "ost1" >> $DIR/$tfile
1016                 count=$((count + 1));
1017         done
1018
1019         # tmp file that will be used as mirror
1020         $LFS setstripe -E EOF -o 1 $DIR/$tfile-2
1021         count=0
1022         while [ $count -lt $max_count ]; do
1023                 echo "ost2" >> $DIR/$tfile-2
1024                 count=$((count + 1));
1025         done
1026
1027         # create a mirrored file
1028         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
1029                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1030                       "with verification should fail"
1031         $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
1032                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1033                       "without verification failed"
1034
1035         # make sure that $tfile has two mirrors and $tfile-2 does not exist
1036         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1037                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1038
1039         [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
1040
1041         # execpted file size
1042         local fsize=$((5 * max_count))
1043         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1044                 error "mirrored file size is not $fsize"
1045
1046         # read file - all OSTs are available
1047         echo "reading file (data can be provided by any ost)... "
1048         local rs=$(cat $DIR/$tfile | head -1)
1049         [[ "$rs" == "ost1" || "$rs" == "ost2" ]] ||
1050                 error "file content error: expected: \"ost1\" or \"ost2\""
1051
1052         # read file again with ost1 failed
1053         stop_osts 1
1054         drop_client_cache
1055
1056         echo "reading file (data should be provided by ost2)..."
1057         local rs=$(cat $DIR/$tfile | head -1)
1058         [[ "$rs" == "ost2" ]] ||
1059                 error "file content error: expected: \"ost2\", actual: \"$rs\""
1060
1061         # remount ost1
1062         start_osts 1
1063
1064         # read file again with ost2 failed
1065         stop_osts 2
1066         drop_client_cache
1067
1068         # check size, glimpse should work
1069         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1070                 error "mirrored file size is not $fsize"
1071
1072         echo "reading file (data should be provided by ost1)..."
1073         local rs=$(cat $DIR/$tfile | head -1)
1074         [[ "$rs" == "ost1" ]] ||
1075                 error "file content error: expected: \"ost1\", actual: \"$rs\""
1076
1077         start_osts 2
1078 }
1079 run_test 33 "read can choose available mirror to read"
1080
1081 test_34a() {
1082         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1083
1084         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1085
1086         # reference file
1087         $LFS setstripe -o 0 $DIR/$tfile-ref
1088         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1089
1090         # create a file with two mirrors
1091         $LFS setstripe -E -1 -o 0,1 -S 1M $DIR/$tfile
1092         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1093
1094         $LFS setstripe -E -1 -o 2,3 -S 1M $DIR/$tfile-2
1095         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1096
1097         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1098                 error "mirrored file size is not 3M"
1099
1100         # merge a mirrored file
1101         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1102                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1103
1104         cancel_lru_locks osc
1105
1106         # stop two OSTs, so the 2nd stripe of the 1st mirror and
1107         # the 1st stripe of the 2nd mirror will be inaccessible, ...
1108         stop_osts 2 3
1109
1110         echo "comparing files ... "
1111
1112         # however, read can still return the correct data. It should return
1113         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1114         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1115                 $DIR/$tfile-ref || error "file reading error"
1116
1117         start_osts 2 3
1118 }
1119 run_test 34a "read mirrored file with multiple stripes"
1120
1121 test_34b() {
1122         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1123
1124         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1125
1126         # reference file
1127         $LFS setstripe -o 0 $DIR/$tfile-ref
1128         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1129
1130         $LFS setstripe -E 1M -S 1M -o 0 -E eof -o 1 $DIR/$tfile
1131         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1132
1133         $LFS setstripe -E 1M -S 1M -o 2 -E eof -o 3 $DIR/$tfile-2
1134         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1135
1136         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1137                 error "mirrored file size is not 3M"
1138
1139         # merge a mirrored file
1140         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1141                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1142
1143         cancel_lru_locks osc
1144
1145         # stop two OSTs, so the 2nd component of the 1st mirror and
1146         # the 1st component of the 2nd mirror will be inaccessible, ...
1147         stop_osts 2 3
1148
1149         echo "comparing files ... "
1150
1151         # however, read can still return the correct data. It should return
1152         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1153         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1154                 $DIR/$tfile-ref || error "file reading error"
1155
1156         start_osts 2 3
1157 }
1158 run_test 34b "read mirrored file with multiple components"
1159
1160 test_35() {
1161         local tf=$DIR/$tfile
1162
1163         $LFS setstripe -E eof $tf
1164
1165         # add an out-of-sync mirror to the file
1166         $LFS mirror extend -N -c 2 $tf ||
1167                 error "extending mirrored file $tf failed"
1168
1169         $MULTIOP $tf oO_WRONLY:c ||
1170                 error "write open a mirrored file failed"
1171
1172         # truncate file should return error
1173         $TRUNCATE $tf 100 || error "error truncating a mirrored file"
1174 }
1175 run_test 35 "allow to write to mirrored files"
1176
1177 verify_ost_layout_version() {
1178         local tf=$1
1179
1180         # get file layout version
1181         local flv=$($LFS getstripe $tf | awk '/lcm_layout_gen/{print $2}')
1182
1183         # layout version from OST objects
1184         local olv=$($MULTIOP $tf oXc | awk '/ostlayoutversion/{print $2}')
1185
1186         [ $flv -eq $olv ] || error "layout version mismatch: $flv vs. $olv"
1187 }
1188
1189 create_file_36() {
1190         local tf
1191
1192         for tf in "$@"; do
1193                 $LFS setstripe -E 1M -S 1M -E 2M -E 4M -E eof -c -1 $tf
1194                 $LFS setstripe -E 3M -S 1M -E 6M -E eof -c -1 $tf-tmp
1195
1196                 $LFS mirror extend -N -f $tf-tmp $tf ||
1197                         error "merging $tf-tmp into $tf failed"
1198         done
1199 }
1200
1201 test_36() {
1202         local tf=$DIR/$tfile
1203
1204         create_file_36 $tf $tf-2 $tf-3
1205
1206         [ $($LFS getstripe -N $tf) -gt 1 ] || error "wrong mirror count"
1207
1208         # test case 1 - check file write and verify layout version
1209         $MULTIOP $tf oO_WRONLY:c ||
1210                 error "write open a mirrored file failed"
1211
1212         # write open file should not return error
1213         $MULTIOP $tf oO_WRONLY:w1024Yc || error "write mirrored file error"
1214
1215         # instantiate components should work
1216         dd if=/dev/zero of=$tf bs=1M count=12 || error "write file error"
1217
1218         # verify OST layout version
1219         verify_ost_layout_version $tf
1220
1221         # test case 2
1222         local mds_facet=mds$(($($LFS getstripe -m $tf-2) + 1))
1223
1224         local delay_sec=10
1225         do_facet $mds_facet $LCTL set_param fail_val=$delay_sec
1226
1227         #define OBD_FAIL_FLR_LV_DELAY 0x1A01
1228         do_facet $mds_facet $LCTL set_param fail_loc=0x1A01
1229
1230         # write should take at least $fail_loc seconds and succeed
1231         local st=$(date +%s)
1232         $MULTIOP $tf-2 oO_WRONLY:w1024Yc || error "write mirrored file error"
1233
1234         [ $(date +%s) -ge $((st+delay_sec)) ] ||
1235                 error "write finished before layout version is transmitted"
1236
1237         # verify OST layout version
1238         verify_ost_layout_version $tf
1239
1240         do_facet $mds_facet $LCTL set_param fail_loc=0
1241
1242         # test case 3
1243         mds_idx=mds$(($($LFS getstripe -m $tf-3) + 1))
1244
1245         #define OBD_FAIL_FLR_LV_INC 0x1A02
1246         do_facet $mds_facet $LCTL set_param fail_loc=0x1A02
1247
1248         # write open file should return error
1249         $MULTIOP $tf-3 oO_WRONLY:O_SYNC:w1024c &&
1250                 error "write a mirrored file succeeded" || true
1251
1252         do_facet $mds_facet $LCTL set_param fail_loc=0
1253 }
1254 run_test 36 "write to mirrored files"
1255
1256 create_files_37() {
1257         local tf
1258         local fsize=$1
1259
1260         echo "create test files with size $fsize .."
1261
1262         shift
1263         for tf in "$@"; do
1264                 $LFS setstripe -E 1M -S 1M -c 1 -E eof -c -1 $tf
1265
1266                 dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
1267                 $TRUNCATE $tf $fsize
1268         done
1269 }
1270
1271 test_37()
1272 {
1273         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
1274                 skip "Need MDS version at least 2.11.57"
1275
1276         local tf=$DIR/$tfile
1277         local tf2=$DIR/$tfile-2
1278         local tf3=$DIR/$tfile-3
1279         local tf4=$DIR/$tfile-4
1280
1281         create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
1282         rm -f $tf4
1283         cp $tf $tf4
1284
1285         # assume the mirror id will be 1, 2, and 3
1286         declare -A checksums
1287         checksums[1]=$(cat $tf | md5sum)
1288         checksums[2]=$(cat $tf2 | md5sum)
1289         checksums[3]=$(cat $tf3 | md5sum)
1290
1291         printf '%s\n' "${checksums[@]}"
1292
1293         # merge these files into a mirrored file
1294         $LFS mirror extend --no-verify -N -f $tf2 $tf ||
1295                 error "merging $tf2 into $tf failed"
1296         $LFS mirror extend --no-verify -N -f $tf3 $tf ||
1297                 error "merging $tf3 into $tf failed"
1298
1299         get_mirror_ids $tf
1300
1301         # verify mirror read, checksums should equal to the original files'
1302         echo "Verifying mirror read .."
1303
1304         local sum
1305         for i in ${mirror_array[@]}; do
1306                 $LCTL set_param ldlm.namespaces.*.lru_size=clear > /dev/null
1307                 sum=$($LFS mirror read -N $i $tf | md5sum)
1308                 [ "$sum" = "${checksums[$i]}" ] ||
1309                         error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
1310         done
1311
1312         # verify mirror write
1313         echo "Verifying mirror write .."
1314         $LFS mirror write -N2 $tf < $tf4
1315
1316         sum=$($LFS mirror read -N2 $tf | md5sum)
1317         [[ "$sum" = "${checksums[1]}" ]] ||
1318                 error "2: mismatch \'${checksums[1]}\' vs. \'$sum\'"
1319
1320         # verify mirror copy, write to this mirrored file will invalidate
1321         # the other two mirrors
1322         echo "Verifying mirror copy .."
1323
1324         local osts=$(comma_list $(osts_nodes))
1325
1326         $LFS mirror copy -i ${mirror_array[0]} -o-1 $tf ||
1327                 error "mirror copy error"
1328
1329         # verify copying is successful by checking checksums
1330         remount_client $MOUNT
1331         for i in ${mirror_array[@]}; do
1332                 sum=$($LFS mirror read -N $i $tf | md5sum)
1333                 [ "$sum" = "${checksums[1]}" ] ||
1334                         error "$i: mismatch checksum after copy \'$sum\'"
1335         done
1336
1337         rm -f $tf
1338 }
1339 run_test 37 "mirror I/O API verification"
1340
1341 verify_flr_state()
1342 {
1343         local tf=$1
1344         local expected_state=$2
1345
1346         local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
1347         [ $expected_state = $state ] ||
1348                 error "expected: $expected_state, actual $state"
1349 }
1350
1351 test_38() {
1352         local tf=$DIR/$tfile
1353         local ref=$DIR/${tfile}-ref
1354
1355         $LFS setstripe -E 1M -S 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf ||
1356                 error "creating $tf failed"
1357         $LFS setstripe -E 2M -S 1M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 \
1358                 $tf-2 || error "creating $tf-2 failed"
1359         $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3 ||
1360                 error "creating $tf-3 failed"
1361
1362         # instantiate all components
1363         $LFS mirror extend -N -f $tf-2 $tf ||
1364                 error "merging $tf-2 into $tf failed"
1365         $LFS mirror extend -N -f $tf-3 $tf ||
1366                 error "merging $tf-3 into $tf failed"
1367         $LFS mirror extend -N -c 1 $tf ||
1368                 error "extending mirrored file $tf failed"
1369
1370         verify_flr_state $tf "ro"
1371
1372         dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
1373
1374         local fsize=$((RANDOM << 8 + 1048576))
1375         $TRUNCATE $ref $fsize
1376
1377         local ref_cksum=$(cat $ref | md5sum)
1378
1379         # case 1: verify write to mirrored file & resync work
1380         cp $ref $tf || error "copy from $ref to $f error"
1381         verify_flr_state $tf "wp"
1382
1383         local file_cksum=$(cat $tf | md5sum)
1384         [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
1385
1386         get_mirror_ids $tf
1387         echo "mirror IDs: ${mirror_array[@]}"
1388
1389         local valid_mirror stale_mirror id mirror_cksum
1390         for id in "${mirror_array[@]}"; do
1391                 mirror_cksum=$($LFS mirror read -N $id $tf | md5sum)
1392                 [ "$ref_cksum" == "$mirror_cksum" ] &&
1393                         { valid_mirror=$id; continue; }
1394
1395                 stale_mirror=$id
1396         done
1397
1398         [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
1399         [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
1400
1401         mirror_io resync $tf || error "resync failed"
1402         verify_flr_state $tf "ro"
1403
1404         mirror_cksum=$($LFS mirror read -N $stale_mirror $tf | md5sum)
1405         [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
1406
1407         # case 2: inject an error to make mirror_io exit after changing
1408         # the file state to sync_pending so that we can start a concurrent
1409         # write.
1410         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1411         verify_flr_state $tf "wp"
1412
1413         mirror_io resync -e resync_start $tf && error "resync succeeded"
1414         verify_flr_state $tf "sp"
1415
1416         # from sync_pending to write_pending
1417         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1418         verify_flr_state $tf "wp"
1419
1420         mirror_io resync -e resync_start $tf && error "resync succeeded"
1421         verify_flr_state $tf "sp"
1422
1423         # from sync_pending to read_only
1424         mirror_io resync $tf || error "resync failed"
1425         verify_flr_state $tf "ro"
1426 }
1427 run_test 38 "resync"
1428
1429 test_39() {
1430         local tf=$DIR/$tfile
1431
1432         rm -f $tf
1433         $LFS mirror create -N2 -E1m -c1 -S1M -E-1 $tf ||
1434         error "create PFL file $tf failed"
1435
1436         verify_mirror_count $tf 2
1437         verify_comp_count $tf 4
1438
1439         rm -f $tf || error "delete $tf failed"
1440 }
1441 run_test 39 "check FLR+PFL (a.k.a. PFLR) creation"
1442
1443 test_40() {
1444         local tf=$DIR/$tfile
1445         local ops
1446
1447         for ops in "conv=notrunc" ""; do
1448                 rm -f $tf
1449
1450                 $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 --flags=prefer \
1451                                    -N -E 1M -E 2M -E 4M -E -1 $tf ||
1452                         error "create PFLR file $tf failed"
1453                 dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
1454                         error "write PFLR file $tf failed"
1455
1456                 lfs getstripe -vy $tf
1457
1458                 local flags
1459
1460                 # file mirror state should be write_pending
1461                 flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1462                 [ $flags = wp ] ||
1463                 error "file mirror state $flags"
1464                 # the 1st component (in mirror 1) should be inited
1465                 verify_comp_attr lcme_flags $tf 0x10001 init
1466                 # the 2nd component (in mirror 1) should be inited
1467                 verify_comp_attr lcme_flags $tf 0x10002 init
1468                 # the 3rd component (in mirror 1) should be uninited
1469                 verify_comp_attr lcme_flags $tf 0x10003 prefer
1470                 # the 4th component (in mirror 2) should be inited
1471                 verify_comp_attr lcme_flags $tf 0x20004 init
1472                 # the 5th component (in mirror 2) should be uninited
1473                 verify_comp_attr lcme_flags $tf 0x20005 0
1474                 # the 6th component (in mirror 2) should be stale
1475                 verify_comp_attr lcme_flags $tf 0x20006 stale
1476                 # the 7th component (in mirror 2) should be uninited
1477                 if [[ x$ops = "xconv=notrunc" ]]; then
1478                         verify_comp_attr lcme_flags $tf 0x20007 0
1479                 elif [[ x$ops = "x" ]]; then
1480                         verify_comp_attr lcme_flags $tf 0x20007 stale
1481                 fi
1482         done
1483
1484         rm -f $tf || error "delete $tf failed"
1485 }
1486 run_test 40 "PFLR rdonly state instantiation check"
1487
1488 test_41() {
1489         local tf=$DIR/$tfile
1490
1491         rm -f $tf $tf-1
1492         echo " **create two FLR files $tf $tf-1"
1493         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1494                            -N -E 1M -E 2M -E 3M -E -1 $tf ||
1495                 error "create PFLR file $tf failed"
1496         $LFS mirror create -N -E 2M -S 1M -E eof \
1497                            -N -E 1M -E eof --flags prefer \
1498                            -N -E 4m -E eof $tf-1 ||
1499                 error "create PFLR file $tf-1 failed"
1500
1501         # file should be in ro status
1502         echo " **verify files be RDONLY"
1503         verify_flr_state $tf "ro"
1504         verify_flr_state $tf-1 "ro"
1505
1506         # write data in [0, 2M)
1507         dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
1508                 error "writing $tf failed"
1509         dd if=/dev/urandom of=$tf-1 bs=1M count=4 conv=notrunc ||
1510                 error "writing $tf-1 failed"
1511
1512         local sum0=$(cat $tf-1 | md5sum)
1513
1514         echo " **verify files be WRITE_PENDING"
1515         verify_flr_state $tf "wp"
1516         verify_flr_state $tf-1 "wp"
1517
1518         # file should have stale component
1519         echo " **verify files have stale component"
1520         $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
1521                 error "after writing $tf, it does not contain stale component"
1522         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1523                 error "after writing $tf-1, it does not contain stale component"
1524
1525         echo " **full resync"
1526         $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
1527
1528         echo " **verify $tf-1 data consistency in all mirrors"
1529         for i in 1 2 3; do
1530                 local sum=$($LFS mirror read -N$i $tf-1 | md5sum)
1531                 [[ "$sum" = "$sum0" ]] ||
1532                         error "$tf-1.$i: checksum mismatch: $sum != $sum0"
1533         done
1534
1535         echo " **verify files be RDONLY"
1536         verify_flr_state $tf "ro"
1537         verify_flr_state $tf-1 "ro"
1538
1539         # file should not have stale component
1540         echo " **verify files do not contain stale component"
1541         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1542                 error "after resyncing $tf, it contains stale component"
1543         $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
1544                 error "after resyncing $tf, it contains stale component"
1545
1546         # verify partial resync
1547         echo " **write $tf-1 for partial resync test"
1548         dd if=/dev/zero of=$tf-1 bs=1M count=2 conv=notrunc ||
1549                 error "writing $tf-1 failed"
1550
1551         echo " **only resync mirror 2"
1552         verify_flr_state $tf-1 "wp"
1553         $LFS mirror resync --only 2 $tf-1 ||
1554                 error "resync mirror 2 of $tf-1 failed"
1555         verify_flr_state $tf "ro"
1556
1557         # resync synced mirror
1558         echo " **resync mirror 2 again"
1559         $LFS mirror resync --only 2 $tf-1 ||
1560                 error "resync mirror 2 of $tf-1 failed"
1561         verify_flr_state $tf "ro"
1562         echo " **verify $tf-1 contains stale component"
1563         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1564                 error "after writing $tf-1, it does not contain stale component"
1565
1566         echo " **full resync $tf-1"
1567         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1568         verify_flr_state $tf "ro"
1569         echo " **full resync $tf-1 again"
1570         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1571         echo " **verify $tf-1 does not contain stale component"
1572         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1573                 error "after resyncing $tf, it contains stale component"
1574
1575         return 0
1576 }
1577 run_test 41 "lfs mirror resync check"
1578
1579 test_42() {
1580         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1581
1582         local td=$DIR/$tdir
1583         local tf=$td/$tfile
1584         local mirror_cmd="$LFS mirror verify"
1585         local i
1586
1587         # create parent directory
1588         mkdir $td || error "mkdir $td failed"
1589
1590         $mirror_cmd &> /dev/null && error "no file name given"
1591         $mirror_cmd $tf &> /dev/null && error "cannot stat file $tf"
1592         $mirror_cmd $td &> /dev/null && error "$td is not a regular file"
1593
1594         # create mirrored files
1595         $LFS mirror create -N -E 4M -S 1M -E 10M -E EOF $tf ||
1596                 error "create mirrored file $tf failed"
1597         $LFS mirror create -N -E 2M -S 1M -E EOF \
1598                            -N -E 6M -E 8M -E EOF \
1599                            -N -E 16M -E EOF $tf-1 ||
1600                 error "create mirrored file $tf-1 failed"
1601         $LFS mirror create -N -c 2 -o 1,3 -N -S 2M -c -1 $tf-2 ||
1602                 error "create mirrored file $tf-2 failed"
1603
1604         # write data in [0, 10M)
1605         for i in $tf $tf-1 $tf-2; do
1606                 yes | dd of=$i bs=1M count=10 iflag=fullblock conv=notrunc ||
1607                         error "write $i failed"
1608         done
1609
1610         # resync the mirrored files
1611         $LFS mirror resync $tf-1 $tf-2 ||
1612                 error "resync $tf-1 $tf-2 failed"
1613
1614         # verify the mirrored files
1615         $mirror_cmd $tf-1 $tf-2 ||
1616                 error "verify $tf-1 $tf-2 failed"
1617
1618         get_mirror_ids $tf-1
1619         $mirror_cmd --only ${mirror_array[0]} $tf-1 &> /dev/null &&
1620                 error "at least 2 mirror ids needed with '--only' option"
1621         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 $tf-2 \
1622                 &> /dev/null &&
1623                 error "'--only' option cannot be used upon multiple files"
1624         $mirror_cmd --only 65534,${mirror_array[0]},65535 $tf-1 &&
1625                 error "invalid specified mirror ids"
1626
1627         # change the content of $tf and merge it into $tf-1
1628         for i in 6 10; do
1629                 echo a | dd of=$tf bs=1M seek=$i conv=notrunc ||
1630                         error "change $tf with seek=$i failed"
1631                 echo b | dd of=$tf-1 bs=1M seek=$i conv=notrunc ||
1632                         error "change $tf-1 with seek=$i failed"
1633         done
1634
1635         $LFS mirror resync $tf-1 || error "resync $tf-1 failed"
1636         $LFS mirror extend --no-verify -N -f $tf $tf-1 ||
1637                 error "merge $tf into $tf-1 failed"
1638
1639         # verify the mirrored files
1640         echo "Verify $tf-1 without -v option:"
1641         $mirror_cmd $tf-1 &&
1642                 error "verify $tf-1 should fail" || echo "PASS"
1643
1644         echo "Verify $tf-1 with -v option:"
1645         $mirror_cmd -v $tf-1 &&
1646                 error "verify $tf-1 should fail"
1647
1648         get_mirror_ids $tf-1
1649         echo "Verify $tf-1 with --only option:"
1650         $mirror_cmd -v --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 &&
1651                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1652                       "${mirror_array[-1]} should fail"
1653
1654         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 ||
1655                 error "verify $tf-1 with mirror ${mirror_array[0]} and" \
1656                       "${mirror_array[1]} should succeed"
1657
1658         # set stale components in $tf-1
1659         for i in 0x40002 0x40003; do
1660                 $LFS setstripe --comp-set -I$i --comp-flags=stale $tf-1 ||
1661                         error "set stale flag on component $i failed"
1662         done
1663
1664         # verify the mirrored file
1665         echo "Verify $tf-1 with stale components:"
1666         $mirror_cmd -vvv $tf-1 ||
1667                 error "verify $tf-1 with stale components should succeed"
1668
1669         echo "Verify $tf-1 with stale components and --only option:"
1670         $mirror_cmd -vvv --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 ||
1671                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1672                       "${mirror_array[-1]} should succeed"
1673 }
1674 run_test 42 "lfs mirror verify"
1675
1676 # inactivate one OST && write && restore the OST
1677 write_file_43() {
1678         local file=$1
1679         local ost=$2
1680         local PARAM="osp.${FSNAME}-OST000${ost}-osc-M*.active"
1681         local wait
1682
1683         wait=$(do_facet $SINGLEMDS \
1684                 "$LCTL get_param -n lod.*MDT0000-*.qos_maxage")
1685         wait=${wait%%[^0-9]*}
1686
1687         echo "  **deactivate OST$ost, waiting for $((wait*2+2)) seconds"
1688         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 0")
1689         # lod_qos_statfs_update needs 2*$wait seconds to refresh targets statfs
1690         sleep $(($wait * 2 + 2))
1691         echo "  **write $file"
1692         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
1693         echo "  **restore activating OST$ost, waiting for $((wait*2+2)) seconds"
1694         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 1")
1695         sleep $((wait * 2 + 2))
1696
1697         local flags=$($LFS getstripe -v $file | awk '/lcm_flags:/ { print $2 }')
1698         [ $flags = wp ] || error "file mirror state $flags != wp"
1699 }
1700
1701 test_43() {
1702         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
1703
1704         local tf=$DIR/$tfile
1705         local flags
1706
1707         rm -f $tf
1708         ##   mirror 0  ost (0, 1)
1709         ##   mirror 1  ost (1, 2)
1710         ##   mirror 2  ost (2, 0)
1711         $LFS mirror create -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 \
1712                 -N -Eeof -c2 -o2,0 $tf ||
1713                 error "create 3 mirrors file $tf failed"
1714
1715         ################## OST0 ###########################################
1716         write_file_43 $tf 0
1717         echo "  **verify components"
1718         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1719         verify_comp_attr lcme_flags $tf 0x20002 init
1720         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1721
1722         # resync
1723         echo "  **resync $tf"
1724         $LFS mirror resync $tf
1725         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1726         [ $flags = ro ] || error "file mirror state $flags != ro"
1727
1728         ################## OST1 ###########################################
1729         write_file_43 $tf 1
1730         echo "  **verify components"
1731         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1732         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1733         verify_comp_attr lcme_flags $tf 0x30003 init
1734
1735         # resync
1736         echo "  **resync $tf"
1737         $LFS mirror resync $tf
1738         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1739         [ $flags = ro ] || error "file mirror state $flags != ro"
1740
1741         ################## OST2 ###########################################
1742         write_file_43 $tf 2
1743         echo "  **verify components"
1744         verify_comp_attr lcme_flags $tf 0x10001 init
1745         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1746         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1747 }
1748 run_test 43 "mirror pick on write"
1749
1750 test_44() {
1751         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1752         rm -rf $DIR/$tdir
1753         rm -rf $DIR/$tdir-1
1754         local tf=$DIR/$tdir/$tfile
1755         local tf1=$DIR/$tdir-1/$tfile-1
1756
1757         $LFS setdirstripe -i 0 -c 1 $DIR/$tdir ||
1758                 error "create directory failed"
1759         $LFS setdirstripe -i 1 -c 1 $DIR/$tdir-1 ||
1760                 error "create remote directory failed"
1761         rm -f $tf $tf1 $tf.mirror~2
1762         # create file with 4 mirrors
1763         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1764                            -N -E 1M -E 2M -E 3M -E -1 -N2 $tf ||
1765                 error "create PFLR file $tf failed"
1766
1767         # file should be in ro status
1768         verify_flr_state $tf "ro"
1769
1770         # write data in [0, 3M)
1771         dd if=/dev/urandom of=$tf bs=1M count=3 conv=notrunc ||
1772                 error "writing $tf failed"
1773
1774         verify_flr_state $tf "wp"
1775
1776         # synchronize all mirrors of the file
1777         $LFS mirror resync $tf || error "mirror resync $tf failed"
1778
1779         verify_flr_state $tf "ro"
1780
1781         # split mirror 1
1782         $LFS mirror split --mirror-id 1 -f $tf1 $tf ||
1783                 error "split to $tf1 failed"
1784
1785         local idx0=$($LFS getstripe -m $tf)
1786         local idx1=$($LFS getstripe -m $tf1)
1787
1788         [[ x$idx0 == x0 ]] || error "$tf is not on MDT0"
1789         [[ x$idx1 == x1 ]] || error "$tf1 is not on MDT1"
1790
1791         # verify mirror count
1792         verify_mirror_count $tf 3
1793         verify_mirror_count $tf1 1
1794
1795         $LFS mirror split --mirror-id 2 $tf ||
1796                 error "split mirror 2 failed"
1797
1798         verify_mirror_count $tf 2
1799         verify_mirror_count $tf.mirror~2 1
1800
1801         $LFS mirror split --mirror-id 3 -d $tf ||
1802                 error "split and delte mirror 3 failed"
1803         verify_mirror_count $tf 1
1804
1805         # verify splitted file contains the same content as the orig file does
1806         diff $tf $tf1 || error "splited file $tf1 diffs from $tf"
1807         diff $tf $tf.mirror~2 ||
1808                 error "splited file $tf.mirror~2 diffs from $tf"
1809 }
1810 run_test 44 "lfs mirror split check"
1811
1812 test_45() {
1813         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
1814
1815         local file=$DIR/$tdir/$tfile
1816         local dir=$DIR/$tdir/$dir
1817         local temp=$DIR/$tdir/template
1818         rm -rf $DIR/$tdir
1819         test_mkdir $DIR/$tdir
1820
1821         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
1822                 -N -E3m -S1m -Eeof -N -E8m -Eeof $file ||
1823                         error "Create $file failed"
1824
1825         verify_yaml_layout $file $file.copy $temp "1. FLR file"
1826         rm -f $file $file.copy
1827
1828         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
1829                 -N -E3m -S1m -Eeof -N -E8m --flags=prefer -Eeof $file ||
1830                         error "Create $file failed"
1831
1832         verify_yaml_layout $file $file.copy $temp "2. FLR file with flags"
1833 }
1834 run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
1835
1836 verify_46() {
1837         local src=$1
1838         local dst=$2
1839         local msg_prefix=$3
1840
1841         $LFS setstripe --copy=$src $dst || error "setstripe $dst failed"
1842
1843         local layout1=$(get_layout_param $src)
1844         local layout2=$(get_layout_param $dst)
1845         # compare their layout info
1846         [ "$layout1" == "$layout2" ] ||
1847                 error "$msg_prefix $src <=> $dst layouts are not equal"
1848 }
1849
1850 test_46() {
1851         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1852
1853         local file=$DIR/$tdir/$tfile
1854         test_mkdir $DIR/$tdir
1855
1856         ########################### 1. PFL file #############################
1857         echo "  ** 1. PFL file"
1858         rm -f $file
1859         $LFS setstripe -E1m -S 1M -c2 -o0,1 -E2m -c2 -E3m -o1,0 -E4m -c1 -E-1 \
1860                 $file || error "1. Create PFL $file failed"
1861
1862         rm -f $file.copy
1863         verify_46 $file $file.copy "1. PFL file"
1864
1865         ########################### 2. plain file ###########################
1866         echo "  ** 2. plain file"
1867         rm -f $file
1868         $LFS setstripe -c2 -o0,1 -i1 $file ||
1869                 error "2. Create plain $file failed"
1870
1871         rm -f $file.copy
1872         verify_46 $file $file.copy "2. plain file"
1873
1874         ########################### 3. FLR file #############################
1875         echo "  ** 3. FLR file"
1876         $LFS setstripe -N -E1m -S 1M -c2 -o0,1 -E4m -c1 -Eeof -N -E16m -Eeof \
1877                 $file || error "3. Create FLR $file failed"
1878
1879         rm -f $file.copy
1880         verify_46 $file $file.copy "3. FLR file"
1881
1882         local dir=$DIR/$tdir/dir
1883         ########################### 4. PFL dir ##############################
1884         echo "  ** 4. PFL dir"
1885         test_mkdir $dir
1886         $LFS setstripe -E1m -S 1M -c2 -E2m -c1 -E-1 $dir ||
1887                 error "4. setstripe PFL $dir failed"
1888
1889         test_mkdir $dir.copy
1890         verify_46 $dir $dir.copy "4. PFL dir"
1891
1892         ########################### 5. plain dir ############################
1893         echo "  ** 5. plain dir"
1894         $LFS setstripe -c2 -i-1 $dir || error "5. setstripe plain $dir failed"
1895
1896         verify_46 $dir $dir.copy "5. plain dir"
1897
1898         ########################### 6. FLR dir ##############################
1899         echo "  ** 6. FLR dir"
1900         $LFS setstripe -N -E1m -S 1M -c2 -E2m -c1 -Eeof -N -E4m -Eeof $dir ||
1901                 error "6. setstripe FLR $dir failed"
1902
1903         verify_46 $dir $dir.copy "6. FLR dir"
1904 }
1905 run_test 46 "Verify setstripe --copy option"
1906
1907 test_47() {
1908         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
1909
1910         local file=$DIR/$tdir/$tfile
1911         local ids
1912         local ost
1913         local osts
1914
1915         test_mkdir $DIR/$tdir
1916
1917         # test case 1:
1918         rm -f $file
1919         # mirror1: [comp0]ost0,    [comp1]ost1 and ost2
1920         # mirror2: [comp2]    ,    [comp3] should not use ost1 or ost2
1921         $LFS mirror create -N -E2m -c1 -o0 --flags=prefer -Eeof -c2 -o1,2 \
1922                 -N -E2m -c1 -Eeof -c1 $file || error "create FLR $file failed"
1923         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
1924
1925         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
1926         $LFS mirror resync $file || error "resync $file failed"
1927
1928         ost=$($LFS getstripe -I${ids[2]} $file | awk '/l_ost_idx/{print $5}')
1929         if [[ x$ost == "x0," ]]; then
1930                 $LFS getstripe $file
1931                 error "component ${ids[2]} objects allocated on $ost " \
1932                       "shouldn't on OST0"
1933         fi
1934
1935         ost=$($LFS getstripe -I${ids[3]} $file | awk '/l_ost_idx/{print $5}')
1936         if [[ x$ost == "x1," || x$ost == "x2," ]]; then
1937                 $LFS getstripe $file
1938                 error "component ${ids[3]} objects allocated on $ost " \
1939                       "shouldn't on OST1 or on OST2"
1940         fi
1941
1942         ## test case 2:
1943         rm -f $file
1944         # mirror1: [comp0]    [comp1]
1945         # mirror2: [comp2]    [comp3]
1946         # mirror3: [comp4]    [comp5]
1947         # mirror4: [comp6]    [comp7]
1948         $LFS mirror create -N4 -E1m -c1 -Eeof -c1 $file ||
1949                 error "create FLR $file failed"
1950         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
1951
1952         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
1953         $LFS mirror resync $file || error "resync $file failed"
1954
1955         for ((i = 0; i < 6; i++)); do
1956                 osts[$i]=$($LFS getstripe -I${ids[$i]} $file |
1957                         awk '/l_ost_idx/{print $5}')
1958         done
1959         # comp[0],comp[2],comp[4] should use different osts
1960         if [[ ${osts[0]} == ${osts[2]} || ${osts[0]} == ${osts[4]} ||
1961               ${osts[2]} == ${osts[4]} ]]; then
1962                 $LFS getstripe $file
1963                 error "component ${ids[0]}, ${ids[2]}, ${ids[4]} have objects "\
1964                       "allocated on duplicated OSTs"
1965         fi
1966         # comp[1],comp[3],comp[5] should use different osts
1967         if [[ ${osts[1]} == ${osts[3]} || ${osts[1]} == ${osts[5]} ||
1968               ${osts[3]} == ${osts[5]} ]]; then
1969                 $LFS getstripe $file
1970                 error "component ${ids[1]}, ${ids[3]}, ${ids[5]} have objects "\
1971                       "allocated on duplicated OSTs"
1972         fi
1973
1974         return 0
1975 }
1976 run_test 47 "Verify mirror obj alloc"
1977
1978 test_48() {
1979         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
1980                 skip "Need MDS version at least 2.11.55"
1981
1982         local tf=$DIR/$tfile
1983
1984         rm -f $tf
1985         echo " ** create 2 mirrors FLR file $tf"
1986         $LFS mirror create -N -E2M -Eeof --flags prefer \
1987                            -N -E1M -Eeof $tf ||
1988                 error "create FLR file $tf failed"
1989
1990         echo " ** write it"
1991         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
1992         verify_flr_state $tf "wp"
1993
1994         local sum0=$(md5sum < $tf)
1995
1996         echo " ** resync the file"
1997         $LFS mirror resync $tf
1998
1999         echo " ** snapshot mirror 2"
2000         $LFS setstripe --comp-set -I 0x20003 --comp-flags=nosync $tf
2001
2002         echo " ** write it again"
2003         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2004         echo " ** resync it again"
2005         $LFS mirror resync $tf
2006
2007         verify_flr_state $tf "wp"
2008         verify_comp_attr lcme_flags $tf 0x20003 nosync,stale
2009
2010         local sum1=$($LFS mirror read -N1 $tf | md5sum)
2011         local sum2=$($LFS mirror read -N2 $tf | md5sum)
2012
2013         echo " ** verify mirror 2 doesn't change"
2014         echo "original checksum: $sum0"
2015         echo "mirror 1 checksum: $sum1"
2016         echo "mirror 2 checksum: $sum2"
2017         [[ $sum0 = $sum2 ]] ||
2018                 error "original checksum: $sum0, mirror 2 checksum: $sum2"
2019         echo " ** mirror 2 stripe info"
2020         $LFS getstripe -v --mirror-index=2 $tf
2021
2022         echo " ** resync mirror 2"
2023         $LFS mirror resync --only 2 $tf
2024
2025         verify_flr_state $tf "ro"
2026         verify_comp_attr lcme_flags $tf 0x20003 nosync,^stale
2027
2028         sum1=$($LFS mirror read -N1 $tf | md5sum)
2029         sum2=$($LFS mirror read -N2 $tf | md5sum)
2030
2031         echo " ** verify mirror 2 resync-ed"
2032         echo "original checksum: $sum0"
2033         echo "mirror 1 checksum: $sum1"
2034         echo "mirror 2 checksum: $sum2"
2035         [[ $sum1 = $sum2 ]] ||
2036                 error "mirror 1 checksum: $sum1, mirror 2 checksum: $sum2"
2037         echo " ** mirror 2 stripe info"
2038         $LFS getstripe -v --mirror-index=2 $tf
2039 }
2040 run_test 48 "Verify snapshot mirror"
2041
2042 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
2043 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
2044
2045 write_file_200() {
2046         local tf=$1
2047
2048         local fsize=$(stat --printf=%s $tf)
2049
2050         while [ -f $ctrl_file ]; do
2051                 local off=$((RANDOM << 8))
2052                 local len=$((RANDOM << 5 + 131072))
2053
2054                 [ $((off + len)) -gt $fsize ] && {
2055                         fsize=$((off + len))
2056                         echo "Extending file size to $fsize .."
2057                 }
2058
2059                 flock -s $lock_file -c \
2060                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
2061                                 { rm -f $ctrl_file;
2062                                   error "failed writing to $off:$len"; }
2063                 sleep 0.$((RANDOM % 2 + 1))
2064         done
2065 }
2066
2067 read_file_200() {
2068         local tf=$1
2069
2070         while [ -f $ctrl_file ]; do
2071                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
2072                         { rm -f $ctrl_file; error "read failed"; }
2073                 sleep 0.$((RANDOM % 2 + 1))
2074         done
2075 }
2076
2077 resync_file_200() {
2078         local tf=$1
2079
2080         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
2081
2082         exec 200<>$lock_file
2083         while [ -f $ctrl_file ]; do
2084                 local lock_taken=false
2085                 local index=$((RANDOM % ${#options[@]}))
2086                 local cmd="mirror_io resync ${options[$index]}"
2087
2088                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
2089
2090                 [ $((RANDOM % 4)) -eq 0 ] && {
2091                         index=0
2092                         lock_taken=true
2093                         echo -n "lock to "
2094                 }
2095
2096                 echo -n "resync file $tf with '$cmd' .."
2097
2098                 if [[ $lock_taken = "true" ]]; then
2099                         flock -x 200 -c "$cmd $tf &> /dev/null" &&
2100                                 echo "done" || echo "failed"
2101                         flock -u 200
2102                 else
2103                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
2104                 fi
2105
2106                 sleep 0.$((RANDOM % 8 + 1))
2107         done
2108 }
2109
2110 test_200() {
2111         local tf=$DIR/$tfile
2112         local tf2=$DIR2/$tfile
2113         local tf3=$DIR3/$tfile
2114
2115         $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
2116         $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
2117         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
2118
2119         $LFS mirror extend -N -f $tf-2 $tf ||
2120                 error "merging $tf-2 into $tf failed"
2121         $LFS mirror extend -N -f $tf-3 $tf ||
2122                 error "merging $tf-3 into $tf failed"
2123
2124         mkdir -p $MOUNT2 && mount_client $MOUNT2
2125
2126         mkdir -p $MOUNT3 && mount_client $MOUNT3
2127
2128         verify_flr_state $tf3 "ro"
2129
2130         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
2131         $LCTL set_param fail_loc=0x1A03
2132
2133         local mds_idx=mds$(($($LFS getstripe -m $tf) + 1))
2134         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
2135
2136         declare -a pids
2137
2138         write_file_200 $tf &
2139         pids+=($!)
2140
2141         read_file_200 $tf &
2142         pids+=($!)
2143
2144         write_file_200 $tf2 &
2145         pids+=($!)
2146
2147         read_file_200 $tf2 &
2148         pids+=($!)
2149
2150         resync_file_200 $tf3 &
2151         pids+=($!)
2152
2153         local sleep_time=60
2154         [ "$SLOW" = "yes" ] && sleep_time=360
2155         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
2156                 sleep 1
2157                 ((--sleep_time))
2158         done
2159
2160         rm -f $ctrl_file
2161
2162         echo "Waiting ${pids[@]}"
2163         wait ${pids[@]}
2164
2165         umount_client $MOUNT2
2166         umount_client $MOUNT3
2167
2168         rm -f $lock_file
2169
2170         # resync and verify mirrors
2171         mirror_io resync $tf
2172         get_mirror_ids $tf
2173
2174         local csum=$($LFS mirror read -N ${mirror_array[0]} $tf | md5sum)
2175         for id in ${mirror_array[@]:1}; do
2176                 [ "$($LFS mirror read -N $id $tf | md5sum)" = "$csum" ] ||
2177                         error "checksum error for mirror $id"
2178         done
2179
2180         true
2181 }
2182 run_test 200 "stress test"
2183
2184 cleanup_test_201() {
2185         trap 0
2186         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
2187
2188         umount_client $MOUNT2
2189 }
2190
2191 test_201() {
2192         local delay=${RESYNC_DELAY:-5}
2193
2194         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
2195                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
2196
2197         trap cleanup_test_201 EXIT
2198
2199         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
2200                         changelog_register -n)
2201
2202         mkdir -p $MOUNT2 && mount_client $MOUNT2
2203
2204         local index=0
2205         while :; do
2206                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
2207                 [ -z "$log" ] && { sleep 1; continue; }
2208
2209                 index=$(echo $log | awk '{print $1}')
2210                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
2211                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
2212                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
2213
2214                 ((++index))
2215                 [ -z "$file" ] && continue
2216
2217                 local now=$(date +%s)
2218
2219                 echo "file: $file $fid was modified at $ts, now: $now, " \
2220                      "will be resynced at $((ts+delay))"
2221
2222                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
2223
2224                 mirror_io resync $file
2225                 echo "$file resync done"
2226         done
2227
2228         cleanup_test_201
2229 }
2230 run_test 201 "FLR data mover"
2231
2232 test_202() {
2233         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
2234
2235         local tf=$DIR/$tfile
2236         local ids
2237
2238         $LFS setstripe -E 1M -S 1M -c 1 $tf
2239         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2240         verify_comp_attr stripe-count $tf ${ids[0]} 1
2241
2242         $LFS setstripe --component-add -E 2M -c -1 $tf
2243         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2244         verify_comp_attr stripe-count $tf ${ids[0]} 1
2245         verify_comp_attr stripe-count $tf ${ids[1]} -1
2246
2247         dd if=/dev/zero of=$tf bs=1M count=2
2248         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2249         verify_comp_attr stripe-count $tf ${ids[0]} 1
2250         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
2251 }
2252 run_test 202 "lfs setstripe --add-component wide striping"
2253
2254 test_203() {
2255         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
2256                 skip "Need MDS version at least 2.11.55"
2257         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
2258
2259         local tf=$DIR/$tfile
2260
2261         #create 2 mirrors
2262         $LFS mirror create -N2 -c1 $tf || error "create FLR file $tf"
2263         #delete first mirror
2264         $LFS mirror split --mirror-id=1 -d $tf || error "delete first mirror"
2265
2266         $LFS getstripe $tf
2267         local old_id=$($LFS getstripe --mirror-id=2 -I $tf)
2268         local count=$($LFS getstripe --mirror-id=2 -c $tf) ||
2269                 error "getstripe count of mirror 2"
2270         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
2271
2272         #extend a mirror with 2 OSTs
2273         $LFS mirror extend -N -c2 $tf || error "extend mirror"
2274         $LFS getstripe $tf
2275
2276         local new_id=$($LFS getstripe --mirror-id=2 -I $tf)
2277         count=$($LFS getstripe --mirror-id=2 -c $tf) ||
2278                 error "getstripe count of mirror 2"
2279         [[ x$oldid = x$newid ]] ||
2280                 error "mirror 2 changed ID from $old_id to $new_id"
2281         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
2282
2283         count=$($LFS getstripe --mirror-id=3 -c $tf) ||
2284                 error "getstripe count of mirror 3"
2285         [[ x$count = x2 ]] || error "mirror 3 stripe count $count is not 2"
2286 }
2287 run_test 203 "mirror file preserve mirror ID"
2288
2289 # Simple test of FLR + self-extending layout, SEL in non-primary mirror
2290 test_204a() {
2291         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code $SEL_VER) ] &&
2292                 skip "skipped for lustre < $SEL_VER"
2293
2294         local comp_file=$DIR/$tdir/$tfile
2295         local flg_opts=""
2296         local found=""
2297
2298         test_mkdir $DIR/$tdir
2299
2300         # first mirror is 0-10M, then 10M-(-1), second mirror is 1M followed
2301         # by extension space to -1
2302         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E-1 -z64M $comp_file ||
2303                 error "Create $comp_file failed"
2304
2305         # Write to first component, extending & staling second mirror
2306         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
2307                 error "dd to extend + stale failed"
2308
2309         $LFS getstripe $comp_file
2310
2311         flg_opts="--component-flags init,stale"
2312         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
2313         [ $found -eq 1 ] || error "write: Second comp end incorrect"
2314
2315         flg_opts="--component-flags extension"
2316         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
2317         [ $found -eq 1 ] || error "write: Third comp start incorrect"
2318
2319         # mirror resync should not change the extents
2320         $LFS mirror resync $comp_file
2321
2322         flg_opts="--component-flags init"
2323         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
2324         [ $found -eq 1 ] || error "resync: Second comp end incorrect"
2325
2326         flg_opts="--component-flags extension"
2327         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
2328         [ $found -eq 1 ] || error "resync: Third comp start incorrect"
2329
2330         sel_layout_sanity $comp_file 5
2331
2332         rm -f $comp_file
2333 }
2334 run_test 204a "FLR write/stale/resync tests with self-extending mirror"
2335
2336 # Simple test of FLR + self-extending layout, SEL in primary mirror
2337 test_204b() {
2338         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code $SEL_VER) ] &&
2339                 skip "skipped for lustre < $SEL_VER"
2340
2341         local comp_file=$DIR/$tdir/$tfile
2342         local flg_opts=""
2343         local found=""
2344
2345         test_mkdir $DIR/$tdir
2346
2347         # first mirror is 1M followed by extension space to -1, second mirror
2348         # is 0-10M, then 10M-(-1),
2349         $LFS setstripe -N -E 1M -E-1 -z64M -N -E 10M -E-1 $comp_file ||
2350                 error "Create $comp_file failed"
2351
2352         # Write to first component, extending first component & staling
2353         # other mirror
2354         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
2355                 error "dd to extend + stale failed"
2356
2357         $LFS getstripe $comp_file
2358
2359         flg_opts="--component-flags init"
2360         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
2361         [ $found -eq 1 ] || error "write: First comp end incorrect"
2362
2363         flg_opts="--component-flags extension"
2364         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
2365         [ $found -eq 1 ] || error "write: Second comp start incorrect"
2366
2367         flg_opts="--component-flags init,stale"
2368         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
2369         [ $found -eq 1 ] || error "write: First mirror comp flags incorrect"
2370
2371         # This component is staled because it overlaps the extended first
2372         # component of the primary mirror, even though it doesn't overlap
2373         # the actual write - thus not inited.
2374         flg_opts="--component-flags stale"
2375         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
2376         [ $found -eq 1 ] || error "write: Second mirror comp flags incorrect"
2377
2378         # mirror resync should not change the extents
2379         $LFS mirror resync $comp_file
2380
2381         $LFS getstripe $comp_file
2382
2383         flg_opts="--component-flags init"
2384         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
2385         [ $found -eq 1 ] || error "resync: First comp end incorrect"
2386
2387         flg_opts="--component-flags extension"
2388         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
2389         [ $found -eq 1 ] || error "resync: Second comp start incorrect"
2390
2391         flg_opts="--component-flags init"
2392         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
2393         [ $found -eq 1 ] || error "resync: First mirror comp flags incorrect"
2394
2395         flg_opts="--component-flags init"
2396         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
2397         [ $found -eq 1 ] || error "resync: Second mirror comp flags incorrect"
2398
2399         sel_layout_sanity $comp_file 5
2400
2401         rm -f $comp_file
2402 }
2403 run_test 204b "FLR write/stale/resync tests with self-extending primary"
2404
2405 # FLR + SEL failed extension & component removal
2406 # extension space in second mirror
2407 test_204c() {
2408         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2409         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code $SEL_VER) ] &&
2410                 skip "skipped for lustre < $SEL_VER"
2411
2412         local comp_file=$DIR/$tdir/$tfile
2413         local found=""
2414         local ost_idx1=0
2415         local ost_name=$(ostname_from_index $ost_idx1)
2416
2417         test_mkdir $DIR/$tdir
2418
2419         # first mirror is is 0-10M, then 10M-(-1), second mirror is 0-1M, then
2420         # extension space from 1M to 1G, then normal space to -1
2421         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E 1G -i $ost_idx1 -z 64M \
2422                 -E -1 $comp_file || error "Create $comp_file failed"
2423
2424         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
2425         sleep_maxage
2426
2427         # write to first comp (0 - 10M) of mirror 1, extending + staling
2428         # first + second comp of mirror 2
2429         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
2430         RC=$?
2431
2432         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
2433         sleep_maxage
2434
2435         [ $RC -eq 0 ] || error "dd to extend + stale failed"
2436
2437         $LFS getstripe $comp_file
2438
2439         found=$($LFS find --component-start 0m --component-end 1m \
2440                 --comp-flags init,stale $comp_file | wc -l)
2441         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
2442
2443         found=$($LFS find --component-start 1m --component-end EOF \
2444                 --comp-flags stale,^init $comp_file | wc -l)
2445         [ $found -eq 1 ] || error "write: Second mirror comp incorrect"
2446
2447         local mirror_id=$($LFS getstripe --component-start=1m   \
2448                          --component-end=EOF $comp_file |       \
2449                          grep lcme_mirror_id | awk '{ print $2 }')
2450
2451         [[ $mirror_id -eq 2 ]] ||
2452                 error "component not in correct mirror? $mirror_id"
2453
2454         $LFS mirror resync $comp_file
2455
2456         $LFS getstripe $comp_file
2457
2458         # component dimensions should not change from resync
2459         found=$($LFS find --component-start 1m --component-end EOF \
2460                 --component-flags init $comp_file | wc -l)
2461         [ $found -eq 1 ] || error "resync: Second mirror comp incorrect"
2462
2463         sel_layout_sanity $comp_file 4
2464
2465         rm -f $comp_file
2466 }
2467 run_test 204c "FLR write/stale/resync test with component removal"
2468
2469 # Successful repeated component in primary mirror
2470 test_204d() {
2471         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2472         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code $SEL_VER) ] &&
2473                 skip "skipped for lustre < $SEL_VERSION"
2474
2475         local comp_file=$DIR/$tdir/$tfile
2476         local found=""
2477
2478         wait_delete_completed
2479         wait_mds_ost_sync
2480         test_mkdir $DIR/$tdir
2481
2482         # first mirror is 64M followed by extension space to -1, second mirror
2483         # is 0-10M, then 10M-(-1)
2484         $LFS setstripe -N -E-1 -z64M -N -E 10M -E-1 $comp_file ||
2485                 error "Create $comp_file failed"
2486
2487         local ost_idx1=$($LFS getstripe -I65537 -i $comp_file)
2488         local ost_name=$(ostname_from_index $ost_idx1)
2489         # degrade OST for first comp so we won't extend there
2490         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
2491                 obdfilter.$ost_name.degraded=1
2492         sleep_maxage
2493
2494         # Write beyond first component, causing repeat & stale second mirror
2495         dd if=/dev/zero bs=1M count=1 seek=66 of=$comp_file conv=notrunc
2496         RC=$?
2497
2498         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
2499                 obdfilter.$ost_name.degraded=0
2500         sleep_maxage
2501
2502         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
2503
2504         $LFS getstripe $comp_file
2505
2506         found=$($LFS find --component-start 64m --component-end 128m \
2507                 --component-flags init $comp_file | wc -l)
2508         [ $found -eq 1 ] || error "write: Repeat comp incorrect"
2509
2510         local ost_idx2=$($LFS getstripe --component-start=64m           \
2511                          --component-end=128m --component-flags=init    \
2512                          -i $comp_file)
2513         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
2514         local mirror_id=$($LFS getstripe --component-start=64m          \
2515                          --component-end=128m --component-flags=init    \
2516                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
2517         [[ $mirror_id -eq 1 ]] ||
2518                 error "component not in correct mirror: $mirror_id, not 1"
2519
2520         $LFS mirror resync $comp_file
2521
2522         $LFS getstripe $comp_file
2523
2524         # component dimensions should not change from resync
2525         found=$($LFS find --component-start 0m --component-end 64m \
2526                 --component-flags init $comp_file | wc -l)
2527         [ $found -eq 1 ] || error "resync: first comp incorrect"
2528         found=$($LFS find --component-start 64m --component-end 128m \
2529                 --component-flags init $comp_file | wc -l)
2530         [ $found -eq 1 ] || error "resync: repeat comp incorrect"
2531
2532         sel_layout_sanity $comp_file 5
2533
2534         rm -f $comp_file
2535 }
2536 run_test 204d "FLR write/stale/resync sel test with repeated comp"
2537
2538 # Successful repeated component, SEL in non-primary mirror
2539 test_204e() {
2540         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2541         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code $SEL_VER) ] &&
2542                 skip "skipped for lustre < $SEL_VERSION"
2543
2544         local comp_file=$DIR/$tdir/$tfile
2545         local found=""
2546
2547         wait_delete_completed
2548         wait_mds_ost_sync
2549
2550         test_mkdir $DIR/$tdir
2551
2552         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
2553         # space to -1 (-z 64M, so first comp is 0-64M)
2554         # Note: we have to place both 1st components on OST0, otherwise 2 OSTs
2555         # will be not enough - one will be degraded, the other is used on
2556         # an overlapping mirror.
2557         $LFS setstripe -N -E 100M -i 0 -E-1 -N -E-1 -i 0 -z 64M $comp_file ||
2558                 error "Create $comp_file failed"
2559
2560         local ost_idx1=$($LFS getstripe --component-start=0 \
2561                          --component-end=64m -i $comp_file)
2562         local ost_name=$(ostname_from_index $ost_idx1)
2563         # degrade OST for first comp of 2nd mirror so we won't extend there
2564         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
2565                 obdfilter.$ost_name.degraded=1
2566         sleep_maxage
2567
2568         $LFS getstripe $comp_file
2569
2570         # Write to first component, stale & instantiate second mirror components
2571         # overlapping with the written component (0-100M);
2572         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
2573         RC=$?
2574
2575         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
2576                 obdfilter.$ost_name.degraded=0
2577         sleep_maxage
2578         $LFS getstripe $comp_file
2579
2580         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
2581
2582         found=$($LFS find --component-start 0m --component-end 64m \
2583                 --component-flags init,stale $comp_file | wc -l)
2584         [ $found -eq 1 ] || error "write: first comp incorrect"
2585
2586         # was repeated due to degraded ost
2587         found=$($LFS find --component-start 64m --component-end 128m \
2588                 --component-flags init,stale $comp_file | wc -l)
2589         [ $found -eq 1 ] || error "write: repeated comp incorrect"
2590
2591         local ost_idx2=$($LFS getstripe --component-start=64m           \
2592                          --component-end=128m --component-flags=init    \
2593                          -i $comp_file)
2594         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
2595         local mirror_id=$($LFS getstripe --component-start=0m           \
2596                          --component-end=64m --component-flags=init     \
2597                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
2598         [[ $mirror_id -eq 2 ]] ||
2599                 error "component not in correct mirror? $mirror_id"
2600
2601         $LFS mirror resync $comp_file
2602
2603         $LFS getstripe $comp_file
2604
2605         # component dimensions should not change from resync
2606         found=$($LFS find --component-start 0m --component-end 64m \
2607                 --component-flags init,^stale $comp_file | wc -l)
2608         [ $found -eq 1 ] || error "resync: first comp incorrect"
2609         found=$($LFS find --component-start 64m --component-end 128m \
2610                 --component-flags init,^stale $comp_file | wc -l)
2611         [ $found -eq 1 ] || error "resync: repeated comp incorrect"
2612
2613         sel_layout_sanity $comp_file 5
2614
2615         rm -f $comp_file
2616 }
2617 run_test 204e "FLR write/stale/resync sel test with repeated comp"
2618
2619 # FLR + SEL: failed repeated component, SEL in non-primary mirror
2620 test_204f() {
2621         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2622         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code $SEL_VER) ] &&
2623                 skip "skipped for lustre < $SEL_VERSION"
2624
2625         local comp_file=$DIR/$tdir/$tfile
2626         local found=""
2627
2628         wait_delete_completed
2629         wait_mds_ost_sync
2630         test_mkdir $DIR/$tdir
2631
2632         pool_add $TESTNAME || error "Pool creation failed"
2633         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2634
2635         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
2636         # space to -1 (-z 64M, so first comp is 0-64M)
2637         $LFS setstripe -N -E 100M -E-1 -N --pool="$TESTNAME" \
2638                 -E-1 -c 1 -z 64M $comp_file || error "Create $comp_file failed"
2639
2640         local ost_name0=$(ostname_from_index 0)
2641         local ost_name1=$(ostname_from_index 1)
2642
2643         # degrade both OSTs in pool, so we'll try to repeat, then fail and
2644         # extend original comp
2645         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=1
2646         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=1
2647         sleep_maxage
2648
2649         # a write to the 1st component, 100M length, which will try to stale
2650         # the first 100M of mirror 2, attempting to extend its 0-64M component
2651         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
2652         RC=$?
2653
2654         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=0
2655         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=0
2656         sleep_maxage
2657
2658         [ $RC -eq 0 ] || error "dd to extend mirror comp failed"
2659
2660         $LFS getstripe $comp_file
2661
2662         found=$($LFS find --component-start 0m --component-end 128m \
2663                 --component-flags init,stale $comp_file | wc -l)
2664         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
2665
2666         local mirror_id=$($LFS getstripe --component-start=0m           \
2667                          --component-end=128m --component-flags=init    \
2668                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
2669
2670         [[ $mirror_id -eq 2 ]] ||
2671                 error "component not in correct mirror? $mirror_id, not 2"
2672
2673         $LFS mirror resync $comp_file
2674
2675         $LFS getstripe $comp_file
2676
2677         # component dimensions should not change from resync
2678         found=$($LFS find --component-start 0m --component-end 128m \
2679                 --component-flags init,^stale $comp_file | wc -l)
2680         [ $found -eq 1 ] || error "resync: First mirror comp incorrect"
2681
2682         sel_layout_sanity $comp_file 4
2683
2684         rm -f $comp_file
2685 }
2686 run_test 204f "FLR write/stale/resync sel w/forced extension"
2687
2688 complete $SECONDS
2689 check_and_cleanup_lustre
2690 exit_status