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