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