Whamcloud - gitweb
95b0caf14394a713e882eb209283826ef98f2864
[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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, 2013, 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_MDS
39
40 #include <lustre/lustre_idl.h>
41 #include <lustre_disk.h>
42 #include <dt_object.h>
43
44 #include "osd_internal.h"
45 #include "osd_oi.h"
46 #include "osd_scrub.h"
47
48 #define HALF_SEC        (CFS_HZ >> 1)
49
50 #define OSD_OTABLE_MAX_HASH             0x00000000ffffffffULL
51
52 #define SCRUB_NEXT_BREAK        1 /* exit current loop and process next group */
53 #define SCRUB_NEXT_CONTINUE     2 /* skip current object and process next bit */
54 #define SCRUB_NEXT_EXIT         3 /* exit all the loops */
55 #define SCRUB_NEXT_WAIT         4 /* wait for free cache slot */
56 #define SCRUB_NEXT_CRASH        5 /* simulate system crash during OI scrub */
57 #define SCRUB_NEXT_FATAL        6 /* simulate failure during OI scrub */
58 #define SCRUB_NEXT_NOSCRUB      7 /* new created object, no scrub on it */
59 #define SCRUB_NEXT_NOLMA        8 /* the inode has no FID-in-LMA */
60 #define SCRUB_NEXT_OSTOBJ       9 /* for OST-object */
61 #define SCRUB_NEXT_OSTOBJ_OLD   10 /* old OST-object, no LMA or no FID-on-OST
62                                     * flags in LMA */
63
64 /* misc functions */
65
66 static inline struct osd_device *osd_scrub2dev(struct osd_scrub *scrub)
67 {
68         return container_of0(scrub, struct osd_device, od_scrub);
69 }
70
71 static inline struct super_block *osd_scrub2sb(struct osd_scrub *scrub)
72 {
73         return osd_sb(osd_scrub2dev(scrub));
74 }
75
76 static inline int osd_scrub_has_window(struct osd_scrub *scrub,
77                                        struct osd_otable_cache *ooc)
78 {
79         return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
80 }
81
82 /**
83  * update/insert/delete the specified OI mapping (@fid @id) according to the ops
84  *
85  * \retval   1, changed nothing
86  * \retval   0, changed successfully
87  * \retval -ve, on error
88  */
89 static int osd_scrub_refresh_mapping(struct osd_thread_info *info,
90                                      struct osd_device *dev,
91                                      const struct lu_fid *fid,
92                                      const struct osd_inode_id *id,
93                                      int ops, enum oi_check_flags flags)
94 {
95         const struct lu_env *env = info->oti_env;
96         struct thandle      *th;
97         struct osd_thandle  *oh;
98         int                  rc;
99         ENTRY;
100
101         th = dt_trans_create(env, &dev->od_dt_dev);
102         if (IS_ERR(th))
103                 RETURN(PTR_ERR(th));
104
105         oh = container_of0(th, struct osd_thandle, ot_super);
106         LASSERT(oh->ot_handle == NULL);
107
108         switch (ops) {
109         case DTO_INDEX_UPDATE:
110                 osd_trans_declare_op(env, oh, OSD_OT_UPDATE,
111                                      osd_dto_credits_noquota[DTO_INDEX_UPDATE]);
112                 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
113                 if (rc != 0)
114                         GOTO(stop, rc);
115
116                 rc = osd_oi_update(info, dev, fid, id, th, flags);
117                 if (unlikely(rc == -ENOENT)) {
118                         /* Some unlink thread may removed the OI mapping. */
119                         rc = 1;
120                 }
121                 break;
122         case DTO_INDEX_INSERT:
123                 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
124                                      osd_dto_credits_noquota[DTO_INDEX_INSERT]);
125                 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
126                 if (rc != 0)
127                         GOTO(stop, rc);
128
129                 rc = osd_oi_insert(info, dev, fid, id, th, flags);
130                 if (unlikely(rc == -EEXIST)) {
131                         rc = 1;
132                         /* XXX: There are trouble things when adding OI
133                          *      mapping for IGIF object, which may cause
134                          *      multiple objects to be mapped to the same
135                          *      IGIF formatted FID. Consider the following
136                          *      situations:
137                          *
138                          *      1) The MDT is upgrading from 1.8 device.
139                          *      The OI scrub generates IGIF FID1 for the
140                          *      OBJ1 and adds the OI mapping.
141                          *
142                          *      2) For some reason, the OI scrub does not
143                          *      process all the IGIF objects completely.
144                          *
145                          *      3) The MDT is backuped and restored against
146                          *      this device.
147                          *
148                          *      4) When the MDT mounts up, the OI scrub will
149                          *      try to rebuild the OI files. For some IGIF
150                          *      object, OBJ2, which was not processed by the
151                          *      OI scrub before the backup/restore, and the
152                          *      new generated IGIF formatted FID may be just
153                          *      the FID1, the same as OBJ1.
154                          *
155                          *      Under such case, the OI scrub cannot know how
156                          *      to generate new FID for the OBJ2.
157                          *
158                          *      Currently, we do nothing for that. One possible
159                          *      solution is to generate new normal FID for the
160                          *      conflict object.
161                          *
162                          *      Anyway, it is rare, only exists in theory. */
163                 }
164                 break;
165         case DTO_INDEX_DELETE:
166                 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
167                                      osd_dto_credits_noquota[DTO_INDEX_DELETE]);
168                 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
169                 if (rc != 0)
170                         GOTO(stop, rc);
171
172                 rc = osd_oi_delete(info, dev, fid, th, flags);
173                 if (rc == -ENOENT) {
174                         /* It is normal that the unlink thread has removed the
175                          * OI mapping already. */
176                         rc = 1;
177                 }
178                 break;
179         default:
180                 LASSERTF(0, "Unexpected ops %d\n", ops);
181                 break;
182         }
183
184         GOTO(stop, rc);
185
186 stop:
187         dt_trans_stop(env, &dev->od_dt_dev, th);
188         return rc;
189 }
190
191 /* OI_scrub file ops */
192
193 static void osd_scrub_file_to_cpu(struct scrub_file *des,
194                                   struct scrub_file *src)
195 {
196         memcpy(des->sf_uuid, src->sf_uuid, 16);
197         des->sf_flags   = le64_to_cpu(src->sf_flags);
198         des->sf_magic   = le32_to_cpu(src->sf_magic);
199         des->sf_status  = le16_to_cpu(src->sf_status);
200         des->sf_param   = le16_to_cpu(src->sf_param);
201         des->sf_time_last_complete      =
202                                 le64_to_cpu(src->sf_time_last_complete);
203         des->sf_time_latest_start       =
204                                 le64_to_cpu(src->sf_time_latest_start);
205         des->sf_time_last_checkpoint    =
206                                 le64_to_cpu(src->sf_time_last_checkpoint);
207         des->sf_pos_latest_start        =
208                                 le64_to_cpu(src->sf_pos_latest_start);
209         des->sf_pos_last_checkpoint     =
210                                 le64_to_cpu(src->sf_pos_last_checkpoint);
211         des->sf_pos_first_inconsistent  =
212                                 le64_to_cpu(src->sf_pos_first_inconsistent);
213         des->sf_items_checked           =
214                                 le64_to_cpu(src->sf_items_checked);
215         des->sf_items_updated           =
216                                 le64_to_cpu(src->sf_items_updated);
217         des->sf_items_failed            =
218                                 le64_to_cpu(src->sf_items_failed);
219         des->sf_items_updated_prior     =
220                                 le64_to_cpu(src->sf_items_updated_prior);
221         des->sf_run_time        = le32_to_cpu(src->sf_run_time);
222         des->sf_success_count   = le32_to_cpu(src->sf_success_count);
223         des->sf_oi_count        = le16_to_cpu(src->sf_oi_count);
224         memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
225 }
226
227 static void osd_scrub_file_to_le(struct scrub_file *des,
228                                  struct scrub_file *src)
229 {
230         memcpy(des->sf_uuid, src->sf_uuid, 16);
231         des->sf_flags   = cpu_to_le64(src->sf_flags);
232         des->sf_magic   = cpu_to_le32(src->sf_magic);
233         des->sf_status  = cpu_to_le16(src->sf_status);
234         des->sf_param   = cpu_to_le16(src->sf_param);
235         des->sf_time_last_complete      =
236                                 cpu_to_le64(src->sf_time_last_complete);
237         des->sf_time_latest_start       =
238                                 cpu_to_le64(src->sf_time_latest_start);
239         des->sf_time_last_checkpoint    =
240                                 cpu_to_le64(src->sf_time_last_checkpoint);
241         des->sf_pos_latest_start        =
242                                 cpu_to_le64(src->sf_pos_latest_start);
243         des->sf_pos_last_checkpoint     =
244                                 cpu_to_le64(src->sf_pos_last_checkpoint);
245         des->sf_pos_first_inconsistent  =
246                                 cpu_to_le64(src->sf_pos_first_inconsistent);
247         des->sf_items_checked           =
248                                 cpu_to_le64(src->sf_items_checked);
249         des->sf_items_updated           =
250                                 cpu_to_le64(src->sf_items_updated);
251         des->sf_items_failed            =
252                                 cpu_to_le64(src->sf_items_failed);
253         des->sf_items_updated_prior     =
254                                 cpu_to_le64(src->sf_items_updated_prior);
255         des->sf_run_time        = cpu_to_le32(src->sf_run_time);
256         des->sf_success_count   = cpu_to_le32(src->sf_success_count);
257         des->sf_oi_count        = cpu_to_le16(src->sf_oi_count);
258         memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
259 }
260
261 static void osd_scrub_file_init(struct osd_scrub *scrub, __u8 *uuid)
262 {
263         struct scrub_file *sf = &scrub->os_file;
264
265         memset(sf, 0, sizeof(*sf));
266         memcpy(sf->sf_uuid, uuid, 16);
267         sf->sf_magic = SCRUB_MAGIC_V1;
268         sf->sf_status = SS_INIT;
269 }
270
271 void osd_scrub_file_reset(struct osd_scrub *scrub, __u8 *uuid, __u64 flags)
272 {
273         struct scrub_file *sf = &scrub->os_file;
274
275         CDEBUG(D_LFSCK, "Reset OI scrub file, flags = "LPX64"\n", flags);
276         memcpy(sf->sf_uuid, uuid, 16);
277         sf->sf_status = SS_INIT;
278         sf->sf_flags |= flags;
279         sf->sf_param = 0;
280         sf->sf_run_time = 0;
281         sf->sf_time_latest_start = 0;
282         sf->sf_time_last_checkpoint = 0;
283         sf->sf_pos_latest_start = 0;
284         sf->sf_pos_last_checkpoint = 0;
285         sf->sf_pos_first_inconsistent = 0;
286         sf->sf_items_checked = 0;
287         sf->sf_items_updated = 0;
288         sf->sf_items_failed = 0;
289         sf->sf_items_updated_prior = 0;
290         sf->sf_items_noscrub = 0;
291         sf->sf_items_igif = 0;
292 }
293
294 static int osd_scrub_file_load(struct osd_scrub *scrub)
295 {
296         loff_t  pos  = 0;
297         char   *name = LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name;
298         int     len  = sizeof(scrub->os_file_disk);
299         int     rc;
300
301         rc = osd_ldiskfs_read(scrub->os_inode, &scrub->os_file_disk, len, &pos);
302         if (rc == len) {
303                 struct scrub_file *sf = &scrub->os_file;
304
305                 osd_scrub_file_to_cpu(sf, &scrub->os_file_disk);
306                 if (sf->sf_magic != SCRUB_MAGIC_V1) {
307                         CWARN("%.16s: invalid scrub magic 0x%x != 0x%x\n,",
308                               name, sf->sf_magic, SCRUB_MAGIC_V1);
309                         /* Process it as new scrub file. */
310                         rc = -ENOENT;
311                 } else {
312                         rc = 0;
313                 }
314         } else if (rc != 0) {
315                 CERROR("%.16s: fail to load scrub file, expected = %d, "
316                        "rc = %d\n", name, len, rc);
317                 if (rc > 0)
318                         rc = -EFAULT;
319         } else {
320                 /* return -ENOENT for empty scrub file case. */
321                 rc = -ENOENT;
322         }
323
324         return rc;
325 }
326
327 int osd_scrub_file_store(struct osd_scrub *scrub)
328 {
329         struct osd_device *dev;
330         handle_t          *jh;
331         loff_t             pos     = 0;
332         int                len     = sizeof(scrub->os_file_disk);
333         int                credits;
334         int                rc;
335
336         dev = container_of0(scrub, struct osd_device, od_scrub);
337         credits = osd_dto_credits_noquota[DTO_WRITE_BASE] +
338                   osd_dto_credits_noquota[DTO_WRITE_BLOCK];
339         jh = ldiskfs_journal_start_sb(osd_sb(dev), credits);
340         if (IS_ERR(jh)) {
341                 rc = PTR_ERR(jh);
342                 CERROR("%.16s: fail to start trans for scrub store, rc = %d\n",
343                        LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,rc);
344                 return rc;
345         }
346
347         osd_scrub_file_to_le(&scrub->os_file_disk, &scrub->os_file);
348         rc = osd_ldiskfs_write_record(scrub->os_inode, &scrub->os_file_disk,
349                                       len, 0, &pos, jh);
350         ldiskfs_journal_stop(jh);
351         if (rc != 0)
352                 CERROR("%.16s: fail to store scrub file, expected = %d, "
353                        "rc = %d\n",
354                        LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
355                        len, rc);
356         scrub->os_time_last_checkpoint = cfs_time_current();
357         scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
358                                 cfs_time_seconds(SCRUB_CHECKPOINT_INTERVAL);
359         return rc;
360 }
361
362 /* OI scrub APIs */
363
364 static int osd_scrub_prep(struct osd_device *dev)
365 {
366         struct osd_scrub     *scrub  = &dev->od_scrub;
367         struct ptlrpc_thread *thread = &scrub->os_thread;
368         struct scrub_file    *sf     = &scrub->os_file;
369         __u32                 flags  = scrub->os_start_flags;
370         int                   rc;
371         ENTRY;
372
373         down_write(&scrub->os_rwsem);
374         if (flags & SS_SET_FAILOUT)
375                 sf->sf_param |= SP_FAILOUT;
376
377         if (flags & SS_CLEAR_FAILOUT)
378                 sf->sf_param &= ~SP_FAILOUT;
379
380         if (flags & SS_RESET)
381                 osd_scrub_file_reset(scrub,
382                         LDISKFS_SB(osd_sb(dev))->s_es->s_uuid, 0);
383
384         if (flags & SS_AUTO) {
385                 scrub->os_full_speed = 1;
386                 sf->sf_flags |= SF_AUTO;
387         } else {
388                 scrub->os_full_speed = 0;
389         }
390
391         if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
392                 scrub->os_full_speed = 1;
393
394         scrub->os_in_prior = 0;
395         scrub->os_waiting = 0;
396         scrub->os_paused = 0;
397         scrub->os_new_checked = 0;
398         if (sf->sf_pos_last_checkpoint != 0)
399                 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
400         else
401                 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
402
403         scrub->os_pos_current = sf->sf_pos_latest_start;
404         sf->sf_status = SS_SCANNING;
405         sf->sf_time_latest_start = cfs_time_current_sec();
406         sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
407         rc = osd_scrub_file_store(scrub);
408         if (rc == 0) {
409                 spin_lock(&scrub->os_lock);
410                 thread_set_flags(thread, SVC_RUNNING);
411                 spin_unlock(&scrub->os_lock);
412                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
413         }
414         up_write(&scrub->os_rwsem);
415
416         RETURN(rc);
417 }
418
419 static int
420 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
421                        struct osd_idmap_cache *oic, int val)
422 {
423         struct osd_scrub             *scrub  = &dev->od_scrub;
424         struct scrub_file            *sf     = &scrub->os_file;
425         struct lu_fid                *fid    = &oic->oic_fid;
426         struct osd_inode_id          *lid    = &oic->oic_lid;
427         struct osd_inode_id          *lid2   = &info->oti_id;
428         struct osd_inconsistent_item *oii    = NULL;
429         struct inode                 *inode  = NULL;
430         int                           ops    = DTO_INDEX_UPDATE;
431         int                           idx;
432         int                           rc;
433         ENTRY;
434
435         down_write(&scrub->os_rwsem);
436         scrub->os_new_checked++;
437         if (val < 0)
438                 GOTO(out, rc = val);
439
440         if (scrub->os_in_prior)
441                 oii = cfs_list_entry(oic, struct osd_inconsistent_item,
442                                      oii_cache);
443
444         if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
445                 GOTO(out, rc = 0);
446
447         if (fid_is_igif(fid))
448                 sf->sf_items_igif++;
449
450         if (val == SCRUB_NEXT_OSTOBJ_OLD) {
451                 inode = osd_iget(info, dev, lid);
452                 if (IS_ERR(inode)) {
453                         rc = PTR_ERR(inode);
454                         /* Someone removed the inode. */
455                         if (rc == -ENOENT || rc == -ESTALE)
456                                 rc = 0;
457                         GOTO(out, rc);
458                 }
459
460                 sf->sf_flags |= SF_UPGRADE;
461                 rc = osd_ea_fid_set(info, inode, fid,
462                                     LMAC_FID_ON_OST, 0);
463                 if (rc != 0)
464                         GOTO(out, rc);
465         }
466
467         if ((val == SCRUB_NEXT_NOLMA) &&
468             (!dev->od_handle_nolma || OBD_FAIL_CHECK(OBD_FAIL_FID_NOLMA)))
469                 GOTO(out, rc = 0);
470
471         if ((oii != NULL && oii->oii_insert) || (val == SCRUB_NEXT_NOLMA))
472                 goto iget;
473
474         rc = osd_oi_lookup(info, dev, fid, lid2,
475                 (val == SCRUB_NEXT_OSTOBJ ||
476                  val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
477         if (rc != 0) {
478                 if (rc != -ENOENT)
479                         GOTO(out, rc);
480
481 iget:
482                 if (inode == NULL) {
483                         inode = osd_iget(info, dev, lid);
484                         if (IS_ERR(inode)) {
485                                 rc = PTR_ERR(inode);
486                                 /* Someone removed the inode. */
487                                 if (rc == -ENOENT || rc == -ESTALE)
488                                         rc = 0;
489                                 GOTO(out, rc);
490                         }
491                 }
492
493                 scrub->os_full_speed = 1;
494                 ops = DTO_INDEX_INSERT;
495                 idx = osd_oi_fid2idx(dev, fid);
496                 switch (val) {
497                 case SCRUB_NEXT_NOLMA:
498                         sf->sf_flags |= SF_UPGRADE;
499                         rc = osd_ea_fid_set(info, inode, fid, 0, 0);
500                         if (rc != 0)
501                                 GOTO(out, rc);
502
503                         if (!(sf->sf_flags & SF_INCONSISTENT))
504                                 dev->od_igif_inoi = 0;
505                         break;
506                 case SCRUB_NEXT_OSTOBJ:
507                         sf->sf_flags |= SF_INCONSISTENT;
508                 case SCRUB_NEXT_OSTOBJ_OLD:
509                         break;
510                 default:
511                         sf->sf_flags |= SF_RECREATED;
512                         if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
513                                 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
514                         break;
515                 }
516         } else if (osd_id_eq(lid, lid2)) {
517                 GOTO(out, rc = 0);
518         } else {
519                 scrub->os_full_speed = 1;
520                 sf->sf_flags |= SF_INCONSISTENT;
521
522                 /* XXX: If the device is restored from file-level backup, then
523                  *      some IGIFs may have been already in OI files, and some
524                  *      may be not yet. Means upgrading from 1.8 may be partly
525                  *      processed, but some clients may hold some immobilized
526                  *      IGIFs, and use them to access related objects. Under
527                  *      such case, OSD does not know whether an given IGIF has
528                  *      been processed or to be processed, and it also cannot
529                  *      generate local ino#/gen# directly from the immobilized
530                  *      IGIF because of the backup/restore. Then force OSD to
531                  *      lookup the given IGIF in OI files, and if no entry,
532                  *      then ask the client to retry after upgrading completed.
533                  *      No better choice. */
534                 dev->od_igif_inoi = 1;
535         }
536
537         rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops,
538                         (val == SCRUB_NEXT_OSTOBJ ||
539                          val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
540         if (rc == 0) {
541                 if (scrub->os_in_prior)
542                         sf->sf_items_updated_prior++;
543                 else
544                         sf->sf_items_updated++;
545
546                 /* The target has been changed, need to be re-loaded. */
547                 lu_object_purge(info->oti_env, osd2lu_dev(dev), fid);
548         }
549
550         GOTO(out, rc);
551
552 out:
553         if (rc < 0) {
554                 sf->sf_items_failed++;
555                 if (sf->sf_pos_first_inconsistent == 0 ||
556                     sf->sf_pos_first_inconsistent > lid->oii_ino)
557                         sf->sf_pos_first_inconsistent = lid->oii_ino;
558         } else {
559                 rc = 0;
560         }
561
562         /* There may be conflict unlink during the OI scrub,
563          * if happend, then remove the new added OI mapping. */
564         if (ops == DTO_INDEX_INSERT && inode != NULL && !IS_ERR(inode) &&
565             unlikely(inode->i_nlink == 0))
566                 osd_scrub_refresh_mapping(info, dev, fid, lid,
567                                 DTO_INDEX_DELETE,
568                                 (val == SCRUB_NEXT_OSTOBJ ||
569                                  val == SCRUB_NEXT_OSTOBJ_OLD) ?
570                                 OI_KNOWN_ON_OST : 0);
571         up_write(&scrub->os_rwsem);
572
573         if (inode != NULL && !IS_ERR(inode))
574                 iput(inode);
575
576         if (oii != NULL) {
577                 LASSERT(!cfs_list_empty(&oii->oii_list));
578
579                 spin_lock(&scrub->os_lock);
580                 cfs_list_del_init(&oii->oii_list);
581                 spin_unlock(&scrub->os_lock);
582                 OBD_FREE_PTR(oii);
583         }
584         RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
585 }
586
587 static int osd_scrub_checkpoint(struct osd_scrub *scrub)
588 {
589         struct scrub_file *sf = &scrub->os_file;
590         int                rc;
591
592         if (likely(cfs_time_before(cfs_time_current(),
593                                    scrub->os_time_next_checkpoint) ||
594                    scrub->os_new_checked == 0))
595                 return 0;
596
597         down_write(&scrub->os_rwsem);
598         sf->sf_items_checked += scrub->os_new_checked;
599         scrub->os_new_checked = 0;
600         sf->sf_pos_last_checkpoint = scrub->os_pos_current;
601         sf->sf_time_last_checkpoint = cfs_time_current_sec();
602         sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
603                                             scrub->os_time_last_checkpoint);
604         rc = osd_scrub_file_store(scrub);
605         up_write(&scrub->os_rwsem);
606
607         return rc;
608 }
609
610 static void osd_scrub_post(struct osd_scrub *scrub, int result)
611 {
612         struct scrub_file *sf = &scrub->os_file;
613         ENTRY;
614
615         down_write(&scrub->os_rwsem);
616         spin_lock(&scrub->os_lock);
617         thread_set_flags(&scrub->os_thread, SVC_STOPPING);
618         spin_unlock(&scrub->os_lock);
619         if (scrub->os_new_checked > 0) {
620                 sf->sf_items_checked += scrub->os_new_checked;
621                 scrub->os_new_checked = 0;
622                 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
623         }
624         sf->sf_time_last_checkpoint = cfs_time_current_sec();
625         if (result > 0) {
626                 struct osd_device *dev =
627                         container_of0(scrub, struct osd_device, od_scrub);
628
629                 dev->od_igif_inoi = 1;
630                 sf->sf_status = SS_COMPLETED;
631                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
632                 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
633                                   SF_UPGRADE | SF_AUTO);
634                 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
635                 sf->sf_success_count++;
636         } else if (result == 0) {
637                 if (scrub->os_paused)
638                         sf->sf_status = SS_PAUSED;
639                 else
640                         sf->sf_status = SS_STOPPED;
641         } else {
642                 sf->sf_status = SS_FAILED;
643         }
644         sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
645                                             scrub->os_time_last_checkpoint);
646         result = osd_scrub_file_store(scrub);
647         if (result < 0)
648                 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
649                        LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
650                        result);
651         up_write(&scrub->os_rwsem);
652
653         EXIT;
654 }
655
656 /* iteration engine */
657
658 struct osd_iit_param {
659         struct super_block *sb;
660         struct buffer_head *bitmap;
661         ldiskfs_group_t bg;
662         __u32 gbase;
663         __u32 offset;
664 };
665
666 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
667                                    struct osd_device *dev,
668                                    struct osd_iit_param *param,
669                                    struct osd_idmap_cache **oic,
670                                    int noslot);
671
672 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
673                                    struct osd_device *dev,
674                                    struct osd_iit_param *param,
675                                    struct osd_idmap_cache *oic,
676                                    int *noslot, int rc);
677
678 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
679 {
680         param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
681                         LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
682         if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
683                 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
684                 return SCRUB_NEXT_BREAK;
685         } else {
686                 *pos = param->gbase + param->offset;
687                 return 0;
688         }
689 }
690
691 /**
692  * \retval SCRUB_NEXT_OSTOBJ_OLD: FID-on-OST
693  * \retval 0: FID-on-MDT
694  */
695 static int osd_scrub_check_local_fldb(struct osd_thread_info *info,
696                                       struct osd_device *dev,
697                                       struct lu_fid *fid)
698 {
699         /* XXX: The initial OI scrub will scan the top level /O to generate
700          *      a small local FLDB according to the <seq>. If the given FID
701          *      is in the local FLDB, then it is FID-on-OST; otherwise it's
702          *      quite possible for FID-on-MDT. */
703         return 0;
704 }
705
706 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
707                         struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
708                         struct super_block *sb, bool scrub)
709 {
710         struct lustre_mdt_attrs *lma            = &info->oti_mdt_attrs;
711         struct inode            *inode;
712         int                      rc             = 0;
713         bool                     has_lma        = false;
714         ENTRY;
715
716         osd_id_gen(lid, pos, OSD_OII_NOGEN);
717         inode = osd_iget(info, dev, lid);
718         if (IS_ERR(inode)) {
719                 rc = PTR_ERR(inode);
720                 /* The inode may be removed after bitmap searching, or the
721                  * file is new created without inode initialized yet. */
722                 if (rc == -ENOENT || rc == -ESTALE)
723                         RETURN(SCRUB_NEXT_CONTINUE);
724
725                 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
726                        LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
727                 RETURN(rc);
728         }
729
730         /* If the inode has no OI mapping, then it is special locally used,
731          * should be invisible to OI scrub or up layer LFSCK. */
732         if (ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI))
733                 GOTO(put, rc = SCRUB_NEXT_CONTINUE);
734
735         if (scrub &&
736             ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
737                 /* Only skip it for the first OI scrub accessing. */
738                 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
739                 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
740         }
741
742         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
743         if (rc == 0) {
744                 has_lma = true;
745                 if (lma->lma_compat & LMAC_NOT_IN_OI) {
746                         ldiskfs_set_inode_state(inode,
747                                                 LDISKFS_STATE_LUSTRE_NO_OI);
748                         GOTO(put, rc = SCRUB_NEXT_CONTINUE);
749                 }
750
751                 if (fid_is_llog(&lma->lma_self_fid))
752                         GOTO(put, rc = SCRUB_NEXT_CONTINUE);
753
754                 *fid = lma->lma_self_fid;
755                 if (fid_is_internal(&lma->lma_self_fid)) {
756                         if (!scrub)
757                                 rc = SCRUB_NEXT_CONTINUE;
758                         GOTO(put, rc);
759                 }
760
761                 if (!scrub)
762                         GOTO(put, rc);
763
764                 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
765                         GOTO(put, rc);
766
767                 if (lma->lma_compat & LMAC_FID_ON_OST || fid_is_last_id(fid))
768                         GOTO(put, rc = SCRUB_NEXT_OSTOBJ);
769
770                 if (fid_is_idif(fid))
771                         GOTO(put, rc = SCRUB_NEXT_OSTOBJ_OLD);
772
773                 if (lma->lma_incompat & LMAI_AGENT)
774                         GOTO(put, rc = SCRUB_NEXT_CONTINUE);
775
776                 /* Here, it may be MDT-object, or may be 2.4 OST-object.
777                  * Fall through. */
778         }
779
780         if (rc == -ENODATA || rc == 0) {
781                 rc = osd_get_idif(info, inode, &info->oti_obj_dentry, fid);
782                 if (rc == 0) {
783                         if (scrub)
784                                 /* It is old 2.x (x <= 3) or 1.8 OST-object. */
785                                 rc = SCRUB_NEXT_OSTOBJ_OLD;
786                         GOTO(put, rc);
787                 }
788
789                 if (rc > 0) {
790                         if (!has_lma)
791                                 /* It is FID-on-OST, but we do not know how
792                                  * to generate its FID, ignore it directly. */
793                                 rc = SCRUB_NEXT_CONTINUE;
794                         else
795                                 /* It is 2.4 OST-object. */
796                                 rc = SCRUB_NEXT_OSTOBJ_OLD;
797                         GOTO(put, rc);
798                 }
799
800                 if (rc != -ENODATA)
801                         GOTO(put, rc);
802
803                 if (!has_lma) {
804                         if (dev->od_handle_nolma) {
805                                 lu_igif_build(fid, inode->i_ino,
806                                               inode->i_generation);
807                                 if (scrub)
808                                         rc = SCRUB_NEXT_NOLMA;
809                                 else
810                                         rc = 0;
811                         } else {
812                                 /* It may be FID-on-OST, or may be FID for
813                                  * non-MDT0, anyway, we do not know how to
814                                  * generate its FID, ignore it directly. */
815                                 rc = SCRUB_NEXT_CONTINUE;
816                         }
817                         GOTO(put, rc);
818                 }
819
820                 /* For OI scrub case only: the object has LMA but has no ff
821                  * (or ff crashed). It may be MDT-object, may be OST-object
822                  * with crashed ff. The last check is local FLDB. */
823                 rc = osd_scrub_check_local_fldb(info, dev, fid);
824         }
825
826         GOTO(put, rc);
827
828 put:
829         iput(inode);
830         return rc;
831 }
832
833 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
834                           struct osd_iit_param *param,
835                           struct osd_idmap_cache **oic, int noslot)
836 {
837         struct osd_scrub     *scrub  = &dev->od_scrub;
838         struct ptlrpc_thread *thread = &scrub->os_thread;
839         struct lu_fid        *fid;
840         struct osd_inode_id  *lid;
841         int                   rc;
842
843         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
844                 struct l_wait_info lwi;
845
846                 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
847                 l_wait_event(thread->t_ctl_waitq,
848                              !cfs_list_empty(&scrub->os_inconsistent_items) ||
849                              !thread_is_running(thread),
850                              &lwi);
851         }
852
853         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
854                 spin_lock(&scrub->os_lock);
855                 thread_set_flags(thread, SVC_STOPPING);
856                 spin_unlock(&scrub->os_lock);
857                 return SCRUB_NEXT_CRASH;
858         }
859
860         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
861                 return SCRUB_NEXT_FATAL;
862
863         if (unlikely(!thread_is_running(thread)))
864                 return SCRUB_NEXT_EXIT;
865
866         if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
867                 struct osd_inconsistent_item *oii;
868
869                 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
870                                      struct osd_inconsistent_item, oii_list);
871                 *oic = &oii->oii_cache;
872                 scrub->os_in_prior = 1;
873                 return 0;
874         }
875
876         if (noslot != 0)
877                 return SCRUB_NEXT_WAIT;
878
879         rc = osd_iit_next(param, &scrub->os_pos_current);
880         if (rc != 0)
881                 return rc;
882
883         *oic = &scrub->os_oic;
884         fid = &(*oic)->oic_fid;
885         lid = &(*oic)->oic_lid;
886         rc = osd_iit_iget(info, dev, fid, lid,
887                           scrub->os_pos_current, param->sb, true);
888         return rc;
889 }
890
891 static int osd_preload_next(struct osd_thread_info *info,
892                             struct osd_device *dev, struct osd_iit_param *param,
893                             struct osd_idmap_cache **oic, int noslot)
894 {
895         struct osd_otable_cache *ooc    = &dev->od_otable_it->ooi_cache;
896         struct osd_scrub        *scrub;
897         struct ptlrpc_thread    *thread;
898         int                      rc;
899
900         rc = osd_iit_next(param, &ooc->ooc_pos_preload);
901         if (rc != 0)
902                 return rc;
903
904         scrub = &dev->od_scrub;
905         thread = &scrub->os_thread;
906         if (thread_is_running(thread) &&
907             ooc->ooc_pos_preload >= scrub->os_pos_current)
908                 return SCRUB_NEXT_EXIT;
909
910         rc = osd_iit_iget(info, dev,
911                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
912                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
913                           ooc->ooc_pos_preload, param->sb, false);
914         /* If succeed, it needs to move forward; otherwise up layer LFSCK may
915          * ignore the failure, so it still need to skip the inode next time. */
916         ooc->ooc_pos_preload = param->gbase + ++(param->offset);
917         return rc;
918 }
919
920 static inline int
921 osd_scrub_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
922 {
923         spin_lock(&scrub->os_lock);
924         if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
925             !cfs_list_empty(&scrub->os_inconsistent_items) ||
926             it->ooi_waiting || !thread_is_running(&scrub->os_thread))
927                 scrub->os_waiting = 0;
928         else
929                 scrub->os_waiting = 1;
930         spin_unlock(&scrub->os_lock);
931
932         return !scrub->os_waiting;
933 }
934
935 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
936                           struct osd_iit_param *param,
937                           struct osd_idmap_cache *oic, int *noslot, int rc)
938 {
939         struct l_wait_info       lwi    = { 0 };
940         struct osd_scrub        *scrub  = &dev->od_scrub;
941         struct scrub_file       *sf     = &scrub->os_file;
942         struct ptlrpc_thread    *thread = &scrub->os_thread;
943         struct osd_otable_it    *it     = dev->od_otable_it;
944         struct osd_otable_cache *ooc    = it ? &it->ooi_cache : NULL;
945
946         switch (rc) {
947         case SCRUB_NEXT_CONTINUE:
948                 goto next;
949         case SCRUB_NEXT_WAIT:
950                 goto wait;
951         case SCRUB_NEXT_NOSCRUB:
952                 down_write(&scrub->os_rwsem);
953                 scrub->os_new_checked++;
954                 sf->sf_items_noscrub++;
955                 up_write(&scrub->os_rwsem);
956                 goto next;
957         }
958
959         rc = osd_scrub_check_update(info, dev, oic, rc);
960         if (rc != 0)
961                 return rc;
962
963         rc = osd_scrub_checkpoint(scrub);
964         if (rc != 0) {
965                 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
966                        LDISKFS_SB(param->sb)->s_es->s_volume_name,
967                        scrub->os_pos_current, rc);
968                 /* Continue, as long as the scrub itself can go ahead. */
969         }
970
971         if (scrub->os_in_prior) {
972                 scrub->os_in_prior = 0;
973                 return 0;
974         }
975
976 next:
977         scrub->os_pos_current = param->gbase + ++(param->offset);
978
979 wait:
980         if (it != NULL && it->ooi_waiting &&
981             ooc->ooc_pos_preload < scrub->os_pos_current) {
982                 spin_lock(&scrub->os_lock);
983                 it->ooi_waiting = 0;
984                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
985                 spin_unlock(&scrub->os_lock);
986         }
987
988         if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
989                 return 0;
990
991         if (osd_scrub_has_window(scrub, ooc)) {
992                 *noslot = 0;
993                 return 0;
994         }
995
996         l_wait_event(thread->t_ctl_waitq,
997                      osd_scrub_wakeup(scrub, it),
998                      &lwi);
999
1000         if (osd_scrub_has_window(scrub, ooc))
1001                 *noslot = 0;
1002         else
1003                 *noslot = 1;
1004         return 0;
1005 }
1006
1007 static int osd_preload_exec(struct osd_thread_info *info,
1008                             struct osd_device *dev, struct osd_iit_param *param,
1009                             struct osd_idmap_cache *oic, int *noslot, int rc)
1010 {
1011         struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1012
1013         if (rc == 0) {
1014                 ooc->ooc_cached_items++;
1015                 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
1016                                         ~OSD_OTABLE_IT_CACHE_MASK;
1017         }
1018         return rc > 0 ? 0 : rc;
1019 }
1020
1021 #define SCRUB_IT_ALL    1
1022 #define SCRUB_IT_CRASH  2
1023
1024 static int osd_inode_iteration(struct osd_thread_info *info,
1025                                struct osd_device *dev, __u32 max, bool preload)
1026 {
1027         osd_iit_next_policy   next;
1028         osd_iit_exec_policy   exec;
1029         __u32                *pos;
1030         __u32                *count;
1031         struct osd_iit_param  param;
1032         __u32                 limit;
1033         int                   noslot = 0;
1034         int                   rc;
1035         ENTRY;
1036
1037         if (!preload) {
1038                 struct osd_scrub *scrub = &dev->od_scrub;
1039
1040                 next = osd_scrub_next;
1041                 exec = osd_scrub_exec;
1042                 pos = &scrub->os_pos_current;
1043                 count = &scrub->os_new_checked;
1044         } else {
1045                 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1046
1047                 next = osd_preload_next;
1048                 exec = osd_preload_exec;
1049                 pos = &ooc->ooc_pos_preload;
1050                 count = &ooc->ooc_cached_items;
1051         }
1052         param.sb = osd_sb(dev);
1053         limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
1054
1055         while (*pos <= limit && *count < max) {
1056                 struct osd_idmap_cache *oic = NULL;
1057
1058                 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
1059                 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
1060                 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
1061                 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
1062                 if (param.bitmap == NULL) {
1063                         CERROR("%.16s: fail to read bitmap for %u, "
1064                                "scrub will stop, urgent mode\n",
1065                                LDISKFS_SB(param.sb)->s_es->s_volume_name,
1066                                (__u32)param.bg);
1067                         RETURN(-EIO);
1068                 }
1069
1070                 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
1071                        *count < max) {
1072                         rc = next(info, dev, &param, &oic, noslot);
1073                         switch (rc) {
1074                         case SCRUB_NEXT_BREAK:
1075                                 goto next_group;
1076                         case SCRUB_NEXT_EXIT:
1077                                 brelse(param.bitmap);
1078                                 RETURN(0);
1079                         case SCRUB_NEXT_CRASH:
1080                                 brelse(param.bitmap);
1081                                 RETURN(SCRUB_IT_CRASH);
1082                         case SCRUB_NEXT_FATAL:
1083                                 brelse(param.bitmap);
1084                                 RETURN(-EINVAL);
1085                         }
1086
1087                         rc = exec(info, dev, &param, oic, &noslot, rc);
1088                         if (rc != 0) {
1089                                 brelse(param.bitmap);
1090                                 RETURN(rc);
1091                         }
1092                 }
1093
1094 next_group:
1095                 brelse(param.bitmap);
1096         }
1097
1098         if (*pos > limit)
1099                 RETURN(SCRUB_IT_ALL);
1100         RETURN(0);
1101 }
1102
1103 static int osd_otable_it_preload(const struct lu_env *env,
1104                                  struct osd_otable_it *it)
1105 {
1106         struct osd_device       *dev   = it->ooi_dev;
1107         struct osd_scrub        *scrub = &dev->od_scrub;
1108         struct osd_otable_cache *ooc   = &it->ooi_cache;
1109         int                      rc;
1110         ENTRY;
1111
1112         rc = osd_inode_iteration(osd_oti_get(env), dev,
1113                                  OSD_OTABLE_IT_CACHE_SIZE, true);
1114         if (rc == SCRUB_IT_ALL)
1115                 it->ooi_all_cached = 1;
1116
1117         CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1118                le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1119                ooc->ooc_pos_preload, rc);
1120
1121         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1122                 scrub->os_waiting = 0;
1123                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1124         }
1125
1126         RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1127 }
1128
1129 static int osd_scrub_main(void *args)
1130 {
1131         struct lu_env         env;
1132         struct osd_device    *dev    = (struct osd_device *)args;
1133         struct osd_scrub     *scrub  = &dev->od_scrub;
1134         struct ptlrpc_thread *thread = &scrub->os_thread;
1135         struct super_block   *sb     = osd_sb(dev);
1136         int                   rc;
1137         ENTRY;
1138
1139         rc = lu_env_init(&env, LCT_LOCAL);
1140         if (rc != 0) {
1141                 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
1142                        LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1143                 GOTO(noenv, rc);
1144         }
1145
1146         rc = osd_scrub_prep(dev);
1147         if (rc != 0) {
1148                 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
1149                        LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1150                 GOTO(out, rc);
1151         }
1152
1153         if (!scrub->os_full_speed) {
1154                 struct l_wait_info lwi = { 0 };
1155                 struct osd_otable_it *it = dev->od_otable_it;
1156                 struct osd_otable_cache *ooc = &it->ooi_cache;
1157
1158                 l_wait_event(thread->t_ctl_waitq,
1159                              it->ooi_user_ready || !thread_is_running(thread),
1160                              &lwi);
1161                 if (unlikely(!thread_is_running(thread)))
1162                         GOTO(post, rc = 0);
1163
1164                 scrub->os_pos_current = ooc->ooc_pos_preload;
1165         }
1166
1167         CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
1168                scrub->os_start_flags, scrub->os_pos_current);
1169
1170         rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1171         if (unlikely(rc == SCRUB_IT_CRASH))
1172                 GOTO(out, rc = -EINVAL);
1173         GOTO(post, rc);
1174
1175 post:
1176         osd_scrub_post(scrub, rc);
1177         CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
1178                rc, scrub->os_pos_current);
1179
1180 out:
1181         while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
1182                 struct osd_inconsistent_item *oii;
1183
1184                 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
1185                                      struct osd_inconsistent_item, oii_list);
1186                 cfs_list_del_init(&oii->oii_list);
1187                 OBD_FREE_PTR(oii);
1188         }
1189         lu_env_fini(&env);
1190
1191 noenv:
1192         spin_lock(&scrub->os_lock);
1193         thread_set_flags(thread, SVC_STOPPED);
1194         cfs_waitq_broadcast(&thread->t_ctl_waitq);
1195         spin_unlock(&scrub->os_lock);
1196         return rc;
1197 }
1198
1199 /* initial OI scrub */
1200
1201 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1202                          struct dentry *, filldir_t filldir);
1203
1204 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1205                                loff_t offset, __u64 ino, unsigned d_type);
1206
1207 static int
1208 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1209                      struct dentry *dentry, filldir_t filldir);
1210 static int
1211 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1212                   struct dentry *dentry, filldir_t filldir);
1213
1214 static int
1215 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1216                      struct dentry *dentry, filldir_t filldir);
1217
1218 enum osd_lf_flags {
1219         OLF_SCAN_SUBITEMS       = 0x0001,
1220         OLF_HIDE_FID            = 0x0002,
1221         OLF_SHOW_NAME           = 0x0004,
1222 };
1223
1224 struct osd_lf_map {
1225         char            *olm_name;
1226         struct lu_fid    olm_fid;
1227         __u16            olm_flags;
1228         scandir_t        olm_scandir;
1229         filldir_t        olm_filldir;
1230 };
1231
1232 /* Add the new introduced local files in the list in the future. */
1233 static const struct osd_lf_map osd_lf_maps[] = {
1234         /* CATALOGS */
1235         { CATLIST, { FID_SEQ_LOCAL_FILE, LLOG_CATALOGS_OID, 0 }, OLF_SHOW_NAME,
1236                 NULL, NULL },
1237
1238         /* CONFIGS */
1239         { MOUNT_CONFIGS_DIR, { FID_SEQ_LOCAL_FILE, MGS_CONFIGS_OID, 0 },
1240                 OLF_SCAN_SUBITEMS, osd_ios_general_scan,
1241                 osd_ios_varfid_fill },
1242
1243         /* NIDTBL_VERSIONS */
1244         { MGS_NIDTBL_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1245                 osd_ios_general_scan, osd_ios_varfid_fill },
1246
1247         /* PENDING */
1248         { "PENDING", { 0, 0, 0 }, 0, NULL, NULL },
1249
1250         /* ROOT */
1251         { "ROOT", { FID_SEQ_ROOT, 1, 0 },
1252                 OLF_SCAN_SUBITEMS | OLF_HIDE_FID, osd_ios_ROOT_scan, NULL },
1253
1254         /* changelog_catalog */
1255         { CHANGELOG_CATALOG, { 0, 0, 0 }, 0, NULL, NULL },
1256
1257         /* changelog_users */
1258         { CHANGELOG_USERS, { 0, 0, 0 }, 0, NULL, NULL },
1259
1260         /* fld */
1261         { "fld", { FID_SEQ_LOCAL_FILE, FLD_INDEX_OID, 0 }, OLF_SHOW_NAME,
1262                 NULL, NULL },
1263
1264         /* last_rcvd */
1265         { LAST_RCVD, { FID_SEQ_LOCAL_FILE, LAST_RECV_OID, 0 }, OLF_SHOW_NAME,
1266                 NULL, NULL },
1267
1268         /* lfsck_bookmark */
1269         { "lfsck_bookmark", { 0, 0, 0 }, 0, NULL, NULL },
1270
1271         /* lov_objid */
1272         { LOV_OBJID, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OID, 0 }, OLF_SHOW_NAME,
1273                 NULL, NULL },
1274
1275         /* lov_objseq */
1276         { LOV_OBJSEQ, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OSEQ, 0 },
1277                 OLF_SHOW_NAME, NULL, NULL },
1278
1279         /* quota_master */
1280         { QMT_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1281                 osd_ios_general_scan, osd_ios_varfid_fill },
1282
1283         /* quota_slave */
1284         { QSD_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1285                 osd_ios_general_scan, osd_ios_varfid_fill },
1286
1287         /* seq_ctl */
1288         { "seq_ctl", { FID_SEQ_LOCAL_FILE, FID_SEQ_CTL_OID, 0 },
1289                 OLF_SHOW_NAME, NULL, NULL },
1290
1291         /* seq_srv */
1292         { "seq_srv", { FID_SEQ_LOCAL_FILE, FID_SEQ_SRV_OID, 0 },
1293                 OLF_SHOW_NAME, NULL, NULL },
1294
1295         /* health_check */
1296         { HEALTH_CHECK, { FID_SEQ_LOCAL_FILE, OFD_HEALTH_CHECK_OID, 0 },
1297                 OLF_SHOW_NAME, NULL, NULL },
1298
1299         /* lfsck_namespace */
1300         { "lfsck_namespace", { 0, 0, 0 }, 0, NULL, NULL },
1301
1302         /* OBJECTS, upgrade from old device */
1303         { OBJECTS, { 0, 0, 0 }, OLF_SCAN_SUBITEMS, osd_ios_OBJECTS_scan, NULL },
1304
1305         /* lquota_v2.user, upgrade from old device */
1306         { "lquota_v2.user", { 0, 0, 0 }, 0, NULL, NULL },
1307
1308         /* lquota_v2.group, upgrade from old device */
1309         { "lquota_v2.group", { 0, 0, 0 }, 0, NULL, NULL },
1310
1311         /* LAST_GROUP, upgrade from old device */
1312         { "LAST_GROUP", { FID_SEQ_LOCAL_FILE, OFD_LAST_GROUP_OID, 0 },
1313                 OLF_SHOW_NAME, NULL, NULL },
1314
1315         { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1316 };
1317
1318 struct osd_ios_item {
1319         cfs_list_t       oii_list;
1320         struct dentry   *oii_dentry;
1321         scandir_t        oii_scandir;
1322         filldir_t        oii_filldir;
1323 };
1324
1325 struct osd_ios_filldir_buf {
1326         struct osd_thread_info  *oifb_info;
1327         struct osd_device       *oifb_dev;
1328         struct dentry           *oifb_dentry;
1329 };
1330
1331 static inline struct dentry *
1332 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1333 {
1334         struct dentry *dentry;
1335
1336         dentry = ll_lookup_one_len(name, parent, namelen);
1337         if (!IS_ERR(dentry) && dentry->d_inode == NULL) {
1338                 dput(dentry);
1339                 return ERR_PTR(-ENOENT);
1340         }
1341
1342         return dentry;
1343 }
1344
1345 static inline void
1346 osd_ios_llogname2fid(struct lu_fid *fid, const char *name, int namelen)
1347 {
1348         obd_id id = 0;
1349         int    i  = 0;
1350
1351         fid->f_seq = FID_SEQ_LLOG;
1352         while (i < namelen)
1353                 id = id * 10 + name[i++] - '0';
1354
1355         fid->f_oid = id & 0x00000000ffffffffULL;
1356         fid->f_ver = id >> 32;
1357 }
1358
1359 static inline void
1360 osd_ios_Oname2fid(struct lu_fid *fid, const char *name, int namelen)
1361 {
1362         __u64 seq = 0;
1363         int   i   = 0;
1364
1365         while (i < namelen)
1366                 seq = seq * 10 + name[i++] - '0';
1367
1368         lu_last_id_fid(fid, seq);
1369 }
1370
1371 static int
1372 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1373                  scandir_t scandir, filldir_t filldir)
1374 {
1375         struct osd_ios_item *item;
1376
1377         OBD_ALLOC_PTR(item);
1378         if (item == NULL)
1379                 return -ENOMEM;
1380
1381         CFS_INIT_LIST_HEAD(&item->oii_list);
1382         item->oii_dentry = dget(dentry);
1383         item->oii_scandir = scandir;
1384         item->oii_filldir = filldir;
1385         cfs_list_add_tail(&item->oii_list, &dev->od_ios_list);
1386         return 0;
1387 }
1388
1389 /**
1390  * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1391  *
1392  * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1393  * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1394  * reference the inode, or fixed if it is missing or references another inode.
1395  */
1396 static int
1397 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1398                  struct inode *inode, const struct lu_fid *fid, int flags)
1399 {
1400         struct lustre_mdt_attrs *lma    = &info->oti_mdt_attrs;
1401         struct osd_inode_id     *id     = &info->oti_id;
1402         struct osd_inode_id     *id2    = &info->oti_id2;
1403         struct osd_scrub        *scrub  = &dev->od_scrub;
1404         struct scrub_file       *sf     = &scrub->os_file;
1405         struct lu_fid            tfid;
1406         int                      rc;
1407         ENTRY;
1408
1409         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
1410         if (rc != 0 && rc != -ENODATA)
1411                 RETURN(rc);
1412
1413         osd_id_gen(id, inode->i_ino, inode->i_generation);
1414         if (rc == -ENODATA) {
1415                 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID)
1416                         lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1417                 else
1418                         tfid = *fid;
1419                 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1420                 if (rc != 0)
1421                         RETURN(rc);
1422         } else {
1423                 if (lma->lma_compat & LMAC_NOT_IN_OI)
1424                         RETURN(0);
1425
1426                 tfid = lma->lma_self_fid;
1427         }
1428
1429         rc = osd_oi_lookup(info, dev, &tfid, id2, 0);
1430         if (rc != 0) {
1431                 if (rc != -ENOENT)
1432                         RETURN(rc);
1433
1434                 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1435                                                DTO_INDEX_INSERT, 0);
1436                 if (rc > 0)
1437                         rc = 0;
1438
1439                 RETURN(rc);
1440         }
1441
1442         if (osd_id_eq_strict(id, id2))
1443                 RETURN(0);
1444
1445         if (!(sf->sf_flags & SF_INCONSISTENT)) {
1446                 osd_scrub_file_reset(scrub,
1447                                      LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1448                                      SF_INCONSISTENT);
1449                 rc = osd_scrub_file_store(scrub);
1450                 if (rc != 0)
1451                         RETURN(rc);
1452         }
1453
1454         rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1455                                        DTO_INDEX_UPDATE, 0);
1456         if (rc > 0)
1457                 rc = 0;
1458
1459         RETURN(rc);
1460 }
1461
1462 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1463                                loff_t offset, __u64 ino, unsigned d_type)
1464 {
1465         struct osd_ios_filldir_buf *fill_buf = buf;
1466         struct osd_device          *dev      = fill_buf->oifb_dev;
1467         struct dentry              *child;
1468         int                         rc;
1469         ENTRY;
1470
1471         /* skip any '.' started names */
1472         if (name[0] == '.')
1473                 RETURN(0);
1474
1475         child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1476         if (IS_ERR(child))
1477                 RETURN(PTR_ERR(child));
1478
1479         rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1480                               NULL, 0);
1481         if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1482                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1483                                       osd_ios_varfid_fill);
1484         dput(child);
1485
1486         RETURN(rc);
1487 }
1488
1489 static int osd_ios_root_fill(void *buf, const char *name, int namelen,
1490                              loff_t offset, __u64 ino, unsigned d_type)
1491 {
1492         struct osd_ios_filldir_buf *fill_buf = buf;
1493         struct osd_device          *dev      = fill_buf->oifb_dev;
1494         const struct osd_lf_map    *map;
1495         struct dentry              *child;
1496         int                         rc       = 0;
1497         ENTRY;
1498
1499         /* skip any '.' started names */
1500         if (name[0] == '.')
1501                 RETURN(0);
1502
1503         for (map = osd_lf_maps; map->olm_name != NULL; map++) {
1504                 if (strlen(map->olm_name) != namelen)
1505                         continue;
1506
1507                 if (strncmp(map->olm_name, name, namelen) == 0)
1508                         break;
1509         }
1510
1511         if (map->olm_name == NULL)
1512                 RETURN(0);
1513
1514         child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1515         if (IS_ERR(child))
1516                 RETURN(PTR_ERR(child));
1517
1518         rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1519                               &map->olm_fid, map->olm_flags);
1520         if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
1521                 rc = osd_ios_new_item(dev, child, map->olm_scandir,
1522                                       map->olm_filldir);
1523         dput(child);
1524
1525         RETURN(rc);
1526 }
1527
1528 static int
1529 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1530                      struct dentry *dentry, filldir_t filldir)
1531 {
1532         struct osd_ios_filldir_buf    buf   = { info, dev, dentry };
1533         struct file                  *filp  = &info->oti_it_ea.oie_file;
1534         struct inode                 *inode = dentry->d_inode;
1535         const struct file_operations *fops  = inode->i_fop;
1536         int                           rc;
1537         ENTRY;
1538
1539         LASSERT(filldir != NULL);
1540
1541         filp->f_pos = 0;
1542         filp->f_dentry = dentry;
1543         filp->f_mode = FMODE_64BITHASH;
1544         filp->f_mapping = inode->i_mapping;
1545         filp->f_op = fops;
1546         filp->private_data = NULL;
1547
1548         rc = fops->readdir(filp, &buf, filldir);
1549         fops->release(inode, filp);
1550
1551         RETURN(rc);
1552 }
1553
1554 static int
1555 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1556                   struct dentry *dentry, filldir_t filldir)
1557 {
1558         struct osd_scrub  *scrub  = &dev->od_scrub;
1559         struct scrub_file *sf     = &scrub->os_file;
1560         struct dentry     *child;
1561         int                rc;
1562         ENTRY;
1563
1564         /* It is existing MDT0 device. We only allow the case of object without
1565          * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
1566          * can generate IGIF mode FID for the object and related OI mapping. If
1567          * it is on other MDTs, then becuase file-level backup/restore, related
1568          * OI mapping may be invalid already, we do not know which is the right
1569          * FID for the object. We only allow IGIF objects to reside on the MDT0.
1570          *
1571          * XXX: For the case of object on non-MDT0 device with neither LMA nor
1572          *      "fid" xattr, then something crashed. We cannot re-generate the
1573          *      FID directly, instead, the OI scrub will scan the OI structure
1574          *      and try to re-generate the LMA from the OI mapping. But if the
1575          *      OI mapping crashed or lost also, then we have to give up under
1576          *      double failure cases. */
1577         dev->od_handle_nolma = 1;
1578         child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
1579                                        strlen(dot_lustre_name));
1580         if (IS_ERR(child)) {
1581                 rc = PTR_ERR(child);
1582                 if (rc == -ENOENT) {
1583                         /* It is 1.8 MDT device. */
1584                         if (!(sf->sf_flags & SF_UPGRADE)) {
1585                                 osd_scrub_file_reset(scrub,
1586                                         LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1587                                         SF_UPGRADE);
1588                                 rc = osd_scrub_file_store(scrub);
1589                         } else {
1590                                 rc = 0;
1591                         }
1592                 }
1593         } else {
1594                 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
1595                  * so the client will get IGIF for the ".lustre" object when
1596                  * the MDT restart.
1597                  *
1598                  * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
1599                  * it does not know whether there are some old clients cached
1600                  * the ".lustre" IGIF during the upgrading. Two choices:
1601                  *
1602                  * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
1603                  *    It will allow the old connected clients to access the
1604                  *    ".lustre" with cached IGIF. But it will cause others
1605                  *    on the MDT failed to check "fid_is_dot_lustre()".
1606                  *
1607                  * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
1608                  *    for ".lustre" in spite of whether there are some clients
1609                  *    cached the ".lustre" IGIF or not. It enables the check
1610                  *    "fid_is_dot_lustre()" on the MDT, although it will cause
1611                  *    that the old connected clients cannot access the ".lustre"
1612                  *    with the cached IGIF.
1613                  *
1614                  * Usually, it is rare case for the old connected clients
1615                  * to access the ".lustre" with cached IGIF. So we prefer
1616                  * to the solution 2). */
1617                 rc = osd_ios_scan_one(info, dev, child->d_inode,
1618                                       &LU_DOT_LUSTRE_FID, 0);
1619                 dput(child);
1620         }
1621
1622         RETURN(rc);
1623 }
1624
1625 static int
1626 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1627                      struct dentry *dentry, filldir_t filldir)
1628 {
1629         struct dentry *child;
1630         int            rc;
1631         ENTRY;
1632
1633         child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
1634         if (!IS_ERR(child)) {
1635                 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1636                 dput(child);
1637         } else {
1638                 rc = PTR_ERR(child);
1639         }
1640
1641         if (rc != 0 && rc != -ENOENT)
1642                 RETURN(rc);
1643
1644         child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
1645         if (!IS_ERR(child)) {
1646                 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1647                 dput(child);
1648         } else {
1649                 rc = PTR_ERR(child);
1650         }
1651
1652         if (rc == -ENOENT)
1653                 rc = 0;
1654
1655         RETURN(rc);
1656 }
1657
1658 static int osd_initial_OI_scrub(struct osd_thread_info *info,
1659                                 struct osd_device *dev)
1660 {
1661         struct osd_ios_item     *item    = NULL;
1662         scandir_t                scandir = osd_ios_general_scan;
1663         filldir_t                filldir = osd_ios_root_fill;
1664         struct dentry           *dentry  = osd_sb(dev)->s_root;
1665         const struct osd_lf_map *map     = osd_lf_maps;
1666         int                      rc;
1667         ENTRY;
1668
1669         while (1) {
1670                 rc = scandir(info, dev, dentry, filldir);
1671                 if (item != NULL) {
1672                         dput(item->oii_dentry);
1673                         OBD_FREE_PTR(item);
1674                 }
1675
1676                 if (rc != 0)
1677                         break;
1678
1679                 if (cfs_list_empty(&dev->od_ios_list))
1680                         break;
1681
1682                 item = cfs_list_entry(dev->od_ios_list.next,
1683                                       struct osd_ios_item, oii_list);
1684                 cfs_list_del_init(&item->oii_list);
1685
1686                 LASSERT(item->oii_scandir != NULL);
1687                 scandir = item->oii_scandir;
1688                 filldir = item->oii_filldir;
1689                 dentry = item->oii_dentry;
1690         }
1691
1692         while (!cfs_list_empty(&dev->od_ios_list)) {
1693                 item = cfs_list_entry(dev->od_ios_list.next,
1694                                       struct osd_ios_item, oii_list);
1695                 cfs_list_del_init(&item->oii_list);
1696                 dput(item->oii_dentry);
1697                 OBD_FREE_PTR(item);
1698         }
1699
1700         if (rc != 0)
1701                 RETURN(rc);
1702
1703         /* There maybe the case that the object has been removed, but its OI
1704          * mapping is still in the OI file, such as the "CATALOGS" after MDT
1705          * file-level backup/restore. So here cleanup the stale OI mappings. */
1706         while (map->olm_name != NULL) {
1707                 struct dentry *child;
1708
1709                 if (fid_is_zero(&map->olm_fid)) {
1710                         map++;
1711                         continue;
1712                 }
1713
1714                 child = osd_ios_lookup_one_len(map->olm_name,
1715                                                osd_sb(dev)->s_root,
1716                                                strlen(map->olm_name));
1717                 if (!IS_ERR(child))
1718                         dput(child);
1719                 else if (PTR_ERR(child) == -ENOENT)
1720                         osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
1721                                                   NULL, DTO_INDEX_DELETE, 0);
1722                 map++;
1723         }
1724
1725         RETURN(0);
1726 }
1727
1728 char *osd_lf_fid2name(const struct lu_fid *fid)
1729 {
1730         const struct osd_lf_map *map = osd_lf_maps;
1731
1732         while (map->olm_name != NULL) {
1733                 if (!lu_fid_eq(fid, &map->olm_fid)) {
1734                         map++;
1735                         continue;
1736                 }
1737
1738                 if (map->olm_flags & OLF_SHOW_NAME)
1739                         return map->olm_name;
1740                 else
1741                         return "";
1742         }
1743
1744         return NULL;
1745 }
1746
1747 /* OI scrub start/stop */
1748
1749 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
1750 {
1751         struct osd_scrub     *scrub  = &dev->od_scrub;
1752         struct ptlrpc_thread *thread = &scrub->os_thread;
1753         struct l_wait_info    lwi    = { 0 };
1754         int                   rc;
1755         ENTRY;
1756
1757 again:
1758         /* os_lock: sync status between stop and scrub thread */
1759         spin_lock(&scrub->os_lock);
1760         if (thread_is_running(thread)) {
1761                 spin_unlock(&scrub->os_lock);
1762                 RETURN(-EALREADY);
1763         } else if (unlikely(thread_is_stopping(thread))) {
1764                 spin_unlock(&scrub->os_lock);
1765                 l_wait_event(thread->t_ctl_waitq,
1766                              thread_is_stopped(thread),
1767                              &lwi);
1768                 goto again;
1769         }
1770         spin_unlock(&scrub->os_lock);
1771
1772         if (scrub->os_file.sf_status == SS_COMPLETED)
1773                 flags |= SS_RESET;
1774
1775         scrub->os_start_flags = flags;
1776         thread_set_flags(thread, 0);
1777         rc = PTR_ERR(kthread_run(osd_scrub_main, dev, "OI_scrub"));
1778         if (IS_ERR_VALUE(rc)) {
1779                 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
1780                        LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
1781                 RETURN(rc);
1782         }
1783
1784         l_wait_event(thread->t_ctl_waitq,
1785                      thread_is_running(thread) || thread_is_stopped(thread),
1786                      &lwi);
1787
1788         RETURN(0);
1789 }
1790
1791 int osd_scrub_start(struct osd_device *dev)
1792 {
1793         int rc;
1794         ENTRY;
1795
1796         /* od_otable_mutex: prevent curcurrent start/stop */
1797         mutex_lock(&dev->od_otable_mutex);
1798         rc = do_osd_scrub_start(dev, SS_AUTO);
1799         mutex_unlock(&dev->od_otable_mutex);
1800
1801         RETURN(rc == -EALREADY ? 0 : rc);
1802 }
1803
1804 static void do_osd_scrub_stop(struct osd_scrub *scrub)
1805 {
1806         struct ptlrpc_thread *thread = &scrub->os_thread;
1807         struct l_wait_info    lwi    = { 0 };
1808
1809         /* os_lock: sync status between stop and scrub thread */
1810         spin_lock(&scrub->os_lock);
1811         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
1812                 thread_set_flags(thread, SVC_STOPPING);
1813                 spin_unlock(&scrub->os_lock);
1814                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1815                 l_wait_event(thread->t_ctl_waitq,
1816                              thread_is_stopped(thread),
1817                              &lwi);
1818                 /* Do not skip the last lock/unlock, which can guarantee that
1819                  * the caller cannot return until the OI scrub thread exit. */
1820                 spin_lock(&scrub->os_lock);
1821         }
1822         spin_unlock(&scrub->os_lock);
1823 }
1824
1825 static void osd_scrub_stop(struct osd_device *dev)
1826 {
1827         /* od_otable_mutex: prevent curcurrent start/stop */
1828         mutex_lock(&dev->od_otable_mutex);
1829         dev->od_scrub.os_paused = 1;
1830         do_osd_scrub_stop(&dev->od_scrub);
1831         mutex_unlock(&dev->od_otable_mutex);
1832 }
1833
1834 /* OI scrub setup/cleanup */
1835
1836 static const char osd_scrub_name[] = "OI_scrub";
1837
1838 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
1839 {
1840         struct osd_thread_info     *info   = osd_oti_get(env);
1841         struct osd_scrub           *scrub  = &dev->od_scrub;
1842         struct lvfs_run_ctxt       *ctxt   = &scrub->os_ctxt;
1843         struct scrub_file          *sf     = &scrub->os_file;
1844         struct super_block         *sb     = osd_sb(dev);
1845         struct ldiskfs_super_block *es     = LDISKFS_SB(sb)->s_es;
1846         struct lvfs_run_ctxt        saved;
1847         struct file                *filp;
1848         struct inode               *inode;
1849         struct lu_fid              *fid    = &info->oti_fid;
1850         int                         dirty  = 0;
1851         int                         rc     = 0;
1852         ENTRY;
1853
1854         memset(scrub, 0, sizeof(*scrub));
1855         OBD_SET_CTXT_MAGIC(ctxt);
1856         ctxt->pwdmnt = dev->od_mnt;
1857         ctxt->pwd = dev->od_mnt->mnt_root;
1858         ctxt->fs = get_ds();
1859
1860         cfs_waitq_init(&scrub->os_thread.t_ctl_waitq);
1861         init_rwsem(&scrub->os_rwsem);
1862         spin_lock_init(&scrub->os_lock);
1863         CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1864
1865         push_ctxt(&saved, ctxt, NULL);
1866         filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
1867         if (IS_ERR(filp)) {
1868                 pop_ctxt(&saved, ctxt, NULL);
1869                 RETURN(PTR_ERR(filp));
1870         }
1871
1872         inode = filp->f_dentry->d_inode;
1873         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
1874         /* 'What the @fid is' is not imporatant, because the object
1875          * has no OI mapping, and only is visible inside the OSD.*/
1876         lu_igif_build(fid, inode->i_ino, inode->i_generation);
1877         rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
1878         if (rc != 0) {
1879                 filp_close(filp, 0);
1880                 pop_ctxt(&saved, ctxt, NULL);
1881                 RETURN(rc);
1882         }
1883
1884         scrub->os_inode = igrab(inode);
1885         filp_close(filp, 0);
1886         pop_ctxt(&saved, ctxt, NULL);
1887
1888         rc = osd_scrub_file_load(scrub);
1889         if (rc == -ENOENT) {
1890                 osd_scrub_file_init(scrub, es->s_uuid);
1891                 dirty = 1;
1892         } else if (rc != 0) {
1893                 RETURN(rc);
1894         } else {
1895                 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
1896                         osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
1897                         dirty = 1;
1898                 } else if (sf->sf_status == SS_SCANNING) {
1899                         sf->sf_status = SS_CRASHED;
1900                         dirty = 1;
1901                 }
1902         }
1903
1904         if (sf->sf_pos_last_checkpoint != 0)
1905                 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1906         else
1907                 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
1908
1909         if (dirty != 0) {
1910                 rc = osd_scrub_file_store(scrub);
1911                 if (rc != 0)
1912                         RETURN(rc);
1913         }
1914
1915         /* Initialize OI files. */
1916         rc = osd_oi_init(info, dev);
1917         if (rc < 0)
1918                 RETURN(rc);
1919
1920         rc = osd_initial_OI_scrub(info, dev);
1921         if (rc == 0) {
1922                 if ((sf->sf_flags & SF_UPGRADE) &&
1923                    !(sf->sf_flags & SF_INCONSISTENT))
1924                         /* The 'od_igif_inoi' will be set after the
1925                          * upgrading completed, needs NOT remount. */
1926                         dev->od_igif_inoi = 0;
1927                 else
1928                         /* The 'od_igif_inoi' will be set under the
1929                          * following cases:
1930                          * 1) new created system, or
1931                          * 2) restored from file-level backup, or
1932                          * 3) the upgrading completed.
1933                          *
1934                          * The 'od_igif_inoi' may be cleared by OI scrub
1935                          * later if found that the system is upgrading. */
1936                         dev->od_igif_inoi = 1;
1937
1938                 if (!dev->od_noscrub &&
1939                     ((sf->sf_status == SS_PAUSED) ||
1940                      (sf->sf_status == SS_CRASHED &&
1941                       sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1942                                       SF_UPGRADE | SF_AUTO)) ||
1943                      (sf->sf_status == SS_INIT &&
1944                       sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1945                                       SF_UPGRADE))))
1946                         rc = osd_scrub_start(dev);
1947         }
1948
1949         /* it is possible that dcache entries may keep objects after they are
1950          * deleted by OSD. While it looks safe this can cause object data to
1951          * stay until umount causing failures in tests calculating free space,
1952          * e.g. replay-ost-single. Since those dcache entries are not used
1953          * anymore let's just free them after use here */
1954         shrink_dcache_sb(sb);
1955
1956         RETURN(rc);
1957 }
1958
1959 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1960 {
1961         struct osd_scrub *scrub = &dev->od_scrub;
1962
1963         LASSERT(dev->od_otable_it == NULL);
1964
1965         if (scrub->os_inode != NULL) {
1966                 osd_scrub_stop(dev);
1967                 iput(scrub->os_inode);
1968                 scrub->os_inode = NULL;
1969         }
1970         if (dev->od_oi_table != NULL)
1971                 osd_oi_fini(osd_oti_get(env), dev);
1972 }
1973
1974 /* object table based iteration APIs */
1975
1976 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1977                                        struct dt_object *dt, __u32 attr,
1978                                        struct lustre_capa *capa)
1979 {
1980         enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
1981         enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
1982         struct osd_device      *dev   = osd_dev(dt->do_lu.lo_dev);
1983         struct osd_scrub       *scrub = &dev->od_scrub;
1984         struct osd_otable_it   *it;
1985         __u32                   start = 0;
1986         int                     rc;
1987         ENTRY;
1988
1989         /* od_otable_mutex: prevent curcurrent init/fini */
1990         mutex_lock(&dev->od_otable_mutex);
1991         if (dev->od_otable_it != NULL)
1992                 GOTO(out, it = ERR_PTR(-EALREADY));
1993
1994         OBD_ALLOC_PTR(it);
1995         if (it == NULL)
1996                 GOTO(out, it = ERR_PTR(-ENOMEM));
1997
1998         dev->od_otable_it = it;
1999         it->ooi_dev = dev;
2000         it->ooi_cache.ooc_consumer_idx = -1;
2001         if (flags & DOIF_OUTUSED)
2002                 it->ooi_used_outside = 1;
2003
2004         if (flags & DOIF_RESET)
2005                 start |= SS_RESET;
2006
2007         if (valid & DOIV_ERROR_HANDLE) {
2008                 if (flags & DOIF_FAILOUT)
2009                         start |= SS_SET_FAILOUT;
2010                 else
2011                         start |= SS_CLEAR_FAILOUT;
2012         }
2013
2014         rc = do_osd_scrub_start(dev, start);
2015         if (rc < 0 && rc != -EALREADY) {
2016                 dev->od_otable_it = NULL;
2017                 OBD_FREE_PTR(it);
2018                 GOTO(out, it = ERR_PTR(rc));
2019         }
2020
2021         it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2022
2023         GOTO(out, it);
2024
2025 out:
2026         mutex_unlock(&dev->od_otable_mutex);
2027         return (struct dt_it *)it;
2028 }
2029
2030 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2031 {
2032         struct osd_otable_it *it  = (struct osd_otable_it *)di;
2033         struct osd_device    *dev = it->ooi_dev;
2034
2035         /* od_otable_mutex: prevent curcurrent init/fini */
2036         mutex_lock(&dev->od_otable_mutex);
2037         do_osd_scrub_stop(&dev->od_scrub);
2038         LASSERT(dev->od_otable_it == it);
2039
2040         dev->od_otable_it = NULL;
2041         mutex_unlock(&dev->od_otable_mutex);
2042         OBD_FREE_PTR(it);
2043 }
2044
2045 static int osd_otable_it_get(const struct lu_env *env,
2046                              struct dt_it *di, const struct dt_key *key)
2047 {
2048         return 0;
2049 }
2050
2051 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2052 {
2053 }
2054
2055 static inline int
2056 osd_otable_it_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
2057 {
2058         spin_lock(&scrub->os_lock);
2059         if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2060             scrub->os_waiting ||
2061             !thread_is_running(&scrub->os_thread))
2062                 it->ooi_waiting = 0;
2063         else
2064                 it->ooi_waiting = 1;
2065         spin_unlock(&scrub->os_lock);
2066
2067         return !it->ooi_waiting;
2068 }
2069
2070 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2071 {
2072         struct osd_otable_it    *it     = (struct osd_otable_it *)di;
2073         struct osd_device       *dev    = it->ooi_dev;
2074         struct osd_scrub        *scrub  = &dev->od_scrub;
2075         struct osd_otable_cache *ooc    = &it->ooi_cache;
2076         struct ptlrpc_thread    *thread = &scrub->os_thread;
2077         struct l_wait_info       lwi    = { 0 };
2078         int                      rc;
2079         ENTRY;
2080
2081         LASSERT(it->ooi_user_ready);
2082
2083 again:
2084         if (!thread_is_running(thread) && !it->ooi_used_outside)
2085                 RETURN(1);
2086
2087         if (ooc->ooc_cached_items > 0) {
2088                 ooc->ooc_cached_items--;
2089                 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2090                                         ~OSD_OTABLE_IT_CACHE_MASK;
2091                 RETURN(0);
2092         }
2093
2094         if (it->ooi_all_cached) {
2095                 l_wait_event(thread->t_ctl_waitq,
2096                              !thread_is_running(thread),
2097                              &lwi);
2098                 RETURN(1);
2099         }
2100
2101         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2102                 spin_lock(&scrub->os_lock);
2103                 scrub->os_waiting = 0;
2104                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
2105                 spin_unlock(&scrub->os_lock);
2106         }
2107
2108         if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2109                 l_wait_event(thread->t_ctl_waitq,
2110                              osd_otable_it_wakeup(scrub, it),
2111                              &lwi);
2112
2113         if (!thread_is_running(thread) && !it->ooi_used_outside)
2114                 RETURN(1);
2115
2116         rc = osd_otable_it_preload(env, it);
2117         if (rc >= 0)
2118                 goto again;
2119
2120         RETURN(rc);
2121 }
2122
2123 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2124                                         const struct dt_it *di)
2125 {
2126         return NULL;
2127 }
2128
2129 static int osd_otable_it_key_size(const struct lu_env *env,
2130                                   const struct dt_it *di)
2131 {
2132         return sizeof(__u64);
2133 }
2134
2135 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2136                              struct dt_rec *rec, __u32 attr)
2137 {
2138         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2139         struct osd_otable_cache *ooc = &it->ooi_cache;
2140
2141         *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2142
2143         /* Filter out Invald FID already. */
2144         LASSERTF(fid_is_sane((struct lu_fid *)rec),
2145                  "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2146                  PFID((struct lu_fid *)rec),
2147                  ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2148
2149         return 0;
2150 }
2151
2152 static __u64 osd_otable_it_store(const struct lu_env *env,
2153                                  const struct dt_it *di)
2154 {
2155         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2156         struct osd_otable_cache *ooc = &it->ooi_cache;
2157         __u64                    hash;
2158
2159         if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2160                 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2161         else
2162                 hash = ooc->ooc_pos_preload;
2163         return hash;
2164 }
2165
2166 /**
2167  * Set the OSD layer iteration start position as the specified hash.
2168  */
2169 static int osd_otable_it_load(const struct lu_env *env,
2170                               const struct dt_it *di, __u64 hash)
2171 {
2172         struct osd_otable_it    *it    = (struct osd_otable_it *)di;
2173         struct osd_device       *dev   = it->ooi_dev;
2174         struct osd_otable_cache *ooc   = &it->ooi_cache;
2175         struct osd_scrub        *scrub = &dev->od_scrub;
2176         int                      rc;
2177         ENTRY;
2178
2179         /* Forbid to set iteration position after iteration started. */
2180         if (it->ooi_user_ready)
2181                 RETURN(-EPERM);
2182
2183         if (hash > OSD_OTABLE_MAX_HASH)
2184                 hash = OSD_OTABLE_MAX_HASH;
2185
2186         ooc->ooc_pos_preload = hash;
2187         if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2188                 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2189
2190         it->ooi_user_ready = 1;
2191         if (!scrub->os_full_speed)
2192                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
2193
2194         /* Unplug OSD layer iteration by the first next() call. */
2195         rc = osd_otable_it_next(env, (struct dt_it *)it);
2196
2197         RETURN(rc);
2198 }
2199
2200 static int osd_otable_it_key_rec(const struct lu_env *env,
2201                                  const struct dt_it *di, void *key_rec)
2202 {
2203         return 0;
2204 }
2205
2206 const struct dt_index_operations osd_otable_ops = {
2207         .dio_it = {
2208                 .init     = osd_otable_it_init,
2209                 .fini     = osd_otable_it_fini,
2210                 .get      = osd_otable_it_get,
2211                 .put      = osd_otable_it_put,
2212                 .next     = osd_otable_it_next,
2213                 .key      = osd_otable_it_key,
2214                 .key_size = osd_otable_it_key_size,
2215                 .rec      = osd_otable_it_rec,
2216                 .store    = osd_otable_it_store,
2217                 .load     = osd_otable_it_load,
2218                 .key_rec  = osd_otable_it_key_rec,
2219         }
2220 };
2221
2222 /* high priority inconsistent items list APIs */
2223
2224 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
2225                    int insert)
2226 {
2227         struct osd_inconsistent_item *oii;
2228         struct osd_scrub             *scrub  = &dev->od_scrub;
2229         struct ptlrpc_thread         *thread = &scrub->os_thread;
2230         int                           wakeup = 0;
2231         ENTRY;
2232
2233         OBD_ALLOC_PTR(oii);
2234         if (unlikely(oii == NULL))
2235                 RETURN(-ENOMEM);
2236
2237         CFS_INIT_LIST_HEAD(&oii->oii_list);
2238         oii->oii_cache = *oic;
2239         oii->oii_insert = insert;
2240
2241         spin_lock(&scrub->os_lock);
2242         if (unlikely(!thread_is_running(thread))) {
2243                 spin_unlock(&scrub->os_lock);
2244                 OBD_FREE_PTR(oii);
2245                 RETURN(-EAGAIN);
2246         }
2247
2248         if (cfs_list_empty(&scrub->os_inconsistent_items))
2249                 wakeup = 1;
2250         cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
2251         spin_unlock(&scrub->os_lock);
2252
2253         if (wakeup != 0)
2254                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
2255
2256         RETURN(0);
2257 }
2258
2259 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2260                    struct osd_inode_id *id)
2261 {
2262         struct osd_scrub             *scrub = &dev->od_scrub;
2263         struct osd_inconsistent_item *oii;
2264         ENTRY;
2265
2266         spin_lock(&scrub->os_lock);
2267         cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2268                 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2269                         *id = oii->oii_cache.oic_lid;
2270                         spin_unlock(&scrub->os_lock);
2271                         RETURN(0);
2272                 }
2273         }
2274         spin_unlock(&scrub->os_lock);
2275
2276         RETURN(-ENOENT);
2277 }
2278
2279 /* OI scrub dump */
2280
2281 static const char *scrub_status_names[] = {
2282         "init",
2283         "scanning",
2284         "completed",
2285         "failed",
2286         "stopped",
2287         "paused",
2288         "crashed",
2289         NULL
2290 };
2291
2292 static const char *scrub_flags_names[] = {
2293         "recreated",
2294         "inconsistent",
2295         "auto",
2296         "upgrade",
2297         NULL
2298 };
2299
2300 static const char *scrub_param_names[] = {
2301         "failout",
2302         NULL
2303 };
2304
2305 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
2306                            const char *prefix)
2307 {
2308         int save = *len;
2309         int flag;
2310         int rc;
2311         int i;
2312
2313         rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
2314         if (rc <= 0)
2315                 return -ENOSPC;
2316
2317         *buf += rc;
2318         *len -= rc;
2319         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
2320                 if (flag & bits) {
2321                         bits &= ~flag;
2322                         rc = snprintf(*buf, *len, "%s%c", names[i],
2323                                       bits != 0 ? ',' : '\n');
2324                         if (rc <= 0)
2325                                 return -ENOSPC;
2326
2327                         *buf += rc;
2328                         *len -= rc;
2329                 }
2330         }
2331         return save - *len;
2332 }
2333
2334 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
2335 {
2336         int rc;
2337
2338         if (time != 0)
2339                 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
2340                               cfs_time_current_sec() - time);
2341         else
2342                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2343         if (rc <= 0)
2344                 return -ENOSPC;
2345
2346         *buf += rc;
2347         *len -= rc;
2348         return rc;
2349 }
2350
2351 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
2352 {
2353         int rc;
2354
2355         if (pos != 0)
2356                 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
2357         else
2358                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2359         if (rc <= 0)
2360                 return -ENOSPC;
2361
2362         *buf += rc;
2363         *len -= rc;
2364         return rc;
2365 }
2366
2367 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
2368 {
2369         struct osd_scrub  *scrub   = &dev->od_scrub;
2370         struct scrub_file *sf      = &scrub->os_file;
2371         __u64              checked;
2372         __u64              speed;
2373         int                save    = len;
2374         int                ret     = -ENOSPC;
2375         int                rc;
2376
2377         down_read(&scrub->os_rwsem);
2378         rc = snprintf(buf, len,
2379                       "name: OI_scrub\n"
2380                       "magic: 0x%x\n"
2381                       "oi_files: %d\n"
2382                       "status: %s\n",
2383                       sf->sf_magic, (int)sf->sf_oi_count,
2384                       scrub_status_names[sf->sf_status]);
2385         if (rc <= 0)
2386                 goto out;
2387
2388         buf += rc;
2389         len -= rc;
2390         rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
2391                              "flags");
2392         if (rc < 0)
2393                 goto out;
2394
2395         rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
2396                              "param");
2397         if (rc < 0)
2398                 goto out;
2399
2400         rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
2401                              "time_since_last_completed");
2402         if (rc < 0)
2403                 goto out;
2404
2405         rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
2406                              "time_since_latest_start");
2407         if (rc < 0)
2408                 goto out;
2409
2410         rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
2411                              "time_since_last_checkpoint");
2412         if (rc < 0)
2413                 goto out;
2414
2415         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
2416                             "latest_start_position");
2417         if (rc < 0)
2418                 goto out;
2419
2420         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
2421                             "last_checkpoint_position");
2422         if (rc < 0)
2423                 goto out;
2424
2425         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
2426                             "first_failure_position");
2427         if (rc < 0)
2428                 goto out;
2429
2430         checked = sf->sf_items_checked + scrub->os_new_checked;
2431         rc = snprintf(buf, len,
2432                       "checked: "LPU64"\n"
2433                       "updated: "LPU64"\n"
2434                       "failed: "LPU64"\n"
2435                       "prior_updated: "LPU64"\n"
2436                       "noscrub: "LPU64"\n"
2437                       "igif: "LPU64"\n"
2438                       "success_count: %u\n",
2439                       checked, sf->sf_items_updated, sf->sf_items_failed,
2440                       sf->sf_items_updated_prior, sf->sf_items_noscrub,
2441                       sf->sf_items_igif, sf->sf_success_count);
2442         if (rc <= 0)
2443                 goto out;
2444
2445         buf += rc;
2446         len -= rc;
2447         speed = checked;
2448         if (thread_is_running(&scrub->os_thread)) {
2449                 cfs_duration_t duration = cfs_time_current() -
2450                                           scrub->os_time_last_checkpoint;
2451                 __u64 new_checked = scrub->os_new_checked * CFS_HZ;
2452                 __u32 rtime = sf->sf_run_time +
2453                               cfs_duration_sec(duration + HALF_SEC);
2454
2455                 if (duration != 0)
2456                         do_div(new_checked, duration);
2457                 if (rtime != 0)
2458                         do_div(speed, rtime);
2459                 rc = snprintf(buf, len,
2460                               "run_time: %u seconds\n"
2461                               "average_speed: "LPU64" objects/sec\n"
2462                               "real-time_speed: "LPU64" objects/sec\n"
2463                               "current_position: %u\n",
2464                               rtime, speed, new_checked, scrub->os_pos_current);
2465         } else {
2466                 if (sf->sf_run_time != 0)
2467                         do_div(speed, sf->sf_run_time);
2468                 rc = snprintf(buf, len,
2469                               "run_time: %u seconds\n"
2470                               "average_speed: "LPU64" objects/sec\n"
2471                               "real-time_speed: N/A\n"
2472                               "current_position: N/A\n",
2473                               sf->sf_run_time, speed);
2474         }
2475         if (rc <= 0)
2476                 goto out;
2477
2478         buf += rc;
2479         len -= rc;
2480         ret = save - len;
2481
2482 out:
2483         up_read(&scrub->os_rwsem);
2484         return ret;
2485 }