Whamcloud - gitweb
LU-4895 lfsck: not create object to repair dangling by default
[fs/lustre-release.git] / lustre / tests / sanity-lfsck.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_LFSCK_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 MCREATE=${MCREATE:-mcreate}
23 SAVED_MDSSIZE=${MDSSIZE}
24 SAVED_OSTSIZE=${OSTSIZE}
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
30 check_and_setup_lustre
31
32 [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.60) ]] &&
33         skip "Need MDS version at least 2.3.60" && check_and_cleanup_lustre &&
34         exit 0
35
36 [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.4.90) ]] &&
37         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 2c"
38
39 [[ $(lustre_version_code ost1) -lt $(version_code 2.5.55) ]] &&
40         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 11 12 13 14 15 16 17 18 19"
41
42 build_test_filter
43
44 $LCTL set_param debug=+lfsck > /dev/null || true
45
46 MDT_DEV="${FSNAME}-MDT0000"
47 OST_DEV="${FSNAME}-OST0000"
48 MDT_DEVNAME=$(mdsdevname ${SINGLEMDS//mds/})
49 START_NAMESPACE="do_facet $SINGLEMDS \
50                 $LCTL lfsck_start -M ${MDT_DEV} -t namespace"
51 START_LAYOUT="do_facet $SINGLEMDS \
52                 $LCTL lfsck_start -M ${MDT_DEV} -t layout"
53 START_LAYOUT_ON_OST="do_facet ost1 $LCTL lfsck_start -M ${OST_DEV} -t layout"
54 STOP_LFSCK="do_facet $SINGLEMDS $LCTL lfsck_stop -M ${MDT_DEV}"
55 SHOW_NAMESPACE="do_facet $SINGLEMDS \
56                 $LCTL get_param -n mdd.${MDT_DEV}.lfsck_namespace"
57 SHOW_LAYOUT="do_facet $SINGLEMDS \
58                 $LCTL get_param -n mdd.${MDT_DEV}.lfsck_layout"
59 SHOW_LAYOUT_ON_OST="do_facet ost1 \
60                 $LCTL get_param -n obdfilter.${OST_DEV}.lfsck_layout"
61 MOUNT_OPTS_SCRUB="-o user_xattr"
62 MOUNT_OPTS_NOSCRUB="-o user_xattr,noscrub"
63
64 lfsck_prep() {
65         local ndirs=$1
66         local nfiles=$2
67         local igif=$3
68
69         echo "formatall"
70         formatall > /dev/null
71
72         echo "setupall"
73         setupall > /dev/null
74
75         if [ ! -z $igif ]; then
76                 #define OBD_FAIL_FID_IGIF       0x1504
77                 do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1504
78         fi
79
80         echo "preparing... ${nfiles} * ${ndirs} files will be created."
81         mkdir -p $DIR/$tdir
82         cp $LUSTRE/tests/*.sh $DIR/
83         for ((i = 0; i < ${ndirs}; i++)); do
84                 mkdir $DIR/$tdir/d${i}
85                 touch $DIR/$tdir/f${i}
86                 for ((j = 0; j < ${nfiles}; j++)); do
87                         touch $DIR/$tdir/d${i}/f${j}
88                 done
89                 mkdir $DIR/$tdir/e${i}
90         done
91
92         if [ ! -z $igif ]; then
93                 touch $DIR/$tdir/dummy
94                 do_facet $SINGLEMDS $LCTL set_param fail_loc=0
95         fi
96
97         echo "prepared."
98         cleanup_mount $MOUNT > /dev/null || error "Fail to stop client!"
99         echo "stop $SINGLEMDS"
100         stop $SINGLEMDS > /dev/null || error "Fail to stop MDS!"
101 }
102
103 test_0() {
104         lfsck_prep 10 10
105         echo "start $SINGLEMDS"
106         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
107                 error "(1) Fail to start MDS!"
108
109         #define OBD_FAIL_LFSCK_DELAY1           0x1600
110         do_facet $SINGLEMDS $LCTL set_param fail_val=3
111         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1600
112         $START_NAMESPACE || error "(2) Fail to start LFSCK for namespace!"
113
114         $SHOW_NAMESPACE || error "Fail to monitor LFSCK (3)"
115
116         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
117         [ "$STATUS" == "scanning-phase1" ] ||
118                 error "(4) Expect 'scanning-phase1', but got '$STATUS'"
119
120         $STOP_LFSCK || error "(5) Fail to stop LFSCK!"
121
122         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
123         [ "$STATUS" == "stopped" ] ||
124                 error "(6) Expect 'stopped', but got '$STATUS'"
125
126         $START_NAMESPACE || error "(7) Fail to start LFSCK for namespace!"
127
128         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
129         [ "$STATUS" == "scanning-phase1" ] ||
130                 error "(8) Expect 'scanning-phase1', but got '$STATUS'"
131
132         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
133         do_facet $SINGLEMDS $LCTL set_param fail_val=0
134         wait_update_facet $SINGLEMDS \
135                 "$LCTL get_param -n mdd.${MDT_DEV}.lfsck_namespace | \
136                  awk '/^status/ { print \\\$2 }'" "completed" 20 || \
137                 error "(9) unexpected status"
138
139         local repaired=$($SHOW_NAMESPACE |
140                          awk '/^updated_phase1/ { print $2 }')
141         [ $repaired -eq 0 ] ||
142                 error "(10) Expect nothing to be repaired, but got: $repaired"
143
144         local scanned1=$($SHOW_NAMESPACE | awk '/^success_count/ { print $2 }')
145         $START_NAMESPACE -r || error "(11) Fail to reset LFSCK!"
146         wait_update_facet $SINGLEMDS \
147                 "$LCTL get_param -n mdd.${MDT_DEV}.lfsck_namespace | \
148                  awk '/^status/ { print \\\$2 }'" "completed" 20 || \
149                 error "(12) unexpected status"
150
151         local scanned2=$($SHOW_NAMESPACE | awk '/^success_count/ { print $2 }')
152         [ $((scanned1 + 1)) -eq $scanned2 ] ||
153                 error "(13) Expect success $((scanned1 + 1)), but got $scanned2"
154
155         echo "stopall, should NOT crash LU-3649"
156         stopall > /dev/null
157 }
158 run_test 0 "Control LFSCK manually"
159
160 test_1a() {
161         [ $(facet_fstype $SINGLEMDS) != ldiskfs ] &&
162                 skip "OI Scrub not implemented for ZFS" && return
163
164         lfsck_prep 1 1
165         echo "start $SINGLEMDS"
166         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
167                 error "(1) Fail to start MDS!"
168
169         mount_client $MOUNT || error "(2) Fail to start client!"
170
171         #define OBD_FAIL_FID_INDIR      0x1501
172         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1501
173         touch $DIR/$tdir/dummy
174
175         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
176         umount_client $MOUNT
177         $START_NAMESPACE || error "(3) Fail to start LFSCK for namespace!"
178
179         sleep 3
180         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
181         [ "$STATUS" == "completed" ] ||
182                 error "(4) Expect 'completed', but got '$STATUS'"
183
184         local repaired=$($SHOW_NAMESPACE |
185                          awk '/^updated_phase1/ { print $2 }')
186         [ $repaired -eq 1 ] ||
187                 error "(5) Fail to repair crashed FID-in-dirent: $repaired"
188
189         mount_client $MOUNT || error "(6) Fail to start client!"
190
191         #define OBD_FAIL_FID_LOOKUP     0x1505
192         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1505
193         ls $DIR/$tdir/ > /dev/null || error "(7) no FID-in-dirent."
194
195         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
196 }
197 run_test 1a "LFSCK can find out and repair crashed FID-in-dirent"
198
199 test_1b()
200 {
201         [ $(facet_fstype $SINGLEMDS) != ldiskfs ] &&
202                 skip "OI Scrub not implemented for ZFS" && return
203
204         lfsck_prep 1 1
205         echo "start $SINGLEMDS"
206         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
207                 error "(1) Fail to start MDS!"
208
209         mount_client $MOUNT || error "(2) Fail to start client!"
210
211         #define OBD_FAIL_FID_INLMA      0x1502
212         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1502
213         touch $DIR/$tdir/dummy
214
215         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
216         umount_client $MOUNT
217         #define OBD_FAIL_FID_NOLMA      0x1506
218         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1506
219         $START_NAMESPACE || error "(3) Fail to start LFSCK for namespace!"
220
221         sleep 3
222         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
223         [ "$STATUS" == "completed" ] ||
224                 error "(4) Expect 'completed', but got '$STATUS'"
225
226         local repaired=$($SHOW_NAMESPACE |
227                          awk '/^updated_phase1/ { print $2 }')
228         [ $repaired -eq 1 ] ||
229                 error "(5) Fail to repair missed FID-in-LMA: $repaired"
230
231         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
232         mount_client $MOUNT || error "(6) Fail to start client!"
233
234         #define OBD_FAIL_FID_LOOKUP     0x1505
235         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1505
236         stat $DIR/$tdir/dummy > /dev/null || error "(7) no FID-in-LMA."
237
238         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
239 }
240 run_test 1b "LFSCK can find out and repair missed FID-in-LMA"
241
242 test_2a() {
243         lfsck_prep 1 1
244         echo "start $SINGLEMDS"
245         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
246                 error "(1) Fail to start MDS!"
247
248         mount_client $MOUNT || error "(2) Fail to start client!"
249
250         #define OBD_FAIL_LFSCK_LINKEA_CRASH     0x1603
251         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1603
252         touch $DIR/$tdir/dummy
253
254         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
255         umount_client $MOUNT
256         $START_NAMESPACE || error "(3) Fail to start LFSCK for namespace!"
257
258         sleep 3
259         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
260         [ "$STATUS" == "completed" ] ||
261                 error "(4) Expect 'completed', but got '$STATUS'"
262
263         local repaired=$($SHOW_NAMESPACE |
264                          awk '/^updated_phase1/ { print $2 }')
265         [ $repaired -eq 1 ] ||
266                 error "(5) Fail to repair crashed linkEA: $repaired"
267
268         mount_client $MOUNT || error "(6) Fail to start client!"
269
270         stat $DIR/$tdir/dummy | grep "Links: 1" > /dev/null ||
271                 error "(7) Fail to stat $DIR/$tdir/dummy"
272
273         local dummyfid=$($LFS path2fid $DIR/$tdir/dummy)
274         local dummyname=$($LFS fid2path $DIR $dummyfid)
275         [ "$dummyname" == "$DIR/$tdir/dummy" ] ||
276                 error "(8) Fail to repair linkEA: $dummyfid $dummyname"
277 }
278 run_test 2a "LFSCK can find out and repair crashed linkEA entry"
279
280 test_2b()
281 {
282         lfsck_prep 1 1
283         echo "start $SINGLEMDS"
284         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
285                 error "(1) Fail to start MDS!"
286
287         mount_client $MOUNT || error "(2) Fail to start client!"
288
289         #define OBD_FAIL_LFSCK_LINKEA_MORE      0x1604
290         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1604
291         touch $DIR/$tdir/dummy
292
293         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
294         umount_client $MOUNT
295         $START_NAMESPACE || error "(3) Fail to start LFSCK for namespace!"
296
297         sleep 3
298         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
299         [ "$STATUS" == "completed" ] ||
300                 error "(4) Expect 'completed', but got '$STATUS'"
301
302         local repaired=$($SHOW_NAMESPACE |
303                          awk '/^updated_phase2/ { print $2 }')
304         [ $repaired -eq 1 ] ||
305                 error "(5) Fail to repair crashed linkEA: $repaired"
306
307         mount_client $MOUNT || error "(6) Fail to start client!"
308
309         stat $DIR/$tdir/dummy | grep "Links: 1" > /dev/null ||
310                 error "(7) Fail to stat $DIR/$tdir/dummy"
311
312         local dummyfid=$($LFS path2fid $DIR/$tdir/dummy)
313         local dummyname=$($LFS fid2path $DIR $dummyfid)
314         [ "$dummyname" == "$DIR/$tdir/dummy" ] ||
315                 error "(8) Fail to repair linkEA: $dummyfid $dummyname"
316 }
317 run_test 2b "LFSCK can find out and remove invalid linkEA entry"
318
319 test_2c()
320 {
321         lfsck_prep 1 1
322         echo "start $SINGLEMDS"
323         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
324                 error "(1) Fail to start MDS!"
325
326         mount_client $MOUNT || error "(2) Fail to start client!"
327
328         #define OBD_FAIL_LFSCK_LINKEA_MORE2     0x1605
329         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1605
330         touch $DIR/$tdir/dummy
331
332         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
333         umount_client $MOUNT
334         $START_NAMESPACE || error "(3) Fail to start LFSCK for namespace!"
335
336         sleep 3
337         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
338         [ "$STATUS" == "completed" ] ||
339                 error "(4) Expect 'completed', but got '$STATUS'"
340
341         local repaired=$($SHOW_NAMESPACE |
342                          awk '/^updated_phase2/ { print $2 }')
343         [ $repaired -eq 1 ] ||
344                 error "(5) Fail to repair crashed linkEA: $repaired"
345
346         mount_client $MOUNT || error "(6) Fail to start client!"
347
348         stat $DIR/$tdir/dummy | grep "Links: 1" > /dev/null ||
349                 error "(7) Fail to stat $DIR/$tdir/dummy"
350
351         local dummyfid=$($LFS path2fid $DIR/$tdir/dummy)
352         local dummyname=$($LFS fid2path $DIR $dummyfid)
353         [ "$dummyname" == "$DIR/$tdir/dummy" ] ||
354                 error "(8) Fail to repair linkEA: $dummyfid $dummyname"
355 }
356 run_test 2c "LFSCK can find out and remove repeated linkEA entry"
357
358 test_4()
359 {
360         [ $(facet_fstype $SINGLEMDS) != ldiskfs ] &&
361                 skip "OI Scrub not implemented for ZFS" && return
362
363         lfsck_prep 3 3
364         mds_backup_restore $SINGLEMDS || error "(1) Fail to backup/restore!"
365         echo "start $SINGLEMDS with disabling OI scrub"
366         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_NOSCRUB > /dev/null ||
367                 error "(2) Fail to start MDS!"
368
369         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
370         [ "$STATUS" == "init" ] ||
371                 error "(3) Expect 'init', but got '$STATUS'"
372
373         #define OBD_FAIL_LFSCK_DELAY2           0x1601
374         do_facet $SINGLEMDS $LCTL set_param fail_val=1
375         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1601
376         $START_NAMESPACE || error "(4) Fail to start LFSCK for namespace!"
377
378         sleep 5
379         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
380         [ "$STATUS" == "scanning-phase1" ] ||
381                 error "(5) Expect 'scanning-phase1', but got '$STATUS'"
382
383         local FLAGS=$($SHOW_NAMESPACE | awk '/^flags/ { print $2 }')
384         [ "$FLAGS" == "inconsistent" ] ||
385                 error "(6) Expect 'inconsistent', but got '$FLAGS'"
386
387         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
388         do_facet $SINGLEMDS $LCTL set_param fail_val=0
389         sleep 3
390         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
391         [ "$STATUS" == "completed" ] ||
392                 error "(7) Expect 'completed', but got '$STATUS'"
393
394         FLAGS=$($SHOW_NAMESPACE | awk '/^flags/ { print $2 }')
395         [ -z "$FLAGS" ] || error "(8) Expect empty flags, but got '$FLAGS'"
396
397         local repaired=$($SHOW_NAMESPACE |
398                          awk '/^updated_phase1/ { print $2 }')
399         [ $repaired -ge 9 ] ||
400                 error "(9) Fail to repair crashed linkEA: $repaired"
401
402         mount_client $MOUNT || error "(10) Fail to start client!"
403
404         #define OBD_FAIL_FID_LOOKUP     0x1505
405         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1505
406         ls $DIR/$tdir/ > /dev/null || error "(11) no FID-in-dirent."
407
408         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
409 }
410 run_test 4 "FID-in-dirent can be rebuilt after MDT file-level backup/restore"
411
412 test_5()
413 {
414         [ $(facet_fstype $SINGLEMDS) != ldiskfs ] &&
415                 skip "OI Scrub not implemented for ZFS" && return
416
417         lfsck_prep 1 1 1
418         mds_backup_restore $SINGLEMDS 1 || error "(1) Fail to backup/restore!"
419         echo "start $SINGLEMDS with disabling OI scrub"
420         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_NOSCRUB > /dev/null ||
421                 error "(2) Fail to start MDS!"
422
423         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
424         [ "$STATUS" == "init" ] ||
425                 error "(3) Expect 'init', but got '$STATUS'"
426
427         #define OBD_FAIL_LFSCK_DELAY2           0x1601
428         do_facet $SINGLEMDS $LCTL set_param fail_val=1
429         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1601
430         $START_NAMESPACE || error "(4) Fail to start LFSCK for namespace!"
431
432         sleep 5
433         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
434         [ "$STATUS" == "scanning-phase1" ] ||
435                 error "(5) Expect 'scanning-phase1', but got '$STATUS'"
436
437         local FLAGS=$($SHOW_NAMESPACE | awk '/^flags/ { print $2 }')
438         [ "$FLAGS" == "inconsistent,upgrade" ] ||
439                 error "(6) Expect 'inconsistent,upgrade', but got '$FLAGS'"
440
441         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
442         do_facet $SINGLEMDS $LCTL set_param fail_val=0
443         sleep 3
444         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
445         [ "$STATUS" == "completed" ] ||
446                 error "(7) Expect 'completed', but got '$STATUS'"
447
448         FLAGS=$($SHOW_NAMESPACE | awk '/^flags/ { print $2 }')
449         [ -z "$FLAGS" ] || error "(8) Expect empty flags, but got '$FLAGS'"
450
451         local repaired=$($SHOW_NAMESPACE |
452                          awk '/^updated_phase1/ { print $2 }')
453         [ $repaired -ge 2 ] ||
454                 error "(9) Fail to repair crashed linkEA: $repaired"
455
456         mount_client $MOUNT || error "(10) Fail to start client!"
457
458         #define OBD_FAIL_FID_LOOKUP     0x1505
459         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1505
460         stat $DIR/$tdir/dummy > /dev/null || error "(11) no FID-in-LMA."
461
462         ls $DIR/$tdir/ > /dev/null || error "(12) no FID-in-dirent."
463
464         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
465         local dummyfid=$($LFS path2fid $DIR/$tdir/dummy)
466         local dummyname=$($LFS fid2path $DIR $dummyfid)
467         [ "$dummyname" == "$DIR/$tdir/dummy" ] ||
468                 error "(13) Fail to generate linkEA: $dummyfid $dummyname"
469 }
470 run_test 5 "LFSCK can handle IFIG object upgrading"
471
472 test_6a() {
473         lfsck_prep 10 10
474         echo "start $SINGLEMDS"
475         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
476                 error "(1) Fail to start MDS!"
477
478         #define OBD_FAIL_LFSCK_DELAY1           0x1600
479         do_facet $SINGLEMDS $LCTL set_param fail_val=1
480         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1600
481         $START_NAMESPACE || error "(2) Fail to start LFSCK for namespace!"
482
483         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
484         [ "$STATUS" == "scanning-phase1" ] ||
485                 error "(3) Expect 'scanning-phase1', but got '$STATUS'"
486
487         # Sleep 3 sec to guarantee at least one object processed by LFSCK
488         sleep 3
489         # Fail the LFSCK to guarantee there is at least one checkpoint
490         #define OBD_FAIL_LFSCK_FATAL1           0x1608
491         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x80001608
492         sleep 3
493         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
494         [ "$STATUS" == "failed" ] ||
495                 error "(4) Expect 'failed', but got '$STATUS'"
496
497         local POSITION0=$($SHOW_NAMESPACE |
498                           awk '/^last_checkpoint_position/ { print $2 }' |
499                           tr -d ',')
500
501         #define OBD_FAIL_LFSCK_DELAY1           0x1600
502         do_facet $SINGLEMDS $LCTL set_param fail_val=1
503         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1600
504         $START_NAMESPACE || error "(5) Fail to start LFSCK for namespace!"
505
506         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
507         [ "$STATUS" == "scanning-phase1" ] ||
508                 error "(6) Expect 'scanning-phase1', but got '$STATUS'"
509
510         local POSITION1=$($SHOW_NAMESPACE |
511                           awk '/^latest_start_position/ { print $2 }' |
512                           tr -d ',')
513         [ $POSITION0 -lt $POSITION1 ] ||
514                 error "(7) Expect larger than: $POSITION0, but got $POSITION1"
515
516         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
517         do_facet $SINGLEMDS $LCTL set_param fail_val=0
518         sleep 3
519         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
520         [ "$STATUS" == "completed" ] ||
521                 error "(8) Expect 'completed', but got '$STATUS'"
522 }
523 run_test 6a "LFSCK resumes from last checkpoint (1)"
524
525 test_6b() {
526         lfsck_prep 10 10
527         echo "start $SINGLEMDS"
528         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
529                 error "(1) Fail to start MDS!"
530
531         #define OBD_FAIL_LFSCK_DELAY2           0x1601
532         do_facet $SINGLEMDS $LCTL set_param fail_val=1
533         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1601
534         $START_NAMESPACE || error "(2) Fail to start LFSCK for namespace!"
535
536         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
537         [ "$STATUS" == "scanning-phase1" ] ||
538                 error "(3) Expect 'scanning-phase1', but got '$STATUS'"
539
540         # Sleep 3 sec to guarantee at least one object processed by LFSCK
541         sleep 3
542         # Fail the LFSCK to guarantee there is at least one checkpoint
543         #define OBD_FAIL_LFSCK_FATAL2           0x1609
544         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x80001609
545         sleep 3
546         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
547         [ "$STATUS" == "failed" ] ||
548                 error "(4) Expect 'failed', but got '$STATUS'"
549
550         local POSITION0=$($SHOW_NAMESPACE |
551                           awk '/^last_checkpoint_position/ { print $4 }')
552
553         #define OBD_FAIL_LFSCK_DELAY2           0x1601
554         do_facet $SINGLEMDS $LCTL set_param fail_val=1
555         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1601
556         $START_NAMESPACE || error "(5) Fail to start LFSCK for namespace!"
557
558         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
559         [ "$STATUS" == "scanning-phase1" ] ||
560                 error "(6) Expect 'scanning-phase1', but got '$STATUS'"
561
562         local POSITION1=$($SHOW_NAMESPACE |
563                           awk '/^latest_start_position/ { print $4 }')
564         if [ $POSITION0 -gt $POSITION1 ]; then
565                 [ $POSITION1 -eq 0 -a $POSITION0 -eq $((POSITION1 + 1)) ] ||
566                 error "(7) Expect larger than: $POSITION0, but got $POSITION1"
567         fi
568
569         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
570         do_facet $SINGLEMDS $LCTL set_param fail_val=0
571         sleep 3
572         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
573         [ "$STATUS" == "completed" ] ||
574                 error "(8) Expect 'completed', but got '$STATUS'"
575 }
576 run_test 6b "LFSCK resumes from last checkpoint (2)"
577
578 test_7a()
579 {
580         lfsck_prep 10 10
581         echo "start $SINGLEMDS"
582         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
583                 error "(1) Fail to start MDS!"
584
585         #define OBD_FAIL_LFSCK_DELAY2           0x1601
586         do_facet $SINGLEMDS $LCTL set_param fail_val=1
587         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1601
588         $START_NAMESPACE || error "(2) Fail to start LFSCK for namespace!"
589
590         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
591         [ "$STATUS" == "scanning-phase1" ] ||
592                 error "(3) Expect 'scanning-phase1', but got '$STATUS'"
593
594         # Sleep 3 sec to guarantee at least one object processed by LFSCK
595         sleep 3
596         echo "stop $SINGLEMDS"
597         stop $SINGLEMDS > /dev/null || error "(4) Fail to stop MDS!"
598
599         echo "start $SINGLEMDS"
600         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
601                 error "(5) Fail to start MDS!"
602
603         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
604         [ "$STATUS" == "scanning-phase1" ] ||
605                 error "(6) Expect 'scanning-phase1', but got '$STATUS'"
606
607         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
608         do_facet $SINGLEMDS $LCTL set_param fail_val=0
609         sleep 3
610         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
611         [ "$STATUS" == "completed" ] ||
612                 error "(7) Expect 'completed', but got '$STATUS'"
613 }
614 run_test 7a "non-stopped LFSCK should auto restarts after MDS remount (1)"
615
616 test_7b()
617 {
618         lfsck_prep 2 2
619         echo "start $SINGLEMDS"
620         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
621                 error "(1) Fail to start MDS!"
622
623         mount_client $MOUNT || error "(2) Fail to start client!"
624
625         #define OBD_FAIL_LFSCK_LINKEA_MORE      0x1604
626         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1604
627         for ((i = 0; i < 20; i++)); do
628                 touch $DIR/$tdir/dummy${i}
629         done
630
631         #define OBD_FAIL_LFSCK_DELAY3           0x1602
632         do_facet $SINGLEMDS $LCTL set_param fail_val=1
633         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1602
634         $START_NAMESPACE || error "(3) Fail to start LFSCK for namespace!"
635
636         sleep 3
637         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
638         [ "$STATUS" == "scanning-phase2" ] ||
639                 error "(4) Expect 'scanning-phase2', but got '$STATUS'"
640
641         echo "stop $SINGLEMDS"
642         stop $SINGLEMDS > /dev/null || error "(5) Fail to stop MDS!"
643
644         echo "start $SINGLEMDS"
645         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
646                 error "(6) Fail to start MDS!"
647
648         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
649         [ "$STATUS" == "scanning-phase2" ] ||
650                 error "(7) Expect 'scanning-phase2', but got '$STATUS'"
651
652         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
653         do_facet $SINGLEMDS $LCTL set_param fail_val=0
654         sleep 3
655         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
656         [ "$STATUS" == "completed" ] ||
657                 error "(8) Expect 'completed', but got '$STATUS'"
658 }
659 run_test 7b "non-stopped LFSCK should auto restarts after MDS remount (2)"
660
661 test_8()
662 {
663         lfsck_prep 20 20
664         echo "start $SINGLEMDS"
665         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
666                 error "(1) Fail to start MDS!"
667
668         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
669         [ "$STATUS" == "init" ] ||
670                 error "(2) Expect 'init', but got '$STATUS'"
671
672         mount_client $MOUNT || error "(3) Fail to start client!"
673
674         #define OBD_FAIL_LFSCK_LINKEA_CRASH     0x1603
675         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1603
676         mkdir $DIR/$tdir/crashed
677
678         #define OBD_FAIL_LFSCK_LINKEA_MORE      0x1604
679         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1604
680         for ((i = 0; i < 5; i++)); do
681                 touch $DIR/$tdir/dummy${i}
682         done
683
684         #define OBD_FAIL_LFSCK_DELAY2           0x1601
685         do_facet $SINGLEMDS $LCTL set_param fail_val=2
686         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1601
687         $START_NAMESPACE || error "(4) Fail to start LFSCK for namespace!"
688
689         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
690         [ "$STATUS" == "scanning-phase1" ] ||
691                 error "(5) Expect 'scanning-phase1', but got '$STATUS'"
692
693         $STOP_LFSCK || error "(6) Fail to stop LFSCK!"
694
695         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
696         [ "$STATUS" == "stopped" ] ||
697                 error "(7) Expect 'stopped', but got '$STATUS'"
698
699         $START_NAMESPACE || error "(8) Fail to start LFSCK for namespace!"
700
701         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
702         [ "$STATUS" == "scanning-phase1" ] ||
703                 error "(9) Expect 'scanning-phase1', but got '$STATUS'"
704
705         #define OBD_FAIL_LFSCK_FATAL2           0x1609
706         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x80001609
707         sleep 3
708         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
709         [ "$STATUS" == "failed" ] ||
710                 error "(10) Expect 'failed', but got '$STATUS'"
711
712         #define OBD_FAIL_LFSCK_DELAY1           0x1600
713         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1600
714         $START_NAMESPACE || error "(11) Fail to start LFSCK for namespace!"
715
716         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
717         [ "$STATUS" == "scanning-phase1" ] ||
718                 error "(12) Expect 'scanning-phase1', but got '$STATUS'"
719
720         #define OBD_FAIL_LFSCK_CRASH            0x160a
721         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x160a
722         sleep 5
723
724         echo "stop $SINGLEMDS"
725         stop $SINGLEMDS > /dev/null || error "(13) Fail to stop MDS!"
726
727         #define OBD_FAIL_LFSCK_NO_AUTO          0x160b
728         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x160b
729
730         echo "start $SINGLEMDS"
731         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
732                 error "(14) Fail to start MDS!"
733
734         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
735         [ "$STATUS" == "crashed" ] ||
736                 error "(15) Expect 'crashed', but got '$STATUS'"
737
738         #define OBD_FAIL_LFSCK_DELAY2           0x1601
739         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1601
740         $START_NAMESPACE || error "(16) Fail to start LFSCK for namespace!"
741
742         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
743         [ "$STATUS" == "scanning-phase1" ] ||
744                 error "(17) Expect 'scanning-phase1', but got '$STATUS'"
745
746         echo "stop $SINGLEMDS"
747         stop $SINGLEMDS > /dev/null || error "(18) Fail to stop MDS!"
748
749         #define OBD_FAIL_LFSCK_NO_AUTO          0x160b
750         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x160b
751
752         echo "start $SINGLEMDS"
753         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
754                 error "(19) Fail to start MDS!"
755
756         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
757         [ "$STATUS" == "paused" ] ||
758                 error "(20) Expect 'paused', but got '$STATUS'"
759
760         #define OBD_FAIL_LFSCK_DELAY3           0x1602
761         do_facet $SINGLEMDS $LCTL set_param fail_val=2
762         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1602
763
764         $START_NAMESPACE || error "(21) Fail to start LFSCK for namespace!"
765         sleep 2
766         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
767         [ "$STATUS" == "scanning-phase2" ] ||
768                 error "(22) Expect 'scanning-phase2', but got '$STATUS'"
769
770         local FLAGS=$($SHOW_NAMESPACE | awk '/^flags/ { print $2 }')
771         [ "$FLAGS" == "scanned-once,inconsistent" ] ||
772                 error "(23) Expect 'scanned-once,inconsistent',but got '$FLAGS'"
773
774         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
775         do_facet $SINGLEMDS $LCTL set_param fail_val=0
776         sleep 2
777         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
778         [ "$STATUS" == "completed" ] ||
779                 error "(24) Expect 'completed', but got '$STATUS'"
780
781         FLAGS=$($SHOW_NAMESPACE | awk '/^flags/ { print $2 }')
782         [ -z "$FLAGS" ] || error "(25) Expect empty flags, but got '$FLAGS'"
783
784 }
785 run_test 8 "LFSCK state machine"
786
787 test_9a() {
788         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
789                 skip "Testing on UP system, the speed may be inaccurate."
790                 return 0
791         fi
792
793         lfsck_prep 70 70
794         echo "start $SINGLEMDS"
795         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
796                 error "(1) Fail to start MDS!"
797
798         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
799         [ "$STATUS" == "init" ] ||
800                 error "(2) Expect 'init', but got '$STATUS'"
801
802         local BASE_SPEED1=100
803         local RUN_TIME1=10
804         $START_NAMESPACE -s $BASE_SPEED1 || error "(3) Fail to start LFSCK!"
805
806         sleep $RUN_TIME1
807         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
808         [ "$STATUS" == "scanning-phase1" ] ||
809                 error "(3) Expect 'scanning-phase1', but got '$STATUS'"
810
811         local SPEED=$($SHOW_NAMESPACE |
812                       awk '/^average_speed_phase1/ { print $2 }')
813
814         # There may be time error, normally it should be less than 2 seconds.
815         # We allow another 20% schedule error.
816         local TIME_DIFF=2
817         # MAX_MARGIN = 1.2 = 12 / 10
818         local MAX_SPEED=$((BASE_SPEED1 * (RUN_TIME1 + TIME_DIFF) / \
819                            RUN_TIME1 * 12 / 10))
820         [ $SPEED -lt $MAX_SPEED ] ||
821                 error "(4) Got speed $SPEED, expected less than $MAX_SPEED"
822
823         # adjust speed limit
824         local BASE_SPEED2=300
825         local RUN_TIME2=10
826         do_facet $SINGLEMDS \
827                 $LCTL set_param -n mdd.${MDT_DEV}.lfsck_speed_limit $BASE_SPEED2
828         sleep $RUN_TIME2
829
830         SPEED=$($SHOW_NAMESPACE | awk '/^average_speed_phase1/ { print $2 }')
831         # MIN_MARGIN = 0.8 = 8 / 10
832         local MIN_SPEED=$(((BASE_SPEED1 * (RUN_TIME1 - TIME_DIFF) + \
833                             BASE_SPEED2 * (RUN_TIME2 - TIME_DIFF)) / \
834                            (RUN_TIME1 + RUN_TIME2) * 8 / 10))
835         [ $SPEED -gt $MIN_SPEED ] ||
836                 error "(5) Got speed $SPEED, expected more than $MIN_SPEED"
837
838         # MAX_MARGIN = 1.2 = 12 / 10
839         MAX_SPEED=$(((BASE_SPEED1 * (RUN_TIME1 + TIME_DIFF) + \
840                       BASE_SPEED2 * (RUN_TIME2 + TIME_DIFF)) / \
841                      (RUN_TIME1 + RUN_TIME2) * 12 / 10))
842         [ $SPEED -lt $MAX_SPEED ] ||
843                 error "(6) Got speed $SPEED, expected less than $MAX_SPEED"
844
845         do_facet $SINGLEMDS \
846                 $LCTL set_param -n mdd.${MDT_DEV}.lfsck_speed_limit 0
847
848         wait_update_facet $SINGLEMDS \
849             "$LCTL get_param -n mdd.${MDT_DEV}.lfsck_namespace|\
850             awk '/^status/ { print \\\$2 }'" "completed" 30 ||
851                 error "(7) Failed to get expected 'completed'"
852 }
853 run_test 9a "LFSCK speed control (1)"
854
855 test_9b() {
856         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
857                 skip "Testing on UP system, the speed may be inaccurate."
858                 return 0
859         fi
860
861         lfsck_prep 0 0
862         echo "start $SINGLEMDS"
863         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
864                 error "(1) Fail to start MDS!"
865
866         mount_client $MOUNT || error "(2) Fail to start client!"
867
868         echo "Another preparing... 50 * 50 files (with error) will be created."
869         #define OBD_FAIL_LFSCK_LINKEA_MORE      0x1604
870         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1604
871         for ((i = 0; i < 50; i++)); do
872                 mkdir -p $DIR/$tdir/d${i}
873                 touch $DIR/$tdir/f${i}
874                 for ((j = 0; j < 50; j++)); do
875                         touch $DIR/$tdir/d${i}/f${j}
876                 done
877         done
878
879         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
880         [ "$STATUS" == "init" ] ||
881                 error "(3) Expect 'init', but got '$STATUS'"
882
883         #define OBD_FAIL_LFSCK_NO_DOUBLESCAN    0x160c
884         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x160c
885         $START_NAMESPACE || error "(4) Fail to start LFSCK!"
886
887         sleep 10
888         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
889         [ "$STATUS" == "stopped" ] ||
890                 error "(5) Expect 'stopped', but got '$STATUS'"
891
892         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
893
894         local BASE_SPEED1=50
895         local RUN_TIME1=10
896         $START_NAMESPACE -s $BASE_SPEED1 || error "(6) Fail to start LFSCK!"
897
898         sleep $RUN_TIME1
899         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
900         [ "$STATUS" == "scanning-phase2" ] ||
901                 error "(7) Expect 'scanning-phase2', but got '$STATUS'"
902
903         local SPEED=$($SHOW_NAMESPACE |
904                       awk '/^average_speed_phase2/ { print $2 }')
905         # There may be time error, normally it should be less than 2 seconds.
906         # We allow another 20% schedule error.
907         local TIME_DIFF=2
908         # MAX_MARGIN = 1.2 = 12 / 10
909         local MAX_SPEED=$((BASE_SPEED1 * (RUN_TIME1 + TIME_DIFF) / \
910                           RUN_TIME1 * 12 / 10))
911         [ $SPEED -lt $MAX_SPEED ] ||
912                 error "(8) Got speed $SPEED, expected less than $MAX_SPEED"
913
914         # adjust speed limit
915         local BASE_SPEED2=150
916         local RUN_TIME2=10
917         do_facet $SINGLEMDS \
918                 $LCTL set_param -n mdd.${MDT_DEV}.lfsck_speed_limit $BASE_SPEED2
919         sleep $RUN_TIME2
920
921         SPEED=$($SHOW_NAMESPACE | awk '/^average_speed_phase2/ { print $2 }')
922         # MIN_MARGIN = 0.8 = 8 / 10
923         local MIN_SPEED=$(((BASE_SPEED1 * (RUN_TIME1 - TIME_DIFF) + \
924                             BASE_SPEED2 * (RUN_TIME2 - TIME_DIFF)) / \
925                            (RUN_TIME1 + RUN_TIME2) * 8 / 10))
926         [ $SPEED -gt $MIN_SPEED ] ||
927                 error "(9) Got speed $SPEED, expected more than $MIN_SPEED"
928
929         # MAX_MARGIN = 1.2 = 12 / 10
930         MAX_SPEED=$(((BASE_SPEED1 * (RUN_TIME1 + TIME_DIFF) + \
931                       BASE_SPEED2 * (RUN_TIME2 + TIME_DIFF)) / \
932                      (RUN_TIME1 + RUN_TIME2) * 12 / 10))
933         [ $SPEED -lt $MAX_SPEED ] ||
934                 error "(10) Got speed $SPEED, expected less than $MAX_SPEED"
935
936         do_facet $SINGLEMDS \
937                 $LCTL set_param -n mdd.${MDT_DEV}.lfsck_speed_limit 0
938         sleep 5
939         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
940         [ "$STATUS" == "completed" ] ||
941                 error "(11) Expect 'completed', but got '$STATUS'"
942 }
943 run_test 9b "LFSCK speed control (2)"
944
945 test_10()
946 {
947         [ $(facet_fstype $SINGLEMDS) != ldiskfs ] &&
948                 skip "lookup(..)/linkea on ZFS issue" && return
949
950         lfsck_prep 1 1
951         echo "start $SINGLEMDS"
952         start $SINGLEMDS $MDT_DEVNAME $MOUNT_OPTS_SCRUB > /dev/null ||
953                 error "(1) Fail to start MDS!"
954
955         mount_client $MOUNT || error "(2) Fail to start client!"
956
957         #define OBD_FAIL_LFSCK_LINKEA_CRASH     0x1603
958         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1603
959         for ((i = 0; i < 1000; i = $((i+2)))); do
960                 mkdir -p $DIR/$tdir/d${i}
961                 touch $DIR/$tdir/f${i}
962                 for ((j = 0; j < 5; j++)); do
963                         touch $DIR/$tdir/d${i}/f${j}
964                 done
965         done
966
967         #define OBD_FAIL_LFSCK_LINKEA_MORE      0x1604
968         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1604
969         for ((i = 1; i < 1000; i = $((i+2)))); do
970                 mkdir -p $DIR/$tdir/d${i}
971                 touch $DIR/$tdir/f${i}
972                 for ((j = 0; j < 5; j++)); do
973                         touch $DIR/$tdir/d${i}/f${j}
974                 done
975         done
976
977         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
978         ln $DIR/$tdir/f200 $DIR/$tdir/d200/dummy
979
980         umount_client $MOUNT
981         mount_client $MOUNT || error "(3) Fail to start client!"
982
983         local STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
984         [ "$STATUS" == "init" ] ||
985                 error "(4) Expect 'init', but got '$STATUS'"
986
987         $START_NAMESPACE -s 100 || error "(5) Fail to start LFSCK!"
988
989         sleep 10
990         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
991         [ "$STATUS" == "scanning-phase1" ] ||
992                 error "(6) Expect 'scanning-phase1', but got '$STATUS'"
993
994         ls -ailR $MOUNT > /dev/null || error "(7) Fail to ls!"
995
996         touch $DIR/$tdir/d198/a0 || error "(8) Fail to touch!"
997
998         mkdir $DIR/$tdir/d199/a1 || error "(9) Fail to mkdir!"
999
1000         unlink $DIR/$tdir/f200 || error "(10) Fail to unlink!"
1001
1002         rm -rf $DIR/$tdir/d201 || error "(11) Fail to rmdir!"
1003
1004         mv $DIR/$tdir/f202 $DIR/$tdir/d203/ || error "(12) Fail to rename!"
1005
1006         ln $DIR/$tdir/f204 $DIR/$tdir/d205/a3 || error "(13) Fail to hardlink!"
1007
1008         ln -s $DIR/$tdir/d206 $DIR/$tdir/d207/a4 ||
1009                 error "(14) Fail to softlink!"
1010
1011         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
1012         [ "$STATUS" == "scanning-phase1" ] ||
1013                 error "(15) Expect 'scanning-phase1', but got '$STATUS'"
1014
1015         do_facet $SINGLEMDS \
1016                 $LCTL set_param -n mdd.${MDT_DEV}.lfsck_speed_limit 0
1017         umount_client $MOUNT
1018         sleep 10
1019         STATUS=$($SHOW_NAMESPACE | awk '/^status/ { print $2 }')
1020         [ "$STATUS" == "completed" ] ||
1021                 error "(16) Expect 'completed', but got '$STATUS'"
1022 }
1023 run_test 10 "System is available during LFSCK scanning"
1024
1025 # remove LAST_ID
1026 ost_remove_lastid() {
1027         local ost=$1
1028         local idx=$2
1029         local rcmd="do_facet ost${ost}"
1030
1031         echo "remove LAST_ID on ost${ost}: idx=${idx}"
1032
1033         # step 1: local mount
1034         mount_fstype ost${ost} || return 1
1035         # step 2: remove the specified LAST_ID
1036         ${rcmd} rm -fv $(facet_mntpt ost${ost})/O/${idx}/{LAST_ID,d0/0}
1037         # step 3: umount
1038         unmount_fstype ost${ost} || return 2
1039 }
1040
1041 test_11a() {
1042         echo "stopall"
1043         stopall > /dev/null
1044         echo "formatall"
1045         formatall > /dev/null
1046         echo "setupall"
1047         setupall > /dev/null
1048
1049         mkdir -p $DIR/$tdir
1050         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
1051         createmany -o $DIR/$tdir/f 64
1052
1053         echo "stopall"
1054         stopall > /dev/null
1055
1056         ost_remove_lastid 1 0 || error "(1) Fail to remove LAST_ID"
1057
1058         echo "start ost1"
1059         start ost1 $(ostdevname 1) $MOUNT_OPTS_NOSCRUB > /dev/null ||
1060                 error "(2) Fail to start ost1"
1061
1062         local STATUS=$($SHOW_LAYOUT_ON_OST | awk '/^status/ { print $2 }')
1063         [ "$STATUS" == "init" ] ||
1064                 error "(3) Expect 'init', but got '$STATUS'"
1065
1066         #define OBD_FAIL_LFSCK_DELAY4           0x160e
1067         do_facet ost1 $LCTL set_param fail_val=3
1068         do_facet ost1 $LCTL set_param fail_loc=0x160e
1069
1070         echo "trigger LFSCK for layout on ost1 to rebuild the LAST_ID(s)"
1071         $START_LAYOUT_ON_OST || error "(4) Fail to start LFSCK on OST!"
1072
1073         wait_update_facet ost1 "$LCTL get_param -n \
1074                 obdfilter.${OST_DEV}.lfsck_layout |
1075                 awk '/^flags/ { print \\\$2 }'" "crashed_lastid" 60 || {
1076                 $SHOW_LAYOUT_ON_OST
1077                 return 5
1078         }
1079
1080         do_facet ost1 $LCTL set_param fail_val=0
1081         do_facet ost1 $LCTL set_param fail_loc=0
1082
1083         wait_update_facet ost1 "$LCTL get_param -n \
1084                 obdfilter.${OST_DEV}.lfsck_layout |
1085                 awk '/^status/ { print \\\$2 }'" "completed" 3 || {
1086                 $SHOW_LAYOUT_ON_OST
1087                 return 6
1088         }
1089
1090         echo "the LAST_ID(s) should have been rebuilt"
1091         FLAGS=$($SHOW_LAYOUT_ON_OST | awk '/^flags/ { print $2 }')
1092         [ -z "$FLAGS" ] || error "(7) Expect empty flags, but got '$FLAGS'"
1093 }
1094 run_test 11a "LFSCK can rebuild lost last_id"
1095
1096 test_11b() {
1097         echo "stopall"
1098         stopall > /dev/null
1099         echo "formatall"
1100         formatall > /dev/null
1101         echo "setupall"
1102         setupall > /dev/null
1103
1104         mkdir -p $DIR/$tdir
1105         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
1106
1107         echo "set fail_loc=0x160d to skip the updating LAST_ID on-disk"
1108         #define OBD_FAIL_LFSCK_SKIP_LASTID      0x160d
1109         do_facet ost1 $LCTL set_param fail_loc=0x160d
1110         createmany -o $DIR/$tdir/f 64
1111         local lastid1=$(do_facet ost1 "lctl get_param -n \
1112                 obdfilter.${ost1_svc}.last_id" | grep 0x100000000 |
1113                 awk -F: '{ print $2 }')
1114
1115         umount_client $MOUNT
1116         echo "stop ost1"
1117         stop ost1 || error "(1) Fail to stop ost1"
1118
1119         #define OBD_FAIL_OST_ENOSPC              0x215
1120         do_facet ost1 $LCTL set_param fail_loc=0x215
1121
1122         echo "start ost1"
1123         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS ||
1124                 error "(2) Fail to start ost1"
1125
1126         local STATUS=$($SHOW_LAYOUT_ON_OST | awk '/^status/ { print $2 }')
1127         [ "$STATUS" == "init" ] ||
1128                 error "(3) Expect 'init', but got '$STATUS'"
1129
1130         for ((i = 0; i < 60; i++)); do
1131                 lastid2=$(do_facet ost1 "lctl get_param -n \
1132                         obdfilter.${ost1_svc}.last_id" | grep 0x100000000 |
1133                         awk -F: '{ print $2 }')
1134                 [ ! -z $lastid2 ] && break;
1135                 sleep 1
1136         done
1137
1138         echo "the on-disk LAST_ID should be smaller than the expected one"
1139         [ $lastid1 -gt $lastid2 ] ||
1140                 error "(4) expect lastid1 [ $lastid1 ] > lastid2 [ $lastid2 ]"
1141
1142         echo "trigger LFSCK for layout on ost1 to rebuild the on-disk LAST_ID"
1143         $START_LAYOUT_ON_OST || error "(5) Fail to start LFSCK on OST!"
1144
1145         wait_update_facet ost1 "$LCTL get_param -n \
1146                 obdfilter.${OST_DEV}.lfsck_layout |
1147                 awk '/^status/ { print \\\$2 }'" "completed" 3 || {
1148                 $SHOW_LAYOUT_ON_OST
1149                 return 6
1150         }
1151
1152         echo "stop ost1"
1153         stop ost1 || error "(7) Fail to stop ost1"
1154
1155         echo "start ost1"
1156         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS ||
1157                 error "(8) Fail to start ost1"
1158
1159         echo "the on-disk LAST_ID should have been rebuilt"
1160         wait_update_facet ost1 "$LCTL get_param -n \
1161                 obdfilter.${ost1_svc}.last_id | grep 0x100000000 |
1162                 awk -F: '{ print \\\$2 }'" "$lastid1" 60 || {
1163                 $LCTL get_param -n obdfilter.${ost1_svc}.last_id
1164                 error "(9) expect lastid1 0x100000000:$lastid1"
1165         }
1166
1167         do_facet ost1 $LCTL set_param fail_loc=0
1168 }
1169 run_test 11b "LFSCK can rebuild crashed last_id"
1170
1171 test_12() {
1172         [ $MDSCOUNT -lt 2 ] &&
1173                 skip "We need at least 2 MDSes for test_12" && exit 0
1174
1175         echo "stopall"
1176         stopall > /dev/null
1177         echo "formatall"
1178         formatall > /dev/null
1179         echo "setupall"
1180         setupall > /dev/null
1181
1182         mkdir -p $DIR/$tdir
1183
1184         echo "All the LFSCK targets should be in 'init' status."
1185         for k in $(seq $MDSCOUNT); do
1186                 local STATUS=$(do_facet mds${k} $LCTL get_param -n \
1187                                 mdd.$(facet_svc mds${k}).lfsck_layout |
1188                                 awk '/^status/ { print $2 }')
1189                 [ "$STATUS" == "init" ] ||
1190                         error "(1) MDS${k} Expect 'init', but got '$STATUS'"
1191
1192                 $LFS mkdir -i $((k - 1)) $DIR/$tdir/${k}
1193                 createmany -o $DIR/$tdir/${k}/f 100
1194         done
1195
1196         echo "Start namespace LFSCK on all targets by single command (-s 1)."
1197         do_facet mds1 $LCTL lfsck_start -M ${FSNAME}-MDT0000 -t namespace -A \
1198                 -s 1 || error "(2) Fail to start LFSCK on all devices!"
1199
1200         echo "All the LFSCK targets should be in 'scanning-phase1' status."
1201         for k in $(seq $MDSCOUNT); do
1202                 local STATUS=$(do_facet mds${k} $LCTL get_param -n \
1203                                 mdd.$(facet_svc mds${k}).lfsck_namespace |
1204                                 awk '/^status/ { print $2 }')
1205                 [ "$STATUS" == "scanning-phase1" ] ||
1206                 error "(3) MDS${k} Expect 'scanning-phase1', but got '$STATUS'"
1207         done
1208
1209         echo "Stop namespace LFSCK on all targets by single lctl command."
1210         do_facet mds1 $LCTL lfsck_stop -M ${FSNAME}-MDT0000 -A ||
1211                 error "(4) Fail to stop LFSCK on all devices!"
1212
1213         echo "All the LFSCK targets should be in 'stopped' status."
1214         for k in $(seq $MDSCOUNT); do
1215                 local STATUS=$(do_facet mds${k} $LCTL get_param -n \
1216                                 mdd.$(facet_svc mds${k}).lfsck_namespace |
1217                                 awk '/^status/ { print $2 }')
1218                 [ "$STATUS" == "stopped" ] ||
1219                         error "(5) MDS${k} Expect 'stopped', but got '$STATUS'"
1220         done
1221
1222         echo "Re-start namespace LFSCK on all targets by single command (-s 0)."
1223         do_facet mds1 $LCTL lfsck_start -M ${FSNAME}-MDT0000 -t namespace -A \
1224                 -s 0 -r || error "(6) Fail to start LFSCK on all devices!"
1225
1226         echo "All the LFSCK targets should be in 'completed' status."
1227         for k in $(seq $MDSCOUNT); do
1228                 wait_update_facet mds${k} "$LCTL get_param -n \
1229                         mdd.$(facet_svc mds${k}).lfsck_namespace |
1230                         awk '/^status/ { print \\\$2 }'" "completed" 8 ||
1231                         error "(7) MDS${k} is not the expected 'completed'"
1232         done
1233
1234         echo "Start layout LFSCK on all targets by single command (-s 1)."
1235         do_facet mds1 $LCTL lfsck_start -M ${FSNAME}-MDT0000 -t layout -A \
1236                 -s 1 || error "(8) Fail to start LFSCK on all devices!"
1237
1238         echo "All the LFSCK targets should be in 'scanning-phase1' status."
1239         for k in $(seq $MDSCOUNT); do
1240                 local STATUS=$(do_facet mds${k} $LCTL get_param -n \
1241                                 mdd.$(facet_svc mds${k}).lfsck_layout |
1242                                 awk '/^status/ { print $2 }')
1243                 [ "$STATUS" == "scanning-phase1" ] ||
1244                 error "(9) MDS${k} Expect 'scanning-phase1', but got '$STATUS'"
1245         done
1246
1247         echo "Stop layout LFSCK on all targets by single lctl command."
1248         do_facet mds1 $LCTL lfsck_stop -M ${FSNAME}-MDT0000 -A ||
1249                 error "(10) Fail to stop LFSCK on all devices!"
1250
1251         echo "All the LFSCK targets should be in 'stopped' status."
1252         for k in $(seq $MDSCOUNT); do
1253                 local STATUS=$(do_facet mds${k} $LCTL get_param -n \
1254                                 mdd.$(facet_svc mds${k}).lfsck_layout |
1255                                 awk '/^status/ { print $2 }')
1256                 [ "$STATUS" == "stopped" ] ||
1257                         error "(11) MDS${k} Expect 'stopped', but got '$STATUS'"
1258         done
1259
1260         for k in $(seq $OSTCOUNT); do
1261                 local STATUS=$(do_facet ost${k} $LCTL get_param -n \
1262                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1263                                 awk '/^status/ { print $2 }')
1264                 [ "$STATUS" == "stopped" ] ||
1265                         error "(12) OST${k} Expect 'stopped', but got '$STATUS'"
1266         done
1267
1268         echo "Re-start layout LFSCK on all targets by single command (-s 0)."
1269         do_facet mds1 $LCTL lfsck_start -M ${FSNAME}-MDT0000 -t layout -A \
1270                 -s 0 -r || error "(13) Fail to start LFSCK on all devices!"
1271
1272         echo "All the LFSCK targets should be in 'completed' status."
1273         for k in $(seq $MDSCOUNT); do
1274                 # The LFSCK status query internal is 30 seconds. For the case
1275                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1276                 # time to guarantee the status sync up.
1277                 wait_update_facet mds${k} "$LCTL get_param -n \
1278                         mdd.$(facet_svc mds${k}).lfsck_layout |
1279                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1280                         error "(14) MDS${k} is not the expected 'completed'"
1281         done
1282 }
1283 run_test 12 "single command to trigger LFSCK on all devices"
1284
1285 test_13() {
1286         echo "#####"
1287         echo "The lmm_oi in layout EA should be consistent with the MDT-object"
1288         echo "FID; otherwise, the LFSCK should re-generate the lmm_oi from the"
1289         echo "MDT-object FID."
1290         echo "#####"
1291
1292         echo "stopall"
1293         stopall > /dev/null
1294         echo "formatall"
1295         formatall > /dev/null
1296         echo "setupall"
1297         setupall > /dev/null
1298
1299         mkdir -p $DIR/$tdir
1300
1301         echo "Inject failure stub to simulate bad lmm_oi"
1302         #define OBD_FAIL_LFSCK_BAD_LMMOI        0x160f
1303         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x160f
1304         createmany -o $DIR/$tdir/f 32
1305         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
1306
1307         echo "stopall to cleanup object cache"
1308         stopall > /dev/null
1309         echo "setupall"
1310         setupall > /dev/null
1311
1312         echo "Trigger layout LFSCK to find out the bad lmm_oi and fix them"
1313         $START_LAYOUT || error "(1) Fail to start LFSCK for layout!"
1314
1315         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1316                 mdd.${MDT_DEV}.lfsck_layout |
1317                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 2
1318
1319         local repaired=$($SHOW_LAYOUT |
1320                          awk '/^repaired_others/ { print $2 }')
1321         [ $repaired -eq 32 ] ||
1322                 error "(3) Fail to repair crashed lmm_oi: $repaired"
1323 }
1324 run_test 13 "LFSCK can repair crashed lmm_oi"
1325
1326 test_14() {
1327         echo "#####"
1328         echo "The OST-object referenced by the MDT-object should be there;"
1329         echo "otherwise, the LFSCK should re-create the missed OST-object."
1330         echo "#####"
1331
1332         echo "stopall"
1333         stopall > /dev/null
1334         echo "formatall"
1335         formatall > /dev/null
1336         echo "setupall"
1337         setupall > /dev/null
1338
1339         mkdir -p $DIR/$tdir
1340         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1341
1342         echo "Inject failure stub to simulate dangling referenced MDT-object"
1343         #define OBD_FAIL_LFSCK_DANGLING 0x1610
1344         do_facet ost1 $LCTL set_param fail_loc=0x1610
1345         createmany -o $DIR/$tdir/f 64
1346         do_facet ost1 $LCTL set_param fail_loc=0
1347
1348         echo "stopall to cleanup object cache"
1349         stopall > /dev/null
1350         echo "setupall"
1351         setupall > /dev/null
1352
1353         echo "'ls' should fail because of dangling referenced MDT-object"
1354         ls -ail $DIR/$tdir > /dev/null 2>&1 && error "(1) ls should fail."
1355
1356         echo "Trigger layout LFSCK to find out dangling reference"
1357         $START_LAYOUT || error "(2) Fail to start LFSCK for layout!"
1358
1359         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1360                 mdd.${MDT_DEV}.lfsck_layout |
1361                 awk '/^status/ { print \\\$2 }'" "completed" 6 || return 3
1362
1363         local repaired=$($SHOW_LAYOUT |
1364                          awk '/^repaired_dangling/ { print $2 }')
1365         [ $repaired -eq 32 ] ||
1366                 error "(4) Fail to repair dangling reference: $repaired"
1367
1368         echo "'ls' should fail because it will not repair dangling by default"
1369         ls -ail $DIR/$tdir > /dev/null 2>&1 && error "(5) ls should fail."
1370
1371         echo "Trigger layout LFSCK to repair dangling reference"
1372         $START_LAYOUT -r -c || error "(6) Fail to start LFSCK for layout!"
1373
1374         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1375                 mdd.${MDT_DEV}.lfsck_layout |
1376                 awk '/^status/ { print \\\$2 }'" "completed" 6 || return 3
1377
1378         local repaired=$($SHOW_LAYOUT |
1379                          awk '/^repaired_dangling/ { print $2 }')
1380         [ $repaired -eq 32 ] ||
1381                 error "(7) Fail to repair dangling reference: $repaired"
1382
1383         echo "'ls' should success after layout LFSCK repairing"
1384         ls -ail $DIR/$tdir > /dev/null || error "(8) ls should success."
1385 }
1386 run_test 14 "LFSCK can repair MDT-object with dangling reference"
1387
1388 test_15a() {
1389         echo "#####"
1390         echo "If the OST-object referenced by the MDT-object back points"
1391         echo "to some non-exist MDT-object, then the LFSCK should repair"
1392         echo "the OST-object to back point to the right MDT-object."
1393         echo "#####"
1394
1395         echo "stopall"
1396         stopall > /dev/null
1397         echo "formatall"
1398         formatall > /dev/null
1399         echo "setupall"
1400         setupall > /dev/null
1401
1402         mkdir -p $DIR/$tdir
1403         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1404
1405         echo "Inject failure stub to make the OST-object to back point to"
1406         echo "non-exist MDT-object."
1407         #define OBD_FAIL_LFSCK_UNMATCHED_PAIR1  0x1611
1408
1409         do_facet ost1 $LCTL set_param fail_loc=0x1611
1410         dd if=/dev/zero of=$DIR/$tdir/f0 bs=1M count=1
1411         cancel_lru_locks osc
1412         sync
1413         sleep 2
1414         do_facet ost1 $LCTL set_param fail_loc=0
1415
1416         echo "stopall to cleanup object cache"
1417         stopall > /dev/null
1418         echo "setupall"
1419         setupall > /dev/null
1420
1421         echo "Trigger layout LFSCK to find out unmatched pairs and fix them"
1422         $START_LAYOUT || error "(1) Fail to start LFSCK for layout!"
1423
1424         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1425                 mdd.${MDT_DEV}.lfsck_layout |
1426                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 2
1427
1428         local repaired=$($SHOW_LAYOUT |
1429                          awk '/^repaired_unmatched_pair/ { print $2 }')
1430         [ $repaired -eq 1 ] ||
1431                 error "(3) Fail to repair unmatched pair: $repaired"
1432 }
1433 run_test 15a "LFSCK can repair unmatched MDT-object/OST-object pairs (1)"
1434
1435 test_15b() {
1436         echo "#####"
1437         echo "If the OST-object referenced by the MDT-object back points"
1438         echo "to other MDT-object that doesn't recognize the OST-object,"
1439         echo "then the LFSCK should repair it to back point to the right"
1440         echo "MDT-object (the first one)."
1441         echo "#####"
1442
1443         echo "stopall"
1444         stopall > /dev/null
1445         echo "formatall"
1446         formatall > /dev/null
1447         echo "setupall"
1448         setupall > /dev/null
1449
1450         mkdir -p $DIR/$tdir
1451         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1452         dd if=/dev/zero of=$DIR/$tdir/guard bs=1K count=1
1453         cancel_lru_locks osc
1454
1455         echo "Inject failure stub to make the OST-object to back point to"
1456         echo "other MDT-object"
1457
1458         #define OBD_FAIL_LFSCK_UNMATCHED_PAIR2  0x1612
1459         do_facet ost1 $LCTL set_param fail_loc=0x1612
1460         dd if=/dev/zero of=$DIR/$tdir/f0 bs=1M count=1
1461         cancel_lru_locks osc
1462         sync
1463         sleep 2
1464         do_facet ost1 $LCTL set_param fail_loc=0
1465
1466         echo "stopall to cleanup object cache"
1467         stopall > /dev/null
1468         echo "setupall"
1469         setupall > /dev/null
1470
1471         echo "Trigger layout LFSCK to find out unmatched pairs and fix them"
1472         $START_LAYOUT || error "(1) Fail to start LFSCK for layout!"
1473
1474         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1475                 mdd.${MDT_DEV}.lfsck_layout |
1476                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 2
1477
1478         local repaired=$($SHOW_LAYOUT |
1479                          awk '/^repaired_unmatched_pair/ { print $2 }')
1480         [ $repaired -eq 1 ] ||
1481                 error "(3) Fail to repair unmatched pair: $repaired"
1482 }
1483 run_test 15b "LFSCK can repair unmatched MDT-object/OST-object pairs (2)"
1484
1485 test_16() {
1486         echo "#####"
1487         echo "If the OST-object's owner information does not match the owner"
1488         echo "information stored in the MDT-object, then the LFSCK trust the"
1489         echo "MDT-object and update the OST-object's owner information."
1490         echo "#####"
1491
1492         echo "stopall"
1493         stopall > /dev/null
1494         echo "formatall"
1495         formatall > /dev/null
1496         echo "setupall"
1497         setupall > /dev/null
1498
1499         mkdir -p $DIR/$tdir
1500         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1501         dd if=/dev/zero of=$DIR/$tdir/f0 bs=1M count=1
1502         cancel_lru_locks osc
1503         sync
1504         sleep 2
1505
1506         echo "Inject failure stub to skip OST-object owner changing"
1507         #define OBD_FAIL_LFSCK_BAD_OWNER        0x1613
1508         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1613
1509         chown 1.1 $DIR/$tdir/f0
1510         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
1511
1512         echo "Trigger layout LFSCK to find out inconsistent OST-object owner"
1513         echo "and fix them"
1514
1515         $START_LAYOUT || error "(1) Fail to start LFSCK for layout!"
1516
1517         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1518                 mdd.${MDT_DEV}.lfsck_layout |
1519                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 2
1520
1521         local repaired=$($SHOW_LAYOUT |
1522                          awk '/^repaired_inconsistent_owner/ { print $2 }')
1523         [ $repaired -eq 1 ] ||
1524                 error "(3) Fail to repair inconsistent owner: $repaired"
1525 }
1526 run_test 16 "LFSCK can repair inconsistent MDT-object/OST-object owner"
1527
1528 test_17() {
1529         echo "#####"
1530         echo "If more than one MDT-objects reference the same OST-object,"
1531         echo "and the OST-object only recognizes one MDT-object, then the"
1532         echo "LFSCK should create new OST-objects for such non-recognized"
1533         echo "MDT-objects."
1534         echo "#####"
1535
1536         echo "stopall"
1537         stopall > /dev/null
1538         echo "formatall"
1539         formatall > /dev/null
1540         echo "setupall"
1541         setupall > /dev/null
1542
1543         mkdir -p $DIR/$tdir
1544         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1545
1546         echo "Inject failure stub to make two MDT-objects to refernce"
1547         echo "the OST-object"
1548
1549         do_facet $SINGLEMDS $LCTL set_param fail_val=0
1550         #define OBD_FAIL_LFSCK_MULTIPLE_REF     0x1614
1551         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1614
1552
1553         dd if=/dev/zero of=$DIR/$tdir/guard bs=1M count=1
1554         cancel_lru_locks osc
1555         sync
1556         sleep 2
1557
1558         createmany -o $DIR/$tdir/f 1 > /dev/null 2>&1
1559
1560         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
1561         do_facet $SINGLEMDS $LCTL set_param fail_val=0
1562
1563         echo "stopall to cleanup object cache"
1564         stopall > /dev/null
1565         echo "setupall"
1566         setupall > /dev/null
1567
1568         echo "$DIR/$tdir/f0 and $DIR/$tdir/guard use the same OST-objects"
1569         local size=$(ls -l $DIR/$tdir/f0 | awk '{ print $5 }')
1570         [ $size -eq 1048576 ] ||
1571                 error "(1) f0 (wrong) size should be 1048576, but got $size"
1572
1573         echo "Trigger layout LFSCK to find out multiple refenced MDT-objects"
1574         echo "and fix them"
1575
1576         $START_LAYOUT || error "(2) Fail to start LFSCK for layout!"
1577
1578         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1579                 mdd.${MDT_DEV}.lfsck_layout |
1580                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 3
1581
1582         local repaired=$($SHOW_LAYOUT |
1583                          awk '/^repaired_multiple_referenced/ { print $2 }')
1584         [ $repaired -eq 1 ] ||
1585                 error "(4) Fail to repair multiple references: $repaired"
1586
1587         echo "$DIR/$tdir/f0 and $DIR/$tdir/guard should use diff OST-objects"
1588         dd if=/dev/zero of=$DIR/$tdir/f0 bs=1M count=2 ||
1589                 error "(5) Fail to write f0."
1590         size=$(ls -l $DIR/$tdir/guard | awk '{ print $5 }')
1591         [ $size -eq 1048576 ] ||
1592                 error "(6) guard size should be 1048576, but got $size"
1593 }
1594 run_test 17 "LFSCK can repair multiple references"
1595
1596 test_18a() {
1597         [ $MDSCOUNT -lt 2 ] &&
1598                 skip "We need at least 2 MDSes for test_18a" && exit 0
1599
1600         [ $OSTCOUNT -lt 2 ] &&
1601                 skip "We need at least 2 OSTs for test_18a" && exit 0
1602
1603         echo "#####"
1604         echo "The target MDT-object is there, but related stripe information"
1605         echo "is lost or partly lost. The LFSCK should regenerate the missed"
1606         echo "layout EA entries."
1607         echo "#####"
1608
1609         echo "stopall"
1610         stopall > /dev/null
1611         echo "formatall"
1612         formatall > /dev/null
1613         echo "setupall"
1614         setupall > /dev/null
1615
1616         mkdir -p $DIR/$tdir
1617         $LFS mkdir -i 0 $DIR/$tdir/a1
1618         $LFS mkdir -i 1 $DIR/$tdir/a2
1619         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
1620         $LFS setstripe -c 2 -i 1 -s 1M $DIR/$tdir/a2
1621         dd if=/dev/zero of=$DIR/$tdir/a1/f1 bs=1M count=2
1622         dd if=/dev/zero of=$DIR/$tdir/a2/f2 bs=1M count=2
1623
1624         local saved_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1625
1626         $LFS path2fid $DIR/$tdir/a1/f1
1627         $LFS getstripe $DIR/$tdir/a1/f1
1628         $LFS path2fid $DIR/$tdir/a2/f2
1629         $LFS getstripe $DIR/$tdir/a2/f2
1630         sync
1631         cancel_lru_locks osc
1632
1633         echo "Inject failure, to make the MDT-object lost its layout EA"
1634         #define OBD_FAIL_LFSCK_LOST_STRIPE 0x1615
1635         do_facet mds1 $LCTL set_param fail_loc=0x1615
1636         chown 1.1 $DIR/$tdir/a1/f1
1637         do_facet mds2 $LCTL set_param fail_loc=0x1615
1638         chown 1.1 $DIR/$tdir/a2/f2
1639         sync
1640         sleep 2
1641         do_facet mds1 $LCTL set_param fail_loc=0
1642         do_facet mds2 $LCTL set_param fail_loc=0
1643
1644         echo "stopall to cleanup object cache"
1645         stopall > /dev/null
1646         echo "setupall"
1647         setupall > /dev/null
1648
1649         echo "The file size should be incorrect since layout EA is lost"
1650         local cur_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1651         [ "$cur_size" != "$saved_size" ] ||
1652                 error "(1) Expect incorrect file1 size"
1653
1654         cur_size=$(ls -il $DIR/$tdir/a2/f2 | awk '{ print $6 }')
1655         [ "$cur_size" != "$saved_size" ] ||
1656                 error "(2) Expect incorrect file2 size"
1657
1658         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
1659         $START_LAYOUT -o || error "(3) Fail to start LFSCK for layout!"
1660
1661         for k in $(seq $MDSCOUNT); do
1662                 # The LFSCK status query internal is 30 seconds. For the case
1663                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1664                 # time to guarantee the status sync up.
1665                 wait_update_facet mds${k} "$LCTL get_param -n \
1666                         mdd.$(facet_svc mds${k}).lfsck_layout |
1667                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1668                         error "(4) MDS${k} is not the expected 'completed'"
1669         done
1670
1671         for k in $(seq $OSTCOUNT); do
1672                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
1673                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1674                                 awk '/^status/ { print $2 }')
1675                 [ "$cur_status" == "completed" ] ||
1676                 error "(5) OST${k} Expect 'completed', but got '$cur_status'"
1677         done
1678
1679         for k in 1 2; do
1680                 local repaired=$(do_facet mds${k} $LCTL get_param -n \
1681                                  mdd.$(facet_svc mds${k}).lfsck_layout |
1682                                  awk '/^repaired_orphan/ { print $2 }')
1683                 [ $repaired -eq ${k} ] ||
1684                 error "(6) Expect ${k} fixed on mds${k}, but got: $repaired"
1685         done
1686
1687         $LFS path2fid $DIR/$tdir/a1/f1
1688         $LFS getstripe $DIR/$tdir/a1/f1
1689         $LFS path2fid $DIR/$tdir/a2/f2
1690         $LFS getstripe $DIR/$tdir/a2/f2
1691
1692         echo "The file size should be correct after layout LFSCK scanning"
1693         cur_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1694         [ "$cur_size" == "$saved_size" ] ||
1695                 error "(7) Expect file1 size $saved_size, but got $cur_size"
1696
1697         cur_size=$(ls -il $DIR/$tdir/a2/f2 | awk '{ print $6 }')
1698         [ "$cur_size" == "$saved_size" ] ||
1699                 error "(8) Expect file2 size $saved_size, but got $cur_size"
1700 }
1701 run_test 18a "Find out orphan OST-object and repair it (1)"
1702
1703 test_18b() {
1704         [ $MDSCOUNT -lt 2 ] &&
1705                 skip "We need at least 2 MDSes for test_18b" && exit 0
1706
1707         [ $OSTCOUNT -lt 2 ] &&
1708                 skip "We need at least 2 OSTs for test_18b" && exit 0
1709
1710         echo "#####"
1711         echo "The target MDT-object is lost. The LFSCK should re-create the"
1712         echo "MDT-object under .lustre/lost+found/MDTxxxx. The admin should"
1713         echo "can move it back to normal namespace manually."
1714         echo "#####"
1715
1716         echo "stopall"
1717         stopall > /dev/null
1718         echo "formatall"
1719         formatall > /dev/null
1720         echo "setupall"
1721         setupall > /dev/null
1722
1723         mkdir -p $DIR/$tdir
1724         $LFS mkdir -i 0 $DIR/$tdir/a1
1725         $LFS mkdir -i 1 $DIR/$tdir/a2
1726         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
1727         $LFS setstripe -c 2 -i 1 -s 1M $DIR/$tdir/a2
1728         dd if=/dev/zero of=$DIR/$tdir/a1/f1 bs=1M count=2
1729         dd if=/dev/zero of=$DIR/$tdir/a2/f2 bs=1M count=2
1730         local saved_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1731         local fid1=$($LFS path2fid $DIR/$tdir/a1/f1)
1732         echo ${fid1}
1733         $LFS getstripe $DIR/$tdir/a1/f1
1734         local fid2=$($LFS path2fid $DIR/$tdir/a2/f2)
1735         echo ${fid2}
1736         $LFS getstripe $DIR/$tdir/a2/f2
1737         sync
1738         cancel_lru_locks osc
1739
1740         echo "Inject failure, to simulate the case of missing the MDT-object"
1741         #define OBD_FAIL_LFSCK_LOST_MDTOBJ      0x1616
1742         do_facet mds1 $LCTL set_param fail_loc=0x1616
1743         rm -f $DIR/$tdir/a1/f1
1744         do_facet mds2 $LCTL set_param fail_loc=0x1616
1745         rm -f $DIR/$tdir/a2/f2
1746         sync
1747         sleep 2
1748         do_facet mds1 $LCTL set_param fail_loc=0
1749         do_facet mds2 $LCTL set_param fail_loc=0
1750
1751         echo "stopall to cleanup object cache"
1752         stopall > /dev/null
1753         echo "setupall"
1754         setupall > /dev/null
1755
1756         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
1757         $START_LAYOUT -o || error "(1) Fail to start LFSCK for layout!"
1758
1759         for k in $(seq $MDSCOUNT); do
1760                 # The LFSCK status query internal is 30 seconds. For the case
1761                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1762                 # time to guarantee the status sync up.
1763                 wait_update_facet mds${k} "$LCTL get_param -n \
1764                         mdd.$(facet_svc mds${k}).lfsck_layout |
1765                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1766                         error "(2) MDS${k} is not the expected 'completed'"
1767         done
1768
1769         for k in $(seq $OSTCOUNT); do
1770                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
1771                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1772                                 awk '/^status/ { print $2 }')
1773                 [ "$cur_status" == "completed" ] ||
1774                 error "(3) OST${k} Expect 'completed', but got '$cur_status'"
1775         done
1776
1777         for k in 1 2; do
1778                 local repaired=$(do_facet mds${k} $LCTL get_param -n \
1779                                  mdd.$(facet_svc mds${k}).lfsck_layout |
1780                                  awk '/^repaired_orphan/ { print $2 }')
1781                 [ $repaired -eq ${k} ] ||
1782                 error "(4) Expect ${k} fixed on mds${k}, but got: $repaired"
1783         done
1784
1785         echo "Move the files from ./lustre/lost+found/MDTxxxx to namespace"
1786         mv $MOUNT/.lustre/lost+found/MDT0000/R-${fid1} $DIR/$tdir/a1/f1 ||
1787         error "(5) Fail to move $MOUNT/.lustre/lost+found/MDT0000/R-${fid1}"
1788
1789         mv $MOUNT/.lustre/lost+found/MDT0001/R-${fid2} $DIR/$tdir/a2/f2 ||
1790         error "(6) Fail to move $MOUNT/.lustre/lost+found/MDT0001/R-${fid2}"
1791
1792         $LFS path2fid $DIR/$tdir/a1/f1
1793         $LFS getstripe $DIR/$tdir/a1/f1
1794         $LFS path2fid $DIR/$tdir/a2/f2
1795         $LFS getstripe $DIR/$tdir/a2/f2
1796
1797         echo "The file size should be correct after layout LFSCK scanning"
1798         local cur_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1799         [ "$cur_size" == "$saved_size" ] ||
1800                 error "(7) Expect file1 size $saved_size, but got $cur_size"
1801
1802         cur_size=$(ls -il $DIR/$tdir/a2/f2 | awk '{ print $6 }')
1803         [ "$cur_size" == "$saved_size" ] ||
1804                 error "(8) Expect file2 size $saved_size, but got $cur_size"
1805 }
1806 run_test 18b "Find out orphan OST-object and repair it (2)"
1807
1808 test_18c() {
1809         [ $MDSCOUNT -lt 2 ] &&
1810                 skip "We need at least 2 MDSes for test_18c" && exit 0
1811
1812         [ $OSTCOUNT -lt 2 ] &&
1813                 skip "We need at least 2 OSTs for test_18c" && exit 0
1814
1815         echo "#####"
1816         echo "The target MDT-object is lost, and the OST-object FID is missing."
1817         echo "The LFSCK should re-create the MDT-object with new FID under the "
1818         echo "directory .lustre/lost+found/MDTxxxx."
1819         echo "#####"
1820
1821         echo "stopall"
1822         stopall > /dev/null
1823         echo "formatall"
1824         formatall > /dev/null
1825         echo "setupall"
1826         setupall > /dev/null
1827
1828         mkdir -p $DIR/$tdir
1829         $LFS mkdir -i 0 $DIR/$tdir/a1
1830         $LFS mkdir -i 1 $DIR/$tdir/a2
1831         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
1832         $LFS setstripe -c 2 -i 1 -s 1M $DIR/$tdir/a2
1833
1834         echo "Inject failure, to simulate the case of missing parent FID"
1835         #define OBD_FAIL_LFSCK_NOPFID           0x1617
1836         do_facet ost1 $LCTL set_param fail_loc=0x1617
1837         do_facet ost2 $LCTL set_param fail_loc=0x1617
1838
1839         dd if=/dev/zero of=$DIR/$tdir/a1/f1 bs=1M count=2
1840         dd if=/dev/zero of=$DIR/$tdir/a2/f2 bs=1M count=2
1841         $LFS getstripe $DIR/$tdir/a1/f1
1842         $LFS getstripe $DIR/$tdir/a2/f2
1843         sync
1844         cancel_lru_locks osc
1845
1846         echo "Inject failure, to simulate the case of missing the MDT-object"
1847         #define OBD_FAIL_LFSCK_LOST_MDTOBJ      0x1616
1848         do_facet mds1 $LCTL set_param fail_loc=0x1616
1849         rm -f $DIR/$tdir/a1/f1
1850         do_facet mds2 $LCTL set_param fail_loc=0x1616
1851         rm -f $DIR/$tdir/a2/f2
1852         sync
1853         sleep 2
1854         do_facet mds1 $LCTL set_param fail_loc=0
1855         do_facet mds2 $LCTL set_param fail_loc=0
1856
1857         echo "stopall to cleanup object cache"
1858         stopall > /dev/null
1859         echo "setupall"
1860         setupall > /dev/null
1861
1862         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
1863         $START_LAYOUT -o || error "(1) Fail to start LFSCK for layout!"
1864
1865         for k in $(seq $MDSCOUNT); do
1866                 # The LFSCK status query internal is 30 seconds. For the case
1867                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1868                 # time to guarantee the status sync up.
1869                 wait_update_facet mds${k} "$LCTL get_param -n \
1870                         mdd.$(facet_svc mds${k}).lfsck_layout |
1871                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1872                         error "(2) MDS${k} is not the expected 'completed'"
1873         done
1874
1875         for k in $(seq $OSTCOUNT); do
1876                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
1877                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1878                                 awk '/^status/ { print $2 }')
1879                 [ "$cur_status" == "completed" ] ||
1880                 error "(3) OST${k} Expect 'completed', but got '$cur_status'"
1881         done
1882
1883         local repaired=$(do_facet mds1 $LCTL get_param -n \
1884                          mdd.$(facet_svc mds1).lfsck_layout |
1885                          awk '/^repaired_orphan/ { print $2 }')
1886         [ $repaired -eq 3 ] ||
1887                 error "(4) Expect 3 fixed on mds1, but got: $repaired"
1888
1889         repaired=$(do_facet mds2 $LCTL get_param -n \
1890                    mdd.$(facet_svc mds2).lfsck_layout |
1891                    awk '/^repaired_orphan/ { print $2 }')
1892         [ $repaired -eq 0 ] ||
1893                 error "(5) Expect 0 fixed on mds2, but got: $repaired"
1894
1895         echo "There should be some stub under .lustre/lost+found/MDT0001/"
1896         ls -ail $MOUNT/.lustre/lost+found/MDT0001/N-* &&
1897                 error "(6) .lustre/lost+found/MDT0001/ should be empty"
1898
1899         echo "There should be some stub under .lustre/lost+found/MDT0000/"
1900         ls -ail $MOUNT/.lustre/lost+found/MDT0000/N-* ||
1901                 error "(7) .lustre/lost+found/MDT0000/ should not be empty"
1902 }
1903 run_test 18c "Find out orphan OST-object and repair it (3)"
1904
1905 test_18d() {
1906         echo "#####"
1907         echo "The target MDT-object layout EA slot is occpuied by some new"
1908         echo "created OST-object when repair dangling reference case. Such"
1909         echo "conflict OST-object has never been modified. Then when found"
1910         echo "the orphan OST-object, LFSCK will replace it with the orphan"
1911         echo "OST-object."
1912         echo "#####"
1913
1914         echo "stopall"
1915         stopall > /dev/null
1916         echo "formatall"
1917         formatall > /dev/null
1918         echo "setupall"
1919         setupall > /dev/null
1920
1921         mkdir -p $DIR/$tdir/a1
1922         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
1923         echo "guard" > $DIR/$tdir/a1/f1
1924         echo "foo" > $DIR/$tdir/a1/f2
1925         local saved_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
1926         $LFS path2fid $DIR/$tdir/a1/f1
1927         $LFS getstripe $DIR/$tdir/a1/f1
1928         $LFS path2fid $DIR/$tdir/a1/f2
1929         $LFS getstripe $DIR/$tdir/a1/f2
1930         sync
1931         cancel_lru_locks osc
1932
1933         echo "Inject failure to make $DIR/$tdir/a1/f1 and $DIR/$tdir/a1/f2"
1934         echo "to reference the same OST-object (which is f1's OST-obejct)."
1935         echo "Then drop $DIR/$tdir/a1/f1 and its OST-object, so f2 becomes"
1936         echo "dangling reference case, but f2's old OST-object is there."
1937         echo
1938
1939         #define OBD_FAIL_LFSCK_CHANGE_STRIPE    0x1618
1940         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1618
1941         chown 1.1 $DIR/$tdir/a1/f2
1942         rm -f $DIR/$tdir/a1/f1
1943         sync
1944         sleep 2
1945         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
1946
1947         echo "stopall to cleanup object cache"
1948         stopall > /dev/null
1949         echo "setupall"
1950         setupall > /dev/null
1951
1952         echo "The file size should be incorrect since dangling referenced"
1953         local cur_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
1954         [ "$cur_size" != "$saved_size" ] ||
1955                 error "(1) Expect incorrect file2 size"
1956
1957         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
1958         $START_LAYOUT -o -c || error "(2) Fail to start LFSCK for layout!"
1959
1960         for k in $(seq $MDSCOUNT); do
1961                 # The LFSCK status query internal is 30 seconds. For the case
1962                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1963                 # time to guarantee the status sync up.
1964                 wait_update_facet mds${k} "$LCTL get_param -n \
1965                         mdd.$(facet_svc mds${k}).lfsck_layout |
1966                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1967                         error "(3) MDS${k} is not the expected 'completed'"
1968         done
1969
1970         for k in $(seq $OSTCOUNT); do
1971                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
1972                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1973                                 awk '/^status/ { print $2 }')
1974                 [ "$cur_status" == "completed" ] ||
1975                 error "(4) OST${k} Expect 'completed', but got '$cur_status'"
1976         done
1977
1978         local repaired=$(do_facet $SINGLEMDS $LCTL get_param -n \
1979                          mdd.$(facet_svc $SINGLEMDS).lfsck_layout |
1980                          awk '/^repaired_orphan/ { print $2 }')
1981         [ $repaired -eq 1 ] ||
1982                 error "(5) Expect 1 orphan has been fixed, but got: $repaired"
1983
1984         echo "The file size should be correct after layout LFSCK scanning"
1985         cur_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
1986         [ "$cur_size" == "$saved_size" ] ||
1987                 error "(6) Expect file2 size $saved_size, but got $cur_size"
1988
1989         echo "There should be some stub under .lustre/lost+found/MDT0000/"
1990         ls -ail $MOUNT/.lustre/lost+found/MDT0000/ &&
1991                 error "(7) .lustre/lost+found/MDT0000/ should be empty"
1992
1993         echo "The LFSCK should find back the original data."
1994         cat $DIR/$tdir/a1/f2
1995         $LFS path2fid $DIR/$tdir/a1/f2
1996         $LFS getstripe $DIR/$tdir/a1/f2
1997 }
1998 run_test 18d "Find out orphan OST-object and repair it (4)"
1999
2000 test_18e() {
2001         echo "#####"
2002         echo "The target MDT-object layout EA slot is occpuied by some new"
2003         echo "created OST-object when repair dangling reference case. Such"
2004         echo "conflict OST-object has been modified by others. To keep the"
2005         echo "new data, the LFSCK will create a new file to refernece this"
2006         echo "old orphan OST-object."
2007         echo "#####"
2008
2009         echo "stopall"
2010         stopall > /dev/null
2011         echo "formatall"
2012         formatall > /dev/null
2013         echo "setupall"
2014         setupall > /dev/null
2015
2016         mkdir -p $DIR/$tdir/a1
2017         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
2018         echo "guard" > $DIR/$tdir/a1/f1
2019         echo "foo" > $DIR/$tdir/a1/f2
2020         local saved_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
2021         $LFS path2fid $DIR/$tdir/a1/f1
2022         $LFS getstripe $DIR/$tdir/a1/f1
2023         $LFS path2fid $DIR/$tdir/a1/f2
2024         $LFS getstripe $DIR/$tdir/a1/f2
2025         sync
2026         cancel_lru_locks osc
2027
2028         echo "Inject failure to make $DIR/$tdir/a1/f1 and $DIR/$tdir/a1/f2"
2029         echo "to reference the same OST-object (which is f1's OST-obejct)."
2030         echo "Then drop $DIR/$tdir/a1/f1 and its OST-object, so f2 becomes"
2031         echo "dangling reference case, but f2's old OST-object is there."
2032         echo
2033
2034         #define OBD_FAIL_LFSCK_CHANGE_STRIPE    0x1618
2035         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1618
2036         chown 1.1 $DIR/$tdir/a1/f2
2037         rm -f $DIR/$tdir/a1/f1
2038         sync
2039         sleep 2
2040         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
2041
2042         echo "stopall to cleanup object cache"
2043         stopall > /dev/null
2044         echo "setupall"
2045         setupall > /dev/null
2046
2047         echo "The file size should be incorrect since dangling referenced"
2048         local cur_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
2049         [ "$cur_size" != "$saved_size" ] ||
2050                 error "(1) Expect incorrect file2 size"
2051
2052         #define OBD_FAIL_LFSCK_DELAY3           0x1602
2053         do_facet $SINGLEMDS $LCTL set_param fail_val=10
2054         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1602
2055
2056         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
2057         $START_LAYOUT -o -c || error "(2) Fail to start LFSCK for layout!"
2058
2059         wait_update_facet mds1 "$LCTL get_param -n \
2060                 mdd.$(facet_svc mds1).lfsck_layout |
2061                 awk '/^status/ { print \\\$2 }'" "scanning-phase2" 6 ||
2062                 error "(3) MDS1 is not the expected 'scanning-phase2'"
2063
2064         # to guarantee all updates are synced.
2065         sync
2066         sleep 2
2067
2068         echo "Write new data to f2 to modify the new created OST-object."
2069         echo "dummy" >> $DIR/$tdir/a1/f2
2070
2071         do_facet $SINGLEMDS $LCTL set_param fail_val=0
2072         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
2073
2074         for k in $(seq $MDSCOUNT); do
2075                 # The LFSCK status query internal is 30 seconds. For the case
2076                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
2077                 # time to guarantee the status sync up.
2078                 wait_update_facet mds${k} "$LCTL get_param -n \
2079                         mdd.$(facet_svc mds${k}).lfsck_layout |
2080                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
2081                         error "(4) MDS${k} is not the expected 'completed'"
2082         done
2083
2084         for k in $(seq $OSTCOUNT); do
2085                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
2086                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
2087                                 awk '/^status/ { print $2 }')
2088                 [ "$cur_status" == "completed" ] ||
2089                 error "(5) OST${k} Expect 'completed', but got '$cur_status'"
2090         done
2091
2092         local repaired=$(do_facet $SINGLEMDS $LCTL get_param -n \
2093                          mdd.$(facet_svc $SINGLEMDS).lfsck_layout |
2094                          awk '/^repaired_orphan/ { print $2 }')
2095         [ $repaired -eq 1 ] ||
2096                 error "(6) Expect 1 orphan has been fixed, but got: $repaired"
2097
2098         echo "There should be stub file under .lustre/lost+found/MDT0000/"
2099         local cname=$(ls $MOUNT/.lustre/lost+found/MDT0000/C-*)
2100         [ ! -z $name ] ||
2101                 error "(7) .lustre/lost+found/MDT0000/ should not be empty"
2102
2103         echo "The stub file should keep the original f2 data"
2104         cur_size=$(ls -il $cname | awk '{ print $6 }')
2105         [ "$cur_size" == "$saved_size" ] ||
2106                 error "(8) Expect file2 size $saved_size, but got $cur_size"
2107
2108         cat $cname
2109         $LFS path2fid $cname
2110         $LFS getstripe $cname
2111
2112         echo "The f2 should contains new data."
2113         cat $DIR/$tdir/a1/f2
2114         $LFS path2fid $DIR/$tdir/a1/f2
2115         $LFS getstripe $DIR/$tdir/a1/f2
2116 }
2117 run_test 18e "Find out orphan OST-object and repair it (5)"
2118
2119 test_19a() {
2120         echo "stopall"
2121         stopall > /dev/null
2122         echo "formatall"
2123         formatall > /dev/null
2124         echo "setupall"
2125         setupall > /dev/null
2126
2127         mkdir -p $DIR/$tdir
2128         $LFS setstripe -c 1 -i 0 $DIR/$tdir
2129
2130         echo "foo" > $DIR/$tdir/a0
2131         echo "guard" > $DIR/$tdir/a1
2132
2133         cancel_lru_locks osc
2134         umount_client $MOUNT || error "(1) Fail to stop client!"
2135         mount_client $MOUNT || error "(2) Fail to start client!"
2136
2137         echo "Inject failure, then client will offer wrong parent FID when read"
2138         do_facet ost1 $LCTL set_param -n \
2139                 obdfilter.${FSNAME}-OST0000.lfsck_verify_pfid 1
2140         #define OBD_FAIL_LFSCK_INVALID_PFID     0x1619
2141         $LCTL set_param fail_loc=0x1619
2142
2143         echo "Read RPC with wrong parent FID should be denied"
2144         cat $DIR/$tdir/a0 && error "(3) Read should be denied!"
2145         $LCTL set_param fail_loc=0
2146 }
2147 run_test 19a "OST-object inconsistency self detect"
2148
2149 test_19b() {
2150         echo "stopall"
2151         stopall > /dev/null
2152         echo "formatall"
2153         formatall > /dev/null
2154         echo "setupall"
2155         setupall > /dev/null
2156
2157         mkdir -p $DIR/$tdir
2158         $LFS setstripe -c 1 -i 0 $DIR/$tdir
2159
2160         echo "Inject failure stub to make the OST-object to back point to"
2161         echo "non-exist MDT-object"
2162
2163         #define OBD_FAIL_LFSCK_UNMATCHED_PAIR1  0x1611
2164         do_facet ost1 $LCTL set_param fail_loc=0x1611
2165         echo "foo" > $DIR/$tdir/f0
2166         cancel_lru_locks osc
2167         sync
2168         sleep 2
2169         do_facet ost1 $LCTL set_param fail_loc=0
2170
2171         echo "Nothing should be fixed since self detect and repair is disabled"
2172         local repaired=$(do_facet ost1 $LCTL get_param -n \
2173                         obdfilter.${FSNAME}-OST0000.lfsck_verify_pfid |
2174                         awk '/^repaired/ { print $2 }')
2175         [ $repaired -eq 0 ] ||
2176                 error "(1) Expected 0 repaired, but got $repaired"
2177
2178         echo "Read RPC with right parent FID should be accepted,"
2179         echo "and cause parent FID on OST to be fixed"
2180
2181         do_facet ost1 $LCTL set_param -n \
2182                 obdfilter.${FSNAME}-OST0000.lfsck_verify_pfid 1
2183         cat $DIR/$tdir/f0 || error "(2) Read should not be denied!"
2184
2185         repaired=$(do_facet ost1 $LCTL get_param -n \
2186                 obdfilter.${FSNAME}-OST0000.lfsck_verify_pfid |
2187                 awk '/^repaired/ { print $2 }')
2188         [ $repaired -eq 1 ] ||
2189                 error "(3) Expected 1 repaired, but got $repaired"
2190 }
2191 run_test 19b "OST-object inconsistency self repair"
2192
2193 $LCTL set_param debug=-lfsck > /dev/null || true
2194
2195 # restore MDS/OST size
2196 MDSSIZE=${SAVED_MDSSIZE}
2197 OSTSIZE=${SAVED_OSTSIZE}
2198
2199 # cleanup the system at last
2200 formatall
2201
2202 complete $SECONDS
2203 exit_status