Whamcloud - gitweb
LU-15626 tests: Fix "error" reported by shellcheck for sanity-flr.sh
[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-14818 LU-11381
18 ALWAYS_EXCEPT+="                  6        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
382         # verify component ${ids[2]}
383         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
384         verify_comp_attr stripe-count $tf ${ids[2]} 2
385         verify_comp_attr pool $tf ${ids[2]} flash
386
387         # verify component ${ids[3]}
388         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
389         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
390         verify_comp_attr pool $tf ${ids[3]} flash
391
392         # verify component ${ids[4]}
393         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
394         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
395         verify_comp_attr pool $tf ${ids[4]} archive
396
397         # verify component ${ids[5]}
398         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
399         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
400         verify_comp_attr_with_parent pool $tf ${ids[5]}
401
402         if [ $MDS1_VERSION -ge $(version_code 2.12.55) ]; then
403                 # LU-11022 - remove mirror by pool name
404                 local=cnt cnt=$($LFS getstripe $tf | grep archive | wc -l)
405                 [ "$cnt" != "1" ] && error "unexpected mirror count $cnt"
406                 $LFS mirror delete --pool archive $tf || error "delete mirror"
407                 cnt=$($LFS getstripe $tf | grep archive | wc -l)
408                 [ "$cnt" != "0" ] && error "mirror count after removal: $cnt"
409         fi
410
411         # destroy OST pool
412         destroy_test_pools
413 }
414 run_test 0b "lfs mirror create plain layout mirrors"
415
416 test_0c() {
417         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
418
419         local td=$DIR/$tdir
420         local tf=$td/$tfile
421         local mirror_cmd="$LFS mirror create"
422         local ids
423         local i
424
425         # create a new OST pool
426         local pool_name=$TESTNAME
427         create_pool $FSNAME.$pool_name ||
428                 error "create OST pool $pool_name failed"
429
430         # add OSTs into the pool
431         pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
432                 error "add OSTs into pool $pool_name failed"
433
434         # create parent directory
435         mkdir $td || error "mkdir $td failed"
436         $LFS setstripe -E 32M -S 8M -c -1 -p $pool_name -E eof -S 16M $td ||
437                 error "$LFS setstripe $td failed"
438
439         # create a mirrored file with composite layout mirrors
440         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
441                     -N -c 4 -p none \
442                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
443                 error "create mirrored file $tf failed"
444         verify_mirror_count $tf 6
445         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
446
447         # verify components ${ids[0]} and ${ids[2]}
448         for i in 0 2; do
449                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
450                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
451                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
452                 verify_comp_extent $tf ${ids[$i]} 0 4194304
453         done
454
455         # verify components ${ids[1]} and ${ids[3]}
456         for i in 1 3; do
457                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
458                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
459                 verify_comp_attr pool $tf ${ids[$i]} flash
460                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
461         done
462
463         # verify component ${ids[4]}
464         verify_comp_attr stripe-size $tf ${ids[4]} 4194304
465         verify_comp_attr stripe-count $tf ${ids[4]} 4
466         verify_comp_attr_with_parent pool $tf ${ids[4]}
467         verify_comp_extent $tf ${ids[4]} 0 EOF
468
469         # verify components ${ids[5]}, ${ids[7]} and ${ids[9]}
470         for i in 5 7 9; do
471                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
472                 verify_comp_attr stripe-count $tf ${ids[$i]} 4
473                 verify_comp_attr pool $tf ${ids[$i]} archive
474                 verify_comp_extent $tf ${ids[$i]} 0 536870912
475         done
476
477         # verify components ${ids[6]}, ${ids[8]} and ${ids[10]}
478         for i in 6 8 10; do
479                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
480                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
481                 verify_comp_attr pool $tf ${ids[$i]} archive
482                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
483         done
484
485         # destroy OST pool
486         destroy_test_pools
487 }
488 run_test 0c "lfs mirror create composite layout mirrors"
489
490 test_0d() {
491         local td=$DIR/$tdir
492         local tf=$td/$tfile
493         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
494         local mirror_cmd="$LFS mirror extend"
495         local ids
496         local i
497
498         # create parent directory
499         mkdir $td || error "mkdir $td failed"
500
501         $mirror_cmd $tf &> /dev/null && error "miss -N option"
502         $mirror_cmd -N $tf &> /dev/null && error "$tf does not exist"
503
504         # create a non-mirrored file, convert it to a mirrored file and extend
505         touch $tf || error "touch $tf failed"
506         $mirror_cmd -N $tf || error "convert and extend $tf failed"
507         verify_mirror_count $tf 2
508         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
509         for ((i = 0; i < 2; i++)); do
510                 verify_comp_attrs $tf ${ids[$i]}
511                 verify_comp_extent $tf ${ids[$i]} 0 EOF
512         done
513
514         lfsck_verify_pfid $tf || error "PFID is not set"
515
516         # create a mirrored file and extend it
517         $LFS mirror create -N $tf-1 || error "create mirrored file $tf-1 failed"
518         $LFS mirror create -N $tf-2 || error "create mirrored file $tf-2 failed"
519         $LFS mirror create -N $tf-3 || error "create mirrored file $tf-3 failed"
520
521         $mirror_cmd -N -S 4M -N -f $tf-2 $tf-1 ||
522                 error "extend mirror with -f failed"
523
524         $mirror_cmd -N$((mirror_count - 1)) $tf-3 ||
525                 error "extend mirrored file $tf-3 failed"
526         verify_mirror_count $tf-3 $mirror_count
527         ids=($($LFS getstripe $tf-3 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
528         for ((i = 0; i < $mirror_count; i++)); do
529                 verify_comp_attrs $tf-3 ${ids[$i]}
530                 verify_comp_extent $tf-3 ${ids[$i]} 0 EOF
531         done
532
533         $mirror_cmd -N $tf-3 &> /dev/null
534         rc=$?
535         [[ $rc == 34 ]] ||
536                 error "exceeded maximum mirror count returns $rc not ERANGE(34)"
537 }
538 run_test 0d "lfs mirror extend with -N option"
539
540 test_0e() {
541         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
542
543         local td=$DIR/$tdir
544         local tf=$td/$tfile
545         local mirror_cmd="$LFS mirror extend"
546         local ids
547         local i
548
549         # create parent directory
550         mkdir $td || error "mkdir $td failed"
551
552         # create a mirrored file with plain layout mirrors
553         $LFS mirror create -N -S 32M -c 3 -p ssd -i 1 -o 1,2,3 $tf ||
554                 error "create mirrored file $tf failed"
555
556         # extend the mirrored file with plain layout mirrors
557         $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
558                     -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
559                 error "extend mirrored file $tf failed"
560         verify_mirror_count $tf 6
561         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
562         for ((i = 0; i < 6; i++)); do
563                 verify_comp_extent $tf ${ids[$i]} 0 EOF
564         done
565
566         # verify component ${ids[0]}
567         verify_comp_attr stripe-size $tf ${ids[0]} 33554432
568         verify_comp_attr stripe-count $tf ${ids[0]} 3
569         verify_comp_attr stripe-index $tf ${ids[0]} 1
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
576         # verify component ${ids[2]}
577         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
578         verify_comp_attr stripe-count $tf ${ids[2]} 2
579         verify_comp_attr pool $tf ${ids[2]} flash
580
581         # verify component ${ids[3]}
582         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
583         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
584         verify_comp_attr pool $tf ${ids[3]} flash
585
586         # verify component ${ids[4]}
587         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
588         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
589         verify_comp_attr pool $tf ${ids[4]} archive
590
591         # verify component ${ids[5]}
592         verify_comp_attr stripe-size $tf ${ids[5]} 16777216
593         verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
594         verify_comp_attr_with_parent pool $tf ${ids[5]}
595 }
596 run_test 0e "lfs mirror extend plain layout mirrors"
597
598 test_0f() {
599         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
600
601         local td=$DIR/$tdir
602         local tf=$td/$tfile
603         local mirror_cmd="$LFS mirror extend"
604         local ids
605         local i
606
607         # create parent directory
608         mkdir $td || error "mkdir $td failed"
609
610         # create a mirrored file with composite layout mirror
611         $LFS mirror create -N -E 32M -S 16M -p ssd -E eof -S 32M $tf ||
612                 error "create mirrored file $tf failed"
613
614         # extend the mirrored file with composite layout mirrors
615         $mirror_cmd -N -p archive \
616                     -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
617                     -N -c -1 -p none \
618                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
619                 error "extend mirrored file $tf failed"
620         verify_mirror_count $tf 8
621         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
622
623         # verify component ${ids[0]}
624         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
625         verify_comp_attr_with_default stripe-count $tf ${ids[0]}
626         verify_comp_attr pool $tf ${ids[0]} ssd
627         verify_comp_extent $tf ${ids[0]} 0 33554432
628
629         # verify component ${ids[1]}
630         verify_comp_attr stripe-size $tf ${ids[1]} 33554432
631         verify_comp_attr_with_default stripe-count $tf ${ids[1]}
632         verify_comp_attr pool $tf ${ids[1]} ssd
633         verify_comp_extent $tf ${ids[1]} 33554432 EOF
634
635         # verify component ${ids[2]}
636         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
637         verify_comp_attr_with_default stripe-count $tf ${ids[2]}
638         verify_comp_attr pool $tf ${ids[2]} archive
639         verify_comp_extent $tf ${ids[2]} 0 EOF
640
641         # verify components ${ids[3]} and ${ids[5]}
642         for i in 3 5; do
643                 verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
644                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
645                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
646                 verify_comp_extent $tf ${ids[$i]} 0 4194304
647         done
648
649         # verify components ${ids[4]} and ${ids[6]}
650         for i in 4 6; do
651                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
652                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
653                 verify_comp_attr pool $tf ${ids[$i]} flash
654                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
655         done
656
657         # verify component ${ids[7]}
658         verify_comp_attr stripe-size $tf ${ids[7]} 4194304
659         verify_comp_attr stripe-count $tf ${ids[7]} $OSTCOUNT
660         verify_comp_attr_with_parent pool $tf ${ids[7]}
661         verify_comp_extent $tf ${ids[7]} 0 EOF
662
663         # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
664         for i in 8 10 12; do
665                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
666                 verify_comp_attr stripe-count $tf ${ids[$i]} $OSTCOUNT
667                 verify_comp_attr pool $tf ${ids[$i]} archive
668                 verify_comp_extent $tf ${ids[$i]} 0 536870912
669         done
670
671         # verify components ${ids[9]}, ${ids[11]} and ${ids[13]}
672         for i in 9 11 13; do
673                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
674                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
675                 verify_comp_attr pool $tf ${ids[$i]} archive
676                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
677         done
678 }
679 run_test 0f "lfs mirror extend composite layout mirrors"
680
681 test_0g() {
682         local tf=$DIR/$tfile
683
684         ! $LFS mirror create --flags prefer $tf ||
685                 error "creating $tf w/ --flags but w/o -N option should fail"
686
687         ! $LFS mirror create -N --flags foo $tf ||
688                 error "creating $tf with '--flags foo' should fail"
689
690         ! $LFS mirror create -N --flags stale $tf ||
691                 error "creating $tf with '--flags stale' should fail"
692
693         ! $LFS mirror create -N --flags prefer,init $tf ||
694                 error "creating $tf with '--flags prefer,init' should fail"
695
696         ! $LFS mirror create -N --flags ^prefer $tf ||
697                 error "creating $tf with '--flags ^prefer' should fail"
698
699         $LFS mirror create -N -E 1M -S 1M -o0 --flags=prefer -E eof -o1 \
700                            -N -o1 $tf || error "create mirrored file $tf failed"
701
702         verify_comp_attr lcme_flags $tf 0x10001 prefer
703         verify_comp_attr lcme_flags $tf 0x10002 prefer
704
705         # write to the mirrored file and check primary
706         cp /etc/hosts $tf || error "error writing file '$tf'"
707
708         verify_comp_attr lcme_flags $tf 0x20003 stale
709
710         # resync file and check prefer flag
711         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
712
713         cancel_lru_locks osc
714         $LCTL set_param osc.*.stats=clear
715         cat $tf &> /dev/null || error "error reading file '$tf'"
716
717         # verify that the data was provided by OST1 where mirror 1 resides
718         local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-[-0-9a-f]*.stats |
719                         awk '/ost_read/{print $2}')
720         [ -n "$nr_read" ] || error "read was not provided by OST1"
721 }
722 run_test 0g "lfs mirror create flags support"
723
724 test_0h() {
725         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
726                 skip "Need MDS version at least 2.11.57"
727
728         local td=$DIR/$tdir
729         local tf=$td/$tfile
730         local ids
731         local i
732
733         # create parent directory
734         test_mkdir $td || error "mkdir $td failed"
735
736         $LFS setstripe -N -E 1M -S 1M --flags=prefer -E eof -N2 $td ||
737                 error "set default mirrored layout on directory $td failed"
738
739         # verify flags are inherited from the directory
740         touch $tf
741
742         verify_comp_attr lcme_flags $tf 0x10001 prefer
743         verify_comp_attr lcme_flags $tf 0x10002 prefer
744
745         # set flags to the first component
746         ! $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,foo $tf ||
747                 error "setting '^prefer,foo' flags should fail"
748
749         ! $LFS getstripe --component-flags=prefer,foo $tf ||
750                 error "getting component(s) with 'prefer,foo' flags should fail"
751
752         $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,stale $tf
753
754         verify_comp_attr lcme_flags $tf 0x10001 stale
755         verify_comp_attr lcme_flags $tf 0x10002 prefer
756
757         $LFS setstripe --comp-set -I0x10001 --comp-flags=^stale $tf &&
758                 error "clearing 'stale' should fail"
759
760         # write and resync file. It can't resync the file directly because the
761         # file state is still 'ro'
762         cp /etc/hosts $tf || error "error writing file '$tf'"
763         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
764
765         $LFS setstripe --comp-set -I 0x20003 --comp-flags=prefer $tf ||
766                 error "error setting flag prefer"
767
768         verify_comp_attr lcme_flags $tf 0x20003 prefer
769
770         $LFS setstripe --comp-set -I 0x20003 --comp-flags=^prefer $tf ||
771                 error "error clearing prefer flag from component 0x20003"
772
773         # MDS disallows setting stale flag on the last non-stale mirror
774         [[ "$MDS1_VERSION" -ge $(version_code 2.12.57) ]] || return 0
775
776         cp /etc/hosts $tf || error "error writing file '$tf'"
777
778         verify_comp_attr lcme_flags $tf 0x10002 prefer
779         verify_comp_attr lcme_flags $tf 0x20003 stale
780         verify_comp_attr lcme_flags $tf 0x30004 stale
781
782         ! $LFS setstripe --comp-set -I 0x10002 --comp-flags=^prefer,stale $tf \
783                 > /dev/null 2>&1 ||
784                 error "setting stale flag on component 0x10002 should fail"
785
786         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
787
788         $LFS setstripe --comp-set -I 0x10001 --comp-flags=stale $tf ||
789                 error "error setting stale flag on component 0x10001"
790         $LFS setstripe --comp-set -I 0x20003 --comp-flags=stale $tf ||
791                 error "error setting stale flag on component 0x20003"
792
793         ! $LFS setstripe --comp-set -I 0x30004 --comp-flags=stale $tf \
794                 > /dev/null 2>&1 ||
795                 error "setting stale flag on component 0x30004 should fail"
796
797         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
798 }
799 run_test 0h "set, clear and test flags for FLR files"
800
801 test_0j() {
802         $LFS mirror create -N2 $DIR/$tfile || error "create $DIR/$tfile failed"
803
804         cp /etc/hosts $DIR/$tfile || error "write to $DIR/$tfile failed"
805         $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed"
806         cmp /etc/hosts $DIR/$tfile || error "cmp with /etc/hosts failed"
807
808         $LFS mirror read -N2 -o $TMP/$tfile $DIR/$tfile || error "read mirror failed"
809         stack_trap "rm -f $TMP/$tfile"
810         cmp $TMP/$tfile $DIR/$tfile || error "cmp with $TMP/$tfile failed"
811         $LFS mirror write -N2 -i /etc/passwd $DIR/$tfile || error "write failed"
812         $LFS setstripe --comp-set -I 65537 --comp-flags=stale $DIR/$tfile ||
813                 error "set component 1 stale failed"
814         $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed"
815         cmp /etc/passwd $DIR/$tfile || error "cmp with /etc/passwd failed"
816 }
817 run_test 0j "test lfs mirror read/write commands"
818
819 test_1() {
820         local tf=$DIR/$tfile
821         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
822         local mirror_create_cmd="$LFS mirror create"
823         local stripes[0]=$OSTCOUNT
824
825         mirror_create_cmd+=" -N -c ${stripes[0]}"
826         for ((i = 1; i < $mirror_count; i++)); do
827                 # add mirrors with different stripes to the file
828                 stripes[$i]=$((RANDOM % OSTCOUNT))
829                 [ ${stripes[$i]} -eq 0 ] && stripes[$i]=1
830
831                 mirror_create_cmd+=" -N -c ${stripes[$i]}"
832         done
833
834         $mirror_create_cmd $tf || error "create mirrored file $tf failed"
835         verify_mirror_count $tf $mirror_count
836
837         # can't create mirrors exceeding LUSTRE_MIRROR_COUNT_MAX
838         $LFS mirror extend -N $tf &&
839                 error "Creating the $((mirror_count+1))th mirror succeeded"
840
841         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
842                         tr '\n' ' '))
843
844         # verify the range of components and stripe counts
845         for ((i = 0; i < $mirror_count; i++)); do
846                 verify_comp_attr stripe-count $tf ${ids[$i]} ${stripes[$i]}
847                 verify_comp_extent $tf ${ids[$i]} 0 EOF
848         done
849 }
850 run_test 1 "create components with setstripe options"
851
852 test_2() {
853         local tf=$DIR/$tfile
854         local tf2=$DIR/$tfile-2
855
856         $LFS setstripe -E 1M -S 1M -E EOF -c 1 $tf
857         $LFS setstripe -E 2M -S 1M -E EOF -c -1 $tf2
858
859         $LFS mirror extend -N -f $tf2 $tf ||
860                 error "merging $tf2 into $tf failed"
861
862         verify_mirror_count $tf 2
863         [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
864 }
865 run_test 2 "create components from existing files"
866
867 test_3() {
868         [[ $MDSCOUNT -lt 2 ]] && skip "need >= 2 MDTs" && return
869
870         for ((i = 0; i < 2; i++)); do
871                 $LFS mkdir -i $i $DIR/$tdir-$i
872                 $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
873         done
874
875         $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
876                 $DIR/$tdir-0/$tfile || error "creating mirrors"
877
878         # mdt doesn't support to cancel layout lock for remote objects, do
879         # it here manually.
880         cancel_lru_locks mdc
881
882         # make sure the mirrorted file was created successfully
883         [[ $($LFS getstripe --component-count $DIR/$tdir-0/$tfile) -eq 2 ]] ||
884                 { $LFS getstripe $DIR/$tdir-0/$tfile;
885                         error "expected 2 components"; }
886
887         # cleanup
888         rm -rf $DIR/$tdir-*
889 }
890 run_test 3 "create components from files located on different MDTs"
891
892 test_4() {
893         local tf=$DIR/$tdir/$tfile
894         local ids=()
895
896         test_mkdir $DIR/$tdir
897
898         # set mirror with setstripe options to directory
899         $LFS mirror create -N2 -E 1M -S 1M -E eof $DIR/$tdir ||
900                 error "set mirror to directory error"
901
902         [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
903                 x"mirrored" ] || error "failed to create mirrored dir"
904
905         touch $tf
906         verify_mirror_count $tf 2
907
908         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
909         verify_comp_extent $tf ${ids[0]} 0 1048576
910         verify_comp_extent $tf ${ids[1]} 1048576 EOF
911
912         # sub directory should inherit mirror setting from parent
913         test_mkdir $DIR/$tdir/td
914         [ x$($LFS getstripe -v $DIR/$tdir/td | awk '/lcm_flags/{print $2}') = \
915                 x"mirrored" ] || error "failed to inherit mirror from parent"
916
917         # mirror extend won't be applied to directory
918         $LFS mirror extend -N2 $DIR/$tdir &&
919                 error "expecting mirror extend failure"
920         true
921 }
922 run_test 4 "Make sure mirror attributes can be inhertied from directory"
923
924 test_5() {
925         local tf=$DIR/$tfile
926         local ids=()
927
928         $MULTIOP $tf oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T12345c ||
929                 error "failed to create file with non-empty layout"
930         $CHECKSTAT -t file -s 12345 $tf || error "size error: expecting 12345"
931
932         $LFS mirror create -N3 $tf || error "failed to attach mirror layout"
933         verify_mirror_count $tf 3
934
935         $CHECKSTAT -t file -s 12345 $tf ||
936                 error "size error after attaching layout "
937 }
938 run_test 5 "Make sure init size work for mirrored layout"
939
940 test_6() {
941         (( $MDS1_VERSION >= $(version_code 2.12.58) )) ||
942                 skip "MDS version older than 2.12.58"
943
944         local tf=$DIR/$tfile
945
946         $LFS mirror create -N -E 1M -L mdt -E eof -S 1M -N -E eof $tf ||
947                 error "failure to create DoM file with mirror"
948
949         $LFS mirror create -N -E 1M -S 1M -E eof -N -E 1M -L mdt -E eof $tf ||
950                 error "failure to create mirrored file with DoM"
951
952         $LFS setstripe -E 1M -L mdt -E eof -S 1M $tf ||
953                 error "failure to create PFL with DoM file"
954         $LFS mirror extend -N2 $tf ||
955                 error "failure to extend mirror with DoM"
956
957         $LFS setstripe -E 1M -L mdt -E eof -S 1M $tf-1 ||
958                 error "failure to create PFL with DoM file"
959         $LFS mirror create -N2 -E 1M -S 1M -E eof $tf-2 ||
960                 error "failure to create mirrored file"
961         $LFS mirror extend -N -f $tf-1 $tf-2 ||
962                 error "failure to extend mirrored file with DoM extent"
963 }
964 run_test 6 "DoM and FLR work together"
965
966 test_7() {
967         local tf=$DIR/$tfile
968
969         # create DoM with setting stripe_size == component size
970         $LFS mirror create -N -E1M -S1M -L mdt -Eeof $tf ||
971                 error "failure to create DoM with stripe_size == comp size"
972         rm -f $tf || error "delete $tf"
973
974         # DoM should not inherit previous component stripe_size
975         $LFS mirror create -N -E4M -S2M -Eeof -N -E1M -L mdt -Eeof $tf ||
976                 error "DoM component shouldn't inherit previous stripe_size"
977 }
978 run_test 7 "Create mirror with DoM component"
979
980 test_21() {
981         local tf=$DIR/$tfile
982         local tf2=$DIR/$tfile-2
983
984         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
985
986         stack_trap "rm -f $tf $tf2"
987
988         $LFS setstripe -E EOF -o 0 $tf
989         $LFS setstripe -E EOF -o 1 $tf2
990
991         local dd_count=$((RANDOM % 20 + 1))
992         dd if=/dev/zero of=$tf bs=1M count=$dd_count oflag=sync
993         dd if=/dev/zero of=$tf2 bs=1M count=1 seek=$((dd_count - 1)) oflag=sync
994
995         # for zfs - sync OST dataset so that du below will return
996         # accurate results
997         [ "$FSTYPE" = "zfs" ] &&
998                 do_nodes $(comma_list $(osts_nodes)) "$ZPOOL sync"
999
1000         local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
1001
1002         # add component
1003         $LFS mirror extend -N -f $tf2 $tf ||
1004                 error "merging $tf2 into $tf failed"
1005
1006         # cancel layout lock
1007         cancel_lru_locks mdc
1008
1009         local new_blocks=$(du -k $tf | awk '{print $1}')
1010         [ $new_blocks -eq $blocks ] ||
1011         error "i_blocks error expected: $blocks, actual: $new_blocks"
1012 }
1013 run_test 21 "glimpse should report accurate i_blocks"
1014
1015 get_osc_lock_count() {
1016         local lock_count=0
1017
1018         for idx in "$@"; do
1019                 local osc_name
1020                 local count
1021
1022                 osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'[-0-9a-f]*'
1023                 count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
1024                 lock_count=$((lock_count + count))
1025         done
1026         echo $lock_count
1027 }
1028
1029 test_22() {
1030         local tf=$DIR/$tfile
1031
1032         stack_trap "rm -f $tf"
1033
1034         $LFS setstripe -E EOF -o 0 $tf
1035         dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
1036
1037         # add component, two mirrors located on the same OST ;-)
1038         $LFS mirror extend -N -o 0 $tf ||
1039                 error "extending mirrored file $tf failed"
1040
1041         size_blocks=$(stat --format="%b %s" $tf)
1042
1043         cancel_lru_locks mdc
1044         cancel_lru_locks osc
1045
1046         local new_size_blocks=$(stat --format="%b %s" $tf)
1047
1048         # make sure there is no lock cached
1049         [ $(get_osc_lock_count 1) -eq 0 ] || error "glimpse requests were sent"
1050
1051         [ "$new_size_blocks" = "$size_blocks" ] ||
1052                 echo "size expected: $size_blocks, actual: $new_size_blocks"
1053
1054         rm -f $tmpfile
1055 }
1056 run_test 22 "no glimpse to OSTs for READ_ONLY files"
1057
1058 test_31() {
1059         local tf=$DIR/$tfile
1060
1061         $LFS mirror create -N -o 0 -N -o 1 $tf ||
1062                 error "creating mirrored file $tf failed"
1063
1064         #define OBD_FAIL_GLIMPSE_IMMUTABLE 0x1A00
1065         $LCTL set_param fail_loc=0x1A00
1066
1067         local ost_idx
1068         for ((ost_idx = 1; ost_idx <= 2; ost_idx++)); do
1069                 cancel_lru_locks osc
1070                 stop_osts $ost_idx
1071
1072                 local tmpfile=$(mktemp)
1073                 stat --format="%b %s" $tf > $tmpfile  &
1074                 local pid=$!
1075
1076                 local cnt=0
1077                 while [ $cnt -le 5 ]; do
1078                         kill -0 $pid > /dev/null 2>&1 || break
1079                         sleep 1
1080                         ((cnt += 1))
1081                 done
1082                 kill -0 $pid > /dev/null 2>&1 &&
1083                         error "stat process stuck due to unavailable OSTs"
1084
1085                 # make sure glimpse request has been sent
1086                 [ $(get_osc_lock_count 1 2) -ne 0 ] ||
1087                         error "OST $ost_idx: no glimpse request was sent"
1088
1089                 start_osts $ost_idx
1090         done
1091 }
1092 run_test 31 "make sure glimpse request can be retried"
1093
1094 test_32() {
1095         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1096         rm -f $DIR/$tfile $DIR/$tfile-2
1097
1098         stack_trap "rm -f $DIR/$tfile"
1099
1100         $LFS setstripe -E EOF -o 0 $DIR/$tfile
1101         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=$((RANDOM % 10 + 2))
1102
1103         local fsize=$(stat -c %s $DIR/$tfile)
1104         [[ $fsize -ne 0 ]] || error "file size is (wrongly) zero"
1105
1106         local cksum=$(md5sum $DIR/$tfile)
1107
1108         # create a new mirror in sync mode
1109         $LFS mirror extend -N -o 1 $DIR/$tfile ||
1110                 error "extending mirrored file $DIR/$tfile failed"
1111
1112         # make sure the mirrored file was created successfully
1113         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1114                 { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
1115
1116         drop_client_cache
1117         stop_osts 1
1118
1119         # check size is correct, glimpse request should go to the 2nd mirror
1120         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1121                 error "file size error $fsize vs. $(stat -c %s $DIR/$tfile)"
1122
1123         echo "reading file from the 2nd mirror and verify checksum"
1124         [[ "$cksum" == "$(md5sum $DIR/$tfile)" ]] ||
1125                 error "checksum error: expected $cksum"
1126
1127         start_osts 1
1128 }
1129 run_test 32 "data should be mirrored to newly created mirror"
1130
1131 test_33a() {
1132         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1133
1134         rm -f $DIR/$tfile $DIR/$tfile-2
1135
1136         # create a file with two mirrors
1137         $LFS setstripe -E EOF -o 0 $DIR/$tfile
1138         local max_count=100
1139         local count=0
1140         while [ $count -lt $max_count ]; do
1141                 echo "ost1" >> $DIR/$tfile
1142                 count=$((count + 1));
1143         done
1144
1145         # tmp file that will be used as mirror
1146         $LFS setstripe -E EOF -o 1 $DIR/$tfile-2
1147         count=0
1148         while [ $count -lt $max_count ]; do
1149                 echo "ost2" >> $DIR/$tfile-2
1150                 count=$((count + 1));
1151         done
1152
1153         # create a mirrored file
1154         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
1155                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1156                       "with verification should fail"
1157         $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
1158                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
1159                       "without verification failed"
1160
1161         # make sure that $tfile has two mirrors and $tfile-2 does not exist
1162         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1163                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1164
1165         [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
1166
1167         # execpted file size
1168         local fsize=$((5 * max_count))
1169         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1170                 error "mirrored file size is not $fsize"
1171
1172         # read file - all OSTs are available
1173         echo "reading file (data can be provided by any ost)... "
1174         local rs=$(cat $DIR/$tfile | head -1)
1175         [[ "$rs" == "ost1" || "$rs" == "ost2" ]] ||
1176                 error "file content error: expected: \"ost1\" or \"ost2\""
1177
1178         # read file again with ost1 failed
1179         stop_osts 1
1180         drop_client_cache
1181
1182         echo "reading file (data should be provided by ost2)..."
1183         local rs=$(cat $DIR/$tfile | head -1)
1184         [[ "$rs" == "ost2" ]] ||
1185                 error "file content error: expected: \"ost2\", actual: \"$rs\""
1186
1187         # remount ost1
1188         start_osts 1
1189
1190         # read file again with ost2 failed
1191         stop_osts 2
1192         drop_client_cache
1193
1194         # check size, glimpse should work
1195         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
1196                 error "mirrored file size is not $fsize"
1197
1198         echo "reading file (data should be provided by ost1)..."
1199         local rs=$(cat $DIR/$tfile | head -1)
1200         [[ "$rs" == "ost1" ]] ||
1201                 error "file content error: expected: \"ost1\", actual: \"$rs\""
1202
1203         start_osts 2
1204 }
1205 run_test 33a "read can choose available mirror to read"
1206
1207 test_33b() {
1208         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
1209
1210         rm -f $DIR/$tfile
1211
1212         stack_trap "rm -f $DIR/$tfile" EXIT
1213
1214         # create a file with two mirrors on OST0000 and OST0001
1215         $LFS setstripe -N -Eeof -o0 -N -Eeof -o1 $DIR/$tfile
1216
1217         # make sure that $tfile has two mirrors
1218         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1219                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1220
1221         # write 50M
1222         dd if=/dev/urandom of=$DIR/$tfile bs=2M count=25 ||
1223                 error "write failed for $DIR/$tfile"
1224         $LFS mirror resync $DIR/$tfile || error "resync failed for $DIR/$tfile"
1225         verify_flr_state $DIR/$tfile "ro"
1226         drop_client_cache
1227
1228         ls -l $DIR/$tfile
1229
1230         # read file - all OSTs are available
1231         echo "reading file (data can be provided by any ost)... "
1232         local t1=$SECONDS
1233         time cat $DIR/$tfile > /dev/null || error "read all"
1234         local t2=$SECONDS
1235         ra=$((t2 - t1))
1236
1237         # read file again with ost1 {OST0000} failed
1238         stop_osts 1
1239         drop_client_cache
1240         echo "reading file (data should be provided by ost2)..."
1241         t1=$SECONDS
1242         time cat $DIR/$tfile > /dev/null || error "read ost2"
1243         t2=$SECONDS
1244         r1=$((t2 - t1))
1245
1246         # remount ost1
1247         start_osts 1
1248
1249         # read file again with ost2 {OST0001} failed
1250         stop_osts 2
1251         drop_client_cache
1252
1253         echo "reading file (data should be provided by ost1)..."
1254         t1=$SECONDS
1255         time cat $DIR/$tfile > /dev/null || error "read ost1"
1256         t2=$SECONDS
1257         r2=$((t2 - t1))
1258
1259         # remount ost2
1260         start_osts 2
1261
1262         [ $((r1 * 100)) -gt $((ra * 105)) -a $r1 -gt $((ra + 2)) ] &&
1263                 error "read mirror too slow without ost1, from $ra to $r1"
1264         [ $((r2 * 100)) -gt $((ra * 105)) -a $r2 -gt $((ra + 2)) ] &&
1265                 error "read mirror too slow without ost2, from $ra to $r2"
1266
1267         wait_osc_import_ready client ost2
1268 }
1269 run_test 33b "avoid reading from unhealthy mirror"
1270
1271 test_33c() {
1272         [[ $OSTCOUNT -lt 3 ]] && skip "need >= 3 OSTs" && return
1273
1274         rm -f $DIR/$tfile
1275
1276         stack_trap "rm -f $DIR/$tfile" EXIT
1277
1278         # create a file with two mirrors
1279         # mirror1: {OST0000, OST0001}
1280         # mirror2: {OST0001, OST0002}
1281         $LFS setstripe -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 $DIR/$tfile
1282
1283         # make sure that $tfile has two mirrors
1284         [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
1285                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
1286
1287         # write 50M
1288         dd if=/dev/urandom of=$DIR/$tfile bs=2M count=25 ||
1289                 error "write failed for $DIR/$tfile"
1290         $LFS mirror resync $DIR/$tfile || error "resync failed for $DIR/$tfile"
1291         verify_flr_state $DIR/$tfile "ro"
1292         drop_client_cache
1293
1294         ls -l $DIR/$tfile
1295
1296         # read file - all OSTs are available
1297         echo "reading file (data can be provided by any ost)... "
1298         time cat $DIR/$tfile > /dev/null || error "read all"
1299
1300         # read file again with ost2 (OST0001) failed
1301         stop_osts 2
1302         drop_client_cache
1303
1304         echo "reading file (data should be provided by ost1 and ost3)..."
1305         time cat $DIR/$tfile > /dev/null || error "read ost1 & ost3"
1306
1307         # remount ost2
1308         start_osts 2
1309
1310         wait_osc_import_ready client ost2
1311 }
1312 run_test 33c "keep reading among unhealthy mirrors"
1313
1314 test_34a() {
1315         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1316
1317         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1318
1319         # reference file
1320         $LFS setstripe -o 0 $DIR/$tfile-ref
1321         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1322
1323         # create a file with two mirrors
1324         $LFS setstripe -E -1 -o 0,1 -S 1M $DIR/$tfile
1325         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1326
1327         $LFS setstripe -E -1 -o 2,3 -S 1M $DIR/$tfile-2
1328         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1329
1330         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1331                 error "mirrored file size is not 3M"
1332
1333         # merge a mirrored file
1334         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1335                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1336
1337         cancel_lru_locks osc
1338
1339         # stop two OSTs, so the 2nd stripe of the 1st mirror and
1340         # the 1st stripe of the 2nd mirror will be inaccessible, ...
1341         stop_osts 2 3
1342
1343         echo "comparing files ... "
1344
1345         # however, read can still return the correct data. It should return
1346         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1347         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1348                 $DIR/$tfile-ref || error "file reading error"
1349
1350         start_osts 2 3
1351 }
1352 run_test 34a "read mirrored file with multiple stripes"
1353
1354 test_34b() {
1355         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1356
1357         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1358
1359         # reference file
1360         $LFS setstripe -o 0 $DIR/$tfile-ref
1361         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1362
1363         $LFS setstripe -E 1M -S 1M -o 0 -E eof -o 1 $DIR/$tfile
1364         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1365
1366         $LFS setstripe -E 1M -S 1M -o 2 -E eof -o 3 $DIR/$tfile-2
1367         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1368
1369         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1370                 error "mirrored file size is not 3M"
1371
1372         # merge a mirrored file
1373         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1374                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1375
1376         cancel_lru_locks osc
1377
1378         # stop two OSTs, so the 2nd component of the 1st mirror and
1379         # the 1st component of the 2nd mirror will be inaccessible, ...
1380         stop_osts 2 3
1381
1382         echo "comparing files ... "
1383
1384         # however, read can still return the correct data. It should return
1385         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1386         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1387                 $DIR/$tfile-ref || error "file reading error"
1388
1389         start_osts 2 3
1390 }
1391 run_test 34b "read mirrored file with multiple components"
1392
1393 test_35() {
1394         local tf=$DIR/$tfile
1395
1396         $LFS setstripe -E eof $tf
1397
1398         # add an out-of-sync mirror to the file
1399         $LFS mirror extend -N -c 2 $tf ||
1400                 error "extending mirrored file $tf failed"
1401
1402         $MULTIOP $tf oO_WRONLY:c ||
1403                 error "write open a mirrored file failed"
1404
1405         # truncate file should return error
1406         $TRUNCATE $tf 100 || error "error truncating a mirrored file"
1407 }
1408 run_test 35 "allow to write to mirrored files"
1409
1410 verify_ost_layout_version() {
1411         local tf=$1
1412
1413         # get file layout version
1414         local flv=$($LFS getstripe $tf | awk '/lcm_layout_gen/{print $2}')
1415
1416         # layout version from OST objects
1417         local olv=$($MULTIOP $tf oXc | awk '/ostlayoutversion/{print $2}')
1418
1419         [ $flv -eq $olv ] || error "layout version mismatch: $flv vs. $olv"
1420 }
1421
1422 create_file_36() {
1423         local tf
1424
1425         for tf in "$@"; do
1426                 $LFS setstripe -E 1M -S 1M -E 2M -E 4M -E eof -c -1 $tf
1427                 $LFS setstripe -E 3M -S 1M -E 6M -E eof -c -1 $tf-tmp
1428
1429                 $LFS mirror extend -N -f $tf-tmp $tf ||
1430                         error "merging $tf-tmp into $tf failed"
1431         done
1432 }
1433
1434 test_36() {
1435         local tf=$DIR/$tfile
1436
1437         stack_trap "rm -f $tf $tf-2 $tf-3"
1438
1439         create_file_36 $tf $tf-2 $tf-3
1440
1441         [ $($LFS getstripe -N $tf) -gt 1 ] || error "wrong mirror count"
1442
1443         # test case 1 - check file write and verify layout version
1444         $MULTIOP $tf oO_WRONLY:c ||
1445                 error "write open a mirrored file failed"
1446
1447         # write open file should not return error
1448         $MULTIOP $tf oO_WRONLY:w1024Yc || error "write mirrored file error"
1449
1450         # instantiate components should work
1451         dd if=/dev/zero of=$tf bs=1M count=12 || error "write file error"
1452
1453         # verify OST layout version
1454         verify_ost_layout_version $tf
1455
1456         # test case 2
1457         local mds_facet=mds$(($($LFS getstripe -m $tf-2) + 1))
1458
1459         local delay_sec=10
1460         do_facet $mds_facet $LCTL set_param fail_val=$delay_sec
1461
1462         #define OBD_FAIL_FLR_LV_DELAY 0x1A01
1463         do_facet $mds_facet $LCTL set_param fail_loc=0x1A01
1464
1465         # write should take at least $fail_loc seconds and succeed
1466         local st=$(date +%s)
1467         $MULTIOP $tf-2 oO_WRONLY:w1024Yc || error "write mirrored file error"
1468
1469         [ $(date +%s) -ge $((st+delay_sec)) ] ||
1470                 error "write finished before layout version is transmitted"
1471
1472         # verify OST layout version
1473         verify_ost_layout_version $tf
1474
1475         do_facet $mds_facet $LCTL set_param fail_loc=0
1476
1477         # test case 3
1478         mds_idx=mds$(($($LFS getstripe -m $tf-3) + 1))
1479
1480         #define OBD_FAIL_FLR_LV_INC 0x1A02
1481         do_facet $mds_facet $LCTL set_param fail_loc=0x1A02
1482
1483         # write open file should return error
1484         $MULTIOP $tf-3 oO_WRONLY:O_SYNC:w1024c &&
1485                 error "write a mirrored file succeeded" || true
1486
1487         do_facet $mds_facet $LCTL set_param fail_loc=0
1488 }
1489 run_test 36 "write to mirrored files"
1490
1491 create_files_37() {
1492         local tf
1493         local fsize=$1
1494
1495         echo "create test files with size $fsize .."
1496
1497         shift
1498         for tf in "$@"; do
1499                 $LFS setstripe -E 1M -S 1M -c 1 -E eof -c -1 $tf
1500
1501                 dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
1502                 $TRUNCATE $tf $fsize
1503         done
1504 }
1505
1506 test_37()
1507 {
1508         [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
1509                 skip "Need MDS version at least 2.11.57"
1510
1511         local tf=$DIR/$tfile
1512         local tf2=$DIR/$tfile-2
1513         local tf3=$DIR/$tfile-3
1514         local tf4=$DIR/$tfile-4
1515
1516         stack_trap "rm -f $tf $tf2 $tf3 $tf4"
1517
1518         create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
1519         rm -f $tf4
1520         cp $tf $tf4
1521
1522         # assume the mirror id will be 1, 2, and 3
1523         declare -A checksums
1524         checksums[1]=$(cat $tf | md5sum)
1525         checksums[2]=$(cat $tf2 | md5sum)
1526         checksums[3]=$(cat $tf3 | md5sum)
1527
1528         printf '%s\n' "${checksums[@]}"
1529
1530         # merge these files into a mirrored file
1531         $LFS mirror extend --no-verify -N -f $tf2 $tf ||
1532                 error "merging $tf2 into $tf failed"
1533         $LFS mirror extend --no-verify -N -f $tf3 $tf ||
1534                 error "merging $tf3 into $tf failed"
1535
1536         get_mirror_ids $tf
1537
1538         # verify mirror read, checksums should equal to the original files'
1539         echo "Verifying mirror read .."
1540
1541         local sum
1542         for i in "${mirror_array[@]}"; do
1543                 $LCTL set_param ldlm.namespaces.*.lru_size=clear > /dev/null
1544                 sum=$($LFS mirror read -N $i $tf | md5sum)
1545                 [ "$sum" = "${checksums[$i]}" ] ||
1546                         error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
1547         done
1548
1549         # verify mirror write
1550         echo "Verifying mirror write .."
1551         $LFS mirror write -N2 $tf < $tf4
1552
1553         sum=$($LFS mirror read -N2 $tf | md5sum)
1554         [[ "$sum" = "${checksums[1]}" ]] ||
1555                 error "2: mismatch \'${checksums[1]}\' vs. \'$sum\'"
1556
1557         # verify mirror copy, write to this mirrored file will invalidate
1558         # the other two mirrors
1559         echo "Verifying mirror copy .."
1560
1561         local osts=$(comma_list $(osts_nodes))
1562
1563         $LFS mirror copy -i ${mirror_array[0]} -o-1 $tf ||
1564                 error "mirror copy error"
1565
1566         # verify copying is successful by checking checksums
1567         remount_client $MOUNT
1568         for i in "${mirror_array[@]}"; do
1569                 sum=$($LFS mirror read -N $i $tf | md5sum)
1570                 [ "$sum" = "${checksums[1]}" ] ||
1571                         error "$i: mismatch checksum after copy \'$sum\'"
1572         done
1573
1574         rm -f $tf
1575 }
1576 run_test 37 "mirror I/O API verification"
1577
1578 test_38() {
1579         local tf=$DIR/$tfile
1580         local ref=$DIR/${tfile}-ref
1581
1582         stack_trap "rm -f $tf $ref"
1583
1584         $LFS setstripe -E 1M -S 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf ||
1585                 error "creating $tf failed"
1586         $LFS setstripe -E 2M -S 1M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 \
1587                 $tf-2 || error "creating $tf-2 failed"
1588         $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3 ||
1589                 error "creating $tf-3 failed"
1590
1591         # instantiate all components
1592         $LFS mirror extend -N -f $tf-2 $tf ||
1593                 error "merging $tf-2 into $tf failed"
1594         $LFS mirror extend -N -f $tf-3 $tf ||
1595                 error "merging $tf-3 into $tf failed"
1596         $LFS mirror extend -N -c 1 $tf ||
1597                 error "extending mirrored file $tf failed"
1598
1599         verify_flr_state $tf "ro"
1600
1601         dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
1602
1603         local fsize=$((RANDOM << 8 + 1048576))
1604         $TRUNCATE $ref $fsize
1605
1606         local ref_cksum=$(cat $ref | md5sum)
1607
1608         # case 1: verify write to mirrored file & resync work
1609         cp $ref $tf || error "copy from $ref to $f error"
1610         verify_flr_state $tf "wp"
1611
1612         local file_cksum=$(cat $tf | md5sum)
1613         [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
1614
1615         get_mirror_ids $tf
1616         echo "mirror IDs: ${mirror_array[*]}"
1617
1618         local valid_mirror stale_mirror id mirror_cksum
1619         for id in "${mirror_array[@]}"; do
1620                 mirror_cksum=$($LFS mirror read -N $id $tf | md5sum)
1621                 [ "$ref_cksum" == "$mirror_cksum" ] &&
1622                         { valid_mirror=$id; continue; }
1623
1624                 stale_mirror=$id
1625         done
1626
1627         [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
1628         [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
1629
1630         $LFS mirror resync $tf || error "resync failed"
1631         verify_flr_state $tf "ro"
1632
1633         mirror_cksum=$($LFS mirror read -N $stale_mirror $tf | md5sum)
1634         [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
1635
1636         # case 2: inject an error to make mirror_io exit after changing
1637         # the file state to sync_pending so that we can start a concurrent
1638         # write.
1639         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1640         verify_flr_state $tf "wp"
1641
1642         mirror_io resync -e resync_start $tf && error "resync succeeded"
1643         verify_flr_state $tf "sp"
1644
1645         # from sync_pending to write_pending
1646         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1647         verify_flr_state $tf "wp"
1648
1649         mirror_io resync -e resync_start $tf && error "resync succeeded"
1650         verify_flr_state $tf "sp"
1651
1652         # from sync_pending to read_only
1653         $LFS mirror resync $tf || error "resync failed"
1654         verify_flr_state $tf "ro"
1655 }
1656 run_test 38 "resync"
1657
1658 test_39() {
1659         local tf=$DIR/$tfile
1660
1661         rm -f $tf
1662         $LFS mirror create -N2 -E1m -c1 -S1M -E-1 $tf ||
1663         error "create PFL file $tf failed"
1664
1665         verify_mirror_count $tf 2
1666         verify_comp_count $tf 4
1667
1668         rm -f $tf || error "delete $tf failed"
1669 }
1670 run_test 39 "check FLR+PFL (a.k.a. PFLR) creation"
1671
1672 test_40() {
1673         local tf=$DIR/$tfile
1674         local ops
1675
1676         for ops in "conv=notrunc" ""; do
1677                 rm -f $tf
1678
1679                 $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 --flags=prefer \
1680                                    -N -E 1M -E 2M -E 4M -E -1 $tf ||
1681                         error "create PFLR file $tf failed"
1682                 dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
1683                         error "write PFLR file $tf failed"
1684
1685                 lfs getstripe -vy $tf
1686
1687                 local flags
1688
1689                 # file mirror state should be write_pending
1690                 flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1691                 [ $flags = wp ] ||
1692                 error "file mirror state $flags"
1693                 # the 1st component (in mirror 1) should be inited
1694                 verify_comp_attr lcme_flags $tf 0x10001 init
1695                 # the 2nd component (in mirror 1) should be inited
1696                 verify_comp_attr lcme_flags $tf 0x10002 init
1697                 # the 3rd component (in mirror 1) should be uninited
1698                 verify_comp_attr lcme_flags $tf 0x10003 prefer
1699                 # the 4th component (in mirror 2) should be inited
1700                 verify_comp_attr lcme_flags $tf 0x20004 init
1701                 # the 5th component (in mirror 2) should be uninited
1702                 verify_comp_attr lcme_flags $tf 0x20005 0
1703                 # the 6th component (in mirror 2) should be stale
1704                 verify_comp_attr lcme_flags $tf 0x20006 stale
1705                 # the 7th component (in mirror 2) should be uninited
1706                 if [[ x$ops = "xconv=notrunc" ]]; then
1707                         verify_comp_attr lcme_flags $tf 0x20007 0
1708                 elif [[ x$ops = "x" ]]; then
1709                         verify_comp_attr lcme_flags $tf 0x20007 stale
1710                 fi
1711         done
1712
1713         rm -f $tf || error "delete $tf failed"
1714 }
1715 run_test 40 "PFLR rdonly state instantiation check"
1716
1717 test_41() {
1718         local tf=$DIR/$tfile
1719
1720         stack_trap "rm -f $tf $tf-1"
1721
1722         rm -f $tf $tf-1
1723         echo " **create two FLR files $tf $tf-1"
1724         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
1725                            -N -E 1M -E 2M -E 3M -E -1 $tf ||
1726                 error "create PFLR file $tf failed"
1727         $LFS mirror create -N -E 2M -S 1M -E eof \
1728                            -N -E 1M -E eof --flags prefer \
1729                            -N -E 4m -E eof $tf-1 ||
1730                 error "create PFLR file $tf-1 failed"
1731
1732         # file should be in ro status
1733         echo " **verify files be RDONLY"
1734         verify_flr_state $tf "ro"
1735         verify_flr_state $tf-1 "ro"
1736
1737         # write data in [0, 2M)
1738         dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
1739                 error "writing $tf failed"
1740         dd if=/dev/urandom of=$tf-1 bs=1M count=4 conv=notrunc ||
1741                 error "writing $tf-1 failed"
1742
1743         local sum0=$(cat $tf-1 | md5sum)
1744
1745         echo " **verify files be WRITE_PENDING"
1746         verify_flr_state $tf "wp"
1747         verify_flr_state $tf-1 "wp"
1748
1749         # file should have stale component
1750         echo " **verify files have stale component"
1751         $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
1752                 error "after writing $tf, it does not contain stale component"
1753         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1754                 error "after writing $tf-1, it does not contain stale component"
1755
1756         echo " **full resync"
1757         $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
1758
1759         echo " **verify $tf-1 data consistency in all mirrors"
1760         for i in 1 2 3; do
1761                 local sum=$($LFS mirror read -N$i $tf-1 | md5sum)
1762                 [[ "$sum" = "$sum0" ]] ||
1763                         error "$tf-1.$i: checksum mismatch: $sum != $sum0"
1764         done
1765
1766         echo " **verify files be RDONLY"
1767         verify_flr_state $tf "ro"
1768         verify_flr_state $tf-1 "ro"
1769
1770         # file should not have stale component
1771         echo " **verify files do not contain stale component"
1772         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1773                 error "after resyncing $tf, it contains stale component"
1774         $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
1775                 error "after resyncing $tf, it contains stale component"
1776
1777         # verify partial resync
1778         echo " **write $tf-1 for partial resync test"
1779         dd if=/dev/zero of=$tf-1 bs=1M count=2 conv=notrunc ||
1780                 error "writing $tf-1 failed"
1781
1782         echo " **only resync mirror 2"
1783         verify_flr_state $tf-1 "wp"
1784         $LFS mirror resync --only 2 $tf-1 ||
1785                 error "resync mirror 2 of $tf-1 failed"
1786         verify_flr_state $tf "ro"
1787
1788         # resync synced mirror
1789         echo " **resync mirror 2 again"
1790         $LFS mirror resync --only 2 $tf-1 ||
1791                 error "resync mirror 2 of $tf-1 failed"
1792         verify_flr_state $tf "ro"
1793         echo " **verify $tf-1 contains stale component"
1794         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1795                 error "after writing $tf-1, it does not contain stale component"
1796
1797         echo " **full resync $tf-1"
1798         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1799         verify_flr_state $tf "ro"
1800         echo " **full resync $tf-1 again"
1801         $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
1802         echo " **verify $tf-1 does not contain stale component"
1803         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1804                 error "after resyncing $tf, it contains stale component"
1805
1806         return 0
1807 }
1808 run_test 41 "lfs mirror resync check"
1809
1810 test_42() {
1811         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1812
1813         local td=$DIR/$tdir
1814         local tf=$td/$tfile
1815         local mirror_cmd="$LFS mirror verify"
1816         local i
1817
1818         stack_trap "rm -rf $td"
1819
1820         # create parent directory
1821         mkdir $td || error "mkdir $td failed"
1822
1823         $mirror_cmd &> /dev/null && error "no file name given"
1824         $mirror_cmd $tf &> /dev/null && error "cannot stat file $tf"
1825         $mirror_cmd $td &> /dev/null && error "$td is not a regular file"
1826
1827         # create mirrored files
1828         $LFS mirror create -N -E 4M -S 1M -E 10M -E EOF $tf ||
1829                 error "create mirrored file $tf failed"
1830         $LFS mirror create -N -E 2M -S 1M -E EOF \
1831                            -N -E 6M -E 8M -E EOF \
1832                            -N -E 16M -E EOF $tf-1 ||
1833                 error "create mirrored file $tf-1 failed"
1834         $LFS mirror create -N -c 2 -o 1,3 -N -S 2M -c -1 $tf-2 ||
1835                 error "create mirrored file $tf-2 failed"
1836
1837         # write data in [0, 10M)
1838         for i in $tf $tf-1 $tf-2; do
1839                 yes | dd of=$i bs=1M count=10 iflag=fullblock conv=notrunc ||
1840                         error "write $i failed"
1841         done
1842
1843         # resync the mirrored files
1844         $LFS mirror resync $tf-1 $tf-2 ||
1845                 error "resync $tf-1 $tf-2 failed"
1846
1847         # verify the mirrored files
1848         $mirror_cmd $tf-1 $tf-2 ||
1849                 error "verify $tf-1 $tf-2 failed"
1850
1851         get_mirror_ids $tf-1
1852         $mirror_cmd --only ${mirror_array[0]} $tf-1 &> /dev/null &&
1853                 error "at least 2 mirror ids needed with '--only' option"
1854         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 $tf-2 \
1855                 &> /dev/null &&
1856                 error "'--only' option cannot be used upon multiple files"
1857         $mirror_cmd --only 65534,${mirror_array[0]},65535 $tf-1 &&
1858                 error "invalid specified mirror ids"
1859
1860         # change the content of $tf and merge it into $tf-1
1861         for i in 6 10; do
1862                 echo a | dd of=$tf bs=1M seek=$i conv=notrunc ||
1863                         error "change $tf with seek=$i failed"
1864                 echo b | dd of=$tf-1 bs=1M seek=$i conv=notrunc ||
1865                         error "change $tf-1 with seek=$i failed"
1866         done
1867
1868         $LFS mirror resync $tf-1 || error "resync $tf-1 failed"
1869         $LFS mirror extend --no-verify -N -f $tf $tf-1 ||
1870                 error "merge $tf into $tf-1 failed"
1871
1872         # verify the mirrored files
1873         echo "Verify $tf-1 without -v option:"
1874         $mirror_cmd $tf-1 &&
1875                 error "verify $tf-1 should fail" || echo "PASS"
1876
1877         echo "Verify $tf-1 with -v option:"
1878         $mirror_cmd -v $tf-1 &&
1879                 error "verify $tf-1 should fail"
1880
1881         get_mirror_ids $tf-1
1882         echo "Verify $tf-1 with --only option:"
1883         $mirror_cmd -v --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 &&
1884                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1885                       "${mirror_array[-1]} should fail"
1886
1887         $mirror_cmd --only ${mirror_array[0]},${mirror_array[1]} $tf-1 ||
1888                 error "verify $tf-1 with mirror ${mirror_array[0]} and" \
1889                       "${mirror_array[1]} should succeed"
1890
1891         # set stale components in $tf-1
1892         for i in 0x40002 0x40003; do
1893                 $LFS setstripe --comp-set -I$i --comp-flags=stale $tf-1 ||
1894                         error "set stale flag on component $i failed"
1895         done
1896
1897         # verify the mirrored file
1898         echo "Verify $tf-1 with stale components:"
1899         $mirror_cmd -vvv $tf-1 ||
1900                 error "verify $tf-1 with stale components should succeed"
1901
1902         echo "Verify $tf-1 with stale components and --only option:"
1903         $mirror_cmd -vvv --only ${mirror_array[1]},${mirror_array[-1]} $tf-1 ||
1904                 error "verify $tf-1 with mirror ${mirror_array[1]} and" \
1905                       "${mirror_array[-1]} should succeed"
1906 }
1907 run_test 42 "lfs mirror verify"
1908
1909 # inactivate one OST && write && restore the OST
1910 write_file_43() {
1911         local file=$1
1912         local ost=$2
1913         local PARAM="osp.${FSNAME}-OST000${ost}-osc-M*.active"
1914         local wait
1915
1916         wait=$(do_facet $SINGLEMDS \
1917                 "$LCTL get_param -n lod.*MDT0000-*.qos_maxage")
1918         wait=${wait%%[^0-9]*}
1919
1920         echo "  **deactivate OST$ost, waiting for $((wait*2+2)) seconds"
1921         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 0")
1922         # lod_qos_statfs_update needs 2*$wait seconds to refresh targets statfs
1923         sleep $(($wait * 2 + 2))
1924         echo "  **write $file"
1925         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
1926         echo "  **restore activating OST$ost, waiting for $((wait*2+2)) seconds"
1927         $(do_facet $SINGLEMDS "$LCTL set_param -n $PARAM 1")
1928         sleep $((wait * 2 + 2))
1929
1930         local flags=$($LFS getstripe -v $file | awk '/lcm_flags:/ { print $2 }')
1931         [ $flags = wp ] || error "file mirror state $flags != wp"
1932 }
1933
1934 test_43a() {
1935         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
1936
1937         local tf=$DIR/$tfile
1938         local flags
1939
1940         rm -f $tf
1941         ##   mirror 0  ost (0, 1)
1942         ##   mirror 1  ost (1, 2)
1943         ##   mirror 2  ost (2, 0)
1944         $LFS mirror create -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 \
1945                 -N -Eeof -c2 -o2,0 $tf ||
1946                 error "create 3 mirrors file $tf failed"
1947
1948         ################## OST0 ###########################################
1949         write_file_43 $tf 0
1950         echo "  **verify components"
1951         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1952         verify_comp_attr lcme_flags $tf 0x20002 init
1953         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1954
1955         # resync
1956         echo "  **resync $tf"
1957         $LFS mirror resync $tf
1958         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1959         [ $flags = ro ] || error "file mirror state $flags != ro"
1960
1961         ################## OST1 ###########################################
1962         write_file_43 $tf 1
1963         echo "  **verify components"
1964         verify_comp_attr lcme_flags $tf 0x10001 init,stale
1965         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1966         verify_comp_attr lcme_flags $tf 0x30003 init
1967
1968         # resync
1969         echo "  **resync $tf"
1970         $LFS mirror resync $tf
1971         flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1972         [ $flags = ro ] || error "file mirror state $flags != ro"
1973
1974         ################## OST2 ###########################################
1975         write_file_43 $tf 2
1976         echo "  **verify components"
1977         verify_comp_attr lcme_flags $tf 0x10001 init
1978         verify_comp_attr lcme_flags $tf 0x20002 init,stale
1979         verify_comp_attr lcme_flags $tf 0x30003 init,stale
1980 }
1981 run_test 43a "mirror pick on write"
1982
1983 test_43b() {
1984         local tf=$DIR/$tdir/$tfile
1985
1986         test_mkdir $DIR/$tdir
1987         rm -f $tf
1988         stack_trap "rm -rf $tf"
1989
1990         # create 3 mirrors FLR file, the first 2 mirrors are preferred
1991         $LFS setstripe -N -Eeof --flags=prefer -N -Eeof --flags=prefer \
1992                 -N -Eeof $tf || error "create 3 mirrors file $tf failed"
1993         verify_flr_state $tf "ro"
1994
1995         echo " ** write to $tf"
1996         dd if=/dev/zero of=$tf bs=1M count=1 || error "write $tf failed"
1997         verify_flr_state $tf "wp"
1998
1999         echo " ** resync $tf"
2000         $LFS mirror resync $tf || error "resync $tf failed"
2001         verify_flr_state $tf "ro"
2002 }
2003 run_test 43b "allow writing to multiple preferred mirror file"
2004
2005 test_44a() {
2006         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2007         rm -rf $DIR/$tdir
2008         rm -rf $DIR/$tdir-1
2009         local tf=$DIR/$tdir/$tfile
2010         local tf1=$DIR/$tdir-1/$tfile-1
2011
2012         stack_trap "rm -rf $tf $tf1"
2013
2014         $LFS setdirstripe -i 0 -c 1 $DIR/$tdir ||
2015                 error "create directory failed"
2016         $LFS setdirstripe -i 1 -c 1 $DIR/$tdir-1 ||
2017                 error "create remote directory failed"
2018         rm -f $tf $tf1 $tf.mirror~2
2019         # create file with 4 mirrors
2020         $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
2021                            -N -E 1M -E 2M -E 3M -E -1 -N2 $tf ||
2022                 error "create PFLR file $tf failed"
2023
2024         # file should be in ro status
2025         verify_flr_state $tf "ro"
2026
2027         # write data in [0, 3M)
2028         dd if=/dev/urandom of=$tf bs=1M count=3 conv=notrunc ||
2029                 error "writing $tf failed"
2030
2031         verify_flr_state $tf "wp"
2032
2033         # disallow destroying the last non-stale mirror
2034         ! $LFS mirror delete --mirror-id 1 $tf > /dev/null 2>&1 ||
2035                 error "destroying mirror 1 should fail"
2036
2037         # synchronize all mirrors of the file
2038         $LFS mirror resync $tf || error "mirror resync $tf failed"
2039
2040         verify_flr_state $tf "ro"
2041
2042         # split mirror 1
2043         $LFS mirror split --mirror-id 1 -f $tf1 $tf ||
2044                 error "split to $tf1 failed"
2045
2046         local idx0=$($LFS getstripe -m $tf)
2047         local idx1=$($LFS getstripe -m $tf1)
2048
2049         [[ x$idx0 == x0 ]] || error "$tf is not on MDT0"
2050         [[ x$idx1 == x1 ]] || error "$tf1 is not on MDT1"
2051
2052         # verify mirror count
2053         verify_mirror_count $tf 3
2054         verify_mirror_count $tf1 1
2055
2056         $LFS mirror split --mirror-id 2 $tf ||
2057                 error "split mirror 2 failed"
2058
2059         verify_mirror_count $tf 2
2060         verify_mirror_count $tf.mirror~2 1
2061
2062         $LFS setstripe --comp-set -I 0x30008 --comp-flags=stale $tf ||
2063                 error "setting stale flag on component 0x30008 failed"
2064
2065         # disallow destroying the last non-stale mirror
2066         ! $LFS mirror split --mirror-id 4 -d $tf > /dev/null 2>&1 ||
2067                 error "destroying mirror 4 should fail"
2068
2069         $LFS mirror resync $tf || error "resynchronizing $tf failed"
2070
2071         $LFS mirror split --mirror-id 3 -d $tf ||
2072                 error "destroying mirror 3 failed"
2073         verify_mirror_count $tf 1
2074
2075         # verify splitted file contains the same content as the orig file does
2076         diff $tf $tf1 || error "splited file $tf1 diffs from $tf"
2077         diff $tf $tf.mirror~2 ||
2078                 error "splited file $tf.mirror~2 diffs from $tf"
2079 }
2080 run_test 44a "lfs mirror split check"
2081
2082 test_44b() {
2083         (( $MDS1_VERSION >= $(version_code 2.14.56) )) ||
2084                 skip "Need MDS version at least 2.14.56"
2085
2086         rm -rf $DIR/$tdir
2087         local tf=$DIR/$tdir/$tfile
2088
2089         mkdir -p $DIR/$tdir || error "create directory failed"
2090
2091         echo XXX > $tf
2092
2093         # create 2 mirrors file
2094         $LFS mirror extend -N -c1 $tf
2095
2096         echo YYY > $tf
2097
2098         verify_flr_state $tf "wp"
2099
2100         local str=$(cat $tf)
2101
2102         [[ $str == "YYY" ]] || error "$tf content is not YYY"
2103
2104         # get the non-stale mirror id
2105         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
2106                         tr '\n' ' '))
2107         local mirror_ids=($($LFS getstripe $tf |
2108                         awk '/lcme_mirror_id/{print $2}' | tr '\n' ' '))
2109         for ((i = 0; i < 2; i++)); do
2110                 $LFS getstripe -I${ids[$i]} --component-flags $tf |
2111                         grep stale > /dev/null || break
2112         done
2113
2114         [[ $i -ge 2 ]] && ( $LFS getstripe $tf; error "no stale mirror" )
2115
2116         $LFS getstripe $tf
2117
2118         # split the updated mirror, should fail
2119         echo "split mirror_id ${mirror_ids[$i]} id ${ids[$i]}, should fail"
2120         $LFS mirror split --mirror-id=${mirror_ids[$i]} $tf &> /dev/null &&
2121                 error "split --mirror-id=${mirror_ids[$i]} $tf should fail"
2122
2123         i=$(( 1 - i ))
2124         # split the stale mirror
2125         echo "split mirror_id ${mirror_ids[$i]} id ${ids[$i]}"
2126         $LFS mirror split --mirror-id=${mirror_ids[$i]} -d $tf ||
2127                 error "mirror split --mirror-id=${mirror_ids[$i]} $tf failed"
2128
2129         echo "make sure there's no stale comp in the file"
2130         # make sure there's no stale comp in the file
2131         $LFS getstripe $tf | awk '/lcme_flags/{print $2}' | grep stale &&
2132                 ( $LFS getstripe $tf; error "stale mirror file" )
2133
2134         str=$(cat $tf)
2135         [[ $str == "YYY" ]] ||
2136                 ( cat $tf; error "$tf content is not YYY after split" )
2137 }
2138 run_test 44b "mirror split does not create stale file"
2139
2140 test_44c() {
2141         local tf=$DIR/$tdir/$tfile
2142
2143         stack_trap "rm -f $tf"
2144
2145         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
2146                 skip "Need MDS version at least 2.14.52"
2147
2148         [ "$FSTYPE" != "zfs" ] || skip "ZFS file's block number is not accurate"
2149
2150         mkdir -p $DIR/$tdir || error "create directroy failed"
2151
2152         dd if=/dev/zero of=$tf bs=1M count=10 || error "dd write $tfile failed"
2153         sync
2154         block1=$(( $(stat -c "%b*%B" $tf) ))
2155         echo " ** before mirror ops, file blocks=$((block1/1024)) KiB"
2156
2157         $LFS mirror extend -N2 -c1 $tf || error "mirror extend $tfile failed"
2158         sync
2159         block2=$(( $(stat -c "%b*%B" $tf) ))
2160         echo " ** after mirror extend, file blocks=$((block2/1024)) KiB"
2161
2162         $LFS mirror split -d --mirror-id=2 $tf ||
2163                 error "mirror split $tfile failed"
2164         $LFS mirror split -d --mirror-id=3 $tf ||
2165                 error "mirror split $tfile failed"
2166         sync
2167         lfs getsom $tf
2168         block3=$(( $(stat -c "%b*%B" $tf) ))
2169         echo " ** after mirror split, file blocks=$((block3/1024)) KiB"
2170
2171         [[ $block1 -eq $block3 ]] ||
2172                 error "mirror split does not reduce block# $block3 != $block1"
2173 }
2174 run_test 44c "lfs mirror split reduces block size of a file"
2175
2176 test_45() {
2177         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
2178
2179         local file=$DIR/$tdir/$tfile
2180         local dir=$DIR/$tdir/$dir
2181         local temp=$DIR/$tdir/template
2182         rm -rf $DIR/$tdir
2183         test_mkdir $DIR/$tdir
2184
2185         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
2186                 -N -E3m -S1m -Eeof -N -E8m -Eeof $file ||
2187                         error "Create $file failed"
2188
2189         verify_yaml_layout $file $file.copy $temp "1. FLR file"
2190         rm -f $file $file.copy
2191
2192         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
2193                 -N -E3m -S1m -Eeof -N -E8m --flags=prefer -Eeof $file ||
2194                         error "Create $file failed"
2195
2196         verify_yaml_layout $file $file.copy $temp "2. FLR file with flags"
2197 }
2198 run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
2199
2200 verify_46() {
2201         local src=$1
2202         local dst=$2
2203         local msg_prefix=$3
2204
2205         $LFS setstripe --copy=$src $dst || error "setstripe $dst failed"
2206
2207         local layout1=$(get_layout_param $src)
2208         local layout2=$(get_layout_param $dst)
2209         # compare their layout info
2210         [ "$layout1" == "$layout2" ] ||
2211                 error "$msg_prefix $src <=> $dst layouts are not equal"
2212 }
2213
2214 test_46() {
2215         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2216
2217         local file=$DIR/$tdir/$tfile
2218         test_mkdir $DIR/$tdir
2219
2220         ########################### 1. PFL file #############################
2221         echo "  ** 1. PFL file"
2222         rm -f $file
2223         $LFS setstripe -E1m -S 1M -c2 -o0,1 -E2m -c2 -E3m -o1,0 -E4m -c1 -E-1 \
2224                 $file || error "1. Create PFL $file failed"
2225
2226         rm -f $file.copy
2227         verify_46 $file $file.copy "1. PFL file"
2228
2229         ########################### 2. plain file ###########################
2230         echo "  ** 2. plain file"
2231         rm -f $file
2232         $LFS setstripe -c2 -o0,1 -i1 $file ||
2233                 error "2. Create plain $file failed"
2234
2235         rm -f $file.copy
2236         verify_46 $file $file.copy "2. plain file"
2237
2238         ########################### 3. FLR file #############################
2239         echo "  ** 3. FLR file"
2240         rm -f $file
2241         $LFS setstripe -N -E1m -S 1M -c2 -o0,1 -E4m -c1 -Eeof -N -E16m -Eeof \
2242                 $file || error "3. Create FLR $file failed"
2243
2244         rm -f $file.copy
2245         verify_46 $file $file.copy "3. FLR file"
2246
2247         local dir=$DIR/$tdir/dir
2248         ########################### 4. PFL dir ##############################
2249         echo "  ** 4. PFL dir"
2250         test_mkdir $dir
2251         $LFS setstripe -E1m -S 1M -c2 -E2m -c1 -E-1 $dir ||
2252                 error "4. setstripe PFL $dir failed"
2253
2254         test_mkdir $dir.copy
2255         verify_46 $dir $dir.copy "4. PFL dir"
2256
2257         ########################### 5. plain dir ############################
2258         echo "  ** 5. plain dir"
2259         $LFS setstripe -c2 -i-1 $dir || error "5. setstripe plain $dir failed"
2260
2261         verify_46 $dir $dir.copy "5. plain dir"
2262
2263         ########################### 6. FLR dir ##############################
2264         echo "  ** 6. FLR dir"
2265         $LFS setstripe -N -E1m -S 1M -c2 -E2m -c1 -Eeof -N -E4m -Eeof $dir ||
2266                 error "6. setstripe FLR $dir failed"
2267
2268         verify_46 $dir $dir.copy "6. FLR dir"
2269
2270         (( $MDS1_VERSION >= $(version_code 2.13.53.205) )) ||
2271                 echo "server version $MDS1_VERSION does not support SEL" &&
2272                 return 0
2273
2274         ########################### 7. SEL file ##############################
2275         echo "  ** 7. SEL file"
2276         rm -f $file
2277         $LFS setstripe -E256M -S 1M -c2 -o0,1 -z 64M -E-1 -o1,0 -z 128M \
2278                 $file || error "Create $file failed"
2279
2280         rm -f $file.copy
2281         verify_46 $file $file.copy "7. SEL file"
2282
2283         ########################### 8. SEL dir ##############################
2284         echo "  ** 8. SEL dir"
2285         $LFS setstripe -E256M -S 1M -c2 -z 64M -E-1 -z 128M \
2286                 $dir || error "setstripe $dir failed"
2287
2288         verify_46 $dir $dir.copy "8. SEL dir"
2289
2290         ########################### 9. FLR SEL file ##########################
2291         echo "  ** 9. FLR SEL file"
2292         rm -f $file
2293         $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
2294                 -N -E1G -c4 -z128M -E-1 -z256M $file || error "Create $file failed"
2295
2296         rm -f $file.copy
2297         verify_46 $file $file.copy "9. SEL file"
2298
2299         ########################### 10. FLR SEL dir #########################
2300         echo "  ** 10. FLR SEL dir"
2301         $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
2302                 -N -E1G -c4 -z128M -E-1 -z256M $dir || error "Create $file failed"
2303
2304         verify_46 $dir $dir.copy "10. SEL dir"
2305 }
2306 run_test 46 "Verify setstripe --copy option"
2307
2308 test_47() {
2309         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
2310
2311         local file=$DIR/$tdir/$tfile
2312         local ids
2313         local ost
2314         local osts
2315
2316         test_mkdir $DIR/$tdir
2317
2318         # test case 1:
2319         rm -f $file
2320         # mirror1: [comp0]ost0,    [comp1]ost1 and ost2
2321         # mirror2: [comp2]    ,    [comp3] should not use ost1 or ost2
2322         $LFS mirror create -N -E2m -c1 -o0 --flags=prefer -Eeof -c2 -o1,2 \
2323                 -N -E2m -c1 -Eeof -c1 $file || error "create FLR $file failed"
2324         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2325
2326         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
2327         $LFS mirror resync $file || error "resync $file failed"
2328
2329         ost=$($LFS getstripe -I${ids[2]} $file | awk '/l_ost_idx/{print $5}')
2330         if [[ x$ost == "x0," ]]; then
2331                 $LFS getstripe $file
2332                 error "component ${ids[2]} objects allocated on $ost " \
2333                       "shouldn't on OST0"
2334         fi
2335
2336         ost=$($LFS getstripe -I${ids[3]} $file | awk '/l_ost_idx/{print $5}')
2337         if [[ x$ost == "x1," || x$ost == "x2," ]]; then
2338                 $LFS getstripe $file
2339                 error "component ${ids[3]} objects allocated on $ost " \
2340                       "shouldn't on OST1 or on OST2"
2341         fi
2342
2343         ## test case 2:
2344         rm -f $file
2345         # mirror1: [comp0]    [comp1]
2346         # mirror2: [comp2]    [comp3]
2347         # mirror3: [comp4]    [comp5]
2348         # mirror4: [comp6]    [comp7]
2349         $LFS mirror create -N4 -E1m -c1 -Eeof -c1 $file ||
2350                 error "create FLR $file failed"
2351         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2352
2353         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
2354         $LFS mirror resync $file || error "resync $file failed"
2355
2356         for ((i = 0; i < 6; i++)); do
2357                 osts[$i]=$($LFS getstripe -I${ids[$i]} $file |
2358                         awk '/l_ost_idx/{print $5}')
2359         done
2360         # comp[0],comp[2],comp[4] should use different osts
2361         if [[ ${osts[0]} == ${osts[2]} || ${osts[0]} == ${osts[4]} ||
2362               ${osts[2]} == ${osts[4]} ]]; then
2363                 $LFS getstripe $file
2364                 error "component ${ids[0]}, ${ids[2]}, ${ids[4]} have objects "\
2365                       "allocated on duplicated OSTs"
2366         fi
2367         # comp[1],comp[3],comp[5] should use different osts
2368         if [[ ${osts[1]} == ${osts[3]} || ${osts[1]} == ${osts[5]} ||
2369               ${osts[3]} == ${osts[5]} ]]; then
2370                 $LFS getstripe $file
2371                 error "component ${ids[1]}, ${ids[3]}, ${ids[5]} have objects "\
2372                       "allocated on duplicated OSTs"
2373         fi
2374
2375         return 0
2376 }
2377 run_test 47 "Verify mirror obj alloc"
2378
2379 test_48() {
2380         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
2381                 skip "Need MDS version at least 2.11.55"
2382
2383         local tf=$DIR/$tfile
2384
2385         rm -f $tf
2386         echo " ** create 2 mirrors FLR file $tf"
2387         $LFS mirror create -N -E2M -Eeof --flags prefer \
2388                            -N -E1M -Eeof $tf ||
2389                 error "create FLR file $tf failed"
2390
2391         echo " ** write it"
2392         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2393         verify_flr_state $tf "wp"
2394
2395         local sum0=$(md5sum < $tf)
2396
2397         echo " ** resync the file"
2398         $LFS mirror resync $tf
2399
2400         echo " ** snapshot mirror 2"
2401         $LFS setstripe --comp-set -I 0x20003 --comp-flags=nosync $tf
2402
2403         echo " ** write it again"
2404         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2405         echo " ** resync it again"
2406         $LFS mirror resync $tf
2407
2408         verify_flr_state $tf "wp"
2409         verify_comp_attr lcme_flags $tf 0x20003 nosync,stale
2410
2411         local sum1=$($LFS mirror read -N1 $tf | md5sum)
2412         local sum2=$($LFS mirror read -N2 $tf | md5sum)
2413
2414         echo " ** verify mirror 2 doesn't change"
2415         echo "original checksum: $sum0"
2416         echo "mirror 1 checksum: $sum1"
2417         echo "mirror 2 checksum: $sum2"
2418         [[ $sum0 = $sum2 ]] ||
2419                 error "original checksum: $sum0, mirror 2 checksum: $sum2"
2420         echo " ** mirror 2 stripe info"
2421         $LFS getstripe -v --mirror-index=2 $tf
2422
2423         echo " ** resync mirror 2"
2424         $LFS mirror resync --only 2 $tf
2425
2426         verify_flr_state $tf "ro"
2427         verify_comp_attr lcme_flags $tf 0x20003 nosync,^stale
2428
2429         sum1=$($LFS mirror read -N1 $tf | md5sum)
2430         sum2=$($LFS mirror read -N2 $tf | md5sum)
2431
2432         echo " ** verify mirror 2 resync-ed"
2433         echo "original checksum: $sum0"
2434         echo "mirror 1 checksum: $sum1"
2435         echo "mirror 2 checksum: $sum2"
2436         [[ $sum1 = $sum2 ]] ||
2437                 error "mirror 1 checksum: $sum1, mirror 2 checksum: $sum2"
2438         echo " ** mirror 2 stripe info"
2439         $LFS getstripe -v --mirror-index=2 $tf
2440 }
2441 run_test 48 "Verify snapshot mirror"
2442
2443 OLDIFS="$IFS"
2444 cleanup_49() {
2445         trap 0
2446         IFS="$OLDIFS"
2447 }
2448
2449 test_49a() {
2450         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
2451
2452         trap cleanup_49 EXIT RETURN
2453
2454         local file=$DIR/$tfile
2455
2456         $LFS setstripe -N -E eof -c1 -o1 -N -E eof -c1 -o0 $file ||
2457                 error "setstripe on $file"
2458
2459         dd if=/dev/zero of=$file bs=1M count=1 || error "dd failed for $file"
2460         $LFS mirror resync $file
2461
2462         filefrag -ves $file || error "filefrag $file failed"
2463         filefrag_op=$(filefrag -ve -k $file |
2464                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
2465
2466 #Filesystem type is: bd00bd0
2467 #File size of /mnt/lustre/f49a.sanity-flr is 1048576 (1024 blocks of 1024 bytes)
2468 # ext:     device_logical:        physical_offset: length:  dev: flags:
2469 #   0:        0..    1023:    1572864..   1573887:   1024: 0001: net,eof
2470 #   1:        0..    1023:    1572864..   1573887:   1024: 0000: last,net,eof
2471 #/mnt/lustre/f49a.sanity-flr: 2 extents found
2472
2473         last_lun=$(echo $filefrag_op | cut -d: -f5)
2474         IFS=$'\n'
2475         tot_len=0
2476         num_luns=1
2477         for line in $filefrag_op; do
2478                 frag_lun=$(echo $line | cut -d: -f5)
2479                 ext_len=$(echo $line | cut -d: -f4)
2480                 if [[ "$frag_lun" != "$last_lun" ]]; then
2481                         if (( tot_len != 1024 )); then
2482                                 cleanup_49
2483                                 error "$file: OST$last_lun $tot_len != 1024"
2484                         else
2485                                 (( num_luns += 1 ))
2486                                 tot_len=0
2487                         fi
2488                 fi
2489                 (( tot_len += ext_len ))
2490                 last_lun=$frag_lun
2491         done
2492         if (( num_luns != 2 || tot_len != 1024 )); then
2493                 cleanup_49
2494                 error "$file: $num_luns != 2, $tot_len != 1024 on OST$last_lun"
2495         fi
2496
2497         echo "FIEMAP on $file succeeded"
2498 }
2499 run_test 49a "FIEMAP upon FLR file"
2500
2501 test_50A() {    # EX-2179
2502         mkdir -p $DIR/$tdir
2503
2504         local file=$DIR/$tdir/$tfile
2505
2506         $LFS setstripe -c1 -i0 $file || error "setstripe $file failed"
2507
2508         $LFS mirror extend -N -c1 -i1 $file ||
2509                 error "extending mirror for $file failed"
2510
2511         local olv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
2512
2513         fail mds1
2514
2515         $LFS mirror split -d --mirror-id=1 $file || error "split $file failed"
2516
2517         local flv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
2518
2519         echo "$file layout generation from $olv to $flv"
2520         (( $flv != ($olv + 1) )) &&
2521                 error "split does not increase layout gen from $olv to $flv"
2522
2523         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
2524
2525         $LFS getstripe -v $file || error "getstripe $file failed"
2526 }
2527 run_test 50A "mirror split update layout generation"
2528
2529 test_50a() {
2530         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2531                 skip "OST does not support SEEK_HOLE"
2532
2533         local file=$DIR/$tdir/$tfile
2534         local offset
2535         local sum1
2536         local sum2
2537         local blocks
2538
2539         mkdir -p $DIR/$tdir
2540
2541         echo " ** create striped file $file"
2542         $LFS setstripe -E 1M -c1 -S 1M -E eof -c2 -S1M $file ||
2543                 error "cannot create file with PFL layout"
2544         echo " ** write 1st data chunk at 1M boundary"
2545         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2546                 error "cannot write data at 1M boundary"
2547         echo " ** write 2nd data chunk at 2M boundary"
2548         dd if=/dev/urandom of=$file bs=1k count=20 seek=2041 ||
2549                 error "cannot write data at 2M boundary"
2550         echo " ** create hole at the file end"
2551         $TRUNCATE $file 3700000 || error "truncate fails"
2552
2553         echo " ** verify sparseness"
2554         offset=$(lseek_test -d 1000 $file)
2555         echo "    first data offset: $offset"
2556         [[ $offset == 1000 ]] &&
2557                 error "src: data is not expected at offset $offset"
2558         offset=$(lseek_test -l 3500000 $file)
2559         echo "    hole at the end: $offset"
2560         [[ $offset == 3500000 ]] ||
2561                 error "src: hole is expected at offset $offset"
2562
2563         echo " ** extend the file with new mirror"
2564         # migrate_copy_data() is used
2565         $LFS mirror extend -N -E 2M -S 1M -E 1G -S 2M -E eof $file ||
2566                 error "cannot create mirror"
2567         $LFS getstripe $file | grep lcme_flags | grep stale > /dev/null &&
2568                 error "$file still has stale component"
2569
2570         # check migrate_data_copy() was correct
2571         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2572         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2573         [[ $sum_1 == $sum_2 ]] ||
2574                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2575
2576         # stale first mirror
2577         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale $file
2578         $LFS setstripe --comp-set -I0x10002 --comp-flags=stale $file
2579
2580         echo " ** verify mirror #2 sparseness"
2581         offset=$(lseek_test -d 1000 $file)
2582         echo "    first data offset: $offset"
2583         [[ $offset == 1000 ]] &&
2584                 error "dst: data is not expected at offset $offset"
2585         offset=$(lseek_test -l 3500000 $file)
2586         echo "    hole at the end: $offset"
2587         [[ $offset == 3500000 ]] ||
2588                 error "dst: hole is expected at offset $offset"
2589
2590         echo " ** copy mirror #2 to mirror #1"
2591         $LFS mirror copy -i 2 -o 1 $file || error "mirror copy fails"
2592         $LFS getstripe $file | grep lcme_flags | grep stale > /dev/null &&
2593                 error "$file still has stale component"
2594
2595         # check llapi_mirror_copy_many correctness
2596         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2597         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2598         [[ $sum_1 == $sum_2 ]] ||
2599                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2600
2601         # stale 1st component of mirror #2 before lseek call
2602         $LFS setstripe --comp-set -I0x20001 --comp-flags=stale $file
2603
2604         echo " ** verify mirror #1 sparseness again"
2605         offset=$(lseek_test -d 1000 $file)
2606         echo "    first data offset: $offset"
2607         [[ $offset == 1000 ]] &&
2608                 error "dst: data is not expected at offset $offset"
2609         offset=$(lseek_test -l 3500000 $file)
2610         echo "    hole at the end: $offset"
2611         [[ $offset == 3500000 ]] ||
2612                 error "dst: hole is expected at offset $offset"
2613
2614         cancel_lru_locks osc
2615
2616         blocks=$(stat -c%b $file)
2617         echo " ** final consumed blocks: $blocks"
2618         # for 3.5Mb file consumes ~6000 blocks, use 1000 to check
2619         # that file is still sparse
2620         (( blocks < 1000 )) ||
2621                 error "Mirrored file consumes $blocks blocks"
2622
2623         rm $file
2624 }
2625 run_test 50a "mirror extend/copy preserves sparseness"
2626
2627 test_50b() {
2628         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2629                 skip "OST does not support SEEK_HOLE"
2630
2631         local file=$DIR/$tdir/$tfile
2632         local offset
2633         local sum1
2634         local sum2
2635         local blocks
2636
2637         mkdir -p $DIR/$tdir
2638
2639         echo " ** create mirrored file $file"
2640         $LFS mirror create -N -E1M -c1 -S1M -E eof \
2641                 -N -E2M -S1M -E eof -S2M $file ||
2642                 error "cannot create mirrored file"
2643         echo " ** write data chunk at 1M boundary"
2644         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2645                 error "cannot write data at 1M boundary"
2646         echo " ** create hole at the file end"
2647         $TRUNCATE $file 3700000 || error "truncate fails"
2648
2649         echo " ** verify sparseness"
2650         offset=$(lseek_test -d 1000 $file)
2651         echo "    first data offset: $offset"
2652         [[ $offset == 1000 ]] &&
2653                 error "src: data is not expected at offset $offset"
2654         offset=$(lseek_test -l 3500000 $file)
2655         echo "    hole at the end: $offset"
2656         [[ $offset == 3500000 ]] ||
2657                 error "src: hole is expected at 3500000"
2658
2659         echo " ** resync mirror #2 to mirror #1"
2660         $LFS mirror resync $file
2661
2662         # check llapi_mirror_copy_many correctness
2663         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2664         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2665         [[ $sum_1 == $sum_2 ]] ||
2666                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2667
2668         cancel_lru_locks osc
2669
2670         blocks=$(stat -c%b $file)
2671         echo " ** consumed blocks: $blocks"
2672         # without full punch() support the first component can be not sparse
2673         # but the last one should be, so file should use far fewer blocks
2674         (( blocks < 5000 )) ||
2675                 error "Mirrored file consumes $blocks blocks"
2676
2677         # stale first component in mirror #1
2678         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,nosync $file
2679         echo " ** truncate file down"
2680         $TRUNCATE $file 0
2681         echo " ** write data chunk at 2M boundary"
2682         dd if=/dev/urandom of=$file bs=1k count=20 seek=2041 conv=notrunc ||
2683                 error "cannot write data at 2M boundary"
2684         echo " ** resync mirror #2 to mirror #1 with nosync 1st component"
2685         $LFS mirror resync $file || error "mirror rsync fails"
2686         # first component is still stale
2687         $LFS getstripe $file | grep 'lcme_flags:.*stale' > /dev/null ||
2688                 error "$file still has no stale component"
2689         echo " ** resync mirror #2 to mirror #1 again"
2690         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,^nosync $file
2691         $LFS mirror resync $file || error "mirror rsync fails"
2692         $LFS getstripe $file | grep 'lcme_flags:.*stale' > /dev/null &&
2693                 error "$file still has stale component"
2694
2695         # check llapi_mirror_copy_many correctness
2696         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2697         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2698         [[ $sum_1 == $sum_2 ]] ||
2699                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2700
2701         cancel_lru_locks osc
2702
2703         blocks=$(stat -c%b $file)
2704         echo " ** final consumed blocks: $blocks"
2705         # while the first component can lose sparseness, the last one should
2706         # not, so whole file should still use far fewer blocks in total
2707         (( blocks < 3000 )) ||
2708                 error "Mirrored file consumes $blocks blocks"
2709         rm $file
2710 }
2711 run_test 50b "mirror rsync handles sparseness"
2712
2713 test_50c() {
2714         local tf=$DIR/$tdir/$tfile
2715
2716         test_mkdir $DIR/$tdir
2717
2718         $LFS setstripe -N2 -c-1 $tf || error "create FLR $tf failed"
2719         verify_flr_state $tf "ro"
2720
2721         if [[ "$FSTYPE" == "ldiskfs" ]]; then
2722                 # ZFS does not support fallocate for now
2723                 out=$(fallocate -p -o 1MiB -l 1MiB $tf 2>&1) ||
2724                         skip_eopnotsupp "$out|punch hole in $tf failed"
2725                 verify_flr_state $tf "wp"
2726         fi
2727
2728         dd if=/dev/zero of=$tf bs=4096 count=4 || error "write $tf failed"
2729         $LFS mirror resync $tf || error "mirror resync $tf failed"
2730         verify_flr_state $tf "ro"
2731
2732         $MULTIOP $tf OSMWUc || error "$MULTIOP $tf failed"
2733         verify_flr_state $tf "wp"
2734 }
2735 run_test 50c "punch_hole/mmap_write stale other mirrors"
2736
2737 test_50d() {
2738         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2739                 skip "OST does not support SEEK_HOLE"
2740         (( $LINUX_VERSION_CODE > $(version_code 3.0.0) )) ||
2741                 skip "client kernel does not support SEEK_HOLE"
2742
2743         local file=$DIR/$tdir/$tfile
2744         local offset
2745         local prt
2746         local rc
2747
2748         mkdir -p $DIR/$tdir
2749
2750         echo " ** create mirrored file $file"
2751         $LFS mirror create -N -E1M -c1 -S1M -E eof \
2752                 -N -E2M -S1M -E eof -S2M $file ||
2753                 error "cannot create mirrored file"
2754         echo " ** write data chunk at 1M boundary"
2755         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2756                 error "cannot write data at 1M boundary"
2757         echo " ** create hole at the file start"
2758         prt=$(fallocate -p -o 0 -l 1M $file 2>&1)
2759         rc=$?
2760
2761         if [[ $rc -eq 0 ]]; then
2762                 verify_flr_state $file "wp"
2763         elif [[ ! $prt =~ unsupported ]]; then
2764                 error "punch hole in $file failed: $prt"
2765         else
2766                 skip "Fallocate punch is not supported: $prt"
2767         fi
2768
2769         echo " ** verify sparseness"
2770         offset=$(lseek_test -d 1000 $file)
2771         echo "    first data offset: $offset"
2772         (( $offset >= 1024 * 1024 )) ||
2773                 error "src: data is not expected at offset $offset"
2774
2775         echo " ** resync mirror #2"
2776         $LFS mirror resync $file
2777
2778         # check llapi_mirror_copy_many correctness
2779         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2780         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2781         [[ $sum_1 == $sum_2 ]] ||
2782                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2783
2784         cancel_lru_locks osc
2785
2786         # stale first component in mirror #1
2787         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,nosync $file
2788         echo " ** verify sparseness of mirror #2"
2789         offset=$(lseek_test -d 1000 $file)
2790         echo "    first data offset: $offset"
2791         (( $offset >= 1024 * 1024 )) ||
2792                 error "src: data is not expected at offset $offset"
2793 }
2794 run_test 50d "mirror rsync keep holes"
2795
2796 test_60a() {
2797         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2798                 skip "OST does not support SEEK_HOLE"
2799
2800         local file=$DIR/$tdir/$tfile
2801         local old_size=2147483648 # 2GiB
2802         local new_size
2803
2804         mkdir -p $DIR/$tdir
2805         dd if=/dev/urandom of=$file bs=4096 count=1 seek=$((134217728 / 4096))
2806         $TRUNCATE $file $old_size
2807
2808         $LFS mirror extend -N -c 1 $file
2809         dd if=/dev/urandom of=$file bs=4096 count=1 seek=$((134217728 / 4096)) conv=notrunc
2810         $LFS mirror resync $file
2811
2812         new_size=$(stat --format='%s' $file)
2813         if ((new_size != old_size)); then
2814                 error "new_size ($new_size) is not equal to old_size ($old_size)"
2815         fi
2816
2817         rm $file
2818 }
2819 run_test 60a "mirror extend sets correct size on sparse file"
2820
2821 get_flr_layout_gen() {
2822         getfattr -n lustre.lov --only-values $tf 2>/dev/null |
2823                 od -tx4 | awk '/000000/ { print "0x"$4; exit; }'
2824 }
2825
2826 check_layout_gen() {
2827         local tf=$1
2828
2829         local v1=$(get_flr_layout_gen $tf)
2830         local v2=$($LFS getstripe -v $tf | awk '/lcm_layout_gen/ { print $2 }')
2831
2832         [[ $v1 -eq $v2 ]] ||
2833                 error "$tf in-memory layout gen $v1 != $v2 after $2"
2834 }
2835
2836 test_60b() {
2837         local tf=$DIR/$tdir/$tfile
2838
2839         test_mkdir $DIR/$tdir
2840
2841         $LFS setstripe -Eeof $tf || error "setstripe $tf failed"
2842
2843         for ((i = 0; i < 20; i++)); do
2844                 $LFS mirror extend -N $tf ||
2845                         error "extending mirror for $tf failed"
2846                 check_layout_gen $tf "extend"
2847
2848                 $LFS mirror split -d --mirror-id=$((i+1)) $tf ||
2849                         error "split $tf failed"
2850                 check_layout_gen $tf "split"
2851         done
2852 }
2853 run_test 60b "mirror merge/split cancel client's in-memory layout gen"
2854
2855 get_times_61() {
2856         stat --format='%X %Y %Z' $file || error "$file: cannot get times"
2857 }
2858
2859 check_times_61() {
2860         local file=$1
2861         local -a old=( $2 $3 $4 )
2862         local -a new
2863
2864         new=( $(get_times_61 $file) )
2865         ((${old[0]} == ${new[0]})) ||
2866                 error "$file: atime: old '${old[0]}' != new '${new[0]}'"
2867
2868         ((${old[1]} == ${new[1]})) ||
2869                 error "$file: mtime: old '${old[1]}' != new '${new[1]}'"
2870 }
2871
2872 test_61a() { # LU-14508
2873         local file=$DIR/$tdir/$tfile
2874         local -a tim
2875         local nap=5
2876
2877         mkdir -p $DIR/$tdir
2878         echo "create $file"
2879         $LFS setstripe -E1M -Eeof $file || error "setstripe $file failed"
2880         echo "create $file-2"
2881         $LFS setstripe -E2M -Eeof $file-2 || error "setstripe $file-2 failed"
2882
2883         echo XXX > $file || error "write $file failed"
2884         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
2885
2886         echo "sleep $nap seconds, then cat $tfile"
2887         sleep $nap
2888         cat $file || error "cat $file failed"
2889
2890         echo "sleep $nap seconds, then re-write $tfile"
2891         sleep $nap
2892         echo XXXX > $file || error "write $file failed"
2893         cp -p $file $file-2 || error "copy $file-2 failed"
2894
2895         echo "sleep $nap seconds"
2896         sleep $nap
2897
2898         tim=( $(get_times_61 $file) )
2899
2900         echo "mirror merge $tfile-2 to $tfile and test timestamps"
2901         $LFS mirror extend -N -f $file-2 $file ||
2902                 error "cannot mirror merge $file-2 to $file"
2903         check_times_61 $file "${tim[@]}"
2904
2905         echo "mirror extend $tfile and test timestamps"
2906         $LFS mirror extend -N -c1 -i1 $file ||
2907                 error "cannot extend mirror $file"
2908         check_times_61 $file "${tim[@]}"
2909
2910         echo "migrate $tfile and test timestamps"
2911         $LFS migrate -n $file || error "cannot migrate $file"
2912         check_times_61 $file "${tim[@]}"
2913
2914         echo "normal user migrate $tfile and test timestamps"
2915         $RUNAS $LFS migrate -n $file || error "cannot migrate $file"
2916         check_times_61 $file "${tim[@]}"
2917 }
2918 run_test 61a "mirror extend and migrate preserve timestamps"
2919
2920 test_61b() { # LU-14508
2921         local file=$DIR/$tdir/$tfile
2922         local -a tim
2923         local nap=5
2924
2925         mkdir -p $DIR/$tdir
2926         echo "create $file"
2927         echo XXX > $file || error "create $file failed"
2928         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
2929
2930         echo "sleep $nap seconds, then cat $tfile"
2931         sleep $nap
2932         cat $file || error "cat $file failed"
2933
2934         echo "sleep $nap seconds, then re-write $tfile"
2935         sleep $nap
2936         echo XXXX > $file || error "write $file failed"
2937
2938         echo "sleep $nap seconds, then test timestamps"
2939         sleep $nap
2940
2941         tim=( $(get_times_61 $file) )
2942
2943         echo "mirror extend $tfile and test timestamps"
2944         $LFS mirror extend -N -c1 -i1 $file ||
2945                 error "cannot extend mirror $file"
2946         check_times_61 $file "${tim[@]}"
2947
2948         echo "mirror split $tfile and test timestamps"
2949         $LFS mirror split -d --mirror-id=1 $file ||
2950                 error "cannot split mirror 1 off $file"
2951         check_times_61 $file "${tim[@]}"
2952
2953         echo "normal user mirror extend $tfile and test timestamps"
2954         $RUNAS $LFS mirror extend -N -c1 -i1 $file ||
2955                 error "cannot extend mirror $file"
2956         check_times_61 $file "${tim[@]}"
2957 }
2958 run_test 61b "mirror extend and split preserve timestamps"
2959
2960 test_61c() { # LU-14508
2961         local file=$DIR/$tdir/$tfile
2962         local -a tim
2963         local nap=5
2964
2965         mkdir -p $DIR/$tdir
2966         echo "create $file"
2967         echo XXX > $file || error "create $file failed"
2968         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
2969
2970         echo "sleep $nap seconds, then cat $tfile"
2971         sleep $nap
2972         cat $file || error "cat $file failed"
2973
2974         echo "sleep $nap seconds, then mirror extend $tfile and write it"
2975         sleep $nap
2976         $LFS mirror extend -N -c1 -i1 $file ||
2977                 error "cannot extend mirror $file"
2978         echo XXXX > $file || error "write $file failed"
2979
2980         echo "sleep $nap seconds, then resync $tfile and test timestamps"
2981         tim=( $(get_times_61 $file) )
2982         sleep $nap
2983         $LFS mirror resync $file || error "cannot resync mirror $file"
2984         check_times_61 $file "${tim[@]}"
2985
2986         echo XXXXXX > $file || error "write $tfile failed"
2987
2988         echo "normal user resync $tfile and test timestamps"
2989         tim=( $(get_times_61 $file) )
2990         $RUNAS $LFS mirror resync $file || error "cannot resync mirror $file"
2991         check_times_61 $file "${tim[@]}"
2992 }
2993 run_test 61c "mirror resync preserves timestamps"
2994
2995 test_70() {
2996         local tf=$DIR/$tdir/$tfile
2997
2998         test_mkdir $DIR/$tdir
2999
3000         while true; do
3001                 rm -f $tf
3002                 $LFS mirror create -N -E 1M -c -1 -E eof -N $tf
3003                 echo xxxx > $tf
3004         done &
3005         c_pid=$!
3006         echo "mirror create pid $c_pid"
3007
3008         while true; do
3009                 $LFS mirror split -d --mirror-id=1 $tf &> /dev/null
3010         done &
3011         s_pid=$!
3012         echo "mirror split pid $s_pid"
3013
3014         echo "mirror create and split race for 60 seconds, should not crash"
3015         sleep 60
3016         kill -9 $c_pid &> /dev/null
3017         kill -9 $s_pid &> /dev/null
3018
3019         rm -f $tf
3020         true
3021 }
3022 run_test 70 "mirror create and split race"
3023
3024 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
3025 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
3026
3027 write_file_200() {
3028         local tf=$1
3029
3030         local fsize=$(stat --printf=%s $tf)
3031
3032         while [ -f $ctrl_file ]; do
3033                 local off=$((RANDOM << 8))
3034                 local len=$((RANDOM << 5 + 131072))
3035
3036                 [ $((off + len)) -gt $fsize ] && {
3037                         fsize=$((off + len))
3038                         echo "Extending file size to $fsize .."
3039                 }
3040
3041                 flock -s $lock_file -c \
3042                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
3043                                 { rm -f $ctrl_file;
3044                                   error "failed writing to $off:$len"; }
3045                 sleep 0.$((RANDOM % 2 + 1))
3046         done
3047 }
3048
3049 read_file_200() {
3050         local tf=$1
3051
3052         while [ -f $ctrl_file ]; do
3053                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
3054                         { rm -f $ctrl_file; error "read failed"; }
3055                 sleep 0.$((RANDOM % 2 + 1))
3056         done
3057 }
3058
3059 resync_file_200() {
3060         local tf=$1
3061
3062         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
3063
3064         exec 200<>$lock_file
3065         while [ -f $ctrl_file ]; do
3066                 local lock_taken=false
3067                 local index=$((RANDOM % ${#options[@]}))
3068                 local cmd="mirror_io resync ${options[$index]}"
3069
3070                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
3071
3072                 [ $((RANDOM % 4)) -eq 0 ] && {
3073                         index=0
3074                         lock_taken=true
3075                         echo -n "lock to "
3076                 }
3077
3078                 echo -n "resync file $tf with '$cmd' .."
3079
3080                 if [[ $lock_taken = "true" ]]; then
3081                         flock -x 200
3082                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
3083                         flock -u 200
3084                 else
3085                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
3086                 fi
3087
3088                 sleep 0.$((RANDOM % 8 + 1))
3089         done
3090 }
3091
3092 test_200() {
3093         local tf=$DIR/$tfile
3094         local tf2=$DIR2/$tfile
3095         local tf3=$DIR3/$tfile
3096
3097         $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
3098         $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
3099         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
3100
3101         $LFS mirror extend -N -f $tf-2 $tf ||
3102                 error "merging $tf-2 into $tf failed"
3103         $LFS mirror extend -N -f $tf-3 $tf ||
3104                 error "merging $tf-3 into $tf failed"
3105
3106         mkdir -p $MOUNT2 && mount_client $MOUNT2
3107
3108         mkdir -p $MOUNT3 && mount_client $MOUNT3
3109
3110         verify_flr_state $tf3 "ro"
3111
3112         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
3113         $LCTL set_param fail_loc=0x1A03
3114
3115         local mds_idx=mds$(($($LFS getstripe -m $tf) + 1))
3116         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
3117
3118         declare -a pids
3119
3120         write_file_200 $tf &
3121         pids+=($!)
3122
3123         read_file_200 $tf &
3124         pids+=($!)
3125
3126         write_file_200 $tf2 &
3127         pids+=($!)
3128
3129         read_file_200 $tf2 &
3130         pids+=($!)
3131
3132         resync_file_200 $tf3 &
3133         pids+=($!)
3134
3135         local sleep_time=60
3136         [ "$SLOW" = "yes" ] && sleep_time=360
3137         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
3138                 sleep 1
3139                 ((--sleep_time))
3140         done
3141
3142         rm -f $ctrl_file
3143
3144         echo "Waiting ${pids[*]}"
3145         wait "${pids[@]}"
3146
3147         umount_client $MOUNT2
3148         umount_client $MOUNT3
3149
3150         rm -f $lock_file
3151
3152         # resync and verify mirrors
3153         $LFS mirror resync $tf || error "final resync failed"
3154         get_mirror_ids $tf
3155
3156         local csum=$($LFS mirror read -N "${mirror_array[0]}" $tf | md5sum)
3157
3158         for id in "${mirror_array[@]:1}"; do
3159                 [ "$($LFS mirror read -N $id $tf | md5sum)" = "$csum" ] ||
3160                         error "checksum error for mirror $id"
3161         done
3162
3163         true
3164 }
3165 run_test 200 "stress test"
3166
3167 cleanup_test_201() {
3168         trap 0
3169         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
3170
3171         umount_client $MOUNT2
3172 }
3173
3174 test_201() {
3175         local delay=${RESYNC_DELAY:-5}
3176
3177         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
3178                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
3179
3180         trap cleanup_test_201 EXIT
3181
3182         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
3183                         changelog_register -n)
3184
3185         mkdir -p $MOUNT2 && mount_client $MOUNT2
3186
3187         local index=0
3188         while :; do
3189                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
3190                 [ -z "$log" ] && { sleep 1; continue; }
3191
3192                 index=$(echo $log | awk '{print $1}')
3193                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
3194                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
3195                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
3196
3197                 ((++index))
3198                 [ -z "$file" ] && continue
3199
3200                 local now=$(date +%s)
3201
3202                 echo "file: $file $fid was modified at $ts, now: $now, " \
3203                      "will be resynced at $((ts+delay))"
3204
3205                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
3206
3207                 $LFS mirror resync $file
3208                 echo "$file resync done"
3209         done
3210
3211         cleanup_test_201
3212 }
3213 run_test 201 "FLR data mover"
3214
3215 test_202() {
3216         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
3217
3218         local tf=$DIR/$tfile
3219         local ids
3220
3221         $LFS setstripe -E 1M -S 1M -c 1 $tf
3222         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
3223         verify_comp_attr stripe-count $tf ${ids[0]} 1
3224
3225         $LFS setstripe --component-add -E 2M -c -1 $tf
3226         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
3227         verify_comp_attr stripe-count $tf ${ids[0]} 1
3228         verify_comp_attr stripe-count $tf ${ids[1]} -1
3229
3230         dd if=/dev/zero of=$tf bs=1M count=2
3231         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
3232         verify_comp_attr stripe-count $tf ${ids[0]} 1
3233         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
3234 }
3235 run_test 202 "lfs setstripe --add-component wide striping"
3236
3237 test_203() {
3238         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
3239                 skip "Need MDS version at least 2.11.55"
3240         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
3241
3242         local tf=$DIR/$tfile
3243
3244         #create 2 mirrors
3245         $LFS mirror create -N2 -c1 $tf || error "create FLR file $tf"
3246         #delete first mirror
3247         $LFS mirror delete --mirror-id=1 $tf || error "delete first mirror"
3248
3249         $LFS getstripe $tf
3250         local old_id=$($LFS getstripe --mirror-id=2 -I $tf)
3251         local count=$($LFS getstripe --mirror-id=2 -c $tf) ||
3252                 error "getstripe count of mirror 2"
3253         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
3254
3255         #extend a mirror with 2 OSTs
3256         $LFS mirror extend -N -c2 $tf || error "extend mirror"
3257         $LFS getstripe $tf
3258
3259         local new_id=$($LFS getstripe --mirror-id=2 -I $tf)
3260         count=$($LFS getstripe --mirror-id=2 -c $tf) ||
3261                 error "getstripe count of mirror 2"
3262         [[ x$old_id = x$new_id ]] ||
3263                 error "mirror 2 changed ID from $old_id to $new_id"
3264         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
3265
3266         count=$($LFS getstripe --mirror-id=3 -c $tf) ||
3267                 error "getstripe count of mirror 3"
3268         [[ x$count = x2 ]] || error "mirror 3 stripe count $count is not 2"
3269 }
3270 run_test 203 "mirror file preserve mirror ID"
3271
3272 # Simple test of FLR + self-extending layout, SEL in non-primary mirror
3273 test_204a() {
3274         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3275                 skip "skipped for lustre < $SEL_VER"
3276
3277         local comp_file=$DIR/$tdir/$tfile
3278         local flg_opts=""
3279         local found=""
3280
3281         test_mkdir $DIR/$tdir
3282
3283         # first mirror is 0-10M, then 10M-(-1), second mirror is 1M followed
3284         # by extension space to -1
3285         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E-1 -z64M $comp_file ||
3286                 error "Create $comp_file failed"
3287
3288         # Write to first component, extending & staling second mirror
3289         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
3290                 error "dd to extend + stale failed"
3291
3292         $LFS getstripe $comp_file
3293
3294         flg_opts="--component-flags init,stale"
3295         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3296         [ $found -eq 1 ] || error "write: Second comp end incorrect"
3297
3298         flg_opts="--component-flags extension"
3299         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3300         [ $found -eq 1 ] || error "write: Third comp start incorrect"
3301
3302         # mirror resync should not change the extents
3303         $LFS mirror resync $comp_file
3304
3305         flg_opts="--component-flags init"
3306         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3307         [ $found -eq 1 ] || error "resync: Second comp end incorrect"
3308
3309         flg_opts="--component-flags extension"
3310         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3311         [ $found -eq 1 ] || error "resync: Third comp start incorrect"
3312
3313         sel_layout_sanity $comp_file 5
3314
3315         rm -f $comp_file
3316 }
3317 run_test 204a "FLR write/stale/resync tests with self-extending mirror"
3318
3319 # Simple test of FLR + self-extending layout, SEL in primary mirror
3320 test_204b() {
3321         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3322                 skip "skipped for lustre < $SEL_VER"
3323
3324         local comp_file=$DIR/$tdir/$tfile
3325         local flg_opts=""
3326         local found=""
3327
3328         test_mkdir $DIR/$tdir
3329
3330         # first mirror is 1M followed by extension space to -1, second mirror
3331         # is 0-10M, then 10M-(-1),
3332         $LFS setstripe -N -E 1M -E-1 -z64M -N -E 10M -E-1 $comp_file ||
3333                 error "Create $comp_file failed"
3334
3335         # Write to first component, extending first component & staling
3336         # other mirror
3337         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
3338                 error "dd to extend + stale failed"
3339
3340         $LFS getstripe $comp_file
3341
3342         flg_opts="--component-flags init"
3343         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3344         [ $found -eq 1 ] || error "write: First comp end incorrect"
3345
3346         flg_opts="--component-flags extension"
3347         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3348         [ $found -eq 1 ] || error "write: Second comp start incorrect"
3349
3350         flg_opts="--component-flags init,stale"
3351         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
3352         [ $found -eq 1 ] || error "write: First mirror comp flags incorrect"
3353
3354         # This component is staled because it overlaps the extended first
3355         # component of the primary mirror, even though it doesn't overlap
3356         # the actual write - thus not inited.
3357         flg_opts="--component-flags stale"
3358         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
3359         [ $found -eq 1 ] || error "write: Second mirror comp flags incorrect"
3360
3361         # mirror resync should not change the extents
3362         $LFS mirror resync $comp_file
3363
3364         $LFS getstripe $comp_file
3365
3366         flg_opts="--component-flags init"
3367         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3368         [ $found -eq 1 ] || error "resync: First comp end incorrect"
3369
3370         flg_opts="--component-flags extension"
3371         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3372         [ $found -eq 1 ] || error "resync: Second comp start incorrect"
3373
3374         flg_opts="--component-flags init"
3375         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
3376         [ $found -eq 1 ] || error "resync: First mirror comp flags incorrect"
3377
3378         flg_opts="--component-flags init"
3379         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
3380         [ $found -eq 1 ] || error "resync: Second mirror comp flags incorrect"
3381
3382         sel_layout_sanity $comp_file 5
3383
3384         rm -f $comp_file
3385 }
3386 run_test 204b "FLR write/stale/resync tests with self-extending primary"
3387
3388 # FLR + SEL failed extension & component removal
3389 # extension space in second mirror
3390 test_204c() {
3391         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3392         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3393                 skip "skipped for lustre < $SEL_VER"
3394
3395         local comp_file=$DIR/$tdir/$tfile
3396         local found=""
3397         local ost_idx1=0
3398         local ost_name=$(ostname_from_index $ost_idx1)
3399
3400         test_mkdir $DIR/$tdir
3401
3402         # first mirror is is 0-10M, then 10M-(-1), second mirror is 0-1M, then
3403         # extension space from 1M to 1G, then normal space to -1
3404         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E 1G -i $ost_idx1 -z 64M \
3405                 -E -1 $comp_file || error "Create $comp_file failed"
3406
3407         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
3408         sleep_maxage
3409
3410         # write to first comp (0 - 10M) of mirror 1, extending + staling
3411         # first + second comp of mirror 2
3412         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3413         RC=$?
3414
3415         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
3416         sleep_maxage
3417
3418         [ $RC -eq 0 ] || error "dd to extend + stale failed"
3419
3420         $LFS getstripe $comp_file
3421
3422         found=$($LFS find --component-start 0m --component-end 1m \
3423                 --comp-flags init,stale $comp_file | wc -l)
3424         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
3425
3426         found=$($LFS find --component-start 1m --component-end EOF \
3427                 --comp-flags stale,^init $comp_file | wc -l)
3428         [ $found -eq 1 ] || error "write: Second mirror comp incorrect"
3429
3430         local mirror_id=$($LFS getstripe --component-start=1m   \
3431                          --component-end=EOF $comp_file |       \
3432                          grep lcme_mirror_id | awk '{ print $2 }')
3433
3434         [[ $mirror_id -eq 2 ]] ||
3435                 error "component not in correct mirror? $mirror_id"
3436
3437         $LFS mirror resync $comp_file
3438
3439         $LFS getstripe $comp_file
3440
3441         # component dimensions should not change from resync
3442         found=$($LFS find --component-start 1m --component-end EOF \
3443                 --component-flags init $comp_file | wc -l)
3444         [ $found -eq 1 ] || error "resync: Second mirror comp incorrect"
3445
3446         sel_layout_sanity $comp_file 4
3447
3448         rm -f $comp_file
3449 }
3450 run_test 204c "FLR write/stale/resync test with component removal"
3451
3452 # Successful repeated component in primary mirror
3453 test_204d() {
3454         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3455         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3456                 skip "skipped for lustre < $SEL_VER"
3457
3458         local comp_file=$DIR/$tdir/$tfile
3459         local found=""
3460
3461         wait_delete_completed
3462         wait_mds_ost_sync
3463         test_mkdir $DIR/$tdir
3464
3465         # first mirror is 64M followed by extension space to -1, second mirror
3466         # is 0-10M, then 10M-(-1)
3467         $LFS setstripe -N -E-1 -z64M -N -E 10M -E-1 $comp_file ||
3468                 error "Create $comp_file failed"
3469
3470         local ost_idx1=$($LFS getstripe -I65537 -i $comp_file)
3471         local ost_name=$(ostname_from_index $ost_idx1)
3472         # degrade OST for first comp so we won't extend there
3473         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3474                 obdfilter.$ost_name.degraded=1
3475         sleep_maxage
3476
3477         # Write beyond first component, causing repeat & stale second mirror
3478         dd if=/dev/zero bs=1M count=1 seek=66 of=$comp_file conv=notrunc
3479         RC=$?
3480
3481         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3482                 obdfilter.$ost_name.degraded=0
3483         sleep_maxage
3484
3485         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
3486
3487         $LFS getstripe $comp_file
3488
3489         found=$($LFS find --component-start 64m --component-end 128m \
3490                 --component-flags init $comp_file | wc -l)
3491         [ $found -eq 1 ] || error "write: Repeat comp incorrect"
3492
3493         local ost_idx2=$($LFS getstripe --component-start=64m           \
3494                          --component-end=128m --component-flags=init    \
3495                          -i $comp_file)
3496         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
3497         local mirror_id=$($LFS getstripe --component-start=64m          \
3498                          --component-end=128m --component-flags=init    \
3499                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3500         [[ $mirror_id -eq 1 ]] ||
3501                 error "component not in correct mirror: $mirror_id, not 1"
3502
3503         $LFS mirror resync $comp_file
3504
3505         $LFS getstripe $comp_file
3506
3507         # component dimensions should not change from resync
3508         found=$($LFS find --component-start 0m --component-end 64m \
3509                 --component-flags init $comp_file | wc -l)
3510         [ $found -eq 1 ] || error "resync: first comp incorrect"
3511         found=$($LFS find --component-start 64m --component-end 128m \
3512                 --component-flags init $comp_file | wc -l)
3513         [ $found -eq 1 ] || error "resync: repeat comp incorrect"
3514
3515         sel_layout_sanity $comp_file 5
3516
3517         rm -f $comp_file
3518 }
3519 run_test 204d "FLR write/stale/resync sel test with repeated comp"
3520
3521 # Successful repeated component, SEL in non-primary mirror
3522 test_204e() {
3523         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3524         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3525                 skip "skipped for lustre < $SEL_VER"
3526
3527         local comp_file=$DIR/$tdir/$tfile
3528         local found=""
3529
3530         wait_delete_completed
3531         wait_mds_ost_sync
3532
3533         test_mkdir $DIR/$tdir
3534
3535         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
3536         # space to -1 (-z 64M, so first comp is 0-64M)
3537         # Note: we have to place both 1st components on OST0, otherwise 2 OSTs
3538         # will be not enough - one will be degraded, the other is used on
3539         # an overlapping mirror.
3540         $LFS setstripe -N -E 100M -i 0 -E-1 -N -E-1 -i 0 -z 64M $comp_file ||
3541                 error "Create $comp_file failed"
3542
3543         local ost_idx1=$($LFS getstripe --component-start=0 \
3544                          --component-end=64m -i $comp_file)
3545         local ost_name=$(ostname_from_index $ost_idx1)
3546         # degrade OST for first comp of 2nd mirror so we won't extend there
3547         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3548                 obdfilter.$ost_name.degraded=1
3549         sleep_maxage
3550
3551         $LFS getstripe $comp_file
3552
3553         # Write to first component, stale & instantiate second mirror components
3554         # overlapping with the written component (0-100M);
3555         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3556         RC=$?
3557
3558         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3559                 obdfilter.$ost_name.degraded=0
3560         sleep_maxage
3561         $LFS getstripe $comp_file
3562
3563         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
3564
3565         found=$($LFS find --component-start 0m --component-end 64m \
3566                 --component-flags init,stale $comp_file | wc -l)
3567         [ $found -eq 1 ] || error "write: first comp incorrect"
3568
3569         # was repeated due to degraded ost
3570         found=$($LFS find --component-start 64m --component-end 128m \
3571                 --component-flags init,stale $comp_file | wc -l)
3572         [ $found -eq 1 ] || error "write: repeated comp incorrect"
3573
3574         local ost_idx2=$($LFS getstripe --component-start=64m           \
3575                          --component-end=128m --component-flags=init    \
3576                          -i $comp_file)
3577         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
3578         local mirror_id=$($LFS getstripe --component-start=0m           \
3579                          --component-end=64m --component-flags=init     \
3580                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3581         [[ $mirror_id -eq 2 ]] ||
3582                 error "component not in correct mirror? $mirror_id"
3583
3584         $LFS mirror resync $comp_file
3585
3586         $LFS getstripe $comp_file
3587
3588         # component dimensions should not change from resync
3589         found=$($LFS find --component-start 0m --component-end 64m \
3590                 --component-flags init,^stale $comp_file | wc -l)
3591         [ $found -eq 1 ] || error "resync: first comp incorrect"
3592         found=$($LFS find --component-start 64m --component-end 128m \
3593                 --component-flags init,^stale $comp_file | wc -l)
3594         [ $found -eq 1 ] || error "resync: repeated comp incorrect"
3595
3596         sel_layout_sanity $comp_file 5
3597
3598         rm -f $comp_file
3599 }
3600 run_test 204e "FLR write/stale/resync sel test with repeated comp"
3601
3602 # FLR + SEL: failed repeated component, SEL in non-primary mirror
3603 test_204f() {
3604         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3605         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3606                 skip "skipped for lustre < $SEL_VER"
3607
3608         local comp_file=$DIR/$tdir/$tfile
3609         local found=""
3610
3611         wait_delete_completed
3612         wait_mds_ost_sync
3613         test_mkdir $DIR/$tdir
3614
3615         pool_add $TESTNAME || error "Pool creation failed"
3616         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
3617
3618         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
3619         # space to -1 (-z 64M, so first comp is 0-64M)
3620         $LFS setstripe -N -E 100M -E-1 -N --pool="$TESTNAME" \
3621                 -E-1 -c 1 -z 64M $comp_file || error "Create $comp_file failed"
3622
3623         local ost_name0=$(ostname_from_index 0)
3624         local ost_name1=$(ostname_from_index 1)
3625
3626         # degrade both OSTs in pool, so we'll try to repeat, then fail and
3627         # extend original comp
3628         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=1
3629         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=1
3630         sleep_maxage
3631
3632         # a write to the 1st component, 100M length, which will try to stale
3633         # the first 100M of mirror 2, attempting to extend its 0-64M component
3634         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3635         RC=$?
3636
3637         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=0
3638         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=0
3639         sleep_maxage
3640
3641         [ $RC -eq 0 ] || error "dd to extend mirror comp failed"
3642
3643         $LFS getstripe $comp_file
3644
3645         found=$($LFS find --component-start 0m --component-end 128m \
3646                 --component-flags init,stale $comp_file | wc -l)
3647         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
3648
3649         local mirror_id=$($LFS getstripe --component-start=0m           \
3650                          --component-end=128m --component-flags=init    \
3651                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3652
3653         [[ $mirror_id -eq 2 ]] ||
3654                 error "component not in correct mirror? $mirror_id, not 2"
3655
3656         $LFS mirror resync $comp_file
3657
3658         $LFS getstripe $comp_file
3659
3660         # component dimensions should not change from resync
3661         found=$($LFS find --component-start 0m --component-end 128m \
3662                 --component-flags init,^stale $comp_file | wc -l)
3663         [ $found -eq 1 ] || error "resync: First mirror comp incorrect"
3664
3665         sel_layout_sanity $comp_file 4
3666
3667         rm -f $comp_file
3668 }
3669 run_test 204f "FLR write/stale/resync sel w/forced extension"
3670
3671 function test_205() {
3672         local tf=$DIR/$tfile
3673         local mirrors
3674
3675         $LFS setstripe -c1 $tf
3676         $LFS mirror extend -N $tf
3677         mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
3678         (( $mirrors == 2 )) || error "no new mirror was created?"
3679
3680         $LFS mirror extend -N --flags=prefer $tf
3681         mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
3682         (( $mirrors == 3 )) || error "no new mirror was created?"
3683
3684         $($LFS getstripe $tf | grep lcme_flags: | tail -1 | grep -q prefer) ||
3685                 error "prefer flag was not set on the new mirror"
3686 }
3687 run_test 205 "lfs mirror extend to set prefer flag"
3688
3689 function test_206() {
3690         # create a new OST pool
3691         local pool_name=$TESTNAME
3692
3693         create_pool $FSNAME.$pool_name ||
3694                 error "create OST pool $pool_name failed"
3695         # add OSTs into the pool
3696         pool_add_targets $pool_name 0 1 ||
3697                 error "add OSTs into pool $pool_name failed"
3698
3699         $LFS setstripe -c1 --pool=$pool_name $DIR/$tfile ||
3700                 error "can't setstripe"
3701         $LFS mirror extend -N $DIR/$tfile ||
3702                 error "can't create replica"
3703         if $LFS getstripe $DIR/$tfile | grep -q prefer ; then
3704                 $LFS getstripe $DIR/$tfile
3705                 error "prefer found"
3706         fi
3707         $LFS setstripe --comp-set --comp-flags=prefer -p $pool_name $DIR/$tfile || {
3708                 $LFS getstripe $DIR/$tfile
3709                 error "can't setstripe prefer"
3710         }
3711
3712         if ! $LFS getstripe $DIR/$tfile | grep -q prefer ; then
3713                 $LFS getstripe $DIR/$tfile
3714                 error "no prefer found"
3715         fi
3716
3717         # destroy OST pool
3718         destroy_test_pools
3719 }
3720 run_test 206 "lfs setstripe -pool .. --comp-flags=.. "
3721
3722 test_207() {
3723        local file=$DIR/$tfile
3724        local tmpfile=$DIR/$tfile-tt
3725
3726         [ $MDS1_VERSION -lt $(version_code 2.14.50) ] &&
3727                 skip "Need MDS version at least 2.14.50"
3728
3729         stack_trap "rm -f $tmpfile $file"
3730
3731         # generate data for verification
3732         dd if=/dev/urandom of=$tmpfile bs=1M count=1 ||
3733                 error "can't generate file with random data"
3734
3735         # create a mirrored file with one stale replica
3736         $LFS mirror create -N -S 4M -c 2 -N -S 1M -c -1 $file ||
3737                 error "create mirrored file $file failed"
3738         get_mirror_ids $file
3739         echo "mirror IDs: ${mirror_array[*]}"
3740
3741         dd if=$tmpfile of=$file bs=1M || error "can't copy"
3742         get_mirror_ids $file
3743         echo "mirror IDs: ${mirror_array[*]}"
3744
3745         drop_client_cache
3746         cmp $tmpfile $file || error "files don't match"
3747         get_mirror_ids $file
3748         echo "mirror IDs: ${mirror_array[*]}"
3749
3750         # mirror creation should work fine
3751         $LFS mirror extend -N -S 8M -c -1 $file ||
3752                 error "mirror extend $file failed"
3753
3754         get_mirror_ids $file
3755         echo "mirror IDs: ${mirror_array[*]}"
3756
3757         drop_client_cache
3758         $LFS mirror verify -v $file || error "verification failed"
3759         cmp $tmpfile $file || error "files don't match"
3760 }
3761 run_test 207 "create another replica with existing out-of-sync one"
3762
3763 function check_ost_used() {
3764         local ddarg
3765         local ost
3766         local i
3767         local file=$1
3768         local io=$2
3769
3770         shift 2
3771
3772         cancel_lru_locks osc # to drop pages
3773         cancel_lru_locks mdc # to refresh layout
3774         # XXX: cancel_lru_locks mdc doesn't work
3775         # XXX: need a better way to reload the layout
3776         umount_client $MOUNT || error "umount failed"
3777         mount_client $MOUNT || error "mount failed"
3778
3779         # refresh non-rotation status on MDTs
3780         sleep 10
3781         touch $DIR/$tfile-temp
3782         rm -f $DIR/$tfile-temp
3783         # refresh non-rotational status on the client
3784         $LFS df >&/dev/null
3785         sleep 2
3786
3787         $LCTL set_param osc.*.stats=clear >/dev/null
3788         if [[ $io == "read" ]]; then
3789                 ddarg="if=$file of=/dev/null"
3790         elif [[ $io == "write" ]]; then
3791                 ddarg="if=/dev/zero of=$file"
3792         else
3793                 error "unknown type $io"
3794         fi
3795         dd $ddarg bs=2M count=1 || error "can't $io $file"
3796         cancel_lru_locks osc
3797
3798         # check only specified OSTs got reads
3799         for ((ost = 0; ost < $OSTCOUNT; ost++)); do
3800                 local nr=$($LCTL get_param -n \
3801                         osc.$FSNAME-OST000$ost-osc-[-0-9a-f]*.stats |
3802                         awk "/ost_$io/{print \$2}")
3803                 nr=${nr:-0}
3804                 if [[ " $* " =~ $ost ]]; then
3805                         (( nr > 0 )) || error "expected reads on $ost"
3806                 else
3807                         (( nr == 0 )) || error "unexpected $nr reads on $ost"
3808                 fi
3809         done
3810 }
3811
3812 test_208a() {
3813         local tf=$DIR/$tfile
3814         local osts=$(comma_list $(osts_nodes))
3815
3816         (( $OSTCOUNT >= 4 )) || skip "needs >= 4 OSTs"
3817         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
3818                 skip "Need MDS version at least 2.14.55"
3819
3820         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
3821
3822         save_lustre_params $(get_facets OST) osd*.*OST*.nonrotational > $p
3823         stack_trap "restore_lustre_params < $p; rm -f $p"
3824
3825         stack_trap "rm -f $tf"
3826         $LFS setstripe -i0 -c1 $tf || error "can't setstripe"
3827         dd if=/dev/zero of=$tf bs=2M count=1 || error "can't dd (1)"
3828         $LFS mirror extend -N -c1 -o1 $tf || error "can't create mirror"
3829         $LFS mirror extend -N -c2 -o 2,3 $tf || error "can't create mirror"
3830         $LFS mirror resync $tf || error "can't resync"
3831         $LFS getstripe $tf
3832
3833         log "set OST0000 non-rotational"
3834         do_nodes $osts \
3835                 $LCTL set_param osd*.*OST0000*.nonrotational=1
3836         check_ost_used $tf read 0
3837
3838         log "set OST0002 and OST0003 non-rotational, two fast OSTs is better"
3839         do_nodes $osts \
3840                 $LCTL set_param osd*.*OST0002*.nonrotational=1 \
3841                         osd*.*OST0003*.nonrotational=1
3842         check_ost_used $tf read 2 3
3843
3844         log "set mirror 1 on OST0001 preferred"
3845         $LFS setstripe --comp-set -I 0x20001 --comp-flags=prefer $tf ||
3846                 error "can't set prefer"
3847         check_ost_used $tf read 1
3848 }
3849 run_test 208a "mirror selection to prefer non-rotational devices for reads"
3850
3851 test_208b() {
3852         local tf=$DIR/$tfile
3853         local osts=$(comma_list $(osts_nodes))
3854
3855         (( $OSTCOUNT >= 4 )) || skip "needs >= 4 OSTs"
3856         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
3857                 skip "Need MDS version at least 2.14.55"
3858
3859         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
3860
3861         save_lustre_params $(get_facets OST) osd*.*OST*.nonrotational > $p
3862         stack_trap "restore_lustre_params < $p; rm -f $p"
3863
3864         stack_trap "rm -f $tf"
3865         $LFS setstripe -i0 -c1 $tf || error "can't setstripe"
3866         dd if=/dev/zero of=$tf bs=2M count=1 || error "can't dd (1)"
3867         $LFS mirror extend -N -c1 -o1 $tf || error "can't create mirror"
3868         $LFS mirror extend -N -c2 -o 2,3 $tf || error "can't create mirror"
3869         $LFS mirror resync $tf || error "can't resync"
3870         $LFS getstripe $tf | grep -q flags.*stale && error "still stale"
3871
3872         log "set OST0000 non-rotational"
3873         do_nodes $osts \
3874                 $LCTL set_param osd*.*OST0000*.nonrotational=1
3875         check_ost_used $tf write 0
3876         $LFS mirror resync $tf || error "can't resync"
3877
3878         log "set OST0002 and OST0003 non-rotational, two fast OSTs is better"
3879         do_nodes $osts \
3880                 $LCTL set_param osd*.*OST0002*.nonrotational=1 \
3881                         osd*.*OST0003*.nonrotational=1
3882         check_ost_used $tf write 2 3
3883         $LFS mirror resync $tf || error "can't resync"
3884
3885         log "set mirror 1 on OST0001 preferred"
3886         $LFS setstripe --comp-set -I 0x20001 --comp-flags=prefer $tf ||
3887                 error "can't set prefer"
3888         check_ost_used $tf write 1
3889 }
3890 run_test 208b "mirror selection to prefer non-rotational devices for writes"
3891
3892 complete $SECONDS
3893 check_and_cleanup_lustre
3894 exit_status