Whamcloud - gitweb
LU-9324 lfs: add setstripe --yaml=template parameter
[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 SRCDIR=$(dirname $0)
9 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
10
11 ONLY=${ONLY:-"$*"}
12 # Bug number for skipped test:
13 ALWAYS_EXCEPT="$SANITY_FLR_EXCEPT 201"
14 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
15
16 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
17 . $LUSTRE/tests/test-framework.sh
18 init_test_env $@
19 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
20 init_logging
21
22 [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
23         { skip "Need MDS version at least 2.10.56"; exit 0; }
24
25 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
26         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
27 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
28
29 check_and_setup_lustre
30 DIR=${DIR:-$MOUNT}
31 assert_DIR
32
33 build_test_filter
34
35 # global array to store mirror IDs
36 declare -a mirror_array
37 get_mirror_ids() {
38         local tf=$1
39         local id
40         local array
41
42         array=()
43         for id in $($LFS getstripe $tf | awk '/lcme_id/{print $2}'); do
44                 array[${#array[@]}]=$((id >> 16))
45         done
46
47         mirror_array=($(printf "%s\n" "${array[@]}" | sort -u))
48
49         echo ${#mirror_array[@]}
50 }
51
52 drop_client_cache() {
53         echo 3 > /proc/sys/vm/drop_caches
54 }
55
56 stop_osts() {
57         local idx
58
59         for idx in "$@"; do
60                 stop ost$idx
61         done
62
63         for idx in "$@"; do
64                 wait_osc_import_state client ost$idx DISCONN
65         done
66 }
67
68 start_osts() {
69         local idx
70
71         for idx in "$@"; do
72                 start ost$idx $(ostdevname $idx) $OST_MOUNT_OPTS ||
73                         error "start ost$idx failed"
74         done
75
76         for idx in "$@"; do
77                 wait_osc_import_state client ost$idx FULL
78         done
79 }
80
81 #
82 # Verify mirror count with an expected value for a given file.
83 #
84 verify_mirror_count() {
85         local tf=$1
86         local expected=$2
87         local mirror_count=$(get_mirror_ids $tf)
88
89         [[ $mirror_count = $expected ]] || {
90                 $LFS getstripe -v $tf
91                 error "verify mirror count failed on $tf:" \
92                       "$mirror_count != $expected"
93         }
94 }
95
96 #
97 # Verify component count with an expected value for a given file.
98 #       $1 coposited layout file
99 #       $2 expected component number
100 #
101 verify_comp_count() {
102         local tf=$1
103         local expected=$2
104         local comp_count=$($LFS getstripe --component-count $tf)
105
106         [[ $comp_count = $expected ]] || {
107                 $LFS getstripe -v $tf
108                 error "verify component count failed on $tf:" \
109                       "$comp_count != $expected"
110         }
111 }
112
113 #
114 # Verify component attribute with an expected value for a given file
115 # and component ID.
116 #
117 verify_comp_attr() {
118         local attr=$1
119         local tf=$2
120         local comp_id=$3
121         local expected=$4
122         local cmd="$LFS getstripe -I$comp_id"
123         local getstripe_cmd="$cmd -v"
124         local value
125
126         case $attr in
127                 stripe-size) cmd+=" -S $tf" ;;
128                 stripe-count) cmd+=" -c $tf" ;;
129                 stripe-index) cmd+=" -i $tf" ;;
130                 pool) cmd+=" -p $tf" ;;
131                 comp-start) cmd+=" --component-start $tf" ;;
132                 comp-end) cmd+=" --component-end $tf" ;;
133                 lcme_flags) cmd+=" $tf | awk '/lcme_flags:/ { print \$2 }'" ;;
134                 *) error "invalid attribute $attr";;
135         esac
136
137         value=$(eval $cmd)
138
139         [ $attr = lcme_flags ] && {
140                 local fl
141                 local expected_list=$(comma_list $expected)
142                 for fl in ${expected_list//,/ }; do
143                         echo $value | grep -q $fl || {
144                                 $getstripe_cmd $tf
145                                 error "expected flag $fl existing on $comp_id"
146                         }
147                 done
148                 return
149         }
150
151         [[ $value = $expected ]] || {
152                 $getstripe_cmd $tf
153                 error "verify $attr failed on $tf: $value != $expected"
154         }
155 }
156
157 #
158 # Verify component extent with expected start and end extent values
159 # for a given file and component ID.
160 #
161 verify_comp_extent() {
162         local tf=$1
163         local comp_id=$2
164         local expected_start=$3
165         local expected_end=$4
166
167         verify_comp_attr comp-start $tf $comp_id $expected_start
168         verify_comp_attr comp-end $tf $comp_id $expected_end
169 }
170
171 #
172 # Verify component attribute with parent directory for a given file
173 # and component ID.
174 #
175 verify_comp_attr_with_parent() {
176         local attr=$1
177         local tf=$2
178         local comp_id=$3
179         local td=$(cd $(dirname $tf); echo $PWD)
180         local tf_cmd="$LFS getstripe -I$comp_id"
181         local td_cmd="$LFS getstripe"
182         local opt
183         local expected
184         local value
185
186         case $attr in
187                 stripe-size) opt="-S" ;;
188                 stripe-count) opt="-c" ;;
189                 pool) opt="-p" ;;
190                 *) error "invalid attribute $attr";;
191         esac
192
193         expected=$($td_cmd $opt $td)
194         [[ $expected = -1 ]] && expected=$OSTCOUNT
195
196         value=$($tf_cmd $opt $tf)
197         [[ $value = -1 ]] && value=$OSTCOUNT
198
199         [[ $value = $expected ]] || {
200                 $td_cmd -d $td
201                 $tf_cmd -v $tf
202                 error "verify $attr failed with parent on $tf:" \
203                       "$value != $expected"
204         }
205 }
206
207 #
208 # Verify component attribute with filesystem-wide default value for a given file
209 # and component ID.
210 #
211 verify_comp_attr_with_default() {
212         local attr=$1
213         local tf=$2
214         local comp_id=$3
215         local tf_cmd="$LFS getstripe -I$comp_id"
216         local opt
217         local expected
218         local value
219
220         case $attr in
221                 stripe-size)
222                         opt="-S"
223                         expected=$($LCTL get_param -n \
224                                    lov.$FSNAME-clilov-*.stripesize)
225                         ;;
226                 stripe-count)
227                         opt="-c"
228                         expected=$($LCTL get_param -n \
229                                    lov.$FSNAME-clilov-*.stripecount)
230                         [[ $expected = -1 ]] && expected=$OSTCOUNT
231                         ;;
232                 *) error "invalid attribute $attr";;
233         esac
234
235         value=$($tf_cmd $opt $tf)
236         [[ $value = -1 ]] && value=$OSTCOUNT
237
238         [[ $value = $expected ]] || {
239                 $tf_cmd -v $tf
240                 error "verify $attr failed with default value on $tf:" \
241                       "$value != $expected"
242         }
243 }
244
245 #
246 # Verify unspecified component attributes for a given file
247 # and component ID.
248 #
249 # This will only verify the inherited attributes:
250 # stripe size, stripe count and OST pool name
251 #
252 verify_comp_attrs() {
253         local tf=$1
254         local comp_id=$2
255
256         verify_comp_attr_with_default stripe-size $tf $comp_id
257         verify_comp_attr_with_default stripe-count $tf $comp_id
258         verify_comp_attr_with_parent pool $tf $comp_id
259 }
260
261 # command line test cases
262 test_0a() {
263         local td=$DIR/$tdir
264         local tf=$td/$tfile
265         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
266         local mirror_cmd="$LFS mirror create"
267         local id
268         local ids
269         local i
270
271         # create parent directory
272         mkdir $td || error "mkdir $td failed"
273
274         $mirror_cmd $tf &> /dev/null && error "miss -N option"
275
276         $mirror_cmd -N $tf || error "create mirrored file $tf failed"
277         verify_mirror_count $tf 1
278         id=$($LFS getstripe -I $tf)
279         verify_comp_attrs $tf $id
280         verify_comp_extent $tf $id 0 EOF
281
282         $mirror_cmd -N0 $tf-1 &> /dev/null && error "invalid mirror count 0"
283         $mirror_cmd -N$((mirror_count + 1)) $tf-1 &> /dev/null &&
284                 error "invalid mirror count $((mirror_count + 1))"
285
286         $mirror_cmd -N$mirror_count $tf-1 ||
287                 error "create mirrored file $tf-1 failed"
288         verify_mirror_count $tf-1 $mirror_count
289         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
290         for ((i = 0; i < $mirror_count; i++)); do
291                 verify_comp_attrs $tf-1 ${ids[$i]}
292                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
293         done
294
295         $mirror_cmd -N -N2 -N3 -N4 $tf-2 ||
296                 error "create mirrored file $tf-2 failed"
297         verify_mirror_count $tf-2 10
298         ids=($($LFS getstripe $tf-2 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
299         for ((i = 0; i < 10; i++)); do
300                 verify_comp_attrs $tf-2 ${ids[$i]}
301                 verify_comp_extent $tf-2 ${ids[$i]} 0 EOF
302         done
303 }
304 run_test 0a "lfs mirror create with -N option"
305
306 test_0b() {
307         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
308
309         local td=$DIR/$tdir
310         local tf=$td/$tfile
311         local mirror_cmd="$LFS mirror create"
312         local ids
313         local i
314
315         # create a new OST pool
316         local pool_name=$TESTNAME
317         create_pool $FSNAME.$pool_name ||
318                 error "create OST pool $pool_name failed"
319
320         # add OSTs into the pool
321         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
322                 error "add OSTs into pool $pool_name failed"
323
324         # create parent directory
325         mkdir $td || error "mkdir $td failed"
326         $LFS setstripe -S 8M -c -1 -p $pool_name $td ||
327                 error "$LFS setstripe $td failed"
328
329         # create a mirrored file with plain layout mirrors
330         $mirror_cmd -N -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
331                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
332                 error "create mirrored file $tf failed"
333         verify_mirror_count $tf 6
334         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
335         for ((i = 0; i < 6; i++)); do
336                 verify_comp_extent $tf ${ids[$i]} 0 EOF
337         done
338
339         # verify component ${ids[0]}
340         verify_comp_attrs $tf ${ids[0]}
341
342         # verify component ${ids[1]}
343         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
344         verify_comp_attr stripe-count $tf ${ids[1]} 2
345         verify_comp_attr stripe-index $tf ${ids[1]} 2
346         verify_comp_attr pool $tf ${ids[1]} flash
347
348         # verify component ${ids[2]}
349         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
350         verify_comp_attr stripe-count $tf ${ids[2]} 2
351         verify_comp_attr pool $tf ${ids[2]} flash
352
353         # verify component ${ids[3]}
354         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
355         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
356         verify_comp_attr pool $tf ${ids[3]} flash
357
358         # verify component ${ids[4]}
359         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
360         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
361         verify_comp_attr pool $tf ${ids[4]} archive
362
363         # verify component ${ids[5]}
364         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
365         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
366         verify_comp_attr_with_parent pool $tf ${ids[5]}
367
368         # destroy OST pool
369         destroy_test_pools
370 }
371 run_test 0b "lfs mirror create plain layout mirrors"
372
373 test_0c() {
374         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
375
376         local td=$DIR/$tdir
377         local tf=$td/$tfile
378         local mirror_cmd="$LFS mirror create"
379         local ids
380         local i
381
382         # create a new OST pool
383         local pool_name=$TESTNAME
384         create_pool $FSNAME.$pool_name ||
385                 error "create OST pool $pool_name failed"
386
387         # add OSTs into the pool
388         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
389                 error "add OSTs into pool $pool_name failed"
390
391         # create parent directory
392         mkdir $td || error "mkdir $td failed"
393         $LFS setstripe -E 32M -S 8M -c -1 -p $pool_name -E eof -S 16M $td ||
394                 error "$LFS setstripe $td failed"
395
396         # create a mirrored file with composite layout mirrors
397         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
398                     -N -c 4 -p none \
399                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
400                 error "create mirrored file $tf failed"
401         verify_mirror_count $tf 6
402         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
403
404         # verify components ${ids[0]} and ${ids[2]}
405         for i in 0 2; do
406                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
407                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
408                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
409                 verify_comp_attr pool $tf ${ids[$i]} flash
410                 verify_comp_extent $tf ${ids[$i]} 0 4194304
411         done
412
413         # verify components ${ids[1]} and ${ids[3]}
414         for i in 1 3; do
415                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
416                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
417                 verify_comp_attr pool $tf ${ids[$i]} flash
418                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
419         done
420
421         # verify component ${ids[4]}
422         verify_comp_attr stripe-size $tf ${ids[4]} 4194304
423         verify_comp_attr stripe-count $tf ${ids[4]} 4
424         verify_comp_attr_with_parent pool $tf ${ids[4]}
425         verify_comp_extent $tf ${ids[4]} 0 EOF
426
427         # verify components ${ids[5]}, ${ids[7]} and ${ids[9]}
428         for i in 5 7 9; do
429                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
430                 verify_comp_attr stripe-count $tf ${ids[$i]} 4
431                 verify_comp_attr pool $tf ${ids[$i]} archive
432                 verify_comp_extent $tf ${ids[$i]} 0 536870912
433         done
434
435         # verify components ${ids[6]}, ${ids[8]} and ${ids[10]}
436         for i in 6 8 10; do
437                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
438                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
439                 verify_comp_attr pool $tf ${ids[$i]} archive
440                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
441         done
442
443         # destroy OST pool
444         destroy_test_pools
445 }
446 run_test 0c "lfs mirror create composite layout mirrors"
447
448 test_0d() {
449         local td=$DIR/$tdir
450         local tf=$td/$tfile
451         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
452         local mirror_cmd="$LFS mirror extend"
453         local ids
454         local i
455
456         # create parent directory
457         mkdir $td || error "mkdir $td failed"
458
459         $mirror_cmd $tf &> /dev/null && error "miss -N option"
460         $mirror_cmd -N $tf &> /dev/null && error "$tf does not exist"
461
462         # create a non-mirrored file, convert it to a mirrored file and extend
463         touch $tf || error "touch $tf failed"
464         $mirror_cmd -N $tf || error "convert and extend $tf failed"
465         verify_mirror_count $tf 2
466         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
467         for ((i = 0; i < 2; i++)); do
468                 verify_comp_attrs $tf ${ids[$i]}
469                 verify_comp_extent $tf ${ids[$i]} 0 EOF
470         done
471
472         lfsck_verify_pfid $tf || error "PFID is not set"
473
474         # create a mirrored file and extend it
475         $LFS mirror create -N $tf-1 || error "create mirrored file $tf-1 failed"
476         $LFS mirror create -N $tf-2 || error "create mirrored file $tf-2 failed"
477
478         $mirror_cmd -N -S 4M -N -f $tf-2 $tf-1 &> /dev/null &&
479                 error "setstripe options should not be specified with -f option"
480
481         $mirror_cmd -N$((mirror_count - 1)) $tf-1 ||
482                 error "extend mirrored file $tf-1 failed"
483         verify_mirror_count $tf-1 $mirror_count
484         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
485         for ((i = 0; i < $mirror_count; i++)); do
486                 verify_comp_attrs $tf-1 ${ids[$i]}
487                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
488         done
489
490         $mirror_cmd -N $tf-1 &> /dev/null &&
491                 error "exceeded maximum mirror count $mirror_count" || true
492 }
493 run_test 0d "lfs mirror extend with -N option"
494
495 test_0e() {
496         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
497
498         local td=$DIR/$tdir
499         local tf=$td/$tfile
500         local mirror_cmd="$LFS mirror extend"
501         local ids
502         local i
503
504         # create parent directory
505         mkdir $td || error "mkdir $td failed"
506
507         # create a mirrored file with plain layout mirrors
508         $LFS mirror create -N -S 32M -c 3 -p ssd -i 1 -o 1,2,3 $tf ||
509                 error "create mirrored file $tf failed"
510
511         # extend the mirrored file with plain layout mirrors
512         $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
513                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
514                 error "extend mirrored file $tf failed"
515         verify_mirror_count $tf 6
516         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
517         for ((i = 0; i < 6; i++)); do
518                 verify_comp_extent $tf ${ids[$i]} 0 EOF
519         done
520
521         # verify component ${ids[0]}
522         verify_comp_attr stripe-size $tf ${ids[0]} 33554432
523         verify_comp_attr stripe-count $tf ${ids[0]} 3
524         verify_comp_attr stripe-index $tf ${ids[0]} 1
525         verify_comp_attr pool $tf ${ids[0]} ssd
526
527         # verify component ${ids[1]}
528         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
529         verify_comp_attr stripe-count $tf ${ids[1]} 2
530         verify_comp_attr stripe-index $tf ${ids[1]} 2
531         verify_comp_attr pool $tf ${ids[1]} flash
532
533         # verify component ${ids[2]}
534         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
535         verify_comp_attr stripe-count $tf ${ids[2]} 2
536         verify_comp_attr pool $tf ${ids[2]} flash
537
538         # verify component ${ids[3]}
539         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
540         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
541         verify_comp_attr pool $tf ${ids[3]} flash
542
543         # verify component ${ids[4]}
544         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
545         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
546         verify_comp_attr pool $tf ${ids[4]} archive
547
548         # verify component ${ids[5]}
549         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
550         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
551         verify_comp_attr_with_parent pool $tf ${ids[5]}
552 }
553 run_test 0e "lfs mirror extend plain layout mirrors"
554
555 test_0f() {
556         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
557
558         local td=$DIR/$tdir
559         local tf=$td/$tfile
560         local mirror_cmd="$LFS mirror extend"
561         local ids
562         local i
563
564         # create parent directory
565         mkdir $td || error "mkdir $td failed"
566
567         # create a mirrored file with composite layout mirror
568         $LFS mirror create -N -E 32M -S 16M -p ssd -E eof -S 32M $tf ||
569                 error "create mirrored file $tf failed"
570
571         # extend the mirrored file with composite layout mirrors
572         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
573                     -N -c -1 -p none \
574                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
575                 error "extend mirrored file $tf failed"
576         verify_mirror_count $tf 7
577         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
578
579         # verify component ${ids[0]}
580         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
581         verify_comp_attr_with_default stripe-count $tf ${ids[0]}
582         verify_comp_attr pool $tf ${ids[0]} ssd
583         verify_comp_extent $tf ${ids[0]} 0 33554432
584
585         # verify component ${ids[1]}
586         verify_comp_attr stripe-size $tf ${ids[1]} 33554432
587         verify_comp_attr_with_default stripe-count $tf ${ids[1]}
588         verify_comp_attr pool $tf ${ids[1]} ssd
589         verify_comp_extent $tf ${ids[1]} 33554432 EOF
590
591         # verify components ${ids[2]} and ${ids[4]}
592         for i in 2 4; do
593                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
594                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
595                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
596                 verify_comp_attr pool $tf ${ids[$i]} flash
597                 verify_comp_extent $tf ${ids[$i]} 0 4194304
598         done
599
600         # verify components ${ids[3]} and ${ids[5]}
601         for i in 3 5; do
602                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
603                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
604                 verify_comp_attr pool $tf ${ids[$i]} flash
605                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
606         done
607
608         # verify component ${ids[6]}
609         verify_comp_attr stripe-size $tf ${ids[6]} 4194304
610         verify_comp_attr stripe-count $tf ${ids[6]} $OSTCOUNT
611         verify_comp_attr_with_parent pool $tf ${ids[6]}
612         verify_comp_extent $tf ${ids[6]} 0 EOF
613
614         # verify components ${ids[7]}, ${ids[9]} and ${ids[11]}
615         for i in 7 9 11; do
616                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
617                 verify_comp_attr stripe-count $tf ${ids[$i]} $OSTCOUNT
618                 verify_comp_attr pool $tf ${ids[$i]} archive
619                 verify_comp_extent $tf ${ids[$i]} 0 536870912
620         done
621
622         # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
623         for i in 8 10 12; do
624                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
625                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
626                 verify_comp_attr pool $tf ${ids[$i]} archive
627                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
628         done
629 }
630 run_test 0f "lfs mirror extend composite layout mirrors"
631
632 test_0g() {
633         local tf=$DIR/$tfile
634
635         $LFS mirror create -N -E 1M -o0 --flags=prefer -E eof -o1 -N -o1 $tf ||
636                 error "create mirrored file $tf failed"
637
638         verify_comp_attr lcme_flags $tf 0x10001 prefer
639         verify_comp_attr lcme_flags $tf 0x10002 prefer
640
641         # write to the mirrored file and check primary
642         cp /etc/hosts $tf || error "error writing file '$tf'"
643
644         verify_comp_attr lcme_flags $tf 0x20003 stale
645
646         # resync file and check prefer flag
647         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
648
649         cancel_lru_locks osc
650         $LCTL set_param osc.*.stats=clear
651         cat $tf &> /dev/null || error "error reading file '$tf'"
652
653         # verify that the data was provided by OST1 where mirror 1 resides
654         local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-ffff*.stats |
655                         awk '/ost_read/{print $2}')
656         [ -n "$nr_read" ] || error "read was not provided by OST1"
657 }
658 run_test 0g "lfs mirror create flags support"
659
660 test_0h() {
661         local tf=$DIR/$tfile
662
663         $LFS mirror create -N -E 1M --flags=prefer -E eof -N2 $tf ||
664                 error "create mirrored file $tf failed"
665
666         verify_comp_attr lcme_flags $tf 0x10001 prefer
667         verify_comp_attr lcme_flags $tf 0x10002 prefer
668
669         # set flags to the first component
670         $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,stale $tf
671
672         verify_comp_attr lcme_flags $tf 0x10001 stale
673         verify_comp_attr lcme_flags $tf 0x10002 prefer
674
675         $LFS setstripe --comp-set -I0x10001 --comp-flags=^stale $tf &&
676                 error "clearing 'stale' should fail"
677
678         # write and resync file. It can't resync the file directly because the
679         # file state is still 'ro'
680         cp /etc/hosts $tf || error "error writing file '$tf'"
681         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
682
683         $LFS setstripe --comp-set -I 0x20003 --comp-flags=prefer $tf ||
684                 error "error setting flag prefer"
685
686         verify_comp_attr lcme_flags $tf 0x20003 prefer
687 }
688 run_test 0h "set, clear and test flags for FLR files"
689
690 test_1() {
691         local tf=$DIR/$tfile
692         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
693         local mirror_create_cmd="$LFS mirror create"
694         local stripes[0]=$OSTCOUNT
695
696         mirror_create_cmd+=" -N -c ${stripes[0]}"
697         for ((i = 1; i < $mirror_count; i++)); do
698                 # add mirrors with different stripes to the file
699                 stripes[$i]=$((RANDOM % OSTCOUNT))
700                 [ ${stripes[$i]} -eq 0 ] && stripes[$i]=1
701
702                 mirror_create_cmd+=" -N -c ${stripes[$i]}"
703         done
704
705         $mirror_create_cmd $tf || error "create mirrored file $tf failed"
706         verify_mirror_count $tf $mirror_count
707
708         # can't create mirrors exceeding LUSTRE_MIRROR_COUNT_MAX
709         $LFS mirror extend -N $tf &&
710                 error "Creating the $((mirror_count+1))th mirror succeeded"
711
712         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
713                         tr '\n' ' '))
714
715         # verify the range of components and stripe counts
716         for ((i = 0; i < $mirror_count; i++)); do
717                 verify_comp_attr stripe-count $tf ${ids[$i]} ${stripes[$i]}
718                 verify_comp_extent $tf ${ids[$i]} 0 EOF
719         done
720 }
721 run_test 1 "create components with setstripe options"
722
723 test_2() {
724         local tf=$DIR/$tfile
725         local tf2=$DIR/$tfile-2
726
727         $LFS setstripe -E 1M -E EOF -c 1 $tf
728         $LFS setstripe -E 2M -E EOF -c -1 $tf2
729
730         local layout=$($LFS getstripe $tf2 | grep -A 4 lmm_objects)
731
732         $LFS mirror extend -N -f $tf2 $tf ||
733                 error "merging $tf2 into $tf failed"
734
735         verify_mirror_count $tf 2
736         [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
737 }
738 run_test 2 "create components from existing files"
739
740 test_3() {
741         [[ $MDSCOUNT -lt 2 ]] && skip "need >= 2 MDTs" && return
742
743         for ((i = 0; i < 2; i++)); do
744                 $LFS mkdir -i $i $DIR/$tdir-$i
745                 $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
746         done
747
748         $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
749                 $DIR/$tdir-0/$tfile || error "creating mirrors"
750
751         # mdt doesn't support to cancel layout lock for remote objects, do
752         # it here manually.
753         cancel_lru_locks mdc
754
755         # make sure the mirrorted file was created successfully
756         [[ $($LFS getstripe --component-count $DIR/$tdir-0/$tfile) -eq 2 ]] ||
757                 { $LFS getstripe $DIR/$tdir-0/$tfile;
758                         error "expected 2 components"; }
759
760         # cleanup
761         rm -rf $DIR/$tdir-*
762 }
763 run_test 3 "create components from files located on different MDTs"
764
765 test_4() {
766         local tf=$DIR/$tdir/$tfile
767         local ids=()
768
769         test_mkdir $DIR/$tdir
770
771         # set mirror with setstripe options to directory
772         $LFS mirror create -N2 -E 1M -E eof $DIR/$tdir ||
773                 error "set mirror to directory error"
774
775         [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
776                 x"mirrored" ] || error "failed to create mirrored dir"
777
778         touch $tf
779         verify_mirror_count $tf 2
780
781         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
782         verify_comp_extent $tf ${ids[0]} 0 1048576
783         verify_comp_extent $tf ${ids[1]} 1048576 EOF
784
785         # sub directory should inherit mirror setting from parent
786         test_mkdir $DIR/$tdir/td
787         [ x$($LFS getstripe -v $DIR/$tdir/td | awk '/lcm_flags/{print $2}') = \
788                 x"mirrored" ] || error "failed to inherit mirror from parent"
789
790         # mirror extend won't be applied to directory
791         $LFS mirror extend -N2 $DIR/$tdir &&
792                 error "expecting mirror extend failure"
793         true
794 }
795 run_test 4 "Make sure mirror attributes can be inhertied from directory"
796
797 test_5() {
798         local tf=$DIR/$tfile
799         local ids=()
800
801         $MULTIOP $tf oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T12345c ||
802                 error "failed to create file with non-empty layout"
803         $CHECKSTAT -t file -s 12345 $tf || error "size error: expecting 12345"
804
805         $LFS mirror create -N3 $tf || error "failed to attach mirror layout"
806         verify_mirror_count $tf 3
807
808         $CHECKSTAT -t file -s 12345 $tf ||
809                 error "size error after attaching layout "
810 }
811 run_test 5 "Make sure init size work for mirrored layout"
812
813 # LU=10112: disable dom+flr for phase 1
814 test_6() {
815         local tf=$DIR/$tfile
816
817         $LFS mirror create -N -E 1M -L mdt -E eof -N -E eof $tf &&
818                 error "expect failure to create mirrored file with DoM"
819
820         $LFS mirror create -N -E 1M -E eof -N -E 1M -L mdt -E eof $tf &&
821                 error "expect failure to create mirrored file with DoM"
822
823         $LFS setstripe -E 1M -L mdt -E eof $tf
824         $LFS mirror extend -N2 $tf &&
825                 error "expect failure to extend mirror with DoM"
826
827         $LFS mirror create -N2 -E 1M -E eof $tf-2
828         $LFS mirror extend -N -f $tf $tf-2 &&
829                 error "expect failure to extend mirrored file with DoM extent"
830
831         true
832 }
833 run_test 6 "DoM and FLR won't co-exist for phase 1"
834
835 test_21() {
836         local tf=$DIR/$tfile
837         local tf2=$DIR/$tfile-2
838
839         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
840
841         $LFS setstripe -E EOF -o 0 $tf
842         $LFS setstripe -E EOF -o 1 $tf2
843
844         local dd_count=$((RANDOM % 20 + 1))
845         dd if=/dev/zero of=$tf bs=1M count=$dd_count oflag=sync
846         dd if=/dev/zero of=$tf2 bs=1M count=1 seek=$((dd_count - 1)) oflag=sync
847
848         # for zfs - sync OST dataset so that du below will return
849         # accurate results
850         [ "$FSTYPE" = "zfs" ] &&
851                 do_nodes $(comma_list $(osts_nodes)) "$ZPOOL sync"
852
853         local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
854
855         # add component
856         $LFS mirror extend -N -f $tf2 $tf ||
857                 error "merging $tf2 into $tf failed"
858
859         # cancel layout lock
860         cancel_lru_locks mdc
861
862         local new_blocks=$(du -k $tf | awk '{print $1}')
863         [ $new_blocks -eq $blocks ] ||
864         error "i_blocks error expected: $blocks, actual: $new_blocks"
865 }
866 run_test 21 "glimpse should report accurate i_blocks"
867
868 get_osc_lock_count() {
869         local lock_count=0
870
871         for idx in "$@"; do
872                 local osc_name
873                 local count
874
875                 osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'ffff*'
876                 count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
877                 lock_count=$((lock_count + count))
878         done
879         echo $lock_count
880 }
881
882 test_22() {
883         local tf=$DIR/$tfile
884
885         $LFS setstripe -E EOF -o 0 $tf
886         dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
887
888         # add component, two mirrors located on the same OST ;-)
889         $LFS mirror extend -N -o 0 $tf ||
890                 error "extending mirrored file $tf failed"
891
892         size_blocks=$(stat --format="%b %s" $tf)
893
894         cancel_lru_locks mdc
895         cancel_lru_locks osc
896
897         local new_size_blocks=$(stat --format="%b %s" $tf)
898
899         # make sure there is no lock cached
900         [ $(get_osc_lock_count 1) -eq 0 ] || error "glimpse requests were sent"
901
902         [ "$new_size_blocks" = "$size_blocks" ] ||
903                 echo "size expected: $size_blocks, actual: $new_size_blocks"
904
905         rm -f $tmpfile
906 }
907 run_test 22 "no glimpse to OSTs for READ_ONLY files"
908
909 test_31() {
910         local tf=$DIR/$tfile
911
912         $LFS mirror create -N -o 0 -N -o 1 $tf ||
913                 error "creating mirrored file $tf failed"
914
915         #define OBD_FAIL_GLIMPSE_IMMUTABLE 0x1A00
916         $LCTL set_param fail_loc=0x1A00
917
918         local ost_idx
919         for ((ost_idx = 1; ost_idx <= 2; ost_idx++)); do
920                 cancel_lru_locks osc
921                 stop_osts $ost_idx
922
923                 local tmpfile=$(mktemp)
924                 stat --format="%b %s" $tf > $tmpfile  &
925                 local pid=$!
926
927                 local cnt=0
928                 while [ $cnt -le 5 ]; do
929                         kill -0 $pid > /dev/null 2>&1 || break
930                         sleep 1
931                         ((cnt += 1))
932                 done
933                 kill -0 $pid > /dev/null 2>&1 &&
934                         error "stat process stuck due to unavailable OSTs"
935
936                 # make sure glimpse request has been sent
937                 [ $(get_osc_lock_count 1 2) -ne 0 ] ||
938                         error "OST $ost_idx: no glimpse request was sent"
939
940                 start_osts $ost_idx
941         done
942 }
943 run_test 31 "make sure glimpse request can be retried"
944
945 test_32() {
946         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
947         rm -f $DIR/$tfile $DIR/$tfile-2
948
949         $LFS setstripe -E EOF -o 0 $DIR/$tfile
950         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=$((RANDOM % 10 + 2))
951
952         local fsize=$(stat -c %s $DIR/$tfile)
953         [[ $fsize -ne 0 ]] || error "file size is (wrongly) zero"
954
955         local cksum=$(md5sum $DIR/$tfile)
956
957         # create a new mirror in sync mode
958         $LFS mirror extend -N -o 1 $DIR/$tfile ||
959                 error "extending mirrored file $DIR/$tfile failed"
960
961         # make sure the mirrored file was created successfully
962         [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
963                 { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
964
965         drop_client_cache
966         stop_osts 1
967
968         # check size is correct, glimpse request should go to the 2nd mirror
969         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
970                 error "file size error $fsize vs. $(stat -c %s $DIR/$tfile)"
971
972         echo "reading file from the 2nd mirror and verify checksum"
973         [[ "$cksum" == "$(md5sum $DIR/$tfile)" ]] ||
974                 error "checksum error: expected $cksum"
975
976         start_osts 1
977 }
978 run_test 32 "data should be mirrored to newly created mirror"
979
980 test_33() {
981         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
982
983         rm -f $DIR/$tfile $DIR/$tfile-2
984
985         # create a file with two mirrors
986         $LFS setstripe -E EOF -o 0 $DIR/$tfile
987         local max_count=100
988         local count=0
989         while [ $count -lt $max_count ]; do
990                 echo "ost1" >> $DIR/$tfile
991                 count=$((count + 1));
992         done
993
994         # tmp file that will be used as mirror
995         $LFS setstripe -E EOF -o 1 $DIR/$tfile-2
996         count=0
997         while [ $count -lt $max_count ]; do
998                 echo "ost2" >> $DIR/$tfile-2
999                 count=$((count + 1));
1000         done
1001
1002         # create a mirrored file
1003         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
1004                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1005                       "with verification should fail"
1006         $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
1007                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1008                       "without verification failed"
1009
1010         # make sure that $tfile has two mirrors and $tfile-2 does not exist
1011         [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
1012                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1013
1014         [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
1015
1016         # execpted file size
1017         local fsize=$((5 * max_count))
1018         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1019                 error "mirrored file size is not $fsize"
1020
1021         # read file - all OSTs are available
1022         echo "reading file (data can be provided by any ost)... "
1023         local rs=$(cat $DIR/$tfile | head -1)
1024         [[ "$rs" == "ost1" || "$rs" == "ost2" ]] ||
1025                 error "file content error: expected: \"ost1\" or \"ost2\""
1026
1027         # read file again with ost1 failed
1028         stop_osts 1
1029         drop_client_cache
1030
1031         echo "reading file (data should be provided by ost2)..."
1032         local rs=$(cat $DIR/$tfile | head -1)
1033         [[ "$rs" == "ost2" ]] ||
1034                 error "file content error: expected: \"ost2\", actual: \"$rs\""
1035
1036         # remount ost1
1037         start_osts 1
1038
1039         # read file again with ost2 failed
1040         $LCTL set_param ldlm.namespaces.lustre-*-osc-ffff*.lru_size=clear
1041
1042         fail ost2 &
1043         sleep 1
1044
1045         # check size, glimpse should work
1046         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1047                 error "mirrored file size is not $fsize"
1048
1049         echo "reading file (data should be provided by ost1)..."
1050         local rs=$(cat $DIR/$tfile | head -1)
1051         [[ "$rs" == "ost1" ]] ||
1052                 error "file content error: expected: \"ost1\", actual: \"$rs\""
1053
1054         wait_osc_import_state client ost2 FULL
1055 }
1056 run_test 33 "read can choose available mirror to read"
1057
1058 test_34a() {
1059         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1060
1061         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1062
1063         # reference file
1064         $LFS setstripe -o 0 $DIR/$tfile-ref
1065         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1066
1067         # create a file with two mirrors
1068         $LFS setstripe -E -1 -o 0,1 -S 1M $DIR/$tfile
1069         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1070
1071         $LFS setstripe -E -1 -o 2,3 -S 1M $DIR/$tfile-2
1072         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1073
1074         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1075                 error "mirrored file size is not 3M"
1076
1077         # merge a mirrored file
1078         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1079                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1080
1081         cancel_lru_locks osc
1082
1083         # stop two OSTs, so the 2nd stripe of the 1st mirror and
1084         # the 1st stripe of the 2nd mirror will be inaccessible, ...
1085         stop_osts 2 3
1086
1087         echo "comparing files ... "
1088
1089         # however, read can still return the correct data. It should return
1090         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1091         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1092                 $DIR/$tfile-ref || error "file reading error"
1093
1094         start_osts 2 3
1095 }
1096 run_test 34a "read mirrored file with multiple stripes"
1097
1098 test_34b() {
1099         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1100
1101         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1102
1103         # reference file
1104         $LFS setstripe -o 0 $DIR/$tfile-ref
1105         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1106
1107         $LFS setstripe -E 1M -S 1M -o 0 -E eof -o 1 $DIR/$tfile
1108         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1109
1110         $LFS setstripe -E 1M -S 1M -o 2 -E eof -o 3 $DIR/$tfile-2
1111         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1112
1113         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1114                 error "mirrored file size is not 3M"
1115
1116         # merge a mirrored file
1117         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1118                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1119
1120         cancel_lru_locks osc
1121
1122         # stop two OSTs, so the 2nd component of the 1st mirror and
1123         # the 1st component of the 2nd mirror will be inaccessible, ...
1124         stop_osts 2 3
1125
1126         echo "comparing files ... "
1127
1128         # however, read can still return the correct data. It should return
1129         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1130         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1131                 $DIR/$tfile-ref || error "file reading error"
1132
1133         start_osts 2 3
1134 }
1135 run_test 34b "read mirrored file with multiple components"
1136
1137 test_35() {
1138         local tf=$DIR/$tfile
1139
1140         $LFS setstripe -E eof $tf
1141
1142         # add an out-of-sync mirror to the file
1143         $LFS mirror extend -N -c 2 $tf ||
1144                 error "extending mirrored file $tf failed"
1145
1146         $MULTIOP $tf oO_WRONLY:c ||
1147                 error "write open a mirrored file failed"
1148
1149         # truncate file should return error
1150         $TRUNCATE $tf 100 || error "error truncating a mirrored file"
1151 }
1152 run_test 35 "allow to write to mirrored files"
1153
1154 verify_ost_layout_version() {
1155         local tf=$1
1156
1157         # get file layout version
1158         local flv=$($LFS getstripe $tf | awk '/lcm_layout_gen/{print $2}')
1159
1160         # layout version from OST objects
1161         local olv=$($MULTIOP $tf oXc | awk '/ostlayoutversion/{print $2}')
1162
1163         [ $flv -eq $olv ] || error "layout version mismatch: $flv vs. $olv"
1164 }
1165
1166 create_file_36() {
1167         local tf
1168
1169         for tf in "$@"; do
1170                 $LFS setstripe -E 1M -E 2M -E 4M -E eof -c -1 $tf
1171                 $LFS setstripe -E 3M -E 6M -E eof -c -1 $tf-tmp
1172
1173                 $LFS mirror extend -N -f $tf-tmp $tf ||
1174                         error "merging $tf-tmp into $tf failed"
1175         done
1176 }
1177
1178 test_36() {
1179         local tf=$DIR/$tfile
1180
1181         create_file_36 $tf $tf-2 $tf-3
1182
1183         [ $(get_mirror_ids $tf) -gt 1 ] || error "wrong mirror count"
1184
1185         # test case 1 - check file write and verify layout version
1186         $MULTIOP $tf oO_WRONLY:c ||
1187                 error "write open a mirrored file failed"
1188
1189         # write open file should not return error
1190         $MULTIOP $tf oO_WRONLY:w1024Yc || error "write mirrored file error"
1191
1192         # instantiate components should work
1193         dd if=/dev/zero of=$tf bs=1M count=12 || error "write file error"
1194
1195         # verify OST layout version
1196         verify_ost_layout_version $tf
1197
1198         # test case 2
1199         local mds_idx=mds$(($($LFS getstripe -M $tf-2) + 1))
1200
1201         local delay_sec=10
1202         do_facet $mds_idx $LCTL set_param fail_val=$delay_sec
1203
1204         #define OBD_FAIL_FLR_LV_DELAY 0x1A01
1205         do_facet $mds_idx $LCTL set_param fail_loc=0x1A01
1206
1207         # write should take at least $fail_loc seconds and succeed
1208         local st=$(date +%s)
1209         $MULTIOP $tf-2 oO_WRONLY:w1024Yc || error "write mirrored file error"
1210
1211         [ $(date +%s) -ge $((st+delay_sec)) ] ||
1212                 error "write finished before layout version is transmitted"
1213
1214         # verify OST layout version
1215         verify_ost_layout_version $tf
1216
1217         do_facet $mds_idx $LCTL set_param fail_loc=0
1218
1219         # test case 3
1220         mds_idx=mds$(($($LFS getstripe -M $tf-3) + 1))
1221
1222         #define OBD_FAIL_FLR_LV_INC 0x1A02
1223         do_facet $mds_idx $LCTL set_param fail_loc=0x1A02
1224
1225         # write open file should return error
1226         $MULTIOP $tf-3 oO_WRONLY:O_SYNC:w1024c &&
1227                 error "write a mirrored file succeeded" || true
1228
1229         do_facet $mds_idx $LCTL set_param fail_loc=0
1230 }
1231 run_test 36 "write to mirrored files"
1232
1233 create_files_37() {
1234         local tf
1235         local fsize=$1
1236
1237         echo "create test files with size $fsize .."
1238
1239         shift
1240         for tf in "$@"; do
1241                 $LFS setstripe -E 1M -c 1 -E eof -c -1 $tf
1242
1243                 dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
1244                 $TRUNCATE $tf $fsize
1245         done
1246 }
1247
1248 test_37()
1249 {
1250         local tf=$DIR/$tfile
1251         local tf2=$DIR/$tfile-2
1252         local tf3=$DIR/$tfile-3
1253
1254         create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
1255
1256         # assume the mirror id will be 1, 2, and 3
1257         declare -A checksums
1258         checksums[1]=$(md5sum $tf | cut -f 1 -d' ')
1259         checksums[2]=$(md5sum $tf2 | cut -f 1 -d' ')
1260         checksums[3]=$(md5sum $tf3 | cut -f 1 -d' ')
1261
1262         printf '%s\n' "${checksums[@]}"
1263
1264         # merge these files into a mirrored file
1265         $LFS mirror extend --no-verify -N -f $tf2 $tf ||
1266                 error "merging $tf2 into $tf failed"
1267         $LFS mirror extend --no-verify -N -f $tf3 $tf ||
1268                 error "merging $tf3 into $tf failed"
1269
1270         get_mirror_ids $tf
1271
1272         # verify mirror read, checksums should equal to the original files'
1273         echo "Verifying mirror read .."
1274
1275         local sum
1276         for i in ${mirror_array[@]}; do
1277                 sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
1278                 [ "$sum" = "${checksums[$i]}" ] ||
1279                         error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
1280         done
1281
1282         # verify mirror copy, write to this mirrored file will invalidate
1283         # the other two mirrors
1284         echo "Verifying mirror copy .."
1285
1286         local osts=$(comma_list $(osts_nodes))
1287
1288         # define OBD_FAIL_OST_SKIP_LV_CHECK     0x241
1289         do_nodes $osts lctl set_param fail_loc=0x241
1290
1291         mirror_io copy -i ${mirror_array[0]} \
1292                 -t $(echo ${mirror_array[@]:1} | tr ' ' ',') $tf ||
1293                         error "mirror copy error"
1294
1295         do_nodes $osts lctl set_param fail_loc=0
1296
1297         # verify copying is successful by checking checksums
1298         remount_client $MOUNT
1299         for i in ${mirror_array[@]}; do
1300                 sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
1301                 [ "$sum" = "${checksums[1]}" ] ||
1302                         error "$i: mismatch checksum after copy"
1303         done
1304
1305         rm -f $tf
1306 }
1307 run_test 37 "mirror I/O API verification"
1308
1309 verify_flr_state()
1310 {
1311         local tf=$1
1312         local expected_state=$2
1313
1314         local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
1315         [ $expected_state = $state ] ||
1316                 error "expected: $expected_state, actual $state"
1317 }
1318
1319 test_38() {
1320         local tf=$DIR/$tfile
1321         local ref=$DIR/${tfile}-ref
1322
1323         $LFS setstripe -E 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf
1324         $LFS setstripe -E 2M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 $tf-2
1325         $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3
1326
1327         # instantiate all components
1328         $LFS mirror extend -N -f $tf-2 $tf ||
1329                 error "merging $tf-2 into $tf failed"
1330         $LFS mirror extend -N -f $tf-3 $tf ||
1331                 error "merging $tf-3 into $tf failed"
1332         $LFS mirror extend -N -c 1 $tf ||
1333                 error "extending mirrored file $tf failed"
1334
1335         verify_flr_state $tf "ro"
1336
1337         dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
1338
1339         local fsize=$((RANDOM << 8 + 1048576))
1340         $TRUNCATE $ref $fsize
1341
1342         local ref_cksum=$(md5sum $ref | cut -f 1 -d' ')
1343
1344         # case 1: verify write to mirrored file & resync work
1345         cp $ref $tf || error "copy from $ref to $f error"
1346         verify_flr_state $tf "wp"
1347
1348         local file_cksum=$(md5sum $tf | cut -f 1 -d' ')
1349         [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
1350
1351         get_mirror_ids $tf
1352         echo "mirror IDs: ${mirror_array[@]}"
1353
1354         local valid_mirror stale_mirror id mirror_cksum
1355         for id in "${mirror_array[@]}"; do
1356                 mirror_cksum=$(mirror_io dump -i $id $tf |
1357                                 md5sum | cut -f 1 -d' ')
1358                 [ "$ref_cksum" == "$mirror_cksum" ] &&
1359                         { valid_mirror=$id; continue; }
1360
1361                 stale_mirror=$id
1362         done
1363
1364         [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
1365         [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
1366
1367         mirror_io resync $tf || error "resync failed"
1368         verify_flr_state $tf "ro"
1369
1370         mirror_cksum=$(mirror_io dump -i $stale_mirror $tf |
1371                         md5sum | cut -f 1 -d' ')
1372         [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
1373
1374         # case 2: inject an error to make mirror_io exit after changing
1375         # the file state to sync_pending so that we can start a concurrent
1376         # write.
1377         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1378         verify_flr_state $tf "wp"
1379
1380         mirror_io resync -e resync_start $tf && error "resync succeeded"
1381         verify_flr_state $tf "sp"
1382
1383         # from sync_pending to write_pending
1384         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1385         verify_flr_state $tf "wp"
1386
1387         mirror_io resync -e resync_start $tf && error "resync succeeded"
1388         verify_flr_state $tf "sp"
1389
1390         # from sync_pending to read_only
1391         mirror_io resync $tf || error "resync failed"
1392         verify_flr_state $tf "ro"
1393 }
1394 run_test 38 "resync"
1395
1396 test_39() {
1397         local tf=$DIR/$tfile
1398
1399         rm -f $tf
1400         $LFS mirror create -N2 -E1m -c1 -S1M -E-1 $tf ||
1401         error "create PFL file $tf failed"
1402
1403         verify_mirror_count $tf 2
1404         verify_comp_count $tf 4
1405
1406         rm -f $tf || error "delete $tf failed"
1407 }
1408 run_test 39 "check FLR+PFL (a.k.a. PFLR) creation"
1409
1410 test_40() {
1411         local tf=$DIR/$tfile
1412         local ops
1413
1414         for ops in "conv=notrunc" ""; do
1415                 rm -f $tf
1416
1417                 $LFS mirror create -N -E2m -E4m -E-1  --flags=prefer \
1418                                 -N -E1m -E2m -E4m -E-1 $tf ||
1419                         error "create PFLR file $tf failed"
1420                 dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
1421                         error "write PFLR file $tf failed"
1422
1423                 lfs getstripe -vy $tf
1424
1425                 local flags
1426
1427                 # file mirror state should be write_pending
1428                 flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1429                 [ $flags = wp ] ||
1430                 error "file mirror state $flags"
1431                 # the 1st component (in mirror 1) should be inited
1432                 verify_comp_attr lcme_flags $tf 0x10001 init
1433                 # the 2nd component (in mirror 1) should be inited
1434                 verify_comp_attr lcme_flags $tf 0x10002 init
1435                 # the 3rd component (in mirror 1) should be uninited
1436                 verify_comp_attr lcme_flags $tf 0x10003 prefer
1437                 # the 4th component (in mirror 2) should be inited
1438                 verify_comp_attr lcme_flags $tf 0x20004 init
1439                 # the 5th component (in mirror 2) should be uninited
1440                 verify_comp_attr lcme_flags $tf 0x20005 0
1441                 # the 6th component (in mirror 2) should be stale
1442                 verify_comp_attr lcme_flags $tf 0x20006 stale
1443                 # the 7th component (in mirror 2) should be uninited
1444                 if [[ x$ops = "xconv=notrunc" ]]; then
1445                         verify_comp_attr lcme_flags $tf 0x20007 0
1446                 elif [[ x$ops = "x" ]]; then
1447                         verify_comp_attr lcme_flags $tf 0x20007 stale
1448                 fi
1449         done
1450
1451         rm -f $tf || error "delete $tf failed"
1452 }
1453 run_test 40 "PFLR rdonly state instantiation check"
1454
1455 test_41() {
1456         local tf=$DIR/$tfile
1457
1458         rm -f $tf $tf-1
1459         echo " **create two FLR files $tf $tf-1"
1460         $LFS mirror create -N -E2m -E4m -E-1 -N -E1m -E2m -E3m -E-1 $tf ||
1461                 error "create PFLR file $tf failed"
1462         $LFS mirror create -N -E2m -Eeof -N -E1m -Eeof --flags prefer \
1463                 -N -E4m -Eeof $tf-1 || error "create PFLR file $tf-1 failed"
1464
1465         # file should be in ro status
1466         echo " **verify files be RDONLY"
1467         verify_flr_state $tf "ro"
1468         verify_flr_state $tf-1 "ro"
1469
1470         # write data in [0, 2M)
1471         dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
1472                 error "writing $tf failed"
1473         dd if=/dev/zero of=$tf-1 bs=1M count=4 conv=notrunc ||
1474                 error "writing $tf-1 failed"
1475
1476         echo " **verify files be WRITE_PENDING"
1477         verify_flr_state $tf "wp"
1478         verify_flr_state $tf-1 "wp"
1479
1480         # file should have stale component
1481         echo " **verify files have stale component"
1482         $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
1483                 error "after writing $tf, it does not contain stale component"
1484         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1485                 error "after writing $tf-1, it does not contain stale component"
1486
1487         echo " **full resync"
1488         $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
1489
1490         echo " **verify files be RDONLY"
1491         verify_flr_state $tf "ro"
1492         verify_flr_state $tf-1 "ro"
1493
1494         # file should not have stale component
1495         echo " **verify files do not contain stale component"
1496         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1497                 error "after resyncing $tf, it contains stale component"
1498         $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
1499                 error "after resyncing $tf, it contains stale component"
1500
1501         # verify partial resync
1502         echo " **write $tf-1 for partial resync test"
1503         dd if=/dev/zero of=$tf-1 bs=1M count=2 conv=notrunc ||
1504                 error "writing $tf-1 failed"
1505
1506         echo " **only resync mirror 2"
1507         verify_flr_state $tf-1 "wp"
1508         $LFS mirror resync --only 2 $tf-1 ||
1509                 error "resync mirror 2 of $tf-1 failed"
1510         verify_flr_state $tf "ro"
1511
1512         # resync synced mirror
1513         echo " **resync mirror 2 again"
1514         $LFS mirror resync --only 2 $tf-1 ||
1515                 error "resync mirror 2 of $tf-1 failed"
1516         verify_flr_state $tf "ro"
1517         echo " **verify $tf-1 contains stale component"
1518         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1519                 error "after writing $tf-1, it does not contain stale component"
1520
1521         echo " **full resync $tf-1"
1522         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1523         verify_flr_state $tf "ro"
1524         echo " **full resync $tf-1 again"
1525         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1526         echo " **verify $tf-1 does not contain stale component"
1527         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1528                 error "after resyncing $tf, it contains stale component"
1529
1530         return 0
1531 }
1532 run_test 41 "lfs mirror resync check"
1533
1534 test_42() {
1535         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1536
1537         local td=$DIR/$tdir
1538         local tf=$td/$tfile
1539         local mirror_cmd="$LFS mirror verify"
1540         local i
1541
1542         # create parent directory
1543         mkdir $td || error "mkdir $td failed"
1544
1545         $mirror_cmd &> /dev/null && error "no file name given"
1546         $mirror_cmd $tf &> /dev/null && error "cannot stat file $tf"
1547         $mirror_cmd $td &> /dev/null && error "$td is not a regular file"
1548
1549         # create mirrored files
1550         $LFS mirror create -N -E 4M -E 10M -E EOF $tf ||
1551                 error "create mirrored file $tf failed"
1552         $LFS mirror create -N -E 2M -E EOF \
1553                            -N -E 6M -E 8M -E EOF \
1554                            -N -E 16M -E EOF $tf-1 ||
1555                 error "create mirrored file $tf-1 failed"
1556         $LFS mirror create -N -c 2 -o 1,3 -N -S 4M -c -1 $tf-2 ||
1557                 error "create mirrored file $tf-2 failed"
1558
1559         # write data in [0, 10M)
1560         for i in $tf $tf-1 $tf-2; do
1561                 yes | dd of=$i bs=1M count=10 iflag=fullblock conv=notrunc ||
1562                         error "write $i failed"
1563         done
1564
1565         # resync the mirrored files
1566         $LFS mirror resync $tf-1 $tf-2 ||
1567                 error "resync $tf-1 $tf-2 failed"
1568
1569         # verify the mirrored files
1570         $mirror_cmd $tf-1 $tf-2 ||
1571                 error "verify $tf-1 $tf-2 failed"
1572
1573         get_mirror_ids $tf-1
1574         $mirror_cmd --only ${mirror_array[0]} $tf-1 &> /dev/null &&
1575                 error "at least 2 mirror ids needed with '--only' option"
1576         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 $tf-2 \
1577                 &> /dev/null &&
1578                 error "'--only' option cannot be used upon multiple files"
1579         $mirror_cmd --only 65534,${mirror_array[0]},65535 $tf-1 &&
1580                 error "invalid specified mirror ids"
1581
1582         # change the content of $tf and merge it into $tf-1
1583         for i in 6 10; do
1584                 echo a | dd of=$tf bs=1M seek=$i conv=notrunc ||
1585                         error "change $tf with seek=$i failed"
1586                 echo b | dd of=$tf-1 bs=1M seek=$i conv=notrunc ||
1587                         error "change $tf-1 with seek=$i failed"
1588         done
1589
1590         $LFS mirror resync $tf-1 || error "resync $tf-1 failed"
1591         $LFS mirror extend --no-verify -N -f $tf $tf-1 ||
1592                 error "merge $tf into $tf-1 failed"
1593
1594         # verify the mirrored files
1595         echo "Verify $tf-1 without -v option:"
1596         $mirror_cmd $tf-1 &&
1597                 error "verify $tf-1 should fail" || echo "PASS"
1598
1599         echo "Verify $tf-1 with -v option:"
1600         $mirror_cmd -v $tf-1 &&
1601                 error "verify $tf-1 should fail"
1602
1603         get_mirror_ids $tf-1
1604         echo "Verify $tf-1 with --only option:"
1605         $mirror_cmd -v --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 &&
1606                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1607                       "${mirror_array[-1]} should fail"
1608
1609         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 ||
1610                 error "verify $tf-1 with mirror ${mirror_array[0]} and" \
1611                       "${mirror_array[1]} should succeed"
1612
1613         # set stale components in $tf-1
1614         for i in 0x40002 0x40003; do
1615                 $LFS setstripe --comp-set -I$i --comp-flags=stale $tf-1 ||
1616                         error "set stale flag on component $i failed"
1617         done
1618
1619         # verify the mirrored file
1620         echo "Verify $tf-1 with stale components:"
1621         $mirror_cmd -vvv $tf-1 ||
1622                 error "verify $tf-1 with stale components should succeed"
1623
1624         echo "Verify $tf-1 with stale components and --only option:"
1625         $mirror_cmd -vvv --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 ||
1626                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1627                       "${mirror_array[-1]} should succeed"
1628 }
1629 run_test 42 "lfs mirror verify"
1630
1631 # inactivate one OST && write && restore the OST
1632 write_file_43() {
1633         local file=$1
1634         local ost=$2
1635         local PARAM="osp.${FSNAME}-OST000${ost}-osc-M*.active"
1636         local wait
1637
1638         wait=$(do_facet $SINGLEMDS \
1639                 "$LCTL get_param -n lod.*MDT0000-*.qos_maxage")
1640         wait=${wait%%[^0-9]*}
1641
1642         echo "  **deactivate OST$ost, waiting for $((wait*2+2)) seconds"
1643         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 0")
1644         # lod_qos_statfs_update needs 2*$wait seconds to refresh targets statfs
1645         sleep $(($wait * 2 + 2))
1646         echo "  **write $file"
1647         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
1648         echo "  **restore activating OST$ost, waiting for $((wait*2+2)) seconds"
1649         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 1")
1650         sleep $((wait * 2 + 2))
1651
1652         local flags=$($LFS getstripe -v $file | awk '/lcm_flags:/ { print $2 }')
1653         [ $flags = wp ] || error "file mirror state $flags != wp"
1654 }
1655
1656 test_43() {
1657         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
1658
1659         local tf=$DIR/$tfile
1660         local flags
1661
1662         rm -f $tf
1663         ##   mirror 0  ost (0, 1)
1664         ##   mirror 1  ost (1, 2)
1665         ##   mirror 2  ost (2, 0)
1666         $LFS mirror create -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 \
1667                 -N -Eeof -c2 -o2,0 $tf ||
1668                 error "create 3 mirrors file $tf failed"
1669
1670         ################## OST0 ###########################################
1671         write_file_43 $tf 0
1672         echo "  **verify components"
1673         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1674         verify_comp_attr lcme_flags $tf 0x20002 init
1675         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1676
1677         # resync
1678         echo "  **resync $tf"
1679         $LFS mirror resync $tf
1680         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1681         [ $flags = ro ] || error "file mirror state $flags != ro"
1682
1683         ################## OST1 ###########################################
1684         write_file_43 $tf 1
1685         echo "  **verify components"
1686         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1687         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1688         verify_comp_attr lcme_flags $tf 0x30003 init
1689
1690         # resync
1691         echo "  **resync $tf"
1692         $LFS mirror resync $tf
1693         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1694         [ $flags = ro ] || error "file mirror state $flags != ro"
1695
1696         ################## OST2 ###########################################
1697         write_file_43 $tf 2
1698         echo "  **verify components"
1699         verify_comp_attr lcme_flags $tf 0x10001 init
1700         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1701         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1702 }
1703 run_test 43 "mirror pick on write"
1704
1705 test_44() {
1706         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1707         rm -rf $DIR/$tdir
1708         rm -rf $DIR/$tdir-1
1709         local tf=$DIR/$tdir/$tfile
1710         local tf1=$DIR/$tdir-1/$tfile-1
1711
1712         $LFS setdirstripe -i 0 -c 1 $DIR/$tdir ||
1713                 error "create directory failed"
1714         $LFS setdirstripe -i 1 -c 1 $DIR/$tdir-1 ||
1715                 error "create remote directory failed"
1716         rm -f $tf $tf1 $tf.mirror~2
1717         # create file with 4 mirrors
1718         $LFS mirror create -N -E2m -E4m -E-1 -N -E1m -E2m -E3m -E-1 -N2 $tf ||
1719                 error "create PFLR file $tf failed"
1720
1721         # file should be in ro status
1722         verify_flr_state $tf "ro"
1723
1724         # write data in [0, 3M)
1725         dd if=/dev/urandom of=$tf bs=1M count=3 conv=notrunc ||
1726                 error "writing $tf failed"
1727
1728         verify_flr_state $tf "wp"
1729
1730         # synchronize all mirrors of the file
1731         $LFS mirror resync $tf || error "mirror resync $tf failed"
1732
1733         verify_flr_state $tf "ro"
1734
1735         # split mirror 1
1736         $LFS mirror split --mirror-id 1 -f $tf1 $tf ||
1737                 error "split to $tf1 failed"
1738
1739         local idx0=$($LFS getstripe -m $tf)
1740         local idx1=$($LFS getstripe -m $tf1)
1741
1742         [[ x$idx0 == x0 ]] || error "$tf is not on MDT0"
1743         [[ x$idx1 == x1 ]] || error "$tf1 is not on MDT1"
1744
1745         # verify mirror count
1746         verify_mirror_count $tf 3
1747         verify_mirror_count $tf1 1
1748
1749         $LFS mirror split --mirror-id 2 $tf ||
1750                 error "split mirror 2 failed"
1751
1752         verify_mirror_count $tf 2
1753         verify_mirror_count $tf.mirror~2 1
1754
1755         $LFS mirror split --mirror-id 3 -d $tf ||
1756                 error "split and delte mirror 3 failed"
1757         verify_mirror_count $tf 1
1758
1759         # verify splitted file contains the same content as the orig file does
1760         diff $tf $tf1 || error "splited file $tf1 diffs from $tf"
1761         diff $tf $tf.mirror~2 ||
1762                 error "splited file $tf.mirror~2 diffs from $tf"
1763 }
1764 run_test 44 "lfs mirror split check"
1765
1766 test_45() {
1767         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1768
1769         local file=$DIR/$tdir/$tfile
1770         local dir=$DIR/$tdir/$dir
1771         local temp=$DIR/$tdir/template
1772         rm -rf $DIR/$tdir
1773         test_mkdir $DIR/$tdir
1774
1775         $LFS setstripe -N -E1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
1776                 -N -E3m -Eeof -N -E8m -Eeof $file || error "Create $file failed"
1777
1778         echo "getstripe --yaml $file"
1779         $LFS getstripe --yaml $file > $temp || error "getstripe $file failed"
1780         echo "setstripe --yaml=$temp $file.2"
1781         $LFS setstripe --yaml=$temp $file.2 || error "setstripe $file.2 failed"
1782
1783         echo "compare layout"
1784         local layout1=$(get_layout_param $file)
1785         local layout2=$(get_layout_param $file.2)
1786         [ "$layout1" == "$layout2" ] ||
1787                 error "FLR file $file/$file.2 layouts are not equal"
1788 }
1789 run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
1790
1791 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
1792 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
1793
1794 write_file_200() {
1795         local tf=$1
1796
1797         local fsize=$(stat --printf=%s $tf)
1798
1799         while [ -f $ctrl_file ]; do
1800                 local off=$((RANDOM << 8))
1801                 local len=$((RANDOM << 5 + 131072))
1802
1803                 [ $((off + len)) -gt $fsize ] && {
1804                         fsize=$((off + len))
1805                         echo "Extending file size to $fsize .."
1806                 }
1807
1808                 flock -s $lock_file -c \
1809                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
1810                                 { rm -f $ctrl_file;
1811                                   error "failed writing to $off:$len"; }
1812                 sleep 0.$((RANDOM % 2 + 1))
1813         done
1814 }
1815
1816 read_file_200() {
1817         local tf=$1
1818
1819         while [ -f $ctrl_file ]; do
1820                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
1821                         { rm -f $ctrl_file; error "read failed"; }
1822                 sleep 0.$((RANDOM % 2 + 1))
1823         done
1824 }
1825
1826 resync_file_200() {
1827         local tf=$1
1828
1829         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
1830
1831         exec 200<>$lock_file
1832         while [ -f $ctrl_file ]; do
1833                 local lock_taken=false
1834                 local index=$((RANDOM % ${#options[@]}))
1835                 local cmd="mirror_io resync ${options[$index]}"
1836
1837                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
1838
1839                 [ $((RANDOM % 4)) -eq 0 ] && {
1840                         index=0
1841                         lock_taken=true
1842                         echo -n "lock to "
1843                 }
1844
1845                 echo -n "resync file $tf with '$cmd' .."
1846
1847                 $lock_taken && flock -x 200
1848                 $cmd $tf &> /dev/null && echo "done" || echo "failed"
1849                 $lock_taken && flock -u 200
1850
1851                 sleep 0.$((RANDOM % 8 + 1))
1852         done
1853 }
1854
1855 test_200() {
1856         local tf=$DIR/$tfile
1857         local tf2=$DIR2/$tfile
1858         local tf3=$DIR3/$tfile
1859
1860         $LFS setstripe -E 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
1861         $LFS setstripe -E 2M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
1862         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
1863
1864         $LFS mirror extend -N -f $tf-2 $tf ||
1865                 error "merging $tf-2 into $tf failed"
1866         $LFS mirror extend -N -f $tf-3 $tf ||
1867                 error "merging $tf-3 into $tf failed"
1868
1869         mkdir -p $MOUNT2 && mount_client $MOUNT2
1870
1871         mkdir -p $MOUNT3 && mount_client $MOUNT3
1872
1873         verify_flr_state $tf3 "ro"
1874
1875         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
1876         $LCTL set_param fail_loc=0x1A03
1877
1878         local mds_idx=mds$(($($LFS getstripe -M $tf) + 1))
1879         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
1880
1881         declare -a pids
1882
1883         write_file_200 $tf &
1884         pids+=($!)
1885
1886         read_file_200 $tf &
1887         pids+=($!)
1888
1889         write_file_200 $tf2 &
1890         pids+=($!)
1891
1892         read_file_200 $tf2 &
1893         pids+=($!)
1894
1895         resync_file_200 $tf3 &
1896         pids+=($!)
1897
1898         local sleep_time=60
1899         [ "$SLOW" = "yes" ] && sleep_time=360
1900         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
1901                 sleep 1
1902                 ((--sleep_time))
1903         done
1904
1905         rm -f $ctrl_file
1906
1907         echo "Waiting ${pids[@]}"
1908         wait ${pids[@]}
1909
1910         umount_client $MOUNT2
1911         umount_client $MOUNT3
1912
1913         rm -f $lock_file
1914
1915         # resync and verify mirrors
1916         mirror_io resync $tf
1917         get_mirror_ids $tf
1918
1919         local csum=$(mirror_io dump -i ${mirror_array[0]} $tf | md5sum)
1920         for id in ${mirror_array[@]:1}; do
1921                 [ "$(mirror_io dump -i $id $tf | md5sum)" = "$csum" ] ||
1922                         error "checksum error for mirror $id"
1923         done
1924
1925         true
1926 }
1927 run_test 200 "stress test"
1928
1929 cleanup_test_201() {
1930         trap 0
1931         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
1932
1933         umount_client $MOUNT2
1934 }
1935
1936 test_201() {
1937         local delay=${RESYNC_DELAY:-5}
1938
1939         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
1940                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
1941
1942         trap cleanup_test_201 EXIT
1943
1944         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
1945                         changelog_register -n)
1946
1947         mkdir -p $MOUNT2 && mount_client $MOUNT2
1948
1949         local index=0
1950         while :; do
1951                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
1952                 [ -z "$log" ] && { sleep 1; continue; }
1953
1954                 index=$(echo $log | awk '{print $1}')
1955                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
1956                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
1957                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
1958
1959                 ((++index))
1960                 [ -z "$file" ] && continue
1961
1962                 local now=$(date +%s)
1963
1964                 echo "file: $file $fid was modified at $ts, now: $now, " \
1965                      "will be resynced at $((ts+delay))"
1966
1967                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
1968
1969                 mirror_io resync $file
1970                 echo "$file resync done"
1971         done
1972
1973         cleanup_test_201
1974 }
1975 run_test 201 "FLR data mover"
1976
1977 test_202() {
1978         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1979
1980         local tf=$DIR/$tfile
1981         local ids
1982
1983         $LFS setstripe -E 1M -c 1 $tf
1984         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
1985         verify_comp_attr stripe-count $tf ${ids[0]} 1
1986
1987         $LFS setstripe --component-add -E 2M -c -1 $tf
1988         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
1989         verify_comp_attr stripe-count $tf ${ids[0]} 1
1990         verify_comp_attr stripe-count $tf ${ids[1]} -1
1991
1992         dd if=/dev/zero of=$tf bs=1M count=2
1993         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
1994         verify_comp_attr stripe-count $tf ${ids[0]} 1
1995         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
1996 }
1997 run_test 202 "lfs setstripe --add-component wide striping"
1998
1999
2000 complete $SECONDS
2001 check_and_cleanup_lustre
2002 exit_status