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