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