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