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