Whamcloud - gitweb
LU-6313 tests: more robust for scrub test_11
[fs/lustre-release.git] / lustre / tests / sanity-scrub.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 #
6
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10 ALWAYS_EXCEPT="$SANITY_SCRUB_EXCEPT"
11 [ "$SLOW" = "no" ] && EXCEPT_SLOW=""
12 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
13
14 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
15 . $LUSTRE/tests/test-framework.sh
16 init_test_env $@
17 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
18 init_logging
19
20 require_dsh_mds || exit 0
21
22 SAVED_MDSSIZE=${MDSSIZE}
23 SAVED_OSTSIZE=${OSTSIZE}
24 SAVED_OSTCOUNT=${OSTCOUNT}
25 # use small MDS + OST size to speed formatting time
26 # do not use too small MDSSIZE/OSTSIZE, which affect the default journal size
27 # 200M MDT device can guarantee uninitialized groups during the OI scrub
28 MDSSIZE=200000
29 OSTSIZE=100000
30 # no need too much OSTs, to reduce the format/start/stop overhead
31 [ $OSTCOUNT -gt 4 ] && OSTCOUNT=4
32
33 MOUNT_2=""
34
35 # build up a clean test environment.
36 formatall
37 setupall
38
39 [ $(facet_fstype $SINGLEMDS) != "ldiskfs" ] &&
40         skip "test OI scrub only for ldiskfs" && check_and_cleanup_lustre &&
41         exit 0
42 [ $(facet_fstype ost1) != "ldiskfs" ] &&
43         skip "test OI scrub only for ldiskfs" && check_and_cleanup_lustre &&
44         exit 0
45 [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.2.90) ]] &&
46         skip "Need MDS version at least 2.2.90" && check_and_cleanup_lustre &&
47         exit 0
48
49 [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.90) ]] &&
50         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 1a"
51
52 [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.6.50) ]] &&
53         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 4"
54
55 [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.4.1) ]] &&
56         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 15"
57
58 [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.90) ]] &&
59 [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.50) ]] &&
60         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 15"
61
62 [[ $(lustre_version_code ost1) -lt $(version_code 2.4.50) ]] &&
63         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 11 12 13 14"
64
65 [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.59) ]] &&
66         SCRUB_ONLY="-t scrub"
67
68 build_test_filter
69
70 MDT_DEV="${FSNAME}-MDT0000"
71 OST_DEV="${FSNAME}-OST0000"
72 MDT_DEVNAME=$(mdsdevname ${SINGLEMDS//mds/})
73
74 scrub_start() {
75         local error_id=$1
76         local n
77
78         # use "lfsck_start -A" when we no longer need testing interop
79         for n in $(seq $MDSCOUNT); do
80                 do_facet mds$n $LCTL lfsck_start -M $(facet_svc mds$n) \
81                         $SCRUB_ONLY "$@" ||
82                         error "($error_id) Failed to start OI scrub on mds$n"
83         done
84 }
85
86 scrub_stop() {
87         local error_id=$1
88         local n
89
90         # use "lfsck_stop -A" when we no longer need testing interop
91         for n in $(seq $MDSCOUNT); do
92                 do_facet mds$n $LCTL lfsck_stop -M $(facet_svc mds$n) ||
93                         error "($error_id) Failed to stop OI scrub on mds$n"
94         done
95 }
96
97 scrub_status() {
98         local n=$1
99
100         do_facet mds$n $LCTL get_param -n \
101                 osd-ldiskfs.$(facet_svc mds$n).oi_scrub
102 }
103
104 START_SCRUB="do_facet $SINGLEMDS $LCTL lfsck_start -M ${MDT_DEV} $SCRUB_ONLY"
105 START_SCRUB_ON_OST="do_facet ost1 $LCTL lfsck_start -M ${OST_DEV} $SCRUB_ONLY"
106 STOP_SCRUB="do_facet $SINGLEMDS $LCTL lfsck_stop -M ${MDT_DEV}"
107 SHOW_SCRUB="do_facet $SINGLEMDS \
108                 $LCTL get_param -n osd-ldiskfs.${MDT_DEV}.oi_scrub"
109 SHOW_SCRUB_ON_OST="do_facet ost1 \
110                 $LCTL get_param -n osd-ldiskfs.${OST_DEV}.oi_scrub"
111 MOUNT_OPTS_SCRUB="-o user_xattr"
112 MOUNT_OPTS_NOSCRUB="-o user_xattr,noscrub"
113
114 scrub_prep() {
115         local nfiles=$1
116         local n
117
118         check_mount_and_prep
119
120         echo "preparing... $(date)"
121         for n in $(seq $MDSCOUNT); do
122                 echo "creating $nfiles files on mds$n"
123                 if [ $n -eq 1 ]; then
124                         mkdir $DIR/$tdir/mds$n ||
125                                 error "Failed to create directory mds$n"
126                 else
127                         $LFS mkdir -i $((n - 1)) $DIR/$tdir/mds$n ||
128                                 error "Failed to create remote directory mds$n"
129                 fi
130                 cp $LUSTRE/tests/*.sh $DIR/$tdir/mds$n ||
131                         error "Failed to copy files to mds$n"
132                 mkdir -p $DIR/$tdir/mds$n/d_$tfile ||
133                         error "mkdir failed on mds$n"
134                 createmany -m $DIR/$tdir/mds$n/d_$tfile/f 2 > \
135                         /dev/null || error "create failed on mds$n"
136                 if [[ $nfiles -gt 0 ]]; then
137                         createmany -m $DIR/$tdir/mds$n/$tfile $nfiles > \
138                                 /dev/null || error "createmany failed on mds$n"
139                 fi
140         done
141         echo "prepared $(date)."
142         cleanup_mount $MOUNT > /dev/null || error "Fail to stop client!"
143         for n in $(seq $MDSCOUNT); do
144                 echo "stop mds$n"
145                 stop mds$n > /dev/null || error "Fail to stop MDS$n!"
146         done
147 }
148
149 scrub_start_mds() {
150         local error_id=$1
151         local opts=$2
152         local n
153
154         for n in $(seq $MDSCOUNT); do
155                 start mds$n $(mdsdevname $n) $opts >/dev/null ||
156                         error "($error_id) Failed to start mds$n"
157         done
158 }
159
160 scrub_stop_mds() {
161         local error_id=$1
162         local n
163
164         for n in $(seq $MDSCOUNT); do
165                 echo "stopping mds$n"
166                 stop mds$n >/dev/null ||
167                         error "($error_id) Failed to stop mds$n"
168         done
169 }
170
171 scrub_check_status() {
172         local error_id=$1
173         local expected=$2
174         local n
175
176         for n in $(seq $MDSCOUNT); do
177                 wait_update_facet mds$n "$LCTL get_param -n \
178                         osd-ldiskfs.$(facet_svc mds$n).oi_scrub |
179                         awk '/^status/ { print \\\$2 }'" "$expected" 6 ||
180                         error "($error_id) Expected '$expected' on mds$n"
181         done
182 }
183
184 scrub_check_flags() {
185         local error_id=$1
186         local expected=$2
187         local actual
188         local n
189
190         for n in $(seq $MDSCOUNT); do
191                 actual=$(do_facet mds$n $LCTL get_param -n \
192                         osd-ldiskfs.$(facet_svc mds$n).oi_scrub |
193                         awk '/^flags/ { print $2 }')
194                 if [ "$actual" != "$expected" ]; then
195                         error "($error_id) Expected '$expected' on mds$n, but" \
196                                "got '$actual'"
197                 fi
198         done
199 }
200
201 scrub_check_params() {
202         local error_id=$1
203         local expected=$2
204         local actual
205         local n
206
207         for n in $(seq $MDSCOUNT); do
208                 actual=$(do_facet mds$n $LCTL get_param -n \
209                         osd-ldiskfs.$(facet_svc mds$n).oi_scrub |
210                         awk '/^param/ { print $2 }')
211                 if [ "$actual" != "$expected" ]; then
212                         error "($error_id) Expected '$expected' on mds$n, but" \
213                                "got '$actual'"
214                 fi
215         done
216 }
217
218 scrub_check_repaired() {
219         local error_id=$1
220         local expected=$2
221         local actual
222         local n
223
224         for n in $(seq $MDSCOUNT); do
225                 actual=$(do_facet mds$n $LCTL get_param -n \
226                         osd-ldiskfs.$(facet_svc mds$n).oi_scrub |
227                         awk '/^updated/ { print $2 }')
228
229                 if [ $expected -eq 0 -a $actual -ne 0 ]; then
230                         error "($error_id) Expected no repaired on mds$n, but" \
231                                "got '$actual'"
232                 fi
233
234                 if [ $expected -ne 0 -a $actual -lt $expected ]; then
235                         error "($error_id) Expected '$expected' on mds$n, but" \
236                                "got '$actual'"
237                 fi
238         done
239 }
240
241 scrub_check_data() {
242         local error_id=$1
243         local n
244
245         for n in $(seq $MDSCOUNT); do
246                 diff -q $LUSTRE/tests/test-framework.sh \
247                         $DIR/$tdir/mds$n/test-framework.sh ||
248                         error "($error_id) File data check failed"
249         done
250 }
251
252 scrub_check_data2() {
253         local filename=$1
254         local error_id=$2
255         local n
256
257         for n in $(seq $MDSCOUNT); do
258                 diff -q $LUSTRE/tests/$filename \
259                         $DIR/$tdir/mds$n/$filename ||
260                         error "($error_id) File data check failed"
261         done
262 }
263
264 scrub_remove_ois() {
265         local error_id=$1
266         local index=$2
267         local n
268
269         for n in $(seq $MDSCOUNT); do
270                 mds_remove_ois mds$n $index ||
271                         error "($error_id) Failed to remove OI .$index on mds$n"
272         done
273 }
274
275 scrub_backup_restore() {
276         local error_id=$1
277         local igif=$2
278         local n
279
280         for n in $(seq $MDSCOUNT); do
281                 mds_backup_restore mds$n $igif ||
282                         error "(error_id) Backup/restore on mds$n failed"
283         done
284 }
285
286 scrub_enable_auto() {
287         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param -n \
288                 osd-ldiskfs.*.auto_scrub=1
289 }
290
291 full_scrub_ratio() {
292         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.6.50) ]] &&
293                 return
294
295         local ratio=$1
296
297         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param -n \
298                 osd-ldiskfs.*.full_scrub_ratio=$ratio
299 }
300
301 full_scrub_threshold_rate() {
302         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.6.50) ]] &&
303                 return
304
305         local rate=$1
306
307         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param -n \
308                 osd-ldiskfs.*.full_scrub_threshold_rate=$rate
309 }
310
311 test_0() {
312         scrub_prep 0
313         echo "starting MDTs without disabling OI scrub"
314         scrub_start_mds 1 "$MOUNT_OPTS_SCRUB"
315         scrub_check_status 2 init
316         scrub_check_flags 3 ""
317         mount_client $MOUNT || error "(4) Fail to start client!"
318         scrub_check_data 5
319 }
320 run_test 0 "Do not auto trigger OI scrub for non-backup/restore case"
321
322 test_1a() {
323         scrub_prep 0
324         echo "start $SINGLEMDS without disabling OI scrub"
325         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
326                 error "(1) Fail to start MDS!"
327
328         local FLAGS=$($SHOW_SCRUB | awk '/^flags/ { print $2 }')
329         [ -z "$FLAGS" ] || error "(3) Expect empty flags, but got '$FLAGS'"
330
331         mount_client $MOUNT || error "(4) Fail to start client!"
332         #define OBD_FAIL_OSD_FID_MAPPING                        0x193
333         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x193
334         # update .lustre OI mapping
335         touch $MOUNT/.lustre
336         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
337         umount_client $MOUNT || error "(5) Fail to stop client!"
338
339         echo "stop $SINGLEMDS"
340         stop $SINGLEMDS > /dev/null || error "(6) Fail to stop MDS!"
341
342         echo "start $SINGLEMDS with disabling OI scrub"
343         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_NOSCRUB > /dev/null ||
344                 error "(7) Fail to start MDS!"
345
346         local FLAGS=$($SHOW_SCRUB | awk '/^flags/ { print $2 }')
347         [ "$FLAGS" == "inconsistent" ] ||
348                 error "(9) Expect 'inconsistent', but got '$FLAGS'"
349 }
350 run_test 1a "Auto trigger initial OI scrub when server mounts"
351
352 test_1b() {
353         scrub_prep 0
354         scrub_remove_ois 1
355         echo "start MDTs without disabling OI scrub"
356         scrub_start_mds 2 "$MOUNT_OPTS_SCRUB"
357         scrub_check_status 3 completed
358         mount_client $MOUNT || error "(4) Fail to start client!"
359         scrub_check_data 5
360 }
361 run_test 1b "Trigger OI scrub when MDT mounts for OI files remove/recreate case"
362
363 test_1c() {
364         local index
365
366         # OI files to be removed:
367         # idx 0: oi.16.0
368         # idx 2: oi.16.{2,4,8,16,32}
369         # idx 3: oi.16.{3,9,27}
370         for index in 0 2 3; do
371                 scrub_prep 0
372                 scrub_remove_ois 1 $index
373                 echo "start MDTs with OI scrub disabled"
374                 scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
375                 scrub_check_flags 3 recreated
376                 scrub_start 4
377                 scrub_check_status 5 completed
378                 scrub_check_flags 6 ""
379         done
380 }
381 run_test 1c "Auto detect kinds of OI file(s) removed/recreated cases"
382
383 test_2() {
384         scrub_prep 0
385         scrub_backup_restore 1
386         echo "starting MDTs without disabling OI scrub"
387         scrub_start_mds 2 "$MOUNT_OPTS_SCRUB"
388         scrub_check_status 3 completed
389         mount_client $MOUNT || error "(4) Fail to start client!"
390         scrub_check_data 5
391 }
392 run_test 2 "Trigger OI scrub when MDT mounts for backup/restore case"
393
394 # test_3 is obsolete, it will be covered by test_5.
395 test_3() {
396         formatall > /dev/null
397         setupall > /dev/null
398
399         scrub_prep 0
400         scrub_backup_restore 1
401         echo "starting MDTs with OI scrub disabled"
402         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
403         scrub_check_status 3 init
404         scrub_check_flags 4 inconsistent
405 }
406 #run_test 3 "Do not trigger OI scrub when MDT mounts if 'noscrub' specified"
407
408 test_4a() {
409         scrub_prep 0
410         scrub_backup_restore 1
411         echo "starting MDTs with OI scrub disabled"
412         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
413         scrub_check_flags 4 inconsistent
414         mount_client $MOUNT || error "(5) Fail to start client!"
415         scrub_enable_auto
416         full_scrub_ratio 0
417         scrub_check_data 6
418         sleep 3
419
420         scrub_check_status 7 completed
421         scrub_check_flags 8 ""
422
423         local -a updated0
424         for n in $(seq $MDSCOUNT); do
425                 updated0[$n]=$(scrub_status $n |
426                                awk '/^sf_items_updated_prior/ { print $2 }')
427         done
428
429         scrub_check_data2 sanity-scrub.sh 9
430         sleep 3
431
432         local -a updated1
433         for n in $(seq $MDSCOUNT); do
434                 updated1[$n]=$(scrub_status $n |
435                                awk '/^sf_items_updated_prior/ { print $2 }')
436                 [ ${updated0[$n]} -eq ${updated1[$n]} ] ||
437                         error "(10) NOT auto trigger full scrub as expected"
438         done
439 }
440 run_test 4a "Auto trigger OI scrub if bad OI mapping was found (1)"
441
442 test_4b() {
443         scrub_prep 5
444         scrub_backup_restore 1
445         echo "starting MDTs with OI scrub disabled"
446         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
447         scrub_check_flags 4 inconsistent
448         mount_client $MOUNT || error "(5) Fail to start client!"
449         scrub_enable_auto
450         full_scrub_ratio 10
451         full_scrub_threshold_rate 10000
452         scrub_check_data 6
453         sleep 3
454
455         scrub_check_status 7 completed
456         scrub_check_flags 8 ""
457
458         local -a updated0
459         for n in $(seq $MDSCOUNT); do
460                 updated0[$n]=$(scrub_status $n |
461                                awk '/^sf_items_updated_prior/ { print $2 }')
462         done
463
464         scrub_check_data2 sanity-scrub.sh 9
465         sleep 3
466
467         scrub_check_status 10 completed
468         scrub_check_flags 11 ""
469
470         local -a updated1
471         for n in $(seq $MDSCOUNT); do
472                 updated1[$n]=$(scrub_status $n |
473                                awk '/^sf_items_updated_prior/ { print $2 }')
474                 [ ${updated0[$n]} -lt ${updated1[$n]} ] ||
475                         error "(12) Auto trigger full scrub unexpectedly"
476         done
477
478         for n in $(seq $MDSCOUNT); do
479                 ls -l $DIR/$tdir/mds$n/*.sh > /dev/null ||
480                         error "(13) fail to ls"
481         done
482         sleep 3
483
484         scrub_check_status 14 completed
485         scrub_check_flags 15 ""
486
487         for n in $(seq $MDSCOUNT); do
488                 updated0[$n]=$(scrub_status $n |
489                                awk '/^sf_items_updated_prior/ { print $2 }')
490                 [ ${updated0[$n]} -gt ${updated1[$n]} ] ||
491                         error "(16) Auto trigger full scrub unexpectedly"
492         done
493
494         for n in $(seq $MDSCOUNT); do
495                 ls -l $DIR/$tdir/mds$n/d_${tfile}/ || error "(17) fail to ls"
496         done
497         sleep 3
498
499         for n in $(seq $MDSCOUNT); do
500                 updated1[$n]=$(scrub_status $n |
501                                awk '/^sf_items_updated_prior/ { print $2 }')
502                 [ ${updated0[$n]} -eq ${updated1[$n]} ] ||
503                         error "(18) NOT auto trigger full scrub as expected"
504         done
505 }
506 run_test 4b "Auto trigger OI scrub if bad OI mapping was found (2)"
507
508 test_4c() {
509         scrub_prep 500
510         scrub_backup_restore 1
511         echo "starting MDTs with OI scrub disabled"
512         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
513         scrub_check_flags 4 inconsistent
514         mount_client $MOUNT || error "(5) Fail to start client!"
515         scrub_enable_auto
516         full_scrub_ratio 2
517         full_scrub_threshold_rate 20
518         scrub_check_data 6
519         sleep 3
520
521         scrub_check_status 7 completed
522         scrub_check_flags 8 ""
523
524         local -a updated0
525         for n in $(seq $MDSCOUNT); do
526                 updated0[$n]=$(scrub_status $n |
527                                awk '/^sf_items_updated_prior/ { print $2 }')
528         done
529
530         scrub_check_data2 sanity-scrub.sh 9
531         sleep 3
532
533         scrub_check_status 10 completed
534         scrub_check_flags 11 ""
535
536         local -a updated1
537         for n in $(seq $MDSCOUNT); do
538                 updated1[$n]=$(scrub_status $n |
539                                awk '/^sf_items_updated_prior/ { print $2 }')
540                 [ ${updated0[$n]} -lt ${updated1[$n]} ] ||
541                         error "(12) Auto trigger full scrub unexpectedly"
542         done
543
544         for n in $(seq $MDSCOUNT); do
545                 ls -l $DIR/$tdir/mds$n/*.sh > /dev/null ||
546                         error "(13) fail to ls"
547         done
548         sleep 3
549
550         scrub_check_status 14 completed
551         scrub_check_flags 15 ""
552
553         for n in $(seq $MDSCOUNT); do
554                 updated0[$n]=$(scrub_status $n |
555                                awk '/^sf_items_updated_prior/ { print $2 }')
556                 [ ${updated0[$n]} -gt ${updated1[$n]} ] ||
557                         error "(16) Auto trigger full scrub unexpectedly"
558         done
559
560         for n in $(seq $MDSCOUNT); do
561                 ls -l $DIR/$tdir/mds$n/${tfile}1 || error "(17) fail to ls"
562         done
563         sleep 3
564
565         for n in $(seq $MDSCOUNT); do
566                 updated1[$n]=$(scrub_status $n |
567                                awk '/^sf_items_updated_prior/ { print $2 }')
568                 [ ${updated0[$n]} -eq ${updated1[$n]} ] ||
569                         error "(18) NOT auto trigger full scrub as expected"
570         done
571 }
572 run_test 4c "Auto trigger OI scrub if bad OI mapping was found (3)"
573
574 test_5() {
575         formatall > /dev/null
576         setupall > /dev/null
577
578         scrub_prep 1000
579         scrub_backup_restore 1
580         echo "starting MDTs with OI scrub disabled"
581         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
582         scrub_check_status 3 init
583         scrub_check_flags 4 inconsistent
584         mount_client $MOUNT || error "(5) Fail to start client!"
585         scrub_enable_auto
586
587         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
588         do_nodes $(comma_list $(mdts_nodes)) \
589                 $LCTL set_param fail_val=3 fail_loc=0x190
590
591         full_scrub_ratio 0
592         scrub_check_data 6
593         umount_client $MOUNT || error "(7) Fail to stop client!"
594         scrub_check_status 8 scanning
595
596         #define OBD_FAIL_OSD_SCRUB_CRASH         0x191
597         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x191
598
599         sleep 4
600         scrub_stop_mds 9
601
602         do_nodes $(comma_list $(mdts_nodes)) \
603                 $LCTL set_param fail_loc=0 fail_val=0
604
605         echo "starting MDTs with OI scrub disabled"
606         scrub_start_mds 10 "$MOUNT_OPTS_NOSCRUB"
607         scrub_check_status 11 crashed
608         scrub_stop_mds 12
609
610         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
611         do_nodes $(comma_list $(mdts_nodes)) \
612                 $LCTL set_param fail_val=3 fail_loc=0x190
613
614         echo "starting MDTs without disabling OI scrub"
615         scrub_start_mds 13 "$MOUNT_OPTS_SCRUB"
616         scrub_check_status 14 scanning
617
618         #define OBD_FAIL_OSD_SCRUB_FATAL         0x192
619         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x192
620
621         scrub_check_status 15 failed
622         mount_client $MOUNT || error "(16) Fail to start client!"
623
624         full_scrub_ratio 0
625         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
626         do_nodes $(comma_list $(mdts_nodes)) \
627                 $LCTL set_param fail_val=3 fail_loc=0x190
628
629         local n
630         for n in $(seq $MDSCOUNT); do
631                 stat $DIR/$tdir/mds$n/${tfile}800 ||
632                         error "(17) Failed to stat mds$n/${tfile}800"
633         done
634
635         scrub_check_status 18 scanning
636
637         do_nodes $(comma_list $(mdts_nodes)) \
638                 $LCTL set_param fail_loc=0 fail_val=0
639
640         scrub_check_status 19 completed
641         scrub_check_flags 20 ""
642 }
643 run_test 5 "OI scrub state machine"
644
645 test_6() {
646         scrub_prep 1000
647         scrub_backup_restore 1
648         echo "starting MDTs with OI scrub disabled"
649         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
650         scrub_check_flags 4 inconsistent
651         mount_client $MOUNT || error "(5) Fail to start client!"
652         scrub_enable_auto
653
654         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
655         do_nodes $(comma_list $(mdts_nodes)) \
656                 $LCTL set_param fail_val=2 fail_loc=0x190
657
658         full_scrub_ratio 0
659         scrub_check_data 6
660
661         # Sleep 5 sec to guarantee at least one object processed by OI scrub
662         sleep 5
663         # Fail the OI scrub to guarantee there is at least one checkpoint
664         #define OBD_FAIL_OSD_SCRUB_FATAL         0x192
665         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x192
666
667         scrub_check_status 7 failed
668
669         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
670         do_nodes $(comma_list $(mdts_nodes)) \
671                 $LCTL set_param fail_val=3 fail_loc=0x190
672
673         local n
674         for n in $(seq $MDSCOUNT); do
675                 # stat will re-trigger OI scrub
676                 stat $DIR/$tdir/mds$n/${tfile}800 ||
677                         error "(8) Failed to stat mds$n/${tfile}800"
678         done
679
680         umount_client $MOUNT || error "(9) Fail to stop client!"
681         scrub_check_status 10 scanning
682
683         #define OBD_FAIL_OSD_SCRUB_CRASH         0x191
684         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x191
685
686         sleep 4
687         local -a position0
688         for n in $(seq $MDSCOUNT); do
689                 position0[$n]=$(scrub_status $n |
690                         awk '/^last_checkpoint_position/ {print $2}')
691                 position0[$n]=$((${position0[$n]} + 1))
692         done
693
694         scrub_stop_mds 11
695
696         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
697         do_nodes $(comma_list $(mdts_nodes)) \
698                 $LCTL set_param fail_val=3 fail_loc=0x190
699
700         echo "starting MDTs without disabling OI scrub"
701         scrub_start_mds 12 "$MOUNT_OPTS_SCRUB"
702
703         scrub_check_status 13 scanning
704
705         local -a position1
706         for n in $(seq $MDSCOUNT); do
707                 position1[$n]=$(scrub_status $n |
708                         awk '/^latest_start_position/ {print $2}')
709                 if [ ${position0[$n]} -ne ${position1[$n]} ]; then
710                         error "(14) Expected position ${position0[$n]}, but" \
711                                 "got ${position1[$n]}"
712                 fi
713         done
714
715         do_nodes $(comma_list $(mdts_nodes)) \
716                 $LCTL set_param fail_loc=0 fail_val=0
717
718         scrub_check_status 15 completed
719         scrub_check_flags 16 ""
720 }
721 run_test 6 "OI scrub resumes from last checkpoint"
722
723 test_7() {
724         scrub_prep 500
725         scrub_backup_restore 1
726         echo "starting MDTs with OI scrub disabled"
727         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
728         scrub_check_flags 4 inconsistent
729         mount_client $MOUNT || error "(5) Fail to start client!"
730         scrub_enable_auto
731
732         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
733         do_nodes $(comma_list $(mdts_nodes)) \
734                 $LCTL set_param fail_val=3 fail_loc=0x190
735
736         full_scrub_ratio 0
737         scrub_check_data 6
738
739         local n
740         for n in $(seq $MDSCOUNT); do
741                 stat $DIR/$tdir/mds$n/${tfile}300 ||
742                         error "(7) Failed to stat mds$n/${tfile}300!"
743         done
744
745         scrub_check_status 8 scanning
746         scrub_check_flags 9 inconsistent,auto
747
748         do_nodes $(comma_list $(mdts_nodes)) \
749                 $LCTL set_param fail_loc=0 fail_val=0
750
751         scrub_check_status 10 completed
752         scrub_check_flags ""
753 }
754 run_test 7 "System is available during OI scrub scanning"
755
756 test_8() {
757         scrub_prep 128
758         scrub_backup_restore 1
759         echo "starting MDTs with OI scrub disabled"
760         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
761         scrub_check_flags 4 inconsistent
762
763         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
764         do_nodes $(comma_list $(mdts_nodes)) \
765                 $LCTL set_param fail_val=1 fail_loc=0x190
766
767         scrub_start 5
768         scrub_check_status 6 scanning
769         scrub_stop 7
770         scrub_check_status 8 stopped
771         scrub_start 9
772         scrub_check_status 10 scanning
773
774         do_nodes $(comma_list $(mdts_nodes)) \
775                 $LCTL set_param fail_loc=0 fail_val=0
776
777         scrub_check_status 11 completed
778         scrub_check_flags 12 ""
779 }
780 run_test 8 "Control OI scrub manually"
781
782 test_9() {
783         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
784                 skip "Testing on UP system, the speed may be inaccurate."
785                 return 0
786         fi
787
788         scrub_prep 6000
789         scrub_backup_restore 1
790
791         echo "starting MDTs with OI scrub disabled"
792         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
793         scrub_check_flags 4 inconsistent
794
795         local BASE_SPEED1=100
796         local RUN_TIME1=10
797         # OI scrub should run with full speed under inconsistent case
798         scrub_start 5 -s $BASE_SPEED1
799
800         sleep $RUN_TIME1
801         scrub_check_status 6 completed
802         scrub_check_flags 7 ""
803         # OI scrub should run with limited speed under non-inconsistent case
804         scrub_start 8 -s $BASE_SPEED1 -r
805
806         sleep $RUN_TIME1
807         scrub_check_status 9 scanning
808
809         # Do NOT ignore that there are 1024 pre-fetched items. And there
810         # may be time error, normally it should be less than 2 seconds.
811         # We allow another 20% schedule error.
812         local PRE_FETCHED=1024
813         local TIME_DIFF=2
814         # MAX_MARGIN = 1.2 = 12 / 10
815         local MAX_SPEED=$(((PRE_FETCHED + BASE_SPEED1 * \
816                 (RUN_TIME1 + TIME_DIFF)) / RUN_TIME1 * 12 / 10))
817         local n
818         for n in $(seq $MDSCOUNT); do
819                 local SPEED=$(scrub_status $n | \
820                         awk '/^average_speed/ { print $2 }')
821                 [ $SPEED -lt $MAX_SPEED ] ||
822                         error "(10) Got speed $SPEED, expected less than" \
823                                 "$MAX_SPEED"
824         done
825
826         # adjust speed limit
827         local BASE_SPEED2=300
828         local RUN_TIME2=10
829         for n in $(seq $MDSCOUNT); do
830                 do_facet mds$n $LCTL set_param -n \
831                         mdd.$(facet_svc mds$n).lfsck_speed_limit $BASE_SPEED2
832         done
833         sleep $RUN_TIME2
834
835         # MIN_MARGIN = 0.8 = 8 / 10
836         local MIN_SPEED=$(((PRE_FETCHED + \
837                             BASE_SPEED1 * (RUN_TIME1 - TIME_DIFF) + \
838                             BASE_SPEED2 * (RUN_TIME2 - TIME_DIFF)) / \
839                            (RUN_TIME1 + RUN_TIME2) * 8 / 10))
840         # MAX_MARGIN = 1.2 = 12 / 10
841         MAX_SPEED=$(((PRE_FETCHED + \
842                       BASE_SPEED1 * (RUN_TIME1 + TIME_DIFF) + \
843                       BASE_SPEED2 * (RUN_TIME2 + TIME_DIFF)) / \
844                      (RUN_TIME1 + RUN_TIME2) * 12 / 10))
845         for n in $(seq $MDSCOUNT); do
846                 SPEED=$(scrub_status $n | awk '/^average_speed/ { print $2 }')
847                 [ $SPEED -gt $MIN_SPEED ] ||
848                         error "(11) Got speed $SPEED, expected more than" \
849                                 "$MIN_SPEED"
850                 [ $SPEED -lt $MAX_SPEED ] ||
851                         error "(12) Got speed $SPEED, expected less than" \
852                                 "$MAX_SPEED"
853
854                 do_facet mds$n $LCTL set_param -n \
855                                 mdd.$(facet_svc mds$n).lfsck_speed_limit 0
856         done
857
858         scrub_check_status 13 completed
859 }
860 run_test 9 "OI scrub speed control"
861
862 test_10a() {
863         scrub_prep 0
864         scrub_backup_restore 1
865         echo "starting mds$n with OI scrub disabled"
866         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
867         scrub_check_flags 4 inconsistent
868         mount_client $MOUNT || error "(5) Fail to start client!"
869         scrub_enable_auto
870
871         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
872         do_nodes $(comma_list $(mdts_nodes)) \
873                 $LCTL set_param fail_val=1 fail_loc=0x190
874
875         full_scrub_ratio 0
876         scrub_check_data 6
877         scrub_check_status 7 scanning
878         umount_client $MOUNT || error "(8) Fail to stop client!"
879         scrub_stop_mds 9
880         echo "starting MDTs with OI scrub disabled"
881         scrub_start_mds 10 "$MOUNT_OPTS_NOSCRUB"
882         scrub_check_status 11 paused
883         scrub_stop_mds 12
884         echo "starting MDTs without disabling OI scrub"
885         scrub_start_mds 13 "$MOUNT_OPTS_SCRUB"
886         scrub_check_status 14 scanning
887
888         do_nodes $(comma_list $(mdts_nodes)) \
889                 $LCTL set_param fail_loc=0 fail_val=0
890
891         scrub_check_status 15 completed
892         scrub_check_flags 16 ""
893 }
894 run_test 10a "non-stopped OI scrub should auto restarts after MDS remount (1)"
895
896 # test_10b is obsolete, it will be coverded by related sanity-lfsck tests.
897 test_10b() {
898         scrub_prep 0
899         scrub_backup_restore 1
900         echo "starting MDTs with OI scrub disabled"
901         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
902         scrub_check_flags 4 inconsistent
903
904         #define OBD_FAIL_OSD_SCRUB_DELAY         0x190
905         do_nodes $(comma_list $(mdts_nodes)) \
906                 $LCTL set_param fail_val=3 fail_loc=0x190
907
908         scrub_start 5
909         scrub_check_status 6 scanning
910         scrub_stop_mds 7
911         echo "starting MDTs with OI scrub disabled"
912         scrub_start_mds 8 "$MOUNT_OPTS_NOSCRUB"
913         scrub_check_status 9 paused
914         scrub_stop_mds 10
915         echo "starting MDTs without disabling OI scrub"
916         scrub_start_mds 11 "$MOUNT_OPTS_SCRUB"
917         scrub_check_status 12 scanning
918
919         do_nodes $(comma_list $(mdts_nodes)) \
920                 $LCTL set_param fail_loc=0 fail_val=0
921
922         scrub_check_status 13 completed
923         scrub_check_flags 14 ""
924 }
925 #run_test 10b "non-stopped OI scrub should auto restarts after MDS remount (2)"
926
927 test_11() {
928         local CREATED=100
929         local n
930
931         check_mount_and_prep
932
933         for n in $(seq $MDSCOUNT); do
934                 $LFS mkdir -i $((n - 1)) $DIR/$tdir/mds$n ||
935                         error "(1) Fail to mkdir $DIR/$tdir/mds$n"
936
937                 createmany -o $DIR/$tdir/mds$n/f $CREATED ||
938                         error "(2) Fail to create under $tdir/mds$n"
939         done
940
941         # reset OI scrub start point by force
942         scrub_start 3 -r
943         scrub_check_status 4 completed
944
945         declare -a checked0
946         declare -a checked1
947
948         # OI scrub should skip the new created objects for the first accessing
949         # notice we're creating a new llog for every OST on every startup
950         # new features can make this even less stable, so we only check that
951         # the number of skipped files is more than the number or known created
952         local MINIMUM=$((CREATED + 1)) # files + directory
953         for n in $(seq $MDSCOUNT); do
954                 local SKIPPED=$(scrub_status $n | awk '/^noscrub/ { print $2 }')
955                 [ $SKIPPED -lt $MINIMUM ] &&
956                         error "(5) Expect at least $MINIMUM objects" \
957                                 "skipped on mds$n, but got $SKIPPED"
958
959                 checked0[$n]=$(scrub_status $n | awk '/^checked/ { print $2 }')
960         done
961
962         # reset OI scrub start point by force
963         scrub_start 6 -r
964         scrub_check_status 7 completed
965
966         # OI scrub should skip the new created object only once
967         for n in $(seq $MDSCOUNT); do
968                 SKIPPED=$(scrub_status $n | awk '/^noscrub/ { print $2 }')
969                 checked1[$n]=$(scrub_status $n | awk '/^checked/ { print $2 }')
970
971                 [ ${checked0[$n]} -ne ${checked1[$n]} -o $SKIPPED -eq 0 ] ||
972                         error "(8) Expect 0 objects skipped on mds$n, but" \
973                                 "got $SKIPPED"
974         done
975 }
976 run_test 11 "OI scrub skips the new created objects only once"
977
978 test_12() {
979         check_mount_and_prep
980         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
981
982         #define OBD_FAIL_OSD_COMPAT_INVALID_ENTRY               0x195
983         do_facet ost1 $LCTL set_param fail_loc=0x195
984         local count=$(precreated_ost_obj_count 0 0)
985
986         createmany -o $DIR/$tdir/f $((count + 32))
987         umount_client $MOUNT || error "(1) Fail to stop client!"
988
989         stop ost1 || error "(2) Fail to stop ost1"
990
991         #define OBD_FAIL_OST_NODESTROY           0x233
992         do_facet ost1 $LCTL set_param fail_loc=0x233
993
994         start ost1 $(ostdevname 1) $MOUNT_OPTS_NOSCRUB ||
995                 error "(3) Fail to start ost1"
996
997         mount_client $MOUNT || error "(4) Fail to start client!"
998
999         ls -ail $DIR/$tdir > /dev/null 2>&1 && error "(5) ls should fail"
1000
1001         $START_SCRUB_ON_OST -r || error "(6) Fail to start OI scrub on OST!"
1002
1003         do_facet ost1 $LCTL set_param fail_loc=0
1004         wait_update_facet ost1 "$LCTL get_param -n \
1005                 osd-ldiskfs.$(facet_svc ost1).oi_scrub |
1006                 awk '/^status/ { print \\\$2 }'" "completed" 6 ||
1007                 error "(7) Expected '$expected' on ost1"
1008
1009         ls -ail $DIR/$tdir > /dev/null || {
1010                 $SHOW_SCRUB_ON_OST
1011                 error "(8) ls should succeed"
1012         }
1013 }
1014 run_test 12 "OI scrub can rebuild invalid /O entries"
1015
1016 test_13() {
1017         check_mount_and_prep
1018         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
1019
1020         #define OBD_FAIL_OSD_COMPAT_NO_ENTRY            0x196
1021         do_facet ost1 $LCTL set_param fail_loc=0x196
1022         local count=$(precreated_ost_obj_count 0 0)
1023
1024         createmany -o $DIR/$tdir/f $((count + 32))
1025         do_facet ost1 $LCTL set_param fail_loc=0
1026
1027         umount_client $MOUNT || error "(1) Fail to stop client!"
1028
1029         stop ost1 || error "(2) Fail to stop ost1"
1030
1031         start ost1 $(ostdevname 1) $MOUNT_OPTS_NOSCRUB ||
1032                 error "(3) Fail to start ost1"
1033
1034         mount_client $MOUNT || error "(4) Fail to start client!"
1035
1036         ls -ail $DIR/$tdir > /dev/null 2>&1 && error "(5) ls should fail"
1037
1038         $START_SCRUB_ON_OST -r || error "(6) Fail to start OI scrub on OST!"
1039
1040         wait_update_facet ost1 "$LCTL get_param -n \
1041                 osd-ldiskfs.$(facet_svc ost1).oi_scrub |
1042                 awk '/^status/ { print \\\$2 }'" "completed" 6 ||
1043                 error "(7) Expected '$expected' on ost1"
1044
1045         ls -ail $DIR/$tdir > /dev/null || error "(8) ls should succeed"
1046 }
1047 run_test 13 "OI scrub can rebuild missed /O entries"
1048
1049 test_14() {
1050         check_mount_and_prep
1051         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
1052
1053         #define OBD_FAIL_OSD_COMPAT_NO_ENTRY            0x196
1054         do_facet ost1 $LCTL set_param fail_loc=0x196
1055         local count=$(precreated_ost_obj_count 0 0)
1056
1057         createmany -o $DIR/$tdir/f $((count + 32))
1058         do_facet ost1 $LCTL set_param fail_loc=0
1059
1060         umount_client $MOUNT || error "(1) Fail to stop client!"
1061
1062         stop ost1 || error "(2) Fail to stop ost1"
1063
1064         echo "run e2fsck"
1065         run_e2fsck $(facet_host ost1) $(ostdevname 1) "-y" ||
1066                 error "(3) Fail to run e2fsck error"
1067
1068         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS ||
1069                 error "(4) Fail to start ost1"
1070
1071         mount_client $MOUNT || error "(5) Fail to start client!"
1072
1073         local LF_REPAIRED=$($SHOW_SCRUB_ON_OST |
1074                             awk '/^lf_reparied/ { print $2 }')
1075         [ $LF_REPAIRED -gt 0 ] ||
1076                 error "(6) Some entry under /lost+found should be repaired"
1077
1078         ls -ail $DIR/$tdir > /dev/null || error "(7) ls should succeed"
1079 }
1080 run_test 14 "OI scrub can repair objects under lost+found"
1081
1082 test_15() {
1083         # skip test_15 for LU-4182
1084         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return
1085         local server_version=$(lustre_version_code $SINGLEMDS)
1086         scrub_prep 20
1087         scrub_backup_restore 1
1088         echo "starting MDTs with OI scrub disabled"
1089         scrub_start_mds 2 "$MOUNT_OPTS_NOSCRUB"
1090         scrub_check_status 3 init
1091         scrub_check_flags 4 inconsistent
1092
1093         # run under dryrun mode
1094         if [ $server_version -lt $(version_code 2.5.58) ]; then
1095                 scrub_start 5 --dryrun on
1096         else
1097                 scrub_start 5 --dryrun
1098         fi
1099         scrub_check_status 6 completed
1100         scrub_check_flags 7 inconsistent
1101         scrub_check_params 8 dryrun
1102         scrub_check_repaired 9 20
1103
1104         # run under dryrun mode again
1105         if [ $server_version -lt $(version_code 2.5.58) ]; then
1106                 scrub_start 10 --dryrun on
1107         else
1108                 scrub_start 10 --dryrun
1109         fi
1110         scrub_check_status 11 completed
1111         scrub_check_flags 12 inconsistent
1112         scrub_check_params 13 dryrun
1113         scrub_check_repaired 14 20
1114
1115         # run under normal mode
1116         #
1117         # Lustre-2.x (x <= 5) used "-n off" to disable dryrun which does not
1118         # work under Lustre-2.y (y >= 6), the test script should be fixed as
1119         # "-noff" or "--dryrun=off" or nothing by default.
1120         if [ $server_version -lt $(version_code 2.5.58) ]; then
1121                 scrub_start 15 --dryrun off
1122         else
1123                 scrub_start 15
1124         fi
1125         scrub_check_status 16 completed
1126         scrub_check_flags 17 ""
1127         scrub_check_params 18 ""
1128         scrub_check_repaired 19 20
1129
1130         # run under normal mode again
1131         if [ $server_version -lt $(version_code 2.5.58) ]; then
1132                 scrub_start 20 --dryrun off
1133         else
1134                 scrub_start 20
1135         fi
1136         scrub_check_status 21 completed
1137         scrub_check_flags 22 ""
1138         scrub_check_params 23 ""
1139         scrub_check_repaired 24 0
1140 }
1141 run_test 15 "Dryrun mode OI scrub"
1142
1143 # restore MDS/OST size
1144 MDSSIZE=${SAVED_MDSSIZE}
1145 OSTSIZE=${SAVED_OSTSIZE}
1146 OSTCOUNT=${SAVED_OSTCOUNT}
1147
1148 # cleanup the system at last
1149 formatall
1150
1151 complete $SECONDS
1152 exit_status