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