Whamcloud - gitweb
LU-10465 lov: increase default stripe size to 4MB
[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 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
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_osc_import_state client ost$idx FULL
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         local layout=$($LFS getstripe $tf2 | grep -A 4 lmm_objects)
734
735         $LFS mirror extend -N -f $tf2 $tf ||
736                 error "merging $tf2 into $tf failed"
737
738         verify_mirror_count $tf 2
739         [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
740 }
741 run_test 2 "create components from existing files"
742
743 test_3() {
744         [[ $MDSCOUNT -lt 2 ]] && skip "need >= 2 MDTs" && return
745
746         for ((i = 0; i < 2; i++)); do
747                 $LFS mkdir -i $i $DIR/$tdir-$i
748                 $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
749         done
750
751         $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
752                 $DIR/$tdir-0/$tfile || error "creating mirrors"
753
754         # mdt doesn't support to cancel layout lock for remote objects, do
755         # it here manually.
756         cancel_lru_locks mdc
757
758         # make sure the mirrorted file was created successfully
759         [[ $($LFS getstripe --component-count $DIR/$tdir-0/$tfile) -eq 2 ]] ||
760                 { $LFS getstripe $DIR/$tdir-0/$tfile;
761                         error "expected 2 components"; }
762
763         # cleanup
764         rm -rf $DIR/$tdir-*
765 }
766 run_test 3 "create components from files located on different MDTs"
767
768 test_4() {
769         local tf=$DIR/$tdir/$tfile
770         local ids=()
771
772         test_mkdir $DIR/$tdir
773
774         # set mirror with setstripe options to directory
775         $LFS mirror create -N2 -E 1M -S 1M -E eof $DIR/$tdir ||
776                 error "set mirror to directory error"
777
778         [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
779                 x"mirrored" ] || error "failed to create mirrored dir"
780
781         touch $tf
782         verify_mirror_count $tf 2
783
784         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
785         verify_comp_extent $tf ${ids[0]} 0 1048576
786         verify_comp_extent $tf ${ids[1]} 1048576 EOF
787
788         # sub directory should inherit mirror setting from parent
789         test_mkdir $DIR/$tdir/td
790         [ x$($LFS getstripe -v $DIR/$tdir/td | awk '/lcm_flags/{print $2}') = \
791                 x"mirrored" ] || error "failed to inherit mirror from parent"
792
793         # mirror extend won't be applied to directory
794         $LFS mirror extend -N2 $DIR/$tdir &&
795                 error "expecting mirror extend failure"
796         true
797 }
798 run_test 4 "Make sure mirror attributes can be inhertied from directory"
799
800 test_5() {
801         local tf=$DIR/$tfile
802         local ids=()
803
804         $MULTIOP $tf oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T12345c ||
805                 error "failed to create file with non-empty layout"
806         $CHECKSTAT -t file -s 12345 $tf || error "size error: expecting 12345"
807
808         $LFS mirror create -N3 $tf || error "failed to attach mirror layout"
809         verify_mirror_count $tf 3
810
811         $CHECKSTAT -t file -s 12345 $tf ||
812                 error "size error after attaching layout "
813 }
814 run_test 5 "Make sure init size work for mirrored layout"
815
816 # LU=10112: disable dom+flr for phase 1
817 test_6() {
818         local tf=$DIR/$tfile
819
820         $LFS mirror create -N -E 1M -S 1M -L mdt -E eof -N -E eof $tf &&
821                 error "expect failure to create mirrored file with DoM"
822
823         $LFS mirror create -N -E 1M -S 1M -E eof -N -E 1M -L mdt -E eof $tf &&
824                 error "expect failure to create mirrored file with DoM"
825
826         $LFS setstripe -E 1M -S 1M -L mdt -E eof $tf
827         $LFS mirror extend -N2 $tf &&
828                 error "expect failure to extend mirror with DoM"
829
830         $LFS mirror create -N2 -E 1M -S 1M -E eof $tf-2
831         $LFS mirror extend -N -f $tf $tf-2 &&
832                 error "expect failure to extend mirrored file with DoM extent"
833
834         true
835 }
836 run_test 6 "DoM and FLR won't co-exist for phase 1"
837
838 test_21() {
839         local tf=$DIR/$tfile
840         local tf2=$DIR/$tfile-2
841
842         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
843
844         $LFS setstripe -E EOF -o 0 $tf
845         $LFS setstripe -E EOF -o 1 $tf2
846
847         local dd_count=$((RANDOM % 20 + 1))
848         dd if=/dev/zero of=$tf bs=1M count=$dd_count oflag=sync
849         dd if=/dev/zero of=$tf2 bs=1M count=1 seek=$((dd_count - 1)) oflag=sync
850
851         # for zfs - sync OST dataset so that du below will return
852         # accurate results
853         [ "$FSTYPE" = "zfs" ] &&
854                 do_nodes $(comma_list $(osts_nodes)) "$ZPOOL sync"
855
856         local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
857
858         # add component
859         $LFS mirror extend -N -f $tf2 $tf ||
860                 error "merging $tf2 into $tf failed"
861
862         # cancel layout lock
863         cancel_lru_locks mdc
864
865         local new_blocks=$(du -k $tf | awk '{print $1}')
866         [ $new_blocks -eq $blocks ] ||
867         error "i_blocks error expected: $blocks, actual: $new_blocks"
868 }
869 run_test 21 "glimpse should report accurate i_blocks"
870
871 get_osc_lock_count() {
872         local lock_count=0
873
874         for idx in "$@"; do
875                 local osc_name
876                 local count
877
878                 osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'ffff*'
879                 count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
880                 lock_count=$((lock_count + count))
881         done
882         echo $lock_count
883 }
884
885 test_22() {
886         local tf=$DIR/$tfile
887
888         $LFS setstripe -E EOF -o 0 $tf
889         dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
890
891         # add component, two mirrors located on the same OST ;-)
892         $LFS mirror extend -N -o 0 $tf ||
893                 error "extending mirrored file $tf failed"
894
895         size_blocks=$(stat --format="%b %s" $tf)
896
897         cancel_lru_locks mdc
898         cancel_lru_locks osc
899
900         local new_size_blocks=$(stat --format="%b %s" $tf)
901
902         # make sure there is no lock cached
903         [ $(get_osc_lock_count 1) -eq 0 ] || error "glimpse requests were sent"
904
905         [ "$new_size_blocks" = "$size_blocks" ] ||
906                 echo "size expected: $size_blocks, actual: $new_size_blocks"
907
908         rm -f $tmpfile
909 }
910 run_test 22 "no glimpse to OSTs for READ_ONLY files"
911
912 test_31() {
913         local tf=$DIR/$tfile
914
915         $LFS mirror create -N -o 0 -N -o 1 $tf ||
916                 error "creating mirrored file $tf failed"
917
918         #define OBD_FAIL_GLIMPSE_IMMUTABLE 0x1A00
919         $LCTL set_param fail_loc=0x1A00
920
921         local ost_idx
922         for ((ost_idx = 1; ost_idx <= 2; ost_idx++)); do
923                 cancel_lru_locks osc
924                 stop_osts $ost_idx
925
926                 local tmpfile=$(mktemp)
927                 stat --format="%b %s" $tf > $tmpfile  &
928                 local pid=$!
929
930                 local cnt=0
931                 while [ $cnt -le 5 ]; do
932                         kill -0 $pid > /dev/null 2>&1 || break
933                         sleep 1
934                         ((cnt += 1))
935                 done
936                 kill -0 $pid > /dev/null 2>&1 &&
937                         error "stat process stuck due to unavailable OSTs"
938
939                 # make sure glimpse request has been sent
940                 [ $(get_osc_lock_count 1 2) -ne 0 ] ||
941                         error "OST $ost_idx: no glimpse request was sent"
942
943                 start_osts $ost_idx
944         done
945 }
946 run_test 31 "make sure glimpse request can be retried"
947
948 test_32() {
949         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
950         rm -f $DIR/$tfile $DIR/$tfile-2
951
952         $LFS setstripe -E EOF -o 0 $DIR/$tfile
953         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=$((RANDOM % 10 + 2))
954
955         local fsize=$(stat -c %s $DIR/$tfile)
956         [[ $fsize -ne 0 ]] || error "file size is (wrongly) zero"
957
958         local cksum=$(md5sum $DIR/$tfile)
959
960         # create a new mirror in sync mode
961         $LFS mirror extend -N -o 1 $DIR/$tfile ||
962                 error "extending mirrored file $DIR/$tfile failed"
963
964         # make sure the mirrored file was created successfully
965         [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
966                 { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
967
968         drop_client_cache
969         stop_osts 1
970
971         # check size is correct, glimpse request should go to the 2nd mirror
972         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
973                 error "file size error $fsize vs. $(stat -c %s $DIR/$tfile)"
974
975         echo "reading file from the 2nd mirror and verify checksum"
976         [[ "$cksum" == "$(md5sum $DIR/$tfile)" ]] ||
977                 error "checksum error: expected $cksum"
978
979         start_osts 1
980 }
981 run_test 32 "data should be mirrored to newly created mirror"
982
983 test_33() {
984         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
985
986         rm -f $DIR/$tfile $DIR/$tfile-2
987
988         # create a file with two mirrors
989         $LFS setstripe -E EOF -o 0 $DIR/$tfile
990         local max_count=100
991         local count=0
992         while [ $count -lt $max_count ]; do
993                 echo "ost1" >> $DIR/$tfile
994                 count=$((count + 1));
995         done
996
997         # tmp file that will be used as mirror
998         $LFS setstripe -E EOF -o 1 $DIR/$tfile-2
999         count=0
1000         while [ $count -lt $max_count ]; do
1001                 echo "ost2" >> $DIR/$tfile-2
1002                 count=$((count + 1));
1003         done
1004
1005         # create a mirrored file
1006         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
1007                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1008                       "with verification should fail"
1009         $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
1010                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1011                       "without verification failed"
1012
1013         # make sure that $tfile has two mirrors and $tfile-2 does not exist
1014         [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
1015                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1016
1017         [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
1018
1019         # execpted file size
1020         local fsize=$((5 * max_count))
1021         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1022                 error "mirrored file size is not $fsize"
1023
1024         # read file - all OSTs are available
1025         echo "reading file (data can be provided by any ost)... "
1026         local rs=$(cat $DIR/$tfile | head -1)
1027         [[ "$rs" == "ost1" || "$rs" == "ost2" ]] ||
1028                 error "file content error: expected: \"ost1\" or \"ost2\""
1029
1030         # read file again with ost1 failed
1031         stop_osts 1
1032         drop_client_cache
1033
1034         echo "reading file (data should be provided by ost2)..."
1035         local rs=$(cat $DIR/$tfile | head -1)
1036         [[ "$rs" == "ost2" ]] ||
1037                 error "file content error: expected: \"ost2\", actual: \"$rs\""
1038
1039         # remount ost1
1040         start_osts 1
1041
1042         # read file again with ost2 failed
1043         $LCTL set_param ldlm.namespaces.lustre-*-osc-ffff*.lru_size=clear
1044
1045         fail ost2 &
1046         sleep 1
1047
1048         # check size, glimpse should work
1049         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1050                 error "mirrored file size is not $fsize"
1051
1052         echo "reading file (data should be provided by ost1)..."
1053         local rs=$(cat $DIR/$tfile | head -1)
1054         [[ "$rs" == "ost1" ]] ||
1055                 error "file content error: expected: \"ost1\", actual: \"$rs\""
1056
1057         wait_osc_import_state client ost2 FULL
1058 }
1059 run_test 33 "read can choose available mirror to read"
1060
1061 test_34a() {
1062         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1063
1064         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1065
1066         # reference file
1067         $LFS setstripe -o 0 $DIR/$tfile-ref
1068         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1069
1070         # create a file with two mirrors
1071         $LFS setstripe -E -1 -o 0,1 -S 1M $DIR/$tfile
1072         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1073
1074         $LFS setstripe -E -1 -o 2,3 -S 1M $DIR/$tfile-2
1075         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1076
1077         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1078                 error "mirrored file size is not 3M"
1079
1080         # merge a mirrored file
1081         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1082                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1083
1084         cancel_lru_locks osc
1085
1086         # stop two OSTs, so the 2nd stripe of the 1st mirror and
1087         # the 1st stripe of the 2nd mirror will be inaccessible, ...
1088         stop_osts 2 3
1089
1090         echo "comparing files ... "
1091
1092         # however, read can still return the correct data. It should return
1093         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1094         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1095                 $DIR/$tfile-ref || error "file reading error"
1096
1097         start_osts 2 3
1098 }
1099 run_test 34a "read mirrored file with multiple stripes"
1100
1101 test_34b() {
1102         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1103
1104         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1105
1106         # reference file
1107         $LFS setstripe -o 0 $DIR/$tfile-ref
1108         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1109
1110         $LFS setstripe -E 1M -S 1M -o 0 -E eof -o 1 $DIR/$tfile
1111         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1112
1113         $LFS setstripe -E 1M -S 1M -o 2 -E eof -o 3 $DIR/$tfile-2
1114         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1115
1116         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1117                 error "mirrored file size is not 3M"
1118
1119         # merge a mirrored file
1120         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1121                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1122
1123         cancel_lru_locks osc
1124
1125         # stop two OSTs, so the 2nd component of the 1st mirror and
1126         # the 1st component of the 2nd mirror will be inaccessible, ...
1127         stop_osts 2 3
1128
1129         echo "comparing files ... "
1130
1131         # however, read can still return the correct data. It should return
1132         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1133         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1134                 $DIR/$tfile-ref || error "file reading error"
1135
1136         start_osts 2 3
1137 }
1138 run_test 34b "read mirrored file with multiple components"
1139
1140 test_35() {
1141         local tf=$DIR/$tfile
1142
1143         $LFS setstripe -E eof $tf
1144
1145         # add an out-of-sync mirror to the file
1146         $LFS mirror extend -N -c 2 $tf ||
1147                 error "extending mirrored file $tf failed"
1148
1149         $MULTIOP $tf oO_WRONLY:c ||
1150                 error "write open a mirrored file failed"
1151
1152         # truncate file should return error
1153         $TRUNCATE $tf 100 || error "error truncating a mirrored file"
1154 }
1155 run_test 35 "allow to write to mirrored files"
1156
1157 verify_ost_layout_version() {
1158         local tf=$1
1159
1160         # get file layout version
1161         local flv=$($LFS getstripe $tf | awk '/lcm_layout_gen/{print $2}')
1162
1163         # layout version from OST objects
1164         local olv=$($MULTIOP $tf oXc | awk '/ostlayoutversion/{print $2}')
1165
1166         [ $flv -eq $olv ] || error "layout version mismatch: $flv vs. $olv"
1167 }
1168
1169 create_file_36() {
1170         local tf
1171
1172         for tf in "$@"; do
1173                 $LFS setstripe -E 1M -S 1M -E 2M -E 4M -E eof -c -1 $tf
1174                 $LFS setstripe -E 3M -S 1M -E 6M -E eof -c -1 $tf-tmp
1175
1176                 $LFS mirror extend -N -f $tf-tmp $tf ||
1177                         error "merging $tf-tmp into $tf failed"
1178         done
1179 }
1180
1181 test_36() {
1182         local tf=$DIR/$tfile
1183
1184         create_file_36 $tf $tf-2 $tf-3
1185
1186         [ $(get_mirror_ids $tf) -gt 1 ] || error "wrong mirror count"
1187
1188         # test case 1 - check file write and verify layout version
1189         $MULTIOP $tf oO_WRONLY:c ||
1190                 error "write open a mirrored file failed"
1191
1192         # write open file should not return error
1193         $MULTIOP $tf oO_WRONLY:w1024Yc || error "write mirrored file error"
1194
1195         # instantiate components should work
1196         dd if=/dev/zero of=$tf bs=1M count=12 || error "write file error"
1197
1198         # verify OST layout version
1199         verify_ost_layout_version $tf
1200
1201         # test case 2
1202         local mds_idx=mds$(($($LFS getstripe -M $tf-2) + 1))
1203
1204         local delay_sec=10
1205         do_facet $mds_idx $LCTL set_param fail_val=$delay_sec
1206
1207         #define OBD_FAIL_FLR_LV_DELAY 0x1A01
1208         do_facet $mds_idx $LCTL set_param fail_loc=0x1A01
1209
1210         # write should take at least $fail_loc seconds and succeed
1211         local st=$(date +%s)
1212         $MULTIOP $tf-2 oO_WRONLY:w1024Yc || error "write mirrored file error"
1213
1214         [ $(date +%s) -ge $((st+delay_sec)) ] ||
1215                 error "write finished before layout version is transmitted"
1216
1217         # verify OST layout version
1218         verify_ost_layout_version $tf
1219
1220         do_facet $mds_idx $LCTL set_param fail_loc=0
1221
1222         # test case 3
1223         mds_idx=mds$(($($LFS getstripe -M $tf-3) + 1))
1224
1225         #define OBD_FAIL_FLR_LV_INC 0x1A02
1226         do_facet $mds_idx $LCTL set_param fail_loc=0x1A02
1227
1228         # write open file should return error
1229         $MULTIOP $tf-3 oO_WRONLY:O_SYNC:w1024c &&
1230                 error "write a mirrored file succeeded" || true
1231
1232         do_facet $mds_idx $LCTL set_param fail_loc=0
1233 }
1234 run_test 36 "write to mirrored files"
1235
1236 create_files_37() {
1237         local tf
1238         local fsize=$1
1239
1240         echo "create test files with size $fsize .."
1241
1242         shift
1243         for tf in "$@"; do
1244                 $LFS setstripe -E 1M -S 1M -c 1 -E eof -c -1 $tf
1245
1246                 dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
1247                 $TRUNCATE $tf $fsize
1248         done
1249 }
1250
1251 test_37()
1252 {
1253         local tf=$DIR/$tfile
1254         local tf2=$DIR/$tfile-2
1255         local tf3=$DIR/$tfile-3
1256
1257         create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
1258
1259         # assume the mirror id will be 1, 2, and 3
1260         declare -A checksums
1261         checksums[1]=$(md5sum $tf | cut -f 1 -d' ')
1262         checksums[2]=$(md5sum $tf2 | cut -f 1 -d' ')
1263         checksums[3]=$(md5sum $tf3 | cut -f 1 -d' ')
1264
1265         printf '%s\n' "${checksums[@]}"
1266
1267         # merge these files into a mirrored file
1268         $LFS mirror extend --no-verify -N -f $tf2 $tf ||
1269                 error "merging $tf2 into $tf failed"
1270         $LFS mirror extend --no-verify -N -f $tf3 $tf ||
1271                 error "merging $tf3 into $tf failed"
1272
1273         get_mirror_ids $tf
1274
1275         # verify mirror read, checksums should equal to the original files'
1276         echo "Verifying mirror read .."
1277
1278         local sum
1279         for i in ${mirror_array[@]}; do
1280                 sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
1281                 [ "$sum" = "${checksums[$i]}" ] ||
1282                         error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
1283         done
1284
1285         # verify mirror copy, write to this mirrored file will invalidate
1286         # the other two mirrors
1287         echo "Verifying mirror copy .."
1288
1289         local osts=$(comma_list $(osts_nodes))
1290
1291         # define OBD_FAIL_OST_SKIP_LV_CHECK     0x241
1292         do_nodes $osts lctl set_param fail_loc=0x241
1293
1294         mirror_io copy -i ${mirror_array[0]} \
1295                 -t $(echo ${mirror_array[@]:1} | tr ' ' ',') $tf ||
1296                         error "mirror copy error"
1297
1298         do_nodes $osts lctl set_param fail_loc=0
1299
1300         # verify copying is successful by checking checksums
1301         remount_client $MOUNT
1302         for i in ${mirror_array[@]}; do
1303                 sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
1304                 [ "$sum" = "${checksums[1]}" ] ||
1305                         error "$i: mismatch checksum after copy"
1306         done
1307
1308         rm -f $tf
1309 }
1310 run_test 37 "mirror I/O API verification"
1311
1312 verify_flr_state()
1313 {
1314         local tf=$1
1315         local expected_state=$2
1316
1317         local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
1318         [ $expected_state = $state ] ||
1319                 error "expected: $expected_state, actual $state"
1320 }
1321
1322 test_38() {
1323         local tf=$DIR/$tfile
1324         local ref=$DIR/${tfile}-ref
1325
1326         $LFS setstripe -E 1M -S 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf ||
1327                 error "creating $tf failed"
1328         $LFS setstripe -E 2M -S 1M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 \
1329                 $tf-2 || error "creating $tf-2 failed"
1330         $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3 ||
1331                 error "creating $tf-3 failed"
1332
1333         # instantiate all components
1334         $LFS mirror extend -N -f $tf-2 $tf ||
1335                 error "merging $tf-2 into $tf failed"
1336         $LFS mirror extend -N -f $tf-3 $tf ||
1337                 error "merging $tf-3 into $tf failed"
1338         $LFS mirror extend -N -c 1 $tf ||
1339                 error "extending mirrored file $tf failed"
1340
1341         verify_flr_state $tf "ro"
1342
1343         dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
1344
1345         local fsize=$((RANDOM << 8 + 1048576))
1346         $TRUNCATE $ref $fsize
1347
1348         local ref_cksum=$(md5sum $ref | cut -f 1 -d' ')
1349
1350         # case 1: verify write to mirrored file & resync work
1351         cp $ref $tf || error "copy from $ref to $f error"
1352         verify_flr_state $tf "wp"
1353
1354         local file_cksum=$(md5sum $tf | cut -f 1 -d' ')
1355         [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
1356
1357         get_mirror_ids $tf
1358         echo "mirror IDs: ${mirror_array[@]}"
1359
1360         local valid_mirror stale_mirror id mirror_cksum
1361         for id in "${mirror_array[@]}"; do
1362                 mirror_cksum=$(mirror_io dump -i $id $tf |
1363                                 md5sum | cut -f 1 -d' ')
1364                 [ "$ref_cksum" == "$mirror_cksum" ] &&
1365                         { valid_mirror=$id; continue; }
1366
1367                 stale_mirror=$id
1368         done
1369
1370         [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
1371         [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
1372
1373         mirror_io resync $tf || error "resync failed"
1374         verify_flr_state $tf "ro"
1375
1376         mirror_cksum=$(mirror_io dump -i $stale_mirror $tf |
1377                         md5sum | cut -f 1 -d' ')
1378         [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
1379
1380         # case 2: inject an error to make mirror_io exit after changing
1381         # the file state to sync_pending so that we can start a concurrent
1382         # write.
1383         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1384         verify_flr_state $tf "wp"
1385
1386         mirror_io resync -e resync_start $tf && error "resync succeeded"
1387         verify_flr_state $tf "sp"
1388
1389         # from sync_pending to write_pending
1390         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1391         verify_flr_state $tf "wp"
1392
1393         mirror_io resync -e resync_start $tf && error "resync succeeded"
1394         verify_flr_state $tf "sp"
1395
1396         # from sync_pending to read_only
1397         mirror_io resync $tf || error "resync failed"
1398         verify_flr_state $tf "ro"
1399 }
1400 run_test 38 "resync"
1401
1402 test_39() {
1403         local tf=$DIR/$tfile
1404
1405         rm -f $tf
1406         $LFS mirror create -N2 -E1m -c1 -S1M -E-1 $tf ||
1407         error "create PFL file $tf failed"
1408
1409         verify_mirror_count $tf 2
1410         verify_comp_count $tf 4
1411
1412         rm -f $tf || error "delete $tf failed"
1413 }
1414 run_test 39 "check FLR+PFL (a.k.a. PFLR) creation"
1415
1416 test_40() {
1417         local tf=$DIR/$tfile
1418         local ops
1419
1420         for ops in "conv=notrunc" ""; do
1421                 rm -f $tf
1422
1423                 $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 --flags=prefer \
1424                                    -N -E 1M -E 2M -E 4M -E -1 $tf ||
1425                         error "create PFLR file $tf failed"
1426                 dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
1427                         error "write PFLR file $tf failed"
1428
1429                 lfs getstripe -vy $tf
1430
1431                 local flags
1432
1433                 # file mirror state should be write_pending
1434                 flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1435                 [ $flags = wp ] ||
1436                 error "file mirror state $flags"
1437                 # the 1st component (in mirror 1) should be inited
1438                 verify_comp_attr lcme_flags $tf 0x10001 init
1439                 # the 2nd component (in mirror 1) should be inited
1440                 verify_comp_attr lcme_flags $tf 0x10002 init
1441                 # the 3rd component (in mirror 1) should be uninited
1442                 verify_comp_attr lcme_flags $tf 0x10003 prefer
1443                 # the 4th component (in mirror 2) should be inited
1444                 verify_comp_attr lcme_flags $tf 0x20004 init
1445                 # the 5th component (in mirror 2) should be uninited
1446                 verify_comp_attr lcme_flags $tf 0x20005 0
1447                 # the 6th component (in mirror 2) should be stale
1448                 verify_comp_attr lcme_flags $tf 0x20006 stale
1449                 # the 7th component (in mirror 2) should be uninited
1450                 if [[ x$ops = "xconv=notrunc" ]]; then
1451                         verify_comp_attr lcme_flags $tf 0x20007 0
1452                 elif [[ x$ops = "x" ]]; then
1453                         verify_comp_attr lcme_flags $tf 0x20007 stale
1454                 fi
1455         done
1456
1457         rm -f $tf || error "delete $tf failed"
1458 }
1459 run_test 40 "PFLR rdonly state instantiation check"
1460
1461 test_41() {
1462         local tf=$DIR/$tfile
1463
1464         rm -f $tf $tf-1
1465         echo " **create two FLR files $tf $tf-1"
1466         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1467                            -N -E 1M -E 2M -E 3M -E -1 $tf ||
1468                 error "create PFLR file $tf failed"
1469         $LFS mirror create -N -E 2M -S 1M -E eof \
1470                            -N -E 1M -E eof --flags prefer \
1471                            -N -E 4m -E eof $tf-1 ||
1472                 error "create PFLR file $tf-1 failed"
1473
1474         # file should be in ro status
1475         echo " **verify files be RDONLY"
1476         verify_flr_state $tf "ro"
1477         verify_flr_state $tf-1 "ro"
1478
1479         # write data in [0, 2M)
1480         dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
1481                 error "writing $tf failed"
1482         dd if=/dev/zero of=$tf-1 bs=1M count=4 conv=notrunc ||
1483                 error "writing $tf-1 failed"
1484
1485         echo " **verify files be WRITE_PENDING"
1486         verify_flr_state $tf "wp"
1487         verify_flr_state $tf-1 "wp"
1488
1489         # file should have stale component
1490         echo " **verify files have stale component"
1491         $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
1492                 error "after writing $tf, it does not contain stale component"
1493         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1494                 error "after writing $tf-1, it does not contain stale component"
1495
1496         echo " **full resync"
1497         $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
1498
1499         echo " **verify files be RDONLY"
1500         verify_flr_state $tf "ro"
1501         verify_flr_state $tf-1 "ro"
1502
1503         # file should not have stale component
1504         echo " **verify files do not contain stale component"
1505         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1506                 error "after resyncing $tf, it contains stale component"
1507         $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
1508                 error "after resyncing $tf, it contains stale component"
1509
1510         # verify partial resync
1511         echo " **write $tf-1 for partial resync test"
1512         dd if=/dev/zero of=$tf-1 bs=1M count=2 conv=notrunc ||
1513                 error "writing $tf-1 failed"
1514
1515         echo " **only resync mirror 2"
1516         verify_flr_state $tf-1 "wp"
1517         $LFS mirror resync --only 2 $tf-1 ||
1518                 error "resync mirror 2 of $tf-1 failed"
1519         verify_flr_state $tf "ro"
1520
1521         # resync synced mirror
1522         echo " **resync mirror 2 again"
1523         $LFS mirror resync --only 2 $tf-1 ||
1524                 error "resync mirror 2 of $tf-1 failed"
1525         verify_flr_state $tf "ro"
1526         echo " **verify $tf-1 contains stale component"
1527         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1528                 error "after writing $tf-1, it does not contain stale component"
1529
1530         echo " **full resync $tf-1"
1531         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1532         verify_flr_state $tf "ro"
1533         echo " **full resync $tf-1 again"
1534         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1535         echo " **verify $tf-1 does not contain stale component"
1536         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1537                 error "after resyncing $tf, it contains stale component"
1538
1539         return 0
1540 }
1541 run_test 41 "lfs mirror resync check"
1542
1543 test_42() {
1544         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1545
1546         local td=$DIR/$tdir
1547         local tf=$td/$tfile
1548         local mirror_cmd="$LFS mirror verify"
1549         local i
1550
1551         # create parent directory
1552         mkdir $td || error "mkdir $td failed"
1553
1554         $mirror_cmd &> /dev/null && error "no file name given"
1555         $mirror_cmd $tf &> /dev/null && error "cannot stat file $tf"
1556         $mirror_cmd $td &> /dev/null && error "$td is not a regular file"
1557
1558         # create mirrored files
1559         $LFS mirror create -N -E 4M -S 1M -E 10M -E EOF $tf ||
1560                 error "create mirrored file $tf failed"
1561         $LFS mirror create -N -E 2M -S 1M -E EOF \
1562                            -N -E 6M -E 8M -E EOF \
1563                            -N -E 16M -E EOF $tf-1 ||
1564                 error "create mirrored file $tf-1 failed"
1565         $LFS mirror create -N -c 2 -o 1,3 -N -S 2M -c -1 $tf-2 ||
1566                 error "create mirrored file $tf-2 failed"
1567
1568         # write data in [0, 10M)
1569         for i in $tf $tf-1 $tf-2; do
1570                 yes | dd of=$i bs=1M count=10 iflag=fullblock conv=notrunc ||
1571                         error "write $i failed"
1572         done
1573
1574         # resync the mirrored files
1575         $LFS mirror resync $tf-1 $tf-2 ||
1576                 error "resync $tf-1 $tf-2 failed"
1577
1578         # verify the mirrored files
1579         $mirror_cmd $tf-1 $tf-2 ||
1580                 error "verify $tf-1 $tf-2 failed"
1581
1582         get_mirror_ids $tf-1
1583         $mirror_cmd --only ${mirror_array[0]} $tf-1 &> /dev/null &&
1584                 error "at least 2 mirror ids needed with '--only' option"
1585         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 $tf-2 \
1586                 &> /dev/null &&
1587                 error "'--only' option cannot be used upon multiple files"
1588         $mirror_cmd --only 65534,${mirror_array[0]},65535 $tf-1 &&
1589                 error "invalid specified mirror ids"
1590
1591         # change the content of $tf and merge it into $tf-1
1592         for i in 6 10; do
1593                 echo a | dd of=$tf bs=1M seek=$i conv=notrunc ||
1594                         error "change $tf with seek=$i failed"
1595                 echo b | dd of=$tf-1 bs=1M seek=$i conv=notrunc ||
1596                         error "change $tf-1 with seek=$i failed"
1597         done
1598
1599         $LFS mirror resync $tf-1 || error "resync $tf-1 failed"
1600         $LFS mirror extend --no-verify -N -f $tf $tf-1 ||
1601                 error "merge $tf into $tf-1 failed"
1602
1603         # verify the mirrored files
1604         echo "Verify $tf-1 without -v option:"
1605         $mirror_cmd $tf-1 &&
1606                 error "verify $tf-1 should fail" || echo "PASS"
1607
1608         echo "Verify $tf-1 with -v option:"
1609         $mirror_cmd -v $tf-1 &&
1610                 error "verify $tf-1 should fail"
1611
1612         get_mirror_ids $tf-1
1613         echo "Verify $tf-1 with --only option:"
1614         $mirror_cmd -v --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 &&
1615                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1616                       "${mirror_array[-1]} should fail"
1617
1618         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 ||
1619                 error "verify $tf-1 with mirror ${mirror_array[0]} and" \
1620                       "${mirror_array[1]} should succeed"
1621
1622         # set stale components in $tf-1
1623         for i in 0x40002 0x40003; do
1624                 $LFS setstripe --comp-set -I$i --comp-flags=stale $tf-1 ||
1625                         error "set stale flag on component $i failed"
1626         done
1627
1628         # verify the mirrored file
1629         echo "Verify $tf-1 with stale components:"
1630         $mirror_cmd -vvv $tf-1 ||
1631                 error "verify $tf-1 with stale components should succeed"
1632
1633         echo "Verify $tf-1 with stale components and --only option:"
1634         $mirror_cmd -vvv --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 ||
1635                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1636                       "${mirror_array[-1]} should succeed"
1637 }
1638 run_test 42 "lfs mirror verify"
1639
1640 # inactivate one OST && write && restore the OST
1641 write_file_43() {
1642         local file=$1
1643         local ost=$2
1644         local PARAM="osp.${FSNAME}-OST000${ost}-osc-M*.active"
1645         local wait
1646
1647         wait=$(do_facet $SINGLEMDS \
1648                 "$LCTL get_param -n lod.*MDT0000-*.qos_maxage")
1649         wait=${wait%%[^0-9]*}
1650
1651         echo "  **deactivate OST$ost, waiting for $((wait*2+2)) seconds"
1652         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 0")
1653         # lod_qos_statfs_update needs 2*$wait seconds to refresh targets statfs
1654         sleep $(($wait * 2 + 2))
1655         echo "  **write $file"
1656         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
1657         echo "  **restore activating OST$ost, waiting for $((wait*2+2)) seconds"
1658         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 1")
1659         sleep $((wait * 2 + 2))
1660
1661         local flags=$($LFS getstripe -v $file | awk '/lcm_flags:/ { print $2 }')
1662         [ $flags = wp ] || error "file mirror state $flags != wp"
1663 }
1664
1665 test_43() {
1666         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
1667
1668         local tf=$DIR/$tfile
1669         local flags
1670
1671         rm -f $tf
1672         ##   mirror 0  ost (0, 1)
1673         ##   mirror 1  ost (1, 2)
1674         ##   mirror 2  ost (2, 0)
1675         $LFS mirror create -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 \
1676                 -N -Eeof -c2 -o2,0 $tf ||
1677                 error "create 3 mirrors file $tf failed"
1678
1679         ################## OST0 ###########################################
1680         write_file_43 $tf 0
1681         echo "  **verify components"
1682         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1683         verify_comp_attr lcme_flags $tf 0x20002 init
1684         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1685
1686         # resync
1687         echo "  **resync $tf"
1688         $LFS mirror resync $tf
1689         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1690         [ $flags = ro ] || error "file mirror state $flags != ro"
1691
1692         ################## OST1 ###########################################
1693         write_file_43 $tf 1
1694         echo "  **verify components"
1695         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1696         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1697         verify_comp_attr lcme_flags $tf 0x30003 init
1698
1699         # resync
1700         echo "  **resync $tf"
1701         $LFS mirror resync $tf
1702         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1703         [ $flags = ro ] || error "file mirror state $flags != ro"
1704
1705         ################## OST2 ###########################################
1706         write_file_43 $tf 2
1707         echo "  **verify components"
1708         verify_comp_attr lcme_flags $tf 0x10001 init
1709         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1710         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1711 }
1712 run_test 43 "mirror pick on write"
1713
1714 test_44() {
1715         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1716         rm -rf $DIR/$tdir
1717         rm -rf $DIR/$tdir-1
1718         local tf=$DIR/$tdir/$tfile
1719         local tf1=$DIR/$tdir-1/$tfile-1
1720
1721         $LFS setdirstripe -i 0 -c 1 $DIR/$tdir ||
1722                 error "create directory failed"
1723         $LFS setdirstripe -i 1 -c 1 $DIR/$tdir-1 ||
1724                 error "create remote directory failed"
1725         rm -f $tf $tf1 $tf.mirror~2
1726         # create file with 4 mirrors
1727         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1728                            -N -E 1M -E 2M -E 3M -E -1 -N2 $tf ||
1729                 error "create PFLR file $tf failed"
1730
1731         # file should be in ro status
1732         verify_flr_state $tf "ro"
1733
1734         # write data in [0, 3M)
1735         dd if=/dev/urandom of=$tf bs=1M count=3 conv=notrunc ||
1736                 error "writing $tf failed"
1737
1738         verify_flr_state $tf "wp"
1739
1740         # synchronize all mirrors of the file
1741         $LFS mirror resync $tf || error "mirror resync $tf failed"
1742
1743         verify_flr_state $tf "ro"
1744
1745         # split mirror 1
1746         $LFS mirror split --mirror-id 1 -f $tf1 $tf ||
1747                 error "split to $tf1 failed"
1748
1749         local idx0=$($LFS getstripe -m $tf)
1750         local idx1=$($LFS getstripe -m $tf1)
1751
1752         [[ x$idx0 == x0 ]] || error "$tf is not on MDT0"
1753         [[ x$idx1 == x1 ]] || error "$tf1 is not on MDT1"
1754
1755         # verify mirror count
1756         verify_mirror_count $tf 3
1757         verify_mirror_count $tf1 1
1758
1759         $LFS mirror split --mirror-id 2 $tf ||
1760                 error "split mirror 2 failed"
1761
1762         verify_mirror_count $tf 2
1763         verify_mirror_count $tf.mirror~2 1
1764
1765         $LFS mirror split --mirror-id 3 -d $tf ||
1766                 error "split and delte mirror 3 failed"
1767         verify_mirror_count $tf 1
1768
1769         # verify splitted file contains the same content as the orig file does
1770         diff $tf $tf1 || error "splited file $tf1 diffs from $tf"
1771         diff $tf $tf.mirror~2 ||
1772                 error "splited file $tf.mirror~2 diffs from $tf"
1773 }
1774 run_test 44 "lfs mirror split check"
1775
1776 test_45() {
1777         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1778
1779         local file=$DIR/$tdir/$tfile
1780         local dir=$DIR/$tdir/$dir
1781         local temp=$DIR/$tdir/template
1782         rm -rf $DIR/$tdir
1783         test_mkdir $DIR/$tdir
1784
1785         $LFS setstripe -N -E1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
1786                 -N -E3m -Eeof -N -E8m -Eeof $file || error "Create $file failed"
1787
1788         echo "getstripe --yaml $file"
1789         $LFS getstripe --yaml $file > $temp || error "getstripe $file failed"
1790         echo "setstripe --yaml=$temp $file.2"
1791         $LFS setstripe --yaml=$temp $file.2 || error "setstripe $file.2 failed"
1792
1793         echo "compare layout"
1794         local layout1=$(get_layout_param $file)
1795         local layout2=$(get_layout_param $file.2)
1796         [ "$layout1" == "$layout2" ] ||
1797                 error "FLR file $file/$file.2 layouts are not equal"
1798 }
1799 run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
1800
1801 verify_46() {
1802         local src=$1
1803         local dst=$2
1804         local msg_prefix=$3
1805
1806         $LFS setstripe --copy=$src $dst || error "setstripe $dst failed"
1807
1808         local layout1=$(get_layout_param $src)
1809         local layout2=$(get_layout_param $dst)
1810         # compare their layout info
1811         [ "$layout1" == "$layout2" ] ||
1812                 error "$msg_prefix $src <=> $dst layouts are not equal"
1813 }
1814
1815 test_46() {
1816         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1817
1818         local file=$DIR/$tdir/$tfile
1819         test_mkdir $DIR/$tdir
1820
1821         ########################### 1. PFL file #############################
1822         echo "  ** 1. PFL file"
1823         rm -f $file
1824         $LFS setstripe -E1m -c2 -o0,1 -E2m -c2 -E3m -o1,0 -E4m -c1 -E-1 $file ||
1825                 error "1. Create PFL $file failed"
1826
1827         rm -f $file.copy
1828         verify_46 $file $file.copy "1. PFL file"
1829
1830         ########################### 2. plain file ###########################
1831         echo "  ** 2. plain file"
1832         rm -f $file
1833         $LFS setstripe -c2 -o0,1 -i1 $file ||
1834                 error "2. Create plain $file failed"
1835
1836         rm -f $file.copy
1837         verify_46 $file $file.copy "2. plain file"
1838
1839         ########################### 3. FLR file #############################
1840         echo "  ** 3. FLR file"
1841         rm -f $file
1842         $LFS setstripe -N -E1m -c2 -o0,1 -E4m -c1 -Eeof -N -E16m -Eeof $file ||
1843                 error "3. Create FLR $file failed"
1844
1845         rm -f $file.copy
1846         verify_46 $file $file.copy "3. FLR file"
1847
1848         local dir=$DIR/$tdir/dir
1849         ########################### 4. PFL dir ##############################
1850         echo "  ** 4. PFL dir"
1851         test_mkdir $dir
1852         $LFS setstripe -E1m -c2 -E2m -c1 -E-1 $dir ||
1853                 error "4. setstripe PFL $dir failed"
1854
1855         test_mkdir $dir.copy
1856         verify_46 $dir $dir.copy "3. PFL dir"
1857
1858         ########################### 5. plain dir ############################
1859         echo "  ** 5. plain dir"
1860         $LFS setstripe -c2 -i-1 $dir || error "4. setstripe plain $dir failed"
1861
1862         verify_46 $dir $dir.copy "5. plain dir"
1863
1864         ########################### 6. FLR dir ##############################
1865         echo "  ** 6. FLR dir"
1866         $LFS setstripe -N -E1m -c2 -E2m -c1 -Eeof -N -E4m -Eeof $dir ||
1867                 error "6. setstripe FLR $dir failed"
1868
1869         verify_46 $dir $dir.copy "6. FLR dir"
1870 }
1871 run_test 46 "Verify setstripe --copy option"
1872
1873 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
1874 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
1875
1876 write_file_200() {
1877         local tf=$1
1878
1879         local fsize=$(stat --printf=%s $tf)
1880
1881         while [ -f $ctrl_file ]; do
1882                 local off=$((RANDOM << 8))
1883                 local len=$((RANDOM << 5 + 131072))
1884
1885                 [ $((off + len)) -gt $fsize ] && {
1886                         fsize=$((off + len))
1887                         echo "Extending file size to $fsize .."
1888                 }
1889
1890                 flock -s $lock_file -c \
1891                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
1892                                 { rm -f $ctrl_file;
1893                                   error "failed writing to $off:$len"; }
1894                 sleep 0.$((RANDOM % 2 + 1))
1895         done
1896 }
1897
1898 read_file_200() {
1899         local tf=$1
1900
1901         while [ -f $ctrl_file ]; do
1902                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
1903                         { rm -f $ctrl_file; error "read failed"; }
1904                 sleep 0.$((RANDOM % 2 + 1))
1905         done
1906 }
1907
1908 resync_file_200() {
1909         local tf=$1
1910
1911         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
1912
1913         exec 200<>$lock_file
1914         while [ -f $ctrl_file ]; do
1915                 local lock_taken=false
1916                 local index=$((RANDOM % ${#options[@]}))
1917                 local cmd="mirror_io resync ${options[$index]}"
1918
1919                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
1920
1921                 [ $((RANDOM % 4)) -eq 0 ] && {
1922                         index=0
1923                         lock_taken=true
1924                         echo -n "lock to "
1925                 }
1926
1927                 echo -n "resync file $tf with '$cmd' .."
1928
1929                 $lock_taken && flock -x 200
1930                 $cmd $tf &> /dev/null && echo "done" || echo "failed"
1931                 $lock_taken && flock -u 200
1932
1933                 sleep 0.$((RANDOM % 8 + 1))
1934         done
1935 }
1936
1937 test_200() {
1938         local tf=$DIR/$tfile
1939         local tf2=$DIR2/$tfile
1940         local tf3=$DIR3/$tfile
1941
1942         $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
1943         $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
1944         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
1945
1946         $LFS mirror extend -N -f $tf-2 $tf ||
1947                 error "merging $tf-2 into $tf failed"
1948         $LFS mirror extend -N -f $tf-3 $tf ||
1949                 error "merging $tf-3 into $tf failed"
1950
1951         mkdir -p $MOUNT2 && mount_client $MOUNT2
1952
1953         mkdir -p $MOUNT3 && mount_client $MOUNT3
1954
1955         verify_flr_state $tf3 "ro"
1956
1957         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
1958         $LCTL set_param fail_loc=0x1A03
1959
1960         local mds_idx=mds$(($($LFS getstripe -M $tf) + 1))
1961         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
1962
1963         declare -a pids
1964
1965         write_file_200 $tf &
1966         pids+=($!)
1967
1968         read_file_200 $tf &
1969         pids+=($!)
1970
1971         write_file_200 $tf2 &
1972         pids+=($!)
1973
1974         read_file_200 $tf2 &
1975         pids+=($!)
1976
1977         resync_file_200 $tf3 &
1978         pids+=($!)
1979
1980         local sleep_time=60
1981         [ "$SLOW" = "yes" ] && sleep_time=360
1982         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
1983                 sleep 1
1984                 ((--sleep_time))
1985         done
1986
1987         rm -f $ctrl_file
1988
1989         echo "Waiting ${pids[@]}"
1990         wait ${pids[@]}
1991
1992         umount_client $MOUNT2
1993         umount_client $MOUNT3
1994
1995         rm -f $lock_file
1996
1997         # resync and verify mirrors
1998         mirror_io resync $tf
1999         get_mirror_ids $tf
2000
2001         local csum=$(mirror_io dump -i ${mirror_array[0]} $tf | md5sum)
2002         for id in ${mirror_array[@]:1}; do
2003                 [ "$(mirror_io dump -i $id $tf | md5sum)" = "$csum" ] ||
2004                         error "checksum error for mirror $id"
2005         done
2006
2007         true
2008 }
2009 run_test 200 "stress test"
2010
2011 cleanup_test_201() {
2012         trap 0
2013         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
2014
2015         umount_client $MOUNT2
2016 }
2017
2018 test_201() {
2019         local delay=${RESYNC_DELAY:-5}
2020
2021         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
2022                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
2023
2024         trap cleanup_test_201 EXIT
2025
2026         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
2027                         changelog_register -n)
2028
2029         mkdir -p $MOUNT2 && mount_client $MOUNT2
2030
2031         local index=0
2032         while :; do
2033                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
2034                 [ -z "$log" ] && { sleep 1; continue; }
2035
2036                 index=$(echo $log | awk '{print $1}')
2037                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
2038                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
2039                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
2040
2041                 ((++index))
2042                 [ -z "$file" ] && continue
2043
2044                 local now=$(date +%s)
2045
2046                 echo "file: $file $fid was modified at $ts, now: $now, " \
2047                      "will be resynced at $((ts+delay))"
2048
2049                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
2050
2051                 mirror_io resync $file
2052                 echo "$file resync done"
2053         done
2054
2055         cleanup_test_201
2056 }
2057 run_test 201 "FLR data mover"
2058
2059 test_202() {
2060         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
2061
2062         local tf=$DIR/$tfile
2063         local ids
2064
2065         $LFS setstripe -E 1M -S 1M -c 1 $tf
2066         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2067         verify_comp_attr stripe-count $tf ${ids[0]} 1
2068
2069         $LFS setstripe --component-add -E 2M -c -1 $tf
2070         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2071         verify_comp_attr stripe-count $tf ${ids[0]} 1
2072         verify_comp_attr stripe-count $tf ${ids[1]} -1
2073
2074         dd if=/dev/zero of=$tf bs=1M count=2
2075         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2076         verify_comp_attr stripe-count $tf ${ids[0]} 1
2077         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
2078 }
2079 run_test 202 "lfs setstripe --add-component wide striping"
2080
2081 complete $SECONDS
2082 check_and_cleanup_lustre
2083 exit_status