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