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