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