Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[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_old   *ff      = &info->oti_ff;
167         struct dentry           *dentry  = &info->oti_obj_dentry;
168         struct lu_fid           *tfid    = &info->oti_fid;
169         handle_t                *jh;
170         int                      size    = 0;
171         int                      rc;
172         bool                     reset   = false;
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                 ll_vfs_dq_init(inode);
211                 rc = inode->i_op->removexattr(dentry, XATTR_NAME_FID);
212                 if (rc)
213                         GOTO(stop, rc);
214
215                 reset = true;
216         } else if (rc != -ENODATA && rc != sizeof(struct filter_fid)) {
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 (reset) {
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,
476                         LDISKFS_SB(osd_sb(dev))->s_es->s_uuid, 0);
477
478         if (flags & SS_AUTO_FULL) {
479                 scrub->os_full_speed = 1;
480                 scrub->os_partial_scan = 0;
481                 sf->sf_flags |= SF_AUTO;
482         } else if (flags & SS_AUTO_PARTIAL) {
483                 scrub->os_full_speed = 0;
484                 scrub->os_partial_scan = 1;
485                 sf->sf_flags |= SF_AUTO;
486         } else if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
487                                    SF_UPGRADE)) {
488                 scrub->os_full_speed = 1;
489                 scrub->os_partial_scan = 0;
490         } else {
491                 scrub->os_full_speed = 0;
492                 scrub->os_partial_scan = 0;
493         }
494
495         spin_lock(&scrub->os_lock);
496         scrub->os_in_prior = 0;
497         scrub->os_waiting = 0;
498         scrub->os_paused = 0;
499         scrub->os_in_join = 0;
500         scrub->os_full_scrub = 0;
501         spin_unlock(&scrub->os_lock);
502         scrub->os_new_checked = 0;
503         if (drop_dryrun && sf->sf_pos_first_inconsistent != 0)
504                 sf->sf_pos_latest_start = sf->sf_pos_first_inconsistent;
505         else if (sf->sf_pos_last_checkpoint != 0)
506                 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
507         else
508                 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
509
510         scrub->os_pos_current = sf->sf_pos_latest_start;
511         sf->sf_status = SS_SCANNING;
512         sf->sf_time_latest_start = ktime_get_real_seconds();
513         sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
514         sf->sf_pos_last_checkpoint = sf->sf_pos_latest_start - 1;
515         rc = scrub_file_store(env, scrub);
516         if (rc == 0) {
517                 spin_lock(&scrub->os_lock);
518                 thread_set_flags(thread, SVC_RUNNING);
519                 spin_unlock(&scrub->os_lock);
520                 wake_up_all(&thread->t_ctl_waitq);
521         }
522         up_write(&scrub->os_rwsem);
523
524         RETURN(rc);
525 }
526
527 static int osd_scrub_post(const struct lu_env *env, struct osd_device *dev,
528                           int result)
529 {
530         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
531         struct scrub_file *sf = &scrub->os_file;
532         int rc;
533         ENTRY;
534
535         CDEBUG(D_LFSCK, "%s: OI scrub post with result = %d\n",
536                osd_scrub2name(scrub), result);
537
538         down_write(&scrub->os_rwsem);
539         spin_lock(&scrub->os_lock);
540         thread_set_flags(&scrub->os_thread, SVC_STOPPING);
541         spin_unlock(&scrub->os_lock);
542         if (scrub->os_new_checked > 0) {
543                 sf->sf_items_checked += scrub->os_new_checked;
544                 scrub->os_new_checked = 0;
545                 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
546         }
547         sf->sf_time_last_checkpoint = ktime_get_real_seconds();
548         if (result > 0) {
549                 dev->od_igif_inoi = 1;
550                 dev->od_check_ff = 0;
551                 sf->sf_status = SS_COMPLETED;
552                 if (!(sf->sf_param & SP_DRYRUN)) {
553                         memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
554                         sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
555                                           SF_UPGRADE | SF_AUTO);
556                 }
557                 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
558                 sf->sf_success_count++;
559         } else if (result == 0) {
560                 if (scrub->os_paused)
561                         sf->sf_status = SS_PAUSED;
562                 else
563                         sf->sf_status = SS_STOPPED;
564         } else {
565                 sf->sf_status = SS_FAILED;
566         }
567         sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
568                                             scrub->os_time_last_checkpoint);
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         else
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         int rc;
639         bool has_lma = false;
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 old 2.x (x <= 3) or 1.8 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 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                      pos == osd_remote_parent_ino(dev)))
733                 RETURN(SCRUB_NEXT_CONTINUE);
734
735         osd_id_gen(lid, pos, OSD_OII_NOGEN);
736         inode = osd_iget(info, dev, lid);
737         if (IS_ERR(inode)) {
738                 rc = PTR_ERR(inode);
739                 /* The inode may be removed after bitmap searching, or the
740                  * file is new created without inode initialized yet. */
741                 if (rc == -ENOENT || rc == -ESTALE)
742                         RETURN(SCRUB_NEXT_CONTINUE);
743
744                 CDEBUG(D_LFSCK, "%s: fail to read inode, ino# = %u: "
745                        "rc = %d\n", osd_dev2name(dev), pos, rc);
746                 RETURN(rc);
747         }
748
749         /* It is an EA inode, no OI mapping for it, skip it. */
750         if (osd_is_ea_inode(inode))
751                 GOTO(put, rc = SCRUB_NEXT_CONTINUE);
752
753         if (scrub &&
754             ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
755                 /* Only skip it for the first OI scrub accessing. */
756                 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
757                 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
758         }
759
760         rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
761
762         GOTO(put, rc);
763
764 put:
765         iput(inode);
766         return rc;
767 }
768
769 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
770                           struct osd_iit_param *param,
771                           struct osd_idmap_cache **oic, const bool noslot)
772 {
773         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
774         struct ptlrpc_thread *thread = &scrub->os_thread;
775         struct lu_fid *fid;
776         struct osd_inode_id *lid;
777         int rc;
778
779         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
780                 struct l_wait_info lwi;
781
782                 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
783                 if (likely(lwi.lwi_timeout > 0))
784                         l_wait_event(thread->t_ctl_waitq,
785                                 !list_empty(&scrub->os_inconsistent_items) ||
786                                 !thread_is_running(thread),
787                                 &lwi);
788         }
789
790         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
791                 spin_lock(&scrub->os_lock);
792                 thread_set_flags(thread, SVC_STOPPING);
793                 spin_unlock(&scrub->os_lock);
794                 return SCRUB_NEXT_CRASH;
795         }
796
797         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
798                 return SCRUB_NEXT_FATAL;
799
800         if (unlikely(!thread_is_running(thread)))
801                 return SCRUB_NEXT_EXIT;
802
803         if (!list_empty(&scrub->os_inconsistent_items)) {
804                 spin_lock(&scrub->os_lock);
805                 if (likely(!list_empty(&scrub->os_inconsistent_items))) {
806                         struct osd_inconsistent_item *oii;
807
808                         oii = list_entry(scrub->os_inconsistent_items.next,
809                                 struct osd_inconsistent_item, oii_list);
810                         spin_unlock(&scrub->os_lock);
811
812                         *oic = &oii->oii_cache;
813                         scrub->os_in_prior = 1;
814
815                         return 0;
816                 }
817                 spin_unlock(&scrub->os_lock);
818         }
819
820         if (noslot)
821                 return SCRUB_NEXT_WAIT;
822
823         rc = osd_iit_next(param, &scrub->os_pos_current);
824         if (rc != 0)
825                 return rc;
826
827         *oic = &dev->od_scrub.os_oic;
828         fid = &(*oic)->oic_fid;
829         lid = &(*oic)->oic_lid;
830         rc = osd_iit_iget(info, dev, fid, lid,
831                           scrub->os_pos_current, param->sb, true);
832         return rc;
833 }
834
835 static int osd_preload_next(struct osd_thread_info *info,
836                             struct osd_device *dev, struct osd_iit_param *param,
837                             struct osd_idmap_cache **oic, const bool noslot)
838 {
839         struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
840         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
841         struct ptlrpc_thread *thread = &scrub->os_thread;
842         int rc;
843
844         if (thread_is_running(thread) &&
845             ooc->ooc_pos_preload >= scrub->os_pos_current)
846                 return SCRUB_NEXT_EXIT;
847
848         rc = osd_iit_next(param, &ooc->ooc_pos_preload);
849         if (rc)
850                 return rc;
851
852         rc = osd_iit_iget(info, dev,
853                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
854                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
855                           ooc->ooc_pos_preload, param->sb, false);
856         return rc;
857 }
858
859 static inline int
860 osd_scrub_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
861 {
862         spin_lock(&scrub->os_lock);
863         if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
864             !list_empty(&scrub->os_inconsistent_items) ||
865             it->ooi_waiting || !thread_is_running(&scrub->os_thread))
866                 scrub->os_waiting = 0;
867         else
868                 scrub->os_waiting = 1;
869         spin_unlock(&scrub->os_lock);
870
871         return !scrub->os_waiting;
872 }
873
874 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
875                           struct osd_iit_param *param,
876                           struct osd_idmap_cache *oic, bool *noslot, int rc)
877 {
878         struct l_wait_info lwi = { 0 };
879         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
880         struct scrub_file *sf = &scrub->os_file;
881         struct ptlrpc_thread *thread = &scrub->os_thread;
882         struct osd_otable_it *it = dev->od_otable_it;
883         struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
884
885         switch (rc) {
886         case SCRUB_NEXT_NOSCRUB:
887                 down_write(&scrub->os_rwsem);
888                 scrub->os_new_checked++;
889                 sf->sf_items_noscrub++;
890                 up_write(&scrub->os_rwsem);
891         case SCRUB_NEXT_CONTINUE:
892         case SCRUB_NEXT_WAIT:
893                 goto wait;
894         }
895
896         rc = osd_scrub_check_update(info, dev, oic, rc);
897         if (rc != 0) {
898                 scrub->os_in_prior = 0;
899                 return rc;
900         }
901
902         rc = scrub_checkpoint(info->oti_env, scrub);
903         if (rc) {
904                 CDEBUG(D_LFSCK, "%s: fail to checkpoint, pos = %llu: "
905                        "rc = %d\n", osd_scrub2name(scrub),
906                        scrub->os_pos_current, rc);
907                 /* Continue, as long as the scrub itself can go ahead. */
908         }
909
910         if (scrub->os_in_prior) {
911                 scrub->os_in_prior = 0;
912                 return 0;
913         }
914
915 wait:
916         if (it != NULL && it->ooi_waiting && ooc != NULL &&
917             ooc->ooc_pos_preload < scrub->os_pos_current) {
918                 spin_lock(&scrub->os_lock);
919                 it->ooi_waiting = 0;
920                 wake_up_all(&thread->t_ctl_waitq);
921                 spin_unlock(&scrub->os_lock);
922         }
923
924         if (rc == SCRUB_NEXT_CONTINUE)
925                 return 0;
926
927         if (scrub->os_full_speed || !ooc || osd_scrub_has_window(scrub, ooc)) {
928                 *noslot = false;
929                 return 0;
930         }
931
932         if (it != NULL)
933                 l_wait_event(thread->t_ctl_waitq, osd_scrub_wakeup(scrub, it),
934                              &lwi);
935
936         if (!ooc || osd_scrub_has_window(scrub, ooc))
937                 *noslot = false;
938         else
939                 *noslot = true;
940         return 0;
941 }
942
943 static int osd_preload_exec(struct osd_thread_info *info,
944                             struct osd_device *dev, struct osd_iit_param *param,
945                             struct osd_idmap_cache *oic, bool *noslot, int rc)
946 {
947         struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
948
949         if (rc == 0) {
950                 ooc->ooc_cached_items++;
951                 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
952                                         ~OSD_OTABLE_IT_CACHE_MASK;
953         }
954         return rc > 0 ? 0 : rc;
955 }
956
957 #define SCRUB_IT_ALL    1
958 #define SCRUB_IT_CRASH  2
959
960 static void osd_scrub_join(const struct lu_env *env, struct osd_device *dev,
961                            __u32 flags, bool inconsistent)
962 {
963         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
964         struct ptlrpc_thread *thread = &scrub->os_thread;
965         struct scrub_file    *sf     = &scrub->os_file;
966         int                   rc;
967         ENTRY;
968
969         LASSERT(!(flags & SS_AUTO_PARTIAL));
970
971         down_write(&scrub->os_rwsem);
972         scrub->os_in_join = 1;
973         if (flags & SS_SET_FAILOUT)
974                 sf->sf_param |= SP_FAILOUT;
975         else if (flags & SS_CLEAR_FAILOUT)
976                 sf->sf_param &= ~SP_FAILOUT;
977
978         if (flags & SS_SET_DRYRUN)
979                 sf->sf_param |= SP_DRYRUN;
980         else if (flags & SS_CLEAR_DRYRUN)
981                 sf->sf_param &= ~SP_DRYRUN;
982
983         if (flags & SS_RESET) {
984                 scrub_file_reset(scrub, LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
985                         inconsistent ? SF_INCONSISTENT : 0);
986                 sf->sf_status = SS_SCANNING;
987         }
988
989         if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
990                 scrub->os_full_speed = 1;
991         else
992                 scrub->os_full_speed = 0;
993
994         if (flags & SS_AUTO_FULL) {
995                 sf->sf_flags |= SF_AUTO;
996                 scrub->os_full_speed = 1;
997         }
998
999         scrub->os_new_checked = 0;
1000         if (sf->sf_pos_last_checkpoint != 0)
1001                 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
1002         else
1003                 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
1004
1005         scrub->os_pos_current = sf->sf_pos_latest_start;
1006         sf->sf_time_latest_start = ktime_get_real_seconds();
1007         sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
1008         sf->sf_pos_last_checkpoint = sf->sf_pos_latest_start - 1;
1009         rc = scrub_file_store(env, scrub);
1010
1011         spin_lock(&scrub->os_lock);
1012         scrub->os_waiting = 0;
1013         scrub->os_paused = 0;
1014         scrub->os_partial_scan = 0;
1015         scrub->os_in_join = 0;
1016         scrub->os_full_scrub = 0;
1017         spin_unlock(&scrub->os_lock);
1018         wake_up_all(&thread->t_ctl_waitq);
1019         up_write(&scrub->os_rwsem);
1020
1021         CDEBUG(D_LFSCK, "%s: joined in the OI scrub with flag %u: rc = %d\n",
1022                osd_scrub2name(scrub), flags, rc);
1023
1024         EXIT;
1025 }
1026
1027 static int osd_inode_iteration(struct osd_thread_info *info,
1028                                struct osd_device *dev, __u32 max, bool preload)
1029 {
1030         struct lustre_scrub *scrub  = &dev->od_scrub.os_scrub;
1031         struct ptlrpc_thread *thread = &scrub->os_thread;
1032         struct scrub_file *sf = &scrub->os_file;
1033         osd_iit_next_policy next;
1034         osd_iit_exec_policy exec;
1035         __u64 *pos;
1036         __u64 *count;
1037         struct osd_iit_param *param;
1038         struct l_wait_info lwi = { 0 };
1039         __u32 limit;
1040         int rc;
1041         bool noslot = true;
1042         ENTRY;
1043
1044         if (preload)
1045                 goto full;
1046
1047         param = &dev->od_scrub.os_iit_param;
1048         memset(param, 0, sizeof(*param));
1049         param->sb = osd_sb(dev);
1050
1051         while (scrub->os_partial_scan && !scrub->os_in_join) {
1052                 struct osd_idmap_cache *oic = NULL;
1053
1054                 rc = osd_scrub_next(info, dev, param, &oic, noslot);
1055                 switch (rc) {
1056                 case SCRUB_NEXT_EXIT:
1057                         RETURN(0);
1058                 case SCRUB_NEXT_CRASH:
1059                         RETURN(SCRUB_IT_CRASH);
1060                 case SCRUB_NEXT_FATAL:
1061                         RETURN(-EINVAL);
1062                 case SCRUB_NEXT_WAIT: {
1063                         struct kstatfs *ksfs = &info->oti_ksfs;
1064                         __u64 saved_flags;
1065
1066                         if (dev->od_full_scrub_ratio == OFSR_NEVER ||
1067                             unlikely(sf->sf_items_updated_prior == 0))
1068                                 goto wait;
1069
1070                         if (dev->od_full_scrub_ratio == OFSR_DIRECTLY ||
1071                             scrub->os_full_scrub) {
1072                                 osd_scrub_join(info->oti_env, dev,
1073                                                SS_AUTO_FULL | SS_RESET, true);
1074                                 goto full;
1075                         }
1076
1077                         rc = param->sb->s_op->statfs(param->sb->s_root, ksfs);
1078                         if (rc == 0) {
1079                                 __u64 used = ksfs->f_files - ksfs->f_ffree;
1080
1081                                 do_div(used, sf->sf_items_updated_prior);
1082                                 /* If we hit too much inconsistent OI
1083                                  * mappings during the partial scan,
1084                                  * then scan the device completely. */
1085                                 if (used < dev->od_full_scrub_ratio) {
1086                                         osd_scrub_join(info->oti_env, dev,
1087                                                 SS_AUTO_FULL | SS_RESET, true);
1088                                         goto full;
1089                                 }
1090                         }
1091
1092 wait:
1093                         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) &&
1094                             cfs_fail_val > 0)
1095                                 continue;
1096
1097                         saved_flags = sf->sf_flags;
1098                         sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
1099                                           SF_UPGRADE | SF_AUTO);
1100                         sf->sf_status = SS_COMPLETED;
1101                         l_wait_event(thread->t_ctl_waitq,
1102                                      !thread_is_running(thread) ||
1103                                      !scrub->os_partial_scan ||
1104                                      scrub->os_in_join ||
1105                                      !list_empty(&scrub->os_inconsistent_items),
1106                                      &lwi);
1107                         sf->sf_flags = saved_flags;
1108                         sf->sf_status = SS_SCANNING;
1109
1110                         if (unlikely(!thread_is_running(thread)))
1111                                 RETURN(0);
1112
1113                         if (!scrub->os_partial_scan || scrub->os_in_join)
1114                                 goto full;
1115
1116                         continue;
1117                 }
1118                 default:
1119                         LASSERTF(rc == 0, "rc = %d\n", rc);
1120
1121                         osd_scrub_exec(info, dev, param, oic, &noslot, rc);
1122                         break;
1123                 }
1124         }
1125
1126 full:
1127         if (!preload) {
1128                 l_wait_event(thread->t_ctl_waitq,
1129                              !thread_is_running(thread) || !scrub->os_in_join,
1130                              &lwi);
1131
1132                 if (unlikely(!thread_is_running(thread)))
1133                         RETURN(0);
1134         }
1135
1136         noslot = false;
1137         if (!preload) {
1138                 next = osd_scrub_next;
1139                 exec = osd_scrub_exec;
1140                 pos = &scrub->os_pos_current;
1141                 count = &scrub->os_new_checked;
1142                 param->start = *pos;
1143                 param->bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param->sb);
1144                 param->offset =
1145                         (*pos - 1) % LDISKFS_INODES_PER_GROUP(param->sb);
1146                 param->gbase =
1147                         1 + param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
1148         } else {
1149                 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1150
1151                 next = osd_preload_next;
1152                 exec = osd_preload_exec;
1153                 pos = &ooc->ooc_pos_preload;
1154                 count = &ooc->ooc_cached_items;
1155                 param = &dev->od_otable_it->ooi_iit_param;
1156         }
1157
1158         rc = 0;
1159         limit = le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count);
1160         while (*pos <= limit && *count < max) {
1161                 struct ldiskfs_group_desc *desc;
1162                 bool next_group = false;
1163
1164                 desc = ldiskfs_get_group_desc(param->sb, param->bg, NULL);
1165                 if (!desc)
1166                         RETURN(-EIO);
1167
1168                 if (desc->bg_flags & cpu_to_le16(LDISKFS_BG_INODE_UNINIT)) {
1169                         next_group = true;
1170                         goto next_group;
1171                 }
1172
1173                 param->bitmap = ldiskfs_read_inode_bitmap(param->sb, param->bg);
1174                 if (!param->bitmap) {
1175                         CERROR("%s: fail to read bitmap for %u, "
1176                                "scrub will stop, urgent mode\n",
1177                                osd_scrub2name(scrub), (__u32)param->bg);
1178                         RETURN(-EIO);
1179                 }
1180
1181                 do {
1182                         struct osd_idmap_cache *oic = NULL;
1183
1184                         if (param->offset +
1185                                 ldiskfs_itable_unused_count(param->sb, desc) >=
1186                             LDISKFS_INODES_PER_GROUP(param->sb)) {
1187                                 next_group = true;
1188                                 goto next_group;
1189                         }
1190
1191                         rc = next(info, dev, param, &oic, noslot);
1192                         switch (rc) {
1193                         case SCRUB_NEXT_BREAK:
1194                                 next_group = true;
1195                                 goto next_group;
1196                         case SCRUB_NEXT_EXIT:
1197                                 brelse(param->bitmap);
1198                                 RETURN(0);
1199                         case SCRUB_NEXT_CRASH:
1200                                 brelse(param->bitmap);
1201                                 RETURN(SCRUB_IT_CRASH);
1202                         case SCRUB_NEXT_FATAL:
1203                                 brelse(param->bitmap);
1204                                 RETURN(-EINVAL);
1205                         }
1206
1207                         rc = exec(info, dev, param, oic, &noslot, rc);
1208                 } while (!rc && *pos <= limit && *count < max);
1209
1210 next_group:
1211                 if (param->bitmap) {
1212                         brelse(param->bitmap);
1213                         param->bitmap = NULL;
1214                 }
1215
1216                 if (rc < 0)
1217                         GOTO(out, rc);
1218
1219                 if (next_group) {
1220                         param->bg++;
1221                         param->offset = 0;
1222                         param->gbase = 1 +
1223                                 param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
1224                         *pos = param->gbase;
1225                         param->start = *pos;
1226                 }
1227         }
1228
1229         if (*pos > limit)
1230                 RETURN(SCRUB_IT_ALL);
1231
1232 out:
1233         RETURN(rc);
1234 }
1235
1236 static int osd_otable_it_preload(const struct lu_env *env,
1237                                  struct osd_otable_it *it)
1238 {
1239         struct osd_device *dev = it->ooi_dev;
1240         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1241         struct osd_otable_cache *ooc   = &it->ooi_cache;
1242         int                      rc;
1243         ENTRY;
1244
1245         rc = osd_inode_iteration(osd_oti_get(env), dev,
1246                                  OSD_OTABLE_IT_CACHE_SIZE, true);
1247         if (rc == SCRUB_IT_ALL)
1248                 it->ooi_all_cached = 1;
1249
1250         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1251                 spin_lock(&scrub->os_lock);
1252                 scrub->os_waiting = 0;
1253                 wake_up_all(&scrub->os_thread.t_ctl_waitq);
1254                 spin_unlock(&scrub->os_lock);
1255         }
1256
1257         RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1258 }
1259
1260 static int osd_scrub_main(void *args)
1261 {
1262         struct lu_env env;
1263         struct osd_device *dev = (struct osd_device *)args;
1264         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1265         struct ptlrpc_thread *thread = &scrub->os_thread;
1266         int rc;
1267         ENTRY;
1268
1269         rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
1270         if (rc != 0) {
1271                 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
1272                        osd_scrub2name(scrub), rc);
1273                 GOTO(noenv, rc);
1274         }
1275
1276         rc = osd_scrub_prep(&env, dev);
1277         if (rc != 0) {
1278                 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
1279                        osd_scrub2name(scrub), rc);
1280                 GOTO(out, rc);
1281         }
1282
1283         if (!scrub->os_full_speed && !scrub->os_partial_scan) {
1284                 struct l_wait_info lwi = { 0 };
1285                 struct osd_otable_it *it = dev->od_otable_it;
1286                 struct osd_otable_cache *ooc = &it->ooi_cache;
1287
1288                 l_wait_event(thread->t_ctl_waitq,
1289                              it->ooi_user_ready || !thread_is_running(thread),
1290                              &lwi);
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       = "PENDING",
1422                 .olm_namelen    = sizeof("PENDING") - 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         {
1675                 .olm_name       = NULL
1676         }
1677 };
1678
1679 /* Add the new introduced files under .lustre/ in the list in the future. */
1680 static const struct osd_lf_map osd_dl_maps[] = {
1681         /* .lustre/fid */
1682         {
1683                 .olm_name       = "fid",
1684                 .olm_fid        = {
1685                         .f_seq  = FID_SEQ_DOT_LUSTRE,
1686                         .f_oid  = FID_OID_DOT_LUSTRE_OBF,
1687                 },
1688                 .olm_namelen    = sizeof("fid") - 1,
1689         },
1690
1691         /* .lustre/lost+found */
1692         {
1693                 .olm_name       = "lost+found",
1694                 .olm_fid        = {
1695                         .f_seq  = FID_SEQ_DOT_LUSTRE,
1696                         .f_oid  = FID_OID_DOT_LUSTRE_LPF,
1697                 },
1698                 .olm_namelen    = sizeof("lost+found") - 1,
1699         },
1700
1701         {
1702                 .olm_name       = NULL
1703         }
1704 };
1705
1706 struct osd_ios_item {
1707         struct list_head oii_list;
1708         struct dentry   *oii_dentry;
1709         scandir_t        oii_scandir;
1710         filldir_t        oii_filldir;
1711 };
1712
1713 struct osd_ios_filldir_buf {
1714 #ifdef HAVE_DIR_CONTEXT
1715         /* please keep it as first member */
1716         struct dir_context       ctx;
1717 #endif
1718         struct osd_thread_info  *oifb_info;
1719         struct osd_device       *oifb_dev;
1720         struct dentry           *oifb_dentry;
1721 };
1722
1723 static inline struct dentry *
1724 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1725 {
1726         struct dentry *dentry;
1727
1728         dentry = ll_lookup_one_len(name, parent, namelen);
1729         if (IS_ERR(dentry)) {
1730                 int rc = PTR_ERR(dentry);
1731
1732                 if (rc != -ENOENT)
1733                         CERROR("Fail to find %.*s in %.*s (%lu/%u): rc = %d\n",
1734                                namelen, name, parent->d_name.len,
1735                                parent->d_name.name, parent->d_inode->i_ino,
1736                                parent->d_inode->i_generation, rc);
1737
1738                 return dentry;
1739         }
1740
1741         if (dentry->d_inode == NULL) {
1742                 dput(dentry);
1743                 return ERR_PTR(-ENOENT);
1744         }
1745
1746         return dentry;
1747 }
1748
1749 static int
1750 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1751                  scandir_t scandir, filldir_t filldir)
1752 {
1753         struct osd_ios_item *item;
1754         ENTRY;
1755
1756         OBD_ALLOC_PTR(item);
1757         if (item == NULL)
1758                 RETURN(-ENOMEM);
1759
1760         INIT_LIST_HEAD(&item->oii_list);
1761         item->oii_dentry = dget(dentry);
1762         item->oii_scandir = scandir;
1763         item->oii_filldir = filldir;
1764         list_add_tail(&item->oii_list, &dev->od_ios_list);
1765
1766         RETURN(0);
1767 }
1768
1769 /**
1770  * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1771  *
1772  * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1773  * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1774  * reference the inode, or fixed if it is missing or references another inode.
1775  */
1776 static int
1777 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1778                  struct inode *inode, const struct lu_fid *fid, int flags)
1779 {
1780         struct lustre_mdt_attrs *lma    = &info->oti_ost_attrs.loa_lma;
1781         struct osd_inode_id     *id     = &info->oti_id;
1782         struct osd_inode_id     *id2    = &info->oti_id2;
1783         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1784         struct scrub_file       *sf     = &scrub->os_file;
1785         struct lu_fid            tfid;
1786         int                      rc;
1787         ENTRY;
1788
1789         rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
1790                          &info->oti_ost_attrs);
1791         if (rc != 0 && rc != -ENODATA) {
1792                 CDEBUG(D_LFSCK, "%s: fail to get lma for init OI scrub: "
1793                        "rc = %d\n", osd_name(dev), rc);
1794
1795                 RETURN(rc);
1796         }
1797
1798         osd_id_gen(id, inode->i_ino, inode->i_generation);
1799         if (rc == -ENODATA) {
1800                 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID) {
1801                         lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1802                 } else {
1803                         tfid = *fid;
1804                         if (flags & OLF_IDX_IN_FID) {
1805                                 LASSERT(dev->od_index >= 0);
1806
1807                                 tfid.f_oid = dev->od_index;
1808                         }
1809                 }
1810                 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1811                 if (rc != 0) {
1812                         CDEBUG(D_LFSCK, "%s: fail to set LMA for init OI "
1813                               "scrub: rc = %d\n", osd_name(dev), rc);
1814
1815                         RETURN(rc);
1816                 }
1817         } else {
1818                 if (lma->lma_compat & LMAC_NOT_IN_OI)
1819                         RETURN(0);
1820
1821                 tfid = lma->lma_self_fid;
1822         }
1823
1824         rc = osd_oi_lookup(info, dev, &tfid, id2, 0);
1825         if (rc != 0) {
1826                 if (rc != -ENOENT)
1827                         RETURN(rc);
1828
1829                 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1830                                                DTO_INDEX_INSERT, true, 0, NULL);
1831                 if (rc > 0)
1832                         rc = 0;
1833
1834                 RETURN(rc);
1835         }
1836
1837         if (osd_id_eq_strict(id, id2))
1838                 RETURN(0);
1839
1840         if (!(sf->sf_flags & SF_INCONSISTENT)) {
1841                 scrub_file_reset(scrub, LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1842                                  SF_INCONSISTENT);
1843                 rc = scrub_file_store(info->oti_env, scrub);
1844                 if (rc != 0)
1845                         RETURN(rc);
1846         }
1847
1848         rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1849                                        DTO_INDEX_UPDATE, true, 0, NULL);
1850         if (rc > 0)
1851                 rc = 0;
1852
1853         RETURN(rc);
1854 }
1855
1856 /**
1857  * It scans the /lost+found, and for the OST-object (with filter_fid
1858  * or filter_fid_old), move them back to its proper /O/<seq>/d<x>.
1859  */
1860 #ifdef HAVE_FILLDIR_USE_CTX
1861 static int osd_ios_lf_fill(struct dir_context *buf,
1862 #else
1863 static int osd_ios_lf_fill(void *buf,
1864 #endif
1865                            const char *name, int namelen,
1866                            loff_t offset, __u64 ino, unsigned d_type)
1867 {
1868         struct osd_ios_filldir_buf *fill_buf =
1869                 (struct osd_ios_filldir_buf *)buf;
1870         struct osd_thread_info     *info     = fill_buf->oifb_info;
1871         struct osd_device          *dev      = fill_buf->oifb_dev;
1872         struct lu_fid              *fid      = &info->oti_fid;
1873         struct osd_scrub           *scrub    = &dev->od_scrub;
1874         struct dentry              *parent   = fill_buf->oifb_dentry;
1875         struct dentry              *child;
1876         struct inode               *dir      = parent->d_inode;
1877         struct inode               *inode;
1878         int                         rc;
1879         ENTRY;
1880
1881         /* skip any '.' started names */
1882         if (name[0] == '.')
1883                 RETURN(0);
1884
1885         scrub->os_lf_scanned++;
1886         child = osd_ios_lookup_one_len(name, parent, namelen);
1887         if (IS_ERR(child)) {
1888                 CDEBUG(D_LFSCK, "%s: cannot lookup child '%.*s': rc = %d\n",
1889                       osd_name(dev), namelen, name, (int)PTR_ERR(child));
1890                 RETURN(0);
1891         }
1892
1893         inode = child->d_inode;
1894         if (S_ISDIR(inode->i_mode)) {
1895                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1896                                       osd_ios_lf_fill);
1897                 if (rc != 0)
1898                         CDEBUG(D_LFSCK, "%s: cannot add child '%.*s': "
1899                               "rc = %d\n", osd_name(dev), namelen, name, rc);
1900                 GOTO(put, rc);
1901         }
1902
1903         if (!S_ISREG(inode->i_mode))
1904                 GOTO(put, rc = 0);
1905
1906         rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1907         if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1908                 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1909                 if (rc == 0) {
1910                         CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
1911                                "/lost+found.\n", namelen, name, PFID(fid));
1912                         scrub->os_lf_repaired++;
1913                 } else {
1914                         CDEBUG(D_LFSCK, "%s: cannot rename for '%.*s' "
1915                                DFID": rc = %d\n",
1916                                osd_name(dev), namelen, name, PFID(fid), rc);
1917                 }
1918         }
1919
1920         /* XXX: For MDT-objects, we can move them from /lost+found to namespace
1921          *      visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
1922          *      can process them in furtuer. */
1923
1924         GOTO(put, rc);
1925
1926 put:
1927         if (rc < 0)
1928                 scrub->os_lf_failed++;
1929         dput(child);
1930         /* skip the failure to make the scanning to continue. */
1931         return 0;
1932 }
1933
1934 #ifdef HAVE_FILLDIR_USE_CTX
1935 static int osd_ios_varfid_fill(struct dir_context *buf,
1936 #else
1937 static int osd_ios_varfid_fill(void *buf,
1938 #endif
1939                                const char *name, int namelen,
1940                                loff_t offset, __u64 ino, unsigned d_type)
1941 {
1942         struct osd_ios_filldir_buf *fill_buf =
1943                 (struct osd_ios_filldir_buf *)buf;
1944         struct osd_device          *dev      = fill_buf->oifb_dev;
1945         struct dentry              *child;
1946         int                         rc;
1947         ENTRY;
1948
1949         /* skip any '.' started names */
1950         if (name[0] == '.')
1951                 RETURN(0);
1952
1953         child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1954         if (IS_ERR(child))
1955                 RETURN(PTR_ERR(child));
1956
1957         rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1958                               NULL, 0);
1959         if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1960                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1961                                       osd_ios_varfid_fill);
1962         dput(child);
1963
1964         RETURN(rc);
1965 }
1966
1967 #ifdef HAVE_FILLDIR_USE_CTX
1968 static int osd_ios_dl_fill(struct dir_context *buf,
1969 #else
1970 static int osd_ios_dl_fill(void *buf,
1971 #endif
1972                            const char *name, int namelen,
1973                            loff_t offset, __u64 ino, unsigned d_type)
1974 {
1975         struct osd_ios_filldir_buf *fill_buf =
1976                 (struct osd_ios_filldir_buf *)buf;
1977         struct osd_device          *dev      = fill_buf->oifb_dev;
1978         const struct osd_lf_map    *map;
1979         struct dentry              *child;
1980         int                         rc       = 0;
1981         ENTRY;
1982
1983         /* skip any '.' started names */
1984         if (name[0] == '.')
1985                 RETURN(0);
1986
1987         for (map = osd_dl_maps; map->olm_name != NULL; map++) {
1988                 if (map->olm_namelen != namelen)
1989                         continue;
1990
1991                 if (strncmp(map->olm_name, name, namelen) == 0)
1992                         break;
1993         }
1994
1995         if (map->olm_name == NULL)
1996                 RETURN(0);
1997
1998         child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1999         if (IS_ERR(child))
2000                 RETURN(PTR_ERR(child));
2001
2002         rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
2003                               &map->olm_fid, map->olm_flags);
2004         dput(child);
2005
2006         RETURN(rc);
2007 }
2008
2009 #ifdef HAVE_FILLDIR_USE_CTX
2010 static int osd_ios_uld_fill(struct dir_context *buf,
2011 #else
2012 static int osd_ios_uld_fill(void *buf,
2013 #endif
2014                             const char *name, int namelen,
2015                             loff_t offset, __u64 ino, unsigned d_type)
2016 {
2017         struct osd_ios_filldir_buf *fill_buf =
2018                 (struct osd_ios_filldir_buf *)buf;
2019         struct dentry              *child;
2020         struct lu_fid               tfid;
2021         int                         rc       = 0;
2022         ENTRY;
2023
2024         /* skip any non-DFID format name */
2025         if (name[0] != '[')
2026                 RETURN(0);
2027
2028         child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
2029         if (IS_ERR(child))
2030                 RETURN(PTR_ERR(child));
2031
2032         /* skip the start '[' */
2033         sscanf(&name[1], SFID, RFID(&tfid));
2034         if (fid_is_sane(&tfid))
2035                 rc = osd_ios_scan_one(fill_buf->oifb_info, fill_buf->oifb_dev,
2036                                       child->d_inode, &tfid, 0);
2037         else
2038                 rc = -EIO;
2039         dput(child);
2040
2041         RETURN(rc);
2042 }
2043
2044 #ifdef HAVE_FILLDIR_USE_CTX
2045 static int osd_ios_root_fill(struct dir_context *buf,
2046 #else
2047 static int osd_ios_root_fill(void *buf,
2048 #endif
2049                              const char *name, int namelen,
2050                              loff_t offset, __u64 ino, unsigned d_type)
2051 {
2052         struct osd_ios_filldir_buf *fill_buf =
2053                 (struct osd_ios_filldir_buf *)buf;
2054         struct osd_device          *dev      = fill_buf->oifb_dev;
2055         const struct osd_lf_map    *map;
2056         struct dentry              *child;
2057         int                         rc       = 0;
2058         ENTRY;
2059
2060         /* skip any '.' started names */
2061         if (name[0] == '.')
2062                 RETURN(0);
2063
2064         for (map = osd_lf_maps; map->olm_name != NULL; map++) {
2065                 if (map->olm_namelen != namelen)
2066                         continue;
2067
2068                 if (strncmp(map->olm_name, name, namelen) == 0)
2069                         break;
2070         }
2071
2072         if (map->olm_name == NULL)
2073                 RETURN(0);
2074
2075         child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
2076         if (IS_ERR(child))
2077                 RETURN(PTR_ERR(child));
2078
2079         if (!(map->olm_flags & OLF_NO_OI))
2080                 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
2081                                       &map->olm_fid, map->olm_flags);
2082         if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
2083                 rc = osd_ios_new_item(dev, child, map->olm_scandir,
2084                                       map->olm_filldir);
2085         dput(child);
2086
2087         RETURN(rc);
2088 }
2089
2090 static int
2091 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
2092                      struct dentry *dentry, filldir_t filldir)
2093 {
2094         struct osd_ios_filldir_buf    buf   = {
2095 #ifdef HAVE_DIR_CONTEXT
2096                                                 .ctx.actor = filldir,
2097 #endif
2098                                                 .oifb_info = info,
2099                                                 .oifb_dev = dev,
2100                                                 .oifb_dentry = dentry };
2101         struct file                  *filp  = &info->oti_file;
2102         struct inode                 *inode = dentry->d_inode;
2103         const struct file_operations *fops  = inode->i_fop;
2104         int                           rc;
2105         ENTRY;
2106
2107         LASSERT(filldir != NULL);
2108
2109         filp->f_pos = 0;
2110         filp->f_path.dentry = dentry;
2111         filp->f_mode = FMODE_64BITHASH;
2112         filp->f_mapping = inode->i_mapping;
2113         filp->f_op = fops;
2114         filp->private_data = NULL;
2115         set_file_inode(filp, inode);
2116
2117 #ifdef HAVE_DIR_CONTEXT
2118         buf.ctx.pos = filp->f_pos;
2119         rc = fops->iterate(filp, &buf.ctx);
2120         filp->f_pos = buf.ctx.pos;
2121 #else
2122         rc = fops->readdir(filp, &buf, filldir);
2123 #endif
2124         fops->release(inode, filp);
2125
2126         RETURN(rc);
2127 }
2128
2129 static int
2130 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
2131                   struct dentry *dentry, filldir_t filldir)
2132 {
2133         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2134         struct scrub_file *sf = &scrub->os_file;
2135         struct dentry *child;
2136         int rc;
2137         ENTRY;
2138
2139         /* It is existing MDT0 device. We only allow the case of object without
2140          * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
2141          * can generate IGIF mode FID for the object and related OI mapping. If
2142          * it is on other MDTs, then becuase file-level backup/restore, related
2143          * OI mapping may be invalid already, we do not know which is the right
2144          * FID for the object. We only allow IGIF objects to reside on the MDT0.
2145          *
2146          * XXX: For the case of object on non-MDT0 device with neither LMA nor
2147          *      "fid" xattr, then something crashed. We cannot re-generate the
2148          *      FID directly, instead, the OI scrub will scan the OI structure
2149          *      and try to re-generate the LMA from the OI mapping. But if the
2150          *      OI mapping crashed or lost also, then we have to give up under
2151          *      double failure cases. */
2152         scrub->os_convert_igif = 1;
2153         child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
2154                                        strlen(dot_lustre_name));
2155         if (IS_ERR(child)) {
2156                 rc = PTR_ERR(child);
2157                 if (rc == -ENOENT) {
2158                         /* It is 1.8 MDT device. */
2159                         if (!(sf->sf_flags & SF_UPGRADE)) {
2160                                 scrub_file_reset(scrub,
2161                                         LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
2162                                         SF_UPGRADE);
2163                                 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
2164                                 rc = scrub_file_store(info->oti_env, scrub);
2165                         } else {
2166                                 rc = 0;
2167                         }
2168                 }
2169         } else {
2170                 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
2171                  * so the client will get IGIF for the ".lustre" object when
2172                  * the MDT restart.
2173                  *
2174                  * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
2175                  * it does not know whether there are some old clients cached
2176                  * the ".lustre" IGIF during the upgrading. Two choices:
2177                  *
2178                  * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
2179                  *    It will allow the old connected clients to access the
2180                  *    ".lustre" with cached IGIF. But it will cause others
2181                  *    on the MDT failed to check "fid_is_dot_lustre()".
2182                  *
2183                  * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
2184                  *    for ".lustre" in spite of whether there are some clients
2185                  *    cached the ".lustre" IGIF or not. It enables the check
2186                  *    "fid_is_dot_lustre()" on the MDT, although it will cause
2187                  *    that the old connected clients cannot access the ".lustre"
2188                  *    with the cached IGIF.
2189                  *
2190                  * Usually, it is rare case for the old connected clients
2191                  * to access the ".lustre" with cached IGIF. So we prefer
2192                  * to the solution 2). */
2193                 rc = osd_ios_scan_one(info, dev, child->d_inode,
2194                                       &LU_DOT_LUSTRE_FID, 0);
2195                 if (rc == 0)
2196                         rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
2197                                               osd_ios_dl_fill);
2198                 dput(child);
2199         }
2200
2201         RETURN(rc);
2202 }
2203
2204 static int
2205 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
2206                      struct dentry *dentry, filldir_t filldir)
2207 {
2208         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2209         struct scrub_file *sf = &scrub->os_file;
2210         struct dentry *child;
2211         int rc;
2212         ENTRY;
2213
2214         if (unlikely(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID)) {
2215                 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
2216                 rc = scrub_file_store(info->oti_env, scrub);
2217                 if (rc != 0)
2218                         RETURN(rc);
2219         }
2220
2221         child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
2222         if (!IS_ERR(child)) {
2223                 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
2224                 dput(child);
2225         } else {
2226                 rc = PTR_ERR(child);
2227         }
2228
2229         if (rc != 0 && rc != -ENOENT)
2230                 RETURN(rc);
2231
2232         child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
2233         if (!IS_ERR(child)) {
2234                 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
2235                 dput(child);
2236         } else {
2237                 rc = PTR_ERR(child);
2238         }
2239
2240         if (rc == -ENOENT)
2241                 rc = 0;
2242
2243         RETURN(rc);
2244 }
2245
2246 static int osd_initial_OI_scrub(struct osd_thread_info *info,
2247                                 struct osd_device *dev)
2248 {
2249         struct osd_ios_item     *item    = NULL;
2250         scandir_t                scandir = osd_ios_general_scan;
2251         filldir_t                filldir = osd_ios_root_fill;
2252         struct dentry           *dentry  = osd_sb(dev)->s_root;
2253         const struct osd_lf_map *map     = osd_lf_maps;
2254         int                      rc;
2255         ENTRY;
2256
2257         /* Lookup IGIF in OI by force for initial OI scrub. */
2258         dev->od_igif_inoi = 1;
2259
2260         while (1) {
2261                 rc = scandir(info, dev, dentry, filldir);
2262                 if (item != NULL) {
2263                         dput(item->oii_dentry);
2264                         OBD_FREE_PTR(item);
2265                 }
2266
2267                 if (rc != 0)
2268                         break;
2269
2270                 if (list_empty(&dev->od_ios_list))
2271                         break;
2272
2273                 item = list_entry(dev->od_ios_list.next,
2274                                   struct osd_ios_item, oii_list);
2275                 list_del_init(&item->oii_list);
2276
2277                 LASSERT(item->oii_scandir != NULL);
2278                 scandir = item->oii_scandir;
2279                 filldir = item->oii_filldir;
2280                 dentry = item->oii_dentry;
2281         }
2282
2283         while (!list_empty(&dev->od_ios_list)) {
2284                 item = list_entry(dev->od_ios_list.next,
2285                                   struct osd_ios_item, oii_list);
2286                 list_del_init(&item->oii_list);
2287                 dput(item->oii_dentry);
2288                 OBD_FREE_PTR(item);
2289         }
2290
2291         if (rc != 0)
2292                 RETURN(rc);
2293
2294         /* There maybe the case that the object has been removed, but its OI
2295          * mapping is still in the OI file, such as the "CATALOGS" after MDT
2296          * file-level backup/restore. So here cleanup the stale OI mappings. */
2297         while (map->olm_name != NULL) {
2298                 struct dentry *child;
2299
2300                 if (fid_is_zero(&map->olm_fid)) {
2301                         map++;
2302                         continue;
2303                 }
2304
2305                 child = osd_ios_lookup_one_len(map->olm_name,
2306                                                osd_sb(dev)->s_root,
2307                                                map->olm_namelen);
2308                 if (!IS_ERR(child))
2309                         dput(child);
2310                 else if (PTR_ERR(child) == -ENOENT)
2311                         osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
2312                                                   NULL, DTO_INDEX_DELETE,
2313                                                   true, 0, NULL);
2314                 map++;
2315         }
2316
2317         RETURN(0);
2318 }
2319
2320 char *osd_lf_fid2name(const struct lu_fid *fid)
2321 {
2322         const struct osd_lf_map *map = osd_lf_maps;
2323
2324         while (map->olm_name != NULL) {
2325                 if (!lu_fid_eq(fid, &map->olm_fid)) {
2326                         map++;
2327                         continue;
2328                 }
2329
2330                 if (map->olm_flags & OLF_SHOW_NAME)
2331                         return map->olm_name;
2332                 else
2333                         return "";
2334         }
2335
2336         return NULL;
2337 }
2338
2339 /* OI scrub start/stop */
2340
2341 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
2342                     __u32 flags)
2343 {
2344         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2345         int rc;
2346         ENTRY;
2347
2348         if (dev->od_dt_dev.dd_rdonly)
2349                 RETURN(-EROFS);
2350
2351         /* od_otable_mutex: prevent curcurrent start/stop */
2352         mutex_lock(&dev->od_otable_mutex);
2353         rc = scrub_start(osd_scrub_main, scrub, dev, flags);
2354         if (rc == -EALREADY) {
2355                 rc = 0;
2356                 if ((scrub->os_file.sf_flags & SF_AUTO ||
2357                      scrub->os_partial_scan) &&
2358                     !(flags & SS_AUTO_PARTIAL))
2359                         osd_scrub_join(env, dev, flags, false);
2360         }
2361         mutex_unlock(&dev->od_otable_mutex);
2362
2363         RETURN(rc);
2364 }
2365
2366 static void osd_scrub_stop(struct osd_device *dev)
2367 {
2368         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2369
2370         /* od_otable_mutex: prevent curcurrent start/stop */
2371         mutex_lock(&dev->od_otable_mutex);
2372         scrub->os_paused = 1;
2373         scrub_stop(scrub);
2374         mutex_unlock(&dev->od_otable_mutex);
2375 }
2376
2377 /* OI scrub setup/cleanup */
2378
2379 static const char osd_scrub_name[] = "OI_scrub";
2380
2381 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
2382 {
2383         struct osd_thread_info *info = osd_oti_get(env);
2384         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2385         struct lvfs_run_ctxt *ctxt = &dev->od_scrub.os_ctxt;
2386         struct scrub_file *sf = &scrub->os_file;
2387         struct super_block *sb = osd_sb(dev);
2388         struct ldiskfs_super_block *es = LDISKFS_SB(sb)->s_es;
2389         struct lvfs_run_ctxt saved;
2390         struct file *filp;
2391         struct inode *inode;
2392         struct lu_fid *fid = &info->oti_fid;
2393         struct lu_object_conf conf;
2394         struct dt_object *obj;
2395         bool dirty = false;
2396         bool restored = false;
2397         int rc = 0;
2398         ENTRY;
2399
2400         memset(&dev->od_scrub, 0, sizeof(struct osd_scrub));
2401         OBD_SET_CTXT_MAGIC(ctxt);
2402         ctxt->pwdmnt = dev->od_mnt;
2403         ctxt->pwd = dev->od_mnt->mnt_root;
2404         ctxt->fs = get_ds();
2405
2406         init_waitqueue_head(&scrub->os_thread.t_ctl_waitq);
2407         init_rwsem(&scrub->os_rwsem);
2408         spin_lock_init(&scrub->os_lock);
2409         INIT_LIST_HEAD(&scrub->os_inconsistent_items);
2410         scrub->os_name = osd_name(dev);
2411
2412         push_ctxt(&saved, ctxt);
2413         filp = filp_open(osd_scrub_name, O_RDWR |
2414                          (dev->od_dt_dev.dd_rdonly ? 0 : O_CREAT), 0644);
2415         if (IS_ERR(filp)) {
2416                 pop_ctxt(&saved, ctxt);
2417                 RETURN(PTR_ERR(filp));
2418         }
2419
2420         inode = file_inode(filp);
2421         if (!dev->od_dt_dev.dd_rdonly) {
2422                 /* 'What the @fid is' is not imporatant, because the object
2423                  * has no OI mapping, and only is visible inside the OSD.*/
2424                 lu_igif_build(fid, inode->i_ino, inode->i_generation);
2425                 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
2426                 if (rc) {
2427                         filp_close(filp, NULL);
2428                         pop_ctxt(&saved, ctxt);
2429                         RETURN(rc);
2430                 }
2431         }
2432
2433         igrab(inode);
2434         filp_close(filp, NULL);
2435         pop_ctxt(&saved, ctxt);
2436
2437         conf.loc_flags = LOC_F_NEW;
2438         obj = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, &conf));
2439         if (IS_ERR_OR_NULL(obj)) {
2440                 iput(inode);
2441                 RETURN(obj == NULL ? -ENOENT : PTR_ERR(obj));
2442         }
2443
2444         osd_dt_obj(obj)->oo_inode = inode;
2445         scrub->os_obj = obj;
2446         rc = scrub_file_load(env, scrub);
2447         if (rc == -ENOENT || rc == -EFAULT) {
2448                 scrub_file_init(scrub, es->s_uuid);
2449                 /* If the "/O" dir does not exist when mount (indicated by
2450                  * osd_device::od_maybe_new), neither for the "/OI_scrub",
2451                  * then it is quite probably that the device is a new one,
2452                  * under such case, mark it as SIF_NO_HANDLE_OLD_FID.
2453                  *
2454                  * For the rare case that "/O" and "OI_scrub" both lost on
2455                  * an old device, it can be found and cleared later.
2456                  *
2457                  * For the system with "SIF_NO_HANDLE_OLD_FID", we do not
2458                  * need to check "filter_fid_old" and to convert it to
2459                  * "filter_fid" for each object, and all the IGIF should
2460                  * have their FID mapping in OI files already. */
2461                 if (dev->od_maybe_new && rc == -ENOENT)
2462                         sf->sf_internal_flags = SIF_NO_HANDLE_OLD_FID;
2463                 dirty = true;
2464         } else if (rc < 0) {
2465                 GOTO(cleanup_obj, rc);
2466         } else {
2467                 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
2468                         struct obd_uuid *old_uuid;
2469                         struct obd_uuid *new_uuid;
2470
2471                         OBD_ALLOC_PTR(old_uuid);
2472                         OBD_ALLOC_PTR(new_uuid);
2473                         if (old_uuid == NULL || new_uuid == NULL) {
2474                                 CERROR("%s: UUID has been changed, but"
2475                                        "failed to allocate RAM for report\n",
2476                                        osd_dev2name(dev));
2477                         } else {
2478                                 class_uuid_unparse(sf->sf_uuid, old_uuid);
2479                                 class_uuid_unparse(es->s_uuid, new_uuid);
2480                                 CDEBUG(D_LFSCK, "%s: UUID has been changed "
2481                                        "from %s to %s\n", osd_dev2name(dev),
2482                                        old_uuid->uuid, new_uuid->uuid);
2483                         }
2484                         scrub_file_reset(scrub, es->s_uuid, SF_INCONSISTENT);
2485                         dirty = true;
2486                         restored = true;
2487                         if (old_uuid != NULL)
2488                                 OBD_FREE_PTR(old_uuid);
2489                         if (new_uuid != NULL)
2490                                 OBD_FREE_PTR(new_uuid);
2491                 } else if (sf->sf_status == SS_SCANNING) {
2492                         sf->sf_status = SS_CRASHED;
2493                         dirty = true;
2494                 }
2495
2496                 if ((sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0) {
2497                         LCONSOLE_WARN("%s: invalid oi count %d, set it to %d\n",
2498                                       osd_dev2name(dev), sf->sf_oi_count,
2499                                       osd_oi_count);
2500                         sf->sf_oi_count = osd_oi_count;
2501                         dirty = true;
2502                 }
2503         }
2504
2505         if (sf->sf_pos_last_checkpoint != 0)
2506                 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
2507         else
2508                 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
2509
2510         if (dirty) {
2511                 rc = scrub_file_store(env, scrub);
2512                 if (rc)
2513                         GOTO(cleanup_obj, rc);
2514         }
2515
2516         /* Initialize OI files. */
2517         rc = osd_oi_init(info, dev, restored);
2518         if (rc < 0)
2519                 GOTO(cleanup_obj, rc);
2520
2521         if (!dev->od_dt_dev.dd_rdonly) {
2522                 rc = osd_initial_OI_scrub(info, dev);
2523                 if (rc)
2524                         GOTO(cleanup_oi, rc);
2525         }
2526
2527         if (sf->sf_flags & SF_UPGRADE ||
2528             !(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID ||
2529               sf->sf_success_count > 0)) {
2530                 dev->od_igif_inoi = 0;
2531                 dev->od_check_ff = dev->od_is_ost;
2532         } else {
2533                 dev->od_igif_inoi = 1;
2534                 dev->od_check_ff = 0;
2535         }
2536
2537         if (sf->sf_flags & SF_INCONSISTENT)
2538                 /* The 'od_igif_inoi' will be set under the
2539                  * following cases:
2540                  * 1) new created system, or
2541                  * 2) restored from file-level backup, or
2542                  * 3) the upgrading completed.
2543                  *
2544                  * The 'od_igif_inoi' may be cleared by OI scrub
2545                  * later if found that the system is upgrading. */
2546                 dev->od_igif_inoi = 1;
2547
2548         if (!dev->od_dt_dev.dd_rdonly &&
2549             dev->od_auto_scrub_interval != AS_NEVER &&
2550             ((sf->sf_status == SS_PAUSED) ||
2551              (sf->sf_status == SS_CRASHED &&
2552               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2553                               SF_UPGRADE | SF_AUTO)) ||
2554              (sf->sf_status == SS_INIT &&
2555               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2556                               SF_UPGRADE))))
2557                 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
2558
2559         if (rc != 0)
2560                 GOTO(cleanup_oi, rc);
2561
2562         /* it is possible that dcache entries may keep objects after they are
2563          * deleted by OSD. While it looks safe this can cause object data to
2564          * stay until umount causing failures in tests calculating free space,
2565          * e.g. replay-ost-single. Since those dcache entries are not used
2566          * anymore let's just free them after use here */
2567         shrink_dcache_sb(sb);
2568
2569         RETURN(0);
2570 cleanup_oi:
2571         osd_oi_fini(info, dev);
2572 cleanup_obj:
2573         dt_object_put_nocache(env, scrub->os_obj);
2574         scrub->os_obj = NULL;
2575
2576         return rc;
2577 }
2578
2579 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2580 {
2581         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2582
2583         LASSERT(dev->od_otable_it == NULL);
2584
2585         if (scrub->os_obj != NULL) {
2586                 osd_scrub_stop(dev);
2587                 dt_object_put_nocache(env, scrub->os_obj);
2588                 scrub->os_obj = NULL;
2589         }
2590         if (dev->od_oi_table != NULL)
2591                 osd_oi_fini(osd_oti_get(env), dev);
2592 }
2593
2594 /* object table based iteration APIs */
2595
2596 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2597                                        struct dt_object *dt, __u32 attr)
2598 {
2599         enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2600         enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2601         struct osd_device      *dev   = osd_dev(dt->do_lu.lo_dev);
2602         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2603         struct osd_otable_it   *it;
2604         __u32                   start = 0;
2605         int                     rc;
2606         ENTRY;
2607
2608         /* od_otable_mutex: prevent curcurrent init/fini */
2609         mutex_lock(&dev->od_otable_mutex);
2610         if (dev->od_otable_it != NULL)
2611                 GOTO(out, it = ERR_PTR(-EALREADY));
2612
2613         OBD_ALLOC_PTR(it);
2614         if (it == NULL)
2615                 GOTO(out, it = ERR_PTR(-ENOMEM));
2616
2617         dev->od_otable_it = it;
2618         it->ooi_dev = dev;
2619         it->ooi_cache.ooc_consumer_idx = -1;
2620         if (flags & DOIF_OUTUSED)
2621                 it->ooi_used_outside = 1;
2622
2623         if (flags & DOIF_RESET)
2624                 start |= SS_RESET;
2625
2626         if (valid & DOIV_ERROR_HANDLE) {
2627                 if (flags & DOIF_FAILOUT)
2628                         start |= SS_SET_FAILOUT;
2629                 else
2630                         start |= SS_CLEAR_FAILOUT;
2631         }
2632
2633         if (valid & DOIV_DRYRUN) {
2634                 if (flags & DOIF_DRYRUN)
2635                         start |= SS_SET_DRYRUN;
2636                 else
2637                         start |= SS_CLEAR_DRYRUN;
2638         }
2639
2640         rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
2641         if (rc == -EALREADY) {
2642                 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2643         } else  if (rc < 0) {
2644                 dev->od_otable_it = NULL;
2645                 OBD_FREE_PTR(it);
2646                 it = ERR_PTR(rc);
2647         } else {
2648                 /* We have to start from the begining. */
2649                 it->ooi_cache.ooc_pos_preload =
2650                         LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2651         }
2652
2653         GOTO(out, it);
2654
2655 out:
2656         mutex_unlock(&dev->od_otable_mutex);
2657         return (struct dt_it *)it;
2658 }
2659
2660 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2661 {
2662         struct osd_otable_it *it  = (struct osd_otable_it *)di;
2663         struct osd_device    *dev = it->ooi_dev;
2664
2665         /* od_otable_mutex: prevent curcurrent init/fini */
2666         mutex_lock(&dev->od_otable_mutex);
2667         scrub_stop(&dev->od_scrub.os_scrub);
2668         LASSERT(dev->od_otable_it == it);
2669
2670         dev->od_otable_it = NULL;
2671         mutex_unlock(&dev->od_otable_mutex);
2672         OBD_FREE_PTR(it);
2673 }
2674
2675 static int osd_otable_it_get(const struct lu_env *env,
2676                              struct dt_it *di, const struct dt_key *key)
2677 {
2678         return 0;
2679 }
2680
2681 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2682 {
2683 }
2684
2685 static inline int
2686 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
2687 {
2688         spin_lock(&scrub->os_lock);
2689         if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2690             scrub->os_waiting ||
2691             !thread_is_running(&scrub->os_thread))
2692                 it->ooi_waiting = 0;
2693         else
2694                 it->ooi_waiting = 1;
2695         spin_unlock(&scrub->os_lock);
2696
2697         return !it->ooi_waiting;
2698 }
2699
2700 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2701 {
2702         struct osd_otable_it    *it     = (struct osd_otable_it *)di;
2703         struct osd_device       *dev    = it->ooi_dev;
2704         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2705         struct osd_otable_cache *ooc    = &it->ooi_cache;
2706         struct ptlrpc_thread    *thread = &scrub->os_thread;
2707         struct l_wait_info       lwi    = { 0 };
2708         int                      rc;
2709         ENTRY;
2710
2711         LASSERT(it->ooi_user_ready);
2712
2713 again:
2714         if (!thread_is_running(thread) && !it->ooi_used_outside)
2715                 RETURN(1);
2716
2717         if (ooc->ooc_cached_items > 0) {
2718                 ooc->ooc_cached_items--;
2719                 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2720                                         ~OSD_OTABLE_IT_CACHE_MASK;
2721                 RETURN(0);
2722         }
2723
2724         if (it->ooi_all_cached) {
2725                 l_wait_event(thread->t_ctl_waitq,
2726                              !thread_is_running(thread),
2727                              &lwi);
2728                 RETURN(1);
2729         }
2730
2731         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2732                 spin_lock(&scrub->os_lock);
2733                 scrub->os_waiting = 0;
2734                 wake_up_all(&scrub->os_thread.t_ctl_waitq);
2735                 spin_unlock(&scrub->os_lock);
2736         }
2737
2738         if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2739                 l_wait_event(thread->t_ctl_waitq,
2740                              osd_otable_it_wakeup(scrub, it),
2741                              &lwi);
2742
2743         if (!thread_is_running(thread) && !it->ooi_used_outside)
2744                 RETURN(1);
2745
2746         rc = osd_otable_it_preload(env, it);
2747         if (rc >= 0)
2748                 goto again;
2749
2750         RETURN(rc);
2751 }
2752
2753 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2754                                         const struct dt_it *di)
2755 {
2756         return NULL;
2757 }
2758
2759 static int osd_otable_it_key_size(const struct lu_env *env,
2760                                   const struct dt_it *di)
2761 {
2762         return sizeof(__u64);
2763 }
2764
2765 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2766                              struct dt_rec *rec, __u32 attr)
2767 {
2768         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2769         struct osd_otable_cache *ooc = &it->ooi_cache;
2770
2771         *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2772
2773         /* Filter out Invald FID already. */
2774         LASSERTF(fid_is_sane((struct lu_fid *)rec),
2775                  "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2776                  PFID((struct lu_fid *)rec),
2777                  ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2778
2779         return 0;
2780 }
2781
2782 static __u64 osd_otable_it_store(const struct lu_env *env,
2783                                  const struct dt_it *di)
2784 {
2785         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2786         struct osd_otable_cache *ooc = &it->ooi_cache;
2787         __u64                    hash;
2788
2789         if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2790                 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2791         else
2792                 hash = ooc->ooc_pos_preload;
2793         return hash;
2794 }
2795
2796 /**
2797  * Set the OSD layer iteration start position as the specified hash.
2798  */
2799 static int osd_otable_it_load(const struct lu_env *env,
2800                               const struct dt_it *di, __u64 hash)
2801 {
2802         struct osd_otable_it    *it    = (struct osd_otable_it *)di;
2803         struct osd_device       *dev   = it->ooi_dev;
2804         struct osd_otable_cache *ooc   = &it->ooi_cache;
2805         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2806         struct osd_iit_param    *param = &it->ooi_iit_param;
2807         int                      rc;
2808         ENTRY;
2809
2810         /* Forbid to set iteration position after iteration started. */
2811         if (it->ooi_user_ready)
2812                 RETURN(-EPERM);
2813
2814         LASSERT(!scrub->os_partial_scan);
2815
2816         if (hash > OSD_OTABLE_MAX_HASH)
2817                 hash = OSD_OTABLE_MAX_HASH;
2818
2819         /* The hash is the last checkpoint position,
2820          * we will start from the next one. */
2821         ooc->ooc_pos_preload = hash + 1;
2822         if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2823                 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2824
2825         it->ooi_user_ready = 1;
2826         if (!scrub->os_full_speed)
2827                 wake_up_all(&scrub->os_thread.t_ctl_waitq);
2828
2829         memset(param, 0, sizeof(*param));
2830         param->sb = osd_sb(dev);
2831         param->start = ooc->ooc_pos_preload;
2832         param->bg = (ooc->ooc_pos_preload - 1) /
2833                     LDISKFS_INODES_PER_GROUP(param->sb);
2834         param->offset = (ooc->ooc_pos_preload - 1) %
2835                         LDISKFS_INODES_PER_GROUP(param->sb);
2836         param->gbase = 1 + param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
2837
2838         /* Unplug OSD layer iteration by the first next() call. */
2839         rc = osd_otable_it_next(env, (struct dt_it *)it);
2840
2841         RETURN(rc);
2842 }
2843
2844 static int osd_otable_it_key_rec(const struct lu_env *env,
2845                                  const struct dt_it *di, void *key_rec)
2846 {
2847         return 0;
2848 }
2849
2850 const struct dt_index_operations osd_otable_ops = {
2851         .dio_it = {
2852                 .init     = osd_otable_it_init,
2853                 .fini     = osd_otable_it_fini,
2854                 .get      = osd_otable_it_get,
2855                 .put      = osd_otable_it_put,
2856                 .next     = osd_otable_it_next,
2857                 .key      = osd_otable_it_key,
2858                 .key_size = osd_otable_it_key_size,
2859                 .rec      = osd_otable_it_rec,
2860                 .store    = osd_otable_it_store,
2861                 .load     = osd_otable_it_load,
2862                 .key_rec  = osd_otable_it_key_rec,
2863         }
2864 };
2865
2866 /* high priority inconsistent items list APIs */
2867
2868 #define SCRUB_BAD_OIMAP_DECAY_INTERVAL  60
2869
2870 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
2871                    int insert)
2872 {
2873         struct osd_inconsistent_item *oii;
2874         struct osd_scrub *oscrub = &dev->od_scrub;
2875         struct lustre_scrub *lscrub = &oscrub->os_scrub;
2876         struct ptlrpc_thread *thread = &lscrub->os_thread;
2877         int wakeup = 0;
2878         ENTRY;
2879
2880         OBD_ALLOC_PTR(oii);
2881         if (unlikely(oii == NULL))
2882                 RETURN(-ENOMEM);
2883
2884         INIT_LIST_HEAD(&oii->oii_list);
2885         oii->oii_cache = *oic;
2886         oii->oii_insert = insert;
2887
2888         if (lscrub->os_partial_scan) {
2889                 __u64 now = ktime_get_real_seconds();
2890
2891                 /* If there haven't been errors in a long time,
2892                  * decay old count until either the errors are
2893                  * gone or we reach the current interval. */
2894                 while (unlikely(oscrub->os_bad_oimap_count > 0 &&
2895                                 oscrub->os_bad_oimap_time +
2896                                 SCRUB_BAD_OIMAP_DECAY_INTERVAL < now)) {
2897                         oscrub->os_bad_oimap_count >>= 1;
2898                         oscrub->os_bad_oimap_time +=
2899                                 SCRUB_BAD_OIMAP_DECAY_INTERVAL;
2900                 }
2901
2902                 oscrub->os_bad_oimap_time = now;
2903                 if (++oscrub->os_bad_oimap_count >
2904                     dev->od_full_scrub_threshold_rate)
2905                         lscrub->os_full_scrub = 1;
2906         }
2907
2908         spin_lock(&lscrub->os_lock);
2909         if (unlikely(!thread_is_running(thread))) {
2910                 spin_unlock(&lscrub->os_lock);
2911                 OBD_FREE_PTR(oii);
2912                 RETURN(-EAGAIN);
2913         }
2914
2915         if (list_empty(&lscrub->os_inconsistent_items))
2916                 wakeup = 1;
2917         list_add_tail(&oii->oii_list, &lscrub->os_inconsistent_items);
2918         spin_unlock(&lscrub->os_lock);
2919
2920         if (wakeup != 0)
2921                 wake_up_all(&thread->t_ctl_waitq);
2922
2923         RETURN(0);
2924 }
2925
2926 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2927                    struct osd_inode_id *id)
2928 {
2929         struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2930         struct osd_inconsistent_item *oii;
2931         ENTRY;
2932
2933         spin_lock(&scrub->os_lock);
2934         list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2935                 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2936                         *id = oii->oii_cache.oic_lid;
2937                         spin_unlock(&scrub->os_lock);
2938                         RETURN(0);
2939                 }
2940         }
2941         spin_unlock(&scrub->os_lock);
2942
2943         RETURN(-ENOENT);
2944 }
2945
2946 void osd_scrub_dump(struct seq_file *m, struct osd_device *dev)
2947 {
2948         struct osd_scrub *scrub = &dev->od_scrub;
2949
2950         scrub_dump(m, &scrub->os_scrub);
2951         seq_printf(m, "lf_scanned: %llu\n"
2952                    "lf_%s: %llu\n"
2953                    "lf_failed: %llu\n",
2954                    scrub->os_lf_scanned,
2955                    scrub->os_scrub.os_file.sf_param & SP_DRYRUN ?
2956                         "inconsistent" : "repaired",
2957                    scrub->os_lf_repaired,
2958                    scrub->os_lf_failed);
2959 }