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