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