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