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