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