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