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