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