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