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