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