Whamcloud - gitweb
49519c4805cbfaf970ad645c3f223fd1840b467d
[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 and fix them"
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 success after layout LFSCK repairing"
1369         ls -ail $DIR/$tdir > /dev/null || error "(5) ls should success."
1370 }
1371 run_test 14 "LFSCK can repair MDT-object with dangling reference"
1372
1373 test_15a() {
1374         echo "#####"
1375         echo "If the OST-object referenced by the MDT-object back points"
1376         echo "to some non-exist MDT-object, then the LFSCK should repair"
1377         echo "the OST-object to back point to the right MDT-object."
1378         echo "#####"
1379
1380         echo "stopall"
1381         stopall > /dev/null
1382         echo "formatall"
1383         formatall > /dev/null
1384         echo "setupall"
1385         setupall > /dev/null
1386
1387         mkdir -p $DIR/$tdir
1388         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1389
1390         echo "Inject failure stub to make the OST-object to back point to"
1391         echo "non-exist MDT-object."
1392         #define OBD_FAIL_LFSCK_UNMATCHED_PAIR1  0x1611
1393
1394         do_facet ost1 $LCTL set_param fail_loc=0x1611
1395         dd if=/dev/zero of=$DIR/$tdir/f0 bs=1M count=1
1396         cancel_lru_locks osc
1397         sync
1398         sleep 2
1399         do_facet ost1 $LCTL set_param fail_loc=0
1400
1401         echo "stopall to cleanup object cache"
1402         stopall > /dev/null
1403         echo "setupall"
1404         setupall > /dev/null
1405
1406         echo "Trigger layout LFSCK to find out unmatched pairs and fix them"
1407         $START_LAYOUT || error "(1) Fail to start LFSCK for layout!"
1408
1409         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1410                 mdd.${MDT_DEV}.lfsck_layout |
1411                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 2
1412
1413         local repaired=$($SHOW_LAYOUT |
1414                          awk '/^repaired_unmatched_pair/ { print $2 }')
1415         [ $repaired -eq 1 ] ||
1416                 error "(3) Fail to repair unmatched pair: $repaired"
1417 }
1418 run_test 15a "LFSCK can repair unmatched MDT-object/OST-object pairs (1)"
1419
1420 test_15b() {
1421         echo "#####"
1422         echo "If the OST-object referenced by the MDT-object back points"
1423         echo "to other MDT-object that doesn't recognize the OST-object,"
1424         echo "then the LFSCK should repair it to back point to the right"
1425         echo "MDT-object (the first one)."
1426         echo "#####"
1427
1428         echo "stopall"
1429         stopall > /dev/null
1430         echo "formatall"
1431         formatall > /dev/null
1432         echo "setupall"
1433         setupall > /dev/null
1434
1435         mkdir -p $DIR/$tdir
1436         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1437         dd if=/dev/zero of=$DIR/$tdir/guard bs=1K count=1
1438         cancel_lru_locks osc
1439
1440         echo "Inject failure stub to make the OST-object to back point to"
1441         echo "other MDT-object"
1442
1443         #define OBD_FAIL_LFSCK_UNMATCHED_PAIR2  0x1612
1444         do_facet ost1 $LCTL set_param fail_loc=0x1612
1445         dd if=/dev/zero of=$DIR/$tdir/f0 bs=1M count=1
1446         cancel_lru_locks osc
1447         sync
1448         sleep 2
1449         do_facet ost1 $LCTL set_param fail_loc=0
1450
1451         echo "stopall to cleanup object cache"
1452         stopall > /dev/null
1453         echo "setupall"
1454         setupall > /dev/null
1455
1456         echo "Trigger layout LFSCK to find out unmatched pairs and fix them"
1457         $START_LAYOUT || error "(1) Fail to start LFSCK for layout!"
1458
1459         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1460                 mdd.${MDT_DEV}.lfsck_layout |
1461                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 2
1462
1463         local repaired=$($SHOW_LAYOUT |
1464                          awk '/^repaired_unmatched_pair/ { print $2 }')
1465         [ $repaired -eq 1 ] ||
1466                 error "(3) Fail to repair unmatched pair: $repaired"
1467 }
1468 run_test 15b "LFSCK can repair unmatched MDT-object/OST-object pairs (2)"
1469
1470 test_16() {
1471         echo "#####"
1472         echo "If the OST-object's owner information does not match the owner"
1473         echo "information stored in the MDT-object, then the LFSCK trust the"
1474         echo "MDT-object and update the OST-object's owner information."
1475         echo "#####"
1476
1477         echo "stopall"
1478         stopall > /dev/null
1479         echo "formatall"
1480         formatall > /dev/null
1481         echo "setupall"
1482         setupall > /dev/null
1483
1484         mkdir -p $DIR/$tdir
1485         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1486         dd if=/dev/zero of=$DIR/$tdir/f0 bs=1M count=1
1487         cancel_lru_locks osc
1488         sync
1489         sleep 2
1490
1491         echo "Inject failure stub to skip OST-object owner changing"
1492         #define OBD_FAIL_LFSCK_BAD_OWNER        0x1613
1493         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1613
1494         chown 1.1 $DIR/$tdir/f0
1495         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
1496
1497         echo "Trigger layout LFSCK to find out inconsistent OST-object owner"
1498         echo "and fix them"
1499
1500         $START_LAYOUT || error "(1) Fail to start LFSCK for layout!"
1501
1502         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1503                 mdd.${MDT_DEV}.lfsck_layout |
1504                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 2
1505
1506         local repaired=$($SHOW_LAYOUT |
1507                          awk '/^repaired_inconsistent_owner/ { print $2 }')
1508         [ $repaired -eq 1 ] ||
1509                 error "(3) Fail to repair inconsistent owner: $repaired"
1510 }
1511 run_test 16 "LFSCK can repair inconsistent MDT-object/OST-object owner"
1512
1513 test_17() {
1514         echo "#####"
1515         echo "If more than one MDT-objects reference the same OST-object,"
1516         echo "and the OST-object only recognizes one MDT-object, then the"
1517         echo "LFSCK should create new OST-objects for such non-recognized"
1518         echo "MDT-objects."
1519         echo "#####"
1520
1521         echo "stopall"
1522         stopall > /dev/null
1523         echo "formatall"
1524         formatall > /dev/null
1525         echo "setupall"
1526         setupall > /dev/null
1527
1528         mkdir -p $DIR/$tdir
1529         $LFS setstripe -c 1 -i 0 $DIR/$tdir
1530
1531         echo "Inject failure stub to make two MDT-objects to refernce"
1532         echo "the OST-object"
1533
1534         do_facet $SINGLEMDS $LCTL set_param fail_val=0
1535         #define OBD_FAIL_LFSCK_MULTIPLE_REF     0x1614
1536         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1614
1537
1538         dd if=/dev/zero of=$DIR/$tdir/guard bs=1M count=1
1539         cancel_lru_locks osc
1540         sync
1541         sleep 2
1542
1543         createmany -o $DIR/$tdir/f 1 > /dev/null 2>&1
1544
1545         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
1546         do_facet $SINGLEMDS $LCTL set_param fail_val=0
1547
1548         echo "stopall to cleanup object cache"
1549         stopall > /dev/null
1550         echo "setupall"
1551         setupall > /dev/null
1552
1553         echo "$DIR/$tdir/f0 and $DIR/$tdir/guard use the same OST-objects"
1554         local size=$(ls -l $DIR/$tdir/f0 | awk '{ print $5 }')
1555         [ $size -eq 1048576 ] ||
1556                 error "(1) f0 (wrong) size should be 1048576, but got $size"
1557
1558         echo "Trigger layout LFSCK to find out multiple refenced MDT-objects"
1559         echo "and fix them"
1560
1561         $START_LAYOUT || error "(2) Fail to start LFSCK for layout!"
1562
1563         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
1564                 mdd.${MDT_DEV}.lfsck_layout |
1565                 awk '/^status/ { print \\\$2 }'" "completed" 3 || return 3
1566
1567         local repaired=$($SHOW_LAYOUT |
1568                          awk '/^repaired_multiple_referenced/ { print $2 }')
1569         [ $repaired -eq 1 ] ||
1570                 error "(4) Fail to repair multiple references: $repaired"
1571
1572         echo "$DIR/$tdir/f0 and $DIR/$tdir/guard should use diff OST-objects"
1573         dd if=/dev/zero of=$DIR/$tdir/f0 bs=1M count=2 ||
1574                 error "(5) Fail to write f0."
1575         size=$(ls -l $DIR/$tdir/guard | awk '{ print $5 }')
1576         [ $size -eq 1048576 ] ||
1577                 error "(6) guard size should be 1048576, but got $size"
1578 }
1579 run_test 17 "LFSCK can repair multiple references"
1580
1581 test_18a() {
1582         [ $MDSCOUNT -lt 2 ] &&
1583                 skip "We need at least 2 MDSes for test_18a" && exit 0
1584
1585         [ $OSTCOUNT -lt 2 ] &&
1586                 skip "We need at least 2 OSTs for test_18a" && exit 0
1587
1588         echo "#####"
1589         echo "The target MDT-object is there, but related stripe information"
1590         echo "is lost or partly lost. The LFSCK should regenerate the missed"
1591         echo "layout EA entries."
1592         echo "#####"
1593
1594         echo "stopall"
1595         stopall > /dev/null
1596         echo "formatall"
1597         formatall > /dev/null
1598         echo "setupall"
1599         setupall > /dev/null
1600
1601         mkdir -p $DIR/$tdir
1602         $LFS mkdir -i 0 $DIR/$tdir/a1
1603         $LFS mkdir -i 1 $DIR/$tdir/a2
1604         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
1605         $LFS setstripe -c 2 -i 1 -s 1M $DIR/$tdir/a2
1606         dd if=/dev/zero of=$DIR/$tdir/a1/f1 bs=1M count=2
1607         dd if=/dev/zero of=$DIR/$tdir/a2/f2 bs=1M count=2
1608
1609         local saved_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1610
1611         $LFS path2fid $DIR/$tdir/a1/f1
1612         $LFS getstripe $DIR/$tdir/a1/f1
1613         $LFS path2fid $DIR/$tdir/a2/f2
1614         $LFS getstripe $DIR/$tdir/a2/f2
1615         sync
1616         cancel_lru_locks osc
1617
1618         echo "Inject failure, to make the MDT-object lost its layout EA"
1619         #define OBD_FAIL_LFSCK_LOST_STRIPE 0x1615
1620         do_facet mds1 $LCTL set_param fail_loc=0x1615
1621         chown 1.1 $DIR/$tdir/a1/f1
1622         do_facet mds2 $LCTL set_param fail_loc=0x1615
1623         chown 1.1 $DIR/$tdir/a2/f2
1624         sync
1625         sleep 2
1626         do_facet mds1 $LCTL set_param fail_loc=0
1627         do_facet mds2 $LCTL set_param fail_loc=0
1628
1629         echo "stopall to cleanup object cache"
1630         stopall > /dev/null
1631         echo "setupall"
1632         setupall > /dev/null
1633
1634         echo "The file size should be incorrect since layout EA is lost"
1635         local cur_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1636         [ "$cur_size" != "$saved_size" ] ||
1637                 error "(1) Expect incorrect file1 size"
1638
1639         cur_size=$(ls -il $DIR/$tdir/a2/f2 | awk '{ print $6 }')
1640         [ "$cur_size" != "$saved_size" ] ||
1641                 error "(2) Expect incorrect file2 size"
1642
1643         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
1644         $START_LAYOUT -o || error "(3) Fail to start LFSCK for layout!"
1645
1646         for k in $(seq $MDSCOUNT); do
1647                 # The LFSCK status query internal is 30 seconds. For the case
1648                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1649                 # time to guarantee the status sync up.
1650                 wait_update_facet mds${k} "$LCTL get_param -n \
1651                         mdd.$(facet_svc mds${k}).lfsck_layout |
1652                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1653                         error "(4) MDS${k} is not the expected 'completed'"
1654         done
1655
1656         for k in $(seq $OSTCOUNT); do
1657                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
1658                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1659                                 awk '/^status/ { print $2 }')
1660                 [ "$cur_status" == "completed" ] ||
1661                 error "(5) OST${k} Expect 'completed', but got '$cur_status'"
1662         done
1663
1664         for k in 1 2; do
1665                 local repaired=$(do_facet mds${k} $LCTL get_param -n \
1666                                  mdd.$(facet_svc mds${k}).lfsck_layout |
1667                                  awk '/^repaired_orphan/ { print $2 }')
1668                 [ $repaired -eq ${k} ] ||
1669                 error "(6) Expect ${k} fixed on mds${k}, but got: $repaired"
1670         done
1671
1672         $LFS path2fid $DIR/$tdir/a1/f1
1673         $LFS getstripe $DIR/$tdir/a1/f1
1674         $LFS path2fid $DIR/$tdir/a2/f2
1675         $LFS getstripe $DIR/$tdir/a2/f2
1676
1677         echo "The file size should be correct after layout LFSCK scanning"
1678         cur_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1679         [ "$cur_size" == "$saved_size" ] ||
1680                 error "(7) Expect file1 size $saved_size, but got $cur_size"
1681
1682         cur_size=$(ls -il $DIR/$tdir/a2/f2 | awk '{ print $6 }')
1683         [ "$cur_size" == "$saved_size" ] ||
1684                 error "(8) Expect file2 size $saved_size, but got $cur_size"
1685 }
1686 run_test 18a "Find out orphan OST-object and repair it (1)"
1687
1688 test_18b() {
1689         [ $MDSCOUNT -lt 2 ] &&
1690                 skip "We need at least 2 MDSes for test_18b" && exit 0
1691
1692         [ $OSTCOUNT -lt 2 ] &&
1693                 skip "We need at least 2 OSTs for test_18b" && exit 0
1694
1695         echo "#####"
1696         echo "The target MDT-object is lost. The LFSCK should re-create the"
1697         echo "MDT-object under .lustre/lost+found/MDTxxxx. The admin should"
1698         echo "can move it back to normal namespace manually."
1699         echo "#####"
1700
1701         echo "stopall"
1702         stopall > /dev/null
1703         echo "formatall"
1704         formatall > /dev/null
1705         echo "setupall"
1706         setupall > /dev/null
1707
1708         mkdir -p $DIR/$tdir
1709         $LFS mkdir -i 0 $DIR/$tdir/a1
1710         $LFS mkdir -i 1 $DIR/$tdir/a2
1711         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
1712         $LFS setstripe -c 2 -i 1 -s 1M $DIR/$tdir/a2
1713         dd if=/dev/zero of=$DIR/$tdir/a1/f1 bs=1M count=2
1714         dd if=/dev/zero of=$DIR/$tdir/a2/f2 bs=1M count=2
1715         local saved_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1716         local fid1=$($LFS path2fid $DIR/$tdir/a1/f1)
1717         echo ${fid1}
1718         $LFS getstripe $DIR/$tdir/a1/f1
1719         local fid2=$($LFS path2fid $DIR/$tdir/a2/f2)
1720         echo ${fid2}
1721         $LFS getstripe $DIR/$tdir/a2/f2
1722         sync
1723         cancel_lru_locks osc
1724
1725         echo "Inject failure, to simulate the case of missing the MDT-object"
1726         #define OBD_FAIL_LFSCK_LOST_MDTOBJ      0x1616
1727         do_facet mds1 $LCTL set_param fail_loc=0x1616
1728         rm -f $DIR/$tdir/a1/f1
1729         do_facet mds2 $LCTL set_param fail_loc=0x1616
1730         rm -f $DIR/$tdir/a2/f2
1731         sync
1732         sleep 2
1733         do_facet mds1 $LCTL set_param fail_loc=0
1734         do_facet mds2 $LCTL set_param fail_loc=0
1735
1736         echo "stopall to cleanup object cache"
1737         stopall > /dev/null
1738         echo "setupall"
1739         setupall > /dev/null
1740
1741         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
1742         $START_LAYOUT -o || error "(1) Fail to start LFSCK for layout!"
1743
1744         for k in $(seq $MDSCOUNT); do
1745                 # The LFSCK status query internal is 30 seconds. For the case
1746                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1747                 # time to guarantee the status sync up.
1748                 wait_update_facet mds${k} "$LCTL get_param -n \
1749                         mdd.$(facet_svc mds${k}).lfsck_layout |
1750                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1751                         error "(2) MDS${k} is not the expected 'completed'"
1752         done
1753
1754         for k in $(seq $OSTCOUNT); do
1755                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
1756                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1757                                 awk '/^status/ { print $2 }')
1758                 [ "$cur_status" == "completed" ] ||
1759                 error "(3) OST${k} Expect 'completed', but got '$cur_status'"
1760         done
1761
1762         for k in 1 2; do
1763                 local repaired=$(do_facet mds${k} $LCTL get_param -n \
1764                                  mdd.$(facet_svc mds${k}).lfsck_layout |
1765                                  awk '/^repaired_orphan/ { print $2 }')
1766                 [ $repaired -eq ${k} ] ||
1767                 error "(4) Expect ${k} fixed on mds${k}, but got: $repaired"
1768         done
1769
1770         echo "Move the files from ./lustre/lost+found/MDTxxxx to namespace"
1771         mv $MOUNT/.lustre/lost+found/MDT0000/R-${fid1} $DIR/$tdir/a1/f1 ||
1772         error "(5) Fail to move $MOUNT/.lustre/lost+found/MDT0000/R-${fid1}"
1773
1774         mv $MOUNT/.lustre/lost+found/MDT0001/R-${fid2} $DIR/$tdir/a2/f2 ||
1775         error "(6) Fail to move $MOUNT/.lustre/lost+found/MDT0001/R-${fid2}"
1776
1777         $LFS path2fid $DIR/$tdir/a1/f1
1778         $LFS getstripe $DIR/$tdir/a1/f1
1779         $LFS path2fid $DIR/$tdir/a2/f2
1780         $LFS getstripe $DIR/$tdir/a2/f2
1781
1782         echo "The file size should be correct after layout LFSCK scanning"
1783         local cur_size=$(ls -il $DIR/$tdir/a1/f1 | awk '{ print $6 }')
1784         [ "$cur_size" == "$saved_size" ] ||
1785                 error "(7) Expect file1 size $saved_size, but got $cur_size"
1786
1787         cur_size=$(ls -il $DIR/$tdir/a2/f2 | awk '{ print $6 }')
1788         [ "$cur_size" == "$saved_size" ] ||
1789                 error "(8) Expect file2 size $saved_size, but got $cur_size"
1790 }
1791 run_test 18b "Find out orphan OST-object and repair it (2)"
1792
1793 test_18c() {
1794         [ $MDSCOUNT -lt 2 ] &&
1795                 skip "We need at least 2 MDSes for test_18c" && exit 0
1796
1797         [ $OSTCOUNT -lt 2 ] &&
1798                 skip "We need at least 2 OSTs for test_18c" && exit 0
1799
1800         echo "#####"
1801         echo "The target MDT-object is lost, and the OST-object FID is missing."
1802         echo "The LFSCK should re-create the MDT-object with new FID under the "
1803         echo "directory .lustre/lost+found/MDTxxxx."
1804         echo "#####"
1805
1806         echo "stopall"
1807         stopall > /dev/null
1808         echo "formatall"
1809         formatall > /dev/null
1810         echo "setupall"
1811         setupall > /dev/null
1812
1813         mkdir -p $DIR/$tdir
1814         $LFS mkdir -i 0 $DIR/$tdir/a1
1815         $LFS mkdir -i 1 $DIR/$tdir/a2
1816         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
1817         $LFS setstripe -c 2 -i 1 -s 1M $DIR/$tdir/a2
1818
1819         echo "Inject failure, to simulate the case of missing parent FID"
1820         #define OBD_FAIL_LFSCK_NOPFID           0x1617
1821         do_facet ost1 $LCTL set_param fail_loc=0x1617
1822         do_facet ost2 $LCTL set_param fail_loc=0x1617
1823
1824         dd if=/dev/zero of=$DIR/$tdir/a1/f1 bs=1M count=2
1825         dd if=/dev/zero of=$DIR/$tdir/a2/f2 bs=1M count=2
1826         $LFS getstripe $DIR/$tdir/a1/f1
1827         $LFS getstripe $DIR/$tdir/a2/f2
1828         sync
1829         cancel_lru_locks osc
1830
1831         echo "Inject failure, to simulate the case of missing the MDT-object"
1832         #define OBD_FAIL_LFSCK_LOST_MDTOBJ      0x1616
1833         do_facet mds1 $LCTL set_param fail_loc=0x1616
1834         rm -f $DIR/$tdir/a1/f1
1835         do_facet mds2 $LCTL set_param fail_loc=0x1616
1836         rm -f $DIR/$tdir/a2/f2
1837         sync
1838         sleep 2
1839         do_facet mds1 $LCTL set_param fail_loc=0
1840         do_facet mds2 $LCTL set_param fail_loc=0
1841
1842         echo "stopall to cleanup object cache"
1843         stopall > /dev/null
1844         echo "setupall"
1845         setupall > /dev/null
1846
1847         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
1848         $START_LAYOUT -o || error "(1) Fail to start LFSCK for layout!"
1849
1850         for k in $(seq $MDSCOUNT); do
1851                 # The LFSCK status query internal is 30 seconds. For the case
1852                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1853                 # time to guarantee the status sync up.
1854                 wait_update_facet mds${k} "$LCTL get_param -n \
1855                         mdd.$(facet_svc mds${k}).lfsck_layout |
1856                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1857                         error "(2) MDS${k} is not the expected 'completed'"
1858         done
1859
1860         for k in $(seq $OSTCOUNT); do
1861                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
1862                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1863                                 awk '/^status/ { print $2 }')
1864                 [ "$cur_status" == "completed" ] ||
1865                 error "(3) OST${k} Expect 'completed', but got '$cur_status'"
1866         done
1867
1868         local repaired=$(do_facet mds1 $LCTL get_param -n \
1869                          mdd.$(facet_svc mds1).lfsck_layout |
1870                          awk '/^repaired_orphan/ { print $2 }')
1871         [ $repaired -eq 3 ] ||
1872                 error "(4) Expect 3 fixed on mds1, but got: $repaired"
1873
1874         repaired=$(do_facet mds2 $LCTL get_param -n \
1875                    mdd.$(facet_svc mds2).lfsck_layout |
1876                    awk '/^repaired_orphan/ { print $2 }')
1877         [ $repaired -eq 0 ] ||
1878                 error "(5) Expect 0 fixed on mds2, but got: $repaired"
1879
1880         echo "There should be some stub under .lustre/lost+found/MDT0001/"
1881         ls -ail $MOUNT/.lustre/lost+found/MDT0001/N-* &&
1882                 error "(6) .lustre/lost+found/MDT0001/ should be empty"
1883
1884         echo "There should be some stub under .lustre/lost+found/MDT0000/"
1885         ls -ail $MOUNT/.lustre/lost+found/MDT0000/N-* ||
1886                 error "(7) .lustre/lost+found/MDT0000/ should not be empty"
1887 }
1888 run_test 18c "Find out orphan OST-object and repair it (3)"
1889
1890 test_18d() {
1891         echo "#####"
1892         echo "The target MDT-object layout EA slot is occpuied by some new"
1893         echo "created OST-object when repair dangling reference case. Such"
1894         echo "conflict OST-object has never been modified. Then when found"
1895         echo "the orphan OST-object, LFSCK will replace it with the orphan"
1896         echo "OST-object."
1897         echo "#####"
1898
1899         echo "stopall"
1900         stopall > /dev/null
1901         echo "formatall"
1902         formatall > /dev/null
1903         echo "setupall"
1904         setupall > /dev/null
1905
1906         mkdir -p $DIR/$tdir/a1
1907         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
1908         echo "guard" > $DIR/$tdir/a1/f1
1909         echo "foo" > $DIR/$tdir/a1/f2
1910         local saved_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
1911         $LFS path2fid $DIR/$tdir/a1/f1
1912         $LFS getstripe $DIR/$tdir/a1/f1
1913         $LFS path2fid $DIR/$tdir/a1/f2
1914         $LFS getstripe $DIR/$tdir/a1/f2
1915         sync
1916         cancel_lru_locks osc
1917
1918         echo "Inject failure to make $DIR/$tdir/a1/f1 and $DIR/$tdir/a1/f2"
1919         echo "to reference the same OST-object (which is f1's OST-obejct)."
1920         echo "Then drop $DIR/$tdir/a1/f1 and its OST-object, so f2 becomes"
1921         echo "dangling reference case, but f2's old OST-object is there."
1922         echo
1923
1924         #define OBD_FAIL_LFSCK_CHANGE_STRIPE    0x1618
1925         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1618
1926         chown 1.1 $DIR/$tdir/a1/f2
1927         rm -f $DIR/$tdir/a1/f1
1928         sync
1929         sleep 2
1930         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
1931
1932         echo "stopall to cleanup object cache"
1933         stopall > /dev/null
1934         echo "setupall"
1935         setupall > /dev/null
1936
1937         echo "The file size should be incorrect since dangling referenced"
1938         local cur_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
1939         [ "$cur_size" != "$saved_size" ] ||
1940                 error "(1) Expect incorrect file2 size"
1941
1942         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
1943         $START_LAYOUT -o || error "(2) Fail to start LFSCK for layout!"
1944
1945         for k in $(seq $MDSCOUNT); do
1946                 # The LFSCK status query internal is 30 seconds. For the case
1947                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
1948                 # time to guarantee the status sync up.
1949                 wait_update_facet mds${k} "$LCTL get_param -n \
1950                         mdd.$(facet_svc mds${k}).lfsck_layout |
1951                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
1952                         error "(3) MDS${k} is not the expected 'completed'"
1953         done
1954
1955         for k in $(seq $OSTCOUNT); do
1956                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
1957                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
1958                                 awk '/^status/ { print $2 }')
1959                 [ "$cur_status" == "completed" ] ||
1960                 error "(4) OST${k} Expect 'completed', but got '$cur_status'"
1961         done
1962
1963         local repaired=$(do_facet $SINGLEMDS $LCTL get_param -n \
1964                          mdd.$(facet_svc $SINGLEMDS).lfsck_layout |
1965                          awk '/^repaired_orphan/ { print $2 }')
1966         [ $repaired -eq 1 ] ||
1967                 error "(5) Expect 1 orphan has been fixed, but got: $repaired"
1968
1969         echo "The file size should be correct after layout LFSCK scanning"
1970         cur_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
1971         [ "$cur_size" == "$saved_size" ] ||
1972                 error "(6) Expect file2 size $saved_size, but got $cur_size"
1973
1974         echo "There should be some stub under .lustre/lost+found/MDT0000/"
1975         ls -ail $MOUNT/.lustre/lost+found/MDT0000/ &&
1976                 error "(7) .lustre/lost+found/MDT0000/ should be empty"
1977
1978         echo "The LFSCK should find back the original data."
1979         cat $DIR/$tdir/a1/f2
1980         $LFS path2fid $DIR/$tdir/a1/f2
1981         $LFS getstripe $DIR/$tdir/a1/f2
1982 }
1983 run_test 18d "Find out orphan OST-object and repair it (4)"
1984
1985 test_18e() {
1986         echo "#####"
1987         echo "The target MDT-object layout EA slot is occpuied by some new"
1988         echo "created OST-object when repair dangling reference case. Such"
1989         echo "conflict OST-object has been modified by others. To keep the"
1990         echo "new data, the LFSCK will create a new file to refernece this"
1991         echo "old orphan OST-object."
1992         echo "#####"
1993
1994         echo "stopall"
1995         stopall > /dev/null
1996         echo "formatall"
1997         formatall > /dev/null
1998         echo "setupall"
1999         setupall > /dev/null
2000
2001         mkdir -p $DIR/$tdir/a1
2002         $LFS setstripe -c 1 -i 0 -s 1M $DIR/$tdir/a1
2003         echo "guard" > $DIR/$tdir/a1/f1
2004         echo "foo" > $DIR/$tdir/a1/f2
2005         local saved_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
2006         $LFS path2fid $DIR/$tdir/a1/f1
2007         $LFS getstripe $DIR/$tdir/a1/f1
2008         $LFS path2fid $DIR/$tdir/a1/f2
2009         $LFS getstripe $DIR/$tdir/a1/f2
2010         sync
2011         cancel_lru_locks osc
2012
2013         echo "Inject failure to make $DIR/$tdir/a1/f1 and $DIR/$tdir/a1/f2"
2014         echo "to reference the same OST-object (which is f1's OST-obejct)."
2015         echo "Then drop $DIR/$tdir/a1/f1 and its OST-object, so f2 becomes"
2016         echo "dangling reference case, but f2's old OST-object is there."
2017         echo
2018
2019         #define OBD_FAIL_LFSCK_CHANGE_STRIPE    0x1618
2020         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1618
2021         chown 1.1 $DIR/$tdir/a1/f2
2022         rm -f $DIR/$tdir/a1/f1
2023         sync
2024         sleep 2
2025         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
2026
2027         echo "stopall to cleanup object cache"
2028         stopall > /dev/null
2029         echo "setupall"
2030         setupall > /dev/null
2031
2032         echo "The file size should be incorrect since dangling referenced"
2033         local cur_size=$(ls -il $DIR/$tdir/a1/f2 | awk '{ print $6 }')
2034         [ "$cur_size" != "$saved_size" ] ||
2035                 error "(1) Expect incorrect file2 size"
2036
2037         #define OBD_FAIL_LFSCK_DELAY3           0x1602
2038         do_facet $SINGLEMDS $LCTL set_param fail_val=10
2039         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1602
2040
2041         echo "Trigger layout LFSCK on all devices to find out orphan OST-object"
2042         $START_LAYOUT -o || error "(2) Fail to start LFSCK for layout!"
2043
2044         wait_update_facet mds1 "$LCTL get_param -n \
2045                 mdd.$(facet_svc mds1).lfsck_layout |
2046                 awk '/^status/ { print \\\$2 }'" "scanning-phase2" 6 ||
2047                 error "(3) MDS1 is not the expected 'scanning-phase2'"
2048
2049         # to guarantee all updates are synced.
2050         sync
2051         sleep 2
2052
2053         echo "Write new data to f2 to modify the new created OST-object."
2054         echo "dummy" >> $DIR/$tdir/a1/f2
2055
2056         do_facet $SINGLEMDS $LCTL set_param fail_val=0
2057         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
2058
2059         for k in $(seq $MDSCOUNT); do
2060                 # The LFSCK status query internal is 30 seconds. For the case
2061                 # of some LFSCK_NOTIFY RPCs failure/lost, we will wait enough
2062                 # time to guarantee the status sync up.
2063                 wait_update_facet mds${k} "$LCTL get_param -n \
2064                         mdd.$(facet_svc mds${k}).lfsck_layout |
2065                         awk '/^status/ { print \\\$2 }'" "completed" 32 ||
2066                         error "(4) MDS${k} is not the expected 'completed'"
2067         done
2068
2069         for k in $(seq $OSTCOUNT); do
2070                 local cur_status=$(do_facet ost${k} $LCTL get_param -n \
2071                                 obdfilter.$(facet_svc ost${k}).lfsck_layout |
2072                                 awk '/^status/ { print $2 }')
2073                 [ "$cur_status" == "completed" ] ||
2074                 error "(5) OST${k} Expect 'completed', but got '$cur_status'"
2075         done
2076
2077         local repaired=$(do_facet $SINGLEMDS $LCTL get_param -n \
2078                          mdd.$(facet_svc $SINGLEMDS).lfsck_layout |
2079                          awk '/^repaired_orphan/ { print $2 }')
2080         [ $repaired -eq 1 ] ||
2081                 error "(6) Expect 1 orphan has been fixed, but got: $repaired"
2082
2083         echo "There should be stub file under .lustre/lost+found/MDT0000/"
2084         local cname=$(ls $MOUNT/.lustre/lost+found/MDT0000/C-*)
2085         [ ! -z $name ] ||
2086                 error "(7) .lustre/lost+found/MDT0000/ should not be empty"
2087
2088         echo "The stub file should keep the original f2 data"
2089         cur_size=$(ls -il $cname | awk '{ print $6 }')
2090         [ "$cur_size" == "$saved_size" ] ||
2091                 error "(8) Expect file2 size $saved_size, but got $cur_size"
2092
2093         cat $cname
2094         $LFS path2fid $cname
2095         $LFS getstripe $cname
2096
2097         echo "The f2 should contains new data."
2098         cat $DIR/$tdir/a1/f2
2099         $LFS path2fid $DIR/$tdir/a1/f2
2100         $LFS getstripe $DIR/$tdir/a1/f2
2101 }
2102 run_test 18e "Find out orphan OST-object and repair it (5)"
2103
2104 test_19a() {
2105         echo "stopall"
2106         stopall > /dev/null
2107         echo "formatall"
2108         formatall > /dev/null
2109         echo "setupall"
2110         setupall > /dev/null
2111
2112         mkdir -p $DIR/$tdir
2113         $LFS setstripe -c 1 -i 0 $DIR/$tdir
2114
2115         echo "foo" > $DIR/$tdir/a0
2116         echo "guard" > $DIR/$tdir/a1
2117
2118         cancel_lru_locks osc
2119         umount_client $MOUNT || error "(1) Fail to stop client!"
2120         mount_client $MOUNT || error "(2) Fail to start client!"
2121
2122         echo "Inject failure, then client will offer wrong parent FID when read"
2123         do_facet ost1 $LCTL set_param -n \
2124                 obdfilter.${FSNAME}-OST0000.lfsck_verify_pfid 1
2125         #define OBD_FAIL_LFSCK_INVALID_PFID     0x1619
2126         $LCTL set_param fail_loc=0x1619
2127
2128         echo "Read RPC with wrong parent FID should be denied"
2129         cat $DIR/$tdir/a0 && error "(3) Read should be denied!"
2130         $LCTL set_param fail_loc=0
2131 }
2132 run_test 19a "OST-object inconsistency self detect"
2133
2134 test_19b() {
2135         echo "stopall"
2136         stopall > /dev/null
2137         echo "formatall"
2138         formatall > /dev/null
2139         echo "setupall"
2140         setupall > /dev/null
2141
2142         mkdir -p $DIR/$tdir
2143         $LFS setstripe -c 1 -i 0 $DIR/$tdir
2144
2145         echo "Inject failure stub to make the OST-object to back point to"
2146         echo "non-exist MDT-object"
2147
2148         #define OBD_FAIL_LFSCK_UNMATCHED_PAIR1  0x1611
2149         do_facet ost1 $LCTL set_param fail_loc=0x1611
2150         echo "foo" > $DIR/$tdir/f0
2151         cancel_lru_locks osc
2152         sync
2153         sleep 2
2154         do_facet ost1 $LCTL set_param fail_loc=0
2155
2156         echo "Nothing should be fixed since self detect and repair is disabled"
2157         local repaired=$(do_facet ost1 $LCTL get_param -n \
2158                         obdfilter.${FSNAME}-OST0000.lfsck_verify_pfid |
2159                         awk '/^repaired/ { print $2 }')
2160         [ $repaired -eq 0 ] ||
2161                 error "(1) Expected 0 repaired, but got $repaired"
2162
2163         echo "Read RPC with right parent FID should be accepted,"
2164         echo "and cause parent FID on OST to be fixed"
2165
2166         do_facet ost1 $LCTL set_param -n \
2167                 obdfilter.${FSNAME}-OST0000.lfsck_verify_pfid 1
2168         cat $DIR/$tdir/f0 || error "(2) Read should not be denied!"
2169
2170         repaired=$(do_facet ost1 $LCTL get_param -n \
2171                 obdfilter.${FSNAME}-OST0000.lfsck_verify_pfid |
2172                 awk '/^repaired/ { print $2 }')
2173         [ $repaired -eq 1 ] ||
2174                 error "(3) Expected 1 repaired, but got $repaired"
2175 }
2176 run_test 19b "OST-object inconsistency self repair"
2177
2178 $LCTL set_param debug=-lfsck > /dev/null || true
2179
2180 # restore MDS/OST size
2181 MDSSIZE=${SAVED_MDSSIZE}
2182 OSTSIZE=${SAVED_OSTSIZE}
2183
2184 # cleanup the system at last
2185 formatall
2186
2187 complete $SECONDS
2188 exit_status