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