Whamcloud - gitweb
025a0585846ba4d6f274031d410283d213aa634c
[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 SRCDIR=$(dirname $0)
9 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
10
11 ONLY=${ONLY:-"$*"}
12 # Bug number for skipped test:
13 ALWAYS_EXCEPT="$SANITY_FLR_EXCEPT 201"
14 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
15
16 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
17 . $LUSTRE/tests/test-framework.sh
18 init_test_env $@
19 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
20 init_logging
21
22 [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
23         { skip "Need MDS version at least 2.10.56"; exit 0; }
24
25 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
26         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
27 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
28
29 check_and_setup_lustre
30 DIR=${DIR:-$MOUNT}
31 assert_DIR
32
33 build_test_filter
34
35 # global array to store mirror IDs
36 declare -a mirror_array
37 get_mirror_ids() {
38         local tf=$1
39         local id
40         local array
41
42         array=()
43         for id in $($LFS getstripe $tf | awk '/lcme_id/{print $2}'); do
44                 array[${#array[@]}]=$((id >> 16))
45         done
46
47         mirror_array=($(printf "%s\n" "${array[@]}" | sort -u))
48
49         echo ${#mirror_array[@]}
50 }
51
52 drop_client_cache() {
53         echo 3 > /proc/sys/vm/drop_caches
54 }
55
56 stop_osts() {
57         local idx
58
59         for idx in "$@"; do
60                 stop ost$idx
61         done
62
63         for idx in "$@"; do
64                 wait_osc_import_state client ost$idx DISCONN
65         done
66 }
67
68 start_osts() {
69         local idx
70
71         for idx in "$@"; do
72                 start ost$idx $(ostdevname $idx) $OST_MOUNT_OPTS ||
73                         error "start ost$idx failed"
74         done
75
76         for idx in "$@"; do
77                 wait_osc_import_state client ost$idx FULL
78         done
79 }
80
81 #
82 # Verify mirror count with an expected value for a given file.
83 #
84 verify_mirror_count() {
85         local tf=$1
86         local expected=$2
87         local mirror_count=$(get_mirror_ids $tf)
88
89         [[ $mirror_count = $expected ]] || {
90                 $LFS getstripe -v $tf
91                 error "verify mirror count failed on $tf:" \
92                       "$mirror_count != $expected"
93         }
94 }
95
96 #
97 # Verify component count with an expected value for a given file.
98 #       $1 coposited layout file
99 #       $2 expected component number
100 #
101 verify_comp_count() {
102         local tf=$1
103         local expected=$2
104         local comp_count=$($LFS getstripe --component-count $tf)
105
106         [[ $comp_count = $expected ]] || {
107                 $LFS getstripe -v $tf
108                 error "verify component count failed on $tf:" \
109                       "$comp_count != $expected"
110         }
111 }
112
113 #
114 # Verify component attribute with an expected value for a given file
115 # and component ID.
116 #
117 verify_comp_attr() {
118         local attr=$1
119         local tf=$2
120         local comp_id=$3
121         local expected=$4
122         local cmd="$LFS getstripe -I$comp_id"
123         local getstripe_cmd="$cmd -v"
124         local value
125
126         case $attr in
127                 stripe-size) cmd+=" -S $tf" ;;
128                 stripe-count) cmd+=" -c $tf" ;;
129                 stripe-index) cmd+=" -i $tf" ;;
130                 pool) cmd+=" -p $tf" ;;
131                 comp-start) cmd+=" --component-start $tf" ;;
132                 comp-end) cmd+=" --component-end $tf" ;;
133                 lcme_flags) cmd+=" $tf | awk '/lcme_flags:/ { print \$2 }'" ;;
134                 *) error "invalid attribute $attr";;
135         esac
136
137         value=$(eval $cmd)
138
139         [ $attr = lcme_flags ] && {
140                 local fl
141                 local expected_list=$(comma_list $expected)
142                 for fl in ${expected_list//,/ }; do
143                         echo $value | grep -q $fl || {
144                                 $getstripe_cmd $tf
145                                 error "expected flag $fl existing on $comp_id"
146                         }
147                 done
148                 return
149         }
150
151         [[ $value = $expected ]] || {
152                 $getstripe_cmd $tf
153                 error "verify $attr failed on $tf: $value != $expected"
154         }
155 }
156
157 #
158 # Verify component extent with expected start and end extent values
159 # for a given file and component ID.
160 #
161 verify_comp_extent() {
162         local tf=$1
163         local comp_id=$2
164         local expected_start=$3
165         local expected_end=$4
166
167         verify_comp_attr comp-start $tf $comp_id $expected_start
168         verify_comp_attr comp-end $tf $comp_id $expected_end
169 }
170
171 #
172 # Verify component attribute with parent directory for a given file
173 # and component ID.
174 #
175 verify_comp_attr_with_parent() {
176         local attr=$1
177         local tf=$2
178         local comp_id=$3
179         local td=$(cd $(dirname $tf); echo $PWD)
180         local tf_cmd="$LFS getstripe -I$comp_id"
181         local td_cmd="$LFS getstripe"
182         local opt
183         local expected
184         local value
185
186         case $attr in
187                 stripe-size) opt="-S" ;;
188                 stripe-count) opt="-c" ;;
189                 pool) opt="-p" ;;
190                 *) error "invalid attribute $attr";;
191         esac
192
193         expected=$($td_cmd $opt $td)
194         [[ $expected = -1 ]] && expected=$OSTCOUNT
195
196         value=$($tf_cmd $opt $tf)
197         [[ $value = -1 ]] && value=$OSTCOUNT
198
199         [[ $value = $expected ]] || {
200                 $td_cmd -d $td
201                 $tf_cmd -v $tf
202                 error "verify $attr failed with parent on $tf:" \
203                       "$value != $expected"
204         }
205 }
206
207 #
208 # Verify component attributes with parent directory for a given file
209 # and component ID.
210 #
211 # This will only verify the inherited attributes:
212 # stripe size, stripe count and OST pool name
213 #
214 verify_comp_attrs_with_parent() {
215         local tf=$1
216         local comp_id=$2
217
218         verify_comp_attr_with_parent stripe-size $tf $comp_id
219         verify_comp_attr_with_parent stripe-count $tf $comp_id
220         verify_comp_attr_with_parent pool $tf $comp_id
221 }
222
223 # command line test cases
224 test_0a() {
225         local td=$DIR/$tdir
226         local tf=$td/$tfile
227         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
228         local mirror_cmd="$LFS mirror create"
229         local id
230         local ids
231         local i
232
233         # create parent directory
234         mkdir $td || error "mkdir $td failed"
235
236         $mirror_cmd $tf &> /dev/null && error "miss -N option"
237
238         $mirror_cmd -N $tf || error "create mirrored file $tf failed"
239         verify_mirror_count $tf 1
240         id=$($LFS getstripe -I $tf)
241         verify_comp_attrs_with_parent $tf $id
242         verify_comp_extent $tf $id 0 EOF
243
244         $mirror_cmd -N0 $tf-1 &> /dev/null && error "invalid mirror count 0"
245         $mirror_cmd -N$((mirror_count + 1)) $tf-1 &> /dev/null &&
246                 error "invalid mirror count $((mirror_count + 1))"
247
248         $mirror_cmd -N$mirror_count $tf-1 ||
249                 error "create mirrored file $tf-1 failed"
250         verify_mirror_count $tf-1 $mirror_count
251         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
252         for ((i = 0; i < $mirror_count; i++)); do
253                 verify_comp_attrs_with_parent $tf-1 ${ids[$i]}
254                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
255         done
256
257         $mirror_cmd -N -N2 -N3 -N4 $tf-2 ||
258                 error "create mirrored file $tf-2 failed"
259         verify_mirror_count $tf-2 10
260         ids=($($LFS getstripe $tf-2 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
261         for ((i = 0; i < 10; i++)); do
262                 verify_comp_attrs_with_parent $tf-2 ${ids[$i]}
263                 verify_comp_extent $tf-2 ${ids[$i]} 0 EOF
264         done
265 }
266 run_test 0a "lfs mirror create with -N option"
267
268 test_0b() {
269         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
270
271         local td=$DIR/$tdir
272         local tf=$td/$tfile
273         local mirror_cmd="$LFS mirror create"
274         local ids
275         local i
276
277         # create parent directory
278         mkdir $td || error "mkdir $td failed"
279
280         # create a mirrored file with plain layout mirrors
281         $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
282                     -N -S 16M -N -c -1 -N -p archive -N --parent $tf ||
283                 error "create mirrored file $tf failed"
284         verify_mirror_count $tf 5
285         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
286         for ((i = 0; i < 5; i++)); do
287                 verify_comp_extent $tf ${ids[$i]} 0 EOF
288         done
289
290         # verify component ${ids[0]}
291         verify_comp_attr stripe-size $tf ${ids[0]} 4194304
292         verify_comp_attr stripe-count $tf ${ids[0]} 2
293         verify_comp_attr stripe-index $tf ${ids[0]} 2
294         verify_comp_attr pool $tf ${ids[0]} flash
295
296         # verify component ${ids[1]}
297         verify_comp_attr stripe-size $tf ${ids[1]} 16777216
298         verify_comp_attr stripe-count $tf ${ids[1]} 2
299         verify_comp_attr pool $tf ${ids[1]} flash
300
301         # verify component ${ids[2]}
302         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
303         verify_comp_attr stripe-count $tf ${ids[2]} $OSTCOUNT
304         verify_comp_attr pool $tf ${ids[2]} flash
305
306         # verify component ${ids[3]}
307         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
308         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
309         verify_comp_attr pool $tf ${ids[3]} archive
310
311         # verify component ${ids[4]}
312         verify_comp_attrs_with_parent $tf ${ids[4]}
313 }
314 run_test 0b "lfs mirror create plain layout mirrors"
315
316 test_0c() {
317         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
318
319         local td=$DIR/$tdir
320         local tf=$td/$tfile
321         local mirror_cmd="$LFS mirror create"
322         local ids
323         local i
324
325         # create parent directory
326         mkdir $td || error "mkdir $td failed"
327
328         # create a mirrored file with composite layout mirrors
329         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
330                     -N --parent \
331                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
332                 error "create mirrored file $tf failed"
333         verify_mirror_count $tf 6
334         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
335
336         # verify components ${ids[0]} and ${ids[2]}
337         for i in 0 2; do
338                 verify_comp_attr_with_parent stripe-size $tf ${ids[$i]}
339                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
340                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
341                 verify_comp_attr pool $tf ${ids[$i]} flash
342                 verify_comp_extent $tf ${ids[$i]} 0 4194304
343         done
344
345         # verify components ${ids[1]} and ${ids[3]}
346         for i in 1 3; do
347                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
348                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
349                 verify_comp_attr pool $tf ${ids[$i]} flash
350                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
351         done
352
353         # verify component ${ids[4]}
354         verify_comp_attrs_with_parent $tf ${ids[4]}
355         verify_comp_extent $tf ${ids[4]} 0 EOF
356
357         # verify components ${ids[5]}, ${ids[7]} and ${ids[9]}
358         for i in 5 7 9; do
359                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
360                 verify_comp_attr_with_parent stripe-count $tf ${ids[$i]}
361                 verify_comp_attr pool $tf ${ids[$i]} archive
362                 verify_comp_extent $tf ${ids[$i]} 0 536870912
363         done
364
365         # verify components ${ids[6]}, ${ids[8]} and ${ids[10]}
366         for i in 6 8 10; do
367                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
368                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
369                 verify_comp_attr pool $tf ${ids[$i]} archive
370                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
371         done
372 }
373 run_test 0c "lfs mirror create composite layout mirrors"
374
375 test_0d() {
376         local td=$DIR/$tdir
377         local tf=$td/$tfile
378         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
379         local mirror_cmd="$LFS mirror extend"
380         local ids
381         local i
382
383         # create parent directory
384         mkdir $td || error "mkdir $td failed"
385
386         $mirror_cmd $tf &> /dev/null && error "miss -N option"
387         $mirror_cmd -N $tf &> /dev/null && error "$tf does not exist"
388
389         # create a non-mirrored file, convert it to a mirrored file and extend
390         touch $tf || error "touch $tf failed"
391         $mirror_cmd -N $tf || error "convert and extend $tf failed"
392         verify_mirror_count $tf 2
393         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
394         for ((i = 0; i < 2; i++)); do
395                 verify_comp_attrs_with_parent $tf ${ids[$i]}
396                 verify_comp_extent $tf ${ids[$i]} 0 EOF
397         done
398
399         lfsck_verify_pfid $tf || error "PFID is not set"
400
401         # create a mirrored file and extend it
402         $LFS mirror create -N $tf-1 || error "create mirrored file $tf-1 failed"
403         $LFS mirror create -N $tf-2 || error "create mirrored file $tf-2 failed"
404
405         $mirror_cmd -N -S 4M -N -f $tf-2 $tf-1 &> /dev/null &&
406                 error "setstripe options should not be specified with -f option"
407
408         $mirror_cmd -N -f $tf-2 -N --parent $tf-1 &> /dev/null &&
409                 error "--parent option should not be specified with -f option"
410
411         $mirror_cmd -N$((mirror_count - 1)) $tf-1 ||
412                 error "extend mirrored file $tf-1 failed"
413         verify_mirror_count $tf-1 $mirror_count
414         ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
415         for ((i = 0; i < $mirror_count; i++)); do
416                 verify_comp_attrs_with_parent $tf-1 ${ids[$i]}
417                 verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
418         done
419
420         $mirror_cmd -N $tf-1 &> /dev/null &&
421                 error "exceeded maximum mirror count $mirror_count" || true
422 }
423 run_test 0d "lfs mirror extend with -N option"
424
425 test_0e() {
426         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
427
428         local td=$DIR/$tdir
429         local tf=$td/$tfile
430         local mirror_cmd="$LFS mirror extend"
431         local ids
432         local i
433
434         # create parent directory
435         mkdir $td || error "mkdir $td failed"
436
437         # create a mirrored file with plain layout mirrors
438         $LFS mirror create -N -S 32M -c 3 -p ssd -i 1 -o 1,2,3 $tf ||
439                 error "create mirrored file $tf failed"
440
441         # extend the mirrored file with plain layout mirrors
442         $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
443                     -N -S 16M -N -c -1 -N -p archive -N --parent $tf ||
444                 error "extend mirrored file $tf failed"
445         verify_mirror_count $tf 6
446         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
447         for ((i = 0; i < 6; i++)); do
448                 verify_comp_extent $tf ${ids[$i]} 0 EOF
449         done
450
451         # verify component ${ids[0]}
452         verify_comp_attr stripe-size $tf ${ids[0]} 33554432
453         verify_comp_attr stripe-count $tf ${ids[0]} 3
454         verify_comp_attr stripe-index $tf ${ids[0]} 1
455         verify_comp_attr pool $tf ${ids[0]} ssd
456
457         # verify component ${ids[1]}
458         verify_comp_attr stripe-size $tf ${ids[1]} 4194304
459         verify_comp_attr stripe-count $tf ${ids[1]} 2
460         verify_comp_attr stripe-index $tf ${ids[1]} 2
461         verify_comp_attr pool $tf ${ids[1]} flash
462
463         # verify component ${ids[2]}
464         verify_comp_attr stripe-size $tf ${ids[2]} 16777216
465         verify_comp_attr stripe-count $tf ${ids[2]} 2
466         verify_comp_attr pool $tf ${ids[2]} flash
467
468         # verify component ${ids[3]}
469         verify_comp_attr stripe-size $tf ${ids[3]} 16777216
470         verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
471         verify_comp_attr pool $tf ${ids[3]} flash
472
473         # verify component ${ids[4]}
474         verify_comp_attr stripe-size $tf ${ids[4]} 16777216
475         verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
476         verify_comp_attr pool $tf ${ids[4]} archive
477
478         # verify component ${ids[5]}
479         verify_comp_attrs_with_parent $tf ${ids[5]}
480 }
481 run_test 0e "lfs mirror extend plain layout mirrors"
482
483 test_0f() {
484         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
485
486         local td=$DIR/$tdir
487         local tf=$td/$tfile
488         local mirror_cmd="$LFS mirror extend"
489         local ids
490         local i
491
492         # create parent directory
493         mkdir $td || error "mkdir $td failed"
494
495         # create a mirrored file with composite layout mirror
496         $LFS mirror create -N -E 32M -S 16M -p ssd -E eof -S 32M $tf ||
497                 error "create mirrored file $tf failed"
498
499         # extend the mirrored file with composite layout mirrors
500         $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
501                     -N --parent \
502                     -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
503                 error "extend mirrored file $tf failed"
504         verify_mirror_count $tf 7
505         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
506
507         # verify component ${ids[0]}
508         verify_comp_attr stripe-size $tf ${ids[0]} 16777216
509         verify_comp_attr_with_parent stripe-count $tf ${ids[0]}
510         verify_comp_attr pool $tf ${ids[0]} ssd
511         verify_comp_extent $tf ${ids[0]} 0 33554432
512
513         # verify component ${ids[1]}
514         verify_comp_attr stripe-size $tf ${ids[1]} 33554432
515         verify_comp_attr_with_parent stripe-count $tf ${ids[1]}
516         verify_comp_attr pool $tf ${ids[1]} ssd
517         verify_comp_extent $tf ${ids[1]} 33554432 EOF
518
519         # verify components ${ids[2]} and ${ids[4]}
520         for i in 2 4; do
521                 verify_comp_attr_with_parent stripe-size $tf ${ids[$i]}
522                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
523                 verify_comp_attr stripe-index $tf ${ids[$i]} 1
524                 verify_comp_attr pool $tf ${ids[$i]} flash
525                 verify_comp_extent $tf ${ids[$i]} 0 4194304
526         done
527
528         # verify components ${ids[3]} and ${ids[5]}
529         for i in 3 5; do
530                 verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
531                 verify_comp_attr stripe-count $tf ${ids[$i]} 2
532                 verify_comp_attr pool $tf ${ids[$i]} flash
533                 verify_comp_extent $tf ${ids[$i]} 4194304 EOF
534         done
535
536         # verify component ${ids[6]}
537         verify_comp_attrs_with_parent $tf ${ids[6]}
538         verify_comp_extent $tf ${ids[6]} 0 EOF
539
540         # verify components ${ids[7]}, ${ids[9]} and ${ids[11]}
541         for i in 7 9 11; do
542                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
543                 verify_comp_attr_with_parent stripe-count $tf ${ids[$i]}
544                 verify_comp_attr pool $tf ${ids[$i]} archive
545                 verify_comp_extent $tf ${ids[$i]} 0 536870912
546         done
547
548         # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
549         for i in 8 10 12; do
550                 verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
551                 verify_comp_attr stripe-count $tf ${ids[$i]} -1
552                 verify_comp_attr pool $tf ${ids[$i]} archive
553                 verify_comp_extent $tf ${ids[$i]} 536870912 EOF
554         done
555 }
556 run_test 0f "lfs mirror extend composite layout mirrors"
557
558 test_0g() {
559         local tf=$DIR/$tfile
560
561         $LFS mirror create -N -E 1M -o0 --flags=prefer -E eof -o1 -N -o1 $tf ||
562                 error "create mirrored file $tf failed"
563
564         verify_comp_attr lcme_flags $tf 0x10001 prefer
565         verify_comp_attr lcme_flags $tf 0x10002 prefer
566
567         # write to the mirrored file and check primary
568         cp /etc/hosts $tf || error "error writing file '$tf'"
569
570         verify_comp_attr lcme_flags $tf 0x20003 stale
571
572         # resync file and check prefer flag
573         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
574
575         cancel_lru_locks osc
576         $LCTL set_param osc.*.stats=clear
577         cat $tf &> /dev/null || error "error reading file '$tf'"
578
579         # verify that the data was provided by OST1 where mirror 1 resides
580         local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-ffff*.stats |
581                         awk '/ost_read/{print $2}')
582         [ -n "$nr_read" ] || error "read was not provided by OST1"
583 }
584 run_test 0g "lfs mirror create flags support"
585
586 test_0h() {
587         local tf=$DIR/$tfile
588
589         $LFS mirror create -N -E 1M --flags=prefer -E eof -N2 $tf ||
590                 error "create mirrored file $tf failed"
591
592         verify_comp_attr lcme_flags $tf 0x10001 prefer
593         verify_comp_attr lcme_flags $tf 0x10002 prefer
594
595         # set flags to the first component
596         $LFS setstripe --comp-set -I 0x10001 --comp-flags=^prefer,stale $tf
597
598         verify_comp_attr lcme_flags $tf 0x10001 stale
599         verify_comp_attr lcme_flags $tf 0x10002 prefer
600
601         $LFS setstripe --comp-set -I0x10001 --comp-flags=^stale $tf &&
602                 error "clearing 'stale' should fail"
603
604         # write and resync file. It can't resync the file directly because the
605         # file state is still 'ro'
606         cp /etc/hosts $tf || error "error writing file '$tf'"
607         $LFS mirror resync $tf || error "error resync-ing file '$tf'"
608
609         $LFS setstripe --comp-set -I 0x20003 --comp-flags=prefer $tf ||
610                 error "error setting flag prefer"
611
612         verify_comp_attr lcme_flags $tf 0x20003 prefer
613 }
614 run_test 0h "set, clear and test flags for FLR files"
615
616 test_1() {
617         local tf=$DIR/$tfile
618         local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
619         local mirror_create_cmd="$LFS mirror create"
620         local stripes[0]=$OSTCOUNT
621
622         mirror_create_cmd+=" -N -c ${stripes[0]}"
623         for ((i = 1; i < $mirror_count; i++)); do
624                 # add mirrors with different stripes to the file
625                 stripes[$i]=$((RANDOM % OSTCOUNT))
626                 [ ${stripes[$i]} -eq 0 ] && stripes[$i]=1
627
628                 mirror_create_cmd+=" -N -c ${stripes[$i]}"
629         done
630
631         $mirror_create_cmd $tf || error "create mirrored file $tf failed"
632         verify_mirror_count $tf $mirror_count
633
634         # can't create mirrors exceeding LUSTRE_MIRROR_COUNT_MAX
635         $LFS mirror extend -N $tf &&
636                 error "Creating the $((mirror_count+1))th mirror succeeded"
637
638         local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
639                         tr '\n' ' '))
640
641         # verify the range of components and stripe counts
642         for ((i = 0; i < $mirror_count; i++)); do
643                 verify_comp_attr stripe-count $tf ${ids[$i]} ${stripes[$i]}
644                 verify_comp_extent $tf ${ids[$i]} 0 EOF
645         done
646 }
647 run_test 1 "create components with setstripe options"
648
649 test_2() {
650         local tf=$DIR/$tfile
651         local tf2=$DIR/$tfile-2
652
653         $LFS setstripe -E 1M -E EOF -c 1 $tf
654         $LFS setstripe -E 2M -E EOF -c -1 $tf2
655
656         local layout=$($LFS getstripe $tf2 | grep -A 4 lmm_objects)
657
658         $LFS mirror extend -N -f $tf2 $tf ||
659                 error "merging $tf2 into $tf failed"
660
661         verify_mirror_count $tf 2
662         [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
663 }
664 run_test 2 "create components from existing files"
665
666 test_3() {
667         [[ $MDSCOUNT -lt 2 ]] && skip "need >= 2 MDTs" && return
668
669         for ((i = 0; i < 2; i++)); do
670                 $LFS mkdir -i $i $DIR/$tdir-$i
671                 $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
672         done
673
674         $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
675                 $DIR/$tdir-0/$tfile || error "creating mirrors"
676
677         # mdt doesn't support to cancel layout lock for remote objects, do
678         # it here manually.
679         cancel_lru_locks mdc
680
681         # make sure the mirrorted file was created successfully
682         [[ $($LFS getstripe --component-count $DIR/$tdir-0/$tfile) -eq 2 ]] ||
683                 { $LFS getstripe $DIR/$tdir-0/$tfile;
684                         error "expected 2 components"; }
685
686         # cleanup
687         rm -rf $DIR/$tdir-*
688 }
689 run_test 3 "create components from files located on different MDTs"
690
691 test_4() {
692         local tf=$DIR/$tdir/$tfile
693         local ids=()
694
695         test_mkdir $DIR/$tdir
696
697         # set mirror with setstripe options to directory
698         $LFS mirror create -N2 -E 1M -E eof $DIR/$tdir ||
699                 error "set mirror to directory error"
700
701         [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
702                 x"mirrored" ] || error "failed to create mirrored dir"
703
704         touch $tf
705         verify_mirror_count $tf 2
706
707         ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
708         verify_comp_extent $tf ${ids[0]} 0 1048576
709         verify_comp_extent $tf ${ids[1]} 1048576 EOF
710
711         # sub directory should inherit mirror setting from parent
712         test_mkdir $DIR/$tdir/td
713         [ x$($LFS getstripe -v $DIR/$tdir/td | awk '/lcm_flags/{print $2}') = \
714                 x"mirrored" ] || error "failed to inherit mirror from parent"
715
716         # mirror extend won't be applied to directory
717         $LFS mirror extend -N2 $DIR/$tdir &&
718                 error "expecting mirror extend failure"
719         true
720 }
721 run_test 4 "Make sure mirror attributes can be inhertied from directory"
722
723 test_5() {
724         local tf=$DIR/$tfile
725         local ids=()
726
727         $MULTIOP $tf oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T12345c ||
728                 error "failed to create file with non-empty layout"
729         $CHECKSTAT -t file -s 12345 $tf || error "size error: expecting 12345"
730
731         $LFS mirror create -N3 $tf || error "failed to attach mirror layout"
732         verify_mirror_count $tf 3
733
734         $CHECKSTAT -t file -s 12345 $tf ||
735                 error "size error after attaching layout "
736 }
737 run_test 5 "Make sure init size work for mirrored layout"
738
739 # LU=10112: disable dom+flr for phase 1
740 test_6() {
741         local tf=$DIR/$tfile
742
743         $LFS mirror create -N -E 1M -L mdt -E eof -N -E eof $tf &&
744                 error "expect failure to create mirrored file with DoM"
745
746         $LFS mirror create -N -E 1M -E eof -N -E 1M -L mdt -E eof $tf &&
747                 error "expect failure to create mirrored file with DoM"
748
749         $LFS setstripe -E 1M -L mdt -E eof $tf
750         $LFS mirror extend -N2 $tf &&
751                 error "expect failure to extend mirror with DoM"
752
753         $LFS mirror create -N2 -E 1M -E eof $tf-2
754         $LFS mirror extend -N -f $tf $tf-2 &&
755                 error "expect failure to extend mirrored file with DoM extent"
756
757         true
758 }
759 run_test 6 "DoM and FLR won't co-exist for phase 1"
760
761 test_21() {
762         local tf=$DIR/$tfile
763         local tf2=$DIR/$tfile-2
764
765         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
766
767         $LFS setstripe -E EOF -o 0 $tf
768         $LFS setstripe -E EOF -o 1 $tf2
769
770         local dd_count=$((RANDOM % 20 + 1))
771         dd if=/dev/zero of=$tf bs=1M count=$dd_count oflag=sync
772         dd if=/dev/zero of=$tf2 bs=1M count=1 seek=$((dd_count - 1)) oflag=sync
773
774         # for zfs - sync OST dataset so that du below will return
775         # accurate results
776         [ "$FSTYPE" = "zfs" ] &&
777                 do_nodes $(comma_list $(osts_nodes)) "$ZPOOL sync"
778
779         local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
780
781         # add component
782         $LFS mirror extend -N -f $tf2 $tf ||
783                 error "merging $tf2 into $tf failed"
784
785         # cancel layout lock
786         cancel_lru_locks mdc
787
788         local new_blocks=$(du -k $tf | awk '{print $1}')
789         [ $new_blocks -eq $blocks ] ||
790         error "i_blocks error expected: $blocks, actual: $new_blocks"
791 }
792 run_test 21 "glimpse should report accurate i_blocks"
793
794 get_osc_lock_count() {
795         local lock_count=0
796
797         for idx in "$@"; do
798                 local osc_name
799                 local count
800
801                 osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'ffff*'
802                 count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
803                 lock_count=$((lock_count + count))
804         done
805         echo $lock_count
806 }
807
808 test_22() {
809         local tf=$DIR/$tfile
810
811         $LFS setstripe -E EOF -o 0 $tf
812         dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
813
814         # add component, two mirrors located on the same OST ;-)
815         $LFS mirror extend -N -o 0 $tf ||
816                 error "extending mirrored file $tf failed"
817
818         size_blocks=$(stat --format="%b %s" $tf)
819
820         cancel_lru_locks mdc
821         cancel_lru_locks osc
822
823         local new_size_blocks=$(stat --format="%b %s" $tf)
824
825         # make sure there is no lock cached
826         [ $(get_osc_lock_count 1) -eq 0 ] || error "glimpse requests were sent"
827
828         [ "$new_size_blocks" = "$size_blocks" ] ||
829                 echo "size expected: $size_blocks, actual: $new_size_blocks"
830
831         rm -f $tmpfile
832 }
833 run_test 22 "no glimpse to OSTs for READ_ONLY files"
834
835 test_31() {
836         local tf=$DIR/$tfile
837
838         $LFS mirror create -N -o 0 -N -o 1 $tf ||
839                 error "creating mirrored file $tf failed"
840
841         #define OBD_FAIL_GLIMPSE_IMMUTABLE 0x1A00
842         $LCTL set_param fail_loc=0x1A00
843
844         local ost_idx
845         for ((ost_idx = 1; ost_idx <= 2; ost_idx++)); do
846                 cancel_lru_locks osc
847                 stop_osts $ost_idx
848
849                 local tmpfile=$(mktemp)
850                 stat --format="%b %s" $tf > $tmpfile  &
851                 local pid=$!
852
853                 local cnt=0
854                 while [ $cnt -le 5 ]; do
855                         kill -0 $pid > /dev/null 2>&1 || break
856                         sleep 1
857                         ((cnt += 1))
858                 done
859                 kill -0 $pid > /dev/null 2>&1 &&
860                         error "stat process stuck due to unavailable OSTs"
861
862                 # make sure glimpse request has been sent
863                 [ $(get_osc_lock_count 1 2) -ne 0 ] ||
864                         error "OST $ost_idx: no glimpse request was sent"
865
866                 start_osts $ost_idx
867         done
868 }
869 run_test 31 "make sure glimpse request can be retried"
870
871 test_32() {
872         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
873         rm -f $DIR/$tfile $DIR/$tfile-2
874
875         $LFS setstripe -E EOF -o 0 $DIR/$tfile
876         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=$((RANDOM % 10 + 2))
877
878         local fsize=$(stat -c %s $DIR/$tfile)
879         [[ $fsize -ne 0 ]] || error "file size is (wrongly) zero"
880
881         local cksum=$(md5sum $DIR/$tfile)
882
883         # create a new mirror in sync mode
884         $LFS mirror extend -N -o 1 $DIR/$tfile ||
885                 error "extending mirrored file $DIR/$tfile failed"
886
887         # make sure the mirrored file was created successfully
888         [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
889                 { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
890
891         drop_client_cache
892         stop_osts 1
893
894         # check size is correct, glimpse request should go to the 2nd mirror
895         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
896                 error "file size error $fsize vs. $(stat -c %s $DIR/$tfile)"
897
898         echo "reading file from the 2nd mirror and verify checksum"
899         [[ "$cksum" == "$(md5sum $DIR/$tfile)" ]] ||
900                 error "checksum error: expected $cksum"
901
902         start_osts 1
903 }
904 run_test 32 "data should be mirrored to newly created mirror"
905
906 test_33() {
907         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
908
909         rm -f $DIR/$tfile $DIR/$tfile-2
910
911         # create a file with two mirrors
912         $LFS setstripe -E EOF -o 0 $DIR/$tfile
913         local max_count=100
914         local count=0
915         while [ $count -lt $max_count ]; do
916                 echo "ost1" >> $DIR/$tfile
917                 count=$((count + 1));
918         done
919
920         # tmp file that will be used as mirror
921         $LFS setstripe -E EOF -o 1 $DIR/$tfile-2
922         count=0
923         while [ $count -lt $max_count ]; do
924                 echo "ost2" >> $DIR/$tfile-2
925                 count=$((count + 1));
926         done
927
928         # create a mirrored file
929         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
930                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
931                       "with verification should fail"
932         $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
933                 error "merging $DIR/$tfile-2 into $DIR/$tfile" \
934                       "without verification failed"
935
936         # make sure that $tfile has two mirrors and $tfile-2 does not exist
937         [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
938                 { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
939
940         [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
941
942         # execpted file size
943         local fsize=$((5 * max_count))
944         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
945                 error "mirrored file size is not $fsize"
946
947         # read file - all OSTs are available
948         echo "reading file (data can be provided by any ost)... "
949         local rs=$(cat $DIR/$tfile | head -1)
950         [[ "$rs" == "ost1" || "$rs" == "ost2" ]] ||
951                 error "file content error: expected: \"ost1\" or \"ost2\""
952
953         # read file again with ost1 failed
954         stop_osts 1
955         drop_client_cache
956
957         echo "reading file (data should be provided by ost2)..."
958         local rs=$(cat $DIR/$tfile | head -1)
959         [[ "$rs" == "ost2" ]] ||
960                 error "file content error: expected: \"ost2\", actual: \"$rs\""
961
962         # remount ost1
963         start_osts 1
964
965         # read file again with ost2 failed
966         $LCTL set_param ldlm.namespaces.lustre-*-osc-ffff*.lru_size=clear
967
968         fail ost2 &
969         sleep 1
970
971         # check size, glimpse should work
972         $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
973                 error "mirrored file size is not $fsize"
974
975         echo "reading file (data should be provided by ost1)..."
976         local rs=$(cat $DIR/$tfile | head -1)
977         [[ "$rs" == "ost1" ]] ||
978                 error "file content error: expected: \"ost1\", actual: \"$rs\""
979
980         wait_osc_import_state client ost2 FULL
981 }
982 run_test 33 "read can choose available mirror to read"
983
984 test_34a() {
985         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
986
987         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
988
989         # reference file
990         $LFS setstripe -o 0 $DIR/$tfile-ref
991         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
992
993         # create a file with two mirrors
994         $LFS setstripe -E -1 -o 0,1 -S 1M $DIR/$tfile
995         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
996
997         $LFS setstripe -E -1 -o 2,3 -S 1M $DIR/$tfile-2
998         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
999
1000         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1001                 error "mirrored file size is not 3M"
1002
1003         # merge a mirrored file
1004         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1005                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1006
1007         cancel_lru_locks osc
1008
1009         # stop two OSTs, so the 2nd stripe of the 1st mirror and
1010         # the 1st stripe of the 2nd mirror will be inaccessible, ...
1011         stop_osts 2 3
1012
1013         echo "comparing files ... "
1014
1015         # however, read can still return the correct data. It should return
1016         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1017         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1018                 $DIR/$tfile-ref || error "file reading error"
1019
1020         start_osts 2 3
1021 }
1022 run_test 34a "read mirrored file with multiple stripes"
1023
1024 test_34b() {
1025         [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
1026
1027         rm -f $DIR/$tfile $DIR/$tfile-2 $DIR/$tfile-ref
1028
1029         # reference file
1030         $LFS setstripe -o 0 $DIR/$tfile-ref
1031         dd if=/dev/urandom of=$DIR/$tfile-ref bs=1M count=3
1032
1033         $LFS setstripe -E 1M -S 1M -o 0 -E eof -o 1 $DIR/$tfile
1034         dd if=$DIR/$tfile-ref of=$DIR/$tfile bs=1M
1035
1036         $LFS setstripe -E 1M -S 1M -o 2 -E eof -o 3 $DIR/$tfile-2
1037         dd if=$DIR/$tfile-ref of=$DIR/$tfile-2 bs=1M
1038
1039         $CHECKSTAT -t file -s $((3 * 1024 * 1024)) $DIR/$tfile ||
1040                 error "mirrored file size is not 3M"
1041
1042         # merge a mirrored file
1043         $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
1044                 error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
1045
1046         cancel_lru_locks osc
1047
1048         # stop two OSTs, so the 2nd component of the 1st mirror and
1049         # the 1st component of the 2nd mirror will be inaccessible, ...
1050         stop_osts 2 3
1051
1052         echo "comparing files ... "
1053
1054         # however, read can still return the correct data. It should return
1055         # the 1st stripe from mirror 1 and 2st stripe from mirror 2.
1056         cmp -n 2097152 <(rwv -f $DIR/$tfile -r -o -n 1 2097152) \
1057                 $DIR/$tfile-ref || error "file reading error"
1058
1059         start_osts 2 3
1060 }
1061 run_test 34b "read mirrored file with multiple components"
1062
1063 test_35() {
1064         local tf=$DIR/$tfile
1065
1066         $LFS setstripe -E eof $tf
1067
1068         # add an out-of-sync mirror to the file
1069         $LFS mirror extend -N -c 2 $tf ||
1070                 error "extending mirrored file $tf failed"
1071
1072         $MULTIOP $tf oO_WRONLY:c ||
1073                 error "write open a mirrored file failed"
1074
1075         # truncate file should return error
1076         $TRUNCATE $tf 100 || error "error truncating a mirrored file"
1077 }
1078 run_test 35 "allow to write to mirrored files"
1079
1080 verify_ost_layout_version() {
1081         local tf=$1
1082
1083         # get file layout version
1084         local flv=$($LFS getstripe $tf | awk '/lcm_layout_gen/{print $2}')
1085
1086         # layout version from OST objects
1087         local olv=$($MULTIOP $tf oXc | awk '/ostlayoutversion/{print $2}')
1088
1089         [ $flv -eq $olv ] || error "layout version mismatch: $flv vs. $olv"
1090 }
1091
1092 create_file_36() {
1093         local tf
1094
1095         for tf in "$@"; do
1096                 $LFS setstripe -E 1M -E 2M -E 4M -E eof -c -1 $tf
1097                 $LFS setstripe -E 3M -E 6M -E eof -c -1 $tf-tmp
1098
1099                 $LFS mirror extend -N -f $tf-tmp $tf ||
1100                         error "merging $tf-tmp into $tf failed"
1101         done
1102 }
1103
1104 test_36() {
1105         local tf=$DIR/$tfile
1106
1107         create_file_36 $tf $tf-2 $tf-3
1108
1109         [ $(get_mirror_ids $tf) -gt 1 ] || error "wrong mirror count"
1110
1111         # test case 1 - check file write and verify layout version
1112         $MULTIOP $tf oO_WRONLY:c ||
1113                 error "write open a mirrored file failed"
1114
1115         # write open file should not return error
1116         $MULTIOP $tf oO_WRONLY:w1024Yc || error "write mirrored file error"
1117
1118         # instantiate components should work
1119         dd if=/dev/zero of=$tf bs=1M count=12 || error "write file error"
1120
1121         # verify OST layout version
1122         verify_ost_layout_version $tf
1123
1124         # test case 2
1125         local mds_idx=mds$(($($LFS getstripe -M $tf-2) + 1))
1126
1127         local delay_sec=10
1128         do_facet $mds_idx $LCTL set_param fail_val=$delay_sec
1129
1130         #define OBD_FAIL_FLR_LV_DELAY 0x1A01
1131         do_facet $mds_idx $LCTL set_param fail_loc=0x1A01
1132
1133         # write should take at least $fail_loc seconds and succeed
1134         local st=$(date +%s)
1135         $MULTIOP $tf-2 oO_WRONLY:w1024Yc || error "write mirrored file error"
1136
1137         [ $(date +%s) -ge $((st+delay_sec)) ] ||
1138                 error "write finished before layout version is transmitted"
1139
1140         # verify OST layout version
1141         verify_ost_layout_version $tf
1142
1143         do_facet $mds_idx $LCTL set_param fail_loc=0
1144
1145         # test case 3
1146         mds_idx=mds$(($($LFS getstripe -M $tf-3) + 1))
1147
1148         #define OBD_FAIL_FLR_LV_INC 0x1A02
1149         do_facet $mds_idx $LCTL set_param fail_loc=0x1A02
1150
1151         # write open file should return error
1152         $MULTIOP $tf-3 oO_WRONLY:O_SYNC:w1024c &&
1153                 error "write a mirrored file succeeded" || true
1154
1155         do_facet $mds_idx $LCTL set_param fail_loc=0
1156 }
1157 run_test 36 "write to mirrored files"
1158
1159 create_files_37() {
1160         local tf
1161         local fsize=$1
1162
1163         echo "create test files with size $fsize .."
1164
1165         shift
1166         for tf in "$@"; do
1167                 $LFS setstripe -E 1M -c 1 -E eof -c -1 $tf
1168
1169                 dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
1170                 $TRUNCATE $tf $fsize
1171         done
1172 }
1173
1174 test_37()
1175 {
1176         local tf=$DIR/$tfile
1177         local tf2=$DIR/$tfile-2
1178         local tf3=$DIR/$tfile-3
1179
1180         create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
1181
1182         # assume the mirror id will be 1, 2, and 3
1183         declare -A checksums
1184         checksums[1]=$(md5sum $tf | cut -f 1 -d' ')
1185         checksums[2]=$(md5sum $tf2 | cut -f 1 -d' ')
1186         checksums[3]=$(md5sum $tf3 | cut -f 1 -d' ')
1187
1188         printf '%s\n' "${checksums[@]}"
1189
1190         # merge these files into a mirrored file
1191         $LFS mirror extend --no-verify -N -f $tf2 $tf ||
1192                 error "merging $tf2 into $tf failed"
1193         $LFS mirror extend --no-verify -N -f $tf3 $tf ||
1194                 error "merging $tf3 into $tf failed"
1195
1196         get_mirror_ids $tf
1197
1198         # verify mirror read, checksums should equal to the original files'
1199         echo "Verifying mirror read .."
1200
1201         local sum
1202         for i in ${mirror_array[@]}; do
1203                 sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
1204                 [ "$sum" = "${checksums[$i]}" ] ||
1205                         error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
1206         done
1207
1208         # verify mirror copy, write to this mirrored file will invalidate
1209         # the other two mirrors
1210         echo "Verifying mirror copy .."
1211
1212         local osts=$(comma_list $(osts_nodes))
1213
1214         # define OBD_FAIL_OST_SKIP_LV_CHECK     0x241
1215         do_nodes $osts lctl set_param fail_loc=0x241
1216
1217         mirror_io copy -i ${mirror_array[0]} \
1218                 -t $(echo ${mirror_array[@]:1} | tr ' ' ',') $tf ||
1219                         error "mirror copy error"
1220
1221         do_nodes $osts lctl set_param fail_loc=0
1222
1223         # verify copying is successful by checking checksums
1224         remount_client $MOUNT
1225         for i in ${mirror_array[@]}; do
1226                 sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
1227                 [ "$sum" = "${checksums[1]}" ] ||
1228                         error "$i: mismatch checksum after copy"
1229         done
1230
1231         rm -f $tf
1232 }
1233 run_test 37 "mirror I/O API verification"
1234
1235 verify_flr_state()
1236 {
1237         local tf=$1
1238         local expected_state=$2
1239
1240         local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
1241         [ $expected_state = $state ] ||
1242                 error "expected: $expected_state, actual $state"
1243 }
1244
1245 test_38() {
1246         local tf=$DIR/$tfile
1247         local ref=$DIR/${tfile}-ref
1248
1249         $LFS setstripe -E 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf
1250         $LFS setstripe -E 2M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 $tf-2
1251         $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3
1252
1253         # instantiate all components
1254         $LFS mirror extend -N -f $tf-2 $tf ||
1255                 error "merging $tf-2 into $tf failed"
1256         $LFS mirror extend -N -f $tf-3 $tf ||
1257                 error "merging $tf-3 into $tf failed"
1258         $LFS mirror extend -N -c 1 $tf ||
1259                 error "extending mirrored file $tf failed"
1260
1261         verify_flr_state $tf "ro"
1262
1263         dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
1264
1265         local fsize=$((RANDOM << 8 + 1048576))
1266         $TRUNCATE $ref $fsize
1267
1268         local ref_cksum=$(md5sum $ref | cut -f 1 -d' ')
1269
1270         # case 1: verify write to mirrored file & resync work
1271         cp $ref $tf || error "copy from $ref to $f error"
1272         verify_flr_state $tf "wp"
1273
1274         local file_cksum=$(md5sum $tf | cut -f 1 -d' ')
1275         [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
1276
1277         get_mirror_ids $tf
1278         echo "mirror IDs: ${mirror_array[@]}"
1279
1280         local valid_mirror stale_mirror id mirror_cksum
1281         for id in "${mirror_array[@]}"; do
1282                 mirror_cksum=$(mirror_io dump -i $id $tf |
1283                                 md5sum | cut -f 1 -d' ')
1284                 [ "$ref_cksum" == "$mirror_cksum" ] &&
1285                         { valid_mirror=$id; continue; }
1286
1287                 stale_mirror=$id
1288         done
1289
1290         [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
1291         [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
1292
1293         mirror_io resync $tf || error "resync failed"
1294         verify_flr_state $tf "ro"
1295
1296         mirror_cksum=$(mirror_io dump -i $stale_mirror $tf |
1297                         md5sum | cut -f 1 -d' ')
1298         [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
1299
1300         # case 2: inject an error to make mirror_io exit after changing
1301         # the file state to sync_pending so that we can start a concurrent
1302         # write.
1303         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1304         verify_flr_state $tf "wp"
1305
1306         mirror_io resync -e resync_start $tf && error "resync succeeded"
1307         verify_flr_state $tf "sp"
1308
1309         # from sync_pending to write_pending
1310         $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
1311         verify_flr_state $tf "wp"
1312
1313         mirror_io resync -e resync_start $tf && error "resync succeeded"
1314         verify_flr_state $tf "sp"
1315
1316         # from sync_pending to read_only
1317         mirror_io resync $tf || error "resync failed"
1318         verify_flr_state $tf "ro"
1319 }
1320 run_test 38 "resync"
1321
1322 test_39() {
1323         local tf=$DIR/$tfile
1324
1325         rm -f $tf
1326         $LFS mirror create -N2 -E1m -c1 -S1M -E-1 $tf ||
1327         error "create PFL file $tf failed"
1328
1329         verify_mirror_count $tf 2
1330         verify_comp_count $tf 4
1331
1332         rm -f $tf || error "delete $tf failed"
1333 }
1334 run_test 39 "check FLR+PFL (a.k.a. PFLR) creation"
1335
1336 test_40() {
1337         local tf=$DIR/$tfile
1338         local ops
1339
1340         for ops in "conv=notrunc" ""; do
1341                 rm -f $tf
1342
1343                 $LFS mirror create -N -E2m -E4m -E-1 -N -E1m -E2m -E4m -E-1 \
1344                         $tf || error "create PFLR file $tf failed"
1345                 dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
1346                         error "write PFLR file $tf failed"
1347
1348                 lfs getstripe -vy $tf
1349
1350                 local flags
1351
1352                 # file mirror state should be write_pending
1353                 flags=$($LFS getstripe -v $tf | awk '/lcm_flags:/ { print $2 }')
1354                 [ $flags = wp ] ||
1355                 error "file mirror state $flags"
1356                 # the 1st component (in mirror 1) should be inited
1357                 verify_comp_attr lcme_flags $tf 0x10001 init
1358                 # the 2nd component (in mirror 1) should be inited
1359                 verify_comp_attr lcme_flags $tf 0x10002 init
1360                 # the 3rd component (in mirror 1) should be uninited
1361                 verify_comp_attr lcme_flags $tf 0x10003 0
1362                 # the 4th component (in mirror 2) should be inited
1363                 verify_comp_attr lcme_flags $tf 0x20004 init
1364                 # the 5th component (in mirror 2) should be uninited
1365                 verify_comp_attr lcme_flags $tf 0x20005 0
1366                 # the 6th component (in mirror 2) should be stale
1367                 verify_comp_attr lcme_flags $tf 0x20006 stale
1368                 # the 7th component (in mirror 2) should be uninited
1369                 if [[ x$ops = "xconv=notrunc" ]]; then
1370                         verify_comp_attr lcme_flags $tf 0x20007 0
1371                 elif [[ x$ops = "x" ]]; then
1372                         verify_comp_attr lcme_flags $tf 0x20007 stale
1373                 fi
1374         done
1375
1376         rm -f $tf || error "delete $tf failed"
1377 }
1378 run_test 40 "PFLR rdonly state instantiation check"
1379
1380 test_41() {
1381         local tf=$DIR/$tfile
1382
1383         rm -f $tf $tf-1
1384         $LFS mirror create -N -E2m -E4m -E-1 -N -E1m -E2m -E3m -E-1 $tf ||
1385                 error "create PFLR file $tf failed"
1386         $LFS mirror create -N -E4m -E-1 -N -E2m -E3m -E-1 $tf-1 ||
1387                 error "create PFLR file $tf-1 failed"
1388
1389         # file should be in ro status
1390         verify_flr_state $tf "ro"
1391         verify_flr_state $tf-1 "ro"
1392
1393         # write data in [0, 2M)
1394         dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
1395                 error "writing $tf failed"
1396         dd if=/dev/zero of=$tf-1 bs=1M count=4 conv=notrunc ||
1397                 error "writing $tf-1 failed"
1398
1399         verify_flr_state $tf "wp"
1400         verify_flr_state $tf-1 "wp"
1401
1402         # file should have stale component
1403         $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
1404                 error "after writing $tf, it does not contain stale component"
1405         $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
1406                 error "after writing $tf-1, it does not contain stale component"
1407
1408         $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
1409
1410         verify_flr_state $tf "ro"
1411         verify_flr_state $tf-1 "ro"
1412
1413         # file should not have stale component
1414         $LFS getstripe $tf | grep lcme_flags | grep stale &&
1415                 error "after resyncing $tf, it contains stale component"
1416         $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
1417                 error "after resyncing $tf, it contains stale component"
1418
1419         return 0
1420 }
1421 run_test 41 "lfs mirror resync check"
1422
1423 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
1424 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
1425
1426 write_file_200() {
1427         local tf=$1
1428
1429         local fsize=$(stat --printf=%s $tf)
1430
1431         while [ -f $ctrl_file ]; do
1432                 local off=$((RANDOM << 8))
1433                 local len=$((RANDOM << 5 + 131072))
1434
1435                 [ $((off + len)) -gt $fsize ] && {
1436                         fsize=$((off + len))
1437                         echo "Extending file size to $fsize .."
1438                 }
1439
1440                 flock -s $lock_file -c \
1441                         "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
1442                                 { rm -f $ctrl_file;
1443                                   error "failed writing to $off:$len"; }
1444                 sleep 0.$((RANDOM % 2 + 1))
1445         done
1446 }
1447
1448 read_file_200() {
1449         local tf=$1
1450
1451         while [ -f $ctrl_file ]; do
1452                 flock -s $lock_file -c "cat $tf &> /dev/null" ||
1453                         { rm -f $ctrl_file; error "read failed"; }
1454                 sleep 0.$((RANDOM % 2 + 1))
1455         done
1456 }
1457
1458 resync_file_200() {
1459         local tf=$1
1460
1461         options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
1462
1463         exec 200<>$lock_file
1464         while [ -f $ctrl_file ]; do
1465                 local lock_taken=false
1466                 local index=$((RANDOM % ${#options[@]}))
1467                 local cmd="mirror_io resync ${options[$index]}"
1468
1469                 [ "${options[$index]}" = "" ] && cmd="$LFS mirror resync"
1470
1471                 [ $((RANDOM % 4)) -eq 0 ] && {
1472                         index=0
1473                         lock_taken=true
1474                         echo -n "lock to "
1475                 }
1476
1477                 echo -n "resync file $tf with '$cmd' .."
1478
1479                 $lock_taken && flock -x 200
1480                 $cmd $tf &> /dev/null && echo "done" || echo "failed"
1481                 $lock_taken && flock -u 200
1482
1483                 sleep 0.$((RANDOM % 8 + 1))
1484         done
1485 }
1486
1487 test_200() {
1488         local tf=$DIR/$tfile
1489         local tf2=$DIR2/$tfile
1490         local tf3=$DIR3/$tfile
1491
1492         $LFS setstripe -E 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
1493         $LFS setstripe -E 2M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
1494         $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
1495
1496         $LFS mirror extend -N -f $tf-2 $tf ||
1497                 error "merging $tf-2 into $tf failed"
1498         $LFS mirror extend -N -f $tf-3 $tf ||
1499                 error "merging $tf-3 into $tf failed"
1500
1501         mkdir -p $MOUNT2 && mount_client $MOUNT2
1502
1503         mkdir -p $MOUNT3 && mount_client $MOUNT3
1504
1505         verify_flr_state $tf3 "ro"
1506
1507         #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
1508         $LCTL set_param fail_loc=0x1A03
1509
1510         local mds_idx=mds$(($($LFS getstripe -M $tf) + 1))
1511         do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
1512
1513         declare -a pids
1514
1515         write_file_200 $tf &
1516         pids+=($!)
1517
1518         read_file_200 $tf &
1519         pids+=($!)
1520
1521         write_file_200 $tf2 &
1522         pids+=($!)
1523
1524         read_file_200 $tf2 &
1525         pids+=($!)
1526
1527         resync_file_200 $tf3 &
1528         pids+=($!)
1529
1530         local sleep_time=60
1531         [ "$SLOW" = "yes" ] && sleep_time=360
1532         while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
1533                 sleep 1
1534                 ((--sleep_time))
1535         done
1536
1537         rm -f $ctrl_file
1538
1539         echo "Waiting ${pids[@]}"
1540         wait ${pids[@]}
1541
1542         umount_client $MOUNT2
1543         umount_client $MOUNT3
1544
1545         rm -f $lock_file
1546
1547         # resync and verify mirrors
1548         mirror_io resync $tf
1549         get_mirror_ids $tf
1550
1551         local csum=$(mirror_io dump -i ${mirror_array[0]} $tf | md5sum)
1552         for id in ${mirror_array[@]:1}; do
1553                 [ "$(mirror_io dump -i $id $tf | md5sum)" = "$csum" ] ||
1554                         error "checksum error for mirror $id"
1555         done
1556
1557         true
1558 }
1559 run_test 200 "stress test"
1560
1561 cleanup_test_201() {
1562         trap 0
1563         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
1564
1565         umount_client $MOUNT2
1566 }
1567
1568 test_201() {
1569         local delay=${RESYNC_DELAY:-5}
1570
1571         MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
1572                awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
1573
1574         trap cleanup_test_201 EXIT
1575
1576         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
1577                         changelog_register -n)
1578
1579         mkdir -p $MOUNT2 && mount_client $MOUNT2
1580
1581         local index=0
1582         while :; do
1583                 local log=$($LFS changelog $MDT0 $index | grep FLRW)
1584                 [ -z "$log" ] && { sleep 1; continue; }
1585
1586                 index=$(echo $log | awk '{print $1}')
1587                 local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
1588                 local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
1589                 local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
1590
1591                 ((++index))
1592                 [ -z "$file" ] && continue
1593
1594                 local now=$(date +%s)
1595
1596                 echo "file: $file $fid was modified at $ts, now: $now, " \
1597                      "will be resynced at $((ts+delay))"
1598
1599                 [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
1600
1601                 mirror_io resync $file
1602                 echo "$file resync done"
1603         done
1604
1605         cleanup_test_201
1606 }
1607 run_test 201 "FLR data mover"
1608
1609 complete $SECONDS
1610 check_and_cleanup_lustre
1611 exit_status