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