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