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