Whamcloud - gitweb
LU-15574 tests: Skip test sanity/77o in interop
[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         rm -rf $DIR/$tdir
2084         local tf=$DIR/$tdir/$tfile
2085
2086         mkdir -p $DIR/$tdir || error "create directory failed"
2087
2088         echo XXX > $tf
2089
2090         # create 2 mirrors file
2091         $LFS mirror extend -N -c1 $tf
2092
2093         echo YYY > $tf
2094
2095         verify_flr_state $tf "wp"
2096
2097         local str=$(cat $tf)
2098
2099         [[ $str == "YYY" ]] || error "$tf content is not YYY"
2100
2101         # get the non-stale mirror id
2102         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
2103                         tr '\n' ' '))
2104         local mirror_ids=($($LFS getstripe $tf |
2105                         awk '/lcme_mirror_id/{print $2}' | tr '\n' ' '))
2106         for ((i = 0; i < 2; i++)); do
2107                 $LFS getstripe -I${ids[$i]} --component-flags $tf |
2108                         grep stale > /dev/null || break
2109         done
2110
2111         [[ $i -ge 2 ]] && ( $LFS getstripe $tf; error "no stale mirror" )
2112
2113         $LFS getstripe $tf
2114
2115         # split the updated mirror, should fail
2116         echo "split mirror_id ${mirror_ids[$i]} id ${ids[$i]}, should fail"
2117         $LFS mirror split --mirror-id=${mirror_ids[$i]} $tf &> /dev/null &&
2118                 error "split --mirror-id=${mirror_ids[$i]} $tf should fail"
2119
2120         i=$(( 1 - i ))
2121         # split the stale mirror
2122         echo "split mirror_id ${mirror_ids[$i]} id ${ids[$i]}"
2123         $LFS mirror split --mirror-id=${mirror_ids[$i]} -d $tf ||
2124                 error "mirror split --mirror-id=${mirror_ids[$i]} $tf failed"
2125
2126         echo "make sure there's no stale comp in the file"
2127         # make sure there's no stale comp in the file
2128         $LFS getstripe $tf | awk '/lcme_flags/{print $2}' | grep stale &&
2129                 ( $LFS getstripe $tf; error "stale mirror file" )
2130
2131         str=$(cat $tf)
2132         [[ $str == "YYY" ]] ||
2133                 ( cat $tf; error "$tf content is not YYY after split" )
2134 }
2135 run_test 44b "mirror split does not create stale file"
2136
2137 test_44c() {
2138         local tf=$DIR/$tdir/$tfile
2139
2140         stack_trap "rm -f $tf"
2141
2142         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
2143                 skip "Need MDS version at least 2.14.52"
2144
2145         [ "$FSTYPE" != "zfs" ] || skip "ZFS file's block number is not accurate"
2146
2147         mkdir -p $DIR/$tdir || error "create directroy failed"
2148
2149         dd if=/dev/zero of=$tf bs=1M count=10 || error "dd write $tfile failed"
2150         sync
2151         block1=$(( $(stat -c "%b*%B" $tf) ))
2152         echo " ** before mirror ops, file blocks=$((block1/1024)) KiB"
2153
2154         $LFS mirror extend -N2 -c1 $tf || error "mirror extend $tfile failed"
2155         sync
2156         block2=$(( $(stat -c "%b*%B" $tf) ))
2157         echo " ** after mirror extend, file blocks=$((block2/1024)) KiB"
2158
2159         $LFS mirror split -d --mirror-id=2 $tf ||
2160                 error "mirror split $tfile failed"
2161         $LFS mirror split -d --mirror-id=3 $tf ||
2162                 error "mirror split $tfile failed"
2163         sync
2164         lfs getsom $tf
2165         block3=$(( $(stat -c "%b*%B" $tf) ))
2166         echo " ** after mirror split, file blocks=$((block3/1024)) KiB"
2167
2168         [[ $block1 -eq $block3 ]] ||
2169                 error "mirror split does not reduce block# $block3 != $block1"
2170 }
2171 run_test 44c "lfs mirror split reduces block size of a file"
2172
2173 test_45() {
2174         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
2175
2176         local file=$DIR/$tdir/$tfile
2177         local dir=$DIR/$tdir/$dir
2178         local temp=$DIR/$tdir/template
2179         rm -rf $DIR/$tdir
2180         test_mkdir $DIR/$tdir
2181
2182         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
2183                 -N -E3m -S1m -Eeof -N -E8m -Eeof $file ||
2184                         error "Create $file failed"
2185
2186         verify_yaml_layout $file $file.copy $temp "1. FLR file"
2187         rm -f $file $file.copy
2188
2189         $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
2190                 -N -E3m -S1m -Eeof -N -E8m --flags=prefer -Eeof $file ||
2191                         error "Create $file failed"
2192
2193         verify_yaml_layout $file $file.copy $temp "2. FLR file with flags"
2194 }
2195 run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
2196
2197 verify_46() {
2198         local src=$1
2199         local dst=$2
2200         local msg_prefix=$3
2201
2202         $LFS setstripe --copy=$src $dst || error "setstripe $dst failed"
2203
2204         local layout1=$(get_layout_param $src)
2205         local layout2=$(get_layout_param $dst)
2206         # compare their layout info
2207         [ "$layout1" == "$layout2" ] ||
2208                 error "$msg_prefix $src <=> $dst layouts are not equal"
2209 }
2210
2211 test_46() {
2212         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2213
2214         local file=$DIR/$tdir/$tfile
2215         test_mkdir $DIR/$tdir
2216
2217         ########################### 1. PFL file #############################
2218         echo "  ** 1. PFL file"
2219         rm -f $file
2220         $LFS setstripe -E1m -S 1M -c2 -o0,1 -E2m -c2 -E3m -o1,0 -E4m -c1 -E-1 \
2221                 $file || error "1. Create PFL $file failed"
2222
2223         rm -f $file.copy
2224         verify_46 $file $file.copy "1. PFL file"
2225
2226         ########################### 2. plain file ###########################
2227         echo "  ** 2. plain file"
2228         rm -f $file
2229         $LFS setstripe -c2 -o0,1 -i1 $file ||
2230                 error "2. Create plain $file failed"
2231
2232         rm -f $file.copy
2233         verify_46 $file $file.copy "2. plain file"
2234
2235         ########################### 3. FLR file #############################
2236         echo "  ** 3. FLR file"
2237         rm -f $file
2238         $LFS setstripe -N -E1m -S 1M -c2 -o0,1 -E4m -c1 -Eeof -N -E16m -Eeof \
2239                 $file || error "3. Create FLR $file failed"
2240
2241         rm -f $file.copy
2242         verify_46 $file $file.copy "3. FLR file"
2243
2244         local dir=$DIR/$tdir/dir
2245         ########################### 4. PFL dir ##############################
2246         echo "  ** 4. PFL dir"
2247         test_mkdir $dir
2248         $LFS setstripe -E1m -S 1M -c2 -E2m -c1 -E-1 $dir ||
2249                 error "4. setstripe PFL $dir failed"
2250
2251         test_mkdir $dir.copy
2252         verify_46 $dir $dir.copy "4. PFL dir"
2253
2254         ########################### 5. plain dir ############################
2255         echo "  ** 5. plain dir"
2256         $LFS setstripe -c2 -i-1 $dir || error "5. setstripe plain $dir failed"
2257
2258         verify_46 $dir $dir.copy "5. plain dir"
2259
2260         ########################### 6. FLR dir ##############################
2261         echo "  ** 6. FLR dir"
2262         $LFS setstripe -N -E1m -S 1M -c2 -E2m -c1 -Eeof -N -E4m -Eeof $dir ||
2263                 error "6. setstripe FLR $dir failed"
2264
2265         verify_46 $dir $dir.copy "6. FLR dir"
2266
2267         (( $MDS1_VERSION >= $(version_code 2.13.53.205) )) ||
2268                 echo "server version $MDS1_VERSION does not support SEL" &&
2269                 return 0
2270
2271         ########################### 7. SEL file ##############################
2272         echo "  ** 7. SEL file"
2273         rm -f $file
2274         $LFS setstripe -E256M -S 1M -c2 -o0,1 -z 64M -E-1 -o1,0 -z 128M \
2275                 $file || error "Create $file failed"
2276
2277         rm -f $file.copy
2278         verify_46 $file $file.copy "7. SEL file"
2279
2280         ########################### 8. SEL dir ##############################
2281         echo "  ** 8. SEL dir"
2282         $LFS setstripe -E256M -S 1M -c2 -z 64M -E-1 -z 128M \
2283                 $dir || error "setstripe $dir failed"
2284
2285         verify_46 $dir $dir.copy "8. SEL dir"
2286
2287         ########################### 9. FLR SEL file ##########################
2288         echo "  ** 9. FLR SEL file"
2289         rm -f $file
2290         $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
2291                 -N -E1G -c4 -z128M -E-1 -z256M $file || error "Create $file failed"
2292
2293         rm -f $file.copy
2294         verify_46 $file $file.copy "9. SEL file"
2295
2296         ########################### 10. FLR SEL dir #########################
2297         echo "  ** 10. FLR SEL dir"
2298         $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
2299                 -N -E1G -c4 -z128M -E-1 -z256M $dir || error "Create $file failed"
2300
2301         verify_46 $dir $dir.copy "10. SEL dir"
2302 }
2303 run_test 46 "Verify setstripe --copy option"
2304
2305 test_47() {
2306         [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
2307
2308         local file=$DIR/$tdir/$tfile
2309         local ids
2310         local ost
2311         local osts
2312
2313         test_mkdir $DIR/$tdir
2314
2315         # test case 1:
2316         rm -f $file
2317         # mirror1: [comp0]ost0,    [comp1]ost1 and ost2
2318         # mirror2: [comp2]    ,    [comp3] should not use ost1 or ost2
2319         $LFS mirror create -N -E2m -c1 -o0 --flags=prefer -Eeof -c2 -o1,2 \
2320                 -N -E2m -c1 -Eeof -c1 $file || error "create FLR $file failed"
2321         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2322
2323         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
2324         $LFS mirror resync $file || error "resync $file failed"
2325
2326         ost=$($LFS getstripe -I${ids[2]} $file | awk '/l_ost_idx/{print $5}')
2327         if [[ x$ost == "x0," ]]; then
2328                 $LFS getstripe $file
2329                 error "component ${ids[2]} objects allocated on $ost " \
2330                       "shouldn't on OST0"
2331         fi
2332
2333         ost=$($LFS getstripe -I${ids[3]} $file | awk '/l_ost_idx/{print $5}')
2334         if [[ x$ost == "x1," || x$ost == "x2," ]]; then
2335                 $LFS getstripe $file
2336                 error "component ${ids[3]} objects allocated on $ost " \
2337                       "shouldn't on OST1 or on OST2"
2338         fi
2339
2340         ## test case 2:
2341         rm -f $file
2342         # mirror1: [comp0]    [comp1]
2343         # mirror2: [comp2]    [comp3]
2344         # mirror3: [comp4]    [comp5]
2345         # mirror4: [comp6]    [comp7]
2346         $LFS mirror create -N4 -E1m -c1 -Eeof -c1 $file ||
2347                 error "create FLR $file failed"
2348         ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
2349
2350         dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
2351         $LFS mirror resync $file || error "resync $file failed"
2352
2353         for ((i = 0; i < 6; i++)); do
2354                 osts[$i]=$($LFS getstripe -I${ids[$i]} $file |
2355                         awk '/l_ost_idx/{print $5}')
2356         done
2357         # comp[0],comp[2],comp[4] should use different osts
2358         if [[ ${osts[0]} == ${osts[2]} || ${osts[0]} == ${osts[4]} ||
2359               ${osts[2]} == ${osts[4]} ]]; then
2360                 $LFS getstripe $file
2361                 error "component ${ids[0]}, ${ids[2]}, ${ids[4]} have objects "\
2362                       "allocated on duplicated OSTs"
2363         fi
2364         # comp[1],comp[3],comp[5] should use different osts
2365         if [[ ${osts[1]} == ${osts[3]} || ${osts[1]} == ${osts[5]} ||
2366               ${osts[3]} == ${osts[5]} ]]; then
2367                 $LFS getstripe $file
2368                 error "component ${ids[1]}, ${ids[3]}, ${ids[5]} have objects "\
2369                       "allocated on duplicated OSTs"
2370         fi
2371
2372         return 0
2373 }
2374 run_test 47 "Verify mirror obj alloc"
2375
2376 test_48() {
2377         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
2378                 skip "Need MDS version at least 2.11.55"
2379
2380         local tf=$DIR/$tfile
2381
2382         rm -f $tf
2383         echo " ** create 2 mirrors FLR file $tf"
2384         $LFS mirror create -N -E2M -Eeof --flags prefer \
2385                            -N -E1M -Eeof $tf ||
2386                 error "create FLR file $tf failed"
2387
2388         echo " ** write it"
2389         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2390         verify_flr_state $tf "wp"
2391
2392         local sum0=$(md5sum < $tf)
2393
2394         echo " ** resync the file"
2395         $LFS mirror resync $tf
2396
2397         echo " ** snapshot mirror 2"
2398         $LFS setstripe --comp-set -I 0x20003 --comp-flags=nosync $tf
2399
2400         echo " ** write it again"
2401         dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
2402         echo " ** resync it again"
2403         $LFS mirror resync $tf
2404
2405         verify_flr_state $tf "wp"
2406         verify_comp_attr lcme_flags $tf 0x20003 nosync,stale
2407
2408         local sum1=$($LFS mirror read -N1 $tf | md5sum)
2409         local sum2=$($LFS mirror read -N2 $tf | md5sum)
2410
2411         echo " ** verify mirror 2 doesn't change"
2412         echo "original checksum: $sum0"
2413         echo "mirror 1 checksum: $sum1"
2414         echo "mirror 2 checksum: $sum2"
2415         [[ $sum0 = $sum2 ]] ||
2416                 error "original checksum: $sum0, mirror 2 checksum: $sum2"
2417         echo " ** mirror 2 stripe info"
2418         $LFS getstripe -v --mirror-index=2 $tf
2419
2420         echo " ** resync mirror 2"
2421         $LFS mirror resync --only 2 $tf
2422
2423         verify_flr_state $tf "ro"
2424         verify_comp_attr lcme_flags $tf 0x20003 nosync,^stale
2425
2426         sum1=$($LFS mirror read -N1 $tf | md5sum)
2427         sum2=$($LFS mirror read -N2 $tf | md5sum)
2428
2429         echo " ** verify mirror 2 resync-ed"
2430         echo "original checksum: $sum0"
2431         echo "mirror 1 checksum: $sum1"
2432         echo "mirror 2 checksum: $sum2"
2433         [[ $sum1 = $sum2 ]] ||
2434                 error "mirror 1 checksum: $sum1, mirror 2 checksum: $sum2"
2435         echo " ** mirror 2 stripe info"
2436         $LFS getstripe -v --mirror-index=2 $tf
2437 }
2438 run_test 48 "Verify snapshot mirror"
2439
2440 OLDIFS="$IFS"
2441 cleanup_49() {
2442         trap 0
2443         IFS="$OLDIFS"
2444 }
2445
2446 test_49a() {
2447         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
2448
2449         trap cleanup_49 EXIT RETURN
2450
2451         local file=$DIR/$tfile
2452
2453         $LFS setstripe -N -E eof -c1 -o1 -N -E eof -c1 -o0 $file ||
2454                 error "setstripe on $file"
2455
2456         dd if=/dev/zero of=$file bs=1M count=1 || error "dd failed for $file"
2457         $LFS mirror resync $file
2458
2459         filefrag -ves $file || error "filefrag $file failed"
2460         filefrag_op=$(filefrag -ve -k $file |
2461                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
2462
2463 #Filesystem type is: bd00bd0
2464 #File size of /mnt/lustre/f49a.sanity-flr is 1048576 (1024 blocks of 1024 bytes)
2465 # ext:     device_logical:        physical_offset: length:  dev: flags:
2466 #   0:        0..    1023:    1572864..   1573887:   1024: 0001: net,eof
2467 #   1:        0..    1023:    1572864..   1573887:   1024: 0000: last,net,eof
2468 #/mnt/lustre/f49a.sanity-flr: 2 extents found
2469
2470         last_lun=$(echo $filefrag_op | cut -d: -f5)
2471         IFS=$'\n'
2472         tot_len=0
2473         num_luns=1
2474         for line in $filefrag_op; do
2475                 frag_lun=$(echo $line | cut -d: -f5)
2476                 ext_len=$(echo $line | cut -d: -f4)
2477                 if [[ "$frag_lun" != "$last_lun" ]]; then
2478                         if (( tot_len != 1024 )); then
2479                                 cleanup_49
2480                                 error "$file: OST$last_lun $tot_len != 1024"
2481                         else
2482                                 (( num_luns += 1 ))
2483                                 tot_len=0
2484                         fi
2485                 fi
2486                 (( tot_len += ext_len ))
2487                 last_lun=$frag_lun
2488         done
2489         if (( num_luns != 2 || tot_len != 1024 )); then
2490                 cleanup_49
2491                 error "$file: $num_luns != 2, $tot_len != 1024 on OST$last_lun"
2492         fi
2493
2494         echo "FIEMAP on $file succeeded"
2495 }
2496 run_test 49a "FIEMAP upon FLR file"
2497
2498 test_50A() {    # EX-2179
2499         mkdir -p $DIR/$tdir
2500
2501         local file=$DIR/$tdir/$tfile
2502
2503         $LFS setstripe -c1 -i0 $file || error "setstripe $file failed"
2504
2505         $LFS mirror extend -N -c1 -i1 $file ||
2506                 error "extending mirror for $file failed"
2507
2508         local olv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
2509
2510         fail mds1
2511
2512         $LFS mirror split -d --mirror-id=1 $file || error "split $file failed"
2513
2514         local flv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
2515
2516         echo "$file layout generation from $olv to $flv"
2517         (( $flv != ($olv + 1) )) &&
2518                 error "split does not increase layout gen from $olv to $flv"
2519
2520         dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
2521
2522         $LFS getstripe -v $file || error "getstripe $file failed"
2523 }
2524 run_test 50A "mirror split update layout generation"
2525
2526 test_50a() {
2527         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2528                 skip "OST does not support SEEK_HOLE"
2529
2530         local file=$DIR/$tdir/$tfile
2531         local offset
2532         local sum1
2533         local sum2
2534         local blocks
2535
2536         mkdir -p $DIR/$tdir
2537
2538         echo " ** create striped file $file"
2539         $LFS setstripe -E 1M -c1 -S 1M -E eof -c2 -S1M $file ||
2540                 error "cannot create file with PFL layout"
2541         echo " ** write 1st data chunk at 1M boundary"
2542         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2543                 error "cannot write data at 1M boundary"
2544         echo " ** write 2nd data chunk at 2M boundary"
2545         dd if=/dev/urandom of=$file bs=1k count=20 seek=2041 ||
2546                 error "cannot write data at 2M boundary"
2547         echo " ** create hole at the file end"
2548         $TRUNCATE $file 3700000 || error "truncate fails"
2549
2550         echo " ** verify sparseness"
2551         offset=$(lseek_test -d 1000 $file)
2552         echo "    first data offset: $offset"
2553         [[ $offset == 1000 ]] &&
2554                 error "src: data is not expected at offset $offset"
2555         offset=$(lseek_test -l 3500000 $file)
2556         echo "    hole at the end: $offset"
2557         [[ $offset == 3500000 ]] ||
2558                 error "src: hole is expected at offset $offset"
2559
2560         echo " ** extend the file with new mirror"
2561         # migrate_copy_data() is used
2562         $LFS mirror extend -N -E 2M -S 1M -E 1G -S 2M -E eof $file ||
2563                 error "cannot create mirror"
2564         $LFS getstripe $file | grep lcme_flags | grep stale > /dev/null &&
2565                 error "$file still has stale component"
2566
2567         # check migrate_data_copy() was correct
2568         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2569         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2570         [[ $sum_1 == $sum_2 ]] ||
2571                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2572
2573         # stale first mirror
2574         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale $file
2575         $LFS setstripe --comp-set -I0x10002 --comp-flags=stale $file
2576
2577         echo " ** verify mirror #2 sparseness"
2578         offset=$(lseek_test -d 1000 $file)
2579         echo "    first data offset: $offset"
2580         [[ $offset == 1000 ]] &&
2581                 error "dst: data is not expected at offset $offset"
2582         offset=$(lseek_test -l 3500000 $file)
2583         echo "    hole at the end: $offset"
2584         [[ $offset == 3500000 ]] ||
2585                 error "dst: hole is expected at offset $offset"
2586
2587         echo " ** copy mirror #2 to mirror #1"
2588         $LFS mirror copy -i 2 -o 1 $file || error "mirror copy fails"
2589         $LFS getstripe $file | grep lcme_flags | grep stale > /dev/null &&
2590                 error "$file still has stale component"
2591
2592         # check llapi_mirror_copy_many correctness
2593         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2594         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2595         [[ $sum_1 == $sum_2 ]] ||
2596                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2597
2598         # stale 1st component of mirror #2 before lseek call
2599         $LFS setstripe --comp-set -I0x20001 --comp-flags=stale $file
2600
2601         echo " ** verify mirror #1 sparseness again"
2602         offset=$(lseek_test -d 1000 $file)
2603         echo "    first data offset: $offset"
2604         [[ $offset == 1000 ]] &&
2605                 error "dst: data is not expected at offset $offset"
2606         offset=$(lseek_test -l 3500000 $file)
2607         echo "    hole at the end: $offset"
2608         [[ $offset == 3500000 ]] ||
2609                 error "dst: hole is expected at offset $offset"
2610
2611         cancel_lru_locks osc
2612
2613         blocks=$(stat -c%b $file)
2614         echo " ** final consumed blocks: $blocks"
2615         # for 3.5Mb file consumes ~6000 blocks, use 1000 to check
2616         # that file is still sparse
2617         (( blocks < 1000 )) ||
2618                 error "Mirrored file consumes $blocks blocks"
2619
2620         rm $file
2621 }
2622 run_test 50a "mirror extend/copy preserves sparseness"
2623
2624 test_50b() {
2625         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2626                 skip "OST does not support SEEK_HOLE"
2627
2628         local file=$DIR/$tdir/$tfile
2629         local offset
2630         local sum1
2631         local sum2
2632         local blocks
2633
2634         mkdir -p $DIR/$tdir
2635
2636         echo " ** create mirrored file $file"
2637         $LFS mirror create -N -E1M -c1 -S1M -E eof \
2638                 -N -E2M -S1M -E eof -S2M $file ||
2639                 error "cannot create mirrored file"
2640         echo " ** write data chunk at 1M boundary"
2641         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2642                 error "cannot write data at 1M boundary"
2643         echo " ** create hole at the file end"
2644         $TRUNCATE $file 3700000 || error "truncate fails"
2645
2646         echo " ** verify sparseness"
2647         offset=$(lseek_test -d 1000 $file)
2648         echo "    first data offset: $offset"
2649         [[ $offset == 1000 ]] &&
2650                 error "src: data is not expected at offset $offset"
2651         offset=$(lseek_test -l 3500000 $file)
2652         echo "    hole at the end: $offset"
2653         [[ $offset == 3500000 ]] ||
2654                 error "src: hole is expected at 3500000"
2655
2656         echo " ** resync mirror #2 to mirror #1"
2657         $LFS mirror resync $file
2658
2659         # check llapi_mirror_copy_many correctness
2660         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2661         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2662         [[ $sum_1 == $sum_2 ]] ||
2663                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2664
2665         cancel_lru_locks osc
2666
2667         blocks=$(stat -c%b $file)
2668         echo " ** consumed blocks: $blocks"
2669         # without full punch() support the first component can be not sparse
2670         # but the last one should be, so file should use far fewer blocks
2671         (( blocks < 5000 )) ||
2672                 error "Mirrored file consumes $blocks blocks"
2673
2674         # stale first component in mirror #1
2675         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,nosync $file
2676         echo " ** truncate file down"
2677         $TRUNCATE $file 0
2678         echo " ** write data chunk at 2M boundary"
2679         dd if=/dev/urandom of=$file bs=1k count=20 seek=2041 conv=notrunc ||
2680                 error "cannot write data at 2M boundary"
2681         echo " ** resync mirror #2 to mirror #1 with nosync 1st component"
2682         $LFS mirror resync $file || error "mirror rsync fails"
2683         # first component is still stale
2684         $LFS getstripe $file | grep 'lcme_flags:.*stale' > /dev/null ||
2685                 error "$file still has no stale component"
2686         echo " ** resync mirror #2 to mirror #1 again"
2687         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,^nosync $file
2688         $LFS mirror resync $file || error "mirror rsync fails"
2689         $LFS getstripe $file | grep 'lcme_flags:.*stale' > /dev/null &&
2690                 error "$file still has stale component"
2691
2692         # check llapi_mirror_copy_many correctness
2693         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2694         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2695         [[ $sum_1 == $sum_2 ]] ||
2696                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2697
2698         cancel_lru_locks osc
2699
2700         blocks=$(stat -c%b $file)
2701         echo " ** final consumed blocks: $blocks"
2702         # while the first component can lose sparseness, the last one should
2703         # not, so whole file should still use far fewer blocks in total
2704         (( blocks < 3000 )) ||
2705                 error "Mirrored file consumes $blocks blocks"
2706         rm $file
2707 }
2708 run_test 50b "mirror rsync handles sparseness"
2709
2710 test_50c() {
2711         local tf=$DIR/$tdir/$tfile
2712
2713         test_mkdir $DIR/$tdir
2714
2715         $LFS setstripe -N2 -c-1 $tf || error "create FLR $tf failed"
2716         verify_flr_state $tf "ro"
2717
2718         if [[ "$FSTYPE" == "ldiskfs" ]]; then
2719                 # ZFS does not support fallocate for now
2720                 fallocate -p -o 1MiB -l 1MiB $tf ||
2721                         error "punch hole in $tf failed"
2722                 verify_flr_state $tf "wp"
2723         fi
2724
2725         dd if=/dev/zero of=$tf bs=4096 count=4 || error "write $tf failed"
2726         $LFS mirror resync $tf || error "mirror resync $tf failed"
2727         verify_flr_state $tf "ro"
2728
2729         $MULTIOP $tf OSMWUc || error "$MULTIOP $tf failed"
2730         verify_flr_state $tf "wp"
2731 }
2732 run_test 50c "punch_hole/mmap_write stale other mirrors"
2733
2734 test_50d() {
2735         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2736                 skip "OST does not support SEEK_HOLE"
2737         (( $LINUX_VERSION_CODE > $(version_code 3.0.0) )) ||
2738                 skip "client kernel does not support SEEK_HOLE"
2739
2740         local file=$DIR/$tdir/$tfile
2741         local offset
2742         local prt
2743         local rc
2744
2745         mkdir -p $DIR/$tdir
2746
2747         echo " ** create mirrored file $file"
2748         $LFS mirror create -N -E1M -c1 -S1M -E eof \
2749                 -N -E2M -S1M -E eof -S2M $file ||
2750                 error "cannot create mirrored file"
2751         echo " ** write data chunk at 1M boundary"
2752         dd if=/dev/urandom of=$file bs=1k count=20 seek=1021 ||
2753                 error "cannot write data at 1M boundary"
2754         echo " ** create hole at the file start"
2755         prt=$(fallocate -p -o 0 -l 1M $file 2>&1)
2756         rc=$?
2757
2758         if [[ $rc -eq 0 ]]; then
2759                 verify_flr_state $file "wp"
2760         elif [[ ! $prt =~ "unsupported" ]]; then
2761                 error "punch hole in $file failed: $prt"
2762         else
2763                 skip "Fallocate punch is not supported"
2764         fi
2765
2766         echo " ** verify sparseness"
2767         offset=$(lseek_test -d 1000 $file)
2768         echo "    first data offset: $offset"
2769         (( $offset >= 1024 * 1024 )) ||
2770                 error "src: data is not expected at offset $offset"
2771
2772         echo " ** resync mirror #2"
2773         $LFS mirror resync $file
2774
2775         # check llapi_mirror_copy_many correctness
2776         sum_1=$($LFS mirror read -N 1 $file | md5sum)
2777         sum_2=$($LFS mirror read -N 2 $file | md5sum)
2778         [[ $sum_1 == $sum_2 ]] ||
2779                 error "data mismatch: \'$sum_1\' vs. \'$sum_2\'"
2780
2781         cancel_lru_locks osc
2782
2783         # stale first component in mirror #1
2784         $LFS setstripe --comp-set -I0x10001 --comp-flags=stale,nosync $file
2785         echo " ** verify sparseness of mirror #2"
2786         offset=$(lseek_test -d 1000 $file)
2787         echo "    first data offset: $offset"
2788         (( $offset >= 1024 * 1024 )) ||
2789                 error "src: data is not expected at offset $offset"
2790 }
2791 run_test 50d "mirror rsync keep holes"
2792
2793 test_60a() {
2794         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
2795                 skip "OST does not support SEEK_HOLE"
2796
2797         local file=$DIR/$tdir/$tfile
2798         local old_size=2147483648 # 2GiB
2799         local new_size
2800
2801         mkdir -p $DIR/$tdir
2802         dd if=/dev/urandom of=$file bs=4096 count=1 seek=$((134217728 / 4096))
2803         $TRUNCATE $file $old_size
2804
2805         $LFS mirror extend -N -c 1 $file
2806         dd if=/dev/urandom of=$file bs=4096 count=1 seek=$((134217728 / 4096)) conv=notrunc
2807         $LFS mirror resync $file
2808
2809         new_size=$(stat --format='%s' $file)
2810         if ((new_size != old_size)); then
2811                 error "new_size ($new_size) is not equal to old_size ($old_size)"
2812         fi
2813
2814         rm $file
2815 }
2816 run_test 60a "mirror extend sets correct size on sparse file"
2817
2818 get_flr_layout_gen() {
2819         getfattr -n lustre.lov --only-values $tf 2>/dev/null |
2820                 od -tx4 | awk '/000000/ { print "0x"$4; exit; }'
2821 }
2822
2823 check_layout_gen() {
2824         local tf=$1
2825
2826         local v1=$(get_flr_layout_gen $tf)
2827         local v2=$($LFS getstripe -v $tf | awk '/lcm_layout_gen/ { print $2 }')
2828
2829         [[ $v1 -eq $v2 ]] ||
2830                 error "$tf in-memory layout gen $v1 != $v2 after $2"
2831 }
2832
2833 test_60b() {
2834         local tf=$DIR/$tdir/$tfile
2835
2836         test_mkdir $DIR/$tdir
2837
2838         $LFS setstripe -Eeof $tf || error "setstripe $tf failed"
2839
2840         for ((i = 0; i < 20; i++)); do
2841                 $LFS mirror extend -N $tf ||
2842                         error "extending mirror for $tf failed"
2843                 check_layout_gen $tf "extend"
2844
2845                 $LFS mirror split -d --mirror-id=$((i+1)) $tf ||
2846                         error "split $tf failed"
2847                 check_layout_gen $tf "split"
2848         done
2849 }
2850 run_test 60b "mirror merge/split cancel client's in-memory layout gen"
2851
2852 get_times_61() {
2853         stat --format='%X %Y %Z' $file || error "$file: cannot get times"
2854 }
2855
2856 check_times_61() {
2857         local file=$1
2858         local -a old=( $2 $3 $4 )
2859         local -a new
2860
2861         new=( $(get_times_61 $file) )
2862         ((${old[0]} == ${new[0]})) ||
2863                 error "$file: atime: old '${old[0]}' != new '${new[0]}'"
2864
2865         ((${old[1]} == ${new[1]})) ||
2866                 error "$file: mtime: old '${old[1]}' != new '${new[1]}'"
2867 }
2868
2869 test_61a() { # LU-14508
2870         local file=$DIR/$tdir/$tfile
2871         local -a tim
2872         local nap=5
2873
2874         mkdir -p $DIR/$tdir
2875         echo "create $file"
2876         $LFS setstripe -E1M -Eeof $file || error "setstripe $file failed"
2877         echo "create $file-2"
2878         $LFS setstripe -E2M -Eeof $file-2 || error "setstripe $file-2 failed"
2879
2880         echo XXX > $file || error "write $file failed"
2881         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
2882
2883         echo "sleep $nap seconds, then cat $tfile"
2884         sleep $nap
2885         cat $file || error "cat $file failed"
2886
2887         echo "sleep $nap seconds, then re-write $tfile"
2888         sleep $nap
2889         echo XXXX > $file || error "write $file failed"
2890         cp -p $file $file-2 || error "copy $file-2 failed"
2891
2892         echo "sleep $nap seconds"
2893         sleep $nap
2894
2895         tim=( $(get_times_61 $file) )
2896
2897         echo "mirror merge $tfile-2 to $tfile and test timestamps"
2898         $LFS mirror extend -N -f $file-2 $file ||
2899                 error "cannot mirror merge $file-2 to $file"
2900         check_times_61 $file ${tim[@]}
2901
2902         echo "mirror extend $tfile and test timestamps"
2903         $LFS mirror extend -N -c1 -i1 $file ||
2904                 error "cannot extend mirror $file"
2905         check_times_61 $file ${tim[@]}
2906
2907         echo "migrate $tfile and test timestamps"
2908         $LFS migrate -n $file || error "cannot migrate $file"
2909         check_times_61 $file ${tim[@]}
2910
2911         echo "normal user migrate $tfile and test timestamps"
2912         $RUNAS $LFS migrate -n $file || error "cannot migrate $file"
2913         check_times_61 $file ${tim[@]}
2914 }
2915 run_test 61a "mirror extend and migrate preserve timestamps"
2916
2917 test_61b() { # LU-14508
2918         local file=$DIR/$tdir/$tfile
2919         local -a tim
2920         local nap=5
2921
2922         mkdir -p $DIR/$tdir
2923         echo "create $file"
2924         echo XXX > $file || error "create $file failed"
2925         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
2926
2927         echo "sleep $nap seconds, then cat $tfile"
2928         sleep $nap
2929         cat $file || error "cat $file failed"
2930
2931         echo "sleep $nap seconds, then re-write $tfile"
2932         sleep $nap
2933         echo XXXX > $file || error "write $file failed"
2934
2935         echo "sleep $nap seconds, then test timestamps"
2936         sleep $nap
2937
2938         tim=( $(get_times_61 $file) )
2939
2940         echo "mirror extend $tfile and test timestamps"
2941         $LFS mirror extend -N -c1 -i1 $file ||
2942                 error "cannot extend mirror $file"
2943         check_times_61 $file ${tim[@]}
2944
2945         echo "mirror split $tfile and test timestamps"
2946         $LFS mirror split -d --mirror-id=1 $file ||
2947                 error "cannot split mirror 1 off $file"
2948         check_times_61 $file ${tim[@]}
2949
2950         echo "normal user mirror extend $tfile and test timestamps"
2951         $RUNAS $LFS mirror extend -N -c1 -i1 $file ||
2952                 error "cannot extend mirror $file"
2953         check_times_61 $file ${tim[@]}
2954 }
2955 run_test 61b "mirror extend and split preserve timestamps"
2956
2957 test_61c() { # LU-14508
2958         local file=$DIR/$tdir/$tfile
2959         local -a tim
2960         local nap=5
2961
2962         mkdir -p $DIR/$tdir
2963         echo "create $file"
2964         echo XXX > $file || error "create $file failed"
2965         chown $RUNAS_ID $DIR/$tdir $file || error "chown $file failed"
2966
2967         echo "sleep $nap seconds, then cat $tfile"
2968         sleep $nap
2969         cat $file || error "cat $file failed"
2970
2971         echo "sleep $nap seconds, then mirror extend $tfile and write it"
2972         sleep $nap
2973         $LFS mirror extend -N -c1 -i1 $file ||
2974                 error "cannot extend mirror $file"
2975         echo XXXX > $file || error "write $file failed"
2976
2977         echo "sleep $nap seconds, then resync $tfile and test timestamps"
2978         tim=( $(get_times_61 $file) )
2979         sleep $nap
2980         $LFS mirror resync $file || error "cannot resync mirror $file"
2981         check_times_61 $file ${tim[@]}
2982
2983         echo XXXXXX > $file || error "write $tfile failed"
2984
2985         echo "normal user resync $tfile and test timestamps"
2986         tim=( $(get_times_61 $file) )
2987         $RUNAS $LFS mirror resync $file || error "cannot resync mirror $file"
2988         check_times_61 $file ${tim[@]}
2989 }
2990 run_test 61c "mirror resync preserves timestamps"
2991
2992 test_70() {
2993         local tf=$DIR/$tdir/$tfile
2994
2995         test_mkdir $DIR/$tdir
2996
2997         while true; do
2998                 rm -f $tf
2999                 $LFS mirror create -N -E 1M -c -1 -E eof -N $tf
3000                 echo xxxx > $tf
3001         done &
3002         c_pid=$!
3003         echo "mirror create pid $c_pid"
3004
3005         while true; do
3006                 $LFS mirror split -d --mirror-id=1 $tf &> /dev/null
3007         done &
3008         s_pid=$!
3009         echo "mirror split pid $s_pid"
3010
3011         echo "mirror create and split race for 60 seconds, should not crash"
3012         sleep 60
3013         kill -9 $c_pid &> /dev/null
3014         kill -9 $s_pid &> /dev/null
3015
3016         rm -f $tf
3017         true
3018 }
3019 run_test 70 "mirror create and split race"
3020
3021 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
3022 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
3023
3024 write_file_200() {
3025         local tf=$1
3026
3027         local fsize=$(stat --printf=%s $tf)
3028
3029         while [ -f $ctrl_file ]; do
3030                 local off=$((RANDOM << 8))
3031                 local len=$((RANDOM << 5 + 131072))
3032
3033                 [ $((off + len)) -gt $fsize ] && {
3034                         fsize=$((off + len))
3035                         echo "Extending file size to $fsize .."
3036                 }
3037
3038                 flock -s $lock_file -c \
3039                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
3040                                 { rm -f $ctrl_file;
3041                                   error "failed writing to $off:$len"; }
3042                 sleep 0.$((RANDOM % 2 + 1))
3043         done
3044 }
3045
3046 read_file_200() {
3047         local tf=$1
3048
3049         while [ -f $ctrl_file ]; do
3050                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
3051                         { rm -f $ctrl_file; error "read failed"; }
3052                 sleep 0.$((RANDOM % 2 + 1))
3053         done
3054 }
3055
3056 resync_file_200() {
3057         local tf=$1
3058
3059         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
3060
3061         exec 200<>$lock_file
3062         while [ -f $ctrl_file ]; do
3063                 local lock_taken=false
3064                 local index=$((RANDOM % ${#options[@]}))
3065                 local cmd="mirror_io resync ${options[$index]}"
3066
3067                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
3068
3069                 [ $((RANDOM % 4)) -eq 0 ] && {
3070                         index=0
3071                         lock_taken=true
3072                         echo -n "lock to "
3073                 }
3074
3075                 echo -n "resync file $tf with '$cmd' .."
3076
3077                 if [[ $lock_taken = "true" ]]; then
3078                         flock -x 200
3079                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
3080                         flock -u 200
3081                 else
3082                         $cmd $tf &> /dev/null && echo "done" || echo "failed"
3083                 fi
3084
3085                 sleep 0.$((RANDOM % 8 + 1))
3086         done
3087 }
3088
3089 test_200() {
3090         local tf=$DIR/$tfile
3091         local tf2=$DIR2/$tfile
3092         local tf3=$DIR3/$tfile
3093
3094         $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
3095         $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
3096         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
3097
3098         $LFS mirror extend -N -f $tf-2 $tf ||
3099                 error "merging $tf-2 into $tf failed"
3100         $LFS mirror extend -N -f $tf-3 $tf ||
3101                 error "merging $tf-3 into $tf failed"
3102
3103         mkdir -p $MOUNT2 && mount_client $MOUNT2
3104
3105         mkdir -p $MOUNT3 && mount_client $MOUNT3
3106
3107         verify_flr_state $tf3 "ro"
3108
3109         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
3110         $LCTL set_param fail_loc=0x1A03
3111
3112         local mds_idx=mds$(($($LFS getstripe -m $tf) + 1))
3113         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
3114
3115         declare -a pids
3116
3117         write_file_200 $tf &
3118         pids+=($!)
3119
3120         read_file_200 $tf &
3121         pids+=($!)
3122
3123         write_file_200 $tf2 &
3124         pids+=($!)
3125
3126         read_file_200 $tf2 &
3127         pids+=($!)
3128
3129         resync_file_200 $tf3 &
3130         pids+=($!)
3131
3132         local sleep_time=60
3133         [ "$SLOW" = "yes" ] && sleep_time=360
3134         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
3135                 sleep 1
3136                 ((--sleep_time))
3137         done
3138
3139         rm -f $ctrl_file
3140
3141         echo "Waiting ${pids[@]}"
3142         wait ${pids[@]}
3143
3144         umount_client $MOUNT2
3145         umount_client $MOUNT3
3146
3147         rm -f $lock_file
3148
3149         # resync and verify mirrors
3150         $LFS mirror resync $tf || error "final resync failed"
3151         get_mirror_ids $tf
3152
3153         local csum=$($LFS mirror read -N ${mirror_array[0]} $tf | md5sum)
3154         for id in ${mirror_array[@]:1}; do
3155                 [ "$($LFS mirror read -N $id $tf | md5sum)" = "$csum" ] ||
3156                         error "checksum error for mirror $id"
3157         done
3158
3159         true
3160 }
3161 run_test 200 "stress test"
3162
3163 cleanup_test_201() {
3164         trap 0
3165         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
3166
3167         umount_client $MOUNT2
3168 }
3169
3170 test_201() {
3171         local delay=${RESYNC_DELAY:-5}
3172
3173         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
3174                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
3175
3176         trap cleanup_test_201 EXIT
3177
3178         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
3179                         changelog_register -n)
3180
3181         mkdir -p $MOUNT2 && mount_client $MOUNT2
3182
3183         local index=0
3184         while :; do
3185                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
3186                 [ -z "$log" ] && { sleep 1; continue; }
3187
3188                 index=$(echo $log | awk '{print $1}')
3189                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
3190                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
3191                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
3192
3193                 ((++index))
3194                 [ -z "$file" ] && continue
3195
3196                 local now=$(date +%s)
3197
3198                 echo "file: $file $fid was modified at $ts, now: $now, " \
3199                      "will be resynced at $((ts+delay))"
3200
3201                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
3202
3203                 $LFS mirror resync $file
3204                 echo "$file resync done"
3205         done
3206
3207         cleanup_test_201
3208 }
3209 run_test 201 "FLR data mover"
3210
3211 test_202() {
3212         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
3213
3214         local tf=$DIR/$tfile
3215         local ids
3216
3217         $LFS setstripe -E 1M -S 1M -c 1 $tf
3218         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
3219         verify_comp_attr stripe-count $tf ${ids[0]} 1
3220
3221         $LFS setstripe --component-add -E 2M -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         verify_comp_attr stripe-count $tf ${ids[1]} -1
3225
3226         dd if=/dev/zero of=$tf bs=1M count=2
3227         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
3228         verify_comp_attr stripe-count $tf ${ids[0]} 1
3229         verify_comp_attr stripe-count $tf ${ids[1]} $OSTCOUNT
3230 }
3231 run_test 202 "lfs setstripe --add-component wide striping"
3232
3233 test_203() {
3234         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
3235                 skip "Need MDS version at least 2.11.55"
3236         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
3237
3238         local tf=$DIR/$tfile
3239
3240         #create 2 mirrors
3241         $LFS mirror create -N2 -c1 $tf || error "create FLR file $tf"
3242         #delete first mirror
3243         $LFS mirror delete --mirror-id=1 $tf || error "delete first mirror"
3244
3245         $LFS getstripe $tf
3246         local old_id=$($LFS getstripe --mirror-id=2 -I $tf)
3247         local count=$($LFS getstripe --mirror-id=2 -c $tf) ||
3248                 error "getstripe count of mirror 2"
3249         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
3250
3251         #extend a mirror with 2 OSTs
3252         $LFS mirror extend -N -c2 $tf || error "extend mirror"
3253         $LFS getstripe $tf
3254
3255         local new_id=$($LFS getstripe --mirror-id=2 -I $tf)
3256         count=$($LFS getstripe --mirror-id=2 -c $tf) ||
3257                 error "getstripe count of mirror 2"
3258         [[ x$oldid = x$newid ]] ||
3259                 error "mirror 2 changed ID from $old_id to $new_id"
3260         [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
3261
3262         count=$($LFS getstripe --mirror-id=3 -c $tf) ||
3263                 error "getstripe count of mirror 3"
3264         [[ x$count = x2 ]] || error "mirror 3 stripe count $count is not 2"
3265 }
3266 run_test 203 "mirror file preserve mirror ID"
3267
3268 # Simple test of FLR + self-extending layout, SEL in non-primary mirror
3269 test_204a() {
3270         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3271                 skip "skipped for lustre < $SEL_VER"
3272
3273         local comp_file=$DIR/$tdir/$tfile
3274         local flg_opts=""
3275         local found=""
3276
3277         test_mkdir $DIR/$tdir
3278
3279         # first mirror is 0-10M, then 10M-(-1), second mirror is 1M followed
3280         # by extension space to -1
3281         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E-1 -z64M $comp_file ||
3282                 error "Create $comp_file failed"
3283
3284         # Write to first component, extending & staling second mirror
3285         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
3286                 error "dd to extend + stale failed"
3287
3288         $LFS getstripe $comp_file
3289
3290         flg_opts="--component-flags init,stale"
3291         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3292         [ $found -eq 1 ] || error "write: Second comp end incorrect"
3293
3294         flg_opts="--component-flags extension"
3295         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3296         [ $found -eq 1 ] || error "write: Third comp start incorrect"
3297
3298         # mirror resync should not change the extents
3299         $LFS mirror resync $comp_file
3300
3301         flg_opts="--component-flags init"
3302         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3303         [ $found -eq 1 ] || error "resync: Second comp end incorrect"
3304
3305         flg_opts="--component-flags extension"
3306         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3307         [ $found -eq 1 ] || error "resync: Third comp start incorrect"
3308
3309         sel_layout_sanity $comp_file 5
3310
3311         rm -f $comp_file
3312 }
3313 run_test 204a "FLR write/stale/resync tests with self-extending mirror"
3314
3315 # Simple test of FLR + self-extending layout, SEL in primary mirror
3316 test_204b() {
3317         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3318                 skip "skipped for lustre < $SEL_VER"
3319
3320         local comp_file=$DIR/$tdir/$tfile
3321         local flg_opts=""
3322         local found=""
3323
3324         test_mkdir $DIR/$tdir
3325
3326         # first mirror is 1M followed by extension space to -1, second mirror
3327         # is 0-10M, then 10M-(-1),
3328         $LFS setstripe -N -E 1M -E-1 -z64M -N -E 10M -E-1 $comp_file ||
3329                 error "Create $comp_file failed"
3330
3331         # Write to first component, extending first component & staling
3332         # other mirror
3333         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
3334                 error "dd to extend + stale failed"
3335
3336         $LFS getstripe $comp_file
3337
3338         flg_opts="--component-flags init"
3339         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3340         [ $found -eq 1 ] || error "write: First comp end incorrect"
3341
3342         flg_opts="--component-flags extension"
3343         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3344         [ $found -eq 1 ] || error "write: Second comp start incorrect"
3345
3346         flg_opts="--component-flags init,stale"
3347         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
3348         [ $found -eq 1 ] || error "write: First mirror comp flags incorrect"
3349
3350         # This component is staled because it overlaps the extended first
3351         # component of the primary mirror, even though it doesn't overlap
3352         # the actual write - thus not inited.
3353         flg_opts="--component-flags stale"
3354         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
3355         [ $found -eq 1 ] || error "write: Second mirror comp flags incorrect"
3356
3357         # mirror resync should not change the extents
3358         $LFS mirror resync $comp_file
3359
3360         $LFS getstripe $comp_file
3361
3362         flg_opts="--component-flags init"
3363         found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
3364         [ $found -eq 1 ] || error "resync: First comp end incorrect"
3365
3366         flg_opts="--component-flags extension"
3367         found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
3368         [ $found -eq 1 ] || error "resync: Second comp start incorrect"
3369
3370         flg_opts="--component-flags init"
3371         found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
3372         [ $found -eq 1 ] || error "resync: First mirror comp flags incorrect"
3373
3374         flg_opts="--component-flags init"
3375         found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
3376         [ $found -eq 1 ] || error "resync: Second mirror comp flags incorrect"
3377
3378         sel_layout_sanity $comp_file 5
3379
3380         rm -f $comp_file
3381 }
3382 run_test 204b "FLR write/stale/resync tests with self-extending primary"
3383
3384 # FLR + SEL failed extension & component removal
3385 # extension space in second mirror
3386 test_204c() {
3387         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3388         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3389                 skip "skipped for lustre < $SEL_VER"
3390
3391         local comp_file=$DIR/$tdir/$tfile
3392         local found=""
3393         local ost_idx1=0
3394         local ost_name=$(ostname_from_index $ost_idx1)
3395
3396         test_mkdir $DIR/$tdir
3397
3398         # first mirror is is 0-10M, then 10M-(-1), second mirror is 0-1M, then
3399         # extension space from 1M to 1G, then normal space to -1
3400         $LFS setstripe -N -E 10M -E-1 -N -E 1M -E 1G -i $ost_idx1 -z 64M \
3401                 -E -1 $comp_file || error "Create $comp_file failed"
3402
3403         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
3404         sleep_maxage
3405
3406         # write to first comp (0 - 10M) of mirror 1, extending + staling
3407         # first + second comp of mirror 2
3408         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3409         RC=$?
3410
3411         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
3412         sleep_maxage
3413
3414         [ $RC -eq 0 ] || error "dd to extend + stale failed"
3415
3416         $LFS getstripe $comp_file
3417
3418         found=$($LFS find --component-start 0m --component-end 1m \
3419                 --comp-flags init,stale $comp_file | wc -l)
3420         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
3421
3422         found=$($LFS find --component-start 1m --component-end EOF \
3423                 --comp-flags stale,^init $comp_file | wc -l)
3424         [ $found -eq 1 ] || error "write: Second mirror comp incorrect"
3425
3426         local mirror_id=$($LFS getstripe --component-start=1m   \
3427                          --component-end=EOF $comp_file |       \
3428                          grep lcme_mirror_id | awk '{ print $2 }')
3429
3430         [[ $mirror_id -eq 2 ]] ||
3431                 error "component not in correct mirror? $mirror_id"
3432
3433         $LFS mirror resync $comp_file
3434
3435         $LFS getstripe $comp_file
3436
3437         # component dimensions should not change from resync
3438         found=$($LFS find --component-start 1m --component-end EOF \
3439                 --component-flags init $comp_file | wc -l)
3440         [ $found -eq 1 ] || error "resync: Second mirror comp incorrect"
3441
3442         sel_layout_sanity $comp_file 4
3443
3444         rm -f $comp_file
3445 }
3446 run_test 204c "FLR write/stale/resync test with component removal"
3447
3448 # Successful repeated component in primary mirror
3449 test_204d() {
3450         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3451         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3452                 skip "skipped for lustre < $SEL_VER"
3453
3454         local comp_file=$DIR/$tdir/$tfile
3455         local found=""
3456
3457         wait_delete_completed
3458         wait_mds_ost_sync
3459         test_mkdir $DIR/$tdir
3460
3461         # first mirror is 64M followed by extension space to -1, second mirror
3462         # is 0-10M, then 10M-(-1)
3463         $LFS setstripe -N -E-1 -z64M -N -E 10M -E-1 $comp_file ||
3464                 error "Create $comp_file failed"
3465
3466         local ost_idx1=$($LFS getstripe -I65537 -i $comp_file)
3467         local ost_name=$(ostname_from_index $ost_idx1)
3468         # degrade OST for first comp so we won't extend there
3469         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3470                 obdfilter.$ost_name.degraded=1
3471         sleep_maxage
3472
3473         # Write beyond first component, causing repeat & stale second mirror
3474         dd if=/dev/zero bs=1M count=1 seek=66 of=$comp_file conv=notrunc
3475         RC=$?
3476
3477         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3478                 obdfilter.$ost_name.degraded=0
3479         sleep_maxage
3480
3481         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
3482
3483         $LFS getstripe $comp_file
3484
3485         found=$($LFS find --component-start 64m --component-end 128m \
3486                 --component-flags init $comp_file | wc -l)
3487         [ $found -eq 1 ] || error "write: Repeat comp incorrect"
3488
3489         local ost_idx2=$($LFS getstripe --component-start=64m           \
3490                          --component-end=128m --component-flags=init    \
3491                          -i $comp_file)
3492         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
3493         local mirror_id=$($LFS getstripe --component-start=64m          \
3494                          --component-end=128m --component-flags=init    \
3495                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3496         [[ $mirror_id -eq 1 ]] ||
3497                 error "component not in correct mirror: $mirror_id, not 1"
3498
3499         $LFS mirror resync $comp_file
3500
3501         $LFS getstripe $comp_file
3502
3503         # component dimensions should not change from resync
3504         found=$($LFS find --component-start 0m --component-end 64m \
3505                 --component-flags init $comp_file | wc -l)
3506         [ $found -eq 1 ] || error "resync: first comp incorrect"
3507         found=$($LFS find --component-start 64m --component-end 128m \
3508                 --component-flags init $comp_file | wc -l)
3509         [ $found -eq 1 ] || error "resync: repeat comp incorrect"
3510
3511         sel_layout_sanity $comp_file 5
3512
3513         rm -f $comp_file
3514 }
3515 run_test 204d "FLR write/stale/resync sel test with repeated comp"
3516
3517 # Successful repeated component, SEL in non-primary mirror
3518 test_204e() {
3519         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3520         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3521                 skip "skipped for lustre < $SEL_VER"
3522
3523         local comp_file=$DIR/$tdir/$tfile
3524         local found=""
3525
3526         wait_delete_completed
3527         wait_mds_ost_sync
3528
3529         test_mkdir $DIR/$tdir
3530
3531         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
3532         # space to -1 (-z 64M, so first comp is 0-64M)
3533         # Note: we have to place both 1st components on OST0, otherwise 2 OSTs
3534         # will be not enough - one will be degraded, the other is used on
3535         # an overlapping mirror.
3536         $LFS setstripe -N -E 100M -i 0 -E-1 -N -E-1 -i 0 -z 64M $comp_file ||
3537                 error "Create $comp_file failed"
3538
3539         local ost_idx1=$($LFS getstripe --component-start=0 \
3540                          --component-end=64m -i $comp_file)
3541         local ost_name=$(ostname_from_index $ost_idx1)
3542         # degrade OST for first comp of 2nd mirror so we won't extend there
3543         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3544                 obdfilter.$ost_name.degraded=1
3545         sleep_maxage
3546
3547         $LFS getstripe $comp_file
3548
3549         # Write to first component, stale & instantiate second mirror components
3550         # overlapping with the written component (0-100M);
3551         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3552         RC=$?
3553
3554         do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
3555                 obdfilter.$ost_name.degraded=0
3556         sleep_maxage
3557         $LFS getstripe $comp_file
3558
3559         [ $RC -eq 0 ] || error "dd to repeat & stale failed"
3560
3561         found=$($LFS find --component-start 0m --component-end 64m \
3562                 --component-flags init,stale $comp_file | wc -l)
3563         [ $found -eq 1 ] || error "write: first comp incorrect"
3564
3565         # was repeated due to degraded ost
3566         found=$($LFS find --component-start 64m --component-end 128m \
3567                 --component-flags init,stale $comp_file | wc -l)
3568         [ $found -eq 1 ] || error "write: repeated comp incorrect"
3569
3570         local ost_idx2=$($LFS getstripe --component-start=64m           \
3571                          --component-end=128m --component-flags=init    \
3572                          -i $comp_file)
3573         [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
3574         local mirror_id=$($LFS getstripe --component-start=0m           \
3575                          --component-end=64m --component-flags=init     \
3576                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3577         [[ $mirror_id -eq 2 ]] ||
3578                 error "component not in correct mirror? $mirror_id"
3579
3580         $LFS mirror resync $comp_file
3581
3582         $LFS getstripe $comp_file
3583
3584         # component dimensions should not change from resync
3585         found=$($LFS find --component-start 0m --component-end 64m \
3586                 --component-flags init,^stale $comp_file | wc -l)
3587         [ $found -eq 1 ] || error "resync: first comp incorrect"
3588         found=$($LFS find --component-start 64m --component-end 128m \
3589                 --component-flags init,^stale $comp_file | wc -l)
3590         [ $found -eq 1 ] || error "resync: repeated comp incorrect"
3591
3592         sel_layout_sanity $comp_file 5
3593
3594         rm -f $comp_file
3595 }
3596 run_test 204e "FLR write/stale/resync sel test with repeated comp"
3597
3598 # FLR + SEL: failed repeated component, SEL in non-primary mirror
3599 test_204f() {
3600         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
3601         [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
3602                 skip "skipped for lustre < $SEL_VER"
3603
3604         local comp_file=$DIR/$tdir/$tfile
3605         local found=""
3606
3607         wait_delete_completed
3608         wait_mds_ost_sync
3609         test_mkdir $DIR/$tdir
3610
3611         pool_add $TESTNAME || error "Pool creation failed"
3612         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
3613
3614         # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
3615         # space to -1 (-z 64M, so first comp is 0-64M)
3616         $LFS setstripe -N -E 100M -E-1 -N --pool="$TESTNAME" \
3617                 -E-1 -c 1 -z 64M $comp_file || error "Create $comp_file failed"
3618
3619         local ost_name0=$(ostname_from_index 0)
3620         local ost_name1=$(ostname_from_index 1)
3621
3622         # degrade both OSTs in pool, so we'll try to repeat, then fail and
3623         # extend original comp
3624         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=1
3625         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=1
3626         sleep_maxage
3627
3628         # a write to the 1st component, 100M length, which will try to stale
3629         # the first 100M of mirror 2, attempting to extend its 0-64M component
3630         dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
3631         RC=$?
3632
3633         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=0
3634         do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=0
3635         sleep_maxage
3636
3637         [ $RC -eq 0 ] || error "dd to extend mirror comp failed"
3638
3639         $LFS getstripe $comp_file
3640
3641         found=$($LFS find --component-start 0m --component-end 128m \
3642                 --component-flags init,stale $comp_file | wc -l)
3643         [ $found -eq 1 ] || error "write: First mirror comp incorrect"
3644
3645         local mirror_id=$($LFS getstripe --component-start=0m           \
3646                          --component-end=128m --component-flags=init    \
3647                          $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
3648
3649         [[ $mirror_id -eq 2 ]] ||
3650                 error "component not in correct mirror? $mirror_id, not 2"
3651
3652         $LFS mirror resync $comp_file
3653
3654         $LFS getstripe $comp_file
3655
3656         # component dimensions should not change from resync
3657         found=$($LFS find --component-start 0m --component-end 128m \
3658                 --component-flags init,^stale $comp_file | wc -l)
3659         [ $found -eq 1 ] || error "resync: First mirror comp incorrect"
3660
3661         sel_layout_sanity $comp_file 4
3662
3663         rm -f $comp_file
3664 }
3665 run_test 204f "FLR write/stale/resync sel w/forced extension"
3666
3667 function test_205() {
3668         local tf=$DIR/$tfile
3669         local mirrors
3670
3671         $LFS setstripe -c1 $tf
3672         $LFS mirror extend -N $tf
3673         mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
3674         (( $mirrors == 2 )) || error "no new mirror was created?"
3675
3676         $LFS mirror extend -N --flags=prefer $tf
3677         mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
3678         (( $mirrors == 3 )) || error "no new mirror was created?"
3679
3680         $($LFS getstripe $tf | grep lcme_flags: | tail -1 | grep -q prefer) ||
3681                 error "prefer flag was not set on the new mirror"
3682 }
3683 run_test 205 "lfs mirror extend to set prefer flag"
3684
3685 function test_206() {
3686         # create a new OST pool
3687         local pool_name=$TESTNAME
3688
3689         create_pool $FSNAME.$pool_name ||
3690                 error "create OST pool $pool_name failed"
3691         # add OSTs into the pool
3692         pool_add_targets $pool_name 0 1 ||
3693                 error "add OSTs into pool $pool_name failed"
3694
3695         $LFS setstripe -c1 --pool=$pool_name $DIR/$tfile ||
3696                 error "can't setstripe"
3697         $LFS mirror extend -N $DIR/$tfile ||
3698                 error "can't create replica"
3699         if $LFS getstripe $DIR/$tfile | grep -q prefer ; then
3700                 $LFS getstripe $DIR/$tfile
3701                 error "prefer found"
3702         fi
3703         $LFS setstripe --comp-set --comp-flags=prefer -p $pool_name $DIR/$tfile || {
3704                 $LFS getstripe $DIR/$tfile
3705                 error "can't setstripe prefer"
3706         }
3707
3708         if ! $LFS getstripe $DIR/$tfile | grep -q prefer ; then
3709                 $LFS getstripe $DIR/$tfile
3710                 error "no prefer found"
3711         fi
3712
3713         # destroy OST pool
3714         destroy_test_pools
3715 }
3716 run_test 206 "lfs setstripe -pool .. --comp-flags=.. "
3717
3718 test_207() {
3719        local file=$DIR/$tfile
3720        local tmpfile=$DIR/$tfile-tt
3721
3722         [ $MDS1_VERSION -lt $(version_code 2.14.50) ] &&
3723                 skip "Need MDS version at least 2.14.50"
3724
3725         stack_trap "rm -f $tmpfile $file"
3726
3727         # generate data for verification
3728         dd if=/dev/urandom of=$tmpfile bs=1M count=1 ||
3729                 error "can't generate file with random data"
3730
3731         # create a mirrored file with one stale replica
3732         $LFS mirror create -N -S 4M -c 2 -N -S 1M -c -1 $file ||
3733                 error "create mirrored file $file failed"
3734         get_mirror_ids $file
3735         echo "mirror IDs: ${mirror_array[@]}"
3736
3737         dd if=$tmpfile of=$file bs=1M || error "can't copy"
3738         get_mirror_ids $file
3739         echo "mirror IDs: ${mirror_array[@]}"
3740
3741         drop_client_cache
3742         cmp $tmpfile $file || error "files don't match"
3743         get_mirror_ids $file
3744         echo "mirror IDs: ${mirror_array[@]}"
3745
3746         # mirror creation should work fine
3747         $LFS mirror extend -N -S 8M -c -1 $file ||
3748                 error "mirror extend $file failed"
3749
3750         get_mirror_ids $file
3751         echo "mirror IDs: ${mirror_array[@]}"
3752
3753         drop_client_cache
3754         $LFS mirror verify -v $file || error "verification failed"
3755         cmp $tmpfile $file || error "files don't match"
3756 }
3757 run_test 207 "create another replica with existing out-of-sync one"
3758
3759 function check_ost_used() {
3760         local ddarg
3761         local ost
3762         local i
3763         local file=$1
3764         local io=$2
3765
3766         shift 2
3767
3768         cancel_lru_locks osc # to drop pages
3769         cancel_lru_locks mdc # to refresh layout
3770         # XXX: cancel_lru_locks mdc doesn't work
3771         # XXX: need a better way to reload the layout
3772         umount_client $MOUNT || error "umount failed"
3773         mount_client $MOUNT || error "mount failed"
3774
3775         # refresh non-rotation status on MDTs
3776         sleep 10
3777         touch $DIR/$tfile-temp
3778         rm -f $DIR/$tfile-temp
3779         # refresh non-rotational status on the client
3780         $LFS df >&/dev/null
3781         sleep 2
3782
3783         $LCTL set_param osc.*.stats=clear >/dev/null
3784         if [[ $io == "read" ]]; then
3785                 ddarg="if=$file of=/dev/null"
3786         elif [[ $io == "write" ]]; then
3787                 ddarg="if=/dev/zero of=$file"
3788         else
3789                 error "unknown type $io"
3790         fi
3791         dd $ddarg bs=2M count=1 || error "can't $io $file"
3792         cancel_lru_locks osc
3793
3794         # check only specified OSTs got reads
3795         for ((ost = 0; ost < $OSTCOUNT; ost++)); do
3796                 local nr=$($LCTL get_param -n \
3797                         osc.$FSNAME-OST000$ost-osc-[-0-9a-f]*.stats |
3798                         awk "/ost_$io/{print \$2}")
3799                 nr=${nr:-0}
3800                 if [[ " $* " =~ " $ost " ]]; then
3801                         (( nr > 0 )) || error "expected reads on $ost"
3802                 else
3803                         (( nr == 0 )) || error "unexpected $nr reads on $ost"
3804                 fi
3805         done
3806 }
3807
3808 test_208a() {
3809         local tf=$DIR/$tfile
3810         local osts=$(comma_list $(osts_nodes))
3811
3812         (( $OSTCOUNT >= 4 )) || skip_env "needs >= 4 OSTs"
3813
3814         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
3815
3816         save_lustre_params $(get_facets OST) osd*.*OST*.nonrotational > $p
3817         stack_trap "restore_lustre_params < $p; rm -f $p"
3818
3819         stack_trap "rm -f $tf"
3820         $LFS setstripe -i0 -c1 $tf || error "can't setstripe"
3821         dd if=/dev/zero of=$tf bs=2M count=1 || error "can't dd (1)"
3822         $LFS mirror extend -N -c1 -o1 $tf || error "can't create mirror"
3823         $LFS mirror extend -N -c2 -o 2,3 $tf || error "can't create mirror"
3824         $LFS mirror resync $tf || error "can't resync"
3825         $LFS getstripe $tf
3826
3827         log "set OST0000 non-rotational"
3828         do_nodes $osts \
3829                 $LCTL set_param osd*.*OST0000*.nonrotational=1
3830         check_ost_used $tf read 0
3831
3832         log "set OST0002 and OST0003 non-rotational, two fast OSTs is better"
3833         do_nodes $osts \
3834                 $LCTL set_param osd*.*OST0002*.nonrotational=1 \
3835                         osd*.*OST0003*.nonrotational=1
3836         check_ost_used $tf read 2 3
3837
3838         log "set mirror 1 on OST0001 preferred"
3839         $LFS setstripe --comp-set -I 0x20001 --comp-flags=prefer $tf ||
3840                 error "can't set prefer"
3841         check_ost_used $tf read 1
3842 }
3843 run_test 208a "mirror selection to prefer non-rotational devices for reads"
3844
3845 test_208b() {
3846         local tf=$DIR/$tfile
3847         local osts=$(comma_list $(osts_nodes))
3848
3849         (( $OSTCOUNT >= 4 )) || skip_env "needs >= 4 OSTs"
3850
3851         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
3852
3853         save_lustre_params $(get_facets OST) osd*.*OST*.nonrotational > $p
3854         stack_trap "restore_lustre_params < $p; rm -f $p"
3855
3856         stack_trap "rm -f $tf"
3857         $LFS setstripe -i0 -c1 $tf || error "can't setstripe"
3858         dd if=/dev/zero of=$tf bs=2M count=1 || error "can't dd (1)"
3859         $LFS mirror extend -N -c1 -o1 $tf || error "can't create mirror"
3860         $LFS mirror extend -N -c2 -o 2,3 $tf || error "can't create mirror"
3861         $LFS mirror resync $tf || error "can't resync"
3862         $LFS getstripe $tf | grep -q flags.*stale && error "still stale"
3863
3864         log "set OST0000 non-rotational"
3865         do_nodes $osts \
3866                 $LCTL set_param osd*.*OST0000*.nonrotational=1
3867         check_ost_used $tf write 0
3868         $LFS mirror resync $tf || error "can't resync"
3869
3870         log "set OST0002 and OST0003 non-rotational, two fast OSTs is better"
3871         do_nodes $osts \
3872                 $LCTL set_param osd*.*OST0002*.nonrotational=1 \
3873                         osd*.*OST0003*.nonrotational=1
3874         check_ost_used $tf write 2 3
3875         $LFS mirror resync $tf || error "can't resync"
3876
3877         log "set mirror 1 on OST0001 preferred"
3878         $LFS setstripe --comp-set -I 0x20001 --comp-flags=prefer $tf ||
3879                 error "can't set prefer"
3880         check_ost_used $tf write 1
3881 }
3882 run_test 208b "mirror selection to prefer non-rotational devices for writes"
3883
3884 complete $SECONDS
3885 check_and_cleanup_lustre
3886 exit_status