Whamcloud - gitweb
LU-13783 osd-ldiskfs: use alloc_file_pseudo to create fake files
[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 path path;
2146         int rc;
2147
2148         ENTRY;
2149         LASSERT(filldir);
2150         path.dentry = dget(dentry);
2151         path.mnt = mntget(dev->od_mnt);
2152
2153         filp = dentry_open(&path, O_RDONLY, current_cred());
2154         path_put(&path);
2155         if (IS_ERR(filp))
2156                 RETURN(PTR_ERR(filp));
2157
2158         filp->f_mode |= FMODE_64BITHASH | FMODE_NONOTIFY;
2159         filp->f_flags |= O_NOATIME;
2160         filp->f_pos = 0;
2161
2162         do {
2163                 buf.oifb_items = 0;
2164                 rc = iterate_dir(filp, &buf.ctx);
2165         } while (rc >= 0 && buf.oifb_items > 0 &&
2166                  filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
2167         fput(filp);
2168
2169         RETURN(rc);
2170 }
2171
2172 static int
2173 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
2174                   struct dentry *dentry, filldir_t filldir)
2175 {
2176         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2177         struct scrub_file *sf = &scrub->os_file;
2178         struct dentry *child;
2179         int rc;
2180         ENTRY;
2181
2182         /* It is existing MDT0 device. We only allow the case of object without
2183          * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
2184          * can generate IGIF mode FID for the object and related OI mapping. If
2185          * it is on other MDTs, then becuase file-level backup/restore, related
2186          * OI mapping may be invalid already, we do not know which is the right
2187          * FID for the object. We only allow IGIF objects to reside on the MDT0.
2188          *
2189          * XXX: For the case of object on non-MDT0 device with neither LMA nor
2190          *      "fid" xattr, then something crashed. We cannot re-generate the
2191          *      FID directly, instead, the OI scrub will scan the OI structure
2192          *      and try to re-generate the LMA from the OI mapping. But if the
2193          *      OI mapping crashed or lost also, then we have to give up under
2194          *      double failure cases.
2195          */
2196         spin_lock(&scrub->os_lock);
2197         scrub->os_convert_igif = 1;
2198         spin_unlock(&scrub->os_lock);
2199         child = osd_lookup_one_len_unlocked(dev, dot_lustre_name, dentry,
2200                                             strlen(dot_lustre_name));
2201         if (IS_ERR(child)) {
2202                 if (PTR_ERR(child) != -ENOENT)
2203                         RETURN(PTR_ERR(child));
2204                 goto out_scrub;
2205         }
2206
2207         /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
2208          * so the client will get IGIF for the ".lustre" object when
2209          * the MDT restart.
2210          *
2211          * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
2212          * it does not know whether there are some old clients cached
2213          * the ".lustre" IGIF during the upgrading. Two choices:
2214          *
2215          * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
2216          *    It will allow the old connected clients to access the
2217          *    ".lustre" with cached IGIF. But it will cause others
2218          *    on the MDT failed to check "fid_is_dot_lustre()".
2219          *
2220          * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
2221          *    for ".lustre" in spite of whether there are some clients
2222          *    cached the ".lustre" IGIF or not. It enables the check
2223          *    "fid_is_dot_lustre()" on the MDT, although it will cause
2224          *    that the old connected clients cannot access the ".lustre"
2225          *    with the cached IGIF.
2226          *
2227          * Usually, it is rare case for the old connected clients
2228          * to access the ".lustre" with cached IGIF. So we prefer
2229          * to the solution 2).
2230          */
2231         inode_lock(dentry->d_inode);
2232         rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2233                               child->d_inode, &LU_DOT_LUSTRE_FID,
2234                               dot_lustre_name,
2235                               strlen(dot_lustre_name), 0);
2236         inode_unlock(dentry->d_inode);
2237         if (rc == -ENOENT) {
2238 out_scrub:
2239                 /* It is 1.8 MDT device. */
2240                 if (!(sf->sf_flags & SF_UPGRADE)) {
2241                         scrub_file_reset(scrub, dev->od_uuid,
2242                                          SF_UPGRADE);
2243                         sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
2244                         rc = scrub_file_store(info->oti_env, scrub);
2245                 } else {
2246                         rc = 0;
2247                 }
2248         } else if (rc == 0) {
2249                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
2250                                       osd_ios_dl_fill);
2251         }
2252         dput(child);
2253
2254         RETURN(rc);
2255 }
2256
2257 static int
2258 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
2259                      struct dentry *dentry, filldir_t filldir)
2260 {
2261         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2262         struct scrub_file *sf = &scrub->os_file;
2263         struct dentry *child;
2264         int rc;
2265         ENTRY;
2266
2267         if (unlikely(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID)) {
2268                 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
2269                 rc = scrub_file_store(info->oti_env, scrub);
2270                 if (rc != 0)
2271                         RETURN(rc);
2272         }
2273
2274         child = osd_lookup_one_len_unlocked(dev, ADMIN_USR, dentry,
2275                                             strlen(ADMIN_USR));
2276         if (IS_ERR(child)) {
2277                 rc = PTR_ERR(child);
2278         } else {
2279                 inode_lock(dentry->d_inode);
2280                 rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2281                                       child->d_inode, NULL, ADMIN_USR,
2282                                       strlen(ADMIN_USR), 0);
2283                 inode_unlock(dentry->d_inode);
2284                 dput(child);
2285         }
2286
2287         if (rc != 0 && rc != -ENOENT)
2288                 GOTO(out, rc);
2289
2290         child = osd_lookup_one_len_unlocked(dev, ADMIN_GRP, dentry,
2291                                             strlen(ADMIN_GRP));
2292         if (IS_ERR(child))
2293                 GOTO(out, rc = PTR_ERR(child));
2294
2295         inode_lock(dentry->d_inode);
2296         rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2297                               child->d_inode, NULL, ADMIN_GRP,
2298                               strlen(ADMIN_GRP), 0);
2299         inode_unlock(dentry->d_inode);
2300         dput(child);
2301 out:
2302         RETURN(rc == -ENOENT ? 0 : rc);
2303 }
2304
2305 static void osd_initial_OI_scrub(struct osd_thread_info *info,
2306                                  struct osd_device *dev)
2307 {
2308         struct osd_ios_item     *item    = NULL;
2309         scandir_t                scandir = osd_ios_general_scan;
2310         filldir_t                filldir = osd_ios_root_fill;
2311         struct dentry           *dentry  = osd_sb(dev)->s_root;
2312         const struct osd_lf_map *map     = osd_lf_maps;
2313         ENTRY;
2314
2315         /* Lookup IGIF in OI by force for initial OI scrub. */
2316         dev->od_igif_inoi = 1;
2317
2318         while (1) {
2319                 /* Don't take inode_lock here since scandir() callbacks
2320                  * can call VFS functions which may manully take the
2321                  * inode lock itself like iterate_dir(). Since this
2322                  * is the case it is best to leave the scandir()
2323                  * callbacks to managing the inode lock.
2324                  */
2325                 scandir(info, dev, dentry, filldir);
2326                 if (item != NULL) {
2327                         dput(item->oii_dentry);
2328                         OBD_FREE_PTR(item);
2329                 }
2330
2331                 if (list_empty(&dev->od_ios_list))
2332                         break;
2333
2334                 item = list_entry(dev->od_ios_list.next,
2335                                   struct osd_ios_item, oii_list);
2336                 list_del_init(&item->oii_list);
2337
2338                 LASSERT(item->oii_scandir != NULL);
2339                 scandir = item->oii_scandir;
2340                 filldir = item->oii_filldir;
2341                 dentry = item->oii_dentry;
2342         }
2343
2344         /* There maybe the case that the object has been removed, but its OI
2345          * mapping is still in the OI file, such as the "CATALOGS" after MDT
2346          * file-level backup/restore. So here cleanup the stale OI mappings. */
2347         while (map->olm_name != NULL) {
2348                 struct dentry *child;
2349
2350                 if (fid_is_zero(&map->olm_fid)) {
2351                         map++;
2352                         continue;
2353                 }
2354
2355                 child = osd_lookup_one_len_unlocked(dev, map->olm_name,
2356                                                     osd_sb(dev)->s_root,
2357                                                     map->olm_namelen);
2358                 if (PTR_ERR(child) == -ENOENT ||
2359                     (!IS_ERR(child) && !child->d_inode))
2360                         osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
2361                                                   NULL, DTO_INDEX_DELETE,
2362                                                   true, 0, NULL);
2363                 if (!IS_ERR(child))
2364                         dput(child);
2365                 map++;
2366         }
2367
2368         if (!list_empty(&dev->od_index_restore_list)) {
2369                 char *buf;
2370
2371                 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
2372                 if (!buf)
2373                         CERROR("%s: not enough RAM for rebuild index\n",
2374                                osd_name(dev));
2375
2376                 while (!list_empty(&dev->od_index_restore_list)) {
2377                         struct lustre_index_restore_unit *liru;
2378
2379                         liru = list_entry(dev->od_index_restore_list.next,
2380                                           struct lustre_index_restore_unit,
2381                                           liru_link);
2382                         list_del(&liru->liru_link);
2383                         if (buf)
2384                                 osd_index_restore(info->oti_env, dev, liru,
2385                                                   buf, INDEX_BACKUP_BUFSIZE);
2386                         OBD_FREE(liru, liru->liru_len);
2387                 }
2388
2389                 if (buf)
2390                         OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
2391         }
2392
2393         EXIT;
2394 }
2395
2396 char *osd_lf_fid2name(const struct lu_fid *fid)
2397 {
2398         const struct osd_lf_map *map = osd_lf_maps;
2399
2400         while (map->olm_name != NULL) {
2401                 if (!lu_fid_eq(fid, &map->olm_fid)) {
2402                         map++;
2403                         continue;
2404                 }
2405
2406                 if (map->olm_flags & OLF_SHOW_NAME)
2407                         return map->olm_name;
2408                 else
2409                         return "";
2410         }
2411
2412         return NULL;
2413 }
2414
2415 /* OI scrub start/stop */
2416
2417 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
2418                     __u32 flags)
2419 {
2420         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2421         int rc;
2422         ENTRY;
2423
2424         if (dev->od_dt_dev.dd_rdonly)
2425                 RETURN(-EROFS);
2426
2427         /* od_otable_mutex: prevent curcurrent start/stop */
2428         mutex_lock(&dev->od_otable_mutex);
2429         rc = scrub_start(osd_scrub_main, scrub, dev, flags);
2430         if (rc == -EALREADY) {
2431                 rc = 0;
2432                 if ((scrub->os_file.sf_flags & SF_AUTO ||
2433                      scrub->os_partial_scan) &&
2434                     !(flags & SS_AUTO_PARTIAL))
2435                         osd_scrub_join(env, dev, flags, false);
2436         }
2437         mutex_unlock(&dev->od_otable_mutex);
2438
2439         RETURN(rc);
2440 }
2441
2442 void osd_scrub_stop(struct osd_device *dev)
2443 {
2444         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2445
2446         /* od_otable_mutex: prevent curcurrent start/stop */
2447         mutex_lock(&dev->od_otable_mutex);
2448         spin_lock(&scrub->os_lock);
2449         scrub->os_paused = 1;
2450         spin_unlock(&scrub->os_lock);
2451         scrub_stop(scrub);
2452         mutex_unlock(&dev->od_otable_mutex);
2453 }
2454
2455 /* OI scrub setup/cleanup */
2456
2457 static const char osd_scrub_name[] = "OI_scrub";
2458
2459 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev,
2460                     bool restored)
2461 {
2462         struct osd_thread_info *info = osd_oti_get(env);
2463         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2464         struct lvfs_run_ctxt *ctxt = &dev->od_scrub.os_ctxt;
2465         time64_t interval = scrub->os_auto_scrub_interval;
2466         struct scrub_file *sf = &scrub->os_file;
2467         struct super_block *sb = osd_sb(dev);
2468         struct lvfs_run_ctxt saved;
2469         struct file *filp;
2470         struct inode *inode;
2471         struct lu_fid *fid = &info->oti_fid;
2472         struct osd_inode_id *id = &info->oti_id;
2473         struct dt_object *obj;
2474         bool dirty = false;
2475         int rc = 0;
2476         ENTRY;
2477
2478         memset(&dev->od_scrub, 0, sizeof(struct osd_scrub));
2479         OBD_SET_CTXT_MAGIC(ctxt);
2480         ctxt->pwdmnt = dev->od_mnt;
2481         ctxt->pwd = dev->od_mnt->mnt_root;
2482
2483         init_rwsem(&scrub->os_rwsem);
2484         spin_lock_init(&scrub->os_lock);
2485         INIT_LIST_HEAD(&scrub->os_inconsistent_items);
2486         scrub->os_name = osd_name(dev);
2487         scrub->os_auto_scrub_interval = interval;
2488
2489         push_ctxt(&saved, ctxt);
2490         filp = filp_open(osd_scrub_name, O_RDWR |
2491                          (dev->od_dt_dev.dd_rdonly ? 0 : O_CREAT), 0644);
2492         if (IS_ERR(filp)) {
2493                 pop_ctxt(&saved, ctxt);
2494                 RETURN(PTR_ERR(filp));
2495         }
2496
2497         inode = file_inode(filp);
2498         ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2499         if (!dev->od_dt_dev.dd_rdonly) {
2500                 /* 'What the @fid is' is not imporatant, because the object
2501                  * has no OI mapping, and only is visible inside the OSD.*/
2502                 lu_igif_build(fid, inode->i_ino, inode->i_generation);
2503                 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
2504                 if (rc) {
2505                         filp_close(filp, NULL);
2506                         pop_ctxt(&saved, ctxt);
2507                         RETURN(rc);
2508                 }
2509         }
2510
2511         osd_id_gen(id, inode->i_ino, inode->i_generation);
2512         osd_add_oi_cache(info, dev, id, fid);
2513         filp_close(filp, NULL);
2514         pop_ctxt(&saved, ctxt);
2515
2516         obj = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, NULL));
2517         if (IS_ERR_OR_NULL(obj))
2518                 RETURN(obj ? PTR_ERR(obj) : -ENOENT);
2519
2520 #ifndef HAVE_S_UUID_AS_UUID_T
2521         memcpy(dev->od_uuid.b, sb->s_uuid, sizeof(dev->od_uuid));
2522 #else
2523         uuid_copy(&dev->od_uuid, &sb->s_uuid);
2524 #endif
2525         scrub->os_obj = obj;
2526         rc = scrub_file_load(env, scrub);
2527         if (rc == -ENOENT || rc == -EFAULT) {
2528                 scrub_file_init(scrub, dev->od_uuid);
2529                 /* If the "/O" dir does not exist when mount (indicated by
2530                  * osd_device::od_maybe_new), neither for the "/OI_scrub",
2531                  * then it is quite probably that the device is a new one,
2532                  * under such case, mark it as SIF_NO_HANDLE_OLD_FID.
2533                  *
2534                  * For the rare case that "/O" and "OI_scrub" both lost on
2535                  * an old device, it can be found and cleared later.
2536                  *
2537                  * For the system with "SIF_NO_HANDLE_OLD_FID", we do not
2538                  * need to check "filter_fid_18_23" and to convert it to
2539                  * "filter_fid" for each object, and all the IGIF should
2540                  * have their FID mapping in OI files already. */
2541                 if (dev->od_maybe_new && rc == -ENOENT)
2542                         sf->sf_internal_flags = SIF_NO_HANDLE_OLD_FID;
2543                 dirty = true;
2544         } else if (rc < 0) {
2545                 GOTO(cleanup_obj, rc);
2546         } else {
2547                 if (!uuid_equal(&sf->sf_uuid, &dev->od_uuid)) {
2548                         CDEBUG(D_LFSCK,
2549                                "%s: UUID has been changed from %pU to %pU\n",
2550                                osd_dev2name(dev), &sf->sf_uuid, &dev->od_uuid);
2551                         scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
2552                         dirty = true;
2553                         restored = true;
2554                 } else if (sf->sf_status == SS_SCANNING) {
2555                         sf->sf_status = SS_CRASHED;
2556                         dirty = true;
2557                 }
2558
2559                 if ((sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0) {
2560                         LCONSOLE_WARN("%s: invalid oi count %d, set it to %d\n",
2561                                       osd_dev2name(dev), sf->sf_oi_count,
2562                                       osd_oi_count);
2563                         sf->sf_oi_count = osd_oi_count;
2564                         dirty = true;
2565                 }
2566         }
2567
2568         if (sf->sf_pos_last_checkpoint != 0)
2569                 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
2570         else
2571                 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
2572
2573         if (dirty) {
2574                 rc = scrub_file_store(env, scrub);
2575                 if (rc)
2576                         GOTO(cleanup_obj, rc);
2577         }
2578
2579         /* Initialize OI files. */
2580         rc = osd_oi_init(info, dev, restored);
2581         if (rc < 0)
2582                 GOTO(cleanup_obj, rc);
2583
2584         if (!dev->od_dt_dev.dd_rdonly)
2585                 osd_initial_OI_scrub(info, dev);
2586
2587         if (sf->sf_flags & SF_UPGRADE ||
2588             !(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID ||
2589               sf->sf_success_count > 0)) {
2590                 dev->od_igif_inoi = 0;
2591                 dev->od_check_ff = dev->od_is_ost;
2592         } else {
2593                 dev->od_igif_inoi = 1;
2594                 dev->od_check_ff = 0;
2595         }
2596
2597         if (sf->sf_flags & SF_INCONSISTENT)
2598                 /* The 'od_igif_inoi' will be set under the
2599                  * following cases:
2600                  * 1) new created system, or
2601                  * 2) restored from file-level backup, or
2602                  * 3) the upgrading completed.
2603                  *
2604                  * The 'od_igif_inoi' may be cleared by OI scrub
2605                  * later if found that the system is upgrading. */
2606                 dev->od_igif_inoi = 1;
2607
2608         if (!dev->od_dt_dev.dd_rdonly &&
2609             dev->od_scrub.os_scrub.os_auto_scrub_interval != AS_NEVER &&
2610             ((sf->sf_status == SS_PAUSED) ||
2611              (sf->sf_status == SS_CRASHED &&
2612               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2613                               SF_UPGRADE | SF_AUTO)) ||
2614              (sf->sf_status == SS_INIT &&
2615               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2616                               SF_UPGRADE))))
2617                 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
2618
2619         if (rc != 0)
2620                 GOTO(cleanup_oi, rc);
2621
2622         /* it is possible that dcache entries may keep objects after they are
2623          * deleted by OSD. While it looks safe this can cause object data to
2624          * stay until umount causing failures in tests calculating free space,
2625          * e.g. replay-ost-single. Since those dcache entries are not used
2626          * anymore let's just free them after use here */
2627         shrink_dcache_sb(sb);
2628
2629         RETURN(0);
2630 cleanup_oi:
2631         osd_oi_fini(info, dev);
2632 cleanup_obj:
2633         dt_object_put_nocache(env, scrub->os_obj);
2634         scrub->os_obj = NULL;
2635
2636         return rc;
2637 }
2638
2639 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2640 {
2641         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2642
2643         LASSERT(dev->od_otable_it == NULL);
2644
2645         if (scrub->os_obj != NULL) {
2646                 osd_scrub_stop(dev);
2647                 dt_object_put_nocache(env, scrub->os_obj);
2648                 scrub->os_obj = NULL;
2649         }
2650 }
2651
2652 /* object table based iteration APIs */
2653
2654 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2655                                        struct dt_object *dt, __u32 attr)
2656 {
2657         enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2658         enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2659         struct osd_device      *dev   = osd_dev(dt->do_lu.lo_dev);
2660         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2661         struct osd_otable_it   *it;
2662         __u32                   start = 0;
2663         int                     rc;
2664         ENTRY;
2665
2666         /* od_otable_mutex: prevent curcurrent init/fini */
2667         mutex_lock(&dev->od_otable_mutex);
2668         if (dev->od_otable_it != NULL)
2669                 GOTO(out, it = ERR_PTR(-EALREADY));
2670
2671         OBD_ALLOC_PTR(it);
2672         if (it == NULL)
2673                 GOTO(out, it = ERR_PTR(-ENOMEM));
2674
2675         dev->od_otable_it = it;
2676         it->ooi_dev = dev;
2677         it->ooi_cache.ooc_consumer_idx = -1;
2678         if (flags & DOIF_OUTUSED)
2679                 it->ooi_used_outside = 1;
2680
2681         if (flags & DOIF_RESET)
2682                 start |= SS_RESET;
2683
2684         if (valid & DOIV_ERROR_HANDLE) {
2685                 if (flags & DOIF_FAILOUT)
2686                         start |= SS_SET_FAILOUT;
2687                 else
2688                         start |= SS_CLEAR_FAILOUT;
2689         }
2690
2691         if (valid & DOIV_DRYRUN) {
2692                 if (flags & DOIF_DRYRUN)
2693                         start |= SS_SET_DRYRUN;
2694                 else
2695                         start |= SS_CLEAR_DRYRUN;
2696         }
2697
2698         rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
2699         if (rc == -EALREADY) {
2700                 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2701         } else  if (rc < 0) {
2702                 dev->od_otable_it = NULL;
2703                 OBD_FREE_PTR(it);
2704                 it = ERR_PTR(rc);
2705         } else {
2706                 /* We have to start from the begining. */
2707                 it->ooi_cache.ooc_pos_preload =
2708                         LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2709         }
2710
2711         GOTO(out, it);
2712
2713 out:
2714         mutex_unlock(&dev->od_otable_mutex);
2715         return (struct dt_it *)it;
2716 }
2717
2718 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2719 {
2720         struct osd_otable_it *it  = (struct osd_otable_it *)di;
2721         struct osd_device    *dev = it->ooi_dev;
2722
2723         /* od_otable_mutex: prevent curcurrent init/fini */
2724         mutex_lock(&dev->od_otable_mutex);
2725         scrub_stop(&dev->od_scrub.os_scrub);
2726         LASSERT(dev->od_otable_it == it);
2727
2728         dev->od_otable_it = NULL;
2729         mutex_unlock(&dev->od_otable_mutex);
2730         OBD_FREE_PTR(it);
2731 }
2732
2733 static int osd_otable_it_get(const struct lu_env *env,
2734                              struct dt_it *di, const struct dt_key *key)
2735 {
2736         return 0;
2737 }
2738
2739 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2740 {
2741 }
2742
2743 static inline int
2744 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
2745 {
2746         spin_lock(&scrub->os_lock);
2747         if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2748             scrub->os_waiting || !scrub->os_running)
2749                 it->ooi_waiting = 0;
2750         else
2751                 it->ooi_waiting = 1;
2752         spin_unlock(&scrub->os_lock);
2753
2754         return !it->ooi_waiting;
2755 }
2756
2757 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2758 {
2759         struct osd_otable_it *it = (struct osd_otable_it *)di;
2760         struct osd_device *dev = it->ooi_dev;
2761         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2762         struct osd_otable_cache *ooc = &it->ooi_cache;
2763         int rc;
2764         ENTRY;
2765
2766         LASSERT(it->ooi_user_ready);
2767
2768 again:
2769         if (!scrub->os_running && !it->ooi_used_outside)
2770                 RETURN(1);
2771
2772         if (ooc->ooc_cached_items > 0) {
2773                 ooc->ooc_cached_items--;
2774                 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2775                                         ~OSD_OTABLE_IT_CACHE_MASK;
2776                 RETURN(0);
2777         }
2778
2779         if (it->ooi_all_cached) {
2780                 wait_var_event(scrub, !scrub->os_running);
2781                 RETURN(1);
2782         }
2783
2784         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2785                 spin_lock(&scrub->os_lock);
2786                 scrub->os_waiting = 0;
2787                 wake_up_var(scrub);
2788                 spin_unlock(&scrub->os_lock);
2789         }
2790
2791         if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2792                 wait_var_event(scrub, osd_otable_it_wakeup(scrub, it));
2793
2794         if (!scrub->os_running && !it->ooi_used_outside)
2795                 RETURN(1);
2796
2797         rc = osd_otable_it_preload(env, it);
2798         if (rc >= 0)
2799                 goto again;
2800
2801         RETURN(rc);
2802 }
2803
2804 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2805                                         const struct dt_it *di)
2806 {
2807         return NULL;
2808 }
2809
2810 static int osd_otable_it_key_size(const struct lu_env *env,
2811                                   const struct dt_it *di)
2812 {
2813         return sizeof(__u64);
2814 }
2815
2816 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2817                              struct dt_rec *rec, __u32 attr)
2818 {
2819         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2820         struct osd_otable_cache *ooc = &it->ooi_cache;
2821
2822         *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2823
2824         /* Filter out Invald FID already. */
2825         LASSERTF(fid_is_sane((struct lu_fid *)rec),
2826                  "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2827                  PFID((struct lu_fid *)rec),
2828                  ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2829
2830         return 0;
2831 }
2832
2833 static __u64 osd_otable_it_store(const struct lu_env *env,
2834                                  const struct dt_it *di)
2835 {
2836         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2837         struct osd_otable_cache *ooc = &it->ooi_cache;
2838         __u64                    hash;
2839
2840         if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2841                 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2842         else
2843                 hash = ooc->ooc_pos_preload;
2844         return hash;
2845 }
2846
2847 /**
2848  * Set the OSD layer iteration start position as the specified hash.
2849  */
2850 static int osd_otable_it_load(const struct lu_env *env,
2851                               const struct dt_it *di, __u64 hash)
2852 {
2853         struct osd_otable_it    *it    = (struct osd_otable_it *)di;
2854         struct osd_device       *dev   = it->ooi_dev;
2855         struct osd_otable_cache *ooc   = &it->ooi_cache;
2856         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2857         struct osd_iit_param    *param = &it->ooi_iit_param;
2858         int                      rc;
2859         ENTRY;
2860
2861         /* Forbid to set iteration position after iteration started. */
2862         if (it->ooi_user_ready)
2863                 RETURN(-EPERM);
2864
2865         LASSERT(!scrub->os_partial_scan);
2866
2867         if (hash > OSD_OTABLE_MAX_HASH)
2868                 hash = OSD_OTABLE_MAX_HASH;
2869
2870         /* The hash is the last checkpoint position,
2871          * we will start from the next one. */
2872         ooc->ooc_pos_preload = hash + 1;
2873         if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2874                 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2875
2876         it->ooi_user_ready = 1;
2877         if (!scrub->os_full_speed)
2878                 wake_up_var(scrub);
2879
2880         memset(param, 0, sizeof(*param));
2881         param->sb = osd_sb(dev);
2882         param->start = ooc->ooc_pos_preload;
2883         param->bg = (ooc->ooc_pos_preload - 1) /
2884                     LDISKFS_INODES_PER_GROUP(param->sb);
2885         param->offset = (ooc->ooc_pos_preload - 1) %
2886                         LDISKFS_INODES_PER_GROUP(param->sb);
2887         param->gbase = 1 + param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
2888
2889         /* Unplug OSD layer iteration by the first next() call. */
2890         rc = osd_otable_it_next(env, (struct dt_it *)it);
2891
2892         RETURN(rc);
2893 }
2894
2895 static int osd_otable_it_key_rec(const struct lu_env *env,
2896                                  const struct dt_it *di, void *key_rec)
2897 {
2898         return 0;
2899 }
2900
2901 const struct dt_index_operations osd_otable_ops = {
2902         .dio_it = {
2903                 .init     = osd_otable_it_init,
2904                 .fini     = osd_otable_it_fini,
2905                 .get      = osd_otable_it_get,
2906                 .put      = osd_otable_it_put,
2907                 .next     = osd_otable_it_next,
2908                 .key      = osd_otable_it_key,
2909                 .key_size = osd_otable_it_key_size,
2910                 .rec      = osd_otable_it_rec,
2911                 .store    = osd_otable_it_store,
2912                 .load     = osd_otable_it_load,
2913                 .key_rec  = osd_otable_it_key_rec,
2914         }
2915 };
2916
2917 /* high priority inconsistent items list APIs */
2918
2919 #define SCRUB_BAD_OIMAP_DECAY_INTERVAL  60
2920
2921 int osd_oii_insert(struct osd_device *dev, const struct lu_fid *fid,
2922                    struct osd_inode_id *id, int insert)
2923 {
2924         struct osd_inconsistent_item *oii;
2925         struct osd_scrub *oscrub = &dev->od_scrub;
2926         struct lustre_scrub *lscrub = &oscrub->os_scrub;
2927         int wakeup = 0;
2928         ENTRY;
2929
2930         OBD_ALLOC_PTR(oii);
2931         if (unlikely(oii == NULL))
2932                 RETURN(-ENOMEM);
2933
2934         INIT_LIST_HEAD(&oii->oii_list);
2935         oii->oii_cache.oic_fid = *fid;
2936         oii->oii_cache.oic_lid = *id;
2937         oii->oii_cache.oic_dev = dev;
2938         oii->oii_insert = insert;
2939
2940         spin_lock(&lscrub->os_lock);
2941         if (lscrub->os_partial_scan) {
2942                 __u64 now = ktime_get_real_seconds();
2943
2944                 /* If there haven't been errors in a long time,
2945                  * decay old count until either the errors are
2946                  * gone or we reach the current interval. */
2947                 while (unlikely(oscrub->os_bad_oimap_count > 0 &&
2948                                 oscrub->os_bad_oimap_time +
2949                                 SCRUB_BAD_OIMAP_DECAY_INTERVAL < now)) {
2950                         oscrub->os_bad_oimap_count >>= 1;
2951                         oscrub->os_bad_oimap_time +=
2952                                 SCRUB_BAD_OIMAP_DECAY_INTERVAL;
2953                 }
2954
2955                 oscrub->os_bad_oimap_time = now;
2956                 if (++oscrub->os_bad_oimap_count >
2957                     dev->od_full_scrub_threshold_rate)
2958                         lscrub->os_full_scrub = 1;
2959         }
2960
2961         if (!lscrub->os_running) {
2962                 spin_unlock(&lscrub->os_lock);
2963                 OBD_FREE_PTR(oii);
2964                 RETURN(-EAGAIN);
2965         }
2966
2967         if (list_empty(&lscrub->os_inconsistent_items))
2968                 wakeup = 1;
2969         list_add_tail(&oii->oii_list, &lscrub->os_inconsistent_items);
2970         spin_unlock(&lscrub->os_lock);
2971
2972         if (wakeup)
2973                 wake_up_var(lscrub);
2974
2975         RETURN(0);
2976 }
2977
2978 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2979                    struct osd_inode_id *id)
2980 {
2981         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2982         struct osd_inconsistent_item *oii;
2983         ENTRY;
2984
2985         spin_lock(&scrub->os_lock);
2986         list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2987                 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2988                         *id = oii->oii_cache.oic_lid;
2989                         spin_unlock(&scrub->os_lock);
2990                         RETURN(0);
2991                 }
2992         }
2993         spin_unlock(&scrub->os_lock);
2994
2995         RETURN(-ENOENT);
2996 }
2997
2998 void osd_scrub_dump(struct seq_file *m, struct osd_device *dev)
2999 {
3000         struct osd_scrub *scrub = &dev->od_scrub;
3001
3002         scrub_dump(m, &scrub->os_scrub);
3003         seq_printf(m, "lf_scanned: %llu\n"
3004                    "lf_%s: %llu\n"
3005                    "lf_failed: %llu\n",
3006                    scrub->os_lf_scanned,
3007                    scrub->os_scrub.os_file.sf_param & SP_DRYRUN ?
3008                         "inconsistent" : "repaired",
3009                    scrub->os_lf_repaired,
3010                    scrub->os_lf_failed);
3011 }
3012
3013 typedef int (*scan_dir_helper_t)(const struct lu_env *env,
3014                                  struct osd_device *dev, struct inode *dir,
3015                                  struct osd_it_ea *oie);
3016
3017 static int osd_scan_dir(const struct lu_env *env, struct osd_device *dev,
3018                         struct inode *inode, scan_dir_helper_t cb)
3019 {
3020         struct osd_it_ea *oie;
3021         int rc;
3022
3023         ENTRY;
3024
3025         oie = osd_it_dir_init(env, dev, inode, LUDA_TYPE);
3026         if (IS_ERR(oie))
3027                 RETURN(PTR_ERR(oie));
3028
3029         oie->oie_file->f_pos = 0;
3030         rc = osd_ldiskfs_it_fill(env, (struct dt_it *)oie);
3031         if (rc > 0)
3032                 rc = -ENODATA;
3033         if (rc)
3034                 GOTO(out, rc);
3035
3036         while (oie->oie_it_dirent <= oie->oie_rd_dirent) {
3037                 if (!name_is_dot_or_dotdot(oie->oie_dirent->oied_name,
3038                                            oie->oie_dirent->oied_namelen))
3039                         cb(env, dev, inode, oie);
3040
3041                 oie->oie_dirent = (void *)oie->oie_dirent +
3042                                 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3043                                 oie->oie_dirent->oied_namelen);
3044
3045                 oie->oie_it_dirent++;
3046                 if (oie->oie_it_dirent <= oie->oie_rd_dirent)
3047                         continue;
3048
3049                 if (oie->oie_file->f_pos ==
3050                     ldiskfs_get_htree_eof(oie->oie_file))
3051                         break;
3052
3053                 rc = osd_ldiskfs_it_fill(env, (struct dt_it *)oie);
3054                 if (rc) {
3055                         if (rc > 0)
3056                                 rc = 0;
3057                         break;
3058                 }
3059         }
3060
3061 out:
3062         osd_it_dir_fini(env, oie, inode);
3063         RETURN(rc);
3064 }
3065
3066 static int osd_remove_ml_file(struct osd_thread_info *info,
3067                               struct osd_device *dev, struct inode *dir,
3068                               struct inode *inode, struct osd_it_ea *oie)
3069 {
3070         handle_t *th;
3071         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
3072         struct dentry dentry;
3073         int rc;
3074
3075         ENTRY;
3076
3077         if (scrub->os_file.sf_param & SP_DRYRUN)
3078                 RETURN(0);
3079
3080         th = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC,
3081                                   osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3082                                   osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3083         if (IS_ERR(th))
3084                 RETURN(PTR_ERR(th));
3085
3086         /* Should be created by the VFS layer */
3087         dentry.d_inode = dir;
3088         dentry.d_sb = dir->i_sb;
3089         rc = osd_obj_del_entry(info, dev, &dentry, oie->oie_dirent->oied_name,
3090                                oie->oie_dirent->oied_namelen, th);
3091         drop_nlink(inode);
3092         mark_inode_dirty(inode);
3093         ldiskfs_journal_stop(th);
3094         RETURN(rc);
3095 }
3096
3097 static int osd_scan_ml_file(const struct lu_env *env, struct osd_device *dev,
3098                             struct inode *dir, struct osd_it_ea *oie)
3099 {
3100         struct osd_thread_info *info = osd_oti_get(env);
3101         struct osd_inode_id id;
3102         struct inode *inode;
3103         struct osd_obj_seq *oseq;
3104         struct ost_id *ostid = &info->oti_ostid;
3105         struct lu_fid *fid = &oie->oie_dirent->oied_fid;
3106         char name[32];
3107         int dirn, rc = 0;
3108
3109         ENTRY;
3110
3111         osd_id_gen(&id, oie->oie_dirent->oied_ino, OSD_OII_NOGEN);
3112
3113         if (!fid_is_sane(fid))
3114                 inode = osd_iget_fid(info, dev, &id, fid);
3115         else
3116                 inode = osd_iget(info, dev, &id);
3117
3118         if (IS_ERR(inode))
3119                 RETURN(PTR_ERR(inode));
3120
3121         fid_to_ostid(fid, ostid);
3122         oseq = osd_seq_load(info, dev, ostid_seq(ostid));
3123         if (IS_ERR(oseq))
3124                 RETURN(PTR_ERR(oseq));
3125
3126         dirn = ostid_id(ostid) & (oseq->oos_subdir_count - 1);
3127         LASSERT(oseq->oos_dirs[dirn] != NULL);
3128
3129         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
3130         if (((strlen(oseq->oos_root->d_name.name) !=
3131               info->oti_seq_dirent->oied_namelen) ||
3132              strncmp(oseq->oos_root->d_name.name,
3133                      info->oti_seq_dirent->oied_name,
3134                      info->oti_seq_dirent->oied_namelen) != 0) ||
3135             ((strlen(oseq->oos_dirs[dirn]->d_name.name) !=
3136               info->oti_dir_dirent->oied_namelen) ||
3137              strncmp(oseq->oos_dirs[dirn]->d_name.name,
3138                      info->oti_dir_dirent->oied_name,
3139                      info->oti_dir_dirent->oied_namelen) != 0) ||
3140             ((strlen(name) != oie->oie_dirent->oied_namelen) ||
3141              strncmp(oie->oie_dirent->oied_name, name,
3142                      oie->oie_dirent->oied_namelen) != 0)) {
3143                 CDEBUG(D_LFSCK, "%s: the file O/%s/%s/%s is corrupted\n",
3144                        osd_name(dev), info->oti_seq_dirent->oied_name,
3145                        info->oti_dir_dirent->oied_name,
3146                        oie->oie_dirent->oied_name);
3147
3148                 rc = osd_remove_ml_file(info, dev, dir, inode, oie);
3149         }
3150
3151         iput(inode);
3152         RETURN(rc);
3153 }
3154
3155 static int osd_scan_ml_file_dir(const struct lu_env *env,
3156                                 struct osd_device *dev, struct inode *dir,
3157                                 struct osd_it_ea *oie)
3158 {
3159         struct osd_thread_info *info = osd_oti_get(env);
3160         struct inode *inode;
3161         struct osd_inode_id id;
3162         int rc;
3163
3164         ENTRY;
3165
3166         osd_id_gen(&id, oie->oie_dirent->oied_ino, OSD_OII_NOGEN);
3167         inode = osd_iget(info, dev, &id);
3168         if (IS_ERR(inode))
3169                 RETURN(PTR_ERR(inode));
3170
3171         if (!S_ISDIR(inode->i_mode))
3172                 GOTO(out, rc = 0);
3173
3174         info->oti_dir_dirent = oie->oie_dirent;
3175         rc = osd_scan_dir(env, dev, inode, osd_scan_ml_file);
3176         info->oti_dir_dirent = NULL;
3177
3178 out:
3179         iput(inode);
3180         RETURN(rc);
3181 }
3182
3183 static int osd_scan_ml_file_seq(const struct lu_env *env,
3184                                 struct osd_device *dev, struct inode *dir,
3185                                 struct osd_it_ea *oie)
3186 {
3187         struct osd_thread_info *info = osd_oti_get(env);
3188         struct inode *inode;
3189         struct osd_inode_id id;
3190         int rc;
3191
3192         ENTRY;
3193
3194         osd_id_gen(&id, oie->oie_dirent->oied_ino, OSD_OII_NOGEN);
3195         inode = osd_iget(info, dev, &id);
3196         if (IS_ERR(inode))
3197                 RETURN(PTR_ERR(inode));
3198
3199         if (!S_ISDIR(inode->i_mode))
3200                 GOTO(out, rc = 0);
3201
3202         info->oti_seq_dirent = oie->oie_dirent;
3203         rc = osd_scan_dir(env, dev, inode, osd_scan_ml_file_dir);
3204         info->oti_seq_dirent = NULL;
3205
3206 out:
3207         iput(inode);
3208         RETURN(rc);
3209 }
3210
3211 static int osd_scan_ml_file_main(const struct lu_env *env,
3212                                  struct osd_device *dev)
3213 {
3214         return osd_scan_dir(env, dev, dev->od_ost_map->om_root->d_inode,
3215                             osd_scan_ml_file_seq);
3216 }