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