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