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