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