Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_scrub.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, 2017, Intel Corporation.
24  */
25 /*
26  * lustre/osd-ldiskfs/osd_scrub.c
27  *
28  * Top-level entry points into osd module
29  *
30  * The OI scrub is used for rebuilding Object Index files when restores MDT from
31  * file-level backup.
32  *
33  * The otable based iterator scans ldiskfs inode table to feed up layer LFSCK.
34  *
35  * Author: Fan Yong <yong.fan@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LFSCK
39
40 #include <linux/kthread.h>
41 #include <uapi/linux/lustre/lustre_idl.h>
42 #include <lustre_disk.h>
43 #include <dt_object.h>
44 #include <linux/xattr.h>
45 #include <lustre_scrub.h>
46 #include <lustre_nodemap.h>
47
48 #include "osd_internal.h"
49 #include "osd_oi.h"
50 #include "osd_scrub.h"
51
52 #define OSD_OTABLE_MAX_HASH             0x00000000ffffffffULL
53
54 /* high priority inconsistent items list APIs */
55 #define SCRUB_BAD_OIMAP_DECAY_INTERVAL  60
56
57 /**
58  * Add mapping into scrub.os_inconsistent_item list, and the OI scrub thread
59  * will fix them in priority.
60  */
61 int osd_scrub_oi_insert(struct osd_device *dev, const struct lu_fid *fid,
62                         struct osd_inode_id *id, int insert)
63 {
64         struct osd_inconsistent_item *oii;
65         struct osd_scrub *oscrub = &dev->od_scrub;
66         struct lustre_scrub *lscrub = &oscrub->os_scrub;
67         int wakeup = 0;
68
69         ENTRY;
70
71         OBD_ALLOC_PTR(oii);
72         if (unlikely(oii == NULL))
73                 RETURN(-ENOMEM);
74
75         INIT_LIST_HEAD(&oii->oii_list);
76         oii->oii_cache.oic_fid = *fid;
77         oii->oii_cache.oic_lid = *id;
78         oii->oii_cache.oic_dev = dev;
79         oii->oii_insert = insert;
80
81         spin_lock(&lscrub->os_lock);
82         if (lscrub->os_partial_scan) {
83                 __u64 now = ktime_get_real_seconds();
84
85                 /* If there haven't been errors in a long time,
86                  * decay old count until either the errors are
87                  * gone or we reach the current interval.
88                  */
89                 while (unlikely(oscrub->os_bad_oimap_count > 0 &&
90                                 oscrub->os_bad_oimap_time +
91                                 SCRUB_BAD_OIMAP_DECAY_INTERVAL < now)) {
92                         oscrub->os_bad_oimap_count >>= 1;
93                         oscrub->os_bad_oimap_time +=
94                                 SCRUB_BAD_OIMAP_DECAY_INTERVAL;
95                 }
96
97                 oscrub->os_bad_oimap_time = now;
98                 if (++oscrub->os_bad_oimap_count >
99                     dev->od_full_scrub_threshold_rate)
100                         lscrub->os_full_scrub = 1;
101         }
102
103         if (list_empty(&lscrub->os_inconsistent_items)) {
104                 wakeup = 1;
105         } else {
106                 struct osd_inconsistent_item *tmp;
107
108                 list_for_each_entry(tmp, &lscrub->os_inconsistent_items,
109                                     oii_list) {
110                         if (lu_fid_eq(fid, &tmp->oii_cache.oic_fid)) {
111                                 spin_unlock(&lscrub->os_lock);
112                                 OBD_FREE_PTR(oii);
113                                 RETURN(0);
114                         }
115                 }
116         }
117
118         list_add_tail(&oii->oii_list, &lscrub->os_inconsistent_items);
119         spin_unlock(&lscrub->os_lock);
120
121         if (wakeup)
122                 wake_up_var(lscrub);
123
124         RETURN(0);
125 }
126
127 /* if item could not be repaired, add it to the os_stale_items list to avoid
128  * triggering scrub repeatedly.
129  */
130 static inline void osd_scrub_oi_mark_stale(struct lustre_scrub *scrub,
131                                            struct osd_inconsistent_item *oii)
132 {
133         spin_lock(&scrub->os_lock);
134         list_move_tail(&oii->oii_list, &scrub->os_stale_items);
135         spin_unlock(&scrub->os_lock);
136 }
137
138 /* OI of \a fid may be marked stale, and if its mapping is scrubbed, remove it
139  * from os_stale_items list.
140  */
141 bool osd_scrub_oi_resurrect(struct lustre_scrub *scrub,
142                             const struct lu_fid *fid)
143 {
144         struct osd_inconsistent_item *oii;
145         bool resurrected = false;
146
147         if (list_empty(&scrub->os_stale_items))
148                 return resurrected;
149
150         spin_lock(&scrub->os_lock);
151         list_for_each_entry(oii, &scrub->os_stale_items, oii_list) {
152                 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
153                         list_del(&oii->oii_list);
154                         OBD_FREE_PTR(oii);
155                         resurrected = true;
156                         break;
157                 }
158         }
159         spin_unlock(&scrub->os_lock);
160
161         return resurrected;
162 }
163
164 static void osd_scrub_ois_fini(struct lustre_scrub *scrub,
165                                struct list_head *list)
166 {
167         struct osd_inconsistent_item *oii;
168         struct osd_inconsistent_item *tmp;
169
170         spin_lock(&scrub->os_lock);
171         list_for_each_entry_safe(oii, tmp, list, oii_list) {
172                 list_del(&oii->oii_list);
173                 OBD_FREE_PTR(oii);
174         }
175         spin_unlock(&scrub->os_lock);
176 }
177
178 static inline int osd_scrub_has_window(struct lustre_scrub *scrub,
179                                        struct osd_otable_cache *ooc)
180 {
181         return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
182 }
183
184 /**
185  * update/insert/delete the specified OI mapping (@fid @id) according to the ops
186  *
187  * \retval   1, changed nothing
188  * \retval   0, changed successfully
189  * \retval -ve, on error
190  */
191 int osd_scrub_refresh_mapping(struct osd_thread_info *info,
192                               struct osd_device *dev,
193                               const struct lu_fid *fid,
194                               const struct osd_inode_id *id,
195                               int ops, bool force,
196                               enum oi_check_flags flags, bool *exist)
197 {
198         handle_t *th;
199         int       rc;
200         ENTRY;
201
202         if (dev->od_scrub.os_scrub.os_file.sf_param & SP_DRYRUN && !force)
203                 RETURN(0);
204
205         /* DTO_INDEX_INSERT is enough for other two ops:
206          * delete/update, but save stack. */
207         th = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC,
208                                 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
209         if (IS_ERR(th)) {
210                 rc = PTR_ERR(th);
211                 CWARN("%s: fail to start trans for scrub op %d "
212                       DFID" => %u/%u: rc = %d\n", osd_name(dev), ops,
213                       PFID(fid), id ? id->oii_ino : -1, id ? id->oii_gen : -1,
214                       rc);
215                 RETURN(rc);
216         }
217
218         switch (ops) {
219         case DTO_INDEX_UPDATE:
220                 rc = osd_oi_update(info, dev, fid, id, th, flags);
221                 if (unlikely(rc == -ENOENT)) {
222                         /* Some unlink thread may removed the OI mapping. */
223                         rc = 1;
224                 }
225                 break;
226         case DTO_INDEX_INSERT:
227                 rc = osd_oi_insert(info, dev, fid, id, th, flags, exist);
228                 if (unlikely(rc == -EEXIST)) {
229                         rc = 1;
230                         /* XXX: There are trouble things when adding OI
231                          *      mapping for IGIF object, which may cause
232                          *      multiple objects to be mapped to the same
233                          *      IGIF formatted FID. Consider the following
234                          *      situations:
235                          *
236                          *      1) The MDT is upgrading from 1.8 device.
237                          *      The OI scrub generates IGIF FID1 for the
238                          *      OBJ1 and adds the OI mapping.
239                          *
240                          *      2) For some reason, the OI scrub does not
241                          *      process all the IGIF objects completely.
242                          *
243                          *      3) The MDT is backuped and restored against
244                          *      this device.
245                          *
246                          *      4) When the MDT mounts up, the OI scrub will
247                          *      try to rebuild the OI files. For some IGIF
248                          *      object, OBJ2, which was not processed by the
249                          *      OI scrub before the backup/restore, and the
250                          *      new generated IGIF formatted FID may be just
251                          *      the FID1, the same as OBJ1.
252                          *
253                          *      Under such case, the OI scrub cannot know how
254                          *      to generate new FID for the OBJ2.
255                          *
256                          *      Currently, we do nothing for that. One possible
257                          *      solution is to generate new normal FID for the
258                          *      conflict object.
259                          *
260                          *      Anyway, it is rare, only exists in theory. */
261                 }
262                 break;
263         case DTO_INDEX_DELETE:
264                 rc = osd_oi_delete(info, dev, fid, th, flags);
265                 if (rc == -ENOENT) {
266                         /* It is normal that the unlink thread has removed the
267                          * OI mapping already. */
268                         rc = 1;
269                 }
270                 break;
271         default:
272                 LASSERTF(0, "Unexpected ops %d\n", ops);
273                 break;
274         }
275
276         ldiskfs_journal_stop(th);
277         if (rc < 0)
278                 CDEBUG(D_LFSCK, "%s: fail to refresh OI map for scrub op %d "
279                        DFID" => %u/%u: rc = %d\n", osd_name(dev), ops,
280                        PFID(fid), id ? id->oii_ino : -1, id ? id->oii_gen : -1,
281                        rc);
282
283         RETURN(rc);
284 }
285
286 static int
287 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
288                        struct osd_idmap_cache *oic, int val)
289 {
290         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
291         struct scrub_file *sf = &scrub->os_file;
292         struct lu_fid *fid = &oic->oic_fid;
293         struct osd_inode_id *lid = &oic->oic_lid;
294         struct osd_inode_id *lid2 = &info->oti_id;
295         struct osd_inconsistent_item *oii = NULL;
296         struct inode *inode = NULL;
297         int ops = DTO_INDEX_UPDATE;
298         bool exist = false;
299         bool bad_inode = false;
300         int flags = 0;
301         int rc;
302
303         ENTRY;
304         down_write(&scrub->os_rwsem);
305         /* remove IDIF support to simplify logic */
306         if (val == SCRUB_NEXT_OSTOBJ_OLD)
307                 GOTO(out, rc = -EOPNOTSUPP);
308
309         if (val == SCRUB_NEXT_OSTOBJ)
310                 flags = OI_KNOWN_ON_OST;
311
312         scrub->os_new_checked++;
313         if (val < 0)
314                 GOTO(out, rc = val);
315
316         if (scrub->os_in_prior) {
317                 oii = list_entry(oic, struct osd_inconsistent_item,
318                                  oii_cache);
319                 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_STALE))
320                         GOTO(out, rc = -ESTALE);
321         }
322
323         if (lid->oii_ino < sf->sf_pos_latest_start && !oii)
324                 GOTO(skip, rc = 0);
325         if (lid->oii_ino < LDISKFS_FIRST_INO(osd_sb(dev)))
326                 GOTO(out, rc = -ENOENT);
327
328         if (fid_is_igif(fid))
329                 sf->sf_items_igif++;
330
331         /* verify inode */
332         inode = osd_iget(info, dev, lid);
333         if (IS_ERR(inode)) {
334                 rc = PTR_ERR(inode);
335                 /* someone removed the inode. */
336                 if (rc == -ENOENT || rc == -ESTALE)
337                         bad_inode = true;
338                 else
339                         GOTO(out, rc);
340         } else if (val == SCRUB_NEXT_NOLMA) {
341                 if (!scrub->os_convert_igif ||
342                     CFS_FAIL_CHECK(OBD_FAIL_FID_NOLMA))
343                         GOTO(out, rc = 0);
344
345                 /* set LMA if missing */
346                 sf->sf_flags |= SF_UPGRADE;
347                 if (!(sf->sf_param & SP_DRYRUN)) {
348                         rc = osd_ea_fid_set(info, inode, fid, 0, 0);
349                         if (rc)
350                                 GOTO(out, rc);
351                 }
352         }
353
354         /* checking existing mapping */
355         rc = osd_oi_lookup(info, dev, fid, lid2, flags);
356         if (rc != 0) {
357                 /* insert if mapping doesn't exist */
358                 if (rc == -ENOENT)
359                         ops = DTO_INDEX_INSERT;
360                 else if (rc != -ESTALE)
361                         GOTO(out, rc);
362
363                 if (bad_inode)
364                         GOTO(skip, rc = 0);
365
366                 if (val == SCRUB_NEXT_OSTOBJ)
367                         sf->sf_flags |= SF_INCONSISTENT;
368         } else if (osd_id_eq(lid, lid2)) {
369                 /* mapping matches */
370                 if (bad_inode) {
371                         /* delete mapping if it's stale */
372                         rc = osd_scrub_refresh_mapping(info, dev, fid, lid,
373                                 DTO_INDEX_DELETE, false, flags, NULL);
374                         CDEBUG(D_LFSCK,
375                                "%s: delete stale OI "DFID" -> %u/%u: rc = %d\n",
376                                osd_dev2name(dev), PFID(fid), lid->oii_ino,
377                                lid->oii_gen, rc);
378                 }
379                 GOTO(out, rc);
380         } else {
381                 struct inode *inode2;
382                 struct lu_fid *fid2;
383
384                 /* mapping mismatch */
385                 if (!scrub->os_partial_scan) {
386                         spin_lock(&scrub->os_lock);
387                         scrub->os_full_speed = 1;
388                         spin_unlock(&scrub->os_lock);
389                 }
390                 sf->sf_flags |= SF_INCONSISTENT;
391
392                 /* if new inode is bad, keep existing mapping */
393                 if (bad_inode)
394                         GOTO(skip, rc = 0);
395
396                 /* verify existing mapping */
397                 inode2 = osd_iget(info, dev, lid2);
398                 if (IS_ERR(inode2)) {
399                         rc = PTR_ERR(inode2);
400                         if (rc == -ENOENT || rc == -ESTALE)
401                                 goto delete;
402                         GOTO(out, rc);
403                 }
404
405                 rc = osd_get_lma(info, inode2, &info->oti_obj_dentry,
406                                  &info->oti_ost_attrs);
407                 if (rc) {
408                         iput(inode2);
409                         if (rc == -ENODATA)
410                                 goto delete;
411                         GOTO(out, rc);
412                 }
413
414                 /* if inode2 looks better, keep existing mapping */
415                 fid2 = &info->oti_ost_attrs.loa_lma.lma_self_fid;
416                 if ((rc == 0 && lu_fid_eq(fid, fid2)) &&
417                     ((inode->i_size == 0 && inode2->i_size > 0 &&
418                       inode_get_mtime_sec(inode) == inode_get_mtime_sec(inode2)) ||
419                      inode_get_mtime_sec(inode) < inode_get_mtime_sec(inode2))) {
420                         iput(inode2);
421                         GOTO(skip, rc);
422                 }
423                 iput(inode2);
424 delete:
425                 /* otherwise delete existing mapping */
426                 CDEBUG(D_LFSCK, "%s: delete stale OI "DFID" -> %u/%u\n",
427                        osd_dev2name(dev), PFID(fid), lid2->oii_ino,
428                        lid2->oii_gen);
429                 rc = osd_scrub_refresh_mapping(info, dev, fid, lid2,
430                                 DTO_INDEX_DELETE, false, flags, NULL);
431                 if (rc < 0)
432                         GOTO(out, rc);
433                 /* and then insert new one */
434                 ops = DTO_INDEX_INSERT;
435         }
436         LASSERT(ops == DTO_INDEX_INSERT || ops == DTO_INDEX_UPDATE);
437         CDEBUG(D_LFSCK, "%s: %s OI "DFID" -> %u/%u\n",
438                osd_dev2name(dev), ops == DTO_INDEX_INSERT ? "insert" : "update",
439                PFID(fid), lid->oii_ino, lid->oii_gen);
440         rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops, false, flags,
441                                        &exist);
442         if (rc == 0) {
443                 if (scrub->os_in_prior)
444                         sf->sf_items_updated_prior++;
445                 else
446                         sf->sf_items_updated++;
447
448                 if (ops == DTO_INDEX_INSERT && val == 0 && !exist) {
449                         int idx = osd_oi_fid2idx(dev, fid);
450
451                         sf->sf_flags |= SF_RECREATED;
452                         if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
453                                 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
454                 }
455         }
456         GOTO(out, rc);
457 out:
458         if (rc < 0) {
459                 sf->sf_items_failed++;
460                 if (lid->oii_ino >= LDISKFS_FIRST_INO(osd_sb(dev)) &&
461                     (sf->sf_pos_first_inconsistent == 0 ||
462                     sf->sf_pos_first_inconsistent > lid->oii_ino))
463                         sf->sf_pos_first_inconsistent = lid->oii_ino;
464         } else {
465                 if (!oii && !CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_STALE)) {
466                         if (osd_scrub_oi_resurrect(scrub, fid))
467                                 CDEBUG(D_LFSCK,
468                                        "%s: resurrect OI "DFID" -> %u/%u\n",
469                                        osd_dev2name(dev), PFID(fid),
470                                        lid->oii_ino, lid->oii_gen);
471                 } else if (oii) {
472                         /* release fixed inconsistent item */
473                         CDEBUG(D_LFSCK,
474                                "%s: inconsistent OI "DFID" -> %u/%u %s\n",
475                                osd_dev2name(dev), PFID(fid), lid->oii_ino,
476                                lid->oii_gen, bad_inode ? "deleted" : "fixed");
477                         spin_lock(&scrub->os_lock);
478                         list_del_init(&oii->oii_list);
479                         spin_unlock(&scrub->os_lock);
480
481                         OBD_FREE_PTR(oii);
482                         oii = NULL;
483                 }
484                 rc = 0;
485         }
486 skip:
487         if (oii) {
488                 /* something strange with item, moving to stale */
489                 osd_scrub_oi_mark_stale(scrub, oii);
490                 CDEBUG(D_LFSCK,
491                        "%s: fix inconsistent OI "DFID" -> %u/%u failed: %d\n",
492                        osd_dev2name(dev), PFID(fid), lid->oii_ino,
493                        lid->oii_gen, rc);
494         }
495         up_write(&scrub->os_rwsem);
496
497         if (!IS_ERR_OR_NULL(inode))
498                 iput(inode);
499
500         RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
501 }
502
503 /* iteration engine */
504
505 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
506                                    struct osd_device *dev,
507                                    struct osd_iit_param *param,
508                                    struct osd_idmap_cache **oic,
509                                    const bool noslot);
510
511 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
512                                    struct osd_device *dev,
513                                    struct osd_iit_param *param,
514                                    struct osd_idmap_cache *oic,
515                                    bool *noslot, int rc);
516
517 static int osd_iit_next(struct osd_iit_param *param, __u64 *pos)
518 {
519         __u32 offset;
520
521 again:
522         param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
523                         LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
524         if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
525                 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
526                 return SCRUB_NEXT_BREAK;
527         }
528
529         offset = param->offset++;
530         if (unlikely(*pos == param->gbase + offset && *pos != param->start)) {
531                 /* We should NOT find the same object more than once. */
532                 CERROR("%s: scan the same object multiple times at the pos: "
533                        "group = %u, base = %u, offset = %u, start = %u\n",
534                        osd_sb2name(param->sb), (__u32)param->bg, param->gbase,
535                        offset, param->start);
536                 goto again;
537         }
538
539         *pos = param->gbase + offset;
540         return 0;
541 }
542
543 /**
544  * \retval SCRUB_NEXT_OSTOBJ_OLD: FID-on-OST
545  * \retval 0: FID-on-MDT
546  */
547 static int osd_scrub_check_local_fldb(struct osd_thread_info *info,
548                                       struct osd_device *dev,
549                                       struct lu_fid *fid)
550 {
551         /* XXX: The initial OI scrub will scan the top level /O to generate
552          *      a small local FLDB according to the <seq>. If the given FID
553          *      is in the local FLDB, then it is FID-on-OST; otherwise it's
554          *      quite possible for FID-on-MDT. */
555         if (dev->od_is_ost)
556                 return SCRUB_NEXT_OSTOBJ_OLD;
557
558         return 0;
559 }
560
561 static int osd_scrub_get_fid(struct osd_thread_info *info,
562                              struct osd_device *dev, struct inode *inode,
563                              struct lu_fid *fid, bool scrub)
564 {
565         struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
566         bool has_lma = false;
567         int rc;
568
569         rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
570                          &info->oti_ost_attrs);
571         if (rc == 0) {
572                 has_lma = true;
573                 if (lma->lma_compat & LMAC_NOT_IN_OI ||
574                     lma->lma_incompat & LMAI_AGENT)
575                         return SCRUB_NEXT_CONTINUE;
576
577                 *fid = lma->lma_self_fid;
578                 if (!scrub)
579                         return 0;
580
581                 if (lma->lma_compat & LMAC_FID_ON_OST)
582                         return SCRUB_NEXT_OSTOBJ;
583
584                 if (fid_is_idif(fid))
585                         return SCRUB_NEXT_OSTOBJ_OLD;
586
587                 /* For local object. */
588                 if (fid_is_internal(fid))
589                         return 0;
590
591                 /* For external visible MDT-object with non-normal FID. */
592                 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
593                         return 0;
594
595                 /* For the object with normal FID, it may be MDT-object,
596                  * or may be 2.4 OST-object, need further distinguish.
597                  * Fall through to next section. */
598         }
599
600         if (rc == -ENODATA || rc == 0) {
601                 rc = osd_get_idif(info, inode, &info->oti_obj_dentry, fid);
602                 if (rc == 0) {
603                         if (scrub)
604                                 /* It is 2.3 or older OST-object. */
605                                 rc = SCRUB_NEXT_OSTOBJ_OLD;
606                         return rc;
607                 }
608
609                 if (rc > 0) {
610                         if (!has_lma)
611                                 /* It is FID-on-OST, but we do not know how
612                                  * to generate its FID, ignore it directly. */
613                                 rc = SCRUB_NEXT_CONTINUE;
614                         else
615                                 /* It is 2.4 or newer OST-object. */
616                                 rc = SCRUB_NEXT_OSTOBJ_OLD;
617                         return rc;
618                 }
619
620                 if (rc != -ENODATA)
621                         return rc;
622
623                 if (!has_lma) {
624                         if (dev->od_scrub.os_scrub.os_convert_igif) {
625                                 lu_igif_build(fid, inode->i_ino,
626                                               inode->i_generation);
627                                 if (scrub)
628                                         rc = SCRUB_NEXT_NOLMA;
629                                 else
630                                         rc = 0;
631                         } else {
632                                 /* It may be FID-on-OST, or may be FID for
633                                  * non-MDT0, anyway, we do not know how to
634                                  * generate its FID, ignore it directly. */
635                                 rc = SCRUB_NEXT_CONTINUE;
636                         }
637                         return rc;
638                 }
639
640                 /* For OI scrub case only: the object has LMA but has no ff
641                  * (or ff crashed). It may be MDT-object, may be OST-object
642                  * with crashed ff. The last check is local FLDB. */
643                 rc = osd_scrub_check_local_fldb(info, dev, fid);
644         }
645
646         return rc;
647 }
648
649 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
650                         struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
651                         struct super_block *sb, bool scrub)
652 {
653         struct inode *inode;
654         int           rc;
655         ENTRY;
656
657         /* Not handle the backend root object and agent parent object.
658          * They are neither visible to namespace nor have OI mappings. */
659         if (unlikely(pos == osd_sb(dev)->s_root->d_inode->i_ino ||
660                      is_remote_parent_ino(dev, pos)))
661                 RETURN(SCRUB_NEXT_CONTINUE);
662
663          /* Skip project quota inode since it is greater than s_first_ino. */
664 #ifdef HAVE_PROJECT_QUOTA
665         if (ldiskfs_has_feature_project(sb) &&
666             pos == le32_to_cpu(LDISKFS_SB(sb)->s_es->s_prj_quota_inum))
667                 RETURN(SCRUB_NEXT_CONTINUE);
668 #endif
669
670         osd_id_gen(lid, pos, OSD_OII_NOGEN);
671         inode = osd_iget(info, dev, lid);
672         if (IS_ERR(inode)) {
673                 rc = PTR_ERR(inode);
674                 /* The inode may be removed after bitmap searching, or the
675                  * file is new created without inode initialized yet.
676                  * LU-15754: After "new primitive: discard_new_inode()" change
677                  * in the kernel find_inode_fast() returns -ESTALE, but
678                  * iget_locked replaces it to the NULL and finally
679                  * ldiskfs_inode_attach_jinode() returns -ENOMEM
680                  * Let's skip an inode if -ENOMEM returned.
681                  */
682                 if (rc == -ENOENT || rc == -ESTALE || rc == -ENOMEM)
683                         RETURN(SCRUB_NEXT_CONTINUE);
684
685                 CDEBUG(D_LFSCK, "%s: fail to read inode, ino# = %u: "
686                        "rc = %d\n", osd_dev2name(dev), pos, rc);
687                 RETURN(rc);
688         }
689
690         if (dev->od_is_ost && S_ISREG(inode->i_mode) && inode->i_nlink > 1)
691                 dev->od_scrub.os_scrub.os_has_ml_file = 1;
692
693         if (scrub &&
694             ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
695                 /* Only skip it for the first OI scrub accessing. */
696                 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
697                 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
698         }
699
700         rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
701
702         GOTO(put, rc);
703
704 put:
705         iput(inode);
706         return rc;
707 }
708
709 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
710                           struct osd_iit_param *param,
711                           struct osd_idmap_cache **oic, const bool noslot)
712 {
713         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
714         struct lu_fid *fid;
715         struct osd_inode_id *lid;
716         int rc;
717
718         if (CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0)
719                 wait_var_event_timeout(
720                         scrub,
721                         !list_empty(&scrub->os_inconsistent_items) ||
722                         kthread_should_stop(),
723                         cfs_time_seconds(cfs_fail_val));
724
725         if (CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
726                 spin_lock(&scrub->os_lock);
727                 scrub->os_running = 0;
728                 spin_unlock(&scrub->os_lock);
729                 return SCRUB_NEXT_CRASH;
730         }
731
732         if (CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
733                 return SCRUB_NEXT_FATAL;
734
735         if (kthread_should_stop())
736                 return SCRUB_NEXT_EXIT;
737
738         if (!list_empty(&scrub->os_inconsistent_items)) {
739                 spin_lock(&scrub->os_lock);
740                 if (likely(!list_empty(&scrub->os_inconsistent_items))) {
741                         struct osd_inconsistent_item *oii;
742
743                         oii = list_first_entry(&scrub->os_inconsistent_items,
744                                                struct osd_inconsistent_item,
745                                                oii_list);
746
747                         *oic = &oii->oii_cache;
748                         scrub->os_in_prior = 1;
749                         spin_unlock(&scrub->os_lock);
750
751                         return 0;
752                 }
753                 spin_unlock(&scrub->os_lock);
754         }
755
756         if (noslot)
757                 return SCRUB_NEXT_WAIT;
758
759         rc = osd_iit_next(param, &scrub->os_pos_current);
760         if (rc != 0)
761                 return rc;
762
763         *oic = &dev->od_scrub.os_oic;
764         fid = &(*oic)->oic_fid;
765         lid = &(*oic)->oic_lid;
766         rc = osd_iit_iget(info, dev, fid, lid,
767                           scrub->os_pos_current, param->sb, true);
768         return rc;
769 }
770
771 static int osd_preload_next(struct osd_thread_info *info,
772                             struct osd_device *dev, struct osd_iit_param *param,
773                             struct osd_idmap_cache **oic, const bool noslot)
774 {
775         struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
776         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
777         int rc;
778
779         if (scrub->os_running &&
780             ooc->ooc_pos_preload >= scrub->os_pos_current)
781                 return SCRUB_NEXT_EXIT;
782
783         rc = osd_iit_next(param, &ooc->ooc_pos_preload);
784         if (rc)
785                 return rc;
786
787         rc = osd_iit_iget(info, dev,
788                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
789                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
790                           ooc->ooc_pos_preload, param->sb, false);
791         return rc;
792 }
793
794 static inline int
795 osd_scrub_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
796 {
797         spin_lock(&scrub->os_lock);
798         if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
799             !list_empty(&scrub->os_inconsistent_items) ||
800             it->ooi_waiting || kthread_should_stop())
801                 scrub->os_waiting = 0;
802         else
803                 scrub->os_waiting = 1;
804         spin_unlock(&scrub->os_lock);
805
806         return !scrub->os_waiting;
807 }
808
809 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
810                           struct osd_iit_param *param,
811                           struct osd_idmap_cache *oic, bool *noslot, int rc)
812 {
813         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
814         struct scrub_file *sf = &scrub->os_file;
815         struct osd_otable_it *it = dev->od_otable_it;
816         struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
817
818         switch (rc) {
819         case SCRUB_NEXT_NOSCRUB:
820                 down_write(&scrub->os_rwsem);
821                 scrub->os_new_checked++;
822                 sf->sf_items_noscrub++;
823                 up_write(&scrub->os_rwsem);
824         case SCRUB_NEXT_CONTINUE:
825         case SCRUB_NEXT_WAIT:
826                 goto wait;
827         }
828
829         rc = osd_scrub_check_update(info, dev, oic, rc);
830         if (rc != 0) {
831                 spin_lock(&scrub->os_lock);
832                 scrub->os_in_prior = 0;
833                 spin_unlock(&scrub->os_lock);
834                 return rc;
835         }
836
837         rc = scrub_checkpoint(info->oti_env, scrub);
838         if (rc) {
839                 CDEBUG(D_LFSCK, "%s: fail to checkpoint, pos = %llu: "
840                        "rc = %d\n", osd_scrub2name(scrub),
841                        scrub->os_pos_current, rc);
842                 /* Continue, as long as the scrub itself can go ahead. */
843         }
844
845         if (scrub->os_in_prior) {
846                 spin_lock(&scrub->os_lock);
847                 scrub->os_in_prior = 0;
848                 spin_unlock(&scrub->os_lock);
849                 return 0;
850         }
851
852 wait:
853         if (it != NULL && it->ooi_waiting && ooc != NULL &&
854             ooc->ooc_pos_preload < scrub->os_pos_current) {
855                 spin_lock(&scrub->os_lock);
856                 it->ooi_waiting = 0;
857                 wake_up_var(scrub);
858                 spin_unlock(&scrub->os_lock);
859         }
860
861         if (rc == SCRUB_NEXT_CONTINUE)
862                 return 0;
863
864         if (scrub->os_full_speed || !ooc || osd_scrub_has_window(scrub, ooc)) {
865                 *noslot = false;
866                 return 0;
867         }
868
869         if (it)
870                 wait_var_event(scrub, osd_scrub_wakeup(scrub, it));
871
872         if (!ooc || osd_scrub_has_window(scrub, ooc))
873                 *noslot = false;
874         else
875                 *noslot = true;
876         return 0;
877 }
878
879 static int osd_preload_exec(struct osd_thread_info *info,
880                             struct osd_device *dev, struct osd_iit_param *param,
881                             struct osd_idmap_cache *oic, bool *noslot, int rc)
882 {
883         struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
884
885         if (rc == 0) {
886                 ooc->ooc_cached_items++;
887                 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
888                                         ~OSD_OTABLE_IT_CACHE_MASK;
889         }
890         return rc > 0 ? 0 : rc;
891 }
892
893 #define SCRUB_IT_ALL    1
894 #define SCRUB_IT_CRASH  2
895
896 static void osd_scrub_join(const struct lu_env *env, struct osd_device *dev,
897                            __u32 flags, bool inconsistent)
898 {
899         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
900         struct scrub_file    *sf     = &scrub->os_file;
901         int                   rc;
902         ENTRY;
903
904         LASSERT(!(flags & SS_AUTO_PARTIAL));
905
906         down_write(&scrub->os_rwsem);
907         spin_lock(&scrub->os_lock);
908         scrub->os_in_join = 1;
909         if (flags & SS_SET_FAILOUT)
910                 sf->sf_param |= SP_FAILOUT;
911         else if (flags & SS_CLEAR_FAILOUT)
912                 sf->sf_param &= ~SP_FAILOUT;
913
914         if (flags & SS_SET_DRYRUN)
915                 sf->sf_param |= SP_DRYRUN;
916         else if (flags & SS_CLEAR_DRYRUN)
917                 sf->sf_param &= ~SP_DRYRUN;
918
919         if (flags & SS_RESET) {
920                 scrub_file_reset(scrub, dev->od_uuid,
921                                  inconsistent ? SF_INCONSISTENT : 0);
922                 sf->sf_status = SS_SCANNING;
923         }
924
925         if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
926                 scrub->os_full_speed = 1;
927         else
928                 scrub->os_full_speed = 0;
929
930         if (flags & SS_AUTO_FULL) {
931                 sf->sf_flags |= SF_AUTO;
932                 scrub->os_full_speed = 1;
933         }
934         spin_unlock(&scrub->os_lock);
935
936         scrub->os_new_checked = 0;
937         if (sf->sf_pos_last_checkpoint != 0)
938                 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
939         else
940                 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
941
942         scrub->os_pos_current = sf->sf_pos_latest_start;
943         sf->sf_time_latest_start = ktime_get_real_seconds();
944         sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
945         sf->sf_pos_last_checkpoint = sf->sf_pos_latest_start - 1;
946         rc = scrub_file_store(env, scrub);
947
948         spin_lock(&scrub->os_lock);
949         scrub->os_waiting = 0;
950         scrub->os_paused = 0;
951         scrub->os_partial_scan = 0;
952         scrub->os_in_join = 0;
953         scrub->os_full_scrub = 0;
954         spin_unlock(&scrub->os_lock);
955         wake_up_var(scrub);
956         up_write(&scrub->os_rwsem);
957
958         CDEBUG(D_LFSCK, "%s: joined in the OI scrub with flag %u: rc = %d\n",
959                osd_scrub2name(scrub), flags, rc);
960
961         EXIT;
962 }
963
964 static int osd_inode_iteration(struct osd_thread_info *info,
965                                struct osd_device *dev, __u32 max, bool preload)
966 {
967         struct lustre_scrub *scrub  = &dev->od_scrub.os_scrub;
968         struct scrub_file *sf = &scrub->os_file;
969         osd_iit_next_policy next;
970         osd_iit_exec_policy exec;
971         __u64 *pos;
972         __u64 *count;
973         struct osd_iit_param *param;
974         __u32 limit;
975         int rc;
976         bool noslot = true;
977         ENTRY;
978
979         if (preload)
980                 goto full;
981
982         param = &dev->od_scrub.os_iit_param;
983         memset(param, 0, sizeof(*param));
984         param->sb = osd_sb(dev);
985
986         while (scrub->os_partial_scan && !scrub->os_in_join) {
987                 struct osd_idmap_cache *oic = NULL;
988
989                 rc = osd_scrub_next(info, dev, param, &oic, noslot);
990                 switch (rc) {
991                 case SCRUB_NEXT_EXIT:
992                         RETURN(0);
993                 case SCRUB_NEXT_CRASH:
994                         RETURN(SCRUB_IT_CRASH);
995                 case SCRUB_NEXT_FATAL:
996                         RETURN(-EINVAL);
997                 case SCRUB_NEXT_WAIT: {
998                         struct kstatfs *ksfs = &info->oti_ksfs;
999                         __u64 saved_flags;
1000
1001                         if (dev->od_full_scrub_ratio == OFSR_NEVER ||
1002                             unlikely(sf->sf_items_updated_prior == 0))
1003                                 goto wait;
1004
1005                         if (dev->od_full_scrub_ratio == OFSR_DIRECTLY ||
1006                             scrub->os_full_scrub) {
1007                                 osd_scrub_join(info->oti_env, dev,
1008                                                SS_AUTO_FULL | SS_RESET, true);
1009                                 goto full;
1010                         }
1011
1012                         rc = param->sb->s_op->statfs(param->sb->s_root, ksfs);
1013                         if (rc == 0) {
1014                                 __u64 used = ksfs->f_files - ksfs->f_ffree;
1015
1016                                 used = div64_u64(used, sf->sf_items_updated_prior);
1017                                 /* If we hit too much inconsistent OI
1018                                  * mappings during the partial scan,
1019                                  * then scan the device completely. */
1020                                 if (used < dev->od_full_scrub_ratio) {
1021                                         osd_scrub_join(info->oti_env, dev,
1022                                                 SS_AUTO_FULL | SS_RESET, true);
1023                                         goto full;
1024                                 }
1025                         }
1026
1027 wait:
1028                         if (CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) &&
1029                             cfs_fail_val > 0)
1030                                 continue;
1031
1032                         saved_flags = sf->sf_flags;
1033                         sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
1034                                           SF_UPGRADE | SF_AUTO);
1035                         sf->sf_status = SS_COMPLETED;
1036                         wait_var_event(
1037                                 scrub,
1038                                 kthread_should_stop() ||
1039                                 !scrub->os_partial_scan ||
1040                                 scrub->os_in_join ||
1041                                 !list_empty(&scrub->os_inconsistent_items));
1042                         sf->sf_flags = saved_flags;
1043                         sf->sf_status = SS_SCANNING;
1044
1045                         if (kthread_should_stop())
1046                                 RETURN(0);
1047
1048                         if (!scrub->os_partial_scan || scrub->os_in_join)
1049                                 goto full;
1050
1051                         continue;
1052                 }
1053                 default:
1054                         LASSERTF(rc == 0, "rc = %d\n", rc);
1055
1056                         osd_scrub_exec(info, dev, param, oic, &noslot, rc);
1057                         break;
1058                 }
1059         }
1060
1061 full:
1062         if (!preload) {
1063                 wait_var_event(scrub,
1064                                kthread_should_stop() ||
1065                                !scrub->os_in_join);
1066
1067                 if (kthread_should_stop())
1068                         RETURN(0);
1069         }
1070
1071         noslot = false;
1072         if (!preload) {
1073                 next = osd_scrub_next;
1074                 exec = osd_scrub_exec;
1075                 pos = &scrub->os_pos_current;
1076                 count = &scrub->os_new_checked;
1077                 param->start = *pos;
1078                 param->bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param->sb);
1079                 param->offset =
1080                         (*pos - 1) % LDISKFS_INODES_PER_GROUP(param->sb);
1081                 param->gbase =
1082                         1 + param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
1083         } else {
1084                 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1085
1086                 next = osd_preload_next;
1087                 exec = osd_preload_exec;
1088                 pos = &ooc->ooc_pos_preload;
1089                 count = &ooc->ooc_cached_items;
1090                 param = &dev->od_otable_it->ooi_iit_param;
1091         }
1092
1093         rc = 0;
1094         limit = le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count);
1095         while (*pos <= limit && *count < max) {
1096                 struct ldiskfs_group_desc *desc;
1097                 bool next_group = false;
1098
1099                 desc = ldiskfs_get_group_desc(param->sb, param->bg, NULL);
1100                 if (!desc)
1101                         RETURN(-EIO);
1102
1103                 if (desc->bg_flags & cpu_to_le16(LDISKFS_BG_INODE_UNINIT)) {
1104                         next_group = true;
1105                         goto next_group;
1106                 }
1107
1108                 param->bitmap = ldiskfs_read_inode_bitmap(param->sb, param->bg);
1109                 if (IS_ERR_OR_NULL(param->bitmap)) {
1110                         if (param->bitmap) {
1111                                 rc = PTR_ERR(param->bitmap);
1112                                 param->bitmap = NULL;
1113                         } else {
1114                                 rc = -EIO;
1115                         }
1116                         CERROR("%s: fail to read bitmap for %u, scrub will stop, urgent mode: rc = %d\n",
1117                                osd_scrub2name(scrub), (__u32)param->bg, rc);
1118                         GOTO(out, rc);
1119                 }
1120
1121                 do {
1122                         struct osd_idmap_cache *oic = NULL;
1123
1124                         if (param->offset +
1125                                 ldiskfs_itable_unused_count(param->sb, desc) >=
1126                             LDISKFS_INODES_PER_GROUP(param->sb)) {
1127                                 next_group = true;
1128                                 goto next_group;
1129                         }
1130
1131                         rc = next(info, dev, param, &oic, noslot);
1132                         switch (rc) {
1133                         case SCRUB_NEXT_BREAK:
1134                                 next_group = true;
1135                                 goto next_group;
1136                         case SCRUB_NEXT_EXIT:
1137                                 brelse(param->bitmap);
1138                                 RETURN(0);
1139                         case SCRUB_NEXT_CRASH:
1140                                 brelse(param->bitmap);
1141                                 RETURN(SCRUB_IT_CRASH);
1142                         case SCRUB_NEXT_FATAL:
1143                                 brelse(param->bitmap);
1144                                 RETURN(-EINVAL);
1145                         }
1146
1147                         rc = exec(info, dev, param, oic, &noslot, rc);
1148                 } while (!rc && *pos <= limit && *count < max);
1149
1150 next_group:
1151                 if (param->bitmap) {
1152                         brelse(param->bitmap);
1153                         param->bitmap = NULL;
1154                 }
1155
1156                 if (rc < 0)
1157                         GOTO(out, rc);
1158
1159                 if (next_group) {
1160                         param->bg++;
1161                         param->offset = 0;
1162                         param->gbase = 1 +
1163                                 param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
1164                         *pos = param->gbase;
1165                         param->start = *pos;
1166                 }
1167         }
1168
1169         if (*pos > limit)
1170                 RETURN(SCRUB_IT_ALL);
1171
1172 out:
1173         RETURN(rc);
1174 }
1175
1176 static int osd_otable_it_preload(const struct lu_env *env,
1177                                  struct osd_otable_it *it)
1178 {
1179         struct osd_device *dev = it->ooi_dev;
1180         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1181         struct osd_otable_cache *ooc   = &it->ooi_cache;
1182         int                      rc;
1183         ENTRY;
1184
1185         rc = osd_inode_iteration(osd_oti_get(env), dev,
1186                                  OSD_OTABLE_IT_CACHE_SIZE, true);
1187         if (rc == SCRUB_IT_ALL)
1188                 it->ooi_all_cached = 1;
1189
1190         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1191                 spin_lock(&scrub->os_lock);
1192                 scrub->os_waiting = 0;
1193                 wake_up_var(scrub);
1194                 spin_unlock(&scrub->os_lock);
1195         }
1196
1197         RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1198 }
1199
1200 static int osd_scan_ml_file_main(const struct lu_env *env,
1201                                  struct osd_device *dev);
1202
1203 static int osd_scrub_main(void *args)
1204 {
1205         struct lu_env env;
1206         struct osd_device *dev = (struct osd_device *)args;
1207         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1208         int rc, ret;
1209         ENTRY;
1210
1211         rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
1212         if (rc != 0) {
1213                 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
1214                        osd_scrub2name(scrub), rc);
1215                 GOTO(noenv, rc);
1216         }
1217
1218         rc = scrub_thread_prep(&env, scrub, dev->od_uuid,
1219                                LDISKFS_FIRST_INO(osd_sb(dev)) + 1);
1220         if (rc != 0) {
1221                 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
1222                        osd_scrub2name(scrub), rc);
1223                 GOTO(out, rc);
1224         }
1225
1226         if (!scrub->os_full_speed && !scrub->os_partial_scan) {
1227                 struct osd_otable_it *it = dev->od_otable_it;
1228                 struct osd_otable_cache *ooc = &it->ooi_cache;
1229
1230                 wait_var_event(scrub,
1231                                it->ooi_user_ready || kthread_should_stop());
1232                 if (kthread_should_stop())
1233                         GOTO(post, rc = 0);
1234
1235                 scrub->os_pos_current = ooc->ooc_pos_preload;
1236         }
1237
1238         CDEBUG(D_LFSCK, "%s: OI scrub start, flags = 0x%x, pos = %llu%s\n",
1239                osd_scrub2name(scrub), scrub->os_start_flags,
1240                scrub->os_pos_current,
1241                scrub->os_file.sf_param & SP_DRYRUN ? " dryrun mode" : "");
1242
1243         rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1244         if (unlikely(rc == SCRUB_IT_CRASH)) {
1245                 spin_lock(&scrub->os_lock);
1246                 scrub->os_running = 0;
1247                 spin_unlock(&scrub->os_lock);
1248                 GOTO(out, rc = -EINVAL);
1249         }
1250
1251         if (scrub->os_has_ml_file) {
1252                 ret = osd_scan_ml_file_main(&env, dev);
1253                 if (ret != 0)
1254                         rc = ret;
1255         }
1256
1257         GOTO(post, rc);
1258
1259 post:
1260         if (rc > 0) {
1261                 dev->od_igif_inoi = 1;
1262                 dev->od_check_ff = 0;
1263         }
1264         rc = scrub_thread_post(&env, &dev->od_scrub.os_scrub, rc);
1265         CDEBUG(D_LFSCK, "%s: OI scrub: stop, pos = %llu: rc = %d%s\n",
1266                osd_scrub2name(scrub), scrub->os_pos_current, rc,
1267                scrub->os_file.sf_param & SP_DRYRUN ? " dryrun mode" : "");
1268
1269
1270 out:
1271         osd_scrub_ois_fini(scrub, &scrub->os_inconsistent_items);
1272         lu_env_fini(&env);
1273
1274 noenv:
1275         spin_lock(&scrub->os_lock);
1276         scrub->os_running = 0;
1277         spin_unlock(&scrub->os_lock);
1278         if (xchg(&scrub->os_task, NULL) == NULL)
1279                 /* scrub_stop() is waiting, we need to synchronize */
1280                 wait_var_event(scrub, kthread_should_stop());
1281         wake_up_var(scrub);
1282         return rc;
1283 }
1284
1285 /* initial OI scrub */
1286
1287 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1288                          struct dentry *, filldir_t filldir);
1289
1290 #ifdef HAVE_FILLDIR_USE_CTX
1291 static FILLDIR_TYPE
1292 osd_ios_varfid_fill(struct dir_context *buf, const char *name, int namelen,
1293                     loff_t offset, __u64 ino, unsigned int d_type);
1294
1295 static FILLDIR_TYPE
1296 osd_ios_lf_fill(struct dir_context *buf, const char *name, int namelen,
1297                 loff_t offset, __u64 ino, unsigned int d_type);
1298
1299 static FILLDIR_TYPE
1300 osd_ios_dl_fill(struct dir_context *buf, const char *name, int namelen,
1301                 loff_t offset, __u64 ino, unsigned int d_type);
1302
1303 static FILLDIR_TYPE
1304 osd_ios_uld_fill(struct dir_context *buf, const char *name, int namelen,
1305                  loff_t offset, __u64 ino, unsigned int d_type);
1306 #else
1307 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1308                                loff_t offset, __u64 ino, unsigned int d_type);
1309 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1310                            loff_t offset, __u64 ino, unsigned int d_type);
1311 static int osd_ios_dl_fill(void *buf, const char *name, int namelen,
1312                            loff_t offset, __u64 ino, unsigned int d_type);
1313 static int osd_ios_uld_fill(void *buf, const char *name, int namelen,
1314                             loff_t offset, __u64 ino, unsigned int d_type);
1315 #endif
1316
1317 static int
1318 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1319                      struct dentry *dentry, filldir_t filldir);
1320 static int
1321 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1322                   struct dentry *dentry, filldir_t filldir);
1323
1324 static int
1325 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1326                      struct dentry *dentry, filldir_t filldir);
1327
1328 struct osd_lf_map {
1329         char            *olm_name;
1330         struct lu_fid    olm_fid;
1331         __u16            olm_flags;
1332         __u16            olm_namelen;
1333         scandir_t        olm_scandir;
1334         filldir_t        olm_filldir;
1335 };
1336
1337 /* Add the new introduced local files in the list in the future. */
1338 static const struct osd_lf_map osd_lf_maps[] = {
1339         /* CATALOGS */
1340         {
1341                 .olm_name       = CATLIST,
1342                 .olm_fid        = {
1343                         .f_seq  = FID_SEQ_LOCAL_FILE,
1344                         .f_oid  = LLOG_CATALOGS_OID,
1345                 },
1346                 .olm_flags      = OLF_SHOW_NAME,
1347                 .olm_namelen    = sizeof(CATLIST) - 1,
1348         },
1349
1350         /* CONFIGS */
1351         {
1352                 .olm_name       = MOUNT_CONFIGS_DIR,
1353                 .olm_fid        = {
1354                         .f_seq  = FID_SEQ_LOCAL_FILE,
1355                         .f_oid  = MGS_CONFIGS_OID,
1356                 },
1357                 .olm_flags      = OLF_SCAN_SUBITEMS,
1358                 .olm_namelen    = sizeof(MOUNT_CONFIGS_DIR) - 1,
1359                 .olm_scandir    = osd_ios_general_scan,
1360                 .olm_filldir    = osd_ios_varfid_fill,
1361         },
1362
1363         /* NIDTBL_VERSIONS */
1364         {
1365                 .olm_name       = MGS_NIDTBL_DIR,
1366                 .olm_flags      = OLF_SCAN_SUBITEMS,
1367                 .olm_namelen    = sizeof(MGS_NIDTBL_DIR) - 1,
1368                 .olm_scandir    = osd_ios_general_scan,
1369                 .olm_filldir    = osd_ios_varfid_fill,
1370         },
1371
1372         /* PENDING */
1373         {
1374                 .olm_name       = MDT_ORPHAN_DIR,
1375                 .olm_namelen    = sizeof(MDT_ORPHAN_DIR) - 1,
1376         },
1377
1378         /* ROOT */
1379         {
1380                 .olm_name       = "ROOT",
1381                 .olm_fid        = {
1382                         .f_seq  = FID_SEQ_ROOT,
1383                         .f_oid  = FID_OID_ROOT,
1384                 },
1385                 .olm_flags      = OLF_SCAN_SUBITEMS | OLF_HIDE_FID,
1386                 .olm_namelen    = sizeof("ROOT") - 1,
1387                 .olm_scandir    = osd_ios_ROOT_scan,
1388         },
1389
1390         /* changelog_catalog */
1391         {
1392                 .olm_name       = CHANGELOG_CATALOG,
1393                 .olm_namelen    = sizeof(CHANGELOG_CATALOG) - 1,
1394         },
1395
1396         /* changelog_users */
1397         {
1398                 .olm_name       = CHANGELOG_USERS,
1399                 .olm_namelen    = sizeof(CHANGELOG_USERS) - 1,
1400         },
1401
1402         /* fld */
1403         {
1404                 .olm_name       = "fld",
1405                 .olm_fid        = {
1406                         .f_seq  = FID_SEQ_LOCAL_FILE,
1407                         .f_oid  = FLD_INDEX_OID,
1408                 },
1409                 .olm_flags      = OLF_SHOW_NAME,
1410                 .olm_namelen    = sizeof("fld") - 1,
1411         },
1412
1413         /* last_rcvd */
1414         {
1415                 .olm_name       = LAST_RCVD,
1416                 .olm_fid        = {
1417                         .f_seq  = FID_SEQ_LOCAL_FILE,
1418                         .f_oid  = LAST_RECV_OID,
1419                 },
1420                 .olm_flags      = OLF_SHOW_NAME,
1421                 .olm_namelen    = sizeof(LAST_RCVD) - 1,
1422         },
1423
1424         /* reply_data */
1425         {
1426                 .olm_name       = REPLY_DATA,
1427                 .olm_fid        = {
1428                         .f_seq  = FID_SEQ_LOCAL_FILE,
1429                         .f_oid  = REPLY_DATA_OID,
1430                 },
1431                 .olm_flags      = OLF_SHOW_NAME,
1432                 .olm_namelen    = sizeof(REPLY_DATA) - 1,
1433         },
1434
1435         /* lov_objid */
1436         {
1437                 .olm_name       = LOV_OBJID,
1438                 .olm_fid        = {
1439                         .f_seq  = FID_SEQ_LOCAL_FILE,
1440                         .f_oid  = MDD_LOV_OBJ_OID,
1441                 },
1442                 .olm_flags      = OLF_SHOW_NAME,
1443                 .olm_namelen    = sizeof(LOV_OBJID) - 1,
1444         },
1445
1446         /* lov_objseq */
1447         {
1448                 .olm_name       = LOV_OBJSEQ,
1449                 .olm_fid        = {
1450                         .f_seq  = FID_SEQ_LOCAL_FILE,
1451                         .f_oid  = MDD_LOV_OBJ_OSEQ,
1452                 },
1453                 .olm_flags      = OLF_SHOW_NAME,
1454                 .olm_namelen    = sizeof(LOV_OBJSEQ) - 1,
1455         },
1456
1457         /* quota_master */
1458         {
1459                 .olm_name       = QMT_DIR,
1460                 .olm_flags      = OLF_SCAN_SUBITEMS,
1461                 .olm_namelen    = sizeof(QMT_DIR) - 1,
1462                 .olm_scandir    = osd_ios_general_scan,
1463                 .olm_filldir    = osd_ios_varfid_fill,
1464         },
1465
1466         /* quota_slave */
1467         {
1468                 .olm_name       = QSD_DIR,
1469                 .olm_flags      = OLF_SCAN_SUBITEMS,
1470                 .olm_namelen    = sizeof(QSD_DIR) - 1,
1471                 .olm_scandir    = osd_ios_general_scan,
1472                 .olm_filldir    = osd_ios_varfid_fill,
1473         },
1474
1475         /* seq_ctl */
1476         {
1477                 .olm_name       = "seq_ctl",
1478                 .olm_fid        = {
1479                         .f_seq  = FID_SEQ_LOCAL_FILE,
1480                         .f_oid  = FID_SEQ_CTL_OID,
1481                 },
1482                 .olm_flags      = OLF_SHOW_NAME,
1483                 .olm_namelen    = sizeof("seq_ctl") - 1,
1484         },
1485
1486         /* seq_srv */
1487         {
1488                 .olm_name       = "seq_srv",
1489                 .olm_fid        = {
1490                         .f_seq  = FID_SEQ_LOCAL_FILE,
1491                         .f_oid  = FID_SEQ_SRV_OID,
1492                 },
1493                 .olm_flags      = OLF_SHOW_NAME,
1494                 .olm_namelen    = sizeof("seq_srv") - 1,
1495         },
1496
1497         /* health_check */
1498         {
1499                 .olm_name       = HEALTH_CHECK,
1500                 .olm_fid        = {
1501                         .f_seq  = FID_SEQ_LOCAL_FILE,
1502                         .f_oid  = OFD_HEALTH_CHECK_OID,
1503                 },
1504                 .olm_flags      = OLF_SHOW_NAME,
1505                 .olm_namelen    = sizeof(HEALTH_CHECK) - 1,
1506         },
1507
1508         /* LFSCK */
1509         {
1510                 .olm_name       = LFSCK_DIR,
1511                 .olm_flags      = OLF_SCAN_SUBITEMS,
1512                 .olm_namelen    = sizeof(LFSCK_DIR) - 1,
1513                 .olm_scandir    = osd_ios_general_scan,
1514                 .olm_filldir    = osd_ios_varfid_fill,
1515         },
1516
1517         /* lfsck_bookmark */
1518         {
1519                 .olm_name       = LFSCK_BOOKMARK,
1520                 .olm_namelen    = sizeof(LFSCK_BOOKMARK) - 1,
1521         },
1522
1523         /* lfsck_layout */
1524         {
1525                 .olm_name       = LFSCK_LAYOUT,
1526                 .olm_namelen    = sizeof(LFSCK_LAYOUT) - 1,
1527         },
1528
1529         /* lfsck_namespace */
1530         {
1531                 .olm_name       = LFSCK_NAMESPACE,
1532                 .olm_namelen    = sizeof(LFSCK_NAMESPACE) - 1,
1533         },
1534
1535         /* OBJECTS, upgrade from old device */
1536         {
1537                 .olm_name       = OBJECTS,
1538                 .olm_flags      = OLF_SCAN_SUBITEMS,
1539                 .olm_namelen    = sizeof(OBJECTS) - 1,
1540                 .olm_scandir    = osd_ios_OBJECTS_scan,
1541         },
1542
1543         /* lquota_v2.user, upgrade from old device */
1544         {
1545                 .olm_name       = "lquota_v2.user",
1546                 .olm_namelen    = sizeof("lquota_v2.user") - 1,
1547         },
1548
1549         /* lquota_v2.group, upgrade from old device */
1550         {
1551                 .olm_name       = "lquota_v2.group",
1552                 .olm_namelen    = sizeof("lquota_v2.group") - 1,
1553         },
1554
1555         /* LAST_GROUP, upgrade from old device */
1556         {
1557                 .olm_name       = "LAST_GROUP",
1558                 .olm_fid        = {
1559                         .f_seq  = FID_SEQ_LOCAL_FILE,
1560                         .f_oid  = OFD_LAST_GROUP_OID,
1561                 },
1562                 .olm_flags      = OLF_SHOW_NAME,
1563                 .olm_namelen    = sizeof("LAST_GROUP") - 1,
1564         },
1565
1566         /* committed batchid for cross-MDT operation */
1567         {
1568                 .olm_name       = "BATCHID",
1569                 .olm_fid        = {
1570                         .f_seq  = FID_SEQ_LOCAL_FILE,
1571                         .f_oid  = BATCHID_COMMITTED_OID,
1572                 },
1573                 .olm_flags      = OLF_SHOW_NAME,
1574                 .olm_namelen    = sizeof("BATCHID") - 1,
1575         },
1576
1577         /* OSP update logs update_log{_dir} use f_seq = FID_SEQ_UPDATE_LOG{_DIR}
1578          * and f_oid = index for their log files.  See lu_update_log{_dir}_fid()
1579          * for more details. */
1580
1581         /* update_log */
1582         {
1583                 .olm_name       = "update_log",
1584                 .olm_fid        = {
1585                         .f_seq  = FID_SEQ_UPDATE_LOG,
1586                 },
1587                 .olm_flags      = OLF_SHOW_NAME | OLF_IDX_IN_FID,
1588                 .olm_namelen    = sizeof("update_log") - 1,
1589         },
1590
1591         /* update_log_dir */
1592         {
1593                 .olm_name       = "update_log_dir",
1594                 .olm_fid        = {
1595                         .f_seq  = FID_SEQ_UPDATE_LOG_DIR,
1596                 },
1597                 .olm_flags      = OLF_SHOW_NAME | OLF_SCAN_SUBITEMS |
1598                                   OLF_IDX_IN_FID,
1599                 .olm_namelen    = sizeof("update_log_dir") - 1,
1600                 .olm_scandir    = osd_ios_general_scan,
1601                 .olm_filldir    = osd_ios_uld_fill,
1602         },
1603
1604         /* lost+found */
1605         {
1606                 .olm_name       = "lost+found",
1607                 .olm_fid        = {
1608                         .f_seq  = FID_SEQ_LOCAL_FILE,
1609                         .f_oid  = OSD_LPF_OID,
1610                 },
1611                 .olm_flags      = OLF_SCAN_SUBITEMS,
1612                 .olm_namelen    = sizeof("lost+found") - 1,
1613                 .olm_scandir    = osd_ios_general_scan,
1614                 .olm_filldir    = osd_ios_lf_fill,
1615         },
1616
1617         /* hsm_actions */
1618         {
1619                 .olm_name       = HSM_ACTIONS,
1620         },
1621
1622         /* nodemap */
1623         {
1624                 .olm_name       = LUSTRE_NODEMAP_NAME,
1625         },
1626
1627         /* index_backup */
1628         {
1629                 .olm_name       = INDEX_BACKUP_DIR,
1630                 .olm_fid        = {
1631                         .f_seq  = FID_SEQ_LOCAL_FILE,
1632                         .f_oid  = INDEX_BACKUP_OID,
1633                 },
1634                 .olm_flags      = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
1635                 .olm_namelen    = sizeof(INDEX_BACKUP_DIR) - 1,
1636                 .olm_scandir    = osd_ios_general_scan,
1637                 .olm_filldir    = osd_ios_varfid_fill,
1638         },
1639
1640         {
1641                 .olm_name       = NULL
1642         }
1643 };
1644
1645 /* Add the new introduced files under .lustre/ in the list in the future. */
1646 static const struct osd_lf_map osd_dl_maps[] = {
1647         /* .lustre/fid */
1648         {
1649                 .olm_name       = "fid",
1650                 .olm_fid        = {
1651                         .f_seq  = FID_SEQ_DOT_LUSTRE,
1652                         .f_oid  = FID_OID_DOT_LUSTRE_OBF,
1653                 },
1654                 .olm_namelen    = sizeof("fid") - 1,
1655         },
1656
1657         /* .lustre/lost+found */
1658         {
1659                 .olm_name       = "lost+found",
1660                 .olm_fid        = {
1661                         .f_seq  = FID_SEQ_DOT_LUSTRE,
1662                         .f_oid  = FID_OID_DOT_LUSTRE_LPF,
1663                 },
1664                 .olm_namelen    = sizeof("lost+found") - 1,
1665         },
1666
1667         {
1668                 .olm_name       = NULL
1669         }
1670 };
1671
1672 struct osd_ios_item {
1673         struct list_head oii_list;
1674         struct dentry   *oii_dentry;
1675         scandir_t        oii_scandir;
1676         filldir_t        oii_filldir;
1677 };
1678
1679 struct osd_ios_filldir_buf {
1680         /* please keep it as first member */
1681         struct dir_context       ctx;
1682         struct osd_thread_info  *oifb_info;
1683         struct osd_device       *oifb_dev;
1684         struct dentry           *oifb_dentry;
1685         int                      oifb_items;
1686 };
1687
1688 static int
1689 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1690                  scandir_t scandir, filldir_t filldir)
1691 {
1692         struct osd_ios_item *item;
1693         ENTRY;
1694
1695         OBD_ALLOC_PTR(item);
1696         if (item == NULL)
1697                 RETURN(-ENOMEM);
1698
1699         INIT_LIST_HEAD(&item->oii_list);
1700         item->oii_dentry = dget(dentry);
1701         item->oii_scandir = scandir;
1702         item->oii_filldir = filldir;
1703         list_add_tail(&item->oii_list, &dev->od_ios_list);
1704
1705         RETURN(0);
1706 }
1707
1708 static bool osd_index_need_recreate(const struct lu_env *env,
1709                                     struct osd_device *dev, struct inode *inode)
1710 {
1711         struct osd_directory *iam = &osd_oti_get(env)->oti_iam;
1712         struct iam_container *bag = &iam->od_container;
1713         int rc;
1714         ENTRY;
1715
1716         rc = iam_container_init(bag, &iam->od_descr, inode);
1717         if (rc)
1718                 RETURN(true);
1719
1720         rc = iam_container_setup(bag);
1721         iam_container_fini(bag);
1722         if (rc)
1723                 RETURN(true);
1724
1725         RETURN(false);
1726 }
1727
1728 static void osd_ios_index_register(const struct lu_env *env,
1729                                    struct osd_device *osd,
1730                                    const struct lu_fid *fid,
1731                                    struct inode *inode)
1732 {
1733         struct osd_directory *iam = &osd_oti_get(env)->oti_iam;
1734         struct iam_container *bag = &iam->od_container;
1735         struct super_block *sb = osd_sb(osd);
1736         struct iam_descr *descr;
1737         __u32 keysize = 0;
1738         __u32 recsize = 0;
1739         int rc;
1740         ENTRY;
1741
1742         /* Index must be a regular file. */
1743         if (!S_ISREG(inode->i_mode))
1744                 RETURN_EXIT;
1745
1746         /* Index's size must be block aligned. */
1747         if (inode->i_size < sb->s_blocksize ||
1748             (inode->i_size & (sb->s_blocksize - 1)) != 0)
1749                 RETURN_EXIT;
1750
1751         iam_container_init(bag, &iam->od_descr, inode);
1752         rc = iam_container_setup(bag);
1753         if (rc)
1754                 GOTO(fini, rc = 1);
1755
1756         descr = bag->ic_descr;
1757         /* May be regular file with IAM_LFIX_ROOT_MAGIC matched
1758          * coincidentally, or corrupted index object, skip it. */
1759         if (descr->id_ptr_size != 4)
1760                 GOTO(fini, rc = 1);
1761
1762         keysize = descr->id_key_size;
1763         recsize = descr->id_rec_size;
1764         rc = osd_index_register(osd, fid, keysize, recsize);
1765
1766         GOTO(fini, rc);
1767
1768 fini:
1769         iam_container_fini(bag);
1770         if (!rc)
1771                 CDEBUG(D_LFSCK, "%s: index object "DFID" (%u/%u) registered\n",
1772                        osd_name(osd), PFID(fid), keysize, recsize);
1773 }
1774
1775 static void osd_index_restore(const struct lu_env *env, struct osd_device *dev,
1776                               struct lustre_index_restore_unit *liru,
1777                               void *buf, int bufsize)
1778 {
1779         struct osd_thread_info *info = osd_oti_get(env);
1780         struct osd_inode_id *id = &info->oti_id;
1781         struct lu_fid *tgt_fid = &liru->liru_cfid;
1782         struct inode *bak_inode = NULL;
1783         struct ldiskfs_dir_entry_2 *de = NULL;
1784         struct buffer_head *bh = NULL;
1785         struct dentry *dentry;
1786         char *name = buf;
1787         struct lu_fid bak_fid;
1788         int rc;
1789         ENTRY;
1790
1791         lustre_fid2lbx(name, tgt_fid, bufsize);
1792         dentry = osd_child_dentry_by_inode(env, dev->od_index_backup_inode,
1793                                            name, strlen(name));
1794         bh = osd_ldiskfs_find_entry(dev->od_index_backup_inode,
1795                                     &dentry->d_name, &de, NULL, NULL);
1796         if (IS_ERR(bh))
1797                 GOTO(log, rc = PTR_ERR(bh));
1798
1799         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1800         brelse(bh);
1801         bak_inode = osd_iget_fid(info, dev, id, &bak_fid);
1802         if (IS_ERR(bak_inode))
1803                 GOTO(log, rc = PTR_ERR(bak_inode));
1804
1805         iput(bak_inode);
1806         /* The OI mapping for index may be invalid, since it will be
1807          * re-created, not update the OI mapping, just cache it in RAM. */
1808         osd_id_gen(id, liru->liru_clid, OSD_OII_NOGEN);
1809         osd_add_oi_cache(info, dev, id, tgt_fid);
1810         rc = lustre_index_restore(env, &dev->od_dt_dev, &liru->liru_pfid,
1811                                   tgt_fid, &bak_fid, liru->liru_name,
1812                                   &dev->od_index_backup_list, &dev->od_lock,
1813                                   buf, bufsize);
1814         GOTO(log, rc);
1815
1816 log:
1817         CDEBUG(D_WARNING, "%s: restore index '%s' with "DFID": rc = %d\n",
1818                osd_name(dev), liru->liru_name, PFID(tgt_fid), rc);
1819 }
1820
1821 /**
1822  * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1823  *
1824  * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1825  * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1826  * reference the inode, or fixed if it is missing or references another inode.
1827  */
1828 static int
1829 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1830                  struct inode *parent, struct inode *inode,
1831                  const struct lu_fid *fid, const char *name,
1832                  int namelen, int flags)
1833 {
1834         struct lustre_mdt_attrs *lma    = &info->oti_ost_attrs.loa_lma;
1835         struct osd_inode_id     *id     = &info->oti_id;
1836         struct osd_inode_id     *id2    = &info->oti_id2;
1837         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1838         struct scrub_file       *sf     = &scrub->os_file;
1839         struct lu_fid            tfid;
1840         int                      rc;
1841         ENTRY;
1842
1843         if (!inode) {
1844                 CDEBUG(D_INODE, "%s: child '%.*s' lacks inode: rc = -2\n",
1845                        osd_name(dev), namelen, name);
1846                 RETURN(-ENOENT);
1847         }
1848
1849         rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
1850                          &info->oti_ost_attrs);
1851         if (rc != 0 && rc != -ENODATA) {
1852                 CDEBUG(D_LFSCK, "%s: fail to get lma for init OI scrub: "
1853                        "rc = %d\n", osd_name(dev), rc);
1854
1855                 RETURN(rc);
1856         }
1857
1858         osd_id_gen(id, inode->i_ino, inode->i_generation);
1859         if (rc == -ENODATA) {
1860                 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID) {
1861                         lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1862                 } else {
1863                         tfid = *fid;
1864                         if (flags & OLF_IDX_IN_FID) {
1865                                 LASSERT(dev->od_index >= 0);
1866
1867                                 tfid.f_oid = dev->od_index;
1868                         }
1869                 }
1870                 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1871                 if (rc != 0) {
1872                         CDEBUG(D_LFSCK, "%s: fail to set LMA for init OI "
1873                               "scrub: rc = %d\n", osd_name(dev), rc);
1874
1875                         RETURN(rc);
1876                 }
1877         } else {
1878                 if (lma->lma_compat & LMAC_NOT_IN_OI)
1879                         RETURN(0);
1880
1881                 tfid = lma->lma_self_fid;
1882                 if (lma->lma_compat & LMAC_IDX_BACKUP &&
1883                     osd_index_need_recreate(info->oti_env, dev, inode)) {
1884                         struct lu_fid *pfid = &info->oti_fid3;
1885
1886                         if (is_root_inode(parent)) {
1887                                 lu_local_obj_fid(pfid, OSD_FS_ROOT_OID);
1888                         } else {
1889                                 rc = osd_scrub_get_fid(info, dev, parent, pfid,
1890                                                        false);
1891                                 if (rc)
1892                                         RETURN(rc);
1893                         }
1894
1895                         rc = lustre_liru_new(&dev->od_index_restore_list, pfid,
1896                                         &tfid, inode->i_ino, name, namelen);
1897
1898                         RETURN(rc);
1899                 }
1900
1901                 if (!(flags & OLF_NOT_BACKUP))
1902                         osd_ios_index_register(info->oti_env, dev, &tfid,
1903                                                inode);
1904         }
1905
1906         /* Since this called from iterate_dir() the inode lock will be taken */
1907         rc = osd_oi_lookup(info, dev, &tfid, id2, OI_LOCKED);
1908         if (rc != 0) {
1909                 if (rc != -ENOENT)
1910                         RETURN(rc);
1911
1912                 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1913                                                DTO_INDEX_INSERT, true, 0, NULL);
1914                 if (rc > 0)
1915                         rc = 0;
1916
1917                 RETURN(rc);
1918         }
1919
1920         if (osd_id_eq_strict(id, id2))
1921                 RETURN(0);
1922
1923         if (!(sf->sf_flags & SF_INCONSISTENT)) {
1924                 scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
1925                 rc = scrub_file_store(info->oti_env, scrub);
1926                 if (rc != 0)
1927                         RETURN(rc);
1928         }
1929
1930         rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1931                                        DTO_INDEX_UPDATE, true, 0, NULL);
1932         if (rc > 0)
1933                 rc = 0;
1934
1935         RETURN(rc);
1936 }
1937
1938 /**
1939  * It scans the /lost+found, and for the OST-object (with filter_fid
1940  * or filter_fid_18_23), move them back to its proper /O/<seq>/d<x>.
1941  */
1942 #ifdef HAVE_FILLDIR_USE_CTX
1943 static FILLDIR_TYPE do_osd_ios_lf_fill(struct dir_context *buf,
1944 #else
1945 static int osd_ios_lf_fill(void *buf,
1946 #endif
1947                            const char *name, int namelen,
1948                            loff_t offset, __u64 ino, unsigned int d_type)
1949 {
1950         struct osd_ios_filldir_buf *fill_buf =
1951                 (struct osd_ios_filldir_buf *)buf;
1952         struct osd_thread_info     *info     = fill_buf->oifb_info;
1953         struct osd_device          *dev      = fill_buf->oifb_dev;
1954         struct lu_fid              *fid      = &info->oti_fid;
1955         struct osd_scrub           *scrub    = &dev->od_scrub;
1956         struct dentry              *parent   = fill_buf->oifb_dentry;
1957         struct dentry              *child;
1958         struct inode               *dir      = parent->d_inode;
1959         struct inode               *inode;
1960         int                         rc;
1961         ENTRY;
1962
1963         fill_buf->oifb_items++;
1964
1965         /* skip any '.' started names */
1966         if (name[0] == '.')
1967                 RETURN(0);
1968
1969         scrub->os_lf_scanned++;
1970         child = osd_lookup_one_len(dev, name, parent, namelen);
1971         if (IS_ERR(child)) {
1972                 rc = PTR_ERR(child);
1973                 CDEBUG(D_LFSCK, "%s: cannot lookup child '%.*s': rc = %d\n",
1974                       osd_name(dev), namelen, name, rc);
1975                 RETURN(rc);
1976         } else if (!child->d_inode) {
1977                 dput(child);
1978                 CDEBUG(D_INODE, "%s: child '%.*s' lacks inode\n",
1979                        osd_name(dev), namelen, name);
1980                 RETURN(-ENOENT);
1981         }
1982
1983         inode = child->d_inode;
1984         if (S_ISDIR(inode->i_mode)) {
1985                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1986                                       osd_ios_lf_fill);
1987                 if (rc != 0)
1988                         CDEBUG(D_LFSCK, "%s: cannot add child '%.*s': "
1989                               "rc = %d\n", osd_name(dev), namelen, name, rc);
1990                 GOTO(put, rc);
1991         }
1992
1993         if (!S_ISREG(inode->i_mode))
1994                 GOTO(put, rc = 0);
1995
1996         rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1997         if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1998                 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1999                 if (rc == 0) {
2000                         CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
2001                                "/lost+found.\n", namelen, name, PFID(fid));
2002                         scrub->os_lf_repaired++;
2003                 } else {
2004                         CDEBUG(D_LFSCK, "%s: cannot rename for '%.*s' "
2005                                DFID": rc = %d\n",
2006                                osd_name(dev), namelen, name, PFID(fid), rc);
2007                 }
2008         }
2009
2010         /* XXX: For MDT-objects, we can move them from /lost+found to namespace
2011          *      visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
2012          *      can process them in furtuer. */
2013
2014         GOTO(put, rc);
2015
2016 put:
2017         if (rc < 0)
2018                 scrub->os_lf_failed++;
2019         dput(child);
2020         /* skip the failure to make the scanning to continue. */
2021         return 0;
2022 }
2023 WRAP_FILLDIR_FN(do_, osd_ios_lf_fill)
2024
2025 #ifdef HAVE_FILLDIR_USE_CTX
2026 static FILLDIR_TYPE do_osd_ios_varfid_fill(struct dir_context *buf,
2027 #else
2028 static int osd_ios_varfid_fill(void *buf,
2029 #endif
2030                                const char *name, int namelen,
2031                                loff_t offset, __u64 ino, unsigned int d_type)
2032 {
2033         struct osd_ios_filldir_buf *fill_buf =
2034                 (struct osd_ios_filldir_buf *)buf;
2035         struct osd_device          *dev      = fill_buf->oifb_dev;
2036         struct dentry              *child;
2037         int                         rc;
2038         ENTRY;
2039
2040         fill_buf->oifb_items++;
2041
2042         /* skip any '.' started names */
2043         if (name[0] == '.')
2044                 RETURN(0);
2045
2046         child = osd_lookup_one_len(dev, name, fill_buf->oifb_dentry, namelen);
2047         if (IS_ERR(child))
2048                 RETURN(PTR_ERR(child));
2049
2050         rc = osd_ios_scan_one(fill_buf->oifb_info, dev,
2051                               fill_buf->oifb_dentry->d_inode, child->d_inode,
2052                               NULL, name, namelen, 0);
2053         if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
2054                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
2055                                       osd_ios_varfid_fill);
2056         dput(child);
2057
2058         RETURN(rc);
2059 }
2060 WRAP_FILLDIR_FN(do_, osd_ios_varfid_fill)
2061
2062 #ifdef HAVE_FILLDIR_USE_CTX
2063 static FILLDIR_TYPE do_osd_ios_dl_fill(struct dir_context *buf,
2064 #else
2065 static int osd_ios_dl_fill(void *buf,
2066 #endif
2067                            const char *name, int namelen,
2068                            loff_t offset, __u64 ino, unsigned int d_type)
2069 {
2070         struct osd_ios_filldir_buf *fill_buf =
2071                 (struct osd_ios_filldir_buf *)buf;
2072         struct osd_device          *dev      = fill_buf->oifb_dev;
2073         const struct osd_lf_map    *map;
2074         struct dentry              *child;
2075         int                         rc       = 0;
2076         ENTRY;
2077
2078         fill_buf->oifb_items++;
2079
2080         /* skip any '.' started names */
2081         if (name[0] == '.')
2082                 RETURN(0);
2083
2084         for (map = osd_dl_maps; map->olm_name != NULL; map++) {
2085                 if (map->olm_namelen != namelen)
2086                         continue;
2087
2088                 if (strncmp(map->olm_name, name, namelen) == 0)
2089                         break;
2090         }
2091
2092         if (map->olm_name == NULL)
2093                 RETURN(0);
2094
2095         child = osd_lookup_one_len(dev, name, fill_buf->oifb_dentry, namelen);
2096         if (IS_ERR(child))
2097                 RETURN(PTR_ERR(child));
2098
2099         rc = osd_ios_scan_one(fill_buf->oifb_info, dev,
2100                               fill_buf->oifb_dentry->d_inode, child->d_inode,
2101                               &map->olm_fid, name, namelen, map->olm_flags);
2102         dput(child);
2103
2104         RETURN(rc);
2105 }
2106 WRAP_FILLDIR_FN(do_, osd_ios_dl_fill)
2107
2108 #ifdef HAVE_FILLDIR_USE_CTX
2109 static FILLDIR_TYPE do_osd_ios_uld_fill(struct dir_context *buf,
2110 #else
2111 static int osd_ios_uld_fill(void *buf,
2112 #endif
2113                             const char *name, int namelen,
2114                             loff_t offset, __u64 ino, unsigned int d_type)
2115 {
2116         struct osd_ios_filldir_buf *fill_buf =
2117                 (struct osd_ios_filldir_buf *)buf;
2118         struct osd_device *dev = fill_buf->oifb_dev;
2119         struct dentry              *child;
2120         struct lu_fid               tfid;
2121         int                         rc       = 0;
2122         ENTRY;
2123
2124         fill_buf->oifb_items++;
2125
2126         /* skip any non-DFID format name */
2127         if (name[0] != '[')
2128                 RETURN(0);
2129
2130         child = osd_lookup_one_len(dev, name, fill_buf->oifb_dentry, namelen);
2131         if (IS_ERR(child))
2132                 RETURN(PTR_ERR(child));
2133
2134         /* skip the start '[' */
2135         sscanf(&name[1], SFID, RFID(&tfid));
2136         if (fid_is_sane(&tfid))
2137                 rc = osd_ios_scan_one(fill_buf->oifb_info, fill_buf->oifb_dev,
2138                                       fill_buf->oifb_dentry->d_inode,
2139                                       child->d_inode, &tfid, name, namelen, 0);
2140         else
2141                 rc = -EIO;
2142         dput(child);
2143
2144         RETURN(rc);
2145 }
2146 WRAP_FILLDIR_FN(do_, osd_ios_uld_fill)
2147
2148 #ifdef HAVE_FILLDIR_USE_CTX
2149 static FILLDIR_TYPE do_osd_ios_root_fill(struct dir_context *buf,
2150 #else
2151 static int osd_ios_root_fill(void *buf,
2152 #endif
2153                              const char *name, int namelen,
2154                              loff_t offset, __u64 ino, unsigned int d_type)
2155 {
2156         struct osd_ios_filldir_buf *fill_buf =
2157                 (struct osd_ios_filldir_buf *)buf;
2158         struct osd_device          *dev      = fill_buf->oifb_dev;
2159         const struct osd_lf_map    *map;
2160         struct dentry              *child;
2161         int                         rc       = 0;
2162         ENTRY;
2163
2164         fill_buf->oifb_items++;
2165
2166         /* skip any '.' started names */
2167         if (name[0] == '.')
2168                 RETURN(0);
2169
2170         for (map = osd_lf_maps; map->olm_name != NULL; map++) {
2171                 if (map->olm_namelen != namelen)
2172                         continue;
2173
2174                 if (strncmp(map->olm_name, name, namelen) == 0)
2175                         break;
2176         }
2177
2178         if (map->olm_name == NULL)
2179                 RETURN(0);
2180
2181         child = osd_lookup_one_len(dev, name, fill_buf->oifb_dentry, namelen);
2182         if (IS_ERR(child))
2183                 RETURN(PTR_ERR(child));
2184         else if (!child->d_inode)
2185                 GOTO(out_put, rc = -ENOENT);
2186
2187         if (!(map->olm_flags & OLF_NO_OI))
2188                 rc = osd_ios_scan_one(fill_buf->oifb_info, dev,
2189                                 fill_buf->oifb_dentry->d_inode, child->d_inode,
2190                                 &map->olm_fid, name, namelen, map->olm_flags);
2191         if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
2192                 rc = osd_ios_new_item(dev, child, map->olm_scandir,
2193                                       map->olm_filldir);
2194 out_put:
2195         dput(child);
2196
2197         RETURN(rc);
2198 }
2199
2200 WRAP_FILLDIR_FN(do_, osd_ios_root_fill)
2201
2202 static int
2203 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
2204                      struct dentry *dentry, filldir_t filldir)
2205 {
2206         struct osd_ios_filldir_buf buf = {
2207                 .ctx.actor = filldir,
2208                 .oifb_info = info,
2209                 .oifb_dev = dev,
2210                 .oifb_dentry = dentry
2211         };
2212         struct file *filp;
2213         struct path path;
2214         int rc;
2215
2216         ENTRY;
2217         LASSERT(filldir);
2218         path.dentry = dget(dentry);
2219         path.mnt = mntget(dev->od_mnt);
2220
2221         filp = dentry_open(&path, O_RDONLY, current_cred());
2222         path_put(&path);
2223         if (IS_ERR(filp))
2224                 RETURN(PTR_ERR(filp));
2225
2226         filp->f_mode |= FMODE_64BITHASH | FMODE_NONOTIFY;
2227         filp->f_flags |= O_NOATIME;
2228         filp->f_pos = 0;
2229
2230         do {
2231                 buf.oifb_items = 0;
2232                 rc = iterate_dir(filp, &buf.ctx);
2233         } while (rc >= 0 && buf.oifb_items > 0 &&
2234                  filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
2235         fput(filp);
2236
2237         RETURN(rc);
2238 }
2239
2240 static int
2241 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
2242                   struct dentry *dentry, filldir_t filldir)
2243 {
2244         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2245         struct scrub_file *sf = &scrub->os_file;
2246         struct dentry *child;
2247         int rc;
2248         ENTRY;
2249
2250         /* It is existing MDT0 device. We only allow the case of object without
2251          * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
2252          * can generate IGIF mode FID for the object and related OI mapping. If
2253          * it is on other MDTs, then becuase file-level backup/restore, related
2254          * OI mapping may be invalid already, we do not know which is the right
2255          * FID for the object. We only allow IGIF objects to reside on the MDT0.
2256          *
2257          * XXX: For the case of object on non-MDT0 device with neither LMA nor
2258          *      "fid" xattr, then something crashed. We cannot re-generate the
2259          *      FID directly, instead, the OI scrub will scan the OI structure
2260          *      and try to re-generate the LMA from the OI mapping. But if the
2261          *      OI mapping crashed or lost also, then we have to give up under
2262          *      double failure cases.
2263          */
2264         spin_lock(&scrub->os_lock);
2265         scrub->os_convert_igif = 1;
2266         spin_unlock(&scrub->os_lock);
2267         child = osd_lookup_one_len_unlocked(dev, dot_lustre_name, dentry,
2268                                             strlen(dot_lustre_name));
2269         if (IS_ERR(child)) {
2270                 if (PTR_ERR(child) != -ENOENT)
2271                         RETURN(PTR_ERR(child));
2272                 goto out_scrub;
2273         }
2274
2275         /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
2276          * so the client will get IGIF for the ".lustre" object when
2277          * the MDT restart.
2278          *
2279          * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
2280          * it does not know whether there are some old clients cached
2281          * the ".lustre" IGIF during the upgrading. Two choices:
2282          *
2283          * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
2284          *    It will allow the old connected clients to access the
2285          *    ".lustre" with cached IGIF. But it will cause others
2286          *    on the MDT failed to check "fid_is_dot_lustre()".
2287          *
2288          * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
2289          *    for ".lustre" in spite of whether there are some clients
2290          *    cached the ".lustre" IGIF or not. It enables the check
2291          *    "fid_is_dot_lustre()" on the MDT, although it will cause
2292          *    that the old connected clients cannot access the ".lustre"
2293          *    with the cached IGIF.
2294          *
2295          * Usually, it is rare case for the old connected clients
2296          * to access the ".lustre" with cached IGIF. So we prefer
2297          * to the solution 2).
2298          */
2299         inode_lock(dentry->d_inode);
2300         rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2301                               child->d_inode, &LU_DOT_LUSTRE_FID,
2302                               dot_lustre_name,
2303                               strlen(dot_lustre_name), 0);
2304         inode_unlock(dentry->d_inode);
2305         if (rc == -ENOENT) {
2306 out_scrub:
2307                 /* It is 1.8 MDT device. */
2308                 if (!(sf->sf_flags & SF_UPGRADE)) {
2309                         scrub_file_reset(scrub, dev->od_uuid,
2310                                          SF_UPGRADE);
2311                         sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
2312                         rc = scrub_file_store(info->oti_env, scrub);
2313                 } else {
2314                         rc = 0;
2315                 }
2316         } else if (rc == 0) {
2317                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
2318                                       osd_ios_dl_fill);
2319         }
2320         dput(child);
2321
2322         RETURN(rc);
2323 }
2324
2325 static int
2326 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
2327                      struct dentry *dentry, filldir_t filldir)
2328 {
2329         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2330         struct scrub_file *sf = &scrub->os_file;
2331         struct dentry *child;
2332         int rc;
2333         ENTRY;
2334
2335         if (unlikely(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID)) {
2336                 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
2337                 rc = scrub_file_store(info->oti_env, scrub);
2338                 if (rc != 0)
2339                         RETURN(rc);
2340         }
2341
2342         child = osd_lookup_one_len_unlocked(dev, ADMIN_USR, dentry,
2343                                             strlen(ADMIN_USR));
2344         if (IS_ERR(child)) {
2345                 rc = PTR_ERR(child);
2346         } else {
2347                 inode_lock(dentry->d_inode);
2348                 rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2349                                       child->d_inode, NULL, ADMIN_USR,
2350                                       strlen(ADMIN_USR), 0);
2351                 inode_unlock(dentry->d_inode);
2352                 dput(child);
2353         }
2354
2355         if (rc != 0 && rc != -ENOENT)
2356                 GOTO(out, rc);
2357
2358         child = osd_lookup_one_len_unlocked(dev, ADMIN_GRP, dentry,
2359                                             strlen(ADMIN_GRP));
2360         if (IS_ERR(child))
2361                 GOTO(out, rc = PTR_ERR(child));
2362
2363         inode_lock(dentry->d_inode);
2364         rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2365                               child->d_inode, NULL, ADMIN_GRP,
2366                               strlen(ADMIN_GRP), 0);
2367         inode_unlock(dentry->d_inode);
2368         dput(child);
2369 out:
2370         RETURN(rc == -ENOENT ? 0 : rc);
2371 }
2372
2373 static void osd_initial_OI_scrub(struct osd_thread_info *info,
2374                                  struct osd_device *dev)
2375 {
2376         struct osd_ios_item     *item    = NULL;
2377         scandir_t                scandir = osd_ios_general_scan;
2378         filldir_t                filldir = osd_ios_root_fill;
2379         struct dentry           *dentry  = osd_sb(dev)->s_root;
2380         const struct osd_lf_map *map     = osd_lf_maps;
2381         ENTRY;
2382
2383         /* Lookup IGIF in OI by force for initial OI scrub. */
2384         dev->od_igif_inoi = 1;
2385
2386         while (1) {
2387                 /* Don't take inode_lock here since scandir() callbacks
2388                  * can call VFS functions which may manully take the
2389                  * inode lock itself like iterate_dir(). Since this
2390                  * is the case it is best to leave the scandir()
2391                  * callbacks to managing the inode lock.
2392                  */
2393                 scandir(info, dev, dentry, filldir);
2394                 if (item != NULL) {
2395                         dput(item->oii_dentry);
2396                         OBD_FREE_PTR(item);
2397                 }
2398
2399                 if (list_empty(&dev->od_ios_list))
2400                         break;
2401
2402                 item = list_first_entry(&dev->od_ios_list,
2403                                         struct osd_ios_item, oii_list);
2404                 list_del_init(&item->oii_list);
2405
2406                 LASSERT(item->oii_scandir != NULL);
2407                 scandir = item->oii_scandir;
2408                 filldir = item->oii_filldir;
2409                 dentry = item->oii_dentry;
2410         }
2411
2412         /* There maybe the case that the object has been removed, but its OI
2413          * mapping is still in the OI file, such as the "CATALOGS" after MDT
2414          * file-level backup/restore. So here cleanup the stale OI mappings. */
2415         while (map->olm_name != NULL) {
2416                 struct dentry *child;
2417
2418                 if (fid_is_zero(&map->olm_fid)) {
2419                         map++;
2420                         continue;
2421                 }
2422
2423                 child = osd_lookup_one_len_unlocked(dev, map->olm_name,
2424                                                     osd_sb(dev)->s_root,
2425                                                     map->olm_namelen);
2426                 if (PTR_ERR(child) == -ENOENT ||
2427                     (!IS_ERR(child) && !child->d_inode))
2428                         osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
2429                                                   NULL, DTO_INDEX_DELETE,
2430                                                   true, 0, NULL);
2431                 if (!IS_ERR(child))
2432                         dput(child);
2433                 map++;
2434         }
2435
2436         if (!list_empty(&dev->od_index_restore_list)) {
2437                 char *buf;
2438
2439                 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
2440                 if (!buf)
2441                         CERROR("%s: not enough RAM for rebuild index\n",
2442                                osd_name(dev));
2443
2444                 while (!list_empty(&dev->od_index_restore_list)) {
2445                         struct lustre_index_restore_unit *liru;
2446
2447                         liru = list_first_entry(&dev->od_index_restore_list,
2448                                                 struct lustre_index_restore_unit,
2449                                                 liru_link);
2450                         list_del(&liru->liru_link);
2451                         if (buf)
2452                                 osd_index_restore(info->oti_env, dev, liru,
2453                                                   buf, INDEX_BACKUP_BUFSIZE);
2454                         OBD_FREE(liru, liru->liru_len);
2455                 }
2456
2457                 if (buf)
2458                         OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
2459         }
2460
2461         EXIT;
2462 }
2463
2464 char *osd_lf_fid2name(const struct lu_fid *fid)
2465 {
2466         const struct osd_lf_map *map = osd_lf_maps;
2467
2468         while (map->olm_name != NULL) {
2469                 if (!lu_fid_eq(fid, &map->olm_fid)) {
2470                         map++;
2471                         continue;
2472                 }
2473
2474                 if (map->olm_flags & OLF_SHOW_NAME)
2475                         return map->olm_name;
2476                 else
2477                         return "";
2478         }
2479
2480         return NULL;
2481 }
2482
2483 /* OI scrub start/stop */
2484
2485 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
2486                     __u32 flags)
2487 {
2488         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2489         int rc;
2490         ENTRY;
2491
2492         if (dev->od_dt_dev.dd_rdonly)
2493                 RETURN(-EROFS);
2494
2495         /* od_otable_mutex: prevent curcurrent start/stop */
2496         mutex_lock(&dev->od_otable_mutex);
2497         rc = scrub_start(osd_scrub_main, scrub, dev, flags);
2498         if (rc == -EALREADY) {
2499                 rc = 0;
2500                 if ((scrub->os_file.sf_flags & SF_AUTO ||
2501                      scrub->os_partial_scan) &&
2502                     !(flags & SS_AUTO_PARTIAL))
2503                         osd_scrub_join(env, dev, flags, false);
2504         }
2505         mutex_unlock(&dev->od_otable_mutex);
2506
2507         RETURN(rc);
2508 }
2509
2510 void osd_scrub_stop(struct osd_device *dev)
2511 {
2512         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2513
2514         /* od_otable_mutex: prevent curcurrent start/stop */
2515         mutex_lock(&dev->od_otable_mutex);
2516         spin_lock(&scrub->os_lock);
2517         scrub->os_paused = 1;
2518         spin_unlock(&scrub->os_lock);
2519         scrub_stop(scrub);
2520         mutex_unlock(&dev->od_otable_mutex);
2521
2522         osd_scrub_ois_fini(scrub, &scrub->os_inconsistent_items);
2523         osd_scrub_ois_fini(scrub, &scrub->os_stale_items);
2524 }
2525
2526 /* OI scrub setup/cleanup */
2527
2528 static const char osd_scrub_name[] = "OI_scrub";
2529
2530 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev,
2531                     bool restored)
2532 {
2533         struct osd_thread_info *info = osd_oti_get(env);
2534         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2535         struct lvfs_run_ctxt *ctxt = &dev->od_scrub.os_ctxt;
2536         time64_t interval = scrub->os_auto_scrub_interval;
2537         struct scrub_file *sf = &scrub->os_file;
2538         struct super_block *sb = osd_sb(dev);
2539         struct lvfs_run_ctxt saved;
2540         struct file *filp;
2541         struct inode *inode;
2542         struct lu_fid *fid = &info->oti_fid;
2543         struct osd_inode_id *id = &info->oti_id;
2544         struct dt_object *obj;
2545         bool dirty = false;
2546         int rc = 0;
2547         ENTRY;
2548
2549         memset(&dev->od_scrub, 0, sizeof(struct osd_scrub));
2550         OBD_SET_CTXT_MAGIC(ctxt);
2551         ctxt->pwdmnt = dev->od_mnt;
2552         ctxt->pwd = dev->od_mnt->mnt_root;
2553
2554         init_rwsem(&scrub->os_rwsem);
2555         spin_lock_init(&scrub->os_lock);
2556         INIT_LIST_HEAD(&scrub->os_inconsistent_items);
2557         INIT_LIST_HEAD(&scrub->os_stale_items);
2558         scrub->os_name = osd_name(dev);
2559         scrub->os_auto_scrub_interval = interval;
2560
2561         push_ctxt(&saved, ctxt);
2562         filp = filp_open(osd_scrub_name,
2563                          (dev->od_dt_dev.dd_rdonly ? O_RDONLY :
2564                                                      O_RDWR | O_CREAT),
2565                          0644);
2566         if (IS_ERR(filp)) {
2567                 pop_ctxt(&saved, ctxt);
2568                 RETURN(PTR_ERR(filp));
2569         }
2570
2571         inode = file_inode(filp);
2572         ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2573         if (!dev->od_dt_dev.dd_rdonly) {
2574                 /* 'What the @fid is' is not imporatant, because the object
2575                  * has no OI mapping, and only is visible inside the OSD.*/
2576                 lu_igif_build(fid, inode->i_ino, inode->i_generation);
2577                 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
2578                 if (rc) {
2579                         filp_close(filp, NULL);
2580                         pop_ctxt(&saved, ctxt);
2581                         RETURN(rc);
2582                 }
2583         }
2584
2585         osd_id_gen(id, inode->i_ino, inode->i_generation);
2586         osd_add_oi_cache(info, dev, id, fid);
2587         filp_close(filp, NULL);
2588         pop_ctxt(&saved, ctxt);
2589
2590         obj = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, NULL));
2591         if (IS_ERR_OR_NULL(obj))
2592                 RETURN(obj ? PTR_ERR(obj) : -ENOENT);
2593
2594         guid_copy(&dev->od_uuid, (guid_t *)&sb->s_uuid);
2595         scrub->os_obj = obj;
2596         rc = scrub_file_load(env, scrub);
2597         if (rc == -ENOENT || rc == -EFAULT) {
2598                 scrub_file_init(scrub, dev->od_uuid);
2599                 /* If the "/O" dir does not exist when mount (indicated by
2600                  * osd_device::od_maybe_new), neither for the "/OI_scrub",
2601                  * then it is quite probably that the device is a new one,
2602                  * under such case, mark it as SIF_NO_HANDLE_OLD_FID.
2603                  *
2604                  * For the rare case that "/O" and "OI_scrub" both lost on
2605                  * an old device, it can be found and cleared later.
2606                  *
2607                  * For the system with "SIF_NO_HANDLE_OLD_FID", we do not
2608                  * need to check "filter_fid_18_23" and to convert it to
2609                  * "filter_fid" for each object, and all the IGIF should
2610                  * have their FID mapping in OI files already. */
2611                 if (dev->od_maybe_new && rc == -ENOENT)
2612                         sf->sf_internal_flags = SIF_NO_HANDLE_OLD_FID;
2613                 dirty = true;
2614         } else if (rc < 0) {
2615                 GOTO(cleanup_obj, rc);
2616         } else {
2617                 if (!guid_equal(&sf->sf_uuid, &dev->od_uuid)) {
2618                         CDEBUG(D_LFSCK,
2619                                "%s: UUID has been changed from %pU to %pU\n",
2620                                osd_dev2name(dev), &sf->sf_uuid, &dev->od_uuid);
2621                         scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
2622                         dirty = true;
2623                         restored = true;
2624                 } else if (sf->sf_status == SS_SCANNING) {
2625                         sf->sf_status = SS_CRASHED;
2626                         dirty = true;
2627                 }
2628
2629                 if ((sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0) {
2630                         LCONSOLE_WARN("%s: invalid oi count %d, set it to %d\n",
2631                                       osd_dev2name(dev), sf->sf_oi_count,
2632                                       osd_oi_count);
2633                         sf->sf_oi_count = osd_oi_count;
2634                         dirty = true;
2635                 }
2636         }
2637
2638         if (sf->sf_pos_last_checkpoint != 0)
2639                 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
2640         else
2641                 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
2642
2643         if (dirty) {
2644                 rc = scrub_file_store(env, scrub);
2645                 if (rc)
2646                         GOTO(cleanup_obj, rc);
2647         }
2648
2649         /* Initialize OI files. */
2650         rc = osd_oi_init(info, dev, restored);
2651         if (rc < 0)
2652                 GOTO(cleanup_obj, rc);
2653
2654         if (!dev->od_dt_dev.dd_rdonly)
2655                 osd_initial_OI_scrub(info, dev);
2656
2657         if (sf->sf_flags & SF_UPGRADE ||
2658             !(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID ||
2659               sf->sf_success_count > 0)) {
2660                 dev->od_igif_inoi = 0;
2661                 dev->od_check_ff = dev->od_is_ost;
2662         } else {
2663                 dev->od_igif_inoi = 1;
2664                 dev->od_check_ff = 0;
2665         }
2666
2667         if (sf->sf_flags & SF_INCONSISTENT)
2668                 /* The 'od_igif_inoi' will be set under the
2669                  * following cases:
2670                  * 1) new created system, or
2671                  * 2) restored from file-level backup, or
2672                  * 3) the upgrading completed.
2673                  *
2674                  * The 'od_igif_inoi' may be cleared by OI scrub
2675                  * later if found that the system is upgrading. */
2676                 dev->od_igif_inoi = 1;
2677
2678         if (!dev->od_dt_dev.dd_rdonly &&
2679             dev->od_scrub.os_scrub.os_auto_scrub_interval != AS_NEVER &&
2680             ((sf->sf_status == SS_PAUSED) ||
2681              (sf->sf_status == SS_CRASHED &&
2682               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2683                               SF_UPGRADE | SF_AUTO)) ||
2684              (sf->sf_status == SS_INIT &&
2685               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2686                               SF_UPGRADE))))
2687                 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
2688
2689         if (rc != 0)
2690                 GOTO(cleanup_oi, rc);
2691
2692         /* it is possible that dcache entries may keep objects after they are
2693          * deleted by OSD. While it looks safe this can cause object data to
2694          * stay until umount causing failures in tests calculating free space,
2695          * e.g. replay-ost-single. Since those dcache entries are not used
2696          * anymore let's just free them after use here */
2697         shrink_dcache_sb(sb);
2698
2699         RETURN(0);
2700 cleanup_oi:
2701         osd_oi_fini(info, dev);
2702 cleanup_obj:
2703         dt_object_put_nocache(env, scrub->os_obj);
2704         scrub->os_obj = NULL;
2705
2706         return rc;
2707 }
2708
2709 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2710 {
2711         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2712
2713         LASSERT(dev->od_otable_it == NULL);
2714
2715         if (scrub->os_obj != NULL) {
2716                 osd_scrub_stop(dev);
2717                 dt_object_put_nocache(env, scrub->os_obj);
2718                 scrub->os_obj = NULL;
2719         }
2720 }
2721
2722 /* object table based iteration APIs */
2723
2724 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2725                                        struct dt_object *dt, __u32 attr)
2726 {
2727         enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2728         enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2729         struct osd_device      *dev   = osd_dev(dt->do_lu.lo_dev);
2730         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2731         struct osd_otable_it   *it;
2732         __u32                   start = 0;
2733         int                     rc;
2734         ENTRY;
2735
2736         /* od_otable_mutex: prevent curcurrent init/fini */
2737         mutex_lock(&dev->od_otable_mutex);
2738         if (dev->od_otable_it != NULL)
2739                 GOTO(out, it = ERR_PTR(-EALREADY));
2740
2741         OBD_ALLOC_PTR(it);
2742         if (it == NULL)
2743                 GOTO(out, it = ERR_PTR(-ENOMEM));
2744
2745         dev->od_otable_it = it;
2746         it->ooi_dev = dev;
2747         it->ooi_cache.ooc_consumer_idx = -1;
2748         if (flags & DOIF_OUTUSED)
2749                 it->ooi_used_outside = 1;
2750
2751         if (flags & DOIF_RESET)
2752                 start |= SS_RESET;
2753
2754         if (valid & DOIV_ERROR_HANDLE) {
2755                 if (flags & DOIF_FAILOUT)
2756                         start |= SS_SET_FAILOUT;
2757                 else
2758                         start |= SS_CLEAR_FAILOUT;
2759         }
2760
2761         if (valid & DOIV_DRYRUN) {
2762                 if (flags & DOIF_DRYRUN)
2763                         start |= SS_SET_DRYRUN;
2764                 else
2765                         start |= SS_CLEAR_DRYRUN;
2766         }
2767
2768         rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
2769         if (rc == -EALREADY) {
2770                 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2771         } else  if (rc < 0) {
2772                 dev->od_otable_it = NULL;
2773                 OBD_FREE_PTR(it);
2774                 it = ERR_PTR(rc);
2775         } else {
2776                 /* We have to start from the begining. */
2777                 it->ooi_cache.ooc_pos_preload =
2778                         LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2779         }
2780
2781         GOTO(out, it);
2782
2783 out:
2784         mutex_unlock(&dev->od_otable_mutex);
2785         return (struct dt_it *)it;
2786 }
2787
2788 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2789 {
2790         struct osd_otable_it *it  = (struct osd_otable_it *)di;
2791         struct osd_device    *dev = it->ooi_dev;
2792
2793         /* od_otable_mutex: prevent curcurrent init/fini */
2794         mutex_lock(&dev->od_otable_mutex);
2795         scrub_stop(&dev->od_scrub.os_scrub);
2796         LASSERT(dev->od_otable_it == it);
2797
2798         dev->od_otable_it = NULL;
2799         mutex_unlock(&dev->od_otable_mutex);
2800         OBD_FREE_PTR(it);
2801 }
2802
2803 static int osd_otable_it_get(const struct lu_env *env,
2804                              struct dt_it *di, const struct dt_key *key)
2805 {
2806         return 0;
2807 }
2808
2809 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2810 {
2811 }
2812
2813 static inline int
2814 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
2815 {
2816         spin_lock(&scrub->os_lock);
2817         if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2818             scrub->os_waiting || !scrub->os_running)
2819                 it->ooi_waiting = 0;
2820         else
2821                 it->ooi_waiting = 1;
2822         spin_unlock(&scrub->os_lock);
2823
2824         return !it->ooi_waiting;
2825 }
2826
2827 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2828 {
2829         struct osd_otable_it *it = (struct osd_otable_it *)di;
2830         struct osd_device *dev = it->ooi_dev;
2831         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2832         struct osd_otable_cache *ooc = &it->ooi_cache;
2833         int rc;
2834         ENTRY;
2835
2836         LASSERT(it->ooi_user_ready);
2837
2838 again:
2839         if (!scrub->os_running && !it->ooi_used_outside)
2840                 RETURN(1);
2841
2842         if (ooc->ooc_cached_items > 0) {
2843                 ooc->ooc_cached_items--;
2844                 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2845                                         ~OSD_OTABLE_IT_CACHE_MASK;
2846                 RETURN(0);
2847         }
2848
2849         if (it->ooi_all_cached) {
2850                 wait_var_event(scrub, !scrub->os_running);
2851                 RETURN(1);
2852         }
2853
2854         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2855                 spin_lock(&scrub->os_lock);
2856                 scrub->os_waiting = 0;
2857                 wake_up_var(scrub);
2858                 spin_unlock(&scrub->os_lock);
2859         }
2860
2861         if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2862                 wait_var_event(scrub, osd_otable_it_wakeup(scrub, it));
2863
2864         if (!scrub->os_running && !it->ooi_used_outside)
2865                 RETURN(1);
2866
2867         rc = osd_otable_it_preload(env, it);
2868         if (rc >= 0)
2869                 goto again;
2870
2871         RETURN(rc);
2872 }
2873
2874 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2875                                         const struct dt_it *di)
2876 {
2877         return NULL;
2878 }
2879
2880 static int osd_otable_it_key_size(const struct lu_env *env,
2881                                   const struct dt_it *di)
2882 {
2883         return sizeof(__u64);
2884 }
2885
2886 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2887                              struct dt_rec *rec, __u32 attr)
2888 {
2889         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2890         struct osd_otable_cache *ooc = &it->ooi_cache;
2891
2892         *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2893
2894         /* Filter out Invald FID already. */
2895         LASSERTF(fid_is_sane((struct lu_fid *)rec),
2896                  "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2897                  PFID((struct lu_fid *)rec),
2898                  ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2899
2900         return 0;
2901 }
2902
2903 static __u64 osd_otable_it_store(const struct lu_env *env,
2904                                  const struct dt_it *di)
2905 {
2906         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2907         struct osd_otable_cache *ooc = &it->ooi_cache;
2908         __u64                    hash;
2909
2910         if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2911                 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2912         else
2913                 hash = ooc->ooc_pos_preload;
2914         return hash;
2915 }
2916
2917 /**
2918  * Set the OSD layer iteration start position as the specified hash.
2919  */
2920 static int osd_otable_it_load(const struct lu_env *env,
2921                               const struct dt_it *di, __u64 hash)
2922 {
2923         struct osd_otable_it    *it    = (struct osd_otable_it *)di;
2924         struct osd_device       *dev   = it->ooi_dev;
2925         struct osd_otable_cache *ooc   = &it->ooi_cache;
2926         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2927         struct osd_iit_param    *param = &it->ooi_iit_param;
2928         int                      rc;
2929         ENTRY;
2930
2931         /* Forbid to set iteration position after iteration started. */
2932         if (it->ooi_user_ready)
2933                 RETURN(-EPERM);
2934
2935         LASSERT(!scrub->os_partial_scan);
2936
2937         if (hash > OSD_OTABLE_MAX_HASH)
2938                 hash = OSD_OTABLE_MAX_HASH;
2939
2940         /* The hash is the last checkpoint position,
2941          * we will start from the next one. */
2942         ooc->ooc_pos_preload = hash + 1;
2943         if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2944                 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2945
2946         it->ooi_user_ready = 1;
2947         if (!scrub->os_full_speed)
2948                 wake_up_var(scrub);
2949
2950         memset(param, 0, sizeof(*param));
2951         param->sb = osd_sb(dev);
2952         param->start = ooc->ooc_pos_preload;
2953         param->bg = (ooc->ooc_pos_preload - 1) /
2954                     LDISKFS_INODES_PER_GROUP(param->sb);
2955         param->offset = (ooc->ooc_pos_preload - 1) %
2956                         LDISKFS_INODES_PER_GROUP(param->sb);
2957         param->gbase = 1 + param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
2958
2959         /* Unplug OSD layer iteration by the first next() call. */
2960         rc = osd_otable_it_next(env, (struct dt_it *)it);
2961
2962         RETURN(rc);
2963 }
2964
2965 static int osd_otable_it_key_rec(const struct lu_env *env,
2966                                  const struct dt_it *di, void *key_rec)
2967 {
2968         return 0;
2969 }
2970
2971 const struct dt_index_operations osd_otable_ops = {
2972         .dio_it = {
2973                 .init     = osd_otable_it_init,
2974                 .fini     = osd_otable_it_fini,
2975                 .get      = osd_otable_it_get,
2976                 .put      = osd_otable_it_put,
2977                 .next     = osd_otable_it_next,
2978                 .key      = osd_otable_it_key,
2979                 .key_size = osd_otable_it_key_size,
2980                 .rec      = osd_otable_it_rec,
2981                 .store    = osd_otable_it_store,
2982                 .load     = osd_otable_it_load,
2983                 .key_rec  = osd_otable_it_key_rec,
2984         }
2985 };
2986
2987 void osd_scrub_dump(struct seq_file *m, struct osd_device *dev)
2988 {
2989         struct osd_scrub *scrub = &dev->od_scrub;
2990
2991         scrub_dump(m, &scrub->os_scrub);
2992         seq_printf(m, "lf_scanned: %llu\n"
2993                    "lf_%s: %llu\n"
2994                    "lf_failed: %llu\n",
2995                    scrub->os_lf_scanned,
2996                    scrub->os_scrub.os_file.sf_param & SP_DRYRUN ?
2997                         "inconsistent" : "repaired",
2998                    scrub->os_lf_repaired,
2999                    scrub->os_lf_failed);
3000 }
3001
3002 typedef int (*scan_dir_helper_t)(const struct lu_env *env,
3003                                  struct osd_device *dev, struct inode *dir,
3004                                  struct osd_it_ea *oie);
3005
3006 static int osd_scan_dir(const struct lu_env *env, struct osd_device *dev,
3007                         struct inode *inode, scan_dir_helper_t cb)
3008 {
3009         struct osd_it_ea *oie;
3010         int rc;
3011
3012         ENTRY;
3013
3014         oie = osd_it_dir_init(env, dev, inode, LUDA_TYPE);
3015         if (IS_ERR(oie))
3016                 RETURN(PTR_ERR(oie));
3017
3018         oie->oie_file->f_pos = 0;
3019         rc = osd_ldiskfs_it_fill(env, (struct dt_it *)oie);
3020         if (rc > 0)
3021                 rc = -ENODATA;
3022         if (rc)
3023                 GOTO(out, rc);
3024
3025         while (oie->oie_it_dirent <= oie->oie_rd_dirent) {
3026                 if (!name_is_dot_or_dotdot(oie->oie_dirent->oied_name,
3027                                            oie->oie_dirent->oied_namelen))
3028                         cb(env, dev, inode, oie);
3029
3030                 oie->oie_dirent = (void *)oie->oie_dirent +
3031                                   round_up(sizeof(struct osd_it_ea_dirent) +
3032                                            oie->oie_dirent->oied_namelen, 8);
3033
3034                 oie->oie_it_dirent++;
3035                 if (oie->oie_it_dirent <= oie->oie_rd_dirent)
3036                         continue;
3037
3038                 if (oie->oie_file->f_pos ==
3039                     ldiskfs_get_htree_eof(oie->oie_file))
3040                         break;
3041
3042                 rc = osd_ldiskfs_it_fill(env, (struct dt_it *)oie);
3043                 if (rc) {
3044                         if (rc > 0)
3045                                 rc = 0;
3046                         break;
3047                 }
3048         }
3049
3050 out:
3051         osd_it_dir_fini(env, oie, inode);
3052         RETURN(rc);
3053 }
3054
3055 static int osd_remove_ml_file(struct osd_thread_info *info,
3056                               struct osd_device *dev, struct inode *dir,
3057                               struct inode *inode, struct osd_it_ea *oie)
3058 {
3059         handle_t *th;
3060         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
3061         struct dentry dentry;
3062         int rc;
3063
3064         ENTRY;
3065
3066         if (scrub->os_file.sf_param & SP_DRYRUN)
3067                 RETURN(0);
3068
3069         th = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC,
3070                                   osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3071                                   osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3072         if (IS_ERR(th))
3073                 RETURN(PTR_ERR(th));
3074
3075         /* Should be created by the VFS layer */
3076         dentry.d_inode = dir;
3077         dentry.d_sb = dir->i_sb;
3078         rc = osd_obj_del_entry(info, dev, &dentry, oie->oie_dirent->oied_name,
3079                                oie->oie_dirent->oied_namelen, th);
3080         drop_nlink(inode);
3081         mark_inode_dirty(inode);
3082         ldiskfs_journal_stop(th);
3083         RETURN(rc);
3084 }
3085
3086 static int osd_scan_ml_file(const struct lu_env *env, struct osd_device *dev,
3087                             struct inode *dir, struct osd_it_ea *oie)
3088 {
3089         struct osd_thread_info *info = osd_oti_get(env);
3090         struct osd_inode_id id;
3091         struct inode *inode;
3092         struct osd_obj_seq *oseq;
3093         struct ost_id *ostid = &info->oti_ostid;
3094         struct lu_fid *fid = &oie->oie_dirent->oied_fid;
3095         char name[32];
3096         int dirn, rc = 0;
3097
3098         ENTRY;
3099
3100         osd_id_gen(&id, oie->oie_dirent->oied_ino, OSD_OII_NOGEN);
3101
3102         if (!fid_is_sane(fid))
3103                 inode = osd_iget_fid(info, dev, &id, fid);
3104         else
3105                 inode = osd_iget(info, dev, &id);
3106
3107         if (IS_ERR(inode))
3108                 RETURN(PTR_ERR(inode));
3109
3110         fid_to_ostid(fid, ostid);
3111         oseq = osd_seq_load(info, dev, ostid_seq(ostid));
3112         if (IS_ERR(oseq))
3113                 RETURN(PTR_ERR(oseq));
3114
3115         dirn = ostid_id(ostid) & (oseq->oos_subdir_count - 1);
3116         LASSERT(oseq->oos_dirs[dirn] != NULL);
3117
3118         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
3119         if (((strlen(oseq->oos_root->d_name.name) !=
3120               info->oti_seq_dirent->oied_namelen) ||
3121              strncmp(oseq->oos_root->d_name.name,
3122                      info->oti_seq_dirent->oied_name,
3123                      info->oti_seq_dirent->oied_namelen) != 0) ||
3124             ((strlen(oseq->oos_dirs[dirn]->d_name.name) !=
3125               info->oti_dir_dirent->oied_namelen) ||
3126              strncmp(oseq->oos_dirs[dirn]->d_name.name,
3127                      info->oti_dir_dirent->oied_name,
3128                      info->oti_dir_dirent->oied_namelen) != 0) ||
3129             ((strlen(name) != oie->oie_dirent->oied_namelen) ||
3130              strncmp(oie->oie_dirent->oied_name, name,
3131                      oie->oie_dirent->oied_namelen) != 0)) {
3132                 CDEBUG(D_LFSCK, "%s: the file O/%s/%s/%s is corrupted\n",
3133                        osd_name(dev), info->oti_seq_dirent->oied_name,
3134                        info->oti_dir_dirent->oied_name,
3135                        oie->oie_dirent->oied_name);
3136
3137                 rc = osd_remove_ml_file(info, dev, dir, inode, oie);
3138         }
3139
3140         iput(inode);
3141         RETURN(rc);
3142 }
3143
3144 static int osd_scan_ml_file_dir(const struct lu_env *env,
3145                                 struct osd_device *dev, struct inode *dir,
3146                                 struct osd_it_ea *oie)
3147 {
3148         struct osd_thread_info *info = osd_oti_get(env);
3149         struct inode *inode;
3150         struct osd_inode_id id;
3151         int rc;
3152
3153         ENTRY;
3154
3155         osd_id_gen(&id, oie->oie_dirent->oied_ino, OSD_OII_NOGEN);
3156         inode = osd_iget(info, dev, &id);
3157         if (IS_ERR(inode))
3158                 RETURN(PTR_ERR(inode));
3159
3160         if (!S_ISDIR(inode->i_mode))
3161                 GOTO(out, rc = 0);
3162
3163         info->oti_dir_dirent = oie->oie_dirent;
3164         rc = osd_scan_dir(env, dev, inode, osd_scan_ml_file);
3165         info->oti_dir_dirent = NULL;
3166
3167 out:
3168         iput(inode);
3169         RETURN(rc);
3170 }
3171
3172 static int osd_scan_ml_file_seq(const struct lu_env *env,
3173                                 struct osd_device *dev, struct inode *dir,
3174                                 struct osd_it_ea *oie)
3175 {
3176         struct osd_thread_info *info = osd_oti_get(env);
3177         struct inode *inode;
3178         struct osd_inode_id id;
3179         int rc;
3180
3181         ENTRY;
3182
3183         osd_id_gen(&id, oie->oie_dirent->oied_ino, OSD_OII_NOGEN);
3184         inode = osd_iget(info, dev, &id);
3185         if (IS_ERR(inode))
3186                 RETURN(PTR_ERR(inode));
3187
3188         if (!S_ISDIR(inode->i_mode))
3189                 GOTO(out, rc = 0);
3190
3191         info->oti_seq_dirent = oie->oie_dirent;
3192         rc = osd_scan_dir(env, dev, inode, osd_scan_ml_file_dir);
3193         info->oti_seq_dirent = NULL;
3194
3195 out:
3196         iput(inode);
3197         RETURN(rc);
3198 }
3199
3200 static int osd_scan_ml_file_main(const struct lu_env *env,
3201                                  struct osd_device *dev)
3202 {
3203         return osd_scan_dir(env, dev, dev->od_ost_map->om_root->d_inode,
3204                             osd_scan_ml_file_seq);
3205 }